fpmc.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. Free Pascal Message Compiler (command-line version)
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. program fpmc;
  14. uses msgcomp,getopts,sysutils,classes;
  15. Type
  16. TFPMC = Class (TObject)
  17. CmdUnitName,
  18. InputFileName,
  19. MsgFileName,
  20. PascalFileName,
  21. RCFileName : String;
  22. LanguageID,
  23. SubLanguageID : Integer;
  24. CmdVerbose,
  25. CmdUseEscape : Boolean;
  26. Procedure DoError(Sender : TObject; Msg : String);
  27. Procedure DoVerbose(Sender : TObject; Msg : String);
  28. Procedure Usage(WithError : Boolean);
  29. Procedure HandleOptions;
  30. Procedure CompileFile;
  31. end;
  32. Procedure TFPMC.DoError(Sender : TObject; Msg : String);
  33. begin
  34. Writeln(StdErr,Msg);
  35. end;
  36. Procedure TFPMC.DoVerbose(Sender : TObject; Msg : String);
  37. begin
  38. Writeln(Msg);
  39. end;
  40. Procedure TFPMC.Usage(WithError : Boolean);
  41. begin
  42. Writeln('Usage : ',ExtractFileName(Paramstr(0)),' <option>');
  43. Writeln('Where options is one or more of :');
  44. Writeln(' -a Produce all files (-m -p -r)');
  45. Writeln(' -e Do not escape backslashes in filenames');
  46. Writeln(' -h Usage screen');
  47. Writeln(' -i filename Input filename');
  48. Writeln(' -l ID Set locale (language ID)');
  49. Writeln(' -m [msgfile] Create message file.');
  50. Writeln(' -p [pasfile] Create pascal unit from message aliases.');
  51. Writeln(' -r [rcfile] Create .RC file for use with message file');
  52. Writeln(' -s ID Set sublocale (sublanguage)');
  53. Writeln(' -u name Set unitname');
  54. Writeln(' -v Be verbose');
  55. Writeln('Names of output files are deduced from input filename if needed.');
  56. Halt(Ord(WithError));
  57. end;
  58. Procedure TFPMC.Handleoptions;
  59. Var
  60. C : Char;
  61. NeedPasFileName,
  62. NeedMsgFileName,
  63. NeedRCFileName : Boolean;
  64. begin
  65. NeedPasFileName:=False;
  66. NeedMsgFileName:=False;
  67. NeedRCFileName :=False;
  68. CmdUnitName:='';
  69. LanguageID:=-1;
  70. SubLanguageID:=-1;
  71. CmdUseEscape:=True;
  72. repeat
  73. C:=GetOpt('vahei:m::l:s:u:r::p::');
  74. Case C of
  75. 'a' : begin
  76. NeedMsgFilename:=(MsgFileName='');
  77. NeedPasFilename:=(PascalFileName='');
  78. NeedRCFilename:=(RCFileName='');
  79. end;
  80. 'e' : CmdUseEscape:=False;
  81. 'h','?' : Usage(false);
  82. 'i' : InputFileName:=OptArg;
  83. 'm' : begin
  84. MsgFileName:=OptArg;
  85. NeedMsgFilename:=(MsgFileName='');
  86. end;
  87. 'l' : LanguageID:=StrToIntDef(OptArg,-1);
  88. 'p' : begin
  89. PascalFileName:=OptArg;
  90. NeedPasFilename:=(PascalFileName='');
  91. end;
  92. 's' : SubLanguageID:=StrToIntDef(OptArg,-1);
  93. 'u' : CmdUnitName:=OptArg;
  94. 'r' : begin
  95. RCFileName:=OptArg;
  96. NeedRCFilename:=(RCFileName='');
  97. end;
  98. 'v' : CmdVerbose:=True;
  99. end;
  100. Until (C=EndOfOptions);
  101. If (InputFileName='') Then
  102. Usage(true);
  103. If NeedMsgFileName then
  104. MsgFileName:=ChangeFileExt(InputFilename,'.msg');
  105. If NeedPasFileName then
  106. PascalFileName:=ChangeFileExt(InputFilename,'.pp');
  107. If NeedRCFileName then
  108. RCFileName:=ChangeFileExt(InputFilename,'.rc');
  109. If (PascalFileName<>'') and (CmdUnitName='') then
  110. CmdUnitName:=ChangeFileExt(ExtractFileName(PascalFileName),'');
  111. end;
  112. Procedure TFPMC.CompileFile;
  113. Var
  114. M,P,R,I : TStream;
  115. Procedure SetupStreams;
  116. begin
  117. I:=TFileStream.Create(InputFileName,fmOpenRead);
  118. If (PascalFileName<>'') then
  119. P:=TFileStream.Create(PascalFileName,fmCreate);
  120. If (MsgFileName<>'') then
  121. M:=TFileStream.Create(MsgFileName,fmCreate);
  122. If (RCFileName<>'') then
  123. R:=TFileStream.Create(RCFileName,fmCreate);
  124. end;
  125. Procedure CloseStreams;
  126. begin
  127. M.Free;
  128. P.Free;
  129. R.Free;
  130. I.Free;
  131. end;
  132. begin
  133. SetupStreams;
  134. Try
  135. With TMessageCompiler.Create do
  136. Try
  137. Msg:=M;
  138. MC:=I;
  139. RC:=R;
  140. Pas:=P;
  141. OnError:=@DoError;
  142. If CmdVerbose then
  143. OnVerbose:=@DoVerbose;
  144. UnitName:=CmdUnitName;
  145. MessageFileName:=MsgFileName;
  146. EscapeNeeded:=CmdUseEscape;
  147. If (LanguageID<>-1) then
  148. LocaleID:=LanguageID;
  149. If (SubLanguageID<>-1) then
  150. SubLocaleID:=SubLanguageID;
  151. Compile;
  152. Finally
  153. Free;
  154. end;
  155. Finally
  156. CloseStreams;
  157. end;
  158. end;
  159. begin
  160. With TFPMC.Create do
  161. Try
  162. HandleOptions;
  163. CompileFile;
  164. Finally
  165. Free;
  166. end;
  167. end.