chmcmd.lpr 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. { Copyright (C) <2005> <Andrew Haines> chmcmd.pas
  2. This library is free software; you can redistribute it and/or modify it
  3. under the terms of the GNU Library General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or (at your
  5. option) any later version.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  9. for more details.
  10. You should have received a copy of the GNU Library General Public License
  11. along with this library; if not, write to the Free Software Foundation,
  12. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  13. }
  14. {
  15. See the file COPYING, included in this distribution,
  16. for details about the copyright.
  17. }
  18. program chmcmd;
  19. {$mode objfpc}{$H+}
  20. uses
  21. {$ifdef Unix}cthreads,{$endif} Classes, Sysutils, chmfilewriter, GetOpts;
  22. Const
  23. CHMCMDVersion = '3.1.1';
  24. Procedure Usage;
  25. begin
  26. Writeln(StdErr,'Usage: chmcmd [options] <filename>');
  27. writeln(stderr);
  28. writeln(stderr,'The following options are available :');
  29. writeln(stderr,' --html-scan : scan html for missing files or alinks ');
  30. writeln(stderr,' --no-html-scan : don''t scan html for missing files or alinks ');
  31. writeln(stderr,' -h, --help : print this text');
  32. writeln(stderr,'--verbosity number : set verbosity level 0..5, 0 is least');
  33. writeln(stderr,'--generate-xml : (if .hhp file), also generate a xml project from .hhp');
  34. writeln(stderr);
  35. writeln(stderr,' .hhp projects are default scanned for html, .xml not');
  36. Halt(1);
  37. end;
  38. var
  39. theopts : array[1..7] of TOption;
  40. cores : Integer = 0;
  41. procedure InitOptions;
  42. begin
  43. with theopts[1] do
  44. begin
  45. name:='html-scan';
  46. has_arg:=0;
  47. flag:=nil;
  48. value:=#0;
  49. end;
  50. with theopts[2] do
  51. begin
  52. name:='no-html-scan';
  53. has_arg:=0;
  54. flag:=nil;
  55. value:=#0;
  56. end;
  57. with theopts[3] do
  58. begin
  59. name:='verbosity';
  60. has_arg:=1;
  61. flag:=nil;
  62. value:=#0;
  63. end;
  64. with theopts[4] do
  65. begin
  66. name:='generate-xml';
  67. has_arg:=0;
  68. flag:=nil;
  69. value:=#0;
  70. end;
  71. with theopts[5] do
  72. begin
  73. name:='help';
  74. has_arg:=0;
  75. flag:=nil;
  76. end;
  77. with theopts[6] do
  78. begin
  79. name:='cores';
  80. has_arg:=1;
  81. flag:=nil;
  82. end;
  83. with theopts[7] do
  84. begin
  85. name:='';
  86. has_arg:=0;
  87. flag:=nil;
  88. end;
  89. end;
  90. Type THtmlScanenum = (scandefault,scanforce,scanforcedno);
  91. var
  92. GenerateXMLForHHP : boolean = false;
  93. alloweddetaillevel : integer = 0; // show if msg.detaillevel<=allowdetaillevel
  94. htmlscan : THtmlScanEnum = Scandefault;
  95. procedure OnError (Project: TChmProject;errorkind:TChmProjectErrorKind;msg:String;detailevel:integer=0);
  96. begin
  97. if (detailevel<=alloweddetaillevel) or (errorkind < chmnote) then
  98. if errorkind<>chmnone then
  99. writeln(ChmErrorKindText[errorkind],': ',msg)
  100. else
  101. writeln(msg);
  102. end;
  103. procedure Processfile(name:string);
  104. var
  105. OutStream: TFileStream;
  106. Project: TChmProject;
  107. xmlname: string;
  108. ishhp : boolean;
  109. begin
  110. ishhp:=uppercase(extractfileext(name))='.HHP';
  111. Project := TChmProject.Create;
  112. Project.ReadMeMessage:='Compiled by CHMCmd '+CHMCMDVersion;
  113. if ishhp then
  114. begin
  115. xmlname:=changefileext(name,'.hhp.xml');
  116. Project.OnError:=@OnError;
  117. try
  118. Project.LoadFromHHP(name,false) ; // we need a param for this second param later
  119. except
  120. on e:exception do
  121. begin
  122. Writeln('This HHP CHM project seems corrupt, please check it ',name,' (', e.message,')');
  123. halt(1);
  124. end;
  125. end;
  126. project.ScanHtmlContents:=htmlscan<>scanforcedno; // .hhp default SCAN
  127. end
  128. else
  129. begin
  130. try
  131. project.ScanHtmlContents:=htmlscan=scanforce; // .hhp default SCAN
  132. Project.LoadFromFile(name);
  133. except
  134. on e:exception do
  135. begin
  136. Writeln('This XML CHM project seems corrupt, please check it ',name);
  137. halt(1);
  138. end;
  139. end;
  140. end;
  141. OutStream := TFileStream.Create(Project.OutputFileName, fmCreate, fmOpenWrite);
  142. Project.WriteChm(OutStream);
  143. if Project.ScanHtmlContents then
  144. Project.ShowUndefinedAnchors;
  145. if ishhp and GenerateXMLForHHP then
  146. begin
  147. Writeln('Generating XML ',xmlname,'.');
  148. Project.SaveToFile(xmlname);
  149. end;
  150. OutStream.Free;
  151. Project.Free;
  152. end;
  153. var
  154. name : string;
  155. optionindex : integer;
  156. c : char;
  157. verbtemp : integer;
  158. verbbool : boolean;
  159. begin
  160. InitOptions;
  161. Writeln(stderr,'chmcmd, a CHM compiler. (c) 2010 Free Pascal core.');
  162. Writeln(Stderr);
  163. repeat
  164. c:=getlongopts('h',@theopts[1],optionindex);
  165. case c of
  166. #0 : begin
  167. case optionindex-1 of
  168. 0 : htmlscan:=scanforce;
  169. 1 : htmlscan:=scanforcedno;
  170. 2 : begin
  171. verbbool:=trystrtoint(optarg,verbtemp);
  172. if verbbool then
  173. verbbool:=(verbtemp>=0) and (verbtemp<6);
  174. if verbbool then
  175. alloweddetaillevel:=verbtemp
  176. else
  177. begin
  178. Writeln('Illegal value for switch --verbosity :',optarg);
  179. Usage;
  180. Halt;
  181. end;
  182. end;
  183. 3 : GenerateXMLForHHP:=true;
  184. 4 : begin;
  185. Usage;
  186. Halt;
  187. end;
  188. 5 : begin
  189. if not trystrtoint(optarg,cores) then
  190. begin
  191. Writeln('Illegal value for switch --cores :',optarg);
  192. Usage;
  193. Halt;
  194. end;
  195. end;
  196. end;
  197. end;
  198. '?' : begin
  199. writeln('unknown option',optopt);
  200. usage;
  201. halt;
  202. end;
  203. end; { case }
  204. until c=endofoptions;
  205. if (paramcount-optind)=0 then // if equal, then 1 parameter
  206. begin
  207. name:=paramstr(optind);
  208. if not fileexists(name) then
  209. begin
  210. Writeln('Can''t find project file ',name);
  211. halt;
  212. end;
  213. ProcessFile(Name);
  214. end
  215. else
  216. begin
  217. Writeln('Invalid number of parameters :', paramcount-optind+1);
  218. Usage;
  219. halt;
  220. end;
  221. end.