barvinok_enumerate: use argp
[barvinok.git] / lib / error.c
blobcf8634332020392497c23a438eef02aaaa0c71eb
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2005, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21 #if !_LIBC
22 # include <config.h>
23 #endif
25 #include "error.h"
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #if !_LIBC && ENABLE_NLS
33 # include "gettext.h"
34 #endif
36 #ifdef _LIBC
37 # include <libintl.h>
38 # include <stdbool.h>
39 # include <stdint.h>
40 # include <wchar.h>
41 # define mbsrtowcs __mbsrtowcs
42 #endif
44 #if USE_UNLOCKED_IO
45 # include "unlocked-io.h"
46 #endif
48 #ifndef _
49 # define _(String) String
50 #endif
52 /* If NULL, error will flush stdout, then print on stderr the program
53 name, a colon and a space. Otherwise, error will call this
54 function without parameters instead. */
55 void (*error_print_progname) (void);
57 /* This variable is incremented each time `error' is called. */
58 unsigned int error_message_count;
60 #ifdef _LIBC
61 /* In the GNU C library, there is a predefined variable for this. */
63 # define program_name program_invocation_name
64 # include <errno.h>
65 # include <limits.h>
66 # include <libio/libioP.h>
68 /* In GNU libc we want do not want to use the common name `error' directly.
69 Instead make it a weak alias. */
70 extern void __error (int status, int errnum, const char *message, ...)
71 __attribute__ ((__format__ (__printf__, 3, 4)));
72 extern void __error_at_line (int status, int errnum, const char *file_name,
73 unsigned int line_number, const char *message,
74 ...)
75 __attribute__ ((__format__ (__printf__, 5, 6)));;
76 # define error __error
77 # define error_at_line __error_at_line
79 # include <libio/iolibio.h>
80 # define fflush(s) INTUSE(_IO_fflush) (s)
81 # undef putc
82 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
84 # include <bits/libc-lock.h>
86 #else /* not _LIBC */
88 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
89 # ifndef HAVE_DECL_STRERROR_R
90 "this configure-time declaration test was not run"
91 # endif
92 char *strerror_r ();
93 # endif
95 /* The calling program should define program_name and set it to the
96 name of the executing program. */
97 extern char *program_name;
99 # if HAVE_STRERROR_R || defined strerror_r
100 # define __strerror_r strerror_r
101 # endif /* HAVE_STRERROR_R || defined strerror_r */
102 #endif /* not _LIBC */
104 static void
105 print_errno_message (int errnum)
107 char const *s;
109 #if defined HAVE_STRERROR_R || _LIBC
110 char errbuf[1024];
111 # if STRERROR_R_CHAR_P || _LIBC
112 s = __strerror_r (errnum, errbuf, sizeof errbuf);
113 # else
114 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
115 s = errbuf;
116 else
117 s = 0;
118 # endif
119 #else
120 s = strerror (errnum);
121 #endif
123 #if !_LIBC
124 if (! s)
125 s = _("Unknown system error");
126 #endif
128 #if _LIBC
129 __fxprintf (NULL, ": %s", s);
130 #else
131 fprintf (stderr, ": %s", s);
132 #endif
135 static void
136 error_tail (int status, int errnum, const char *message, va_list args)
138 #if _LIBC
139 if (_IO_fwide (stderr, 0) > 0)
141 # define ALLOCA_LIMIT 2000
142 size_t len = strlen (message) + 1;
143 wchar_t *wmessage = NULL;
144 mbstate_t st;
145 size_t res;
146 const char *tmp;
147 bool use_malloc = false;
149 while (1)
151 if (__libc_use_alloca (len * sizeof (wchar_t)))
152 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
153 else
155 if (!use_malloc)
156 wmessage = NULL;
158 wchar_t *p = (wchar_t *) realloc (wmessage,
159 len * sizeof (wchar_t));
160 if (p == NULL)
162 free (wmessage);
163 fputws_unlocked (L"out of memory\n", stderr);
164 return;
166 wmessage = p;
167 use_malloc = true;
170 memset (&st, '\0', sizeof (st));
171 tmp = message;
173 res = mbsrtowcs (wmessage, &tmp, len, &st);
174 if (res != len)
175 break;
177 if (__builtin_expect (len >= SIZE_MAX / 2, 0))
179 /* This really should not happen if everything is fine. */
180 res = (size_t) -1;
181 break;
184 len *= 2;
187 if (res == (size_t) -1)
189 /* The string cannot be converted. */
190 if (use_malloc)
192 free (wmessage);
193 use_malloc = false;
195 wmessage = (wchar_t *) L"???";
198 __vfwprintf (stderr, wmessage, args);
200 if (use_malloc)
201 free (wmessage);
203 else
204 #endif
205 vfprintf (stderr, message, args);
206 va_end (args);
208 ++error_message_count;
209 if (errnum)
210 print_errno_message (errnum);
211 #if _LIBC
212 __fxprintf (NULL, "\n");
213 #else
214 putc ('\n', stderr);
215 #endif
216 fflush (stderr);
217 if (status)
218 exit (status);
222 /* Print the program name and error message MESSAGE, which is a printf-style
223 format string with optional args.
224 If ERRNUM is nonzero, print its corresponding system error message.
225 Exit with status STATUS if it is nonzero. */
226 void
227 error (int status, int errnum, const char *message, ...)
229 va_list args;
231 #if defined _LIBC && defined __libc_ptf_call
232 /* We do not want this call to be cut short by a thread
233 cancellation. Therefore disable cancellation for now. */
234 int state = PTHREAD_CANCEL_ENABLE;
235 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
237 #endif
239 fflush (stdout);
240 #ifdef _LIBC
241 _IO_flockfile (stderr);
242 #endif
243 if (error_print_progname)
244 (*error_print_progname) ();
245 else
247 #if _LIBC
248 __fxprintf (NULL, "%s: ", program_name);
249 #else
250 fprintf (stderr, "%s: ", program_name);
251 #endif
254 va_start (args, message);
255 error_tail (status, errnum, message, args);
257 #ifdef _LIBC
258 _IO_funlockfile (stderr);
259 # ifdef __libc_ptf_call
260 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
261 # endif
262 #endif
265 /* Sometimes we want to have at most one error per line. This
266 variable controls whether this mode is selected or not. */
267 int error_one_per_line;
269 void
270 error_at_line (int status, int errnum, const char *file_name,
271 unsigned int line_number, const char *message, ...)
273 va_list args;
275 if (error_one_per_line)
277 static const char *old_file_name;
278 static unsigned int old_line_number;
280 if (old_line_number == line_number
281 && (file_name == old_file_name
282 || strcmp (old_file_name, file_name) == 0))
283 /* Simply return and print nothing. */
284 return;
286 old_file_name = file_name;
287 old_line_number = line_number;
290 #if defined _LIBC && defined __libc_ptf_call
291 /* We do not want this call to be cut short by a thread
292 cancellation. Therefore disable cancellation for now. */
293 int state = PTHREAD_CANCEL_ENABLE;
294 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
296 #endif
298 fflush (stdout);
299 #ifdef _LIBC
300 _IO_flockfile (stderr);
301 #endif
302 if (error_print_progname)
303 (*error_print_progname) ();
304 else
306 #if _LIBC
307 __fxprintf (NULL, "%s:", program_name);
308 #else
309 fprintf (stderr, "%s:", program_name);
310 #endif
313 #if _LIBC
314 __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
315 file_name, line_number);
316 #else
317 fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
318 file_name, line_number);
319 #endif
321 va_start (args, message);
322 error_tail (status, errnum, message, args);
324 #ifdef _LIBC
325 _IO_funlockfile (stderr);
326 # ifdef __libc_ptf_call
327 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
328 # endif
329 #endif
332 #ifdef _LIBC
333 /* Make the weak alias. */
334 # undef error
335 # undef error_at_line
336 weak_alias (__error, error)
337 weak_alias (__error_at_line, error_at_line)
338 #endif