sh: Implement support for batch files

main
Ry 2023-12-16 22:30:26 -08:00
parent 87ac29eb0d
commit 956c527391
2 changed files with 62 additions and 0 deletions

View File

@ -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

View File

@ -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"