	section	ReadCon,CODE
	IDNT	MAGIC

**	ReadCon
**		for doing magic inside batch files
**
**		Copyright ©1990 DigiSoft
**
**	Permission is granted to purchasers of Macro68 to modify this source
**	file.  Permission is further granted to distribute the source code that
**	they have modified subject to the following conditions:
**	1.  That the original, unaltered, source is included with the
**	    distribution.
**	2.  That no charge is made for the distribution, other than the cost of
**	    media.                    (Distribution via Fred Fish is permitted)
**	3.  That my copyright notice remains as part of this and any other
**	    file adapted from this file.
**                                               Paul Coward
**                                                         DigiSoft
**
**	Tabstops : 8
**	Assemble with Macro68


**	19-07-90	** written


**  FUNCTION & USAGE
**
**	ReadCon TO filename PREFIX text DEFAULT text SUFFIX text [switchs]
**
**	Reads characters typed to the console and stores them in the file.
**	The characters are prefixed by the prefix string and the suffix
**	string is appended to them.
**
**  KEYWORDS
**
**	TO	destination file
**
**	PREFIX	text to prefix the character string with
**
**	SUFFIX	text to append to the end of the character string
**
**	DEFAULT	text to be used if a null string is entered.
**
**  SWITCHES
**
**	dir	the character string is expected to be an existing directory
**
**	make	make the directory path if it dosn't already exist
**
**	prompt	prompt the user for help in makeing decisions
**
***********************************************************



		OBJFILE		"ram:ReadCon"
	;	ERRFILE		"ram:ReadCon.bugs"

	;	PALL
	;	HALL
	;	LFCOND
		EXEOBJ
	;	LIST
	;	LISTSYMS
	;	ADDSYM
	;	LISTFILE	"ram:ReadCon.lst"
	;	CREFFILE	"ram:ReadCon.cref"
	;	EQUFILE		"ram:ReadCon.equ"
	;	REALTIME
		STRICT



	ifnd	EXEC_TYPES_I
_AbsExecBase		equ	4
_LVOAlert		equ	-108	;exec
_LVOCloseLibrary	equ	-414	;exec
_LVOOpenLibrary		equ	-552	;exec
ACCESS_READ		equ	-2
MODE_NEWFILE		equ	1006
AT_Recovery		equ	$00000000
AG_OpenLib		equ	$00030000
RETURN_OK		equ	0
RETURN_FAIL		equ	20
	endc


_LVOdsAllocCPremember	equ	-30
_LVOdsAllocScratch	equ	-42
_LVOdsDosClose		equ	-414
_LVOdsDosCreateDir	equ	-714
_LVOdsDosLock		equ	-678
_LVOdsDosOpen		equ	-408
_LVOdsDosUnLock		equ	-684
_LVOdsDosWrite		equ	-864
_LVOdsParse		equ	-216
_LVOdsPuts		equ	-648
_LVOdsReadLine		equ	-654
_LVOdsToLower		equ	-840


	ifnd	csd_Input
AO_DSSLib		equ	$00008099	;alert number id support library can't be opened
Lf			equ	$0a
csd_Input		equ	19*4
	endc


		setso	84
csd_RememberKey	so.l	1
csd_Args	so.b	8
csd_Buffer	so.b	256
csd_QueryBuffer	so.b	20
csd_SIZEOF	soval


Release			equ	'1.0 '
VerNo			equ	'0.02'

*************************
*	COLD ENTRY	*
*************************


START:		moveq		#0,d3
		move.l		a0,d6
		move.l		d0,d7
		movea.l		(_AbsExecBase).w,a6	;establish exec for calls

	; --- open the support library

		moveq		#2,d0			;version #
		lea	 	(supportLibName,pc),a1	;point to liberary name
		jsr		(_LVOOpenLibrary,a6)	;exec
		tst.l		d0
		bne.b		1$
		move.l		#AT_Recovery!AG_OpenLib!AO_DSSLib,d7
		jsr		(_LVOAlert,a6)
		bra		exitFail

	; --- initialize the scratch area

1$		movea.l		d0,a6
		move.l		#csd_SIZEOF,d0
		moveq		#33,d1
		moveq		#0,d2
		jsr		(_LVOdsAllocScratch,a6)
		bne		fail

	; --- initialize the argument pointers

		movem.l		d6-d7,(csd_Args,a5)	;& a_Length(A0)

	; --- parse args

		lea		(csd_RememberKey,a5),a0
		move.l		#256,d0
		jsr		(_LVOdsAllocCPremember,a6)
		move.l		d0,(ArgPointers).l
		beq		fail

		lea		(csd_Args,a5),a0
		lea		(additionalHelp,pc),a1
		lea		(ArgPointers+4,pc),a2
		lea		(CmdTemplate,pc),a3
		jsr		(_LVOdsParse,a6)
		bpl.b		configure
		movea.l		(ArgPointers,pc),a1
		jsr		(_LVOdsPuts,a6)
		bra		fail

	; --- do it

configure:	move.l		#256-1,d2
		lea		(csd_Buffer,a5),a0
		jsr		(_LVOdsReadLine,a6)
		beq		fail
		subq.l		#1,d0
		bne.b		.haveOutput
		movea.l		(defaultText,pc),a0
		lea		(csd_Buffer,a5),a1
1$		move.b		(a0)+,(a1)+		;copy default string into buffer
		bne.b		1$

