11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)error.c	8.2 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD$");
401556Srgrimes
411556Srgrimes/*
421556Srgrimes * Errors and exceptions.
431556Srgrimes */
441556Srgrimes
451556Srgrimes#include "shell.h"
461556Srgrimes#include "main.h"
471556Srgrimes#include "options.h"
481556Srgrimes#include "output.h"
491556Srgrimes#include "error.h"
5053891Scracauer#include "nodes.h" /* show.h needs nodes.h */
5117987Speter#include "show.h"
5238521Scracauer#include "trap.h"
531556Srgrimes#include <signal.h>
5478732Sdd#include <stdlib.h>
5517987Speter#include <unistd.h>
561556Srgrimes#include <errno.h>
571556Srgrimes
581556Srgrimes
591556Srgrimes/*
601556Srgrimes * Code to handle exceptions in C.
611556Srgrimes */
621556Srgrimes
631556Srgrimesstruct jmploc *handler;
6438521Scracauervolatile sig_atomic_t exception;
6538530Scracauervolatile sig_atomic_t suppressint;
6638521Scracauervolatile sig_atomic_t intpending;
671556Srgrimeschar *commandname;
681556Srgrimes
691556Srgrimes
70213811Sobrienstatic void exverror(int, const char *, va_list) __printf0like(2, 0) __dead2;
7120425Ssteve
721556Srgrimes/*
731556Srgrimes * Called to raise an exception.  Since C doesn't include exceptions, we
741556Srgrimes * just do a longjmp to the exception handler.  The type of exception is
751556Srgrimes * stored in the global variable "exception".
76199660Sjilles *
77199660Sjilles * Interrupts are disabled; they should be reenabled when the exception is
78199660Sjilles * caught.
791556Srgrimes */
801556Srgrimes
811556Srgrimesvoid
8290111Simpexraise(int e)
8317987Speter{
84199660Sjilles	INTOFF;
851556Srgrimes	if (handler == NULL)
861556Srgrimes		abort();
871556Srgrimes	exception = e;
881556Srgrimes	longjmp(handler->loc, 1);
891556Srgrimes}
901556Srgrimes
911556Srgrimes
921556Srgrimes/*
931556Srgrimes * Called from trap.c when a SIGINT is received.  (If the user specifies
941556Srgrimes * that SIGINT is to be trapped or ignored using the trap builtin, then
9538536Scracauer * this routine is not called.)  Suppressint is nonzero when interrupts
9638521Scracauer * are held using the INTOFF macro.  If SIGINTs are not suppressed and
9738521Scracauer * the shell is not a root shell, then we want to be terminated if we
9838521Scracauer * get here, as if we were terminated directly by a SIGINT.  Arrange for
9938521Scracauer * this here.
1001556Srgrimes */
1011556Srgrimes
1021556Srgrimesvoid
10390111Simponint(void)
10490111Simp{
105211281Sjilles	sigset_t sigs;
10617987Speter
10738536Scracauer	/*
10838536Scracauer	 * The !in_dotrap here is safe.  The only way we can arrive here
10938536Scracauer	 * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL
11038521Scracauer	 * and killed itself.
11138521Scracauer	 */
11238521Scracauer
11338521Scracauer	if (suppressint && !in_dotrap) {
1141556Srgrimes		intpending++;
1151556Srgrimes		return;
1161556Srgrimes	}
1171556Srgrimes	intpending = 0;
118211281Sjilles	sigemptyset(&sigs);
119211281Sjilles	sigprocmask(SIG_SETMASK, &sigs, NULL);
12038521Scracauer
12138536Scracauer	/*
12238536Scracauer	 * This doesn't seem to be needed, since main() emits a newline.
12338536Scracauer	 */
12438521Scracauer#if 0
125155301Sschweikh	if (tcgetpgrp(0) == getpid())
12638521Scracauer		write(STDERR_FILENO, "\n", 1);
12738521Scracauer#endif
1281556Srgrimes	if (rootshell && iflag)
1291556Srgrimes		exraise(EXINT);
13038521Scracauer	else {
13138521Scracauer		signal(SIGINT, SIG_DFL);
13238521Scracauer		kill(getpid(), SIGINT);
13338521Scracauer	}
1341556Srgrimes}
1351556Srgrimes
1361556Srgrimes
137216622Sjillesstatic void
138216622Sjillesvwarning(const char *msg, va_list ap)
139216622Sjilles{
140216622Sjilles	if (commandname)
141216622Sjilles		outfmt(out2, "%s: ", commandname);
142216622Sjilles	doformat(out2, msg, ap);
143216622Sjilles	out2fmt_flush("\n");
144216622Sjilles}
145216622Sjilles
146216622Sjilles
147216622Sjillesvoid
148216622Sjilleswarning(const char *msg, ...)
149216622Sjilles{
150216622Sjilles	va_list ap;
151216622Sjilles	va_start(ap, msg);
152216622Sjilles	vwarning(msg, ap);
153216622Sjilles	va_end(ap);
154216622Sjilles}
155216622Sjilles
156216622Sjilles
1571556Srgrimes/*
15820425Ssteve * Exverror is called to raise the error exception.  If the first argument
1591556Srgrimes * is not NULL then error prints an error message using printf style
1601556Srgrimes * formatting.  It then raises the error exception.
1611556Srgrimes */
162213811Sobrienstatic void
16390111Simpexverror(int cond, const char *msg, va_list ap)
16420425Ssteve{
165199660Sjilles	/*
166199660Sjilles	 * An interrupt trumps an error.  Certain places catch error
167199660Sjilles	 * exceptions or transform them to a plain nonzero exit code
168199660Sjilles	 * in child processes, and if an error exception can be handled,
169199660Sjilles	 * an interrupt can be handled as well.
170199660Sjilles	 *
171199660Sjilles	 * exraise() will disable interrupts for the exception handler.
172199660Sjilles	 */
173199660Sjilles	FORCEINTON;
1741556Srgrimes
17520425Ssteve#ifdef DEBUG
17620425Ssteve	if (msg)
17720425Ssteve		TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
17820425Ssteve	else
17920425Ssteve		TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
18020425Ssteve#endif
181216622Sjilles	if (msg)
182216622Sjilles		vwarning(msg, ap);
18320425Ssteve	flushall();
18420425Ssteve	exraise(cond);
18520425Ssteve}
18620425Ssteve
18720425Ssteve
1881556Srgrimesvoid
18975577Skriserror(const char *msg, ...)
19017987Speter{
1911556Srgrimes	va_list ap;
19220425Ssteve	va_start(ap, msg);
19320425Ssteve	exverror(EXERROR, msg, ap);
19420425Ssteve	va_end(ap);
19520425Ssteve}
19617987Speter
19720425Ssteve
19820425Sstevevoid
19975577Skrisexerror(int cond, const char *msg, ...)
20020425Ssteve{
20120425Ssteve	va_list ap;
2021556Srgrimes	va_start(ap, msg);
20320425Ssteve	exverror(cond, msg, ap);
2041556Srgrimes	va_end(ap);
2051556Srgrimes}
206