TermEmCmds:
CEDAR
PROGRAM
IMPORTS CharDisplays, Commander, CommanderOps, DisplayControllers, FileNames, 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 ¬ FileNames.CurrentWorkingDirectory[];
lastEm: Em ¬ NIL;
MakeEm:
PROC [cmd: Commander.Handle]
RETURNS [result:
REF ¬
NIL, msg:
ROPE ¬
NIL] = {
termName: ROPE ¬ "dm2500w";
displayClass: ROPE ¬ "UnselectedFlushedBufferedTioga";
protocolName: ROPE ¬ SELECT Basics.bitsPerWord FROM 16 => "PupTelnet", 32 => "Echo", ENDCASE => ERROR;
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: CommanderOps.ArgumentVector ¬ CommanderOps.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.PutFR1["Arg %g empty", IO.int[i]]];
argv[i].Fetch[0]='- => RETURN [$Failure, IO.PutFR1["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;
};
IF displayClass.Equal["?"]
THEN {
first: BOOL ¬ TRUE;
NoteClass:
PROC [cdc: CharDisplayClass] = {
IF first THEN first ¬ FALSE ELSE cmd.out.PutRope[", "];
cmd.out.PutRope[cdc.name];
};
cmd.out.PutRope["Available Display Classes: "];
CharDisplays.EnumerateClasses[NoteClass];
cmd.out.PutRope[".\n"];
RETURN;
};
IF protocolName.Equal["?"]
THEN {
first: BOOL ¬ TRUE;
NoteProtocol:
PROC [p: Protocol] = {
IF first THEN first ¬ FALSE ELSE cmd.out.PutRope[", "];
cmd.out.PutRope[p.name];
};
cmd.out.PutRope["Available Protocols: "];
Protocols.EnumerateProtocols[NoteProtocol];
cmd.out.PutRope[".\n"];
RETURN;
};
term ¬ TermProgs.GetTerm[termName, cmd];
IF term = NIL THEN RETURN [$Failed, IO.PutFR1["Couldn't find terminal %g", IO.rope[termName]]];
cdc ¬ CharDisplays.GetClass[displayClass];
IF cdc = NIL THEN RETURN [$Failed, IO.PutFR1["Couldn't find display class %g", IO.rope[displayClass]]];
p ¬ Protocols.GetProtocol[protocolName];
IF p = NIL THEN RETURN [$Failed, IO.PutFR1["Couldn't find protocol %g", IO.rope[protocolName]]];
cd ¬ CharDisplays.Create[class: cdc, client: unchangeableClient, tipTableName: term.tipTableName, 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 ¬ Menus.GetNumberOfLines[m];
Menus.ChangeNumberOfLines[m, 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 => {
Menus.AppendMenuEntry[m, em.myEntries[Login], em.myLine];
Menus.AppendMenuEntry[m, em.myEntries[Connect], em.myLine];
};
connecting => {
Menus.AppendMenuEntry[m, em.myEntries[GiveUp], em.myLine];
};
connected => {
Menus.AppendMenuEntry[m, em.myEntries[Disconnect], em.myLine];
};
disconnecting => {
Menus.AppendMenuEntry[m, em.myEntries[GiveUp], em.myLine];
};
ENDCASE => ERROR;
SELECT c.state.onLine
FROM
FALSE => Menus.AppendMenuEntry[m, em.myEntries[Remote], em.myLine];
TRUE => Menus.AppendMenuEntry[m, 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: "(-term (<terminal type>|?) | -display (<class>|?) | -protocol (<name>|?) | [-host] <name> | - login | -connect | -logRaw | -dontLogRaw)* --- Make a terminal emulator"];
}.