-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
243 lines (208 loc) · 7.22 KB
/
Copy pathshell.c
File metadata and controls
243 lines (208 loc) · 7.22 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
// This module implements a very simple command-line shell that
// runs on top of the UART serial interface. It allows interactive
// input/output through QEMU’s terminal window and supports basic
// commands for exploring the in-memory filesystem and launching
// demo tasks.
//
// ---------------------------------------------------------------
// Built-in commands:
// help - Display this help message
// ls - List all files in the filesystem
// cat <file> - Display contents of a specific file
// tasks - List registered demo tasks
// run <task> - Execute a named task
// load <file> - Loads a seperate file
// whoami - Display current user
// su - Switch to superuser (password: riscv)
// clear - Clear the screen
// !! - Repeat the last command
// ---------------------------------------------------------------
//
// added features:
// • fake user system (user/root)
// • persistent command history
// • clear screen using ANSI escape codes
// • expanded help and comments for clarity
#include "uart.h"
#include "fs.h"
#include "tasks.h"
#include "shell.h"
#include "loader.h"
#define CMD_BUF_SIZE 64
// Local string helper utilities
// compare two strings for equality (returns 1 if equal, 0 otherwise)
static int str_eq(const char *a, const char *b) {
while (*a && *b) {
if (*a != *b) return 0;
a++;
b++;
}
return (*a == '\0' && *b == '\0');
}
// check if a string starts with a given prefix
static int starts_with(const char *s, const char *prefix) {
while (*prefix) {
if (*s != *prefix) return 0;
s++;
prefix++;
}
return 1;
}
// calculate string length
static int str_len(const char *s) {
int n = 0;
while (*s++) n++;
return n;
}
// Simple fake user management system
static const char *current_user = "user"; // default account
static int is_root = 0; // 0 = normal, 1 = superuser
// prints the current user identity
static void shell_whoami(void) {
uart_puts("Current user: ");
uart_puts(current_user);
uart_puts(is_root ? " (root)\n" : "\n");
}
// prompts for password and enables superuser mode if correct
static void shell_su(void) {
char buf[CMD_BUF_SIZE];
uart_puts("Password: ");
int i = 0;
while (i < CMD_BUF_SIZE - 1) {
char c = uart_getc();
if (c == '\r' || c == '\n') {
uart_putc('\n');
break;
} else if ((c == '\b' || c == 127) && i > 0) {
i--;
uart_puts("\b \b");
} else {
uart_putc('*'); // mask input
buf[i++] = c;
}
}
buf[i] = '\0';
// check for the hardcoded password
if (str_eq(buf, "riscv")) {
is_root = 1;
current_user = "root";
uart_puts("Superuser mode enabled.\n");
} else {
uart_puts("Authentication failed.\n");
}
}
static void cmd_quit(void) {
uart_puts("Exiting shell + halting CPU. Use host to kill QEMU if needed.\n");
for (;;) { asm volatile("wfi"); }
}
// try and load program from file system and get a pcb, return 0 if fail
static int try_run_file(const char *name) {
if (!name || name[0] == '\0') return -1;
uart_puts("Attempting to load file: ");
uart_puts(name);
uart_puts("\n");
pcb_t pcb;
int r = load_program_from_fs(name, &pcb);
if (r != 0) {
uart_puts("loader: failed to load file or not an ELF.\n");
return -1;
}
if (tasks_add_pcb(&pcb) < 0) {
uart_puts("tasks: out of slots\n");
return -1;
}
tasks_start_program(&pcb);
return 0;
}
static void cmd_load(const char *arg) {
if (!arg || arg[0] == '\0') {
uart_puts("usage: load <file>\n");
return;
}
while (*arg == ' ' || *arg == '\t') arg++;
if (try_run_file(arg) != 0) {
uart_puts("load failed: no such ELF or loader error.\n");
}
}
// Help menu listing all supported commands
static void shell_help(void) {
uart_puts("Available commands:\n");
uart_puts(" help - Show this help message\n");
uart_puts(" ls - List files\n");
uart_puts(" cat <file> - Display file contents\n");
uart_puts(" tasks - List available tasks\n");
uart_puts(" run <task> - Run a demo task\n");
uart_puts(" load <file> - Load and start an ELF program from the file system\n");
uart_puts(" quit - Exit the shell (halts the kernel)\n");
uart_puts(" whoami - Show current user (user/root)\n");
uart_puts(" su - Become superuser (password: riscv)\n");
uart_puts(" clear - Clear the screen\n");
uart_puts(" !! - Repeat the last command\n");
}
// Shell main loop
void shell_run(void) {
char cmd[CMD_BUF_SIZE];
static char last_cmd[CMD_BUF_SIZE] = {0};
uart_puts("> ");
while (1) {
int i = 0;
// read a line into cmd[]
while (i < CMD_BUF_SIZE - 1) {
char c = uart_getc();
if (c == '\r' || c == '\n') {
uart_putc('\n');
cmd[i] = '\0';
break;
} else if ((c == '\b' || c == 127) && i > 0) {
i--;
uart_puts("\b \b");
} else {
uart_putc(c);
cmd[i++] = c;
}
}
cmd[i] = '\0';
// handle command input
if (str_eq(cmd, "help")) {
shell_help();
} else if (str_eq(cmd, "ls")) {
fs_list();
} else if (starts_with(cmd, "cat ")) {
fs_cat(cmd + 4);
} else if (str_eq(cmd, "tasks")) {
tasks_list();
} else if (starts_with(cmd, "run ")) {
tasks_run(cmd + 4);
} else if (str_eq(cmd, "whoami")) {
shell_whoami();
} else if (str_eq(cmd, "su")) {
shell_su();
} else if (str_eq(cmd, "quit") || str_eq(cmd, "exit")) {
cmd_quit();
} else if (starts_with(cmd, "load ")) {
cmd_load(cmd);
} else if (str_eq(cmd, "clear")) {
uart_puts("\033[2J\033[H"); // ANSI escape: clear screen + move cursor home
} else if (str_eq(cmd, "!!")) {
if (str_len(last_cmd) > 0) {
uart_puts("Repeating last command: ");
uart_puts(last_cmd);
uart_puts("\n");
// re-run the previous command by copying it into cmd[]
for (int j = 0; j < CMD_BUF_SIZE; j++)
cmd[j] = last_cmd[j];
continue; // loop back with cmd now equal to last command
} else {
uart_puts("No previous command.\n");
}
} else if (str_len(cmd) > 0) {
uart_puts("Unknown command. Type 'help'.\n");
}
// store last command (if non-empty)
if (str_len(cmd) > 0 && !str_eq(cmd, "!!")) {
for (int j = 0; j < CMD_BUF_SIZE; j++)
last_cmd[j] = cmd[j];
}
uart_puts("> ");
}
}