-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp-sh.cc
275 lines (254 loc) · 7.36 KB
/
p-sh.cc
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "u-lib.hh"
#define ANDAND 1
#define OROR 2
static void run_list(char* list);
void process_main() {
static char buf[4096]; // static so stack size is small
size_t pos = 0;
bool done = false;
while (!done) {
// exit if too-long command line
if (pos == sizeof(buf) - 1) {
dprintf(2, "Command line too long\n");
sys_exit(1);
}
// print prompt
if (pos == 0) {
ssize_t n = sys_write(1, "sh$ ", 4);
assert(n == 4);
}
// read from input
ssize_t n = sys_read(0, buf + pos, sizeof(buf) - pos - 1);
if (n < 0) {
dprintf(2, "Error reading from stdin\n");
sys_exit(1);
} else if (n == 0) {
buf[pos + n] = '\n';
done = true;
++n;
}
pos += n;
buf[pos] = 0;
// null characters not allowed
assert(memchr(buf, 0, pos) == nullptr);
// process lists from input
size_t i = 0;
while (true) {
// skip whitespace
while (isspace((unsigned char) buf[i])) {
++i;
}
// scan ahead to next separator
size_t j = i;
while (true) {
if (buf[j] == ';' || buf[j] == '\n' || buf[j] == 0) {
break;
} else if (buf[j] == '&') {
if (buf[j + 1] == 0) {
++j;
} else if (buf[j + 1] == '&') {
j += 2;
} else {
break;
}
} else {
++j;
}
}
// process separated list
char sep = buf[j];
if (sep == '&') {
pid_t p = sys_fork();
assert(p >= 0);
if (p == 0) {
buf[j] = 0;
run_list(buf + i);
sys_exit(0);
}
i = j + 1;
} else if (sep == ';' || sep == '\n') {
buf[j] = 0;
run_list(buf + i);
i = j + 1;
} else {
break;
}
}
// move remaining text to start of `buf`
memmove(buf, buf + i, n - i);
pos = n - i;
// reap zombies
while (sys_waitpid(0, nullptr, W_NOHANG) > 0) {
}
}
sys_exit(0);
}
static pid_t create_child(char** words, char nextch,
char** redir, int* pipein);
static void run_list(char* s) {
static char* words[512];
char** word = words;
int pipein = 0;
int skip_status = -1;
char* redir[] = { nullptr, nullptr, nullptr };
int redir_status = -1;
while (*s || word != words) {
// read a word
while (isspace((unsigned char) *s)
&& *s != '\n') {
++s;
}
char* wordstart = s;
while (*s != 0
&& !isspace((unsigned char) *s)
&& *s != ';'
&& *s != '&'
&& *s != '|'
&& *s != '<'
&& *s != '>') {
++s;
}
char nextch = *s;
if (*s != 0) {
*s = 0;
++s;
}
if (nextch == '&' && *s == '&') {
nextch = ANDAND;
++s;
} else if (nextch == '|' && *s == '|') {
nextch = OROR;
++s;
}
// add to word list
if (*wordstart != 0) {
if (redir_status != -1) {
redir[redir_status] = wordstart;
redir_status = -1;
} else {
*word = wordstart;
++word;
assert(word < words + arraysize(words));
}
} else {
if (word == words
&& redir_status == -1
&& pipein == 0) {
break;
}
assert(word != words);
assert(redir_status == -1);
}
// maybe execute
if (nextch == '<') {
redir_status = 0;
} else if (nextch == '>') {
redir_status = 1;
} else if (!isspace((unsigned char) nextch)) {
*word = nullptr;
if (skip_status < 0) {
pid_t child = create_child(words, nextch,
redir, &pipein);
if (nextch != '|'
&& nextch != '&'
&& child > 0) {
int r, status = -1;
while ((r = sys_waitpid(child, &status)) == E_AGAIN) {
}
assert(r == child);
if ((status == 0 && nextch == OROR)
|| (status != 0 && nextch == ANDAND)) {
skip_status = status != 0;
}
}
redir[0] = redir[1] = redir[2] = nullptr;
} else {
if ((skip_status == 0 && nextch == ANDAND)
|| (skip_status != 0 && nextch == OROR)) {
skip_status = -1;
}
}
word = words;
}
}
}
static void child_redirect(int fd, char* pathname) {
int flags;
if (fd == 0) {
flags = OF_READ;
} else {
flags = OF_WRITE | OF_CREATE | OF_TRUNC;
}
int r = sys_open(pathname, flags);
if (r < 0) {
dprintf(2, "%s: error %d\n", pathname, r);
sys_exit(1);
}
if (r != fd) {
sys_dup2(r, fd);
sys_close(r);
}
}
static pid_t create_child(char** words, char nextch,
char** redir, int* pipein) {
int pfd[2] = {0, 0};
if (nextch == '|') {
int r = sys_pipe(pfd);
assert_gt(r, -1);
}
// handle `exit [N]`
int exit_status = -1;
if (strcmp(words[0], "exit") == 0) {
if (words[1] && isdigit((unsigned char) words[1][0])) {
char* endl;
exit_status = strtol(words[1], &endl, 10);
if (*endl != '\0' || exit_status < 0) {
exit_status = -2;
}
} else if (words[1]) {
exit_status = -2;
}
}
pid_t child = sys_fork();
if (child == 0) {
if (*pipein != 0) {
sys_dup2(*pipein, 0);
sys_close(*pipein);
}
for (int fd = 0; fd != 3; ++fd) {
if (redir[fd] != nullptr) {
child_redirect(fd, redir[fd]);
}
}
if (nextch == '|') {
sys_close(pfd[0]);
sys_dup2(pfd[1], 1);
sys_close(pfd[1]);
}
if (exit_status == -1) {
// normal command execution
int r = sys_execv(words[0], words);
dprintf(2, "%s: execv failed (error %d)\n", words[0], r);
exit_status = 1;
} else {
// `exit` command
if (exit_status == -2) {
dprintf(2, "exit: numeric argument required\n");
exit_status = 1;
}
}
sys_exit(exit_status);
}
if (*pipein != 0) {
sys_close(*pipein);
*pipein = 0;
}
if (nextch == '|') {
*pipein = pfd[0];
sys_close(pfd[1]);
}
if (exit_status != -1) {
sys_exit(exit_status < 0 ? 1 : exit_status);
}
return child;
}