	TITLE TTYIN
; CALL TTYIN(LINE)
; LINE IS 132 BYTES LONG
DATA	SEGMENT PUBLIC 'DATA'
DATA	ENDS
DGROUP	GROUP DATA
CODE	SEGMENT 'CODE'
	ASSUME CS:CODE,DS:DGROUP,SS:DGROUP
PUBLIC TTYIN
TTYIN PROC FAR
    PUSH      BP
    MOV       BP,SP
    push   si
    push   di
; GET ADDRESS OF STRING...
    LES       BX,DWORD PTR [BP].06H
; ES:[BX] NOW HAS AREA BEING PASSED.
    MOV       CX,132
;	MOV	DX,BX	;SAVE INITIAL BX TOO...
	XOR	DX,DX	;USE DX AS FLAG
; DX IS SET TO 1 AFTER 1ST CHAR TO KEEP / FROM ACTING
; AS DELIMITER AFTER THAT.
L9:
; DISTRUST DOS SO SAVE/RESTORE ACCUMULATORS WE NEED AROUND CALLS.
	PUSH	CX
	PUSH	ES
	PUSH	BX
	PUSH	DX
	MOV	AH,1	;SET FOR READ A CHARACTER
	INT	21H	;CALL DOS READIN AND ECHO
	POP	DX
	POP	BX
	POP	ES
	POP	CX
; 13 IS ASCII FOR C.R. ON WHICH WE END (WITHOUT PLACING IT IN
; THE BUFFER)
	CMP	AL,13	;END ON C.R. ALSO
	JZ	L11	;LIKE THIS...
	CMP	AL,47	;IS IT A /?
	JNZ	VV1
; TEST FLAG... IF 2ND CHAR OR LATER / ISN'T TERMINATOR
	CMP	DX,0	;SEE IF 1ST TIME
	JNZ	VV1	;IF NOT, NO SPECIAL TERMINATION...
; SEE IF MODE IS 1 NOW...
	PUSH	ES
	PUSH	BX
	PUSH	CX
	LES	BX,DWORD PTR[BP+10]
; GET THE VALUE...
	MOV	CL,ES:[BX]
	CMP	CL,1	;IS IT A 1?
	JZ	VVT	;IF ZERO YES, THIS IS A TERMINATOR
	POP	CX
	POP	BX
	POP	ES	;RESTORE STACK
	JMP	VV1
VVT:	POP	CX	;RESTORE STACK & FINISH UP
	POP	BX
	POP	ES
	MOV	ES:[BX],AL ;FILL IN CHARACTER TO OUTPUT ARRAY
	JMP	L11	;FINISH ON / IN CMD MODE...
VV1:
	MOV	DX,1	;FLAG PAST 1ST TIME
	CMP	AL,8	;SEE IF IT'S A BACKSPACE
	JNZ	L100
	ADD	CX,1	;IF SO BACK UP STRING
	CMP	CX,132	;BE SURE CX-132 IS NEGATIVE
	JG	L101	;IF + THEN ERROR, SKIP IT...
	SUB	BX,1	;AND BACK UP POINTER...
	JMP	L9	;AND GO BACK TO OVERWRITE THE CHARACTER.
L101:
; BACKSPACE AT START OF STRING... JUST RESTART.
	MOV	CX,132	;RESET CX TO FULL LENGTH
	JMP	L9
L100:
	MOV	ES:[BX],AL ;FILL IN CHARACTER TO OUTPUT ARRAY
	ADD	BX,1	;BUMP OUTPUT ADDRESS
	CMP	AL,0	;SEE IF ZERO CHAR FLAGGING SPECIAL CHAR NEXT...
	JZ	L10	;IF SO GET NEXT AND SCRAM
	SUB	CX,1	;DECREMENT CX REG
; COUNT DOWN 132,131,...
	JG	L9	;AND CONTINUE READIN
	JMP	L11
L10:	MOV	AH,1
	CMP	CX,2	;SEE IF ANY OUTPUT SPACE REMAINS
	JLE	L11	;SKIP IF NO SPACE FOR EXTRA CODE.
	PUSH	DX
	PUSH	ES
	PUSH	BX
	INT	21H	;GET SPEC CHAR CODE.
	POP	BX
	POP	ES
	POP	DX
	MOV	ES:[BX],AL	;STASH IT AWAY
; NOW LEAVE.
; NOTE THAT THE GETTTL PROGRAM CALLS THIS AND THIS ROUTINE WILL
; RETURN THE STRING WITH ALL SPECIAL CODES INTACT.
; GETTTL WILL THEN REPLACE SPECIAL CODES (STARTING WITH 59) BY
; __{AB, __{AC, ... SO THAT THE COMMANDS ACTUALLY EXECUTED WILL
; BE THOSE SET UP IN THOSE CELLS' FORMULAS, PRESUMABLY BY A SETUP
; COMMAND FILE. ACINIT.PRM WILL BE KEPT OPEN LONG ENOUGH THAT IT
; CAN BE USED FOR THIS TO FILL THE DESIRED TEXT IN. THIS WILL MAKE
; USE OF ANSI.SYS TOTALLY OPTIONAL.
L11:
; RETURN WITH RESULT IN OUTPUT STRING.
    pop  di
    pop  si
    MOV       SP,BP
    POP       BP
    RET      0008H
TTYIN ENDP
CODE ENDS
	END


