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 usage(BaseSequentialStream *chp, char *p) {
|
2009-12-10 15:44:55 +00:00
|
|
|
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "Usage: %s\r\n", p);
|
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-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;
|
2012-05-08 17:09:20 +00:00
|
|
|
BaseSequentialStream *chp = ((ShellConfig *)p)->sc_channel;
|
2009-12-10 15:44:55 +00:00
|
|
|
const ShellCommand *scp = ((ShellConfig *)p)->sc_commands;
|
|
|
|
char *lp, *cmd, *tokp, line[SHELL_MAX_LINE_LENGTH];
|
|
|
|
char *args[SHELL_MAX_ARGUMENTS + 1];
|
|
|
|
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "\r\nChibiOS/RT Shell\r\n");
|
2015-04-03 12:48:22 +00:00
|
|
|
while (true) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "ch> ");
|
2009-12-10 15:44:55 +00:00
|
|
|
if (shellGetLine(chp, line, sizeof(line))) {
|
2016-03-22 14:02:36 +00:00
|
|
|
#if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "\r\nlogout");
|
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) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "too many arguments\r\n");
|
2009-12-10 15:44:55 +00:00
|
|
|
cmd = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
args[n++] = lp;
|
|
|
|
}
|
|
|
|
args[n] = NULL;
|
|
|
|
if (cmd != NULL) {
|
2015-11-20 10:02:30 +00:00
|
|
|
if (strcmp(cmd, "exit") == 0) {
|
2011-06-28 06:29:04 +00:00
|
|
|
if (n > 0) {
|
2009-12-10 15:44:55 +00:00
|
|
|
usage(chp, "exit");
|
2011-06-28 06:29:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-12-10 15:44:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-20 10:02:30 +00:00
|
|
|
else if (strcmp(cmd, "help") == 0) {
|
2011-06-28 06:29:04 +00:00
|
|
|
if (n > 0) {
|
2009-12-10 15:44:55 +00:00
|
|
|
usage(chp, "help");
|
2011-06-28 06:29:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
chprintf(chp, "Commands: help exit ");
|
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);
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "\r\n");
|
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);
|
|
|
|
chprintf(chp, " ?\r\n");
|
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
|
|
|
*
|
2012-05-08 17:09:20 +00:00
|
|
|
* @param[in] chp pointer to a @p BaseSequentialStream 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
|
|
|
*/
|
2013-09-05 09:01:21 +00:00
|
|
|
bool shellGetLine(BaseSequentialStream *chp, char *line, unsigned size) {
|
2009-12-10 15:44:55 +00:00
|
|
|
char *p = line;
|
|
|
|
|
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-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') {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "\r\n");
|
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
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|