-
Notifications
You must be signed in to change notification settings - Fork 4
/
interpreter.c
258 lines (211 loc) · 4.1 KB
/
interpreter.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* see LICENSE file for copyright and license details */
/* recursive walk down the AST, implementing the various operators */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "reporting.h"
#include "region.h"
#include "stringport.h"
#include "tokenizer.h"
#include "variables.h"
#include "parser.h"
#include "interpreter.h"
#include "builtins.h"
#include "util.h"
//#include "linenoise.h"
//#include "encodings/utf8.h"
int interactive_mode = 0;
void
interpret_command(struct AST* n)
{
assert(n->type == NODE_COMMAND);
execvp(n->node.tokens[0], n->node.tokens);
_reporterr("%s: command not found", n->node.tokens[0]);
}
void
interpret_junction(struct AST* n)
{
pid_t p;
int r;
if (n->type == NODE_COMMAND)
interpret_command(n);
switch (p = fork()) {
case -1:
_reporterr("fork() failure");
break;
case 0:
interpret(n->node.child.l);
break;
default:
waitpid(p, &r, 0);
/* DISJ and CONJ are dual */
/* xor flips the boolean for us depending on the case we're in */
/* so: in a disj (conj) node we exit right away */
/* if the exit status is (isn't) true */
/* otherwise continue executing the disj (conj) */
/* chain. */
if ((!WEXITSTATUS(r)) ^ (n->type == NODE_CONJ))
_exit(WEXITSTATUS(r));
else
interpret(n->node.child.r);
break;
}
}
void
interpret_pipe(struct AST* n)
{
if (n->type == NODE_COMMAND)
interpret_command(n);
int fd[2];
int f;
pipe(fd);
f = fork();
if (f == -1) {
_reporterr("fork() failure");
} else if (!f) { /* child */
close(fd[0]);
close(STDOUT_FILENO);
dup(fd[1]);
close(fd[1]);
interpret_command(n->node.child.l);
} else { /* parent */
close(fd[1]);
close(STDIN_FILENO);
dup(fd[0]);
close(fd[0]);
interpret_pipe(n->node.child.r);
}
}
void
interpret(struct AST* n)
{
switch (n->type) {
case NODE_COMMAND:
interpret_command(n);
break;
case NODE_CONJ:
case NODE_DISJ:
interpret_junction(n);
break;
case NODE_PIPE:
interpret_pipe(n);
break;
}
}
int
prompt(string_port *port)
{
char *line;
if (interactive_mode) {
/*
if ((line = getline(geteuid() == 0 ? "s# " : "s$ "))) {
*port = (string_port){ .kind=STRPORT_CHAR, .text=line, .place=0 };
return 0;
}
*/
return 1;
}
return 0;
}
void
drain_pipe(int fd, char **out)
{
int len = 0;
int size = 1000;
char *str = malloc(size);
int i, n;
int delta = 0;
/* read everything from the pipe into a buffer */
while ((n = read(fd, str + len, size - 1 - len)) > 0) {
len += n;
if (len > size/2) {
size *= 2;
str = realloc(str, size);
}
}
/* now strip out the \0 characters */
for (i = 0; i < len; i++) {
str[i] = str[i + delta];
if (str[i] == '\0') {
delta++;
len--;
i--;
continue;
}
}
str[len] = '\0';
*out = str;
}
int
parse_and_execute(string_port *port, char **string_capture)
{
pid_t p;
region r;
struct AST *n;
int bg;
int status = 0;
int fd[2];
if (string_capture)
*string_capture = NULL;
region_create(&r);
n = parse(&r, port, &bg);
if (n && !perform_builtin(n)) {
if (string_capture)
pipe(&fd[0]);
if (!(p = fork())) {
if (string_capture) {
close(fd[0]);
close(STDOUT_FILENO);
dup(fd[1]);
}
interpret(n);
_reporterr("== SHOULD NEVER GET HERE ==");
}
if (string_capture) {
close(fd[1]);
drain_pipe(fd[0], string_capture);
close(fd[0]);
}
if (!bg)
waitpid(p, &status, 0);
}
region_free(&r);
return status;
}
void
interpreter_loop(FILE *f)
{
string_port port;
if (interactive_mode) {
/*
linenoiseSetEncodingFunctions(
linenoiseUtf8PrevCharLen,
linenoiseUtf8NextCharLen,
linenoiseUtf8ReadCode);
*/
}
else
port = (string_port){ .kind=STRPORT_FILE, .fptr=f };
do {
if (prompt(&port)) {
if (errno == EAGAIN) {
errno = 0;
continue;
} else break;
}
parse_and_execute(&port, NULL);
if (interactive_mode) {
/* TODO: Only add if command was sucessful? */
// linenoiseHistoryAdd(port.text);
efree(port.text);
} else {
skip_newline(&port);
}
} while (!feof(f));
fclose(f);
vars_unset();
}