OpenCoverage

error.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/error.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* error.c -- Functions for handling errors. */-
2-
3/* Copyright (C) 1993-2009 Free Software Foundation, Inc.-
4-
5 This file is part of GNU Bash, the Bourne Again SHell.-
6-
7 Bash is free software: you can redistribute it and/or modify-
8 it under the terms of the GNU General Public License as published by-
9 the Free Software Foundation, either version 3 of the License, or-
10 (at your option) any later version.-
11-
12 Bash is distributed in the hope that it will be useful,-
13 but WITHOUT ANY WARRANTY; without even the implied warranty of-
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
15 GNU General Public License for more details.-
16-
17 You should have received a copy of the GNU General Public License-
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.-
19*/-
20-
21#include "config.h"-
22-
23#include "bashtypes.h"-
24#include <fcntl.h>-
25-
26#if defined (HAVE_UNISTD_H)-
27# include <unistd.h>-
28#endif-
29-
30#if defined (PREFER_STDARG)-
31# include <stdarg.h>-
32#else-
33# include <varargs.h>-
34#endif-
35-
36#include <stdio.h>-
37-
38#include <errno.h>-
39#if !defined (errno)-
40extern int errno;-
41#endif /* !errno */-
42-
43#include "bashansi.h"-
44#include "bashintl.h"-
45-
46#include "shell.h"-
47#include "execute_cmd.h"-
48#include "flags.h"-
49#include "input.h"-
50-
51#if defined (HISTORY)-
52# include "bashhist.h"-
53#endif-
54-
55extern int executing_line_number __P((void));-
56-
57#if defined (JOB_CONTROL)-
58extern pid_t shell_pgrp;-
59extern int give_terminal_to __P((pid_t, int));-
60#endif /* JOB_CONTROL */-
61-
62#if defined (ARRAY_VARS)-
63extern const char * const bash_badsub_errmsg;-
64#endif-
65-
66static void error_prolog __P((int));-
67-
68/* The current maintainer of the shell. You change this in the-
69 Makefile. */-
70#if !defined (MAINTAINER)-
71#define MAINTAINER "bash-maintainers@gnu.org"-
72#endif-
73-
74const char * const the_current_maintainer = MAINTAINER;-
75-
76int gnu_error_format = 0;-
77-
78static void-
79error_prolog (print_lineno)-
80 int print_lineno;-
81{-
82 char *ename;-
83 int line;-
84-
85 ename = get_name_for_error ();-
86 line = (print_lineno && interactive_shell == 0) ? executing_line_number () : -1;
print_linenoDescription
TRUEevaluated 1158 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
interactive_shell == 0Description
TRUEevaluated 1156 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
2-1158
87-
88 if (line > 0)
line > 0Description
TRUEevaluated 1081 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 80 times by 1 test
Evaluated by:
  • Self test
80-1081
89 fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : _(" line "), line);
executed 1081 times by 1 test: fprintf ( stderr , "%s:%s%d: ", ename, gnu_error_format ? "" : dcgettext (((void *)0), " line " , 5) , line);
Executed by:
  • Self test
1081
90 else-
91 fprintf (stderr, "%s: ", ename);
executed 80 times by 1 test: fprintf ( stderr , "%s: ", ename);
Executed by:
  • Self test
