xref: /netbsd-src/bin/sh/error.c (revision 37b34d511dea595d3ba03a661cf3b775038ea5f8)
1 /*	$NetBSD: error.c,v 1.27 2002/09/27 21:32:24 mycroft 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.27 2002/09/27 21:32:24 mycroft Exp $");
45 #endif
46 #endif /* not lint */
47 
48 /*
49  * Errors and exceptions.
50  */
51 
52 #include <signal.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <errno.h>
56 #include <stdio.h>
57 
58 #include "shell.h"
59 #include "main.h"
60 #include "options.h"
61 #include "output.h"
62 #include "error.h"
63 #include "show.h"
64 
65 
66 /*
67  * Code to handle exceptions in C.
68  */
69 
70 struct jmploc *handler;
71 int exception;
72 volatile int suppressint;
73 volatile int intpending;
74 char *commandname;
75 
76 
77 static void exverror __P((int, const char *, va_list))
78     __attribute__((__noreturn__));
79 
80 /*
81  * Called to raise an exception.  Since C doesn't include exceptions, we
82  * just do a longjmp to the exception handler.  The type of exception is
83  * stored in the global variable "exception".
84  */
85 
86 void
87 exraise(e)
88 	int e;
89 {
90 	if (handler == NULL)
91 		abort();
92 	exception = e;
93 	longjmp(handler->loc, 1);
94 }
95 
96 
97 /*
98  * Called from trap.c when a SIGINT is received.  (If the user specifies
99  * that SIGINT is to be trapped or ignored using the trap builtin, then
100  * this routine is not called.)  Suppressint is nonzero when interrupts
101  * are held using the INTOFF macro.  The call to _exit is necessary because
102  * there is a short period after a fork before the signal handlers are
103  * set to the appropriate value for the child.  (The test for iflag is
104  * just defensive programming.)
105  */
106 
107 void
108 onint() {
109 	sigset_t sigset;
110 
111 	if (suppressint) {
112 		intpending++;
113 		return;
114 	}
115 	intpending = 0;
116 	sigemptyset(&sigset);
117 	sigprocmask(SIG_SETMASK, &sigset, NULL);
118 	if (rootshell && iflag)
119 		exraise(EXINT);
120 	else {
121 		signal(SIGINT, SIG_DFL);
122 		raise(SIGINT);
123 	}
124 	/* NOTREACHED */
125 }
126 
127 
128 /*
129  * Exverror is called to raise the error exception.  If the first argument
130  * is not NULL then error prints an error message using printf style
131  * formatting.  It then raises the error exception.
132  */
133 static void
134 exverror(cond, msg, ap)
135 	int cond;
136 	const char *msg;
137 	va_list ap;
138 {
139 	CLEAR_PENDING_INT;
140 	INTOFF;
141 
142 #ifdef DEBUG
143 	if (msg)
144 		TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
145 	else
146 		TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
147 #endif
148 	if (msg) {
149 		if (commandname)
150 			outfmt(&errout, "%s: ", commandname);
151 		doformat(&errout, msg, ap);
152 		out2c('\n');
153 	}
154 	flushall();
155 	exraise(cond);
156 	/* NOTREACHED */
157 }
158 
159 
160 void
161 error(const char *msg, ...)
162 {
163 	va_list ap;
164 
165 	va_start(ap, msg);
166 	exverror(EXERROR, msg, ap);
167 	/* NOTREACHED */
168 	va_end(ap);
169 }
170 
171 
172 void
173 exerror(int cond, const char *msg, ...)
174 {
175 	va_list ap;
176 
177 	va_start(ap, msg);
178 	exverror(cond, msg, ap);
179 	/* NOTREACHED */
180 	va_end(ap);
181 }
182 
183 
184 
185 /*
186  * Table of error messages.
187  */
188 
189 struct errname {
190 	short errcode;		/* error number */
191 	short action;		/* operation which encountered the error */
192 	const char *msg;	/* text describing the error */
193 };
194 
195 
196 #define ALL (E_OPEN|E_CREAT|E_EXEC)
197 
198 STATIC const struct errname errormsg[] = {
199 	{ EINTR,	ALL,	"interrupted" },
200 	{ EACCES,	ALL,	"permission denied" },
201 	{ EIO,		ALL,	"I/O error" },
202 	{ EEXIST,	ALL,	"file exists" },
203 	{ ENOENT,	E_OPEN,	"no such file" },
204 	{ ENOENT,	E_CREAT,"directory nonexistent" },
205 	{ ENOENT,	E_EXEC,	"not found" },
206 	{ ENOTDIR,	E_OPEN,	"no such file" },
207 	{ ENOTDIR,	E_CREAT,"directory nonexistent" },
208 	{ ENOTDIR,	E_EXEC,	"not found" },
209 	{ EISDIR,	ALL,	"is a directory" },
210 #ifdef EMFILE
211 	{ EMFILE,	ALL,	"too many open files" },
212 #endif
213 	{ ENFILE,	ALL,	"file table overflow" },
214 	{ ENOSPC,	ALL,	"file system full" },
215 #ifdef EDQUOT
216 	{ EDQUOT,	ALL,	"disk quota exceeded" },
217 #endif
218 #ifdef ENOSR
219 	{ ENOSR,	ALL,	"no streams resources" },
220 #endif
221 	{ ENXIO,	ALL,	"no such device or address" },
222 	{ EROFS,	ALL,	"read-only file system" },
223 	{ ETXTBSY,	ALL,	"text busy" },
224 #ifdef EAGAIN
225 	{ EAGAIN,	E_EXEC,	"not enough memory" },
226 #endif
227 	{ ENOMEM,	ALL,	"not enough memory" },
228 #ifdef ENOLINK
229 	{ ENOLINK,	ALL,	"remote access failed" },
230 #endif
231 #ifdef EMULTIHOP
232 	{ EMULTIHOP,	ALL,	"remote access failed" },
233 #endif
234 #ifdef ECOMM
235 	{ ECOMM,	ALL,	"remote access failed" },
236 #endif
237 #ifdef ESTALE
238 	{ ESTALE,	ALL,	"remote access failed" },
239 #endif
240 #ifdef ETIMEDOUT
241 	{ ETIMEDOUT,	ALL,	"remote access failed" },
242 #endif
243 #ifdef ELOOP
244 	{ ELOOP,	ALL,	"symbolic link loop" },
245 #endif
246 	{ E2BIG,	E_EXEC,	"argument list too long" },
247 #ifdef ELIBACC
248 	{ ELIBACC,	E_EXEC,	"shared library missing" },
249 #endif
250 	{ 0,		0,	NULL },
251 };
252 
253 
254 /*
255  * Return a string describing an error.  The returned string may be a
256  * pointer to a static buffer that will be overwritten on the next call.
257  * Action describes the operation that got the error.
258  */
259 
260 const char *
261 errmsg(e, action)
262 	int e;
263 	int action;
264 {
265 	struct errname const *ep;
266 	static char buf[12];
267 
268 	for (ep = errormsg ; ep->errcode ; ep++) {
269 		if (ep->errcode == e && (ep->action & action) != 0)
270 			return ep->msg;
271 	}
272 	fmtstr(buf, sizeof buf, "error %d", e);
273 	return buf;
274 }
275