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