XNSFileCommandsImpl.mesa
Copyright Ó 1990, 1992 by Xerox Corporation. All rights reserved.
Willie-s, January 31, 1992 5:51 pm PST
Weiser, March 31, 1991 5:47 pm PST
DIRECTORY
Commander USING [CommandProc, Register],
CommanderOps,
Convert USING [CardFromRope, Error],
IO,
PFS,
PFSClass,
PFSPrefixMap,
PFSNames,
Rope,
UserProfile USING [Token],
XNSRemoteFileDeserialize;
XNSFileCommandsImpl:
CEDAR
PROGRAM
IMPORTS Commander, CommanderOps, Convert, IO, PFS, PFSClass, PFSPrefixMap, PFSNames, Rope, UserProfile, XNSRemoteFileDeserialize
~ BEGIN
Copied Types and Constants
ROPE: TYPE ~ Rope.ROPE;
STREAM: TYPE ~ IO.STREAM;
PATH: TYPE ~ PFSNames.PATH;
ChangeFileType: Commander.CommandProc = {
[cmd: Handle] RETURNS [result: REF ← NIL, msg: ROPE ← NIL]
CommandObject = [in, out, err: STREAM, commandLine, command: ROPE, ...]
fileName: ROPE ¬ CommanderOps.NextArgument[cmd];
arg: ROPE;
newFileType: CARD32;
fs: PFSClass.FSHandle;
nameOnFS: PATH;
attributes: PFS.CreateOptions;
openFile: PFS.OpenFile;
IF fileName =
NIL
THEN {
msg ¬ "No fileName supplied\n";
GOTO failed;
};
IF ( arg ¬ CommanderOps.NextArgument[cmd] ) =
NIL
THEN {
msg ¬ "No newFileType supplied\n";
GOTO failed;
};
IF arg.Fetch[0] = 't
THEN {
SELECT
TRUE
FROM
arg.Equal["tUnspecified", FALSE] => newFileType ¬ 0;
arg.Equal["tDirectory", FALSE] => newFileType ¬ 1;
arg.Equal["tText", FALSE] => newFileType ¬ 2;
ENDCASE => {
msg ¬ IO.PutFR1["Unknown filetype (%g) supplied\n", [rope[arg]]];
GOTO failed;
};
}
ELSE {
ok: BOOL ¬ TRUE;
newFileType ¬ Convert.CardFromRope[arg
! Convert.Error => { ok ¬ FALSE; CONTINUE} ];
IF
NOT ok
THEN {
msg ¬ IO.PutFR1["Could not parse (%g) as a cardinal\n", [rope[arg]] ];
GOTO failed;
};
};
msg ¬ ServerAndStuff[fileName].err;
IF msg # NIL THEN GOTO failed;
attributes.fileType ¬ [newFileType];
openFile ¬ PFS.Open[PFS.PathFromRope[fileName]];
PFS.SetAttributes[openFile, attributes];
PFS.Close[openFile];
EXITS
failed=> result ¬ $Failure;
};
PrintFileType: Commander.CommandProc = {
[cmd: Handle] RETURNS [result: REF ← NIL, msg: ROPE ← NIL]
CommandObject = [in, out, err: STREAM, commandLine, command: ROPE, ...]
fileName: ROPE ¬ CommanderOps.NextArgument[cmd];
arg: ROPE;
fileType: CARD32;
fs: PFSClass.FSHandle;
nameOnFS: PATH;
attributes: PFS.CreateOptions;
openFile: PFS.OpenFile;
IF fileName =
NIL
THEN {
msg ¬ "No fileName supplied\n";
GOTO failed;
};
msg ¬ ServerAndStuff[fileName].err;
IF msg # NIL THEN GOTO failed;
openFile ¬ PFS.Open[PFS.PathFromRope[fileName]];
fileType ¬ PFS.GetInfo[openFile].fileType;
IO.PutF1[cmd.out, "type is %g.\n", IO.card[fileType]];
PFS.Close[openFile];
EXITS
failed=> result ¬ $Failure;
};
Deserialize: Commander.CommandProc = {
[cmd: Handle] RETURNS [result: REF ← NIL, msg: ROPE ← NIL]
CommandObject = [in, out, err: STREAM, commandLine, command: ROPE, ...]
desDir, arrow, server, directory, source: ROPE ¬ NIL;
destinationDirectory, fileName, nameOnFS: PATH;
sourceStr: STREAM;
arg: ROPE;
fs: PFSClass.FSHandle;
arg ¬ CommanderOps.NextArgument[cmd];
IF arg = NIL THEN { msg ¬ "No arguments\n"; GOTO Die };
arrow ¬ CommanderOps.NextArgument[cmd];
IF arrow =
NIL
THEN {
desDir ¬ UserProfile.Token["XNSMail.DeserializeDir", NIL];
IF ( desDir =
NIL )
THEN
{
msg ¬ "No destination directory supplied\n";
GOTO Die;
};
source ¬ arg;
}
ELSE {
desDir ¬ arg;
IF
NOT arrow.Equal["←"]
THEN {
msg ¬ IO.PutFR1["Expected a ← but saw a %g\n", [rope[arrow]] ];
GOTO Die;
};
IF ( source ¬ CommanderOps.NextArgument[cmd] ) =
NIL
THEN
{
msg ¬ "No source name after ←\n";
GOTO Die;
};
};
destinationDirectory ¬ PFS.PathFromRope[desDir];
IF
NOT PFSNames.IsADirectory[destinationDirectory]
THEN {
msg ¬ IO.PutFR1["%g is not a directory\n", [rope[desDir]] ];
GOTO Die;
};
fileName ¬ PFSNames.ExpandName[name: PFS.PathFromRope[source],
wDir: PFS.GetWDir[] ];
[fs, nameOnFS, msg] ¬ ServerAndStuff[desDir];
IF msg # NIL THEN GOTO Die;
BEGIN
ENABLE
UNWIND => { sourceStr.Close[] };
sourceStr ¬ PFS.StreamOpen[fileName];
XNSRemoteFileDeserialize.XNSDeserialize[fs, nameOnFS, sourceStr, NIL];
END;
sourceStr.Close[];
cmd.out.PutF["\n%g has been deserialized to %g\n", [rope[PFS.RopeFromPath[fileName]]], [rope[PFS.RopeFromPath[destinationDirectory]]] ];
EXITS
Die => result ¬ $Failure;
};
ServerAndStuff:
PROC[fileName:
ROPE]
RETURNS[ fs: PFSClass.FSHandle, nameOnFS:
PATH, err:
ROPE ¬
NIL ] = {
translation: PFSPrefixMap.PrefixTableEntry ¬ PFSPrefixMap.TranslateAndGetHints[PFS.PathFromRope[fileName]];
IF
NOT translation.translation.IsAbsolute[]
THEN {
err ¬ Rope.Cat["The prefix map produced a relative translation for ", fileName, " and an absolute translation is required."];
RETURN
};
fs ¬ PFSClass.GetFS[translation.fsName
! PFSClass.Error => { err ¬ msg; CONTINUE } ];
IF fs #
NIL
THEN
IF fs.flavor # $XNS
THEN
err ¬ IO.PutFR1["Wrong flavor of file (%g) - not XNS\n", [atom[fs.flavor]] ];
nameOnFS ¬ translation.nameOnFS;
};
cftDoc: ROPE ~ "fileName { tUnspecified tDirectory tText } | fileName newType(as number)";
deserializeDoc: ROPE ~ "directory ← serialized-fileName | serialized-fileName (uses profile option XNSMail.DeserializeDir as the destination directory)";
pftDoc
: ROPE ~ "fileName";
Init:
PROC = {
Commander.Register["ChangeFileType", ChangeFileType, cftDoc];
Commander.Register["Deserialize", Deserialize, deserializeDoc];
Commander.Register["PrintFileType", PrintFileType, pftDoc];
};
Init[];
END. ..