-- ListInterface.mesa
-- modified by Bruce, 13-Jan-81 11:04:10
-- modified by Satterthwaite, September 20, 1982 1:39 pm

DIRECTORY
  Ascii USING [CR],
  CommanderOps USING [AddCommand, CommandBlockHandle],
  FileSegment: TYPE USING [Pages, nullPages],
  ListerDefs USING [
    IncorrectVersion, Load, MultipleModules, NoCode, NoFGT, NoFile,
    NoSymbols, PrintSei, SetRoutineSymbols, WriteFileID, WriteString],
  OutputDefs USING [
    CloseOutput, NumberFormat, OpenOutput, PutChar, PutNumber, PutString],
  Strings USING [AppendString],
  Symbols USING [ISEIndex, ISENull, TransferMode],
  SymbolTable USING [Acquire, Base, Release];

ListInterface: PROGRAM
  IMPORTS
    CommanderOps, ListerDefs, OutputDefs, Strings, SymbolTable = {
  OPEN ListerDefs, OutputDefs, Symbols;
  
  symbols: SymbolTable.Base;
  
  PrintInterface: PROC = {
    OPEN symbols;
    sei: ISEIndex;
    FOR sei ← FirstCtxSe[stHandle.outerCtx], NextSe[sei] UNTIL sei = ISENull DO
      SELECT LinkMode[sei] FROM
	val => {
	  PutValue[seb[sei].idValue, sei];
	  PutModeName[XferMode[seb[sei].idType]];
	  PutChar[Ascii.CR]};
	ref => {
	  PutValue[seb[sei].idValue, sei];
	  PutString["EXPORTED Variable"L];
	  PutChar[Ascii.CR]};
	manifest => NULL; -- constant
	
	ENDCASE;
      ENDLOOP};
    
  PutValue: PROC [u: UNSPECIFIED, sei: ISEIndex] = {
    number: NumberFormat = NumberFormat[10, FALSE, FALSE, 4];
    PutNumber[u, number];
    PutString[" -- "L];
    PrintSei[sei];
    PutString[": "L]};
    
  ModePrintName: ARRAY TransferMode OF STRING = [
    "PROC"L, "PORT"L, "SIGNAL"L, "ERROR"L, "PROCESS"L, "PROGRAM"L, "NONE"L];
  
  PutModeName: PROC [n: TransferMode] = {PutString[ModePrintName[n]]};
    
  Interface: PROC [root: STRING] = {
    bcdFile: STRING ← [100];
    sseg, cseg: FileSegment.Pages;
    Strings.AppendString[bcdFile, root];
    FOR i: CARDINAL IN [0..bcdFile.length) DO
      IF bcdFile[i] = '. THEN EXIT;
      REPEAT FINISHED => Strings.AppendString[bcdFile, ".bcd"L];
      ENDLOOP;
    BEGIN
    [code: cseg, symbols: sseg] ← Load[bcdFile
      ! NoFGT => {RESUME};
        NoCode => {RESUME};
        NoSymbols, IncorrectVersion, MultipleModules => {GOTO badformat};
        NoFile => {GOTO badname}];
    IF cseg # FileSegment.nullPages THEN GOTO badformat;
    symbols ← SymbolTable.Acquire[sseg];
    ListerDefs.SetRoutineSymbols[symbols];
    OpenOutput[root, ".il"];
    WriteFileID[];
    PrintInterface[];
    SymbolTable.Release[symbols];
    CloseOutput[];
    EXITS
      badformat => ListerDefs.WriteString["Bad Format!"];
      badname => ListerDefs.WriteString["File Not Found!"];
    END};
    
  command: CommanderOps.CommandBlockHandle;
  
  command ← CommanderOps.AddCommand["Interface", LOOPHOLE[Interface], 1];
  command.params[0] ← [type: string, prompt: "Filename"];
  
  }.