]> git.saurik.com Git - bison.git/blame - lib/error.c
yyprint -> print_token_value
[bison.git] / lib / error.c
CommitLineData
ff4a34be 1/* Error handler for noninteractive utilities
dda7aa54 2 Copyright (C) 1990-1998, 2000, 2001, 2002 Free Software Foundation, Inc.
d5629ce3
PE
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.
ff4a34be 7
d5629ce3 8 This program is distributed in the hope that it will be useful,
ff4a34be 9 but WITHOUT ANY WARRANTY; without even the implied warranty of
d5629ce3
PE
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
ff4a34be 12
dda7aa54
PE
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,
d5629ce3 15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
ff4a34be
AD
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>
dda7aa54
PE
24
25#ifdef _LIBC
5141b016 26# include <libintl.h>
dda7aa54
PE
27#else
28# include "gettext.h"
5141b016 29#endif
dda7aa54
PE
30#define _(msgid) gettext (msgid)
31
4a0d8936
PE
32#ifdef _LIBC
33# include <wchar.h>
34# define mbsrtowcs __mbsrtowcs
35#endif
ff4a34be
AD
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
54void exit ();
55#endif
56
57#include "error.h"
d5629ce3 58#include "unlocked-io.h"
ff4a34be 59
ff4a34be
AD
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. */
63void (*error_print_progname) (
64#if __STDC__ - 0
65 void
66#endif
67 );
68
69/* This variable is incremented each time `error' is called. */
70unsigned 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. */
4a0d8936
PE
80extern void __error (int status, int errnum, const char *message, ...)
81 __attribute__ ((__format__ (__printf__, 3, 4)));
82extern 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)));;
ff4a34be
AD
86# define error __error
87# define error_at_line __error_at_line
88
5141b016
AD
89# ifdef USE_IN_LIBIO
90# include <libio/iolibio.h>
91# define fflush(s) _IO_fflush (s)
92# endif
93
ff4a34be
AD
94#else /* not _LIBC */
95
4a0d8936
PE
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
100char *strerror_r ();
101# endif
102
ff4a34be
AD
103/* The calling program should define program_name and set it to the
104 name of the executing program. */
105extern char *program_name;
106
4a0d8936 107# if HAVE_STRERROR_R || defined strerror_r
ff4a34be
AD
108# define __strerror_r strerror_r
109# else
110# if HAVE_STRERROR
4a0d8936
PE
111# ifndef HAVE_DECL_STRERROR
112"this configure-time declaration test was not run"
113# endif
114# if !HAVE_DECL_STRERROR
ff4a34be
AD
115char *strerror ();
116# endif
117# else
118static char *
4a0d8936 119private_strerror (int errnum)
ff4a34be
AD
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 */
4a0d8936 130# endif /* HAVE_STRERROR_R || defined strerror_r */
ff4a34be
AD
131#endif /* not _LIBC */
132
4a0d8936
PE
133static void
134print_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
169static void
170error_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
ff4a34be
AD
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 */
ff4a34be
AD
242void
243#if defined VA_START && __STDC__
244error (int status, int errnum, const char *message, ...)
245#else
246error (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
4a0d8936
PE
257 fflush (stdout);
258#ifdef _LIBC
259# ifdef USE_IN_LIBIO
260 _IO_flockfile (stderr);
261# else
262 __flockfile (stderr);
263# endif
264#endif
ff4a34be
AD
265 if (error_print_progname)
266 (*error_print_progname) ();
267 else
268 {
4a0d8936
PE
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);
ff4a34be
AD
275 }
276
277#ifdef VA_START
278 VA_START (args, message);
4a0d8936 279 error_tail (status, errnum, message, args);
ff4a34be
AD
280#else
281 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
ff4a34be
AD
282
283 ++error_message_count;
284 if (errnum)
4a0d8936 285 print_errno_message (errnum);
ff4a34be
AD
286 putc ('\n', stderr);
287 fflush (stderr);
288 if (status)
289 exit (status);
4a0d8936
PE
290#endif
291
292#ifdef _LIBC
293# ifdef USE_IN_LIBIO
294 _IO_funlockfile (stderr);
295# else
296 __funlockfile (stderr);
297# endif
298#endif
ff4a34be
AD
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. */
303int error_one_per_line;
304
305void
306#if defined VA_START && __STDC__
307error_at_line (int status, int errnum, const char *file_name,
308 unsigned int line_number, const char *message, ...)
309#else
310error_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
4a0d8936
PE
328 if (old_line_number == line_number
329 && (file_name == old_file_name
330 || strcmp (old_file_name, file_name) == 0))
ff4a34be
AD
331 /* Simply return and print nothing. */
332 return;
333
334 old_file_name = file_name;
335 old_line_number = line_number;
336 }
337
4a0d8936
PE
338 fflush (stdout);
339#ifdef _LIBC
340# ifdef USE_IN_LIBIO
341 _IO_flockfile (stderr);
342# else
343 __flockfile (stderr);
344# endif
345#endif
ff4a34be
AD
346 if (error_print_progname)
347 (*error_print_progname) ();
348 else
349 {
4a0d8936
PE
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);
ff4a34be
AD
356 }
357
358 if (file_name != NULL)
4a0d8936
PE
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 }
ff4a34be
AD
367
368#ifdef VA_START
369 VA_START (args, message);
4a0d8936 370 error_tail (status, errnum, message, args);
ff4a34be
AD
371#else
372 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
ff4a34be
AD
373
374 ++error_message_count;
375 if (errnum)
4a0d8936 376 print_errno_message (errnum);
ff4a34be
AD
377 putc ('\n', stderr);
378 fflush (stderr);
379 if (status)
380 exit (status);
4a0d8936
PE
381#endif
382
383#ifdef _LIBC
384# ifdef USE_IN_LIBIO
385 _IO_funlockfile (stderr);
386# else
387 __funlockfile (stderr);
388# endif
389#endif
ff4a34be
AD
390}
391
392#ifdef _LIBC
393/* Make the weak alias. */
394# undef error
395# undef error_at_line
396weak_alias (__error, error)
397weak_alias (__error_at_line, error_at_line)
398#endif