;           IF.ASM by: Ward Christensen (as of 08/30/81)
; 
;   Disassembled from CPMUG Volume 78 by: Kelly Smith (05/14/82)
; 
; 
; 'IF'  allows  you  to conditionally abort or continue  a  SUBMIT, 
; depending upon whether a file exists or not.
; 
; Examples, in a submit file command string might be:
; 
; if foo.zot continue	; continues submit if foo.zot exists
; if foo.zot abort	; aborts submit if foo.zot exists
; 
;                                              - WC -
; 
; There is a simple elegance that Ward Christensen creates,  in the 
; use  of  CP/M Function Calls...I was intrigued with what  he  had 
; done  here,  so  I disassembled it for my own interest  ('IF'  is 
; supplied on CPMUG Volume 78 as a '.COM' file only).   This should 
; be  a one-for-one correspondence to the original  file,  with  no 
; superfluous changes...just straight forward Christensen coding.  
; 
;                                              - KS -
;
;
;


	ORG	100H

bdos	EQU	05H	; bdos entry address
fcb	EQU	5CH	; primary file control block
sfcb	EQU	6CH	; secondary file control block

lf	equ	0ah	; line feed character
cr	equ	0dh	; carriage return character

	CALL	start
;
	DB	'IF.COM by Ward C. as of '
	DB	'08/30/81',cr,lf,'$'
;
start:	POP	D		; get pointer to previous message
	LXI	H,0		; save old CP/M stack pointer
	DAD	SP
	SHLD	oldstk
	LXI	SP,oldstk	; make new stack
	MVI	C,9		; print string function
	CALL	bdos
	LXI	H,cont$opt	; check if CONTINUE option
	LXI	B,2		; option length
	LXI	d,sfcb+1	; point to command string
	CALL	opt$comp
	JZ	do$cont
	LXI	H,abrt$opt	; check if ABORT option
	LXI	B,2		; option length	
	LXI	d,sfcb+1	; point to command string
	CALL	opt$comp
	JZ	do$abrt
	LXI	H,quit$opt	; check if QUIT option
	LXI	B,2		; option length
	LXI	d,sfcb+1	; point to command string
	CALL	opt$comp
	JZ	do$abrt
	CALL	del$sub		; delete '$$$.SUB' file
	CALL	msgxit
;
	DB	'++ Invalid option on "IF" ++'
	DB	cr,lf
	DB	'++ Not "Abort" or "Continue" ++$'
;
; do CONTINUE routine
;
do$cont:
;
	MVI	C,00FH		; open file function
	LXI	d,fcb		; point to filename
	CALL	bdos
	INR	A		; check return code
	JNZ	exit		; just exit, if file found	
	CALL	del$sub		; delete '$$$.SUB' file
	CALL	msgxit
;
	DB	'++ IF: Specified file does not exist',cr,lf
	DB	'++ SUBMIT ABORTED ++$'
;
; do ABORT option
;
do$abrt:
;
	MVI	C,00FH		; open file function
	LXI	d,fcb		; point to filename
	CALL	bdos
	INR	A		; check return code
	JZ	exit		; just exit, if file not found 
	CALL	del$sub		; delete '$$$.SUB' file
	CALL	msgxit
;
	DB	'++ IF: Specified file already exists',cr,lf
	DB	'++ SUBMIT ABORTED ++$'
;
; delete '$$$.SUB' file
;
del$sub:
;
	MVI	A,1		; set disk 'A' in fcb
	STA	fcb
	LXI	H,sub$name	; move '$$$.SUB' to fcb
	LXI	B,11
	LXI	d,fcb+1
	CALL	move
	MVI	C,013H		; delete file function
	LXI	d,fcb
	CALL	bdos
	RET
;
cont$opt:	DB	'CO'

abrt$opt:	DB	'AB'

quit$opt:	DB	'QU'

sub$name:	DB	'$$$     SUB'
;
;
;
; move routine - Source in [HL], desination in [DE], length in [BC]
;
move:	MOV	A,M
	STAX	D
	INX	H
	INX	D
	DCX	B
	MOV	A,B
	ORA	C
	JNZ	move
	RET
;
;
;
; compare if option present in command string
;
opt$comp:
;
	LDAX	D
	CMP	M
	RNZ
	INX	D
	INX	H
	DCX	B
	MOV	A,B
	ORA	C
	JNZ	opt$comp
	RET
;
;
;
msgxit:	POP	D	; get message string pointer
	MVI	C,9	; print string function
	CALL	bdos
exit:	LHLD	oldstk	; restore old CP/M stack pointer
	SPHL
	RET		; return to CP/M (no warm boot)
;	
	ds	64	; 32 level stack
oldstk:	ds	2	; old CP/M stack pointer
;
;
;
	END
