1 /* error.c -- Functions for handling errors. */
2 /* Copyright (C) 1993 Free Software Foundation, Inc.
4 This file is part of GNU Bash, the Bourne Again SHell.
6 Bash is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License along
17 with Bash; see the file COPYING. If not, write to the Free Software
18 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #include "bashtypes.h"
25 #if defined (HAVE_UNISTD_H)
29 #if defined (PREFER_STDARG)
51 # include "bashhist.h"
54 extern int executing_line_number
__P((void));
56 extern int interactive_shell
, interactive
, startup_state
;
57 extern char *dollar_vars
[];
58 extern char *shell_name
;
59 #if defined (JOB_CONTROL)
60 extern pid_t shell_pgrp
;
61 extern int give_terminal_to
__P((pid_t
, int));
62 #endif /* JOB_CONTROL */
64 static void error_prolog
__P((int));
66 /* The current maintainer of the shell. You change this in the
68 #if !defined (MAINTAINER)
69 #define MAINTAINER "bash-maintainers@gnu.org"
72 char *the_current_maintainer
= MAINTAINER
;
75 error_prolog (print_lineno
)
80 fprintf (stderr
, "%s: ", get_name_for_error ());
82 if (print_lineno
&& interactive_shell
== 0)
84 line
= executing_line_number ();
86 fprintf (stderr
, "line %d: ", line
);
90 /* Return the name of the shell or the shell script for error reporting. */
97 if (interactive_shell
== 0)
98 name
= dollar_vars
[0];
99 if (name
== 0 && shell_name
&& *shell_name
)
100 name
= base_pathname (shell_name
);
102 #if defined (PROGRAM)
111 /* Report an error having to do with FILENAME. This does not use
112 sys_error so the filename is not interpreted as a printf-style
115 file_error (filename
)
116 const char *filename
;
118 report_error ("%s: %s", filename
, strerror (errno
));
122 #if defined (PREFER_STDARG)
123 programming_error (const char *format
, ...)
125 programming_error (format
, va_alist
)
133 #if defined (JOB_CONTROL)
134 give_terminal_to (shell_pgrp
, 0);
135 #endif /* JOB_CONTROL */
137 SH_VA_START (args
, format
);
139 vfprintf (stderr
, format
, args
);
140 fprintf (stderr
, "\n");
143 #if defined (HISTORY)
144 if (remember_on_history
)
146 h
= last_history_line ();
147 fprintf (stderr
, "last command: %s\n", h
? h
: "(null)");
152 fprintf (stderr
, "Report this to %s\n", the_current_maintainer
);
155 fprintf (stderr
, "Stopping myself...");
161 /* Print an error message and, if `set -e' has been executed, exit the
162 shell. Used in this file by file_error and programming_error. Used
163 outside this file mostly to report substitution and expansion errors,
164 and for bad invocation options. */
166 #if defined (PREFER_STDARG)
167 report_error (const char *format
, ...)
169 report_error (format
, va_alist
)
178 SH_VA_START (args
, format
);
180 vfprintf (stderr
, format
, args
);
181 fprintf (stderr
, "\n");
184 if (exit_immediately_on_error
)
189 #if defined (PREFER_STDARG)
190 fatal_error (const char *format
, ...)
192 fatal_error (format
, va_alist
)
201 SH_VA_START (args
, format
);
203 vfprintf (stderr
, format
, args
);
204 fprintf (stderr
, "\n");
211 #if defined (PREFER_STDARG)
212 internal_error (const char *format
, ...)
214 internal_error (format
, va_alist
)
223 SH_VA_START (args
, format
);
225 vfprintf (stderr
, format
, args
);
226 fprintf (stderr
, "\n");
232 #if defined (PREFER_STDARG)
233 internal_warning (const char *format
, ...)
235 internal_warning (format
, va_alist
)
242 fprintf (stderr
, "%s: warning: ", get_name_for_error ());
244 SH_VA_START (args
, format
);
246 vfprintf (stderr
, format
, args
);
247 fprintf (stderr
, "\n");
253 #if defined (PREFER_STDARG)
254 sys_error (const char *format
, ...)
256 sys_error (format
, va_alist
)
267 SH_VA_START (args
, format
);
269 vfprintf (stderr
, format
, args
);
270 fprintf (stderr
, ": %s\n", strerror (e
));
275 /* An error from the parser takes the general form
277 shell_name: input file name: line number: message
279 The input file name and line number are omitted if the shell is
280 currently interactive. If the shell is not currently interactive,
281 the input file name is inserted only if it is different from the
284 #if defined (PREFER_STDARG)
285 parser_error (int lineno
, const char *format
, ...)
287 parser_error (lineno
, format
, va_alist
)
296 ename
= get_name_for_error ();
297 iname
= yy_input_name ();
300 fprintf (stderr
, "%s: ", ename
);
301 else if (interactive_shell
)
302 fprintf (stderr
, "%s: %s: line %d: ", ename
, iname
, lineno
);
303 else if (STREQ (ename
, iname
))
304 fprintf (stderr
, "%s: line %d: ", ename
, lineno
);
306 fprintf (stderr
, "%s: %s: line %d: ", ename
, iname
, lineno
);
308 SH_VA_START (args
, format
);
310 vfprintf (stderr
, format
, args
);
311 fprintf (stderr
, "\n");
315 if (exit_immediately_on_error
)
321 #if defined (PREFER_STDARG)
322 itrace (const char *format
, ...)
324 itrace (format
, va_alist
)
331 fprintf(stderr
, "TRACE: pid %ld: ", (long)getpid());
333 SH_VA_START (args
, format
);
335 vfprintf (stderr
, format
, args
);
336 fprintf (stderr
, "\n");
343 /* A trace function for silent debugging -- doesn't require a control
346 #if defined (PREFER_STDARG)
347 trace (const char *format
, ...)
349 trace (format
, va_alist
)
355 static FILE *tracefp
= (FILE *)NULL
;
358 tracefp
= fopen("/tmp/bash-trace.log", "a+");
363 fcntl (fileno (tracefp
), F_SETFD
, 1); /* close-on-exec */
365 fprintf(tracefp
, "TRACE: pid %ld: ", (long)getpid());
367 SH_VA_START (args
, format
);
369 vfprintf (tracefp
, format
, args
);
370 fprintf (tracefp
, "\n");
379 /* **************************************************************** */
381 /* Common error reporting */
383 /* **************************************************************** */
386 static char *cmd_error_table
[] = {
387 "unknown command error", /* CMDERR_DEFAULT */
388 "bad command type", /* CMDERR_BADTYPE */
389 "bad connector", /* CMDERR_BADCONN */
390 "bad jump", /* CMDERR_BADJUMP */
395 command_error (func
, code
, e
, flags
)
397 int code
, e
, flags
; /* flags currently unused */
399 if (code
> CMDERR_LAST
)
400 code
= CMDERR_DEFAULT
;
402 programming_error ("%s: %s: %d", func
, cmd_error_table
[code
], e
);
406 command_errstr (code
)
409 if (code
> CMDERR_LAST
)
410 code
= CMDERR_DEFAULT
;
412 return (cmd_error_table
[code
]);
420 report_error ("%s: bad array subscript", s
);
428 report_error ("%s: unbound variable", s
);
435 report_error ("%s: readonly variable", s
);