SimpleStreamsOnTypeScriptImpl.mesa
Copyright Ó 1988, 1992 by Xerox Corporation. All rights reserved.
Christian Jacobi, September 2, 1988 11:31:49 am PDT
Christian Jacobi, March 27, 1992 4:49 pm PST
DIRECTORY
Ascii, IO, Process, Rope, SimpleStreams, TypeScript, ViewerOps;
SimpleStreamsOnTypeScriptImpl: CEDAR MONITOR
IMPORTS IO, Process, Rope, SimpleStreams, TypeScript, ViewerOps =
BEGIN
MyRec: TYPE = RECORD [
ts: TypeScript.TS,
alive: BOOL ¬ TRUE
];
TSCreateSimpleStreams: SimpleStreams.CreateProcType = {
hh: REF MyRec ¬ NEW[MyRec];
IF Rope.IsEmpty[header] THEN header ¬ "Simple Stream";
hh.ts ¬ TypeScript.Create[info: [name: header]];
out ¬ IO.CreateStream[outputStreamProcs, hh];
in ¬ IO.CreateStream[inputStreamProcs, hh];
hasEcho ¬ FALSE;
};
inputStreamProcs: REF IO.StreamProcs ¬ IO.CreateStreamProcs[
variety: $input,
class: $X11SimpleStreamsInput,
charsAvail: MyCharsAvail,
getChar: MyGetChar,
close: MyCloseBoth
];
outputStreamProcs: REF IO.StreamProcs ¬ IO.CreateStreamProcs[
variety: $output,
class: $X11SimpleStreamsOutput,
putChar: MyPutChar,
eraseChar: MyEraseChar,
close: MyCloseBoth
];
MyPutChar: PROC [self: IO.STREAM, char: CHAR] = {
hh: REF MyRec = NARROW[self.streamData];
IF hh.ts.destroyed THEN GOTO oops;
IF char=Ascii.BS
THEN TypeScript.BackSpace[hh.ts ! TypeScript.Destroyed => {GOTO oops}]
ELSE TypeScript.PutChar[hh.ts, char ! TypeScript.Destroyed => {GOTO oops}];
EXITS oops => {MyCloseBoth[self]; ERROR IO.Error[StreamClosed, self]};
};
MyGetChar: PROC [self: IO.STREAM] RETURNS [ch: CHAR] = {
hh: REF MyRec = NARROW[self.streamData];
IF hh.ts.destroyed THEN GOTO oops;
ch ¬ TypeScript.GetChar[hh.ts ! TypeScript.Destroyed => {GOTO oops}];
IF ch=4C THEN MyCloseBoth[self]; --**works only if somebody is asking for input...
EXITS oops => {MyCloseBoth[self]; ERROR IO.Error[StreamClosed, self]};
};
MyEraseChar: PROC [self: IO.STREAM, char: CHAR] = {
hh: REF MyRec = NARROW[self.streamData];
IF hh.ts.destroyed THEN GOTO oops;
TypeScript.BackSpace[hh.ts ! TypeScript.Destroyed => {GOTO oops}];
EXITS oops => {MyCloseBoth[self]; ERROR IO.Error[StreamClosed, self]};
};
MyCharsAvail: PROC [self: IO.STREAM, wait: BOOL] RETURNS [n: INT] = {
hh: REF MyRec = NARROW[self.streamData];
IF hh.ts.destroyed THEN GOTO oops;
IF wait THEN {
WHILE ~TypeScript.CharsAvailable[hh.ts ! TypeScript.Destroyed => GOTO oops] DO
Process.Pause[1];
ENDLOOP;
RETURN [1]
};
IF TypeScript.CharsAvailable[hh.ts ! TypeScript.Destroyed => GOTO oops]
THEN RETURN [1]
ELSE RETURN [0];
EXITS oops => {MyCloseBoth[self]; n ¬ LAST[INT]};
};
SetDead: ENTRY PROC [hh: REF MyRec] RETURNS [was: BOOL¬FALSE] = {
ENABLE UNWIND => NULL;
IF hh#NIL THEN {was ¬ hh.alive; hh.alive ¬ FALSE};
};
MyCloseBoth: PROC [self: IO.STREAM, abort: BOOL ¬ FALSE] = {
hh: REF MyRec = NARROW[self.streamData];
wasAlive: BOOL ¬ SetDead[hh];
IF wasAlive THEN {
ViewerOps.DestroyViewer[hh.ts]
};
};
SimpleStreams.ImplementCreate[$TypeScript, TSCreateSimpleStreams];
END.