80
92}-
93-
94/* Return the name of the shell or the shell script for error reporting. */-
95char *-
96get_name_for_error ()-
97{-
98 char *name;-
99#if defined (ARRAY_VARS)-
100 SHELL_VAR *bash_source_v;-
101 ARRAY *bash_source_a;-
102#endif-
103-
104 name = (char *)NULL;-
105 if (interactive_shell == 0)
interactive_shell == 0Description
TRUEevaluated 1968 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
5-1968
106 {-
107#if defined (ARRAY_VARS)-
108 bash_source_v = find_variable ("BASH_SOURCE");-
109 if (bash_source_v && array_p (bash_source_v) &&
bash_source_vDescription
TRUEevaluated 1935 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 33 times by 1 test
Evaluated by:
  • Self test
((((bash_sourc... (0x0000004)))Description
TRUEevaluated 1935 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-1935
110 (bash_source_a = array_cell (bash_source_v)))
(bash_source_a...ce_v)->value))Description
TRUEevaluated 1935 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-1935
111 name = array_reference (bash_source_a, 0);
executed 1935 times by 1 test: name = array_reference (bash_source_a, 0);
Executed by:
  • Self test
1935
112 if (name == 0 || *name == '\0') /* XXX - was just name == 0 */
name == 0Description
TRUEevaluated 100 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1868 times by 1 test
Evaluated by:
  • Self test
*name == '\0'Description
TRUEnever evaluated
FALSEevaluated 1868 times by 1 test
Evaluated by:
  • Self test
0-1868
113#endif-
114 name = dollar_vars[0];
executed 100 times by 1 test: name = dollar_vars[0];
Executed by:
  • Self test
100
115 }
executed 1968 times by 1 test: end of block
Executed by:
  • Self test
1968
116 if (name == 0 && shell_name && *shell_name)
name == 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1968 times by 1 test
Evaluated by:
  • Self test
shell_nameDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
*shell_nameDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-1968
117 name = base_pathname (shell_name);
executed 5 times by 1 test: name = base_pathname (shell_name);
Executed by:
  • Self test
5
118 if (name == 0)
name == 0Description
TRUEnever evaluated
FALSEevaluated 1973 times by 1 test
Evaluated by:
  • Self test
0-1973
119#if defined (PROGRAM)-
120 name = PROGRAM;
never executed: name = "bash";
0
121#else-
122 name = "bash";-
123#endif-
124-
125 return (name);
executed 1973 times by 1 test: return (name);
Executed by:
  • Self test
1973
126}-
127-
128/* Report an error having to do with FILENAME. This does not use-
129 sys_error so the filename is not interpreted as a printf-style-
130 format string. */-
131void-
132file_error (filename)-
133 const char *filename;-
134{-
135 report_error ("%s: %s", filename, strerror (errno));-
136}
executed 11 times by 1 test: end of block
Executed by:
  • Self test
11
137-
138void-
139#if defined (PREFER_STDARG)-
140programming_error (const char *format, ...)-
141#else-
142programming_error (format, va_alist)-
143 const char *format;-
144 va_dcl-
145#endif-
146{-
147 va_list args;-
148 char *h;-
149-
150#if defined (JOB_CONTROL)-
151 give_terminal_to (shell_pgrp, 0);-
152#endif /* JOB_CONTROL */-
153-
154 SH_VA_START (args, format);-
155-
156 vfprintf (stderr, format, args);-
157 fprintf (stderr, "\n");-
158 va_end (args);-
159-
160#if defined (HISTORY)-
161 if (remember_on_history)
remember_on_historyDescription
TRUEnever evaluated
FALSEnever evaluated
0
162 {-
163 h = last_history_line ();-
164 fprintf (stderr, _("last command: %s\n"), h ? h : "(null)");-
165 }
never executed: end of block
0
166#endif-
167-
168#if 0-
169 fprintf (stderr, "Report this to %s\n", the_current_maintainer);-
170#endif-
171-
172 fprintf (stderr, _("Aborting..."));-
173 fflush (stderr);-
174-
175 abort ();
never executed: abort ();
0
176}-
177-
178/* Print an error message and, if `set -e' has been executed, exit the-
179 shell. Used in this file by file_error and programming_error. Used-
180 outside this file mostly to report substitution and expansion errors,-
181 and for bad invocation options. */-
182void-
183#if defined (PREFER_STDARG)-
184report_error (const char *format, ...)-
185#else-
186report_error (format, va_alist)-
187 const char *format;-
188 va_dcl-
189#endif-
190{-
191 va_list args;-
192-
193 error_prolog (1);-
194-
195 SH_VA_START (args, format);-
196-
197 vfprintf (stderr, format, args);-
198 fprintf (stderr, "\n");-
199-
200 va_end (args);-
201 if (exit_immediately_on_error)
exit_immediately_on_errorDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 335 times by 1 test
Evaluated by:
  • Self test
1-335
202 {-
203 if (last_command_exit_value == 0)
last_command_exit_value == 0Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
0-1
204 last_command_exit_value = 1;
never executed: last_command_exit_value = 1;
0
205 exit_shell (last_command_exit_value);-
206 }
never executed: end of block
0
207}
executed 335 times by 1 test: end of block
Executed by:
  • Self test
