*******************************************************************************
* ParseCmdLn 1.2.3
*******************************************************************************
* INFO	executes the parsing of a commandline and sets the variables
*	accordingly
* SYN	success = ParseCmdLn[StrPtr]
*	d0.l                 a0.l
* IN	StrPtr	pointer to commandline string
* OUT	success	0=ERROR! else OK
* MOD	FlNm	filled with the input filename
*	BsNm	filled with the base name (if specified)
*	flgs	bit # F_QUIETMODE updated
*******************************************************************************

	function ParseCmdLn[a0],a0:d0

	moveq.l	#0,d0	;returncode

	SkpSpcs[a0]
	when.s #"-q"=(a0).w	;if quiet mode
	 bset.b	#F_QUIETMODE,flgs	;set appropriate flag
	 addq.l	#2,a0	;skip "-q"
	 SkpSpcs[a0]
	ewhen

	when.s GetFlNm[sav:a0,#InFl]	;if InFl specified (correctly)
	 SkpSpcs[a0]

	 move.l	ValU[a0],ChnkSz	;get chunk length
	 when.s d0 & {#$7fffffff»=d0}	;d0 returned by ValU[]
	  SkpSpcs[a0]
	  when.s ~GetFlNm[a0,#OutBs]	;if OutBs not specified (correctly)
	   clr.b	OutBs	;void its path
	  ewhen
	  moveq.l	#-1,d0	;commamdline is good
	 othw
	  moveq.l	#0,d0	;error
	 ewhen

	ewhen

	efunc



*******************************************************************************
* GetFlNm 1.2.2
*******************************************************************************
* INFO	copies a filename, discarding the ""s or ''s (if present);
*	it stops at the first ENTER,BLANK or SPACE and NULL-terminates
*	the string copied
* SYN	NewStrPtr = GetFlNm[StrPtr,DstBufAdr]
*	a0.l                a1.l   a2.l
* IN	StrPtr	pointer to string (filename)
*	DstBufAdr	address of destination buffer
* OUT	NewStrPtr	pointer after source filename (0 if ERROR!)
* WARN	be careful there is *no* check!!!
*******************************************************************************

	function	GetFlNm[a1-a2],d0/a1-a2:a0

	move.b	(a1),d0	;get first character

	when.s {#"'"=d0.b}|{#'"'=d0.b}	;if quoted...
	 CpyQtd.s[a1,a2]	;returns in a0.l

	owhen {#0=d0.b}|{#10=d0.b}|{#' '=d0.b} ;else if BLANK or ENTER...
	 suba.l	a0,a0	;error!

	othw
	 addq.l	#1,a1	;skip character already picked
	 repeat
	  move.b	d0,(a2)+	;store character
	  move.b	(a1)+,d0	;get new character
	 until.s {#0=d0.b}|{#10=d0.b}|{#' '=d0.b}
	 clr.b	(a2)	;NULL-terminated
	 movea.l	a1,a0	;update source pointer
	ewhen

	pop



*******************************************************************************
* CpyQtd 1.1.2
*******************************************************************************
* INFO	copies a string enclosed between two quotes: it is considered
*	a "quote" the first char of the string;
*	the copy is NULL-terminated;
*	it fails at the first ENTER or BLANK
* SYN	CpyQtd[StrPtr,DstBufAdr]
*	       a1.l   a2.l
* IN	StrPtr	pointer to string (filename)
*	DstBufAdr	address of destination buffer
* MOD	a0.l	pointer after string (0 if ERROR!)
* WARN	be careful there is *no* check!!!
* NOTE	local procedure of GetFlNm[]
*******************************************************************************
*
	procedure loc:CpyQtd[a1-a2],d0-d1/a1-a2
	move.b	(a1)+,d0	;get "quote"
	repeat
	 move.b	(a1)+,d1

	 switch.s d1.b
	 -> d0		;successful copy
	  clr.b	(a2)	;NULL-termination
	  moveq.l	#0,d1	;exit loop
	 -> #0		;unvalid character, exit loop
	  suba.l	a1,a1	;error
	 -> #10		;unvalid character
	  moveq.l	#0,d1	;exit loop
	  suba.l	a1,a1	;error
	 def
	  move.b	d1,(a2)+	;copy character
	 eswitch

	until.s ~d1.b
	movea.l	a1,a0	;new string pointer

	eproc
*
*******************************************************************************

	efunc



*******************************************************************************
* settings 1.1.5
*******************************************************************************
* INFO	reserves the memory and opens the files according to the
*	values retrived by ParseCmdLn[]
* SYN	ErrCode = settings[]
*	d0.l
* OUT	ErrCode	0=OK, else E_xxxxxx (see defs.i)
* REQ	_DOSName	address of "dos.library",0
*******************************************************************************

	function	settings[],d1/a0-a1/a6:d0

	move.l	ChnkSz,d0
	moveq.l	#0,d1	;best available memory
	movea.l	4.w,a6
	jsr	(_LVOAllocMem,a6)

	when d0.l		;if enough memory for a whole
	 move.l	ChnkSz,WrkBufLen	;chunk, BufSz=ChnkSz
	othw		;else allocate biggest buffer available
	 move.l	#MEMF_LARGEST,d1
	 jsr	(_LVOAvailMem,a6)
	 move.l	d0,WrkBufLen	;size of biggest buffer
	 moveq.l	#0,d1	;best available memory
	 jsr	(_LVOAllocMem,a6)
	ewhen

	when d0.l		;if buffer successfully reserved
	 move.l	d0,WrkBufAdr	;keep buffer address
	 move.l	#InFl,d1
	 move.l	#MODE_OLDFILE,d2
	 movea.l	_DOSBase,a6
	 jsr	(_LVOOpen,a6)

	 when d0.l
	  move.l	d0,InFlHnd	;store handle

	  lea.l	OutBs,a0
	  when.s	~(a0).b	;if no OutBs specified
	   lea.l	InFl,a1	;copy source filename
	   repeat
	    move.b	(a1)+,d0
	    move.b	d0,(a0)+
	   until ~d0.b

	  othw
	   while (a0)+.b	;find end of OutBs
	   ewhile
	  ewhen

	  move.b	#'.',(-1,a0)	;extension dot
	  move.l	a0,ExtnAdr	;store pointer to final BLANK

	  move.l	GetFlSz[InFlHnd],InFlSz
	  divul.l	ChnkSz,d1:d0	;d0=max chunk number
	  when.s ~d1.l
	   subq.l	#1,d0	;maximum chunk number
	  ewhen
	  when.s #MAXCHNKSNO»d0	;if reasonable number of chunks
	   move.l	d0,MaxChnk
	   moveq.l	#1,d1	;minimum number of digits
	   while.s #10«=d0.l
	    addq.l	#1,d1	;one more digit needed
	    divu.l	#10,d0
	   ewhile
	   move.l	d1,DgtNo
	   moveq.l	#0,d0	;no error
	  othw
	   moveq.l	#E_CHNKNO,d0	;too many chunks
	  ewhen

	 othw
	  moveq.l	#E_SOUFL,d0
	 ewhen

	othw
	 moveq.l	#E_LOWMEM,d0	;not enough memory
	ewhen

	efunc



*******************************************************************************
* GvInfo 1.1.1
*******************************************************************************
* INFO	prints some info about the current operations
* SYN	GvInfo[]
* NOTE	- prt[] and StrU[] always called without "sav:"
*	  (regs already saved)
*	- uses TmpBuf
*******************************************************************************

	procedure GvInfo[],d0-d2/a0
	prt[#txt_InfoFlNm]
	prt[#InFl]
	prt[#txt_ENTER]	;show source name

	prt[#txt_InfoOFl]
	StrU[#0,ExtnAdr,DgtNo]	;build first extension
	prt[#OutBs]		;show first chunk filename

	when.s MaxChnk.l	;if 2+ chunks
	 prt[#txt_InfoArw]
	 StrU[MaxChnk,ExtnAdr,DgtNo]	;build last extension
	 prt[#OutBs]	;show last chunk filename
	ewhen
	prt[#txt_ENTER]

	prt[#txt_InfoCSz]
	StrU[ChnkSz,#TmpBuf,#10]
	prt[#TmpBuf]	;show chunk size
	prt[#txt_InfoBytes]

	prt[#txt_InfoBSz]
	StrU[WrkBufLen,#TmpBuf,#10]
	prt[#TmpBuf]	;show buffer size
	prt[#txt_InfoBytes]
	eproc
