2010-09-22 17:25:19 +02:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright 2009, Freie Universitaet Berlin (FUB). All rights reserved.
|
|
|
|
|
|
|
|
These sources were developed at the Freie Universitaet Berlin, Computer Systems
|
|
|
|
and Telematics group (http://cst.mi.fu-berlin.de).
|
|
|
|
-------------------------------------------------------------------------------
|
2013-03-07 20:51:26 +01:00
|
|
|
This file is part of RIOT.
|
2010-09-22 17:25:19 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
2013-03-07 20:51:26 +01:00
|
|
|
RIOT is distributed in the hope that it will be useful, but WITHOUT
|
2010-09-22 17:25:19 +02:00
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see http://www.gnu.org/licenses/ .
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
For further information and questions please use the web site
|
|
|
|
http://scatterweb.mi.fu-berlin.de
|
|
|
|
and the mailinglist (subscription via web site)
|
|
|
|
scatterweb@lists.spline.inf.fu-berlin.de
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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>
|
|
|
|
*/
|
|
|
|
|
2010-12-03 22:22:58 +01:00
|
|
|
//#include <sys/unistd.h>
|
2010-09-22 17:25:19 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <shell.h>
|
2010-11-04 19:06:46 +01:00
|
|
|
#include <shell_commands.h>
|
|
|
|
#include <stdint.h>
|
2010-12-03 22:22:58 +01:00
|
|
|
#include <stdlib.h>
|
2010-09-22 17:25:19 +02:00
|
|
|
|
2010-10-01 15:24:43 +02:00
|
|
|
static void(*find_handler(const shell_command_t *command_list, char *command))(char*) {
|
2010-11-04 19:06:46 +01:00
|
|
|
const shell_command_t* entry = command_list;
|
2010-11-05 23:43:14 +01:00
|
|
|
if (entry) {
|
|
|
|
while (entry->name != NULL) {
|
|
|
|
if ( strcmp(entry->name, command) == 0) {
|
|
|
|
return entry->handler;
|
|
|
|
} else {
|
|
|
|
entry++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-04 19:06:46 +01:00
|
|
|
#ifdef MODULE_SHELL_COMMANDS
|
|
|
|
entry = _shell_command_list;
|
|
|
|
while (entry->name != NULL) {
|
2010-10-01 15:24:43 +02:00
|
|
|
if ( strcmp(entry->name, command) == 0) {
|
|
|
|
return entry->handler;
|
|
|
|
} else {
|
2010-10-25 15:38:22 +02:00
|
|
|
entry++;
|
2010-10-01 15:24:43 +02:00
|
|
|
}
|
|
|
|
}
|
2010-11-04 19:06:46 +01:00
|
|
|
#endif
|
2010-10-01 15:24:43 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-04 15:05:51 +01:00
|
|
|
static void print_help(const shell_command_t *command_list) {
|
|
|
|
const shell_command_t *entry = command_list;
|
|
|
|
|
|
|
|
printf("%-20s %s\n", "Command", "Description");
|
2010-11-04 19:06:46 +01:00
|
|
|
puts("---------------------------------------");
|
2010-11-04 15:05:51 +01:00
|
|
|
|
2010-11-05 23:43:14 +01:00
|
|
|
if (entry) {
|
|
|
|
while (entry->name != NULL) {
|
|
|
|
printf("%-20s %s\n", entry->name, entry->desc);
|
|
|
|
entry++;
|
|
|
|
}
|
|
|
|
}
|
2010-11-04 19:06:46 +01:00
|
|
|
|
|
|
|
#ifdef MODULE_SHELL_COMMANDS
|
|
|
|
entry = _shell_command_list;
|
|
|
|
while (entry->name != NULL) {
|
2010-11-04 15:05:51 +01:00
|
|
|
printf("%-20s %s\n", entry->name, entry->desc);
|
|
|
|
entry++;
|
|
|
|
}
|
2010-11-04 19:06:46 +01:00
|
|
|
#endif
|
2010-11-04 15:05:51 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 17:25:19 +02:00
|
|
|
static void handle_input_line(shell_t *shell, char* line) {
|
|
|
|
char* saveptr;
|
2010-11-03 13:53:11 +01:00
|
|
|
char* linedup = strdup(line);
|
|
|
|
char* command = strtok_r(linedup, " ", &saveptr);
|
2010-09-22 17:25:19 +02:00
|
|
|
|
|
|
|
void (*handler)(char*) = NULL;
|
|
|
|
|
|
|
|
if (command) {
|
2010-10-01 15:24:43 +02:00
|
|
|
handler = find_handler(shell->command_list, command);
|
2010-10-25 15:38:22 +02:00
|
|
|
if (handler != NULL) {
|
2010-09-22 17:25:19 +02:00
|
|
|
handler(line);
|
|
|
|
} else {
|
2010-11-04 15:05:51 +01:00
|
|
|
if ( strcmp("help", command) == 0) {
|
|
|
|
print_help(shell->command_list);
|
|
|
|
} else {
|
|
|
|
puts("shell: command not found.");
|
|
|
|
}
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
}
|
2010-11-03 13:53:11 +01:00
|
|
|
|
|
|
|
free(linedup);
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
|
2012-02-14 14:49:45 +01: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;
|
|
|
|
|
|
|
|
while (1) {
|
2012-02-14 14:49:45 +01:00
|
|
|
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
|
|
|
|
|
|
|
if (c == 13) continue;
|
|
|
|
|
|
|
|
if (c == 10) {
|
|
|
|
*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;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2010-09-22 17:25:19 +02:00
|
|
|
void shell_run(shell_t *shell) {
|
|
|
|
char line_buf[255];
|
|
|
|
|
2013-03-18 15:37:32 +01:00
|
|
|
print_prompt(shell);
|
2010-09-22 17:25:19 +02:00
|
|
|
while(1) {
|
2010-09-24 16:28:34 +02:00
|
|
|
int res = readline(shell, line_buf, sizeof(line_buf));
|
|
|
|
if (! res ) {
|
2010-10-25 15:38:22 +02:00
|
|
|
char* line_copy = strdup(line_buf);
|
|
|
|
handle_input_line(shell, line_copy);
|
|
|
|
free(line_copy);
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
2013-03-18 15:37:32 +01:00
|
|
|
print_prompt(shell);
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-02 17:23:10 +01:00
|
|
|
void shell_init(shell_t *shell, const shell_command_t *shell_commands, int(*readchar)(void), void(*put_char)(int)) {
|
|
|
|
shell->command_list = shell_commands;
|
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
|
|
|
/** @} */
|