Imported from ../bash-1.14.7.tar.gz.
[platform/upstream/bash.git] / error.c
1 /* error.c -- Functions for handling errors. */
2 /* Copyright (C) 1993 Free Software Foundation, Inc.
3
4    This file is part of GNU Bash, the Bourne Again SHell.
5
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
9    version.
10
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
14    for more details.
15
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. */
19
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23
24 #if defined (HAVE_VFPRINTF)
25 #include <varargs.h>
26 #endif
27
28 #include <errno.h>
29 #if !defined (errno)
30 extern int errno;
31 #endif /* !errno */
32
33 #include "bashansi.h"
34 #include "flags.h"
35 #include "error.h"
36 #include "command.h"
37 #include "general.h"
38
39 extern int interactive_shell;
40 extern char *dollar_vars[];
41 extern char *shell_name;
42 extern char *the_current_maintainer;
43 #if defined (JOB_CONTROL)
44 extern pid_t shell_pgrp;
45 #endif /* JOB_CONTROL */
46
47 /* Return the name of the shell or the shell script for error reporting. */
48 char *
49 get_name_for_error ()
50 {
51   char *name = (char *) NULL;
52
53   if (!interactive_shell)
54     name = dollar_vars[0];
55   if (!name && shell_name && *shell_name)
56     name = base_pathname (shell_name);
57   if (!name)
58     name = "bash";
59
60   return (name);
61 }
62
63 /* Report an error having to do with FILENAME. */
64 void
65 file_error (filename)
66      char *filename;
67 {
68   report_error ("%s: %s", filename, strerror (errno));
69 }
70
71 #if !defined (HAVE_VFPRINTF)
72 void
73 programming_error (reason, arg1, arg2, arg3, arg4, arg5)
74      char *reason;
75 {
76 #if defined (JOB_CONTROL)
77   give_terminal_to (shell_pgrp);
78 #endif /* JOB_CONTROL */
79
80   report_error (reason, arg1, arg2);
81   fprintf (stderr, "Report this to %s\n", the_current_maintainer);
82   fprintf (stderr, "Stopping myself...");
83   fflush (stderr);
84   abort ();
85 }
86
87 void
88 report_error (format, arg1, arg2, arg3, arg4, arg5)
89      char *format;
90 {
91   fprintf (stderr, "%s: ", get_name_for_error ());
92
93   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
94   fprintf (stderr, "\n");
95   if (exit_immediately_on_error)
96     exit (1);
97 }  
98
99 void
100 fatal_error (format, arg1, arg2, arg3, arg4, arg5)
101      char *format;
102 {
103   fprintf (stderr, "%s: ", get_name_for_error ());
104
105   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
106   fprintf (stderr, "\n");
107
108   exit (2);
109 }
110
111 void
112 internal_error (format, arg1, arg2, arg3, arg4, arg5)
113      char *format;
114 {
115   fprintf (stderr, "%s: ", get_name_for_error ());
116
117   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
118   fprintf (stderr, "\n");
119 }
120
121 #else /* We have VARARGS support, so use it. */
122
123 void
124 programming_error (va_alist)
125      va_dcl
126 {
127   va_list args;
128   char *format;
129
130 #if defined (JOB_CONTROL)
131   give_terminal_to (shell_pgrp);
132 #endif /* JOB_CONTROL */
133
134   va_start (args);
135   format = va_arg (args, char *);
136   vfprintf (stderr, format, args);
137   fprintf (stderr, "\n");
138   va_end (args);
139
140   fprintf (stderr, "Tell %s to fix this someday.\n", the_current_maintainer);
141   fprintf (stderr, "Stopping myself...");
142   fflush (stderr);
143   abort ();
144 }
145
146 void
147 report_error (va_alist)
148      va_dcl
149 {
150   va_list args;
151   char *format;
152
153   fprintf (stderr, "%s: ", get_name_for_error ());
154   va_start (args);
155   format = va_arg (args, char *);
156   vfprintf (stderr, format, args);
157   fprintf (stderr, "\n");
158
159   va_end (args);
160   if (exit_immediately_on_error)
161     exit (1);
162 }
163
164 void
165 fatal_error (va_alist)
166      va_dcl
167 {
168   va_list args;
169   char *format;
170
171   fprintf (stderr, "%s: ", get_name_for_error ());
172   va_start (args);
173   format = va_arg (args, char *);
174   vfprintf (stderr, format, args);
175   fprintf (stderr, "\n");
176
177   va_end (args);
178   exit (2);
179 }
180
181 void
182 internal_error (va_alist)
183      va_dcl
184 {
185   va_list args;
186   char *format;
187
188   fprintf (stderr, "%s: ", get_name_for_error ());
189   va_start (args);
190   format = va_arg (args, char *);
191   vfprintf (stderr, format, args);
192   fprintf (stderr, "\n");
193
194   va_end (args);
195 }
196
197 itrace (va_alist)
198      va_dcl
199 {
200   va_list args;
201   char *format;
202
203   fprintf(stderr, "TRACE: pid %d: ", getpid());
204   va_start (args);
205   format = va_arg (args, char *);
206   vfprintf (stderr, format, args);
207   fprintf (stderr, "\n");
208
209   va_end (args);
210
211   fflush(stderr);
212 }
213
214 #if 0
215 /* A trace function for silent debugging -- doesn't require a control
216    terminal. */
217 trace (va_alist)
218      va_dcl
219 {
220   va_list args;
221   char *format;
222   static FILE *tracefp = (FILE *)NULL;
223
224   if (tracefp == NULL)
225     tracefp = fopen("/usr/tmp/bash-trace.log", "a+");
226
227   if (tracefp == NULL)
228     tracefp = stderr;
229   else
230     fcntl (fileno (tracefp), F_SETFD, 1);     /* close-on-exec */
231
232   fprintf(tracefp, "TRACE: pid %d: ", getpid());
233
234   va_start (args);
235   format = va_arg (args, char *);
236   vfprintf (tracefp, format, args);
237   fprintf (tracefp, "\n");
238
239   va_end (args);
240
241   fflush(tracefp);
242 }
243 #endif
244 #endif /* HAVE_VFPRINTF */