]> git.saurik.com Git - bison.git/blame - src/complain.c
* src/Makefile.am: Add bison.c++.
[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
117void
342b8b6e 118#if defined VA_START && defined __STDC__
a0f6b076
AD
119warn (const char *message, ...)
120#else
121warn (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;
4a120d45 133 static int old_lineno;
a0f6b076
AD
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
169void
342b8b6e 170#if defined VA_START && defined __STDC__
a0f6b076
AD
171complain (const char *message, ...)
172#else
173complain (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;
4a120d45 185 static int old_lineno;
a0f6b076
AD
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
219void
342b8b6e 220#if defined VA_START && defined __STDC__
a0f6b076
AD
221fatal (const char *message, ...)
222#else
223fatal (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}