fpcsubst.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. {$Mode objfpc}
  2. {$H+}
  3. program fpcsubst;
  4. uses SysUtils,Classes,Usubst;
  5. Const
  6. BuildVersion={$I %FPCVERSION%};
  7. BuildTarget={$I %FPCTARGET%};
  8. Resourcestring
  9. SUsage00 = 'Usage: %s [options]';
  10. SUsage10 = 'Where options is one or more of';
  11. SUSage20 = ' -i filename Set input file. Default is standard input';
  12. SUSage30 = ' -o filename Set output file. Default is standard output.';
  13. SUsage40 = ' -d name=value define name=value pair.';
  14. SUsage50 = ' -h show this help and exit.';
  15. SUsage60 = ' -u name remove name from list of name/value pairs.';
  16. SUsage70 = ' -l filename read name/value pairs from filename';
  17. SUsage80 = ' -b show builtin list and exit.';
  18. SUsage90 = ' -v be verbose.';
  19. SErrUnknownOption = 'Error: Unknown option.';
  20. SErrArgExpected = 'Error: Option "%s" requires an argument.';
  21. SErrNoSuchFile = 'Error: File "%s" does not exist.';
  22. SErrBackupFailed = 'Error: Backup of file "%s" to "%s" failed.';
  23. SErrDelBackupFailed = 'Error: Delete of old backup file "%s" failed.';
  24. SWarnIgnoringFile = 'Warning: Ignoring non-existent file: ';
  25. SWarnIgnoringPair = 'Warning: ignoring wrong name/value pair: ';
  26. SStats = 'Replaced %d placeholders in %d lines.';
  27. SSubstInLine = 'Replaced %s placeholders in line %d.';
  28. Var
  29. List : TStringList;
  30. InputFileName : String;
  31. OutputFileName : String;
  32. Verbose : Boolean;
  33. SkipBackup : Boolean;
  34. procedure Init;
  35. begin
  36. Verbose:=False;
  37. List:=TStringList.Create;
  38. AddToList(List,'FPCVERSION',BuildVersion);
  39. AddToList(List,'FPCTARGET',BuildTarget);
  40. AddToList(List,'PWD',GetCurrentDir);
  41. AddToList(List,'BUILDDATE',DateToStr(Date));
  42. AddToList(List,'BUILDTIME',TimeToStr(Time));
  43. end;
  44. Procedure Done;
  45. begin
  46. FreeAndNil(List);
  47. end;
  48. Procedure Usage;
  49. begin
  50. Writeln(Format(SUsage00,[ExtractFileName(Paramstr(0))]));
  51. Writeln(SUsage10);
  52. Writeln(SUsage20);
  53. Writeln(SUsage30);
  54. Writeln(SUsage40);
  55. Writeln(SUsage50);
  56. Writeln(SUsage60);
  57. Writeln(SUsage70);
  58. Writeln(SUsage80);
  59. Writeln(SUsage90);
  60. Halt(1);
  61. end;
  62. Procedure ShowBuiltIns;
  63. var
  64. I : Integer;
  65. begin
  66. for I:=0 to List.Count-1 do
  67. Writeln(List[i]);
  68. end;
  69. Procedure AddFromFile(FN : String);
  70. Var
  71. F : Text;
  72. S : String;
  73. begin
  74. If Not FileExists(FN) then
  75. begin
  76. Writeln(StdErr,SWarnIgnoringFile,FN);
  77. Exit;
  78. end;
  79. Assign(F,FN);
  80. Reset(F);
  81. Try
  82. While not EOF(F) do
  83. begin
  84. ReadLn(F,S);
  85. If (Length(S)>0) and (not (S[1] in ['#',';'])) then
  86. If not AddPair(List,S) then
  87. If Verbose then
  88. Writeln(StdErr,SWarnIgnoringPair,S)
  89. end;
  90. finally
  91. Close(F);
  92. end;
  93. end;
  94. Procedure UnknownOption(Const S : String);
  95. begin
  96. Writeln(SErrUnknownOption,S);
  97. Usage;
  98. end;
  99. Procedure ProcessCommandline;
  100. Var
  101. I : Integer;
  102. S : String;
  103. Function GetOptArg : String;
  104. begin
  105. If I=ParamCount then
  106. begin
  107. Writeln(StdErr,Format(SErrArgExpected,[S]));
  108. Halt(1);
  109. end;
  110. inc(I);
  111. Result:=ParamStr(I);
  112. end;
  113. begin
  114. I:=1;
  115. While( I<=ParamCount) do
  116. begin
  117. S:=Paramstr(i);
  118. If (Length(S)<=1) or (S[1]<>'-') then
  119. UnknownOption(S)
  120. else
  121. case S[2] of
  122. 'v' : Verbose:=True;
  123. 'h' : Usage;
  124. 'b' : begin
  125. ShowBuiltins;
  126. halt(0);
  127. end;
  128. 'l' : AddFromFile(GetOptArg);
  129. 'd' : AddPair(List,GetOptArg);
  130. 'u' : AddPair(List,GetOptArg+'=');
  131. 'i' : InputFileName:=GetOptArg;
  132. 'o' : OutputFileName:=GetoptArg;
  133. 's' : SkipBackup:=True;
  134. else
  135. UnknownOption(S);
  136. end;
  137. Inc(I);
  138. end;
  139. end;
  140. Procedure DoFile;
  141. Var
  142. Fin,Fout : Text;
  143. S,BFN : String;
  144. N,LCount,RCount : Integer;
  145. begin
  146. If (InputFileName<>'') and not FileExists(InputFIleName) then
  147. begin
  148. Writeln(StdErr,Format(SErrNoSuchFile,[InputFileName]));
  149. Halt(1)
  150. end;
  151. If (OutputFileName<>'')
  152. and FileExists(OutputFileName)
  153. and not SkipBackup then
  154. begin
  155. BFN:=ChangeFileExt(OutputFileName,'.bak');
  156. If FileExists(BFN) and not DeleteFile(BFN) then
  157. begin
  158. Writeln(StdErr,Format(SErrDelBackupFailed,[BFN]));
  159. Halt(1);
  160. end;
  161. If not RenameFile(OutputFileName,BFN) then
  162. begin
  163. Writeln(StdErr,Format(SErrBackupFailed,[OutputFileName,BFN]));
  164. Halt(1);
  165. end;
  166. end;
  167. Assign(Fin,InputFileName);
  168. Assign(Fout,OutputFileName);
  169. Reset(Fin);
  170. Try
  171. Rewrite(FOut);
  172. Try
  173. LCount:=0;
  174. RCount:=0;
  175. While Not EOF(Fin) do
  176. begin
  177. Inc(LCount);
  178. ReadLn(Fin,S);
  179. N:=DoSubstitutions(List,S);
  180. If Verbose and (N>0) then
  181. Writeln(StdErr,Format(SSubstInLine,[N,LCount]));
  182. Inc(RCount,N);
  183. Writeln(Fout,S);
  184. end;
  185. If Verbose then
  186. Writeln(StdErr,Format(SStats,[RCount,LCount]));
  187. Finally
  188. Close(Fout);
  189. end;
  190. Finally
  191. Close(Fin);
  192. end;
  193. end;
  194. begin
  195. Init;
  196. Try
  197. ProcessCommandLine;
  198. DoFile;
  199. Finally
  200. Done;
  201. end;
  202. end.