]>
Commit | Line | Data |
---|---|---|
726f6388 | 1 | /* error.c -- Functions for handling errors. */ |
3185942a JA |
2 | |
3 | /* Copyright (C) 1993-2009 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" | |
726f6388 | 47 | #include "flags.h" |
ccc6cda3 | 48 | #include "input.h" |
726f6388 | 49 | |
ccc6cda3 JA |
50 | #if defined (HISTORY) |
51 | # include "bashhist.h" | |
52 | #endif | |
53 | ||
7117c2d2 JA |
54 | extern int executing_line_number __P((void)); |
55 | ||
0001803f | 56 | extern int last_command_exit_value; |
726f6388 | 57 | extern char *shell_name; |
726f6388 JA |
58 | #if defined (JOB_CONTROL) |
59 | extern pid_t shell_pgrp; | |
f73dda09 | 60 | extern int give_terminal_to __P((pid_t, int)); |
726f6388 JA |
61 | #endif /* JOB_CONTROL */ |
62 | ||
b80f6443 | 63 | #if defined (ARRAY_VARS) |
3185942a | 64 | extern const char * const bash_badsub_errmsg; |
b80f6443 JA |
65 | #endif |
66 | ||
7117c2d2 JA |
67 | static void error_prolog __P((int)); |
68 | ||
ccc6cda3 JA |
69 | /* The current maintainer of the shell. You change this in the |
70 | Makefile. */ | |
71 | #if !defined (MAINTAINER) | |
bb70624e | 72 | #define MAINTAINER "bash-maintainers@gnu.org" |
ccc6cda3 JA |
73 | #endif |
74 | ||
3185942a | 75 | const char * const the_current_maintainer = MAINTAINER; |
ccc6cda3 | 76 | |
b80f6443 JA |
77 | int gnu_error_format = 0; |
78 | ||
7117c2d2 JA |
79 | static void |
80 | error_prolog (print_lineno) | |
81 | int print_lineno; | |
82 | { | |
b80f6443 | 83 | char *ename; |
7117c2d2 JA |
84 | int line; |
85 | ||
b80f6443 JA |
86 | ename = get_name_for_error (); |
87 | line = (print_lineno && interactive_shell == 0) ? executing_line_number () : -1; | |
7117c2d2 | 88 | |
b80f6443 | 89 | if (line > 0) |
3185942a | 90 | fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : _(" line "), line); |
b80f6443 JA |
91 | else |
92 | fprintf (stderr, "%s: ", ename); | |
7117c2d2 JA |
93 | } |
94 | ||
726f6388 JA |
95 | /* Return the name of the shell or the shell script for error reporting. */ |
96 | char * | |
97 | get_name_for_error () | |
98 | { | |
ccc6cda3 | 99 | char *name; |
b80f6443 JA |
100 | #if defined (ARRAY_VARS) |
101 | SHELL_VAR *bash_source_v; | |
102 | ARRAY *bash_source_a; | |
103 | #endif | |
726f6388 | 104 | |
ccc6cda3 JA |
105 | name = (char *)NULL; |
106 | if (interactive_shell == 0) | |
b80f6443 JA |
107 | { |
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); | |
3185942a | 113 | if (name == 0 || *name == '\0') /* XXX - was just name == 0 */ |
b80f6443 JA |
114 | #endif |
115 | name = dollar_vars[0]; | |
116 | } | |
ccc6cda3 | 117 | if (name == 0 && shell_name && *shell_name) |
726f6388 | 118 | name = base_pathname (shell_name); |
ccc6cda3 JA |
119 | if (name == 0) |
120 | #if defined (PROGRAM) | |
121 | name = PROGRAM; | |
122 | #else | |
726f6388 | 123 | name = "bash"; |
ccc6cda3 | 124 | #endif |
726f6388 JA |
125 | |
126 | return (name); | |
127 | } | |
128 | ||
ccc6cda3 JA |
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 | |
131 | format string. */ | |
726f6388 JA |
132 | void |
133 | file_error (filename) | |
f73dda09 | 134 | const char *filename; |
726f6388 JA |
135 | { |
136 | report_error ("%s: %s", filename, strerror (errno)); | |
137 | } | |
138 | ||
726f6388 | 139 | void |
ccc6cda3 JA |
140 | #if defined (PREFER_STDARG) |
141 | programming_error (const char *format, ...) | |
142 | #else | |
143 | programming_error (format, va_alist) | |
144 | const char *format; | |
726f6388 | 145 | va_dcl |
ccc6cda3 | 146 | #endif |
726f6388 JA |
147 | { |
148 | va_list args; | |
ccc6cda3 | 149 | char *h; |
726f6388 JA |
150 | |
151 | #if defined (JOB_CONTROL) | |
f73dda09 | 152 | give_terminal_to (shell_pgrp, 0); |
726f6388 JA |
153 | #endif /* JOB_CONTROL */ |
154 | ||
7117c2d2 | 155 | SH_VA_START (args, format); |
ccc6cda3 | 156 | |
726f6388 JA |
157 | vfprintf (stderr, format, args); |
158 | fprintf (stderr, "\n"); | |
159 | va_end (args); | |
160 | ||
ccc6cda3 JA |
161 | #if defined (HISTORY) |
162 | if (remember_on_history) | |
163 | { | |
164 | h = last_history_line (); | |
b80f6443 | 165 | fprintf (stderr, _("last command: %s\n"), h ? h : "(null)"); |
ccc6cda3 JA |
166 | } |
167 | #endif | |
168 | ||
bb70624e | 169 | #if 0 |
d166f048 | 170 | fprintf (stderr, "Report this to %s\n", the_current_maintainer); |
bb70624e JA |
171 | #endif |
172 | ||
b80f6443 | 173 | fprintf (stderr, _("Aborting...")); |
726f6388 | 174 | fflush (stderr); |
ccc6cda3 | 175 | |
726f6388 JA |
176 | abort (); |
177 | } | |
178 | ||
7117c2d2 JA |
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. */ | |
726f6388 | 183 | void |
ccc6cda3 JA |
184 | #if defined (PREFER_STDARG) |
185 | report_error (const char *format, ...) | |
186 | #else | |
187 | report_error (format, va_alist) | |
188 | const char *format; | |
726f6388 | 189 | va_dcl |
ccc6cda3 | 190 | #endif |
726f6388 JA |
191 | { |
192 | va_list args; | |
726f6388 | 193 | |
7117c2d2 | 194 | error_prolog (1); |
ccc6cda3 | 195 | |
7117c2d2 | 196 | SH_VA_START (args, format); |
ccc6cda3 | 197 | |
726f6388 JA |
198 | vfprintf (stderr, format, args); |
199 | fprintf (stderr, "\n"); | |
200 | ||
201 | va_end (args); | |
202 | if (exit_immediately_on_error) | |
98043138 CR |
203 | { |
204 | if (last_command_exit_value == 0) | |
205 | last_command_exit_value = 1; | |
206 | exit_shell (last_command_exit_value); | |
207 | } | |
726f6388 JA |
208 | } |
209 | ||
210 | void | |
ccc6cda3 JA |
211 | #if defined (PREFER_STDARG) |
212 | fatal_error (const char *format, ...) | |
213 | #else | |
214 | fatal_error (format, va_alist) | |
215 | const char *format; | |
726f6388 | 216 | va_dcl |
ccc6cda3 | 217 | #endif |
726f6388 JA |
218 | { |
219 | va_list args; | |
726f6388 | 220 | |
7117c2d2 | 221 | error_prolog (0); |
ccc6cda3 | 222 | |
7117c2d2 | 223 | SH_VA_START (args, format); |
ccc6cda3 | 224 | |
726f6388 JA |
225 | vfprintf (stderr, format, args); |
226 | fprintf (stderr, "\n"); | |
227 | ||
228 | va_end (args); | |
7117c2d2 | 229 | sh_exit (2); |
726f6388 JA |
230 | } |
231 | ||
232 | void | |
ccc6cda3 JA |
233 | #if defined (PREFER_STDARG) |
234 | internal_error (const char *format, ...) | |
235 | #else | |
236 | internal_error (format, va_alist) | |
237 | const char *format; | |
238 | va_dcl | |
239 | #endif | |
240 | { | |
241 | va_list args; | |
242 | ||
7117c2d2 | 243 | error_prolog (1); |
ccc6cda3 | 244 | |
7117c2d2 | 245 | SH_VA_START (args, format); |
ccc6cda3 JA |
246 | |
247 | vfprintf (stderr, format, args); | |
248 | fprintf (stderr, "\n"); | |
249 | ||
250 | va_end (args); | |
251 | } | |
252 | ||
cce855bc JA |
253 | void |
254 | #if defined (PREFER_STDARG) | |
255 | internal_warning (const char *format, ...) | |
256 | #else | |
257 | internal_warning (format, va_alist) | |
258 | const char *format; | |
259 | va_dcl | |
260 | #endif | |
261 | { | |
262 | va_list args; | |
263 | ||
3185942a JA |
264 | error_prolog (1); |
265 | fprintf (stderr, _("warning: ")); | |
cce855bc | 266 | |
7117c2d2 | 267 | SH_VA_START (args, format); |
cce855bc JA |
268 | |
269 | vfprintf (stderr, format, args); | |
270 | fprintf (stderr, "\n"); | |
271 | ||
272 | va_end (args); | |
273 | } | |
274 | ||
ccc6cda3 JA |
275 | void |
276 | #if defined (PREFER_STDARG) | |
277 | sys_error (const char *format, ...) | |
278 | #else | |
279 | sys_error (format, va_alist) | |
280 | const char *format; | |
726f6388 | 281 | va_dcl |
ccc6cda3 | 282 | #endif |
726f6388 | 283 | { |
7117c2d2 | 284 | int e; |
726f6388 | 285 | va_list args; |
726f6388 | 286 | |
7117c2d2 JA |
287 | e = errno; |
288 | error_prolog (0); | |
ccc6cda3 | 289 | |
7117c2d2 | 290 | SH_VA_START (args, format); |
ccc6cda3 JA |
291 | |
292 | vfprintf (stderr, format, args); | |
7117c2d2 | 293 | fprintf (stderr, ": %s\n", strerror (e)); |
ccc6cda3 JA |
294 | |
295 | va_end (args); | |
296 | } | |
297 | ||
298 | /* An error from the parser takes the general form | |
299 | ||
300 | shell_name: input file name: line number: message | |
301 | ||
302 | The input file name and line number are omitted if the shell is | |
303 | currently interactive. If the shell is not currently interactive, | |
304 | the input file name is inserted only if it is different from the | |
305 | shell name. */ | |
306 | void | |
307 | #if defined (PREFER_STDARG) | |
308 | parser_error (int lineno, const char *format, ...) | |
309 | #else | |
310 | parser_error (lineno, format, va_alist) | |
311 | int lineno; | |
312 | const char *format; | |
313 | va_dcl | |
314 | #endif | |
315 | { | |
316 | va_list args; | |
317 | char *ename, *iname; | |
318 | ||
319 | ename = get_name_for_error (); | |
7117c2d2 | 320 | iname = yy_input_name (); |
ccc6cda3 JA |
321 | |
322 | if (interactive) | |
323 | fprintf (stderr, "%s: ", ename); | |
324 | else if (interactive_shell) | |
3185942a | 325 | fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : _(" line "), lineno); |
ccc6cda3 | 326 | else if (STREQ (ename, iname)) |
3185942a | 327 | fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : _(" line "), lineno); |
ccc6cda3 | 328 | else |
3185942a | 329 | fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : _(" line "), lineno); |
ccc6cda3 | 330 | |
7117c2d2 | 331 | SH_VA_START (args, format); |
ccc6cda3 | 332 | |
726f6388 JA |
333 | vfprintf (stderr, format, args); |
334 | fprintf (stderr, "\n"); | |
335 | ||
336 | va_end (args); | |
ccc6cda3 JA |
337 | |
338 | if (exit_immediately_on_error) | |
0001803f | 339 | exit_shell (last_command_exit_value = 2); |
726f6388 JA |
340 | } |
341 | ||
28ef6c31 | 342 | #ifdef DEBUG |
ccc6cda3 JA |
343 | void |
344 | #if defined (PREFER_STDARG) | |
345 | itrace (const char *format, ...) | |
346 | #else | |
347 | itrace (format, va_alist) | |
348 | const char *format; | |
726f6388 | 349 | va_dcl |
ccc6cda3 | 350 | #endif |
726f6388 JA |
351 | { |
352 | va_list args; | |
726f6388 | 353 | |
f73dda09 | 354 | fprintf(stderr, "TRACE: pid %ld: ", (long)getpid()); |
ccc6cda3 | 355 | |
7117c2d2 | 356 | SH_VA_START (args, format); |
ccc6cda3 | 357 | |
726f6388 JA |
358 | vfprintf (stderr, format, args); |
359 | fprintf (stderr, "\n"); | |
360 | ||
361 | va_end (args); | |
362 | ||
363 | fflush(stderr); | |
364 | } | |
365 | ||
726f6388 JA |
366 | /* A trace function for silent debugging -- doesn't require a control |
367 | terminal. */ | |
ccc6cda3 JA |
368 | void |
369 | #if defined (PREFER_STDARG) | |
370 | trace (const char *format, ...) | |
371 | #else | |
372 | trace (format, va_alist) | |
373 | const char *format; | |
726f6388 | 374 | va_dcl |
ccc6cda3 | 375 | #endif |
726f6388 JA |
376 | { |
377 | va_list args; | |
726f6388 JA |
378 | static FILE *tracefp = (FILE *)NULL; |
379 | ||
380 | if (tracefp == NULL) | |
bb70624e | 381 | tracefp = fopen("/tmp/bash-trace.log", "a+"); |
726f6388 JA |
382 | |
383 | if (tracefp == NULL) | |
384 | tracefp = stderr; | |
385 | else | |
386 | fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */ | |
387 | ||
f73dda09 | 388 | fprintf(tracefp, "TRACE: pid %ld: ", (long)getpid()); |
726f6388 | 389 | |
7117c2d2 | 390 | SH_VA_START (args, format); |
ccc6cda3 | 391 | |
726f6388 JA |
392 | vfprintf (tracefp, format, args); |
393 | fprintf (tracefp, "\n"); | |
394 | ||
395 | va_end (args); | |
396 | ||
397 | fflush(tracefp); | |
398 | } | |
ccc6cda3 | 399 | |
28ef6c31 | 400 | #endif /* DEBUG */ |
b72432fd | 401 | |
7117c2d2 JA |
402 | /* **************************************************************** */ |
403 | /* */ | |
404 | /* Common error reporting */ | |
405 | /* */ | |
406 | /* **************************************************************** */ | |
407 | ||
408 | ||
3185942a | 409 | static const char * const cmd_error_table[] = { |
b80f6443 JA |
410 | N_("unknown command error"), /* CMDERR_DEFAULT */ |
411 | N_("bad command type"), /* CMDERR_BADTYPE */ | |
412 | N_("bad connector"), /* CMDERR_BADCONN */ | |
413 | N_("bad jump"), /* CMDERR_BADJUMP */ | |
b72432fd JA |
414 | 0 |
415 | }; | |
416 | ||
417 | void | |
418 | command_error (func, code, e, flags) | |
419 | const char *func; | |
420 | int code, e, flags; /* flags currently unused */ | |
421 | { | |
422 | if (code > CMDERR_LAST) | |
423 | code = CMDERR_DEFAULT; | |
424 | ||
b80f6443 | 425 | programming_error ("%s: %s: %d", func, _(cmd_error_table[code]), e); |
b72432fd JA |
426 | } |
427 | ||
428 | char * | |
429 | command_errstr (code) | |
430 | int code; | |
431 | { | |
432 | if (code > CMDERR_LAST) | |
433 | code = CMDERR_DEFAULT; | |
434 | ||
b80f6443 | 435 | return (_(cmd_error_table[code])); |
b72432fd | 436 | } |
7117c2d2 JA |
437 | |
438 | #ifdef ARRAY_VARS | |
439 | void | |
440 | err_badarraysub (s) | |
441 | const char *s; | |
442 | { | |
b80f6443 | 443 | report_error ("%s: %s", s, _(bash_badsub_errmsg)); |
7117c2d2 JA |
444 | } |
445 | #endif | |
446 | ||
447 | void | |
448 | err_unboundvar (s) | |
449 | const char *s; | |
450 | { | |
b80f6443 | 451 | report_error (_("%s: unbound variable"), s); |
7117c2d2 JA |
452 | } |
453 | ||
454 | void | |
455 | err_readonly (s) | |
456 | const char *s; | |
457 | { | |
b80f6443 | 458 | report_error (_("%s: readonly variable"), s); |
7117c2d2 | 459 | } |