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