2015-02-12 18:06:34 +01:00
|
|
|
/*
|
2018-12-18 19:01:46 +01:00
|
|
|
* Copyright (C) 2009, 2020 Freie Universität Berlin
|
2013-06-22 05:11:53 +02:00
|
|
|
* Copyright (C) 2013, INRIA.
|
2015-07-14 18:18:28 +02:00
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
2013-06-22 05:11:53 +02:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-06-22 05:11:53 +02:00
|
|
|
*/
|
2010-09-22 17:25:19 +02:00
|
|
|
|
|
|
|
/**
|
2015-02-12 18:06:34 +01:00
|
|
|
* @ingroup sys_shell
|
2010-09-22 17:25:19 +02:00
|
|
|
* @{
|
2015-02-12 18:06:34 +01:00
|
|
|
*
|
2010-09-22 17:25:19 +02:00
|
|
|
* @file
|
2014-03-10 18:15:17 +01:00
|
|
|
* @brief Implementation of a very simple command interpreter.
|
2010-09-22 17:25:19 +02:00
|
|
|
* For each command (i.e. "echo"), a handler can be specified.
|
|
|
|
* If the first word of a user-entered command line matches the
|
|
|
|
* name of a handler, the handler will be called with the whole
|
|
|
|
* command line as parameter.
|
|
|
|
*
|
2014-03-10 18:15:17 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* @author René Kijewski <rene.kijewski@fu-berlin.de>
|
2018-12-18 19:01:46 +01:00
|
|
|
* @author Juan Carrano <j.carrano@fu-berlin.de>
|
|
|
|
* @author Hendrik van Essen <hendrik.ve@fu-berlin.de>
|
2015-02-12 18:06:34 +01:00
|
|
|
*
|
|
|
|
* @}
|
2010-09-22 17:25:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2010-11-04 19:06:46 +01:00
|
|
|
#include <stdint.h>
|
2010-12-03 22:22:58 +01:00
|
|
|
#include <stdlib.h>
|
2018-12-18 19:01:46 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "shell.h"
|
|
|
|
#include "shell_commands.h"
|
2010-09-22 17:25:19 +02:00
|
|
|
|
2019-02-07 12:39:11 +01:00
|
|
|
#define ETX '\x03' /** ASCII "End-of-Text", or ctrl-C */
|
2020-02-08 12:52:46 +01:00
|
|
|
#define BS '\x08' /** ASCII "Backspace" */
|
|
|
|
#define DEL '\x7f' /** ASCII "Delete" */
|
|
|
|
|
|
|
|
#ifdef MODULE_SHELL_COMMANDS
|
|
|
|
#define MORE_COMMANDS _shell_command_list
|
|
|
|
#else
|
|
|
|
#define MORE_COMMANDS
|
|
|
|
#endif /* MODULE_SHELL_COMMANDS */
|
2015-09-04 19:32:54 +02:00
|
|
|
|
2018-12-18 14:49:28 +01:00
|
|
|
#ifdef MODULE_NEWLIB
|
2020-02-08 12:52:46 +01:00
|
|
|
#define flush_if_needed() fflush(stdout)
|
|
|
|
#else
|
|
|
|
#define flush_if_needed()
|
|
|
|
#endif /* MODULE_NEWLIB */
|
|
|
|
|
|
|
|
#ifndef SHELL_NO_ECHO
|
|
|
|
#define ECHO_ON 1
|
|
|
|
#else
|
|
|
|
#define ECHO_ON 0
|
|
|
|
#endif /* SHELL_NO_ECHO */
|
|
|
|
|
|
|
|
#ifndef SHELL_NO_PROMPT
|
|
|
|
#define PROMPT_ON 1
|
|
|
|
#else
|
|
|
|
#define PROMPT_ON 0
|
|
|
|
#endif /* SHELL_NO_PROMPT */
|
2018-12-18 14:49:28 +01:00
|
|
|
|
2014-02-21 19:10:24 +01:00
|
|
|
static shell_command_handler_t find_handler(const shell_command_t *command_list, char *command)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2013-11-04 17:02:57 +01:00
|
|
|
const shell_command_t *command_lists[] = {
|
|
|
|
command_list,
|
2020-02-08 12:52:46 +01:00
|
|
|
MORE_COMMANDS
|
2013-11-04 17:02:57 +01:00
|
|
|
};
|
|
|
|
|
2013-12-02 17:23:56 +01:00
|
|
|
/* iterating over command_lists */
|
2019-07-18 15:16:43 +02:00
|
|
|
for (unsigned int i = 0; i < ARRAY_SIZE(command_lists); i++) {
|
2019-07-29 00:42:41 +02:00
|
|
|
|
|
|
|
const shell_command_t *entry;
|
|
|
|
|
2013-11-04 17:02:57 +01:00
|
|
|
if ((entry = command_lists[i])) {
|
2013-12-02 17:23:56 +01:00
|
|
|
/* iterating over commands in command_lists entry */
|
2013-11-04 17:02:57 +01:00
|
|
|
while (entry->name != NULL) {
|
|
|
|
if (strcmp(entry->name, command) == 0) {
|
|
|
|
return entry->handler;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
entry++;
|
|
|
|
}
|
|
|
|
}
|
2010-10-01 15:24:43 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2010-10-01 15:24:43 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
static void print_help(const shell_command_t *command_list)
|
|
|
|
{
|
2010-11-04 15:05:51 +01:00
|
|
|
printf("%-20s %s\n", "Command", "Description");
|
2010-11-04 19:06:46 +01:00
|
|
|
puts("---------------------------------------");
|
2010-11-04 15:05:51 +01:00
|
|
|
|
2013-11-04 17:02:57 +01:00
|
|
|
const shell_command_t *command_lists[] = {
|
|
|
|
command_list,
|
2020-02-08 12:52:46 +01:00
|
|
|
MORE_COMMANDS
|
2013-11-04 17:02:57 +01:00
|
|
|
};
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-12-02 17:23:56 +01:00
|
|
|
/* iterating over command_lists */
|
2019-07-18 15:16:43 +02:00
|
|
|
for (unsigned int i = 0; i < ARRAY_SIZE(command_lists); i++) {
|
2019-07-29 00:42:41 +02:00
|
|
|
|
|
|
|
const shell_command_t *entry;
|
|
|
|
|
2013-11-04 17:02:57 +01:00
|
|
|
if ((entry = command_lists[i])) {
|
2013-12-02 17:23:56 +01:00
|
|
|
/* iterating over commands in command_lists entry */
|
2013-11-04 17:02:57 +01:00
|
|
|
while (entry->name != NULL) {
|
|
|
|
printf("%-20s %s\n", entry->name, entry->desc);
|
|
|
|
entry++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-04 15:05:51 +01:00
|
|
|
}
|
|
|
|
|
2015-07-14 18:18:28 +02:00
|
|
|
static void handle_input_line(const shell_command_t *command_list, char *line)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2014-02-21 19:40:58 +01:00
|
|
|
static const char *INCORRECT_QUOTING = "shell: incorrect quoting";
|
|
|
|
|
2014-02-21 19:10:24 +01:00
|
|
|
/* first we need to calculate the number of arguments */
|
|
|
|
unsigned argc = 0;
|
2014-02-26 13:21:25 +01:00
|
|
|
char *pos = line;
|
2014-02-26 18:09:46 +01:00
|
|
|
int contains_esc_seq = 0;
|
2014-02-26 13:21:25 +01:00
|
|
|
while (1) {
|
2014-03-10 13:47:33 +01:00
|
|
|
if ((unsigned char) *pos > ' ') {
|
2014-02-26 13:21:25 +01:00
|
|
|
/* found an argument */
|
2014-02-26 18:09:46 +01:00
|
|
|
if (*pos == '"' || *pos == '\'') {
|
|
|
|
/* it's a quoted argument */
|
|
|
|
const char quote_char = *pos;
|
2014-02-26 13:21:25 +01:00
|
|
|
do {
|
|
|
|
++pos;
|
|
|
|
if (!*pos) {
|
|
|
|
puts(INCORRECT_QUOTING);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-26 18:09:46 +01:00
|
|
|
else if (*pos == '\\') {
|
|
|
|
/* skip over the next character */
|
|
|
|
++contains_esc_seq;
|
|
|
|
++pos;
|
|
|
|
if (!*pos) {
|
|
|
|
puts(INCORRECT_QUOTING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} while (*pos != quote_char);
|
2014-03-10 13:47:33 +01:00
|
|
|
if ((unsigned char) pos[1] > ' ') {
|
2014-02-21 19:40:58 +01:00
|
|
|
puts(INCORRECT_QUOTING);
|
2014-02-21 19:10:24 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-02-21 19:40:58 +01:00
|
|
|
}
|
2014-02-26 13:21:25 +01:00
|
|
|
else {
|
|
|
|
/* it's an unquoted argument */
|
|
|
|
do {
|
2014-02-26 18:09:46 +01:00
|
|
|
if (*pos == '\\') {
|
|
|
|
/* skip over the next character */
|
|
|
|
++contains_esc_seq;
|
|
|
|
++pos;
|
|
|
|
if (!*pos) {
|
|
|
|
puts(INCORRECT_QUOTING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 13:21:25 +01:00
|
|
|
++pos;
|
|
|
|
if (*pos == '"') {
|
|
|
|
puts(INCORRECT_QUOTING);
|
|
|
|
return;
|
|
|
|
}
|
2014-03-10 13:47:33 +01:00
|
|
|
} while ((unsigned char) *pos > ' ');
|
2014-02-26 13:21:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* count the number of arguments we got */
|
|
|
|
++argc;
|
|
|
|
}
|
|
|
|
|
2020-02-07 12:50:49 +01:00
|
|
|
/* zero out current position (space or quotation mark) and advance */
|
2014-02-26 13:21:25 +01:00
|
|
|
if (*pos > 0) {
|
|
|
|
*pos = 0;
|
|
|
|
++pos;
|
2014-02-21 19:10:24 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-02-26 13:21:25 +01:00
|
|
|
break;
|
2014-02-21 19:10:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!argc) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-22 17:25:19 +02:00
|
|
|
|
2014-02-21 19:10:24 +01:00
|
|
|
/* then we fill the argv array */
|
|
|
|
char *argv[argc + 1];
|
|
|
|
argv[argc] = NULL;
|
|
|
|
pos = line;
|
|
|
|
for (unsigned i = 0; i < argc; ++i) {
|
|
|
|
while (!*pos) {
|
|
|
|
++pos;
|
|
|
|
}
|
2014-02-26 18:09:46 +01:00
|
|
|
if (*pos == '"' || *pos == '\'') {
|
2014-02-21 19:10:24 +01:00
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
argv[i] = pos;
|
|
|
|
while (*pos) {
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 18:09:46 +01:00
|
|
|
for (char **arg = argv; contains_esc_seq && *arg; ++arg) {
|
|
|
|
for (char *c = *arg; *c; ++c) {
|
|
|
|
if (*c != '\\') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (char *d = c; *d; ++d) {
|
|
|
|
*d = d[1];
|
|
|
|
}
|
|
|
|
if (--contains_esc_seq == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2014-02-21 19:10:24 +01:00
|
|
|
/* then we call the appropriate handler */
|
2015-07-14 18:18:28 +02:00
|
|
|
shell_command_handler_t handler = find_handler(command_list, argv[0]);
|
2014-02-21 19:10:24 +01:00
|
|
|
if (handler != NULL) {
|
|
|
|
handler(argc, argv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (strcmp("help", argv[0]) == 0) {
|
2015-07-14 18:18:28 +02:00
|
|
|
print_help(command_list);
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-08-12 13:16:37 +02:00
|
|
|
printf("shell: command not found: %s\n", argv[0]);
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 13:42:14 +01:00
|
|
|
static inline void print_prompt(void)
|
|
|
|
{
|
|
|
|
if (PROMPT_ON) {
|
|
|
|
putchar('>');
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_if_needed();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void echo_char(char c)
|
|
|
|
{
|
|
|
|
if (ECHO_ON) {
|
|
|
|
putchar(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void white_tape(void)
|
|
|
|
{
|
|
|
|
if (ECHO_ON) {
|
|
|
|
putchar('\b');
|
|
|
|
putchar(' ');
|
|
|
|
putchar('\b');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void new_line(void)
|
|
|
|
{
|
|
|
|
if (ECHO_ON) {
|
|
|
|
putchar('\r');
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 19:01:46 +01:00
|
|
|
/**
|
|
|
|
* @brief Read a single line from standard input into a buffer.
|
|
|
|
*
|
|
|
|
* In addition to copying characters, this routine echoes the line back to
|
|
|
|
* stdout and also supports primitive line editing.
|
|
|
|
*
|
|
|
|
* If the input line is too long, the input will still be consumed until the end
|
|
|
|
* to prevent the next line from containing garbage.
|
|
|
|
*
|
2020-02-08 13:42:14 +01:00
|
|
|
* We allow Unix (\n), DOS (\r\n), and Mac linebreaks (\r).
|
|
|
|
* QEMU transmits only a single '\r' == 13 on hitting enter ("-serial stdio").
|
|
|
|
* DOS newlines are handled like hitting enter twice.
|
|
|
|
*
|
2018-12-18 19:01:46 +01:00
|
|
|
* @param buf Buffer where the input will be placed.
|
|
|
|
* @param size Size of the buffer. The maximum line length will be one less
|
|
|
|
* than size, to accommodate for the null terminator.
|
|
|
|
* The minimum buffer size is 1.
|
|
|
|
*
|
|
|
|
* @return length of the read line, excluding the terminator, if reading was
|
|
|
|
* successful.
|
|
|
|
* @return EOF, if the end of the input stream was reached.
|
2020-02-08 13:42:14 +01:00
|
|
|
* @return -ENOBUFS if the buffer size was exceeded.
|
2018-12-18 19:01:46 +01:00
|
|
|
*/
|
2015-07-14 18:18:28 +02:00
|
|
|
static int readline(char *buf, size_t size)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2018-12-18 19:01:46 +01:00
|
|
|
int curr_pos = 0;
|
|
|
|
bool length_exceeded = false;
|
|
|
|
|
|
|
|
assert((size_t) size > 0);
|
2010-09-24 16:28:34 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (1) {
|
2018-12-18 19:01:46 +01:00
|
|
|
assert((size_t) curr_pos < size);
|
2010-09-24 16:28:34 +02:00
|
|
|
|
2015-07-14 18:18:28 +02:00
|
|
|
int c = getchar();
|
2015-04-29 20:53:03 +02:00
|
|
|
|
2020-02-08 13:42:14 +01:00
|
|
|
switch (c) {
|
2018-12-18 19:01:46 +01:00
|
|
|
|
2020-02-08 13:42:14 +01:00
|
|
|
case EOF:
|
|
|
|
return EOF;
|
2014-09-11 15:24:51 +02:00
|
|
|
|
2020-02-08 13:42:14 +01:00
|
|
|
case ETX:
|
|
|
|
/* Ctrl-C cancels the current line. */
|
|
|
|
curr_pos = 0;
|
|
|
|
length_exceeded = false;
|
|
|
|
/* fall-thru */
|
|
|
|
case '\r':
|
|
|
|
/* fall-thru */
|
|
|
|
case '\n':
|
|
|
|
buf[curr_pos] = '\0';
|
|
|
|
|
|
|
|
new_line();
|
|
|
|
|
|
|
|
return (length_exceeded) ? -ENOBUFS : curr_pos;
|
|
|
|
|
|
|
|
/* check for backspace: */
|
|
|
|
case BS: /* 0x08 (BS) for most terminals */
|
|
|
|
/* fall-thru */
|
|
|
|
case DEL: /* 0x7f (DEL) when using QEMU */
|
|
|
|
if (curr_pos > 0) {
|
|
|
|
curr_pos--;
|
|
|
|
if ((size_t) curr_pos < size) {
|
|
|
|
buf[curr_pos] = '\0';
|
|
|
|
length_exceeded = false;
|
|
|
|
}
|
|
|
|
white_tape();
|
|
|
|
}
|
|
|
|
break;
|
2020-02-08 12:52:46 +01:00
|
|
|
|
2020-02-08 13:42:14 +01:00
|
|
|
default:
|
|
|
|
/* Always consume characters, but do not not always store them */
|
|
|
|
if ((size_t) curr_pos < size - 1) {
|
|
|
|
buf[curr_pos++] = c;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
length_exceeded = true;
|
|
|
|
}
|
|
|
|
echo_char(c);
|
|
|
|
break;
|
2014-09-11 15:24:51 +02:00
|
|
|
}
|
2020-02-08 12:52:46 +01:00
|
|
|
|
2018-12-18 14:49:28 +01:00
|
|
|
flush_if_needed();
|
2010-09-24 16:28:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:26:31 +02:00
|
|
|
void shell_run_once(const shell_command_t *shell_commands,
|
|
|
|
char *line_buf, int len)
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2015-07-14 18:18:28 +02:00
|
|
|
print_prompt();
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (1) {
|
2015-07-14 18:18:28 +02:00
|
|
|
int res = readline(line_buf, len);
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2018-12-18 19:01:46 +01:00
|
|
|
switch (res) {
|
|
|
|
|
|
|
|
case EOF:
|
|
|
|
return;
|
2018-10-02 18:05:15 +02:00
|
|
|
|
2018-12-18 19:01:46 +01:00
|
|
|
case -ENOBUFS:
|
|
|
|
puts("shell: maximum line length exceeded");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
handle_input_line(shell_commands, line_buf);
|
|
|
|
break;
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2015-07-14 18:18:28 +02:00
|
|
|
print_prompt();
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
}
|