1 /* error.c -- Functions for handling errors. */
3 /* Copyright (C) 1993-2022 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)
42 #include "execute_cmd.h"
47 # include "bashhist.h"
50 extern int executing_line_number (void);
52 #if defined (JOB_CONTROL)
53 extern pid_t shell_pgrp
;
54 extern int give_terminal_to (pid_t
, int);
55 #endif /* JOB_CONTROL */
57 #if defined (ARRAY_VARS)
58 extern const char * const bash_badsub_errmsg
;
61 static void error_prolog (int);
63 /* The current maintainer of the shell. You change this in the
65 #if !defined (MAINTAINER)
66 #define MAINTAINER "bash-maintainers@gnu.org"
69 const char * const the_current_maintainer
= MAINTAINER
;
71 int gnu_error_format
= 0;
74 error_prolog (int print_lineno
)
79 ename
= get_name_for_error ();
80 line
= (print_lineno
&& interactive_shell
== 0) ? executing_line_number () : -1;
83 fprintf (stderr
, "%s:%s%d: ", ename
, gnu_error_format
? "" : _(" line "), line
);
85 fprintf (stderr
, "%s: ", ename
);
88 /* Return the name of the shell or the shell script for error reporting. */
90 get_name_for_error (void)
93 #if defined (ARRAY_VARS)
94 SHELL_VAR
*bash_source_v
;
99 if (interactive_shell
== 0)
101 #if defined (ARRAY_VARS)
102 bash_source_v
= find_variable ("BASH_SOURCE");
103 if (bash_source_v
&& array_p (bash_source_v
) &&
104 (bash_source_a
= array_cell (bash_source_v
)))
105 name
= array_reference (bash_source_a
, 0);
106 if (name
== 0 || *name
== '\0') /* XXX - was just name == 0 */
108 name
= dollar_vars
[0];
110 if (name
== 0 && shell_name
&& *shell_name
)
111 name
= base_pathname (shell_name
);
113 #if defined (PROGRAM)
122 /* Report an error having to do with FILENAME. This does not use
123 sys_error so the filename is not interpreted as a printf-style
126 file_error (const char *filename
)
128 report_error ("%s: %s", filename
, strerror (errno
));
132 programming_error (const char *format
, ...)
137 #if defined (JOB_CONTROL)
138 give_terminal_to (shell_pgrp
, 0);
139 #endif /* JOB_CONTROL */
141 va_start (args
, format
);
143 vfprintf (stderr
, format
, args
);
144 fprintf (stderr
, "\n");
147 #if defined (HISTORY)
148 if (remember_on_history
)
150 h
= last_history_line ();
151 fprintf (stderr
, _("last command: %s\n"), h
? h
: "(null)");
156 fprintf (stderr
, "Report this to %s\n", the_current_maintainer
);
159 fprintf (stderr
, _("Aborting..."));
165 /* Print an error message and, if `set -e' has been executed, exit the
166 shell. Used in this file by file_error and programming_error. Used
167 outside this file mostly to report substitution and expansion errors,
168 and for bad invocation options. */
170 report_error (const char *format
, ...)
176 va_start (args
, format
);
178 vfprintf (stderr
, format
, args
);
179 fprintf (stderr
, "\n");
182 if (exit_immediately_on_error
)
184 if (last_command_exit_value
== 0)
185 last_command_exit_value
= EXECUTION_FAILURE
;
186 exit_shell (last_command_exit_value
);
191 fatal_error (const char *format
, ...)
197 va_start (args
, format
);
199 vfprintf (stderr
, format
, args
);
200 fprintf (stderr
, "\n");
207 internal_error (const char *format
, ...)
213 va_start (args
, format
);
215 vfprintf (stderr
, format
, args
);
216 fprintf (stderr
, "\n");
222 internal_warning (const char *format
, ...)
227 fprintf (stderr
, _("warning: "));
229 va_start (args
, format
);
231 vfprintf (stderr
, format
, args
);
232 fprintf (stderr
, "\n");
238 internal_inform (const char *format
, ...)
243 /* TRANSLATORS: this is a prefix for informational messages. */
244 fprintf (stderr
, _("INFORM: "));
246 va_start (args
, format
);
248 vfprintf (stderr
, format
, args
);
249 fprintf (stderr
, "\n");
255 internal_debug (const char *format
, ...)
261 fprintf (stderr
, _("DEBUG warning: "));
263 va_start (args
, format
);
265 vfprintf (stderr
, format
, args
);
266 fprintf (stderr
, "\n");
275 sys_error (const char *format
, ...)
283 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 parser_error (int lineno
, const char *format
, ...)
305 ename
= get_name_for_error ();
306 iname
= yy_input_name ();
309 fprintf (stderr
, "%s: ", ename
);
310 else if (interactive_shell
)
311 fprintf (stderr
, "%s: %s:%s%d: ", ename
, iname
, gnu_error_format
? "" : _(" line "), lineno
);
312 else if (STREQ (ename
, iname
))
313 fprintf (stderr
, "%s:%s%d: ", ename
, gnu_error_format
? "" : _(" line "), lineno
);
315 fprintf (stderr
, "%s: %s:%s%d: ", ename
, iname
, gnu_error_format
? "" : _(" line "), lineno
);
317 va_start (args
, format
);
319 vfprintf (stderr
, format
, args
);
320 fprintf (stderr
, "\n");
324 if (exit_immediately_on_error
)
325 exit_shell (last_command_exit_value
= 2);
329 /* This assumes ASCII and is suitable only for debugging */
331 strescape (const char *str
)
336 r
= result
= (char *)xmalloc (strlen (str
) * 2 + 1);
338 for (s
= (unsigned char *)str
; s
&& *s
; s
++)
359 itrace (const char *format
, ...)
363 fprintf(stderr
, "TRACE: pid %ld: ", (long)getpid());
365 va_start (args
, format
);
367 vfprintf (stderr
, format
, args
);
368 fprintf (stderr
, "\n");
375 /* A trace function for silent debugging -- doesn't require a control
378 trace (const char *format
, ...)
381 static FILE *tracefp
= (FILE *)NULL
;
384 tracefp
= fopen("/tmp/bash-trace.log", "a+");
389 fcntl (fileno (tracefp
), F_SETFD
, 1); /* close-on-exec */
391 fprintf(tracefp
, "TRACE: pid %ld: ", (long)getpid());
393 va_start (args
, format
);
395 vfprintf (tracefp
, format
, args
);
396 fprintf (tracefp
, "\n");
405 /* **************************************************************** */
407 /* Common error reporting */
409 /* **************************************************************** */
412 static const char * const cmd_error_table
[] = {
413 N_("unknown command error"), /* CMDERR_DEFAULT */
414 N_("bad command type"), /* CMDERR_BADTYPE */
415 N_("bad connector"), /* CMDERR_BADCONN */
416 N_("bad jump"), /* CMDERR_BADJUMP */
421 command_error (const char *func
, int code
, int e
, int flags
)
423 if (code
> CMDERR_LAST
)
424 code
= CMDERR_DEFAULT
;
426 programming_error ("%s: %s: %d", func
, _(cmd_error_table
[code
]), e
);
430 command_errstr (int code
)
432 if (code
> CMDERR_LAST
)
433 code
= CMDERR_DEFAULT
;
435 return (_(cmd_error_table
[code
]));
440 err_badarraysub (const char *s
)
442 report_error ("%s: %s", s
, _(bash_badsub_errmsg
));
447 err_unboundvar (const char *s
)
449 report_error (_("%s: unbound variable"), s
);
453 err_readonly (const char *s
)
455 report_error (_("%s: readonly variable"), s
);