fpdoc.pp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. {
  2. $Id$
  3. FPDoc - Free Pascal Documentation Tool
  4. Copyright (C) 2000 - 2003 by
  5. Areca Systems GmbH / Sebastian Guenther, [email protected]
  6. See the file COPYING, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. program FPDoc;
  13. uses
  14. SysUtils, Classes, Gettext, DOM, XMLWrite, PasTree, PParser,
  15. dGlobals, // GLobal definitions, constants.
  16. dwriter, // TFPDocWriter definition.
  17. dwlinear, // Linear (abstract) writer
  18. dw_LaTeX, // TLaTex writer
  19. dw_XML, // XML writer
  20. dw_HTML, // HTML writer
  21. dw_ipf, // IPF writer
  22. dw_man, // Man page writer
  23. dw_txt; // TXT writer
  24. const
  25. OSTarget: String = {$I %FPCTARGETOS%};
  26. CPUTarget: String = {$I %FPCTARGETCPU%};
  27. var
  28. Backend : String;
  29. BackendOptions : TStrings;
  30. InputFiles, DescrFiles: TStringList;
  31. PackageName, DocLang, ContentFile : String;
  32. Engine: TFPDocEngine;
  33. Procedure Usage(AnExitCode : Byte);
  34. Var
  35. I,P : Integer;
  36. S : String;
  37. L : TStringList;
  38. C : TFPDocWriterClass;
  39. begin
  40. Writeln(Format(SCmdLineHelp,[ExtractFileName(Paramstr(0))]));
  41. Writeln(SUsageOption010);
  42. Writeln(SUsageOption020);
  43. Writeln(SUsageOption030);
  44. Writeln(SUsageOption040);
  45. Writeln(SUsageOption050);
  46. Writeln(SUsageOption060);
  47. Writeln(SUsageOption070);
  48. Writeln(SUsageOption080);
  49. Writeln(SUsageOption090);
  50. Writeln(SUsageOption100);
  51. Writeln(SUsageOption110);
  52. Writeln(SUsageOption120);
  53. Writeln(SUsageOption130);
  54. Writeln(SUsageOption140);
  55. Writeln(SUsageOption150);
  56. Writeln(SUsageOption160);
  57. Writeln(SUsageOption170);
  58. L:=TStringList.Create;
  59. Try
  60. If (Backend='') then
  61. begin
  62. Writeln;
  63. Writeln(SUsageFormats);
  64. EnumWriters(L);
  65. For I:=0 to L.Count-1 do
  66. begin
  67. S:=L[i];
  68. P:=Pos('=',S);
  69. Writeln(Format(' %s - %s',[Copy(S,1,P-1)+Space(10-p),Copy(S,P+1,Length(S))]));
  70. end;
  71. Writeln(SUsageBackendHelp);
  72. end
  73. else
  74. begin
  75. Writeln;
  76. Writeln(Format(SUsageFormatSpecific,[Lowercase(Backend)]));
  77. C:=GetWriterClass(backend);
  78. C.Usage(L);
  79. If L.Count>0 then
  80. For I:=0 to (L.Count-1) div 2 do
  81. begin
  82. S:=L[i*2];
  83. Writeln(Format('%s %s',[S+Space(30-Length(S)),L[(i*2)+1]]));
  84. end;
  85. end;
  86. Finally
  87. L.Free;
  88. end;
  89. Halt(AnExitCode);
  90. end;
  91. procedure InitOptions;
  92. begin
  93. InputFiles := TStringList.Create;
  94. DescrFiles := TStringList.Create;
  95. BackendOptions := TStringList.Create;
  96. Engine := TFPDocEngine.Create;
  97. end;
  98. procedure FreeOptions;
  99. begin
  100. Engine.Free;
  101. BackendOptions.Free;
  102. DescrFiles.Free;
  103. InputFiles.Free;
  104. end;
  105. procedure ReadContentFile(const AParams: String);
  106. var
  107. i: Integer;
  108. begin
  109. i := Pos(',', AParams);
  110. Engine.ReadContentFile(Copy(AParams, 1, i - 1),
  111. Copy(AParams, i + 1, Length(AParams)));
  112. end;
  113. procedure ParseOption(const s: String);
  114. procedure AddToFileList(List: TStringList; const FileName: String);
  115. var
  116. f: Text;
  117. s: String;
  118. begin
  119. if Copy(FileName, 1, 1) = '@' then
  120. begin
  121. Assign(f, Copy(FileName, 2, Length(FileName)));
  122. Reset(f);
  123. while not EOF(f) do
  124. begin
  125. ReadLn(f, s);
  126. List.Add(s);
  127. end;
  128. Close(f);
  129. end else
  130. List.Add(FileName);
  131. end;
  132. var
  133. i: Integer;
  134. Cmd, Arg: String;
  135. begin
  136. if (s = '-h') or (s = '--help') then
  137. Usage(0)
  138. else if s = '--hide-protected' then
  139. Engine.HideProtected := True
  140. else if s = '--warn-no-node' then
  141. Engine.WarnNoNode := True
  142. else if s = '--show-private' then
  143. Engine.HidePrivate := False
  144. else
  145. begin
  146. i := Pos('=', s);
  147. if i > 0 then
  148. begin
  149. Cmd := Copy(s, 1, i - 1);
  150. Arg := Copy(s, i + 1, Length(s));
  151. end
  152. else
  153. begin
  154. Cmd := s;
  155. SetLength(Arg, 0);
  156. end;
  157. if Cmd = '--descr' then
  158. AddToFileList(DescrFiles, Arg)
  159. else if (Cmd = '-f') or (Cmd = '--format') then
  160. begin
  161. Arg:=UpperCase(Arg);
  162. If FindWriterClass(Arg)=-1 then
  163. WriteLn(StdErr, Format(SCmdLineInvalidFormat, [Arg]))
  164. else
  165. BackEnd:=Arg;
  166. end
  167. else if (Cmd = '-l') or (Cmd = '--lang') then
  168. DocLang := Arg
  169. else if (Cmd = '-i') or (Cmd = '--input') then
  170. AddToFileList(InputFiles, Arg)
  171. else if (Cmd = '-o') or (Cmd = '--output') then
  172. Engine.Output := Arg
  173. else if Cmd = '--content' then
  174. ContentFile := Arg
  175. else if Cmd = '--import' then
  176. ReadContentFile(Arg)
  177. else if Cmd = '--package' then
  178. PackageName := Arg
  179. else if Cmd = '--ostarget' then
  180. OSTarget := Arg
  181. else if Cmd = '--cputarget' then
  182. CPUTarget := Arg
  183. else
  184. begin
  185. BackendOptions.Add(Cmd);
  186. BackendOptions.Add(Arg);
  187. end;
  188. end;
  189. end;
  190. procedure ParseCommandLine;
  191. var
  192. i: Integer;
  193. begin
  194. for i := 1 to ParamCount do
  195. ParseOption(ParamStr(i));
  196. If (BackEnd='') then
  197. BackEnd:='html';
  198. if (PackageName='') then
  199. begin
  200. Writeln(SNeedPackageName);
  201. Usage(1);
  202. end;
  203. end;
  204. procedure CreateDocumentation;
  205. var
  206. i: Integer;
  207. WriterClass : TFPDocWriterClass;
  208. Writer : TFPDocWriter;
  209. begin
  210. for i := 0 to DescrFiles.Count - 1 do
  211. Engine.AddDocFile(DescrFiles[i]);
  212. Engine.SetPackageName(PackageName);
  213. if Length(DocLang) > 0 then
  214. TranslateDocStrings(DocLang);
  215. for i := 0 to InputFiles.Count - 1 do
  216. try
  217. ParseSource(Engine, InputFiles[i], OSTarget, CPUTarget);
  218. except
  219. on e: EParserError do
  220. WriteLn(StdErr, Format('%s(%d,%d): %s',
  221. [e.Filename, e.Row, e.Column, e.Message]));
  222. end;
  223. WriterClass:=GetWriterClass(Backend);
  224. Writer:=WriterClass.Create(Engine.Package,Engine);
  225. With Writer do
  226. Try
  227. If BackendOptions.Count>0 then
  228. for I:=0 to ((BackendOptions.Count-1) div 2) do
  229. If not InterPretOption(BackendOptions[I*2],BackendOptions[I*2+1]) then
  230. WriteLn(StdErr, Format(SCmdLineInvalidOption,[BackendOptions[I*2]+' '+BackendOptions[I*2+1]]));
  231. WriteDoc;
  232. Finally
  233. Free;
  234. end;
  235. if Length(ContentFile) > 0 then
  236. Engine.WriteContentFile(ContentFile);
  237. end;
  238. begin
  239. {$IFDEF Unix}
  240. gettext.TranslateResourceStrings('/usr/local/share/locale/%s/LC_MESSAGES/fpdoc.mo');
  241. {$ELSE}
  242. gettext.TranslateResourceStrings('intl/fpdoc.%s.mo');
  243. {$ENDIF}
  244. WriteLn(STitle);
  245. WriteLn(SCopyright);
  246. WriteLn;
  247. InitOptions;
  248. Try
  249. ParseCommandLine;
  250. CreateDocumentation;
  251. WriteLn(SDone);
  252. Finally
  253. FreeOptions;
  254. end;
  255. end.
  256. {
  257. $Log$
  258. Revision 1.10 2005-02-14 17:13:39 peter
  259. * truncate log
  260. Revision 1.9 2005/02/05 12:30:08 michael
  261. + Fixed bug report from Ales Katona
  262. Revision 1.8 2005/01/14 17:55:07 michael
  263. + Added unix man page output; Implemented usage
  264. Revision 1.7 2005/01/12 21:11:41 michael
  265. + New structure for writers. Implemented TXT writer
  266. Revision 1.6 2005/01/09 15:59:50 michael
  267. + Split out latex writer to linear and latex writer
  268. Revision 1.5 2004/08/28 18:03:23 michael
  269. + Added warning if docnode not found (option --warn-no-node
  270. Revision 1.4 2003/10/08 11:41:54 yuri
  271. + Initial OS/2 IPF support added
  272. Revision 1.3 2003/03/27 17:14:13 sg
  273. * Added --ostarget and --cputarget
  274. Revision 1.2 2003/03/18 19:28:44 michael
  275. + Some changes to output handling, more suitable for tex output
  276. Revision 1.1 2003/03/17 23:03:20 michael
  277. + Initial import in CVS
  278. Revision 1.13 2003/03/13 22:02:13 sg
  279. * New version with many bugfixes and our own parser (now independent of the
  280. compiler source)
  281. Revision 1.12 2002/10/12 17:09:45 michael
  282. + Added check for package name
  283. Revision 1.11 2002/05/24 00:13:22 sg
  284. * much improved new version, including many linking and output fixes
  285. Revision 1.10 2002/03/12 10:58:36 sg
  286. * reworked linking engine and internal structure
  287. Revision 1.9 2002/01/08 13:00:06 michael
  288. + Added correct array handling and syntax highlighting is now optional
  289. Revision 1.8 2001/12/17 23:24:11 sg
  290. * Added "--package" switch
  291. * Now uses translation files written in lower-case
  292. Revision 1.7 2001/07/27 12:17:20 sg
  293. * Added "--html-search" command line argument
  294. Revision 1.6 2001/07/27 10:21:42 sg
  295. * Just a new, improved version ;)
  296. (detailed changelogs will be provided again with the next commits)
  297. }