Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
SimpleShell.cpp@3:ebb4893f033d, 2018-12-02 (annotated)
- Committer:
- shimniok
- Date:
- Sun Dec 02 17:14:28 2018 +0000
- Revision:
- 3:ebb4893f033d
- Parent:
- 2:4f0affdb7db9
- Child:
- 4:8b8fa59d0015
Stubbed command lookup
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 | 2:4f0affdb7db9 | 5 | lookupEnd = 0; |
shimniok | 0:49820d5a38c9 | 6 | } |
shimniok | 0:49820d5a38c9 | 7 | |
shimniok | 0:49820d5a38c9 | 8 | void SimpleShell::run() |
shimniok | 0:49820d5a38c9 | 9 | { |
shimniok | 0:49820d5a38c9 | 10 | bool done=false; |
shimniok | 0:49820d5a38c9 | 11 | strcpy(_cwd, "/log"); |
shimniok | 0:49820d5a38c9 | 12 | |
shimniok | 0:49820d5a38c9 | 13 | printf("Type help for assistance.\n"); |
shimniok | 0:49820d5a38c9 | 14 | while (!done) { |
shimniok | 0:49820d5a38c9 | 15 | printPrompt(); |
shimniok | 0:49820d5a38c9 | 16 | readCommand(); |
shimniok | 3:ebb4893f033d | 17 | //cb = findCommand(); |
shimniok | 3:ebb4893f033d | 18 | //if cb |
shimniok | 3:ebb4893f033d | 19 | // status = cb.call(); |
shimniok | 3:ebb4893f033d | 20 | // done = status == EXIT |
shimniok | 3:ebb4893f033d | 21 | //else error not found |
shimniok | 0:49820d5a38c9 | 22 | } |
shimniok | 0:49820d5a38c9 | 23 | puts("exiting shell\n"); |
shimniok | 0:49820d5a38c9 | 24 | |
shimniok | 0:49820d5a38c9 | 25 | return; |
shimniok | 0:49820d5a38c9 | 26 | } |
shimniok | 0:49820d5a38c9 | 27 | |
shimniok | 0:49820d5a38c9 | 28 | |
shimniok | 2:4f0affdb7db9 | 29 | void SimpleShell::attach(Callback<void()> cb, char *command) |
shimniok | 2:4f0affdb7db9 | 30 | { |
shimniok | 2:4f0affdb7db9 | 31 | if (lookupEnd < MAXLOOKUP) { |
shimniok | 2:4f0affdb7db9 | 32 | lookup[lookupEnd].cb = cb; |
shimniok | 2:4f0affdb7db9 | 33 | lookup[lookupEnd].command = command; |
shimniok | 2:4f0affdb7db9 | 34 | lookupEnd++; |
shimniok | 2:4f0affdb7db9 | 35 | } |
shimniok | 2:4f0affdb7db9 | 36 | |
shimniok | 2:4f0affdb7db9 | 37 | for (int i=0; i < lookupEnd; i++) { |
shimniok | 2:4f0affdb7db9 | 38 | printf("%s\n", lookup[i].command); |
shimniok | 2:4f0affdb7db9 | 39 | } |
shimniok | 2:4f0affdb7db9 | 40 | |
shimniok | 2:4f0affdb7db9 | 41 | return; |
shimniok | 1:998a7ed04f10 | 42 | } |
shimniok | 1:998a7ed04f10 | 43 | |
shimniok | 1:998a7ed04f10 | 44 | |
shimniok | 3:ebb4893f033d | 45 | Callback<void()> SimpleShell::findCommand() |
shimniok | 3:ebb4893f033d | 46 | { |
shimniok | 3:ebb4893f033d | 47 | Callback<void()> cb=NULL; |
shimniok | 3:ebb4893f033d | 48 | |
shimniok | 3:ebb4893f033d | 49 | return cb; |
shimniok | 3:ebb4893f033d | 50 | } |
shimniok | 3:ebb4893f033d | 51 | |
shimniok | 3:ebb4893f033d | 52 | |
shimniok | 0:49820d5a38c9 | 53 | void SimpleShell::printPrompt() |
shimniok | 0:49820d5a38c9 | 54 | { |
shimniok | 0:49820d5a38c9 | 55 | fputc('(', stdout); |
shimniok | 0:49820d5a38c9 | 56 | fputs(_cwd, stdout); |
shimniok | 0:49820d5a38c9 | 57 | fputs(")# ", stdout); |
shimniok | 2:4f0affdb7db9 | 58 | |
shimniok | 2:4f0affdb7db9 | 59 | return; |
shimniok | 0:49820d5a38c9 | 60 | } |
shimniok | 0:49820d5a38c9 | 61 | |
shimniok | 0:49820d5a38c9 | 62 | |
shimniok | 0:49820d5a38c9 | 63 | void SimpleShell::readCommand() |
shimniok | 0:49820d5a38c9 | 64 | { |
shimniok | 0:49820d5a38c9 | 65 | int i=0; |
shimniok | 0:49820d5a38c9 | 66 | char c; |
shimniok | 0:49820d5a38c9 | 67 | bool done = false; |
shimniok | 0:49820d5a38c9 | 68 | |
shimniok | 0:49820d5a38c9 | 69 | memset(cmd, 0, MAXBUF); |
shimniok | 0:49820d5a38c9 | 70 | do { |
shimniok | 0:49820d5a38c9 | 71 | cmd[i] = 0; |
shimniok | 0:49820d5a38c9 | 72 | c = fgetc(stdin); |
shimniok | 0:49820d5a38c9 | 73 | if (c == '\r') { // if return is hit, we're done, don't add \r to cmd |
shimniok | 0:49820d5a38c9 | 74 | done = true; |
shimniok | 0:49820d5a38c9 | 75 | } else if (i < MAXBUF-1) { |
shimniok | 0:49820d5a38c9 | 76 | if (c == 0x7f || c == '\b') { // backspace or delete |
shimniok | 0:49820d5a38c9 | 77 | if (i > 0) { // if we're at the beginning, do nothing |
shimniok | 0:49820d5a38c9 | 78 | i--; |
shimniok | 0:49820d5a38c9 | 79 | fputs("\b \b", stdout); |
shimniok | 0:49820d5a38c9 | 80 | |
shimniok | 0:49820d5a38c9 | 81 | } |
shimniok | 0:49820d5a38c9 | 82 | } else { |
shimniok | 0:49820d5a38c9 | 83 | fputc(c, stdout); |
shimniok | 0:49820d5a38c9 | 84 | cmd[i++] = c; |
shimniok | 0:49820d5a38c9 | 85 | } |
shimniok | 0:49820d5a38c9 | 86 | } |
shimniok | 0:49820d5a38c9 | 87 | } while (!done); |
shimniok | 0:49820d5a38c9 | 88 | fputc('\n', stdout); |
shimniok | 0:49820d5a38c9 | 89 | |
shimniok | 2:4f0affdb7db9 | 90 | return; |
shimniok | 0:49820d5a38c9 | 91 | } |