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 int interactive_shell, interactive, startup_state;
56 extern char *dollar_vars[];
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 char *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 char *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);
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)
207 #if defined (PREFER_STDARG)
208 fatal_error (const char *format, ...)
210 fatal_error (format, va_alist)
219 SH_VA_START (args, format);
221 vfprintf (stderr, format, args);
222 fprintf (stderr, "\n");
229 #if defined (PREFER_STDARG)
230 internal_error (const char *format, ...)
232 internal_error (format, va_alist)
241 SH_VA_START (args, format);
243 vfprintf (stderr, format, args);
244 fprintf (stderr, "\n");
250 #if defined (PREFER_STDARG)
251 internal_warning (const char *format, ...)
253 internal_warning (format, va_alist)
260 fprintf (stderr, _("%s: warning: "), get_name_for_error ());
262 SH_VA_START (args, format);
264 vfprintf (stderr, format, args);
265 fprintf (stderr, "\n");
271 #if defined (PREFER_STDARG)
272 sys_error (const char *format, ...)
274 sys_error (format, va_alist)
285 SH_VA_START (args, format);
287 vfprintf (stderr, format, args);
288 fprintf (stderr, ": %s\n", strerror (e));
293 /* An error from the parser takes the general form
295 shell_name: input file name: line number: message
297 The input file name and line number are omitted if the shell is
298 currently interactive. If the shell is not currently interactive,
299 the input file name is inserted only if it is different from the
302 #if defined (PREFER_STDARG)
303 parser_error (int lineno, const char *format, ...)
305 parser_error (lineno, format, va_alist)
314 ename = get_name_for_error ();
315 iname = yy_input_name ();
318 fprintf (stderr, "%s: ", ename);
319 else if (interactive_shell)
320 fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : " line ", lineno);
321 else if (STREQ (ename, iname))
322 fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : " line ", lineno);
324 fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : " line ", lineno);
326 SH_VA_START (args, format);
328 vfprintf (stderr, format, args);
329 fprintf (stderr, "\n");
333 if (exit_immediately_on_error)
339 #if defined (PREFER_STDARG)
340 itrace (const char *format, ...)
342 itrace (format, va_alist)
349 fprintf(stderr, "TRACE: pid %ld: ", (long)getpid());
351 SH_VA_START (args, format);
353 vfprintf (stderr, format, args);
354 fprintf (stderr, "\n");
361 /* A trace function for silent debugging -- doesn't require a control
364 #if defined (PREFER_STDARG)
365 trace (const char *format, ...)
367 trace (format, va_alist)
373 static FILE *tracefp = (FILE *)NULL;
376 tracefp = fopen("/tmp/bash-trace.log", "a+");
381 fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
383 fprintf(tracefp, "TRACE: pid %ld: ", (long)getpid());
385 SH_VA_START (args, format);
387 vfprintf (tracefp, format, args);
388 fprintf (tracefp, "\n");
397 /* **************************************************************** */
399 /* Common error reporting */
401 /* **************************************************************** */
404 static char *cmd_error_table[] = {
405 N_("unknown command error"), /* CMDERR_DEFAULT */
406 N_("bad command type"), /* CMDERR_BADTYPE */
407 N_("bad connector"), /* CMDERR_BADCONN */
408 N_("bad jump"), /* CMDERR_BADJUMP */
413 command_error (func, code, e, flags)
415 int code, e, flags; /* flags currently unused */
417 if (code > CMDERR_LAST)
418 code = CMDERR_DEFAULT;
420 programming_error ("%s: %s: %d", func, _(cmd_error_table[code]), e);
424 command_errstr (code)
427 if (code > CMDERR_LAST)
428 code = CMDERR_DEFAULT;
430 return (_(cmd_error_table[code]));
438 report_error ("%s: %s", s, _(bash_badsub_errmsg));
446 report_error (_("%s: unbound variable"), s);
453 report_error (_("%s: readonly variable"), s);