Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
Diff: SimpleShell.cpp
- Revision:
- 14:75b5918090ae
- Parent:
- 13:a29fb89018e1
- Child:
- 15:242626d8d104
diff -r a29fb89018e1 -r 75b5918090ae SimpleShell.cpp --- a/SimpleShell.cpp Thu Dec 13 23:26:34 2018 +0000 +++ b/SimpleShell.cpp Wed Dec 19 00:29:05 2018 +0000 @@ -57,11 +57,11 @@ while (!done) { printPrompt(); readCommand(); - if (cmd[0]) { // skip blank command + if (argv[0]) { // skip blank command if (cb = findCommand()) { cb.call(); } else { - printf("command <%s> not found\n", cmd); + printf("command <%s> not found\n", argv[0]); } } } @@ -88,7 +88,7 @@ Callback<void()> cb=NULL; for (int i=0; i < lookupEnd; i++) { - if (strncmp(cmd, lookup[i].command, MAXBUF) == 0) { + if (strncmp(argv[0], lookup[i].command, MAXBUF) == 0) { cb = lookup[i].cb; break; } @@ -113,6 +113,7 @@ int i=0; char c; bool done = false; + static char cmd[MAXBUF]; memset(cmd, 0, MAXBUF); do { @@ -171,11 +172,30 @@ } while (!done); fputc('\n', stdout); - // chomp - for (int j=i; j >= 0; j--) { - if (isspace(cmd[j])) { - cmd[j] = '\0'; - } + // remove leading/trailing whitespace + char *s = cmd; + while (isspace(*s)) { + s++; + } + for (int j=i; j >= 0 && isspace(cmd[j]); j--) { + cmd[j] = '\0'; + } + + // split into command and arguments + argc = 0; + char *t; + + for (int i=0; i < MAXARGS; i++) { + argv[i] = NULL; + } + t = strtok(s, " "); + while (t && argc < 10) { + argv[argc++] = t; + t = strtok(NULL, " "); + } + // Print out argv + for (int i=0; i < argc; i++) { + printf("argv[%d] = <%s>\n", i, argv[i]); } return;