TermEmCmds:
CEDAR
PROGRAM
IMPORTS CharDisplays, Commander, CommandTool, DisplayControllers, IO, Menus, Protocols, Rope, TermProgs, ViewerOps, ViewerTools
= {OPEN CharDisplays, DisplayControllers, TermProgs;
Protocol: TYPE = Protocols.Protocol;
Conversant: TYPE = Protocols.Conversant;
Em: TYPE = REF EmRep;
EmRep:
TYPE =
RECORD [
termName: ROPE,
term: Term,
dc: DisplayController,
p: Protocol,
c: Conversant,
myLine: Menus.MenuLine ← 0,
myEntries: MenuEntries ← NIL];
MenuEntries: TYPE = REF MenuEntriesArray;
MenuEntriesArray: TYPE = ARRAY MenuEntry OF Menus.MenuEntry;
MenuEntry: TYPE = {Login, Connect, GiveUp, Disconnect, Local, Remote};
emType: Protocols.ClientType ←
NEW [Protocols.ClientTypePrivate ← [
NoteState: NoteState]];
installationDirectory: ROPE ← CommandTool.CurrentWorkingDirectory[];
lastEm: Em ← NIL;
MakeEm:
PROC [cmd: Commander.Handle]
RETURNS [result:
REF ←
NIL, msg:
ROPE ←
NIL] = {
termName: ROPE ← "dm2500w";
displayClass: ROPE ← "UnselectedFlushedBufferedTioga";
protocolName: ROPE ← "PupTelnet";
serverName: ROPE ← NIL;
login: BOOL ← TRUE;
log: BOOL ← FALSE;
em: Em;
term: Term;
cdc: CharDisplayClass;
cd: CharDisplay ← NIL;
dc: DisplayController ← NIL;
p: Protocol;
i: INT ← 1;
argv: CommandTool.ArgumentVector ← CommandTool.Parse[cmd];
WHILE i < argv.argc
DO
SELECT
TRUE
FROM
argv[i].Equal["-term"] => {termName ← argv[i+1]; i ← i+2};
argv[i].Equal["-display"] => {displayClass ← argv[i+1]; i ← i+2};
argv[i].Equal["-protocol"] => {protocolName ← argv[i+1]; i ← i+2};
argv[i].Equal["-host"] => {serverName ← argv[i+1]; i ← i+2};
argv[i].Equal["-login"] => {login ← TRUE; i ← i + 1};
argv[i].Equal["-connect"] => {login ← FALSE; i ← i + 1};
argv[i].Equal["-logRaw"] => {log ← TRUE; i ← i + 1};
argv[i].Equal["-dontLogRaw"] => {log ← FALSE; i ← i + 1};
argv[i].Length[]=0 => RETURN [$Failure, IO.PutFR["Arg %g empty", IO.int[i]]];
argv[i].Fetch[0]='- => RETURN [$Failure, IO.PutFR["Unrecognized switch: %g", IO.refAny[argv[i]]]];
ENDCASE => {serverName ← argv[i]; i ← i+1};
ENDLOOP;
IF termName.Equal["?"]
THEN {
first: BOOL ← TRUE;
NoteTerm:
PROC [name:
ROPE, term: Term] = {
IF first THEN first ← FALSE ELSE cmd.out.PutRope[", "];
cmd.out.PutRope[name];
};
cmd.out.PutRope["Available Terminal Types: "];
TermProgs.EnumerateTerms[NoteTerm];
cmd.out.PutRope[".\n"];
RETURN;
};
term ← TermProgs.GetTerm[termName, cmd];
IF term = NIL THEN RETURN [$Failed, IO.PutFR["Couldn't find terminal %g", IO.rope[termName]]];
cdc ← CharDisplays.GetClass[displayClass];
IF cdc = NIL THEN RETURN [$Failed, IO.PutFR["Couldn't find display class %g", IO.rope[displayClass]]];
p ← Protocols.GetProtocol[protocolName];
IF p = NIL THEN RETURN [$Failed, IO.PutFR["Couldn't find protocol %g", IO.rope[protocolName]]];
cd ← CharDisplays.Create[class: cdc, det: term.det];
dc ← DisplayControllers.Create[cd, term.cp];
em ← NEW [EmRep ← [termName, term, dc, p]];
em.c ← em.p.Instantiate[
NEW [Protocols.ClientPrivate ← [
fromClient: em.dc.fromDisplay,
toClient: em.dc.toDisplay,
type: emType,
clientData: em
]],
IF log THEN "Term.RawLog" ELSE NIL
];
IF dc.cd.viewer #
NIL
THEN {
m: Menus.Menu ← em.dc.cd.viewer.menu;
oldLines: Menus.MenuLine ← em.myLine ← m.GetNumberOfLines[];
m.ChangeNumberOfLines[oldLines+1];
em.myEntries ←
NEW [MenuEntriesArray ← [
Login: Menus.CreateEntry["Login", Login, em],
Connect: Menus.CreateEntry["Connect", Connect, em],
GiveUp: Menus.CreateEntry["GiveUp", GiveUp, em],
Disconnect: Menus.CreateEntry["Disconnect", Disconnect, em],
Local: Menus.CreateEntry["Local", Local, em],
Remote: Menus.CreateEntry["Remote", Remote, em]
]];
};
NoteState[em.c];
IF serverName.Length[] > 0 THEN em.p.StartConnect[em.c, serverName, login];
lastEm ← em;
};
NoteState:
PROC [c: Conversant] = {
em: Em = NARROW[c.client.clientData];
SetDriver[em.dc, c.driver];
IF em.dc.cd.viewer #
NIL
AND
NOT em.dc.cd.class.Destroyed[em.dc.cd]
THEN {
m: Menus.Menu ← em.dc.cd.viewer.menu;
Menus.SetLine[m, em.myLine, NIL];
SELECT c.state.connectivity
FROM
disconnected => {
m.AppendMenuEntry[em.myEntries[Login], em.myLine];
m.AppendMenuEntry[em.myEntries[Connect], em.myLine];
};
connecting => {
m.AppendMenuEntry[em.myEntries[GiveUp], em.myLine];
};
connected => {
m.AppendMenuEntry[em.myEntries[Disconnect], em.myLine];
};
disconnecting => {
m.AppendMenuEntry[em.myEntries[GiveUp], em.myLine];
};
ENDCASE => ERROR;
SELECT c.state.onLine
FROM
FALSE => m.AppendMenuEntry[em.myEntries[Remote], em.myLine];
TRUE => m.AppendMenuEntry[em.myEntries[Local], em.myLine];
ENDCASE => ERROR;
ViewerOps.PaintViewer[em.dc.cd.viewer, menu];
SetName[em, IF c.state.connectivity=connected THEN c.serverName ELSE NIL];
};
};
SetName:
PROC [em: Em, serverName:
ROPE] = {
em.dc.cd.viewer.name ← IO.PutFR["%g; %g; %g", IO.rope[em.termName], IO.rope[em.dc.cd.class.name], IO.rope[em.p.name]];
IF serverName # NIL THEN em.dc.cd.viewer.name ← serverName.Cat["; ", em.dc.cd.viewer.name];
ViewerOps.PaintViewer[em.dc.cd.viewer, caption];
};
Login:
PROC [parent:
REF
ANY, clientData:
REF
ANY ←
NIL, mouseButton: Menus.MouseButton ← red, shift, control:
BOOL ←
FALSE]
--Menus.MenusProc-- = {
em: Em = NARROW[clientData];
serverName: ROPE ← ViewerTools.GetSelectionContents[];
em.p.StartConnect[em.c, serverName, TRUE];
};
Connect:
PROC [parent:
REF
ANY, clientData:
REF
ANY ←
NIL, mouseButton: Menus.MouseButton ← red, shift, control:
BOOL ←
FALSE]
--Menus.MenusProc-- = {
em: Em = NARROW[clientData];
serverName: ROPE ← ViewerTools.GetSelectionContents[];
em.p.StartConnect[em.c, serverName, FALSE];
};
GiveUp:
PROC [parent:
REF
ANY, clientData:
REF
ANY ←
NIL, mouseButton: Menus.MouseButton ← red, shift, control:
BOOL ←
FALSE]
--Menus.MenusProc-- = {
em: Em = NARROW[clientData];
em.p.GiveUp[em.c];
};
Disconnect:
PROC [parent:
REF
ANY, clientData:
REF
ANY ←
NIL, mouseButton: Menus.MouseButton ← red, shift, control:
BOOL ←
FALSE]
--Menus.MenusProc-- = {
em: Em = NARROW[clientData];
em.p.StartDisconnect[em.c];
};
Local:
PROC [parent:
REF
ANY, clientData:
REF
ANY ←
NIL, mouseButton: Menus.MouseButton ← red, shift, control:
BOOL ←
FALSE]
--Menus.MenusProc-- = {
em: Em = NARROW[clientData];
em.p.SetOnLine[em.c, FALSE];
};
Remote:
PROC [parent:
REF
ANY, clientData:
REF
ANY ←
NIL, mouseButton: Menus.MouseButton ← red, shift, control:
BOOL ←
FALSE]
--Menus.MenusProc-- = {
em: Em = NARROW[clientData];
em.p.SetOnLine[em.c, TRUE];
};
Commander.Register[key: "TerminalEmulator", proc: MakeEm, doc: "Make a terminal emulator"];
}.