-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell.c
70 lines (61 loc) · 1.45 KB
/
shell.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "signals.h"
#include "parser.h"
#include "runner.h"
#include "jobs.h"
int main() {
// Assign space for jobs and input
char* input = (char*) malloc(sizeof(char) * 2001);
jobs = (struct JobsList*) malloc(sizeof(struct JobsList));
init_jobs(20);
gpidRunning = 0;
setup_handlers();
jobDone = false;
do {
if (jobDone) {
print_jobs();
jobDone = false;
}
printf("# ");
fgets(input, 2000, stdin);
// If we get ^D then quit the terminal
if (feof(stdin)) {
break;
}
// If the error flag is set, don't parse input and skip line
if (ferror(stdin)) {
clearerr(stdin);
continue;
}
// Get rid of newline from fgets
input[strcspn(input, "\n")] = 0;
// Check input for job control commands
if (strcmp(input, "jobs") == 0) {
print_jobs();
} else if (strcmp(input, "fg") == 0) {
fg_job();
} else if (strcmp(input, "bg") == 0) {
bg_job();
} else {
// Parse input
struct ParsedInput* parsedInput = parse_input(input);
if (parsedInput != NULL) {
if (parsedInput->numCommands > 1) {
run(parsedInput);
} else {
run_single(parsedInput);
}
// Free the parsed input
free_tokens(parsedInput);
}
}
} while (1);
// Free jobs and input
free_jobs();
free(jobs);
free(input);
return 0;
}