ISTestImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
MKaplan, September 12, 1985 3:21:57 am PDT
DIRECTORY
Process USING [CheckForAbort],
FS USING [ErrorDesc, Error, StreamOpen, ExpandName],
IO USING [STREAM, GetIndex, PutF],
RopeFile USING [FromStream],
Rope USING [ROPE, Concat, Equal, Length, Fetch],
Commander USING [CommandProc, Register, Handle],
CommandTool USING [ArgumentVector, Parse, Failed],
FileNames USING [ResolveRelativePath, IsAPattern, IsADirectory, ConvertToSlashFormat, CurrentWorkingDirectory],
ISMessage USING [OpenTempFile, OpenTiogaOutput, WriteTiogaOutputFile],
ISNode USING [Implementation, ImplementationObject, GetImplementation, SetImplementation],
ISTiogaIntern USING [DefaultPrintProc, DefaultInternalizeProc, InternalizeDocument, PrintPara, InternalizePara, PrintChars, InternalizeChars, InternalizeStyle, InternalizeProperties, InternalizeLooks],
ISToken USING [NodeTVHandle],
ISScan USING [CreateScan, CollectNode, ScanHandle, InitializeScanThroughCurly, AdvanceScan];
ISTestImpl: CEDAR PROGRAM
IMPORTS Commander, CommandTool, FileNames, FS, IO, RopeFile, ISScan, ISNode, ISTiogaIntern, ISMessage, Rope, Process
~ BEGIN
ISTestProc: Commander.CommandProc = {
[cmd: Commander.Handle] RETURNS [result: REF ANY ← NIL, msg: ROPE ← NIL]
destinationDirectory: Rope.ROPENIL;
leftArrowExists: BOOL;
FSErrorMsg1: PROC [error: FS.ErrorDesc] RETURNS [Rope.ROPE] = {
SELECT error.group FROM
lock => RETURN[" -- locked!"];
ENDCASE =>
IF error.code = $unknownFile
THEN RETURN [" -- not found!"]
ELSE RETURN[Rope.Concat["\n -- FS.Error: ", error.explanation]];
};
HandleAFile: PROC [to, from: Rope.ROPE] = {
Process.CheckForAbort[];
cmd.out.PutF[" %g ← %g\n", [rope[to]], [rope[from]]];
{ENABLE
FS.Error =>
IF error.group # bug THEN {msg ← FSErrorMsg1[error];};
s: IO.STREAMFS.StreamOpen[to, $create];
script: Rope.ROPE;
i: INT;
pattern: Rope.ROPE;
handle: ISToken.NodeTVHandle;
scanner: ISScan.ScanHandle;
implementation, documentImpl, paraImpl, charsImpl, styleImpl, propertiesImpl, looksImpl: ISNode.Implementation;
ISMessage.OpenTempFile[];
ISMessage.OpenTiogaOutput[to];
pattern ← from;
pattern ← FileNames.ResolveRelativePath[pattern];
pattern ← FS.ExpandName[pattern].fullFName;
s ← FS.StreamOpen[pattern];
i ← IO.GetIndex[s];
scriptRopeFile.FromStream[s];
documentImpl ← NEW[ISNode.ImplementationObject];
documentImpl.internalize ← ISTiogaIntern.InternalizeDocument;
[] ← ISNode.SetImplementation [$DOCUMENT, $Tioga, documentImpl];
paraImpl ← NEW[ISNode.ImplementationObject];
paraImpl.internalize ← ISTiogaIntern.InternalizePara;
paraImpl.printTioga ← ISTiogaIntern.PrintPara;
[] ← ISNode.SetImplementation [$PARA, $Tioga, paraImpl];
charsImpl ← NEW[ISNode.ImplementationObject];
charsImpl.internalize ← ISTiogaIntern.InternalizeChars;
charsImpl.printTioga ← ISTiogaIntern.PrintChars;
[] ← ISNode.SetImplementation [$CHARS, $Tioga, charsImpl];
styleImpl ← NEW[ISNode.ImplementationObject];
styleImpl.internalize ← ISTiogaIntern.InternalizeStyle;
[] ← ISNode.SetImplementation [$STYLE, $Tioga, styleImpl];
looksImpl ← NEW[ISNode.ImplementationObject];
looksImpl.internalize ← ISTiogaIntern.InternalizeLooks;
[] ← ISNode.SetImplementation [$LOOKS, $Tioga, looksImpl];
propertiesImpl ← NEW[ISNode.ImplementationObject];
propertiesImpl.internalize ← ISTiogaIntern.InternalizeProperties;
[] ← ISNode.SetImplementation [$PROPERTIES, $Tioga, propertiesImpl];
implementation ← ISNode.GetImplementation[NIL, NIL];
implementation.internalize ← ISTiogaIntern.DefaultInternalizeProc;
implementation.printTioga ← ISTiogaIntern.DefaultPrintProc;
[] ← ISNode.SetImplementation[NIL, NIL, implementation];
scanner ← ISScan.CreateScan[base: script, index: i];
ISScan.InitializeScanThroughCurly[scanner];
ISScan.AdvanceScan[scanner];
handle ← ISScan.CollectNode[scanner];
ISMessage.WriteTiogaOutputFile[];
};
};
argv: CommandTool.ArgumentVector ← CommandTool.Parse[cmd
! CommandTool.Failed => {msg ← errorMsg; GO TO Die}];
nArgs: NAT ← argv.argc;
{
Process switches
i: NAT ← 1;
length: INT;
Bump: PROC [scratch: NAT] = {
FOR j: NAT IN (scratch..nArgs) DO
argv[j - 1] ← argv[j];
ENDLOOP;
nArgs ← nArgs - 1;
};
WHILE i < nArgs DO
length ← argv[i].Length[];
SELECT TRUE FROM
length = 0 => Bump[i];
argv[i].Fetch[0] = '- => {
FOR j: INT IN [1..length) DO
SELECT argv[i].Fetch[j] FROM
no switches...
ENDCASE;
ENDLOOP;
Bump[i];
};
ENDCASE => i ← i + 1;
ENDLOOP;
}; -- end of switch processing
First find out whether there is a ← anywhere. If there is, it must be the second arg.
leftArrowExists ← nArgs >= 3 AND Rope.Equal[argv[2], "←"];
FOR i: NAT IN [1..nArgs) DO
argv[i] ← FileNames.ConvertToSlashFormat[FileNames.ResolveRelativePath[argv[i]]];
ENDLOOP;
IF leftArrowExists
THEN {
IF FileNames.IsADirectory[argv[1]]
THEN destinationDirectory ← argv[1]
ELSE {
IF nArgs # 4 OR FileNames.IsAPattern[argv[1]] OR FileNames.IsAPattern[argv[3]]
THEN RETURN[$Failure, "Bad syntax for copying a file"];
HandleAFile[from: argv[3], to: argv[1]];
RETURN[IF msg # NIL THEN $Failure ELSE NIL, msg];
};
}
ELSE destinationDirectory ←
FileNames.ConvertToSlashFormat[FileNames.CurrentWorkingDirectory[]];
EXITS
Die => result ← $Failure;
};
Commander.Register["ISTest", ISTestProc, "Try out IS parser."];
END.