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:
Fri Dec 28 17:10:31 2018 +0000
Revision:
31:27e8130a0d8f
Parent:
30:35522ea06236
Child:
32:fc09f0eb1e8a
began wildcard implementation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 21:5d7ac1f0b842 1 #include "SimpleShell.h"
shimniok 21:5d7ac1f0b842 2 #include <ctype.h>
shimniok 21:5d7ac1f0b842 3 #include <string>
shimniok 28:753db82debb1 4 #include <list>
shimniok 21:5d7ac1f0b842 5
shimniok 21:5d7ac1f0b842 6 #define ESC 0x1b
shimniok 21:5d7ac1f0b842 7 #define UP 0x41
shimniok 21:5d7ac1f0b842 8 #define DOWN 0x42
shimniok 21:5d7ac1f0b842 9 #define RIGHT 0x43
shimniok 21:5d7ac1f0b842 10 #define LEFT 0x44
shimniok 21:5d7ac1f0b842 11 #define HOME 0x31
shimniok 21:5d7ac1f0b842 12 #define INS 0x32
shimniok 21:5d7ac1f0b842 13 #define DEL 0x33
shimniok 21:5d7ac1f0b842 14 #define PGUP 0x35
shimniok 21:5d7ac1f0b842 15 #define PGDN 0x36
shimniok 21:5d7ac1f0b842 16 #define TAIL 0x7e
shimniok 21:5d7ac1f0b842 17
shimniok 21:5d7ac1f0b842 18
shimniok 29:8d4132274445 19 SimpleShell::SimpleShell(char *home)
shimniok 28:753db82debb1 20 {
shimniok 28:753db82debb1 21 lookupEnd = 0;
shimniok 28:753db82debb1 22
shimniok 29:8d4132274445 23 // Set home directory
shimniok 29:8d4132274445 24 strncpy(_home, canon(home), MAXBUF);
shimniok 29:8d4132274445 25
shimniok 29:8d4132274445 26 // Set current working directory
shimniok 29:8d4132274445 27 strncpy(_cwd, _home, MAXBUF);
shimniok 29:8d4132274445 28
shimniok 28:753db82debb1 29 // Built-in shell commands
shimniok 28:753db82debb1 30 command(callback(this, &SimpleShell::help), "help");
shimniok 28:753db82debb1 31 command(callback(this, &SimpleShell::pwd), "pwd");
shimniok 28:753db82debb1 32 command(callback(this, &SimpleShell::cat), "cat");
shimniok 28:753db82debb1 33 command(callback(this, &SimpleShell::cd), "cd");
shimniok 28:753db82debb1 34 command(callback(this, &SimpleShell::rm), "rm");
shimniok 28:753db82debb1 35 command(callback(this, &SimpleShell::touch), "touch");
shimniok 28:753db82debb1 36 command(callback(this, &SimpleShell::ls), "ls");
shimniok 28:753db82debb1 37 command(callback(this, &SimpleShell::send), "send");
shimniok 28:753db82debb1 38 }
shimniok 28:753db82debb1 39
shimniok 28:753db82debb1 40
shimniok 31:27e8130a0d8f 41 bool SimpleShell::haswildcard(char *s) {
shimniok 31:27e8130a0d8f 42 bool result = \
shimniok 31:27e8130a0d8f 43 strchr(s, '*') != NULL || strchr(s, '?') != NULL ||
shimniok 31:27e8130a0d8f 44 (strchr(s, '[') != NULL && strchr(s, ']') != NULL);
shimniok 31:27e8130a0d8f 45 return result;
shimniok 31:27e8130a0d8f 46 }
shimniok 31:27e8130a0d8f 47
shimniok 31:27e8130a0d8f 48
shimniok 28:753db82debb1 49 /// Path stack representation
shimniok 28:753db82debb1 50 typedef std::list<char*> path_t;
shimniok 28:753db82debb1 51
shimniok 21:5d7ac1f0b842 52 char *SimpleShell::canon(char *path) {
shimniok 28:753db82debb1 53 path_t pstack;
shimniok 28:753db82debb1 54 static char tmp[MAXBUF*2];
shimniok 28:753db82debb1 55 char *e;
shimniok 21:5d7ac1f0b842 56
shimniok 28:753db82debb1 57 // if we're passed empty/null string, just send back cwd so nothing breaks
shimniok 28:753db82debb1 58 if (path == NULL || strlen(path) == 0) {
shimniok 28:753db82debb1 59 strcpy(tmp, _cwd);
shimniok 28:753db82debb1 60 return tmp;
shimniok 28:753db82debb1 61 }
shimniok 28:753db82debb1 62
shimniok 28:753db82debb1 63 // relative path? add current working directory to path stack
shimniok 21:5d7ac1f0b842 64 if (path[0] != '/') {
shimniok 28:753db82debb1 65 strcpy(tmp, _cwd);
shimniok 28:753db82debb1 66 strcat(tmp, "/");
shimniok 28:753db82debb1 67 strcat(tmp, path);
shimniok 28:753db82debb1 68 } else {
shimniok 28:753db82debb1 69 strcpy(tmp, path);
shimniok 21:5d7ac1f0b842 70 }
shimniok 28:753db82debb1 71
shimniok 28:753db82debb1 72 // now canonicalize the path spec
shimniok 28:753db82debb1 73 e = strtok(tmp+1, "/");
shimniok 28:753db82debb1 74 while (e) {
shimniok 28:753db82debb1 75 //printf("e = <%s>\n", e);
shimniok 28:753db82debb1 76 if (strcmp("..", e) == 0) {
shimniok 28:753db82debb1 77 // pop most recent directory
shimniok 28:753db82debb1 78 if (!pstack.empty())
shimniok 28:753db82debb1 79 pstack.pop_back();
shimniok 28:753db82debb1 80 } else if (strcmp(".", e) != 0) {
shimniok 28:753db82debb1 81 // push this dir onto path
shimniok 28:753db82debb1 82 if (strlen(e) > 0)
shimniok 28:753db82debb1 83 pstack.push_back(e);
shimniok 28:753db82debb1 84 }
shimniok 28:753db82debb1 85 e = strtok(NULL, "/");
shimniok 28:753db82debb1 86 }
shimniok 21:5d7ac1f0b842 87
shimniok 28:753db82debb1 88 static std::string result;
shimniok 28:753db82debb1 89 result = "";
shimniok 28:753db82debb1 90
shimniok 28:753db82debb1 91 for (path_t::iterator it = pstack.begin(); it != pstack.end(); it++) {
shimniok 28:753db82debb1 92 result.append("/");
shimniok 28:753db82debb1 93 result.append(*it);
shimniok 28:753db82debb1 94 }
shimniok 28:753db82debb1 95
shimniok 28:753db82debb1 96 // if empty, add a /
shimniok 28:753db82debb1 97 if (result.size() < 2) {
shimniok 28:753db82debb1 98 result.append("/");
shimniok 28:753db82debb1 99 }
shimniok 28:753db82debb1 100
shimniok 28:753db82debb1 101 strcpy(tmp, result.c_str());
shimniok 28:753db82debb1 102
shimniok 28:753db82debb1 103 return tmp;
shimniok 21:5d7ac1f0b842 104 }
shimniok 21:5d7ac1f0b842 105
shimniok 21:5d7ac1f0b842 106
shimniok 23:b1e49cfcaef6 107 char *SimpleShell::basename(char *path) {
shimniok 24:004e33abce37 108 char *result = path + strlen(path);
shimniok 24:004e33abce37 109
shimniok 24:004e33abce37 110 while (result != path) {
shimniok 24:004e33abce37 111 if (*result == '/') {
shimniok 24:004e33abce37 112 result++;
shimniok 24:004e33abce37 113 break;
shimniok 24:004e33abce37 114 }
shimniok 24:004e33abce37 115 result--;
shimniok 24:004e33abce37 116 }
shimniok 24:004e33abce37 117
shimniok 24:004e33abce37 118 return result;
shimniok 23:b1e49cfcaef6 119 }
shimniok 23:b1e49cfcaef6 120
shimniok 23:b1e49cfcaef6 121
shimniok 21:5d7ac1f0b842 122 void SimpleShell::help(int argc, char **argv)
shimniok 21:5d7ac1f0b842 123 {
shimniok 21:5d7ac1f0b842 124 printf("Available commands: ");
shimniok 21:5d7ac1f0b842 125 for (int i=0; i < lookupEnd; i++) {
shimniok 21:5d7ac1f0b842 126 printf("%s ", lookup[i].command);
shimniok 21:5d7ac1f0b842 127 }
shimniok 21:5d7ac1f0b842 128 printf("\n\n");
shimniok 21:5d7ac1f0b842 129 }
shimniok 21:5d7ac1f0b842 130
shimniok 21:5d7ac1f0b842 131
shimniok 21:5d7ac1f0b842 132 void SimpleShell::cd(int argc, char **argv)
shimniok 21:5d7ac1f0b842 133 {
shimniok 29:8d4132274445 134 if (argc == 1) {
shimniok 29:8d4132274445 135 strncpy(_cwd, _home, MAXBUF);
shimniok 29:8d4132274445 136 } else if (argc == 2) {
shimniok 28:753db82debb1 137 strncpy(_cwd, canon(argv[1]), MAXBUF);
shimniok 21:5d7ac1f0b842 138 } else {
shimniok 21:5d7ac1f0b842 139 puts("usage: cd directory");
shimniok 21:5d7ac1f0b842 140 }
shimniok 21:5d7ac1f0b842 141 return;
shimniok 21:5d7ac1f0b842 142 }
shimniok 21:5d7ac1f0b842 143
shimniok 21:5d7ac1f0b842 144
shimniok 21:5d7ac1f0b842 145 void SimpleShell::pwd(int argc, char **argv)
shimniok 21:5d7ac1f0b842 146 {
shimniok 21:5d7ac1f0b842 147 puts(_cwd);
shimniok 21:5d7ac1f0b842 148 return;
shimniok 21:5d7ac1f0b842 149 }
shimniok 21:5d7ac1f0b842 150
shimniok 21:5d7ac1f0b842 151
shimniok 21:5d7ac1f0b842 152 void SimpleShell::ls(int argc, char **argv)
shimniok 21:5d7ac1f0b842 153 {
shimniok 21:5d7ac1f0b842 154 DIR *d;
shimniok 21:5d7ac1f0b842 155 struct dirent *p;
shimniok 21:5d7ac1f0b842 156 char *path;
shimniok 21:5d7ac1f0b842 157
shimniok 21:5d7ac1f0b842 158 if (argc == 1) {
shimniok 21:5d7ac1f0b842 159 path = _cwd;
shimniok 21:5d7ac1f0b842 160 } else if (argc == 2) {
shimniok 28:753db82debb1 161 path = canon(argv[1]);
shimniok 21:5d7ac1f0b842 162 } else {
shimniok 21:5d7ac1f0b842 163 puts("usage: ls [directory]");
shimniok 21:5d7ac1f0b842 164 return;
shimniok 21:5d7ac1f0b842 165 }
shimniok 21:5d7ac1f0b842 166
shimniok 21:5d7ac1f0b842 167 int cols=0;
shimniok 21:5d7ac1f0b842 168 if ((d = opendir(path)) != NULL) {
shimniok 21:5d7ac1f0b842 169 while ((p = readdir(d)) != NULL) {
shimniok 21:5d7ac1f0b842 170 if (p->d_name && p->d_name[0] != 0xff) {
shimniok 21:5d7ac1f0b842 171 if (cols++ > 3) {
shimniok 21:5d7ac1f0b842 172 putc('\n', stdout);
shimniok 21:5d7ac1f0b842 173 cols = 0;
shimniok 21:5d7ac1f0b842 174 }
shimniok 21:5d7ac1f0b842 175 printf("%-15s ", p->d_name);
shimniok 21:5d7ac1f0b842 176 }
shimniok 21:5d7ac1f0b842 177 }
shimniok 21:5d7ac1f0b842 178 putc('\n', stdout);
shimniok 21:5d7ac1f0b842 179 if (cols < 3)
shimniok 21:5d7ac1f0b842 180 putc('\n', stdout);
shimniok 21:5d7ac1f0b842 181 closedir(d);
shimniok 21:5d7ac1f0b842 182 } else {
shimniok 21:5d7ac1f0b842 183 puts(path);
shimniok 21:5d7ac1f0b842 184 puts(": No such directory\n");
shimniok 21:5d7ac1f0b842 185 }
shimniok 21:5d7ac1f0b842 186
shimniok 21:5d7ac1f0b842 187 return;
shimniok 21:5d7ac1f0b842 188 }
shimniok 21:5d7ac1f0b842 189
shimniok 21:5d7ac1f0b842 190
shimniok 21:5d7ac1f0b842 191 void SimpleShell::rm(int argc, char **argv)
shimniok 21:5d7ac1f0b842 192 {
shimniok 21:5d7ac1f0b842 193 if (argc >= 2) {
shimniok 21:5d7ac1f0b842 194 for (int i=1; i < argc; i++) {
shimniok 21:5d7ac1f0b842 195 if (remove(canon(argv[i]))) {
shimniok 21:5d7ac1f0b842 196 printf("%s: cannot remove\n", argv[i]);
shimniok 21:5d7ac1f0b842 197 }
shimniok 21:5d7ac1f0b842 198 }
shimniok 21:5d7ac1f0b842 199 } else {
shimniok 21:5d7ac1f0b842 200 puts("usage: rm [file1 [file2 ...]]");
shimniok 21:5d7ac1f0b842 201 }
shimniok 21:5d7ac1f0b842 202 }
shimniok 21:5d7ac1f0b842 203
shimniok 21:5d7ac1f0b842 204
shimniok 21:5d7ac1f0b842 205 void SimpleShell::touch(int argc, char **argv)
shimniok 21:5d7ac1f0b842 206 {
shimniok 21:5d7ac1f0b842 207 FILE *fp;
shimniok 21:5d7ac1f0b842 208
shimniok 21:5d7ac1f0b842 209 if (argc >= 2) {
shimniok 21:5d7ac1f0b842 210 for (int i=1; i < argc; i++) {
shimniok 21:5d7ac1f0b842 211 if ((fp = fopen(canon(argv[i]), "w")) != NULL) {
shimniok 21:5d7ac1f0b842 212 fclose(fp);
shimniok 21:5d7ac1f0b842 213 } else {
shimniok 21:5d7ac1f0b842 214 printf("%s: cannot touch\n", argv[1]);
shimniok 21:5d7ac1f0b842 215 }
shimniok 21:5d7ac1f0b842 216 }
shimniok 21:5d7ac1f0b842 217 } else {
shimniok 21:5d7ac1f0b842 218 puts("usage: touch [file1 [file2 ...]]");
shimniok 21:5d7ac1f0b842 219 }
shimniok 21:5d7ac1f0b842 220 }
shimniok 21:5d7ac1f0b842 221
shimniok 21:5d7ac1f0b842 222
shimniok 21:5d7ac1f0b842 223 void SimpleShell::cat(int argc, char **argv)
shimniok 21:5d7ac1f0b842 224 {
shimniok 21:5d7ac1f0b842 225 FILE *fp;
shimniok 21:5d7ac1f0b842 226 //int status=0;
shimniok 21:5d7ac1f0b842 227 char *buf = new char[MAXBUF];
shimniok 21:5d7ac1f0b842 228
shimniok 21:5d7ac1f0b842 229 for (int i=1; i < argc; i++) {
shimniok 21:5d7ac1f0b842 230 //resolveDirectory(path, arg);
shimniok 21:5d7ac1f0b842 231 if ((fp = fopen(canon(argv[i]), "r")) != NULL) {
shimniok 21:5d7ac1f0b842 232 while (!feof(fp)) {
shimniok 21:5d7ac1f0b842 233 fgets(buf, MAXBUF-1, fp);
shimniok 21:5d7ac1f0b842 234 fputs(buf, stdout);
shimniok 21:5d7ac1f0b842 235 }
shimniok 21:5d7ac1f0b842 236 fclose(fp);
shimniok 21:5d7ac1f0b842 237 } else {
shimniok 21:5d7ac1f0b842 238 fputs(argv[i], stdout);
shimniok 21:5d7ac1f0b842 239 fputs(": No such file\n", stdout);
shimniok 21:5d7ac1f0b842 240 //status = 1;
shimniok 21:5d7ac1f0b842 241 }
shimniok 21:5d7ac1f0b842 242 }
shimniok 21:5d7ac1f0b842 243 delete[] buf;
shimniok 21:5d7ac1f0b842 244
shimniok 21:5d7ac1f0b842 245 return;
shimniok 21:5d7ac1f0b842 246 }
shimniok 21:5d7ac1f0b842 247
shimniok 21:5d7ac1f0b842 248
shimniok 23:b1e49cfcaef6 249 void SimpleShell::send(int argc, char **argv)
shimniok 23:b1e49cfcaef6 250 {
shimniok 23:b1e49cfcaef6 251 const char SOF=0x01;
shimniok 23:b1e49cfcaef6 252 const char ETX=0x03;
shimniok 23:b1e49cfcaef6 253 const char EOT=0x04;
shimniok 23:b1e49cfcaef6 254 const char ACK=0x07;
shimniok 23:b1e49cfcaef6 255 const char NACK=0x18;
shimniok 24:004e33abce37 256 char buf[1024];
shimniok 23:b1e49cfcaef6 257 FILE *fp;
shimniok 24:004e33abce37 258 char c;
shimniok 23:b1e49cfcaef6 259
shimniok 23:b1e49cfcaef6 260 int i = 1;
shimniok 23:b1e49cfcaef6 261 if (argc >= 2) {
shimniok 23:b1e49cfcaef6 262 if ((fp = fopen(canon(argv[i]), "r")) == 0) {
shimniok 23:b1e49cfcaef6 263 printf("%s: can't open file\n", basename(argv[i]));
shimniok 23:b1e49cfcaef6 264 } else {
shimniok 24:004e33abce37 265 puts("Ready; initiate receive on client.");
shimniok 24:004e33abce37 266 c = getchar();
shimniok 24:004e33abce37 267 if (c == SOF) {
shimniok 24:004e33abce37 268 puts(basename(argv[i]));
shimniok 24:004e33abce37 269 c = getchar();
shimniok 24:004e33abce37 270 if (c == ACK) {
shimniok 24:004e33abce37 271 while (!feof(fp)) {
shimniok 24:004e33abce37 272 fgets(buf, 1024, fp);
shimniok 24:004e33abce37 273 fputs(buf, stdout);
shimniok 24:004e33abce37 274 }
shimniok 24:004e33abce37 275 putchar(EOT);
shimniok 24:004e33abce37 276 } else if (c == NACK) {
shimniok 24:004e33abce37 277 puts("client error.");
shimniok 24:004e33abce37 278 } else {
shimniok 24:004e33abce37 279 puts("ACK/NACK expected");
shimniok 24:004e33abce37 280 }
shimniok 24:004e33abce37 281 } else if (c == NACK) {
shimniok 24:004e33abce37 282 puts("server cancelled.");
shimniok 24:004e33abce37 283 } else {
shimniok 24:004e33abce37 284 puts("SOF/NACK expected.");
shimniok 24:004e33abce37 285 }
shimniok 24:004e33abce37 286 fclose(fp);
shimniok 23:b1e49cfcaef6 287 }
shimniok 23:b1e49cfcaef6 288 } else {
shimniok 23:b1e49cfcaef6 289 puts("usage: send file1 [file2 ...]");
shimniok 23:b1e49cfcaef6 290 }
shimniok 23:b1e49cfcaef6 291 }
shimniok 23:b1e49cfcaef6 292
shimniok 23:b1e49cfcaef6 293
shimniok 21:5d7ac1f0b842 294 void SimpleShell::run()
shimniok 21:5d7ac1f0b842 295 {
shimniok 21:5d7ac1f0b842 296 bool done=false;
shimniok 21:5d7ac1f0b842 297 callback_t cb;
shimniok 21:5d7ac1f0b842 298 //int status; // TODO implement command status return
shimniok 21:5d7ac1f0b842 299 std::string x;
shimniok 21:5d7ac1f0b842 300
shimniok 21:5d7ac1f0b842 301 printf("Type help for assistance.\n");
shimniok 21:5d7ac1f0b842 302 help(0, NULL);
shimniok 21:5d7ac1f0b842 303 while (!done) {
shimniok 21:5d7ac1f0b842 304 printPrompt();
shimniok 21:5d7ac1f0b842 305 readCommand();
shimniok 21:5d7ac1f0b842 306 if (argv[0]) { // skip blank command
shimniok 21:5d7ac1f0b842 307 if (cb = findCommand()) {
shimniok 21:5d7ac1f0b842 308 cb.call(argc, argv);
shimniok 21:5d7ac1f0b842 309 } else {
shimniok 21:5d7ac1f0b842 310 printf("command <%s> not found\n", argv[0]);
shimniok 21:5d7ac1f0b842 311 }
shimniok 21:5d7ac1f0b842 312 }
shimniok 21:5d7ac1f0b842 313 }
shimniok 21:5d7ac1f0b842 314 puts("exiting shell\n");
shimniok 21:5d7ac1f0b842 315
shimniok 21:5d7ac1f0b842 316 return;
shimniok 21:5d7ac1f0b842 317 }
shimniok 21:5d7ac1f0b842 318
shimniok 21:5d7ac1f0b842 319
shimniok 28:753db82debb1 320 void SimpleShell::command(callback_t cb, char *command)
shimniok 21:5d7ac1f0b842 321 {
shimniok 21:5d7ac1f0b842 322 if (lookupEnd < MAXLOOKUP) {
shimniok 21:5d7ac1f0b842 323 lookup[lookupEnd].cb = cb;
shimniok 21:5d7ac1f0b842 324 lookup[lookupEnd].command = command;
shimniok 21:5d7ac1f0b842 325 lookupEnd++;
shimniok 21:5d7ac1f0b842 326 }
shimniok 21:5d7ac1f0b842 327
shimniok 21:5d7ac1f0b842 328 return;
shimniok 21:5d7ac1f0b842 329 }
shimniok 21:5d7ac1f0b842 330
shimniok 21:5d7ac1f0b842 331
shimniok 21:5d7ac1f0b842 332 SimpleShell::callback_t SimpleShell::findCommand()
shimniok 21:5d7ac1f0b842 333 {
shimniok 21:5d7ac1f0b842 334 SimpleShell::callback_t cb=NULL;
shimniok 21:5d7ac1f0b842 335
shimniok 21:5d7ac1f0b842 336 for (int i=0; i < lookupEnd; i++) {
shimniok 21:5d7ac1f0b842 337 if (strncmp(argv[0], lookup[i].command, MAXBUF) == 0) {
shimniok 21:5d7ac1f0b842 338 cb = lookup[i].cb;
shimniok 21:5d7ac1f0b842 339 break;
shimniok 21:5d7ac1f0b842 340 }
shimniok 21:5d7ac1f0b842 341 }
shimniok 21:5d7ac1f0b842 342
shimniok 21:5d7ac1f0b842 343 return cb;
shimniok 21:5d7ac1f0b842 344 }
shimniok 21:5d7ac1f0b842 345
shimniok 21:5d7ac1f0b842 346
shimniok 21:5d7ac1f0b842 347 void SimpleShell::printPrompt()
shimniok 21:5d7ac1f0b842 348 {
shimniok 21:5d7ac1f0b842 349 fputc('(', stdout);
shimniok 21:5d7ac1f0b842 350 fputs(_cwd, stdout);
shimniok 21:5d7ac1f0b842 351 fputs(")# ", stdout);
shimniok 21:5d7ac1f0b842 352
shimniok 21:5d7ac1f0b842 353 return;
shimniok 21:5d7ac1f0b842 354 }
shimniok 21:5d7ac1f0b842 355
shimniok 21:5d7ac1f0b842 356
shimniok 21:5d7ac1f0b842 357 void SimpleShell::readCommand()
shimniok 21:5d7ac1f0b842 358 {
shimniok 21:5d7ac1f0b842 359 int i=0;
shimniok 21:5d7ac1f0b842 360 char c;
shimniok 21:5d7ac1f0b842 361 bool done = false;
shimniok 21:5d7ac1f0b842 362 static char cmd[MAXBUF];
shimniok 21:5d7ac1f0b842 363
shimniok 21:5d7ac1f0b842 364 memset(cmd, 0, MAXBUF);
shimniok 21:5d7ac1f0b842 365 do {
shimniok 21:5d7ac1f0b842 366 cmd[i] = 0;
shimniok 21:5d7ac1f0b842 367 c = fgetc(stdin);
shimniok 21:5d7ac1f0b842 368 if (c == '\r') { // if return is hit, we're done, don't add \r to cmd
shimniok 21:5d7ac1f0b842 369 done = true;
shimniok 21:5d7ac1f0b842 370 } else if (c == ESC) { // keyboard escape codes (arrow keys, etc)
shimniok 21:5d7ac1f0b842 371 int c2 = getchar();
shimniok 21:5d7ac1f0b842 372 int c3 = getchar();
shimniok 21:5d7ac1f0b842 373
shimniok 21:5d7ac1f0b842 374 if (c2 == 0x4f && c3 == 0x46) {
shimniok 21:5d7ac1f0b842 375 printf("<END>");
shimniok 21:5d7ac1f0b842 376 } else if (c2 == 0x5b) {
shimniok 21:5d7ac1f0b842 377
shimniok 21:5d7ac1f0b842 378 if (c3 == UP) {
shimniok 21:5d7ac1f0b842 379 printf("<UP>");
shimniok 21:5d7ac1f0b842 380 } else if (c3 == DOWN) {
shimniok 21:5d7ac1f0b842 381 printf("<DOWN>");
shimniok 21:5d7ac1f0b842 382 } else if (c3 == RIGHT) {
shimniok 21:5d7ac1f0b842 383 printf("<RIGHT>");
shimniok 21:5d7ac1f0b842 384 } else if (c3 == LEFT) {
shimniok 21:5d7ac1f0b842 385 printf("<LEFT>");
shimniok 21:5d7ac1f0b842 386 } else if (c3 == HOME || c3 == INS || c3 == DEL ||
shimniok 21:5d7ac1f0b842 387 c3 == PGUP || c3 == PGDN) {
shimniok 21:5d7ac1f0b842 388 char c4 = getchar();
shimniok 21:5d7ac1f0b842 389 if (c4 == TAIL) {
shimniok 21:5d7ac1f0b842 390 if (c4 == HOME) {
shimniok 21:5d7ac1f0b842 391 printf("<HOME>");
shimniok 21:5d7ac1f0b842 392 } else if (c4 == INS) {
shimniok 21:5d7ac1f0b842 393 printf("<INS>");
shimniok 21:5d7ac1f0b842 394 } else if (c4 == DEL) {
shimniok 21:5d7ac1f0b842 395 printf("<DEL>");
shimniok 21:5d7ac1f0b842 396 } else if (c4 == PGUP) {
shimniok 21:5d7ac1f0b842 397 printf("<PGUP>");
shimniok 21:5d7ac1f0b842 398 } else if (c4 == PGDN) {
shimniok 21:5d7ac1f0b842 399 printf("<PGDN>");
shimniok 21:5d7ac1f0b842 400 }//if
shimniok 21:5d7ac1f0b842 401 }//if
shimniok 21:5d7ac1f0b842 402 }//if
shimniok 21:5d7ac1f0b842 403 }//if
shimniok 21:5d7ac1f0b842 404 //printf("\n");
shimniok 21:5d7ac1f0b842 405 } else if (i < MAXBUF-1) {
shimniok 21:5d7ac1f0b842 406 if (c == 0x7f || c == '\b') { // backspace or delete
shimniok 21:5d7ac1f0b842 407 if (i > 0) { // if we're at the beginning, do nothing
shimniok 21:5d7ac1f0b842 408 i--;
shimniok 21:5d7ac1f0b842 409 fputs("\b \b", stdout);
shimniok 21:5d7ac1f0b842 410 }
shimniok 21:5d7ac1f0b842 411 } else {
shimniok 21:5d7ac1f0b842 412 if (isprint(c))
shimniok 21:5d7ac1f0b842 413 fputc(c, stdout);
shimniok 21:5d7ac1f0b842 414 cmd[i++] = c;
shimniok 21:5d7ac1f0b842 415 }
shimniok 21:5d7ac1f0b842 416 }
shimniok 21:5d7ac1f0b842 417
shimniok 21:5d7ac1f0b842 418 } while (!done);
shimniok 21:5d7ac1f0b842 419 fputc('\n', stdout);
shimniok 21:5d7ac1f0b842 420
shimniok 21:5d7ac1f0b842 421 // remove leading/trailing whitespace
shimniok 21:5d7ac1f0b842 422 char *s = cmd;
shimniok 21:5d7ac1f0b842 423 while (isspace(*s)) {
shimniok 21:5d7ac1f0b842 424 s++;
shimniok 21:5d7ac1f0b842 425 }
shimniok 21:5d7ac1f0b842 426 for (int j=i; j >= 0 && isspace(cmd[j]); j--) {
shimniok 21:5d7ac1f0b842 427 cmd[j] = '\0';
shimniok 21:5d7ac1f0b842 428 }
shimniok 21:5d7ac1f0b842 429
shimniok 21:5d7ac1f0b842 430 // split into command and arguments
shimniok 21:5d7ac1f0b842 431 argc = 0;
shimniok 21:5d7ac1f0b842 432 char *t;
shimniok 21:5d7ac1f0b842 433
shimniok 21:5d7ac1f0b842 434 for (int i=0; i < MAXARGS; i++) {
shimniok 21:5d7ac1f0b842 435 argv[i] = NULL;
shimniok 21:5d7ac1f0b842 436 }
shimniok 21:5d7ac1f0b842 437 t = strtok(s, " ");
shimniok 30:35522ea06236 438 while (t && argc < MAXARGS) {
shimniok 21:5d7ac1f0b842 439 argv[argc++] = t;
shimniok 21:5d7ac1f0b842 440 t = strtok(NULL, " ");
shimniok 21:5d7ac1f0b842 441 }
shimniok 21:5d7ac1f0b842 442
shimniok 21:5d7ac1f0b842 443 return;
shimniok 21:5d7ac1f0b842 444 }