-- TerminalIOMXCode.mesa
-- Copyright Ó 1992 by Xerox Corporation. All rights reserved.
DIRECTORY Ascii, Atom, Rope, Scheme, TerminalIO;
TerminalIOMXCode: CEDAR PROGRAM
IMPORTS Atom, Scheme, TerminalIO
= BEGIN OPEN Scheme;
SymbolFromRope: PROC [Rope.ROPE] RETURNS [Symbol] ~ Atom.MakeAtom;
RopeFromSymbol: PROC [Symbol] RETURNS [Rope.ROPE] ~ Atom.GetPName;
TerminalIOPrim: PROC [SELF: Primitive, ARG1,ARG2,ARG3: Any, REST: ProperList] RETURNS [result: Any ¬ unspecified] = {
POP: PROC RETURNS [a: Any ¬ undefined] = {
IF REST#NIL THEN {a ¬ REST.car; REST ¬ NARROW[REST.cdr]}};
DATA: Pair ~ NARROW[SELF.data];
env: Environment ~ NARROW[DATA.cdr];
SELECT NAT[NARROW[DATA.car, REF INT]­] FROM
7 => {
header: Any ¬ ARG1;
choice: Any ¬ ARG2;
headerdoc: Any ¬ ARG3;
choicedoc: Any ¬ POP[];
default: Any ¬ POP[];
timeout: Any ¬ POP[];
position: Any ¬ POP[];
dontlog: Any ¬ POP[];
result ¬ MakeFixnum[TerminalIO.RequestSelection[
TheROPE[header],
TheROPELIST[choice],
IF headerdoc=undefined THEN NIL ELSE TheROPE[headerdoc],
IF choicedoc=undefined THEN NIL ELSE TheROPELIST[choicedoc],
IF default=undefined THEN 0 ELSE TheNAT[default],
IF timeout=undefined THEN 0 ELSE TheNAT[timeout],
IF position=undefined THEN NIL ELSE TheREF[position],
IF dontlog=undefined THEN FALSE ELSE TheBOOL[dontlog]
]];
};
6 => {
text: Any ¬ ARG1;
help: Any ¬ ARG2;
timeout: Any ¬ ARG3;
dontlog: Any ¬ POP[];
result ¬ MakeBoolean[TerminalIO.Confirm[
Scheme.TheROPE[text],
IF help=undefined THEN NIL ELSE TheROPE[help],
IF timeout=undefined THEN 0 ELSE TheNAT[timeout],
IF dontlog=undefined THEN FALSE ELSE TheBOOL[dontlog]
]];
};
5 => {
prompt: Any ¬ ARG1;
timeout: Any ¬ ARG2;
result ¬ MakeFixnum[TerminalIO.RequestInt[
IF prompt=undefined THEN NIL ELSE Scheme.TheROPE[prompt],
IF timeout=undefined THEN 0 ELSE TheNAT[timeout]]];
};
4 => {
prompt: Any ¬ ARG1;
timeout: Any ¬ ARG2;
result ¬ TerminalIO.RequestRope[
IF prompt=undefined THEN NIL ELSE Scheme.TheROPE[prompt],
IF timeout=undefined THEN 0 ELSE TheNAT[timeout]];
};
3 => {
rope1: Any ¬ ARG1;
rope2: Any ¬ ARG2;
rope3: Any ¬ ARG3;
TerminalIO.PutRopes[Scheme.TheROPE[rope1], IF rope2=undefined THEN NIL ELSE Scheme.TheROPE[rope2], IF rope3=undefined THEN NIL ELSE Scheme.TheROPE[rope3]];
};
2 => {
rope: Any ¬ ARG1;
TerminalIO.PutRope[Scheme.TheROPE[rope]];
};
1 => {
result ¬ TerminalIO.TIS[];
};
0 => {
result ¬ TerminalIO.TOS[];
};
ENDCASE => ERROR
};

TerminalIOInit: PROC [env: Environment] = {
DefinePrimitive[name: "terminalio.requestselection", nArgs: 8, proc: TerminalIOPrim, doc: "(header choice [ headerdoc ] [ choicedoc ] [ default ] [ timeout ] [ position ] [ dontlog ]) PROC [header: Rope.ROPE ← NIL, choice: LIST OF Rope.ROPE, headerDoc: Rope.ROPE ← NIL, choiceDoc: LIST OF Rope.ROPE ← NIL,default: NAT ← 0, timeOut: NAT ← 0, position: REF ← NIL, dontLog: BOOL ← FALSE] RETURNS [INT]; Shows a pop-up menu and returns the number of the selected entry.", env: env, optional: 6, dotted: FALSE, data: Cons[MakeFixnum[7], env]];
DefinePrimitive[name: "terminalio.confirm", nArgs: 4, proc: TerminalIOPrim, doc: "(text [ help ] [ timeout ] [ dontlog ]) PROC [text: Rope.ROPE, help: Rope.ROPE←NIL, timeOut: NAT ← 0, dontLog: BOOL�LSE] RETURNS [BOOL]; Requests user for confirmation; defaults to FALSE. timeOut: in seconds; 0 for no timeout. dontLog: prevent loging in viewer and log file.", env: env, optional: 3, dotted: FALSE, data: Cons[MakeFixnum[6], env]];
DefinePrimitive[name: "terminalio.requestint", nArgs: 2, proc: TerminalIOPrim, doc: "([ prompt ] [ timeout ]) PROC [prompt: Rope.ROPE ← NIL, timeOut: NAT𡤀] RETURNS [INT]; Shortcut; calls RequestRope and converts rope to integer.", env: env, optional: 2, dotted: FALSE, data: Cons[MakeFixnum[5], env]];
DefinePrimitive[name: "terminalio.requestrope", nArgs: 2, proc: TerminalIOPrim, doc: "([ prompt ] [ timeout ]) PROC [prompt: Rope.ROPE←NIL, timeOut: NAT𡤀] RETURNS [Rope.ROPE]; Requests user to type in a rope. May raise UserAbort and TimeOut. prompt: NIL is ok, but others are better. timeOut: in seconds; 0 for no timeout [the user can always abort].", env: env, optional: 2, dotted: FALSE, data: Cons[MakeFixnum[4], env]];
DefinePrimitive[name: "terminalio.putropes", nArgs: 3, proc: TerminalIOPrim, doc: "(rope1 [ rope2 ] [ rope3 ]) PROC [text: Rope.ROPE];", env: env, optional: 2, dotted: FALSE, data: Cons[MakeFixnum[3], env]];
DefinePrimitive[name: "terminalio.putrope", nArgs: 1, proc: TerminalIOPrim, doc: "(rope) Output a Scheme Rope to TerminalIO.PutRope", env: env, optional: 0, dotted: FALSE, data: Cons[MakeFixnum[2], env]];
DefinePrimitive[name: "terminalio.tis", nArgs: 0, proc: TerminalIOPrim, doc: "() PROC [] RETURNS [stream: IO.STREAM]; Creates an Input stream which reads from the terminal viewer. This procedure is lightweight; the viewer is only created when Input is done. No signals or errors are raised, neither on call nor on sequential output.", env: env, optional: 0, dotted: FALSE, data: Cons[MakeFixnum[1], env]];
DefinePrimitive[name: "terminalio.tos", nArgs: 0, proc: TerminalIOPrim, doc: "() PROC [] RETURNS [stream: IO.STREAM]; Creates an output stream which writes its output into the terminal viewer. This procedure is lightweight; the viewer is only created when output is done. No signals or errors are raised, neither on call nor on sequential output.", env: env, optional: 0, dotted: FALSE, data: Cons[MakeFixnum[0], env]];
};

ROPE: TYPE ~ Rope.ROPE; -- a node with top-level Cedar code. This is underlined if you have the right style.

TheNAT: PROC [val: Any] RETURNS [result: NAT] = {
RETURN[Scheme.TheCARD[val]];
};

TheREF: PROC [val: Any] RETURNS [result: REF] = TRUSTED {
RETURN[LOOPHOLE[val]];
};

TheROPELIST: PROC [val: Any] RETURNS [ropes: LIST OF ROPE] = {
WITH val SELECT FROM
val: LIST OF ROPE => RETURN [val];
ENDCASE => Complain[val, "is not a LIST OF ROPE"];
};

RegisterInit[TerminalIOInit];
END.