; CP4WLD.ASM
;	KERMIT - (Celtic for "FREE")
;
;	This is the CP/M-80 implementation of the Columbia University
;	KERMIT file transfer protocol.
;
;	Version 4.0
;
;	Copyright June 1981,1982,1983,1984
;	Columbia University
;
; Originally written by Bill Catchings of the Columbia University Center for
; Computing Activities, 612 W. 115th St., New York, NY 10025.
;
; Contributions by Frank da Cruz, Daphne Tzoar, Bernie Eiben,
; Bruce Tanner, Nick Bush, Greg Small, Kimmo Laaksonen, Jeff Damens, and many
; others. 
;
;	Multi-file access subroutine.  Allows processing of multiple files
;	(i.e., *.ASM) from disk.  This routine builds the proper name in the
;	FCB each time it is called.  This command would be used in such pro-
;	grams such as modem transfer, tape save, etc. in which you want to
;	process single or multiple files.
;	Note that it will fail if more than 256 entries match the wildcard.
;
; revision history:
; edit 3: July 27, 1984
;	support LASM: remove exclamation points, link to CP4CMD.
;
; edit 2: June 7, 1984 (CJC)
;	formatting and documentation; add module version string; redo movfcb,
;	in preparation for moving DMA buffer (later...).
;
; edit 1: May, 1984 (CJC)
;	extracted from CPMBASE.M80 version 3.9; modifications are described
;	in the accompanying .UPD file.
;
wldver:	db	'CP4WLD.ASM (3) 27-Jul-84$'

;	The FCB will be set up with the next name, ready to do normal 
;	processing (OPEN, READ, etc.) when routine is called.
;
;	Carry is set if no more names can be found
;
;	MFFLG1 is count/switch [0 for first time thru, pos for all others]
;	MFFLG2 is counted down for each successive GETNEXT file call
;
;	Technique used is to repeat SFIRST/SNEXT sequence N+1 times for each
;	successive call, till sequence fails. CP/M does NOT allow disk-handling
;	between SFIRST and SNEXT.
;	called by: send, seof, dir

mfname:	ora	a		; clear carry
	push	b		;Save registers
	push	d
	push	h
	mvi	c,setdma	;Init DMA addr, FCB
	lxi	d,80H
	call	bdos
	xra	a		;A = 0
	sta	fcbext		;clear extension
	lda	mfflg1		;find out if "second" call in row
	ora	a
	jnz	mfn01		;Were here before
	sta	mfflg2
	lxi	h,fcb
	lxi	d,mfreq
	lxi	b,12
	call	mover		;.from FCB to MFREQ
	mvi	c,SFIRST	;Search first
	lxi	d,fcb
	call	bdos
	jmp	mfn02		;and check results

mfn01:	dcr	a
	sta 	mfflg2		;store down-counter
	lxi	h,mfreq		;SFIRST REQ name
	lxi	d,fcb
	lxi	b,12
	call	mover		;.from MFREQ to FCB
	mvi	c,sfirst	;Search first old one,we got it before
	lxi	d,fcb
	call	bdos		;no error's expected -we got that before
mfn01a:
	mvi	c,snext		;Search next
	call	bdos
mfn02:	push	psw
	lda	mfflg2		;get "repeat file counter"
	ora 	a
	jz	mfn02a		;if zero, check if SNEXT had ERROR
	dcr	a		;count down
	sta	mfflg2		;store back
	pop	psw		;no error-check, we got it before
	jmp	mfn01a		;next SNEXT

mfn02a:	pop	psw
	ora	a
	jm	mffix2		;No (more) found
	call	movfcb		;move data to fcb
	lda	mfreq		;the original disk-designator
	sta	fcb		;back into fcb
	lda	mfflg1		;get file-flag
	inr	a		;increment
	sta	mfflg1		;and store for next go-around
	mvi	a,0		;Setup FCB
	sta	fcbext		;clean up FCB for OPEN etc
	sta	fcbrno
mffix1:	pop	h		;restore registers
	pop	d
	pop	b
	ret			;and return

mffix2:	stc			;set carry
	jmp	mffix1		;return with CARRY set

;	copy directory entry to FCB
;	called with A/ entry number in directory (0-3)
;		    directory block in DMA buffer (buff)

movfcb:	add 	a
	add	a
	add 	a
	add	a
	add	a		;* 32
	mov	c,a		; copy offset to bc
	mvi	b,0		; (high byte is zero)
	lxi	h,buff		; get start of disk buffer
	dad	b		; calculate start of directory entry
	lxi	d,fcb
	lxi	b,12
	call	mover
	ret

;	Data storage for MFNAME (multi-file access)
mfreq:	DS	12		;Requested name
mfflg1:	DB	0		;First time thru flag for MFNAME
mfflg2:	DB	0		;Down counter for MFNAME
;
IF lasm
	LINK	CP4CMD
ENDIF;lasm
