libitize
[gnulib.git] / lib / error.c
1 /* error.c -- error handler for noninteractive utilities
2    Copyright (C) 1990, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any 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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25
26 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
27 # if __STDC__
28 #  include <stdarg.h>
29 #  define VA_START(args, lastarg) va_start(args, lastarg)
30 # else
31 #  include <varargs.h>
32 #  define VA_START(args, lastarg) va_start(args)
33 # endif
34 #else
35 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
36 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
37 #endif
38
39 #if STDC_HEADERS || _LIBC
40 # include <stdlib.h>
41 # include <string.h>
42 #else
43 void exit ();
44 #endif
45
46 #ifndef _
47 # define _(String) String
48 #endif
49
50 /* Get prototypes for the functions defined here.  */
51 #include <error.h>
52
53 /* If NULL, error will flush stdout, then print on stderr the program
54    name, a colon and a space.  Otherwise, error will call this
55    function without parameters instead.  */
56 void (*error_print_progname) (
57 #if __STDC__ - 0
58                               void
59 #endif
60                               );
61
62 /* This variable is incremented each time `error' is called.  */
63 unsigned int error_message_count;
64
65 #ifdef _LIBC
66 /* In the GNU C library, there is a predefined variable for this.  */
67
68 # define program_name program_invocation_name
69 # include <errno.h>
70
71 #else /* not _LIBC */
72
73 /* The calling program should define program_name and set it to the
74    name of the executing program.  */
75 extern char *program_name;
76
77 # if HAVE_STRERROR
78 #  ifndef strerror              /* On some systems, strerror is a macro */
79 char *strerror ();
80 #  endif
81 # else
82 static char *
83 private_strerror (errnum)
84      int errnum;
85 {
86   extern char *sys_errlist[];
87   extern int sys_nerr;
88
89   if (errnum > 0 && errnum <= sys_nerr)
90     return sys_errlist[errnum];
91   return _("Unknown system error");
92 }
93 #  define strerror private_strerror
94 # endif /* HAVE_STRERROR */
95
96 #endif  /* not _LIBC */
97
98 /* Print the program name and error message MESSAGE, which is a printf-style
99    format string with optional args.
100    If ERRNUM is nonzero, print its corresponding system error message.
101    Exit with status STATUS if it is nonzero.  */
102 /* VARARGS */
103
104 void
105 #if defined(VA_START) && __STDC__
106 error (int status, int errnum, const char *message, ...)
107 #else
108 error (status, errnum, message, va_alist)
109      int status;
110      int errnum;
111      char *message;
112      va_dcl
113 #endif
114 {
115 #ifdef VA_START
116   va_list args;
117 #endif
118
119   if (error_print_progname)
120     (*error_print_progname) ();
121   else
122     {
123       fflush (stdout);
124       fprintf (stderr, "%s: ", program_name);
125     }
126
127 #ifdef VA_START
128   VA_START (args, message);
129 # if HAVE_VPRINTF || _LIBC
130   vfprintf (stderr, message, args);
131 # else
132   _doprnt (message, args, stderr);
133 # endif
134   va_end (args);
135 #else
136   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
137 #endif
138
139   ++error_message_count;
140   if (errnum)
141     fprintf (stderr, ": %s", strerror (errnum));
142   putc ('\n', stderr);
143   fflush (stderr);
144   if (status)
145     exit (status);
146 }
147 \f
148 /* Sometimes we want to have at most one error per line.  This
149    variable controls whether this mode is selected or not.  */
150 int error_one_per_line;
151
152 void
153 #if defined(VA_START) && __STDC__
154 error_at_line (int status, int errnum, const char *file_name,
155                unsigned int line_number, const char *message, ...)
156 #else
157 error_at_line (status, errnum, file_name, line_number, message, va_alist)
158      int status;
159      int errnum;
160      const char *file_name;
161      unsigned int line_number;
162      char *message;
163      va_dcl
164 #endif
165 {
166 #ifdef VA_START
167   va_list args;
168 #endif
169
170   if (error_one_per_line)
171     {
172       static const char *old_file_name;
173       static unsigned int old_line_number;
174
175       if (old_line_number == line_number &&
176           (file_name == old_file_name || !strcmp (old_file_name, file_name)))
177         /* Simply return and print nothing.  */
178         return;
179
180       old_file_name = file_name;
181       old_line_number = line_number;
182     }
183
184   if (error_print_progname)
185     (*error_print_progname) ();
186   else
187     {
188       fflush (stdout);
189       fprintf (stderr, "%s:", program_name);
190     }
191
192   if (file_name != NULL)
193     fprintf (stderr, "%s:%d: ", file_name, line_number);
194
195 #ifdef VA_START
196   VA_START (args, message);
197 # if HAVE_VPRINTF || _LIBC
198   vfprintf (stderr, message, args);
199 # else
200   _doprnt (message, args, stderr);
201 # endif
202   va_end (args);
203 #else
204   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
205 #endif
206
207   ++error_message_count;
208   if (errnum)
209     fprintf (stderr, ": %s", strerror (errnum));
210   putc ('\n', stderr);
211   fflush (stderr);
212   if (status)
213     exit (status);
214 }