Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
SimpleShell.cpp@0:49820d5a38c9, 2018-12-01 (annotated)
- Committer:
- shimniok
- Date:
- Sat Dec 01 23:18:03 2018 +0000
- Revision:
- 0:49820d5a38c9
- Child:
- 1:998a7ed04f10
Initial functionality working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:49820d5a38c9 | 1 | #include "SimpleShell.h" |
shimniok | 0:49820d5a38c9 | 2 | |
shimniok | 0:49820d5a38c9 | 3 | SimpleShell::SimpleShell() |
shimniok | 0:49820d5a38c9 | 4 | { |
shimniok | 0:49820d5a38c9 | 5 | } |
shimniok | 0:49820d5a38c9 | 6 | |
shimniok | 0:49820d5a38c9 | 7 | void SimpleShell::run() |
shimniok | 0:49820d5a38c9 | 8 | { |
shimniok | 0:49820d5a38c9 | 9 | bool done=false; |
shimniok | 0:49820d5a38c9 | 10 | strcpy(_cwd, "/log"); |
shimniok | 0:49820d5a38c9 | 11 | |
shimniok | 0:49820d5a38c9 | 12 | printf("Type help for assistance.\n"); |
shimniok | 0:49820d5a38c9 | 13 | while (!done) { |
shimniok | 0:49820d5a38c9 | 14 | printPrompt(); |
shimniok | 0:49820d5a38c9 | 15 | readCommand(); |
shimniok | 0:49820d5a38c9 | 16 | //this.doCommand(cmdline); |
shimniok | 0:49820d5a38c9 | 17 | } |
shimniok | 0:49820d5a38c9 | 18 | puts("exiting shell\n"); |
shimniok | 0:49820d5a38c9 | 19 | |
shimniok | 0:49820d5a38c9 | 20 | return; |
shimniok | 0:49820d5a38c9 | 21 | } |
shimniok | 0:49820d5a38c9 | 22 | |
shimniok | 0:49820d5a38c9 | 23 | |
shimniok | 0:49820d5a38c9 | 24 | void SimpleShell::printPrompt() |
shimniok | 0:49820d5a38c9 | 25 | { |
shimniok | 0:49820d5a38c9 | 26 | fputc('(', stdout); |
shimniok | 0:49820d5a38c9 | 27 | fputs(_cwd, stdout); |
shimniok | 0:49820d5a38c9 | 28 | fputs(")# ", stdout); |
shimniok | 0:49820d5a38c9 | 29 | } |
shimniok | 0:49820d5a38c9 | 30 | |
shimniok | 0:49820d5a38c9 | 31 | |
shimniok | 0:49820d5a38c9 | 32 | void SimpleShell::readCommand() |
shimniok | 0:49820d5a38c9 | 33 | { |
shimniok | 0:49820d5a38c9 | 34 | int i=0; |
shimniok | 0:49820d5a38c9 | 35 | char c; |
shimniok | 0:49820d5a38c9 | 36 | bool done = false; |
shimniok | 0:49820d5a38c9 | 37 | |
shimniok | 0:49820d5a38c9 | 38 | memset(cmd, 0, MAXBUF); |
shimniok | 0:49820d5a38c9 | 39 | do { |
shimniok | 0:49820d5a38c9 | 40 | cmd[i] = 0; |
shimniok | 0:49820d5a38c9 | 41 | c = fgetc(stdin); |
shimniok | 0:49820d5a38c9 | 42 | if (c == '\r') { // if return is hit, we're done, don't add \r to cmd |
shimniok | 0:49820d5a38c9 | 43 | done = true; |
shimniok | 0:49820d5a38c9 | 44 | } else if (i < MAXBUF-1) { |
shimniok | 0:49820d5a38c9 | 45 | if (c == 0x7f || c == '\b') { // backspace or delete |
shimniok | 0:49820d5a38c9 | 46 | if (i > 0) { // if we're at the beginning, do nothing |
shimniok | 0:49820d5a38c9 | 47 | i--; |
shimniok | 0:49820d5a38c9 | 48 | fputs("\b \b", stdout); |
shimniok | 0:49820d5a38c9 | 49 | |
shimniok | 0:49820d5a38c9 | 50 | } |
shimniok | 0:49820d5a38c9 | 51 | } else { |
shimniok | 0:49820d5a38c9 | 52 | fputc(c, stdout); |
shimniok | 0:49820d5a38c9 | 53 | cmd[i++] = c; |
shimniok | 0:49820d5a38c9 | 54 | } |
shimniok | 0:49820d5a38c9 | 55 | } |
shimniok | 0:49820d5a38c9 | 56 | } while (!done); |
shimniok | 0:49820d5a38c9 | 57 | fputc('\n', stdout); |
shimniok | 0:49820d5a38c9 | 58 | |
shimniok | 0:49820d5a38c9 | 59 | |
shimniok | 0:49820d5a38c9 | 60 | } |