1 /* error.c -- error handler for noninteractive utilities 2 Copyright (C) 1990-1992 Free Software Foundation, Inc. 3 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) 7 any later version. 8 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. */ 13 14 /* David MacKenzie */ 15 /* Brian Berliner added support for CVS */ 16 17 #include "cvs.h" 18 19 #include <stdio.h> 20 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; 26 27 #ifdef HAVE_VPRINTF 28 29 #ifdef __STDC__ 30 #include <stdarg.h> 31 #define VA_START(args, lastarg) va_start(args, lastarg) 32 #else /* ! __STDC__ */ 33 #include <varargs.h> 34 #define VA_START(args, lastarg) va_start(args) 35 #endif /* __STDC__ */ 36 37 #else /* ! HAVE_VPRINTF */ 38 39 #ifdef HAVE_DOPRNT 40 #define va_alist args 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 */ 46 47 #endif /* HAVE_VPRINTF */ 48 49 #if STDC_HEADERS 50 #include <stdlib.h> 51 #include <string.h> 52 #else /* ! STDC_HEADERS */ 53 #ifdef __STDC__ 54 void exit(int status); 55 #else /* ! __STDC__ */ 56 void exit (); 57 #endif /* __STDC__ */ 58 #endif /* STDC_HEADERS */ 59 60 #ifndef strerror 61 extern char *strerror (); 62 #endif 63 64 void 65 error_exit PROTO ((void)) 66 { 67 rcs_cleanup (); 68 Lock_Cleanup (); 69 #ifdef SERVER_SUPPORT 70 if (server_active) 71 server_cleanup (0); 72 #endif 73 #ifdef SYSTEM_CLEANUP 74 /* Hook for OS-specific behavior, for example socket subsystems on 75 NT and OS2 or dealing with windows and arguments on Mac. */ 76 SYSTEM_CLEANUP (); 77 #endif 78 exit (EXIT_FAILURE); 79 } 80 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. 85 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. 89 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. 95 96 error() does not molest errno; some code (e.g. Entries_Open) depends 97 on being able to say something like: 98 error (0, 0, "foo"); 99 error (0, errno, "bar"); 100 101 */ 102 103 /* VARARGS */ 104 void 105 #if defined (__STDC__) 106 error (int status, int errnum, const char *message, ...) 107 #else 108 error (status, errnum, message, va_alist) 109 int status; 110 int errnum; 111 const char *message; 112 va_dcl 113 #endif 114 { 115 int save_errno = errno; 116 117 if (message[0] != '\0') 118 { 119 va_list args; 120 const char *p; 121 char *q; 122 char *str; 123 int num; 124 unsigned int unum; 125 int ch; 126 char buf[100]; 127 128 cvs_outerr (program_name, 0); 129 if (command_name && *command_name) 130 { 131 cvs_outerr (" ", 1); 132 if (status != 0) 133 cvs_outerr ("[", 1); 134 cvs_outerr (command_name, 0); 135 if (status != 0) 136 cvs_outerr (" aborted]", 0); 137 } 138 cvs_outerr (": ", 2); 139 140 VA_START (args, message); 141 p = message; 142 while ((q = strchr (p, '%')) != NULL) 143 { 144 static const char msg[] = 145 "\ninternal error: bad % in error()\n"; 146 if (q - p > 0) 147 cvs_outerr (p, q - p); 148 149 switch (q[1]) 150 { 151 case 's': 152 str = va_arg (args, char *); 153 cvs_outerr (str, strlen (str)); 154 break; 155 case 'd': 156 num = va_arg (args, int); 157 sprintf (buf, "%d", num); 158 cvs_outerr (buf, strlen (buf)); 159 break; 160 case 'x': 161 unum = va_arg (args, unsigned int); 162 sprintf (buf, "%x", unum); 163 cvs_outerr (buf, strlen (buf)); 164 break; 165 case 'c': 166 ch = va_arg (args, int); 167 buf[0] = ch; 168 cvs_outerr (buf, 1); 169 break; 170 case '%': 171 cvs_outerr ("%", 1); 172 break; 173 default: 174 cvs_outerr (msg, sizeof (msg) - 1); 175 /* Don't just keep going, because q + 1 might point to the 176 terminating '\0'. */ 177 goto out; 178 } 179 p = q + 2; 180 } 181 cvs_outerr (p, strlen (p)); 182 out: 183 va_end (args); 184 185 if (errnum != 0) 186 { 187 cvs_outerr (": ", 2); 188 cvs_outerr (strerror (errnum), 0); 189 } 190 cvs_outerr ("\n", 1); 191 } 192 193 if (status) 194 error_exit (); 195 errno = save_errno; 196 } 197 198 /* Print the program name and error message MESSAGE, which is a printf-style 199 format string with optional args to the file specified by FP. 200 If ERRNUM is nonzero, print its corresponding system error message. 201 Exit with status EXIT_FAILURE if STATUS is nonzero. */ 202 /* VARARGS */ 203 void 204 #if defined (HAVE_VPRINTF) && defined (__STDC__) 205 fperrmsg (FILE *fp, int status, int errnum, char *message, ...) 206 #else 207 fperrmsg (fp, status, errnum, message, va_alist) 208 FILE *fp; 209 int status; 210 int errnum; 211 char *message; 212 va_dcl 213 #endif 214 { 215 #ifdef HAVE_VPRINTF 216 va_list args; 217 #endif 218 219 fprintf (fp, "%s: ", program_name); 220 #ifdef HAVE_VPRINTF 221 VA_START (args, message); 222 vfprintf (fp, message, args); 223 va_end (args); 224 #else 225 #ifdef HAVE_DOPRNT 226 _doprnt (message, &args, fp); 227 #else 228 fprintf (fp, message, a1, a2, a3, a4, a5, a6, a7, a8); 229 #endif 230 #endif 231 if (errnum) 232 fprintf (fp, ": %s", strerror (errnum)); 233 putc ('\n', fp); 234 fflush (fp); 235 if (status) 236 error_exit (); 237 } 238