; CLI TOY #3.
; ask.asm:	A routine to allow a single character answer to a question
;		posed by a ansiecho or echo question.
;		This character is then tested, and an ERROR issued if the
;		answer is not "y" or "Y".
;
;		The idea is based on H. Fischer's INFO-IBMPC submission of
;		12/83, called bat-ques. 
;
; Complaint:    IF should really allow IF ERROR=40... which would make this
;		a more general purpose hack. Probably better to just do a
;		more reasonable CLI.
; Perpetrator:  Dewi Williams  ..!ihnp4!druca!dewi
;               Unconditionally placed in the public domain.
; Restrictions: You'll probably need the V1.1 linker & assembler.
; Usage:
;		failat 20
;		ansiecho "do you want to load up the RAM disk? \c"
;		ask
;		if not error
;		ansiecho loading RAM...
;		else
;		ansiecho not loading RAM...
;		endif

* Included equate files.
* -----------------------------------------------------------------------
      	NOLIST 
      	INCLUDE  "exec/types.i"
      	INCLUDE  "exec/libraries.i"
      	INCLUDE  "libraries/dos.i"
      	LIST

* External references
* -----------------------------------------------------------------------

	EXTERN_LIB	OpenLibrary
	EXTERN_LIB	Read
	EXTERN_LIB	Input

* Local Equates
* ------------------------------------------------------------------------
SysBase  EQU   4        ; The one known address in the system.
TRUE     EQU   -1
FALSE    EQU   0
CMD_SIZE EQU   300      ; plenty big enough!

* Macros
* ------------------------------------------------------------------------

* Calls to dos.library and exec.library
callsys  MACRO
	CALLLIB      _LVO\1
      	ENDM

* The code segment
* -------------------------------------------------------------------------

	RORG     0                 ; Relocatable code.

	LINK     A2,#(-CMD_SIZE)   ; expand onto stack
	LEA.L    -CMD_SIZE(A2),A3  ; start of read buffer
	MOVEM.L  A0-A3,-(SP)	   ; Save command line (OpenLibrary trashes).

	;------ get Exec's library base pointer:
	move.l   SysBase,a6
	LEA.L    DOSName(PC),A1    ; name of dos library
	move.l   #LIBRARY_VERSION,d0

	callsys  OpenLibrary
	MOVE.L   D0,A6             ; Save library pointer (always in A6).
	BNE.S    gotdos

	; Should do a dos failure alert here. They'll find out soon enough!
	move.l	#1000,D0
	bra.s	fini
gotdos:
	MOVEM.L  (SP)+,A0-A3   ; restore stack addressing.

*  	Obtain the Input handle.
	callsys  Input         ; Always works -- copy from process structure.
	MOVE.L   D0,D1         ; Save for the read

*	Read the console
	move	#CMD_SIZE,d3	; Read >1 to swallow the CR
	move.l	a3,D2		; read onto the stack
	callsys Read            ; D1 = handle D2 = address D3 = length

*	Check out the read character
	cmpi.b  #'Y',(A3)
	beq.s	yes
	cmpi.b  #'y',(a3)
	beq.s	yes
	moveq	#RETURN_ERROR,D0	; anything else means no
	bra.s	fini
yes:
	moveq	#RETURN_OK,D0
fini:
	unlk	a2
	rts			; return with exit code in D0

* Data declarations
* -------------------------------------------------------------------------
DOSName		DOSNAME
   END
