2013-06-22 05:11:53 +02:00
|
|
|
/**
|
2013-12-19 17:11:06 +01:00
|
|
|
* Shell interpreter
|
2013-06-22 05:11:53 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009, Freie Universitaet Berlin (FUB).
|
|
|
|
* Copyright (C) 2013, INRIA.
|
|
|
|
*
|
2013-11-22 20:47:05 +01:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
2013-06-22 05:11:53 +02:00
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
2010-09-22 17:25:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup shell
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of a very simple command interpreter.
|
|
|
|
* 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.
|
|
|
|
*
|
2010-09-23 10:33:00 +02:00
|
|
|
* @author Freie Universität Berlin, Computer Systems & Telematics
|
2010-09-22 17:25:19 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "shell.h"
|
|
|
|
#include "shell_commands.h"
|
2010-09-22 17:25:19 +02: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,
|
2010-11-04 19:06:46 +01:00
|
|
|
#ifdef MODULE_SHELL_COMMANDS
|
2013-11-04 17:02:57 +01:00
|
|
|
_shell_command_list,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
const shell_command_t *entry;
|
|
|
|
|
2013-12-02 17:23:56 +01:00
|
|
|
/* iterating over command_lists */
|
2013-12-19 17:11:06 +01:00
|
|
|
for (unsigned int i = 0; i < sizeof(command_lists) / sizeof(entry); i++) {
|
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,
|
2010-11-04 19:06:46 +01:00
|
|
|
#ifdef MODULE_SHELL_COMMANDS
|
2013-11-04 17:02:57 +01:00
|
|
|
_shell_command_list,
|
|
|
|
#endif
|
|
|
|
};
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-11-04 17:02:57 +01:00
|
|
|
const shell_command_t *entry;
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-12-02 17:23:56 +01:00
|
|
|
/* iterating over command_lists */
|
2013-12-19 17:11:06 +01:00
|
|
|
for (unsigned int i = 0; i < sizeof(command_lists) / sizeof(entry); i++) {
|
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
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
static void handle_input_line(shell_t *shell, char *line)
|
|
|
|
{
|
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;
|
|
|
|
while (1) {
|
|
|
|
if (*pos > ' ') {
|
|
|
|
/* found an argument */
|
|
|
|
if (*pos == '"') {
|
|
|
|
/* it's an quoted argument */
|
|
|
|
do {
|
|
|
|
++pos;
|
|
|
|
if (!*pos) {
|
|
|
|
puts(INCORRECT_QUOTING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (*pos != '"');
|
|
|
|
if (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 {
|
|
|
|
++pos;
|
|
|
|
if (*pos == '"') {
|
|
|
|
puts(INCORRECT_QUOTING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (*pos > ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count the number of arguments we got */
|
|
|
|
++argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zero out the current position (space or quotation mark) and advance */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (*pos == '"') {
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
argv[i] = pos;
|
|
|
|
while (*pos) {
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2014-02-21 19:10:24 +01:00
|
|
|
/* then we call the appropriate handler */
|
|
|
|
shell_command_handler_t handler = find_handler(shell->command_list, argv[0]);
|
|
|
|
if (handler != NULL) {
|
|
|
|
handler(argc, argv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (strcmp("help", argv[0]) == 0) {
|
|
|
|
print_help(shell->command_list);
|
2013-06-22 05:11:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-02-21 19:10:24 +01:00
|
|
|
puts("shell: command not found.");
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
static int readline(shell_t *shell, char *buf, size_t size)
|
|
|
|
{
|
2010-09-24 16:28:34 +02:00
|
|
|
char *line_buf_ptr = buf;
|
|
|
|
int c;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (1) {
|
|
|
|
if ((line_buf_ptr - buf) >= ((int) size) - 1) {
|
2010-09-24 16:28:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = shell->readchar();
|
2010-09-30 15:10:39 +02:00
|
|
|
shell->put_char(c);
|
2010-09-24 16:28:34 +02:00
|
|
|
|
2014-01-24 22:21:07 +01:00
|
|
|
/* We allow Unix linebreaks (\n), DOS linebreaks (\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, but handle_input_line() ignores empty lines. */
|
|
|
|
if (c == 10 || c == 13) {
|
2010-09-24 16:28:34 +02:00
|
|
|
*line_buf_ptr = '\0';
|
|
|
|
return 0;
|
2012-02-16 21:33:41 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-09-24 16:28:34 +02:00
|
|
|
*line_buf_ptr++ = c;
|
|
|
|
}
|
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2012-02-14 14:49:45 +01:00
|
|
|
return 1;
|
2010-09-24 16:28:34 +02:00
|
|
|
}
|
|
|
|
|
2013-03-18 15:37:32 +01:00
|
|
|
static inline void print_prompt(shell_t *shell)
|
|
|
|
{
|
|
|
|
shell->put_char('>');
|
|
|
|
shell->put_char(' ');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-22 05:11:53 +02:00
|
|
|
void shell_run(shell_t *shell)
|
|
|
|
{
|
2013-12-19 17:11:06 +01:00
|
|
|
char line_buf[shell->shell_buffer_size];
|
2010-09-22 17:25:19 +02:00
|
|
|
|
2013-03-18 15:37:32 +01:00
|
|
|
print_prompt(shell);
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (1) {
|
2010-09-24 16:28:34 +02:00
|
|
|
int res = readline(shell, line_buf, sizeof(line_buf));
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (!res) {
|
2013-10-29 21:48:26 +01:00
|
|
|
handle_input_line(shell, line_buf);
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
2013-06-22 05:11:53 +02:00
|
|
|
|
2013-03-18 15:37:32 +01:00
|
|
|
print_prompt(shell);
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:11:06 +01:00
|
|
|
void shell_init(shell_t *shell, const shell_command_t *shell_commands,
|
|
|
|
uint16_t shell_buffer_size, int(*readchar)(void), void(*put_char)(int))
|
2013-06-22 05:11:53 +02:00
|
|
|
{
|
2010-11-02 17:23:10 +01:00
|
|
|
shell->command_list = shell_commands;
|
2013-12-19 17:11:06 +01:00
|
|
|
shell->shell_buffer_size = shell_buffer_size;
|
2010-09-22 17:25:19 +02:00
|
|
|
shell->readchar = readchar;
|
2010-09-30 15:10:39 +02:00
|
|
|
shell->put_char = put_char;
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
|
2010-09-23 10:33:00 +02:00
|
|
|
/** @} */
|