335
208-
209void-
210#if defined (PREFER_STDARG)-
211fatal_error (const char *format, ...)-
212#else-
213fatal_error (format, va_alist)-
214 const char *format;-
215 va_dcl-
216#endif-
217{-
218 va_list args;-
219-
220 error_prolog (0);-
221-
222 SH_VA_START (args, format);-
223-
224 vfprintf (stderr, format, args);-
225 fprintf (stderr, "\n");-
226-
227 va_end (args);-
228 sh_exit (2);-
229}
never executed: end of block
0
230-
231void-
232#if defined (PREFER_STDARG)-
233internal_error (const char *format, ...)-
234#else-
235internal_error (format, va_alist)-
236 const char *format;-
237 va_dcl-
238#endif-
239{-
240 va_list args;-
241-
242 error_prolog (1);-
243-
244 SH_VA_START (args, format);-
245-
246 vfprintf (stderr, format, args);-
247 fprintf (stderr, "\n");-
248-
249 va_end (args);-
250}
executed 381 times by 1 test: end of block
Executed by:
  • Self test
381
251-
252void-
253#if defined (PREFER_STDARG)-
254internal_warning (const char *format, ...)-
255#else-
256internal_warning (format, va_alist)-
257 const char *format;-
258 va_dcl-
259#endif-
260{-
261 va_list args;-
262-
263 error_prolog (1);-
264 fprintf (stderr, _("warning: "));-
265-
266 SH_VA_START (args, format);-
267-
268 vfprintf (stderr, format, args);-
269 fprintf (stderr, "\n");-
270-
271 va_end (args);-
272}
executed 439 times by 1 test: end of block
Executed by:
  • Self test
439
273-
274void-
275#if defined (PREFER_STDARG)-
276internal_inform (const char *format, ...)-
277#else-
278internal_inform (format, va_alist)-
279 const char *format;-
280 va_dcl-
281#endif-
282{-
283 va_list args;-
284-
285 error_prolog (1);-
286 /* TRANSLATORS: this is a prefix for informational messages. */-
287 fprintf (stderr, _("INFORM: "));-
288-
289 SH_VA_START (args, format);-
290-
291 vfprintf (stderr, format, args);-
292 fprintf (stderr, "\n");-
293-
294 va_end (args);-
295}
executed 2 times by 1 test: end of block
Executed by:
  • Self test
2
296-
297void-
298#if defined (PREFER_STDARG)-
299sys_error (const char *format, ...)-
300#else-
301sys_error (format, va_alist)-
302 const char *format;-
303 va_dcl-
304#endif-
305{-
306 int e;-
307 va_list args;-
308-
309 e = errno;-
310 error_prolog (0);-
311-
312 SH_VA_START (args, format);-
313-
314 vfprintf (stderr, format, args);-
315 fprintf (stderr, ": %s\n", strerror (e));-
316-
317 va_end (args);-
318}
executed 3 times by 1 test: end of block
Executed by:
  • Self test
