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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
/*
This file is part of UFFS, the Ultra-low-cost Flash File System.
Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
UFFS is free software; you can redistribute it and/or modify it under
the GNU Library General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
UFFS is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
or GNU Library General Public License, as applicable, for more details.
You should have received a copy of the GNU General Public License
and GNU Library General Public License along with UFFS; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
As a special exception, if other files instantiate templates or use
macros or inline functions from this file, or you compile this file
and link it with other works to produce a work based on this file,
this file does not by itself cause the resulting work to be covered
by the GNU General Public License. However the source code for this
file must still be made available in accordance with section (3) of
the GNU General Public License v2.
This exception does not invalidate any other reasons why a work based
on this file might be covered by the GNU General Public License.
*/
/**
* \file cmdline.c
* \brief command line test interface
* \author Ricky Zheng, created in 22th Feb, 2007
*/
#include <string.h>
#include <stdio.h>
//#include <conio.h>
#include "uffs_config.h"
#include "cmdline.h"
#include "uffs/uffs_fs.h"
#define PROMPT "UFFS>"
#define PFX "cli : "
#define MSGLN(msg,...) uffs_Perror(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
#define MSG(msg,...) uffs_PerrorRaw(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
#define MAX_CLI_ARGS_BUF_LEN 120
#define MAX_CLI_ARGS_NUM 20
#define MAX_CLI_ENV_NUM 11 // '?', '0' - '9'
struct cli_arg {
int argc;
char *argv[MAX_CLI_ARGS_NUM];
char _buf[MAX_CLI_ARGS_BUF_LEN];
};
static BOOL m_exit = FALSE;
static BOOL m_abort = FALSE;
static struct cli_commandset *m_cmdset_head = NULL;
// Note: last command return code stored in env 0.
static int m_cli_envs[MAX_CLI_ENV_NUM] = {0}; // cli environment variables
static const struct cli_command * cli_find(const char *cmd);
static int cmd_help(int argc, char *argv[]);
#define FOR_EACH_CLI_CMD(set, cmd) \
for (set = m_cmdset_head; set && set->cmds; set = set->next) \
for (cmd = set->cmds; cmd && cmd->handler; cmd++)
/*** filter out leading and tailing spaces, discard comments
* return pointer to start of new command line
*/
static char * cli_process_line(char *p)
{
char *s;
char *x;
if (!p)
return NULL;
// skip leading spaces
while (p && (*p == ' ' || *p == '\t'))
p++;
for (s = x = p; *p; x++, p++) {
switch(*p) {
case '\\':
p++;
if (*p) {
switch(*p) {
case 'n':
*x = '\n';
break;
case 'r':
*x = '\r';
break;
case 't':
*x = '\t';
break;
case 'b':
*x = '\b';
break;
default:
if (*p >= '0' && *p <= '9')
*x = *p - '0';
else
*x = *p;
break;
}
}
break;
default:
if (*p == '\r' || *p == '\n' || *p == '#') *p = '\0';
*x = *p;
break;
}
if (*p == 0)
break;
}
// trim tailing spaces
p--;
while (p > s && (*p == ' ' || *p == '\t'))
*p-- = '\0';
return s;
}
static int cli_env_to_idx(char env)
{
int idx = -1;
if (env >= '0' && env <= '9') {
idx = env - '0' + 1;
}
else if (env == '?') {
idx = 0;
}
return idx;
}
int cli_env_set(char env, int val)
{
int idx = cli_env_to_idx(env);
if (idx >= 0) {
m_cli_envs[idx] = val;
return 0;
}
else
return -1;
}
int cli_env_get(char env)
{
int idx = cli_env_to_idx(env);
return idx >= 0 ? m_cli_envs[idx] : 0;
}
/** exec command <n> times:
* exec <n> <cmd> [...]
*/
static int cmd_exec(int argc, char *argv[])
{
int n = 0;
const struct cli_command *cmd;
CHK_ARGC(3, 0);
if (sscanf(argv[1], "%d", &n) != 1)
return CLI_INVALID_ARG;
if (n <= 0)
return CLI_INVALID_ARG;
cmd = cli_find(argv[2]);
if (cmd == NULL) {
MSG("Unknown command '%s'\n", argv[2]);
return -1;
}
else {
argv += 2;
while (n-- >= 0) {
if (cmd->handler(argc - 2, argv) != 0)
return -1;
}
}
return 0;
}
/**
* test expression
* test <a> <op> <b>
* for example:
* test 1 > 0 ==> return 0
* test 1 <= 0 ==> return -1
*/
static int cmd_test(int argc, char *argv[])
{
int a, b;
char *op;
BOOL tst = FALSE;
CHK_ARGC(4, 4);
if (sscanf(argv[1], "%d", &a) != 1 ||
sscanf(argv[3], "%d", &b) != 1)
{
return CLI_INVALID_ARG;
}
op = argv[2];
if (!strcmp(op, ">")) {
tst = (a > b);
}
else if (!strcmp(op, "<")) {
tst = (a < b);
}
else if (!strcmp(op, "==")) {
tst = (a == b);
}
else if (!strcmp(op, ">=")) {
tst = (a >= b);
}
else if (!strcmp(op, "<=")) {
tst = (a <= b);
}
else if (!strcmp(op, "!=")) {
tst = (a != b);
}
else {
return CLI_INVALID_ARG;
}
return tst ? 0 : -1;
}
/** if last command failed (return != 0), run <cmd>
* ! <cmd>
*/
static int cmd_failed(int argc, char *argv[])
{
const struct cli_command *cmd;
CHK_ARGC(2, 0);
cmd = cli_find(argv[1]);
if (cmd == NULL) {
MSG("Unknown command '%s'\n", argv[1]);
return -1;
}
else {
argv++;
return (cli_env_get('?') == 0 ? 0 : cmd->handler(argc - 1, argv));
}
}
/** print messages
* echo [<arg> ...]
*/
static int cmd_echo(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++) {
MSG("%s%s", i > 1 ? " " : "", argv[i]);
}
MSG("\n");
return 0;
}
/** set cli environment variable
* set <env> <value>
*/
static int cmd_set(int argc, char *argv[])
{
int val;
int ret = -1;
CHK_ARGC(3, 0);
if (sscanf(argv[2], "%d", &val) == 1) {
ret = cli_env_set(argv[1][0], val);
}
return ret;
}
/** evaluation the expresstion, result to $1
* evl <value> <op> <value>
*/
static int cmd_evl(int argc, char *argv[])
{
int val1, val2, result = 0;
int ret = -1;
CHK_ARGC(4, 4);
if (sscanf(argv[1], "%d", &val1) == 1 &&
sscanf(argv[3], "%d", &val2) == 1) {
ret = 0;
switch(argv[2][0]) {
case '+':
result = val1 + val2;
break;
case '-':
result = val1 - val2;
break;
case '*':
result = val1 * val2;
break;
case '/':
if (val2 == 0)
ret = -1;
else
result = val1 / val2;
break;
case '%':
if (val2 == 0)
ret = -1;
else
result = val1 % val2;
break;
default:
ret = CLI_INVALID_ARG;
break;
}
}
if (ret == 0)
ret = cli_env_set('1', result);
return ret;
}
static int cmd_exit(int argc, char *argv[])
{
m_exit = TRUE;
return 0;
}
/** Abort current script
* abort [...]
*/
static int cmd_abort(int argc, char *argv[])
{
if (argc > 1) {
cmd_echo(argc, argv);
}
m_abort = TRUE;
return 0;
}
/** run local file system's script
* script <filename>
*/
static int cmd_script(int argc, char *argv[])
{
char line_buf[256];
char *p;
FILE *fp;
const char *name;
int ret = 0;
static int stack = 0;
CHK_ARGC(2, 0);
if (stack++ == 0)
m_abort = FALSE;
name = argv[1];
fp = fopen(name, "r");
if (fp) {
memset(line_buf, 0, sizeof(line_buf));
while (!m_abort && fgets(line_buf, sizeof(line_buf) - 1, fp)) {
p = line_buf + sizeof(line_buf) - 1;
while (*p == 0 && p > line_buf)
p--;
while ((*p == '\r' || *p == '\n') && p > line_buf) {
*p-- = 0;
}
p = cli_process_line(line_buf);
if (*p)
ret = cli_interpret(p);
memset(line_buf, 0, sizeof(line_buf));
}
fclose(fp);
}
else {
MSG("Can't open host script file '%s' for read\n", name);
ret = -1;
}
stack--;
return ret;
}
static const struct cli_command default_cmds[] =
{
{ cmd_help, "help|?", "[<command>]", "show commands or help on one command" },
{ cmd_exit, "exit", NULL, "exit command line" },
{ cmd_exec, "*", "<n> <cmd> [...]>", "run <cmd> <n> times" },
{ cmd_failed, "!", "<cmd> [...]", "run <cmd> if last command failed" },
{ cmd_echo, "echo", "[...]", "print messages" },
{ cmd_set, "set", "<env> <val>", "set env variable" },
{ cmd_evl, "evl", "<val> <op> <val>", "evaluation expresstion" },
{ cmd_test, "test", "<a> <op> <b>", "test expression: <a> <op> <b>" },
{ cmd_script, "script", "<file>", "run host script <file>" },
{ cmd_abort, "abort", NULL, "abort from the running script" },
{ NULL, NULL, NULL, NULL }
};
static struct cli_commandset default_cmdset = {
default_cmds,
};
static BOOL match_cmd(const char *src, int start, int end, const char *des)
{
while (src[start] == ' ' && start < end)
start++;
while (src[end] == ' ' && start < end)
end--;
if ((int)strlen(des) == (end - start + 1)) {
if (memcmp(src + start, des, end - start + 1) == 0) {
return TRUE;
}
}
return FALSE;
}
static BOOL check_cmd(const char *cmds, const char *cmd)
{
int start, end;
for (start = end = 0; cmds[end] != 0 && cmds[end] != '|'; end++);
while (end > start) {
if (match_cmd(cmds, start, end - 1, cmd) == TRUE)
return TRUE;
if (cmds[end] == 0)
break;
if (cmds[end] == '|') {
end++;
for (start = end; cmds[end] != 0 && cmds[end] != '|'; end++);
}
}
return FALSE;
}
static const struct cli_command * cli_find(const char *cmd)
{
struct cli_commandset *work;
const struct cli_command *s;
FOR_EACH_CLI_CMD(work, s) {
if (check_cmd(s->cmd, cmd) == TRUE)
return s;
}
return NULL;
}
static void show_cmd_usage(const struct cli_command *cmd)
{
MSG("%s: %s\n", cmd->cmd, cmd->descr);
MSG("Usage: %s %s\n", cmd->cmd, cmd->args ? cmd->args : "");
}
static int cmd_help(int argc, char *argv[])
{
const struct cli_command *cmd;
struct cli_commandset *cmdset;
int i, n;
if (argc < 2) {
MSG("Available commands:\n");
n = 0;
FOR_EACH_CLI_CMD(cmdset, cmd) {
MSG("%s", cmd->cmd);
for (i = strlen(cmd->cmd); i%10; i++, MSG(" "));
if ((++n % 5) == 0) MSG("\n");
}
MSG("\n");
}
else {
cmd = cli_find(argv[1]);
if (cmd == NULL) {
MSG("No such command\n");
return -1;
}
else {
show_cmd_usage(cmd);
}
}
return 0;
}
static void cli_parse_args(const char *cmd, struct cli_arg *arg)
{
char *p;
int val;
if (arg) {
arg->argc = 0;
if (cmd) {
p = arg->_buf;
while (*cmd && arg->argc < MAX_CLI_ARGS_NUM && (p - arg->_buf < MAX_CLI_ARGS_BUF_LEN)) {
while(*cmd && (*cmd == ' ' || *cmd == '\t'))
cmd++;
arg->argv[arg->argc] = p;
while (*cmd && (*cmd != ' ' && *cmd != '\t') && (p - arg->_buf < MAX_CLI_ARGS_BUF_LEN)) {
if (*cmd == '$') {
// command env replacement
cmd++;
val = cli_env_get(*cmd++);
if (p - arg->_buf < MAX_CLI_ARGS_BUF_LEN - 12) { // 12 is long enough for 32bit 'int'
p += sprintf(p, "%d", val & 0xFFFFFFFF);
}
}
else
*p++ = *cmd++;
}
*p++ = '\0';
if (*(arg->argv[arg->argc]) == '\0')
break;
arg->argc++;
}
}
}
}
int cli_interpret(const char *line)
{
struct cli_arg arg = {0};
const struct cli_command *cmd;
int ret = -1;
cli_parse_args(line, &arg);
if (arg.argc > 0) {
cmd = cli_find(arg.argv[0]);
if (cmd == NULL) {
MSG("Unknown command '%s'\n", arg.argv[0]);
}
else {
ret = cmd->handler(arg.argc, arg.argv);
if (ret == CLI_INVALID_ARG) {
MSG("\n");
show_cmd_usage(cmd);
}
}
}
cli_env_set('?', ret); // $? = last command return code
return ret;
}
void cli_add_commandset(struct cli_commandset *set)
{
if (set) {
set->next = m_cmdset_head;
m_cmdset_head = set;
}
}
void cli_main_entry()
{
char line[80];
int linelen = 0;
char *p;
MSG("$ ");
cli_add_commandset(&default_cmdset);
while (!m_exit) {
char ch;
if (linelen >= sizeof(line))
continue;
ch = getc(stdin);
switch (ch) {
case 8:
case 127:
if (linelen > 0) {
--linelen;
MSG("\x08 \x08");
}
break;
case '\r':
case '\n':
//MSG("\r\n");
if (linelen > 0) {
line[linelen] = 0;
p = cli_process_line(line);
if (*p)
cli_interpret(p);
linelen = 0;
}
MSG("$ ");
break;
case 21:
while (linelen > 0) {
--linelen;
MSG("\x08 \x08");
}
break;
default:
if (ch >= ' ' && ch < 127 && linelen < sizeof(line) - 1) {
line[linelen++] = ch;
//MSG("%c", ch);
}
}
}
}
|