From d3c75a7c9ce24f68be7d14bcd037ab71e7d70b6c Mon Sep 17 00:00:00 2001 From: Ry Date: Fri, 31 Mar 2023 16:28:23 -0700 Subject: [PATCH] sh: Add `rdnext` command This command redirects the IO stream for the next command to the specified file or named stream --- applications/sh/commands/commands.asm | 9 ++++++++ applications/sh/commands/help.asm | 1 + applications/sh/commands/rdnext.asm | 29 ++++++++++++++++++++++++++ applications/sh/launch.asm | 2 +- applications/sh/main.asm | 30 ++++++++++++++++----------- 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 applications/sh/commands/rdnext.asm diff --git a/applications/sh/commands/commands.asm b/applications/sh/commands/commands.asm index 39c523a..ddef996 100644 --- a/applications/sh/commands/commands.asm +++ b/applications/sh/commands/commands.asm @@ -1,6 +1,9 @@ ; command parser shell_parse_command: + cmp.8 [shell_redirect_next], 0 + ifnz mov [shell_stream_struct_ptr], shell_redirect_stream_struct + mov r0, shell_text_buf_bottom ; clear @@ -33,6 +36,11 @@ shell_parse_command: call compare_string ifz jmp shell_help_command + ; rdnext + mov r1, shell_rdnext_command_string + call compare_string + ifz jmp shell_rdnext_command + ; shutdown mov r1, shell_shutdown_command_string call compare_string @@ -61,5 +69,6 @@ shell_invalid_command_string: data.str "invalid command or FXF binary" data.8 10 #include "commands/diskrm.asm" #include "commands/exit.asm" #include "commands/help.asm" + #include "commands/rdnext.asm" #include "commands/shutdown.asm" #include "commands/type.asm" diff --git a/applications/sh/commands/help.asm b/applications/sh/commands/help.asm index ea4cd47..c74d5d4 100644 --- a/applications/sh/commands/help.asm +++ b/applications/sh/commands/help.asm @@ -22,6 +22,7 @@ shell_help_text: data.str "diskrm | remove disk $0" data.8 10 data.str "exit | exit the shell" data.8 10 data.str "help | show this help text" data.8 10 + data.str "rdnext | redirect the next command's IO" data.8 10 data.str "shutdown| turn the computer off" data.8 10 data.str "type | print contents of file $0" data.8 10 data.8 10 diff --git a/applications/sh/commands/rdnext.asm b/applications/sh/commands/rdnext.asm new file mode 100644 index 0000000..296a20c --- /dev/null +++ b/applications/sh/commands/rdnext.asm @@ -0,0 +1,29 @@ +; rdnext command + +shell_rdnext_command_string: data.strz "rdnext" + +shell_rdnext_command: + cmp.8 [shell_redirect_next], 0 + ifnz ret + + call shell_parse_arguments + push r0 + call get_current_disk_id + mov r1, r0 + pop r0 + mov r2, shell_redirect_stream_struct + call open + cmp r0, 0 + ifz jmp shell_rdnext_command_failed_to_open + + mov.8 [shell_redirect_next], 2 + mov [shell_old_stream_struct_ptr], [shell_stream_struct_ptr] + + ret + +shell_rdnext_command_failed_to_open: + mov r0, shell_rdnext_command_failed_to_open_string + call print_str_to_terminal + ret + +shell_rdnext_command_failed_to_open_string: data.str "failed to open file for redirect" data.8 10 data.8 0 diff --git a/applications/sh/launch.asm b/applications/sh/launch.asm index 4f8fa5d..49e5abe 100644 --- a/applications/sh/launch.asm +++ b/applications/sh/launch.asm @@ -72,7 +72,7 @@ launch_fxf_name_loop_done: push r2 push r1 push r0 - push [shell_terminal_stream_struct_ptr] + push [shell_stream_struct_ptr] sub rsp, 65516 mov [launch_fxf_stack_ptr], rsp mov rsp, r4 diff --git a/applications/sh/main.asm b/applications/sh/main.asm index 7d932be..4c2aa58 100644 --- a/applications/sh/main.asm +++ b/applications/sh/main.asm @@ -6,19 +6,24 @@ const MOVE_CURSOR: 0xF1 const SET_COLOR: 0xF2 const REDRAW_LINE: 0xFE - pop [shell_terminal_stream_struct_ptr] + pop [shell_stream_struct_ptr] + shell_task_return: + cmp.8 [shell_redirect_next], 0 + ifnz mov [shell_stream_struct_ptr], [shell_old_stream_struct_ptr] + ifnz dec.8 [shell_redirect_next] + call shell_clear_buffer call shell_print_prompt shell_task_loop: mov r0, 1 - mov r1, [shell_terminal_stream_struct_ptr] + mov r1, [shell_stream_struct_ptr] mov r2, shell_char_buffer call read movz.8 r0, [shell_char_buffer] cmp.8 r0, 0 - ifnz call shell_task_parse_key + ifnz jmp shell_task_parse_key rjmp shell_task_loop @@ -51,7 +56,7 @@ shell_task_parse_key: call print_character_to_terminal call print_character_to_terminal call print_character_to_terminal - ret + jmp shell_task_loop shell_key_down_enter: ; clear the cursor from the screen mov r0, 8 ; backspace character @@ -68,15 +73,13 @@ shell_key_down_enter: call shell_push_character call shell_parse_line - call shell_clear_buffer - call shell_print_prompt - ret + jmp shell_task_return shell_key_down_backspace: ; check if we are already at the start of the prompt mov r1, [shell_text_buf_ptr] cmp r1, shell_text_buf_bottom - iflteq ret + iflteq jmp shell_task_loop ; delete the last character from the screen, draw the cursor, and pop the last character from the buffer mov r0, 8 ; backspace character call print_character_to_terminal @@ -92,7 +95,7 @@ shell_key_down_backspace: call print_character_to_terminal call print_character_to_terminal call print_character_to_terminal - ret + jmp shell_task_loop shell_print_prompt: call get_current_disk_id @@ -241,7 +244,7 @@ print_character_to_terminal: mov.8 [shell_char_buffer], r0 mov r0, 1 - mov r1, [shell_terminal_stream_struct_ptr] + mov r1, [shell_stream_struct_ptr] mov r2, shell_char_buffer call write @@ -259,7 +262,7 @@ print_str_to_terminal: push r0 push r2 - mov r1, [shell_terminal_stream_struct_ptr] + mov r1, [shell_stream_struct_ptr] mov r2, r0 print_str_to_terminal_loop: mov r0, 1 @@ -311,7 +314,10 @@ shell_args_ptr: data.32 0 ; pointer to the beginning of the command argum shell_prompt: data.str "> " data.8 CURSOR data.8 0 -shell_terminal_stream_struct_ptr: data.32 0 +shell_stream_struct_ptr: data.32 0 +shell_old_stream_struct_ptr: data.32 0 +shell_redirect_next: data.8 0 +shell_redirect_stream_struct: data.fill 0, 32 shell_char_buffer: data.32 0 #include "commands/commands.asm"