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#include "sh_cdefs.h"
36
37/*
38 * We enclose jmp_buf in a structure so that we can declare pointers to
39 * jump locations. The global variable handler contains the location to
40 * jump to when an exception occurs, and the global variable exception
41 * contains a code identifying the exception. To implement nested
42 * exception handlers, the user should save the value of handler on entry
43 * to an inner scope, set handler to point to a jmploc structure for the
44 * inner scope, and restore handler on exit from the scope.
45 */
46
47#include <setjmp.h>
48#include <signal.h>
49
50struct jmploc {
51 jmp_buf loc;
52};
53
54extern struct jmploc *handler;
55extern volatile sig_atomic_t exception;
56
57/* exceptions */
58#define EXINT 0 /* SIGINT received */
59#define EXERROR 1 /* a generic error with exitstatus */
60#define EXEXIT 2 /* call exitshell(exitstatus) */
61
62/*
63 * These macros allow the user to suspend the handling of interrupt signals
64 * over a period of time. This is similar to SIGHOLD to or sigblock, but
65 * much more efficient and portable. (But hacking the kernel is so much
66 * more fun than worrying about efficiency and portability. :-))
67 */
68
69extern volatile sig_atomic_t suppressint;
70extern volatile sig_atomic_t intpending;
71
72#define INTOFF suppressint++
73#define INTON \
74 { \
75 if (--suppressint == 0 && intpending) \
76 onint(); \
77 }
78#define is_int_on() suppressint
79#define SETINTON(s) \
80 do { \
81 suppressint = (s); \
82 if (suppressint == 0 && intpending) \
83 onint(); \
84 } while (0)
85#define FORCEINTON \
86 { \
87 suppressint = 0; \
88 if (intpending) \
89 onint(); \
90 }
91#define SET_PENDING_INT intpending = 1
92#define CLEAR_PENDING_INT intpending = 0
93#define int_pending() intpending
94
95void exraise(int) __dead2;
96void onint(void) __dead2;
97void warning(const char *, ...) __printflike(1, 2);
98void error(const char *, ...) __printf0like(1, 2) __dead2;
99void errorwithstatus(int, const char *, ...) __printf0like(2, 3) __dead2;
100
101/*
102 * BSD setjmp saves the signal mask, which violates ANSI C and takes time,
103 * so we use _setjmp instead.
104 */
105
106#undef setjmp
107#define setjmp(jmploc) _setjmp(jmploc)
108#undef longjmp
109#define longjmp(jmploc, val) _longjmp(jmploc, val)