Home | History | Annotate | Line # | Download | only in sh
error.c revision 1.17
      1  1.17  christos /*	$NetBSD: error.c,v 1.17 1997/07/04 21:01:54 christos Exp $	*/
      2  1.11       cgd 
      3   1.1       cgd /*-
      4   1.6       jtc  * Copyright (c) 1991, 1993
      5   1.6       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39  1.17  christos #include <sys/cdefs.h>
     40   1.1       cgd #ifndef lint
     41  1.11       cgd #if 0
     42  1.14  christos static char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
     43  1.11       cgd #else
     44  1.17  christos __RCSID("$NetBSD: error.c,v 1.17 1997/07/04 21:01:54 christos Exp $");
     45  1.11       cgd #endif
     46   1.1       cgd #endif /* not lint */
     47   1.1       cgd 
     48   1.1       cgd /*
     49   1.1       cgd  * Errors and exceptions.
     50   1.1       cgd  */
     51   1.1       cgd 
     52   1.1       cgd #include "shell.h"
     53   1.1       cgd #include "main.h"
     54   1.1       cgd #include "options.h"
     55   1.1       cgd #include "output.h"
     56   1.1       cgd #include "error.h"
     57  1.14  christos #include "show.h"
     58   1.1       cgd #include <signal.h>
     59   1.7       jtc #include <unistd.h>
     60   1.1       cgd #include <errno.h>
     61   1.1       cgd 
     62   1.1       cgd 
     63   1.1       cgd /*
     64   1.1       cgd  * Code to handle exceptions in C.
     65   1.1       cgd  */
     66   1.1       cgd 
     67   1.1       cgd struct jmploc *handler;
     68   1.1       cgd int exception;
     69   1.1       cgd volatile int suppressint;
     70   1.1       cgd volatile int intpending;
     71   1.1       cgd char *commandname;
     72   1.1       cgd 
     73   1.1       cgd 
     74  1.17  christos static void exverror __P((int, char *, va_list)) __attribute__((__noreturn__));
     75  1.15  christos 
     76   1.1       cgd /*
     77   1.1       cgd  * Called to raise an exception.  Since C doesn't include exceptions, we
     78   1.1       cgd  * just do a longjmp to the exception handler.  The type of exception is
     79   1.1       cgd  * stored in the global variable "exception".
     80   1.1       cgd  */
     81   1.1       cgd 
     82   1.1       cgd void
     83  1.15  christos exraise(e)
     84   1.9       cgd 	int e;
     85   1.9       cgd {
     86   1.1       cgd 	if (handler == NULL)
     87   1.1       cgd 		abort();
     88   1.1       cgd 	exception = e;
     89   1.1       cgd 	longjmp(handler->loc, 1);
     90   1.1       cgd }
     91   1.1       cgd 
     92   1.1       cgd 
     93   1.1       cgd /*
     94   1.1       cgd  * Called from trap.c when a SIGINT is received.  (If the user specifies
     95   1.1       cgd  * that SIGINT is to be trapped or ignored using the trap builtin, then
     96   1.1       cgd  * this routine is not called.)  Suppressint is nonzero when interrupts
     97   1.1       cgd  * are held using the INTOFF macro.  The call to _exit is necessary because
     98   1.1       cgd  * there is a short period after a fork before the signal handlers are
     99   1.1       cgd  * set to the appropriate value for the child.  (The test for iflag is
    100   1.1       cgd  * just defensive programming.)
    101   1.1       cgd  */
    102   1.1       cgd 
    103   1.1       cgd void
    104   1.1       cgd onint() {
    105  1.13   mycroft 	sigset_t sigset;
    106  1.12   mycroft 
    107   1.1       cgd 	if (suppressint) {
    108   1.1       cgd 		intpending++;
    109   1.1       cgd 		return;
    110   1.1       cgd 	}
    111   1.1       cgd 	intpending = 0;
    112  1.13   mycroft 	sigemptyset(&sigset);
    113  1.13   mycroft 	sigprocmask(SIG_SETMASK, &sigset, NULL);
    114   1.1       cgd 	if (rootshell && iflag)
    115   1.1       cgd 		exraise(EXINT);
    116   1.1       cgd 	else
    117   1.1       cgd 		_exit(128 + SIGINT);
    118   1.1       cgd }
    119   1.1       cgd 
    120   1.1       cgd 
    121   1.1       cgd /*
    122  1.15  christos  * Exverror is called to raise the error exception.  If the first argument
    123   1.1       cgd  * is not NULL then error prints an error message using printf style
    124   1.1       cgd  * formatting.  It then raises the error exception.
    125   1.1       cgd  */
    126  1.15  christos static void
    127  1.15  christos exverror(cond, msg, ap)
    128  1.15  christos 	int cond;
    129  1.15  christos 	char *msg;
    130  1.15  christos 	va_list ap;
    131  1.15  christos {
    132  1.15  christos 	CLEAR_PENDING_INT;
    133  1.15  christos 	INTOFF;
    134  1.15  christos 
    135  1.15  christos #ifdef DEBUG
    136  1.15  christos 	if (msg)
    137  1.15  christos 		TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
    138  1.15  christos 	else
    139  1.15  christos 		TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
    140  1.15  christos #endif
    141  1.15  christos 	if (msg) {
    142  1.15  christos 		if (commandname)
    143  1.15  christos 			outfmt(&errout, "%s: ", commandname);
    144  1.15  christos 		doformat(&errout, msg, ap);
    145  1.15  christos 		out2c('\n');
    146  1.15  christos 	}
    147  1.15  christos 	flushall();
    148  1.15  christos 	exraise(cond);
    149  1.15  christos }
    150  1.15  christos 
    151   1.1       cgd 
    152  1.16  christos #ifdef __STDC__
    153   1.1       cgd void
    154  1.14  christos error(char *msg, ...)
    155   1.1       cgd #else
    156   1.1       cgd void
    157   1.1       cgd error(va_alist)
    158   1.1       cgd 	va_dcl
    159  1.14  christos #endif
    160  1.14  christos {
    161  1.16  christos #ifndef __STDC__
    162   1.1       cgd 	char *msg;
    163   1.1       cgd #endif
    164   1.1       cgd 	va_list ap;
    165  1.16  christos #ifdef __STDC__
    166  1.15  christos 	va_start(ap, msg);
    167  1.15  christos #else
    168  1.15  christos 	va_start(ap);
    169  1.15  christos 	msg = va_arg(ap, char *);
    170  1.15  christos #endif
    171  1.15  christos 	exverror(EXERROR, msg, ap);
    172  1.15  christos 	va_end(ap);
    173  1.15  christos }
    174  1.15  christos 
    175  1.14  christos 
    176  1.16  christos #ifdef __STDC__
    177  1.15  christos void
    178  1.15  christos exerror(int cond, char *msg, ...)
    179  1.15  christos #else
    180  1.15  christos void
    181  1.15  christos exerror(va_alist)
    182  1.15  christos 	va_dcl
    183  1.15  christos #endif
    184  1.15  christos {
    185  1.16  christos #ifndef __STDC__
    186  1.15  christos 	int cond;
    187  1.15  christos 	char *msg;
    188  1.15  christos #endif
    189  1.15  christos 	va_list ap;
    190  1.16  christos #ifdef __STDC__
    191   1.1       cgd 	va_start(ap, msg);
    192   1.1       cgd #else
    193   1.1       cgd 	va_start(ap);
    194  1.15  christos 	cond = va_arg(ap, int);
    195   1.1       cgd 	msg = va_arg(ap, char *);
    196   1.1       cgd #endif
    197  1.15  christos 	exverror(cond, msg, ap);
    198   1.1       cgd 	va_end(ap);
    199   1.1       cgd }
    200   1.1       cgd 
    201   1.1       cgd 
    202   1.1       cgd 
    203   1.1       cgd /*
    204   1.1       cgd  * Table of error messages.
    205   1.1       cgd  */
    206   1.1       cgd 
    207   1.1       cgd struct errname {
    208   1.1       cgd 	short errcode;		/* error number */
    209   1.1       cgd 	short action;		/* operation which encountered the error */
    210   1.1       cgd 	char *msg;		/* text describing the error */
    211   1.1       cgd };
    212   1.1       cgd 
    213   1.1       cgd 
    214   1.1       cgd #define ALL (E_OPEN|E_CREAT|E_EXEC)
    215   1.1       cgd 
    216   1.1       cgd STATIC const struct errname errormsg[] = {
    217  1.14  christos 	{ EINTR,	ALL,	"interrupted" },
    218  1.14  christos 	{ EACCES,	ALL,	"permission denied" },
    219  1.14  christos 	{ EIO,		ALL,	"I/O error" },
    220  1.14  christos 	{ ENOENT,	E_OPEN,	"no such file" },
    221  1.14  christos 	{ ENOENT,	E_CREAT,"directory nonexistent" },
    222  1.14  christos 	{ ENOENT,	E_EXEC,	"not found" },
    223  1.14  christos 	{ ENOTDIR,	E_OPEN,	"no such file" },
    224  1.14  christos 	{ ENOTDIR,	E_CREAT,"directory nonexistent" },
    225  1.14  christos 	{ ENOTDIR,	E_EXEC,	"not found" },
    226  1.14  christos 	{ EISDIR,	ALL,	"is a directory" },
    227  1.14  christos #ifdef notdef
    228  1.14  christos 	{ EMFILE,	ALL,	"too many open files" },
    229  1.14  christos #endif
    230  1.14  christos 	{ ENFILE,	ALL,	"file table overflow" },
    231  1.14  christos 	{ ENOSPC,	ALL,	"file system full" },
    232   1.1       cgd #ifdef EDQUOT
    233  1.14  christos 	{ EDQUOT,	ALL,	"disk quota exceeded" },
    234   1.1       cgd #endif
    235   1.1       cgd #ifdef ENOSR
    236  1.14  christos 	{ ENOSR,	ALL,	"no streams resources" },
    237   1.1       cgd #endif
    238  1.14  christos 	{ ENXIO,	ALL,	"no such device or address" },
    239  1.14  christos 	{ EROFS,	ALL,	"read-only file system" },
    240  1.14  christos 	{ ETXTBSY,	ALL,	"text busy" },
    241   1.1       cgd #ifdef SYSV
    242  1.14  christos 	{ EAGAIN,	E_EXEC,	"not enough memory" },
    243   1.1       cgd #endif
    244  1.14  christos 	{ ENOMEM,	ALL,	"not enough memory" },
    245   1.1       cgd #ifdef ENOLINK
    246  1.14  christos 	{ ENOLINK,	ALL,	"remote access failed" },
    247   1.1       cgd #endif
    248   1.1       cgd #ifdef EMULTIHOP
    249  1.14  christos 	{ EMULTIHOP,	ALL,	"remote access failed" },
    250   1.1       cgd #endif
    251   1.1       cgd #ifdef ECOMM
    252  1.14  christos 	{ ECOMM,	ALL,	"remote access failed" },
    253   1.1       cgd #endif
    254   1.1       cgd #ifdef ESTALE
    255  1.14  christos 	{ ESTALE,	ALL,	"remote access failed" },
    256   1.1       cgd #endif
    257   1.1       cgd #ifdef ETIMEDOUT
    258  1.14  christos 	{ ETIMEDOUT,	ALL,	"remote access failed" },
    259   1.1       cgd #endif
    260   1.1       cgd #ifdef ELOOP
    261  1.14  christos 	{ ELOOP,	ALL,	"symbolic link loop" },
    262   1.1       cgd #endif
    263  1.14  christos 	{ E2BIG,	E_EXEC,	"argument list too long" },
    264   1.1       cgd #ifdef ELIBACC
    265  1.14  christos 	{ ELIBACC,	E_EXEC,	"shared library missing" },
    266   1.1       cgd #endif
    267  1.14  christos 	{ 0,		0,	NULL },
    268   1.1       cgd };
    269   1.1       cgd 
    270   1.1       cgd 
    271   1.1       cgd /*
    272   1.1       cgd  * Return a string describing an error.  The returned string may be a
    273   1.1       cgd  * pointer to a static buffer that will be overwritten on the next call.
    274   1.1       cgd  * Action describes the operation that got the error.
    275   1.1       cgd  */
    276   1.1       cgd 
    277   1.1       cgd char *
    278  1.15  christos errmsg(e, action)
    279  1.10       cgd 	int e;
    280  1.10       cgd 	int action;
    281  1.10       cgd {
    282   1.1       cgd 	struct errname const *ep;
    283   1.1       cgd 	static char buf[12];
    284   1.1       cgd 
    285   1.1       cgd 	for (ep = errormsg ; ep->errcode ; ep++) {
    286   1.1       cgd 		if (ep->errcode == e && (ep->action & action) != 0)
    287   1.1       cgd 			return ep->msg;
    288   1.1       cgd 	}
    289   1.1       cgd 	fmtstr(buf, sizeof buf, "error %d", e);
    290   1.1       cgd 	return buf;
    291   1.1       cgd }
    292