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 sys_error (format, arg1, arg2, arg3, arg4, arg5)
196 fprintf (stderr, "%s: ", get_name_for_error ());
198 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
199 fprintf (stderr, ": %s\n", strerror (errno));
202 #else /* We have VARARGS support, so use it. */
205 #if defined (PREFER_STDARG)
206 programming_error (const char *format, ...)
208 programming_error (format, va_alist)
216 #if defined (JOB_CONTROL)
217 give_terminal_to (shell_pgrp);
218 #endif /* JOB_CONTROL */
220 #if defined (PREFER_STDARG)
221 va_start (args, format);
226 vfprintf (stderr, format, args);
227 fprintf (stderr, "\n");
230 #if defined (HISTORY)
231 if (remember_on_history)
233 h = last_history_line ();
234 fprintf (stderr, "last command: %s\n", h ? h : "(null)");
238 fprintf (stderr, "Report this to %s\n", the_current_maintainer);
239 fprintf (stderr, "Stopping myself...");
246 #if defined (PREFER_STDARG)
247 report_error (const char *format, ...)
249 report_error (format, va_alist)
256 fprintf (stderr, "%s: ", get_name_for_error ());
258 #if defined (PREFER_STDARG)
259 va_start (args, format);
264 vfprintf (stderr, format, args);
265 fprintf (stderr, "\n");
268 if (exit_immediately_on_error)
273 #if defined (PREFER_STDARG)
274 fatal_error (const char *format, ...)
276 fatal_error (format, va_alist)
283 fprintf (stderr, "%s: ", get_name_for_error ());
285 #if defined (PREFER_STDARG)
286 va_start (args, format);
291 vfprintf (stderr, format, args);
292 fprintf (stderr, "\n");
299 #if defined (PREFER_STDARG)
300 internal_error (const char *format, ...)
302 internal_error (format, va_alist)
309 fprintf (stderr, "%s: ", get_name_for_error ());
311 #if defined (PREFER_STDARG)
312 va_start (args, format);
317 vfprintf (stderr, format, args);
318 fprintf (stderr, "\n");
324 #if defined (PREFER_STDARG)
325 sys_error (const char *format, ...)
327 sys_error (format, va_alist)
334 fprintf (stderr, "%s: ", get_name_for_error ());
336 #if defined (PREFER_STDARG)
337 va_start (args, format);
342 vfprintf (stderr, format, args);
343 fprintf (stderr, ": %s\n", strerror (errno));
348 /* An error from the parser takes the general form
350 shell_name: input file name: line number: message
352 The input file name and line number are omitted if the shell is
353 currently interactive. If the shell is not currently interactive,
354 the input file name is inserted only if it is different from the
357 #if defined (PREFER_STDARG)
358 parser_error (int lineno, const char *format, ...)
360 parser_error (lineno, format, va_alist)
369 ename = get_name_for_error ();
370 iname = bash_input.name ? bash_input.name : "stdin";
373 fprintf (stderr, "%s: ", ename);
374 else if (interactive_shell)
375 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
376 else if (STREQ (ename, iname))
377 fprintf (stderr, "%s: line %d: ", ename, lineno);
379 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
381 #if defined (PREFER_STDARG)
382 va_start (args, format);
387 vfprintf (stderr, format, args);
388 fprintf (stderr, "\n");
392 if (exit_immediately_on_error)
397 #if defined (PREFER_STDARG)
398 itrace (const char *format, ...)
400 itrace (format, va_alist)
407 fprintf(stderr, "TRACE: pid %d: ", (int)getpid());
409 #if defined (PREFER_STDARG)
410 va_start (args, format);
415 vfprintf (stderr, format, args);
416 fprintf (stderr, "\n");
424 /* A trace function for silent debugging -- doesn't require a control
427 #if defined (PREFER_STDARG)
428 trace (const char *format, ...)
430 trace (format, va_alist)
436 static FILE *tracefp = (FILE *)NULL;
439 tracefp = fopen("/usr/tmp/bash-trace.log", "a+");
444 fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
446 fprintf(tracefp, "TRACE: pid %d: ", getpid());
448 #if defined (PREFER_STDARG)
449 va_start (args, format);
454 vfprintf (tracefp, format, args);
455 fprintf (tracefp, "\n");
463 #endif /* USE_VARARGS */