Repository navigation
Expand file tree
/
Copy pathshell.c
More file actions
246 lines (223 loc) · 6.59 KB
/
Copy pathshell.c
File metadata and controls
246 lines (223 loc) · 6.59 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
#include "shell.h"
#include "isr.h"
#include "tfs.h"
#include "proc.h"
#include "auth.h"
#include <stddef.h>
/* --------------- write ---------------- */
void write_int(uint32_t n)
{
char buf[12];
int i = 0;
if (n == 0) { cursor_write("0"); return; }
while (n) { buf[i++] = '0' + (n % 10); n /= 10; }
while (i--) {
char s[2] = { buf[i], 0 };
cursor_write(s);
}
}
/* ---------- tiny str helpers ---------- */
static int strcmp(const char *a, const char *b)
{
while (*a && *b && *a == *b) { a++; b++; }
return *a - *b;
}
static int strlen(const char *s)
{
int n = 0;
while (*s++) n++;
return n;
}
/* ---------- argument parser ---------- */
static int parse_args(char *input, const char *argv[], int max)
{
int argc = 0;
while (*input) {
while (*input == ' ') input++;
if (!*input) break;
if (argc >= max) break;
argv[argc++] = input;
while (*input && *input != ' ') input++;
if(*input)
*input++ = '\0';
}
return argc;
}
/* ---------- command declarations ---------- */
static void cmd_ls (int argc, const char *argv[]);
static void cmd_run (int argc, const char *argv[]);
static void cmd_cat(int argc, const char *argv[]);
static void cmd_touch (int argc, const char *argv[]);
static void cmd_rm (int argc, const char *argv[]);
static void cmd_clear(int argc, const char *argv[]);
static void cmd_info(int argc, const char *argv[]);
static void cmd_help(int argc, const char *argv[]);
static void cmd_login(int argc, const char *argv[]);
static void cmd_logout(int argc, const char *argv[]);
static void cmd_whoami(int argc, const char *argv[]);
static void cmd_passwd(int argc, const char *argv[]);
static void cmd_useradd(int argc, const char *argv[]);
static void cmd_crash(int argc, const char *argv[]);
/* ---------- command table ---------- */
typedef struct {
const char *name;
void (*func)(int argc, const char *argv[]);
const char *help;
} cmd_t;
static const cmd_t cmd_table[] = {
{"ls", cmd_ls, "list files"},
{"run", cmd_run, "run <file>"},
{"cat", cmd_cat, "cat <file>"},
{"touch", cmd_touch, "touch <file> <data>"},
{"rm", cmd_rm, "rm <file>"},
{"clear", cmd_clear, "clear screen"},
{"info", cmd_info, "show cpu regs"},
{"help", cmd_help, "show this"},
{"login", cmd_login, "login <user> <pass>"},
{"logout", cmd_logout, "logout"},
{"whoami", cmd_whoami, "whoami"},
{"passwd", cmd_passwd, "change my password"},
{"useradd",cmd_useradd,"useradd <user> <pass>"},
{"crash", cmd_crash, "trigger exception 0"},
{0, 0, 0}
};
/* ---------- shell entry ---------- */
void shell_run(char *input)
{
if (!input || !*input) return;
const char *argv[16];
int argc = parse_args(input, argv, 16);
if (argc == 0) return;
for (int i = 0; cmd_table[i].name; i++) {
if (strcmp(argv[0], cmd_table[i].name) == 0) {
cmd_table[i].func(argc, argv);
return;
}
}
cursor_write("? ");
cursor_write(argv[0]);
cursor_write("\n");
}
/* ---------- commands ---------- */
static void cmd_ls(int argc, const char *argv[])
{
(void)argc; (void)argv;
tfs_list();
}
static void cmd_run(int argc, const char *argv[])
{
if (argc < 2) {
cursor_write("run <file>\n");
return;
}
int ret = proc_load(argv[1]);
if (ret < 0) {
cursor_write("file not found: ");
cursor_write(argv[1]);
cursor_write("\n");
}
}
static void cmd_cat(int argc, const char *argv[])
{
if (argc < 2) {
cursor_write("cat <file>\n");
return;
}
char buf[256];
int len = tfs_read(argv[1], buf, sizeof(buf) - 1);
if (len < 0) {
cursor_write("file not found: ");
cursor_write(argv[1]);
cursor_write("\n");
return;
}
buf[len] = 0;
cursor_write(buf);
cursor_write("\n");
}
static void cmd_touch(int argc, const char *argv[])
{
if (argc < 3) {
cursor_write("touch <file> <data>\n");
return;
}
int ret = tfs_create(argv[1], argv[2], strlen(argv[2]));
if (ret < 0)
cursor_write("create failed (maybe full)\n");
else
cursor_write("ok\n");
}
static void cmd_rm(int argc, const char *argv[])
{
if (argc < 2) {
cursor_write("rm <file>\n");
return;
}
int ret = tfs_delete(argv[1]);
if (ret < 0)
cursor_write("delete failed\n");
else
cursor_write("ok\n");
}
static void cmd_clear(int argc, const char *argv[])
{
(void)argc; (void)argv;
cursor_clear();
cursor_write("TokiOS> ");
}
static void cmd_info(int argc, const char *argv[])
{
(void)argc; (void)argv;
uint32_t cr0, cr2, cr3;
__asm__ volatile("mov %%cr0, %0" : "=r"(cr0));
__asm__ volatile("mov %%cr2, %0" : "=r"(cr2));
__asm__ volatile("mov %%cr3, %0" : "=r"(cr3));
cursor_write("CR0: "); write_int(cr0); cursor_write("\n");
cursor_write("CR2: "); write_int(cr2); cursor_write("\n");
cursor_write("CR3: "); write_int(cr3); cursor_write("\n");
}
static void cmd_help(int argc, const char *argv[])
{
(void)argc; (void)argv;
for (int i = 0; cmd_table[i].name; i++) {
cursor_write(" ");
cursor_write(cmd_table[i].name);
for (int j = strlen(cmd_table[i].name); j < 10; j++)
cursor_write(" ");
cursor_write(cmd_table[i].help);
cursor_write("\n");
}
}
static void cmd_login(int argc, const char *argv[]) {
if (argc < 3) { cursor_write("login <user> <pass>\n"); return; }
if (auth_login(argv[1], argv[2])) {
cursor_write("login ok ("); cursor_write(argv[1]); cursor_write(")\n");
}
}
static void cmd_logout(int argc, const char *argv[]) {
(void)argc; (void)argv;
auth_logout();
cursor_write("logged out\n");
}
static void cmd_whoami(int argc, const char *argv[]) {
(void)argc; (void)argv;
const char *u = auth_whoami();
cursor_write(u[0] ? u : "(nobody)");
cursor_write("\n");
}
static void cmd_passwd(int argc, const char *argv[]) {
if (argc < 2) { cursor_write("passwd <new_password>\n"); return; }
if (auth_passwd(argv[1]) < 0) return; /* error already printed */
cursor_write("password changed\n");
}
static void cmd_useradd(int argc, const char *argv[]) {
if (argc < 3) { cursor_write("useradd <user> <pass>\n"); return; }
if (auth_adduser(argv[1], argv[2]) < 0) return;
cursor_write("user "); cursor_write(argv[1]); cursor_write(" added\n");
}
static void cmd_crash(int argc, const char *argv[])
{
(void)argc;(void)argv;
cursor_write("triggering #DE...\n");
__asm__ volatile("int $0");
}