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:09:08 2018 +0000
Revision:
2:4f0affdb7db9
Parent:
1:998a7ed04f10
Child:
3:ebb4893f033d
implemented attach command basics

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 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 0:49820d5a38c9 17 //this.doCommand(cmdline);
shimniok 0:49820d5a38c9 18 }
shimniok 0:49820d5a38c9 19 puts("exiting shell\n");
shimniok 0:49820d5a38c9 20
shimniok 0:49820d5a38c9 21 return;
shimniok 0:49820d5a38c9 22 }
shimniok 0:49820d5a38c9 23
shimniok 0:49820d5a38c9 24
shimniok 2:4f0affdb7db9 25 void SimpleShell::attach(Callback<void()> cb, char *command)
shimniok 2:4f0affdb7db9 26 {
shimniok 2:4f0affdb7db9 27 if (lookupEnd < MAXLOOKUP) {
shimniok 2:4f0affdb7db9 28 lookup[lookupEnd].cb = cb;
shimniok 2:4f0affdb7db9 29 lookup[lookupEnd].command = command;
shimniok 2:4f0affdb7db9 30 lookupEnd++;
shimniok 2:4f0affdb7db9 31 }
shimniok 2:4f0affdb7db9 32
shimniok 2:4f0affdb7db9 33 for (int i=0; i < lookupEnd; i++) {
shimniok 2:4f0affdb7db9 34 printf("%s\n", lookup[i].command);
shimniok 2:4f0affdb7db9 35 }
shimniok 2:4f0affdb7db9 36
shimniok 2:4f0affdb7db9 37 return;
shimniok 1:998a7ed04f10 38 }
shimniok 1:998a7ed04f10 39
shimniok 1:998a7ed04f10 40
shimniok 0:49820d5a38c9 41 void SimpleShell::printPrompt()
shimniok 0:49820d5a38c9 42 {
shimniok 0:49820d5a38c9 43 fputc('(', stdout);
shimniok 0:49820d5a38c9 44 fputs(_cwd, stdout);
shimniok 0:49820d5a38c9 45 fputs(")# ", stdout);
shimniok 2:4f0affdb7db9 46
shimniok 2:4f0affdb7db9 47 return;
shimniok 0:49820d5a38c9 48 }
shimniok 0:49820d5a38c9 49
shimniok 0:49820d5a38c9 50
shimniok 0:49820d5a38c9 51 void SimpleShell::readCommand()
shimniok 0:49820d5a38c9 52 {
shimniok 0:49820d5a38c9 53 int i=0;
shimniok 0:49820d5a38c9 54 char c;
shimniok 0:49820d5a38c9 55 bool done = false;
shimniok 0:49820d5a38c9 56
shimniok 0:49820d5a38c9 57 memset(cmd, 0, MAXBUF);
shimniok 0:49820d5a38c9 58 do {
shimniok 0:49820d5a38c9 59 cmd[i] = 0;
shimniok 0:49820d5a38c9 60 c = fgetc(stdin);
shimniok 0:49820d5a38c9 61 if (c == '\r') { // if return is hit, we're done, don't add \r to cmd
shimniok 0:49820d5a38c9 62 done = true;
shimniok 0:49820d5a38c9 63 } else if (i < MAXBUF-1) {
shimniok 0:49820d5a38c9 64 if (c == 0x7f || c == '\b') { // backspace or delete
shimniok 0:49820d5a38c9 65 if (i > 0) { // if we're at the beginning, do nothing
shimniok 0:49820d5a38c9 66 i--;
shimniok 0:49820d5a38c9 67 fputs("\b \b", stdout);
shimniok 0:49820d5a38c9 68
shimniok 0:49820d5a38c9 69 }
shimniok 0:49820d5a38c9 70 } else {
shimniok 0:49820d5a38c9 71 fputc(c, stdout);
shimniok 0:49820d5a38c9 72 cmd[i++] = c;
shimniok 0:49820d5a38c9 73 }
shimniok 0:49820d5a38c9 74 }
shimniok 0:49820d5a38c9 75 } while (!done);
shimniok 0:49820d5a38c9 76 fputc('\n', stdout);
shimniok 0:49820d5a38c9 77
shimniok 2:4f0affdb7db9 78 return;
shimniok 0:49820d5a38c9 79 }