forked from jordanbray/CECS-525-in-C-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
203 lines (180 loc) · 3.53 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
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
/*
* 68K Shell
*
* Features:
* Tab completion
* Easily add functions
* More to come...
*/
#include "shell.h"
struct tnode *root;
void print_command_tree() {
tnode_print(root, 0);
}
struct linked_list *get_commands(char *command) {
struct linked_list *commands = NULL;
tnode_startswith(root, &commands, command);
return commands;
}
void putoptions(struct linked_list *options)
{
if (options == NULL)
{
return;
}
putstr("\n\t");
putstr((char*)options->value);
putoptions(options->next);
kfree(options);
}
int add_next_character(char *command, struct linked_list *commands, int *length) {
while (commands->next) {
if (((char *)commands->value)[*length] != ((char *) commands->next->value)[*length])
return 0; // stop
commands = commands->next;
}
if (((char *)commands->value)[*length] == 0) {
command[*length] = ' ';
command[*length+1] = 0;
*length += 1;
return 0;
}
command[*length] = ((char *)commands->value)[*length];
command[*length+1] = 0;
*length += 1;
return 1; // continue
}
void tab_complete(char *command, int *length) {
struct linked_list *commands = get_commands(command);
if (commands == NULL) return;
while (add_next_character(command, commands, length));
while (commands) {
kfree(commands); // yes, I know this is ugly
commands = commands->next; // but I'm doing it anyways
}
}
void initialize_shell() {
root = NULL;
initialize_commands();
}
void shell(char* curUser) {
char **argv, str[BUF_LEN], ch;
int i, j, argc;
shell_func func;
while(1)
{
i = 0;
putstr(curUser);
putstr("> ");
while (i < BUF_LEN-1)
{
str[i] = 0;
ch = getch();
if (ch == '\r') //if enter key is hit stop
{
break;
}
else if (ch == '\b' || ch == 127)
{
if (i > 0)
{
putch('\b');
i--;
}
}
else if (ch == '\t')
{
if (strchr(str, ' ') == NULL)
{
j = i;
tab_complete(str, &j);
if (i == j)
{
putoptions(get_commands(str));
putch('\n');
putstr(curUser);
putstr("> ");
putstr(str);
}
else
{
for (; i < j; i++)
{
putch(str[i]);
}
}
}
}
else if (IS_PRINTABLE(ch))
{
str[i] = ch;
putch(ch);
i++;
}
}
str[i] = 0;
putch('\n');
if (strcmp(str, "exit") == 0)
{
putstr("Exiting...\n");
break;
}
else
{
argc = i;
argv = parse_parameters(str, &argc);
func = get_cmd(argv[0]);
if (func == NULL)
{
putstr("Invalid Command: ");
putstr(argv[0]);
putch('\n');
}
else
{
func(argc, (const char **)argv);
}
kfree(argv);
}
}
}
void add_cmd(const char *cmd, shell_func func, shell_func help) {
shell_cmd *node = kmalloc(sizeof(shell_cmd));
node->run = func;
node->help = help;
root = tnode_insert(root, cmd, node);
}
shell_func get_cmd(const char *cmd) {
shell_cmd *node;
struct tnode *cmd_node = tnode_search(root, cmd);
if (cmd_node == NULL)
return NULL;
node = cmd_node->value;
return node->run;
}
shell_func get_help(const char *cmd) {
shell_cmd *node;
struct tnode *cmd_node = tnode_search(root, cmd);
if (cmd_node == NULL)
return NULL;
node = cmd_node->value;
return node->help;
}
char **parse_parameters(char *params, int *length) {
int space_count = 0;
int i;
for (i = 0; i < *length; i++)
if (params[i] == ' ')
space_count++;
char **ret = kmalloc(space_count+1);
ret[0] = params;
int index = 1;
for (i = 0; i < *length; i++) {
if (params[i] == ' ') {
params[i] = '\0';
ret[index++] = ¶ms[i+1];
}
}
*length = space_count+1;
return ret;
}