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