]>
Commit | Line | Data |
---|---|---|
726f6388 | 1 | /* error.c -- Functions for handling errors. */ |
3185942a | 2 | |
74091dd4 | 3 | /* Copyright (C) 1993-2021 Free Software Foundation, Inc. |
726f6388 JA |
4 | |
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
3185942a JA |
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. | |
726f6388 | 11 | |
3185942a JA |
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. | |
726f6388 | 16 | |
3185942a JA |
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/>. | |
19 | */ | |
726f6388 | 20 | |
ccc6cda3 JA |
21 | #include "config.h" |
22 | ||
d166f048 | 23 | #include "bashtypes.h" |
726f6388 JA |
24 | #include <fcntl.h> |
25 | ||
ccc6cda3 JA |
26 | #if defined (HAVE_UNISTD_H) |
27 | # include <unistd.h> | |
28 | #endif | |
29 | ||
30 | #if defined (PREFER_STDARG) | |
31 | # include <stdarg.h> | |
32 | #else | |
7117c2d2 | 33 | # include <varargs.h> |
726f6388 JA |
34 | #endif |
35 | ||
d166f048 JA |
36 | #include <stdio.h> |
37 | ||
726f6388 JA |
38 | #include <errno.h> |
39 | #if !defined (errno) | |
40 | extern int errno; | |
41 | #endif /* !errno */ | |
42 | ||
43 | #include "bashansi.h" | |
b80f6443 JA |
44 | #include "bashintl.h" |
45 | ||
46 | #include "shell.h" | |
d233b485 | 47 | #include "execute_cmd.h" |
726f6388 | 48 | #include "flags.h" |
ccc6cda3 | 49 | #include "input.h" |
726f6388 | 50 | |
ccc6cda3 JA |
51 | #if defined (HISTORY) |
52 | # include "bashhist.h" | |
53 | #endif | |
54 | ||
8868edaf | 55 | extern int executing_line_number PARAMS((void)); |
7117c2d2 | 56 | |
726f6388 JA |
57 | #if defined (JOB_CONTROL) |
58 | extern pid_t shell_pgrp; | |
8868edaf | 59 | extern int give_terminal_to PARAMS((pid_t, int)); |
726f6388 JA |
60 | #endif /* JOB_CONTROL */ |
61 | ||
b80f6443 | 62 | #if defined (ARRAY_VARS) |
3185942a | 63 | extern const char * const bash_badsub_errmsg; |
b80f6443 JA |
64 | #endif |
65 | ||
8868edaf | 66 | static void error_prolog PARAMS((int)); |
7117c2d2 | 67 | |
ccc6cda3 JA |
68 | /* The current maintainer of the shell. You change this in the |
69 | Makefile. */ | |
70 | #if !defined (MAINTAINER) | |
bb70624e | 71 | #define MAINTAINER "bash-maintainers@gnu.org" |
ccc6cda3 JA |
72 | #endif |
73 | ||
3185942a | 74 | const char * const the_current_maintainer = MAINTAINER; |
ccc6cda3 | 75 | |
b80f6443 JA |
76 | int gnu_error_format = 0; |
77 | ||
7117c2d2 JA |
78 | static void |
79 | error_prolog (print_lineno) | |
80 | int print_lineno; | |
81 | { | |
b80f6443 | 82 | char *ename; |
7117c2d2 JA |
83 | int line; |
84 | ||
b80f6443 JA |
85 | ename = get_name_for_error (); |
86 | line = (print_lineno && interactive_shell == 0) ? executing_line_number () : -1; | |
7117c2d2 | 87 | |
b80f6443 | 88 | if (line > 0) |
3185942a | 89 | fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : _(" line "), line); |
b80f6443 JA |
90 | else |
91 | fprintf (stderr, "%s: ", ename); | |
7117c2d2 JA |
92 | } |
93 | ||
726f6388 JA |
94 | /* Return the name of the shell or the shell script for error reporting. */ |
95 | char * | |
96 | get_name_for_error () | |
97 | { | |
ccc6cda3 | 98 | char *name; |
b80f6443 JA |
99 | #if defined (ARRAY_VARS) |
100 | SHELL_VAR *bash_source_v; | |
101 | ARRAY *bash_source_a; | |
102 | #endif | |
726f6388 | 103 | |
ccc6cda3 JA |
104 | name = (char *)NULL; |
105 | if (interactive_shell == 0) | |
b80f6443 JA |
106 | { |
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); | |
3185942a | 112 | if (name == 0 || *name == '\0') /* XXX - was just name == 0 */ |
b80f6443 JA |
113 | #endif |
114 | name = dollar_vars[0]; | |
115 | } | |
ccc6cda3 | 116 | if (name == 0 && shell_name && *shell_name) |
726f6388 | 117 | name = base_pathname (shell_name); |
ccc6cda3 JA |
118 | if (name == 0) |
119 | #if defined (PROGRAM) | |
120 | name = PROGRAM; | |
121 | #else | |
726f6388 | 122 | name = "bash"; |
ccc6cda3 | 123 | #endif |
726f6388 JA |
124 | |
125 | return (name); | |
126 | } | |
127 | ||
ccc6cda3 JA |
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 | |
130 | format string. */ | |
726f6388 JA |
131 | void |
132 | file_error (filename) | |
f73dda09 | 133 | const char *filename; |
726f6388 JA |
134 | { |
135 | report_error ("%s: %s", filename, strerror (errno)); | |
136 | } | |
137 | ||
726f6388 | 138 | void |
ccc6cda3 JA |
139 | #if defined (PREFER_STDARG) |
140 | programming_error (const char *format, ...) | |
141 | #else | |
142 | programming_error (format, va_alist) | |
143 | const char *format; | |
726f6388 | 144 | va_dcl |
ccc6cda3 | 145 | #endif |
726f6388 JA |
146 | { |
147 | va_list args; | |
ccc6cda3 | 148 | char *h; |
726f6388 JA |
149 | |
150 | #if defined (JOB_CONTROL) | |
f73dda09 | 151 | give_terminal_to (shell_pgrp, 0); |
726f6388 JA |
152 | #endif /* JOB_CONTROL */ |
153 | ||
7117c2d2 | 154 | SH_VA_START (args, format); |
ccc6cda3 | 155 | |
726f6388 JA |
156 | vfprintf (stderr, format, args); |
157 | fprintf (stderr, "\n"); | |
158 | va_end (args); | |
159 | ||
ccc6cda3 JA |
160 | #if defined (HISTORY) |
161 | if (remember_on_history) | |
162 | { | |
163 | h = last_history_line (); | |
b80f6443 | 164 | fprintf (stderr, _("last command: %s\n"), h ? h : "(null)"); |
ccc6cda3 JA |
165 | } |
166 | #endif | |
167 | ||
bb70624e | 168 | #if 0 |
d166f048 | 169 | fprintf (stderr, "Report this to %s\n", the_current_maintainer); |
bb70624e JA |
170 | #endif |
171 | ||
b80f6443 | 172 | fprintf (stderr, _("Aborting...")); |
726f6388 | 173 | fflush (stderr); |
ccc6cda3 | 174 | |
726f6388 JA |
175 | abort (); |
176 | } | |
177 | ||
7117c2d2 JA |
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. */ | |
726f6388 | 182 | void |
ccc6cda3 JA |
183 | #if defined (PREFER_STDARG) |
184 | report_error (const char *format, ...) | |
185 | #else | |
186 | report_error (format, va_alist) | |
187 | const char *format; | |
726f6388 | 188 | va_dcl |
ccc6cda3 | 189 | #endif |
726f6388 JA |
190 | { |
191 | va_list args; | |
726f6388 | 192 | |
7117c2d2 | 193 | error_prolog (1); |
ccc6cda3 | 194 | |
7117c2d2 | 195 | SH_VA_START (args, format); |
ccc6cda3 | 196 | |
726f6388 JA |
197 | vfprintf (stderr, format, args); |
198 | fprintf (stderr, "\n"); | |
199 | ||
200 | va_end (args); | |
201 | if (exit_immediately_on_error) | |
98043138 CR |
202 | { |
203 | if (last_command_exit_value == 0) | |
8868edaf | 204 | last_command_exit_value = EXECUTION_FAILURE; |
98043138 CR |
205 | exit_shell (last_command_exit_value); |
206 | } | |
726f6388 JA |
207 | } |
208 | ||
209 | void | |
ccc6cda3 JA |
210 | #if defined (PREFER_STDARG) |
211 | fatal_error (const char *format, ...) | |
212 | #else | |
213 | fatal_error (format, va_alist) | |
214 | const char *format; | |
726f6388 | 215 | va_dcl |
ccc6cda3 | 216 | #endif |
726f6388 JA |
217 | { |
218 | va_list args; | |
726f6388 | 219 | |
7117c2d2 | 220 | error_prolog (0); |
ccc6cda3 | 221 | |
7117c2d2 | 222 | SH_VA_START (args, format); |
ccc6cda3 | 223 | |
726f6388 JA |
224 | vfprintf (stderr, format, args); |
225 | fprintf (stderr, "\n"); | |
226 | ||
227 | va_end (args); | |
7117c2d2 | 228 | sh_exit (2); |
726f6388 JA |
229 | } |
230 | ||
231 | void | |
ccc6cda3 JA |
232 | #if defined (PREFER_STDARG) |
233 | internal_error (const char *format, ...) | |
234 | #else | |
235 | internal_error (format, va_alist) | |
236 | const char *format; | |
237 | va_dcl | |
238 | #endif | |
239 | { | |
240 | va_list args; | |
241 | ||
7117c2d2 | 242 | error_prolog (1); |
ccc6cda3 | 243 | |
7117c2d2 | 244 | SH_VA_START (args, format); |
ccc6cda3 JA |
245 | |
246 | vfprintf (stderr, format, args); | |
247 | fprintf (stderr, "\n"); | |
248 | ||
249 | va_end (args); | |
250 | } | |
251 | ||
cce855bc JA |
252 | void |
253 | #if defined (PREFER_STDARG) | |
254 | internal_warning (const char *format, ...) | |
255 | #else | |
256 | internal_warning (format, va_alist) | |
257 | const char *format; | |
258 | va_dcl | |
259 | #endif | |
260 | { | |
261 | va_list args; | |
262 | ||
3185942a JA |
263 | error_prolog (1); |
264 | fprintf (stderr, _("warning: ")); | |
cce855bc | 265 | |
7117c2d2 | 266 | SH_VA_START (args, format); |
cce855bc JA |
267 | |
268 | vfprintf (stderr, format, args); | |
269 | fprintf (stderr, "\n"); | |
270 | ||
271 | va_end (args); | |
272 | } | |
273 | ||
a0c0a00f CR |
274 | void |
275 | #if defined (PREFER_STDARG) | |
276 | internal_inform (const char *format, ...) | |
277 | #else | |
278 | internal_inform (format, va_alist) | |
279 | const char *format; | |
280 | va_dcl | |
281 | #endif | |
282 | { | |
283 | va_list args; | |
284 | ||
285 | error_prolog (1); | |
286 | /* TRANSLATORS: this is a prefix for informational messages. */ | |
287 | fprintf (stderr, _("INFORM: ")); | |
288 | ||
289 | SH_VA_START (args, format); | |
290 | ||
291 | vfprintf (stderr, format, args); | |
292 | fprintf (stderr, "\n"); | |
293 | ||
294 | va_end (args); | |
295 | } | |
296 | ||
74091dd4 CR |
297 | void |
298 | #if defined (PREFER_STDARG) | |
299 | internal_debug (const char *format, ...) | |
300 | #else | |
301 | internal_debug (format, va_alist) | |
302 | const char *format; | |
303 | va_dcl | |
304 | #endif | |
305 | { | |
306 | #ifdef DEBUG | |
307 | va_list args; | |
308 | ||
309 | error_prolog (1); | |
310 | fprintf (stderr, _("DEBUG warning: ")); | |
311 | ||
312 | SH_VA_START (args, format); | |
313 | ||
314 | vfprintf (stderr, format, args); | |
315 | fprintf (stderr, "\n"); | |
316 | ||
317 | va_end (args); | |
318 | #else | |
319 | return; | |
320 | #endif | |
321 | } | |
322 | ||
ccc6cda3 JA |
323 | void |
324 | #if defined (PREFER_STDARG) | |
325 | sys_error (const char *format, ...) | |
326 | #else | |
327 | sys_error (format, va_alist) | |
328 | const char *format; | |
726f6388 | 329 | va_dcl |
ccc6cda3 | 330 | #endif |
726f6388 | 331 | { |
7117c2d2 | 332 | int e; |
726f6388 | 333 | va_list args; |
726f6388 | 334 | |
7117c2d2 JA |
335 | e = errno; |
336 | error_prolog (0); | |
ccc6cda3 | 337 | |
7117c2d2 | 338 | SH_VA_START (args, format); |
ccc6cda3 JA |
339 | |
340 | vfprintf (stderr, format, args); | |
7117c2d2 | 341 | fprintf (stderr, ": %s\n", strerror (e)); |
ccc6cda3 JA |
342 | |
343 | va_end (args); | |
344 | } | |
345 | ||
346 | /* An error from the parser takes the general form | |
347 | ||
348 | shell_name: input file name: line number: message | |
349 | ||
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 | |
353 | shell name. */ | |
354 | void | |
355 | #if defined (PREFER_STDARG) | |
356 | parser_error (int lineno, const char *format, ...) | |
357 | #else | |
358 | parser_error (lineno, format, va_alist) | |
359 | int lineno; | |
360 | const char *format; | |
361 | va_dcl | |
362 | #endif | |
363 | { | |
364 | va_list args; | |
365 | char *ename, *iname; | |
366 | ||
367 | ename = get_name_for_error (); | |
7117c2d2 | 368 | iname = yy_input_name (); |
ccc6cda3 JA |
369 | |
370 | if (interactive) | |
371 | fprintf (stderr, "%s: ", ename); | |
372 | else if (interactive_shell) | |
3185942a | 373 | fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : _(" line "), lineno); |
ccc6cda3 | 374 | else if (STREQ (ename, iname)) |
3185942a | 375 | fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : _(" line "), lineno); |
ccc6cda3 | 376 | else |
3185942a | 377 | fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : _(" line "), lineno); |
ccc6cda3 | 378 | |
7117c2d2 | 379 | SH_VA_START (args, format); |
ccc6cda3 | 380 | |
726f6388 JA |
381 | vfprintf (stderr, format, args); |
382 | fprintf (stderr, "\n"); | |
383 | ||
384 | va_end (args); | |
ccc6cda3 JA |
385 | |
386 | if (exit_immediately_on_error) | |
0001803f | 387 | exit_shell (last_command_exit_value = 2); |
726f6388 JA |
388 | } |
389 | ||
28ef6c31 | 390 | #ifdef DEBUG |
ac50fbac CR |
391 | /* This assumes ASCII and is suitable only for debugging */ |
392 | char * | |
393 | strescape (str) | |
394 | const char *str; | |
395 | { | |
396 | char *r, *result; | |
397 | unsigned char *s; | |
398 | ||
399 | r = result = (char *)xmalloc (strlen (str) * 2 + 1); | |
400 | ||
401 | for (s = (unsigned char *)str; s && *s; s++) | |
402 | { | |
403 | if (*s < ' ') | |
404 | { | |
405 | *r++ = '^'; | |
406 | *r++ = *s+64; | |
407 | } | |
408 | else if (*s == 127) | |
409 | { | |
410 | *r++ = '^'; | |
411 | *r++ = '?'; | |
412 | } | |
413 | else | |
414 | *r++ = *s; | |
415 | } | |
416 | ||
417 | *r = '\0'; | |
418 | return result; | |
419 | } | |
420 | ||
ccc6cda3 JA |
421 | void |
422 | #if defined (PREFER_STDARG) | |
423 | itrace (const char *format, ...) | |
424 | #else | |
425 | itrace (format, va_alist) | |
426 | const char *format; | |
726f6388 | 427 | va_dcl |
ccc6cda3 | 428 | #endif |
726f6388 JA |
429 | { |
430 | va_list args; | |
726f6388 | 431 | |
f73dda09 | 432 | fprintf(stderr, "TRACE: pid %ld: ", (long)getpid()); |
ccc6cda3 | 433 | |
7117c2d2 | 434 | SH_VA_START (args, format); |
ccc6cda3 | 435 | |
726f6388 JA |
436 | vfprintf (stderr, format, args); |
437 | fprintf (stderr, "\n"); | |
438 | ||
439 | va_end (args); | |
440 | ||
441 | fflush(stderr); | |
442 | } | |
443 | ||
726f6388 JA |
444 | /* A trace function for silent debugging -- doesn't require a control |
445 | terminal. */ | |
ccc6cda3 JA |
446 | void |
447 | #if defined (PREFER_STDARG) | |
448 | trace (const char *format, ...) | |
449 | #else | |
450 | trace (format, va_alist) | |
451 | const char *format; | |
726f6388 | 452 | va_dcl |
ccc6cda3 | 453 | #endif |
726f6388 JA |
454 | { |
455 | va_list args; | |
726f6388 JA |
456 | static FILE *tracefp = (FILE *)NULL; |
457 | ||
458 | if (tracefp == NULL) | |
bb70624e | 459 | tracefp = fopen("/tmp/bash-trace.log", "a+"); |
726f6388 JA |
460 | |
461 | if (tracefp == NULL) | |
462 | tracefp = stderr; | |
463 | else | |
464 | fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */ | |
465 | ||
f73dda09 | 466 | fprintf(tracefp, "TRACE: pid %ld: ", (long)getpid()); |
726f6388 | 467 | |
7117c2d2 | 468 | SH_VA_START (args, format); |
ccc6cda3 | 469 | |
726f6388 JA |
470 | vfprintf (tracefp, format, args); |
471 | fprintf (tracefp, "\n"); | |
472 | ||
473 | va_end (args); | |
474 | ||
475 | fflush(tracefp); | |
476 | } | |
ccc6cda3 | 477 | |
28ef6c31 | 478 | #endif /* DEBUG */ |
b72432fd | 479 | |
7117c2d2 JA |
480 | /* **************************************************************** */ |
481 | /* */ | |
482 | /* Common error reporting */ | |
483 | /* */ | |
484 | /* **************************************************************** */ | |
485 | ||
486 | ||
3185942a | 487 | static const char * const cmd_error_table[] = { |
b80f6443 JA |
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 */ | |
b72432fd JA |
492 | 0 |
493 | }; | |
494 | ||
495 | void | |
496 | command_error (func, code, e, flags) | |
497 | const char *func; | |
498 | int code, e, flags; /* flags currently unused */ | |
499 | { | |
500 | if (code > CMDERR_LAST) | |
501 | code = CMDERR_DEFAULT; | |
502 | ||
b80f6443 | 503 | programming_error ("%s: %s: %d", func, _(cmd_error_table[code]), e); |
b72432fd JA |
504 | } |
505 | ||
506 | char * | |
507 | command_errstr (code) | |
508 | int code; | |
509 | { | |
510 | if (code > CMDERR_LAST) | |
511 | code = CMDERR_DEFAULT; | |
512 | ||
b80f6443 | 513 | return (_(cmd_error_table[code])); |
b72432fd | 514 | } |
7117c2d2 JA |
515 | |
516 | #ifdef ARRAY_VARS | |
517 | void | |
518 | err_badarraysub (s) | |
519 | const char *s; | |
520 | { | |
b80f6443 | 521 | report_error ("%s: %s", s, _(bash_badsub_errmsg)); |
7117c2d2 JA |
522 | } |
523 | #endif | |
524 | ||
525 | void | |
526 | err_unboundvar (s) | |
527 | const char *s; | |
528 | { | |
b80f6443 | 529 | report_error (_("%s: unbound variable"), s); |
7117c2d2 JA |
530 | } |
531 | ||
532 | void | |
533 | err_readonly (s) | |
534 | const char *s; | |
535 | { | |
b80f6443 | 536 | report_error (_("%s: readonly variable"), s); |
7117c2d2 | 537 | } |