-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathushell.c
347 lines (301 loc) · 9.68 KB
/
ushell.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "stdlib.h"
#include "ushell.h"
#include "ushell_config.h"
typedef struct {
char buf[USHELL_CMD_BUF];
unsigned buf_pos;
ushell_cmd_def_t *user_cmds;
unsigned state;
} ushell_ctx_t;
static ushell_ctx_t ctx;
/*ushell atomic functions*/
int ushell_strlen(char *str) {
unsigned j = 0;
while(str[j] != '\0') {
++j;
}
return j;
}
int ushell_strncmp(const char *s1, const char *s2, size_t n) {
unsigned i;
for( i = 0; i < n; ++i) {
if(s1[i] != s2[i]) {
return 1;
}
}
return 0;
}
int putchar(int c)
{
ushell_putc((char) c);
return 1;
}
/*Basic ushell functions*/
void help_cmd( char* argv[], int argc );
void info_cmd( char* argv[], int argc );
void lscmd_cmd( char* argv[], int argc );
void exit_cmd( char* argv[], int argc );
static ushell_cmd_def_t basic_cmds[] = {
#ifndef USHELL_DISABLE_HELP
{"help", help_cmd},
#endif
{"info", info_cmd},
{"lscmd", lscmd_cmd},
{"exit", exit_cmd},
{NULL, NULL}
};
#ifndef USHELL_DISABLE_HELP
void help_cmd( char* argv[], int argc )
{
(void)(argv);
(void)(argc);
ushell_printf("ushell is small and simple shell for your constrainted"USHELL_NEWLINE_PRINT);
ushell_printf("devices. Be careful. In this application your command is"USHELL_NEWLINE_PRINT);
ushell_printf("limited to %d chars and arguments to %d."USHELL_NEWLINE_PRINT, USHELL_CMD_BUF, USHELL_MAX_ARGS);
ushell_printf("It is simple help:"USHELL_NEWLINE_PRINT);
ushell_printf(" - Type command and args with space key: cmd arg1 arg2"USHELL_NEWLINE_PRINT);
ushell_printf(" - Double pressed tab key can show you commands hint"USHELL_NEWLINE_PRINT);
ushell_printf(" - Type lscmd and press enter to list all command"USHELL_NEWLINE_PRINT);
}
#endif
void info_cmd( char* argv[], int argc )
{
(void)(argv);
(void)(argc);
ushell_printf("ushellell version %d.%d"USHELL_NEWLINE_PRINT, USHELL_VER_MAJOR, USHELL_VER_MINOR);
#ifndef USHELL_SMALL_INFO
ushell_printf("ushell is small and simple shell for constrainted"USHELL_NEWLINE_PRINT);
ushell_printf("microcontrollers and processors. ushell can be used"USHELL_NEWLINE_PRINT);
ushell_printf("with 8, 16, 32 and 64bit architectures. It requieres"USHELL_NEWLINE_PRINT);
ushell_printf("to implement only three functions: putc, getc and printf."USHELL_NEWLINE_PRINT);
ushell_printf(USHELL_NEWLINE_PRINT);
ushell_printf("For license constrainted see LICENSE file."USHELL_NEWLINE_PRINT);
ushell_printf(USHELL_NEWLINE_PRINT);
#endif
ushell_printf("Repository is available at: http://github.com/kl-cruz/ushell"USHELL_NEWLINE_PRINT);
ushell_printf(USHELL_NEWLINE_PRINT);
ushell_printf("Karol {cruz} Lasonczyk 2015"USHELL_NEWLINE_PRINT);
}
void lscmd_cmd( char* argv[], int argc )
{
(void)(argv);
(void)(argc);
unsigned i = 0;
ushell_printf("System commands:"USHELL_NEWLINE_PRINT);
while(basic_cmds[i].cmd != NULL) {
ushell_printf(" %s"USHELL_NEWLINE_PRINT, basic_cmds[i++].cmd_str);
}
i = 0;
ushell_printf("User commands:"USHELL_NEWLINE_PRINT);
if(ctx.user_cmds != NULL) {
while(ctx.user_cmds[i].cmd != NULL) {
ushell_printf(" %s"USHELL_NEWLINE_PRINT, ctx.user_cmds[i++].cmd_str);
}
}
}
void exit_cmd( char* argv[], int argc )
{
ctx.state = 0;
(void)(argv);
(void)(argc);
}
/*ushell main functions*/
unsigned ushell_exec_cmd(char *line, ushell_cmd_def_t *cmds)
{
size_t cmd_str_pos = 0;
unsigned i = 0, cmd_i = 0, args_i = 0, result = 0;
char *args[USHELL_MAX_ARGS];
char c = line[i++];
while(c!=' ' && c!='\0') {
c = line[i++];
cmd_str_pos++;
}
/*ushell is not depend of any library so strcmp is implemented below*/
while( cmds[cmd_i].cmd != NULL) {
char *cmd_s = cmds[cmd_i].cmd_str;
unsigned j = 0;
j = ushell_strlen(cmd_s);
if(cmd_str_pos != j) {
cmd_i++;
continue;
}
i = ushell_strncmp(cmds[cmd_i].cmd_str, line, j);
/*Probably we found desired command*/
if(i != 0) {
cmd_i++;
continue;
}
cmd_str_pos = 0;
if((line[j+1] == ' ') || (line[j+1] == '\0') || (cmds[cmd_i].cmd_str[j] == '\0') ) {
/*Ok that is our function! Let's parse args*/
do {
c = line[j++];
if((c == '\0')) {
break;
}
if((c == ' ')) {
args[args_i++] = line+j;
}
} while(c != '\0' && (args_i < USHELL_MAX_ARGS));
#ifndef USHELL_DISABLE_MAX_ARGS_INFO
if(args_i > USHELL_MAX_ARGS) {
ushell_printf("[ushell WARN] Function has too much arguments! Max %d."USHELL_NEWLINE_PRINT, USHELL_MAX_ARGS);
}
#endif
/*arguments must be separate by zero to be parsable like strings*/
for(i = 0; i < USHELL_CMD_BUF; ++i) {
if(line[i] == ' ') {
line[i] = '\0';
}
}
cmds[cmd_i].cmd(args, args_i);
result = 1;
}
cmd_i++;
}
return result;
}
#ifndef USHELL_DISABLE_HINT
char* ushell_hint(char* line, ushell_cmd_def_t *basic_cmds, ushell_cmd_def_t *user_cmds)
{
size_t cmd_str_pos = 0;
unsigned i = 0, cmd_i = 0, founds = 0;
char *cmd;
char c;
if(line[0] == '\0') {
unsigned i = 0;
while(basic_cmds[i].cmd != NULL) {
ushell_printf("%s"USHELL_NEWLINE_PRINT, basic_cmds[i++].cmd_str);
}
i = 0;
if(ctx.user_cmds != NULL) {
while(ctx.user_cmds[i].cmd != NULL) {
ushell_printf("%s"USHELL_NEWLINE_PRINT, ctx.user_cmds[i++].cmd_str);
}
}
} else {
do {
c = line[i++];
cmd_str_pos++;
} while(c!=' ' && c!='\0');
cmd_str_pos--;
/*ushell is not depend of any library so strncmp is implemented below*/
while( basic_cmds[cmd_i].cmd != NULL) {
for( i = 0; i < cmd_str_pos; ++i) {
if(basic_cmds[cmd_i].cmd_str[i] != line[i]) {
i = 0;
break;
}
}
/*Probably we found desired command*/
if(i != 0) {
founds++;
cmd = basic_cmds[cmd_i].cmd_str;
ushell_printf("%s"USHELL_NEWLINE_PRINT, basic_cmds[cmd_i].cmd_str);
}
cmd_i++;
}
if(user_cmds != NULL){
cmd_i = 0;
while( user_cmds[cmd_i].cmd != NULL) {
for( i = 0; i < cmd_str_pos; ++i) {
if(user_cmds[cmd_i].cmd_str[i] != line[i]) {
i = 0;
break;
}
}
/*Probably we found desired command*/
if(i != 0) {
founds++;
cmd = user_cmds[cmd_i].cmd_str;
ushell_printf("%s"USHELL_NEWLINE_PRINT, user_cmds[cmd_i].cmd_str);
}
cmd_i++;
}
}
}
if(founds == 1) {
return cmd;
}
return line;
}
#endif
void ushell_loop(void)
{
char c;
unsigned i, prediction = 0;
ushell_printf(USHELL_NEWLINE_PRINT"ushell version %d.%d"USHELL_NEWLINE_PRINT, USHELL_VER_MAJOR, USHELL_VER_MINOR);
ushell_putc('>');
ushell_putc(' ');
while(ctx.state > 0) {
c = ushell_getc();
#ifndef USHELL_DISABLE_SPEC_KEYS_CHECK
if((c < 0x20) && (c > 127) && (c != USHELL_NEWLINE)
&& (c != USHELL_PREDICT) && (c != USHELL_DELETE)) {
continue;
}
#endif
#ifndef USHELL_DISABLE_HINT
if(c == USHELL_PREDICT) {
prediction++;
if(prediction == 2) {
ushell_printf(USHELL_NEWLINE_PRINT);
char* cmd_hint = ushell_hint(ctx.buf, basic_cmds, ctx.user_cmds);
prediction = 0;
ushell_putc('>');
ushell_putc(' ');
for(i = 0; i < ushell_strlen(cmd_hint); ++i) {
ctx.buf[i] = cmd_hint[i];
ushell_putc(cmd_hint[i]);
}
ctx.buf_pos = i;
}
continue;
} else {
prediction = 0;
}
#endif
if(c == USHELL_NEWLINE) {
ushell_printf(USHELL_NEWLINE_PRINT);
if(ushell_exec_cmd(ctx.buf, basic_cmds) == 1) {
ushell_printf(USHELL_NEWLINE_PRINT);
} else if(ushell_exec_cmd(ctx.buf, ctx.user_cmds) == 1) {
ushell_printf(USHELL_NEWLINE_PRINT);
} else if(ushell_strlen(ctx.buf) == 0) {
;
} else {
ushell_printf("[ushell ERROR] Command %s not recognized!"USHELL_NEWLINE_PRINT, ctx.buf);
}
for(i = 0; i < ctx.buf_pos; ++i) {
ctx.buf[i] = 0;
}
ctx.buf_pos = 0;
if(ctx.state > 0) {
ushell_putc('>');
ushell_putc(' ');
}
continue;
}
if((c == USHELL_DELETE) && (ctx.buf_pos != 0)) {
ctx.buf_pos--;
ctx.buf[ctx.buf_pos] = '\0';
ushell_putc('\b');
ushell_putc(' ');
ushell_putc('\b');
continue;
}
if(c > 0 && ctx.buf_pos < USHELL_CMD_BUF) {
prediction = 0;
#ifndef USHELL_DISABLE_ECHO
ushell_putc(c);
#endif
ctx.buf[ctx.buf_pos++] = c;
}
}
}
void ushell_init(ushell_cmd_def_t *cmd_list)
{
ctx.user_cmds = cmd_list;
ctx.state = 1;
ctx.buf_pos = 0;
}