fpcsubst.pp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. SWarningDeprecated = 'Warning: This utility is deprecated and will be removed from fpc in the future. Please use fpcmkcfg instead.';
  29. Var
  30. List : TStringList;
  31. InputFileName : String;
  32. OutputFileName : String;
  33. Verbose : Boolean;
  34. SkipBackup : Boolean;
  35. procedure Init;
  36. begin
  37. Verbose:=False;
  38. List:=TStringList.Create;
  39. AddToList(List,'FPCVERSION',BuildVersion);
  40. AddToList(List,'FPCTARGET',BuildTarget);
  41. AddToList(List,'PWD',GetCurrentDir);
  42. AddToList(List,'BUILDDATE',DateToStr(Date));
  43. AddToList(List,'BUILDTIME',TimeToStr(Time));
  44. end;
  45. Procedure Done;
  46. begin
  47. FreeAndNil(List);
  48. end;
  49. Procedure Usage;
  50. begin
  51. Writeln(Format(SUsage00,[ExtractFileName(Paramstr(0))]));
  52. Writeln(SUsage10);
  53. Writeln(SUsage20);
  54. Writeln(SUsage30);
  55. Writeln(SUsage40);
  56. Writeln(SUsage50);
  57. Writeln(SUsage60);
  58. Writeln(SUsage70);
  59. Writeln(SUsage80);
  60. Writeln(SUsage90);
  61. Halt(1);
  62. end;
  63. Procedure ShowBuiltIns;
  64. var
  65. I : Integer;
  66. begin
  67. for I:=0 to List.Count-1 do
  68. Writeln(List[i]);
  69. end;
  70. Procedure AddFromFile(FN : String);
  71. Var
  72. F : Text;
  73. S : String;
  74. begin
  75. If Not FileExists(FN) then
  76. begin
  77. Writeln(StdErr,SWarnIgnoringFile,FN);
  78. Exit;
  79. end;
  80. Assign(F,FN);
  81. Reset(F);
  82. Try
  83. While not EOF(F) do
  84. begin
  85. ReadLn(F,S);
  86. If (Length(S)>0) and (not (S[1] in ['#',';'])) then
  87. If not AddPair(List,S) then
  88. If Verbose then
  89. Writeln(StdErr,SWarnIgnoringPair,S)
  90. end;
  91. finally
  92. Close(F);
  93. end;
  94. end;
  95. Procedure UnknownOption(Const S : String);
  96. begin
  97. Writeln(SErrUnknownOption,S);
  98. Usage;
  99. end;
  100. Procedure ProcessCommandline;
  101. Var
  102. I : Integer;
  103. S : String;
  104. Function GetOptArg : String;
  105. begin
  106. If I=ParamCount then
  107. begin
  108. Writeln(StdErr,Format(SErrArgExpected,[S]));
  109. Halt(1);
  110. end;
  111. inc(I);
  112. Result:=ParamStr(I);
  113. end;
  114. begin
  115. I:=1;
  116. While( I<=ParamCount) do
  117. begin
  118. S:=Paramstr(i);
  119. If (Length(S)<=1) or (S[1]<>'-') then
  120. UnknownOption(S)
  121. else
  122. case S[2] of
  123. 'v' : Verbose:=True;
  124. 'h' : Usage;
  125. 'b' : begin
  126. ShowBuiltins;
  127. halt(0);
  128. end;
  129. 'l' : AddFromFile(GetOptArg);
  130. 'd' : AddPair(List,GetOptArg);
  131. 'u' : AddPair(List,GetOptArg+'=');
  132. 'i' : InputFileName:=GetOptArg;
  133. 'o' : OutputFileName:=GetoptArg;
  134. 's' : SkipBackup:=True;
  135. else
  136. UnknownOption(S);
  137. end;
  138. Inc(I);
  139. end;
  140. end;
  141. Procedure DoFile;
  142. Var
  143. Fin,Fout : Text;
  144. S,BFN : String;
  145. N,LCount,RCount : Integer;
  146. begin
  147. If (InputFileName<>'') and not FileExists(InputFIleName) then
  148. begin
  149. Writeln(StdErr,Format(SErrNoSuchFile,[InputFileName]));
  150. Halt(1)
  151. end;
  152. If (OutputFileName<>'')
  153. and FileExists(OutputFileName)
  154. and not SkipBackup then
  155. begin
  156. BFN:=ChangeFileExt(OutputFileName,'.bak');
  157. If FileExists(BFN) and not DeleteFile(BFN) then
  158. begin
  159. Writeln(StdErr,Format(SErrDelBackupFailed,[BFN]));
  160. Halt(1);
  161. end;
  162. If not RenameFile(OutputFileName,BFN) then
  163. begin
  164. Writeln(StdErr,Format(SErrBackupFailed,[OutputFileName,BFN]));
  165. Halt(1);
  166. end;
  167. end;
  168. Assign(Fin,InputFileName);
  169. Assign(Fout,OutputFileName);
  170. Reset(Fin);
  171. Try
  172. Rewrite(FOut);
  173. Try
  174. LCount:=0;
  175. RCount:=0;
  176. While Not EOF(Fin) do
  177. begin
  178. Inc(LCount);
  179. ReadLn(Fin,S);
  180. N:=DoSubstitutions(List,S);
  181. If Verbose and (N>0) then
  182. Writeln(StdErr,Format(SSubstInLine,[N,LCount]));
  183. Inc(RCount,N);
  184. Writeln(Fout,S);
  185. end;
  186. If Verbose then
  187. Writeln(StdErr,Format(SStats,[RCount,LCount]));
  188. Finally
  189. Close(Fout);
  190. end;
  191. Finally
  192. Close(Fin);
  193. end;
  194. end;
  195. begin
  196. WriteLn(StdErr,SWarningDeprecated);
  197. Init;
  198. Try
  199. ProcessCommandLine;
  200. DoFile;
  201. Finally
  202. Done;
  203. end;
  204. end.