script.pas 6.0 KB

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