script.pas 5.8 KB

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