home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TPATCH.ZIP / TPATCH.PAS
Encoding:
Pascal/Delphi Source File  |  1985-12-28  |  5.5 KB  |  172 lines

  1. { Turbo program patcher version 1.00A by Bela Lubkin 1/10/85
  2.   Send suggestions via Borland SIG on CompuServe - GO BOR }
  3.  
  4. Program TurboPatch;
  5.   Const
  6.     MaxPatLen=20;
  7.     MaxPat=10;
  8.     MaxVer=10;
  9.     Missing: String[30]='Missing section in TPATCH.DAT';
  10.  
  11.   Type
  12.     String80=String[80];
  13.     PatArray=Array [1..MaxPatLen] Of Byte;
  14.  
  15.   Var
  16.     CommandLine: String[127] Absolute CSEG:$0080;
  17.     PatchFile: Text;
  18.     S: String[40];
  19.     ComFile: File Of Byte;
  20.     ComName: String[20];
  21.     ComStartAddress: Integer;
  22.     ComVersion: Integer;
  23.     NPatches: Integer;
  24.     PatchName: Array [1..MaxPat] Of String[80];
  25.     NVersions: Integer;
  26.     Version: Array [1..MaxVer] Of Record
  27.                                     StartAddress: Integer;
  28.                                     Name: String[30];
  29.                                   End;
  30.     Patch: Array [1..MaxPat,1..MaxVer] Of Record
  31.                                             Address: Integer;
  32.                                             Len: Byte;
  33.                                             Pat: PatArray;
  34.                                             UnPat: PatArray;
  35.                                           End;
  36.     PatchArea: PatArray;
  37.     Applied: (Yes,No,Maybe,Cant);
  38.     PA,PR,PP,I,J,K: Integer;
  39.     Ans: Char;
  40.     B,C: Byte;
  41.  
  42.   Procedure Error(M: String80);
  43.  
  44.     Procedure X;
  45.       Var I: Integer;
  46.  
  47.       Begin
  48.         I:=IOResult;
  49.       End;
  50.  
  51.     Begin
  52.       Write(M);
  53.       {$I-} Close(PatchFile); X;
  54.       Close(ComFile); X;
  55.       Halt;
  56.     End;
  57.  
  58.   Begin
  59.     ComName:=Copy(CommandLine,2,127);
  60.     WriteLn('Turbo program patcher version 1.00A by Bela Lubkin');
  61.     Assign(PatchFile,'tpatch.dat');
  62.     {$I-} Reset(PatchFile); {$I+}
  63.     If IOResult<>0 Then Error('File TPATCH.DAT not found');
  64.     ReadLn(PatchFile,S);
  65.     If S<>'PATCH NAMES' Then Error(Missing);
  66.     ReadLn(PatchFile,NPatches);
  67.     If (NPatches>MaxPat) Or (NPatches<1) Then
  68.      Begin
  69.       Write('Number of patches not in [1..',MaxPat,']');
  70.       Error('');
  71.      End;
  72.     For I:=1 To NPatches Do ReadLn(PatchFile,PatchName[I]);
  73.     ReadLn(PatchFile,S);
  74.     If S<>'VERSIONS' Then Error(Missing);
  75.     ReadLn(PatchFile,NVersions);
  76.     If (NVersions>MaxVer) Or (NVersions<1) Then
  77.      Begin
  78.       Write('Number of versions not in [1..',MaxVer,']');
  79.       Error('');
  80.      End;
  81.     For I:=1 To NVersions Do
  82.       With Version[I] Do
  83.         ReadLn(PatchFile,StartAddress,Name);
  84.     ReadLn(PatchFile,S);
  85.     If S<>'PATCHES' Then Error(Missing);
  86.     For I:=1 To NPatches Do
  87.       For J:=1 To NVersions Do
  88.         With Patch[I,J] Do
  89.          Begin
  90.           Read(PatchFile,Address);
  91.           If Address<>0 Then
  92.            Begin
  93.             Read(PatchFile,Len);
  94.             If (Len<1) Or (Len>MaxPatLen) Then Error('Patch too short or long in TPATCH.DAT');
  95.             For K:=1 To Len Do Read(PatchFile,Pat[K]);
  96.             For K:=1 To Len Do Read(PatchFile,UnPat[K]);
  97.            End;
  98.           ReadLn(PatchFile);
  99.          End;
  100.     If ComName='' Then
  101.      Begin
  102.       Write('Enter .COM file name: ');
  103.       ReadLn(ComName);
  104.      End;
  105.     For I:=1 To Length(ComName) Do ComName[I]:=UpCase(ComName[I]);
  106.     If Pos('.',ComName)=0 Then ComName:=ComName+'.COM';
  107.     Assign(ComFile,ComName);
  108.     {$I-} Reset(ComFile); {$I+}
  109.     If IOResult<>0 Then Error(ComName+' could not be opened');
  110.     Read(ComFile,B);
  111.     If B<>$E9 Then Error(ComName+' is not a Turbo .COM file');
  112.     Read(ComFile,B);
  113.     Read(ComFile,C);
  114.     ComStartAddress:=B+C Shl 8+$103;
  115.     ComVersion:=0;
  116.     For I:=1 To NVersions Do
  117.       If ComStartAddress=Version[I].StartAddress Then ComVersion:=I;
  118.     If ComVersion=0 Then
  119.      Begin
  120.       WriteLn(ComName,' is not a Turbo .COM file, or was generated by a version of Turbo');
  121.       Error('Pascal not supported by this TPATCH.DAT')
  122.      End
  123.     Else WriteLn(ComName,' was created by Turbo Pascal ',Version[ComVersion].Name);
  124.     PP:=0;
  125.     PA:=0;
  126.     PR:=0;
  127.     For I:=1 To NPatches Do
  128.       With Patch[I,ComVersion] Do
  129.         If Address<>0 Then
  130.          Begin
  131.           PP:=PP+1;
  132.           Seek(ComFile,Address-$100);
  133.           For J:=1 To Len Do Read(ComFile,PatchArea[J]);
  134.           Applied:=Maybe;
  135.           For J:=1 To Len Do
  136.             Case Applied Of
  137.               Maybe: If PatchArea[J]=Pat[J] Then Applied:=Yes
  138.                      Else If PatchArea[J]=UnPat[J] Then Applied:=No
  139.                      Else Applied:=Cant;
  140.               Yes: If PatchArea[J]<>Pat[J] Then Applied:=Cant;
  141.               No: If PatchArea[J]<>UnPat[J] Then Applied:=Cant;
  142.              End;
  143.           Case Applied Of
  144.             Cant: WriteLn('"',PatchName[I],'" cannot be applied.');
  145.             Yes: Write('Remove');
  146.             No: Write('Apply');
  147.            End;
  148.           If Applied<>Cant Then
  149.            Begin
  150.             Write(' "',PatchName[I],'"? ');
  151.             ReadLn(Ans);
  152.             If UpCase(Ans)='Y' Then
  153.               Case Applied Of
  154.                 Yes: Begin
  155.                        PR:=PR+1;
  156.                        Seek(ComFile,Address-$100);
  157.                        For J:=1 To Len Do Write(ComFile,UnPat[J]);
  158.                      End;
  159.                 No: Begin
  160.                       PA:=PA+1;
  161.                       Seek(ComFile,Address-$100);
  162.                       For J:=1 To Len Do Write(ComFile,Pat[J]);
  163.                     End;
  164.                End;
  165.            End;
  166.          End;
  167.     If PP=0 Then Error('No applicable patches');
  168.     WriteLn(PA,' patches applied');
  169.     WriteLn(PR,' patches removed');
  170.     Error(''); { Close files }
  171.   End.
  172.