From 956c527391178c6eee17f158d8fa19563dbcaa4e Mon Sep 17 00:00:00 2001 From: Ry Date: Sat, 16 Dec 2023 22:30:26 -0800 Subject: [PATCH] sh: Implement support for batch files --- applications/sh/batch.asm | 54 +++++++++++++++++++++++++++++++++++++++ applications/sh/main.asm | 8 ++++++ 2 files changed, 62 insertions(+) create mode 100644 applications/sh/batch.asm diff --git a/applications/sh/batch.asm b/applications/sh/batch.asm new file mode 100644 index 0000000..747ab01 --- /dev/null +++ b/applications/sh/batch.asm @@ -0,0 +1,54 @@ +; batch file routines + +; run a batch file whose name is pointed to by [shell_batch_filename_ptr] +; the batch file must end with `exit;` +; inputs: +; none +; outputs: +; none (does not return) +shell_run_batch: + ; open the batch file + call get_current_disk_id + mov r1, r0 + mov r0, [shell_batch_filename_ptr] + mov r2, shell_batch_file_struct + call open + cmp r0, 0 + ifz jmp shell_run_batch_failed_to_open + +shell_run_batch_next: + 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 +shell_run_batch_loop: + ; read a character from the file + mov r0, 1 + mov r1, shell_batch_file_struct + mov r2, shell_batch_file_char_buffer + call read + + ; if it isn't a semicolon or linefeed, push it to the command buffer + ; if it is a semicolon, run the command + movz.8 r0, [shell_batch_file_char_buffer] + cmp.8 r0, ';' + ifz jmp shell_run_batch_end_of_line + cmp.8 r0, 10 + ifnz call shell_push_character + + call yield_task + rjmp shell_run_batch_loop +shell_run_batch_end_of_line: + mov r0, 0 + call shell_push_character + call shell_parse_line + rjmp shell_run_batch_next + +shell_run_batch_failed_to_open: + mov r0, shell_run_batch_failed_to_open_string + call print_str_to_terminal + call end_current_task + +shell_batch_file_struct: data.fill 0, 32 +shell_batch_file_char_buffer: data.8 0 +shell_run_batch_failed_to_open_string: data.str "failed to open batch file" data.8 10 data.8 0 diff --git a/applications/sh/main.asm b/applications/sh/main.asm index e7431f5..a18992d 100644 --- a/applications/sh/main.asm +++ b/applications/sh/main.asm @@ -9,9 +9,15 @@ const SET_COLOR: 0xF2 const REDRAW_LINE: 0xFE pop [shell_stream_struct_ptr] + pop [shell_batch_filename_ptr] cmp [shell_stream_struct_ptr], 0 ifz call end_current_task + ; before dropping to an interactive prompt, + ; check if an argument was passed + cmp [shell_batch_filename_ptr], 0 + ifnz jmp shell_run_batch + shell_task_return: cmp.8 [shell_redirect_next], 0 ifnz mov [shell_stream_struct_ptr], [shell_old_stream_struct_ptr] @@ -322,8 +328,10 @@ 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_batch_filename_ptr: data.32 0 shell_char_buffer: data.32 0 + #include "batch.asm" #include "commands/commands.asm" #include "launch.asm"