C COMMAND-LINE MUNGER FOR AnalytiCalc
C COPYRIGHT (C) 1983 GLENN EVERHART
C all rights reserved
C SUBROUTINE CMDMUN(LINE)
C   MUNGES UP COMMAND LINES PASSED IN ARGUMENT TO ALLOW
C  SPECIAL KEYS TO BE RECOGNIZED.
C  DEFAULT VERSION JUST RETURNS
C THIS VERSION (FOR VAX) ASSUMES ESCAPE SEQUENCES ARE *NOT* SENT TO	!CJD1
C THE PROGRAM.								!CJD1
C NOTE: A FEW LITTLE EXTRAS CAN BE TRIED.
C IF THE COMMAND BEGINS WITH CHARACTER % WE WILL PARSE IT AS
C FOLLOWS:
C  1. UP TO NEXT % SIGN, THE CHARACTERS WILL BE ECHOED TO TERMINAL
C	UNALTERED (USE FOR SENDING OUT ESCAPE SEQUENCES, E.G. TO
C	CHANGE WIDTH OF TERMINAL)
C  2. COMMAND UP TO NEXT % SIGN WILL BE TAKEN AS A LITERAL TO LEAVE
C       INSIDE COMMAND LINE PASSED BACK
C  3. IF ? APPEARS AFTERWARDS, TERMINAL WILL BE READ AND RESULTING
C       TEXT APPENDED TO 2ND GROUP WHEN PASSED BACK. IF RESULTING
C	READ-IN TEXT BEGINS WITH A \ CHARACTER OR ANY CONTROL CHARACTER
C	(E.G. ESCAPE) THE COMMAND TERMINATES; OTHERWISE, IF READ-IN
C	OCCURRED, THE INPUT COMMAND FILE (IF ONE EXISTS) WILL BE
C	REWOUND. NOTE THIS REWIND OPERATION OCCURS ONLY IF THE ? WAS
C	SEEN.
C 	ALSO IF A \ REPLACES THE ?, NO REWIND OCCURS.
C	IF A & IS SEEN, CLOSE FILE IMMEDIATELY PRIOR TO EXIT TO
C GET OUT OF THE WAY FOR W OR PPN/GP COMMANDS.
C
C MODS PER SIRA STUFF DONE 9/29/1987
C IF THIS IS A VT52, USE .CM5 FILE INSTEAD OF .CMD FILE.
C
C	CJD1	Oct-90, Chris Doran, Sira, SMG modifications;
C			.CM5 files no longer required;
C			^ no longer processed, all done by GOLD key definition
C			(except GOLD-GOLD-...-HELP which was never used);
C			Must handle screen width changes here.
C
	SUBROUTINE CMDMUN(LINE)
      PARAMETER CUP = 1, CUU = 2, CUD = 3, CUF = 4, CUB = 5, DECDWL = 6	!CJD1
     $, DECDHL = 7, DECRC = 8, DECSC = 9, DECSWL = 10, ED = 11, EL = 12	!CJD1
     $, SGR = 13, NEL = 14, SCS = 15, SM = 16, RM = 17, ANSI = 18	!CJD1
     $, DECCOLM = 3							!CJD1
	LOGICAL*1 LINE(120),LC,LINBUF(120)
	InTEgeR*4 IOLVL,IGOLD
D	InTEgeR*4 FOOBAR
D	COMMON/FOOBAR/FOOBAR
	COMMON/IOLVL/IOLVL,IGOLD
	InTEgeR*4 PROW,PCOL,DROW,DCOL,DRWV,DCLV,LLCMD,LLDSP		!CJD1
	COMMON/DCTL/PROW,PCOL,DROW,DCOL,DRWV,DCLV,LLCMD,LLDSP		!CJD1
