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