]>
Commit | Line | Data |
---|---|---|
a0f6b076 AD |
1 | /* Declaration for error-reporting function for Bison. |
2 | Copyright (C) 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 | #ifdef HAVE_CONFIG_H | |
23 | # include <config.h> | |
24 | #endif | |
25 | ||
26 | #include <stdio.h> | |
27 | ||
28 | #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC | |
29 | # if __STDC__ | |
30 | # include <stdarg.h> | |
31 | # define VA_START(args, lastarg) va_start(args, lastarg) | |
32 | # else | |
33 | # include <varargs.h> | |
34 | # define VA_START(args, lastarg) va_start(args) | |
35 | # endif | |
36 | #else | |
37 | # define va_alist a1, a2, a3, a4, a5, a6, a7, a8 | |
38 | # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; | |
39 | #endif | |
40 | ||
41 | #if STDC_HEADERS || _LIBC | |
42 | # include <stdlib.h> | |
43 | # include <string.h> | |
44 | #else | |
45 | void exit (); | |
46 | #endif | |
47 | ||
48 | #include "complain.h" | |
49 | ||
50 | #ifndef _ | |
51 | # define _(String) String | |
52 | #endif | |
53 | ||
54 | #ifdef _LIBC | |
55 | /* In the GNU C library, there is a predefined variable for this. */ | |
56 | # define program_name program_invocation_name | |
57 | #else /* not _LIBC */ | |
58 | /* The calling program should define program_name and set it to the | |
59 | name of the executing program. */ | |
60 | extern char *program_name; | |
61 | #endif | |
62 | ||
63 | /* This variable is incremented each time `warn' is called. */ | |
64 | unsigned int warn_message_count; | |
65 | ||
66 | /* This variable is incremented each time `complain' is called. */ | |
67 | unsigned int complain_message_count; | |
68 | ||
69 | /* Sometimes we want to have at most one error per line. This | |
70 | variable controls whether this mode is selected or not. */ | |
71 | int error_one_per_line; | |
72 | \f | |
73 | /*--------------------------------. | |
74 | | Report a warning, and proceed. | | |
75 | `--------------------------------*/ | |
76 | ||
77 | void | |
78 | #if defined VA_START && __STDC__ | |
79 | warn (const char *message, ...) | |
80 | #else | |
81 | warn (message, va_alist) | |
82 | char *message; | |
83 | va_dcl | |
84 | #endif | |
85 | { | |
86 | #ifdef VA_START | |
87 | va_list args; | |
88 | #endif | |
89 | ||
90 | if (error_one_per_line) | |
91 | { | |
92 | static const char *old_infile; | |
4a120d45 | 93 | static int old_lineno; |
a0f6b076 AD |
94 | |
95 | if (old_lineno == lineno && | |
96 | (infile == old_infile || !strcmp (old_infile, infile))) | |
97 | /* Simply return and print nothing. */ | |
98 | return; | |
99 | ||
100 | old_infile = infile; | |
101 | old_lineno = lineno; | |
102 | } | |
103 | ||
104 | fflush (stdout); | |
105 | if (infile != NULL) | |
106 | fprintf (stderr, "%s:%d: ", infile, lineno); | |
107 | else | |
108 | fprintf (stderr, "%s:", program_name); | |
109 | ||
110 | fputs (_("warning: "), stderr); | |
111 | ||
112 | #ifdef VA_START | |
113 | VA_START (args, message); | |
114 | vfprintf (stderr, message, args); | |
115 | va_end (args); | |
116 | #else | |
117 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); | |
118 | #endif | |
119 | ||
120 | ++warn_message_count; | |
121 | putc ('\n', stderr); | |
122 | fflush (stderr); | |
123 | } | |
124 | \f | |
125 | /*-----------------------------------------------------------. | |
126 | | An error has occurred, but we can proceed, and die later. | | |
127 | `-----------------------------------------------------------*/ | |
128 | ||
129 | void | |
130 | #if defined VA_START && __STDC__ | |
131 | complain (const char *message, ...) | |
132 | #else | |
133 | complain (message, va_alist) | |
134 | char *message; | |
135 | va_dcl | |
136 | #endif | |
137 | { | |
138 | #ifdef VA_START | |
139 | va_list args; | |
140 | #endif | |
141 | ||
142 | if (error_one_per_line) | |
143 | { | |
144 | static const char *old_infile; | |
4a120d45 | 145 | static int old_lineno; |
a0f6b076 AD |
146 | |
147 | if (old_lineno == lineno && | |
148 | (infile == old_infile || !strcmp (old_infile, infile))) | |
149 | /* Simply return and print nothing. */ | |
150 | return; | |
151 | ||
152 | old_infile = infile; | |
153 | old_lineno = lineno; | |
154 | } | |
155 | ||
156 | fflush (stdout); | |
157 | if (infile != NULL) | |
158 | fprintf (stderr, "%s:%d: ", infile, lineno); | |
159 | else | |
160 | fprintf (stderr, "%s:", program_name); | |
161 | ||
162 | #ifdef VA_START | |
163 | VA_START (args, message); | |
164 | vfprintf (stderr, message, args); | |
165 | va_end (args); | |
166 | #else | |
167 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); | |
168 | #endif | |
169 | ||
170 | ++complain_message_count; | |
171 | putc ('\n', stderr); | |
172 | fflush (stderr); | |
173 | } | |
174 | \f | |
175 | /*-------------------------------------------------. | |
176 | | A severe error has occurred, we cannot proceed. | | |
177 | `-------------------------------------------------*/ | |
178 | ||
179 | void | |
180 | #if defined VA_START && __STDC__ | |
181 | fatal (const char *message, ...) | |
182 | #else | |
183 | fatal (message, va_alist) | |
184 | char *message; | |
185 | va_dcl | |
186 | #endif | |
187 | { | |
188 | #ifdef VA_START | |
189 | va_list args; | |
190 | #endif | |
191 | ||
192 | fflush (stdout); | |
193 | if (infile != NULL) | |
194 | fprintf (stderr, "%s:%d: ", infile, lineno); | |
195 | else | |
196 | fprintf (stderr, "%s:", program_name); | |
197 | ||
198 | fputs (_("fatal error: "), stderr); | |
199 | ||
200 | #ifdef VA_START | |
201 | VA_START (args, message); | |
202 | vfprintf (stderr, message, args); | |
203 | va_end (args); | |
204 | #else | |
205 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); | |
206 | #endif | |
207 | putc ('\n', stderr); | |
208 | fflush (stderr); | |
209 | exit (1); | |
210 | } | |
211 | \f | |
212 | /*------------------------------------------------------------------. | |
213 | | A severe error has occurred, we cannot proceed. Exit with STATUS, | | |
214 | | and report the error message of the errno ERRNUM. | | |
215 | `------------------------------------------------------------------*/ | |
216 | ||
217 | void | |
218 | #if defined VA_START && __STDC__ | |
219 | error (int status, int errnum, | |
220 | const char *message, ...) | |
221 | #else | |
222 | error (status, errnum, message, va_alist) | |
223 | int status; | |
224 | int errnum; | |
225 | char *message; | |
226 | va_dcl | |
227 | #endif | |
228 | { | |
229 | #ifdef VA_START | |
230 | va_list args; | |
231 | #endif | |
232 | ||
233 | fflush (stdout); | |
234 | if (infile != NULL) | |
235 | fprintf (stderr, "%s:%d: ", infile, lineno); | |
236 | else | |
237 | fprintf (stderr, "%s:", program_name); | |
238 | ||
239 | fputs (_("fatal error: "), stderr); | |
240 | ||
241 | #ifdef VA_START | |
242 | VA_START (args, message); | |
243 | vfprintf (stderr, message, args); | |
244 | va_end (args); | |
245 | #else | |
246 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); | |
247 | #endif | |
248 | ||
249 | if (errnum) | |
250 | { | |
251 | #if defined HAVE_STRERROR_R || _LIBC | |
252 | char errbuf[1024]; | |
253 | # if HAVE_WORKING_STRERROR_R || _LIBC | |
254 | fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf)); | |
255 | # else | |
256 | /* Don't use __strerror_r's return value because on some systems | |
257 | (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */ | |
258 | __strerror_r (errnum, errbuf, sizeof errbuf); | |
259 | fprintf (stderr, ": %s", errbuf); | |
260 | # endif | |
261 | #else | |
262 | fprintf (stderr, ": %s", strerror (errnum)); | |
263 | #endif | |
264 | } | |
265 | putc ('\n', stderr); | |
266 | fflush (stderr); | |
267 | if (status) | |
268 | exit (status); | |
269 | } |