-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
91 lines (75 loc) · 1.7 KB
/
shell.c
File metadata and controls
91 lines (75 loc) · 1.7 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
#include "screen.h"
#include "keyboard.h"
#include "string.h"
#include "shell.h"
#define SHELL_INPUT_MAX 128
static char input_buffer[SHELL_INPUT_MAX];
static void shell_print_prompt() {
puts("mochi> ");
}
static void shell_read_line() {
int i = 0;
while (i < SHELL_INPUT_MAX - 1) {
char c = get_key();
if (!c) continue;
// handle backspace
if (c == '\b') {
if (i > 0) {
i--;
// move cursor back and erase char visually
putc('\b');
putc(' ');
putc('\b');
}
continue;
}
if (c == '\n') {
putc('\n');
break;
}
putc(c);
input_buffer[i++] = c;
}
input_buffer[i] = 0;
}
static void shell_cmd_help() {
puts("Commands:\n");
puts(" help - show this help\n");
puts(" echo X - print X\n");
puts(" clear - clear screen\n");
}
static void shell_cmd_clear() {
// naive clear: print spaces over whole screen
for (int i = 0; i < 80 * 25; i++) {
putc(' ');
}
}
static void shell_execute(const char *cmd) {
if (cmd[0] == 0)
return;
if (strcmp(cmd, "help") == 0) {
shell_cmd_help();
}
else if (strncmp(cmd, "echo ", 5) == 0) {
puts(cmd + 5);
putc('\n');
}
else if (strcmp(cmd, "clear") == 0) {
shell_cmd_clear();
}
else {
puts("Unknown command: ");
puts(cmd);
putc('\n');
}
}
void shell_init() {
puts("Mochi Shell v0.2\n");
}
void shell_run() {
while (1) {
shell_print_prompt();
shell_read_line();
shell_execute(input_buffer);
}
}