c AnalytiCalc Amiga specific terminal I/O routines.
c note ttyini is also special and opens console window...
	Subroutine SWRT(ibuf,isz)
c write isz bytes from ibuf onto console window
	Include dos.inc
	Integer*4 Isz,i
	Integer*4 Amiga
	External Amiga
C	common/consfh/fh
	CHARACTER*1 OARRY(100)
	InTeGer*4 OSWIT,OCNTR
C	COMMON/OAR/OSWIT,OCNTR,OARRY
C COMMON OAR SWITCHES OUTPUT OFF IF OSWIT=2
	InTeGer*4 IPS1,IPS2,MODFLG
C	COMMON/ICPOS/IPS1,IPS2,MODFLG
   	InTeGer*4 XTCFG,IPSET,XTNCNT
   	CHARACTER*1 XTNCMD(80)
C   	COMMON/XCMD/XTNCNT,XTNCMD,XTCFG,IPSET
C VARY FLAG ITERATION COUNT
	INTEGER KALKIT
C	COMMON/VARYIT/KALKIT
	InTeGer*4 FORMFG,RCFGX,PZAP,RCONE
	InTeGer*4 RCMODE,IRCE1,IRCE2
C	COMMON/FFGG/FORMFG,RCFGX,PZAP,RCONE,RCMODE,IRCE1,
C     1  IRCE2
C FORMFG FLAGS FORMAT OF DISPLAY GLOBALLY
C RCFGX FLAGS WHETHER TO DO AUTO RECALCULATION. IF 1, INHIBITS
C AUTO RECALC (USE R COMMAND TO DO A CALC.). 17 COMMAND TURNS
C RCFGX ON.
C PZAP CONTROLS WHETHER TO REDRAW SCREEN. IF ZERO, NORMAL. IF 1
C (NONZERO ANYHOW), INHIBITS RE DISPLAY. V COMMAND RESETS TO 0
C  AND VM INHIBITS. (SETS TO 1).
	INTEGER*4 FH
C FILE HANDLE FOR CONSOLE I/O (RAW)
C	COMMON/CONSFH/FH
	CHARACTER*1 ARGSTR(52,4)
C	COMMON/ARGSTR/ARGSTR
	COMMON/XVXTCD/OSWIT,OCNTR,OARRY,IPS1,IPS2,MODFLG,
     1  XTNCNT,XTNCMD,XTCFG,IPSET,KALKIT,
     2  FORMFG,RCFGX,PZAP,RCONE,RCMODE,IRCE1,
     3  IRCE2,FH,ARGSTR
	If(fh.ne.0)I=amiga(Write,fh,ibuf,isz)
	return
	end
	Subroutine ttyin(IIMODE,line)
c read 132 char line off console
C iimode=0 in Command-Mostly mode, 1 in Enter mostly mode.
	Integer*4 iact,n,IIMODE
	include dos.inc
	Integer*4 Amiga
	External Amiga
C	common/consfh/fh
	CHARACTER*1 OARRY(100)
	InTeGer*4 OSWIT,OCNTR
C	COMMON/OAR/OSWIT,OCNTR,OARRY
C COMMON OAR SWITCHES OUTPUT OFF IF OSWIT=2
	InTeGer*4 IPS1,IPS2,MODFLG
C	COMMON/ICPOS/IPS1,IPS2,MODFLG
   	InTeGer*4 XTCFG,IPSET,XTNCNT
   	CHARACTER*1 XTNCMD(80)
C   	COMMON/XCMD/XTNCNT,XTNCMD,XTCFG,IPSET
C VARY FLAG ITERATION COUNT
	INTEGER KALKIT
C	COMMON/VARYIT/KALKIT
	InTeGer*4 FORMFG,RCFGX,PZAP,RCONE
	InTeGer*4 RCMODE,IRCE1,IRCE2
C	COMMON/FFGG/FORMFG,RCFGX,PZAP,RCONE,RCMODE,IRCE1,
C     1  IRCE2
C FORMFG FLAGS FORMAT OF DISPLAY GLOBALLY
C RCFGX FLAGS WHETHER TO DO AUTO RECALCULATION. IF 1, INHIBITS
C AUTO RECALC (USE R COMMAND TO DO A CALC.). 17 COMMAND TURNS
C RCFGX ON.
C PZAP CONTROLS WHETHER TO REDRAW SCREEN. IF ZERO, NORMAL. IF 1
C (NONZERO ANYHOW), INHIBITS RE DISPLAY. V COMMAND RESETS TO 0
C  AND VM INHIBITS. (SETS TO 1).
	INTEGER*4 FH
	Character*1 wrkchr,lstchr
	Integer*4 iescst
C FILE HANDLE FOR CONSOLE I/O (RAW)
C	COMMON/CONSFH/FH
	CHARACTER*1 ARGSTR(52,4)
C	COMMON/ARGSTR/ARGSTR
	COMMON/XVXTCD/OSWIT,OCNTR,OARRY,IPS1,IPS2,MODFLG,
     1  XTNCNT,XTNCMD,XTCFG,IPSET,KALKIT,
     2  FORMFG,RCFGX,PZAP,RCONE,RCMODE,IRCE1,
     3  IRCE2,FH,ARGSTR
	character*1 line(132)
	InTeGer*4 RRWACT,RCLACT
