-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_state.c
69 lines (53 loc) · 1.61 KB
/
shell_state.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 <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "shell_state.h"
#include "job.h"
void shell_init(ShellState* shell_state, bool batch) {
StrVec search_path;
vec_init(&search_path);
vec_append(&search_path, "/bin/");
shell_state->search_path = search_path;
StrVec built_in;
vec_init(&built_in);
vec_append(&built_in, "exit");
vec_append(&built_in, "cd");
vec_append(&built_in, "path");
shell_state->built_in = built_in;
shell_state->batch = batch;
}
void execute_built_in(ShellState* shell_state, StrVec* tokens) {
char* cmd = vec_get(tokens, 0);
if(strcmp(cmd, "exit") == 0) {
exit(0);
} else if(strcmp(cmd, "cd") == 0) {
if(tokens->len != 2) {
report_error();
return;
}
chdir(vec_get(tokens, 1));
} else if(strcmp(cmd, "path") == 0) {
for(int i=1;i<tokens->len;i++) {
char* path = strdup(vec_get(tokens, i));
int len = strlen(path);
if(path[len-1] != '/') {
path = (char*) realloc(path, (len+2) * sizeof(char));
path[len] = '/';
}
vec_append(&shell_state->search_path, path);
}
}
}
void shell_execute(ShellState* shell_state, Job job) {
if(vec_includes(&shell_state->built_in, vec_get(&job.args, 0))) {
execute_built_in(shell_state, &job.args);
return;
}
job_execute(&job);
}
void report_error() {
char error_message[30] = "An error has occurred\n";
fprintf(stderr, "%s", error_message);
}