mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
sys: shell: get rid of some parameters
This commit is contained in:
parent
448e90ba3a
commit
13995e878f
@ -28,6 +28,11 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Default shell buffer size (maximum line length shell can handle)
|
||||
*/
|
||||
#define SHELL_DEFAULT_BUFSIZE (128)
|
||||
|
||||
/**
|
||||
* @brief Protype of a shell callback handler.
|
||||
* @details The functions supplied to shell_init() must use this signature.
|
||||
@ -59,42 +64,15 @@ typedef struct shell_command_t {
|
||||
} shell_command_t;
|
||||
|
||||
/**
|
||||
* @brief The internal state of a shell session.
|
||||
* @details Use shell_init() to initialize the datum,
|
||||
* and shell_run() to run the REPL session.
|
||||
*/
|
||||
typedef struct shell_t {
|
||||
const shell_command_t *command_list; /**< The commandlist supplied to shell_init(). */
|
||||
uint16_t shell_buffer_size; /**< The maximum line length supplied to shell_init(). */
|
||||
int (*readchar)(void); /**< The read function supplied to shell_init(). */
|
||||
int (*put_char)(int); /**< The write function supplied to shell_init(). */
|
||||
} shell_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize a shell session state.
|
||||
* @param[out] shell The datum to initialize.
|
||||
* @param[in] shell_commands Null-terminated list of commands to understand.
|
||||
* Supply `NULL` to only feature the default commands.
|
||||
* @param shell_buffer_size The backing buffer for the command line.
|
||||
* Allocated on the stack!
|
||||
* @param read_char A blocking function that reads one 8-bit character at a time.
|
||||
* The valid code range is [0;255].
|
||||
* A value of `< 0` denotes a read error.
|
||||
* @param put_char Function used to print back the last read character.
|
||||
* Only valid unsigned chars in [0;255] will be supplied.
|
||||
*/
|
||||
void shell_init(shell_t *shell,
|
||||
const shell_command_t *shell_commands,
|
||||
uint16_t shell_buffer_size,
|
||||
int (*read_char)(void),
|
||||
int (*put_char)(int));
|
||||
|
||||
/**
|
||||
* @brief Start the shell session.
|
||||
* @param[in] shell The session that was previously initialized with shell_init().
|
||||
* @brief Start a shell.
|
||||
*
|
||||
* @param[in] commands ptr to array of command structs
|
||||
* @param[in] line_buf Buffer that will be used for reading a line
|
||||
* @param[in] len nr of bytes that fit in line_buf
|
||||
*
|
||||
* @returns This function does not return.
|
||||
*/
|
||||
void shell_run(shell_t *shell) NORETURN;
|
||||
void shell_run(const shell_command_t *commands, char *line_buf, int len) NORETURN;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Copyright (C) 2009, Freie Universitaet Berlin (FUB).
|
||||
* Copyright (C) 2013, INRIA.
|
||||
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* 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
|
||||
@ -86,7 +87,7 @@ static void print_help(const shell_command_t *command_list)
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_input_line(shell_t *shell, char *line)
|
||||
static void handle_input_line(const shell_command_t *command_list, char *line)
|
||||
{
|
||||
static const char *INCORRECT_QUOTING = "shell: incorrect quoting";
|
||||
|
||||
@ -190,13 +191,13 @@ static void handle_input_line(shell_t *shell, char *line)
|
||||
}
|
||||
|
||||
/* then we call the appropriate handler */
|
||||
shell_command_handler_t handler = find_handler(shell->command_list, argv[0]);
|
||||
shell_command_handler_t handler = find_handler(command_list, argv[0]);
|
||||
if (handler != NULL) {
|
||||
handler(argc, argv);
|
||||
}
|
||||
else {
|
||||
if (strcmp("help", argv[0]) == 0) {
|
||||
print_help(shell->command_list);
|
||||
print_help(command_list);
|
||||
}
|
||||
else {
|
||||
printf("shell: command not found: %s\n", argv[0]);
|
||||
@ -204,7 +205,7 @@ static void handle_input_line(shell_t *shell, char *line)
|
||||
}
|
||||
}
|
||||
|
||||
static int readline(shell_t *shell, char *buf, size_t size)
|
||||
static int readline(char *buf, size_t size)
|
||||
{
|
||||
char *line_buf_ptr = buf;
|
||||
|
||||
@ -213,7 +214,7 @@ static int readline(shell_t *shell, char *buf, size_t size)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int c = shell->readchar();
|
||||
int c = getchar();
|
||||
if (c < 0) {
|
||||
return 1;
|
||||
}
|
||||
@ -223,8 +224,8 @@ static int readline(shell_t *shell, char *buf, size_t size)
|
||||
/* DOS newlines are handled like hitting enter twice, but empty lines are ignored. */
|
||||
if (c == '\r' || c == '\n') {
|
||||
*line_buf_ptr = '\0';
|
||||
shell->put_char('\r');
|
||||
shell->put_char('\n');
|
||||
putchar('\r');
|
||||
putchar('\n');
|
||||
|
||||
/* return 1 if line is empty, 0 otherwise */
|
||||
return line_buf_ptr == buf;
|
||||
@ -238,51 +239,38 @@ static int readline(shell_t *shell, char *buf, size_t size)
|
||||
|
||||
*--line_buf_ptr = '\0';
|
||||
/* white-tape the character */
|
||||
shell->put_char('\b');
|
||||
shell->put_char(' ');
|
||||
shell->put_char('\b');
|
||||
putchar('\b');
|
||||
putchar(' ');
|
||||
putchar('\b');
|
||||
}
|
||||
else {
|
||||
*line_buf_ptr++ = c;
|
||||
shell->put_char(c);
|
||||
putchar(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void print_prompt(shell_t *shell)
|
||||
static inline void print_prompt(void)
|
||||
{
|
||||
shell->put_char('>');
|
||||
shell->put_char(' ');
|
||||
putchar('>');
|
||||
putchar(' ');
|
||||
|
||||
#ifdef MODULE_NEWLIB
|
||||
fflush(stdout);
|
||||
#endif
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void shell_run(shell_t *shell)
|
||||
void shell_run(const shell_command_t *shell_commands, char *line_buf, int len)
|
||||
{
|
||||
char line_buf[shell->shell_buffer_size];
|
||||
|
||||
print_prompt(shell);
|
||||
print_prompt();
|
||||
|
||||
while (1) {
|
||||
int res = readline(shell, line_buf, sizeof(line_buf));
|
||||
int res = readline(line_buf, len);
|
||||
|
||||
if (!res) {
|
||||
handle_input_line(shell, line_buf);
|
||||
handle_input_line(shell_commands, line_buf);
|
||||
}
|
||||
|
||||
print_prompt(shell);
|
||||
print_prompt();
|
||||
}
|
||||
}
|
||||
|
||||
void shell_init(shell_t *shell, const shell_command_t *shell_commands,
|
||||
uint16_t shell_buffer_size, int(*readchar)(void), int(*put_char)(int))
|
||||
{
|
||||
shell->command_list = shell_commands;
|
||||
shell->shell_buffer_size = shell_buffer_size;
|
||||
shell->readchar = readchar;
|
||||
shell->put_char = put_char;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user