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