LoaderDriver.mesa: Loader driver for running programs on top of the basic boot file
Copyright © 1984, 1985 by Xerox Corporation. All rights reserved.
Andrew Birrell September 6, 1983 1:19 pm
Levin, September 22, 1983 2:04 pm
Paul Rovner, August 15, 1983 12:09 pm
Russ Atkinson, February 4, 1985 8:28:59 pm PST
Doug Wyatt, April 21, 1985 3:09:16 pm PST
DIRECTORY
Ascii USING [BS, ControlA, ControlQ, ControlW, ControlX, CR, DEL, Digit, Letter, LF, SP, TAB],
DebuggerSwap USING [CallDebugger],
DefaultRemoteNames USING [DefaultNames, Get],
FS USING [Error, ErrorDesc, Open, OpenFile, StreamOpen],
GermSwap USING [switches ],
IO USING[ BreakProc, Close, EndOfStream, EraseChar, GetChar, GetTokenRope, int, Put, PutChar, PutF, PutFR, PutRope, STREAM ],
Loader USING [Error, ErrorType, Instantiate, IRItem, Start],
PrincOps USING [ControlModule, NullControl],
Rope USING [Cat, Concat, Equal, Fetch, Find, FromChar, Index, Length, ROPE, Substr ],
SimpleTerminal USING [TurnOff, TurnOn],
SystemVersion USING [release],
Terminal USING [Select],
UserCredentials USING [Get];
LoaderDriver:
CEDAR
PROGRAM
IMPORTS Ascii, DebuggerSwap, DefaultRemoteNames, FS, GermSwap, IO, Loader, Rope, SimpleTerminal, SystemVersion, UserCredentials
= BEGIN
ROPE: TYPE = Rope.ROPE;
in: IO.STREAM;
out: IO.STREAM;
GetFileStr:
PROC[cmName:
ROPE]
RETURNS[
IO.
STREAM] = {
RETURN[ FS.StreamOpen[cmName] ];
};
MyBreak:
IO.BreakProc = {
SELECT char
FROM
Ascii.SP, Ascii.CR, Ascii.TAB, Ascii.LF, ', => RETURN[sepr];
'/ => RETURN[break];
ENDCASE => RETURN[other];
};
GetToken:
PROC [stream:
IO.
STREAM]
RETURNS[token:
ROPE ←
NIL] = {
token ← IO.GetTokenRope[stream, MyBreak !IO.EndOfStream => CONTINUE].token;
};
PutFSError:
PROC[error:
FS.ErrorDesc] = {
IF error.code = $unknownFile
THEN out.PutRope["not found"]
ELSE {
out.PutRope["FS.Error: "];
out.PutRope[IF error.explanation.Length[] # 0 THEN error.explanation ELSE "no message"];
};
};
PutLoaderError:
PROC[type: Loader.ErrorType, message:
ROPE] = {
out.PutRope["Loader.Error["];
out.PutRope[
SELECT type
FROM
invalidBcd => "invalid BCD",
fileNotFound => "file not found (?)",
versionMismatch => "version mismatch",
loadStateFull => "loadstate full",
insufficientVM => "insufficient VM",
ENDCASE => "unknown"];
IF message.Length[] # 0 THEN { out.PutChar[',]; out.PutRope[message] };
out.PutChar[']];
};
Loadee:
TYPE =
RECORD[
rest: REF Loadee ← NIL,
name: ROPE ← NIL,
cm: PrincOps.ControlModule ← PrincOps.NullControl,
codeLinks: BOOL ← TRUE,
callDebugger: BOOL ← FALSE,
selectOtherTerminal: BOOL ← FALSE];
TryFile:
PROC[cmName:
ROPE]
RETURNS[done:
BOOL ←
TRUE] = {
fileStr: IO.STREAM;
loadees: REF Loadee ← NIL;
last: REF Loadee ← NIL;
loadFailed: BOOL ← FALSE;
nextToken: ROPE ← NIL;
out.PutF["\nTrying \"%g\" ... ", [rope[cmName]]];
fileStr ← GetFileStr[cmName ! FS.Error => { PutFSError[error]; GOTO failed }];
out.PutRope["ok\nParsing the command file ... "];
DO
-- parse input for each loadee
this: REF Loadee ← NEW[Loadee ← [] ];
end: BOOL ← FALSE;
swSep: ROPE = "/";
IF nextToken = NIL THEN IF (nextToken ← GetToken[fileStr]) = NIL THEN EXIT;
IF nextToken.Equal[swSep]
THEN end ← TRUE
ELSE {
IF last = NIL THEN loadees ← this ELSE last.rest ← this;
last ← this;
this.name ← nextToken;
IF this.name.Find["."] < 0 THEN this.name ← this.name.Cat[".bcd"];
nextToken ← GetToken[fileStr];
};
IF nextToken.Equal[swSep]
THEN {
positive: BOOL ← TRUE;
nextToken ← GetToken[fileStr];
FOR i:
NAT
IN
NAT[0..nextToken.Length[])
DO
SELECT nextToken.Fetch[i]
FROM
'-, '~ => { positive ← FALSE; LOOP };
'l, 'L => this.codeLinks ← positive;
'd, 'D => this.callDebugger ← positive;
't, 'T => this.selectOtherTerminal ← positive;
'e, 'E => end ← positive;
ENDCASE => NULL;
positive ← TRUE
ENDLOOP;
nextToken ← NIL;
};
IF end THEN EXIT;
ENDLOOP;
out.PutRope["ok"];
IO.Close[fileStr];
FOR this:
REF Loadee ← loadees, this.rest
UNTIL this =
NIL
DO
load them
BEGIN
thisFile: FS.OpenFile;
unbound: LIST OF Loader.IRItem;
out.PutF["\nLoading \"%g\"", [rope[this.name]] ];
IF NOT this.codeLinks THEN out.PutRope[" (frame links)"];
DO
out.PutRope[" ... "];
thisFile ←
FS.Open[this.name
!
FS.Error => {
PutFSError[error];
IF Confirm["\nShould this file be tried again? "] THEN LOOP;
GOTO thisFailed;
} ];
EXIT;
ENDLOOP;
TRUSTED{[cm: this.cm, unboundImports: unbound] ←
Loader.Instantiate[file: thisFile, codeLinks: this.codeLinks !
FS.Error => { PutFSError[error]; GOTO thisFailed };
Loader.Error => { PutLoaderError[type, message]; GOTO thisFailed } ]};
out.PutRope["ok"];
IF unbound #
NIL
THEN {
out.PutRope[" (has unbound imports: "];
FOR l:
LIST
OF Loader.IRItem ← unbound, l.rest
UNTIL l =
NIL
DO
out.PutChar['[];
out.PutRope[l.first.interfaceName];
out.PutChar[',];
out.Put[IO.int[l.first.index]];
out.PutRope["] "];
ENDLOOP;
out.PutRope[")"];
};
EXITS thisFailed => loadFailed ← TRUE;
END;
ENDLOOP;
TRUSTED {IF GermSwap.switches[five] THEN DebuggerSwap.CallDebugger["Key stop 5"L]};
IF loadFailed
THEN out.PutRope["\nThere were loading errors. I'm giving up."]
ELSE {
FOR this:
REF Loadee ← loadees, this.rest
UNTIL this =
NIL
DO
start them
IF this.selectOtherTerminal THEN [] ← Terminal.Select[NIL];
IF this.callDebugger
THEN TRUSTED{DebuggerSwap.CallDebugger["/d switch in basic loadees"]};
out.PutF["\nStarting \"%g\" ... ", [rope[this.name]] ];
TRUSTED{Loader.Start[this.cm]};
out.PutRope["ok"];
ENDLOOP;
out.PutRope["\nEnd of the command file"];
SimpleTerminal.TurnOff[];
};
EXITS failed => done ← FALSE;
};
OptionalCommand:
PROC
[condition, prompt, default: ROPE, work: PROC[ROPE] RETURNS[BOOL]] RETURNS[BOOL] = {
"GetID" copied from UserCredentialsImpl
Rubout: ERROR = CODE;
GetID:
PROC [default:
ROPE, echo:
BOOL ←
TRUE]
RETURNS [id:
ROPE] = {
OPEN Ascii;
firstTime: BOOL ← TRUE;
c: CHAR;
EraseAll:
PROC = {
IF echo
THEN
FOR i: INT DECREASING IN [0..id.Length[]) DO out.EraseChar[id.Fetch[i]]; ENDLOOP;
id ← NIL;
};
Done:
PROC [c:
CHAR]
RETURNS [
BOOL] =
INLINE {
IF firstTime
THEN {
SELECT c
FROM
ControlA, BS, ControlQ, ControlW, ControlX, CR, SP, DEL => NULL;
ENDCASE => EraseAll[];
firstTime ← FALSE;
};
RETURN[c = SP OR c = CR]
};
id ← default;
IF echo THEN out.PutRope[default];
c ← in.GetChar[];
UNTIL Done[c]
DO
SELECT c
FROM
DEL => ERROR Rubout;
ControlA,
BS => {
len: INT ← id.Length[];
IF len > 0
THEN {
len ← len - 1;
IF echo THEN out.EraseChar[id.Fetch[len]];
id ← id.Substr[len: len];
};
};
ControlW, ControlQ => {
text to be backed up is of the form ...<non-alpha><alpha><non-alpha>, the <alpha> and following <non-alpha> are to be removed.
alpha: BOOL ← FALSE;
FOR i:
INT
DECREASING
IN [0..id.Length[])
DO
ch: CHAR = id.Fetch[i];
IF Ascii.Letter[ch] OR Ascii.Digit[ch] THEN alpha ← TRUE
ELSE IF alpha THEN {id ← id.Substr[len: i + 1]; EXIT};
IF echo THEN out.EraseChar[ch];
REPEAT
FINISHED => id ← NIL;
ENDLOOP;
};
ControlX => EraseAll[];
ENDCASE => {id ← id.Concat[Rope.FromChar[c]]; IF echo THEN out.PutChar[c]};
c ← in.GetChar[];
ENDLOOP;
};
arg: ROPE ← default;
DO out.PutRope[condition];
SELECT in.GetChar[]
FROM
'y, 'Y, '\n =>
BEGIN
out.PutRope[" yes"];
DO
BEGIN
ENABLE Rubout => { out.PutRope[" XXX"]; EXIT };
out.PutChar['\n]; out.PutRope[prompt]; out.PutRope[": "];
arg ← GetID[arg];
IF work[arg] THEN RETURN[TRUE];
END
ENDLOOP;
END;
'n, 'N => { out.PutRope[" no"]; RETURN[FALSE] };
ENDCASE => out.PutRope["\nPlease respond with \"Y\" or \"N\""];
ENDLOOP;
};
Confirm:
PROC [question:
ROPE]
RETURNS [
BOOL] = {
DO
out.PutRope[question];
SELECT in.GetChar[]
FROM
'y, 'Y, '\n => {
out.PutRope[" yes"];
RETURN[TRUE];
};
'n, 'N => {
out.PutRope[" no"];
RETURN[FALSE];
};
ENDCASE => out.PutRope["\nPlease respond with \"Y\" or \"N\""];
ENDLOOP;
};
Main:
PROC = {
remoteNames: DefaultRemoteNames.DefaultNames = DefaultRemoteNames.Get[];
version:
ROPE =
IO.PutFR["%g.%g",
[integer[SystemVersion.release.major]],
[integer[SystemVersion.release.minor]] ];
loadees: ROPE = "Basic.Loadees";
user, briefUser, userDir: ROPE ← NIL;
[in: in, out: out] ← SimpleTerminal.TurnOn[];
out.PutRope["\nThis is the Basic boot file.\n"];
user ← UserCredentials.Get[].name;
briefUser ← user.Substr[0, user.Index[0, "."]];
userDir ← IO.PutFR["%g<%g>%g>", [rope[remoteNames.userHost]], [rope[briefUser]], [rope[version]] ];
IF GermSwap.switches[l]
AND OptionalCommand[
"\nDo you want to specify an explicit \"Loadees\" command file? ",
"Command file name",
Rope.Concat[userDir, "UtilityLoadees.cm"],
TryFile]
THEN NULL
ELSE {
out.PutRope["\nLooking for a \"BasicLoadees\" command file."];
IF TryFile[Rope.Concat[userDir, loadees]]
OR TryFile[Rope.Cat[remoteNames.current, "Top>", loadees]]
THEN NULL
ELSE out.PutRope["\nCan't find any command file."];
};
};
Main[];
END.