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