1235368Sgnn/*-
2235368Sgnn * Copyright (c) 1991, 1993
3235368Sgnn *	The Regents of the University of California.  All rights reserved.
4235368Sgnn *
5235368Sgnn * This code is derived from software contributed to Berkeley by
6235368Sgnn * Kenneth Almquist.
7235368Sgnn *
8235368Sgnn * Redistribution and use in source and binary forms, with or without
9235368Sgnn * modification, are permitted provided that the following conditions
10235368Sgnn * are met:
11235368Sgnn * 1. Redistributions of source code must retain the above copyright
12235368Sgnn *    notice, this list of conditions and the following disclaimer.
13235368Sgnn * 2. Redistributions in binary form must reproduce the above copyright
14235368Sgnn *    notice, this list of conditions and the following disclaimer in the
15235368Sgnn *    documentation and/or other materials provided with the distribution.
16235368Sgnn * 4. Neither the name of the University nor the names of its contributors
17235368Sgnn *    may be used to endorse or promote products derived from this software
18235368Sgnn *    without specific prior written permission.
19235368Sgnn *
20235368Sgnn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21235368Sgnn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22235368Sgnn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23235368Sgnn * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24235368Sgnn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25235368Sgnn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26235368Sgnn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27235368Sgnn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28235368Sgnn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29235368Sgnn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30235368Sgnn * SUCH DAMAGE.
31235368Sgnn */
32235368Sgnn
33235368Sgnn#ifndef lint
34235368Sgnn#if 0
35235368Sgnnstatic char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
36235368Sgnn#endif
37235368Sgnn#endif /* not lint */
38235368Sgnn#include <sys/cdefs.h>
39235368Sgnn__FBSDID("$FreeBSD: releng/11.0/bin/sh/error.c 279569 2015-03-03 21:21:43Z jilles $");
40235368Sgnn
41235368Sgnn/*
42235368Sgnn * Errors and exceptions.
43235368Sgnn */
44235368Sgnn
45235368Sgnn#include "shell.h"
46235368Sgnn#include "eval.h"
47235368Sgnn#include "main.h"
48235368Sgnn#include "options.h"
49235368Sgnn#include "output.h"
50235368Sgnn#include "error.h"
51235368Sgnn#include "nodes.h" /* show.h needs nodes.h */
52235368Sgnn#include "show.h"
53235368Sgnn#include "trap.h"
54235368Sgnn#include <signal.h>
55235368Sgnn#include <stdlib.h>
56235368Sgnn#include <unistd.h>
57235368Sgnn#include <errno.h>
58235368Sgnn
59235368Sgnn
60235368Sgnn/*
61235368Sgnn * Code to handle exceptions in C.
62235368Sgnn */
63235368Sgnn
64235368Sgnnstruct jmploc *handler;
65235368Sgnnvolatile sig_atomic_t exception;
66235368Sgnnvolatile sig_atomic_t suppressint;
67235368Sgnnvolatile sig_atomic_t intpending;
68235368Sgnn
69235368Sgnn
70235368Sgnnstatic void exverror(int, const char *, va_list) __printf0like(2, 0) __dead2;
71235368Sgnn
72235368Sgnn/*
73235368Sgnn * Called to raise an exception.  Since C doesn't include exceptions, we
74235368Sgnn * just do a longjmp to the exception handler.  The type of exception is
75235368Sgnn * stored in the global variable "exception".
76235368Sgnn *
77235368Sgnn * Interrupts are disabled; they should be reenabled when the exception is
78235368Sgnn * caught.
79235368Sgnn */
80235368Sgnn
81235368Sgnnvoid
82235368Sgnnexraise(int e)
83235368Sgnn{
84235368Sgnn	INTOFF;
85235368Sgnn	if (handler == NULL)
86235368Sgnn		abort();
87235368Sgnn	exception = e;
88235368Sgnn	longjmp(handler->loc, 1);
89235368Sgnn}
90
91
92/*
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.
101 */
102
103void
104onint(void)
105{
106	sigset_t sigs;
107
108	intpending = 0;
109	sigemptyset(&sigs);
110	sigprocmask(SIG_SETMASK, &sigs, NULL);
111
112	/*
113	 * This doesn't seem to be needed, since main() emits a newline.
114	 */
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);
125	}
126}
127
128
129static void
130vwarning(const char *msg, va_list ap)
131{
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");
138}
139
140
141void
142warning(const char *msg, ...)
143{
144	va_list ap;
145	va_start(ap, msg);
146	vwarning(msg, ap);
147	va_end(ap);
148}
149
150
151/*
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.
155 */
156static void
157exverror(int cond, const char *msg, va_list ap)
158{
159	/*
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.
164	 *
165	 * exraise() will disable interrupts for the exception handler.
166	 */
167	FORCEINTON;
168
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);
179}
180
181
182void
183error(const char *msg, ...)
184{
185	va_list ap;
186	va_start(ap, msg);
187	exverror(EXERROR, msg, ap);
188	va_end(ap);
189}
190
191
192void
193exerror(int cond, const char *msg, ...)
194{
195	va_list ap;
196	va_start(ap, msg);
197	exverror(cond, msg, ap);
198	va_end(ap);
199}
200