//MXALTR.BCPL

get "streams.d"

external [
//OS
	CallSwat; OpenFile; CallSubsys; CounterJunta; lvUserFinishProc
	Closes

//MDISP
	SetDisplay

//MXALTIO
	ArgsAltIO; CallingAltIO

//MCMD
	StartCmdOverlay

//MASM
	ErrorProtect

//MINIT0
	MidasRFP

//STATE package
	SaveState

//Defined here
	CallAltIO; InitAltIOFP
]

static [ AltIOFP ]

let CallAltIO() be
[	ErrorProtect(lv StartCmdOverlay,lv ArgsAltIO)
	if CallingAltIO do
	[ SetDisplay(true)
	  while @lvUserFinishProc ne 0 do (@lvUserFinishProc)()
	  SaveState(OpenFile("MIDAS.RESUME",ksTypeWriteOnly,wordItem,
		verLatestCreate,MidasRFP))
	  CounterJunta(CallAltIONow)
	]
]


and CallAltIONow() be
[	let F = OpenFile("AltIO.run",ksTypeReadOnly,wordItem,0,AltIOFP)
//AltIO writes "Midas.run/R" in Com.CM and does a CallSubsys to return
	CallSubsys(F); CallSwat()
]


//Cannot init AltIOFP in MINIT0 because it is used after CounterJunta
//which smashes free storage
and InitAltIOFP() be
[	AltIOFP = table [ 0; 0; 0; 0; 0; 0 ]
	let S = OpenFile("AltIO.run",ksTypeReadOnly,wordItem,0,AltIOFP)
	if S eq 0 then CallSwat("No AltIO.run")
	Closes(S)
]