;----------------------------------------------------------------------
; [PASLIB] RST.MAC: Read string from console with editing priviledges.
; @RST
; Revisions:
; 3/8/81 Last MGL update
;
;
;----------------------------------------------------------------------

	PUBLIC	@RST

	EXTRN	@STR
	EXTRN	@CHW
	EXTRN	@SYSIN
	EXTRN	@LFB
	EXTRN	@CPMRD		;CP/M READ STRING CALLER
				;PROCEDURE @CPMRD(VAR BUF:RDBUF);
				;RDBUF = BYTE,BYTE,ARRAY[1..255] OF CHAR;

OPTION	EQU	56		;(FREAD,FWRITE,...FLSTOUT)
FCONIO	EQU	4		;USED BY THIS MODULE
FEOLN	EQU	59		;BOOLEAN;
FEOF	EQU	60		;BOOLEAN;

	DSEG
STRBUF:	DS	1
	DS	1
	DS	255		;FOR USING CP/M INPUT

RETADR:	DS	2
COLCTR:	DS	1		;FOR TAB EXPANSION

	CSEG

@RST:
	XRA	A
	STA	COLCTR

	POP	H
	SHLD	RETADR

	POP	H			;GET MAX DEFINED LENGTH
	MOV	A,L
	STA	STRBUF
	XRA	A
	STA	STRBUF+1

	LHLD	@LFB
	LXI	D,OPTION
	DAD	D
	MOV	A,M
	CPI	FCONIO
	JNZ	NONCONSOLE	;BR IF NOT A CONSOLE FILE FOR INPUT

;
;	IF IT IS A CONSOLE FILE THEN USE CP/M READ CONSOLE BUFFER
;

	LXI	H,STRBUF
	PUSH	H
	CALL	@CPMRD		;GO READ CONSOLE BUFFER FROM CP/M

	LDA	STRBUF+1
	ORA	A
	JZ	ENDOFINPUT	;NO CTRL/Z FOUND
	LDA	STRBUF+2
	CPI	1AH		;CTRL/Z ON INPUT (EOF)?
	JNZ	ENDOFINPUT

	LHLD	@LFB		;GET POINTER TO FIB
	LXI	D,FEOF
	DAD	D		;POINT TO END OF FILE BOOLEAN
	MVI	M,1

ENDOFINPUT:			;SHARED CODE
	LXI	H,STRBUF+1
	PUSH	H
	LXI	H,255
	PUSH	H
	CALL	@STR		;STORE THE STRING


	LHLD	@LFB
	LXI	D,OPTION
	DAD	D
	MOV	A,M
	CPI	FCONIO		;IF @LFB^.OPTION <> FCONIO THEN BRANCH
	JNZ	RSTXIT

	MVI	C,0DH		;IF A CONSOLE FILE THEN ECHO CRLF
	PUSH	B
	LXI	H,-1
	PUSH	H
	PUSH	H
	CALL	@CHW
	LHLD	@LFB
	LXI	D,FEOLN
	DAD	D
	MVI	M,1		;SET @LFB^.EOLN := TRUE

RSTXIT:
	MVI	A,0DH		;SO READLN WILL WORK
	LHLD	RETADR
	PCHL

;
;	COME HERE IF NON-CONSOLE READ
;	JUST CALL INDIRECT TO @SYSIN UNTIL A CR IS FOUND
;
NONCONSOLE:
	LXI	H,STRBUF+2	;POINT TO INPUT BUFFER AREA
	MVI	A,0
	STA	STRBUF+1	;SET LENGTH

	PUSH	H
	LHLD	@LFB		;GET FIB POINTER
	LXI	B,FEOLN		;READ CHARS UNTIL EOLN
	DAD	B
	MOV	A,M
	POP	H
	RAR
	JC	ENDOFINPUT

NEXTCH:
	PUSH	H
	LXI	H,RETURN
	PUSH	H
	LHLD	@SYSIN
	PCHL			;GO CALL THE INPUT ROUTIN

RETURN:
	POP	B		;GET CHAR


	POP	H		;GET BUFFER POINTER
	LDA	STRBUF+1
	CPI	0FFH		;STRING OVERFLOW?
	JZ	NOSTORE		;IF SO THEN DONT STORE IT

	CALL	STORE$WITH$TAB$EXPANSION

	INX	H		;BUMP POINTER
	LDA	STRBUF+1
	INR	A
	STA	STRBUF+1
NOSTORE:
	XCHG
	LHLD	@LFB		;GET FIB POINTER
	LXI	B,FEOLN		;READ CHARS UNTIL EOLN
	DAD	B
	MOV	A,M
	RAR
	XCHG
	JNC	NEXTCH		;LOOP UNTIL EOLN
	JMP	ENDOFINPUT	;AND WE ARE DONE

STORE$WITH$TAB$EXPANSION:
	MOV	A,C
	CPI	09H		;TAB CHARACTER?
	JZ	TAB$EXPAND
	MOV	M,C
	LDA	COLCTR
	INR	A
	STA	COLCTR
	RET

TAB$EXPAND:
	MVI	M,' '		;STORE AT LEAST ONE BLANK

	LDA	STRBUF+1
	INR	A
	STA	STRBUF+1

	LDA	COLCTR
	INR	A
	STA	COLCTR

	CPI	0FFH		;STRING OVERFLOW?
	RZ			;IF SO THEN EXIT

	ANI	7		;ON BOUNDARY?
	JZ	DONE$TABBING	;RETURN IF YES

	INX	H
	JMP	TAB$EXPAND

DONE$TABBING:
	PUSH	H
	LXI	H,STRBUF
	INX	H
	DCR	M
	POP	H
	RET

	END
