CommandToolProceduresImpl.Mesa
Spreitzer, December 18, 1985 2:35:11 pm PST
DIRECTORY AMBridge, AMTypes, BBUrpEval, Commander, CommandTool, CommandToolProcedures, EvalQuote, InterpreterOps, InterpreterPrivate, IO, List, PPTreeOps, ProcessProps, Rope, SymTab;
CommandToolProceduresImpl: CEDAR PROGRAM
IMPORTS AMBridge, AMTypes, BBUrpEval, CommandTool, EvalQuote, InterpreterOps, InterpreterPrivate, IO, List, PPTreeOps, ProcessProps
EXPORTS CommandToolProcedures =
BEGIN
ROPE: TYPE = Rope.ROPE;
Type: TYPE = AMTypes.Type;
TV: TYPE = AMTypes.TV;
Tree: TYPE = InterpreterOps.Tree;
EvalHead: TYPE = InterpreterOps.EvalHead;
nullType: Type = AMTypes.nullType;
CmdHandle: TYPE = Commander.Handle;
ROPEType: Type ← CODE[ROPE];
empty: TV ← AMTypes.GetEmptyTV[];
EvalCmd: PROC [head: EvalHead, tree: Tree, target: Type ← nullType, data: REFNIL] RETURNS [return: TV] -- EvalQuote.EvalQuoteProc -- =
BEGIN
args: Tree ← PPTreeOps.NthSon[tree, 2];
result: REF ANY;
first: BOOLTRUE;
PerArg: PROC [t: Tree] = {
IF first THEN {
val: TV ← InterpreterOps.Eval[tree: t, head: head, target: ROPEType];
cmdLine: ROPENIL;
first ← FALSE;
DO
type: Type ← AMTypes.TVType[val];
SELECT AMTypes.UnderClass[type] FROM
rope => {cmdLine ← AMTypes.TVToName[val]; EXIT};
ENDCASE => val ← BBUrpEval.UrpWrongType[head: head, parent: t, value: val, target: ROPEType, msg: "should have been a ROPE"];
ENDLOOP;
result ← CmdWork[cmdLine, head];
};
};
return ← empty;
PPTreeOps.ScanList[args, PerArg];
TRUSTED {return ← AMBridge.TVForReferent[NEW [REF ANY ← result], readOnly]};
END;
Cmd: PUBLIC PROC [command: ROPE] RETURNS [result: REF ANY] =
{result ← CmdWork[command]};
CmdWork: PROC [command: ROPE, head: EvalHead ← NIL] RETURNS [result: REF ANY] = {
ch: CmdHandle ← GetHandle[];
oldSymtab: SymTab.Ref ← NARROW[List.Assoc[key: $SymTab, aList: ch.propertyList]];
RestoreSymTab: PROC =
{ch.propertyList ← List.PutAssoc[key: $SymTab, val: oldSymtab, aList: ch.propertyList]};
WithHead: PROC = {
ch.propertyList ← List.PutAssoc[key: $SymTab, val: head.specials, aList: ch.propertyList];
{ENABLE UNWIND => RestoreSymTab[];
result ← CommandTool.DoCommand[command, ch];
};
RestoreSymTab[];
};
IF head = NIL THEN head ← NARROW[List.Assoc[$EvalHead, ProcessProps.GetPropList[]]];
ProcessProps.AddPropList[
propList: List.PutAssoc[key: $EvalHead, val: head, aList: NIL],
inner: WithHead];
};
Stdin: PUBLIC PROC RETURNS [s: IO.STREAM] =
BEGIN
ch: CmdHandle ← GetHandle[];
s ← ch.in;
END;
Stdout: PUBLIC PROC RETURNS [s: IO.STREAM] =
BEGIN
ch: CmdHandle ← GetHandle[];
s ← ch.out;
END;
Stderr: PUBLIC PROC RETURNS [s: IO.STREAM] =
BEGIN
ch: CmdHandle ← GetHandle[];
s ← ch.err;
END;
PrintRope: PROC [rope: ROPE] RETURNS [ROPE] =
BEGIN
Stdout[].PutRope[rope];
RETURN [rope];
END;
GetHandle: PUBLIC PROC RETURNS [ch: CmdHandle] = {
ch ← NARROW[List.Assoc[$CommanderHandle, ProcessProps.GetPropList[]]];
};
EvalQuote.Register["&cmd", EvalCmd, NIL];
TRUSTED {
InterpreterOps.RegisterTV[
name: "&stdin",
tv: AMBridge.TVForReferent[NEW [PROC RETURNS [IO.STREAM] ← Stdin], readOnly],
symTab: InterpreterPrivate.GetGlobalSymTab[]];
InterpreterOps.RegisterTV[
name: "&stdout",
tv: AMBridge.TVForReferent[NEW [PROC RETURNS [IO.STREAM] ← Stdout], readOnly],
symTab: InterpreterPrivate.GetGlobalSymTab[]];
InterpreterOps.RegisterTV[
name: "&stderr",
tv: AMBridge.TVForReferent[NEW [PROC RETURNS [IO.STREAM] ← Stderr], readOnly],
symTab: InterpreterPrivate.GetGlobalSymTab[]];
InterpreterOps.RegisterTV[
name: "&printRope",
tv: AMBridge.TVForReferent[NEW [PROC [ROPE] RETURNS [ROPE] ← PrintRope], readOnly],
symTab: InterpreterPrivate.GetGlobalSymTab[]];
InterpreterOps.RegisterTV[
name: "&cmd",
tv: AMBridge.TVForReferent[NEW [PROC [command: ROPE] RETURNS [result: REF ANY] ← Cmd], readOnly],
symTab: InterpreterPrivate.GetGlobalSymTab[]];
};
END.