]> git.saurik.com Git - bison.git/blob - src/complain.c
Update.
[bison.git] / src / complain.c
1 /* Declaration for error-reporting function for Bison.
2 Copyright 2000 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 #include "complain.h"
45
46 #ifndef HAVE_DECL_STRERROR_R
47 "this configure-time declaration test was not run"
48 #endif
49 #if !HAVE_DECL_STRERROR_R
50 char *strerror_r ();
51 #endif
52
53 #ifndef _
54 # define _(String) String
55 #endif
56
57 #ifdef _LIBC
58 /* In the GNU C library, there is a predefined variable for this. */
59
60 # define program_name program_invocation_name
61 # include <errno.h>
62
63 /* In GNU libc we want do not want to use the common name `error' directly.
64 Instead make it a weak alias. */
65 # define error __error
66 # define error_at_line __error_at_line
67
68 # ifdef USE_IN_LIBIO
69 # include <libio/iolibio.h>
70 # define fflush(s) _IO_fflush (s)
71 # endif
72
73 #else /* not _LIBC */
74
75 /* The calling program should define program_name and set it to the
76 name of the executing program. */
77 extern char *program_name;
78
79 # ifdef HAVE_STRERROR_R
80 # define __strerror_r strerror_r
81 # else
82 # if HAVE_STRERROR
83 # ifndef strerror /* On some systems, strerror is a macro */
84 char *strerror ();
85 # endif
86 # else
87 static char *
88 private_strerror (errnum)
89 int errnum;
90 {
91 extern char *sys_errlist[];
92 extern int sys_nerr;
93
94 if (errnum > 0 && errnum <= sys_nerr)
95 return _(sys_errlist[errnum]);
96 return _("Unknown system error");
97 }
98 # define strerror private_strerror
99 # endif /* HAVE_STRERROR */
100 # endif /* HAVE_STRERROR_R */
101 #endif /* not _LIBC */
102
103 /* This variable is incremented each time `warn' is called. */
104 unsigned int warn_message_count;
105
106 /* This variable is incremented each time `complain' is called. */
107 unsigned int complain_message_count;
108
109 /* Sometimes we want to have at most one error per line. This
110 variable controls whether this mode is selected or not. */
111 int error_one_per_line;
112 \f
113 /*--------------------------------.
114 | Report a warning, and proceed. |
115 `--------------------------------*/
116
117 void
118 #if defined VA_START && defined __STDC__
119 warn (const char *message, ...)
120 #else
121 warn (message, va_alist)
122 char *message;
123 va_dcl
124 #endif
125 {
126 #ifdef VA_START
127 va_list args;
128 #endif
129
130 if (error_one_per_line)
131 {
132 static const char *old_infile;
133 static int old_lineno;
134
135 if (old_lineno == lineno &&
136 (infile == old_infile || !strcmp (old_infile, infile)))
137 /* Simply return and print nothing. */
138 return;
139
140 old_infile = infile;
141 old_lineno = lineno;
142 }
143
144 fflush (stdout);
145 if (infile != NULL)
146 fprintf (stderr, "%s:%d: ", infile, lineno);
147 else
148 fprintf (stderr, "%s:", program_name);
149
150 fputs (_("warning: "), stderr);
151
152 #ifdef VA_START
153 VA_START (args, message);
154 vfprintf (stderr, message, args);
155 va_end (args);
156 #else
157 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
158 #endif
159
160 ++warn_message_count;
161 putc ('\n', stderr);
162 fflush (stderr);
163 }
164 \f
165 /*-----------------------------------------------------------.
166 | An error has occurred, but we can proceed, and die later. |
167 `-----------------------------------------------------------*/
168
169 void
170 #if defined VA_START && defined __STDC__
171 complain (const char *message, ...)
172 #else
173 complain (message, va_alist)
174 char *message;
175 va_dcl
176 #endif
177 {
178 #ifdef VA_START
179 va_list args;
180 #endif
181
182 if (error_one_per_line)
183 {
184 static const char *old_infile;
185 static int old_lineno;
186
187 if (old_lineno == lineno &&
188 (infile == old_infile || !strcmp (old_infile, infile)))
189 /* Simply return and print nothing. */
190 return;
191
192 old_infile = infile;
193 old_lineno = lineno;
194 }
195
196 fflush (stdout);
197 if (infile != NULL)
198 fprintf (stderr, "%s:%d: ", infile, lineno);
199 else
200 fprintf (stderr, "%s:", program_name);
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 ++complain_message_count;
211 putc ('\n', stderr);
212 fflush (stderr);
213 }
214 \f
215 /*-------------------------------------------------.
216 | A severe error has occurred, we cannot proceed. |
217 `-------------------------------------------------*/
218
219 void
220 #if defined VA_START && defined __STDC__
221 fatal (const char *message, ...)
222 #else
223 fatal (message, va_alist)
224 char *message;
225 va_dcl
226 #endif
227 {
228 #ifdef VA_START
229 va_list args;
230 #endif
231
232 fflush (stdout);
233 if (infile != NULL)
234 fprintf (stderr, "%s:%d: ", infile, lineno);
235 else
236 fprintf (stderr, "%s:", program_name);
237
238 fputs (_("fatal error: "), stderr);
239
240 #ifdef VA_START
241 VA_START (args, message);
242 vfprintf (stderr, message, args);
243 va_end (args);
244 #else
245 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
246 #endif
247 putc ('\n', stderr);
248 fflush (stderr);
249 exit (1);
250 }
251 \f
252 /*------------------------------------------------------------------.
253 | A severe error has occurred, we cannot proceed. Exit with STATUS, |
254 | and report the error message of the errno ERRNUM. |
255 `------------------------------------------------------------------*/
256
257 void
258 #if defined VA_START && defined __STDC__
259 error (int status, int errnum,
260 const char *message, ...)
261 #else
262 error (status, errnum, message, va_alist)
263 int status;
264 int errnum;
265 char *message;
266 va_dcl
267 #endif
268 {
269 #ifdef VA_START
270 va_list args;
271 #endif
272
273 fflush (stdout);
274 if (infile != NULL)
275 fprintf (stderr, "%s:%d: ", infile, lineno);
276 else
277 fprintf (stderr, "%s:", program_name);
278
279 fputs (_("fatal error: "), stderr);
280
281 #ifdef VA_START
282 VA_START (args, message);
283 vfprintf (stderr, message, args);
284 va_end (args);
285 #else
286 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
287 #endif
288
289 if (errnum)
290 {
291 #if defined HAVE_STRERROR_R || _LIBC
292 char errbuf[1024];
293 # if HAVE_WORKING_STRERROR_R || _LIBC
294 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
295 # else
296 /* Don't use __strerror_r's return value because on some systems
297 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
298 __strerror_r (errnum, errbuf, sizeof errbuf);
299 fprintf (stderr, ": %s", errbuf);
300 # endif
301 #else
302 fprintf (stderr, ": %s", strerror (errnum));
303 #endif
304 }
305 putc ('\n', stderr);
306 fflush (stderr);
307 if (status)
308 exit (status);
309 }