*************************
*     NULL-Handler	*
*	12.11.95	*
* (C) Martin Gierich	*
*************************

* This is a Handler to provide a DOS device which does nothing like NIL:.
* Mountlist entry looks like:
*
* NULL:
*     Handler = L:NULL-Handler
*     Stacksize = 2000
*     Priority = 5
*     GlobVec = -1
* #


DEBUG	= 1

	INCDIR	source:stripinc/
	INCLUDE	exec/exec_lib.i
	INCLUDE	dos/dos_lib.i
	INCLUDE	dos/dosextens.i
	INCLUDE	dos/filehandler.i

CALL	MACRO
	jsr	_LVO\1(a6)
	ENDM

	IFD	DEBUG
DBUG1	MACRO
	movem.l	d0-d7/a0-a6,-(sp)
	move.l	\2,-(sp)
	lea.l	\1(pc),a0
	lea.l	(sp),a1
	lea	RawHook(pc),a2
	move.l	4.w,a6
	jsr	_LVORawDoFmt(a6)
	addq.l	#4,sp
	movem.l	(sp)+,d0-d7/a0-a6 
	ENDM

DBUG0	MACRO
	movem.l	d0-d7/a0-a6,-(sp)
	lea.l	\1(pc),a0
	lea	RawHook(pc),a2
	move.l	4.w,a6
	jsr	_LVORawDoFmt(a6)
	movem.l	(sp)+,d0-d7/a0-a6 
	ENDM
	ENDC

	IFND	DEBUG
DBUG1	MACRO
	ENDM
DBUG0	MACRO
	ENDM
	ENDC

mn_Node		= 0
fh_Port		= 4
DOS_TRUE	= -1
DOS_FALSE	= 0
_LVORawPutChar	= -516


;private main structure
		RSRESET
MyProcess	RS.L	1
MyMsgPort	RS.L	1
MyPacket	RS.L	1
MyDeviceNode	RS.L	1
MyFileHandle	RS.L	1
OpenCount	RS.L	1
MyStructSize	RS.W	0


*******************************************************

;a5=private struct
;a6=execbase
start:	movem.l	d2-d7/a2-a6,-(sp)
	DBUG0	txstart
	move.l	4.w,a6
	moveq	#MyStructSize,d0
	moveq	#1,d1
	CALL	AllocMem
	tst.l	d0
	beq	exit
	move.l	d0,a5

	sub.l	a1,a1
	CALL	FindTask
	move.l	d0,MyProcess(a5)
	move.l	d0,a0
	bset	#0,pr_Flags+3(a0)
	add.l	#pr_MsgPort,d0
	move.l	d0,MyMsgPort(a5)
	clr.l	OpenCount(a5)

	bsr	GetPacket	process startup message
	move.l	MyPacket(a5),a1
	move.l	dp_Arg3(a1),d0
	lsl.l	#2,d0
	move.l	d0,MyDeviceNode(a5)
	move.l	d0,a0
	move.l	MyMsgPort(a5),dn_Task(a0)
	clr.l	dn_SegList(a0)
	moveq	#DOS_TRUE,d0
	move.l	d0,dp_Res1(a1)
	bsr	ReplyPacket
	DBUG0	txed

loop	bsr	GetPacket
	move.l	MyPacket(a5),a0
	moveq	#DOS_TRUE,d0
	move.l	d0,dp_Res1(a0)
	move.l	dp_Type(a0),d0
	cmp.l	#ACTION_FINDINPUT,d0
	bne.s	.A
	bsr	OpenOldfile
	bra	loopend
.A	cmp.l	#ACTION_FINDOUTPUT,d0
	bne.s	.C
	bsr	OpenNewfile
	bra	loopend
.C	cmp.l	#ACTION_END,d0
	bne.s	.D
	bsr	CloseFile
	bra	loopend
.D	cmp.l	#ACTION_READ,d0
	bne.s	.E
	bsr	ReadFile
	bra	loopend
.E	cmp.l	#ACTION_WRITE,d0
	bne.s	.F
	bsr	WriteFile
	bra	loopend
