1917 TITLE ErrLvl.ASM ;==== Executes a program and shows the errorlevel afterwards ;---------------------------------------------------------------------- INCLUDE SCREEN.MAC INCLUDE KEYB.MAC B EQU DS:BYTE PTR 0 W EQU DS:WORD PTR 0 CSEG SEGMENT ASSUME CS:CSEG,DS:CSEG ORG 100h ;--------------------------------------------------------------------- Start: jmp Begin fcb1 db 40 dup (0) fcb2 db 40 dup (0) parmblk dw 0 ; Segment address of environment string dd 0 ; Pointer to command line dd 0 ; Pointer to default FCB to be passed at PSP+5CH dd 0 ; Pointer to default FCB to be passed at PSP+6CH cmdline db 128 dup (0) old_path db '\',64 dup (0) path_str db 65 dup (0) stacksp db 32 dup ('STACK ') newstack dw 0 ;------------------------------------------------------------------------- Mshrink PROC NEAR mov ax,OFFSET Codesize ;Calculate number of bytes to keep mov cx,16 div cl and ax,0ffh ;Remove Remainder in AH add ax,100h ;Just to be sure... mov bx,ax mov ah,04aH ;Mshrink int 21H ret Mshrink ENDP ;-------------------------------------------------------------------------- Param_Read PROC NEAR xor cx,cx xor bx,bx mov cl,byte ptr ds:[80h] cmp cl,0 jnz Not_Empty call Show_Usage ret Not_Empty: mov si,81h call get_char ;Search for first non-space or non-tab ch. cmp cx,0 ;Only spaces or tabs? jnz go_on call Show_Usage ;True, show usage ret go_on: push si ;Save start of [path]filename string push cx call get_curpath call chg_path ;Change to new path if exists in string pop cx pop si push si inc si dec cx fn_lp: ;Loop as long as "real" characters cmp b[si],20h jbe go_on2 inc si loop fn_lp go_on2: mov al,[si] mov b[si],0 ;Mark end of filename string with '0' cmp cx,00h ;End of string? jz no_params ;Yes, finished inc si mov cx,00h ;Clear char counter mov di,OFFSET cmdline+1 par_lp: ;Build command line for child program cld movsb inc cx cmp b[si],0dh jz ready jmp par_lp ready: mov b[cmdline],cl no_params: clc pop si ret Param_Read ENDP ;---------------------------------------------------------------------- get_char PROC NEAR nxt: cmp b[si],020h ja rdy inc si loop nxt rdy: ret get_char ENDP ;---------------------------------------------------------------------- get_curpath PROC NEAR ;Get current path push si mov ah,47h mov dl,0h mov si,OFFSET old_path+1 int 21h pop si ret get_curpath ENDP rest_path PROC NEAR mov ah,3bh ;Restore old path mov dx,OFFSET old_path int 21h ret rest_path ENDP ;------------------------------------------------------------------------- chg_path PROC NEAR mov di,si add di,cx mov al,'\' std repne scasb ;Search for '\' from beginning ;to end of string cmp cx,0 jz no_path inc cx mov di,OFFSET path_str cld rep movsb mov ah,3bh mov dx,OFFSET path_str int 21h no_path: clc ret chg_path ENDP ;------------------------------------------------------------------------- show_usage PROC NEAR Print 'Executes a program and shows the ERRORLEVEL on return' call crlf Print 'usage: ERRLVL [drive:][path]filename.ext [command parameters]' call crlf mov al,01h stc ret show_usage ENDP ;-------------------------------------------------------------------------- Exec PROC NEAR push ds mov bp,OFFSET parmblk mov [bp+2],OFFSET cmdline mov [bp+4],CS mov [bp+6],OFFSET fcb1 mov [bp+8],CS mov [bp+10],OFFSET fcb2 mov [bp+12],CS mov ah,4BH ; EXEC mov al,00H ; Load and Execute mov dx,si mov bx,OFFSET parmblk int 21H pushf call rest_path ;Restore old_path popf jnc Exec_OK Print 'File not found' call crlf stc mov al,01h jmp Exec_Error Exec_OK: clc Exec_Error: pop ds ret Exec ENDP ;----------------------------------------------------------------------- Get_ErrLvl PROC NEAR mov ah,04dh ;Get return code from Subprocess int 21h call crlf Print_A ' ==> ERRORLEVEL = ',70h push ax mov ah,0fh ;Push back cursor int 10h mov ah,03h int 10h sub dl,3 mov ah,02h int 10h pop ax mov dl,al Wr_ByteA ;Write errorlevel byte call crlf mov al,00h ret Get_ErrLvl ENDP ;------------------------------------------------------------------------ crlf PROC NEAR cr_lf ret crlf ENDP ;------------------------------------------------------------------------ Begin: mov sp,OFFSET newstack ;New stack call Param_Read jc Exit call Mshrink call Exec jc Exit call Get_ErrLvl Exit: mov ah,04ch int 21h ;--------------------------------------------------------------- Codesize: CSEG ENDS END Start . 0