-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
62 lines (55 loc) · 1.3 KB
/
main.c
File metadata and controls
62 lines (55 loc) · 1.3 KB
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
#include "dictionary.h"
#include "repl.h"
#include "user.h"
#define UNUSED(x) (void)(x)
#define ALIAS_FILE ".meeshrc"
User *user = NULL;
Dict *aliases;
Repl *repl;
// TODO: recursive alias?
// TODO: Refactor exec external command functions
void sig_handler(int sig) { // walking dead!
if (sig == SIGINT){
printf("\n");
repl->print_prompt(repl);
return;
}
if (sig == SIGCHLD){
int status;
for (int i = 0; i < user->bg_pids_count; i++) {
pid_t pid = waitpid(user->bg_pids[i], &status, WNOHANG);
if (pid > 0) {
user->remove_bg_process(user, pid);
}
}
return;
}
}
void setup() {
// load aliases
aliases = Dict__new();
user = User__new_user();
Dict__load(aliases, ALIAS_FILE);
// catch ctrl-c
signal(SIGINT, sig_handler);
signal(SIGCHLD, sig_handler);
}
void teardown() {
for (int i = 0 ; i< user->bg_pids_count; i++) {
kill(user->bg_pids[i], SIGHUP);
}
printf("\nExiting shell\n");
// save aliases
Dict__dump(aliases, ALIAS_FILE);
// free aliases
Dict__free(aliases);
user->free(user);
repl->free(repl);
}
int main() {
setup();
repl = Repl__new( user, aliases);
repl->main_loop(repl);
teardown();
return 0;
}