************************************************************************
*							*
*		   CLI Read/Write				*
*							*
*			Version 1.0				*
*							*
*		   17 March, 1989				*
*							*
************************************************************************


;date 17-Mar-89
;Read and Write to the CLI window

**	Tabstops : variable
**	Assemble with Macro68


	ifnd	_AbsExecBase
_AbsExecBase	equ	4		;exec library base address

**********  Exec.lib offsets  **********

_LVOOldOpenLibrary equ	-$198
_LVOCloseLibrary	equ	-$19E

**********  DOS.lib offsets   **********

_LVORead	equ 	-$2A
_LVOWrite	equ 	-$30
_LVOInput	equ 	-$36
_LVOOutput	equ 	-$3C
	endc

**********    ERROR definitions    ***************

ERROR_DOSOUTPUT	equ	310
ERROR_DOSINPUT	equ	311
**************************************************
	exeobj
	errfile	'ram:assem.output'
	objfile	'ram:test'
	strict
**************************************************

	movea.l	(_AbsExecBase),a6
	lea	(doslib),a1	;dos.library
	clr.l	d0	;any version
	jsr	(_LVOOldOpenLibrary,a6) ;open Dos Library
	move.l	d0,(DOSBase)	;save
	movea.l	d0,a6	;DOSBase in a6
	jsr	(_LVOOutput,a6)	;get output
	move.l	#ERROR_DOSOUTPUT,d3	;error code
	move.l	d0,(Output)
	beq.w	exit1	;no output!
	move.l	#ERROR_DOSINPUT,d3	;error code
	jsr	(_LVOInput,a6)	;get input file handle
	move.l	d0,(Input)
	lea	(title_msg,pc),a0
	bsr.b	display
	bsr.b	getinput	;wait for keyboard
	lea	(buffer,pc),a0
	bsr.b	display	;print it on screen
	clr.l	d3
exit1	movea.l	(_AbsExecBase),a6
	movea.l	(DOSBase),a1
	jsr	(_LVOCloseLibrary,a6)
	move.l	d3,d0	 ;return code
	rts


;entry - buffer
;        DOSBase
;        Input
;exit  - d0 = number of characters
;used  - NONE

getinput	movem.l	d1-d3/a0-a2/a6,-(sp)
	movea.l	(DOSBase),a6;	;DosBase
getinput_loop	move.l	(Input,pc),d1
	movea.l	#buffer,a2
	move.l	a2,d2
	move.l	#$FF,d3	;maximum 255 chars
	jsr	(_LVORead,a6)	;get input
	adda.l	d0,a2
	clr.b	(a2)	;NULL terminator
	movem.l	(sp)+,d1-d3/a0-a2/a6
	rts

;Display ASCII text
;entry - a0 - points to msg
;        DOSBase
;used  - NONE

display	movem.l	d0-d3/a6,-(a7)
	move.l	a0,d2
	moveq	#-1,d3
display_loop1	addq.l	#1,d3
	cmpi.b	#0,(a0)+	;look for NULL
	bne.b	display_loop1
	move.l	(Output,pc),d1	;file handle
	movea.l	(DOSBase),a6
	jsr	(_LVOWrite,a6)
	movem.l	(a7)+,d0-d3/a6
	rts


DOSBase	dc.l	0
Input	dc.l	0
Output	dc.l	0

doslib	dc.b	'dos.library',0

title_msg	dc.b	10,10,'Testing text input/output to CLI'
	dc.b	' window.  Type RETURN to end.',10,10,0

buffer	dcb.b	256,0


	END
