2
0

script.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit handles the writing of script files
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit script;
  18. {$i fpcdefs.inc}
  19. interface
  20. {$H+}
  21. uses
  22. sysutils,
  23. globtype,
  24. cclasses;
  25. type
  26. TScript=class
  27. fn : TCmdStr;
  28. data : TCmdStrList;
  29. executable : boolean;
  30. constructor Create(const s:TCmdStr);
  31. constructor CreateExec(const s:TCmdStr);
  32. destructor Destroy;override;
  33. procedure AddStart(const s:TCmdStr);
  34. procedure Add(const s:TCmdStr);
  35. Function Empty:boolean;
  36. procedure WriteToDisk;virtual;
  37. end;
  38. TAsmScript = class (TScript)
  39. Constructor Create(Const ScriptName : TCmdStr); virtual;
  40. Procedure AddAsmCommand (Const Command, Options,FileName : TCmdStr);virtual;abstract;
  41. Procedure AddLinkCommand (Const Command, Options, FileName : TCmdStr);virtual;abstract;
  42. Procedure AddDeleteCommand (Const FileName : TCmdStr);virtual;abstract;
  43. Procedure AddDeleteDirCommand (Const FileName : TCmdStr);virtual;abstract;
  44. end;
  45. TAsmScriptDos = class (TAsmScript)
  46. Constructor Create (Const ScriptName : TCmdStr); override;
  47. Procedure AddAsmCommand (Const Command, Options,FileName : TCmdStr);override;
  48. Procedure AddLinkCommand (Const Command, Options, FileName : TCmdStr);override;
  49. Procedure AddDeleteCommand (Const FileName : TCmdStr);override;
  50. Procedure AddDeleteDirCommand (Const FileName : TCmdStr);override;
  51. Procedure WriteToDisk;override;
  52. end;
  53. TAsmScriptAmiga = class (TAsmScript)
  54. Constructor Create (Const ScriptName : TCmdStr); override;
  55. Procedure AddAsmCommand (Const Command, Options,FileName : TCmdStr);override;
  56. Procedure AddLinkCommand (Const Command, Options, FileName : TCmdStr);override;
  57. Procedure AddDeleteCommand (Const FileName : TCmdStr);override;
  58. Procedure AddDeleteDirCommand (Const FileName : TCmdStr);override;
  59. Procedure WriteToDisk;override;
  60. end;
  61. TAsmScriptUnix = class (TAsmScript)
  62. Constructor Create (Const ScriptName : TCmdStr);override;
  63. Procedure AddAsmCommand (Const Command, Options,FileName : TCmdStr);override;
  64. Procedure AddLinkCommand (Const Command, Options, FileName : TCmdStr);override;
  65. Procedure AddDeleteCommand (Const FileName : TCmdStr);override;
  66. Procedure AddDeleteDirCommand (Const FileName : TCmdStr);override;
  67. Procedure WriteToDisk;override;
  68. end;
  69. TAsmScriptMPW = class (TAsmScript)
  70. Constructor Create (Const ScriptName : TCmdStr); override;
  71. Procedure AddAsmCommand (Const Command, Options,FileName : TCmdStr);override;
  72. Procedure AddLinkCommand (Const Command, Options, FileName : TCmdStr);override;
  73. Procedure AddDeleteCommand (Const FileName : TCmdStr);override;
  74. Procedure AddDeleteDirCommand (Const FileName : TCmdStr);override;
  75. Procedure WriteToDisk;override;
  76. end;
  77. TLinkRes = Class (TScript)
  78. section: string[30];
  79. procedure Add(const s:TCmdStr);
  80. procedure AddFileName(const s:TCmdStr);
  81. procedure EndSection(const s:TCmdStr);
  82. procedure StartSection(const s:TCmdStr);
  83. end;
  84. var
  85. AsmRes : TAsmScript;
  86. Function ScriptFixFileName(const s:TCmdStr):TCmdStr;
  87. Procedure GenerateAsmRes(const st : TCmdStr);
  88. implementation
  89. uses
  90. {$ifdef hasUnix}
  91. BaseUnix,
  92. {$endif}
  93. cutils,cfileutl,
  94. globals,systems,verbose;
  95. {****************************************************************************
  96. Helpers
  97. ****************************************************************************}
  98. Function ScriptFixFileName(const s:TCmdStr):TCmdStr;
  99. begin
  100. if cs_link_on_target in current_settings.globalswitches then
  101. ScriptFixFileName:=TargetFixFileName(s)
  102. else
  103. ScriptFixFileName:=FixFileName(s);
  104. end;
  105. {****************************************************************************
  106. TScript
  107. ****************************************************************************}
  108. constructor TScript.Create(const s: TCmdStr);
  109. begin
  110. fn:=FixFileName(s);
  111. executable:=false;
  112. data:=TCmdStrList.Create;
  113. end;
  114. constructor TScript.CreateExec(const s:TCmdStr);
  115. begin
  116. fn:=FixFileName(s);
  117. if cs_link_on_target in current_settings.globalswitches then
  118. fn:=ChangeFileExt(fn,target_info.scriptext)
  119. else
  120. fn:=ChangeFileExt(fn,source_info.scriptext);
  121. executable:=true;
  122. data:=TCmdStrList.Create;
  123. end;
  124. destructor TScript.Destroy;
  125. begin
  126. data.Free;
  127. end;
  128. procedure TScript.AddStart(const s:TCmdStr);
  129. begin
  130. data.Insert(s);
  131. end;
  132. procedure TScript.Add(const s:TCmdStr);
  133. begin
  134. data.Concat(s);
  135. end;
  136. Function TScript.Empty:boolean;
  137. begin
  138. Empty:=Data.Empty;
  139. end;
  140. procedure TScript.WriteToDisk;
  141. var
  142. t : file;
  143. i : longint;
  144. s : TCmdStr;
  145. le: string[2];
  146. begin
  147. Assign(t,fn);
  148. if cs_link_on_target in current_settings.globalswitches then
  149. le:= target_info.newline
  150. else
  151. le:= source_info.newline;
  152. {$push}{$I-}
  153. Rewrite(t,1);
  154. if ioresult<>0 then
  155. exit;
  156. while not data.Empty do
  157. begin
  158. s:=data.GetFirst;
  159. Blockwrite(t,s[1],length(s),i);
  160. Blockwrite(t,le[1],length(le),i);
  161. end;
  162. Close(t);
  163. {$pop}
  164. i:=ioresult;
  165. {$ifdef hasUnix}
  166. if executable then
  167. fpchmod(fn,493);
  168. {$endif}
  169. end;
  170. {****************************************************************************
  171. Asm Response
  172. ****************************************************************************}
  173. Constructor TAsmScript.Create (Const ScriptName : TCmdStr);
  174. begin
  175. Inherited CreateExec(ScriptName);
  176. end;
  177. {****************************************************************************
  178. DOS Asm Response
  179. ****************************************************************************}
  180. Constructor TAsmScriptDos.Create (Const ScriptName : TCmdStr);
  181. begin
  182. Inherited Create(ScriptName);
  183. end;
  184. Procedure TAsmScriptDos.AddAsmCommand (Const Command, Options,FileName : TCmdStr);
  185. begin
  186. if FileName<>'' then
  187. begin
  188. Add('SET THEFILE='+ScriptFixFileName(FileName));
  189. Add('echo Assembling %THEFILE%');
  190. end;
  191. Add(maybequoted(command)+' '+Options);
  192. Add('if errorlevel 1 goto asmend');
  193. end;
  194. Procedure TAsmScriptDos.AddLinkCommand (Const Command, Options, FileName : TCmdStr);
  195. begin
  196. if FileName<>'' then
  197. begin
  198. Add('SET THEFILE='+ScriptFixFileName(FileName));
  199. Add('echo Linking %THEFILE%');
  200. end;
  201. Add(maybequoted(command)+' '+Options);
  202. Add('if errorlevel 1 goto linkend');
  203. end;
  204. Procedure TAsmScriptDos.AddDeleteCommand (Const FileName : TCmdStr);
  205. begin
  206. Add('Del ' + MaybeQuoted (ScriptFixFileName (FileName)));
  207. end;
  208. Procedure TAsmScriptDos.AddDeleteDirCommand (Const FileName : TCmdStr);
  209. begin
  210. Add('Rmdir ' + MaybeQuoted (ScriptFixFileName (FileName)));
  211. end;
  212. Procedure TAsmScriptDos.WriteToDisk;
  213. Begin
  214. AddStart('@echo off');
  215. Add('goto end');
  216. Add(':asmend');
  217. Add('echo An error occured while assembling %THEFILE%');
  218. Add('goto end');
  219. Add(':linkend');
  220. Add('echo An error occured while linking %THEFILE%');
  221. Add(':end');
  222. inherited WriteToDisk;
  223. end;
  224. {****************************************************************************
  225. Amiga Asm Response
  226. ****************************************************************************}
  227. Constructor TAsmScriptAmiga.Create (Const ScriptName : TCmdStr);
  228. begin
  229. Inherited Create(ScriptName);
  230. end;
  231. Procedure TAsmScriptAmiga.AddAsmCommand (Const Command, Options,FileName : TCmdStr);
  232. begin
  233. if FileName<>'' then
  234. begin
  235. Add('SET THEFILE '+ScriptFixFileName(FileName));
  236. Add('echo Assembling $THEFILE');
  237. end;
  238. Add(maybequoted(command)+' '+Options);
  239. { There is a problem here,
  240. as always return with a non zero error value PM }
  241. Add('if error');
  242. Add('why');
  243. Add('skip asmend');
  244. Add('endif');
  245. end;
  246. Procedure TAsmScriptAmiga.AddLinkCommand (Const Command, Options, FileName : TCmdStr);
  247. begin
  248. if FileName<>'' then
  249. begin
  250. Add('SET THEFILE '+ScriptFixFileName(FileName));
  251. Add('echo Linking $THEFILE');
  252. end;
  253. Add(maybequoted(command)+' '+Options);
  254. Add('if error');
  255. Add('skip linkend');
  256. Add('endif');
  257. end;
  258. Procedure TAsmScriptAmiga.AddDeleteCommand (Const FileName : TCmdStr);
  259. begin
  260. Add('Delete ' + Unix2AmigaPath(MaybeQuoted(ScriptFixFileName(FileName))) + ' Quiet');
  261. end;
  262. Procedure TAsmScriptAmiga.AddDeleteDirCommand (Const FileName : TCmdStr);
  263. begin
  264. Add('Delete ' + Unix2AmigaPath(MaybeQuoted(ScriptFixFileName(FileName))) + ' All Quiet');
  265. end;
  266. Procedure TAsmScriptAmiga.WriteToDisk;
  267. Begin
  268. Add('skip end');
  269. Add('lab asmend');
  270. Add('why');
  271. Add('echo An error occured while assembling $THEFILE');
  272. Add('skip end');
  273. Add('lab linkend');
  274. Add('why');
  275. Add('echo An error occured while linking $THEFILE');
  276. Add('lab end');
  277. inherited WriteToDisk;
  278. end;
  279. {****************************************************************************
  280. Unix Asm Response
  281. ****************************************************************************}
  282. Constructor TAsmScriptUnix.Create (Const ScriptName : TCmdStr);
  283. begin
  284. Inherited Create(ScriptName);
  285. end;
  286. Procedure TAsmScriptUnix.AddAsmCommand (Const Command, Options,FileName : TCmdStr);
  287. begin
  288. if FileName<>'' then
  289. Add('echo Assembling '+ScriptFixFileName(FileName));
  290. Add(maybequoted(command)+' '+Options);
  291. Add('if [ $? != 0 ]; then DoExitAsm '+ScriptFixFileName(FileName)+'; fi');
  292. end;
  293. Procedure TAsmScriptUnix.AddLinkCommand (Const Command, Options, FileName : TCmdStr);
  294. begin
  295. if FileName<>'' then
  296. Add('echo Linking '+ScriptFixFileName(FileName));
  297. Add('OFS=$IFS');
  298. Add('IFS="');
  299. Add('"');
  300. Add(maybequoted(command)+' '+Options);
  301. Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(FileName)+'; fi');
  302. Add('IFS=$OFS');
  303. end;
  304. Procedure TAsmScriptUnix.AddDeleteCommand (Const FileName : TCmdStr);
  305. begin
  306. Add('rm ' + MaybeQuoted (ScriptFixFileName(FileName)));
  307. end;
  308. Procedure TAsmScriptUnix.AddDeleteDirCommand (Const FileName : TCmdStr);
  309. begin
  310. Add('rmdir ' + MaybeQuoted (ScriptFixFileName(FileName)));
  311. end;
  312. Procedure TAsmScriptUnix.WriteToDisk;
  313. Begin
  314. AddStart('{ echo "An error occurred while linking $1"; exit 1; }');
  315. AddStart('DoExitLink ()');
  316. AddStart('{ echo "An error occurred while assembling $1"; exit 1; }');
  317. AddStart('DoExitAsm ()');
  318. {$ifdef BEOS}
  319. AddStart('#!/boot/beos/bin/sh');
  320. {$else}
  321. AddStart('#!/bin/sh');
  322. {$endif}
  323. inherited WriteToDisk;
  324. end;
  325. {****************************************************************************
  326. MPW (MacOS) Asm Response
  327. ****************************************************************************}
  328. Constructor TAsmScriptMPW.Create (Const ScriptName : TCmdStr);
  329. begin
  330. Inherited Create(ScriptName);
  331. end;
  332. Procedure TAsmScriptMPW.AddAsmCommand (Const Command, Options,FileName : TCmdStr);
  333. begin
  334. if FileName<>'' then
  335. Add('Echo Assembling '+ScriptFixFileName(FileName));
  336. Add(maybequoted(command)+' '+Options);
  337. Add('Exit If "{Status}" != 0');
  338. end;
  339. Procedure TAsmScriptMPW.AddLinkCommand (Const Command, Options, FileName : TCmdStr);
  340. begin
  341. if FileName<>'' then
  342. Add('Echo Linking '+ScriptFixFileName(FileName));
  343. Add(maybequoted(command)+' '+Options);
  344. Add('Exit If "{Status}" != 0');
  345. {Add resources}
  346. if apptype = app_cui then {If SIOW}
  347. begin
  348. Add('Rez -append "{RIncludes}"SIOW.r -o '+ ScriptFixFileName(FileName));
  349. Add('Exit If "{Status}" != 0');
  350. end;
  351. end;
  352. Procedure TAsmScriptMPW.AddDeleteCommand (Const FileName : TCmdStr);
  353. begin
  354. Add('Delete ' + MaybeQuoted (ScriptFixFileName(FileName)));
  355. end;
  356. Procedure TAsmScriptMPW.AddDeleteDirCommand (Const FileName : TCmdStr);
  357. begin
  358. Add('Delete ' + MaybeQuoted (ScriptFixFileName (FileName)));
  359. end;
  360. Procedure TAsmScriptMPW.WriteToDisk;
  361. Begin
  362. AddStart('# Script for assembling and linking a FreePascal program on MPW (MacOS)');
  363. Add('Echo Done');
  364. inherited WriteToDisk;
  365. end;
  366. Procedure GenerateAsmRes(const st : TCmdStr);
  367. var
  368. scripttyp : tscripttype;
  369. begin
  370. if cs_link_on_target in current_settings.globalswitches then
  371. scripttyp := target_info.script
  372. else
  373. scripttyp := source_info.script;
  374. case scripttyp of
  375. script_unix :
  376. AsmRes:=TAsmScriptUnix.Create(st);
  377. script_dos :
  378. AsmRes:=TAsmScriptDos.Create(st);
  379. script_amiga :
  380. AsmRes:=TAsmScriptAmiga.Create(st);
  381. script_mpw :
  382. AsmRes:=TAsmScriptMPW.Create(st);
  383. end;
  384. end;
  385. {****************************************************************************
  386. Link Response
  387. ****************************************************************************}
  388. procedure TLinkRes.Add(const s:TCmdStr);
  389. begin
  390. if s<>'' then
  391. inherited Add(s);
  392. end;
  393. procedure TLinkRes.AddFileName(const s:TCmdStr);
  394. begin
  395. if section<>'' then
  396. begin
  397. inherited Add(section);
  398. section:='';
  399. end;
  400. if s<>'' then
  401. begin
  402. if not(s[1] in ['a'..'z','A'..'Z','/','\','.','"']) then
  403. begin
  404. if cs_link_on_target in current_settings.globalswitches then
  405. inherited Add('.'+target_info.DirSep+s)
  406. else
  407. inherited Add('.'+source_info.DirSep+s);
  408. end
  409. else
  410. inherited Add(s);
  411. end;
  412. end;
  413. procedure TLinkRes.EndSection(const s:TCmdStr);
  414. begin
  415. { only terminate if we started the section }
  416. if section='' then
  417. inherited Add(s);
  418. section:='';
  419. end;
  420. procedure TLinkRes.StartSection(const s:TCmdStr);
  421. begin
  422. section:=s;
  423. end;
  424. end.