1 /* error.c -- Functions for handling errors. */
3 /* Copyright (C) 1993-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include "bashtypes.h"
26 #if defined (HAVE_UNISTD_H)
30 #if defined (PREFER_STDARG)
51 # include "bashhist.h"
54 extern int executing_line_number __P((void));
56 extern int last_command_exit_value;
57 extern char *shell_name;
58 #if defined (JOB_CONTROL)
59 extern pid_t shell_pgrp;
60 extern int give_terminal_to __P((pid_t, int));
61 #endif /* JOB_CONTROL */
63 #if defined (ARRAY_VARS)
64 extern const char * const bash_badsub_errmsg;
67 static void error_prolog __P((int));
69 /* The current maintainer of the shell. You change this in the
71 #if !defined (MAINTAINER)
72 #define MAINTAINER "bash-maintainers@gnu.org"
75 const char * const the_current_maintainer = MAINTAINER;
77 int gnu_error_format = 0;
80 error_prolog (print_lineno)
86 ename = get_name_for_error ();
87 line = (print_lineno && interactive_shell == 0) ? executing_line_number () : -1;
90 fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : _(" line "), line);
92 fprintf (stderr, "%s: ", ename);
95 /* Return the name of the shell or the shell script for error reporting. */
100 #if defined (ARRAY_VARS)
101 SHELL_VAR *bash_source_v;
102 ARRAY *bash_source_a;
106 if (interactive_shell == 0)
108 #if defined (ARRAY_VARS)
109 bash_source_v = find_variable ("BASH_SOURCE");
110 if (bash_source_v && array_p (bash_source_v) &&
111 (bash_source_a = array_cell (bash_source_v)))
112 name = array_reference (bash_source_a, 0);
113 if (name == 0 || *name == '\0') /* XXX - was just name == 0 */
115 name = dollar_vars[0];
117 if (name == 0 && shell_name && *shell_name)
118 name = base_pathname (shell_name);
120 #if defined (PROGRAM)
129 /* Report an error having to do with FILENAME. This does not use
130 sys_error so the filename is not interpreted as a printf-style
133 file_error (filename)
134 const char *filename;
136 report_error ("%s: %s", filename, strerror (errno));
140 #if defined (PREFER_STDARG)
141 programming_error (const char *format, ...)
143 programming_error (format, va_alist)
151 #if defined (JOB_CONTROL)
152 give_terminal_to (shell_pgrp, 0);
153 #endif /* JOB_CONTROL */
155 SH_VA_START (args, format);
157 vfprintf (stderr, format, args);
158 fprintf (stderr, "\n");
161 #if defined (HISTORY)
162 if (remember_on_history)
164 h = last_history_line ();
165 fprintf (stderr, _("last command: %s\n"), h ? h : "(null)");
170 fprintf (stderr, "Report this to %s\n", the_current_maintainer);
173 fprintf (stderr, _("Aborting..."));
179 /* Print an error message and, if `set -e' has been executed, exit the
180 shell. Used in this file by file_error and programming_error. Used
181 outside this file mostly to report substitution and expansion errors,
182 and for bad invocation options. */
184 #if defined (PREFER_STDARG)
185 report_error (const char *format, ...)
187 report_error (format, va_alist)
196 SH_VA_START (args, format);
198 vfprintf (stderr, format, args);
199 fprintf (stderr, "\n");
202 if (exit_immediately_on_error)
204 if (last_command_exit_value == 0)
205 last_command_exit_value = 1;
206 exit_shell (last_command_exit_value);
211 #if defined (PREFER_STDARG)
212 fatal_error (const char *format, ...)
214 fatal_error (format, va_alist)
223 SH_VA_START (args, format);
225 vfprintf (stderr, format, args);
226 fprintf (stderr, "\n");
233 #if defined (PREFER_STDARG)
234 internal_error (const char *format, ...)
236 internal_error (format, va_alist)
245 SH_VA_START (args, format);
247 vfprintf (stderr, format, args);
248 fprintf (stderr, "\n");
254 #if defined (PREFER_STDARG)
255 internal_warning (const char *format, ...)
257 internal_warning (format, va_alist)
265 fprintf (stderr, _("warning: "));
267 SH_VA_START (args, format);
269 vfprintf (stderr, format, args);
270 fprintf (stderr, "\n");
276 #if defined (PREFER_STDARG)
277 sys_error (const char *format, ...)
279 sys_error (format, va_alist)
290 SH_VA_START (args, format);
292 vfprintf (stderr, format, args);
293 fprintf (stderr, ": %s\n", strerror (e));
298 /* An error from the parser takes the general form
300 shell_name: input file name: line number: message
302 The input file name and line number are omitted if the shell is
303 currently interactive. If the shell is not currently interactive,
304 the input file name is inserted only if it is different from the
307 #if defined (PREFER_STDARG)
308 parser_error (int lineno, const char *format, ...)
310 parser_error (lineno, format, va_alist)
319 ename = get_name_for_error ();
320 iname = yy_input_name ();
323 fprintf (stderr, "%s: ", ename);
324 else if (interactive_shell)
325 fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : _(" line "), lineno);
326 else if (STREQ (ename, iname))
327 fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : _(" line "), lineno);
329 fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : _(" line "), lineno);
331 SH_VA_START (args, format);
333 vfprintf (stderr, format, args);
334 fprintf (stderr, "\n");
338 if (exit_immediately_on_error)
339 exit_shell (last_command_exit_value = 2);
344 #if defined (PREFER_STDARG)
345 itrace (const char *format, ...)
347 itrace (format, va_alist)
354 fprintf(stderr, "TRACE: pid %ld: ", (long)getpid());
356 SH_VA_START (args, format);
358 vfprintf (stderr, format, args);
359 fprintf (stderr, "\n");
366 /* A trace function for silent debugging -- doesn't require a control
369 #if defined (PREFER_STDARG)
370 trace (const char *format, ...)
372 trace (format, va_alist)
378 static FILE *tracefp = (FILE *)NULL;
381 tracefp = fopen("/tmp/bash-trace.log", "a+");
386 fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
388 fprintf(tracefp, "TRACE: pid %ld: ", (long)getpid());
390 SH_VA_START (args, format);
392 vfprintf (tracefp, format, args);
393 fprintf (tracefp, "\n");
402 /* **************************************************************** */
404 /* Common error reporting */
406 /* **************************************************************** */
409 static const char * const cmd_error_table[] = {
410 N_("unknown command error"), /* CMDERR_DEFAULT */
411 N_("bad command type"), /* CMDERR_BADTYPE */
412 N_("bad connector"), /* CMDERR_BADCONN */
413 N_("bad jump"), /* CMDERR_BADJUMP */
418 command_error (func, code, e, flags)
420 int code, e, flags; /* flags currently unused */
422 if (code > CMDERR_LAST)
423 code = CMDERR_DEFAULT;
425 programming_error ("%s: %s: %d", func, _(cmd_error_table[code]), e);
429 command_errstr (code)
432 if (code > CMDERR_LAST)
433 code = CMDERR_DEFAULT;
435 return (_(cmd_error_table[code]));
443 report_error ("%s: %s", s, _(bash_badsub_errmsg));
451 report_error (_("%s: unbound variable"), s);
458 report_error (_("%s: readonly variable"), s);