master xplshn/aruu / cmd / posix / sh / error.c
  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 "shell.h"
 40#include "eval.h"
 41#include "main.h"
 42#include "options.h"
 43#include "output.h"
 44#include "error.h"
 45#include "nodes.h" /* show.h needs nodes.h */
 46#include "show.h"
 47#include "trap.h"
 48#include <signal.h>
 49#include <stdlib.h>
 50#include <unistd.h>
 51#include <errno.h>
 52
 53
 54/*
 55 * Code to handle exceptions in C.
 56 */
 57
 58struct jmploc *handler;
 59volatile sig_atomic_t exception;
 60volatile sig_atomic_t suppressint;
 61volatile sig_atomic_t intpending;
 62
 63
 64static void verrorwithstatus(int, const char *, va_list) __printf0like(2, 0) __dead2;
 65
 66/*
 67 * Called to raise an exception.  Since C doesn't include exceptions, we
 68 * just do a longjmp to the exception handler.  The type of exception is
 69 * stored in the global variable "exception".
 70 *
 71 * Interrupts are disabled; they should be re-enabled when the exception is
 72 * caught.
 73 */
 74
 75void
 76exraise(int e)
 77{
 78	INTOFF;
 79	if (handler == NULL)
 80		abort();
 81	exception = e;
 82	longjmp(handler->loc, 1);
 83}
 84
 85
 86/*
 87 * Called from trap.c when a SIGINT is received and not suppressed, or when
 88 * an interrupt is pending and interrupts are re-enabled using INTON.
 89 * (If the user specifies that SIGINT is to be trapped or ignored using the
 90 * trap builtin, then this routine is not called.)  Suppressint is nonzero
 91 * when interrupts are held using the INTOFF macro.  If SIGINTs are not
 92 * suppressed and the shell is not a root shell, then we want to be
 93 * terminated if we get here, as if we were terminated directly by a SIGINT.
 94 * Arrange for this here.
 95 */
 96
 97void
 98onint(void)
 99{
100	sigset_t sigs;
101
102	intpending = 0;
103	sigemptyset(&sigs);
104	sigprocmask(SIG_SETMASK, &sigs, NULL);
105
106	/*
107	 * This doesn't seem to be needed, since main() emits a newline.
108	 */
109#if 0
110	if (tcgetpgrp(0) == getpid())
111		write(STDERR_FILENO, "\n", 1);
112#endif
113	if (rootshell && iflag)
114		exraise(EXINT);
115	else {
116		signal(SIGINT, SIG_DFL);
117		kill(getpid(), SIGINT);
118		_exit(128 + SIGINT);
119	}
120}
121
122
123static void
124vwarning(const char *msg, va_list ap)
125{
126	if (commandname)
127		outfmt(out2, "%s: ", commandname);
128	else if (arg0)
129		outfmt(out2, "%s: ", arg0);
130	doformat(out2, msg, ap);
131	out2fmt_flush("\n");
132}
133
134
135void
136warning(const char *msg, ...)
137{
138	va_list ap;
139	va_start(ap, msg);
140	vwarning(msg, ap);
141	va_end(ap);
142}
143
144
145/*
146 * Exverror is called to raise the error exception.  If the first argument
147 * is not NULL then error prints an error message using printf style
148 * formatting.  It then raises the error exception.
149 */
150static void
151verrorwithstatus(int status, const char *msg, va_list ap)
152{
153	/*
154	 * An interrupt trumps an error.  Certain places catch error
155	 * exceptions or transform them to a plain nonzero exit code
156	 * in child processes, and if an error exception can be handled,
157	 * an interrupt can be handled as well.
158	 *
159	 * exraise() will disable interrupts for the exception handler.
160	 */
161	FORCEINTON;
162
163#ifdef DEBUG
164	if (msg)
165		TRACE(("verrorwithstatus(%d, \"%s\") pid=%d\n",
166		    status, msg, getpid()));
167	else
168		TRACE(("verrorwithstatus(%d, NULL) pid=%d\n",
169		    status, getpid()));
170#endif
171	if (msg)
172		vwarning(msg, ap);
173	flushall();
174	exitstatus = status;
175	exraise(EXERROR);
176}
177
178
179void
180error(const char *msg, ...)
181{
182	va_list ap;
183	va_start(ap, msg);
184	verrorwithstatus(2, msg, ap);
185	va_end(ap);
186}
187
188
189void
190errorwithstatus(int status, const char *msg, ...)
191{
192	va_list ap;
193	va_start(ap, msg);
194	verrorwithstatus(status, msg, ap);
195	va_end(ap);
196}