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