VirtualBox

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

Last change on this file since 3387 was 2651, checked in by bird, 12 years ago

hash/error.c: no va_copy on windows yet, sigh.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 8.8 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# ifdef va_copy /* MSC 2010 still doesn't have va_copy. sigh. */
161 va_copy(va2, ap);
162# else
163 va2 = ap;
164# endif
165 TRACEV((psh, msg, va2));
166 va_end(va2);
167 TRACE((psh, "\") pid=%d\n", sh_getpid(psh)));
168 } else
169 TRACE((psh, "exverror(%d, NULL) pid=%d\n", cond, sh_getpid(psh)));
170#endif
171 if (msg)
172 exvwarning(psh, -1, msg, ap);
173
174 output_flushall(psh);
175 exraise(psh, cond);
176 /* NOTREACHED */
177}
178
179
180SH_NORETURN_1 void
181error(shinstance *psh, const char *msg, ...)
182{
183 va_list ap;
184
185 va_start(ap, msg);
186 exverror(psh, EXERROR, msg, ap);
187 /* NOTREACHED */
188 va_end(ap);
189}
190
191
192SH_NORETURN_1 void
193exerror(shinstance *psh, int cond, const char *msg, ...)
194{
195 va_list ap;
196
197 va_start(ap, msg);
198 exverror(psh, cond, msg, ap);
199 /* NOTREACHED */
200 va_end(ap);
201}
202
203/*
204 * error/warning routines for external builtins
205 */
206
207SH_NORETURN_1 void
208sh_exit(shinstance *psh, int rval)
209{
210 psh->exerrno = rval & 255;
211 exraise(psh, EXEXEC);
212}
213
214SH_NORETURN_1 void
215sh_err(shinstance *psh, int status, const char *fmt, ...)
216{
217 va_list ap;
218
219 va_start(ap, fmt);
220 exvwarning(psh, errno, fmt, ap);
221 va_end(ap);
222 sh_exit(psh, status);
223}
224
225SH_NORETURN_1 void
226sh_verr(shinstance *psh, int status, const char *fmt, va_list ap)
227{
228 exvwarning(psh, errno, fmt, ap);
229 sh_exit(psh, status);
230}
231
232SH_NORETURN_1 void
233sh_errx(shinstance *psh, int status, const char *fmt, ...)
234{
235 va_list ap;
236
237 va_start(ap, fmt);
238 exvwarning(psh, -1, fmt, ap);
239 va_end(ap);
240 sh_exit(psh, status);
241}
242
243SH_NORETURN_1 void
244sh_verrx(shinstance *psh, int status, const char *fmt, va_list ap)
245{
246 exvwarning(psh, -1, fmt, ap);
247 sh_exit(psh, status);
248}
249
250void
251sh_warn(shinstance *psh, const char *fmt, ...)
252{
253 va_list ap;
254
255 va_start(ap, fmt);
256 exvwarning(psh, errno, fmt, ap);
257 va_end(ap);
258}
259
260void
261sh_vwarn(shinstance *psh, const char *fmt, va_list ap)
262{
263 exvwarning(psh, errno, fmt, ap);
264}
265
266void
267sh_warnx(shinstance *psh, const char *fmt, ...)
268{
269 va_list ap;
270
271 va_start(ap, fmt);
272 exvwarning(psh, -1, fmt, ap);
273 va_end(ap);
274}
275
276void
277sh_vwarnx(shinstance *psh, const char *fmt, va_list ap)
278{
279 exvwarning(psh, -1, fmt, ap);
280}
281
282
283/*
284 * Table of error messages.
285 */
286
287struct errname {
288 short errcode; /* error number */
289 short action; /* operation which encountered the error */
290 const char *msg; /* text describing the error */
291};
292
293
294#define ALL (E_OPEN|E_CREAT|E_EXEC)
295
296STATIC const struct errname errormsg[] = {
297 { EINTR, ALL, "interrupted" },
298 { EACCES, ALL, "permission denied" },
299 { EIO, ALL, "I/O error" },
300 { EEXIST, ALL, "file exists" },
301 { ENOENT, E_OPEN, "no such file" },
302 { ENOENT, E_CREAT,"directory nonexistent" },
303 { ENOENT, E_EXEC, "not found" },
304 { ENOTDIR, E_OPEN, "no such file" },
305 { ENOTDIR, E_CREAT,"directory nonexistent" },
306 { ENOTDIR, E_EXEC, "not found" },
307 { EISDIR, ALL, "is a directory" },
308#ifdef EMFILE
309 { EMFILE, ALL, "too many open files" },
310#endif
311 { ENFILE, ALL, "file table overflow" },
312 { ENOSPC, ALL, "file system full" },
313#ifdef EDQUOT
314 { EDQUOT, ALL, "disk quota exceeded" },
315#endif
316#ifdef ENOSR
317 { ENOSR, ALL, "no streams resources" },
318#endif
319 { ENXIO, ALL, "no such device or address" },
320 { EROFS, ALL, "read-only file system" },
321#ifdef ETXTBSY
322 { ETXTBSY, ALL, "text busy" },
323#endif
324#ifdef EAGAIN
325 { EAGAIN, E_EXEC, "not enough memory" },
326#endif
327 { ENOMEM, ALL, "not enough memory" },
328#ifdef ENOLINK
329 { ENOLINK, ALL, "remote access failed" },
330#endif
331#ifdef EMULTIHOP
332 { EMULTIHOP, ALL, "remote access failed" },
333#endif
334#ifdef ECOMM
335 { ECOMM, ALL, "remote access failed" },
336#endif
337#ifdef ESTALE
338 { ESTALE, ALL, "remote access failed" },
339#endif
340#ifdef ETIMEDOUT
341 { ETIMEDOUT, ALL, "remote access failed" },
342#endif
343#ifdef ELOOP
344 { ELOOP, ALL, "symbolic link loop" },
345#endif
346 { E2BIG, E_EXEC, "argument list too long" },
347#ifdef ELIBACC
348 { ELIBACC, E_EXEC, "shared library missing" },
349#endif
350 { 0, 0, NULL },
351};
352
353
354/*
355 * Return a string describing an error. The returned string may be a
356 * pointer to a static buffer that will be overwritten on the next call.
357 * Action describes the operation that got the error.
358 */
359
360const char *
361errmsg(shinstance *psh, int e, int action)
362{
363 struct errname const *ep;
364 /*static char buf[12];*/
365
366 for (ep = errormsg ; ep->errcode ; ep++) {
367 if (ep->errcode == e && (ep->action & action) != 0)
368 return ep->msg;
369 }
370 fmtstr(psh->errmsg_buf, sizeof psh->errmsg_buf, "error %d", e);
371 return psh->errmsg_buf;
372}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use