	title	WHICH - File Searcher
	name	('WHICH')
	maclib	base80

; Report where to find a program

; Call WHICH by :

; WHICH file

; All logged drives will be searched for 'file' referring to
; order defined by the SETDEF.COM utility.
; All user groups will be scanned for file, too

; Copyright (c)	Werner Cirsovius
;		Hohe Weide 44
;		D-2000 Hamburg 20
;		Phone: +49 40/4223247

;		Version 1.0, May 1992

_VER	macro
	db	'WHICH 1.0'
	endm

_string	equ	 9
_LOG	equ	24
_SCB	equ	49

_Ord	equ	18h
_get	equ	0

_MaxUsr	equ	16

NoMSB	equ	01111111b
_ATT	equ	7

	ext	GetVer,UsrGet,UsrSet,CurDrv,SelDrv,String
	ext	SrcFrs,Decout,Conout,crlf

	dseg

$HEAD:
	_VER
	db	cr,lf,lf,eot
$ILL.CPU:
	db	'WHICH requires Z80 CPU',eot
$ILL.OS:
	db	'WHICH requires CP/M PLUS (3.x)',eot
$NONE:
	db	'No such file found',eot
$HELP:
	db	'Report where to find a program',cr,lf,lf
	db	'Call WHICH by :',cr,lf,lf
	db	'WHICH file',cr,lf,lf
	db	'All logged drives will be searched for '
	db	'''file'' referring to',cr,lf
	db	'order defined by the SETDEF.COM utility.'
	db	cr,lf
	db	'All all user groups will be scanned for '
	db	'file, too',cr,lf,eot
$SYS:
	db	' * SYS',eot
ExeTab:
	dw	ORD.00,ORD.01,ORD.10,ORD.11
$COM:
	db	'COM'
$SUB:
	db	'SUB'
Found:
	db	FALSE
@SrcPB:
	db	_Ord
	db	_get
CurDsk:
	db	0
CurUsr:
	dw	0
ORDER:
	dw	0
$NUM:
	db	'65535',eot

	cseg

WHICH:
	sub	a		; Test CPU
	jp	po,CPU.OK
	ld	de,$ILL.CPU
	ld	c,_string
	call	BDOS		; Tell error
	jp	OS
CPU.OK:
	ld	sp,(TPAtop)
	call	GetVer		; Get OS version
	ld	de,$ILL.OS
	jr	c,EndWHICH	; .. invalid
	ld	a,(FCBnam)	; Test name here
	cp	' '
	ld	de,$HELP
	jr	z,EndWHICH	; .. should be
	ld	de,$HEAD
	call	String
	call	UsrGet		; Get current user
	push	af
	call	CurDrv		; .. and drive
	push	af
	call	DoWHICH
	pop	af
	call	SelDrv		; Reset drive
	pop	af
	call	UsrSet		; .. and user
	ld	a,(Found)	; Test any found
	cp	TRUE
	jp	z,OS
	ld	de,$NONE	; .. nope
EndWHICH:
	call	String		; Tell any
	jp	OS
;
; The main task
;
DoWHICH:
	call	GetSrcCH	; Get order range
	call	GetLOGIN	; Get login vector
	ld	bc,256*('P'-'A'+1)+'A'-'A'
WHICH.loop:
	push	bc
	push	hl
	bit	0,l		; Test drive set
	call	nz,SrcFile	; .. search
	pop	hl
	pop	bc
	srl	h		; Shift vector
	rr	l
	inc	c		; .. next drive
	djnz	WHICH.loop
	ret
;
; Get order execution
;
GetSrcCH:
	ld	de,@SrcPB
	ld	c,_SCB
	call	BDOS		; Get order chain
	rra			; .. into lower bits
	rra
	and	00000110b	; .. * 2 for index
	ld	e,a
	ld	d,0
	ld	hl,ExeTab
	add	hl,de
	ld	e,(hl)		; Get address
	inc	hl
	ld	d,(hl)
	ld	(ORDER),de	; Save routine
	ret
;
; Get login vector
; EXIT	Reg HL holds vector
;
GetLOGIN:
	ld	c,_LOG
	call	BDOS		; .. get it
	ret
;
; Search file from FCB
; EXIT	Carry set if not found
;	Reg HL points to name of file
;
FndFile:
	ld	de,FCB
	call	SrcFrs		; Search file
	ret
;
; Look for files in drive
; ENTRY	Reg C holds drive
;
SrcFile:
	ld	a,c
	ld	(CurDsk),a	; Save disk
	call	SelDrv		; .. select it
	ld	bc,_MaxUsr*256+0
..Src:
	push	bc
	ld	a,c
	ld	(CurUsr),a	; Save user
	call	UsrSet		; .. set it
	call	Search		; .. go search
	pop	bc
	inc	c
	djnz	..Src
	ret
;
; Do the file search
;
Search:
	ld	hl,(ORDER)
	jp	(hl)		; Execute routine
;
; The chain executions
;
; .COM only
;
ORD.00:
	call	SetCOM		; Set extension .COM
..ORD:
	call	FndFile		; Search file
..print:
	call	nc,TypeFile	; .. found, tell it
	ret
;
; .COM, then .SUB
;
ORD.01:
	call	SetCOM		; Set extension .COM
	call	FndFile
	jr	nc,..print	; .. go it
	call	SetSUB		; Set extension .SUB
	jr	..ORD
;
; .SUB, then .COM
;
ORD.10:
	call	SetSUB		; Set extension .SUB
	call	FndFile		; .. find file
	jr	nc,..print	; .. got it
	jr	ORD.00		; .. try .COM
;
; None of above
;	
ORD.11:
	ret
;
; Give name of file
;
TypeFile:
	ld	a,TRUE
	ld	(Found),a	; .. mark found
	push	hl
	ld	a,(CurDsk)
	add	a,'A'		; Make ASCII drive
	call	Conout		; .. print
	ld	a,'/'
	call	Conout
	ld	hl,(CurUsr)
	ld	a,l
	cp	10		; Test range
	ld	a,' '
	call	c,Conout
	ld	de,$NUM
	push	de
	ld	b,eot
	call	Decout		; .. get decimal
	pop	de
	call	String		; .. print
	ld	a,':'
	call	Conout		; .. delimiter
	pop	hl		; Get name
	push	hl
	call	PrFCB		; .. print
	pop	ix
	bit	_ATT,(ix+_SYS-1)
	ld	de,$SYS
	call	nz,String	; .. Test SYS attribute
	call	crlf		; .. close
	ret
;
; Print name of file
; ENTRY	Reg HL holds FCB
;
PrFCB:
	ld	b,.fname
	call	..Pr		; Do name
	ld	a,'.'
	call	Conout		; Give delimiter
	ld	b,.fext
..Pr:
	ld	a,(hl)
	and	NoMSB
	cp	' '
	call	nz,Conout	; .. only non blanks
	inc	hl
	djnz	..Pr
	ret
;
; Set extension .COM
;
SetCOM:
	ld	hl,$COM
	jr	..Set
;
; Set extension .SUB
;
SetSUB:
	ld	hl,$SUB
..Set:
	ld	de,FCBext
	ld	bc,.fext
	ldir			; Set extension
	ret

	end	WHICH


