VirtualBox

source: kBuild/trunk/src/kash/error.c@ 2649

Last change on this file since 2649 was 2648, checked in by bird, 12 years ago

Made TRACE2 and TRACE2V safe. Fixed DEBUG bug where va_copy was missing. Introduced sh_strerror.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette