* 
* /* KICKDATE - Copyright 1990 Joe Porkka
* 		FREELY DISTRIBUTABLE
* 
* 	Purpose: Keeps the current date on the Kickstart disk
* 		 for A1000 owners with autoboot harddrives.
* 
* Version 1.0
* 
* */
* 
pr_CLI	equ $00ac
cli_CommandName equ $0010

* This is a modified ASTARTUP.ASM file
* it is stripped down to work only from a CLI

******* Imported *******************************************************

xlib	macro
	xref	_LVO\1
	endm

	xref	_AbsExecBase
	xlib	Input
	xlib	Output

	xref	_main			; C code entry point

	xlib	FindTask
	xlib	OpenLibrary
	xlib	CloseLibrary

******* Exported *******************************************************

	xdef	_SysBase
	xdef	_DOSBase

	xdef	_errno
	xdef	_stdin
	xdef	_stdout
	xdef	_stderr

	xdef	_exit			; standard C exit function

callsys macro
	jsr _LVO\1(a6)
	endm


************************************************************************
*
*	Standard Program Entry Point
*
************************************************************************
*
*	main (argc, argv)
*	   int	argc;
*	   char *argv[];
*
************************************************************************

startup:				; reference for Wack users
		move.l	sp,initialSP	; initial task stack pointer
		move.l	d0,dosCmdLen
		move.l	a0,dosCmdBuf

	;------ get Exec's library base pointer:
		move.l	_AbsExecBase,a6
		move.l	a6,_SysBase

	;------ get the address of our task
		suba.l	a1,a1
		callsys FindTask
		move.l	d0,a4

fromCLI:
	;------ attempt to open DOS library:
		bsr	openDOS

	;------ find command name:
		move.l	pr_CLI(a4),a0
		add.l	a0,a0		; bcpl pointer conversion
		add.l	a0,a0
		move.l	cli_CommandName(a0),a0
		add.l	a0,a0		; bcpl pointer conversion
		add.l	a0,a0

	;------ create buffer and array:
		movem.l d2/a2/a3,-(sp)
		lea	argvArray,a3
		moveq.l #1,d2		; param counter

	;------ fetch command name:
		moveq.l #0,d0
		move.b	(a0)+,d0        ; size of command name
		move.l	a0,(a3)+        ; ptr to command name
		clr.b  0(a0,d0.w)          ; zero byte at end

	;------ collect parameters:
		move.l	dosCmdLen,d0
		move.l	dosCmdBuf,a0
	;------ skip control characters and space:
3$:		move.b	(a0)+,d1
		subq.l	#1,d0
		ble.s	parmExit
		cmp.b	#' ',d1
		ble.s	3$

	;------ copy parameter:
		addq.l	#1,d2	; param count
		lea.l  -1(a0),a1
		move.l	a1,(a3)+
;		 bra.s	 5$
4$:		move.b	(a0)+,d1
		subq.l	#1,d0
		cmp.b	#' ',d1
		bgt	4$
;		 ble.s	 6$
5$:		;move.b  d1,(a2)+
;		 bra.s	 4$
;6$:
		clr.b	-1(a0)
		cmp	#32,d2
		blt	3$
;		 bra.s	 3$
parmExit:	clr.b	(a0)+
		clr.l	(a3)+

		move.l	d2,d0
		movem.l (sp)+,d2/a2/a3
		pea	argvArray
		move.l	d0,-(sp)


*
*  The above code relies on the end of line containing a control
*  character of any type, i.e. a valid character must not be the
*  last.  This fact is ensured by DOS.
*


	;------ get standard input handle:
		move.l	_DOSBase,a6
		callsys Input
		move.l	d0,_stdin

	;------ get standard output handle:
		callsys  Output
		move.l	d0,_stdout
		move.l	d0,_stderr

	;------ call C main entry point
		jsr	_main

	;------ return success code:
		moveq.l #0,D0
		move.l	initialSP,sp	; restore stack ptr
		rts

_exit:
		move.l	4(SP),d0        ; extract return code
exit2:
		move.l	initialSP,SP	; restore stack pointer
		move.l	d0,-(SP)        ; save return code

	;------ close DOS library:
		move.l	_AbsExecBase,A6
		move.l	_DOSBase,d0
******************************************

	DATA

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

_SysBase	dc.l	0
_DOSBase	dc.l	0

_errno		dc.l	0
_stdin		dc.l	0
_stdout 	dc.l	0
_stderr 	dc.l	0

initialSP	dc.l	0

dosCmdLen	dc.l	0
dosCmdBuf	dc.l	0

argvArray	ds.l	32  ; MAX 32 args

DOSName 	dc.b 'dos.library',0


	END
