1 /* error.c -- error handler for noninteractive utilities
2 Copyright (C) 1990-1992 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details. */
15 /* Brian Berliner added support for CVS */
21 /* If non-zero, error will use the CVS protocol to stdout to report error
22 messages. This will only be set in the CVS server parent process;
23 most other code is run via do_cvs_command, which forks off a child
24 process and packages up its stderr in the protocol. */
25 int error_use_protocol;
31 #define VA_START(args, lastarg) va_start(args, lastarg)
32 #else /* ! __STDC__ */
34 #define VA_START(args, lastarg) va_start(args)
37 #else /* ! HAVE_VPRINTF */
41 #define va_dcl int args;
42 #else /* ! HAVE_DOPRNT */
43 #define va_alist a1, a2, a3, a4, a5, a6, a7, a8
44 #define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
45 #endif /* HAVE_DOPRNT */
47 #endif /* HAVE_VPRINTF */
52 #else /* ! STDC_HEADERS */
54 void exit(int status);
55 #else /* ! __STDC__ */
58 #endif /* STDC_HEADERS */
61 extern char *strerror ();
65 error_exit PROTO ((void))
74 /* Hook for OS-specific behavior, for example socket subsystems on
75 NT and OS2 or dealing with windows and arguments on Mac. */
81 /* Print the program name and error message MESSAGE, which is a printf-style
82 format string with optional args. This is a very limited printf subset:
83 %s, %d, %c, %x and %% only (without anything between the % and the s,
84 d, &c). Callers who want something fancier can use sprintf.
86 If ERRNUM is nonzero, print its corresponding system error message.
87 Exit with status EXIT_FAILURE if STATUS is nonzero. If MESSAGE is "",
88 no need to print a message.
90 I think this is largely cleaned up to the point where it does the right
91 thing for the server, whether the normal server_active (child process)
92 case or the error_use_protocol (parent process) case. The one exception
93 is that STATUS nonzero for error_use_protocol probably doesn't work yet;
94 in that case still need to use the pending_error machinery in server.c.
96 error() does not molest errno; some code (e.g. Entries_Open) depends
97 on being able to say something like:
99 error (0, errno, "bar");
105 #if defined (__STDC__)
106 error (int status, int errnum, const char *message, ...)
108 error (status, errnum, message, va_alist)
115 int save_errno = errno;
117 if (message[0] != '\0')
130 cvs_outerr (program_name, 0);
131 if (cvs_cmd_name && *cvs_cmd_name)
136 cvs_outerr (cvs_cmd_name, 0);
138 cvs_outerr (" aborted]", 0);
140 cvs_outerr (": ", 2);
142 VA_START (args, message);
144 while ((q = strchr (p, '%')) != NULL)
146 static const char msg[] =
147 "\ninternal error: bad % in error()\n";
149 cvs_outerr (p, q - p);
154 str = va_arg (args, char *);
155 cvs_outerr (str, strlen (str));
158 num = va_arg (args, int);
159 sprintf (buf, "%d", num);
160 cvs_outerr (buf, strlen (buf));
165 lnum = va_arg (args, long);
166 sprintf (buf, "%ld", lnum);
168 else if (q[2] == 'u')
170 ulnum = va_arg (args, unsigned long);
171 sprintf (buf, "%lu", ulnum);
174 cvs_outerr (buf, strlen (buf));
178 unum = va_arg (args, unsigned int);
179 sprintf (buf, "%x", unum);
180 cvs_outerr (buf, strlen (buf));
183 ch = va_arg (args, int);
192 cvs_outerr (msg, sizeof (msg) - 1);
193 /* Don't just keep going, because q + 1 might point to the
199 cvs_outerr (p, strlen (p));
205 cvs_outerr (": ", 2);
206 cvs_outerr (strerror (errnum), 0);
208 cvs_outerr ("\n", 1);
216 /* Print the program name and error message MESSAGE, which is a printf-style
217 format string with optional args to the file specified by FP.
218 If ERRNUM is nonzero, print its corresponding system error message.
219 Exit with status EXIT_FAILURE if STATUS is nonzero. */
222 #if defined (HAVE_VPRINTF) && defined (__STDC__)
223 fperrmsg (FILE *fp, int status, int errnum, char *message, ...)
225 fperrmsg (fp, status, errnum, message, va_alist)
237 fprintf (fp, "%s: ", program_name);
239 VA_START (args, message);
240 vfprintf (fp, message, args);
244 _doprnt (message, &args, fp);
246 fprintf (fp, message, a1, a2, a3, a4, a5, a6, a7, a8);
250 fprintf (fp, ": %s", strerror (errnum));