-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
503 lines (433 loc) · 16.8 KB
/
shell.c
File metadata and controls
503 lines (433 loc) · 16.8 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
#include "shell.h"
#include "terminal.h"
#include "keyboard.h"
#include "kprintf.h"
#include "timer.h"
#include <stddef.h>
#include <stdint.h>
#define LINE_MAX 256
#define ARGS_MAX 16
#define HIST_MAX 16
static char line[LINE_MAX];
static int line_len = 0;
static uint8_t user_color;
/* ── command history ────────────────────────────────────────────────── */
static char history[HIST_MAX][LINE_MAX];
static int hist_count = 0; /* total entries stored */
static int hist_write = 0; /* next slot to write */
static int hist_browse = -1; /* current browse index, -1 = none */
static void hist_push(const char* cmd) {
if (cmd[0] == '\0') return;
/* don't duplicate the last entry */
if (hist_count > 0) {
int prev = (hist_write + HIST_MAX - 1) % HIST_MAX;
int same = 1;
for (int i = 0; cmd[i] || history[prev][i]; i++) {
if (cmd[i] != history[prev][i]) { same = 0; break; }
}
if (same) return;
}
int i = 0;
while (cmd[i] && i < LINE_MAX - 1) { history[hist_write][i] = cmd[i]; i++; }
history[hist_write][i] = '\0';
hist_write = (hist_write + 1) % HIST_MAX;
if (hist_count < HIST_MAX) hist_count++;
}
static const char* hist_get(int index) {
/* index 0 = most recent, 1 = one before, etc. */
if (index < 0 || index >= hist_count) return NULL;
int slot = (hist_write - 1 - index + HIST_MAX) % HIST_MAX;
return history[slot];
}
/* ── minimal string helpers ─────────────────────────────────────────── */
static int kstrcmp(const char* a, const char* b) {
while (*a && *a == *b) { a++; b++; }
return (unsigned char)*a - (unsigned char)*b;
}
static int kstrncmp(const char* a, const char* b, int n) {
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) return (unsigned char)a[i] - (unsigned char)b[i];
if (a[i] == '\0') return 0;
}
return 0;
}
static int kstrlen(const char* s) {
int n = 0;
while (s[n]) n++;
return n;
}
static void kstrcpy(char* dst, const char* src) {
while ((*dst++ = *src++));
}
/* ── argument parser ────────────────────────────────────────────────── */
static int parse_args(char* buf, char* argv[], int max) {
int argc = 0;
char* p = buf;
while (*p && argc < max) {
while (*p == ' ') p++;
if (!*p) break;
argv[argc++] = p;
while (*p && *p != ' ') p++;
if (*p) *p++ = '\0';
}
return argc;
}
/* ── hex parser helper ──────────────────────────────────────────────── */
static int parse_hex(const char* s, uint32_t* out) {
uint32_t val = 0;
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) s += 2;
if (*s == '\0') return 0;
while (*s) {
char c = *s++;
uint32_t d;
if (c >= '0' && c <= '9') d = c - '0';
else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
else return 0;
val = (val << 4) | d;
}
*out = val;
return 1;
}
static int parse_uint(const char* s, uint32_t* out) {
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
return parse_hex(s, out);
uint32_t val = 0;
if (*s == '\0') return 0;
while (*s) {
if (*s < '0' || *s > '9') return 0;
val = val * 10 + (*s - '0');
s++;
}
*out = val;
return 1;
}
/* ── command forward declarations ───────────────────────────────────── */
typedef void (*cmd_fn)(int, char**);
typedef struct {
const char* name;
cmd_fn fn;
const char* desc;
} command_t;
/* Forward declaration so commands can reference the table */
static const command_t commands[];
/* ── command handlers ───────────────────────────────────────────────── */
static void cmd_help(int argc, char* argv[]) {
(void)argc; (void)argv;
terminal_setcolor(terminal_make_color(COLOR_LIGHT_CYAN, COLOR_BLACK));
terminal_writestring("Available commands:\n");
terminal_setcolor(terminal_make_color(COLOR_LIGHT_GREY, COLOR_BLACK));
for (int i = 0; commands[i].name != NULL; i++) {
kprintf(" %-12s %s\n", commands[i].name, commands[i].desc);
}
terminal_setcolor(terminal_make_color(COLOR_DARK_GREY, COLOR_BLACK));
terminal_writestring("Shortcuts: Up/Down = history, Tab = complete\n");
}
static void cmd_clear(int argc, char* argv[]) {
(void)argc; (void)argv;
terminal_init();
user_color = terminal_make_color(COLOR_WHITE, COLOR_BLACK);
}
static void cmd_echo(int argc, char* argv[]) {
for (int i = 1; i < argc; i++) {
terminal_writestring(argv[i]);
if (i < argc - 1) terminal_putchar(' ');
}
terminal_putchar('\n');
}
static void cmd_color(int argc, char* argv[]) {
if (argc < 2) {
terminal_writestring("Usage: color <name>\n");
terminal_writestring("Names: red green cyan blue white grey yellow magenta\n");
return;
}
struct { const char* name; vga_color fg; } colors[] = {
{ "red", COLOR_LIGHT_RED },
{ "green", COLOR_LIGHT_GREEN },
{ "cyan", COLOR_LIGHT_CYAN },
{ "blue", COLOR_LIGHT_BLUE },
{ "white", COLOR_WHITE },
{ "grey", COLOR_LIGHT_GREY },
{ "yellow", COLOR_LIGHT_BROWN },
{ "magenta", COLOR_LIGHT_MAGENTA },
{ NULL, 0 }
};
for (int i = 0; colors[i].name; i++) {
if (kstrcmp(argv[1], colors[i].name) == 0) {
user_color = terminal_make_color(colors[i].fg, COLOR_BLACK);
terminal_setcolor(user_color);
return;
}
}
terminal_setcolor(terminal_make_color(COLOR_LIGHT_RED, COLOR_BLACK));
kprintf("Unknown color: %s\n", argv[1]);
terminal_setcolor(terminal_make_color(COLOR_LIGHT_GREY, COLOR_BLACK));
}
static void cmd_uptime(int argc, char* argv[]) {
(void)argc; (void)argv;
uint32_t secs = timer_get_uptime();
uint32_t hrs = secs / 3600;
uint32_t mins = (secs % 3600) / 60;
uint32_t s = secs % 60;
kprintf("Uptime: %u:%u:%u (%u ticks)\n", hrs, mins, s, timer_get_ticks());
}
static void cmd_sysinfo(int argc, char* argv[]) {
(void)argc; (void)argv;
terminal_setcolor(terminal_make_color(COLOR_LIGHT_CYAN, COLOR_BLACK));
terminal_writestring("clickOS System Info\n");
terminal_setcolor(terminal_make_color(COLOR_LIGHT_GREY, COLOR_BLACK));
kprintf(" Build: %s %s\n", __DATE__, __TIME__);
kprintf(" Compiler: GCC %d.%d.%d\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
kprintf(" VGA: 80x25 text mode @ 0xB8000\n");
kprintf(" Timer: PIT channel 0 at 100 Hz\n");
uint32_t secs = timer_get_uptime();
kprintf(" Uptime: %u seconds\n", secs);
/* Read some CPUID info if available */
uint32_t eax, ebx, ecx, edx;
char vendor[13];
__asm__ volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(0));
*(uint32_t*)&vendor[0] = ebx;
*(uint32_t*)&vendor[4] = edx;
*(uint32_t*)&vendor[8] = ecx;
vendor[12] = '\0';
kprintf(" CPU: %s\n", vendor);
}
static void cmd_peek(int argc, char* argv[]) {
if (argc < 2) {
terminal_writestring("Usage: peek <address> [count]\n");
terminal_writestring(" Reads 32-bit words. Address in hex (0x...).\n");
return;
}
uint32_t addr;
if (!parse_hex(argv[1], &addr)) {
kprintf("Invalid address: %s\n", argv[1]);
return;
}
uint32_t count = 1;
if (argc >= 3) parse_uint(argv[2], &count);
if (count > 64) count = 64;
for (uint32_t i = 0; i < count; i++) {
uint32_t val = *(volatile uint32_t*)(addr + i * 4);
kprintf(" [0x%x] = 0x%x\n", addr + i * 4, val);
}
}
static void cmd_poke(int argc, char* argv[]) {
if (argc < 3) {
terminal_writestring("Usage: poke <address> <value>\n");
terminal_writestring(" Writes a 32-bit word. Both in hex.\n");
return;
}
uint32_t addr, val;
if (!parse_hex(argv[1], &addr)) { kprintf("Invalid address: %s\n", argv[1]); return; }
if (!parse_hex(argv[2], &val)) { kprintf("Invalid value: %s\n", argv[2]); return; }
*(volatile uint32_t*)addr = val;
kprintf(" [0x%x] <- 0x%x\n", addr, val);
}
static void cmd_hexdump(int argc, char* argv[]) {
if (argc < 2) {
terminal_writestring("Usage: hexdump <address> [bytes]\n");
return;
}
uint32_t addr;
if (!parse_hex(argv[1], &addr)) { kprintf("Invalid address: %s\n", argv[1]); return; }
uint32_t len = 128;
if (argc >= 3) parse_uint(argv[2], &len);
if (len > 512) len = 512;
const uint8_t* p = (const uint8_t*)addr;
for (uint32_t off = 0; off < len; off += 16) {
kprintf(" %x: ", addr + off);
/* hex bytes */
for (int i = 0; i < 16; i++) {
if (off + i < len) {
uint8_t b = p[off + i];
kprintf("%x", b >> 4);
kprintf("%x", b & 0xF);
terminal_putchar(' ');
} else {
terminal_writestring(" ");
}
if (i == 7) terminal_putchar(' ');
}
terminal_writestring(" |");
/* ASCII */
for (int i = 0; i < 16 && (off + i) < len; i++) {
uint8_t b = p[off + i];
terminal_putchar((b >= 0x20 && b < 0x7F) ? (char)b : '.');
}
terminal_writestring("|\n");
}
}
static void cmd_history(int argc, char* argv[]) {
(void)argc; (void)argv;
if (hist_count == 0) {
terminal_writestring("(no history)\n");
return;
}
for (int i = hist_count - 1; i >= 0; i--) {
const char* entry = hist_get(i);
if (entry) kprintf(" %d %s\n", hist_count - i, entry);
}
}
static void cmd_halt(int argc, char* argv[]) {
(void)argc; (void)argv;
terminal_setcolor(terminal_make_color(COLOR_LIGHT_RED, COLOR_BLACK));
terminal_writestring("System halted.\n");
__asm__ volatile ("cli; hlt");
}
static void cmd_reboot(int argc, char* argv[]) {
(void)argc; (void)argv;
terminal_setcolor(terminal_make_color(COLOR_LIGHT_RED, COLOR_BLACK));
terminal_writestring("Rebooting...\n");
/* Triple fault: load a zero-length IDT and trigger an interrupt */
struct { uint16_t limit; uint32_t base; } __attribute__((packed)) null_idt = { 0, 0 };
__asm__ volatile ("lidt %0; int $0x03" : : "m"(null_idt));
}
/* ── command table ──────────────────────────────────────────────────── */
static const command_t commands[] = {
{ "help", cmd_help, "show this message" },
{ "clear", cmd_clear, "clear the screen" },
{ "echo", cmd_echo, "print text to screen" },
{ "color", cmd_color, "set text color (red green ...)" },
{ "uptime", cmd_uptime, "show time since boot" },
{ "sysinfo", cmd_sysinfo, "display system information" },
{ "peek", cmd_peek, "read memory (peek addr [n])" },
{ "poke", cmd_poke, "write memory (poke addr val)" },
{ "hexdump", cmd_hexdump, "hex dump memory region" },
{ "history", cmd_history, "show command history" },
{ "halt", cmd_halt, "halt the CPU" },
{ "reboot", cmd_reboot, "reboot the machine" },
{ NULL, NULL, NULL }
};
/* ── tab completion ─────────────────────────────────────────────────── */
static void shell_erase_line(int prompt_len) {
/* Erase current line from screen: backspace over every char */
for (int i = 0; i < line_len; i++)
terminal_putchar('\b');
/* We can't truly erase the prompt easily, just redraw input */
(void)prompt_len;
}
static void shell_redraw_line(void) {
for (int i = 0; i < line_len; i++)
terminal_putchar(line[i]);
}
static void shell_tab_complete(void) {
if (line_len == 0) return;
/* Only complete the first token (command name) */
const char* match = NULL;
int match_count = 0;
for (int i = 0; commands[i].name != NULL; i++) {
if (kstrncmp(line, commands[i].name, line_len) == 0) {
match = commands[i].name;
match_count++;
}
}
if (match_count == 1) {
/* Unique match — fill it in */
shell_erase_line(2);
int mlen = kstrlen(match);
for (int i = 0; i < mlen && i < LINE_MAX - 2; i++)
line[i] = match[i];
line[mlen] = ' ';
line_len = mlen + 1;
shell_redraw_line();
} else if (match_count > 1) {
/* Multiple matches — show them */
terminal_putchar('\n');
terminal_setcolor(terminal_make_color(COLOR_DARK_GREY, COLOR_BLACK));
for (int i = 0; commands[i].name != NULL; i++) {
if (kstrncmp(line, commands[i].name, line_len) == 0) {
terminal_writestring(commands[i].name);
terminal_writestring(" ");
}
}
terminal_putchar('\n');
terminal_setcolor(terminal_make_color(COLOR_LIGHT_GREEN, COLOR_BLACK));
terminal_writestring("> ");
terminal_setcolor(user_color);
shell_redraw_line();
}
}
/* ── execute ────────────────────────────────────────────────────────── */
static void shell_execute(char* input) {
if (input[0] == '\0') return;
char buf[LINE_MAX];
int i = 0;
while (input[i] != '\0' && i < LINE_MAX - 1) { buf[i] = input[i]; i++; }
buf[i] = '\0';
char* argv[ARGS_MAX];
int argc = parse_args(buf, argv, ARGS_MAX);
if (argc == 0) return;
for (int j = 0; commands[j].name != NULL; j++) {
if (kstrcmp(argv[0], commands[j].name) == 0) {
commands[j].fn(argc, argv);
return;
}
}
terminal_setcolor(terminal_make_color(COLOR_LIGHT_RED, COLOR_BLACK));
terminal_writestring("Unknown command: ");
terminal_writestring(argv[0]);
terminal_writestring(" (try 'help')\n");
terminal_setcolor(terminal_make_color(COLOR_LIGHT_GREY, COLOR_BLACK));
}
/* ── prompt & main loop ─────────────────────────────────────────────── */
static void shell_prompt(void) {
terminal_setcolor(terminal_make_color(COLOR_LIGHT_GREEN, COLOR_BLACK));
terminal_writestring("\nclickOS> ");
terminal_setcolor(user_color);
}
void shell_run(void) {
user_color = terminal_make_color(COLOR_WHITE, COLOR_BLACK);
shell_prompt();
while (1) {
char c = keyboard_getchar();
if (c == 0) {
__asm__ volatile ("hlt");
continue;
}
if (c == '\n') {
line[line_len] = '\0';
hist_push(line);
hist_browse = -1;
shell_execute(line);
line_len = 0;
shell_prompt();
} else if (c == '\b') {
if (line_len > 0) line_len--;
} else if (c == '\t') {
shell_tab_complete();
} else if ((unsigned char)c == KEY_UP) {
/* Browse history backward (older) */
int next = hist_browse + 1;
const char* entry = hist_get(next);
if (entry) {
hist_browse = next;
/* Erase current line on screen */
shell_erase_line(2);
/* Copy history entry into line buffer */
kstrcpy(line, entry);
line_len = kstrlen(line);
shell_redraw_line();
}
} else if ((unsigned char)c == KEY_DOWN) {
/* Browse history forward (newer) */
if (hist_browse > 0) {
hist_browse--;
const char* entry = hist_get(hist_browse);
shell_erase_line(2);
if (entry) {
kstrcpy(line, entry);
line_len = kstrlen(line);
} else {
line_len = 0;
}
shell_redraw_line();
} else if (hist_browse == 0) {
hist_browse = -1;
shell_erase_line(2);
line_len = 0;
}
} else if (line_len < LINE_MAX - 1) {
line[line_len++] = c;
}
}
}