*******************************************************************************
* Init v1.1.1
*******************************************************************************
* INFO	execute some system inits
* SYNOPSIS	success = Init[CmdLnPtr,CmdLnLen]
*	d0	  a0       d0.w
* IN	CmdLnPtr	ptr to the shell command line string
*	CmdLnLen	length in chars of the command line
* OUT	success	0=ERROR
* MODIFIES	_DOSBase	dos.library ptr
*	_StdOut	standard output filehandle
*	CmdLn	command line (NULL-terminated) string
* REQUIRES	_DOSName	address of "dos.library",0
*******************************************************************************

	function Init[a0/d0.w],d1/d7/a0-a1/a6:d0.l

	moveq.l	#0,d7

	subq.w	#1,d0	;don't copy closing RETURN
	lea.l	CmdLn,a1	;dest buf
	when.s	{#512»=d0.w} & d0.w
	 subq.w	#1,d0	;"expire" = "dbra"
	 expire d0=d0
	  move.b	(a0)+,(a1)+	;copy command line
	  addq.l	#1,d7
	 nexp
	ewhen
	clr.b	(a1)	;NULL-terminated

	movea.l	4.w,a6
	lea.l	_DOSName,a1
	moveq.l	#36,d0	;at least KS 2.0
	jsr	(_LVOOpenLibrary,a6)
	move.l	d0,_DOSBase

	when.s d0
	 movea.l	d0,a6
	 jsr	(_LVOOutPut,a6)
	 move.l	d0,_StdOut	;still true (~0)
	ewhen

	efunc

*******************************************************************************
* CleanUp v1.0.3
*******************************************************************************
* INFO	frees allocated system resources
* SYNOPSIS	CleanUp[]
* REQUIRES	_DOSBase	dos.library ptr
*******************************************************************************

	procedure CleanUp[],d0-d1/a0-a1/a6

	move.l	InFileHnd,d1	;source file handle
	when.s d1.l
	 movea.l	_DOSBase,a6
	 jsr	(_LVOClose,a6)
	ewhen

	move.l	WrkBufLen,d0	;free allocated buf
	when.s d0.l
	 movea.l	4.w,a6
	 movea.l	WrkBufAdr,a1
	 jsr	(_LVOFreeMem,a6)
	ewhen

	movea.l	_DOSBase,a1
	movea.l	4.w,a6
	jsr	(_LVOCloseLibrary,a6)

	moveq.l	#0,d0	;retcode
	eproc

*******************************************************************************
* Print v1.0.0
*******************************************************************************
* INFO	prints a text to the standard output if quiet mode is OFF
* SYNOPSIS	Print[TxtPtr]
*	      d2
* IN	TxtPtr	ptr to NULL-terminated text string
* REQUIRES	_DOSBase	dos.library ptr
*	_StdOut	standard output handle
*******************************************************************************

	procedure Print[d2],d0-d3/a0-a1/a6

	move.b	flags,d0
	andi.b	#1<<F_QUIETMODE,d0

	when.s ~d0.b		;if quiet mode OFF
	 movea.l	d2,a0
.findend	 tst.b	(a0)+
	 bne.s	.findend	;find end of string
	 move.l	a0,d3
	 sub.l	d2,d3	;length
	 movea.l	_DOSBase,a6
	 move.l	_StdOut,d1
	 jsr	(_LVOWrite,a6)
	ewhen

	eproc

*******************************************************************************
* ShowResult v1.1.1
*******************************************************************************
* INFO	prints out the texts associated to a given errcode
* SYNOPSIS	ShowResult[ErrCode]
*	           d0
* IN	ErrCode	0=OK, else E_xxxxxx (see defs.i)
*******************************************************************************

	procedure ShowResult[d0]
	when.s d0
	 Print[#txt_error]	;"ERROR: "
	ewhen
	Print[(ErrTab.l,d0.l*4)]	;print msg
	eproc

*******************************************************************************
* SkipSpaces v1.0.0
*******************************************************************************
* INFO	starting from the current position in a string, returns the
*	position of the 1st character different from ' '
* SYNOPSIS	SkipSpaces[StrPtr]
*	           a0
* IN	StrPtr	ptr to a string
* MODIFIES	a0.l	position of the 1st char <>' '
*******************************************************************************

	procedure SkipSpaces[a0]
.find	cmpi.b	#' ',(a0)+
	beq.s	.find
	subq.l	#1,a0
	eproc

*******************************************************************************
* Valu v1.0.2
*******************************************************************************
* INFO	converts a decimal ASCII string to an unsigned long integer
* SYNOPSIS	IntVal = Valu[StrPtr]
*	d0	a0
* IN	StrPtr	ptr to numerical string
* OUT	IntVal	unsigned integer
* MODIFIES	a0.l	ptr after string
* NOTE	it stops at the 1st char not inside ['0'...'9']
*******************************************************************************

	function Valu[a0],d1-d2:d0
	moveq.l	#0,d0
	moveq.l	#0,d1
	do
	 move.b	(a0)+,d1	;get a digit (d)
	 subi.b	#'0',d1	;convert to int
	 bcs.s	.exit	;if d<0...
	 cmpi.b	#9,d1
	 bhi.s	.exit	;if d>9...
	 move.l	d0,d2	;IntVal
	 add.l	d0,d0	;2*IntVal
	 lsl.l	#3,d2	;8*IntVal
	 add.l	d2,d0	;10*IntVal
	 add.l	d1,d0	;10*IntVal+d -> IntVal
	loop
.exit
	efunc

*******************************************************************************
* Stru v1.0.0
*******************************************************************************
* INFO	converts an unsigned long integer to a NULL-terminated, decimal
*	ASCII string
* SYNOPSIS	Stru[Int,Dest,Len]
*	     d0  a0   d1
* IN	Int	integer to convert
*	Dest	ptr to destination buffer
*	Len	the string will be exactly Len chars long
*		(MUST be >0!!!)
* NOTE	the destination buffer MUST be at least Len+1 bytes long!
*******************************************************************************

	procedure	Stru[d0/a0/d1],d0-d2/a0
	adda.l	d1,a0	;last digit adr+1
	clr.b	(a0)	;final BLANK
	subq.l	#1,d1
	expire d1=d1
	 divul.l	#10,d2:d0	;last digit
	 addi.b	#'0',d2	;convert to ASCII
	 move.b	d2,-(a0)	;store
	nexp
	eproc

*******************************************************************************
* GetFileSize v1.0.1
*******************************************************************************
* INFO	returns the size in bytes of a file
* SYNOPSIS	Size=GetFileSize[Hnd]
*	d0	d1
* IN	Hnd	filehandle
* OUT	d0	0 on failure
* REQUIRES	_DOSName	address of "dos.library",0
* NOTE	uses TmpBuf (must be on a 4 bytes boundary)
*******************************************************************************

	function GetFileSize[d1],d1-d2/a0-a1/a6:d0
	move.l	#TmpBuf,d2
	movea.l	_DOSBase,a6
	jsr	(_LVOExamineFH,a6)
	when.s d0.l
	 lea.l	TmpBuf,a0
	 move.l	(fib_Size,a0),d0
	ewhen
	efunc

*******************************************************************************
* ChkBrk v1.0.0
*******************************************************************************
* INFO	checks if the user is pressing CTRL-C
* SYNOPSIS	ErrCode=ChkBrk[]
*	d0
* OUT	ErrCode	E_USRBRK if pressed, 0 otherwise
*******************************************************************************

	function ChkBrk[],d1/a0-a1/a6:d0
	movea.l	4.w,a6
	moveq.l	#0,d0
	move.l	#SIGBREAKF_CTRL_C,d1	;chk & clr this signal
	jsr	(-306,a6)	;SetSignal()
	btst.l	#SIGBREAKB_CTRL_C,d0	;if the signal bit
	sne.b	d0	;was ON, then return
	andi.b	#E_USRBRK,d0	;the appropriate ErrCode
	extb.l	d0
	efunc
