/*-1* Copyright (c) 1991, 19932* The Regents of the University of California. All rights reserved.3*4* This code is derived from software contributed to Berkeley by5* Kenneth Almquist.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. Neither the name of the University nor the names of its contributors16* may be used to endorse or promote products derived from this software17* without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132/*33* We enclose jmp_buf in a structure so that we can declare pointers to34* jump locations. The global variable handler contains the location to35* jump to when an exception occurs, and the global variable exception36* contains a code identifying the exception. To implement nested37* exception handlers, the user should save the value of handler on entry38* to an inner scope, set handler to point to a jmploc structure for the39* inner scope, and restore handler on exit from the scope.40*/4142#include <setjmp.h>43#include <signal.h>4445struct jmploc {46jmp_buf loc;47};4849extern struct jmploc *handler;50extern volatile sig_atomic_t exception;5152/* exceptions */53#define EXINT 0 /* SIGINT received */54#define EXERROR 1 /* a generic error with exitstatus */55#define EXEXIT 2 /* call exitshell(exitstatus) */565758/*59* These macros allow the user to suspend the handling of interrupt signals60* over a period of time. This is similar to SIGHOLD to or sigblock, but61* much more efficient and portable. (But hacking the kernel is so much62* more fun than worrying about efficiency and portability. :-))63*/6465extern volatile sig_atomic_t suppressint;66extern volatile sig_atomic_t intpending;6768#define INTOFF suppressint++69#define INTON { if (--suppressint == 0 && intpending) onint(); }70#define is_int_on() suppressint71#define SETINTON(s) do { suppressint = (s); if (suppressint == 0 && intpending) onint(); } while (0)72#define FORCEINTON {suppressint = 0; if (intpending) onint();}73#define SET_PENDING_INT intpending = 174#define CLEAR_PENDING_INT intpending = 075#define int_pending() intpending7677void exraise(int) __dead2;78void onint(void) __dead2;79void warning(const char *, ...) __printflike(1, 2);80void error(const char *, ...) __printf0like(1, 2) __dead2;81void errorwithstatus(int, const char *, ...) __printf0like(2, 3) __dead2;828384/*85* BSD setjmp saves the signal mask, which violates ANSI C and takes time,86* so we use _setjmp instead.87*/8889#define setjmp(jmploc) _setjmp(jmploc)90#define longjmp(jmploc, val) _longjmp(jmploc, val)919293