2
0

ptop.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. Program PtoP;
  2. {
  3. $Id$
  4. This file is part of the Free Pascal run time library.
  5. Copyright (c) 1999-2002 by Michael Van Canneyt, member of
  6. the Free Pascal development team
  7. Pascal pretty print program
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. Uses PtoPu,Objects,getopts;
  15. const
  16. Version = 'Version 1.1';
  17. Title = 'DelPascal';
  18. Copyright = 'Copyright (c) 1999-2002 by the Free Pascal Development Team';
  19. Var
  20. Infilename,OutFileName,ConfigFile : String;
  21. BeVerbose : Boolean;
  22. TheIndent,TheBufSize,TheLineSize : Integer;
  23. Function StrToInt(Const S : String) : Integer;
  24. Var Code : integer;
  25. Int : integer;
  26. begin
  27. Val(S,int,Code);
  28. StrToInt := int;
  29. If Code<>0 then StrToInt:=0;
  30. end;
  31. Procedure Usage;
  32. begin
  33. Writeln ('ptop : Usage : ');
  34. Writeln ('ptop [-v] [-i indent] [-b bufsize ][-c optsfile][-l linesize] infile outfile');
  35. Writeln (' converts infile to outfile.');
  36. Writeln (' -c : read options from optsfile');
  37. Writeln (' -i : Set number of indent spaces.');
  38. Writeln (' -l : Set maximum output linesize.');
  39. Writeln (' -b : Use buffers of size bufsize');
  40. Writeln (' -v : be verbose');
  41. writeln ('ptop -g ofile');
  42. writeln (' generate default options file');
  43. Writeln ('ptop -h : This help');
  44. halt(0);
  45. end;
  46. Procedure Genopts;
  47. Var S : PBufStream;
  48. begin
  49. S:=New(PBufStream,Init(ConfigFile,stCreate,255));
  50. GeneratecfgFile(S);
  51. {$ifndef tp}
  52. S^.Close;
  53. {$endif}
  54. S^.Done;
  55. end;
  56. Procedure ProcessOpts;
  57. Var c : char;
  58. begin
  59. { Set defaults }
  60. Infilename:='';
  61. OutFileName:='';
  62. ConfigFile:='';
  63. TheIndent:=2;
  64. TheBufSize:=255;
  65. TheLineSize:=MaxLineSize;
  66. BeVerbose:=False;
  67. Repeat
  68. c:=getopt('i:c:g:l:b:hv');
  69. case c of
  70. 'i' : begin
  71. TheIndent:=StrToInt(OptArg);
  72. If TheIndent=0 then TheIndent:=2;
  73. end;
  74. 'b' : begin
  75. TheBufSize:=StrToInt(OptArg);
  76. If TheBufSize=0 then TheBufSize:=255;
  77. end;
  78. 'c' : ConfigFile:=OptArg;
  79. 'l' : begin
  80. TheLineSize:=StrToInt(OptArg);
  81. If TheLineSIze=0 Then TheLineSize:=MaxLineSize;
  82. end;
  83. 'g' : begin
  84. ConfigFIle:=OptArg;
  85. GenOpts;
  86. halt(0);
  87. end;
  88. 'h' : usage;
  89. 'v' : BeVerbose:=True;
  90. else
  91. end;
  92. until c=endofoptions;
  93. If optind<=paramcount then
  94. begin
  95. InFileName:=paramstr(OptInd);
  96. Inc(optind);
  97. If OptInd<=paramcount then
  98. OutFilename:=Paramstr(OptInd);
  99. end;
  100. end; { Of ProcessOpts }
  101. Var DiagS : PMemoryStream;
  102. InS,OutS,cfgS : PBufSTream;
  103. PPrinter : TPrettyPrinter;
  104. P : Pchar;
  105. i : longint;
  106. Procedure StreamErrorProcedure(Var S: TStream);{$ifndef fpc}FAR;{$endif}
  107. Begin
  108. If S.Status = StError then
  109. WriteLn('ERROR: General Access failure. Halting');
  110. If S.Status = StInitError then
  111. WriteLn('ERROR: Cannot Init Stream. Halting. ');
  112. If S.Status = StReadError then
  113. WriteLn('ERROR: Read beyond end of Stream. Halting');
  114. If S.Status = StWriteError then
  115. WriteLn('ERROR: Cannot expand Stream. Halting');
  116. If S.Status = StGetError then
  117. WriteLn('ERROR: Get of Unregistered type. Halting');
  118. If S.Status = StPutError then
  119. WriteLn('ERROR: Put of Unregistered type. Halting');
  120. end;
  121. begin
  122. StreamError:=@StreamErrorProcedure;
  123. ProcessOpts;
  124. if BeVerbose then
  125. begin
  126. writeln(Title+' '+Version);
  127. writeln(Copyright);
  128. Writeln;
  129. end;
  130. If (Length(InfileName)=0) or (Length(OutFileName)=0) Then
  131. Usage;
  132. Ins:=New(PBufStream,Init(InFileName,StopenRead,TheBufSize));
  133. OutS:=New(PBufStream,Init(OutFileName,StCreate,TheBufSize));
  134. If BeVerbose then
  135. diagS:=New(PMemoryStream,Init(1000,255))
  136. else
  137. DiagS:=Nil;
  138. If ConfigFile<>'' then
  139. CfgS:=New(PBufStream,Init(ConfigFile,StOpenRead,TheBufSize))
  140. else
  141. CfgS:=Nil;
  142. PPrinter.Create;
  143. PPrinter.Indent:=TheIndent;
  144. PPrinter.LineSize:=TheLineSize;
  145. PPrinter.Ins:=Ins;
  146. PPrinter.outS:=OutS;
  147. PPrinter.cfgS:=CfgS;
  148. PPrinter.DiagS:=DiagS;
  149. PPrinter.PrettyPrint;
  150. If Assigned(DiagS) then
  151. begin
  152. I:=DiagS^.GetSize;
  153. DiagS^.Seek(0);
  154. getmem (P,I+1);
  155. DiagS^.Read(P[0],I);
  156. P[I]:=#0;
  157. {$ifndef tp}
  158. Writeln (stderr,P);
  159. Flush(stderr);
  160. {$else}
  161. Writeln (P);
  162. {$endif}
  163. DiagS^.Done;
  164. end;
  165. If Assigned(CfgS) then
  166. CfgS^.Done;
  167. Ins^.Done;
  168. OutS^.Done;
  169. end.
  170. {
  171. $Log$
  172. Revision 1.3 2002-02-27 17:20:44 carl
  173. + fix for BP
  174. Revision 1.2 2002/02/27 16:33:45 carl
  175. - truncated log
  176. * bugfix of -l parameter (was never valid)
  177. + added version information
  178. }