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.  */
25int 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__
54void exit(int status);
55#else /* ! __STDC__ */
56void exit ();
57#endif /* __STDC__ */
58#endif /* STDC_HEADERS */
59
60#ifndef strerror
61extern char *strerror ();
62#endif
63
64void
65error_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 */
104void
105#if defined (__STDC__)
106error (int status, int errnum, const char *message, ...)
107#else
108error (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	long lnum;
125	unsigned int unum;
126	unsigned long ulnum;
127	int ch;
128	char buf[100];
129
130	cvs_outerr (program_name, 0);
131	if (cvs_cmd_name && *cvs_cmd_name)
132	{
133	    cvs_outerr (" ", 1);
134	    if (status != 0)
135		cvs_outerr ("[", 1);
136	    cvs_outerr (cvs_cmd_name, 0);
137	    if (status != 0)
138		cvs_outerr (" aborted]", 0);
139	}
140	cvs_outerr (": ", 2);
141
142	VA_START (args, message);
143	p = message;
144	while ((q = strchr (p, '%')) != NULL)
145	{
146	    static const char msg[] =
147		"\ninternal error: bad % in error()\n";
148	    if (q - p > 0)
149		cvs_outerr (p, q - p);
150
151	    switch (q[1])
152	    {
153	    case 's':
154		str = va_arg (args, char *);
155		cvs_outerr (str, strlen (str));
156		break;
157	    case 'd':
158		num = va_arg (args, int);
159		sprintf (buf, "%d", num);
160		cvs_outerr (buf, strlen (buf));
161		break;
162	    case 'l':
163		if (q[2] == 'd')
164		{
165		    lnum = va_arg (args, long);
166		    sprintf (buf, "%ld", lnum);
167		}
168		else if (q[2] == 'u')
169		{
170		    ulnum = va_arg (args, unsigned long);
171		    sprintf (buf, "%lu", ulnum);
172		}
173		else goto bad;
174		cvs_outerr (buf, strlen (buf));
175		q++;
176		break;
177	    case 'x':
178		unum = va_arg (args, unsigned int);
179		sprintf (buf, "%x", unum);
180		cvs_outerr (buf, strlen (buf));
181		break;
182	    case 'c':
183		ch = va_arg (args, int);
184		buf[0] = ch;
185		cvs_outerr (buf, 1);
186		break;
187	    case '%':
188		cvs_outerr ("%", 1);
189		break;
190	    default:
191	    bad:
192		cvs_outerr (msg, sizeof (msg) - 1);
193		/* Don't just keep going, because q + 1 might point to the
194		   terminating '\0'.  */
195		goto out;
196	    }
197	    p = q + 2;
198	}
199	cvs_outerr (p, strlen (p));
200    out:
201	va_end (args);
202
203	if (errnum != 0)
204	{
205	    cvs_outerr (": ", 2);
206	    cvs_outerr (strerror (errnum), 0);
207	}
208	cvs_outerr ("\n", 1);
209    }
210
211    if (status)
212	error_exit ();
213    errno = save_errno;
214}
215
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.  */
220/* VARARGS */
221void
222#if defined (HAVE_VPRINTF) && defined (__STDC__)
223fperrmsg (FILE *fp, int status, int errnum, char *message, ...)
224#else
225fperrmsg (fp, status, errnum, message, va_alist)
226    FILE *fp;
227    int status;
228    int errnum;
229    char *message;
230    va_dcl
231#endif
232{
233#ifdef HAVE_VPRINTF
234    va_list args;
235#endif
236
237    fprintf (fp, "%s: ", program_name);
238#ifdef HAVE_VPRINTF
239    VA_START (args, message);
240    vfprintf (fp, message, args);
241    va_end (args);
242#else
243#ifdef HAVE_DOPRNT
244    _doprnt (message, &args, fp);
245#else
246    fprintf (fp, message, a1, a2, a3, a4, a5, a6, a7, a8);
247#endif
248#endif
249    if (errnum)
250	fprintf (fp, ": %s", strerror (errnum));
251    putc ('\n', fp);
252    fflush (fp);
253    if (status)
254	error_exit ();
255}
256