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 16:50:40 2018 +0000
Revision:
1:998a7ed04f10
Parent:
0:49820d5a38c9
Child:
2:4f0affdb7db9
Stubbed out command attach

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 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 1:998a7ed04f10 24 void SimpleShell::attach(Callback<void()> cb, char *command) {
shimniok 1:998a7ed04f10 25 }
shimniok 1:998a7ed04f10 26
shimniok 1:998a7ed04f10 27
shimniok 0:49820d5a38c9 28 void SimpleShell::printPrompt()
shimniok 0:49820d5a38c9 29 {
shimniok 0:49820d5a38c9 30 fputc('(', stdout);
shimniok 0:49820d5a38c9 31 fputs(_cwd, stdout);
shimniok 0:49820d5a38c9 32 fputs(")# ", stdout);
shimniok 0:49820d5a38c9 33 }
shimniok 0:49820d5a38c9 34
shimniok 0:49820d5a38c9 35
shimniok 0:49820d5a38c9 36 void SimpleShell::readCommand()
shimniok 0:49820d5a38c9 37 {
shimniok 0:49820d5a38c9 38 int i=0;
shimniok 0:49820d5a38c9 39 char c;
shimniok 0:49820d5a38c9 40 bool done = false;
shimniok 0:49820d5a38c9 41
shimniok 0:49820d5a38c9 42 memset(cmd, 0, MAXBUF);
shimniok 0:49820d5a38c9 43 do {
shimniok 0:49820d5a38c9 44 cmd[i] = 0;
shimniok 0:49820d5a38c9 45 c = fgetc(stdin);
shimniok 0:49820d5a38c9 46 if (c == '\r') { // if return is hit, we're done, don't add \r to cmd
shimniok 0:49820d5a38c9 47 done = true;
shimniok 0:49820d5a38c9 48 } else if (i < MAXBUF-1) {
shimniok 0:49820d5a38c9 49 if (c == 0x7f || c == '\b') { // backspace or delete
shimniok 0:49820d5a38c9 50 if (i > 0) { // if we're at the beginning, do nothing
shimniok 0:49820d5a38c9 51 i--;
shimniok 0:49820d5a38c9 52 fputs("\b \b", stdout);
shimniok 0:49820d5a38c9 53
shimniok 0:49820d5a38c9 54 }
shimniok 0:49820d5a38c9 55 } else {
shimniok 0:49820d5a38c9 56 fputc(c, stdout);
shimniok 0:49820d5a38c9 57 cmd[i++] = c;
shimniok 0:49820d5a38c9 58 }
shimniok 0:49820d5a38c9 59 }
shimniok 0:49820d5a38c9 60 } while (!done);
shimniok 0:49820d5a38c9 61 fputc('\n', stdout);
shimniok 0:49820d5a38c9 62
shimniok 0:49820d5a38c9 63
shimniok 0:49820d5a38c9 64 }