ptop.pp 4.6 KB

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