1 /***********************************************************************
2  *                                                                      *
3  *               This software is part of the ast package               *
4  *          Copyright (c) 1982-2012 AT&T Intellectual Property          *
5  *                      and is licensed under the                       *
6  *                 Eclipse Public License, Version 1.0                  *
7  *                    by AT&T Intellectual Property                     *
8  *                                                                      *
9  *                A copy of the License is available at                 *
10  *          http://www.eclipse.org/org/documents/epl-v10.html           *
11  *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12  *                                                                      *
13  *              Information and Software Systems Research               *
14  *                            AT&T Research                             *
15  *                           Florham Park NJ                            *
16  *                                                                      *
17  *                    David Korn <dgkorn@gmail.com>                     *
18  *                                                                      *
19  ***********************************************************************/
20 //
21 // UNIX shell
22 // S. R. Bourne
23 // Rewritten by David Korn
24 //
25 #ifndef _FAULT_H
26 #define _FAULT_H 1
27 
28 #ifdef _AST_SIG_H
29 #error You cannot include fault.h after sig.h
30 #endif
31 
32 #include <setjmp.h>
33 #include <signal.h>
34 
35 #include "error.h"
36 
37 #ifndef SIGWINCH
38 #ifdef SIGWIND
39 #define SIGWINCH SIGWIND
40 #else  // SIGWIND
41 #ifdef SIGWINDOW
42 #define SIGWINCH SIGWINDOW
43 #endif  // SIGWINDOW
44 #endif  // SIGWIND
45 #endif  // SIGWINCH
46 
47 typedef void (*SH_SIGTYPE)(int, void (*)(int));
48 
49 #define SH_FORKLIM 16  // fork timeout interval
50 
51 #define SH_TRAP 0200    // bit for internal traps
52 #define SH_ERRTRAP 0    // trap for non-zero exit status
53 #define SH_KEYTRAP 1    // trap for keyboard event
54 #define SH_DEBUGTRAP 4  // must be last internal trap
55 
56 #define SH_SIGBITS 8
57 #define SH_SIGFAULT 1           // signal handler is sh_fault
58 #define SH_SIGOFF 2             // signal handler is SIG_IGN
59 #define SH_SIGSET 4             // pending signal
60 #define SH_SIGTRAP 010          // pending trap
61 #define SH_SIGDONE 020          // default is exit
62 #define SH_SIGIGNORE 040        // default is ignore signal
63 #define SH_SIGINTERACTIVE 0100  // handle interactive specially
64 #define SH_SIGTSTP 0200         // tstp signal received
65 #define SH_SIGTERM SH_SIGOFF    // term signal received
66 #define SH_SIGRUNTIME 0400      // runtime value
67 
68 #define SH_SIGRTMIN 0  // sh.sigruntime[] index
69 #define SH_SIGRTMAX 1  // sh.sigruntime[] index
70 
71 //
72 // These are longjmp values.
73 //
74 #define SH_JMPDOT 2
75 #define SH_JMPEVAL 3
76 #define SH_JMPTRAP 4
77 #define SH_JMPIO 5
78 #define SH_JMPCMD 6
79 #define SH_JMPFUN 7
80 #define SH_JMPERRFN 8
81 #define SH_JMPSUB 9
82 #define SH_JMPERREXIT 10
83 #define SH_JMPEXIT 11
84 #define SH_JMPSCRIPT 12
85 
86 struct openlist {
87     Sfio_t *strm;
88     struct openlist *next;
89 };
90 
91 struct checkpt {
92     sigjmp_buf buff;
93     struct checkpt *prev;
94     int topfd;
95     int mode;
96     int vexi;
97     struct openlist *olist;
98     Error_context_t err;
99 };
100 
101 typedef struct checkpt checkpt_t;
102 
103 struct siginfo_ll {
104     siginfo_t info;
105     struct siginfo_ll *next;
106     struct siginfo_ll *last;
107 };
108 typedef struct siginfo_ll siginfo_ll_t;
109 
110 #if USE_SPAWN
111 #define sh_pushcontext(shp, bp, n)                                                              \
112     ((bp)->mode = (n), (bp)->olist = NULL, (bp)->topfd = shp->topfd, (bp)->prev = shp->jmplist, \
113      (bp)->vexi = shp->vexp->cur, (bp)->err = *ERROR_CONTEXT_BASE, shp->jmplist = bp)
114 
115 #else  // USE_SPAWN
116 
117 #define sh_pushcontext(shp, bp, n)                                                              \
118     ((bp)->mode = (n), (bp)->olist = NULL, (bp)->topfd = shp->topfd, (bp)->prev = shp->jmplist, \
119      (bp)->err = *ERROR_CONTEXT_BASE, shp->jmplist = bp)
120 #endif  // USE_SPAWN
121 
122 #define sh_popcontext(shp, bp) (shp->jmplist = (bp)->prev, errorpop(&((bp)->err)))
123 
124 typedef void (*sh_sigfun_t)(int, siginfo_t *, void *);
125 extern sh_sigfun_t sh_signal(int, sh_sigfun_t);
126 extern void sh_fault(int, siginfo_t *, void *);
127 extern void sh_setsiginfo(siginfo_t *);
128 extern void set_trapinfo(Shell_t *shp, int sig, siginfo_t *info);
129 extern int sig_number(Shell_t *shp, const char *string);
130 #undef signal
131 #define signal(a, b) ERROR("use sh_signal() not signal()")
132 
133 extern __attribute__((noreturn)) void sh_done(void *, int);
134 extern void sh_siginit(Shell_t *shp);
135 extern Timer_t *sh_timeradd(unsigned long, int, void (*)(void *), void *);
136 extern void timerdel(void *);
137 
138 extern const char e_alarm[];
139 
140 #endif  // _FAULT_H
141