3
319-
320/* An error from the parser takes the general form-
321-
322 shell_name: input file name: line number: message-
323-
324 The input file name and line number are omitted if the shell is-
325 currently interactive. If the shell is not currently interactive,-
326 the input file name is inserted only if it is different from the-
327 shell name. */-
328void-
329#if defined (PREFER_STDARG)-
330parser_error (int lineno, const char *format, ...)-
331#else-
332parser_error (lineno, format, va_alist)-
333 int lineno;-
334 const char *format;-
335 va_dcl-
336#endif-
337{-
338 va_list args;-
339 char *ename, *iname;-
340-
341 ename = get_name_for_error ();-
342 iname = yy_input_name ();-
343-
344 if (interactive)
interactiveDescription
TRUEnever evaluated
FALSEevaluated 91 times by 1 test
Evaluated by:
  • Self test
0-91
345 fprintf (stderr, "%s: ", ename);
never executed: fprintf ( stderr , "%s: ", ename);
0
346 else if (interactive_shell)
interactive_shellDescription
TRUEnever evaluated
FALSEevaluated 91 times by 1 test
Evaluated by:
  • Self test
0-91
347 fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : _(" line "), lineno);
never executed: fprintf ( stderr , "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : dcgettext (((void *)0), " line " , 5) , lineno);
0
348 else if (STREQ (ename, iname))
never executed: __result = (((const unsigned char *) (const char *) ( ename ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( iname ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
(ename)[0] == (iname)[0]Description
TRUEevaluated 46 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 45 times by 1 test
Evaluated by:
  • Self test
__extension__ ... )))); }) == 0Description
TRUEevaluated 46 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-46
349 fprintf (stderr, "%s:%s%d: ", ename, gnu_error_format ? "" : _(" line "), lineno);
executed 46 times by 1 test: fprintf ( stderr , "%s:%s%d: ", ename, gnu_error_format ? "" : dcgettext (((void *)0), " line " , 5) , lineno);
Executed by:
  • Self test
46
350 else-
351 fprintf (stderr, "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : _(" line "), lineno);
executed 45 times by 1 test: fprintf ( stderr , "%s: %s:%s%d: ", ename, iname, gnu_error_format ? "" : dcgettext (((void *)0), " line " , 5) , lineno);
Executed by:
  • Self test
45
352-
353 SH_VA_START (args, format);-
354-
355 vfprintf (stderr, format, args);-
356 fprintf (stderr, "\n");-
357-
358 va_end (args);-
359-
360 if (exit_immediately_on_error)
exit_immediately_on_errorDescription
TRUEnever evaluated
FALSEevaluated 91 times by 1 test
Evaluated by:
  • Self test
0-91
361 exit_shell (last_command_exit_value = 2);
never executed: exit_shell (last_command_exit_value = 2);
0
362}
executed 91 times by 1 test: end of block
Executed by:
  • Self test