.F	cmp.l	#ACTION_DIE,d0
	beq	exit2
	DBUG1	txerr1,d0
	move.l	MyPacket(a5),a0
	clr.l	dp_Res1(a0)
	move.l	#ERROR_ACTION_NOT_KNOWN,dp_Res2(a0)
loopend	bsr	ReplyPacket
	bra	loop

exit2	DBUG0	txend
	bsr	ReplyPacket
	tst.l	OpenCount(a5)
	beq.s	.A
	bpl	loop
.A	bsr	FlushPackets
	move.l	MyDeviceNode(a5),a0
	clr.l	dn_Task(a0)
	move.l	a5,a1
	moveq	#MyStructSize,d0
	CALL	FreeMem
	DBUG0	txed
exit	movem.l	(sp)+,d2-d7/a2-a6
	moveq	#0,d0
	rts

*******************************************************

* Wait for a packet then get it into MyPacket
GetPacket:
.A	move.l	MyMsgPort(a5),a0
	CALL	WaitPort
	move.l	MyMsgPort(a5),a0
	CALL	GetMsg
	tst.l	d0
	beq.s	.A
	move.l	d0,a0
	move.l	mn_Node+LN_NAME(a0),MyPacket(a5)
	beq.s	.A
	rts

* Reply packet in MyPacket
ReplyPacket:
	move.l	MyPacket(a5),a2
	move.l	dp_Port(a2),a0		reply port
	move.l	dp_Link(a2),a1		message to send
	move.l	MyMsgPort(a5),dp_Port(a2)
	move.l	a2,mn_Node+LN_NAME(a1)
	clr.l	mn_Node+LN_SUCC(a1)
	clr.l	mn_Node+LN_PRED(a1)
	CALL	PutMsg
	rts

FlushPackets:
.A	move.l	MyMsgPort(a5),a0
	CALL	GetMsg
	tst.l	d0
	beq.s	.C
	move.l	d0,a0
	move.l	mn_Node+LN_NAME(a0),MyPacket(a5)
	beq.s	.A
	move.l	MyPacket(a5),a0
	clr.l	dp_Res1(a0)
	move.l	#ERROR_ACTION_NOT_KNOWN,dp_Res2(a0)
	bsr	ReplyPacket
	bra.s	.A
.C	rts

*******************************************************

OpenOldfile:
	move.l	MyPacket(a5),a1
	DBUG1	txopeno,dp_Arg3(a1)
	move.l	dp_Arg1(a1),a0
	add.l	a0,a0
	add.l	a0,a0
	clr.l	fh_Port(a0)
	addq.l	#1,OpenCount(a5)
	rts

OpenNewfile:
	move.l	MyPacket(a5),a1
	DBUG1	txopenn,dp_Arg3(a1)
	move.l	dp_Arg1(a1),a0
	add.l	a0,a0
	add.l	a0,a0
	clr.l	fh_Port(a0)
	addq.l	#1,OpenCount(a5)
	rts

CloseFile:
	DBUG0	txclose
	subq.l	#1,OpenCount(a5)
	rts

ReadFile:
	move.l	MyPacket(a5),a1
	DBUG0	txread
	moveq	#0,d0
	move.l	d0,dp_Res1(a1)
	subq.l	#1,OpenCount(a5)
	rts

WriteFile:
	move.l	MyPacket(a5),a1
	DBUG1	txwrite,dp_Arg3(a1)
	move.l	dp_Arg3(a1),dp_Res1(a1)
	subq.l	#1,OpenCount(a5)
	rts

*******************************************************

	IFD	DEBUG
RawHook	move.l	a6,-(sp)
	move.l	4.w,a6
	jsr	_LVORawPutChar(a6)
	move.l	(sp)+,a6
	rts

txstart	dc.b	"Handler start",0
txend	dc.b	"Handler end",0
txed	dc.b	"ed",10,0
txerr1	dc.b	"%ld: Action not known",10,0
txopeno	dc.b	"Open old file %b",10,0
txopenn	dc.b	"Open new file %b",10,0
txclose	dc.b	"Close file",10,0
txread	dc.b	"Read access",10,0
txwrite	dc.b	"Write %ld bytes",10,0
	ENDC

