	OPT c+,l-,o+

******************
*                *
*  MsgPrint 1.0  *
*                *
*  PowerPeak '90 *
******************

	INCDIR "INCLUDE:"
	INCLUDE "exec/types.i"
	INCLUDE "exec/memory.i"
	INCLUDE "exec/ports.i"
	INCLUDE "exec/execbase.i"
	INCLUDE "exec/exec_lib.i"
	INCLUDE "libraries/dos.i"
	INCLUDE "libraries/dos_lib.i"
	
	move.l ($4).w,a6
	lea dosname(PC),a1
	jsr _LVOOldOpenLibrary(a6)
	lea _DOSBase(PC),a0
	move.l d0,(a0)
	move.l d0,a6
	jsr _LVOOutput(a6)
	lea output(PC),a0
	move.l d0,(a0)
	lea about.txt(PC),a0
	move.l #about.len,d3
	bsr myputs
	move.l ($4).w,a6
	jsr _LVOForbid(a6)
	lea portname(PC),a1
	jsr _LVOFindPort(a6)
	move.l d0,d2
	jsr _LVOPermit(a6)
	tst.l d2
	beq.s oknoport
	lea twice.txt(PC),a0
	move.l #twice.len,d3
	bsr myputs
	bra closedos
oknoport:
	moveq #0,d0
	lea portname(PC),a1
	bsr _CreatePort
	tst.l d0
	bne.s okport
	lea noport.txt(PC),a0
	move.l #noport.len,d3
	bsr myputs
	bra.s closedos
okport:
	lea myport(PC),a3
	move.l d0,(a3)
	move.l d0,a1
	moveq #1,d0
	move.b MP_SIGBIT(a1),d1
	lsl.l d1,d0
	add.l #SIGBREAKF_CTRL_C,d0
	lea mysig(PC),a2
	move.l d0,(a2)
waitloop:
	move.l (a2),d0
	jsr _LVOWait(a6)

	move.l (a3),a0
	moveq #1,d1
	move.b MP_SIGBIT(a0),d2
	lsl.l d2,d1
	and.l d1,d0
	beq.s ctrlc

getmsg:
	move.l (a3),a0
	jsr _LVOGetMsg(a6)
	tst.l d0
	beq.s waitloop
	move.l d0,a5
	moveq #0,d3
	move.w MN_LENGTH(a5),d3
	move.l d3,d7
	sub.l #MN_SIZE,d3				; len of string
	lea MN_SIZE(a5),a0			; ptr to string
	bsr.s myputs
	move.l ($4).w,a6
	move.l a5,a1
	move.l d7,d0
	jsr _LVOFreeMem(a6)
	bra.s getmsg

ctrlc:
	move.l (a3),a1
	bsr _DeletePort

	lea ending.txt(PC),a0
	move.l #ending.len,d3
	bsr.s myputs
closedos:
	move.l _DOSBase(PC),a1
	move.l ($4).w,a6
	jsr _LVOCloseLibrary(a6)
	moveq #0,d0
	rts

; a0 text, d3 length
myputs:
	move.l _DOSBase(PC),a6
	move.l output(PC),d1
	move.l a0,d2
	jsr _LVOWrite(a6)
	rts

_DOSBase:	dc.l 0
output:		dc.l 0
myport:		dc.l 0
mysig:		dc.l 0
dosname:		dc.b 'dos.library',0
portname:	dc.b 'MsgPrint.port',0
about.txt:	dc.b 27,'[1mMsgPrint 1.0',27,'[0;33m by PowerPeak',27,'[0m'
				dc.b ' (CTRL-C to abort)',10
				dc.b '-------------------------------------------',10,0
about.len:	equ	*-about.txt
ending.txt:	dc.b 'MsgPrint saying bye, bye...',10,0
ending.len	equ	*-ending.txt
noport.txt:	dc.b 'Can''t create port !',10,0
noport.len	equ	*-noport.txt
twice.txt:	dc.b 'Already installed, ninkenpoop !',10,0
twice.len	equ	*-twice.txt

	EVEN

* struct MsgPort *CreatePort (UBYTE *, ULONG)
* d0									a1			d0
_CreatePort:
	movem.l d2-d3/a2-a3/a6,-(a7)
	move.l $4.w,a6
	move.l d0,d2					; remember d0 = pri
	move.l a1,a2					; remember a0 = name
	moveq #-1,d0
	jsr _LVOAllocSignal(a6)
	move.l d0,d3
	bpl.s 1$
	moveq #0,d0
	bra.s exitcreateport
1$	moveq #MP_SIZE,d0
	move.l #MEMF_CLEAR|MEMF_PUBLIC,d1
	jsr _LVOAllocMem(a6)
	tst.l d0
	bne.s 2$
	move.l d3,d0
	jsr _LVOFreeSignal(a6)
	moveq #0,d0
	bra.s exitcreateport
2$	move.l d0,a1
	move.l a2,LN_NAME(a1)
	move.b d2,LN_PRI(a1)
	move.b #NT_MSGPORT,LN_TYPE(a1)
	move.b #PA_SIGNAL,MP_FLAGS(a1)
	move.b d3,MP_SIGBIT(a1)
	move.l ThisTask(a6),MP_SIGTASK(a1)
	move.l a1,a2
	jsr _LVOAddPort(a6)
	move.l a2,d0
exitcreateport:
	movem.l (a7)+,d2-d3/a2-a3/a6
	rts

* void DeletePort (struct MsgPort *);
*						 a1
_DeletePort:
	movem.l a2/a6,-(a7)
	move.l a1,a2
	move.l $4.w,a6
	jsr _LVORemPort(a6)
	move.b MP_SIGBIT(a2),d0
	jsr _LVOFreeSignal(a6)
	moveq #MP_SIZE,d0
	move.l a2,a1
	jsr _LVOFreeMem(a6)
	movem.l (a7)+,a2/a6
	rts

	END

