revision: reorder `read_revisions_from_stdin()`
[alt-git.git] / sigchain.c
blobee778c0580b6d9eb47ebc0d5f1ea7a9d7637a96c
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "sigchain.h"
5 #define SIGCHAIN_MAX_SIGNALS 32
7 struct sigchain_signal {
8 sigchain_fun *old;
9 int n;
10 int alloc;
12 static struct sigchain_signal signals[SIGCHAIN_MAX_SIGNALS];
14 static void check_signum(int sig)
16 if (sig < 1 || sig >= SIGCHAIN_MAX_SIGNALS)
17 BUG("signal out of range: %d", sig);
20 int sigchain_push(int sig, sigchain_fun f)
22 struct sigchain_signal *s = signals + sig;
23 check_signum(sig);
25 ALLOC_GROW(s->old, s->n + 1, s->alloc);
26 s->old[s->n] = signal(sig, f);
27 if (s->old[s->n] == SIG_ERR)
28 return -1;
29 s->n++;
30 return 0;
33 int sigchain_pop(int sig)
35 struct sigchain_signal *s = signals + sig;
36 check_signum(sig);
37 if (s->n < 1)
38 return 0;
40 if (signal(sig, s->old[s->n - 1]) == SIG_ERR)
41 return -1;
42 s->n--;
43 return 0;
46 void sigchain_push_common(sigchain_fun f)
48 sigchain_push(SIGINT, f);
49 sigchain_push(SIGHUP, f);
50 sigchain_push(SIGTERM, f);
51 sigchain_push(SIGQUIT, f);
52 sigchain_push(SIGPIPE, f);
55 void sigchain_pop_common(void)
57 sigchain_pop(SIGPIPE);
58 sigchain_pop(SIGQUIT);
59 sigchain_pop(SIGTERM);
60 sigchain_pop(SIGHUP);
61 sigchain_pop(SIGINT);