xref: /minix/bin/sh/error.c (revision d90bee97)
1 /*	$NetBSD: error.c,v 1.38 2012/03/15 02:02:20 joerg 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. 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 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
39 #else
40 __RCSID("$NetBSD: error.c,v 1.38 2012/03/15 02:02:20 joerg Exp $");
41 #endif
42 #endif /* not lint */
43 
44 /*
45  * Errors and exceptions.
46  */
47 
48 #include <signal.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <string.h>
54 
55 #include "shell.h"
56 #include "eval.h" /* for commandname */
57 #include "main.h"
58 #include "options.h"
59 #include "output.h"
60 #include "error.h"
61 #include "show.h"
62 
63 
64 /*
65  * Code to handle exceptions in C.
66  */
67 
68 struct jmploc *handler;
69 int exception;
70 volatile int suppressint;
71 volatile int intpending;
72 
73 
74 static void exverror(int, const char *, va_list) __dead;
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
exraise(int e)83 exraise(int e)
84 {
85 	if (handler == NULL)
86 		abort();
87 	exception = e;
88 	longjmp(handler->loc, 1);
89 }
90 
91 
92 /*
93  * Called from trap.c when a SIGINT is received.  (If the user specifies
94  * that SIGINT is to be trapped or ignored using the trap builtin, then
95  * this routine is not called.)  Suppressint is nonzero when interrupts
96  * are held using the INTOFF macro.  The call to _exit is necessary because
97  * there is a short period after a fork before the signal handlers are
98  * set to the appropriate value for the child.  (The test for iflag is
99  * just defensive programming.)
100  */
101 
102 void
onint(void)103 onint(void)
104 {
105 	sigset_t nsigset;
106 
107 	if (suppressint) {
108 		intpending = 1;
109 		return;
110 	}
111 	intpending = 0;
112 	sigemptyset(&nsigset);
113 	sigprocmask(SIG_SETMASK, &nsigset, NULL);
114 	if (rootshell && iflag)
115 		exraise(EXINT);
116 	else {
117 		signal(SIGINT, SIG_DFL);
118 		raise(SIGINT);
119 	}
120 	/* NOTREACHED */
121 }
122 
123 static __printflike(2, 0) void
exvwarning(int sv_errno,const char * msg,va_list ap)124 exvwarning(int sv_errno, const char *msg, va_list ap)
125 {
126 	/* Partially emulate line buffered output so that:
127 	 *	printf '%d\n' 1 a 2
128 	 * and
129 	 *	printf '%d %d %d\n' 1 a 2
130 	 * both generate sensible text when stdout and stderr are merged.
131 	 */
132 	if (output.nextc != output.buf && output.nextc[-1] == '\n')
133 		flushout(&output);
134 	if (commandname)
135 		outfmt(&errout, "%s: ", commandname);
136 	else
137 		outfmt(&errout, "%s: ", getprogname());
138 	if (msg != NULL) {
139 		doformat(&errout, msg, ap);
140 		if (sv_errno >= 0)
141 			outfmt(&errout, ": ");
142 	}
143 	if (sv_errno >= 0)
144 		outfmt(&errout, "%s", strerror(sv_errno));
145 	out2c('\n');
146 	flushout(&errout);
147 }
148 
149 /*
150  * Exverror is called to raise the error exception.  If the second argument
151  * is not NULL then error prints an error message using printf style
152  * formatting.  It then raises the error exception.
153  */
154 static __printflike(2, 0) void
exverror(int cond,const char * msg,va_list ap)155 exverror(int cond, const char *msg, va_list ap)
156 {
157 	CLEAR_PENDING_INT;
158 	INTOFF;
159 
160 #ifdef DEBUG
161 	if (msg) {
162 		TRACE(("exverror(%d, \"", cond));
163 		TRACEV((msg, ap));
164 		TRACE(("\") pid=%d\n", getpid()));
165 	} else
166 		TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
167 #endif
168 	if (msg)
169 		exvwarning(-1, msg, ap);
170 
171 	flushall();
172 	exraise(cond);
173 	/* NOTREACHED */
174 }
175 
176 
177 void
error(const char * msg,...)178 error(const char *msg, ...)
179 {
180 	va_list ap;
181 
182 	va_start(ap, msg);
183 	exverror(EXERROR, msg, ap);
184 	/* NOTREACHED */
185 	va_end(ap);
186 }
187 
188 
189 void
exerror(int cond,const char * msg,...)190 exerror(int cond, const char *msg, ...)
191 {
192 	va_list ap;
193 
194 	va_start(ap, msg);
195 	exverror(cond, msg, ap);
196 	/* NOTREACHED */
197 	va_end(ap);
198 }
199 
200 /*
201  * error/warning routines for external builtins
202  */
203 
204 void
sh_exit(int rval)205 sh_exit(int rval)
206 {
207 	exerrno = rval & 255;
208 	exraise(EXEXEC);
209 }
210 
211 void
sh_err(int status,const char * fmt,...)212 sh_err(int status, const char *fmt, ...)
213 {
214 	va_list ap;
215 
216 	va_start(ap, fmt);
217 	exvwarning(errno, fmt, ap);
218 	va_end(ap);
219 	sh_exit(status);
220 }
221 
222 void
sh_verr(int status,const char * fmt,va_list ap)223 sh_verr(int status, const char *fmt, va_list ap)
224 {
225 	exvwarning(errno, fmt, ap);
226 	sh_exit(status);
227 }
228 
229 void
sh_errx(int status,const char * fmt,...)230 sh_errx(int status, const char *fmt, ...)
231 {
232 	va_list ap;
233 
234 	va_start(ap, fmt);
235 	exvwarning(-1, fmt, ap);
236 	va_end(ap);
237 	sh_exit(status);
238 }
239 
240 void
sh_verrx(int status,const char * fmt,va_list ap)241 sh_verrx(int status, const char *fmt, va_list ap)
242 {
243 	exvwarning(-1, fmt, ap);
244 	sh_exit(status);
245 }
246 
247 void
sh_warn(const char * fmt,...)248 sh_warn(const char *fmt, ...)
249 {
250 	va_list ap;
251 
252 	va_start(ap, fmt);
253 	exvwarning(errno, fmt, ap);
254 	va_end(ap);
255 }
256 
257 void
sh_vwarn(const char * fmt,va_list ap)258 sh_vwarn(const char *fmt, va_list ap)
259 {
260 	exvwarning(errno, fmt, ap);
261 }
262 
263 void
sh_warnx(const char * fmt,...)264 sh_warnx(const char *fmt, ...)
265 {
266 	va_list ap;
267 
268 	va_start(ap, fmt);
269 	exvwarning(-1, fmt, ap);
270 	va_end(ap);
271 }
272 
273 void
sh_vwarnx(const char * fmt,va_list ap)274 sh_vwarnx(const char *fmt, va_list ap)
275 {
276 	exvwarning(-1, fmt, ap);
277 }
278 
279 
280 /*
281  * Table of error messages.
282  */
283 
284 struct errname {
285 	short errcode;		/* error number */
286 	short action;		/* operation which encountered the error */
287 	const char *msg;	/* text describing the error */
288 };
289 
290 
291 #define ALL (E_OPEN|E_CREAT|E_EXEC)
292 
293 STATIC const struct errname errormsg[] = {
294 	{ EINTR,	ALL,	"interrupted" },
295 	{ EACCES,	ALL,	"permission denied" },
296 	{ EIO,		ALL,	"I/O error" },
297 	{ EEXIST,	ALL,	"file exists" },
298 	{ ENOENT,	E_OPEN,	"no such file" },
299 	{ ENOENT,	E_CREAT,"directory nonexistent" },
300 	{ ENOENT,	E_EXEC,	"not found" },
301 	{ ENOTDIR,	E_OPEN,	"no such file" },
302 	{ ENOTDIR,	E_CREAT,"directory nonexistent" },
303 	{ ENOTDIR,	E_EXEC,	"not found" },
304 	{ EISDIR,	ALL,	"is a directory" },
305 #ifdef EMFILE
306 	{ EMFILE,	ALL,	"too many open files" },
307 #endif
308 	{ ENFILE,	ALL,	"file table overflow" },
309 	{ ENOSPC,	ALL,	"file system full" },
310 #ifdef EDQUOT
311 	{ EDQUOT,	ALL,	"disk quota exceeded" },
312 #endif
313 #ifdef ENOSR
314 	{ ENOSR,	ALL,	"no streams resources" },
315 #endif
316 	{ ENXIO,	ALL,	"no such device or address" },
317 	{ EROFS,	ALL,	"read-only file system" },
318 	{ ETXTBSY,	ALL,	"text busy" },
319 #ifdef EAGAIN
320 	{ EAGAIN,	E_EXEC,	"not enough memory" },
321 #endif
322 	{ ENOMEM,	ALL,	"not enough memory" },
323 #ifdef ENOLINK
324 	{ ENOLINK,	ALL,	"remote access failed" },
325 #endif
326 #ifdef EMULTIHOP
327 	{ EMULTIHOP,	ALL,	"remote access failed" },
328 #endif
329 #ifdef ECOMM
330 	{ ECOMM,	ALL,	"remote access failed" },
331 #endif
332 #ifdef ESTALE
333 	{ ESTALE,	ALL,	"remote access failed" },
334 #endif
335 #ifdef ETIMEDOUT
336 	{ ETIMEDOUT,	ALL,	"remote access failed" },
337 #endif
338 #ifdef ELOOP
339 	{ ELOOP,	ALL,	"symbolic link loop" },
340 #endif
341 #ifdef ENAMETOOLONG
342 	{ ENAMETOOLONG,	ALL,	"file name too long" },
343 #endif
344 	{ E2BIG,	E_EXEC,	"argument list too long" },
345 #ifdef ELIBACC
346 	{ ELIBACC,	E_EXEC,	"shared library missing" },
347 #endif
348 	{ 0,		0,	NULL },
349 };
350 
351 
352 /*
353  * Return a string describing an error.  The returned string may be a
354  * pointer to a static buffer that will be overwritten on the next call.
355  * Action describes the operation that got the error.
356  */
357 
358 const char *
errmsg(int e,int action)359 errmsg(int e, int action)
360 {
361 	struct errname const *ep;
362 	static char buf[12];
363 
364 	for (ep = errormsg ; ep->errcode ; ep++) {
365 		if (ep->errcode == e && (ep->action & action) != 0)
366 			return ep->msg;
367 	}
368 	fmtstr(buf, sizeof buf, "error %d", e);
369 	return buf;
370 }
371