113571821Stholo /* error.c -- error handler for noninteractive utilities
213571821Stholo Copyright (C) 1990-1992 Free Software Foundation, Inc.
313571821Stholo
413571821Stholo This program is free software; you can redistribute it and/or modify
513571821Stholo it under the terms of the GNU General Public License as published by
613571821Stholo the Free Software Foundation; either version 2, or (at your option)
713571821Stholo any later version.
813571821Stholo
913571821Stholo This program is distributed in the hope that it will be useful,
1013571821Stholo but WITHOUT ANY WARRANTY; without even the implied warranty of
1113571821Stholo MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12461cc63eStholo GNU General Public License for more details. */
1313571821Stholo
1413571821Stholo /* David MacKenzie */
1513571821Stholo /* Brian Berliner added support for CVS */
1613571821Stholo
1713571821Stholo #include "cvs.h"
1813571821Stholo
1913571821Stholo #include <stdio.h>
2013571821Stholo
2113571821Stholo /* If non-zero, error will use the CVS protocol to stdout to report error
2213571821Stholo messages. This will only be set in the CVS server parent process;
2313571821Stholo most other code is run via do_cvs_command, which forks off a child
2413571821Stholo process and packages up its stderr in the protocol. */
2513571821Stholo int error_use_protocol;
2613571821Stholo
2713571821Stholo #ifdef HAVE_VPRINTF
2813571821Stholo
29b6c02222Stholo #ifdef __STDC__
3013571821Stholo #include <stdarg.h>
3113571821Stholo #define VA_START(args, lastarg) va_start(args, lastarg)
3213571821Stholo #else /* ! __STDC__ */
3313571821Stholo #include <varargs.h>
3413571821Stholo #define VA_START(args, lastarg) va_start(args)
3513571821Stholo #endif /* __STDC__ */
3613571821Stholo
3713571821Stholo #else /* ! HAVE_VPRINTF */
3813571821Stholo
3913571821Stholo #ifdef HAVE_DOPRNT
4013571821Stholo #define va_alist args
4113571821Stholo #define va_dcl int args;
4213571821Stholo #else /* ! HAVE_DOPRNT */
4313571821Stholo #define va_alist a1, a2, a3, a4, a5, a6, a7, a8
4413571821Stholo #define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
4513571821Stholo #endif /* HAVE_DOPRNT */
4613571821Stholo
4713571821Stholo #endif /* HAVE_VPRINTF */
4813571821Stholo
4913571821Stholo #if STDC_HEADERS
5013571821Stholo #include <stdlib.h>
5113571821Stholo #include <string.h>
5213571821Stholo #else /* ! STDC_HEADERS */
53b6c02222Stholo #ifdef __STDC__
5413571821Stholo void exit(int status);
5513571821Stholo #else /* ! __STDC__ */
5613571821Stholo void exit ();
5713571821Stholo #endif /* __STDC__ */
5813571821Stholo #endif /* STDC_HEADERS */
5913571821Stholo
6050bf276cStholo #ifndef strerror
6113571821Stholo extern char *strerror ();
6250bf276cStholo #endif
6313571821Stholo
64461cc63eStholo void
error_exit(void)65461cc63eStholo error_exit PROTO ((void))
6613571821Stholo {
67e77048c1Stholo rcs_cleanup ();
68461cc63eStholo Lock_Cleanup ();
69461cc63eStholo #ifdef SERVER_SUPPORT
70461cc63eStholo if (server_active)
71461cc63eStholo server_cleanup (0);
72461cc63eStholo #endif
73461cc63eStholo #ifdef SYSTEM_CLEANUP
74461cc63eStholo /* Hook for OS-specific behavior, for example socket subsystems on
75461cc63eStholo NT and OS2 or dealing with windows and arguments on Mac. */
76461cc63eStholo SYSTEM_CLEANUP ();
77461cc63eStholo #endif
78461cc63eStholo exit (EXIT_FAILURE);
7913571821Stholo }
8013571821Stholo
8113571821Stholo /* Print the program name and error message MESSAGE, which is a printf-style
82c71bc7e2Stholo format string with optional args. This is a very limited printf subset:
83c71bc7e2Stholo %s, %d, %c, %x and %% only (without anything between the % and the s,
84c71bc7e2Stholo d, &c). Callers who want something fancier can use sprintf.
85c71bc7e2Stholo
8613571821Stholo If ERRNUM is nonzero, print its corresponding system error message.
87461cc63eStholo Exit with status EXIT_FAILURE if STATUS is nonzero. If MESSAGE is "",
882286d8edStholo no need to print a message.
892286d8edStholo
902286d8edStholo I think this is largely cleaned up to the point where it does the right
912286d8edStholo thing for the server, whether the normal server_active (child process)
922286d8edStholo case or the error_use_protocol (parent process) case. The one exception
932286d8edStholo is that STATUS nonzero for error_use_protocol probably doesn't work yet;
94b6f6614eStholo in that case still need to use the pending_error machinery in server.c.
95b6f6614eStholo
96b6f6614eStholo error() does not molest errno; some code (e.g. Entries_Open) depends
97b6f6614eStholo on being able to say something like:
98b6f6614eStholo error (0, 0, "foo");
99b6f6614eStholo error (0, errno, "bar");
100b6f6614eStholo
101b6f6614eStholo */
102b6f6614eStholo
10313571821Stholo /* VARARGS */
10413571821Stholo void
105c71bc7e2Stholo #if defined (__STDC__)
error(int status,int errnum,const char * message,...)10613571821Stholo error (int status, int errnum, const char *message, ...)
10713571821Stholo #else
10813571821Stholo error (status, errnum, message, va_alist)
10913571821Stholo int status;
11013571821Stholo int errnum;
11113571821Stholo const char *message;
11213571821Stholo va_dcl
11313571821Stholo #endif
11413571821Stholo {
115b6f6614eStholo int save_errno = errno;
116b6f6614eStholo
117461cc63eStholo if (message[0] != '\0')
11850bf276cStholo {
11913571821Stholo va_list args;
120c71bc7e2Stholo const char *p;
121c71bc7e2Stholo char *q;
122c71bc7e2Stholo char *str;
123c71bc7e2Stholo int num;
124*43c1707eStholo long lnum;
125c71bc7e2Stholo unsigned int unum;
126*43c1707eStholo unsigned long ulnum;
127c71bc7e2Stholo int ch;
128e77048c1Stholo char buf[100];
129c26070a5Stholo
130c71bc7e2Stholo cvs_outerr (program_name, 0);
13113571821Stholo if (command_name && *command_name)
132c26070a5Stholo {
133c71bc7e2Stholo cvs_outerr (" ", 1);
134c71bc7e2Stholo if (status != 0)
135c71bc7e2Stholo cvs_outerr ("[", 1);
136c71bc7e2Stholo cvs_outerr (command_name, 0);
137c71bc7e2Stholo if (status != 0)
138c71bc7e2Stholo cvs_outerr (" aborted]", 0);
139c26070a5Stholo }
140c71bc7e2Stholo cvs_outerr (": ", 2);
141c26070a5Stholo
14213571821Stholo VA_START (args, message);
143c71bc7e2Stholo p = message;
144c71bc7e2Stholo while ((q = strchr (p, '%')) != NULL)
145c71bc7e2Stholo {
146c71bc7e2Stholo static const char msg[] =
147c71bc7e2Stholo "\ninternal error: bad % in error()\n";
148c71bc7e2Stholo if (q - p > 0)
149c71bc7e2Stholo cvs_outerr (p, q - p);
150c26070a5Stholo
151c71bc7e2Stholo switch (q[1])
152c71bc7e2Stholo {
153c71bc7e2Stholo case 's':
154c71bc7e2Stholo str = va_arg (args, char *);
155c71bc7e2Stholo cvs_outerr (str, strlen (str));
156c71bc7e2Stholo break;
157c71bc7e2Stholo case 'd':
158c71bc7e2Stholo num = va_arg (args, int);
159c71bc7e2Stholo sprintf (buf, "%d", num);
160c71bc7e2Stholo cvs_outerr (buf, strlen (buf));
161c71bc7e2Stholo break;
162*43c1707eStholo case 'l':
163*43c1707eStholo if (q[2] == 'd')
164*43c1707eStholo {
165*43c1707eStholo lnum = va_arg (args, long);
166*43c1707eStholo sprintf (buf, "%ld", lnum);
167*43c1707eStholo }
168*43c1707eStholo else if (q[2] == 'u')
169*43c1707eStholo {
170*43c1707eStholo ulnum = va_arg (args, unsigned long);
171*43c1707eStholo sprintf (buf, "%lu", ulnum);
172*43c1707eStholo }
173*43c1707eStholo else goto bad;
174*43c1707eStholo cvs_outerr (buf, strlen (buf));
175*43c1707eStholo q++;
176*43c1707eStholo break;
177c71bc7e2Stholo case 'x':
178c71bc7e2Stholo unum = va_arg (args, unsigned int);
179c71bc7e2Stholo sprintf (buf, "%x", unum);
180c71bc7e2Stholo cvs_outerr (buf, strlen (buf));
181c71bc7e2Stholo break;
182c71bc7e2Stholo case 'c':
183c71bc7e2Stholo ch = va_arg (args, int);
184c71bc7e2Stholo buf[0] = ch;
185c71bc7e2Stholo cvs_outerr (buf, 1);
186c71bc7e2Stholo break;
187c71bc7e2Stholo case '%':
188c71bc7e2Stholo cvs_outerr ("%", 1);
189c71bc7e2Stholo break;
190c71bc7e2Stholo default:
191*43c1707eStholo bad:
192c71bc7e2Stholo cvs_outerr (msg, sizeof (msg) - 1);
193c71bc7e2Stholo /* Don't just keep going, because q + 1 might point to the
194c71bc7e2Stholo terminating '\0'. */
195c71bc7e2Stholo goto out;
19650bf276cStholo }
197c71bc7e2Stholo p = q + 2;
198c71bc7e2Stholo }
199c71bc7e2Stholo cvs_outerr (p, strlen (p));
200c71bc7e2Stholo out:
201c71bc7e2Stholo va_end (args);
20250bf276cStholo
203c71bc7e2Stholo if (errnum != 0)
204c71bc7e2Stholo {
205c71bc7e2Stholo cvs_outerr (": ", 2);
206c71bc7e2Stholo cvs_outerr (strerror (errnum), 0);
207c71bc7e2Stholo }
208c71bc7e2Stholo cvs_outerr ("\n", 1);
209c71bc7e2Stholo }
210c26070a5Stholo
21113571821Stholo if (status)
212461cc63eStholo error_exit ();
213b6f6614eStholo errno = save_errno;
21413571821Stholo }
21513571821Stholo
21613571821Stholo /* Print the program name and error message MESSAGE, which is a printf-style
21713571821Stholo format string with optional args to the file specified by FP.
21813571821Stholo If ERRNUM is nonzero, print its corresponding system error message.
219c2c61682Stholo Exit with status EXIT_FAILURE if STATUS is nonzero. */
22013571821Stholo /* VARARGS */
22113571821Stholo void
222b6c02222Stholo #if defined (HAVE_VPRINTF) && defined (__STDC__)
fperrmsg(FILE * fp,int status,int errnum,char * message,...)223e77048c1Stholo fperrmsg (FILE *fp, int status, int errnum, char *message, ...)
22413571821Stholo #else
225e77048c1Stholo fperrmsg (fp, status, errnum, message, va_alist)
22613571821Stholo FILE *fp;
22713571821Stholo int status;
22813571821Stholo int errnum;
22913571821Stholo char *message;
23013571821Stholo va_dcl
23113571821Stholo #endif
23213571821Stholo {
23313571821Stholo #ifdef HAVE_VPRINTF
23413571821Stholo va_list args;
23513571821Stholo #endif
23613571821Stholo
23713571821Stholo fprintf (fp, "%s: ", program_name);
23813571821Stholo #ifdef HAVE_VPRINTF
23913571821Stholo VA_START (args, message);
24013571821Stholo vfprintf (fp, message, args);
24113571821Stholo va_end (args);
24213571821Stholo #else
24313571821Stholo #ifdef HAVE_DOPRNT
24413571821Stholo _doprnt (message, &args, fp);
24513571821Stholo #else
24613571821Stholo fprintf (fp, message, a1, a2, a3, a4, a5, a6, a7, a8);
24713571821Stholo #endif
24813571821Stholo #endif
24913571821Stholo if (errnum)
25013571821Stholo fprintf (fp, ": %s", strerror (errnum));
25113571821Stholo putc ('\n', fp);
25213571821Stholo fflush (fp);
25313571821Stholo if (status)
254461cc63eStholo error_exit ();
25513571821Stholo }
256