script.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit handles the writing of script files
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  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. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit Script;
  19. interface
  20. uses
  21. CObjects;
  22. type
  23. PScript=^TScript;
  24. TScript=object
  25. fn : string[80];
  26. data : TStringQueue;
  27. executable : boolean;
  28. constructor Init(const s:string);
  29. constructor InitExec(const s:string);
  30. destructor Done;
  31. procedure AddStart(const s:string);
  32. procedure Add(const s:string);
  33. Function Empty:boolean;
  34. procedure WriteToDisk;virtual;
  35. end;
  36. PAsmScript = ^TAsmScript;
  37. TAsmScript = Object (TScript)
  38. Constructor Init (Const ScriptName : String);
  39. Procedure AddAsmCommand (Const Command, Options,FileName : String);
  40. Procedure AddLinkCommand (Const Command, Options, FileName : String);
  41. Procedure AddDeleteCommand (Const FileName : String);
  42. Procedure WriteToDisk;virtual;
  43. end;
  44. PLinkRes = ^TLinkRes;
  45. TLinkRes = Object (TScript)
  46. procedure Add(const s:string);
  47. procedure AddFileName(const s:string);
  48. end;
  49. var
  50. AsmRes : TAsmScript;
  51. LinkRes : TLinkRes;
  52. implementation
  53. uses
  54. {$ifdef linux}
  55. linux,
  56. {$endif}
  57. globals,systems;
  58. {****************************************************************************
  59. TScript
  60. ****************************************************************************}
  61. constructor TScript.Init(const s:string);
  62. begin
  63. fn:=FixFileName(s);
  64. executable:=false;
  65. data.Init;
  66. end;
  67. constructor TScript.InitExec(const s:string);
  68. begin
  69. fn:=FixFileName(s)+source_os.scriptext;
  70. executable:=true;
  71. data.Init;
  72. end;
  73. destructor TScript.Done;
  74. begin
  75. data.done;
  76. end;
  77. procedure TScript.AddStart(const s:string);
  78. begin
  79. data.Insert(s);
  80. end;
  81. procedure TScript.Add(const s:string);
  82. begin
  83. data.Concat(s);
  84. end;
  85. Function TScript.Empty:boolean;
  86. begin
  87. Empty:=Data.Empty;
  88. end;
  89. procedure TScript.WriteToDisk;
  90. var
  91. t : Text;
  92. begin
  93. Assign(t,fn);
  94. Rewrite(t);
  95. while not data.Empty do
  96. Writeln(t,data.Get);
  97. Close(t);
  98. {$ifdef linux}
  99. if executable then
  100. ChMod(fn,493);
  101. {$endif}
  102. end;
  103. {****************************************************************************
  104. Asm Response
  105. ****************************************************************************}
  106. Constructor TAsmScript.Init (Const ScriptName : String);
  107. begin
  108. Inherited InitExec(ScriptName);
  109. end;
  110. Procedure TAsmScript.AddAsmCommand (Const Command, Options,FileName : String);
  111. begin
  112. {$ifdef linux}
  113. if FileName<>'' then
  114. Add('echo Assembling '+FileName);
  115. Add (Command+' '+Options);
  116. Add('if [ $? != 0 ]; then DoExitAsm '+FileName+'; fi');
  117. {$else}
  118. if FileName<>'' then
  119. begin
  120. Add('SET THEFILE='+FileName);
  121. Add('echo Assembling %THEFILE%');
  122. end;
  123. Add(command+' '+Options);
  124. Add('if errorlevel 1 goto asmend');
  125. {$endif}
  126. end;
  127. Procedure TasmScript.AddLinkCommand (Const Command, Options, FileName : String);
  128. begin
  129. {$ifdef linux}
  130. if FileName<>'' then
  131. Add('echo Linking '+FileName);
  132. Add (Command+' '+Options);
  133. Add('if [ $? != 0 ]; then DoExitLink '+FileName+'; fi');
  134. {$else}
  135. if FileName<>'' then
  136. begin
  137. Add('SET THEFILE='+FileName);
  138. Add('echo Linking %THEFILE%');
  139. end;
  140. Add (Command+' '+Options);
  141. Add('if errorlevel 1 goto linkend');
  142. {$endif}
  143. end;
  144. Procedure TAsmScript.AddDeleteCommand (Const FileName : String);
  145. begin
  146. {$ifdef linux}
  147. Add('rm '+FileName);
  148. {$else}
  149. Add('Del '+FileName);
  150. {$endif}
  151. end;
  152. Procedure TAsmScript.WriteToDisk;
  153. Begin
  154. {$ifdef linux}
  155. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  156. AddStart('DoExitLink ()');
  157. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  158. AddStart('DoExitAsm ()');
  159. AddStart('#!/bin/sh');
  160. {$else}
  161. AddStart('@echo off');
  162. Add('goto end');
  163. Add(':asmend');
  164. Add('echo An error occured while assembling %THEFILE%');
  165. Add('goto end');
  166. Add(':linkend');
  167. Add('echo An error occured while linking %THEFILE%');
  168. Add(':end');
  169. {$endif}
  170. inherited WriteToDisk;
  171. end;
  172. {****************************************************************************
  173. Link Response
  174. ****************************************************************************}
  175. procedure TLinkRes.Add(const s:string);
  176. begin
  177. if s<>'' then
  178. inherited Add(s);
  179. end;
  180. procedure TLinkRes.AddFileName(const s:string);
  181. begin
  182. if s<>'' then
  183. begin
  184. if not(s[1] in ['a'..'z','A'..'Z','/','\','.']) then
  185. inherited Add('.'+DirSep+s)
  186. else
  187. inherited Add(s);
  188. end;
  189. end;
  190. end.
  191. {
  192. $Log$
  193. Revision 1.6 2000-02-09 13:23:04 peter
  194. * log truncated
  195. Revision 1.5 2000/02/07 11:52:26 michael
  196. + Changed bash to sh
  197. Revision 1.4 2000/01/07 01:14:39 peter
  198. * updated copyright to 2000
  199. Revision 1.3 1999/10/21 14:29:37 peter
  200. * redesigned linker object
  201. + library support for linux (only procedures can be exported)
  202. }