]>
git.saurik.com Git - bison.git/blob - 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.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
32 # define mbsrtowcs __mbsrtowcs
35 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
38 # define VA_START(args, lastarg) va_start(args, lastarg)
41 # define VA_START(args, lastarg) va_start(args)
44 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
45 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
48 #if STDC_HEADERS || _LIBC
56 #include "unlocked-io.h"
59 # define _(String) String
62 /* If NULL, error will flush stdout, then print on stderr the program
63 name, a colon and a space. Otherwise, error will call this
64 function without parameters instead. */
65 void (*error_print_progname
) (
71 /* This variable is incremented each time `error' is called. */
72 unsigned int error_message_count
;
75 /* In the GNU C library, there is a predefined variable for this. */
77 # define program_name program_invocation_name
80 /* In GNU libc we want do not want to use the common name `error' directly.
81 Instead make it a weak alias. */
82 extern void __error (int status
, int errnum
, const char *message
, ...)
83 __attribute__ ((__format__ (__printf__
, 3, 4)));
84 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
85 unsigned int line_number
, const char *message
,
87 __attribute__ ((__format__ (__printf__
, 5, 6)));;
88 # define error __error
89 # define error_at_line __error_at_line
92 # include <libio/iolibio.h>
93 # define fflush(s) _IO_fflush (s)
98 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
99 # ifndef HAVE_DECL_STRERROR_R
100 "this configure-time declaration test was not run"
105 /* The calling program should define program_name and set it to the
106 name of the executing program. */
107 extern char *program_name
;
109 # if HAVE_STRERROR_R || defined strerror_r
110 # define __strerror_r strerror_r
113 # ifndef HAVE_DECL_STRERROR
114 "this configure-time declaration test was not run"
116 # if !HAVE_DECL_STRERROR
121 private_strerror (int errnum
)
123 extern char *sys_errlist
[];
126 if (errnum
> 0 && errnum
<= sys_nerr
)
127 return _(sys_errlist
[errnum
]);
128 return _("Unknown system error");
130 # define strerror private_strerror
131 # endif /* HAVE_STRERROR */
132 # endif /* HAVE_STRERROR_R || defined strerror_r */
133 #endif /* not _LIBC */
136 print_errno_message (int errnum
)
140 #if defined HAVE_STRERROR_R || _LIBC
142 # if STRERROR_R_CHAR_P || _LIBC
143 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
145 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
151 s
= strerror (errnum
);
156 s
= _("Unknown system error");
159 #if _LIBC && USE_IN_LIBIO
160 if (_IO_fwide (stderr
, 0) > 0)
162 __fwprintf (stderr
, L
": %s", s
);
167 fprintf (stderr
, ": %s", s
);
172 error_tail (int status
, int errnum
, const char *message
, va_list args
)
174 # if HAVE_VPRINTF || _LIBC
175 # if _LIBC && USE_IN_LIBIO
176 if (_IO_fwide (stderr
, 0) > 0)
178 # define ALLOCA_LIMIT 2000
179 size_t len
= strlen (message
) + 1;
180 wchar_t *wmessage
= NULL
;
187 if (len
< ALLOCA_LIMIT
)
188 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
191 if (wmessage
!= NULL
&& len
/ 2 < ALLOCA_LIMIT
)
194 wmessage
= (wchar_t *) realloc (wmessage
,
195 len
* sizeof (wchar_t));
197 if (wmessage
== NULL
)
199 fputws_unlocked (L
"out of memory\n", stderr
);
204 memset (&st
, '\0', sizeof (st
));
207 while ((res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
)) == len
);
209 if (res
== (size_t) -1)
210 /* The string cannot be converted. */
211 wmessage
= (wchar_t *) L
"???";
213 __vfwprintf (stderr
, wmessage
, args
);
217 vfprintf (stderr
, message
, args
);
219 _doprnt (message
, args
, stderr
);
223 ++error_message_count
;
225 print_errno_message (errnum
);
226 # if _LIBC && USE_IN_LIBIO
227 if (_IO_fwide (stderr
, 0) > 0)
228 putwc (L
'\n', stderr
);
239 /* Print the program name and error message MESSAGE, which is a printf-style
240 format string with optional args.
241 If ERRNUM is nonzero, print its corresponding system error message.
242 Exit with status STATUS if it is nonzero. */
245 #if defined VA_START && __STDC__
246 error (int status
, int errnum
, const char *message
, ...)
248 error (status
, errnum
, message
, va_alist
)
262 _IO_flockfile (stderr
);
264 __flockfile (stderr
);
267 if (error_print_progname
)
268 (*error_print_progname
) ();
271 #if _LIBC && USE_IN_LIBIO
272 if (_IO_fwide (stderr
, 0) > 0)
273 __fwprintf (stderr
, L
"%s: ", program_name
);
276 fprintf (stderr
, "%s: ", program_name
);
280 VA_START (args
, message
);
281 error_tail (status
, errnum
, message
, args
);
283 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
285 ++error_message_count
;
287 print_errno_message (errnum
);
296 _IO_funlockfile (stderr
);
298 __funlockfile (stderr
);
303 /* Sometimes we want to have at most one error per line. This
304 variable controls whether this mode is selected or not. */
305 int error_one_per_line
;
308 #if defined VA_START && __STDC__
309 error_at_line (int status
, int errnum
, const char *file_name
,
310 unsigned int line_number
, const char *message
, ...)
312 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
315 const char *file_name
;
316 unsigned int line_number
;
325 if (error_one_per_line
)
327 static const char *old_file_name
;
328 static unsigned int old_line_number
;
330 if (old_line_number
== line_number
331 && (file_name
== old_file_name
332 || strcmp (old_file_name
, file_name
) == 0))
333 /* Simply return and print nothing. */
336 old_file_name
= file_name
;
337 old_line_number
= line_number
;
343 _IO_flockfile (stderr
);
345 __flockfile (stderr
);
348 if (error_print_progname
)
349 (*error_print_progname
) ();
352 #if _LIBC && USE_IN_LIBIO
353 if (_IO_fwide (stderr
, 0) > 0)
354 __fwprintf (stderr
, L
"%s: ", program_name
);
357 fprintf (stderr
, "%s:", program_name
);
360 if (file_name
!= NULL
)
362 #if _LIBC && USE_IN_LIBIO
363 if (_IO_fwide (stderr
, 0) > 0)
364 __fwprintf (stderr
, L
"%s:%d: ", file_name
, line_number
);
367 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
371 VA_START (args
, message
);
372 error_tail (status
, errnum
, message
, args
);
374 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
376 ++error_message_count
;
378 print_errno_message (errnum
);
387 _IO_funlockfile (stderr
);
389 __funlockfile (stderr
);
395 /* Make the weak alias. */
397 # undef error_at_line
398 weak_alias (__error
, error
)
399 weak_alias (__error_at_line
, error_at_line
)