Subversion Repositories SvarDOS

Rev

Rev 517 | Rev 520 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
349 mateuszvis 1
;
2
; rmod - resident module of the SvarCOM command interpreter
3
;
4
; Copyright (C) 2021 Mateusz Viste
5
; MIT license
6
;
7
; this is installed in memory by the transient part of SvarCOM. it has only
8
; two jobs: providing a resident buffer for command history, environment, etc
9
; and respawning COMMAND.COM whenever necessary.
10
 
11
CPU 8086
459 mateuszvis 12
org 0x100
349 mateuszvis 13
 
459 mateuszvis 14
PSP_ENVSEG equ 0x2C
15
 
349 mateuszvis 16
section .text    ; all goes into code segment
17
 
350 mateuszvis 18
                 ; offset
19
SIG1 dw 0x1983   ;  +0
20
SIG2 dw 0x1985   ;  +2
21
SIG3 dw 0x2017   ;  +4
490 mateuszvis 22
SIG4 dw 0x2019   ;  +6  this acts also as a guardval to detect stack overflows
349 mateuszvis 23
 
490 mateuszvis 24
; DOS int 21h functions that I use require at least 40 bytes of stack under
25
; DOS-C (FreeDOS) kernel, so here I reserve 64 bytes juste to be sure
26
STACKBUF db "XXX  SVARCOM RMOD BY MATEUSZ VISTE  XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
27
STACKPTR dw 0
349 mateuszvis 28
 
353 mateuszvis 29
; exit code of last application
490 mateuszvis 30
LEXCODE  dw 0    ; +4Ah
353 mateuszvis 31
 
366 mateuszvis 32
; offset of the COMSPEC variable in the environment block, 0 means "use
33
; boot drive". this value is patched by the transient part of COMMAND.COM
490 mateuszvis 34
COMSPECPTR dw 0  ; +4Ch
349 mateuszvis 35
 
366 mateuszvis 36
; fallback COMSPEC string used if no COMPSEC is present in the environment
37
; drive. drive is patched by the transient part of COMMAND.COM
490 mateuszvis 38
COMSPECBOOT db "@:\COMMAND.COM", 0 ; +4Eh
350 mateuszvis 39
 
460 mateuszvis 40
; ExecParamRec used by INT 21h, AX=4b00 (load and execute program), 14 bytes:
41
;  offset  size  content
42
;     +0     2   segment of environment for child (0 = current)
43
;     +2     4   address of command line to place at PSP:0080
44
;     +6     4   address of an FCB to be placed at PSP:005c
45
;    +0Ah    4   address of an FCB to be placed at PSP:006c
490 mateuszvis 46
EXEC_PARAM_REC db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0   ; +5Dh
366 mateuszvis 47
 
490 mateuszvis 48
; Program to execute, preset by SvarCOM (128 bytes, ASCIIZ)  ; +6Bh
461 mateuszvis 49
EXECPROG dd 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
460 mateuszvis 50
 
517 mateuszvis 51
; offset within EXECPROG for out and in filenames in case stdin or stdout
52
; needs to be redirected (0xffff=no redirection)
53
REDIR_OUTFIL dw 0xffff    ; +EBh
54
REDIR_INFIL dw 0xffff     ; +EDh
55
REDIR_OUTAPPEND dw 0      ; +EFh
461 mateuszvis 56
 
517 mateuszvis 57
skipsig:         ; +F1h
58
 
349 mateuszvis 59
; set up CS=DS=SS and point SP to my private stack buffer
60
mov ax, cs
61
mov ds, ax
62
mov es, ax
63
mov ss, ax
64
mov sp, STACKPTR
65
 
517 mateuszvis 66
; revert stdin/stdout redirections (if any) to their initial state
67
call REVERT_REDIR_IF_ANY
68
 
69
; redirect stdout if required
70
call REDIR_OUTFILE_IF_REQUIRED
71
 
460 mateuszvis 72
; should I executed command.com or a pre-set application?
73
or [EXECPROG], byte 0
74
jz EXEC_COMMAND_COM
75
 
461 mateuszvis 76
; TODO: perhaps I should call the DOS SetPSP function here? But if I do, the
77
;       int 21h, ah=50h call freezes...
78
;mov ah, 0x50           ; DOS 2+ -- Set PSP
79
;mov bx, cs
80
;int 0x21
460 mateuszvis 81
 
