-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminishell.c
More file actions
520 lines (497 loc) · 13.2 KB
/
Copy pathminishell.c
File metadata and controls
520 lines (497 loc) · 13.2 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// minishell.c - kernel minishell, no libc, no processes, no filesystem
#include "console.h"
#include "vga.h"
#define LINE_LEN 256
#define MAX_TOKENS 64
#define MAX_VARS 32
#define MAX_ARRAY_SIZE 64
// ---- minimal helpers (no libc) ----
static int k_isspace(char c) { return c == ' ' || c == '\t' || c == '\r'; }
static int k_isdigit(char c) { return c >= '0' && c <= '9'; }
static int k_strcmp(const char *a, const char *b) {
while (*a && *a == *b) {
a++;
b++;
}
return (unsigned char)*a - (unsigned char)*b;
}
static void k_strcpy(char *d, const char *s) {
while ((*d++ = *s++))
;
}
static void k_memcpy(char *d, const char *s, int n) {
for (int i = 0; i < n; i++)
d[i] = s[i];
}
static int k_atoi(const char *s) {
int n = 0, neg = 0;
if (*s == '-') {
neg = 1;
s++;
}
while (k_isdigit(*s))
n = n * 10 + (*s++ - '0');
return neg ? -n : n;
}
static void print_int(int val) {
if (val == 0) {
print("0", 0);
return;
}
if (val < 0) {
print("-", 0);
val = -val;
}
char buf[12];
int i = 11;
buf[i] = '\0';
while (val > 0) {
buf[--i] = '0' + val % 10;
val /= 10;
}
print(buf + i, 0);
}
// ---- token pool (replaces malloc) ----
// 每个嵌套层用独立的 pool,防止内层 tokenize 覆盖外层 tokens[]
#define MAX_DEPTH 6
static char tok_pools[MAX_DEPTH][MAX_TOKENS][LINE_LEN];
static int pool_depth = 0;
static char val_pool[MAX_ARRAY_SIZE][LINE_LEN]; // array-init 专用,独立于深度池
static void pool_set(char pool[][LINE_LEN], int cols, int idx, const char *s,
int len) {
(void)cols;
if (len > LINE_LEN - 1)
len = LINE_LEN - 1;
k_memcpy(pool[idx], s, len);
pool[idx][len] = '\0';
}
// ---- data types ----
typedef struct {
char name[32];
int is_array;
int value;
int array[MAX_ARRAY_SIZE];
int array_size;
} Var;
typedef enum { ADD = 1, SUB = -1, MUL = 2, DIV = 3, MOD = 4 } Operator;
static Var vars[MAX_VARS];
static int var_count = 0;
static Var *get_var(const char *name) {
for (int i = 0; i < var_count; i++)
if (k_strcmp(vars[i].name, name) == 0)
return &vars[i];
return 0;
}
static void set_var(const char *name, int value) {
Var *v = get_var(name);
if (v) {
v->value = value;
return;
}
if (var_count >= MAX_VARS) {
print("Error: too many variables\n", 0);
return;
}
k_strcpy(vars[var_count].name, name);
vars[var_count].value = value;
var_count++;
}
static void set_array(const char *name, int values[], int size) {
Var *v = get_var(name);
if (!v) {
if (var_count >= MAX_VARS) {
print("Error: too many variables\n", 0);
return;
}
v = &vars[var_count++];
k_strcpy(v->name, name);
}
v->is_array = 1;
v->array_size = size;
for (int i = 0; i < size; i++)
v->array[i] = values[i];
}
static int get_array_element(const char *name, int index) {
Var *v = get_var(name);
if (!v || !v->is_array) {
print("Error: not an array\n", 0);
return 0;
}
if (index < 0 || index >= v->array_size) {
print("Error: index out of bounds\n", 0);
return 0;
}
return v->array[index];
}
static void set_array_element(const char *name, int index, int value) {
Var *v = get_var(name);
if (!v || !v->is_array) {
print("Error: not an array\n", 0);
return;
}
if (index < 0 || index >= v->array_size) {
print("Error: index out of bounds\n", 0);
return;
}
v->array[index] = value;
}
// ---- tokenizer ----
// pool must be char[MAX][64], val_pool variant is char[MAX][16]
static int tokenize_into(char *line, char *tokens[], char pool[][LINE_LEN]) {
int count = 0;
char *p = line;
while (*p && count < MAX_TOKENS - 1) {
if (k_isspace(*p)) {
p++;
continue;
}
// two-char operators
if ((p[0] == '=' && p[1] == '=') || (p[0] == '!' && p[1] == '=') ||
(p[0] == '>' && p[1] == '=') || (p[0] == '<' && p[1] == '=')) {
pool_set(pool, LINE_LEN, count, p, 2);
tokens[count] = pool[count];
count++;
p += 2;
continue;
}
// single-char operators / punctuation
if (*p == '>' || *p == '<' || *p == ';' || *p == '=' || *p == '+' ||
*p == '-' || *p == '*' || *p == '/' || *p == '%' || *p == ',') {
pool_set(pool, LINE_LEN, count, p, 1);
tokens[count] = pool[count];
count++;
p++;
continue;
}
// [ index ]
if (*p == '[') {
pool_set(pool, LINE_LEN, count, "[", 1);
tokens[count] = pool[count];
count++;
p++;
char *start = p;
while (*p && *p != ']')
p++;
pool_set(pool, LINE_LEN, count, start, p - start);
tokens[count] = pool[count];
count++;
if (*p == ']') {
pool_set(pool, LINE_LEN, count, "]", 1);
tokens[count] = pool[count];
count++;
p++;
}
continue;
}
// "string"
if (*p == '"') {
p++;
char *start = p;
while (*p && *p != '"')
p++;
pool_set(pool, LINE_LEN, count, start, p - start);
tokens[count] = pool[count];
count++;
if (*p == '"')
p++;
continue;
}
// { body }
if (*p == '{') {
pool_set(pool, LINE_LEN, count, "{", 1);
tokens[count] = pool[count];
count++;
p++;
char *start = p;
int depth = 1;
while (*p && depth > 0) {
if (*p == '{')
depth++;
else if (*p == '}')
depth--;
if (depth > 0)
p++;
}
pool_set(pool, LINE_LEN, count, start, p - start);
tokens[count] = pool[count];
count++;
pool_set(pool, LINE_LEN, count, "}", 1);
tokens[count] = pool[count];
count++;
if (*p == '}')
p++;
continue;
}
// word / number
{
char *start = p;
while (*p && !k_isspace(*p) && *p != '<' && *p != '>' && *p != '!' &&
*p != ',' && *p != '[' && *p != ']' && *p != '{' && *p != '}' &&
*p != ';' && *p != '=' && *p != '*' && *p != '/' && *p != '%' &&
*p != '+' && *p != '-')
p++;
if (p == start) {
p++;
continue;
} // skip unknown char
pool_set(pool, LINE_LEN, count, start, p - start);
tokens[count] = pool[count];
count++;
}
}
return count;
}
static int tokenize(char *line, char *tokens[]) {
return tokenize_into(line, tokens, tok_pools[pool_depth]);
}
// ---- forward decl ----
void handle_line(char *line);
static int is_number(const char *s) {
if (!*s)
return 0;
while (*s) {
if (!k_isdigit(*s))
return 0;
s++;
}
return 1;
}
static int eval_expr(char *tokens[], int count) {
int result = 0, sign = ADD;
for (int i = 0; i < count; i++) {
char *t = tokens[i];
if (k_strcmp(t, "+") == 0) {
sign = ADD;
continue;
}
if (k_strcmp(t, "-") == 0) {
sign = SUB;
continue;
}
if (k_strcmp(t, "*") == 0) {
sign = MUL;
continue;
}
if (k_strcmp(t, "/") == 0) {
sign = DIV;
continue;
}
if (k_strcmp(t, "%") == 0) {
sign = MOD;
continue;
}
int val = is_number(t) ? k_atoi(t) : (get_var(t) ? get_var(t)->value : 0);
if (sign == ADD)
result += val;
else if (sign == SUB)
result -= val;
else if (sign == MUL)
result *= val;
else if (sign == DIV) {
if (!val) {
print("Error: div by zero\n", 0);
return 0;
}
result /= val;
} else if (sign == MOD) {
if (!val) {
print("Error: div by zero\n", 0);
return 0;
}
result %= val;
}
}
return result;
}
static int eval_cond(int l, const char *op, int r) {
if (k_strcmp(op, "==") == 0)
return l == r;
if (k_strcmp(op, "!=") == 0)
return l != r;
if (k_strcmp(op, ">") == 0)
return l > r;
if (k_strcmp(op, "<") == 0)
return l < r;
if (k_strcmp(op, ">=") == 0)
return l >= r;
if (k_strcmp(op, "<=") == 0)
return l <= r;
return 0;
}
void execute(char *tokens[], int count) {
if (count == 0)
return;
// x = arr [ i ]
if (count >= 6 && k_strcmp(tokens[1], "=") == 0 &&
k_strcmp(tokens[3], "[") == 0 && k_strcmp(tokens[5], "]") == 0) {
int idx = is_number(tokens[4])
? k_atoi(tokens[4])
: (get_var(tokens[4]) ? get_var(tokens[4])->value : 0);
set_var(tokens[0], get_array_element(tokens[2], idx));
return;
}
// arr [ i ] = val (no { })
if (count >= 6 && k_strcmp(tokens[1], "[") == 0 &&
k_strcmp(tokens[3], "]") == 0 && k_strcmp(tokens[4], "=") == 0 &&
k_strcmp(tokens[5], "{") != 0) {
int idx = is_number(tokens[2])
? k_atoi(tokens[2])
: (get_var(tokens[2]) ? get_var(tokens[2])->value : 0);
int val = is_number(tokens[5])
? k_atoi(tokens[5])
: (get_var(tokens[5]) ? get_var(tokens[5])->value : 0);
set_array_element(tokens[0], idx, val);
return;
}
// arr [ size ] = { 1, 2, 3 }
if (count >= 8 && k_strcmp(tokens[1], "[") == 0 &&
k_strcmp(tokens[3], "]") == 0 && k_strcmp(tokens[4], "=") == 0 &&
k_strcmp(tokens[5], "{") == 0) {
// save what we need before inner tokenize overwrites tok_pool
char arr_name[32];
k_strcpy(arr_name, tokens[0]);
int array_size = is_number(tokens[2])
? k_atoi(tokens[2])
: (get_var(tokens[2]) ? get_var(tokens[2])->value : 0);
char body[128];
k_strcpy(body, tokens[6]);
// tokenize body into val_pool (separate pool, won't collide)
char *vt[MAX_ARRAY_SIZE];
int vc = tokenize_into(body, vt, val_pool);
int values[MAX_ARRAY_SIZE] = {0}, size = 0;
for (int i = 0; i < vc; i++)
if (k_strcmp(vt[i], ",") != 0)
values[size++] = k_atoi(vt[i]);
if (size > array_size) {
print("Error: too many initializers\n", 0);
return;
}
set_array(arr_name, values, array_size);
return;
}
// echo var / echo str
if (count == 2 && k_strcmp(tokens[0], "echo") == 0) {
Var *v = get_var(tokens[1]);
if (v) {
print_int(v->value);
print("\n", 0);
} else {
print(tokens[1], 0);
print("\n", 0);
}
return;
}
// echo arr [ i ]
if (count >= 5 && k_strcmp(tokens[0], "echo") == 0 &&
k_strcmp(tokens[2], "[") == 0 && k_strcmp(tokens[4], "]") == 0) {
int idx = is_number(tokens[3])
? k_atoi(tokens[3])
: (get_var(tokens[3]) ? get_var(tokens[3])->value : 0);
print_int(get_array_element(tokens[1], idx));
print("\n", 0);
return;
}
// while cond { body }
// 必须先把 tokens 内容复制到本地 —— handle_line 会覆盖全局 tok_pool,
// 导致第二次循环时 tokens[1/2/3/5] 全被污染
if (count >= 6 && k_strcmp(tokens[0], "while") == 0) {
char lhs[64], op[4], rhs[64], body[LINE_LEN];
k_strcpy(lhs, tokens[1]);
k_strcpy(op, tokens[2]);
k_strcpy(rhs, tokens[3]);
k_strcpy(body, tokens[5]);
char *lp[1] = {lhs}, *rp[1] = {rhs};
int limit = 100000;
while (limit-- > 0) {
if (!eval_cond(eval_expr(lp, 1), op, eval_expr(rp, 1)))
break;
handle_line(body);
}
if (limit <= 0)
print("Error: loop limit reached\n", 0);
return;
}
// if cond { body }
if (count >= 6 && k_strcmp(tokens[0], "if") == 0) {
char lhs[64], op[4], rhs[64], body[LINE_LEN];
k_strcpy(lhs, tokens[1]);
k_strcpy(op, tokens[2]);
k_strcpy(rhs, tokens[3]);
k_strcpy(body, tokens[5]);
char *lp[1] = {lhs}, *rp[1] = {rhs};
if (eval_cond(eval_expr(lp, 1), op, eval_expr(rp, 1)))
handle_line(body);
return;
}
// x = expr (arithmetic)
if (count >= 3 && k_strcmp(tokens[1], "=") == 0) {
set_var(tokens[0], eval_expr(&tokens[2], count - 2));
return;
}
// unknown command
if (count >= 1) {
print("Unknown: ", 0);
print(tokens[0], 0);
print("\n", 0);
}
}
void handle_line(char *line) {
if (pool_depth >= MAX_DEPTH) {
print("Error: nesting too deep\n", 0);
return;
}
char *tokens[MAX_TOKENS];
int depth = pool_depth++; // 占用当前层,深度递增
int count = tokenize_into(line, tokens, tok_pools[depth]);
if (count == 0) {
pool_depth--;
return;
}
if (k_strcmp(tokens[0], "if") == 0 || k_strcmp(tokens[0], "while") == 0) {
int end = count;
for (int i = 0; i < count; i++)
if (k_strcmp(tokens[i], "}") == 0) {
end = i + 1;
break;
}
execute(tokens, end);
// handle trailing ; statements
if (end < count && k_strcmp(tokens[end], ";") == 0) {
int start = ++end;
for (int i = end; i < count; i++) {
if (k_strcmp(tokens[i], ";") == 0) {
execute(&tokens[start], i - start);
start = i + 1;
}
}
}
pool_depth--;
return;
}
int start = 0;
for (int i = 0; i < count; i++) {
if (k_strcmp(tokens[i], ";") == 0) {
execute(&tokens[start], i - start);
start = i + 1;
} else if (i == count - 1) {
print("Error: missing ';'\n", 0);
pool_depth--;
return;
}
}
pool_depth--;
}
void minishell_run(void) {
char line[LINE_LEN];
print("mini-shell (type 'exit' to quit)\n", 0x0B);
while (1) {
print("mini-sh> ", 0x0A);
readline(line, LINE_LEN);
if (k_strcmp(line, "exit") == 0)
break;
if (line[0] == '\0')
continue;
handle_line(line);
}
print("Bye\n", 0);
}