source: trunk/src/kash/error.c

Last change on this file was 3477, checked in by bird, 5 years ago

kash: Use kHlpAssert instead of assert.h (debugger stops on the assertion rather than at exit process code).

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 9.4 KB
RevLine 
[626]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
[1214]35#if 0
[626]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 $");
[1214]40#endif /* not lint */
[626]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"
[883]58#include "shinstance.h"
[626]59
60
61/*
62 * Code to handle exceptions in C.
63 */
64
[883]65/*struct jmploc *handler;
[626]66int exception;
67volatile int suppressint;
68volatile int intpending;
[883]69char *commandname;*/
[626]70
[2298]71SH_NORETURN_1
72static void exverror(shinstance *psh, int, const char *, va_list) SH_NORETURN_2;
[626]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
[2298]80SH_NORETURN_1 void
[883]81exraise(shinstance *psh, int e)
[626]82{
[883]83 if (psh->handler == NULL)
[1222]84 sh_abort(psh);
[883]85 psh->exception = e;
86 longjmp(psh->handler->loc, 1);
[626]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
[883]101onint(shinstance *psh)
[626]102{
[1240]103 shsigset_t nsigset;
[626]104
[883]105 if (psh->suppressint) {
106 psh->intpending = 1;
[626]107 return;
108 }
[883]109 psh->intpending = 0;
110 sh_sigemptyset(&nsigset);
111 sh_sigprocmask(psh, SIG_SETMASK, &nsigset, NULL);
[1198]112 if (psh->rootshell && iflag(psh))
[883]113 exraise(psh, EXINT);
[626]114 else {
[1208]115 sh_signal(psh, SIGINT, SH_SIG_DFL);
[883]116 sh_raise_sigint(psh);/*raise(psh, SIGINT);*/
[626]117 }
118 /* NOTREACHED */
119}
120
121static void
[883]122exvwarning(shinstance *psh, int sv_errno, const char *msg, va_list ap)
[626]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 */
[883]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);
[626]134 if (msg != NULL) {
[883]135 doformat(&psh->errout, msg, ap);
[626]136 if (sv_errno >= 0)
[883]137 outfmt(&psh->errout, ": ");
[626]138 }
139 if (sv_errno >= 0)
[2648]140 outfmt(&psh->errout, "%s", sh_strerror(psh, sv_errno));
[883]141 out2c(psh, '\n');
142 flushout(&psh->errout);
[626]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
[883]151exverror(shinstance *psh, int cond, const char *msg, va_list ap)
[626]152{
153 CLEAR_PENDING_INT;
154 INTOFF;
155
156#ifdef DEBUG
157 if (msg) {
[2648]158 va_list va2;
[1199]159 TRACE((psh, "exverror(%d, \"", cond));
[2651]160# ifdef va_copy /* MSC 2010 still doesn't have va_copy. sigh. */
[2648]161 va_copy(va2, ap);
[2651]162# else
163 va2 = ap;
164# endif
[2648]165 TRACEV((psh, msg, va2));
166 va_end(va2);
[3438]167 TRACE((psh, "\") pid=%" SHPID_PRI "\n", sh_getpid(psh)));
[626]168 } else
[3438]169 TRACE((psh, "exverror(%d, NULL) pid=%" SHPID_PRI "\n", cond, sh_getpid(psh)));
[626]170#endif
171 if (msg)
[883]172 exvwarning(psh, -1, msg, ap);
[626]173
[883]174 output_flushall(psh);
175 exraise(psh, cond);
[626]176 /* NOTREACHED */
177}
178
179
[2298]180SH_NORETURN_1 void
[883]181error(shinstance *psh, const char *msg, ...)
[626]182{
183 va_list ap;
184
185 va_start(ap, msg);
[883]186 exverror(psh, EXERROR, msg, ap);
[626]187 /* NOTREACHED */
188 va_end(ap);
189}
190
191
[2298]192SH_NORETURN_1 void
[883]193exerror(shinstance *psh, int cond, const char *msg, ...)
[626]194{
195 va_list ap;
196
197 va_start(ap, msg);
[883]198 exverror(psh, cond, msg, ap);
[626]199 /* NOTREACHED */
200 va_end(ap);
201}
202
203/*
204 * error/warning routines for external builtins
205 */
206
[2298]207SH_NORETURN_1 void
[883]208sh_exit(shinstance *psh, int rval)
[626]209{
[883]210 psh->exerrno = rval & 255;
211 exraise(psh, EXEXEC);
[626]212}
213
[2298]214SH_NORETURN_1 void
[883]215sh_err(shinstance *psh, int status, const char *fmt, ...)
[626]216{
217 va_list ap;
218
219 va_start(ap, fmt);
[883]220 exvwarning(psh, errno, fmt, ap);
[626]221 va_end(ap);
[883]222 sh_exit(psh, status);
[626]223}
224
[2298]225SH_NORETURN_1 void
[883]226sh_verr(shinstance *psh, int status, const char *fmt, va_list ap)
[626]227{
[883]228 exvwarning(psh, errno, fmt, ap);
229 sh_exit(psh, status);
[626]230}
231
[2298]232SH_NORETURN_1 void
[883]233sh_errx(shinstance *psh, int status, const char *fmt, ...)
[626]234{
235 va_list ap;
236
237 va_start(ap, fmt);
[883]238 exvwarning(psh, -1, fmt, ap);
[626]239 va_end(ap);
[883]240 sh_exit(psh, status);
[626]241}
242
[2298]243SH_NORETURN_1 void
[883]244sh_verrx(shinstance *psh, int status, const char *fmt, va_list ap)
[626]245{
[883]246 exvwarning(psh, -1, fmt, ap);
247 sh_exit(psh, status);
[626]248}
249
250void
[883]251sh_warn(shinstance *psh, const char *fmt, ...)
[626]252{
253 va_list ap;
254
255 va_start(ap, fmt);
[883]256 exvwarning(psh, errno, fmt, ap);
[626]257 va_end(ap);
258}
259
260void
[883]261sh_vwarn(shinstance *psh, const char *fmt, va_list ap)
[626]262{
[883]263 exvwarning(psh, errno, fmt, ap);
[626]264}
265
266void
[883]267sh_warnx(shinstance *psh, const char *fmt, ...)
[626]268{
269 va_list ap;
270
271 va_start(ap, fmt);
[883]272 exvwarning(psh, -1, fmt, ap);
[626]273 va_end(ap);
274}
275
276void
[883]277sh_vwarnx(shinstance *psh, const char *fmt, va_list ap)
[626]278{
[883]279 exvwarning(psh, -1, fmt, ap);
[626]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" },
[632]321#ifdef ETXTBSY
[626]322 { ETXTBSY, ALL, "text busy" },
[632]323#endif
[626]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 *
[883]361errmsg(shinstance *psh, int e, int action)
[626]362{
363 struct errname const *ep;
[883]364 /*static char buf[12];*/
[626]365
366 for (ep = errormsg ; ep->errcode ; ep++) {
367 if (ep->errcode == e && (ep->action & action) != 0)
368 return ep->msg;
369 }
[883]370 fmtstr(psh->errmsg_buf, sizeof psh->errmsg_buf, "error %d", e);
371 return psh->errmsg_buf;
[626]372}
[3477]373
374
375#ifdef K_STRICT
376
377KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction)
378{
379 shinstance *psh = shthread_get_shell();
380
381 TRACE((psh, "Assertion failed in %s --- %s --- %s(%u)\n", pszFunction, pszExpr, pszFile, iLine));
382 dprintf(psh, "Assertion failed in %s --- %s --- %s(%u)\n", pszFunction, pszExpr, pszFile, iLine);
383}
384
385KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...)
386{
387 shinstance *psh = shthread_get_shell();
388 va_list va;
389 va_start(va, pszFormat);
390 doformat(psh->out2, pszFormat, va);
391 va_end(va);
392}
393
394#endif /* K_STRICT */
Note: See TracBrowser for help on using the repository browser.