123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 |
- #include <string.h>
- #include "ch.h"
- #include "hal.h"
- #include "shell.h"
- #include "shell_cmd.h"
- #include "chprintf.h"
- #if !defined(_CHIBIOS_NIL_) || defined(__DOXYGEN__)
- event_source_t shell_terminated;
- #endif
- static char *parse_arguments(char *str, char **saveptr) {
- char *p;
- if (str != NULL)
- *saveptr = str;
- p = *saveptr;
- if (!p) {
- return NULL;
- }
-
- p += strspn(p, " \t");
- if (*p == '"') {
-
- p++;
- *saveptr = strpbrk(p, "\"");
- }
- else {
-
- *saveptr = strpbrk(p, " \t");
- }
-
- if (*saveptr != NULL) {
- *(*saveptr)++ = '\0';
- }
- return *p != '\0' ? p : NULL;
- }
- static void list_commands(BaseSequentialStream *chp, const ShellCommand *scp) {
- while (scp->sc_name != NULL) {
- chprintf(chp, "%s ", scp->sc_name);
- scp++;
- }
- }
- static bool cmdexec(const ShellCommand *scp, BaseSequentialStream *chp,
- char *name, int argc, char *argv[]) {
- while (scp->sc_name != NULL) {
- if (strcmp(scp->sc_name, name) == 0) {
- scp->sc_function(chp, argc, argv);
- return false;
- }
- scp++;
- }
- return true;
- }
- #if (SHELL_USE_HISTORY == TRUE) || defined(__DOXYGEN__)
- static void del_histbuff_entry(ShellHistory *shp) {
- int pos = shp->sh_beg + *(shp->sh_buffer + shp->sh_beg) + 1;
- if (pos >= shp->sh_size)
- pos -= shp->sh_size;
- shp->sh_beg = pos;
- }
- static bool is_histbuff_space(ShellHistory *shp, int length) {
- if (shp->sh_end >= shp->sh_beg) {
- if (length < (shp->sh_size - (shp->sh_end - shp->sh_beg + 1)))
- return true;
- }
- else {
- if (length < (shp->sh_beg - shp->sh_end - 1))
- return true;
- }
- return false;
- }
- static void save_history(ShellHistory *shp, char *line, int length) {
- if (shp == NULL)
- return;
- if (length > shp->sh_size - 2)
- return;
- while ((*(line + length -1) == ' ') && (length > 0))
- length--;
- if (length <= 0)
- return;
- while (!is_histbuff_space(shp, length))
- del_histbuff_entry(shp);
- if (length < shp->sh_size - shp->sh_end - 1)
- memcpy(shp->sh_buffer + shp->sh_end + 1, line, length);
- else {
-
- int part_len = shp->sh_size - shp->sh_end - 1;
- memcpy(shp->sh_buffer + shp->sh_end + 1, line, part_len);
- memcpy(shp->sh_buffer, line + part_len, length - part_len);
- }
-
- *(shp->sh_buffer + shp->sh_end) = (char)length;
- shp->sh_end += length + 1;
- if (shp->sh_end >= shp->sh_size)
- shp->sh_end -= shp->sh_size;
- *(shp->sh_buffer + shp->sh_end) = 0;
- shp->sh_cur = 0;
- }
- static int get_history(ShellHistory *shp, char *line, int dir) {
- int count=0;
- if (shp == NULL)
- return -1;
-
- int idx = shp->sh_beg;
- while (idx != shp->sh_end) {
- idx += *(shp->sh_buffer + idx) + 1;
- if (idx >= shp->sh_size)
- idx -= shp->sh_size;
- count++;
- }
- if (dir == SHELL_HIST_DIR_FW) {
- if (shp->sh_cur > 0)
- shp->sh_cur -= 2;
- else
- return 0;
- }
- if (count >= shp->sh_cur) {
- idx = shp->sh_beg;
- int i = 0;
- while (idx != shp->sh_end && shp->sh_cur != (count - i - 1)) {
- idx += *(shp->sh_buffer + idx) + 1;
- if (idx >= shp->sh_size)
- idx -= shp->sh_size;
- i++;
- }
- int length = *(shp->sh_buffer + idx);
- if (length > 0) {
- shp->sh_cur++;
- memset(line, 0, SHELL_MAX_LINE_LENGTH);
- if ((idx + length) < shp->sh_size) {
- memcpy(line, (shp->sh_buffer + idx + 1), length);
- }
- else {
-
- int part_len = shp->sh_size - idx - 1;
- memcpy(line, shp->sh_buffer + idx + 1, part_len);
- memcpy(line + part_len, shp->sh_buffer, length - part_len);
- }
- return length;
- }
- else if (dir == SHELL_HIST_DIR_FW) {
- shp->sh_cur++;
- return 0;
- }
- }
- return -1;
- }
- #endif
- #if (SHELL_USE_COMPLETION == TRUE) || defined(__DOXYGEN__)
- static void get_completions(ShellConfig *scfg, char *line) {
- const ShellCommand *lcp = shell_local_commands;
- const ShellCommand *scp = scfg->sc_commands;
- char **scmp = scfg->sc_completion;
- char help_cmp[] = "help";
- if (strstr(help_cmp, line) == help_cmp) {
- *scmp++ = help_cmp;
- }
- while (lcp->sc_name != NULL) {
- if (strstr(lcp->sc_name, line) == lcp->sc_name) {
- *scmp++ = (char *)lcp->sc_name;
- }
- lcp++;
- }
- if (scp != NULL) {
- while (scp->sc_name != NULL) {
- if (strstr(scp->sc_name, line) == scp->sc_name) {
- *scmp++ = (char *)scp->sc_name;
- }
- scp++;
- }
- }
- *scmp = NULL;
- }
- static int process_completions(ShellConfig *scfg, char *line, int length, unsigned size) {
- char **scmp = scfg->sc_completion;
- char **cmp = scmp + 1;
- char *c = line + length;
- int clen = 0;
- if (*scmp != NULL) {
- if (*cmp == NULL) {
- clen = strlen(*scmp);
- int i = length;
- while ((c < line + clen) && (c < line + size - 1))
- *c++ = *(*scmp + i++);
- if (c < line + size -1) {
- *c = ' ';
- clen++;
- }
- }
- else {
- while (*(*scmp + clen) != 0) {
- while ((*(*scmp + clen) == *(*cmp + clen)) &&
- (*(*cmp + clen) != 0) && (*cmp != NULL)) {
- cmp++;
- }
- if (*cmp == NULL) {
- if ((c < line + size - 1) && (clen >= length))
- *c++ = *(*scmp + clen);
- cmp = scmp + 1;
- clen++;
- }
- else {
- break;
- }
- }
- }
- *(line + clen) = 0;
- }
- return clen;
- }
- static void write_completions(ShellConfig *scfg, char *line, int pos) {
- BaseSequentialStream *chp = scfg->sc_channel;
- char **scmp = scfg->sc_completion;
- if (*(scmp + 1) != NULL) {
- chprintf(chp, SHELL_NEWLINE_STR);
- while (*scmp != NULL)
- chprintf(chp, " %s", *scmp++);
- chprintf(chp, SHELL_NEWLINE_STR);
- chprintf(chp, SHELL_PROMPT_STR);
- chprintf(chp, "%s", line);
- }
- else {
- chprintf(chp, "%s", line + pos);
- }
- }
- #endif
- THD_FUNCTION(shellThread, p) {
- int n;
- ShellConfig *scfg = p;
- BaseSequentialStream *chp = scfg->sc_channel;
- const ShellCommand *scp = scfg->sc_commands;
- char *lp, *cmd, *tokp, line[SHELL_MAX_LINE_LENGTH];
- char *args[SHELL_MAX_ARGUMENTS + 1];
- #if SHELL_USE_HISTORY == TRUE
- *(scfg->sc_histbuf) = 0;
- ShellHistory hist = {
- scfg->sc_histbuf,
- scfg->sc_histsize,
- 0,
- 0,
- 0
- };
- ShellHistory *shp = &hist;
- #else
- ShellHistory *shp = NULL;
- #endif
- chprintf(chp, SHELL_NEWLINE_STR);
- chprintf(chp, "ChibiOS/RT Shell" SHELL_NEWLINE_STR);
- #if !defined(_CHIBIOS_NIL_)
- while (!chThdShouldTerminateX()) {
- #else
- while (true) {
- #endif
- chprintf(chp, SHELL_PROMPT_STR);
- if (shellGetLine(scfg, line, sizeof(line), shp)) {
- #if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
- chprintf(chp, SHELL_NEWLINE_STR);
- chprintf(chp, "logout");
- break;
- #else
-
- osalThreadSleepMilliseconds(100);
- #endif
- }
- lp = parse_arguments(line, &tokp);
- cmd = lp;
- n = 0;
- while ((lp = parse_arguments(NULL, &tokp)) != NULL) {
- if (n >= SHELL_MAX_ARGUMENTS) {
- chprintf(chp, "too many arguments" SHELL_NEWLINE_STR);
- cmd = NULL;
- break;
- }
- args[n++] = lp;
- }
- args[n] = NULL;
- if (cmd != NULL) {
- if (strcmp(cmd, "help") == 0) {
- if (n > 0) {
- shellUsage(chp, "help");
- continue;
- }
- chprintf(chp, "Commands: help ");
- list_commands(chp, shell_local_commands);
- if (scp != NULL)
- list_commands(chp, scp);
- chprintf(chp, SHELL_NEWLINE_STR);
- }
- else if (cmdexec(shell_local_commands, chp, cmd, n, args) &&
- ((scp == NULL) || cmdexec(scp, chp, cmd, n, args))) {
- chprintf(chp, "%s", cmd);
- chprintf(chp, " ?" SHELL_NEWLINE_STR);
- }
- }
- }
- #if !defined(_CHIBIOS_NIL_)
- shellExit(MSG_OK);
- #endif
- }
- void shellInit(void) {
- #if !defined(_CHIBIOS_NIL_)
- chEvtObjectInit(&shell_terminated);
- #endif
- }
- #if !defined(_CHIBIOS_NIL_) || defined(__DOXYGEN__)
- void shellExit(msg_t msg) {
-
- chSysLock();
- chEvtBroadcastI(&shell_terminated);
- chThdExitS(msg);
- }
- #endif
- bool shellGetLine(ShellConfig *scfg, char *line, unsigned size, ShellHistory *shp) {
- char *p = line;
- BaseSequentialStream *chp = scfg->sc_channel;
- #if SHELL_USE_ESC_SEQ == TRUE
- bool escape = false;
- bool bracket = false;
- #endif
- #if SHELL_USE_HISTORY != TRUE
- (void) shp;
- #endif
- while (true) {
- char c;
- if (streamRead(chp, (uint8_t *)&c, 1) == 0)
- return true;
- #if SHELL_USE_ESC_SEQ == TRUE
- if (c == 27) {
- escape = true;
- continue;
- }
- if (escape) {
- escape = false;
- if (c == '[') {
- escape = true;
- bracket = true;
- continue;
- }
- if (bracket) {
- bracket = false;
- #if SHELL_USE_HISTORY == TRUE
- if (c == 'A') {
- int len = get_history(shp, line, SHELL_HIST_DIR_BK);
- if (len > 0) {
- _shell_reset_cur(chp);
- _shell_clr_line(chp);
- chprintf(chp, "%s", line);
- p = line + len;
- }
- continue;
- }
- if (c == 'B') {
- int len = get_history(shp, line, SHELL_HIST_DIR_FW);
- if (len == 0)
- *line = 0;
- if (len >= 0) {
- _shell_reset_cur(chp);
- _shell_clr_line(chp);
- chprintf(chp, "%s", line);
- p = line + len;
- }
- continue;
- }
- #endif
- }
- continue;
- }
- #endif
- #if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
- if (c == 4) {
- chprintf(chp, "^D");
- return true;
- }
- #endif
- if ((c == 8) || (c == 127)) {
- if (p != line) {
- streamPut(chp, 0x08);
- streamPut(chp, 0x20);
- streamPut(chp, 0x08);
- p--;
- }
- continue;
- }
- if (c == '\r') {
- chprintf(chp, SHELL_NEWLINE_STR);
- #if SHELL_USE_HISTORY == TRUE
- save_history(shp, line, p - line);
- #endif
- *p = 0;
- return false;
- }
- #if SHELL_USE_COMPLETION == TRUE
- if (c == '\t') {
- if (p < line + size - 1) {
- *p = 0;
- get_completions(scfg, line);
- int len = process_completions(scfg, line, p - line, size);
- if (len > 0) {
- write_completions(scfg, line, p - line);
- p = line + len;
- }
- }
- continue;
- }
- #endif
- #if SHELL_USE_HISTORY == TRUE
- if (c == 14) {
- int len = get_history(shp, line, SHELL_HIST_DIR_FW);
- if (len == 0)
- *line = 0;
- if (len >= 0) {
- _shell_reset_cur(chp);
- _shell_clr_line(chp);
- chprintf(chp, "%s", line);
- p = line + len;
- }
- continue;
- }
- if (c == 16) {
- int len = get_history(shp, line, SHELL_HIST_DIR_BK);
- if (len > 0) {
- _shell_reset_cur(chp);
- _shell_clr_line(chp);
- chprintf(chp, "%s", line);
- p = line + len;
- }
- continue;
- }
- #endif
- if (c < 0x20)
- continue;
- if (p < line + size - 1) {
- streamPut(chp, c);
- *p++ = (char)c;
- }
- }
- }
|