1 /* error.c -- Functions for handling errors. */
2 /* Copyright (C) 1993 Free Software Foundation, Inc.
4 This file is part of GNU Bash, the Bourne Again SHell.
6 Bash is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License along
17 with Bash; see the file COPYING. If not, write to the Free Software
18 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #include "bashtypes.h"
25 #if defined (HAVE_UNISTD_H)
29 #if defined (PREFER_STDARG)
32 # if defined (PREFER_VARARGS)
53 # include "bashhist.h"
56 extern int interactive_shell, interactive;
57 extern char *dollar_vars[];
58 extern char *shell_name;
59 #if defined (JOB_CONTROL)
60 extern pid_t shell_pgrp;
61 extern int give_terminal_to ();
62 #endif /* JOB_CONTROL */
64 /* The current maintainer of the shell. You change this in the
66 #if !defined (MAINTAINER)
67 #define MAINTAINER "bash-maintainers@gnu.org"
70 char *the_current_maintainer = MAINTAINER;
72 /* Return the name of the shell or the shell script for error reporting. */
79 if (interactive_shell == 0)
80 name = dollar_vars[0];
81 if (name == 0 && shell_name && *shell_name)
82 name = base_pathname (shell_name);
93 /* Report an error having to do with FILENAME. This does not use
94 sys_error so the filename is not interpreted as a printf-style
100 report_error ("%s: %s", filename, strerror (errno));
103 #if !defined (USE_VARARGS)
105 programming_error (reason, arg1, arg2, arg3, arg4, arg5)
110 #if defined (JOB_CONTROL)
111 give_terminal_to (shell_pgrp);
112 #endif /* JOB_CONTROL */
114 report_error (reason, arg1, arg2);
116 #if defined (HISTORY)
117 if (remember_on_history)
119 h = last_history_line ();
120 fprintf (stderr, "last command: %s\n", h ? h : "(null)");
125 fprintf (stderr, "Report this to %s\n", the_current_maintainer);
128 fprintf (stderr, "Stopping myself...");
135 report_error (format, arg1, arg2, arg3, arg4, arg5)
138 fprintf (stderr, "%s: ", get_name_for_error ());
140 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
141 fprintf (stderr, "\n");
142 if (exit_immediately_on_error)
147 parser_error (lineno, format, arg1, arg2, arg3, arg4, arg5);
154 ename = get_name_for_error ();
155 iname = bash_input.name ? bash_input.name : "stdin";
158 fprintf (stderr, "%s: ", ename);
159 else if (interactive_shell)
160 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
161 else if (STREQ (ename, iname))
162 fprintf (stderr, "%s: line %d: ", ename, lineno);
164 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
166 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
167 fprintf (stderr, "\n");
169 if (exit_immediately_on_error)
174 fatal_error (format, arg1, arg2, arg3, arg4, arg5)
177 fprintf (stderr, "%s: ", get_name_for_error ());
179 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
180 fprintf (stderr, "\n");
186 internal_error (format, arg1, arg2, arg3, arg4, arg5)
189 fprintf (stderr, "%s: ", get_name_for_error ());
191 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
192 fprintf (stderr, "\n");
196 internal_warning (format, arg1, arg2, arg3, arg4, arg5)
199 fprintf (stderr, "%s: warning: ", get_name_for_error ());
201 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
202 fprintf (stderr, "\n");
206 sys_error (format, arg1, arg2, arg3, arg4, arg5)
209 fprintf (stderr, "%s: ", get_name_for_error ());
211 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
212 fprintf (stderr, ": %s\n", strerror (errno));
215 #else /* We have VARARGS support, so use it. */
218 #if defined (PREFER_STDARG)
219 programming_error (const char *format, ...)
221 programming_error (format, va_alist)
229 #if defined (JOB_CONTROL)
230 give_terminal_to (shell_pgrp);
231 #endif /* JOB_CONTROL */
233 #if defined (PREFER_STDARG)
234 va_start (args, format);
239 vfprintf (stderr, format, args);
240 fprintf (stderr, "\n");
243 #if defined (HISTORY)
244 if (remember_on_history)
246 h = last_history_line ();
247 fprintf (stderr, "last command: %s\n", h ? h : "(null)");
252 fprintf (stderr, "Report this to %s\n", the_current_maintainer);
255 fprintf (stderr, "Stopping myself...");
262 #if defined (PREFER_STDARG)
263 report_error (const char *format, ...)
265 report_error (format, va_alist)
272 fprintf (stderr, "%s: ", get_name_for_error ());
274 #if defined (PREFER_STDARG)
275 va_start (args, format);
280 vfprintf (stderr, format, args);
281 fprintf (stderr, "\n");
284 if (exit_immediately_on_error)
289 #if defined (PREFER_STDARG)
290 fatal_error (const char *format, ...)
292 fatal_error (format, va_alist)
299 fprintf (stderr, "%s: ", get_name_for_error ());
301 #if defined (PREFER_STDARG)
302 va_start (args, format);
307 vfprintf (stderr, format, args);
308 fprintf (stderr, "\n");
315 #if defined (PREFER_STDARG)
316 internal_error (const char *format, ...)
318 internal_error (format, va_alist)
325 fprintf (stderr, "%s: ", get_name_for_error ());
327 #if defined (PREFER_STDARG)
328 va_start (args, format);
333 vfprintf (stderr, format, args);
334 fprintf (stderr, "\n");
340 #if defined (PREFER_STDARG)
341 internal_warning (const char *format, ...)
343 internal_warning (format, va_alist)
350 fprintf (stderr, "%s: warning: ", get_name_for_error ());
352 #if defined (PREFER_STDARG)
353 va_start (args, format);
358 vfprintf (stderr, format, args);
359 fprintf (stderr, "\n");
365 #if defined (PREFER_STDARG)
366 sys_error (const char *format, ...)
368 sys_error (format, va_alist)
375 fprintf (stderr, "%s: ", get_name_for_error ());
377 #if defined (PREFER_STDARG)
378 va_start (args, format);
383 vfprintf (stderr, format, args);
384 fprintf (stderr, ": %s\n", strerror (errno));
389 /* An error from the parser takes the general form
391 shell_name: input file name: line number: message
393 The input file name and line number are omitted if the shell is
394 currently interactive. If the shell is not currently interactive,
395 the input file name is inserted only if it is different from the
398 #if defined (PREFER_STDARG)
399 parser_error (int lineno, const char *format, ...)
401 parser_error (lineno, format, va_alist)
410 ename = get_name_for_error ();
411 iname = bash_input.name ? bash_input.name : "stdin";
414 fprintf (stderr, "%s: ", ename);
415 else if (interactive_shell)
416 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
417 else if (STREQ (ename, iname))
418 fprintf (stderr, "%s: line %d: ", ename, lineno);
420 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
422 #if defined (PREFER_STDARG)
423 va_start (args, format);
428 vfprintf (stderr, format, args);
429 fprintf (stderr, "\n");
433 if (exit_immediately_on_error)
438 #if defined (PREFER_STDARG)
439 itrace (const char *format, ...)
441 itrace (format, va_alist)
448 fprintf(stderr, "TRACE: pid %d: ", (int)getpid());
450 #if defined (PREFER_STDARG)
451 va_start (args, format);
456 vfprintf (stderr, format, args);
457 fprintf (stderr, "\n");
464 /* A trace function for silent debugging -- doesn't require a control
467 #if defined (PREFER_STDARG)
468 trace (const char *format, ...)
470 trace (format, va_alist)
476 static FILE *tracefp = (FILE *)NULL;
479 tracefp = fopen("/tmp/bash-trace.log", "a+");
484 fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
486 fprintf(tracefp, "TRACE: pid %d: ", getpid());
488 #if defined (PREFER_STDARG)
489 va_start (args, format);
494 vfprintf (tracefp, format, args);
495 fprintf (tracefp, "\n");
502 #endif /* USE_VARARGS */
504 static char *cmd_error_table[] = {
505 "unknown command error", /* CMDERR_DEFAULT */
506 "bad command type", /* CMDERR_BADTYPE */
507 "bad connector", /* CMDERR_BADCONN */
508 "bad jump", /* CMDERR_BADJUMP */
513 command_error (func, code, e, flags)
515 int code, e, flags; /* flags currently unused */
517 if (code > CMDERR_LAST)
518 code = CMDERR_DEFAULT;
520 programming_error ("%s: %s: %d", func, cmd_error_table[code], e);
524 command_errstr (code)
527 if (code > CMDERR_LAST)
528 code = CMDERR_DEFAULT;
530 return (cmd_error_table[code]);