PageRenderTime 37ms CodeModel.GetById 18ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/error.c

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