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 *
3217987Speter *	@(#)error.h	8.2 (Berkeley) 5/4/95
3350471Speter * $FreeBSD$
341556Srgrimes */
351556Srgrimes
361556Srgrimes/*
371556Srgrimes * We enclose jmp_buf in a structure so that we can declare pointers to
381556Srgrimes * jump locations.  The global variable handler contains the location to
391556Srgrimes * jump to when an exception occurs, and the global variable exception
4046684Skris * contains a code identifying the exception.  To implement nested
411556Srgrimes * exception handlers, the user should save the value of handler on entry
421556Srgrimes * to an inner scope, set handler to point to a jmploc structure for the
431556Srgrimes * inner scope, and restore handler on exit from the scope.
441556Srgrimes */
451556Srgrimes
461556Srgrimes#include <setjmp.h>
4738521Scracauer#include <signal.h>
481556Srgrimes
491556Srgrimesstruct jmploc {
501556Srgrimes	jmp_buf loc;
511556Srgrimes};
521556Srgrimes
531556Srgrimesextern struct jmploc *handler;
5438530Scracauerextern volatile sig_atomic_t exception;
551556Srgrimes
561556Srgrimes/* exceptions */
571556Srgrimes#define EXINT 0		/* SIGINT received */
581556Srgrimes#define EXERROR 1	/* a generic error */
59218306Sjilles#define EXEXEC 2	/* command execution failed */
60220978Sjilles#define EXEXIT 3	/* call exitshell(exitstatus) */
611556Srgrimes
621556Srgrimes
631556Srgrimes/*
641556Srgrimes * These macros allow the user to suspend the handling of interrupt signals
651556Srgrimes * over a period of time.  This is similar to SIGHOLD to or sigblock, but
661556Srgrimes * much more efficient and portable.  (But hacking the kernel is so much
671556Srgrimes * more fun than worrying about efficiency and portability. :-))
681556Srgrimes */
691556Srgrimes
7038521Scracauerextern volatile sig_atomic_t suppressint;
7138521Scracauerextern volatile sig_atomic_t intpending;
721556Srgrimes
731556Srgrimes#define INTOFF suppressint++
7417987Speter#define INTON { if (--suppressint == 0 && intpending) onint(); }
75199660Sjilles#define is_int_on() suppressint
76199660Sjilles#define SETINTON(s) suppressint = (s)
771556Srgrimes#define FORCEINTON {suppressint = 0; if (intpending) onint();}
781556Srgrimes#define CLEAR_PENDING_INT intpending = 0
791556Srgrimes#define int_pending() intpending
801556Srgrimes
81200967Sjillesvoid exraise(int) __dead2;
8290111Simpvoid onint(void);
83216622Sjillesvoid warning(const char *, ...) __printflike(1, 2);
84200967Sjillesvoid error(const char *, ...) __printf0like(1, 2) __dead2;
85200967Sjillesvoid exerror(int, const char *, ...) __printf0like(2, 3) __dead2;
861556Srgrimes
871556Srgrimes
881556Srgrimes/*
891556Srgrimes * BSD setjmp saves the signal mask, which violates ANSI C and takes time,
901556Srgrimes * so we use _setjmp instead.
911556Srgrimes */
921556Srgrimes
931556Srgrimes#define setjmp(jmploc)	_setjmp(jmploc)
941556Srgrimes#define longjmp(jmploc, val)	_longjmp(jmploc, val)
95