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