TerminalIOImpl.mesa
Copyright © 1983, 1986 by Xerox Corporation. All rights reserved.
Created by Christian Jacobi August 3, 1983 9:54 am
Last edited by: Christian Jacobi, November 26, 1986 5:00:28 pm PST
DIRECTORY
BasicTime USING [earliestGMT, GMT, Now, Period],
Commander USING [CommandProc, Register],
Convert USING [IntFromRope],
EditedStream USING [Rubout],
FS USING [StreamOpen],
Icons USING [IconFlavor, NewIconFromFile],
InputFocus,
IO,
PopUpSelection USING [Request],
Process USING [Detach, Pause, MsecToTicks, SetTimeout, SecondsToTicks, Abort, CheckForAbort, DisableTimeout],
Rope USING [ROPE, Find, Match, Equal],
RuntimeError USING [UNCAUGHT],
TerminalIO,
TypeScript USING [Create, PutRope],
ViewerClasses USING [Viewer],
ViewerIO USING [CreateViewerStreams, GetViewerFromStream],
ViewerOps USING [PaintViewer, OpenIcon, EnumerateViewers, EnumProc],
ViewerTools USING [SetSelection];
TerminalIOImpl: CEDAR MONITOR
IMPORTS BasicTime, Commander, Convert, EditedStream, FS, Icons, InputFocus, IO, PopUpSelection, Process, Rope, RuntimeError, TypeScript, ViewerIO, ViewerOps, ViewerTools
EXPORTS TerminalIO =
BEGIN
ROPE: TYPE = Rope.ROPE;
UserAbort: PUBLIC SIGNAL = CODE;
TimedOut: PUBLIC SIGNAL = CODE;
viewer: ViewerClasses.Viewer ← NIL;
in, out: IO.STREAMNIL;
normalName: ROPE = "Terminal Viewer";
inputName: ROPE = "Terminal Viewer [Input Expected]";
normalIcon, inputIcon: Icons.IconFlavor;
del: ROPE = "\177";
log: IO.STREAM;
--using a backing file for viewer makes log unaccessible
shouldFlush: BOOLFALSE;
--whether log file should be flushed
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--locking procedures
occupied: BOOLFALSE; --module occupied
next: CONDITION ← [timeout: Process.MsecToTicks[4000]]; --another client might try entering
Enter: ENTRY PROC [] = {
ENABLE UNWIND => NULL;
WHILE occupied DO WAIT next ENDLOOP;
occupied ← TRUE;
};
Leave: ENTRY PROC [] = {
ENABLE UNWIND => NULL;
occupied ← FALSE;
BROADCAST next;
};
EnterReader: PROC [] = {
v: ViewerClasses.Viewer;
Lock[];
Enter[];
v ← viewer;
WHILE v=NIL OR v.destroyed DO InternalCreate[FALSE]; v ← viewer ENDLOOP;
v.inhibitDestroy ← TRUE;
WHILE v=NIL OR v.destroyed DO InternalCreate[TRUE]; v ← viewer ENDLOOP;
v.icon ← inputIcon;
v.name ← inputName;
ViewerOps.PaintViewer[v, caption];
InputFocus.PushInputFocus[v, $TerminalIO];
ViewerTools.SetSelection[v];--order! SetSelection fools with the InputFocus! a feature
};
LeaveReader: PROC [] = {
v: ViewerClasses.Viewer ← viewer;
foc: InputFocus.Focus ← InputFocus.GetInputFocus[];
IF v#NIL THEN {
v.icon ← normalIcon;
v.name ← normalName;
v.inhibitDestroy ← FALSE; --conveniant here; usefull in all places where called
ViewerOps.PaintViewer[v, caption];
};
IF foc#NIL AND foc.info = $TerminalIO THEN InputFocus.PopInputFocus[];
Leave[];
Free[];
};
ProcList: TYPE = LIST OF PROC;
lockList: ProcList ← NIL;
freeList: ProcList ← NIL;
AddLock: PUBLIC ENTRY PROC [lock, free: PROC] = {
ENABLE UNWIND => NULL;
IF free#NIL THEN freeList ← CONS[free, freeList];
IF lock#NIL THEN
IF lockList=NIL THEN lockList ← LIST[lock]
ELSE
FOR l: ProcList ← lockList, l.rest DO
IF l.rest=NIL THEN {l.rest ← LIST[lock]; EXIT}
ENDLOOP;
};
Lock: PROC = {
FOR l: ProcList ← lockList, l.rest WHILE l#NIL DO
l.first[! RuntimeError.UNCAUGHT => CONTINUE]
ENDLOOP
};
Free: PROC = {
FOR l: ProcList ← freeList, l.rest WHILE l#NIL DO
l.first[! RuntimeError.UNCAUGHT => CONTINUE]
ENDLOOP
};
ReaderProtected: PROC [p: PROC] = {
ENABLE UNWIND => LeaveReader[];
EnterReader[]; p[]; LeaveReader[];
};
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--creation of viewer
Create: PROC [protected: BOOL] = {
--its ok to risk creating accidently too many streams or viewer's
--since this won't cause any wedge but only discomfort [orphaned viewers lying around]
InternalCreate[protected]
};
InternalCreate: PROC [protected: BOOL] = {
--afterwards in any case: viewer #NIL (doesnt really mean good, but certainly not NIL)
FindOldGuys: ViewerOps.EnumProc = {
MakeOrphan: PROC [v: ViewerClasses.Viewer] = {
IF v#NIL THEN {
v.inhibitDestroy ← FALSE;
v.icon ← typescript;
v.name ← "orphaned split terminal viewer";
IO.PutRope[log, "terminal viewer orphaned\n"]; IO.Flush[log];
ViewerOps.PaintViewer[v, caption];
TypeScript.PutRope[v, "\n... terminal viewer orphaned ...\n"];
};
};
IF ~v.destroyed AND v.file=NIL AND v.class.flavor=$Typescript THEN
IF Rope.Equal[v.name, normalName] OR Rope.Equal[v.name, inputName] THEN
MakeOrphan[v ! RuntimeError.UNCAUGHT => CONTINUE];
};
ReallyCreate: PROC [] = {
v: ViewerClasses.Viewer ← TypeScript.Create[info: [name: normalName, inhibitDestroy: protected, iconic: FALSE, icon: normalIcon, column: right]];
[in: in, out: out] ← ViewerIO.CreateViewerStreams[name: normalName, viewer: v, editedStream: TRUE];
viewer ← v;
IO.PutRope[log, "terminal viewer created\n"];
IO.Flush[log];
lastTimeMsg ← BasicTime.earliestGMT; --so the time will be printed
};
--try re-using old pieces, but only if not from input
oldOut: IO.STREAM ← out;
oldIn: IO.STREAM ← in;
IF oldOut#NIL AND oldIn#NIL AND ~protected THEN {
v: ViewerClasses.Viewer;
err: BOOLFALSE;
v ← ViewerIO.GetViewerFromStream[oldOut];
IF v#NIL AND ~v.destroyed AND (v=ViewerIO.GetViewerFromStream[oldIn]) THEN {
--we dare trying to reuse a split viewer,
--but first we try whether it wont cause errors
IO.Flush[oldOut ! RuntimeError.UNCAUGHT => {err ← TRUE; CONTINUE}];
IF ~err THEN IO.Reset[oldOut ! RuntimeError.UNCAUGHT => {err ← TRUE; CONTINUE}];
IF ~err THEN IO.Reset[oldIn ! RuntimeError.UNCAUGHT => {err ← TRUE; CONTINUE}];
IF ~err THEN {
v.icon ← normalIcon;
IF ~v.destroyed THEN {
[in: in, out: out] ← ViewerIO.CreateViewerStreams[name: normalName, viewer: v, editedStream: TRUE];
viewer ← v;
IO.PutRope[log, "usage of different terminal viewer\n"]; IO.Flush[log];
RETURN;
};
};
};
};
--try to mark other [split] viewers as bad
ViewerOps.EnumerateViewers[FindOldGuys ! RuntimeError.UNCAUGHT => CONTINUE];
--the actual creation
ReallyCreate[]
};
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--short cuts
InternalPut: PROC [text: ROPE] = {
IF viewer=NIL OR viewer.destroyed THEN InternalCreate[FALSE];
--asking for viewer.destroyed helps to discover bad splits, but is not mandatory
IO.PutRope[out, text ! IO.Error => {
IF ec=StreamClosed THEN {
InternalCreate[TRUE];
IO.PutRope[out, text];
CONTINUE
}
}];
IO.PutRope[log, text]; shouldFlush ← TRUE;
};
PutRope: PUBLIC PROC [text: ROPE] = {
ENABLE UNWIND => Leave[];
Enter[]; InternalPut[text]; Leave[]
};
PutRopes: PUBLIC PROC [t1, t2, t3: ROPE] = {
ENABLE UNWIND => Leave[];
Enter[]; InternalPut[t1]; InternalPut[t2]; InternalPut[t3]; Leave[]
};
PutF: PUBLIC PROC [format: ROPENIL, v1, v2, v3, v4, v5: IO.Value←[null[]]] = {
--actual usage of IO.PutF inside lock allows clients to use looks
ENABLE UNWIND => Leave[];
Enter[];
IF viewer=NIL OR viewer.destroyed THEN InternalCreate[FALSE];
IO.PutF[out, format, v1, v2, v3, v4, v5
! IO.Error => {
IF ec=StreamClosed THEN {
InternalCreate[TRUE];
IO.PutF[out, format, v1, v2, v3, v4, v5];
CONTINUE
}
}
];
IO.PutF[log, format, v1, v2, v3, v4, v5]; shouldFlush ← TRUE;
Leave[]
};
PutF1: PUBLIC PROC [format: ROPENIL, value: IO.Value] = {
--actual usage of IO.PutF inside lock allows clients to use looks
ENABLE UNWIND => Leave[];
Enter[];
IF viewer=NIL OR viewer.destroyed THEN InternalCreate[FALSE];
IO.PutF1[out, format, value
! IO.Error => {
IF ec=StreamClosed THEN {
InternalCreate[TRUE];
IO.PutF1[out, format, value];
CONTINUE
}
}
];
IO.PutF[log, format, value]; shouldFlush ← TRUE;
Leave[]
};
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--input procedures
RequestRope: PUBLIC PROC [prompt: ROPENIL, timeOut: NAT] RETURNS [ROPE] = {
RETURN [IF timeOut=0 THEN RequestRope0[prompt] ELSE RequestRope1[prompt, timeOut]]
};
IOState: TYPE = {ok, repeat, abort, undef};
InternalGetLine: PROC [promp: ROPE] RETURNS [line: ROPE, state: IOState←ok] = {
ENABLE {
IO.Error => IF ec=StreamClosed THEN {state←repeat; CONTINUE};
EditedStream.Rubout => {state�ort; IO.Reset[in]; CONTINUE};
RuntimeError.UNCAUGHT => {state←repeat; CONTINUE};
};
IF viewer.iconic THEN ViewerOps.OpenIcon[viewer];
InternalPut[promp];
IO.Flush[out ! RuntimeError.UNCAUGHT => CONTINUE];
ViewerTools.SetSelection[viewer];
line ← IO.GetLineRope[in];
IF Rope.Find[line, del]>=0 THEN state�ort;
};
RequestRope0: PROC [prompt: ROPENIL] RETURNS [r: ROPE] = {
DoIt: PROC [] = {[r, state] ← InternalGetLine[prompt]};
state: IOState;
DO
ReaderProtected[DoIt];
SELECT state FROM
ok => RETURN;
repeat => PutRope["XXX\n"];
abort => {PutRope[" ..user abort\n"]; SIGNAL UserAbort} ;
ENDCASE => {PutRope[" ..??\n"]; SIGNAL UserAbort};
ENDLOOP;
};
xProcess: PROCESS;
xStateCondition: CONDITION;
xState: IOState ← undef;
xResult: ROPE;
RequestRope1: PUBLIC PROC [prompt: ROPE, timeOut: NAT] RETURNS [result: ROPE] = {
state: IOState;
DoIt: PROC [] = {
xState ← undef;
IF timeOut=0 THEN TRUSTED {Process.DisableTimeout[@xStateCondition]}
ELSE TRUSTED {
Process.SetTimeout[@xStateCondition, Process.SecondsToTicks[timeOut]]
};
ForkRopeGetter[prompt];
TRUSTED {JOIN xProcess};
state ← xState;
result ← xResult;
IF state=ok THEN IF Rope.Find[result, del]>=0 THEN state ← abort
};
DO
ReaderProtected[DoIt];
SELECT state FROM
ok => RETURN;
repeat => PutRope["XXX\n"];
abort => {PutRope[" ..user abort\n"]; SIGNAL UserAbort} ;
undef => SIGNAL TimedOut
ENDCASE => {PutRope[" ..??\n"]; SIGNAL UserAbort};
ENDLOOP
};
ForkRopeGetter: ENTRY PROC [prompt: ROPENIL] = {
ENABLE UNWIND => NULL;
xProcess ← FORK ForkedTryToGetRope[prompt];
WAIT xStateCondition;
IF xState#ok AND xState#abort THEN TRUSTED {Process.Abort[xProcess]};
};
SetGlobalState: ENTRY PROC [s: IOState] = {
ENABLE UNWIND => NULL;
Process.CheckForAbort[];
xState ← s;
BROADCAST xStateCondition;
};
ForkedTryToGetRope: PROC [prompt: ROPENIL] = {
ReallyTryToGetRope: PROC [prompt: ROPE] = {
d: IOState;
DoIt: PROC [] = {
ENABLE {
UNWIND => IO.Reset[in];
IO.Error => IF ec=StreamClosed THEN {d←repeat; IO.Reset[in]; CONTINUE};
EditedStream.Rubout => {d�ort; IO.Reset[in]; CONTINUE};
};
InternalPut[prompt];
IF viewer.iconic THEN ViewerOps.OpenIcon[viewer];
xResult ← IO.GetLineRope[in];
d ← ok;
};
DO
d ← undef;
DoIt[];
IF d=ok OR d=abort THEN {SetGlobalState[d]; RETURN};
IF d=repeat THEN InternalPut["XXX\n"]
ENDLOOP;
};
ReallyTryToGetRope[prompt ! ABORTED => GOTO NeverMind]
EXITS NeverMind => NULL;
};
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--stream output stuff
CreateStream: PUBLIC PROC [] RETURNS [stream: IO.STREAM] = {
IF viewer=NIL OR viewer.destroyed THEN Create[FALSE];
RETURN[IO.CreateStream[streamProcs: myStreamProcs, streamData: NIL]];
};
myStreamProcs: REF IO.StreamProcs ← IO.CreateStreamProcs[
variety: $output,
class: $TerminalIO,
putChar: MyPutChar,
putBlock: MyPutBlock,
unsafePutBlock: MyUnsafePutBlock,
flush: MyFlush,
eraseChar: MyEraseChar,
reset: MyFlush,
close: MyClose
];
MyPutChar: PROC [self: IO.STREAM, char: CHAR] = {
ENABLE UNWIND => Leave[];
Enter[];
IO.PutChar[out, char ! IO.Error => {
IF ec=StreamClosed THEN {
InternalCreate[TRUE];
IO.PutChar[out, char];
CONTINUE
}
}];
IO.PutChar[log, char]; shouldFlush ← TRUE;
Leave[];
};
MyPutBlock: PROC [self: IO.STREAM, block: REF READONLY TEXT, startIndex: NAT, count: NAT] = {
ENABLE UNWIND => Leave[];
Enter[];
IO.PutBlock[out, block, startIndex, count ! IO.Error => {
IF ec=StreamClosed THEN {
InternalCreate[TRUE];
IO.PutBlock[out, block, startIndex, count];
CONTINUE
}
}];
IO.PutBlock[log, block, startIndex, count]; shouldFlush ← TRUE;
Leave[];
};
MyUnsafePutBlock: PROC [self: IO.STREAM, block: IO.UnsafeBlock] = {
ENABLE UNWIND => Leave[];
Enter[];
IO.UnsafePutBlock[out, block ! IO.Error => {
IF ec=StreamClosed THEN {
InternalCreate[TRUE];
IO.UnsafePutBlock[out, block];
CONTINUE
}
}];
IO.UnsafePutBlock[log, block]; shouldFlush ← TRUE;
Leave[];
};
MyFlush: PROC [self: IO.STREAM] = {
IO.Flush[log];
};
MyEraseChar: PROC [self: IO.STREAM, char: CHAR] = {
};
MyClose: PROC [self: IO.STREAM, abort: BOOL] = {
IO.Flush[log];
};
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--input conveniances
RequestInt: PUBLIC PROC [prompt: Rope.ROPENIL, timeOut: NAT ← 0] RETURNS [i: INT] = {
DO
ok: BOOLTRUE;
r: Rope.ROPE ← RequestRope[prompt, timeOut];
i ← Convert.IntFromRope[r ! RuntimeError.UNCAUGHT => {ok←FALSE; CONTINUE}];
IF ok THEN RETURN;
PutRope[" ?? (integer), please repeat: \n"];
ENDLOOP;
};
Confirm: PUBLIC PROC [text: Rope.ROPE, help: Rope.ROPE, timeOut: NAT, dontLog: BOOL] RETURNS [yes: BOOL] = {
Ask: PROC RETURNS [BOOL] = {
RETURN [
PopUpSelection.Request[
header: "Confirm", headerDoc: "Confirm by hitting entry; discard by hitting outside",
choice: LIST[text], choiceDoc: LIST[help],
timeOut: timeOut
]=1
];
};
AskLogged: PROC = {
InternalPut["confirm: "]; InternalPut[text]; InternalPut[": "];
yes ← Ask[];
InternalPut[(IF yes THEN "yes\n" ELSE "no\n")];
};
IF dontLog THEN yes ← Ask[];
ReaderProtected[AskLogged]
};
RequestSelection: PUBLIC PROC [
header: Rope.ROPENIL, choice: LIST OF Rope.ROPE,
headerDoc: Rope.ROPENIL, choiceDoc: LIST OF Rope.ROPENIL,
default: NAT ← 0, timeOut: NAT ← 0, position: REFNIL,
dontLog: BOOLFALSE
] RETURNS [n: INT] = {
Request: PROC = {
IF header#NIL AND ~dontLog THEN {
InternalPut["pop up selection: "]; InternalPut[header]; InternalPut["\n "];
};
n ← PopUpSelection.Request[header: header, headerDoc: headerDoc, choice: choice, choiceDoc: choiceDoc, timeOut: timeOut];
IF ~dontLog THEN {
IF n<0 THEN InternalPut["timed out\n"]
ELSE IF n=0 THEN InternalPut["skipped\n"]
ELSE {c: LIST OF Rope.ROPE ← choice;
WHILE n>1 DO
c ← c.rest; IF c=NIL THEN RETURN;
ENDLOOP;
InternalPut["select: "]; InternalPut[c.first]; InternalPut["\n"];
};
};
};
IF (choice=NIL) OR (choice.first=NIL) THEN ERROR;
ReaderProtected[Request]
};
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--log and commands
CreateLog: PROC [] = {
log ← FS.StreamOpen[fileName: "TerminalIO.log", accessOptions: create, keep: 5];
IO.PutF1[log, "log file for terminal created %g\n", IO.time[]]
};
Command: Commander.CommandProc = {
IF Rope.Match["*open*", cmd.commandLine, FALSE] THEN {
PutRope["open terminal viewer\n"]; --side effect: creates viewer
ViewerOps.OpenIcon[viewer];
};
IF Rope.Match["*new*", cmd.commandLine, FALSE] THEN {
oldLog: IO.STREAM ← log;
PutRope["start new terminal log file ""TerminalIO.log""\n"];
CreateLog[];
IO.Close[oldLog];
};
};
CheckInhibitDestroy: ENTRY PROC [] = {
v: ViewerClasses.Viewer ← viewer;
IF v#NIL AND ~occupied THEN v.inhibitDestroy ← FALSE;
};
lastTimeMsg: BasicTime.GMT ← BasicTime.earliestGMT;
TimeMessage: PROC [] = {
lastTimeMsg ← BasicTime.Now[];
PutF["%l%g%l\n", [rope["i"]], [time[lastTimeMsg]], [rope[" "]]];
};
Flusher: PROC [] = {
--directly flushing after each output would make TerminalIO far to slow
--process also use to write periodic time mesages
flush: BOOL;
DO
Process.Pause[Process.SecondsToTicks[40]];
IF flush ← shouldFlush THEN {
shouldFlush ← FALSE;
IO.Flush[log ! RuntimeError.UNCAUGHT => CONTINUE];
};
CheckInhibitDestroy[];
IF viewer#NIL AND ~occupied AND ~viewer.destroyed AND BasicTime.Period[from: lastTimeMsg, to: BasicTime.Now[]]>1760 --about half an hour-- THEN TimeMessage[];
ENDLOOP
};
CreateLog[];
normalIcon ← Icons.NewIconFromFile["TerminalIO.icons", 0 ! RuntimeError.UNCAUGHT => CONTINUE];
inputIcon ← Icons.NewIconFromFile["TerminalIO.icons", 1 ! RuntimeError.UNCAUGHT => CONTINUE];
Commander.Register["///Commands/TerminalIO", Command, "usage {TerminalIO ~ | open | new}"];
TRUSTED {Process.Detach[FORK Flusher]};
END.