summaryrefslogtreecommitdiff
path: root/packages/chm/src/chmcmd.lpr
blob: 29885497a752f74a1b0574876ec82941c27104c9 (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
{ Copyright (C) <2005> <Andrew Haines> chmcmd.pas

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  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. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
}
{
  See the file COPYING, included in this distribution,
  for details about the copyright.
}
program chmcmd;

{$mode objfpc}{$H+}

uses
  {$ifdef Unix}cthreads,{$endif} Classes, Sysutils, chmfilewriter, GetOpts;

Const
  CHMCMDVersion = '3.0.4';

Procedure Usage;

begin
  Writeln(StdErr,'Usage: chmcmd [options] <filename>');
  writeln(stderr);
  writeln(stderr,'The following options are available :');
  writeln(stderr,' --html-scan       : scan html for missing files or alinks  ');
  writeln(stderr,' --no-html-scan    : don''t scan html for missing files or alinks ');
  writeln(stderr,' -h, --help        : print this text');
  writeln(stderr,'--verbosity number : set verbosity level 0..5, 0 is least');
  writeln(stderr,'--generate-xml     : (if .hhp file), also generate a xml project from .hhp');
  writeln(stderr);
  writeln(stderr,' .hhp projects are default scanned for html, .xml not');
  Halt(1);
end;

var
  theopts : array[1..7] of TOption;
  cores   : Integer = 0;

procedure InitOptions;

begin
  with theopts[1] do
   begin
    name:='html-scan';
    has_arg:=0;
    flag:=nil;
    value:=#0;
  end;
  with theopts[2] do
   begin
    name:='no-html-scan';
    has_arg:=0;
    flag:=nil;
    value:=#0;
  end;
  with theopts[3] do
   begin
    name:='verbosity';
    has_arg:=1;
    flag:=nil;
    value:=#0;
  end;
  with theopts[4] do
   begin
    name:='generate-xml';
    has_arg:=0;
    flag:=nil;
    value:=#0;
  end;
  with theopts[5] do
   begin
    name:='help';
    has_arg:=0;
    flag:=nil;
  end;
  with theopts[6] do
   begin
    name:='cores';
    has_arg:=1;
    flag:=nil;
  end;
  with theopts[7] do
   begin
    name:='';
    has_arg:=0;
    flag:=nil;
  end;
end;

Type THtmlScanenum = (scandefault,scanforce,scanforcedno);

var
  GenerateXMLForHHP  : boolean = false;
  alloweddetaillevel : integer = 0;     // show if msg.detaillevel<=allowdetaillevel
  htmlscan           : THtmlScanEnum = Scandefault;

procedure OnError (Project: TChmProject;errorkind:TChmProjectErrorKind;msg:String;detailevel:integer=0);
begin
  if (detailevel<=alloweddetaillevel) or (errorkind < chmnote) then
    if errorkind<>chmnone then
      writeln(ChmErrorKindText[errorkind],': ',msg)
    else
      writeln(msg);
end;

procedure Processfile(name:string);

var
  OutStream: TFileStream;
  Project: TChmProject;
  xmlname: string;
  ishhp  : boolean;

begin
  ishhp:=uppercase(extractfileext(name))='.HHP';
  Project := TChmProject.Create;
  Project.ReadMeMessage:='Compiled by CHMCmd '+CHMCMDVersion;
  if ishhp then
    begin
      xmlname:=changefileext(name,'.hhp.xml');
      Project.OnError:=@OnError;
      try
        Project.LoadFromHHP(name,false) ;          // we need a param for this second param later
       except
         on e:exception do
           begin
             Writeln('This HHP CHM project seems corrupt, please check it ',name,' (', e.message,')');
             halt(1);
           end;
       end;
      project.ScanHtmlContents:=htmlscan<>scanforcedno;  // .hhp default SCAN
    end
  else
    begin
     try
      project.ScanHtmlContents:=htmlscan=scanforce;  // .hhp default SCAN
      Project.LoadFromFile(name);
     except
       on e:exception do
         begin
           Writeln('This XML CHM project seems corrupt, please check it ',name);
           halt(1);
         end;
       end;
    end;
  OutStream := TFileStream.Create(Project.OutputFileName, fmCreate);
  Project.WriteChm(OutStream);
  if Project.ScanHtmlContents then
    Project.ShowUndefinedAnchors;
  if ishhp and GenerateXMLForHHP then
    begin
      Writeln('Generating XML ',xmlname,'.');
      Project.SaveToFile(xmlname);
    end;
  OutStream.Free;
  Project.Free;

end;

var
  name   : string;
  optionindex : integer;
  c      : char;
  verbtemp : integer;
  verbbool : boolean;

begin
  InitOptions;
  Writeln(stderr,'chmcmd, a CHM compiler. (c) 2010 Free Pascal core.');
  Writeln(Stderr);
  repeat
    c:=getlongopts('h',@theopts[1],optionindex);
    case c of
      #0 : begin
             case optionindex-1 of
               0 : htmlscan:=scanforce;
               1 : htmlscan:=scanforcedno;
               2 : begin
                     verbbool:=trystrtoint(optarg,verbtemp);
                     if verbbool then
                       verbbool:=(verbtemp>=0) and (verbtemp<6);
                     if verbbool then
                       alloweddetaillevel:=verbtemp
                     else
                       begin
                         Writeln('Illegal value for switch --verbosity :',optarg);
                         Usage;
                         Halt;
                       end;
                   end;
               3 : GenerateXMLForHHP:=true;
               4 : begin;
                    Usage;
                    Halt;
                   end;
               5 : begin
                     if not trystrtoint(optarg,cores) then
                       begin
                         Writeln('Illegal value for switch --cores :',optarg);
                         Usage;
                         Halt;
                       end;

		   end;
                end;
           end;
      '?' : begin
              writeln('unknown option',optopt);
              usage;
              halt;
            end;
   end; { case }
 until c=endofoptions;
 if (paramcount-optind)=0 then  // if equal, then 1 parameter
    begin
      name:=paramstr(optind);
      if not fileexists(name) then
        begin
          Writeln('Can''t find project file ',name);
          halt;
        end;
      ProcessFile(Name);
    end
 else
   begin
     Writeln('Invalid number of parameters :', paramcount-optind+1);
     Usage;
     halt;
   end;
end.