//MXALTIO	overlay for calls to AltIO

get "streams.d"
get "altofilesys.d"

external [
//system
	OpenFile; Wss; Wns; Resets; Closes; Puts; Zero; CallSwat
	fpComCm; Allocate

//Midas
	CmdAbortAct; CmdAbort; QuitCmdOverlay; CreateAction
	WsMarkA; CmdCommentStream; CallAltIO; FormMenu
	FormCmdmenuText; CmdMDFS; CFileStream; CFOutStream
	OverlayZone; ReadAllRegs; SaveRcdFile

//Defined here
	ArgsAltIO; CallingAltIO
]

static	[
//  Actions
	AltDoItAct; ALTACT; AltVec; AltFlag; CallingAltIO
]

manifest [ NAltItems = 5
]

let ArgsAltIO() = valof
[	CallingAltIO = false
	ALTACT = Allocate(OverlayZone,2*NAltItems+1)
	AltVec = ALTACT+NAltItems; Zero(AltVec,NAltItems)
	AltFlag = table [ $B; $S; $R; $P; $H ]
	DefAlt("Boot-MExec",0); DefAlt("RunTenex",1)
	DefAlt("Reset-Memory",2); DefAlt("Protect",3)
	DefAlt("Dont-Go",4)
	AltDoItAct = CreateAction("Do-It",lv AltDoIt,0)
	CmdAbortAct = CreateAction("Abort",lv CmdAbort,0,0,$C-100B)
	resultis AltIOMenu
]


and DefAlt(menuS,vecX,Flag) be
[	ALTACT!vecX = CreateAction(menuS,lv DoAltItem,vecX)
]


and AltIOMenu(S,Nix) be
[	Resets(CmdCommentStream)
	Wss(CmdCommentStream,"AltIO.run")
	WsMarkA(CmdAbortAct)
	for I = 0 to NAltItems-1 do
	[ test AltVec!I eq 0
	  ifso WsMarkA(ALTACT!I)
	  ifnot
	  [ Puts(CmdCommentStream,$/); Puts(CmdCommentStream,AltFlag!I)
	  ]
	]
	WsMarkA(AltDoItAct)
]


and DoAltItem(S,garb,MBUnion,vecX) be
[	AltVec!vecX = AltFlag!vecX; FormMenu(CmdMDFS,FormCmdmenuText)
]


and AltDoIt(S,garb,MBUnion,Nix) be
[	let F = OpenFile("Com.CM",ksTypeWriteOnly,charItem,verLatest,fpComCm)
	if F eq 0 then CallSwat()
	Wss(F,"AltIO.run")
	for I = 0 to NAltItems-1 do
	[ if AltVec!I ne 0 do
	  [ Puts(F,$/); Puts(F,AltFlag!I) ]
	]
	Puts(F,$ ); Closes(F)
	SaveRcdFile()		//Write out extra symbol blocks
	CallingAltIO = true
	if CFileStream ne 0 do [ Closes(CFileStream); CFileStream = 0 ]
	if CFOutStream ne 0 do [ Closes(CFOutStream); CFOutStream = 0 ]
	QuitCmdOverlay()
]