C	COMMON/RCLACT/RRWACT,RCLACT
	InTeGer*4 idol1,idol2,idol3,idol4,idol5,idol6,
     1  IDOL7,IDOL8
C	common/dollr/idol1,idol2,idol3,idol4,idol5,idol6,
C     1  IDOL7,IDOL8
	InTeGer*4 PROW,PCOL,DROW,DCOL,DRWV,DCLV,LLCMD,LLDSP
C	COMMON/DCTL/PROW,PCOL,DROW,DCOL,DRWV,DCLV
	InTeGer*4 IPGMAX,LPGMXF,IPGMOD,LPGMOD
C	COMMON/FILEMX/IPGMAX,LPGMXF,IPGMOD,LPGMOD
C LENGTHS (IN K) OF FILES FOR VALUES OR FORMULAS ARE IPGMAX,LPGMXF
C IPGMOD AND LPGMOD CONTROL PACKING MODE IN THE CORRESPONDING FILES
	InTeGer*4 KLVL
C	COMMON/KLVL/KLVL
	InTeGer*4 IOLVL,IGOLD
C	COMMON/IOLVL/IOLVL
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.
	COMMON/RDD/RRWACT,RCLACT,idol1,idol2,idol3,idol4,idol5,idol6,
     1  IDOL7,IDOL8,PROW,PCOL,DROW,DCOL,DRWV,DCLV,LLCMD,LLDSP,
     2  IPGMAX,LPGMXF,IPGMOD,LPGMOD,KLVL,IOLVL,IGOLD
CCC	InTeGer*4 LLCMD,LLDSP
CCC	InTeGer*4 PROW,PCOL,DROW,DCOL,DRWV,DCLV
CCC	COMMON/DCTL/PROW,PCOL,DROW,DCOL,DRWV,DCLV,LLCMD,LLDSP
	iescst=0
	wrkchr=char(0)
c initially, no ESC seen
c Set up to read raw: device OK.
c If we see an ESC character then look for either a return
c (to terminate in any case) or some character whose value is
c greater than 64. However ESC O will be passed and the scan will
c continue.
C implement deletion of last character also with DEL or with
C backspace keys
c
c Initially zero entire buffer so we later can find length via looking
c for anything non-zero. Also serves to put in terminators for things
c like the INDX function to prevent them from running on indefinitely.
	do 1 n=1,132
1	line(n)=char(0)
c if mode 0, (command mostly) then / is NOT special
	if(fh.eq.0)goto 1000
c Here begin the read loop
	n=1
4000	continue
c	lstchr=wrkchr
c	wrkchr=char(0)
cC zero wrkchr for safety
c	iact=amiga(Read,fh,wrkchr,1)
c	If(n.gt.1.or.wrkchr.ne.'/')goto 4050
CC
CC Add this to just read the line
	iact=amiga(Read,fh,line,132)
cccc	If(line(1).ne.'/')goto 4050
cccc	If(IIMODE.eq.0)goto 4050
ccccC if we see / in column 1, write a brief prompt message in
ccccC the display area. Do this only if in enter-mostly mode.
cccc	CALL UVT100(1,LLDSP,1)
cccc	CALL SWRT('Add,Cpy,Dsp,Fil,Get,Kalc,Loc,Mov,Put,Recal,Set',46)
cccc	CALL SWRT(',Tst,View,Wrt,Xit,Zap,/,Help',28)
cccc	CALL UVT100(1,LLCMD,11)
4050	Continue
c	If(ichar(wrkchr).ne.8.and.ichar(wrkchr).ne.127)goto 4100
cC back up a character and try again
c	n=max0(1,(n-1))
c	lstchr=char(8)
cC echo a backspace
cC 8 is ASCII backspace...
c	Call swrt(lstchr,1)
c	Goto 4000
c4100	Continue
cc C.R. is 13, LF is 10, FF is 14, so terminate on any of these
cc traditional line terminators.
c	If(ichar(wrkchr).lt.16)goto 5000
c	line(n)=wrkchr
c	n=min0(n+1,131)
c	if(ichar(wrkchr).eq.27)iescst=1
cc <ESC>O is actually an escape sequence initiator
c	If(iescst.eq.1.and.wrkchr.eq.'O'.and.ichar(lstchr)
c     1  .eq.27) goto 4200
cc Otherwise an escape sequence ends in a letter
c	if(iescst.eq.1.and.ichar(wrkchr).gt.64)goto 5000
c4200	Continue
cc The above condition terminates an ESC sequence after ESC and any other
cc characters followed by (and including) any character greater than 'A'
cc which should take care of just about every ANSI escape sequence.
c	if(n.lt.131)goto 4000
cc Terminate even if we never get C.R. but not 'till we've got
cc all there is to get...
c5000	continue
c done reading now.
	Return
1000	Continue
C fakeout fallback position, reading workbench window
	Read(*,1500)line
1500	format(132a1)
	return
	end
	subroutine swset(i)
	integer*4 i
c dummy setup sub
	return
	end
	subroutine exitqq
c exit routine ... just do fortran stop to make it complete
	stop "AnalytiCalc exiting..."
	end
	subroutine system(line)
	include dos.inc
c execute an amigados command
	integer*4 inp,outp
	character*80 line
	logical*4 succ
	Logical*4 Amiga
	External Amiga
	succ=amiga(Execute,line(1:80),inp,outp)
	return
	end
