]> git.saurik.com Git - bison.git/blame - src/complain.c
(xfopen, xfclose): Use EXIT_FAILURE rathern than 2.
[bison.git] / src / complain.c
CommitLineData
a0f6b076 1/* Declaration for error-reporting function for Bison.
52489d44 2 Copyright (C) 2000, 2001, 2002 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 23
21184140
PE
24#include <stdarg.h>
25#if ! (HAVE_VPRINTF || defined vfprintf)
26# define vfprintf(stream, message, args) _doprnt (message, args, stream)
a0f6b076
AD
27#endif
28
29#if STDC_HEADERS || _LIBC
30# include <stdlib.h>
31# include <string.h>
32#else
33void exit ();
34#endif
35
36#include "complain.h"
37
38#ifndef _
39# define _(String) String
40#endif
41
42#ifdef _LIBC
43/* In the GNU C library, there is a predefined variable for this. */
8f13fe33 44
a0f6b076 45# define program_name program_invocation_name
8f13fe33
AD
46# include <errno.h>
47
48/* In GNU libc we want do not want to use the common name `error' directly.
49 Instead make it a weak alias. */
50# define error __error
51# define error_at_line __error_at_line
52
53# ifdef USE_IN_LIBIO
54# include <libio/iolibio.h>
55# define fflush(s) _IO_fflush (s)
56# endif
57
a0f6b076 58#else /* not _LIBC */
8f13fe33 59
a0f6b076
AD
60/* The calling program should define program_name and set it to the
61 name of the executing program. */
62extern char *program_name;
8f13fe33 63
9ff012ca
PE
64# if HAVE_STRERROR
65# ifndef HAVE_DECL_STRERROR
66"this configure-time declaration test was not run"
67# endif
68# if !HAVE_DECL_STRERROR && !defined strerror
21184140 69char *strerror (int);
9ff012ca 70# endif
8f13fe33 71# else
8f13fe33 72static char *
21184140 73private_strerror (int errnum)
8f13fe33
AD
74{
75 extern char *sys_errlist[];
76 extern int sys_nerr;
77
78 if (errnum > 0 && errnum <= sys_nerr)
79 return _(sys_errlist[errnum]);
80 return _("Unknown system error");
81}
9ff012ca
PE
82# define strerror private_strerror
83# endif /* HAVE_STRERROR */
8f13fe33 84#endif /* not _LIBC */
a0f6b076 85
0ae6073a
PE
86/* This variable is set each time `warn' is called. */
87bool warning_issued;
a0f6b076 88
0ae6073a
PE
89/* This variable is set each time `complain' is called. */
90bool complaint_issued;
a0f6b076 91
a0f6b076
AD
92\f
93/*--------------------------------.
94| Report a warning, and proceed. |
95`--------------------------------*/
96
e9955c83 97void
ee000ba4 98warn_at (location_t location, const char *message, ...)
e9955c83 99{
e9955c83 100 va_list args;
e9955c83 101
e9955c83 102 fflush (stdout);
ee000ba4
AD
103 LOCATION_PRINT (stderr, location);
104 fputs (": ", stderr);
e9955c83
AD
105 fputs (_("warning: "), stderr);
106
21184140 107 va_start (args, message);
52489d44
AD
108 vfprintf (stderr, message, args);
109 va_end (args);
52489d44 110
0ae6073a 111 warning_issued = true;
52489d44
AD
112 putc ('\n', stderr);
113 fflush (stderr);
114}
115
116void
52489d44 117warn (const char *message, ...)
52489d44 118{
52489d44 119 va_list args;
52489d44
AD
120
121 fflush (stdout);
122 fprintf (stderr, "%s: %s", infile ? infile : program_name, _("warning: "));
123
21184140 124 va_start (args, message);
e9955c83
AD
125 vfprintf (stderr, message, args);
126 va_end (args);
e9955c83 127
0ae6073a 128 warning_issued = true;
e9955c83
AD
129 putc ('\n', stderr);
130 fflush (stderr);
131}
a0f6b076
AD
132\f
133/*-----------------------------------------------------------.
134| An error has occurred, but we can proceed, and die later. |
135`-----------------------------------------------------------*/
136
e9955c83 137void
ee000ba4 138complain_at (location_t location, const char *message, ...)
e9955c83 139{
e9955c83 140 va_list args;
e9955c83 141
e9955c83 142 fflush (stdout);
ee000ba4
AD
143 LOCATION_PRINT (stderr, location);
144 fputs (": ", stderr);
e9955c83 145
21184140 146 va_start (args, message);
52489d44
AD
147 vfprintf (stderr, message, args);
148 va_end (args);
52489d44 149
0ae6073a 150 complaint_issued = true;
52489d44
AD
151 putc ('\n', stderr);
152 fflush (stderr);
153}
154
155void
52489d44 156complain (const char *message, ...)
52489d44 157{
52489d44 158 va_list args;
52489d44
AD
159
160 fflush (stdout);
161 fprintf (stderr, "%s: ", infile ? infile : program_name);
162
21184140 163 va_start (args, message);
e9955c83
AD
164 vfprintf (stderr, message, args);
165 va_end (args);
e9955c83 166
0ae6073a 167 complaint_issued = true;
e9955c83
AD
168 putc ('\n', stderr);
169 fflush (stderr);
170}
a0f6b076
AD
171\f
172/*-------------------------------------------------.
173| A severe error has occurred, we cannot proceed. |
174`-------------------------------------------------*/
175
e9955c83 176void
ee000ba4 177fatal_at (location_t location, const char *message, ...)
e9955c83 178{
e9955c83 179 va_list args;
e9955c83
AD
180
181 fflush (stdout);
ee000ba4
AD
182 LOCATION_PRINT (stderr, location);
183 fputs (": ", stderr);
e9955c83
AD
184 fputs (_("fatal error: "), stderr);
185
21184140 186 va_start (args, message);
e9955c83
AD
187 vfprintf (stderr, message, args);
188 va_end (args);
e9955c83
AD
189 putc ('\n', stderr);
190 fflush (stderr);
0ae6073a 191 exit (EXIT_FAILURE);
e9955c83
AD
192}
193
a0f6b076 194void
a0f6b076 195fatal (const char *message, ...)
a0f6b076 196{
a0f6b076 197 va_list args;
a0f6b076
AD
198
199 fflush (stdout);
a5d50994 200 fprintf (stderr, "%s: ", infile ? infile : program_name);
a0f6b076
AD
201
202 fputs (_("fatal error: "), stderr);
203
21184140 204 va_start (args, message);
a0f6b076
AD
205 vfprintf (stderr, message, args);
206 va_end (args);
a0f6b076
AD
207 putc ('\n', stderr);
208 fflush (stderr);
0ae6073a 209 exit (EXIT_FAILURE);
a0f6b076 210}