home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / oberon / loader / verify.mod < prev   
Text File  |  1977-12-31  |  7KB  |  287 lines

  1. MODULE verify; (* cn 24-Sep-94 *)
  2. (*
  3.  verify takes alist of sym and obj files, and looks, if they
  4.  all have the same keys for each other.
  5. *)
  6.  
  7. FROM SYSTEM IMPORT ADR;
  8.  
  9. IMPORT
  10.  Arguments,Arts,Break,SeqIO,s:String,T:Terminal;
  11.  
  12. CONST
  13.  maxNames=200;
  14.  nameLen=24;
  15.  pathLen=128;
  16.  
  17. (*
  18.  bufferSize for source file.
  19. *)
  20.  bufferSize=04000H;
  21.  
  22. TYPE
  23.  CARDINAL=INTEGER;
  24.  LONGCARD=LONGINT;
  25.  Name=ARRAY [0..nameLen-1] OF CHAR;
  26.  Path=ARRAY [0..pathLen-1] OF CHAR;
  27.  File=RECORD
  28.   path:Path;
  29.   key:LONGCARD;
  30.  END;
  31.  
  32. VAR
  33.  inFile:SeqIO.SeqKey;
  34.  outFile:SeqIO.SeqKey;
  35.  files:ARRAY [1..maxNames] OF File;
  36.  numOfFiles:INTEGER;
  37.  
  38. (*
  39.  Some procedure for easier reading from input file
  40. *)
  41.  
  42. PROCEDURE Read():CHAR; BEGIN RETURN SeqIO.SeqInB(inFile); END Read;
  43. (* Read one byte from input file. *)
  44.  
  45. PROCEDURE ReadShort():INTEGER; BEGIN RETURN SeqIO.SeqInW(inFile); END ReadShort;
  46. (* Read two bytes from input file. *)
  47.  
  48. PROCEDURE ReadLong():LONGINT; BEGIN RETURN SeqIO.SeqInL(inFile); END ReadLong;
  49. (* Read four bytes from input file. *)
  50.  
  51. PROCEDURE ReadName(VAR name: ARRAY OF CHAR);
  52. (* Read a modulename of 24 bytes from input files. *)
  53. BEGIN SeqIO.SeqInCount(inFile,ADR(name),nameLen); END ReadName;
  54.  
  55. PROCEDURE ReadLine(VAR name: ARRAY OF CHAR);
  56. (* Read a filename from input files. *)
  57. VAR
  58.  ch:CHAR;
  59.  i:INTEGER;
  60. BEGIN
  61.  REPEAT SeqIO.SeqGetB(inFile,ch) UNTIL (ch>" ") OR ~SeqIO.SeqOk(inFile);
  62.  WHILE (ch>" ") & SeqIO.SeqOk(inFile) DO
  63.   name[i]:=ch;
  64.   INC(i);
  65.   SeqIO.SeqGetB(inFile,ch);
  66.  END;
  67.  name[i]:=0C;
  68. END ReadLine;
  69.  
  70. PROCEDURE ReadString(VAR name: ARRAY OF CHAR);
  71. (* Read a zero terminated string from input file. *)
  72. VAR i:INTEGER;
  73. BEGIN
  74.  i:=-1; REPEAT INC(i); SeqIO.SeqGetB(inFile,name[i]); UNTIL name[i]=0C;
  75. END ReadString;
  76.  
  77. VAR
  78.  dummy:ARRAY [0..999] OF CHAR;
  79.  
  80. PROCEDURE SkipBlock(size:INTEGER);
  81. (* Skip a block of given length from input file. *)
  82. BEGIN
  83.  Arts.Assert(size<SIZE(dummy),ADR("Illegal size in SkipBlock"));
  84.  SeqIO.SeqInCount(inFile,ADR(dummy),size);
  85. END SkipBlock;
  86.  
  87. PROCEDURE Check(byte:CHAR):BOOLEAN;
  88. (* Check if current byte in objfile=byte; res:=done | invalidObjFile *)
  89. BEGIN RETURN byte=Read(); END Check;
  90.  
  91. PROCEDURE WriteLine(path:Path);
  92. VAR
  93.  i:INTEGER;
  94. BEGIN
  95.  i:=0;
  96.  WHILE path[i]#0C DO
  97.   SeqIO.SeqOutB(outFile,path[i]);
  98.   INC(i);
  99.  END;
  100.  SeqIO.SeqOutB(outFile,CHAR(10));
  101. END WriteLine;
  102.  
  103. (*-------*)
  104.  
  105. PROCEDURE VerifyObjKey(file:File);
  106. VAR
  107.  hasErrors:BOOLEAN;
  108.  i,j:INTEGER;
  109.  key:LONGINT;
  110.  len:INTEGER;
  111.  name:Name;
  112.  nofCommands:INTEGER;
  113.  nofEntries:INTEGER;
  114.  nofImports:INTEGER;
  115.  nofPointers:INTEGER;
  116.  pos:INTEGER;
  117. BEGIN
  118.  SkipBlock(9);
  119.  nofEntries:=ReadShort();
  120.  nofCommands:=ReadShort();
  121.  nofPointers:=ReadShort();
  122.  nofImports:=ReadShort();
  123.  SkipBlock(42);
  124.  Arts.Assert(Check(CHAR(82H)),ADR("No entries?"));       (* Entries *)
  125.  SkipBlock(4*nofEntries);
  126.  Arts.Assert(Check(CHAR(83H)),ADR("No commands ?"));       (* Commands *)
  127.  FOR i:=1 TO nofCommands DO
  128.   ReadString(name);
  129.   SkipBlock(4);
  130.  END;
  131.  Arts.Assert(Check(CHAR(84H)),ADR("No pointers ?"));       (* Pointers *)
  132.  SkipBlock(4*nofPointers);
  133.  Arts.Assert(Check(CHAR(85H)),ADR("No imports ?"));       (* Imports *)
  134.  FOR i:=1 TO nofImports DO
  135.   key:=ReadLong();
  136.   ReadString(name);
  137.   len:=s.Length(name);
  138.   name[len]:="."; name[len+1]:=0C;
  139.   hasErrors:=FALSE;
  140.   FOR j:=1 TO numOfFiles DO
  141.    pos:=s.Occurs(files[j].path,s.first,name,TRUE);
  142.    IF (pos#s.last)
  143.       & ((pos=0) OR (files[j].path[pos-1]="/") OR (files[j].path[pos-1]=":")) THEN
  144.     IF (key#files[j].key) & (files[j].key#0) THEN
  145.      hasErrors:=TRUE;
  146.      T.WriteString(file.path);
  147.      T.FormatS(" imports %s",files[j].path);
  148.      T.FormatNr("[key=%08lx]",files[j].key);
  149.      T.FormatNr(" with key %08lx.\n",key);
  150.     END;
  151.    END;
  152.   END;
  153.   IF hasErrors THEN
  154.    WriteLine(file.path);
  155.   END;
  156.  END;
  157. END VerifyObjKey;
  158.  
  159. PROCEDURE VerifySymKey(file:File);
  160. BEGIN
  161.  (* not yet implemented *)
  162. END VerifySymKey;
  163.  
  164. PROCEDURE GetObjKey():LONGINT;
  165. VAR
  166.  key:LONGINT;
  167. BEGIN
  168.  IF Check("6") THEN (* Verify it's an object file version "6" *)
  169.   SkipBlock(30);
  170.   key:=ReadLong();
  171.  ELSE
  172.   key:=0; HALT;
  173.  END;
  174.  RETURN key;
  175. END GetObjKey;
  176.  
  177. PROCEDURE GetSymKey():LONGINT;
  178. BEGIN
  179.  (* not yet implemented *)
  180.  RETURN 0;
  181. END GetSymKey;
  182.  
  183. PROCEDURE AddKey(VAR file:File);
  184. VAR
  185.  ch:CHAR;
  186. BEGIN
  187.  IF ~SeqIO.OpenSeqIn(inFile,file.path,bufferSize) THEN
  188.   T.FormatS("File %s not found.\n",file.path);
  189.  ELSE
  190.   ch:=Read();
  191.   IF ch=CHAR(0F1H) THEN (* object file *)
  192.    file.key:=GetObjKey();
  193.   ELSIF ch=CHAR(0F9H) THEN (* symbol file *)
  194.    file.key:=GetSymKey();
  195.   ELSE
  196.    file.key:=0; HALT;
  197.    T.FormatS(" %s has unknown file format.\n",file.path);
  198.   END;
  199.   IF file.key=0 THEN
  200.    T.FormatS(" %s has no valid key, so no checks are performed.\n",file.path);
  201.   END;
  202.   SeqIO.CloseSeq(inFile);
  203.  END;
  204. END AddKey;
  205.  
  206. PROCEDURE VerifyKey(VAR file:File);
  207. VAR
  208.  ch:CHAR;
  209. BEGIN
  210.  IF file.key=0 THEN
  211.   T.FormatS(" %s is not verified.\n",file.path);
  212.  ELSE
  213.   IF ~SeqIO.OpenSeqIn(inFile,file.path,bufferSize) THEN
  214.    T.FormatS("File %s not found.\n",file.path);
  215.   ELSE
  216.    ch:=Read();
  217.    IF ch=CHAR(0F1H) THEN (* object file *)
  218.     VerifyObjKey(file);
  219.    ELSIF ch=CHAR(0F9H) THEN (* symbol file *)
  220.     VerifySymKey(file);
  221.    ELSE
  222.     T.FormatS(" %s has unknown file format.\n",file.path);
  223.    END;
  224.    SeqIO.CloseSeq(inFile);
  225.   END;
  226.  END;
  227. END VerifyKey;
  228.  
  229. PROCEDURE Verify(filePath:ARRAY OF CHAR);
  230. VAR
  231.  i:INTEGER;
  232.  path:Path;
  233. BEGIN
  234.  IF ~SeqIO.OpenSeqIn(inFile,filePath,bufferSize) THEN
  235.   T.FormatS("File %s not found.\n",filePath);
  236.  ELSE
  237.   T.FormatS("Reading names from %s:\n",filePath);
  238.   i:=0;
  239.   WHILE SeqIO.SeqOk(inFile) DO
  240.    ReadLine(path);
  241.    IF s.Length(path)>0 THEN
  242.     IF i<maxNames THEN
  243.      INC(i);
  244.      T.FormatNr("\r%4ld",i); T.Flush;
  245.      files[i].path:=path;
  246.     ELSE
  247.      T.FormatNr(" More than %ld files.\n",maxNames);
  248.      SeqIO.CloseSeq(inFile);
  249.      RETURN; (* <------------------------------- error termination. *)
  250.     END;
  251.    END;
  252.   END;
  253.   SeqIO.CloseSeq(inFile);
  254.   numOfFiles:=i;
  255.   T.WriteString(" files found. Getting keys:\n");
  256.   FOR i:=1 TO numOfFiles DO
  257.    T.FormatNr("\r%4ld",i);
  258.    T.FormatNr("/%4ld",numOfFiles);
  259.    T.Flush;
  260.    AddKey(files[i]);
  261.   END;
  262.   T.WriteString(" keys retrieved. Verifying keys:\n");
  263.   IF SeqIO.OpenSeqOut(outFile,"t:errors",bufferSize) THEN END;
  264.   FOR i:=1 TO numOfFiles DO
  265.    T.FormatNr("\r%4ld",i);
  266.    T.FormatNr("/%4ld",numOfFiles);
  267.    T.Flush;
  268.    VerifyKey(files[i]);
  269.   END;
  270.   T.WriteString(" all verified.\n");
  271.   SeqIO.CloseSeq(outFile);
  272.  END;
  273. END Verify;
  274.  
  275. VAR
  276.  len:INTEGER;
  277.  path:Path;
  278. BEGIN
  279.  Break.InstallException;
  280.  IF Arguments.NumArgs()#1 THEN
  281.   T.WriteString("Usage: verifyAll <file>\n\n  where file contains a list of sym/obj files to be checked.\n");
  282.  ELSE
  283.   Arguments.GetArg(1,path,len);
  284.   Verify(path);
  285.  END;
  286. END verify.
  287.