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 */
19 /* If non-zero, error will use the CVS protocol to stdout to report error
20 messages. This will only be set in the CVS server parent process;
21 most other code is run via do_cvs_command, which forks off a child
22 process and packages up its stderr in the protocol. */
23 int error_use_protocol;
26 extern char *strerror (int);
31 /* Print the program name and error message MESSAGE, which is a printf-style
32 format string with optional args. This is a very limited printf subset:
33 %s, %d, %c, %x and %% only (without anything between the % and the s,
34 d, &c). Callers who want something fancier can use sprintf.
36 If ERRNUM is nonzero, print its corresponding system error message.
37 Exit with status EXIT_FAILURE if STATUS is nonzero. If MESSAGE is "",
38 no need to print a message.
40 I think this is largely cleaned up to the point where it does the right
41 thing for the server, whether the normal server_active (child process)
42 case or the error_use_protocol (parent process) case. The one exception
43 is that STATUS nonzero for error_use_protocol probably doesn't work yet;
44 in that case still need to use the pending_error machinery in server.c.
46 error() does not molest errno; some code (e.g. Entries_Open) depends
47 on being able to say something like:
49 error (0, errno, "bar");
55 error (int status, int errnum, const char *message, ...)
57 int save_errno = errno;
59 if (message[0] != '\0')
72 cvs_outerr (program_name, 0);
73 if (cvs_cmd_name && *cvs_cmd_name)
78 cvs_outerr (cvs_cmd_name, 0);
80 cvs_outerr (" aborted]", 0);
84 va_start( args, message );
86 while ((q = strchr (p, '%')) != NULL)
88 static const char msg[] =
89 "\ninternal error: bad % in error()\n";
91 cvs_outerr (p, q - p);
96 str = va_arg (args, char *);
97 cvs_outerr (str, strlen (str));
100 num = va_arg (args, int);
101 sprintf (buf, "%d", num);
102 cvs_outerr (buf, strlen (buf));
107 lnum = va_arg (args, long);
108 sprintf (buf, "%ld", lnum);
110 else if (q[2] == 'u')
112 ulnum = va_arg (args, unsigned long);
113 sprintf (buf, "%lu", ulnum);
116 cvs_outerr (buf, strlen (buf));
120 unum = va_arg (args, unsigned int);
121 sprintf (buf, "%x", unum);
122 cvs_outerr (buf, strlen (buf));
125 ch = va_arg (args, int);
134 cvs_outerr (msg, sizeof (msg) - 1);
135 /* Don't just keep going, because q + 1 might point to the
141 cvs_outerr (p, strlen (p));
147 cvs_outerr (": ", 2);
148 cvs_outerr (strerror (errnum), 0);
150 cvs_outerr ("\n", 1);
158 /* Print the program name and error message MESSAGE, which is a printf-style
159 format string with optional args to the file specified by FP.
160 If ERRNUM is nonzero, print its corresponding system error message.
161 Exit with status EXIT_FAILURE if STATUS is nonzero. */
164 fperrmsg (FILE *fp, int status, int errnum, char *message, ...)
168 fprintf (fp, "%s: ", program_name);
169 va_start( args, message );
170 vfprintf (fp, message, args);
173 fprintf (fp, ": %s", strerror (errnum));