C ALLOW RESULTS OF READ-IN COMMANDS TO BE FURTHER PROCESSED
C (UP TO 10 DEEP) TO ALLOW USE OF THE MACRO FACILITY
C BY COMMAND PROCEDURES.
D	ITERT=0
D6501	CONTINUE
D	ITERT=ITERT+1
D	IF(ITERT.GT.10)RETURN
C IOLVL IS LUN FOR XQTCMD TO USE (NORMALLY 3 FOR INDIRECT FILES OR 5
C FOR TERMINAL. WE USE 5,6 FOR TERMINAL INPUT, OUTPUT NORMALLY.
	LI=1
	LL=LINE(LI)
	IF(LINE(1).EQ.37)GOTO 7000
C CONVERT LOWER TO UPPER CASE
	NMX=120
	DO 41 N=1,120
C CHECK FOR DOUBLE QUOTE (34 DECIMAL). LEAVE L.C. IF SO
	NNN=LINE(N)
	IF(NNN.EQ.34)NMX=2
C IF WE SEE " CHARACTER THEN ONLY CONVERT 1ST 2 CHARACTERS TO U.C.
41	CONTINUE
	JFED=0
	DO 1 N=1,NMX
	LL=LINE(N)
	IF(LL.GT.96.AND.LL.LT.123)LL=LL-32
	LINE(N)=LL
C IF WE SEE A __ (2 SUCCESSIVE UNDERSCORES) IN THE TEXT, CALL
C FRMEDT TO EDIT ANY {V1 FORMS WE SEE INTO THE TEXT IN THE
C FORMULAS POINTED TO. USEFUL FOR MACROS.
	IF(LINE(N).EQ.'_'.AND.LINE(N+1).EQ.'_')JFED=N
1	CONTINUE
	IF(JFED.EQ.0)GOTO 520
C MOVE TEXT DOWN OVER THE __ CHARACTERS
	DO 521 N=JFED,120
	LINE(N)=LINE(N+2)
521	CONTINUE
	LINE(119)=0
	LINE(120)=0
	KK=110
	CALL FRMEDT(LINE,KK)
520	CONTINUE
C	IF(LINE(1).NE.'M')RETURN
	IF(LINE(1).NE.'M')GOTO 2000
	LI=2
	GOTO 1000
1000	RETURN								!CJD1
2000	CONTINUE
	IGOLD=0
	RETURN
7000	CONTINUE
C PROCESS %%% FORMS
	I1=INDEX(LINE(2),37)
C IF I1 IS 1, THEN WE JUST HAVE %% AND THERE'S NOTHING TO DUMP TO
C THE SCREEN. OTHERWISE DUMP IT OUT HERE..
	I1=I1+1
	IF(I1.LE.2.OR.I1.GT.80)GOTO 7002
	II1=I1-1
C Some command files include escape sequences: position: esc [ l ; c H	!CJD1
C					    132/80 cols: esc [ ? 3 h/l	!CJD1
C				     and set VT100 LEDs: esc [ n q	!CJD1
C SMG$ALLOW_ESCAPE handles positioning for us,				!CJD1
C and LEDs are not currently used,					!CJD1
C but we must handle screen width changes.				!CJD1
	I4 = 2								!CJD1
7100	I3 = I4								!CJD1
	I4 = I4 + INDEX(LINE(I3),155)					!CJD1
	IF (I4.LT.80) GOTO 7101						!CJD1
	I4 = I3 + INDEX(LINE(I3),27)					!CJD1
	IF (I4.GT.80) GOTO 7103						!CJD1
	IF (LINE(I4).NE.'[') GOTO 7100					!CJD1
	I4 = I4 + 1 							!CJD1
7101	IF (I4.GE.I1) GOTO 7103						!CJD1
	IF (LINE(I4).NE.'?') GOTO 7100					!CJD1
	I4 = I4 + 1 							!CJD1
	IF (LINE(I4).NE.'3') GOTO 7100					!CJD1
	I4 = I4 + 1 							!CJD1
	IF (LINE(I4).NE.'h') GOTO 7102					!CJD1
	  CALL UVT100(SM,DECCOLM)					!CJD1
	  GOTO 7103							!CJD1
7102	IF (LINE(I4).NE.'l') GOTO 7100					!CJD1
	  CALL UVT100(RM,DECCOLM)					!CJD1
7103	CALL UVT100(CUP,LLCMD,1)					!CJD1
	CALL UVT100(EL,2)						!CJD1
	CALL LWRITE(6,LINE(2),II1-1)					!CJD1
7002	CONTINUE
	IF(I1.GT.80)RETURN
C COPY WHATEVER NEEDS TO BE COPIED TO LINBUF
	DO 7003 II=1,80
7003	LINBUF(II)=0
	I2=INDEX(LINE(I1+1),37)
	IF(I2.GT.80)RETURN
	I2=I2+I1
	I1=I1+1
	II2=I2-1
	II=0
C HANDLE IT RIGHT IF NOTHING'S THERE TO COPY INTO COMMAND ALREADY.
	IF(I1.GT.II2)GOTO 7540
	DO 7004 LL=I1,II2
	II=II+1
7004	LINBUF(II)=LINE(LL)
7540	CONTINUE
	IF(I2.GT.80)RETURN
C IF LINE(I2+1) HAS & THEN CLOSE FILE RIGHT HERE AND BUG OFF
	IF(LINE(I2+1).NE.'&')GOTO 8005
	CLOSE (UNIT=IOLVL)
	IOLVL=5
D	FOOBAR=0
CJD1D	OPEN(UNIT=5,FILE='TT:',CARRIAGECONTROL='NONE')
C RE-OPEN ONLY IN VAX VERSION WHERE THERE'S ROOM FOR THE CODE...
	LINE(I2+1)='\'
8005	CONTINUE
C SEE IF LINE(I2+1) CONTAINS A ?
	IF(LINE(I2+1).NE.'?'.AND.LINE(I2+1).NE.'\')GOTO 7005
C HAVE TO READ IN USER'S LINE HERE... READ OFF UNIT 5 ALWAYS...
	LX=II+1
	IF (LREAD(5,LINBUF(LX),121-LX).LT.0) GOTO 7035
C NOW SEE IF LINBUF(LX) IS EITHER A \ CHAR OR ANY CONTROL CHARACTER
	LC=LINBUF(LX)
	IF(LINE(I2+1).EQ.'\')GOTO 7005
	IF(IOLVL.EQ.5)GOTO 7005
C IF WE SEE ANYTHING EXCEPT A CONTROL CHAR OR \, REWIND THE FILE...
C THIS ALLOWS US TO HAVE A SORT OF "ENTER MODE" AND A "COMMAND MODE"
C A LA SUPERCALC ETC.
	IF(LC.NE.'\'.AND.LC.GT.32)REWIND IOLVL
C COMMENT OUT ANY TERMINAL COMMAND
	IF(LC.EQ.'\'.OR.LC.LE.32)LINBUF(1)='*'
	GOTO 7005
7035	CONTINUE
C RECOVER AFTER CTL-Z ON EXPECTED INPUT.
CJD1	REWIND 5
	LINBUF(1)='*'
	CLOSE (UNIT=IOLVL)
	IOLVL=5
7005	CONTINUE
	DO 7006 II=1,120
7006	LINE(II)=LINBUF(II)
	KK=110
	IF(JFED.GT.0)CALL FRMEDT(LINE,KK)
C	RETURN
C GO LOOP TO RE-EDIT CMD LINE AS APPROPRIATE
C PROCESSING THERE WILL GET US OUT AFTER AT MOST 10 PASSES.
D	GOTO 6501
C REMOVE FOLLOWING RETURN IF UNUSED
6502	RETURN
	END
