-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.c
More file actions
191 lines (174 loc) · 3.77 KB
/
eval.c
File metadata and controls
191 lines (174 loc) · 3.77 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/wait.h>
#include <assert.h>
#include "eval.h"
#include "parse.h"
#include "tokens.h"
#include "hashmap.h"
int do_pipe(sh_ast* left, sh_ast* right) {
int cpid;
if((cpid = fork())) {
waitpid(cpid, 0, 0);
}
else {
int rv;
int pipes[2];
rv = pipe(pipes);
assert(rv != -1);
if((cpid = fork())) {
close(0);
close(pipes[1]);
dup(pipes[0]);
waitpid(cpid, 0, 0);
close(pipes[0]);
}
else {
close(1);
close(pipes[0]);
dup(pipes[1]);
rv = ast_evalue(left);
close(pipes[1]);
exit(rv);
}
rv = ast_evalue(right);
exit(rv);
}
return 0;
}
int do_background(sh_ast* cmd) {
int cpid;
if(!(cpid = fork())) {
int st = ast_evalue(cmd);
exit(st);
}
return 0;
}
int do_andOr(sh_ast* left, sh_ast* right, int is_and) {
int cpid;
if((cpid = fork())) {
int rv;
int st;
rv = waitpid(cpid, &st, 0);
assert(rv != -1);
if(WEXITSTATUS(st)) {
exit(EXIT_FAILURE);
}
return rv;
}
else {
int st = ast_evalue(left);
if(!st ^ is_and) {
exit(st);
}
st = ast_evalue(right);
return st;
}
return 0;
}
int do_redirect(sh_ast* left, sh_ast* right, int is_left) {
int cpid;
// printf("redirect to: %s\n", right->argv[0]);
if((cpid = fork())) {
waitpid(cpid, 0, 0);
}
else {
int fd;
if(is_left) {
fd = open(right->argv[0], O_RDONLY);
assert(fd != -1);
close(0);
}
else {
fd = open(right->argv[0], O_CREAT | O_WRONLY, 0644);
assert(fd != -1);
close(1);
}
dup(fd);
close(fd);
int rv = ast_evalue(left);
exit(rv);
}
return 0;
}
int is(char* s1, char* s2) {
return strcmp(s1, s2) == 0;
}
int ast_evalue(sh_ast* ast) {
if(!ast) {
return 0;
}
if(ast->op) {
char* op = ast->op;
if(is(op, ";")) {
int rv;
ast_evalue(ast->left);
rv = ast_evalue(ast->right);
return rv;
}
else if(is(op, ">")) {
do_redirect(ast->left, ast->right, 0);
return 0;
}
else if(is(op, "<")) {
do_redirect(ast->left, ast->right, 1);
return 0;
}
else if(is(op, "&&")) {
do_andOr(ast->left, ast->right, 1);
return 0;
}
else if(is(op, "||")) {
do_andOr(ast->left, ast->right, 0);
return 0;
}
else if(is(op, "&")) {
do_background(ast->left);
return 0;
}
else if(is(op, "|")) {
do_pipe(ast->left, ast->right);
return 0;
}
}
// built in commands
if(is(ast->argv[0], "cd")) {
chdir(ast->argv[1]);
return 0;
}
else if (is(ast->argv[0], "exit")) {
exit(EXIT_FAILURE);
}
int cpid;
if ((cpid = fork())) {
// parent process
int status;
int rv;
rv = waitpid(cpid, &status, 0);
assert(rv != -1);
return status;
}
else {
if (ast->argv[0] && ast->argv[0][0] == '(') {
char* cmd = strdup(ast->argv[0] + 1);
cmd[strlen(cmd) - 1] = '\0';
list* tokens = tokenize(cmd);
hashmap* map = make_hashmap();
sh_ast* asts = parse(tokens, map);
ast_evalue(asts);
free_list(tokens);
free_ast(asts);
exit(1);
}
execvp(ast->argv[0], ast->argv);
printf("Can't get here, exec only returns on error.");
exit(1);
}
return 0;
}