2009-12-10 15:44:55 +00:00
|
|
|
/*
|
2016-03-18 10:29:35 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2013-05-11 06:08:17 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-06-28 06:29:04 +00:00
|
|
|
* @file shell.c
|
|
|
|
* @brief Simple CLI shell code.
|
|
|
|
*
|
2009-12-10 15:44:55 +00:00
|
|
|
* @addtogroup SHELL
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ch.h"
|
2010-02-22 16:29:48 +00:00
|
|
|
#include "hal.h"
|
2009-12-10 15:44:55 +00:00
|
|
|
#include "shell.h"
|
2016-02-20 13:51:02 +00:00
|
|
|
#include "shell_cmd.h"
|
2011-06-28 06:29:04 +00:00
|
|
|
#include "chprintf.h"
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2016-02-20 13:51:02 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-12-10 15:44:55 +00:00
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Shell termination event source.
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
2013-08-25 09:37:34 +00:00
|
|
|
event_source_t shell_terminated;
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2016-02-20 13:51:02 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2016-02-20 14:32:35 +00:00
|
|
|
static char *parse_arguments(char *str, char **saveptr) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (str != NULL)
|
2009-12-10 15:44:55 +00:00
|
|
|
*saveptr = str;
|
|
|
|
|
2016-02-20 14:32:35 +00:00
|
|
|
p = *saveptr;
|
|
|
|
if (!p) {
|
2009-12-10 15:44:55 +00:00
|
|
|
return NULL;
|
2016-02-20 14:32:35 +00:00
|
|
|
}
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2016-02-20 14:32:35 +00:00
|
|
|
/* Skipping white space.*/
|
|
|
|
p += strspn(p, " \t");
|
|
|
|
|
|
|
|
if (*p == '"') {
|
|
|
|
/* If an argument starts with a double quote then its delimiter is another
|
|
|
|
quote.*/
|
|
|
|
p++;
|
|
|
|
*saveptr = strpbrk(p, "\"");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* The delimiter is white space.*/
|
|
|
|
*saveptr = strpbrk(p, " \t");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replacing the delimiter with a zero.*/
|
|
|
|
if (*saveptr != NULL) {
|
2009-12-10 15:44:55 +00:00
|
|
|
*(*saveptr)++ = '\0';
|
2016-02-20 14:32:35 +00:00
|
|
|
}
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2016-02-20 14:32:35 +00:00
|
|
|
return *p != '\0' ? p : NULL;
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
2012-05-08 17:09:20 +00:00
|
|
|
static void list_commands(BaseSequentialStream *chp, const ShellCommand *scp) {
|
2009-12-10 15:44:55 +00:00
|
|
|
|
|
|
|
while (scp->sc_name != NULL) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "%s ", scp->sc_name);
|
2009-12-10 15:44:55 +00:00
|
|
|
scp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 09:01:21 +00:00
|
|
|
static bool cmdexec(const ShellCommand *scp, BaseSequentialStream *chp,
|
2009-12-10 15:44:55 +00:00
|
|
|
char *name, int argc, char *argv[]) {
|
|
|
|
|
|
|
|
while (scp->sc_name != NULL) {
|
2015-11-20 10:02:30 +00:00
|
|
|
if (strcmp(scp->sc_name, name) == 0) {
|
2009-12-10 15:44:55 +00:00
|
|
|
scp->sc_function(chp, argc, argv);
|
2015-04-03 12:48:22 +00:00
|
|
|
return false;
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
scp++;
|
|
|
|
}
|
2015-04-03 12:48:22 +00:00
|
|
|
return true;
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 12:14:23 +00:00
|
|
|
#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 (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 {
|
|
|
|
/*
|
|
|
|
* Since there isn't enough room left at the end of the buffer,
|
|
|
|
* split the line to save up to the end of the buffer and then
|
|
|
|
* wrap back to the beginning of the buffer.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the length of the current line and move the buffer end pointer */
|
|
|
|
*(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;
|
|
|
|
|
|
|
|
/* Count the number of lines saved in the buffer */
|
|
|
|
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 {
|
|
|
|
/*
|
|
|
|
* Since the saved line was split at the end of the buffer,
|
|
|
|
* get the line in two parts.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
2016-04-10 12:21:53 +00:00
|
|
|
#if (SHELL_USE_COMPLETION == TRUE) || defined(__DOXYGEN__)
|
2016-04-10 12:14:23 +00:00
|
|
|
static void get_completions(ShellConfig *scfg, char *line) {
|
|
|
|
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
|
|
|
|
|
2016-02-20 13:51:02 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-12-10 15:44:55 +00:00
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Shell thread function.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2012-05-08 17:09:20 +00:00
|
|
|
* @param[in] p pointer to a @p BaseSequentialStream object
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
2016-02-19 16:32:09 +00:00
|
|
|
THD_FUNCTION(shellThread, p) {
|
2009-12-10 15:44:55 +00:00
|
|
|
int n;
|
2016-04-10 12:14:23 +00:00
|
|
|
ShellConfig *scfg = p;
|
2016-04-10 12:21:53 +00:00
|
|
|
BaseSequentialStream *chp = scfg->sc_channel;
|
|
|
|
const ShellCommand *scp = scfg->sc_commands;
|
2009-12-10 15:44:55 +00:00
|
|
|
char *lp, *cmd, *tokp, line[SHELL_MAX_LINE_LENGTH];
|
|
|
|
char *args[SHELL_MAX_ARGUMENTS + 1];
|
|
|
|
|
2016-04-10 12:14:23 +00:00
|
|
|
chprintf(chp, SHELL_NEWLINE_STR);
|
|
|
|
chprintf(chp, "ChibiOS/RT Shell"SHELL_NEWLINE_STR);
|
2015-04-03 12:48:22 +00:00
|
|
|
while (true) {
|
2016-04-10 12:14:23 +00:00
|
|
|
chprintf(chp, SHELL_PROMPT_STR);
|
|
|
|
if (shellGetLine(scfg, line, sizeof(line))) {
|
2016-03-22 14:02:36 +00:00
|
|
|
#if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
|
2016-04-10 12:14:23 +00:00
|
|
|
chprintf(chp, SHELL_NEWLINE_STR);
|
|
|
|
chprintf(chp, "logout");
|
2009-12-10 15:44:55 +00:00
|
|
|
break;
|
2016-03-22 14:02:36 +00:00
|
|
|
#else
|
|
|
|
/* Putting a delay in order to avoid an endless loop trying to read
|
|
|
|
an unavailable stream.*/
|
|
|
|
osalThreadSleepMilliseconds(100);
|
|
|
|
#endif
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
2016-02-20 14:32:35 +00:00
|
|
|
lp = parse_arguments(line, &tokp);
|
2009-12-10 15:44:55 +00:00
|
|
|
cmd = lp;
|
|
|
|
n = 0;
|
2016-02-20 14:32:35 +00:00
|
|
|
while ((lp = parse_arguments(NULL, &tokp)) != NULL) {
|
2009-12-10 15:44:55 +00:00
|
|
|
if (n >= SHELL_MAX_ARGUMENTS) {
|
2016-04-10 12:14:23 +00:00
|
|
|
chprintf(chp, "too many arguments"SHELL_NEWLINE_STR);
|
2009-12-10 15:44:55 +00:00
|
|
|
cmd = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
args[n++] = lp;
|
|
|
|
}
|
|
|
|
args[n] = NULL;
|
|
|
|
if (cmd != NULL) {
|
2016-04-10 12:01:22 +00:00
|
|
|
if (strcmp(cmd, "help") == 0) {
|
2011-06-28 06:29:04 +00:00
|
|
|
if (n > 0) {
|
2016-04-10 12:14:23 +00:00
|
|
|
shellUsage(chp, "help");
|
2011-06-28 06:29:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-10 12:01:22 +00:00
|
|
|
chprintf(chp, "Commands: help ");
|
2016-02-20 13:51:02 +00:00
|
|
|
list_commands(chp, shell_local_commands);
|
2009-12-10 15:44:55 +00:00
|
|
|
if (scp != NULL)
|
|
|
|
list_commands(chp, scp);
|
2016-04-10 12:14:23 +00:00
|
|
|
chprintf(chp, SHELL_NEWLINE_STR);
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
2016-02-20 13:51:02 +00:00
|
|
|
else if (cmdexec(shell_local_commands, chp, cmd, n, args) &&
|
2009-12-10 15:44:55 +00:00
|
|
|
((scp == NULL) || cmdexec(scp, chp, cmd, n, args))) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "%s", cmd);
|
2016-04-10 12:14:23 +00:00
|
|
|
chprintf(chp, " ?"SHELL_NEWLINE_STR);
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 12:49:49 +00:00
|
|
|
shellExit(MSG_OK);
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Shell manager initialization.
|
2013-10-30 12:49:49 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
|
|
|
void shellInit(void) {
|
|
|
|
|
2013-08-25 09:37:34 +00:00
|
|
|
chEvtObjectInit(&shell_terminated);
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 14:02:36 +00:00
|
|
|
#if !defined(_CHIBIOS_NIL_) || defined(__DOXYGEN__)
|
2013-10-30 12:49:49 +00:00
|
|
|
/**
|
|
|
|
* @brief Terminates the shell.
|
|
|
|
* @note Must be invoked from the command handlers.
|
|
|
|
* @note Does not return.
|
|
|
|
*
|
|
|
|
* @param[in] msg shell exit code
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
void shellExit(msg_t msg) {
|
|
|
|
|
|
|
|
/* Atomically broadcasting the event source and terminating the thread,
|
|
|
|
there is not a chSysUnlock() because the thread terminates upon return.*/
|
|
|
|
chSysLock();
|
|
|
|
chEvtBroadcastI(&shell_terminated);
|
|
|
|
chThdExitS(msg);
|
|
|
|
}
|
2016-03-22 14:02:36 +00:00
|
|
|
#endif
|
2013-10-30 12:49:49 +00:00
|
|
|
|
2009-12-10 15:44:55 +00:00
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Reads a whole line from the input channel.
|
2016-02-19 16:32:09 +00:00
|
|
|
* @note Input chars are echoed on the same stream object with the
|
|
|
|
* following exceptions:
|
|
|
|
* - DEL and BS are echoed as BS-SPACE-BS.
|
|
|
|
* - CR is echoed as CR-LF.
|
|
|
|
* - 0x4 is echoed as "^D".
|
|
|
|
* - Other values below 0x20 are not echoed.
|
|
|
|
* .
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2016-04-10 12:14:23 +00:00
|
|
|
* @param[in] scfg pointer to a @p ShellConfig object
|
2011-09-23 15:48:55 +00:00
|
|
|
* @param[in] line pointer to the line buffer
|
|
|
|
* @param[in] size buffer maximum length
|
|
|
|
* @return The operation status.
|
2015-04-03 12:48:22 +00:00
|
|
|
* @retval true the channel was reset or CTRL-D pressed.
|
|
|
|
* @retval false operation successful.
|
2013-10-30 12:49:49 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
2016-04-10 12:14:23 +00:00
|
|
|
bool shellGetLine(ShellConfig *scfg, char *line, unsigned size) {
|
2009-12-10 15:44:55 +00:00
|
|
|
char *p = line;
|
2016-04-10 12:14:23 +00:00
|
|
|
BaseSequentialStream *chp = scfg->sc_channel;
|
2016-04-10 12:21:53 +00:00
|
|
|
#if (SHELL_USE_ESC_SEQ == TRUE)
|
2016-04-10 12:14:23 +00:00
|
|
|
bool escape = false;
|
|
|
|
bool bracket = false;
|
2016-04-10 12:21:53 +00:00
|
|
|
#endif
|
2016-04-10 12:14:23 +00:00
|
|
|
|
|
|
|
#if (SHELL_USE_HISTORY == TRUE)
|
|
|
|
ShellHistory *shp = scfg->sc_history;
|
|
|
|
#endif
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2015-04-03 12:48:22 +00:00
|
|
|
while (true) {
|
2012-05-08 17:09:20 +00:00
|
|
|
char c;
|
|
|
|
|
2016-03-22 14:02:36 +00:00
|
|
|
if (streamRead(chp, (uint8_t *)&c, 1) == 0)
|
2015-04-03 12:48:22 +00:00
|
|
|
return true;
|
2016-04-10 12:21:53 +00:00
|
|
|
#if SHELL_USE_ESC_SEQ == TRUE
|
2016-04-10 12:14:23 +00:00
|
|
|
if (c == 27) {
|
|
|
|
escape = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (escape) {
|
|
|
|
escape = false;
|
|
|
|
if (c == '[') {
|
|
|
|
escape = true;
|
|
|
|
bracket = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bracket) {
|
|
|
|
bracket = false;
|
2016-04-10 12:21:53 +00:00
|
|
|
#if SHELL_USE_HISTORY == TRUE
|
2016-04-10 12:14:23 +00:00
|
|
|
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
|
2016-03-22 14:02:36 +00:00
|
|
|
#if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
|
2009-12-10 15:44:55 +00:00
|
|
|
if (c == 4) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "^D");
|
2015-04-03 12:48:22 +00:00
|
|
|
return true;
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
2016-03-22 14:02:36 +00:00
|
|
|
#endif
|
2014-11-01 09:28:17 +00:00
|
|
|
if ((c == 8) || (c == 127)) {
|
2009-12-10 15:44:55 +00:00
|
|
|
if (p != line) {
|
2016-03-22 14:02:36 +00:00
|
|
|
streamPut(chp, c);
|
|
|
|
streamPut(chp, 0x20);
|
|
|
|
streamPut(chp, c);
|
2009-12-10 15:44:55 +00:00
|
|
|
p--;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '\r') {
|
2016-04-10 12:14:23 +00:00
|
|
|
chprintf(chp, SHELL_NEWLINE_STR);
|
2016-04-10 12:21:53 +00:00
|
|
|
#if SHELL_USE_HISTORY == TRUE
|
2016-04-10 12:14:23 +00:00
|
|
|
if (shp != NULL) {
|
|
|
|
save_history(shp, line, p - line);
|
|
|
|
}
|
|
|
|
#endif
|
2009-12-10 15:44:55 +00:00
|
|
|
*p = 0;
|
2015-04-03 12:48:22 +00:00
|
|
|
return false;
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
2016-04-10 12:21:53 +00:00
|
|
|
#if SHELL_USE_COMPLETION == TRUE
|
2016-04-10 12:14:23 +00:00
|
|
|
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
|
2016-04-10 12:21:53 +00:00
|
|
|
#if SHELL_USE_HISTORY == TRUE
|
2016-04-10 12:14:23 +00:00
|
|
|
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
|
2009-12-10 15:44:55 +00:00
|
|
|
if (c < 0x20)
|
|
|
|
continue;
|
|
|
|
if (p < line + size - 1) {
|
2016-03-22 14:02:36 +00:00
|
|
|
streamPut(chp, c);
|
2009-12-10 15:44:55 +00:00
|
|
|
*p++ = (char)c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|