xref: /netbsd-src/bin/sh/error.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: error.c,v 1.17 1997/07/04 21:01:54 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
43 #else
44 __RCSID("$NetBSD: error.c,v 1.17 1997/07/04 21:01:54 christos Exp $");
45 #endif
46 #endif /* not lint */
47 
48 /*
49  * Errors and exceptions.
50  */
51 
52 #include "shell.h"
53 #include "main.h"
54 #include "options.h"
55 #include "output.h"
56 #include "error.h"
57 #include "show.h"
58 #include <signal.h>
59 #include <unistd.h>
60 #include <errno.h>
61 
62 
63 /*
64  * Code to handle exceptions in C.
65  */
66 
67 struct jmploc *handler;
68 int exception;
69 volatile int suppressint;
70 volatile int intpending;
71 char *commandname;
72 
73 
74 static void exverror __P((int, char *, va_list)) __attribute__((__noreturn__));
75 
76 /*
77  * Called to raise an exception.  Since C doesn't include exceptions, we
78  * just do a longjmp to the exception handler.  The type of exception is
79  * stored in the global variable "exception".
80  */
81 
82 void
83 exraise(e)
84 	int e;
85 {
86 	if (handler == NULL)
87 		abort();
88 	exception = e;
89 	longjmp(handler->loc, 1);
90 }
91 
92 
93 /*
94  * Called from trap.c when a SIGINT is received.  (If the user specifies
95  * that SIGINT is to be trapped or ignored using the trap builtin, then
96  * this routine is not called.)  Suppressint is nonzero when interrupts
97  * are held using the INTOFF macro.  The call to _exit is necessary because
98  * there is a short period after a fork before the signal handlers are
99  * set to the appropriate value for the child.  (The test for iflag is
100  * just defensive programming.)
101  */
102 
103 void
104 onint() {
105 	sigset_t sigset;
106 
107 	if (suppressint) {
108 		intpending++;
109 		return;
110 	}
111 	intpending = 0;
112 	sigemptyset(&sigset);
113 	sigprocmask(SIG_SETMASK, &sigset, NULL);
114 	if (rootshell && iflag)
115 		exraise(EXINT);
116 	else
117 		_exit(128 + SIGINT);
118 }
119 
120 
121 /*
122  * Exverror is called to raise the error exception.  If the first argument
123  * is not NULL then error prints an error message using printf style
124  * formatting.  It then raises the error exception.
125  */
126 static void
127 exverror(cond, msg, ap)
128 	int cond;
129 	char *msg;
130 	va_list ap;
131 {
132 	CLEAR_PENDING_INT;
133 	INTOFF;
134 
135 #ifdef DEBUG
136 	if (msg)
137 		TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
138 	else
139 		TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
140 #endif
141 	if (msg) {
142 		if (commandname)
143 			outfmt(&errout, "%s: ", commandname);
144 		doformat(&errout, msg, ap);
145 		out2c('\n');
146 	}
147 	flushall();
148 	exraise(cond);
149 }
150 
151 
152 #ifdef __STDC__
153 void
154 error(char *msg, ...)
155 #else
156 void
157 error(va_alist)
158 	va_dcl
159 #endif
160 {
161 #ifndef __STDC__
162 	char *msg;
163 #endif
164 	va_list ap;
165 #ifdef __STDC__
166 	va_start(ap, msg);
167 #else
168 	va_start(ap);
169 	msg = va_arg(ap, char *);
170 #endif
171 	exverror(EXERROR, msg, ap);
172 	va_end(ap);
173 }
174 
175 
176 #ifdef __STDC__
177 void
178 exerror(int cond, char *msg, ...)
179 #else
180 void
181 exerror(va_alist)
182 	va_dcl
183 #endif
184 {
185 #ifndef __STDC__
186 	int cond;
187 	char *msg;
188 #endif
189 	va_list ap;
190 #ifdef __STDC__
191 	va_start(ap, msg);
192 #else
193 	va_start(ap);
194 	cond = va_arg(ap, int);
195 	msg = va_arg(ap, char *);
196 #endif
197 	exverror(cond, msg, ap);
198 	va_end(ap);
199 }
200 
201 
202 
203 /*
204  * Table of error messages.
205  */
206 
207 struct errname {
208 	short errcode;		/* error number */
209 	short action;		/* operation which encountered the error */
210 	char *msg;		/* text describing the error */
211 };
212 
213 
214 #define ALL (E_OPEN|E_CREAT|E_EXEC)
215 
216 STATIC const struct errname errormsg[] = {
217 	{ EINTR,	ALL,	"interrupted" },
218 	{ EACCES,	ALL,	"permission denied" },
219 	{ EIO,		ALL,	"I/O error" },
220 	{ ENOENT,	E_OPEN,	"no such file" },
221 	{ ENOENT,	E_CREAT,"directory nonexistent" },
222 	{ ENOENT,	E_EXEC,	"not found" },
223 	{ ENOTDIR,	E_OPEN,	"no such file" },
224 	{ ENOTDIR,	E_CREAT,"directory nonexistent" },
225 	{ ENOTDIR,	E_EXEC,	"not found" },
226 	{ EISDIR,	ALL,	"is a directory" },
227 #ifdef notdef
228 	{ EMFILE,	ALL,	"too many open files" },
229 #endif
230 	{ ENFILE,	ALL,	"file table overflow" },
231 	{ ENOSPC,	ALL,	"file system full" },
232 #ifdef EDQUOT
233 	{ EDQUOT,	ALL,	"disk quota exceeded" },
234 #endif
235 #ifdef ENOSR
236 	{ ENOSR,	ALL,	"no streams resources" },
237 #endif
238 	{ ENXIO,	ALL,	"no such device or address" },
239 	{ EROFS,	ALL,	"read-only file system" },
240 	{ ETXTBSY,	ALL,	"text busy" },
241 #ifdef SYSV
242 	{ EAGAIN,	E_EXEC,	"not enough memory" },
243 #endif
244 	{ ENOMEM,	ALL,	"not enough memory" },
245 #ifdef ENOLINK
246 	{ ENOLINK,	ALL,	"remote access failed" },
247 #endif
248 #ifdef EMULTIHOP
249 	{ EMULTIHOP,	ALL,	"remote access failed" },
250 #endif
251 #ifdef ECOMM
252 	{ ECOMM,	ALL,	"remote access failed" },
253 #endif
254 #ifdef ESTALE
255 	{ ESTALE,	ALL,	"remote access failed" },
256 #endif
257 #ifdef ETIMEDOUT
258 	{ ETIMEDOUT,	ALL,	"remote access failed" },
259 #endif
260 #ifdef ELOOP
261 	{ ELOOP,	ALL,	"symbolic link loop" },
262 #endif
263 	{ E2BIG,	E_EXEC,	"argument list too long" },
264 #ifdef ELIBACC
265 	{ ELIBACC,	E_EXEC,	"shared library missing" },
266 #endif
267 	{ 0,		0,	NULL },
268 };
269 
270 
271 /*
272  * Return a string describing an error.  The returned string may be a
273  * pointer to a static buffer that will be overwritten on the next call.
274  * Action describes the operation that got the error.
275  */
276 
277 char *
278 errmsg(e, action)
279 	int e;
280 	int action;
281 {
282 	struct errname const *ep;
283 	static char buf[12];
284 
285 	for (ep = errormsg ; ep->errcode ; ep++) {
286 		if (ep->errcode == e && (ep->action & action) != 0)
287 			return ep->msg;
288 	}
289 	fmtstr(buf, sizeof buf, "error %d", e);
290 	return buf;
291 }
292