Simple embedded shell with runtime pluggable commands.

Dependents:   DataBus2018

Implements a simple unix-like shell for embedded systems with a pluggable command architecture.

Committer:
shimniok
Date:
Sun Dec 02 17:20:15 2018 +0000
Revision:
4:8b8fa59d0015
Parent:
3:ebb4893f033d
Child:
5:8f486f4d29d3
Initial implementation of command lookup

Who changed what in which revision?

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