system.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004-2006 by Karoly Balogh
  4. System unit for AmigaOS 3.x/4.x
  5. Uses parts of the Free Pascal 1.0.x for Commodore Amiga/68k port
  6. by Carl Eric Codere and Nils Sjoholm
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  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.
  12. **********************************************************************}
  13. unit System;
  14. interface
  15. {$define FPC_IS_SYSTEM}
  16. {$I systemh.inc}
  17. {$I osdebugh.inc}
  18. {$ifdef cpum68k}
  19. {$define fpc_softfpu_interface}
  20. {$i softfpu.pp}
  21. {$undef fpc_softfpu_interface}
  22. {$endif cpum68k}
  23. const
  24. LineEnding = #10;
  25. LFNSupport = True;
  26. DirectorySeparator = '/';
  27. DriveSeparator = ':';
  28. ExtensionSeparator = '.';
  29. PathSeparator = ';';
  30. AllowDirectorySeparators : set of char = ['\','/'];
  31. AllowDriveSeparators : set of char = [':'];
  32. maxExitCode = 255;
  33. MaxPathLen = 256;
  34. AllFilesMask = '#?';
  35. const
  36. UnusedHandle : LongInt = -1;
  37. StdInputHandle : LongInt = 0;
  38. StdOutputHandle : LongInt = 0;
  39. StdErrorHandle : LongInt = 0;
  40. FileNameCaseSensitive : Boolean = False;
  41. FileNameCasePreserving: boolean = True;
  42. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  43. sLineBreak = LineEnding;
  44. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  45. BreakOn : Boolean = True;
  46. { FIX ME: remove the kludge required by amunits package, which needs a huge rework }
  47. var
  48. AOS_ExecBase : Pointer; external name '_ExecBase';
  49. _ExecBase: Pointer; external name '_ExecBase'; { amunits compatibility kludge }
  50. AOS_DOSBase : Pointer; public name '_DOSBase'; { the "public" part is amunits compatibility kludge }
  51. _DOSBase: Pointer; external name '_DOSBase'; { amunits compatibility kludge }
  52. AOS_UtilityBase: Pointer; public name '_UtilityBase'; { the "public" part is amunits compatibility kludge }
  53. _UtilityBase: Pointer; external name '_UtilityBase'; { amunits compatibility kludge }
  54. AOS_IntuitionBase: Pointer; public name '_IntuitionBase'; { amunits compatibility kludge }
  55. _IntuitionBase: Pointer; external name '_IntuitionBase'; { amunits compatibility kludge }
  56. {$IFDEF AMIGAOS4}
  57. {$WARNING iExec, iDOS and iUtility should be typed pointer later}
  58. var
  59. IExec : Pointer; external name '_IExec';
  60. IDOS : Pointer;
  61. IUtility : Pointer;
  62. {$ENDIF}
  63. ASYS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  64. ASYS_origDir : LongInt; { original directory on startup }
  65. AOS_wbMsg : Pointer; public name '_WBenchMsg'; { the "public" part is amunits compatibility kludge }
  66. _WBenchMsg : Pointer; external name '_WBenchMsg'; { amunits compatibility kludge }
  67. AOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  68. AOS_ConHandle: LongInt;
  69. argc: LongInt;
  70. argv: PPChar;
  71. envp: PPChar;
  72. implementation
  73. {$ifdef cpum68k}
  74. {$define fpc_softfpu_implementation}
  75. {$i softfpu.pp}
  76. {$undef fpc_softfpu_implementation}
  77. { we get these functions and types from the softfpu code }
  78. {$define FPC_SYSTEM_HAS_float64}
  79. {$define FPC_SYSTEM_HAS_float32}
  80. {$define FPC_SYSTEM_HAS_flag}
  81. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  82. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  83. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  84. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  85. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  86. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  87. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  88. {$endif cpum68k}
  89. {$I system.inc}
  90. {$I osdebug.inc}
  91. {$IFDEF AMIGAOS4}
  92. // Required to allow opening of utility library interface...
  93. {$include utilf.inc}
  94. {$ENDIF}
  95. {$IFDEF ASYS_FPC_FILEDEBUG}
  96. {$WARNING Compiling with file debug enabled!}
  97. {$ENDIF}
  98. {$IFDEF ASYS_FPC_MEMDEBUG}
  99. {$WARNING Compiling with memory debug enabled!}
  100. {$ENDIF}
  101. {*****************************************************************************
  102. Misc. System Dependent Functions
  103. *****************************************************************************}
  104. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  105. procedure System_exit;
  106. var
  107. oldDirLock: LongInt;
  108. begin
  109. { We must remove the CTRL-C FLAG here because halt }
  110. { may call I/O routines, which in turn might call }
  111. { halt, so a recursive stack crash }
  112. if BreakOn then begin
  113. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  114. SetSignal(0,SIGBREAKF_CTRL_C);
  115. end;
  116. { Closing opened files }
  117. CloseList(ASYS_fileList);
  118. { Changing back to original directory if changed }
  119. if ASYS_origDir<>0 then begin
  120. oldDirLock:=CurrentDir(ASYS_origDir);
  121. { unlock our lock if its safe, so we won't leak the lock }
  122. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  123. Unlock(oldDirLock);
  124. end;
  125. {$IFDEF AMIGAOS4}
  126. if iDOS<>nil then DropInterface(iDOS);
  127. if iUtility<>nil then DropInterface(iUtility);
  128. {$ENDIF}
  129. if AOS_IntuitionBase<>nil then CloseLibrary(AOS_IntuitionBase); { amunits kludge }
  130. if AOS_UtilityBase<>nil then CloseLibrary(AOS_UtilityBase);
  131. if AOS_DOSBase<>nil then CloseLibrary(AOS_DOSBase);
  132. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  133. { If in Workbench mode, replying WBMsg }
  134. if AOS_wbMsg<>nil then begin
  135. Forbid;
  136. ReplyMsg(AOS_wbMsg);
  137. end;
  138. haltproc(ExitCode);
  139. end;
  140. { Generates correct argument array on startup }
  141. procedure GenerateArgs;
  142. var
  143. argvlen : longint;
  144. procedure allocarg(idx,len:longint);
  145. var
  146. i,oldargvlen : longint;
  147. begin
  148. if idx>=argvlen then
  149. begin
  150. oldargvlen:=argvlen;
  151. argvlen:=(idx+8) and (not 7);
  152. sysreallocmem(argv,argvlen*sizeof(pointer));
  153. for i:=oldargvlen to argvlen-1 do
  154. argv[i]:=nil;
  155. end;
  156. ArgV [Idx] := SysAllocMem (Succ (Len));
  157. end;
  158. var
  159. count: word;
  160. start: word;
  161. localindex: word;
  162. p : pchar;
  163. temp : string;
  164. begin
  165. p:=GetArgStr;
  166. argvlen:=0;
  167. { Set argv[0] }
  168. temp:=paramstr(0);
  169. allocarg(0,length(temp));
  170. move(temp[1],argv[0]^,length(temp));
  171. argv[0][length(temp)]:=#0;
  172. { check if we're started from Ambient }
  173. if AOS_wbMsg<>nil then
  174. begin
  175. argc:=0;
  176. exit;
  177. end;
  178. { Handle the other args }
  179. count:=0;
  180. { first index is one }
  181. localindex:=1;
  182. while (p[count]<>#0) do
  183. begin
  184. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  185. start:=count;
  186. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  187. if (count-start>0) then
  188. begin
  189. allocarg(localindex,count-start);
  190. move(p[start],argv[localindex]^,count-start);
  191. argv[localindex][count-start]:=#0;
  192. inc(localindex);
  193. end;
  194. end;
  195. argc:=localindex;
  196. end;
  197. function GetProgDir: String;
  198. var
  199. s1 : String;
  200. alock : LongInt;
  201. counter: Byte;
  202. begin
  203. GetProgDir:='';
  204. FillChar(s1,255,#0);
  205. { GetLock of program directory }
  206. alock:=GetProgramDir;
  207. if alock<>0 then begin
  208. if NameFromLock(alock,@s1[1],255) then begin
  209. counter:=1;
  210. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  211. s1[0]:=Char(counter-1);
  212. GetProgDir:=s1;
  213. end;
  214. end;
  215. end;
  216. function GetProgramName: String;
  217. { Returns ONLY the program name }
  218. var
  219. s1 : String;
  220. counter: Byte;
  221. begin
  222. GetProgramName:='';
  223. FillChar(s1,255,#0);
  224. if GetProgramName(@s1[1],255) then begin
  225. { now check out and assign the length of the string }
  226. counter := 1;
  227. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  228. s1[0]:=Char(counter-1);
  229. { now remove any component path which should not be there }
  230. for counter:=length(s1) downto 1 do
  231. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  232. { readjust counterv to point to character }
  233. if counter<>1 then Inc(counter);
  234. GetProgramName:=copy(s1,counter,length(s1));
  235. end;
  236. end;
  237. {*****************************************************************************
  238. ParamStr/Randomize
  239. *****************************************************************************}
  240. { number of args }
  241. function paramcount : longint;
  242. begin
  243. if AOS_wbMsg<>nil then
  244. paramcount:=0
  245. else
  246. paramcount:=argc-1;
  247. end;
  248. { argument number l }
  249. function paramstr(l : longint) : string;
  250. var
  251. s1: String;
  252. begin
  253. paramstr:='';
  254. if AOS_wbMsg<>nil then exit;
  255. if l=0 then begin
  256. s1:=GetProgDir;
  257. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  258. else paramstr:=s1+'/'+GetProgramName;
  259. end else begin
  260. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  261. end;
  262. end;
  263. { set randseed to a new pseudo random value }
  264. procedure randomize;
  265. var tmpTime: TDateStamp;
  266. begin
  267. DateStamp(@tmpTime);
  268. randseed:=tmpTime.ds_tick;
  269. end;
  270. { AmigaOS specific startup }
  271. procedure SysInitAmigaOS;
  272. var self: PProcess;
  273. begin
  274. self:=PProcess(FindTask(nil));
  275. if self^.pr_CLI=0 then begin
  276. { if we're running from Ambient/Workbench, we catch its message }
  277. WaitPort(@self^.pr_MsgPort);
  278. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  279. end;
  280. AOS_DOSBase:=OpenLibrary('dos.library',37);
  281. if AOS_DOSBase=nil then Halt(1);
  282. AOS_UtilityBase:=OpenLibrary('utility.library',37);
  283. if AOS_UtilityBase=nil then Halt(1);
  284. AOS_IntuitionBase:=OpenLibrary('intuition.library',37); { amunits support kludge }
  285. if AOS_IntuitionBase=nil then Halt(1);
  286. {$IFDEF AMIGAOS4}
  287. { Open main interfaces on OS4 }
  288. iDOS := GetInterface(AOS_DOSBase,'main',1,nil);
  289. iUtility := GetInterface(AOS_UtilityBase,'main',1,nil);
  290. {$ENDIF}
  291. { Creating the memory pool for growing heap }
  292. ASYS_heapPool:=CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  293. if ASYS_heapPool=nil then Halt(1);
  294. if AOS_wbMsg=nil then begin
  295. StdInputHandle:=dosInput;
  296. StdOutputHandle:=dosOutput;
  297. end else begin
  298. AOS_ConHandle:=Open(AOS_ConName,MODE_OLDFILE);
  299. if AOS_ConHandle<>0 then begin
  300. StdInputHandle:=AOS_ConHandle;
  301. StdOutputHandle:=AOS_ConHandle;
  302. end else
  303. Halt(1);
  304. end;
  305. end;
  306. procedure SysInitStdIO;
  307. begin
  308. OpenStdIO(Input,fmInput,StdInputHandle);
  309. OpenStdIO(Output,fmOutput,StdOutputHandle);
  310. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  311. { * AmigaOS doesn't have a separate stderr * }
  312. StdErrorHandle:=StdOutputHandle;
  313. //OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  314. //OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  315. end;
  316. function GetProcessID: SizeUInt;
  317. begin
  318. GetProcessID:=SizeUInt(FindTask(NIL));
  319. end;
  320. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  321. begin
  322. result := stklen;
  323. end;
  324. begin
  325. IsConsole := TRUE;
  326. SysResetFPU;
  327. if not(IsLibrary) then
  328. SysInitFPU;
  329. StackLength := CheckInitialStkLen(InitialStkLen);
  330. StackBottom := Sptr - StackLength;
  331. { OS specific startup }
  332. AOS_wbMsg:=nil;
  333. ASYS_origDir:=0;
  334. ASYS_fileList:=nil;
  335. envp:=nil;
  336. SysInitAmigaOS;
  337. { Set up signals handlers }
  338. // InstallSignals;
  339. { Setup heap }
  340. InitHeap;
  341. SysInitExceptions;
  342. initunicodestringmanager;
  343. { Setup stdin, stdout and stderr }
  344. SysInitStdIO;
  345. { Reset IO Error }
  346. InOutRes:=0;
  347. { Arguments }
  348. GenerateArgs;
  349. InitSystemThreads;
  350. InitSystemDynLibs;
  351. end.