1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*
36 * Errors and exceptions.
37 */
38
39#include "error.h"
40#include "eval.h"
41#include "main.h"
42#include "nodes.h" /* show.h needs nodes.h */
43#include "options.h"
44#include "output.h"
45#include "shell.h"
46#include "show.h"
47#include "trap.h"
48#include <errno.h>
49#include <signal.h>
50#include <stdlib.h>
51#include <unistd.h>
52
53/*
54 * Code to handle exceptions in C.
55 */
56
57struct jmploc *handler;
58volatile sig_atomic_t exception;
59volatile sig_atomic_t suppressint;
60volatile sig_atomic_t intpending;
61
62static void verrorwithstatus(int, const char *, va_list) __printf0like(2, 0) __dead2;
63
64/*
65 * Called to raise an exception. Since C doesn't include exceptions, we
66 * just do a longjmp to the exception handler. The type of exception is
67 * stored in the global variable "exception".
68 *
69 * Interrupts are disabled; they should be re-enabled when the exception is
70 * caught.
71 */
72
73void
74exraise(int e)
75{
76 INTOFF;
77 if (handler == NULL)
78 abort();
79 exception = e;
80 longjmp(handler->loc, 1);
81}
82
83/*
84 * Called from trap.c when a SIGINT is received and not suppressed, or when
85 * an interrupt is pending and interrupts are re-enabled using INTON.
86 * (If the user specifies that SIGINT is to be trapped or ignored using the
87 * trap builtin, then this routine is not called.) Suppressint is nonzero
88 * when interrupts are held using the INTOFF macro. If SIGINTs are not
89 * suppressed and the shell is not a root shell, then we want to be
90 * terminated if we get here, as if we were terminated directly by a SIGINT.
91 * Arrange for this here.
92 */
93
94void
95onint(void)
96{
97 sigset_t sigs;
98
99 intpending = 0;
100 sigemptyset(&sigs);
101 sigprocmask(SIG_SETMASK, &sigs, NULL);
102
103 /*
104 * This doesn't seem to be needed, since main() emits a newline.
105 */
106#if 0
107 if (tcgetpgrp(0) == getpid())
108 write(STDERR_FILENO, "\n", 1);
109#endif
110 if (rootshell && iflag)
111 exraise(EXINT);
112 else {
113 signal(SIGINT, SIG_DFL);
114 kill(getpid(), SIGINT);
115 _exit(128 + SIGINT);
116 }
117}
118
119static void
120vwarning(const char *msg, va_list ap)
121{
122 if (commandname)
123 outfmt(out2, "%s: ", commandname);
124 else if (arg0)
125 outfmt(out2, "%s: ", arg0);
126 doformat(out2, msg, ap);
127 out2fmt_flush("\n");
128}
129
130void
131warning(const char *msg, ...)
132{
133 va_list ap;
134 va_start(ap, msg);
135 vwarning(msg, ap);
136 va_end(ap);
137}
138
139/*
140 * Exverror is called to raise the error exception. If the first argument
141 * is not NULL then error prints an error message using printf style
142 * formatting. It then raises the error exception.
143 */
144static void
145verrorwithstatus(int status, const char *msg, va_list ap)
146{
147 /*
148 * An interrupt trumps an error. Certain places catch error
149 * exceptions or transform them to a plain nonzero exit code
150 * in child processes, and if an error exception can be handled,
151 * an interrupt can be handled as well.
152 *
153 * exraise() will disable interrupts for the exception handler.
154 */
155 FORCEINTON;
156
157#ifdef DEBUG
158 if (msg)
159 TRACE(("verrorwithstatus(%d, \"%s\") pid=%d\n", status, msg, getpid()));
160 else
161 TRACE(("verrorwithstatus(%d, NULL) pid=%d\n", status, getpid()));
162#endif
163 if (msg)
164 vwarning(msg, ap);
165 flushall();
166 exitstatus = status;
167 exraise(EXERROR);
168}
169
170void
171error(const char *msg, ...)
172{
173 va_list ap;
174 va_start(ap, msg);
175 verrorwithstatus(2, msg, ap);
176 va_end(ap);
177}
178
179void
180errorwithstatus(int status, const char *msg, ...)
181{
182 va_list ap;
183 va_start(ap, msg);
184 verrorwithstatus(status, msg, ap);
185 va_end(ap);
186}