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 Lock_Cleanup(); 68 #ifdef SERVER_SUPPORT 69 if (server_active) 70 server_cleanup (0); 71 #endif 72 #ifdef SYSTEM_CLEANUP 73 /* Hook for OS-specific behavior, for example socket subsystems on 74 NT and OS2 or dealing with windows and arguments on Mac. */ 75 SYSTEM_CLEANUP (); 76 #endif 77 exit (EXIT_FAILURE); 78 } 79 80 /* Print the program name and error message MESSAGE, which is a printf-style 81 format string with optional args. This is a very limited printf subset: 82 %s, %d, %c, %x and %% only (without anything between the % and the s, 83 d, &c). Callers who want something fancier can use sprintf. 84 85 If ERRNUM is nonzero, print its corresponding system error message. 86 Exit with status EXIT_FAILURE if STATUS is nonzero. If MESSAGE is "", 87 no need to print a message. 88 89 I think this is largely cleaned up to the point where it does the right 90 thing for the server, whether the normal server_active (child process) 91 case or the error_use_protocol (parent process) case. The one exception 92 is that STATUS nonzero for error_use_protocol probably doesn't work yet; 93 in that case still need to use the pending_error machinery in server.c. 94 95 error() does not molest errno; some code (e.g. Entries_Open) depends 96 on being able to say something like: 97 error (0, 0, "foo"); 98 error (0, errno, "bar"); 99 100 */ 101 102 /* VARARGS */ 103 void 104 #if defined (__STDC__) 105 error (int status, int errnum, const char *message, ...) 106 #else 107 error (status, errnum, message, va_alist) 108 int status; 109 int errnum; 110 const char *message; 111 va_dcl 112 #endif 113 { 114 int save_errno = errno; 115 116 if (message[0] != '\0') 117 { 118 va_list args; 119 const char *p; 120 char *q; 121 char *str; 122 int num; 123 unsigned int unum; 124 int ch; 125 unsigned char buf[100]; 126 127 cvs_outerr (program_name, 0); 128 if (command_name && *command_name) 129 { 130 cvs_outerr (" ", 1); 131 if (status != 0) 132 cvs_outerr ("[", 1); 133 cvs_outerr (command_name, 0); 134 if (status != 0) 135 cvs_outerr (" aborted]", 0); 136 } 137 cvs_outerr (": ", 2); 138 139 VA_START (args, message); 140 p = message; 141 while ((q = strchr (p, '%')) != NULL) 142 { 143 static const char msg[] = 144 "\ninternal error: bad % in error()\n"; 145 if (q - p > 0) 146 cvs_outerr (p, q - p); 147 148 switch (q[1]) 149 { 150 case 's': 151 str = va_arg (args, char *); 152 cvs_outerr (str, strlen (str)); 153 break; 154 case 'd': 155 num = va_arg (args, int); 156 sprintf (buf, "%d", num); 157 cvs_outerr (buf, strlen (buf)); 158 break; 159 case 'x': 160 unum = va_arg (args, unsigned int); 161 sprintf (buf, "%x", unum); 162 cvs_outerr (buf, strlen (buf)); 163 break; 164 case 'c': 165 ch = va_arg (args, int); 166 buf[0] = ch; 167 cvs_outerr (buf, 1); 168 break; 169 case '%': 170 cvs_outerr ("%", 1); 171 break; 172 default: 173 cvs_outerr (msg, sizeof (msg) - 1); 174 /* Don't just keep going, because q + 1 might point to the 175 terminating '\0'. */ 176 goto out; 177 } 178 p = q + 2; 179 } 180 cvs_outerr (p, strlen (p)); 181 out: 182 va_end (args); 183 184 if (errnum != 0) 185 { 186 cvs_outerr (": ", 2); 187 cvs_outerr (strerror (errnum), 0); 188 } 189 cvs_outerr ("\n", 1); 190 } 191 192 if (status) 193 error_exit (); 194 errno = save_errno; 195 } 196 197 /* Print the program name and error message MESSAGE, which is a printf-style 198 format string with optional args to the file specified by FP. 199 If ERRNUM is nonzero, print its corresponding system error message. 200 Exit with status EXIT_FAILURE if STATUS is nonzero. */ 201 /* VARARGS */ 202 void 203 #if defined (HAVE_VPRINTF) && defined (__STDC__) 204 fperror (FILE *fp, int status, int errnum, char *message, ...) 205 #else 206 fperror (fp, status, errnum, message, va_alist) 207 FILE *fp; 208 int status; 209 int errnum; 210 char *message; 211 va_dcl 212 #endif 213 { 214 #ifdef HAVE_VPRINTF 215 va_list args; 216 #endif 217 218 fprintf (fp, "%s: ", program_name); 219 #ifdef HAVE_VPRINTF 220 VA_START (args, message); 221 vfprintf (fp, message, args); 222 va_end (args); 223 #else 224 #ifdef HAVE_DOPRNT 225 _doprnt (message, &args, fp); 226 #else 227 fprintf (fp, message, a1, a2, a3, a4, a5, a6, a7, a8); 228 #endif 229 #endif 230 if (errnum) 231 fprintf (fp, ": %s", strerror (errnum)); 232 putc ('\n', fp); 233 fflush (fp); 234 if (status) 235 error_exit (); 236 } 237