1 /*-
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.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Errors and exceptions.
37  */
38 
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <string.h>
45 
46 #include "jobs.h"
47 #include "shell.h"
48 #include "main.h"
49 #include "options.h"
50 #include "output.h"
51 #include "error.h"
52 #include "show.h"
53 #include "eval.h"
54 #include "parser.h"
55 #include "system.h"
56 
57 
58 /*
59  * Code to handle exceptions in C.
60  */
61 
62 struct jmploc *handler;
63 int exception;
64 int suppressint;
65 volatile sig_atomic_t intpending;
66 int errlinno;
67 
68 
69 static void exverror(int, const char *, va_list)
70     __attribute__((__noreturn__));
71 
72 /*
73  * Called to raise an exception.  Since C doesn't include exceptions, we
74  * just do a longjmp to the exception handler.  The type of exception is
75  * stored in the global variable "exception".
76  */
77 
78 void
exraise(int e)79 exraise(int e)
80 {
81 #ifdef DEBUG
82 	if (handler == NULL)
83 		abort();
84 #endif
85 
86 	if (vforked)
87 		_exit(exitstatus);
88 
89 	INTOFF;
90 
91 	exception = e;
92 	longjmp(handler->loc, 1);
93 }
94 
95 
96 /*
97  * Called from trap.c when a SIGINT is received.  (If the user specifies
98  * that SIGINT is to be trapped or ignored using the trap builtin, then
99  * this routine is not called.)  Suppressint is nonzero when interrupts
100  * are held using the INTOFF macro.  (The test for iflag is just
101  * defensive programming.)
102  */
103 
104 void
onint(void)105 onint(void) {
106 
107 	intpending = 0;
108 	sigclearmask();
109 	if (!(rootshell && iflag)) {
110 		signal(SIGINT, SIG_DFL);
111 		raise(SIGINT);
112 	}
113 	exitstatus = SIGINT + 128;
114 	exraise(EXINT);
115 	/* NOTREACHED */
116 }
117 
118 static void
exvwarning2(const char * msg,va_list ap)119 exvwarning2(const char *msg, va_list ap)
120 {
121 	struct output *errs;
122 	const char *name;
123 	const char *fmt;
124 
125 	errs = out2;
126 	name = arg0 ? arg0 : "sh";
127 	if (!commandname)
128 		fmt = "%s: %d: ";
129 	else
130 		fmt = "%s: %d: %s: ";
131 	outfmt(errs, fmt, name, errlinno, commandname);
132 	doformat(errs, msg, ap);
133 #if FLUSHERR
134 	outc('\n', errs);
135 #else
136 	outcslow('\n', errs);
137 #endif
138 }
139 
140 #define exvwarning(a, b, c) exvwarning2(b, c)
141 
142 /*
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.
146  */
147 static void
exverror(int cond,const char * msg,va_list ap)148 exverror(int cond, const char *msg, va_list ap)
149 {
150 #ifdef DEBUG
151 	if (msg) {
152 		va_list aq;
153 		TRACE(("exverror(%d, \"", cond));
154 		va_copy(aq, ap);
155 		TRACEV((msg, aq));
156 		va_end(aq);
157 		TRACE(("\") pid=%d\n", getpid()));
158 	} else
159 		TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
160 	if (msg)
161 #endif
162 		exvwarning(-1, msg, ap);
163 
164 	flushall();
165 	exraise(cond);
166 	/* NOTREACHED */
167 }
168 
169 
170 void
sh_error(const char * msg,...)171 sh_error(const char *msg, ...)
172 {
173 	va_list ap;
174 
175 	exitstatus = 2;
176 
177 	va_start(ap, msg);
178 	exverror(EXERROR, msg, ap);
179 	/* NOTREACHED */
180 	va_end(ap);
181 }
182 
183 
184 void
exerror(int cond,const char * msg,...)185 exerror(int cond, const char *msg, ...)
186 {
187 	va_list ap;
188 
189 	va_start(ap, msg);
190 	exverror(cond, msg, ap);
191 	/* NOTREACHED */
192 	va_end(ap);
193 }
194 
195 /*
196  * error/warning routines for external builtins
197  */
198 
199 void
sh_warnx(const char * fmt,...)200 sh_warnx(const char *fmt, ...)
201 {
202 	va_list ap;
203 
204 	va_start(ap, fmt);
205 	exvwarning(-1, fmt, ap);
206 	va_end(ap);
207 }
208 
209 
210 /*
211  * Return a string describing an error.  The returned string may be a
212  * pointer to a static buffer that will be overwritten on the next call.
213  * Action describes the operation that got the error.
214  */
215 
216 const char *
errmsg(int e,int action)217 errmsg(int e, int action)
218 {
219 	if (e != ENOENT && e != ENOTDIR)
220 		return strerror(e);
221 
222 	if (action & E_OPEN)
223 		return "No such file";
224 	else if (action & E_CREAT)
225 		return "Directory nonexistent";
226 	else
227 		return "not found";
228 }
229 
230 
231 #ifdef REALLY_SMALL
232 void
__inton()233 __inton() {
234 	if (--suppressint == 0 && intpending) {
235 		onint();
236 	}
237 }
238 #endif
239