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