1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#define MAX_SIGNALS 32
#define print(fmt, ...) fprintf(stderr, "test_program: "fmt, ##__VA_ARGS__)
_Bool do_raise = 0;
int raise_signal;
_Bool do_Com_Error = 0;
_Bool recursive = 0;
enum { DEFAULT = 0, CATCH, IGNORE };
int handle[MAX_SIGNALS] = {0};
_Bool handle_any = 0;
_Bool verbose = 0;
static int read_args(int argc, char **argv)
{
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--raise")) {
if (i + 1 >= argc)
goto needs_argument;
do_raise = 1;
raise_signal = atoi(argv[i + 1]);
i++;
} else if (!strcmp(argv[i], "--Com_Error")) {
do_Com_Error = 1;
} else if (!strcmp(argv[i], "--recursive")) {
recursive = 1;
} else if (!strcmp(argv[i], "--catch") ||
!strcmp(argv[i], "--ignore")) {
int sig;
if (i + 1 >= argc)
goto needs_argument;
sig = atoi(argv[i + 1]);
if (sig < 0 || sig >= MAX_SIGNALS) {
print("invalid argument for --catch/--ignore: %i\n",
sig);
return 1;
}
if (!strcmp(argv[i], "--catch"))
handle[sig] = CATCH;
else
handle[sig] = IGNORE;
handle_any = 1;
i++;
} else if (!strcmp(argv[i], "--ignore")) {
} else if (!strcmp(argv[i], "--verbose")) {
verbose = 1;
} else {
print("invalid option '%s'\n", argv[i]);
return 1;
}
continue;
needs_argument:
print("option '%s' needs an argument\n", argv[i]);
return 1;
}
if (do_Com_Error && do_raise) {
print("--raise and --Com_Error are mutually exclusive.\n");
return 1;
}
return 0;
}
_Noreturn void graceful_exit(void)
{
print("Graceful exit.\n");
exit(0);
}
void signal_catcher(int sig)
{
print("Caught signal %i.\n", sig);
if (recursive) {
if (verbose)
print("Raising signal %i from within the handler.\n",
raise_signal);
raise(raise_signal);
}
graceful_exit();
}
_Noreturn void Com_Error(int type, const char *str)
{
print("Com_Error: %s\n", str);
if (recursive)
Com_Error(666, "Error while erroring");
graceful_exit();
}
int main(int argc, char **argv)
{
int rv;
rv = read_args(argc, argv);
if (rv)
return rv;
for (int i = 0; i < MAX_SIGNALS; i++) {
void (*fun)(int);
switch (handle[i]) {
case DEFAULT:
continue;
case CATCH:
fun = signal_catcher;
if (verbose)
print("Signal %i will be caught.\n", i);
break;
case IGNORE:
fun = SIG_IGN;
if (verbose)
print("Signal %i will be ignored.\n", i);
break;
}
if (signal(i, fun) == SIG_ERR) {
print("For signal %i: ", i);
perror("signal");
return 1;
}
}
if (do_raise) {
print("Raising signal %i.\n", raise_signal);
raise(raise_signal);
}
if (do_Com_Error)
Com_Error(123, "Some error string");
print("Reached the end of main().\n");
graceful_exit();
}
|