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. */
23 #include <sys/types.h>
26 #if defined (HAVE_UNISTD_H)
30 #if defined (PREFER_STDARG)
33 # if defined (PREFER_VARARGS)
52 # include "bashhist.h"
55 extern int interactive_shell, interactive;
56 extern char *dollar_vars[];
57 extern char *shell_name;
58 #if defined (JOB_CONTROL)
59 extern pid_t shell_pgrp;
60 extern int give_terminal_to ();
61 #endif /* JOB_CONTROL */
63 /* The current maintainer of the shell. You change this in the
65 #if !defined (MAINTAINER)
66 #define MAINTAINER "bash-maintainers@prep.ai.mit.edu"
69 char *the_current_maintainer = MAINTAINER;
71 /* Return the name of the shell or the shell script for error reporting. */
78 if (interactive_shell == 0)
79 name = dollar_vars[0];
80 if (name == 0 && shell_name && *shell_name)
81 name = base_pathname (shell_name);
92 /* Report an error having to do with FILENAME. This does not use
93 sys_error so the filename is not interpreted as a printf-style
99 report_error ("%s: %s", filename, strerror (errno));
102 #if !defined (USE_VARARGS)
104 programming_error (reason, arg1, arg2, arg3, arg4, arg5)
109 #if defined (JOB_CONTROL)
110 give_terminal_to (shell_pgrp);
111 #endif /* JOB_CONTROL */
113 report_error (reason, arg1, arg2);
115 #if defined (HISTORY)
116 if (remember_on_history)
118 h = last_history_line ();
119 fprintf (stderr, "last command: %s\n", h ? h : "(null)");
123 fprintf (stderr, "Report this to %s\n", the_current_maintainer);
124 fprintf (stderr, "Stopping myself...");
131 report_error (format, arg1, arg2, arg3, arg4, arg5)
134 fprintf (stderr, "%s: ", get_name_for_error ());
136 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
137 fprintf (stderr, "\n");
138 if (exit_immediately_on_error)
143 parser_error (lineno, format, arg1, arg2, arg3, arg4, arg5);
150 ename = get_name_for_error ();
151 iname = bash_input.name ? bash_input.name : "stdin";
154 fprintf (stderr, "%s: ", ename);
155 else if (interactive_shell)
156 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
157 else if (STREQ (ename, iname))
158 fprintf (stderr, "%s: line %d: ", ename, lineno);
160 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
162 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
163 fprintf (stderr, "\n");
165 if (exit_immediately_on_error)
170 fatal_error (format, arg1, arg2, arg3, arg4, arg5)
173 fprintf (stderr, "%s: ", get_name_for_error ());
175 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
176 fprintf (stderr, "\n");
182 internal_error (format, arg1, arg2, arg3, arg4, arg5)
185 fprintf (stderr, "%s: ", get_name_for_error ());
187 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
188 fprintf (stderr, "\n");
192 sys_error (format, arg1, arg2, arg3, arg4, arg5)
195 fprintf (stderr, "%s: ", get_name_for_error ());
197 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
198 fprintf (stderr, ": %s\n", strerror (errno));
201 #else /* We have VARARGS support, so use it. */
204 #if defined (PREFER_STDARG)
205 programming_error (const char *format, ...)
207 programming_error (format, va_alist)
215 #if defined (JOB_CONTROL)
216 give_terminal_to (shell_pgrp);
217 #endif /* JOB_CONTROL */
219 #if defined (PREFER_STDARG)
220 va_start (args, format);
225 vfprintf (stderr, format, args);
226 fprintf (stderr, "\n");
229 #if defined (HISTORY)
230 if (remember_on_history)
232 h = last_history_line ();
233 fprintf (stderr, "last command: %s\n", h ? h : "(null)");
237 fprintf (stderr, "Tell %s to fix this someday.\n", the_current_maintainer);
238 fprintf (stderr, "Stopping myself...");
245 #if defined (PREFER_STDARG)
246 report_error (const char *format, ...)
248 report_error (format, va_alist)
255 fprintf (stderr, "%s: ", get_name_for_error ());
257 #if defined (PREFER_STDARG)
258 va_start (args, format);
263 vfprintf (stderr, format, args);
264 fprintf (stderr, "\n");
267 if (exit_immediately_on_error)
272 #if defined (PREFER_STDARG)
273 fatal_error (const char *format, ...)
275 fatal_error (format, va_alist)
282 fprintf (stderr, "%s: ", get_name_for_error ());
284 #if defined (PREFER_STDARG)
285 va_start (args, format);
290 vfprintf (stderr, format, args);
291 fprintf (stderr, "\n");
298 #if defined (PREFER_STDARG)
299 internal_error (const char *format, ...)
301 internal_error (format, va_alist)
308 fprintf (stderr, "%s: ", get_name_for_error ());
310 #if defined (PREFER_STDARG)
311 va_start (args, format);
316 vfprintf (stderr, format, args);
317 fprintf (stderr, "\n");
323 #if defined (PREFER_STDARG)
324 sys_error (const char *format, ...)
326 sys_error (format, va_alist)
333 fprintf (stderr, "%s: ", get_name_for_error ());
335 #if defined (PREFER_STDARG)
336 va_start (args, format);
341 vfprintf (stderr, format, args);
342 fprintf (stderr, ": %s\n", strerror (errno));
347 /* An error from the parser takes the general form
349 shell_name: input file name: line number: message
351 The input file name and line number are omitted if the shell is
352 currently interactive. If the shell is not currently interactive,
353 the input file name is inserted only if it is different from the
356 #if defined (PREFER_STDARG)
357 parser_error (int lineno, const char *format, ...)
359 parser_error (lineno, format, va_alist)
368 ename = get_name_for_error ();
369 iname = bash_input.name ? bash_input.name : "stdin";
372 fprintf (stderr, "%s: ", ename);
373 else if (interactive_shell)
374 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
375 else if (STREQ (ename, iname))
376 fprintf (stderr, "%s: line %d: ", ename, lineno);
378 fprintf (stderr, "%s: %s: line %d: ", ename, iname, lineno);
380 #if defined (PREFER_STDARG)
381 va_start (args, format);
386 vfprintf (stderr, format, args);
387 fprintf (stderr, "\n");
391 if (exit_immediately_on_error)
396 #if defined (PREFER_STDARG)
397 itrace (const char *format, ...)
399 itrace (format, va_alist)
406 fprintf(stderr, "TRACE: pid %d: ", (int)getpid());
408 #if defined (PREFER_STDARG)
409 va_start (args, format);
414 vfprintf (stderr, format, args);
415 fprintf (stderr, "\n");
423 /* A trace function for silent debugging -- doesn't require a control
426 #if defined (PREFER_STDARG)
427 trace (const char *format, ...)
429 trace (format, va_alist)
435 static FILE *tracefp = (FILE *)NULL;
438 tracefp = fopen("/usr/tmp/bash-trace.log", "a+");
443 fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
445 fprintf(tracefp, "TRACE: pid %d: ", getpid());
447 #if defined (PREFER_STDARG)
448 va_start (args, format);
453 vfprintf (tracefp, format, args);
454 fprintf (tracefp, "\n");
462 #endif /* USE_VARARGS */