91
363-
364#ifdef DEBUG-
365/* This assumes ASCII and is suitable only for debugging */-
366char *-
367strescape (str)-
368 const char *str;-
369{-
370 char *r, *result;-
371 unsigned char *s;-
372-
373 r = result = (char *)xmalloc (strlen (str) * 2 + 1);-
374-
375 for (s = (unsigned char *)str; s && *s; s++)
sDescription
TRUEnever evaluated
FALSEnever evaluated
*sDescription
TRUEnever evaluated
FALSEnever evaluated
0
376 {-
377 if (*s < ' ')
*s < ' 'Description
TRUEnever evaluated
FALSEnever evaluated
0
378 {-
379 *r++ = '^';-
380 *r++ = *s+64;-
381 }
never executed: end of block
0
382 else if (*s == 127)
*s == 127Description
TRUEnever evaluated
FALSEnever evaluated
0
383 {-
384 *r++ = '^';-
385 *r++ = '?';-
386 }
never executed: end of block
0
387 else-
388 *r++ = *s;
never executed: *r++ = *s;
0
389 }-
390-
391 *r = '\0';-
392 return result;
never executed: return result;
0
393}-
394-
395void-
396#if defined (PREFER_STDARG)-
397itrace (const char *format, ...)-
398#else-
399itrace (format, va_alist)-
400 const char *format;-
401 va_dcl-
402#endif-
403{-
404 va_list args;-
405-
406 fprintf(stderr, "TRACE: pid %ld: ", (long)getpid());-
407-
408 SH_VA_START (args, format);-
409-
410 vfprintf (stderr, format, args);-
411 fprintf (stderr, "\n");-
412-
413 va_end (args);-
414-
415 fflush(stderr);-
416}
never executed: end of block
0
417-
418/* A trace function for silent debugging -- doesn't require a control-
419 terminal. */-
420void-
421#if defined (PREFER_STDARG)-
422trace (const char *format, ...)-
423#else-
424trace (format, va_alist)-
425 const char *format;-
426 va_dcl-
427#endif-
428{-
429 va_list args;-
430 static FILE *tracefp = (FILE *)NULL;-
431-
432 if (tracefp == NULL)
tracefp == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
433 tracefp = fopen("/tmp/bash-trace.log", "a+");
never executed: tracefp = fopen("/tmp/bash-trace.log", "a+");
0
434-
435 if (tracefp == NULL)
tracefp == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
436 tracefp = stderr;
never executed: tracefp = stderr ;
0
437 else-
438 fcntl (fileno (tracefp), F_SETFD, 1); /* close-on-exec */
never executed: fcntl (fileno (tracefp), 2 , 1);
0
439-
440 fprintf(tracefp, "TRACE: pid %ld: ", (long)getpid());-
441-
442 SH_VA_START (args, format);-
443-
444 vfprintf (tracefp, format, args);-
445 fprintf (tracefp, "\n");-
446-
447 va_end (args);-
448-
449 fflush(tracefp);-
450}
never executed: end of block
0
451-
452#endif /* DEBUG */-
453-
454/* **************************************************************** */-
455/* */-
456/* Common error reporting */-
457/* */-
458/* **************************************************************** */-
459-
460-
461static const char * const cmd_error_table[] = {-
462 N_("unknown command error"), /* CMDERR_DEFAULT */-
463 N_("bad command type"), /* CMDERR_BADTYPE */-
464 N_("bad connector"), /* CMDERR_BADCONN */-
465 N_("bad jump"), /* CMDERR_BADJUMP */-
466 0-
467};-
468-
469void-
470command_error (func, code, e, flags)-
471 const char *func;-
472 int code, e, flags; /* flags currently unused */-
473{-
474 if (code > CMDERR_LAST)
code > 3Description
TRUEnever evaluated
FALSEnever evaluated
0
475 code = CMDERR_DEFAULT;
never executed: code = 0;
0
476-
477 programming_error ("%s: %s: %d", func, _(cmd_error_table[code]), e);-
478}
never executed: end of block
0
479-
480char *-
481command_errstr (code)-
482 int code;-
483{-
484 if (code > CMDERR_LAST)
code > 3Description
TRUEnever evaluated
FALSEnever evaluated
0
485 code = CMDERR_DEFAULT;
never executed: code = 0;
0
486-
487 return (_(cmd_error_table[code]));
never executed: return ( dcgettext (((void *)0), cmd_error_table[code] , 5) );
0
488}-
489-
490#ifdef ARRAY_VARS-
491void-
492err_badarraysub (s)-
493 const char *s;-
494{-
495 report_error ("%s: %s", s, _(bash_badsub_errmsg));-
496}
executed 67 times by 1 test: end of block
Executed by:
  • Self test
67
497#endif-
498-
499void-
500err_unboundvar (s)-
501 const char *s;-
502{-
503 report_error (_("%s: unbound variable"), s);-
504}
executed 34 times by 1 test: end of block
Executed by:
  • Self test
34
505-
506void-
507err_readonly (s)-
508 const char *s;-
509{-
510 report_error (_("%s: readonly variable"), s);-
511}
executed 82 times by 1 test: end of block
Executed by:
  • Self test
82
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2