1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

* shell: removed hashmap dependency. shell is now O(n)

This commit is contained in:
Kaspar Schleiser 2010-10-01 15:24:43 +02:00
parent a5bfdd957b
commit 59d72c7d6a
8 changed files with 79 additions and 32 deletions

View File

@ -6,6 +6,6 @@
SubDir TOP projects test_shell ;
Module test_shell : test_shell.c : shell posix_io shell_auto_init ps uart0 ;
Module test_shell : test_shell.c : shell posix_io ps uart0 ;
UseModule test_shell ;

View File

@ -18,9 +18,6 @@ void print_testend(char* str) {
printf("[TEST_END]\n");
}
//extern int uart0_init();
//extern int uart0_handler_pid;
int shell_readc() {
char c = 0;
posix_read(uart0_handler_pid, &c, 1);
@ -31,6 +28,12 @@ void shell_putchar(int c) {
putchar(c);
}
const shell_command_t shell_commands[] = {
{"start_test", print_teststart},
{"end_test", print_testend},
{NULL, NULL}
};
int main(void) {
//printf("Moin. build on %s %s SVN-Revision: %s\n", kernel_builddate, kernel_buildtime, kernel_svnrevision);
printf("test_shell.\n");
@ -41,10 +44,8 @@ int main(void) {
shell_t shell;
shell_init(&shell, shell_readc, shell_putchar);
shell_auto_init(&shell);
shell_register_cmd(&shell, "start_test", print_teststart);
shell_register_cmd(&shell, "end_test", print_testend);
shell.command_list = shell_commands;
shell_run(&shell);

View File

@ -32,10 +32,15 @@ and the mailinglist (subscription via web site)
* @ingroup feuerware
*/
#include "hashtable.h"
//#include "hashtable.h"
typedef struct shell_commant_t {
char* name;
void (*handler)(char*);
} shell_command_t;
typedef struct shell_t {
struct hashtable *h;
const shell_command_t *command_list;
int (*readchar)(void);
void (*put_char)(int);
} shell_t;
@ -51,7 +56,7 @@ void shell_init(shell_t *shell, int(*read_char)(void), void (*put_char)(int));
* @param name Name of the command to register.
* @param handler Function pointer to handler that takes the complete command line as parameter.
*/
void shell_register_cmd(shell_t *shell, char* name, void (*handler)(char* args));
//void shell_register_cmd(shell_t *shell, char* name, void (*handler)(char* args));
/**
* @brief Endless loop that waits for command and executes handler.

View File

@ -27,7 +27,8 @@
SubDir TOP sys shell ;
Module shell : shell.c : hashtable hash_string ;
Module ps : ps.c ;
Module shell_auto_init : shell_auto_init.c : shell ;
Module shell : shell.c ;
Module shell_commands : shell_commands.c : shell ;
Module ps : ps.c ;

View File

@ -46,9 +46,21 @@ and the mailinglist (subscription via web site)
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <hash_string.h>
#include <shell.h>
static void(*find_handler(const shell_command_t *command_list, char *command))(char*) {
const shell_command_t *entry = command_list;
while(entry->name != NULL) {
if ( strcmp(entry->name, command) == 0) {
return entry->handler;
} else {
command_list++;
}
}
return NULL;
}
static void handle_input_line(shell_t *shell, char* line) {
char* saveptr;
char* command = strtok_r(line, " ", &saveptr);
@ -56,7 +68,7 @@ static void handle_input_line(shell_t *shell, char* line) {
void (*handler)(char*) = NULL;
if (command) {
handler = hashtable_search(shell->h, command);
handler = find_handler(shell->command_list, command);
if (handler) {
handler(line);
} else {
@ -103,13 +115,8 @@ void shell_run(shell_t *shell) {
}
void shell_init(shell_t *shell, int(*readchar)(void), void(*put_char)(int)) {
shell->h = create_hashtable(16, (unsigned int (*)(void*)) hash_string, (int (*) (void*,void*)) cmp_string);
shell->readchar = readchar;
shell->put_char = put_char;
}
void shell_register_cmd(shell_t *shell, char* name, void (*handler)(char* args)) {
hashtable_insert(shell->h, name, handler);
}
/** @} */

View File

@ -1,11 +0,0 @@
#include <shell.h>
#ifdef MODULE_PS
#include <ps.h>
#endif
void shell_auto_init(shell_t *s) {
#ifdef MODULE_PS
shell_register_cmd(s, "ps", _ps_handler);
#endif
}

View File

@ -0,0 +1,9 @@
#include <shell.h>
const shell_command_t _shell_command_list[] = {
#ifdef MODULE_PS
{"ps", ps_handler},
#endif
{NULL, NULL}
};

35
sys/uart0.c Normal file
View File

@ -0,0 +1,35 @@
#include <chardev_thread.h>
#include <ringbuffer.h>
#include <stdio.h>
#include <thread.h>
#include <msg.h>
#include <board_uart0.h>
#define UART0_BUFSIZE 32
ringbuffer uart0_ringbuffer;
int uart0_handler_pid;
static char buffer[UART0_BUFSIZE];
static void uart0_loop() {
chardev_loop(&uart0_ringbuffer);
}
void board_uart0_init() {
ringbuffer_init(&uart0_ringbuffer, buffer, UART0_BUFSIZE);
int pid = thread_create(KERNEL_CONF_STACKSIZE_MAIN, PRIORITY_MAIN-1, CREATE_STACKTEST, uart0_loop, "uart0");
uart0_handler_pid = pid;
puts("uart0_init() [OK]");
}
void uart0_handle_incoming(int c) {
rb_add_element(&uart0_ringbuffer, c);
}
void uart0_notify_thread() {
msg m;
m.type = 0;
msg_send_int(&m, uart0_handler_pid);
}