]>
git.saurik.com Git - bison.git/blob - 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)
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.
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. */
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
30 #define _(msgid) gettext (msgid)
34 # define mbsrtowcs __mbsrtowcs
37 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
40 # define VA_START(args, lastarg) va_start(args, lastarg)
43 # define VA_START(args, lastarg) va_start(args)
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;
50 #if STDC_HEADERS || _LIBC
58 #include "unlocked-io.h"
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
) (
69 /* This variable is incremented each time `error' is called. */
70 unsigned int error_message_count
;
73 /* In the GNU C library, there is a predefined variable for this. */
75 # define program_name program_invocation_name
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
,
85 __attribute__ ((__format__ (__printf__
, 5, 6)));;
86 # define error __error
87 # define error_at_line __error_at_line
90 # include <libio/iolibio.h>
91 # define fflush(s) _IO_fflush (s)
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"
103 /* The calling program should define program_name and set it to the
104 name of the executing program. */
105 extern char *program_name
;
107 # if HAVE_STRERROR_R || defined strerror_r
108 # define __strerror_r strerror_r
111 # ifndef HAVE_DECL_STRERROR
112 "this configure-time declaration test was not run"
114 # if !HAVE_DECL_STRERROR
119 private_strerror (int errnum
)
121 extern char *sys_errlist
[];
124 if (errnum
> 0 && errnum
<= sys_nerr
)
125 return _(sys_errlist
[errnum
]);
126 return _("Unknown system error");
128 # define strerror private_strerror
129 # endif /* HAVE_STRERROR */
130 # endif /* HAVE_STRERROR_R || defined strerror_r */
131 #endif /* not _LIBC */
134 print_errno_message (int errnum
)
138 #if defined HAVE_STRERROR_R || _LIBC
140 # if STRERROR_R_CHAR_P || _LIBC
141 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
143 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
149 s
= strerror (errnum
);
154 s
= _("Unknown system error");
157 #if _LIBC && USE_IN_LIBIO
158 if (_IO_fwide (stderr
, 0) > 0)
160 __fwprintf (stderr
, L
": %s", s
);
165 fprintf (stderr
, ": %s", s
);
170 error_tail (int status
, int errnum
, const char *message
, va_list args
)
172 # if HAVE_VPRINTF || _LIBC
173 # if _LIBC && USE_IN_LIBIO
174 if (_IO_fwide (stderr
, 0) > 0)
176 # define ALLOCA_LIMIT 2000
177 size_t len
= strlen (message
) + 1;
178 wchar_t *wmessage
= NULL
;
185 if (len
< ALLOCA_LIMIT
)
186 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
189 if (wmessage
!= NULL
&& len
/ 2 < ALLOCA_LIMIT
)
192 wmessage
= (wchar_t *) realloc (wmessage
,
193 len
* sizeof (wchar_t));
195 if (wmessage
== NULL
)
197 fputws_unlocked (L
"out of memory\n", stderr
);
202 memset (&st
, '\0', sizeof (st
));
205 while ((res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
)) == len
);
207 if (res
== (size_t) -1)
208 /* The string cannot be converted. */
209 wmessage
= (wchar_t *) L
"???";
211 __vfwprintf (stderr
, wmessage
, args
);
215 vfprintf (stderr
, message
, args
);
217 _doprnt (message
, args
, stderr
);
221 ++error_message_count
;
223 print_errno_message (errnum
);
224 # if _LIBC && USE_IN_LIBIO
225 if (_IO_fwide (stderr
, 0) > 0)
226 putwc (L
'\n', stderr
);
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. */
243 #if defined VA_START && __STDC__
244 error (int status
, int errnum
, const char *message
, ...)
246 error (status
, errnum
, message
, va_alist
)
260 _IO_flockfile (stderr
);
262 __flockfile (stderr
);
265 if (error_print_progname
)
266 (*error_print_progname
) ();
269 #if _LIBC && USE_IN_LIBIO
270 if (_IO_fwide (stderr
, 0) > 0)
271 __fwprintf (stderr
, L
"%s: ", program_name
);
274 fprintf (stderr
, "%s: ", program_name
);
278 VA_START (args
, message
);
279 error_tail (status
, errnum
, message
, args
);
281 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
283 ++error_message_count
;
285 print_errno_message (errnum
);
294 _IO_funlockfile (stderr
);
296 __funlockfile (stderr
);
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
;
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
, ...)
310 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
313 const char *file_name
;
314 unsigned int line_number
;
323 if (error_one_per_line
)
325 static const char *old_file_name
;
326 static unsigned int old_line_number
;
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. */
334 old_file_name
= file_name
;
335 old_line_number
= line_number
;
341 _IO_flockfile (stderr
);
343 __flockfile (stderr
);
346 if (error_print_progname
)
347 (*error_print_progname
) ();
350 #if _LIBC && USE_IN_LIBIO
351 if (_IO_fwide (stderr
, 0) > 0)
352 __fwprintf (stderr
, L
"%s: ", program_name
);
355 fprintf (stderr
, "%s:", program_name
);
358 if (file_name
!= NULL
)
360 #if _LIBC && USE_IN_LIBIO
361 if (_IO_fwide (stderr
, 0) > 0)
362 __fwprintf (stderr
, L
"%s:%d: ", file_name
, line_number
);
365 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
369 VA_START (args
, message
);
370 error_tail (status
, errnum
, message
, args
);
372 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
374 ++error_message_count
;
376 print_errno_message (errnum
);
385 _IO_funlockfile (stderr
);
387 __funlockfile (stderr
);
393 /* Make the weak alias. */
395 # undef error_at_line
396 weak_alias (__error
, error
)
397 weak_alias (__error_at_line
, error_at_line
)