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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2026 Richard QIn
*/
#include "ulib/stdio.h"
#include "ulib/stdlib.h"
#include "ulib/string.h"
#include "user/user.h"
#include "common/fcntl.h"
#include "drivers/uart_color.h"
constexpr int MAX_ARGS = 10;
const char *WHITESPACE = " \t\r\n\v";
const char *SYMBOLS = "<|>&;()";
void panic(const char *s)
{
printf("shell panic: %s\n", s);
exit(1);
}
int fork_or_panic()
{
int pid = fork();
if (pid == -1)
panic("fork");
return pid;
}
struct Command
{
virtual ~Command() = default;
virtual void run() = 0;
};
struct ExecCommand : public Command
{
char *argv[MAX_ARGS];
ExecCommand()
{
memset(argv, 0, sizeof(argv));
}
void run() override
{
if (argv[0] == nullptr)
exit(1);
exec(argv[0], argv);
printf("exec %s failed\n", argv[0]);
exit(1);
}
};
struct RedirCommand : public Command
{
Command *sub_cmd;
char *file;
char *efile; // file name end pointer (used by parser)
int mode;
int fd;
RedirCommand(Command *c, char *f, char *ef, int m, int fd_in)
: sub_cmd(c), file(f), efile(ef), mode(m), fd(fd_in) {}
~RedirCommand() { delete sub_cmd; }
void run() override
{
close(fd);
int newfd = open(file, mode);
if (newfd < 0)
{
printf("open %s failed\n", file);
exit(1);
}
if (newfd != fd)
{
if (dup(newfd) != fd)
{
printf("Redirection failed: could not move fd %d to %d\n", newfd, fd);
exit(1);
}
close(newfd);
}
sub_cmd->run();
}
};
struct PipeCommand : public Command
{
Command *left;
Command *right;
PipeCommand(Command *l, Command *r) : left(l), right(r) {}
~PipeCommand()
{
delete left;
delete right;
}
void run() override
{
int p[2];
if (pipe(p) < 0)
panic("pipe");
if (fork_or_panic() == 0)
{
// Left child: write to pipe
close(1); // close stdout
dup(p[1]); // dup write-end to stdout
close(p[0]);
close(p[1]);
left->run();
}
if (fork_or_panic() == 0)
{
// Right child: read from pipe
close(0); // close stdin
dup(p[0]); // dup read-end to stdin
close(p[0]);
close(p[1]);
right->run();
}
close(p[0]);
close(p[1]);
wait(0);
wait(0);
exit(0);
}
};
struct ListCommand : public Command
{
Command *left;
Command *right;
ListCommand(Command *l, Command *r) : left(l), right(r) {}
~ListCommand()
{
delete left;
delete right;
}
void run() override
{
if (fork_or_panic() == 0)
{
left->run();
}
wait(0);
right->run();
}
};
struct BackCommand : public Command
{
Command *sub_cmd;
BackCommand(Command *c) : sub_cmd(c) {}
~BackCommand() { delete sub_cmd; }
void run() override
{
if (fork_or_panic() == 0)
{
sub_cmd->run();
}
// Parent doesn't wait
exit(0);
}
};
class Parser
{
public:
static Command *parse(char *s)
{
char *es = s + strlen(s);
Command *cmd = parse_line(&s, es);
peek(&s, es, "");
if (s != es)
{
printf("leftovers: %s\n", s);
// panic("syntax"); // strict syntax check
}
terminate_strings(cmd);
return cmd;
}
private:
// Helper: NUL-terminate strings found during parsing
static void terminate_strings(Command *cmd)
{
if (!cmd)
return;
}
static char get_token(char **ps, char *es, char **q, char **eq)
{
char *s;
int ret;
s = *ps;
while (s < es && strchr(WHITESPACE, *s))
s++;
if (q)
*q = s;
ret = *s;
switch (*s)
{
case 0:
break;
case '|':
case '(':
case ')':
case ';':
case '&':
case '<':
s++;
break;
case '>':
s++;
if (*s == '>')
{
ret = '+';
s++;
}
break;
default:
ret = 'a';
while (s < es && !strchr(WHITESPACE, *s) && !strchr(SYMBOLS, *s))
s++;
break;
}
if (eq)
*eq = s;
while (s < es && strchr(WHITESPACE, *s))
s++;
*ps = s;
return ret;
}
static int peek(char **ps, char *es, const char *toks)
{
char *s = *ps;
while (s < es && strchr(WHITESPACE, *s))
s++;
*ps = s;
return *s && strchr(toks, *s);
}
// Recursive Descent Parsers
static Command *parse_line(char **ps, char *es)
{
Command *cmd = parse_pipe(ps, es);
while (peek(ps, es, "&"))
{
get_token(ps, es, 0, 0);
cmd = new BackCommand(cmd);
}
if (peek(ps, es, ";"))
{
get_token(ps, es, 0, 0);
cmd = new ListCommand(cmd, parse_line(ps, es));
}
return cmd;
}
static Command *parse_pipe(char **ps, char *es)
{
Command *cmd = parse_exec(ps, es);
if (peek(ps, es, "|"))
{
get_token(ps, es, 0, 0);
cmd = new PipeCommand(cmd, parse_pipe(ps, es));
}
return cmd;
}
static Command *parse_exec(char **ps, char *es)
{
char *q, *eq;
int tok, argc;
ExecCommand *cmd = new ExecCommand();
Command *ret = cmd;
argc = 0;
ret = parse_redirs(ret, ps, es);
while (!peek(ps, es, "|)&;"))
{
if ((tok = get_token(ps, es, &q, &eq)) == 0)
break;
if (tok != 'a')
panic("syntax");
cmd->argv[argc] = q;
*eq = 0; // Temp
argc++;
if (argc >= MAX_ARGS)
panic("too many args");
ret = parse_redirs(ret, ps, es);
}
cmd->argv[argc] = 0;
return ret;
}
static Command *parse_redirs(Command *cmd, char **ps, char *es)
{
int tok;
char *q, *eq;
while (peek(ps, es, "<>"))
{
tok = get_token(ps, es, 0, 0);
if (get_token(ps, es, &q, &eq) != 'a')
panic("missing file for redirection");
*eq = 0; // Null terminate filename immediately
switch (tok)
{
case '<':
cmd = new RedirCommand(cmd, q, eq, O_RDONLY, 0);
break;
case '>':
cmd = new RedirCommand(cmd, q, eq, O_WRONLY | O_CREATE | O_TRUNC, 1);
break;
case '+': // >>
cmd = new RedirCommand(cmd, q, eq, O_WRONLY | O_CREATE, 1); // Append not fully supported in simple OS yet?
break;
}
}
return cmd;
}
};
int getcmd(char *buf, int nbuf)
{
write(1, "$ ", 2);
memset(buf, 0, nbuf);
int n = read(0, buf, nbuf - 1);
if (n < 0)
return -1;
buf[n] = 0;
return 0;
}
int main()
{
static char buf[100];
printf(ANSI_GREEN "\n[Lume Shell] Welcome! \n" ANSI_RESET);
printf(ANSI_YELLOW "[Lume Shell] Start your journey of exploration!" ANSI_RESET);
printf("\n");
while (getcmd(buf, sizeof(buf)) >= 0)
{
int len = strlen(buf);
if (len > 0 && buf[len - 1] == '\n')
{
buf[len - 1] = 0;
}
if (buf[0] == 'c' && buf[1] == 'd' && (buf[2] == ' ' || buf[2] == '\n' || buf[2] == 0))
{
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0;
char *path = buf + 3;
if (buf[2] == '\n' || buf[2] == 0)
{
printf("cd: argument missing\n");
continue;
}
if (chdir(path) < 0)
{
printf("cannot cd %s\n", path);
}
continue;
}
if (strcmp(buf, "shutdown") == 0 || strcmp(buf, "poweroff") == 0)
{
printf(ANSI_BLUE "System shutting down..." ANSI_RESET);
printf("\n");
shutdown();
continue;
}
if (fork_or_panic() == 0)
{
Command *cmd = Parser::parse(buf);
if (cmd)
{
cmd->run();
delete cmd;
}
exit(0);
}
wait(0);
}
return 0;
}
|