diff options
Diffstat (limited to 'os')
| -rw-r--r-- | os/commands/echo.c | 5 | ||||
| -rw-r--r-- | os/commands/echo.h | 1 | ||||
| -rw-r--r-- | os/main.c | 28 | ||||
| -rw-r--r-- | os/shell/shell.c | 106 | ||||
| -rw-r--r-- | os/stdlib/sdtlib.h | 3 | ||||
| -rw-r--r-- | os/stdlib/stdlib.c | 14 |
6 files changed, 146 insertions, 11 deletions
diff --git a/os/commands/echo.c b/os/commands/echo.c new file mode 100644 index 0000000..669003e --- /dev/null +++ b/os/commands/echo.c @@ -0,0 +1,5 @@ +#include "../../kernel/gpu/text_mode/display.h" + +void echo(char *args) { + scrn_println(args); +}
\ No newline at end of file diff --git a/os/commands/echo.h b/os/commands/echo.h new file mode 100644 index 0000000..db62a4f --- /dev/null +++ b/os/commands/echo.h @@ -0,0 +1 @@ +void echo(char *args);
\ No newline at end of file @@ -57,6 +57,21 @@ void os_update_infobar_rc() scrn_set_cursor_pos(curr_r, curr_c); } +void os_update_infobar_program() { + int curr_attr_byte = scrn_get_char_attr_byte(); + int curr_r = scrn_get_cursor_row(); + int curr_c = scrn_get_cursor_col(); + + scrn_set_text_colour(COLOUR_WHITE, COLOUR_BLUE); + scrn_set_cursor_pos(0, 11); + line_pad(64); + scrn_set_cursor_pos(0, 11); + scrn_print(focused_program.name); + + scrn_set_char_attr_byte(curr_attr_byte); + scrn_set_cursor_pos(curr_r, curr_c); +} + void os_init_infobar() { scrn_clear(); @@ -64,9 +79,7 @@ void os_init_infobar() scrn_set_text_colour(COLOUR_WHITE, COLOUR_BLUE); line_pad(80); // fill line scrn_set_cursor_pos(0, 2); - scrn_print("tinyOS"); - scrn_set_cursor_pos(0, 30); - scrn_print("running: "); + scrn_print("tinyOS - "); scrn_print(focused_program.name); os_update_infobar_rc(); @@ -77,6 +90,7 @@ void os_init_infobar() // TODO keep a stack of running programs void os_update_focused_program(Program_t program) { focused_program = program; + os_update_infobar_program(); } // Send keypress from kernel to the currently focused programs keypress handler @@ -87,11 +101,11 @@ void os_proxy_keypress(unsigned char key) { void os_start() { - os_update_focused_program(shell_run()); scrn_enable_cursor(10, 12); os_init_infobar(); os_show_splash(); - os_update_infobar_rc(); -} + + os_update_focused_program(shell_run()); -// TODO display handling
\ No newline at end of file + os_update_infobar_rc(); +}
\ No newline at end of file diff --git a/os/shell/shell.c b/os/shell/shell.c index 933b19c..4fb5488 100644 --- a/os/shell/shell.c +++ b/os/shell/shell.c @@ -1,17 +1,116 @@ #include "../../os/main.h" #include "../../kernel/gpu/text_mode/display.h" +#include "../stdlib/sdtlib.h" +#include "../commands/echo.h" + +#define CMD_BUFFER_SIZE 80 + +// TODO history buffer +unsigned char cmd_buffer[CMD_BUFFER_SIZE]; +int cmd_buffer_p = 0; + +// cant malloc yet so just store locally +unsigned char cmd[CMD_BUFFER_SIZE]; +unsigned char args[CMD_BUFFER_SIZE]; + +void shell_set_prompt() { + scrn_print("$> "); +} + +void shell_clear_cmd_buffer() { + for(int i = 0; i < CMD_BUFFER_SIZE; i++) { + cmd_buffer[i] = 0; + } + cmd_buffer_p = 0; +} + +void shell_clear_cmd() { + for(int i = 0; i < CMD_BUFFER_SIZE; i++) { + cmd[i] = 0; + } +} + +void shell_clear_args() { + for(int i = 0; i < CMD_BUFFER_SIZE; i++) { + args[i] = 0; + } +} + +unsigned char* shell_parse_cmd() { + shell_clear_cmd(); + for(int i = 0; i < CMD_BUFFER_SIZE; i++) { + if (cmd_buffer[i] == ' ') { + break; + } + cmd[i] = cmd_buffer[i]; + } + return cmd; +} + +unsigned char* shell_parse_args() { + shell_clear_args(); + int arg_idx = 0; + for(int i = 0; i < CMD_BUFFER_SIZE; i++) { + if (cmd_buffer[i] == ' ') { + arg_idx = i + 1; + break; + } + } + + if (arg_idx == 0) { + return args; + } + else { + for(int i = arg_idx; i < CMD_BUFFER_SIZE; i++) { + args[i - arg_idx] = cmd_buffer[i]; + } + return args; + } +} + +void shell_exec_cmd(unsigned char* cmd_name, unsigned char* args) { + // TODO lookup string to funciton pointer + if (strcmp(cmd_name, "echo") == 0) { + echo(args); + } + else if (strcmp(cmd_name, "") != 0) { + scrn_print(cmd_name); + scrn_println(": command not found"); + } +} + +void shell_process_cmd_buffer() { + scrn_newline(); + + shell_exec_cmd(shell_parse_cmd(), shell_parse_args()); + + shell_clear_cmd_buffer(); + shell_set_prompt(); +} + +void shell_handle_backspace() { + if (cmd_buffer_p != 0) { + cmd_buffer[--cmd_buffer_p] = 0; + scrn_backspace(); + } +} + +void shell_handle_keypress(unsigned char key) { + cmd_buffer[cmd_buffer_p++] = key; + scrn_putchar(key); +} void keypress_handler(unsigned char key) { switch(key) { case '\b': - scrn_backspace(); + shell_handle_backspace(); break; case '\n': - scrn_newline(); + shell_process_cmd_buffer(); break; default: - scrn_putchar(key); + shell_handle_keypress(key); } } @@ -19,5 +118,6 @@ Program_t shell_run() { Program_t this; this.keypress_handler = keypress_handler; this.name = "shell"; + shell_set_prompt(); return this; }
\ No newline at end of file diff --git a/os/stdlib/sdtlib.h b/os/stdlib/sdtlib.h index a0b3402..85073d2 100644 --- a/os/stdlib/sdtlib.h +++ b/os/stdlib/sdtlib.h @@ -1 +1,2 @@ -char* int_to_str(int i);
\ No newline at end of file +char* int_to_str(int i); +int strcmp(const char *str1, const char *str2);
\ No newline at end of file diff --git a/os/stdlib/stdlib.c b/os/stdlib/stdlib.c index 465e978..bdb2242 100644 --- a/os/stdlib/stdlib.c +++ b/os/stdlib/stdlib.c @@ -18,4 +18,18 @@ char* int_to_str(int i) *--p = '-'; } return p; +} + + +int strcmp(const char *str1, const char *str2) +{ + while(*str1){ + if (*str1 != *str2) { + break; + } + str1++; + str2++; + } + + return *(const unsigned char*)str1 - *(const unsigned char*)str2; }
\ No newline at end of file |