]> git.saurik.com Git - bison.git/blame - src/complain.c
Have Bison grammars parsed by a Bison grammar.
[bison.git] / src / complain.c
CommitLineData
a0f6b076 1/* Declaration for error-reporting function for Bison.
0e41b407 2 Copyright 2000, 2001 Free Software Foundation, Inc.
a0f6b076
AD
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA. */
18
19/* Based on error.c and error.h,
20 written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
342b8b6e 22#include "system.h"
a0f6b076
AD
23
24#if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
342b8b6e 25# ifdef __STDC__
a0f6b076
AD
26# include <stdarg.h>
27# define VA_START(args, lastarg) va_start(args, lastarg)
28# else
29# include <varargs.h>
30# define VA_START(args, lastarg) va_start(args)
31# endif
32#else
33# define va_alist a1, a2, a3, a4, a5, a6, a7, a8
34# define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
35#endif
36
37#if STDC_HEADERS || _LIBC
38# include <stdlib.h>
39# include <string.h>
40#else
41void exit ();
42#endif
43
9b53a24f
AD
44/* To get error_one_per_line. */
45#include "error.h"
46
a0f6b076
AD
47#include "complain.h"
48
8f13fe33
AD
49#ifndef HAVE_DECL_STRERROR_R
50"this configure-time declaration test was not run"
51#endif
52#if !HAVE_DECL_STRERROR_R
53char *strerror_r ();
54#endif
55
a0f6b076
AD
56#ifndef _
57# define _(String) String
58#endif
59
60#ifdef _LIBC
61/* In the GNU C library, there is a predefined variable for this. */
8f13fe33 62
a0f6b076 63# define program_name program_invocation_name
8f13fe33
AD
64# include <errno.h>
65
66/* In GNU libc we want do not want to use the common name `error' directly.
67 Instead make it a weak alias. */
68# define error __error
69# define error_at_line __error_at_line
70
71# ifdef USE_IN_LIBIO
72# include <libio/iolibio.h>
73# define fflush(s) _IO_fflush (s)
74# endif
75
a0f6b076 76#else /* not _LIBC */
8f13fe33 77
a0f6b076
AD
78/* The calling program should define program_name and set it to the
79 name of the executing program. */
80extern char *program_name;
8f13fe33
AD
81
82# ifdef HAVE_STRERROR_R
83# define __strerror_r strerror_r
84# else
85# if HAVE_STRERROR
86# ifndef strerror /* On some systems, strerror is a macro */
87char *strerror ();
88# endif
89# else
90static char *
91private_strerror (errnum)
92 int errnum;
93{
94 extern char *sys_errlist[];
95 extern int sys_nerr;
96
97 if (errnum > 0 && errnum <= sys_nerr)
98 return _(sys_errlist[errnum]);
99 return _("Unknown system error");
100}
101# define strerror private_strerror
102# endif /* HAVE_STRERROR */
103# endif /* HAVE_STRERROR_R */
104#endif /* not _LIBC */
a0f6b076
AD
105
106/* This variable is incremented each time `warn' is called. */
107unsigned int warn_message_count;
108
109/* This variable is incremented each time `complain' is called. */
110unsigned int complain_message_count;
111
a0f6b076
AD
112\f
113/*--------------------------------.
114| Report a warning, and proceed. |
115`--------------------------------*/
116
e9955c83
AD
117void
118#if defined VA_START && defined __STDC__
119warn_at (int location, const char *message, ...)
120#else
121warn_at (location, message, va_alist)
122 int location
123 char *message;
124 va_dcl
125#endif
126{
127#ifdef VA_START
128 va_list args;
129#endif
130
131 if (error_one_per_line)
132 {
133 static const char *old_infile;
134 static int old_lineno;
135
136 if (old_lineno == location &&
137 (infile == old_infile || !strcmp (old_infile, infile)))
138 /* Simply return and print nothing. */
139 return;
140
141 old_infile = infile;
142 old_lineno = location;
143 }
144
145 fflush (stdout);
146 if (infile != NULL)
147 fprintf (stderr, "%s:%d: ", infile, location);
148 else
149 fprintf (stderr, "%s:", program_name);
150
151 fputs (_("warning: "), stderr);
152
153#ifdef VA_START
154 VA_START (args, message);
155 vfprintf (stderr, message, args);
156 va_end (args);
157#else
158 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
159#endif
160
161 ++warn_message_count;
162 putc ('\n', stderr);
163 fflush (stderr);
164}
165
166
a0f6b076 167void
342b8b6e 168#if defined VA_START && defined __STDC__
a0f6b076
AD
169warn (const char *message, ...)
170#else
171warn (message, va_alist)
172 char *message;
173 va_dcl
174#endif
175{
176#ifdef VA_START
177 va_list args;
178#endif
179
180 if (error_one_per_line)
181 {
182 static const char *old_infile;
4a120d45 183 static int old_lineno;
a0f6b076
AD
184
185 if (old_lineno == lineno &&
186 (infile == old_infile || !strcmp (old_infile, infile)))
187 /* Simply return and print nothing. */
188 return;
189
190 old_infile = infile;
191 old_lineno = lineno;
192 }
193
194 fflush (stdout);
195 if (infile != NULL)
196 fprintf (stderr, "%s:%d: ", infile, lineno);
197 else
198 fprintf (stderr, "%s:", program_name);
199
200 fputs (_("warning: "), stderr);
201
202#ifdef VA_START
203 VA_START (args, message);
204 vfprintf (stderr, message, args);
205 va_end (args);
206#else
207 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
208#endif
209
210 ++warn_message_count;
211 putc ('\n', stderr);
212 fflush (stderr);
213}
214\f
215/*-----------------------------------------------------------.
216| An error has occurred, but we can proceed, and die later. |
217`-----------------------------------------------------------*/
218
e9955c83
AD
219void
220#if defined VA_START && defined __STDC__
221complain_at (int location, const char *message, ...)
222#else
223complain_at (location, message, va_alist)
224 int location;
225 char *message;
226 va_dcl
227#endif
228{
229#ifdef VA_START
230 va_list args;
231#endif
232
233 if (error_one_per_line)
234 {
235 static const char *old_infile;
236 static int old_lineno;
237
238 if (old_lineno == location &&
239 (infile == old_infile || !strcmp (old_infile, infile)))
240 /* Simply return and print nothing. */
241 return;
242
243 old_infile = infile;
244 old_lineno = location;
245 }
246
247 fflush (stdout);
248 if (infile != NULL)
249 fprintf (stderr, "%s:%d: ", infile, location);
250 else
251 fprintf (stderr, "%s:", program_name);
252
253#ifdef VA_START
254 VA_START (args, message);
255 vfprintf (stderr, message, args);
256 va_end (args);
257#else
258 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
259#endif
260
261 ++complain_message_count;
262 putc ('\n', stderr);
263 fflush (stderr);
264}
265
266
a0f6b076 267void
342b8b6e 268#if defined VA_START && defined __STDC__
a0f6b076
AD
269complain (const char *message, ...)
270#else
271complain (message, va_alist)
272 char *message;
273 va_dcl
274#endif
275{
276#ifdef VA_START
277 va_list args;
278#endif
279
280 if (error_one_per_line)
281 {
282 static const char *old_infile;
4a120d45 283 static int old_lineno;
a0f6b076
AD
284
285 if (old_lineno == lineno &&
286 (infile == old_infile || !strcmp (old_infile, infile)))
287 /* Simply return and print nothing. */
288 return;
289
290 old_infile = infile;
291 old_lineno = lineno;
292 }
293
294 fflush (stdout);
295 if (infile != NULL)
296 fprintf (stderr, "%s:%d: ", infile, lineno);
297 else
298 fprintf (stderr, "%s:", program_name);
299
300#ifdef VA_START
301 VA_START (args, message);
302 vfprintf (stderr, message, args);
303 va_end (args);
304#else
305 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
306#endif
307
308 ++complain_message_count;
309 putc ('\n', stderr);
310 fflush (stderr);
311}
312\f
313/*-------------------------------------------------.
314| A severe error has occurred, we cannot proceed. |
315`-------------------------------------------------*/
316
e9955c83
AD
317void
318#if defined VA_START && defined __STDC__
319fatal_at (int location, const char *message, ...)
320#else
321fatal (location, message, va_alist)
322 int location;
323 char *message;
324 va_dcl
325#endif
326{
327#ifdef VA_START
328 va_list args;
329#endif
330
331 fflush (stdout);
332 if (infile != NULL)
333 fprintf (stderr, "%s:%d: ", infile, location);
334 else
335 fprintf (stderr, "%s:", program_name);
336
337 fputs (_("fatal error: "), stderr);
338
339#ifdef VA_START
340 VA_START (args, message);
341 vfprintf (stderr, message, args);
342 va_end (args);
343#else
344 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
345#endif
346 putc ('\n', stderr);
347 fflush (stderr);
348 exit (1);
349}
350
a0f6b076 351void
342b8b6e 352#if defined VA_START && defined __STDC__
a0f6b076
AD
353fatal (const char *message, ...)
354#else
355fatal (message, va_alist)
356 char *message;
357 va_dcl
358#endif
359{
360#ifdef VA_START
361 va_list args;
362#endif
363
364 fflush (stdout);
365 if (infile != NULL)
366 fprintf (stderr, "%s:%d: ", infile, lineno);
367 else
368 fprintf (stderr, "%s:", program_name);
369
370 fputs (_("fatal error: "), stderr);
371
372#ifdef VA_START
373 VA_START (args, message);
374 vfprintf (stderr, message, args);
375 va_end (args);
376#else
377 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
378#endif
379 putc ('\n', stderr);
380 fflush (stderr);
381 exit (1);
382}