.haveOutput:	move.l		(targetFileName,pc),d1
		move.l		#MODE_NEWFILE,d2
		jsr		(_LVOdsDosOpen,a6)
		beq		fail
		move.l		d0,d1			;target filehandel

		; --- stack is used as a temp buffer for building strings
		; -- no overflow checking is done - but is anyone likely to
		; -- ever type in a path name greater than 1k? - I think not.

		lea		(-1024,sp),sp
		movea.l		sp,a1
		movea.l		(prefixText,pc),a0
..copy		move.b		(a0)+,(a1)+
		bne.b		..			;copy prefix
		subq.l		#1,a1
		lea		(csd_Buffer,a5),a0
..copy		move.b		(a0)+,(a1)+
		bne.b		..			;copy input/default
		subq.l		#1,a1
		tst.l		(dirFlag)
		beq.b		2$
		cmpi.b		#'/',(-1,a1)
		beq.b		2$
		cmpi.b		#':',(-1,a1)
		beq.b		2$
		move.b		#'/',(a1)+
2$		movea.l		(suffixText,pc),a0
..copy		move.b		(a0)+,(a1)+
		bne.b		..			;copy suffix
		move.b		#Lf,(-1,a1)
		move.l		a1,d3
		sub.l		sp,d3
		move.l		sp,d2
		jsr		(_LVOdsDosWrite,a6)
		sne		d2
		jsr		(_LVOdsDosClose,a6)
		lea		(1024,sp),sp
		tst.b		d2
		bne		fail

	; --- now run the required checks on the input (if any)

		tst.l		(dirFlag)
		beq		exitOk

		lea		(csd_Buffer,a5),a1
		move.l		a1,d1
		moveq		#ACCESS_READ,d2
		jsr		(_LVOdsDosLock,a6)
		beq.b		.dirDosntExist
		move.l		d0,d1
		jsr		(_LVOdsDosUnLock,a6)
		bra		exitOk

.dirDosntExist:	tst.l		(promptFlag)
		beq.b		.dontAskUser

		lea		(-1024,sp),sp
		movea.l		sp,a1
		lea		(dirTxt,pc),a0
..copy		move.b		(a0)+,(a1)+
		bne.b		..			;copy directory text
		subq.l		#1,a1
		lea		(csd_Buffer,a5),a0
..copy		move.b		(a0)+,(a1)+
		bne.b		..			;copy input/default
		subq.l		#1,a1
		lea		(queryTxt,pc),a0
..copy		move.b		(a0)+,(a1)+
		bne.b		..			;copy query
		move.l		a1,d3
		sub.l		sp,d3
		move.l		sp,d2
		move.l		(csd_Input,a5),d1
		jsr		(_LVOdsDosWrite,a6)
		lea		(1024,sp),sp

		moveq		#20-1,d2
		lea		(csd_QueryBuffer,a5),a0
		jsr		(_LVOdsReadLine,a6)
		beq.b		fail
		move.b		(a0),d0
		jsr		(_LVOdsToLower,a6)
		cmpi.b		#'y',d0
		bne		fail

.dontAskUser:	bsr		CreatePath
		beq		fail

exitOk:		movea.l		a6,a1
		movea.l		(_AbsExecBase).w,a6
		jsr		(_LVOCloseLibrary,a6)
		moveq		#RETURN_OK,d0
		rts


*****


fail:		movea.l		a6,a1
		movea.l		(_AbsExecBase).w,a6
		jsr		(_LVOCloseLibrary,a6)
exitFail:	moveq		#RETURN_FAIL,d0
		rts


*****


CreatePath:	lea		(csd_Buffer,a5),a0
		movea.l		a0,a1
.nextPath:	cmpi.b		#'/',(a1)
		beq.b		1$
		tst.b		(a1)+
		bne.b		.nextPath
		cmpi.b		#':',(-2,a1)		;check that there is a path to be made
		bne.b		.makePath		;make the last of the path & RTS
		rts

1$		clr.b		(a1)
		bsr.b		.makePath
		move.b		#'/',(a1)+
		bra.b		.nextPath

.makePath:	move.l		a0,d1			;path
		moveq		#ACCESS_READ,d2
		jsr		(_LVOdsDosLock,a6)	;directory exists ?
		move.l		d0,d1
		bne.b		.madePath
		move.l		a0,d1			;path
		jsr		(_LVOdsDosCreateDir,a6)	;create it
		move.l		d0,d1
		bne.b		.madePath
		moveq		#0,d0
		rts
.madePath:	jsr		(_LVOdsDosUnLock,a6)	;free the lock returned by CreateDir (or the test lock)
		moveq		#-1,d0
		rts


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


ArgPointers:	dl	0
targetFileName:	dl	0		;pointer to filename
prefixText:	dl	0		;pointer to text string
suffixText:	dl	0		;pointer to text string
defaultText:	dl	0		;pointer to text string
dirFlag:	dl	0
makeFlag:	dl	0
promptFlag:	dl	0


CmdTemplate:
	cstr	'TO/K/A,PREFIX/K/A,SUFFIX/K,DEFAULT/K,DIR/S,MAKE/S,PROMPT/S'


additionalHelp:
	db	'USAGE:',Lf
	cstr	'ReadCon TO file [PREFIX text] [SUFFIX text] [DEFAULT text] [[[DIR][MAKE]][PROMPT]]'


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


supportLibName:
	cstr	'digisoftSupport.library'


dirTxt:	cstr	'The directory : '

queryTxt:
	db	' dosn''t exist.',Lf
	cstr	'  Shall I create it for you? (Y or N)? > '


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

	END
