;
;	ReqText - Request text
;
;	Pops up an req.library Text requester and sends
;	this text to STDOUT.
;
;	written by Kay-Uwe Drangmeister
;
;	(c) 1991 by Radium Soft and Hardware Development
;

	incdir	'include:'
	include	'exec/exec_lib.i'
	include	'exec/memory.i'
	include	'libraries/dos.i'
	include	"libraries/dosextens.i"
	include	"libraries/arpbase.i"
	include	"libraries/reqbase.i"

JEXEC	MACRO
	jsr	_LVO\1(a6)
	ENDM

JARP	MACRO
	jsr	_LVO\1(a6)
	ENDM

; -------------------------------------------------------------------------
	code

_main	move.l	a0,a2
	move.l	d0,d2
	lea.l	arpname(pc),a1	; Get ArpBase
	moveq	#ArpVersion,d0
	CALLEXEC	OpenLibrary
	tst.l	d0
	bne.s	okgo
	lea	dosname(pc),A1
	JEXEC	OpenLibrary
	tst.l	D0
	beq.s	1$
	move.l	D0,A6
	jsr	_LVOOutput(a6)	;standard output file handle
	move.l	d0,d1
	beq.s	1$	;No output. Phoey.
	lea.l	alibmsg(pc),a0	;tell user he needs to find library
	move.l	a0,d2
	moveq	#aliblng,d3
	jsr	_LVOWrite(a6)
	move.l	a6,a1
	CALLEXEC	CloseLibrary
1$	moveq	#20,d0
	rts

okgo	move.l	d0,a6
	sub.w	#sbss_sizeof,sp	;bss-seg :-)

;	check command line

	move.l	a2,a0
	move.l	d2,d0
	lea	templ(pc),a3	;parse command-line
	lea	hlptext(pc),a1	;usage text
	lea	(sp),a2	;arga(sp)
	JARP	GADS
	moveq	#5,d3
	move.l	d0,d5	;# of args
	bmi	printix	;arg error
	beq	printex	;no args -> help

	lea	reqname(pc),a1
	moveq	#1,d0	;min req version
	JARP	ArpOpenLibrary
	moveq	#20,d3
	move.l	D0,d6
	lea	nortext(pc),a1
	beq.s	printex

	moveq	#60,d0	;default len
	tst.l	arglen(sp)
	beq.s	usedeflen
	move.l	arglen(sp),a0	;user's string
	JARP	Atol
	lea	nantext(pc),a1	;not a number error?
	beq.s	printex

usedeflen	addq.l	#1,d0
	move.l	d0,d2
	JARP	ArpAlloc	;allocate buffer
	lea	nomtext(pc),a1
	move.l	d0,_StrBuff(sp)
	beq.s	printex

	move.l	argdtxt(sp),d0	;copy default string
	beq.s	4$
	move.l	d0,a0
	move.l	_StrBuff(sp),a1
	move.l	d2,d0
	bra.s	2$
1$	move.b	(a0)+,(a1)+
2$	dbf	d0,1$
	sf	-(a1)
4$	move.l	d2,d1	;maxchars
	moveq	#61,d0	;visiblechars
	cmp.l	d2,d0
	ble.s	3$
	move.l	d2,d0
3$	move.l	argatxt(sp),a1	;title
	move.l	_StrBuff(sp),a0	;buffer
	sub.l	a2,a2	;window
	exg.l	d6,a6	;reqbase
	jsr	_LVOGetString(a6)
	exg.l	d6,a6
	moveq	#5,d3	;return code: WARN
	tst.l	d0
	beq.s	exit
	move.l	_StrBuff(sp),a1
	moveq	#0,d3
	bra.s	printex

;	EXIT

printix	move.l	(sp),a1	;arga(sp)
	moveq	#20,d3
printex	JARP	Puts
exit	add.w	#sbss_sizeof,sp
	move.l	a6,a1	;_ArpBase
	move.l	4.w,a6
	jsr	_LVOCloseLibrary(a6)
	move.l	d3,d0
	rts

; -------------------------------------------------------------------------
;	data

TEM	SET	ArpVersion/10
dosname	dc.b	'dos.library',0
alibmsg	dc.b	'you need '
arpname	ARPNAME
	dc.b	' V',TEM+'0',(ArpVersion-(TEM*10))+'0','+',10
aliblng	equ	*-alibmsg

nortext	dc.b	'Can''t open '
reqname	REQNAME

templ	dc.b	'Text,Default,LEN/k',0
hlptext	dc.b	10,$9b,'1;33;3430mReqText',$9b,'0;31;40m Version 1.0 - '
	dc.b	'A Freely-Redistributable Program',10
	dc.b	9,'written by Kay-Uwe Drangmeister',10
	dc.b	9,'© 1991 by ',$9b,'1;31;3430mRadium ',$9b,'0;31;40m'
	dc.b	'Soft and Hardware Development and Performance',10,10
	dc.b	'Usage: ReqText AskText [DefaultText] [LEN length]',10
	dc.b	'       The default length is 60',10,0
nomtext	dc.b	'Out of memory!',0
nantext	dc.b	'Not a number!',0

; -------------------------------------------------------------------------
;	bss

	rsreset
arga	rs.l	3
argatxt	equ	arga	;ask text
argdtxt	equ	arga+4	;default text
arglen	equ	arga+8	;max text len
_StrBuff	rs.l	1
sbss_sizeof	rs.b	0
