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