- simplify findhole() for use for 1 page only
[minix.git] / commands / ash / error.c
bloba24807e4483d0a01f0479c712d29baebff72ffce
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/error.c,v 1.25 2004/04/06 20:06:51 markm Exp $");
44 * Errors and exceptions.
47 #include "shell.h"
48 #include "main.h"
49 #include "options.h"
50 #include "output.h"
51 #include "error.h"
52 #include "nodes.h" /* show.h needs nodes.h */
53 #include "show.h"
54 #include "trap.h"
55 #include <signal.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <errno.h>
59 #include <sys/types.h>
63 * Code to handle exceptions in C.
66 struct jmploc *handler;
67 volatile sig_atomic_t exception;
68 volatile sig_atomic_t suppressint;
69 volatile sig_atomic_t intpending;
70 char *commandname;
73 static void exverror(int, const char *, va_list) __printf0like(2, 0);
76 * Called to raise an exception. Since C doesn't include exceptions, we
77 * just do a longjmp to the exception handler. The type of exception is
78 * stored in the global variable "exception".
81 void
82 exraise(int e)
84 if (handler == NULL)
85 abort();
86 exception = e;
87 longjmp(handler->loc, 1);
92 * Called from trap.c when a SIGINT is received. (If the user specifies
93 * that SIGINT is to be trapped or ignored using the trap builtin, then
94 * this routine is not called.) Suppressint is nonzero when interrupts
95 * are held using the INTOFF macro. If SIGINTs are not suppressed and
96 * the shell is not a root shell, then we want to be terminated if we
97 * get here, as if we were terminated directly by a SIGINT. Arrange for
98 * this here.
101 void
102 onint(void)
104 sigset_t sigset;
107 * The !in_dotrap here is safe. The only way we can arrive here
108 * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL
109 * and killed itself.
112 if (suppressint && !in_dotrap) {
113 intpending++;
114 return;
116 intpending = 0;
117 sigemptyset(&sigset);
118 sigprocmask(SIG_SETMASK, &sigset, NULL);
121 * This doesn't seem to be needed, since main() emits a newline.
123 #if 0
124 if (tcgetpgrp(0) == getpid())
125 write(STDERR_FILENO, "\n", 1);
126 #endif
127 if (rootshell && iflag)
128 exraise(EXINT);
129 else {
130 signal(SIGINT, SIG_DFL);
131 kill(getpid(), SIGINT);
137 * Exverror is called to raise the error exception. If the first argument
138 * is not NULL then error prints an error message using printf style
139 * formatting. It then raises the error exception.
141 static void
142 exverror(int cond, const char *msg, va_list ap)
144 CLEAR_PENDING_INT;
145 INTOFF;
147 #if DEBUG
148 if (msg)
149 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
150 else
151 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
152 #endif
153 if (msg) {
154 if (commandname)
155 outfmt(&errout, "%s: ", commandname);
156 doformat(&errout, msg, ap);
157 out2c('\n');
159 flushall();
160 exraise(cond);
164 void
165 error(const char *msg, ...)
167 va_list ap;
168 va_start(ap, msg);
169 exverror(EXERROR, msg, ap);
170 va_end(ap);
174 void
175 exerror(int cond, const char *msg, ...)
177 va_list ap;
178 va_start(ap, msg);
179 exverror(cond, msg, ap);
180 va_end(ap);
184 * $PchId: error.c,v 1.5 2006/04/10 14:36:23 philip Exp $