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