script.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. {$i defines.inc}
  20. interface
  21. uses
  22. cclasses;
  23. type
  24. PScript=^TScript;
  25. TScript=object
  26. fn : string[80];
  27. data : TStringList;
  28. executable : boolean;
  29. constructor Init(const s:string);
  30. constructor InitExec(const s:string);
  31. destructor Done;
  32. procedure AddStart(const s:string);
  33. procedure Add(const s:string);
  34. Function Empty:boolean;
  35. procedure WriteToDisk;virtual;
  36. end;
  37. PAsmScript = ^TAsmScript;
  38. TAsmScript = Object (TScript)
  39. Constructor Init (Const ScriptName : String);
  40. Procedure AddAsmCommand (Const Command, Options,FileName : String);
  41. Procedure AddLinkCommand (Const Command, Options, FileName : String);
  42. Procedure AddDeleteCommand (Const FileName : String);
  43. Procedure WriteToDisk;virtual;
  44. end;
  45. PLinkRes = ^TLinkRes;
  46. TLinkRes = Object (TScript)
  47. procedure Add(const s:string);
  48. procedure AddFileName(const s:string);
  49. end;
  50. var
  51. AsmRes : TAsmScript;
  52. LinkRes : TLinkRes;
  53. implementation
  54. uses
  55. {$ifdef Unix}
  56. linux,
  57. {$endif}
  58. globals,systems;
  59. {****************************************************************************
  60. TScript
  61. ****************************************************************************}
  62. constructor TScript.Init(const s:string);
  63. begin
  64. fn:=FixFileName(s);
  65. executable:=false;
  66. data:=TStringList.Create;
  67. end;
  68. constructor TScript.InitExec(const s:string);
  69. begin
  70. fn:=FixFileName(s)+source_os.scriptext;
  71. executable:=true;
  72. data:=TStringList.Create;
  73. end;
  74. destructor TScript.Done;
  75. begin
  76. data.Free;
  77. end;
  78. procedure TScript.AddStart(const s:string);
  79. begin
  80. data.Insert(s);
  81. end;
  82. procedure TScript.Add(const s:string);
  83. begin
  84. data.Concat(s);
  85. end;
  86. Function TScript.Empty:boolean;
  87. begin
  88. Empty:=Data.Empty;
  89. end;
  90. procedure TScript.WriteToDisk;
  91. var
  92. t : Text;
  93. begin
  94. Assign(t,fn);
  95. Rewrite(t);
  96. while not data.Empty do
  97. Writeln(t,data.GetFirst);
  98. Close(t);
  99. {$ifdef Unix}
  100. if executable then
  101. ChMod(fn,493);
  102. {$endif}
  103. end;
  104. {****************************************************************************
  105. Asm Response
  106. ****************************************************************************}
  107. Constructor TAsmScript.Init (Const ScriptName : String);
  108. begin
  109. Inherited InitExec(ScriptName);
  110. end;
  111. Procedure TAsmScript.AddAsmCommand (Const Command, Options,FileName : String);
  112. begin
  113. {$ifdef Unix}
  114. if FileName<>'' then
  115. Add('echo Assembling '+FileName);
  116. Add (Command+' '+Options);
  117. Add('if [ $? != 0 ]; then DoExitAsm '+FileName+'; fi');
  118. {$else}
  119. if FileName<>'' then
  120. begin
  121. Add('SET THEFILE='+FileName);
  122. Add('echo Assembling %THEFILE%');
  123. end;
  124. Add(command+' '+Options);
  125. Add('if errorlevel 1 goto asmend');
  126. {$endif}
  127. end;
  128. Procedure TasmScript.AddLinkCommand (Const Command, Options, FileName : String);
  129. begin
  130. {$ifdef Unix}
  131. if FileName<>'' then
  132. Add('echo Linking '+FileName);
  133. Add (Command+' '+Options);
  134. Add('if [ $? != 0 ]; then DoExitLink '+FileName+'; fi');
  135. {$else}
  136. if FileName<>'' then
  137. begin
  138. Add('SET THEFILE='+FileName);
  139. Add('echo Linking %THEFILE%');
  140. end;
  141. Add (Command+' '+Options);
  142. Add('if errorlevel 1 goto linkend');
  143. {$endif}
  144. end;
  145. Procedure TAsmScript.AddDeleteCommand (Const FileName : String);
  146. begin
  147. {$ifdef Unix}
  148. Add('rm '+FileName);
  149. {$else}
  150. Add('Del '+FileName);
  151. {$endif}
  152. end;
  153. Procedure TAsmScript.WriteToDisk;
  154. Begin
  155. {$ifdef Unix}
  156. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  157. AddStart('DoExitLink ()');
  158. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  159. AddStart('DoExitAsm ()');
  160. AddStart('#!/bin/sh');
  161. {$else}
  162. AddStart('@echo off');
  163. Add('goto end');
  164. Add(':asmend');
  165. Add('echo An error occured while assembling %THEFILE%');
  166. Add('goto end');
  167. Add(':linkend');
  168. Add('echo An error occured while linking %THEFILE%');
  169. Add(':end');
  170. {$endif}
  171. inherited WriteToDisk;
  172. end;
  173. {****************************************************************************
  174. Link Response
  175. ****************************************************************************}
  176. procedure TLinkRes.Add(const s:string);
  177. begin
  178. if s<>'' then
  179. inherited Add(s);
  180. end;
  181. procedure TLinkRes.AddFileName(const s:string);
  182. begin
  183. if s<>'' then
  184. begin
  185. if not(s[1] in ['a'..'z','A'..'Z','/','\','.']) then
  186. inherited Add('.'+DirSep+s)
  187. else
  188. inherited Add(s);
  189. end;
  190. end;
  191. end.
  192. {
  193. $Log$
  194. Revision 1.5 2000-12-25 00:07:29 peter
  195. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  196. tlinkedlist objects)
  197. Revision 1.4 2000/11/13 15:43:07 marco
  198. * Renamefest
  199. Revision 1.3 2000/09/24 15:06:28 peter
  200. * use defines.inc
  201. Revision 1.2 2000/07/13 11:32:49 michael
  202. + removed logs
  203. }