trace(1): resolve all level-5 LLVM warnings
[minix3.git] / bin / sh / error.c
blob2ac98b69a2b83f37e74f60d41fb2f8828d3366a4
1 /* $NetBSD: error.c,v 1.38 2012/03/15 02:02:20 joerg Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
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.
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.
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 */
45 * Errors and exceptions.
48 #include <signal.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <string.h>
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"
65 * Code to handle exceptions in C.
68 struct jmploc *handler;
69 int exception;
70 volatile int suppressint;
71 volatile int intpending;
74 static void exverror(int, const char *, va_list) __dead;
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".
82 void
83 exraise(int e)
85 if (handler == NULL)
86 abort();
87 exception = e;
88 longjmp(handler->loc, 1);
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.)
102 void
103 onint(void)
105 sigset_t nsigset;
107 if (suppressint) {
108 intpending = 1;
109 return;
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);
120 /* NOTREACHED */
123 static __printflike(2, 0) void
124 exvwarning(int sv_errno, const char *msg, va_list ap)
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.
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, ": ");
143 if (sv_errno >= 0)
144 outfmt(&errout, "%s", strerror(sv_errno));
145 out2c('\n');
146 flushout(&errout);
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.
154 static __printflike(2, 0) void
155 exverror(int cond, const char *msg, va_list ap)
157 CLEAR_PENDING_INT;
158 INTOFF;
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);
171 flushall();
172 exraise(cond);
173 /* NOTREACHED */
177 void
178 error(const char *msg, ...)
180 va_list ap;
182 va_start(ap, msg);
183 exverror(EXERROR, msg, ap);
184 /* NOTREACHED */
185 va_end(ap);
189 void
190 exerror(int cond, const char *msg, ...)
192 va_list ap;
194 va_start(ap, msg);
195 exverror(cond, msg, ap);
196 /* NOTREACHED */
197 va_end(ap);
201 * error/warning routines for external builtins
204 void
205 sh_exit(int rval)
207 exerrno = rval & 255;
208 exraise(EXEXEC);
211 void
212 sh_err(int status, const char *fmt, ...)
214 va_list ap;
216 va_start(ap, fmt);
217 exvwarning(errno, fmt, ap);
218 va_end(ap);
219 sh_exit(status);
222 void
223 sh_verr(int status, const char *fmt, va_list ap)
225 exvwarning(errno, fmt, ap);
226 sh_exit(status);
229 void
230 sh_errx(int status, const char *fmt, ...)
232 va_list ap;
234 va_start(ap, fmt);
235 exvwarning(-1, fmt, ap);
236 va_end(ap);
237 sh_exit(status);
240 void
241 sh_verrx(int status, const char *fmt, va_list ap)
243 exvwarning(-1, fmt, ap);
244 sh_exit(status);
247 void
248 sh_warn(const char *fmt, ...)
250 va_list ap;
252 va_start(ap, fmt);
253 exvwarning(errno, fmt, ap);
254 va_end(ap);
257 void
258 sh_vwarn(const char *fmt, va_list ap)
260 exvwarning(errno, fmt, ap);
263 void
264 sh_warnx(const char *fmt, ...)
266 va_list ap;
268 va_start(ap, fmt);
269 exvwarning(-1, fmt, ap);
270 va_end(ap);
273 void
274 sh_vwarnx(const char *fmt, va_list ap)
276 exvwarning(-1, fmt, ap);
281 * Table of error messages.
284 struct errname {
285 short errcode; /* error number */
286 short action; /* operation which encountered the error */
287 const char *msg; /* text describing the error */
291 #define ALL (E_OPEN|E_CREAT|E_EXEC)
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 },
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.
358 const char *
359 errmsg(int e, int action)
361 struct errname const *ep;
362 static char buf[12];
364 for (ep = errormsg ; ep->errcode ; ep++) {
365 if (ep->errcode == e && (ep->action & action) != 0)
366 return ep->msg;
368 fmtstr(buf, sizeof buf, "error %d", e);
369 return buf;