Simple embedded shell with runtime pluggable commands.
Implements a simple unix-like shell for embedded systems with a pluggable command architecture.
SimpleShell.cpp
- Committer:
- shimniok
- Date:
- 2018-12-13
- Revision:
- 13:a29fb89018e1
- Parent:
- 12:ecf3fc049bca
- Child:
- 14:75b5918090ae
File content as of revision 13:a29fb89018e1:
#include "SimpleShell.h" #include <ctype.h> #include <string> #define ESC 0x1b #define UP 0x41 #define DOWN 0x42 #define RIGHT 0x43 #define LEFT 0x44 #define HOME 0x31 #define INS 0x32 #define DEL 0x33 #define PGUP 0x35 #define PGDN 0x36 #define TAIL 0x7e SimpleShell::SimpleShell() { lookupEnd = 0; // Built-in shell commands attach(callback(this, &SimpleShell::help), "help"); attach(callback(this, &SimpleShell::pwd), "pwd"); } void SimpleShell::help() { printf("Available commands: "); for (int i=0; i < lookupEnd; i++) { printf("%s ", lookup[i].command); } printf("\n\n"); } void SimpleShell::pwd() { puts(_cwd); return; } void SimpleShell::run() { bool done=false; Callback<void()> cb; //int status; // TODO implement command status return std::string x; // Set current working directory strncpy(_cwd, "/log", MAXBUF); printf("Type help for assistance.\n"); help(); while (!done) { printPrompt(); readCommand(); if (cmd[0]) { // skip blank command if (cb = findCommand()) { cb.call(); } else { printf("command <%s> not found\n", cmd); } } } puts("exiting shell\n"); return; } void SimpleShell::attach(Callback<void()> cb, char *command) { if (lookupEnd < MAXLOOKUP) { lookup[lookupEnd].cb = cb; lookup[lookupEnd].command = command; lookupEnd++; } return; } Callback<void()> SimpleShell::findCommand() { Callback<void()> cb=NULL; for (int i=0; i < lookupEnd; i++) { if (strncmp(cmd, lookup[i].command, MAXBUF) == 0) { cb = lookup[i].cb; break; } } return cb; } void SimpleShell::printPrompt() { fputc('(', stdout); fputs(_cwd, stdout); fputs(")# ", stdout); return; } void SimpleShell::readCommand() { int i=0; char c; bool done = false; memset(cmd, 0, MAXBUF); do { cmd[i] = 0; c = fgetc(stdin); if (c == '\r') { // if return is hit, we're done, don't add \r to cmd done = true; } else if (c == ESC) { // keyboard escape codes (arrow keys, etc) int c2 = getchar(); int c3 = getchar(); if (c2 == 0x4f && c3 == 0x46) { printf("<END>"); } else if (c2 == 0x5b) { if (c3 == UP) { printf("<UP>"); } else if (c3 == DOWN) { printf("<DOWN>"); } else if (c3 == RIGHT) { printf("<RIGHT>"); } else if (c3 == LEFT) { printf("<LEFT>"); } else if (c3 == HOME || c3 == INS || c3 == DEL || c3 == PGUP || c3 == PGDN) { char c4 = getchar(); if (c4 == TAIL) { if (c4 == HOME) { printf("<HOME>"); } else if (c4 == INS) { printf("<INS>"); } else if (c4 == DEL) { printf("<DEL>"); } else if (c4 == PGUP) { printf("<PGUP>"); } else if (c4 == PGDN) { printf("<PGDN>"); }//if }//if }//if }//if //printf("\n"); } else if (i < MAXBUF-1) { if (c == 0x7f || c == '\b') { // backspace or delete if (i > 0) { // if we're at the beginning, do nothing i--; fputs("\b \b", stdout); } } else { if (isprint(c)) fputc(c, stdout); cmd[i++] = c; } } } while (!done); fputc('\n', stdout); // chomp for (int j=i; j >= 0; j--) { if (isspace(cmd[j])) { cmd[j] = '\0'; } } return; }