PeanutToWalnutLogImpl.Mesa
Rick Beach, August 24, 1984 4:54:30 pm PDT
DIRECTORY
Commander, EditSpan, FS, IO, PutGet, Rope, TextNode, WalnutExtras, WalnutExtrasImpl, WalnutLog, WalnutWindow, WalnutWindowImpl;
PeanutToWalnutLogImpl: CEDAR MONITOR
IMPORTS Commander, EditSpan, FS, IO, PutGet, Rope, TextNode, WalnutExtras, WalnutExtrasImpl, WalnutLog, WalnutWindow, WalnutWindowImpl
SHARES WalnutWindowImpl, WalnutExtrasImpl
= {
ROPE: TYPE = Rope.ROPE;
PeanutToWalnutLog: ENTRY Commander.CommandProc ~ {
ENABLE UNWIND => NULL;
h: IO.STREAM;
mfp: LIST OF ROPE;
IF WalnutWindowImpl.walnut = NIL THEN {
WalnutWindowImpl.NoWalnutReport[cmd, "load Peanut mail files into Walnut database\n"];
RETURN};
h← IO.RIS[cmd.commandLine];
WHILE NOT h.EndOf DO
fName: ROPENIL;
fName ← h.GetTokenRope[IO.IDProc ! IO.EndOfStream => CONTINUE].token;
IF fName.IsEmpty[] THEN EXIT;
IF fName.Find["*"] = -1 THEN
mfp ← CONS[fName, mfp]
ELSE {
CatProc: FS.NameProc = {
mfp ← CONS[fullFName, mfp];
RETURN [TRUE]
};
IF fName.Find["!"] = -1 THEN fName ← fName.Concat["!H"];
FS.EnumerateForNames[pattern: fName, proc: CatProc];
};
ENDLOOP;
IF mfp = NIL THEN { WalnutWindow.Report["No input files specified"]; RETURN };
WalnutExtras.DoWaitCall[PeanutReadMailProc, mfp];
};
PeanutReadMailProc: INTERNAL PROC [ra: REF ANY] ~ {
wqe: WalnutExtrasImpl.WalnutQueueEntry;
{ ENABLE UNWIND => { NOTIFY wqe.condition};
mfp: LIST OF ROPE;
wqe← NARROW[ra];
mfp← NARROW[wqe.params];
DoPeanutReadMailFile[mfp];
NOTIFY wqe.condition;
};
};
DoPeanutReadMailFile: PROC [mfp: LIST OF ROPE] ~ {
startPos, numNew: INT;
IF WalnutWindowImpl.walnut = NIL THEN
startPos← WalnutLog.InitializeLog[WalnutWindow.walnutLogName] -- set up WalnutDB & log
ELSE {
WalnutExtras.InternalChangeMenu[WalnutWindow.workingMenu];
WalnutLog.MarkWalnutTransaction[];  -- get in a good state
startPos← WalnutLog.LogLength[TRUE];
};
WHILE mfp # NIL DO
fName: ROPE ← mfp.first;
fullFName: ROPE;
cp: FS.ComponentPositions;
msName: ROPE;
defaultPrefix: ROPE;
peanutFile: TextNode.Ref;
[fullFName, cp] ← FS.ExpandName[fName];
msName ← fullFName.Substr[cp.base.start, cp.base.length];
defaultPrefix ← Rope.Cat["Categories: ", msName, "\n"];
peanutFile ← PutGet.FromFile[fileName: fName];
WalnutWindow.Report["\nReading Peanut messages from ", fName, "... "];
IF ~ReadMessages[peanutFile, defaultPrefix] THEN {
WalnutWindow.Report["There were problems reading ", fName];
WalnutWindow.Report["Bug Confirm to keep messages successfully read, Deny to throw them away"];
IF ~WalnutWindow.UserConfirmed[] THEN {
WalnutLog.ResetLogToExpectedLength[]; RETURN};
};
numNew ← WalnutExtrasImpl.GetMessagesFromLog[startPos];
IF numNew = 0 THEN
WalnutWindow.Report[" No messages in ", fName, " were new"]
ELSE WalnutWindow.Report[IO.PutFR[
"%g new messages from %g were added to database", IO.int[numNew], IO.rope[fName]]];
startPos ← WalnutLog.LogLength[doFlush: TRUE];
WalnutLog.MarkWalnutTransaction[];
mfp ← mfp.rest;
ENDLOOP;
WalnutWindow.Report["\nFinished all Peanut files supplied.\n"];
IF WalnutWindowImpl.walnut # NIL THEN
WalnutExtras.InternalChangeMenu[WalnutWindow.walnutMenu];
};
ReadMessages: PROC [root: TextNode.Ref, prefix: ROPE] RETURNS [BOOLEAN] ~ {
node: TextNode.Ref ← TextNode.FirstChild[root];
WalnutWindow.Report[Rope.Cat["Skipping first node: ", TextNode.NodeRope[TextNode.NarrowToTextNode[node]]]];
FOR node ← TextNode.NthSibling[node, 1], TextNode.NthSibling[node, 1] WHILE node # NIL DO
msgText: ROPE ← RopeFromNodes[node];
IF NOT msgText.IsEmpty[] THEN {
IF msgText.Fetch[0] = '\n THEN msgText ← msgText.Substr[1];
WalnutLog.AddMessageToLog[entryText: msgText, prefix: prefix];
WalnutWindow.ReportRope["."];
};
ENDLOOP;
RETURN [TRUE];
};
RopeFromNodes: PROC [node: TextNode.Ref] RETURNS [rope: ROPE] ~ {
root: TextNode.Ref ← TextNode.NewTextNode[];
make a rope of the subtree spanned by this node
[] ← EditSpan.Copy[
destRoot: root,
sourceRoot: TextNode.Root[node],
dest: TextNode.MakeNodeLoc[root],
source: TextNode.MakeNodeSpan[TextNode.FirstChild[node], TextNode.LastChild[node]],
nesting: 1];
rope ← PutGet.ToRope[root].output;
};
Commander.Register["PeanutToWalnutLog",
PeanutToWalnutLog, "Reads Peanut mail files into Walnut mail database"];
}.