
*	FindName
*	By Preben Nielsen
*
*HISTORY
*          Made with Hisoft V2.12
*
*  V1.0   10-Sep-91: First attempt. Works of course.


	OPT O+
	OPT O1+			; Tells when a branch could be optimised to short
	OPT i+			; Tells when '#' is probably missing

		incdir		"AsmInc:"
		include		"P.i"
		include		"relMacros.i"
		include		"exec/execbase.i"
		include		"exec/exec_lib.i"
		include		"libraries/dosextens.i"

DB		EQUR		A4

 dcDeclare	A4
 dcArea		ArgTable,10*4	; Here ArgParse leaves some pointers
 dcArea		ArgLine,100	; Copy of the commandline
 dcEnd

Start		dcAlloc
		dcReset
		lea		ArgLine(DB),A1
		move.l		D0,D1
		bra.S		2$
1$		move.b		(A0)+,(A1)+
2$		dbf		D1,1$
		lea		ArgLine(DB),A0
		lea		ArgTable(DB),A1
		Call		ArgParse
		cmp.w		#2,D0		; Correct number of arguments ?
		blt		ArgError
		move.l		ArgTable(DB),A0
		cmp.b		#'-',(A0)+
		bne		ArgError
		move.b		(A0),D0
		cmp.b		#'d',D0		; Device ?
		beq.S		SearchDevice
		cmp.b		#'l',D0		; Library ?
		beq.S		SearchLibrary
		cmp.b		#'m',D0		; Memory ?
		beq.S		SearchMemory
		cmp.b		#'p',D0		; Port ?
		beq.S		SearchPort
		cmp.b		#'r',D0		; Resource ?
		beq.S		SearchResource
		cmp.b		#'t',D0		; Task ?
		beq.S		SearchTask
		bra.S		ArgError

SearchDevice	move.l		#DeviceList,D2	; Device !
		bra.S		SearchName
SearchLibrary	move.l		#LibList,D2	; Library !
		bra.S		SearchName
SearchMemory	move.l		#MemList,D2	; Memory !
		bra.S		SearchName
SearchPort	move.l		#PortList,D2	; Port !
		bra.S		SearchName
SearchResource	move.l		#ResourceList,D2; Resource !
		bra.S		SearchName
SearchTask	Prepare		Exec_Call	; Task !
		CallLib		Forbid
		move.l		ArgTable+4(DB),A1
		CallLib		FindTask
		bra.S		CheckResult
SearchName	Prepare		Exec_Call
		CallLib		Forbid
		lea		0(A6,D2.W),A0
		move.l		ArgTable+4(DB),A1
		CallLib		FindName
CheckResult	move.l		D0,D2
		CallLib		Permit
		tst.l		D2
		beq.S		NoLuck
FoundIt		moveq		#RETURN_OK,D0
		bra.S		LetsReturn
NoLuck		moveq		#RETURN_WARN,D0
		bra.S		LetsReturn
ArgError	moveq		#RETURN_ERROR,D0
LetsReturn	dcFree
		rts


*»»» Call:	A0 = String to parse
*»»»		A1 = Table to put arg-pointers
*»»»		D0 = Length of string to parse
*»»» Return:	D0 = Number of arguments
*»»»		Each argument in the string will be NULL-terminated
ArgParse	Push		D1-D2/A0-A3
		move.l		A1,A3
		clr.b		-1(A0,D0)
		moveq		#0,D1
ArgLoop
SpaceLoop	cmp.b		#' ',(A0)+
		beq.S		SpaceLoop
		move.b		-(A0),D0
		beq.S		DoneParse
		cmp.b		#'"',D0
		beq.S		QuoteParse
NormParse	move.l		A0,(A3)+
		addq.l		#1,D1
		moveq		#' ',D2
1$		move.b		(A0)+,D0
		beq.S		DoneParse
		cmp.b		D2,D0
		bne.S		1$
		bra.S		EndParse
QuoteParse	addq.l		#1,A0
		move.l		A0,(A3)+
		addq.l		#1,D1
		moveq		#'"',D2
1$		move.b		(A0)+,D0
		beq.S		QuoteError
		cmp.b		D2,D0
		bne.S		1$
EndParse	clr.b		-1(A0)
		bra.S		ArgLoop
QuoteError	moveq		#0,D1
DoneParse	move.l		D1,D0
		Pop		D1-D2/A0-A3
		rts
		END

