-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
86 lines (73 loc) · 1.42 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <readline/history.h>
#include <readline/readline.h>
#include "builtins.h"
#include "executor.h"
#include "prompt.h"
#include "aliases.h"
#include "parser.h"
#include "command.h"
sigjmp_buf ctrlc;
static void handler(int signo)
{
if (signo == SIGINT)
{
signal_process(signo);
printf("\n");
siglongjmp(ctrlc, 1);
exit(0);
}
else if (signo == SIGCHLD)
{
pid_t pid;
int status;
pid = waitpid(-1, &status, WNOHANG);
}
}
void load_config()
{
char buf[256];
FILE *f = fopen(".dshrc", "r");
if (!f)
return;
while (fgets(buf, sizeof(buf), f))
{
*(strchr(buf, '\n')) = 0;
Command *c = parse(buf);
run(c);
free_cmds(c);
}
fclose(f);
}
int main()
{
signal(SIGINT, handler);
signal(SIGCHLD, handler);
char *input;
char *prompt;
load_config();
sigsetjmp(ctrlc, 1);
while (1)
{
prompt = get_prompt();
input = readline(prompt);
free(prompt);
if (!input) // EOF
{
printf("\n");
break;
}
if (input[0] != 0) add_history(input);
Command *c = parse(input);
run(c);
free_cmds(c);
free(input);
}
return 0;
}