-
Notifications
You must be signed in to change notification settings - Fork 3
/
shell.c
120 lines (113 loc) · 3.08 KB
/
shell.c
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
#include "shell.h"
#include "hexes.h"
#include "fs.h"
#include "keyboard.h"
void shell_list(char *node_name, int isdir) {
int i;
for (i = 0; i < 12 && node_name[i]; i++) {
hexes_print_char(node_name[i]);
}
if (isdir) {
hexes_print_char('/');
}
hexes_print_char(0x0A);
}
void shell_handle_command(char *str) {
char *command, *rest;
command = str;
rest = str;
while (*rest != ' ' && *rest) {
rest++;
}
*rest = '\0';
rest++;
if (str_equal("print", command)) {
uint8_t buffer[512];
fs_read_node(rest, buffer);
int i;
for (i = 0; i < 512 && buffer[i]; i++) {
hexes_print_char(buffer[i]);
}
hexes_print_char(0x0A);
} else if (str_equal("list", command)) {
fs_list_entries(rest, shell_list);
} else if (str_equal("addfile", command)) {
char *first = rest, *second = rest;
while (*second != ' ' && *second) {
second++;
}
*second = '\0';
second++;
fs_add_file(first, second);
} else if (str_equal("adddir", command)) {
char *first = rest, *second = rest;
while (*second != ' ' && *second) {
second++;
}
*second = '\0';
second++;
fs_add_dir(first, second);
} else if (str_equal("del", command)) {
char *first = rest, *second = rest;
while (*second != ' ' && *second) {
second++;
}
*second = '\0';
second++;
fs_del_entry(first, second);
} else if (str_equal("write", command)) {
uint8_t buffer[512];
memset(buffer, 512, 0);
int i = 0;
while (1) {
uint8_t key = keyboard_get_last_key();
if (key) {
if (key == 0xFE) {
break;
} else if (key == 0xFF) {
if (i > 0) {
buffer[i] = '\0';
i--;
hexes_print_backspace();
}
} else {
buffer[i++] = key;
hexes_print_char(key);
}
}
}
fs_write_node(rest, buffer);
hexes_print_char(0x0A);
}
}
void shell_run() {
char buffer[128];
int i;
while (1) {
hexes_print_char('s');
hexes_print_char('h');
hexes_print_char('>');
hexes_print_char(' ');
i = 0;
while (1) {
uint8_t key = keyboard_get_last_key();
if (key) {
if (key == 0x0A) {
hexes_print_char(key);
buffer[i] = 0x0;
break;
} else if (key == 0xFE) {
} else if (key == 0xFF) {
if (i > 0) {
i--;
hexes_print_backspace();
}
} else {
buffer[i++] = key;
hexes_print_char(key);
}
}
}
shell_handle_command(buffer);
}
}