MicroInitImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Willie-sue, February 25, 1986 12:38:09 pm PST
Dave Rumph, November 20, 1986 3:26:51 pm PST
taken from MicInit.bcpl
DIRECTORY
BasicTime USING [GMT, Now],
CommandTool USING [ArgumentVector],
FS USING [ComponentPositions, Error, nullOpenFile, OpenFile,
  Close, ExpandName, Open, StreamOpen],
IO,
Rope,
ViewerClasses USING [Viewer],
ViewerIO USING [CreateViewerStreams],
ViewerOps USING [FindViewer, OpenIcon],
MicroDefs,
MicroGlobalVars,
MicroOps,
MicroUtils USING [];
MicroInitImpl: CEDAR PROGRAM
IMPORTS
BasicTime, FS, IO, Rope, ViewerIO, ViewerOps,
MicroDefs, MicroGlobalVars, MicroOps
EXPORTS
MicroOps, MicroUtils
= BEGIN OPEN MicroDefs, MicroGlobalVars;
savedWDir: ROPENIL;
InitMicroVars: PUBLIC PROC[cmdLine, wDir: ROPE, argv: CommandTool.ArgumentVector]
  RETURNS[srcFileList: SrcFile, ok: BOOL, start: BasicTime.GMT] = {
fixFile, mbFile, rsFile, lsFile, erFile, stFile, lastFileName: ROPE;
xListF, ltoF, rsF, ucF: BOOLFALSE;
binF, stF: BOOLTRUE;
i: NAT ← 1;
lastSrcFile: SrcFile ← NIL;
ok ← FALSE;
srcFileList ← NIL;
errorFileRec ← fixupsFileRec ← listingFileRec ← symbolsFileRec ← NIL;
savedWDir ← wDir;
reportStrm ← TSStream["Micro Assembler"];
StartupMessage[reportStrm, cmdLine, start ← BasicTime.Now[]];
IF argv.argc = 1 THEN {
SIGNAL MicroDefs.Failure["Command line is empty - quitting\n"];
reportStrm.PutRope["Command line is empty - quitting\n"];
RETURN
};
DO  -- global switches
arg: ROPE ← argv[i];
IF arg.Fetch[0] # '- THEN EXIT;
FOR j: INT IN [1..arg.Length[]) DO
SELECT arg.Fetch[j] FROM
'L, 'l => xListF ← TRUE;
'N, 'n => binF ← FALSE;
'O, 'o => stF ← FALSE;
'U, 'u => ucF ← TRUE;
ENDCASE => NULL;
ENDLOOP;
i ← i + 1;
ENDLOOP;
now for file names and perhaps following local switches
start at the top of this loop with a file name
DO
src: BOOLTRUE;
ucF: BOOLFALSE;
IF i >= argv.argc THEN EXIT;  -- finished
lastFileName ← argv[i];
i ← i + 1;
DO
arg: ROPE;
IF i >= argv.argc THEN EXIT;  -- finished
arg ← argv[i];
IF arg.Fetch[0] # '- THEN EXIT;
look at switches
SELECT arg.Fetch[1] FROM  -- is switch
'B, 'b => { binF ← TRUE; mbFile ← lastFileName; src ← FALSE };
'L, 'l => { xListF ← TRUE; lsFile ← lastFileName; src ← FALSE };
'E, 'e => { erFile ← lastFileName; src ← FALSE };
'S, 's => { stF ← TRUE; stFile ← lastFileName; src ← FALSE };
'R, 'r => { rsF ← TRUE; rsFile ← lastFileName; src ← FALSE };
'U, 'u => ucF ← TRUE;
ENDCASE => NULL;
i ← i + 1;
ENDLOOP;
IF src THEN {
srcFile: SrcFile ← NEW[SrcFileRec ← [fullName: lastFileName, upperCaseFlag: ucF]];
IF srcFileList = NIL THEN srcFileList ← lastSrcFile ← srcFile
ELSE { lastSrcFile.next ← srcFile; lastSrcFile ← lastSrcFile.next };
};
ENDLOOP;
IF srcFileList = NIL THEN {
SIGNAL MicroDefs.Failure["No source files - quitting\n"];
reportStrm.PutRope["No source files - quitting\n"];
RETURN
};
IF rsFile # NIL THEN {  -- check if the restore symbols file exists
of: FS.OpenFile ← FS.nullOpenFile;
rsFile ← FullNameWithExt[rsFile, NIL, wDir, "st"];
of ← FS.Open[rsFile ! FS.Error => CONTINUE];
IF of = FS.nullOpenFile THEN {
SIGNAL MicroDefs.Failure[IO.PutFR["Could not open %g - quitting\n", IO.rope[rsFile]]];
reportStrm.PutF["Could not open %g - quitting\n", IO.rope[rsFile] ];
RETURN;
};
FS.Close[of ! FS.Error => CONTINUE];
restoreSymbolsFile ← rsFile;
}
ELSE restoreSymbolsFile ← NIL;
mbFile ← FullNameWithExt[mbFile, lastFileName, wDir, ""];
erFile ← FullNameWithExt[erFile, mbFile, wDir, "er"];
IF xListF THEN lsFile ← FullNameWithExt[lsFile, mbFile, wDir, "ls"];
stFile ← FullNameWithExt[stFile, mbFile, wDir, "st"];
fixFile ← FullNameWithExt["Micro.fixups", NIL, wDir, ""];
FOR sfl: SrcFile ← srcFileList, sfl.next UNTIL sfl = NIL DO
sfl.fullName ← FullNameWithExt[sfl.fullName, NIL, wDir, "mc"]
ENDLOOP;
errorFileRec ← NEW[OutputFileRec ← [fullName: erFile] ];
errorFileRec.strm ←
FS.StreamOpen[fileName: erFile, accessOptions: $create, keep: 2];
StartupMessage[errorFileRec.strm, cmdLine, start];
fixupsFileRec ← NEW[OutputFileRec ← [fullName: fixFile] ];
IF lsFile # NIL THEN {
listingFileRec ← NEW[OutputFileRec ← [fullName: lsFile] ];
listingFileRec.strm ←
FS.StreamOpen[fileName: lsFile, accessOptions: $create, keep: 2];
};
IF stF THEN {
symbolsFileRec ← NEW[OutputFileRec ← [fullName: stFile] ];
symbolsFileRec.strm ←
FS.StreamOpen[fileName: stFile, accessOptions: $create, keep: 2];
};
binaryFile ← NEW[OutputFileRec ← [fullName: mbFile] ];
expandedListingFlag ← xListF;
writeBinaryFlag ← binF;
convertToUpperFlag ← ucF;
errorCount ← 0;
ok ← TRUE;
};
InPushFile: PUBLIC PROC[fileName: ROPE] RETURNS[fullName: ROPE, fileOK: BOOL] = {
srcFile: SrcFile;
fullName ← FS.ExpandName[fileName, savedWDir].fullFName;
srcFile ← NEW[SrcFileRec ← [fullName: fullName, upperCaseFlag: convertToUpperFlag]];
fileOK ← MicroOps.InPush[srcFile];
};
TSStream: PROC[name: ROPE] RETURNS [out: STREAM] = {
v: ViewerClasses.Viewer ← ViewerOps.FindViewer[name];
out ← ViewerIO.CreateViewerStreams[name, v].out;
IF v#NIL THEN IF v.iconic THEN ViewerOps.OpenIcon[v];
};
StartupMessage: PROC[out: STREAM, cmdLine: ROPE, start: BasicTime.GMT] = {
out.PutRope["\n\n**********************************************************\n"];
out.PutF["\t\t Micro Assembler, started @ %g\n", IO.time[start]];
out.PutRope["Command is: "];
out.PutRope[cmdLine]; out.PutChar['\n];
};
FullNameWithExt: PUBLIC PROC[preferred, other, wDir, ext: ROPE]
RETURNS[fullName: ROPE] = {
IF preferred = NIL THEN use other as basis for name;
IF other used, then strip any existing extension and append ext
forceExt: BOOL ← preferred = NIL;
basis: ROPEIF preferred # NIL THEN preferred ELSE other;
cp: FS.ComponentPositions;
IF basis = NIL THEN RETURN;
[fullName, cp, ] ← FS.ExpandName[basis, wDir];
IF cp.ext.length # 0 THEN
IF ~forceExt THEN RETURN
ELSE fullName ← Rope.Substr[fullName, 0, cp.ext.start-1];
RETURN[IF ext.Length[] = 0 THEN fullName ELSE fullName.Cat[".", ext] ]
};
END.