fpmc.pp 4.8 KB

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