b85b76d3230b800ac137d9f306c027b3065a2961
[gnulib.git] / lib / error.c
1 /* Error handler for noninteractive utilities
2    Copyright (C) 1990-1998, 2000, 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.  Its master source is NOT part of
4    the C library, however.  The master source lives in /gd/gnu/lib.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #if HAVE_LIBINTL_H
29 # include <libintl.h>
30 #endif
31 #ifdef _LIBC
32 # include <wchar.h>
33 # define mbsrtowcs __mbsrtowcs
34 #endif
35
36 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
37 # if __STDC__
38 #  include <stdarg.h>
39 #  define VA_START(args, lastarg) va_start(args, lastarg)
40 # else
41 #  include <varargs.h>
42 #  define VA_START(args, lastarg) va_start(args)
43 # endif
44 #else
45 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
46 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
47 #endif
48
49 #if STDC_HEADERS || _LIBC
50 # include <stdlib.h>
51 # include <string.h>
52 #else
53 void exit ();
54 #endif
55
56 #include "error.h"
57
58 #ifndef HAVE_DECL_STRERROR_R
59 "this configure-time declaration test was not run"
60 #endif
61 #if !HAVE_DECL_STRERROR_R
62 char *strerror_r ();
63 #endif
64
65 #ifndef _
66 # define _(String) String
67 #endif
68
69 /* If NULL, error will flush stdout, then print on stderr the program
70    name, a colon and a space.  Otherwise, error will call this
71    function without parameters instead.  */
72 void (*error_print_progname) (
73 #if __STDC__ - 0
74                               void
75 #endif
76                               );
77
78 /* This variable is incremented each time `error' is called.  */
79 unsigned int error_message_count;
80
81 #ifdef _LIBC
82 /* In the GNU C library, there is a predefined variable for this.  */
83
84 # define program_name program_invocation_name
85 # include <errno.h>
86
87 /* In GNU libc we want do not want to use the common name `error' directly.
88    Instead make it a weak alias.  */
89 extern void __error (int status, int errnum, const char *message, ...)
90      __attribute__ ((__format__ (__printf__, 3, 4)));
91 extern void __error_at_line (int status, int errnum, const char *file_name,
92                              unsigned int line_number, const char *message,
93                              ...)
94      __attribute__ ((__format__ (__printf__, 5, 6)));;
95 # define error __error
96 # define error_at_line __error_at_line
97
98 # ifdef USE_IN_LIBIO
99 #  include <libio/iolibio.h>
100 #  define fflush(s) _IO_fflush (s)
101 # endif
102
103 #else /* not _LIBC */
104
105 /* The calling program should define program_name and set it to the
106    name of the executing program.  */
107 extern char *program_name;
108
109 # ifdef HAVE_STRERROR_R
110 #  define __strerror_r strerror_r
111 # else
112 #  if HAVE_STRERROR
113 #   ifndef strerror             /* On some systems, strerror is a macro */
114 char *strerror ();
115 #   endif
116 #  else
117 static char *
118 private_strerror (errnum)
119      int errnum;
120 {
121   extern char *sys_errlist[];
122   extern int sys_nerr;
123
124   if (errnum > 0 && errnum <= sys_nerr)
125     return _(sys_errlist[errnum]);
126   return _("Unknown system error");
127 }
128 #   define strerror private_strerror
129 #  endif /* HAVE_STRERROR */
130 # endif /* HAVE_STRERROR_R */
131 #endif  /* not _LIBC */
132
133
134 #ifdef VA_START
135 static void
136 error_tail (int status, int errnum, const char *message, va_list args)
137 {
138 # if HAVE_VPRINTF || _LIBC
139 #  if _LIBC && USE_IN_LIBIO
140   if (_IO_fwide (stderr, 0) > 0)
141     {
142 #   define ALLOCA_LIMIT 2000
143       size_t len = strlen (message) + 1;
144       wchar_t *wmessage = NULL;
145       mbstate_t st;
146       size_t res;
147       const char *tmp;
148
149       do
150         {
151           if (len < ALLOCA_LIMIT)
152             wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
153           else
154             {
155               if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
156                 wmessage = NULL;
157
158               wmessage = (wchar_t *) realloc (wmessage,
159                                               len * sizeof (wchar_t));
160
161               if (wmessage == NULL)
162                 {
163                   fputws_unlocked (L"out of memory\n", stderr);
164                   return;
165                 }
166             }
167
168           memset (&st, '\0', sizeof (st));
169           tmp =message;
170         }
171       while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
172
173       if (res == (size_t) -1)
174         /* The string cannot be converted.  */
175         wmessage = (wchar_t *) L"???";
176
177       __vfwprintf (stderr, wmessage, args);
178     }
179   else
180 #  endif
181     vfprintf (stderr, message, args);
182 # else
183   _doprnt (message, args, stderr);
184 # endif
185   va_end (args);
186
187   ++error_message_count;
188   if (errnum)
189     {
190 # if defined HAVE_STRERROR_R || _LIBC
191       char errbuf[1024];
192       char *s = __strerror_r (errnum, errbuf, sizeof errbuf);
193 #  if _LIBC && USE_IN_LIBIO
194       if (_IO_fwide (stderr, 0) > 0)
195         __fwprintf (stderr, L": %s", s);
196       else
197 #  endif
198         fprintf (stderr, ": %s", s);
199 # else
200       fprintf (stderr, ": %s", strerror (errnum));
201 # endif
202     }
203 # if _LIBC && USE_IN_LIBIO
204   if (_IO_fwide (stderr, 0) > 0)
205     putwc (L'\n', stderr);
206   else
207 # endif
208     putc ('\n', stderr);
209   fflush (stderr);
210   if (status)
211     exit (status);
212 }
213 #endif
214
215
216 /* Print the program name and error message MESSAGE, which is a printf-style
217    format string with optional args.
218    If ERRNUM is nonzero, print its corresponding system error message.
219    Exit with status STATUS if it is nonzero.  */
220 /* VARARGS */
221 void
222 #if defined VA_START && __STDC__
223 error (int status, int errnum, const char *message, ...)
224 #else
225 error (status, errnum, message, va_alist)
226      int status;
227      int errnum;
228      char *message;
229      va_dcl
230 #endif
231 {
232 #ifdef VA_START
233   va_list args;
234 #endif
235
236   fflush (stdout);
237 #ifdef _LIBC
238 # ifdef USE_IN_LIBIO
239   _IO_flockfile (stderr);
240 # else
241   __flockfile (stderr);
242 # endif
243 #endif
244   if (error_print_progname)
245     (*error_print_progname) ();
246   else
247     {
248 #if _LIBC && USE_IN_LIBIO
249       if (_IO_fwide (stderr, 0) > 0)
250         __fwprintf (stderr, L"%s: ", program_name);
251       else
252 #endif
253         fprintf (stderr, "%s: ", program_name);
254     }
255
256 #ifdef VA_START
257   VA_START (args, message);
258   error_tail (status, errnum, message, args);
259 #else
260   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
261
262   ++error_message_count;
263   if (errnum)
264     {
265 # if defined HAVE_STRERROR_R || _LIBC
266       char errbuf[1024];
267       /* Don't use __strerror_r's return value because on some systems
268          (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'.  */
269       __strerror_r (errnum, errbuf, sizeof errbuf);
270       fprintf (stderr, ": %s", errbuf);
271 # else
272       fprintf (stderr, ": %s", strerror (errnum));
273 # endif
274     }
275   putc ('\n', stderr);
276   fflush (stderr);
277   if (status)
278     exit (status);
279 #endif
280
281 #ifdef _LIBC
282 # ifdef USE_IN_LIBIO
283   _IO_funlockfile (stderr);
284 # else
285   __funlockfile (stderr);
286 # endif
287 #endif
288 }
289 \f
290 /* Sometimes we want to have at most one error per line.  This
291    variable controls whether this mode is selected or not.  */
292 int error_one_per_line;
293
294 void
295 #if defined VA_START && __STDC__
296 error_at_line (int status, int errnum, const char *file_name,
297                unsigned int line_number, const char *message, ...)
298 #else
299 error_at_line (status, errnum, file_name, line_number, message, va_alist)
300      int status;
301      int errnum;
302      const char *file_name;
303      unsigned int line_number;
304      char *message;
305      va_dcl
306 #endif
307 {
308 #ifdef VA_START
309   va_list args;
310 #endif
311
312   if (error_one_per_line)
313     {
314       static const char *old_file_name;
315       static unsigned int old_line_number;
316
317       if (old_line_number == line_number
318           && (file_name == old_file_name
319               || strcmp (old_file_name, file_name) == 0))
320         /* Simply return and print nothing.  */
321         return;
322
323       old_file_name = file_name;
324       old_line_number = line_number;
325     }
326
327   fflush (stdout);
328 #ifdef _LIBC
329 # ifdef USE_IN_LIBIO
330   _IO_flockfile (stderr);
331 # else
332   __flockfile (stderr);
333 # endif
334 #endif
335   if (error_print_progname)
336     (*error_print_progname) ();
337   else
338     {
339 #if _LIBC && USE_IN_LIBIO
340       if (_IO_fwide (stderr, 0) > 0)
341         __fwprintf (stderr, L"%s: ", program_name);
342       else
343 #endif
344         fprintf (stderr, "%s:", program_name);
345     }
346
347   if (file_name != NULL)
348     {
349 #if _LIBC && USE_IN_LIBIO
350       if (_IO_fwide (stderr, 0) > 0)
351         __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
352       else
353 #endif
354         fprintf (stderr, "%s:%d: ", file_name, line_number);
355     }
356
357 #ifdef VA_START
358   VA_START (args, message);
359   error_tail (status, errnum, message, args);
360 #else
361   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
362
363   ++error_message_count;
364   if (errnum)
365     {
366 # if defined HAVE_STRERROR_R || _LIBC
367       char errbuf[1024];
368       /* Don't use __strerror_r's return value because on some systems
369          (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'.  */
370       __strerror_r (errnum, errbuf, sizeof errbuf);
371       fprintf (stderr, ": %s", errbuf);
372 # else
373       fprintf (stderr, ": %s", strerror (errnum));
374 # endif
375     }
376   putc ('\n', stderr);
377   fflush (stderr);
378   if (status)
379     exit (status);
380 #endif
381
382 #ifdef _LIBC
383 # ifdef USE_IN_LIBIO
384   _IO_funlockfile (stderr);
385 # else
386   __funlockfile (stderr);
387 # endif
388 #endif
389 }
390
391 #ifdef _LIBC
392 /* Make the weak alias.  */
393 # undef error
394 # undef error_at_line
395 weak_alias (__error, error)
396 weak_alias (__error_at_line, error_at_line)
397 #endif