1 /* error.c -- Functions for handling errors. */
2 /* Copyright (C) 1993-2003 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)
50 # include "bashhist.h"
53 extern int executing_line_number __P((void));
55 extern char *shell_name;
56 #if defined (JOB_CONTROL)
57 extern pid_t shell_pgrp;
58 extern int give_terminal_to __P((pid_t, int));
59 #endif /* JOB_CONTROL */
61 #if defined (ARRAY_VARS)
62 extern char *bash_badsub_errmsg;
65 static void error_prolog __P((int));
67 /* The current maintainer of the shell. You change this in the
69 #if !defined (MAINTAINER)
70 #define MAINTAINER "bash-maintainers@gnu.org"
73 char *the_current_maintainer = MAINTAINER;
75 int gnu_error_format = 0;
78 error_prolog (print_lineno)
84 ename = get_name_for_error ();
85 line = (print_lineno && interactive_shell == 0) ? executing_line_number () : -1;
88 fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : " line ", line);
90 fprintf (stderr, "%s: ", ename);
93 /* Return the name of the shell or the shell script for error reporting. */
98 #if defined (ARRAY_VARS)
99 SHELL_VAR *bash_source_v;
100 ARRAY *bash_source_a;
104 if (interactive_shell == 0)
106 #if defined (ARRAY_VARS)
107 bash_source_v = find_variable ("BASH_SOURCE");
108 if (bash_source_v && array_p (bash_source_v) &&
109 (bash_source_a = array_cell (bash_source_v)))
110 name = array_reference (bash_source_a, 0);
113 name = dollar_vars[0];
115 if (name == 0 && shell_name && *shell_name)
116 name = base_pathname (shell_name);
118 #if defined (PROGRAM)
127 /* Report an error having to do with FILENAME. This does not use
128 sys_error so the filename is not interpreted as a printf-style
131 file_error (filename)
132 const char *filename;
134 report_error ("%s: %s", filename, strerror (errno));
138 #if defined (PREFER_STDARG)
139 programming_error (const char *format, ...)
141 programming_error (format, va_alist)
149 #if defined (JOB_CONTROL)
150 give_terminal_to (shell_pgrp, 0);
151 #endif /* JOB_CONTROL */
153 SH_VA_START (args, format);
155 vfprintf (stderr, format, args);
156 fprintf (stderr, "\n");
159 #if defined (HISTORY)
160 if (remember_on_history)
162 h = last_history_line ();
163 fprintf (stderr, _("last command: %s\n"), h ? h : "(null)");
168 fprintf (stderr, "Report this to %s\n", the_current_maintainer);
171 fprintf (stderr, _("Aborting..."));
177 /* Print an error message and, if `set -e' has been executed, exit the
178 shell. Used in this file by file_error and programming_error. Used
179 outside this file mostly to report substitution and expansion errors,
180 and for bad invocation options. */
182 #if defined (PREFER_STDARG)
183 report_error (const char *format, ...)
185 report_error (format, va_alist)
194 SH_VA_START (args, format);
196 vfprintf (stderr, format, args);
197 fprintf (stderr, "\n");
200 if (exit_immediately_on_error)
205 #if defined (PREFER_STDARG)
206 fatal_error (const char *format, ...)
208 fatal_error (format, va_alist)
217 SH_VA_START (args, format);
219 vfprintf (stderr, format, args);
220 fprintf (stderr, "\n");
227 #if defined (PREFER_STDARG)
228 internal_error (const char *format, ...)
230 internal_error (format, va_alist)
239 SH_VA_START (args, format);
241 vfprintf (stderr, format, args);
242 fprintf (stderr, "\n");
248 #if defined (PREFER_STDARG)
249 internal_warning (const char *format, ...)
251 internal_warning (format, va_alist)
258 fprintf (stderr, _("%s: warning: "), get_name_for_error ());
260 SH_VA_START (args, format);
262 vfprintf (stderr, format, args);
263 fprintf (stderr, "\n");
269 #if defined (PREFER_STDARG)
270 sys_error (const char *format, ...)
272 sys_error (format, va_alist)
283 SH_VA_START (args, format);
285 vfprintf (stderr, format, args);
286 fprintf (stderr, ": %s\n", strerror (e));
291 /* An error from the parser takes the general form
293 shell_name: input file name: line number: message
295 The input file name and line number are omitted if the shell is
296 currently interactive. If the shell is not currently interactive,
297 the input file name is inserted only if it is different from the
300 #if defined (PREFER_STDARG)
301 parser_error (int lineno, const char *format, ...)
303 parser_error (lineno, format, va_alist)
312 ename = get_name_for_error ();
313 iname = yy_input_name ();
316 fprintf (stderr, "%s: ", ename);
317 else if (interactive_shell)
318 fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : " line ", lineno);
319 else if (STREQ (ename, iname))
320 fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : " line ", lineno);
322 fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : " line ", lineno);
324 SH_VA_START (args, format);
326 vfprintf (stderr, format, args);
327 fprintf (stderr, "\n");
331 if (exit_immediately_on_error)
337 #if defined (PREFER_STDARG)
338 itrace (const char *format, ...)
340 itrace (format, va_alist)
347 fprintf(stderr, "TRACE: pid %ld: ", (long)getpid());
349 SH_VA_START (args, format);
351 vfprintf (stderr, format, args);
352 fprintf (stderr, "\n");
359 /* A trace function for silent debugging -- doesn't require a control
362 #if defined (PREFER_STDARG)
363 trace (const char *format, ...)
365 trace (format, va_alist)
371 static FILE *tracefp = (FILE *)NULL;
374 tracefp = fopen("/tmp/bash-trace.log", "a+");
379 fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
381 fprintf(tracefp, "TRACE: pid %ld: ", (long)getpid());
383 SH_VA_START (args, format);
385 vfprintf (tracefp, format, args);
386 fprintf (tracefp, "\n");
395 /* **************************************************************** */
397 /* Common error reporting */
399 /* **************************************************************** */
402 static char *cmd_error_table[] = {
403 N_("unknown command error"), /* CMDERR_DEFAULT */
404 N_("bad command type"), /* CMDERR_BADTYPE */
405 N_("bad connector"), /* CMDERR_BADCONN */
406 N_("bad jump"), /* CMDERR_BADJUMP */
411 command_error (func, code, e, flags)
413 int code, e, flags; /* flags currently unused */
415 if (code > CMDERR_LAST)
416 code = CMDERR_DEFAULT;
418 programming_error ("%s: %s: %d", func, _(cmd_error_table[code]), e);
422 command_errstr (code)
425 if (code > CMDERR_LAST)
426 code = CMDERR_DEFAULT;
428 return (_(cmd_error_table[code]));
436 report_error ("%s: %s", s, _(bash_badsub_errmsg));
444 report_error (_("%s: unbound variable"), s);
451 report_error (_("%s: readonly variable"), s);