summaryrefslogtreecommitdiff
path: root/utils/mkinsadd.pp
blob: 3ec1d5172d69ce3a0f1ffd9beda286505130a808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
{$MODE FPC}
{
    This file is part of Free Pascal build tools
    Copyright (c) 2014-2015 by Tomas Hajny, member of the FPC core team.

    This program processes one or more listing files created with fpmake
    (e.g. using 'fpmake pkglist --target=<FPC_target> -zp units-' for
    unit packages or without the '-zp <prefix>' for utils), compares
    them to the text-mode installer configuration file install.dat and
    creates file install.add which provides information about packages
    missing in install.dat in a form allowing copy&paste of individual
    lines into install.dat.

    If the original description of a certain package as found in fpmake.pp
    is too long for install.dat, the maximum length is marked
    in the respective line in install.add using a pipe character ('|').

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

program mkinsadd;

uses
 dos, objects;


const
 MaxTarget = 5;
 TargetListShort: array [1..MaxTarget] of string [3] = ('dos', 'emx', 'os2', 'w32', 'src');
 TargetListLong: array [1..MaxTarget] of string = ('dos', 'emx', 'os2', '.i386-win32', '.source');
 DefDiffFN = 'install.add';
 PackageStr = 'package=';
 UnitsStr = 'units-';
 ZipExt = '.zip';


type
 PPackageRec = ^TPackageRec;
 TPackageRec = object (TObject)
  Name, ShortName, Desc: PString;
  Target: byte;
  constructor Init (ALine: string);
  function GetKeyStr: string;
  function GetLine: string;
  function GetSrcLine: string;
  destructor Done; virtual;
 end;

 PPackageCollection = ^TPackageCollection;
 TPackageCollection = object (TSortedCollection)
  constructor Load (FN: string);
  function LoadFile (FN: string; DupSrc: PPackageCollection): boolean;
  function WriteFile (FN: string): boolean;
  function Compare (Key1, Key2: pointer): sw_integer; virtual;
 end;

 PDatFile = ^TDatFile;
 TDatFile = object (TObject)
  DatCollection, LstCollection: PPackageCollection;
  constructor LoadDat (FN: string);
  function ReadLstFile (FN: string): boolean;
  function WriteNew (FN: string): boolean;
  destructor Done; virtual;
 end;


function LoCase (S: string): string;
var
 I: longint;
begin
 for I := 1 to Length (S) do
  if S [I] in ['A'..'Z'] then
   S [I] := char (Ord (S [I]) + 32);
 LoCase := S;
end;


constructor TPackageRec.Init (ALine: string);
var
 I: longint;
 J: byte;
 N, SN, D, TS: string;
 ALine2: string;
begin
 inherited Init;
 N := '';
 SN := '';
 D := '';
 TS := '';
 ALine2 := LoCase (ALine);
 if Copy (ALine2, 1, Length (PackageStr)) = PackageStr then
  begin
   Delete (ALine, 1, Length (PackageStr));
   I := Pos ('[', ALine);
   if I = 0 then
    begin
     I := Pos (',', ALine);
     if I = 0 then
      I := Succ (Length (ALine));
    end
   else
    begin
     SN := Copy (ALine, Succ (I), Pos (',', ALine) - I - 2);
     Delete (ALine, I, Length (SN) + 2);
    end;
   N := Copy (ALine, 1, Pred (I));
   if Length (N) <= 12 then
    SN := N
   else if (Copy (N, 1, Length (UnitsStr)) = UnitsStr) and
                                    (Length (N) - Length (UnitsStr) <= 11) then
    SN := 'u' + Copy (N, Succ (Length (UnitsStr)),
                                               Length (N) - Length (UnitsStr));
   D := Copy (ALine, Succ (I), Length (ALine) - I);
  end;

 Name := NewStr (N);
 if SN <> '' then
  ShortName := NewStr (SN)
 else
  ShortName := nil;
 Desc := NewStr (D);
 Target := 0;

 if SN <> '' then
  begin
   TS := LoCase (Copy (SN, Length (SN) - Length (ZipExt) - 2, 3));
   if Length (TS) <> 3 then
    TS := ''
   else
    for J := 1 to MaxTarget do
     if TS = TargetListShort [J] then
      begin
       Target := J;
       Break;
      end;
  end
 else
  begin
   I := Length (N) - Length (ZipExt);
   while (I > 0) and (N [I] <> '.') do
    Dec (I);
   if I = 0 then
    TS := LoCase (Copy (N, Length (SN) - Length (ZipExt) - 2, 3))
   else
    TS := LoCase (Copy (N, I, Length (N) - Length (ZipExt) - I + 1));
   for J := 1 to MaxTarget do
    if TS = TargetListLong [J] then
     begin
      Target := J;
      Break;
     end;
  end;
 if N = '' then
  begin
   WriteLn ('Err: Init failed (', ALine, ')!');
   Fail;
  end;
end;


destructor TPackageRec.Done;
begin
 DisposeStr (Name);
 if ShortName <> nil then
  DisposeStr (ShortName);
 DisposeStr (Desc);
 inherited Done;
end;


function TPackageRec.GetKeyStr: string;
var
 G: string;
begin
 if ShortName <> nil then
  begin
   if Target = 0 then
    G := LoCase (Copy (ShortName^, 1, Length (ShortName^) - Length (ZipExt)))
   else
    G := LoCase (Copy (ShortName^, 1, Length (ShortName^) - Length (ZipExt) - 3));
  end
 else
  begin
   if Name = nil then
    begin
     GetKeyStr := '';
     WriteLn ('Err - GetKeyStr (nil)!');
     Exit;
    end;
   if Target = 0 then
    G := LoCase (Copy (Name^, 1, Length (Name^) - Length (ZipExt)))
   else
    begin
     if Copy (LoCase (Name^), 1, Length (UnitsStr)) = UnitsStr then
      G := 'u' + LoCase (Copy (Name^, Succ (Length (UnitsStr)),
         Length (Name^) - Length (UnitsStr) - Length (TargetListLong [Target])
                                                            - Length (ZipExt)))
     else
      G := LoCase (Copy (Name^, 1,
         Length (Name^) - Length (TargetListLong [Target]) - Length (ZipExt)));
    end;
  end;

 G := G + '.';
 if Target <> 0 then
  G := G + TargetListShort [Target];
 GetKeyStr := G;
end;


function TPackageRec.GetLine: string;
var
 G: string;
begin
 G := PackageStr + Name^;
 if ShortName <> nil then
  G := G + '[' + ShortName^ + ']';
 if Length (Desc^) <= 45 then
  G := G + ',' + Desc^
 else
  G := G + ',' + Copy (Desc^, 1, 45) + '|' +
                                         Copy (Desc^, 46, Length (Desc^) - 45);
 GetLine := G;
end;


function TPackageRec.GetSrcLine: string;
var
 GS: string;
begin
 if Target = 0 then
  GS := ''
 else
  begin
   GS := PackageStr + Copy (Name^, 1,
     Length (Name^) - Length (TargetListLong [Target]) - Length (ZipExt)) +
                                           TargetListLong [MaxTarget] + ZipExt;
   if ShortName <> nil then
    GS := GS + '[' + Copy (ShortName^, 1, Length (ShortName^)
                    - Length (TargetListShort [Target]) - Length (ZipExt)) +
                                    TargetListShort [MaxTarget] + ZipExt + ']';
   GS := GS + ',' + Desc^;
  end;
 GetSrcLine := GS;
end;


constructor TDatFile.LoadDat (FN: string);
begin
 Init;
 New (DatCollection, Load (FN));
 New (LstCollection, Init (100, 50)); (* false? *)
end;


function TDatFile.ReadLstFile (FN: string): boolean;
begin
 ReadLstFile := LstCollection^.LoadFile (FN, DatCollection);
end;


function TDatFile.WriteNew (FN: string): boolean;
begin
 WriteNew := LstCollection^.WriteFile (FN);
end;


destructor TDatFile.Done;
begin
 Dispose (DatCollection, Done);
 Dispose (LstCollection, Done);
 inherited Done;
end;


constructor TPackageCollection.Load (FN: string);
begin
 Init (100, 50);
 if not (LoadFile (FN, nil)) then
  Fail;
end;


function TPackageCollection.LoadFile (FN: string; DupSrc: PPackageCollection): boolean;
var
 F: text;
 S: ansistring;
 S2: string;
 P, Q: PPackageRec;
 I: SW_Integer;
begin
{$I-}
 Assign (F, FN);
 Reset (F);
 while not (Eof (F)) {and (LastErr = 0)} do
  begin
   S := '';
   ReadLn (F, S);
   if (Length (S) > 255) then
    begin
     WriteLn ('Error: Line too long!');
     WriteLn (S);
     Halt (255); (* Change error handling *)
    end;
   if Copy (LoCase (S), 1, Length (PackageStr)) = PackageStr then
    begin
     New (P, Init (S));
     if DupSrc = nil then
      S2 := ''
     else
      S2 := P^.GetSrcLine;
     if (DupSrc = nil) or not (DupSrc^.Search (P, I)) then
      Insert (P)
     else
      Dispose (P, Done);
     if S2 <> '' then
      begin
       New (Q, Init (S2));
       if (Q <> nil) and not (Search (Q, I)) and
                           ((DupSrc = nil) or not (DupSrc^.Search (Q, I))) then
        Insert (Q)
       else
        Dispose (Q, Done);
      end;
    end;
  end;
 Close (F);
 LoadFile := IOResult = 0;
{
 if P = nil then Fail else
 begin
  if P^.LastErr <> 0 then
  begin
   Dispose (P, Done);
   Fail;
  end else
  begin
   P^.ReadIni (@Self);
   Dispose (P, Done);
  end;
 end;
}
end;


function TPackageCollection.WriteFile (FN: string): boolean;
var
 F: text;
 S: string;
 P: PPackageRec;
 I: SW_Integer;
 J: byte;
begin
 Assign (F, FN);
 Rewrite (F);
 for J := 0 to MaxTarget do
  for I := 0 to Count - 1 do
   begin
    P := At (I);
    if (P <> nil) and (P^.Target = J) then
     begin
{ Write (P^.Name^, '|');
   if P^.ShortName <> nil then
    Write (P^.ShortName^, '|')
   else
    Write ('x|');
   WriteLn (P^.Desc^, '|', P^.Target);
 WriteLn (P^.GetKeyStr);
}
      S := P^.GetLine;
(* Signalize too long description *)
      WriteLn (F, S);
     end;
   end;
 Close (F);
 WriteFile := IOResult = 0;
end;


function TPackageCollection.Compare (Key1, Key2: pointer): SW_Integer;
var
 S1, S2: string;
begin
 S1 := LoCase (PPackageRec (Key1)^.GetKeyStr);
 S2 := LoCase (PPackageRec (Key2)^.GetKeyStr);
 if S1 < S2 then
  Compare := -1
 else if S1 > S2 then
  Compare := 1
 else
  Compare := 0;
end;


function Base (const S: string): string;
var
 D: DirStr;
 N: NameStr;
 E: ExtStr;
begin
 FSplit (S, D, N, E);
 Base := N;
end;


procedure Error (const S: string; B: byte);
begin
 WriteLn;
 WriteLn ('Error: ', S, '!!');
 Halt (B);
end;


procedure Syntax;
begin
 WriteLn;
 WriteLn ('Syntax: ', Base (ParamStr (0)),
                          ' <path_to_install.dat> <LstFile1> [<LstFile2>...]');
 WriteLn;
 WriteLn ('<LstFileN> files are expected to be in the format produced by fpmake');
 WriteLn ('(e.g. using ''fpmake pkglist --target=<FPC_target> -zp units-''');
 WriteLn ('for unit packages or without the ''-zp <prefix>'' parameter for utils).');
 WriteLn;
 WriteLn ('Program compares their content to the list of packages in the text-mode');
 WriteLn ('installer configuration file install.dat and creates file install.add');
 WriteLn ('with information about packages missing in install.dat in a form allowing');
 WriteLn ('copy&paste of individual lines into install.dat.');
 WriteLn;
 WriteLn ('If the original description of a certain package as found in fpmake.pp is');
 WriteLn ('too long for install.dat, the maximum length is marked in the respective line');
 WriteLn ('in install.add using a pipe character (''|'') to give hint for manual editing.');
 Halt;
end;

var
 I, J: byte;
 DAT: TDatFile;
 PrevCount: SW_Integer;

begin
 J := ParamCount;
 if J < 2 then
  begin
   WriteLn;
   WriteLn ('Error: Too few parameters!!');
   Syntax;
  end;
 DAT.LoadDat (ParamStr (1));
 if DAT.DatCollection <> nil then
  WriteLn (LineEnding +
            'Source install.dat file (', ParamStr (1), ') loaded correctly: ',
                                          DAT.DatCollection^.Count, ' records')
 else
  Error ('Failure while loading source install.dat file (' + ParamStr (1) +
                                                                       ')', 1);
 for I := 2 to J do
  begin
   PrevCount := DAT.LstCollection^.Count;
   if DAT.ReadLstFile (ParamStr (I)) then
    WriteLn ('Package listing #', Pred (I), ' (', ParamStr (I),
      ') loaded correctly: ', DAT.LstCollection^.Count - PrevCount,
                                                                ' new records')
   else
    Error ('Failure while loading package listing (' + ParamStr (I) + ')', I);
  end;
 WriteLn ('Total: ', DAT.LstCollection^.Count, ' new records');
 if DAT.WriteNew (DefDiffFN) then
  WriteLn ('Output file (' + DefDiffFN + ') created successfully.')
 else
  Error ('Failure while trying to write records to the output file (' +
                                                    DefDiffFN + ')', Succ (J));
 DAT.Done;
end.