82
; exec an application preset (by SvarCOM) in the ExecParamRec
83
mov ax, 0x4B00         ; DOS 2+ - load & execute program
461 mateuszvis 84
mov dx, EXECPROG       ; DS:DX  - ASCIZ program name (preset at PSP[already)
460 mateuszvis 85
mov bx, EXEC_PARAM_REC ; ES:BX  - parameter block pointer
86
int 0x21
465 mateuszvis 87
mov [cs:EXECPROG], byte 0 ; do not run app again (+DS might have been changed)
460 mateuszvis 88
 
463 mateuszvis 89
jmp short skipsig      ; enforce valid ds/ss/etc (can be lost after int 21,4b)
90
 
460 mateuszvis 91
EXEC_COMMAND_COM:
92
 
353 mateuszvis 93
; collect the exit code of previous application
94
mov ah, 0x4D
95
int 0x21
96
xor ah, ah          ; clear out termination status, I only want the exit code
97
mov [LEXCODE], ax
98
 
460 mateuszvis 99
; zero out the exec param block (14 bytes)
100
mov al, 0              ; byte to write
101
mov cx, 14             ; how many times
102
mov di, EXEC_PARAM_REC ; ES:DI = destination
103
cld                    ; stosb must move forward
104
rep stosb              ; repeat cx times
105
 
366 mateuszvis 106
; preset the default COMSPEC pointer to ES:DX (ES is already set to DS)
107
mov dx, COMSPECBOOT
108
 
109
; do I have a valid COMSPEC?
110
or [COMSPECPTR], word 0
111
jz USEDEFAULTCOMSPEC
459 mateuszvis 112
; set ES:DX to actual COMSPEC (in env segment)
113
mov es, [PSP_ENVSEG]
366 mateuszvis 114
mov dx, [COMSPECPTR]
115
USEDEFAULTCOMSPEC:
116
 
349 mateuszvis 117
; prepare the exec param block
459 mateuszvis 118
mov ax, [PSP_ENVSEG]
350 mateuszvis 119
mov [EXEC_PARAM_REC], ax
465 mateuszvis 120
mov [EXEC_PARAM_REC+2], word CMDTAIL
442 mateuszvis 121
mov [EXEC_PARAM_REC+4], cs
349 mateuszvis 122
 
123
; execute command.com
124
mov ax, 0x4B00         ; DOS 2+ - load & execute program
366 mateuszvis 125
push es                ;
126
pop ds                 ;
127
;mov dx, COMSPEC       ; DS:DX  - ASCIZ program name (preset already)
128
push cs
129
pop es
349 mateuszvis 130
mov bx, EXEC_PARAM_REC ; ES:BX  - parameter block pointer
131
int 0x21
132
 
133
; if all went well, jump back to start
134
jnc skipsig
135
 
366 mateuszvis 136
; restore DS=CS
137
mov bx, cs
138
mov ds, bx
139
 
349 mateuszvis 140
; update error string so it contains the error number
141
add al, '0'
142
mov [ERRLOAD + 4], al
143
 
366 mateuszvis 144
; display error message
349 mateuszvis 145
mov ah, 0x09
146
mov dx, ERRLOAD
147
int 0x21
148
 
149
; wait for keypress
150
mov ah, 0x08
151
int 0x21
152
 
153
; back to program start
154
jmp skipsig
155
 
460 mateuszvis 156
; command.com tail arguments, in PSP format: length byte followed by args and
465 mateuszvis 157
; terminated with \r) - a single 0x0A byte is passed so SvarCOM knows it is
158
; called as respawn (as opposed to being invoked as a normal application)
159
; this allows multiple copies of SvarCOM to stack upon each other.
160
CMDTAIL db 0x01, 0x0A, 0x0D
442 mateuszvis 161
 
366 mateuszvis 162
ERRLOAD db "ERR x, FAILED TO LOAD COMMAND.COM", 13, 10, '$'
517 mateuszvis 163
 
164
; variables used to revert stdin/stdout to their initial state
165
OLD_STDOUT dw 0xffff
166
OLD_STDIN  dw 0xffff
167
 
168
 
169
; *** ROUTINES ***************************************************************
170
 
171
 
172
; revert stdin/stdout redirections (if any) to their initial state
173
; all memory accesses are CS-prefixes because this code may be called at
174
; times when DS is out of whack.
175
REVERT_REDIR_IF_ANY:
176
; is stdout redirected?
177
mov bx, [OLD_STDOUT]
178
cmp bx, 0xffff
179
je STDOUT_DONE
519 mateuszvis 180
; revert the stdout handle (dst in BX already)
517 mateuszvis 181
mov cx, 1        ; src handle (1=stdout)
182
mov ah, 0x46     ; redirect a handle
183
int 0x21
519 mateuszvis 184
; close the old handle (still in bx)
185
mov ah, 0x3e
186
int 0x21
517 mateuszvis 187
mov [OLD_STDOUT], word 0xffff ; mark stdout as "not redirected"
188
STDOUT_DONE:
189
ret
190
 
191
 
192
; redirect stdout if REDIR_OUTFIL points to something
193
REDIR_OUTFILE_IF_REQUIRED:
194
mov si, [REDIR_OUTFIL]
195
cmp si, 0xffff
196
je NO_STDOUT_REDIR
197
add si, EXECPROG       ; si=output file
198
mov ax, 0x6c00         ; Extended Open/Create
199
mov bx, 1              ; access mode (0=read, 1=write, 2=r+w)
200
xor cx, cx             ; file attribs when(if) file is created (0=normal)
201
mov dx, [REDIR_OUTAPPEND] ; action if file exist (0x11=open, 0x12=truncate)
202
int 0x21               ; ax=handle on success (CF clear)
203
mov [REDIR_OUTFIL], word 0xffff
204
jc NO_STDOUT_REDIR     ; TODO: abort with an error message instead
205
; duplicate current stdout so I can revert it later
206
push ax                ; save my file handle in stack
207
mov ah, 0x45           ; duplicate file handle BX
208
mov bx, 1              ; 1=stdout
209
int 0x21               ; ax=new (duplicated) file handle
210
mov [OLD_STDOUT], ax   ; save the old handle in memory
211
; redirect stdout to my file
212
pop bx                 ; dst handle
213
mov cx, 1              ; src handle (1=stdout)
214
mov ah, 0x46           ; "redirect a handle"
215
int 0x21
216
; close the original file handle, I no longer need it
217
mov ah, 0x3e           ; close a file handle (handle in BX)
519 mateuszvis 218
int 0x21
517 mateuszvis 219
NO_STDOUT_REDIR:
220
ret