-
Notifications
You must be signed in to change notification settings - Fork 2
/
mumsh.c
65 lines (63 loc) · 1.71 KB
/
mumsh.c
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
#include "mumsh.h"
// for CTRL-C interruption
sigjmp_buf env;
volatile sig_atomic_t jump_active;
// main entry
int main() {
// subscribe ctrl-c signal and ignore tty-io signal
struct sigaction sa_SIGTT;
struct sigaction sa_SIGINT;
// appoint signal handler funtion (or set ignore)
sa_SIGTT.sa_handler = SIG_IGN;
sa_SIGINT.sa_handler = sigint_handler;
// making certain system calls restartable across signals
sa_SIGTT.sa_flags = SA_RESTART;
sa_SIGINT.sa_flags = SA_RESTART;
// no permission to interrupt execution of signal handler
sigemptyset(&sa_SIGTT.sa_mask);
sigemptyset(&sa_SIGINT.sa_mask);
// activate signal subscription
sigaction(SIGINT, &sa_SIGINT, NULL);
sigaction(SIGTTOU, &sa_SIGTT, NULL);
sigaction(SIGTTIN, &sa_SIGTT, NULL);
// main loop
while (1) {
// ctrl-c interruption jumps to here
if (sigsetjmp(env, 1) == 42) {
printf("\n");
// reap background jobs triggered by ctrl-c
reap_background_jobs();
free_cmds();
continue;
}
// safe lock in case CTRL-C come before jump point is set
jump_active = 1;
// prompt and read input
mumsh_read_cmds();
// reap background jobs triggered by input
reap_background_jobs();
// parse input command
if (mumsh_parser() != NORMAL) {
free_cmds();
continue;
}
// debug_parser();
// no command exist
if (cmd.cnt == 0) continue;
// cmd "exit"
if (mumsh_cmd_exit() == NORMAL) {
free_cmds();
free_jobs();
exit_process(NORMAL, "");
}
// built-in cmd "cd"
if (mumsh_cmd_cd() == NORMAL) {
free_cmds();
continue;
}
// execute cmds which run in child process
mumsh_exec_cmds();
free_cmds();
}
free_jobs();
}