1 /* error.c -- Functions for handling errors. */
3 /* Copyright (C) 1993-2021 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)
47 #include "execute_cmd.h"
52 # include "bashhist.h"
55 extern int executing_line_number
PARAMS((void));
57 #if defined (JOB_CONTROL)
58 extern pid_t shell_pgrp
;
59 extern int give_terminal_to
PARAMS((pid_t
, int));
60 #endif /* JOB_CONTROL */
62 #if defined (ARRAY_VARS)
63 extern const char * const bash_badsub_errmsg
;
66 static void error_prolog
PARAMS((int));
68 /* The current maintainer of the shell. You change this in the
70 #if !defined (MAINTAINER)
71 #define MAINTAINER "bash-maintainers@gnu.org"
74 const char * const the_current_maintainer
= MAINTAINER
;
76 int gnu_error_format
= 0;
79 error_prolog (print_lineno
)
85 ename
= get_name_for_error ();
86 line
= (print_lineno
&& interactive_shell
== 0) ? executing_line_number () : -1;
89 fprintf (stderr
, "%s:%s%d: ", ename
, gnu_error_format
? "" : _(" line "), line
);
91 fprintf (stderr
, "%s: ", ename
);
94 /* Return the name of the shell or the shell script for error reporting. */
99 #if defined (ARRAY_VARS)
100 SHELL_VAR
*bash_source_v
;
101 ARRAY
*bash_source_a
;
105 if (interactive_shell
== 0)
107 #if defined (ARRAY_VARS)
108 bash_source_v
= find_variable ("BASH_SOURCE");
109 if (bash_source_v
&& array_p (bash_source_v
) &&
110 (bash_source_a
= array_cell (bash_source_v
)))
111 name
= array_reference (bash_source_a
, 0);
112 if (name
== 0 || *name
== '\0') /* XXX - was just name == 0 */
114 name
= dollar_vars
[0];
116 if (name
== 0 && shell_name
&& *shell_name
)
117 name
= base_pathname (shell_name
);
119 #if defined (PROGRAM)
128 /* Report an error having to do with FILENAME. This does not use
129 sys_error so the filename is not interpreted as a printf-style
132 file_error (filename
)
133 const char *filename
;
135 report_error ("%s: %s", filename
, strerror (errno
));
139 #if defined (PREFER_STDARG)
140 programming_error (const char *format
, ...)
142 programming_error (format
, va_alist
)
150 #if defined (JOB_CONTROL)
151 give_terminal_to (shell_pgrp
, 0);
152 #endif /* JOB_CONTROL */
154 SH_VA_START (args
, format
);
156 vfprintf (stderr
, format
, args
);
157 fprintf (stderr
, "\n");
160 #if defined (HISTORY)
161 if (remember_on_history
)
163 h
= last_history_line ();
164 fprintf (stderr
, _("last command: %s\n"), h
? h
: "(null)");
169 fprintf (stderr
, "Report this to %s\n", the_current_maintainer
);
172 fprintf (stderr
, _("Aborting..."));
178 /* Print an error message and, if `set -e' has been executed, exit the
179 shell. Used in this file by file_error and programming_error. Used
180 outside this file mostly to report substitution and expansion errors,
181 and for bad invocation options. */
183 #if defined (PREFER_STDARG)
184 report_error (const char *format
, ...)
186 report_error (format
, va_alist
)
195 SH_VA_START (args
, format
);
197 vfprintf (stderr
, format
, args
);
198 fprintf (stderr
, "\n");
201 if (exit_immediately_on_error
)
203 if (last_command_exit_value
== 0)
204 last_command_exit_value
= EXECUTION_FAILURE
;
205 exit_shell (last_command_exit_value
);
210 #if defined (PREFER_STDARG)
211 fatal_error (const char *format
, ...)
213 fatal_error (format
, va_alist
)
222 SH_VA_START (args
, format
);
224 vfprintf (stderr
, format
, args
);
225 fprintf (stderr
, "\n");
232 #if defined (PREFER_STDARG)
233 internal_error (const char *format
, ...)
235 internal_error (format
, va_alist
)
244 SH_VA_START (args
, format
);
246 vfprintf (stderr
, format
, args
);
247 fprintf (stderr
, "\n");
253 #if defined (PREFER_STDARG)
254 internal_warning (const char *format
, ...)
256 internal_warning (format
, va_alist
)
264 fprintf (stderr
, _("warning: "));
266 SH_VA_START (args
, format
);
268 vfprintf (stderr
, format
, args
);
269 fprintf (stderr
, "\n");
275 #if defined (PREFER_STDARG)
276 internal_inform (const char *format
, ...)
278 internal_inform (format
, va_alist
)
286 /* TRANSLATORS: this is a prefix for informational messages. */
287 fprintf (stderr
, _("INFORM: "));
289 SH_VA_START (args
, format
);
291 vfprintf (stderr
, format
, args
);
292 fprintf (stderr
, "\n");
298 #if defined (PREFER_STDARG)
299 internal_debug (const char *format
, ...)
301 internal_debug (format
, va_alist
)
310 fprintf (stderr
, _("DEBUG warning: "));
312 SH_VA_START (args
, format
);
314 vfprintf (stderr
, format
, args
);
315 fprintf (stderr
, "\n");
324 #if defined (PREFER_STDARG)
325 sys_error (const char *format
, ...)
327 sys_error (format
, va_alist
)
338 SH_VA_START (args
, format
);
340 vfprintf (stderr
, format
, args
);
341 fprintf (stderr
, ": %s\n", strerror (e
));
346 /* An error from the parser takes the general form
348 shell_name: input file name: line number: message
350 The input file name and line number are omitted if the shell is
351 currently interactive. If the shell is not currently interactive,
352 the input file name is inserted only if it is different from the
355 #if defined (PREFER_STDARG)
356 parser_error (int lineno
, const char *format
, ...)
358 parser_error (lineno
, format
, va_alist
)
367 ename
= get_name_for_error ();
368 iname
= yy_input_name ();
371 fprintf (stderr
, "%s: ", ename
);
372 else if (interactive_shell
)
373 fprintf (stderr
, "%s: %s:%s%d: ", ename
, iname
, gnu_error_format
? "" : _(" line "), lineno
);
374 else if (STREQ (ename
, iname
))
375 fprintf (stderr
, "%s:%s%d: ", ename
, gnu_error_format
? "" : _(" line "), lineno
);
377 fprintf (stderr
, "%s: %s:%s%d: ", ename
, iname
, gnu_error_format
? "" : _(" line "), lineno
);
379 SH_VA_START (args
, format
);
381 vfprintf (stderr
, format
, args
);
382 fprintf (stderr
, "\n");
386 if (exit_immediately_on_error
)
387 exit_shell (last_command_exit_value
= 2);
391 /* This assumes ASCII and is suitable only for debugging */
399 r
= result
= (char *)xmalloc (strlen (str
) * 2 + 1);
401 for (s
= (unsigned char *)str
; s
&& *s
; s
++)
422 #if defined (PREFER_STDARG)
423 itrace (const char *format
, ...)
425 itrace (format
, va_alist
)
432 fprintf(stderr
, "TRACE: pid %ld: ", (long)getpid());
434 SH_VA_START (args
, format
);
436 vfprintf (stderr
, format
, args
);
437 fprintf (stderr
, "\n");
444 /* A trace function for silent debugging -- doesn't require a control
447 #if defined (PREFER_STDARG)
448 trace (const char *format
, ...)
450 trace (format
, va_alist
)
456 static FILE *tracefp
= (FILE *)NULL
;
459 tracefp
= fopen("/tmp/bash-trace.log", "a+");
464 fcntl (fileno (tracefp
), F_SETFD
, 1); /* close-on-exec */
466 fprintf(tracefp
, "TRACE: pid %ld: ", (long)getpid());
468 SH_VA_START (args
, format
);
470 vfprintf (tracefp
, format
, args
);
471 fprintf (tracefp
, "\n");
480 /* **************************************************************** */
482 /* Common error reporting */
484 /* **************************************************************** */
487 static const char * const cmd_error_table
[] = {
488 N_("unknown command error"), /* CMDERR_DEFAULT */
489 N_("bad command type"), /* CMDERR_BADTYPE */
490 N_("bad connector"), /* CMDERR_BADCONN */
491 N_("bad jump"), /* CMDERR_BADJUMP */
496 command_error (func
, code
, e
, flags
)
498 int code
, e
, flags
; /* flags currently unused */
500 if (code
> CMDERR_LAST
)
501 code
= CMDERR_DEFAULT
;
503 programming_error ("%s: %s: %d", func
, _(cmd_error_table
[code
]), e
);
507 command_errstr (code
)
510 if (code
> CMDERR_LAST
)
511 code
= CMDERR_DEFAULT
;
513 return (_(cmd_error_table
[code
]));
521 report_error ("%s: %s", s
, _(bash_badsub_errmsg
));
529 report_error (_("%s: unbound variable"), s
);
536 report_error (_("%s: readonly variable"), s
);