123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- { 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.1.1';
- 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, fmOpenWrite);
- 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.
|