2
* Copyright (c) 1991, 1993
3
* The Regents of the University of California. All rights reserved.
4
* Copyright (c) 1997-2005
5
* Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7
* This code is derived from software contributed to Berkeley by
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
* Errors and exceptions.
59
* Code to handle exceptions in C.
62
struct jmploc *handler;
65
volatile sig_atomic_t intpending;
68
static void exverror(int, const char *, va_list)
69
__attribute__((__noreturn__));
72
* Called to raise an exception. Since C doesn't include exceptions, we
73
* just do a longjmp to the exception handler. The type of exception is
74
* stored in the global variable "exception".
87
longjmp(handler->loc, 1);
92
* Called from trap.c when a SIGINT is received. (If the user specifies
93
* that SIGINT is to be trapped or ignored using the trap builtin, then
94
* this routine is not called.) Suppressint is nonzero when interrupts
95
* are held using the INTOFF macro. (The test for iflag is just
96
* defensive programming.)
106
if (gotsig[SIGINT - 1] && !trap[SIGINT]) {
107
if (!(rootshell && iflag)) {
108
signal(SIGINT, SIG_DFL);
118
exvwarning2(const char *msg, va_list ap)
131
outfmt(errs, fmt, name, startlinno);
132
doformat(errs, msg, ap);
136
outcslow('\n', errs);
140
#define exvwarning(a, b, c) exvwarning2(b, c)
143
* Exverror is called to raise the error exception. If the second argument
144
* is not NULL then error prints an error message using printf style
145
* formatting. It then raises the error exception.
148
exverror(int cond, const char *msg, va_list ap)
152
TRACE(("exverror(%d, \"", cond));
154
TRACE(("\") pid=%d\n", getpid()));
156
TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
159
exvwarning(-1, msg, ap);
168
sh_error(const char *msg, ...)
173
exverror(EXERROR, msg, ap);
180
exerror(int cond, const char *msg, ...)
185
exverror(cond, msg, ap);
191
* error/warning routines for external builtins
195
sh_warnx(const char *fmt, ...)
200
exvwarning(-1, fmt, ap);
206
* Return a string describing an error. The returned string may be a
207
* pointer to a static buffer that will be overwritten on the next call.
208
* Action describes the operation that got the error.
212
errmsg(int e, int action)
214
if (e != ENOENT && e != ENOTDIR)
218
return "No such file";
219
else if (action & E_CREAT)
220
return "Directory nonexistent";
229
if (--suppressint == 0 && intpending) {