*****************************************
*		MountENV		*
*    Mount tool for HappyENV-Handler	*
*	      1.0  05.06.97		*
*****************************************

; MountENV is Public Domain

;Can be started from WB and CLI
;Tabsize=8


pr_CLI		= 172
pr_MsgPort	= 92
pr_CurrentDir	= 152
sm_NumArgs	= 28
sm_ArgList	= 36

dn_Next		= 0
dn_Type		= 4
dn_Task		= 8
dn_Lock		= 12
dn_Handler	= 16
dn_StackSize	= 20
dn_Priority	= 24
dn_Startup	= 28
dn_SegList	= 32
dn_GlobalVec	= 36
dn_Name		= 40
dn_SIZEOF	= 44

ACCESS_READ	= -2

_LVOAllocMem	= -198
_LVOFindTask	= -294
_LVOWaitPort	= -384
_LVOGetMsg	= -372
_LVOReplyMsg	= -378
_LVOOpenLibrary	= -552
_LVOCloseLibrary = -414
_LVOForbid	= -132
_LVOLock	= -84
_LVOUnLock	= -90
_LVOReadArgs	= -798
_LVOFreeArgs	= -858
_LVOAddDosNode	= -150



;init place for needed structures on stack
start	clr.l	-(sp)			;get place on stack
	clr.l	-(sp)
	clr.l	-(sp)
	clr.l	-(sp)
	moveq	#20,d5			;set return-code to FAIL
	move.l	4.w,a6			;exec-base to a6
	sub.l	a1,a1
	jsr	_LVOFindTask(a6)	;find this task
	move.l	d0,a4			;a4=this task

;check for WB or CLI start
	tst.l	pr_CLI(a4)
	bne.s	CLI
	lea	pr_MsgPort(a4),a0	;it was WB-start
	jsr	_LVOWaitPort(a6)
	lea	pr_MsgPort(a4),a0
	jsr	_LVOGetMsg(a6)
	move.l	d0,d2			;d2=message from WB
	jsr	_LVOForbid(a6)		;must be in forbid-state
	move.l	d2,a1
	jmp	_LVOReplyMsg(a6)	;THE END ! from WB, done nothing


CLI	moveq	#dn_SIZEOF,d0		;get mem for device node
	move.l	#1<<16!1,d1		;CLEAR and PUBLIC
	jsr	_LVOAllocMem(a6)
	tst.l	d0
	beq	Ende2
	move.l	d0,a2			;a2=device node

	lea	handler(pc),a1		;init device node
	bsr	CopyBSTR
	beq	Ende2
	move.l	d0,dn_Handler(a2)
	move.l	#2000,dn_StackSize(a2)	;should be more than enough
	moveq	#5,d0
	move.l	d0,dn_Priority(a2)	;priority=5
	moveq	#-1,d0			;no global vector
	move.l	d0,dn_GlobalVec(a2)

DOS	lea	dosname(pc),a1		;open dos.library
	moveq	#30,d0
	jsr	_LVOOpenLibrary(a6)
	tst.l	d0
	beq	Ende2
	move.l	d0,a6
	lea	envarc(pc),a0
	move.l	a0,d1
	moveq	#ACCESS_READ,d2
	jsr	_LVOLock(a6)		;make real assign out of
	move.l	d0,d1			;defer assign to ENVARC:
	beq.s	Args
	jsr	_LVOUnLock(a6)

Args	moveq	#0,d6			;no startup field
	lea	templat(pc),a0
	move.l	a0,d1
	move.l	sp,d2			;a2=sp=arg-array
	moveq	#0,d3
	jsr	_LVOReadArgs(a6)	;process argumentline
	move.l	d0,d4			;d4=RdArgs-struct
	beq.s	Ende3
	lea	envname(pc),a1
	move.l	(sp),d0
	beq.s	.A
	move.l	d0,a1
.A	bsr	CopyBSTR
	beq.s	Ende3
	move.l	d0,dn_Name(a2)
	move.l	4(sp),d0
	beq.s	.C
	move.l	d0,a1
	bsr	CopyBSTR
	beq.s	.C
	move.l	d0,dn_Startup(a2)

.C	move.l	d4,d1
	jsr	_LVOFreeArgs(a6)	;free RdArgs-struct, if needed
	move.l	a6,a1
	move.l	4.w,a6
	jsr	_LVOCloseLibrary(a6)

Exp	lea	expname(pc),a1	;open expansion.library
	moveq	#30,d0
	move.l	4.w,a6
	jsr	_LVOOpenLibrary(a6)
	tst.l	d0
	beq.s	Ende2
	move.l	d0,a6
	move.l	a2,a0		;add new device node
	moveq	#0,d0
	moveq	#0,d1
	jsr	_LVOAddDosNode(a6)
	tst.l	d0
	beq.s	Ende3
	moveq	#0,d5		;set return code to OK

;clean up all things used
Ende3	move.l	a6,a1
	move.l	4.w,a6
	jsr	_LVOCloseLibrary(a6)

;end program
Ende2	lea	16(sp),sp	;restore stack
	move.l	d5,d0		;set return-code
	rts			;THE END ! from CLI



* Copy CSTR to BSTR in its own memory block
CopyBSTR	;entry: a1=string
		;exit:  d0=BSTR
	movem.l	d2/a0-a2/a6,-(sp)
	move.l	a1,a2
.test	tst.b	(a1)+
	bne.s	.test
	sub.l	a2,a1
	move.l	a1,d2
	subq.l	#1,d2
	beq.s	.end
	move.l	a1,d0
	addq.l	#1,d0
	moveq	#1,d1
	move.l	4.w,a6
	jsr	_LVOAllocMem(a6)
	move.l	d0,a1
	lsr.l	#2,d0
	beq.s	.end
	move.b	d2,(a1)+
.loop	move.b	(a2)+,(a1)+
	bne.s	.loop
	tst.l	d0
.end	movem.l	(sp)+,d2/a0-a2/a6
	rts

version	dc.b	"$VER: MountENV 1.0 (05.06.97)",10,0
templat	dc.b	"DEVNAME/K,STARTUP/F",0
dosname	dc.b	"dos.library",0
expname	dc.b	"expansion.library",0
envarc	dc.b	"ENVARC:",0
handler	dc.b	"L:HappyENV-Handler",0
envname	dc.b	"ENV",0
		end

