]> git.saurik.com Git - bison.git/blob - src/complain.c
* data/yacc.c: Output the copyright notive in the header.
[bison.git] / src / complain.c
1 /* Declaration for error-reporting function for Bison.
2 Copyright 2000, 2001 Free Software Foundation, Inc.
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
22 #include "system.h"
23
24 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
25 # ifdef __STDC__
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
41 void exit ();
42 #endif
43
44 /* To get error_one_per_line. */
45 #include "error.h"
46
47 #include "complain.h"
48
49 #ifndef HAVE_DECL_STRERROR_R
50 "this configure-time declaration test was not run"
51 #endif
52 #if !HAVE_DECL_STRERROR_R
53 char *strerror_r ();
54 #endif
55
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. */
62
63 # define program_name program_invocation_name
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
76 #else /* not _LIBC */
77
78 /* The calling program should define program_name and set it to the
79 name of the executing program. */
80 extern char *program_name;
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 */
87 char *strerror ();
88 # endif
89 # else
90 static char *
91 private_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 */
105
106 /* This variable is incremented each time `warn' is called. */
107 unsigned int warn_message_count;
108
109 /* This variable is incremented each time `complain' is called. */
110 unsigned int complain_message_count;
111
112 \f
113 /*--------------------------------.
114 | Report a warning, and proceed. |
115 `--------------------------------*/
116
117 void
118 #if defined VA_START && defined __STDC__
119 warn_at (location_t location, const char *message, ...)
120 #else
121 warn_at (location, message, va_alist)
122 location_t 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.first_line &&
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.first_line;
143 }
144
145 fflush (stdout);
146 LOCATION_PRINT (stderr, location);
147 fputs (": ", stderr);
148 fputs (_("warning: "), stderr);
149
150 #ifdef VA_START
151 VA_START (args, message);
152 vfprintf (stderr, message, args);
153 va_end (args);
154 #else
155 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
156 #endif
157
158 ++warn_message_count;
159 putc ('\n', stderr);
160 fflush (stderr);
161 }
162
163
164 void
165 #if defined VA_START && defined __STDC__
166 warn (const char *message, ...)
167 #else
168 warn (message, va_alist)
169 char *message;
170 va_dcl
171 #endif
172 {
173 #ifdef VA_START
174 va_list args;
175 #endif
176
177 if (error_one_per_line)
178 {
179 static const char *old_infile;
180 static int old_lineno;
181
182 if (old_lineno == lineno &&
183 (infile == old_infile || !strcmp (old_infile, infile)))
184 /* Simply return and print nothing. */
185 return;
186
187 old_infile = infile;
188 old_lineno = lineno;
189 }
190
191 fflush (stdout);
192 if (infile != NULL)
193 fprintf (stderr, "%s:%d: ", infile, lineno);
194 else
195 fprintf (stderr, "%s:", program_name);
196
197 fputs (_("warning: "), stderr);
198
199 #ifdef VA_START
200 VA_START (args, message);
201 vfprintf (stderr, message, args);
202 va_end (args);
203 #else
204 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
205 #endif
206
207 ++warn_message_count;
208 putc ('\n', stderr);
209 fflush (stderr);
210 }
211 \f
212 /*-----------------------------------------------------------.
213 | An error has occurred, but we can proceed, and die later. |
214 `-----------------------------------------------------------*/
215
216 void
217 #if defined VA_START && defined __STDC__
218 complain_at (location_t location, const char *message, ...)
219 #else
220 complain_at (location, message, va_alist)
221 location_t location;
222 char *message;
223 va_dcl
224 #endif
225 {
226 #ifdef VA_START
227 va_list args;
228 #endif
229
230 if (error_one_per_line)
231 {
232 static const char *old_infile;
233 static int old_lineno;
234
235 if (old_lineno == location.first_line &&
236 (infile == old_infile || !strcmp (old_infile, infile)))
237 /* Simply return and print nothing. */
238 return;
239
240 old_infile = infile;
241 old_lineno = location.first_line;
242 }
243
244 fflush (stdout);
245 LOCATION_PRINT (stderr, location);
246 fputs (": ", stderr);
247
248 #ifdef VA_START
249 VA_START (args, message);
250 vfprintf (stderr, message, args);
251 va_end (args);
252 #else
253 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
254 #endif
255
256 ++complain_message_count;
257 putc ('\n', stderr);
258 fflush (stderr);
259 }
260
261
262 void
263 #if defined VA_START && defined __STDC__
264 complain (const char *message, ...)
265 #else
266 complain (message, va_alist)
267 char *message;
268 va_dcl
269 #endif
270 {
271 #ifdef VA_START
272 va_list args;
273 #endif
274
275 if (error_one_per_line)
276 {
277 static const char *old_infile;
278 static int old_lineno;
279
280 if (old_lineno == lineno &&
281 (infile == old_infile || !strcmp (old_infile, infile)))
282 /* Simply return and print nothing. */
283 return;
284
285 old_infile = infile;
286 old_lineno = lineno;
287 }
288
289 fflush (stdout);
290 if (infile != NULL)
291 fprintf (stderr, "%s:%d: ", infile, lineno);
292 else
293 fprintf (stderr, "%s:", program_name);
294
295 #ifdef VA_START
296 VA_START (args, message);
297 vfprintf (stderr, message, args);
298 va_end (args);
299 #else
300 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
301 #endif
302
303 ++complain_message_count;
304 putc ('\n', stderr);
305 fflush (stderr);
306 }
307 \f
308 /*-------------------------------------------------.
309 | A severe error has occurred, we cannot proceed. |
310 `-------------------------------------------------*/
311
312 void
313 #if defined VA_START && defined __STDC__
314 fatal_at (location_t location, const char *message, ...)
315 #else
316 fatal (location, message, va_alist)
317 location_t location;
318 char *message;
319 va_dcl
320 #endif
321 {
322 #ifdef VA_START
323 va_list args;
324 #endif
325
326 fflush (stdout);
327 LOCATION_PRINT (stderr, location);
328 fputs (": ", stderr);
329 fputs (_("fatal error: "), stderr);
330
331 #ifdef VA_START
332 VA_START (args, message);
333 vfprintf (stderr, message, args);
334 va_end (args);
335 #else
336 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
337 #endif
338 putc ('\n', stderr);
339 fflush (stderr);
340 exit (1);
341 }
342
343 void
344 #if defined VA_START && defined __STDC__
345 fatal (const char *message, ...)
346 #else
347 fatal (message, va_alist)
348 char *message;
349 va_dcl
350 #endif
351 {
352 #ifdef VA_START
353 va_list args;
354 #endif
355
356 fflush (stdout);
357 if (infile != NULL)
358 fprintf (stderr, "%s:%d: ", infile, lineno);
359 else
360 fprintf (stderr, "%s:", program_name);
361
362 fputs (_("fatal error: "), stderr);
363
364 #ifdef VA_START
365 VA_START (args, message);
366 vfprintf (stderr, message, args);
367 va_end (args);
368 #else
369 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
370 #endif
371 putc ('\n', stderr);
372 fflush (stderr);
373 exit (1);
374 }