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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
#include "gbos.h"
/* gbos shell: prompt -> read a line -> parse -> run, with I/O redirection
( > file, < file ) and pipes ( cmd1 | cmd2, via a temp file ).
Operators must be space-separated (e.g. cat readme > out ). */
#define ARGV0 ((char *)0xA000) /* where a child reads its command line */
/* split s into space-separated tokens (NUL-terminated in place). */
static unsigned char tokenize(char *s, char **tok, unsigned char max) {
unsigned char n = 0;
for (;;) {
while (*s == ' ') *s++ = 0;
if (*s == 0) break;
if (n < max) tok[n++] = s;
while (*s && *s != ' ') s++;
}
return n;
}
static unsigned char isop(char *t, char c) { return t[0] == c && t[1] == 0; }
static unsigned char iscmd(char *t, char a, char b) { return t[0] == a && t[1] == b && t[2] == 0; }
/* keep the prompt's path string roughly in sync with chdir(). */
static void set_cwd(char *cwd, char *arg) {
char *d, *s, *last;
if (arg[0] == '/') { /* absolute */
d = cwd;
s = arg;
while (*s) *d++ = *s++;
*d = 0;
} else if (arg[0] == '.' && arg[1] == '.' && arg[2] == 0) {
last = cwd;
d = cwd;
while (*d) {
if (*d == '/') last = d;
d++;
}
if (last == cwd) {
cwd[0] = '/';
cwd[1] = 0;
} else *last = 0;
} else if (arg[0] == '.' && arg[1] == 0) {
/* stay */
} else { /* relative: append */
d = cwd;
while (*d) d++;
if (!(cwd[0] == '/' && cwd[1] == 0)) *d++ = '/';
s = arg;
while (*s) *d++ = *s++;
*d = 0;
}
}
/* build the child command line "name\0arg1 arg2\0" at 0xA000 */
static void set_cmdline(char **tok, unsigned char start, unsigned char end) {
char *p = ARGV0, *t;
unsigned char i;
t = tok[start];
while (*t) *p++ = *t++;
*p++ = 0; /* NUL after the command word */
for (i = start + 1; i < end; i++) {
if (i > start + 1) *p++ = ' ';
t = tok[i];
while (*t) *p++ = *t++;
}
*p = 0;
}
/* run tok[start..end): parse its own >/< redirects, honor forced in/out.
If bg, launch in the background (don't wait; leave fds to the child). */
static void run(char **tok, unsigned char start, unsigned char end, unsigned char infd,
unsigned char outfd, unsigned char bg) {
unsigned char i, cmdend = end, myin = NOFD, myout = NOFD, id, pid;
for (i = start; i < end; i++) {
if (iscmd(tok[i], '>', '>')) {
if (i + 1 < end) myout = open(tok[i + 1], O_APPEND);
if (i < cmdend) cmdend = i;
} else if (isop(tok[i], '>')) {
if (i + 1 < end) myout = open(tok[i + 1], O_WRITE);
if (i < cmdend) cmdend = i;
} else if (isop(tok[i], '<')) {
if (i + 1 < end) myin = open(tok[i + 1], O_READ);
if (i < cmdend) cmdend = i;
}
}
id = lookup(tok[start]);
if (id == NOFD) {
puts(tok[start]);
puts(": not found");
nl();
goto done;
}
set_cmdline(tok, start, cmdend);
pid = fork();
if (pid == 0) {
if (myin != NOFD) setin(myin);
else if (infd != NOFD) setin(infd);
if (myout != NOFD) setout(myout);
else if (outfd != NOFD) setout(outfd);
exec(id);
}
if (bg) {
putc('[');
putu(pid);
puts("]");
nl();
return;
} /* background */
/* wait for THIS child; report any background jobs that finish meanwhile */
for (;;) {
unsigned char r = wait();
if (r == pid || r == NOFD) break;
putc('[');
putu(r);
puts(" done]");
nl();
}
done:
if (myin != NOFD) close(myin);
if (myout != NOFD) close(myout);
}
/* run a multi-stage pipeline (a | b | c): wire stages together with real
kernel pipes, launch them ALL concurrently, then wait for them. */
static void run_pipeline(char **tok, unsigned char nt) {
unsigned char pids[8], npid = 0;
unsigned char cfd[8], ncfd = 0; /* file-redirect fds; closed after all exit */
unsigned char start = 0, prev_r = NOFD;
for (;;) {
unsigned char end = start, last, i, cmdend, fin = NOFD, fout = NOFD;
unsigned char my_out = NOFD, next_r = NOFD, id, pid;
while (end < nt && !isop(tok[end], '|')) end++;
last = (end >= nt);
if (!last) { /* pipe from this stage to the next */
unsigned char pr = pipe();
if (pr != NOFD) {
next_r = pr;
my_out = pr + 1;
}
}
cmdend = end; /* this stage's own >/< redirects */
for (i = start; i < end; i++) {
if (iscmd(tok[i], '>', '>')) {
if (i + 1 < end) fout = open(tok[i + 1], O_APPEND);
if (i < cmdend) cmdend = i;
} else if (isop(tok[i], '>')) {
if (i + 1 < end) fout = open(tok[i + 1], O_WRITE);
if (i < cmdend) cmdend = i;
} else if (isop(tok[i], '<')) {
if (i + 1 < end) fin = open(tok[i + 1], O_READ);
if (i < cmdend) cmdend = i;
}
}
id = lookup(tok[start]);
if (id == NOFD) {
puts(tok[start]);
puts(": not found");
nl();
if (prev_r != NOFD) close(prev_r); /* orphaned pipe ends -> EOF/EPIPE */
if (my_out != NOFD) close(my_out);
if (fin != NOFD) close(fin);
if (fout != NOFD) close(fout);
} else {
set_cmdline(tok, start, cmdend);
pid = fork();
if (pid == 0) {
if (fin != NOFD) setin(fin);
else if (prev_r != NOFD) setin(prev_r);
if (fout != NOFD) setout(fout);
else if (my_out != NOFD) setout(my_out);
exec(id);
}
if (npid < 8) pids[npid++] = pid;
if (fin != NOFD && ncfd < 8) cfd[ncfd++] = fin;
if (fout != NOFD && ncfd < 8) cfd[ncfd++] = fout;
}
prev_r = next_r;
if (last) break;
start = end + 1;
}
{
unsigned char got = 0; /* wait for all stages */
while (got < npid) {
unsigned char r = wait(), j, mine = 0;
if (r == NOFD) break;
for (j = 0; j < npid; j++)
if (pids[j] == r) {
mine = 1;
break;
}
if (mine) got++;
else {
putc('[');
putu(r);
puts(" done]");
nl();
}
}
}
{
unsigned char k;
for (k = 0; k < ncfd; k++) close(cfd[k]);
}
}
void main(void) {
char line[80];
char *tok[16];
char cwd[40];
/* A shell whose stdin is a FILE is running a script: no prompt, no echo
of the script text, and end-of-input just ends the shell. Only init
(pid 1, on the console) treats EOF as "the machine is done". */
unsigned char tty = isatty();
unsigned char isinit = (unsigned char)(getpid() == 1);
cwd[0] = '/';
cwd[1] = 0;
/* bring up networking in the BACKGROUND: dhcp's recv blocks a long time when
no server answers, and wait()ing on it would leave the screen stuck at
"dhcp: discovering" with no prompt. Fork without waiting so the prompt is
immediate; the lease (or "no offer") lands a moment later and the job is
reaped by the loop below. */
if (isinit) {
unsigned char d = lookup("dhcp");
if (d != NOFD && fork() == 0) exec(d);
}
/* init runs /rc if it is there: a boot script, now that a shell fed from
a file behaves itself. Wait for it, so its output lands before the
first prompt. */
if (isinit) {
unsigned char fd = open("/rc", O_READ);
if (fd <= 3) {
unsigned char id = lookup("sh");
if (id != NOFD && fork() == 0) {
setin(fd);
exec(id);
}
wait();
close(fd);
}
}
for (;;) {
unsigned char nt, i, pipepos, c, bg;
/* reap finished background jobs */
{
unsigned char p;
while ((p = reap())) {
putc('[');
putu(p);
puts(" done]");
nl();
}
}
if (tty) {
puts(cwd);
puts("# ");
}
/* read a line (echoing), stop at newline; exit on EOF.
EOF means "the machine is done" only for pid 1 - init's console has
nowhere else to go. Any other shell is running a script
(`sh < file`, the child of a pipe, ...) and must simply exit, or
reaching the end of a script powers the Game Boy off. */
i = 0;
for (;;) {
c = readc();
if (c == EOF) {
if (tty && isinit) poweroff();
sexit(0);
}
if (c == '\r' || c == '\n') {
if (tty) nl();
break;
}
if (c == 8 || c == 127) { /* backspace: drop last char */
if (i > 0) {
i--;
if (tty) putc(8);
}
continue;
}
if (i < 79) {
line[i++] = c;
if (tty) putc(c);
}
}
line[i] = 0;
nt = tokenize(line, tok, 16);
if (nt == 0) continue;
if (iscmd(tok[0], 'c', 'd')) { /* cd builtin */
char *arg = (nt >= 2) ? tok[1] : "/";
if (chdir(arg) == 0) set_cwd(cwd, arg);
else {
puts("cd: no such directory");
nl();
}
continue;
}
if ((tok[0][0] == 'e' && tok[0][1] == 'x') || (tok[0][0] == 'q' && tok[0][1] == 'u'))
poweroff();
bg = 0;
if (isop(tok[nt - 1], '&')) {
bg = 1;
nt--;
if (nt == 0) continue;
}
pipepos = 0;
for (i = 1; i < nt; i++)
if (isop(tok[i], '|')) {
pipepos = i;
break;
}
if (pipepos) run_pipeline(tok, nt);
else run(tok, 0, nt, NOFD, NOFD, bg);
}
}
|