kernel/iwm: Fix building into the kernel without IWM_DEBUG.
[dragonfly.git] / bin / sh / error.c
blob784d2a9eb91a312e71e0226bf8cce013636419de
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 * 3. 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 */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * Errors and exceptions.
45 #include "shell.h"
46 #include "eval.h"
47 #include "main.h"
48 #include "options.h"
49 #include "output.h"
50 #include "error.h"
51 #include "nodes.h" /* show.h needs nodes.h */
52 #include "show.h"
53 #include "trap.h"
54 #include <signal.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <errno.h>
61 * Code to handle exceptions in C.
64 struct jmploc *handler;
65 volatile sig_atomic_t exception;
66 volatile sig_atomic_t suppressint;
67 volatile sig_atomic_t intpending;
70 static void exverror(int, const char *, va_list) __printf0like(2, 0) __dead2;
73 * Called to raise an exception. Since C doesn't include exceptions, we
74 * just do a longjmp to the exception handler. The type of exception is
75 * stored in the global variable "exception".
77 * Interrupts are disabled; they should be reenabled when the exception is
78 * caught.
81 void
82 exraise(int e)
84 INTOFF;
85 if (handler == NULL)
86 abort();
87 exception = e;
88 longjmp(handler->loc, 1);
93 * Called from trap.c when a SIGINT is received and not suppressed, or when
94 * an interrupt is pending and interrupts are re-enabled using INTON.
95 * (If the user specifies that SIGINT is to be trapped or ignored using the
96 * trap builtin, then this routine is not called.) Suppressint is nonzero
97 * when interrupts are held using the INTOFF macro. If SIGINTs are not
98 * suppressed and the shell is not a root shell, then we want to be
99 * terminated if we get here, as if we were terminated directly by a SIGINT.
100 * Arrange for this here.
103 void
104 onint(void)
106 sigset_t sigs;
108 intpending = 0;
109 sigemptyset(&sigs);
110 sigprocmask(SIG_SETMASK, &sigs, NULL);
113 * This doesn't seem to be needed, since main() emits a newline.
115 #if 0
116 if (tcgetpgrp(0) == getpid())
117 write(STDERR_FILENO, "\n", 1);
118 #endif
119 if (rootshell && iflag)
120 exraise(EXINT);
121 else {
122 signal(SIGINT, SIG_DFL);
123 kill(getpid(), SIGINT);
124 _exit(128 + SIGINT);
129 static void
130 vwarning(const char *msg, va_list ap)
132 if (commandname)
133 outfmt(out2, "%s: ", commandname);
134 else if (arg0)
135 outfmt(out2, "%s: ", arg0);
136 doformat(out2, msg, ap);
137 out2fmt_flush("\n");
141 void
142 warning(const char *msg, ...)
144 va_list ap;
145 va_start(ap, msg);
146 vwarning(msg, ap);
147 va_end(ap);
152 * Exverror is called to raise the error exception. If the first argument
153 * is not NULL then error prints an error message using printf style
154 * formatting. It then raises the error exception.
156 static void
157 exverror(int cond, const char *msg, va_list ap)
160 * An interrupt trumps an error. Certain places catch error
161 * exceptions or transform them to a plain nonzero exit code
162 * in child processes, and if an error exception can be handled,
163 * an interrupt can be handled as well.
165 * exraise() will disable interrupts for the exception handler.
167 FORCEINTON;
169 #ifdef DEBUG
170 if (msg)
171 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
172 else
173 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
174 #endif
175 if (msg)
176 vwarning(msg, ap);
177 flushall();
178 exraise(cond);
182 void
183 error(const char *msg, ...)
185 va_list ap;
186 va_start(ap, msg);
187 exverror(EXERROR, msg, ap);
188 va_end(ap);
192 void
193 exerror(int cond, const char *msg, ...)
195 va_list ap;
196 va_start(ap, msg);
197 exverror(cond, msg, ap);
198 va_end(ap);