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, 675 Mass Ave, Cambridge, MA 02139, 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@prep.ai.mit.edu"
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)");
124 fprintf (stderr, "Report this to %s\n", the_current_maintainer);
125 fprintf (stderr, "Stopping myself...");
132 report_error (format, arg1, arg2, arg3, arg4, arg5)
135 fprintf (stderr, "%s: ", get_name_for_error ());
137 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
138 fprintf (stderr, "\n");
139 if (exit_immediately_on_error)
144 parser_error (lineno, format, arg1, arg2, arg3, arg4, arg5);
151 ename = get_name_for_error ();
152 iname = bash_input.name ? bash_input.name : "stdin";
155 fprintf (stderr, "%s: ", ename);
156 else if (interactive_shell)
157 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
158 else if (STREQ (ename, iname))
159 fprintf (stderr, "%s: line %d: ", ename, lineno);
161 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
163 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
164 fprintf (stderr, "\n");
166 if (exit_immediately_on_error)
171 fatal_error (format, arg1, arg2, arg3, arg4, arg5)
174 fprintf (stderr, "%s: ", get_name_for_error ());
176 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
177 fprintf (stderr, "\n");
183 internal_error (format, arg1, arg2, arg3, arg4, arg5)
186 fprintf (stderr, "%s: ", get_name_for_error ());
188 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
189 fprintf (stderr, "\n");
193 internal_warning (format, arg1, arg2, arg3, arg4, arg5)
196 fprintf (stderr, "%s: warning: ", get_name_for_error ());
198 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
199 fprintf (stderr, "\n");
203 sys_error (format, arg1, arg2, arg3, arg4, arg5)
206 fprintf (stderr, "%s: ", get_name_for_error ());
208 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
209 fprintf (stderr, ": %s\n", strerror (errno));
212 #else /* We have VARARGS support, so use it. */
215 #if defined (PREFER_STDARG)
216 programming_error (const char *format, ...)
218 programming_error (format, va_alist)
226 #if defined (JOB_CONTROL)
227 give_terminal_to (shell_pgrp);
228 #endif /* JOB_CONTROL */
230 #if defined (PREFER_STDARG)
231 va_start (args, format);
236 vfprintf (stderr, format, args);
237 fprintf (stderr, "\n");
240 #if defined (HISTORY)
241 if (remember_on_history)
243 h = last_history_line ();
244 fprintf (stderr, "last command: %s\n", h ? h : "(null)");
248 fprintf (stderr, "Report this to %s\n", the_current_maintainer);
249 fprintf (stderr, "Stopping myself...");
256 #if defined (PREFER_STDARG)
257 report_error (const char *format, ...)
259 report_error (format, va_alist)
266 fprintf (stderr, "%s: ", get_name_for_error ());
268 #if defined (PREFER_STDARG)
269 va_start (args, format);
274 vfprintf (stderr, format, args);
275 fprintf (stderr, "\n");
278 if (exit_immediately_on_error)
283 #if defined (PREFER_STDARG)
284 fatal_error (const char *format, ...)
286 fatal_error (format, va_alist)
293 fprintf (stderr, "%s: ", get_name_for_error ());
295 #if defined (PREFER_STDARG)
296 va_start (args, format);
301 vfprintf (stderr, format, args);
302 fprintf (stderr, "\n");
309 #if defined (PREFER_STDARG)
310 internal_error (const char *format, ...)
312 internal_error (format, va_alist)
319 fprintf (stderr, "%s: ", get_name_for_error ());
321 #if defined (PREFER_STDARG)
322 va_start (args, format);
327 vfprintf (stderr, format, args);
328 fprintf (stderr, "\n");
334 #if defined (PREFER_STDARG)
335 internal_warning (const char *format, ...)
337 internal_warning (format, va_alist)
344 fprintf (stderr, "%s: warning: ", get_name_for_error ());
346 #if defined (PREFER_STDARG)
347 va_start (args, format);
352 vfprintf (stderr, format, args);
353 fprintf (stderr, "\n");
359 #if defined (PREFER_STDARG)
360 sys_error (const char *format, ...)
362 sys_error (format, va_alist)
369 fprintf (stderr, "%s: ", get_name_for_error ());
371 #if defined (PREFER_STDARG)
372 va_start (args, format);
377 vfprintf (stderr, format, args);
378 fprintf (stderr, ": %s\n", strerror (errno));
383 /* An error from the parser takes the general form
385 shell_name: input file name: line number: message
387 The input file name and line number are omitted if the shell is
388 currently interactive. If the shell is not currently interactive,
389 the input file name is inserted only if it is different from the
392 #if defined (PREFER_STDARG)
393 parser_error (int lineno, const char *format, ...)
395 parser_error (lineno, format, va_alist)
404 ename = get_name_for_error ();
405 iname = bash_input.name ? bash_input.name : "stdin";
408 fprintf (stderr, "%s: ", ename);
409 else if (interactive_shell)
410 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
411 else if (STREQ (ename, iname))
412 fprintf (stderr, "%s: line %d: ", ename, lineno);
414 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
416 #if defined (PREFER_STDARG)
417 va_start (args, format);
422 vfprintf (stderr, format, args);
423 fprintf (stderr, "\n");
427 if (exit_immediately_on_error)
432 #if defined (PREFER_STDARG)
433 itrace (const char *format, ...)
435 itrace (format, va_alist)
442 fprintf(stderr, "TRACE: pid %d: ", (int)getpid());
444 #if defined (PREFER_STDARG)
445 va_start (args, format);
450 vfprintf (stderr, format, args);
451 fprintf (stderr, "\n");
459 /* A trace function for silent debugging -- doesn't require a control
462 #if defined (PREFER_STDARG)
463 trace (const char *format, ...)
465 trace (format, va_alist)
471 static FILE *tracefp = (FILE *)NULL;
474 tracefp = fopen("/usr/tmp/bash-trace.log", "a+");
479 fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
481 fprintf(tracefp, "TRACE: pid %d: ", getpid());
483 #if defined (PREFER_STDARG)
484 va_start (args, format);
489 vfprintf (tracefp, format, args);
490 fprintf (tracefp, "\n");
498 #endif /* USE_VARARGS */
500 static char *cmd_error_table[] = {
501 "unknown command error", /* CMDERR_DEFAULT */
502 "bad command type", /* CMDERR_BADTYPE */
503 "bad connector", /* CMDERR_BADCONN */
504 "bad jump", /* CMDERR_BADJUMP */
509 command_error (func, code, e, flags)
511 int code, e, flags; /* flags currently unused */
513 if (code > CMDERR_LAST)
514 code = CMDERR_DEFAULT;
516 programming_error ("%s: %s: %d", func, cmd_error_table[code], e);
520 command_errstr (code)
523 if (code > CMDERR_LAST)
524 code = CMDERR_DEFAULT;
526 return (cmd_error_table[code]);