system.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_heapSemaphore: Pointer; { 68k OS from 3.x has no MEMF_SEM_PROTECTED for pools, have to do it ourselves }
  65. ASYS_fileSemaphore: Pointer; { mutex semaphore for filelist access arbitration }
  66. ASYS_origDir : LongInt; { original directory on startup }
  67. AOS_wbMsg : Pointer; public name '_WBenchMsg'; { the "public" part is amunits compatibility kludge }
  68. _WBenchMsg : Pointer; external name '_WBenchMsg'; { amunits compatibility kludge }
  69. AOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  70. AOS_ConHandle: LongInt;
  71. argc: LongInt;
  72. argv: PPChar;
  73. envp: PPChar;
  74. implementation
  75. {$ifdef cpum68k}
  76. {$define fpc_softfpu_implementation}
  77. {$i softfpu.pp}
  78. {$undef fpc_softfpu_implementation}
  79. { we get these functions and types from the softfpu code }
  80. {$define FPC_SYSTEM_HAS_float64}
  81. {$define FPC_SYSTEM_HAS_float32}
  82. {$define FPC_SYSTEM_HAS_flag}
  83. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  84. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  85. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  86. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  87. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  88. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  89. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  90. {$endif cpum68k}
  91. {$I system.inc}
  92. {$I osdebug.inc}
  93. {$I m68kamiga.inc}
  94. {$IFDEF AMIGAOS4}
  95. // Required to allow opening of utility library interface...
  96. {$include utilf.inc}
  97. {$ENDIF}
  98. {$IFDEF ASYS_FPC_FILEDEBUG}
  99. {$WARNING Compiling with file debug enabled!}
  100. {$ENDIF}
  101. {$IFDEF ASYS_FPC_MEMDEBUG}
  102. {$WARNING Compiling with memory debug enabled!}
  103. {$ENDIF}
  104. {*****************************************************************************
  105. Misc. System Dependent Functions
  106. *****************************************************************************}
  107. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  108. procedure System_exit;
  109. var
  110. oldDirLock: LongInt;
  111. begin
  112. { Dispose the thread init/exit chains }
  113. CleanupThreadProcChain(threadInitProcList);
  114. CleanupThreadProcChain(threadExitProcList);
  115. { We must remove the CTRL-C FLAG here because halt }
  116. { may call I/O routines, which in turn might call }
  117. { halt, so a recursive stack crash }
  118. if BreakOn then begin
  119. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  120. SetSignal(0,SIGBREAKF_CTRL_C);
  121. end;
  122. { Closing opened files }
  123. CloseList(ASYS_fileList);
  124. { Changing back to original directory if changed }
  125. if ASYS_origDir<>0 then begin
  126. oldDirLock:=CurrentDir(ASYS_origDir);
  127. { unlock our lock if its safe, so we won't leak the lock }
  128. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  129. Unlock(oldDirLock);
  130. end;
  131. {$IFDEF AMIGAOS4}
  132. if iDOS<>nil then DropInterface(iDOS);
  133. if iUtility<>nil then DropInterface(iUtility);
  134. {$ENDIF}
  135. if AOS_IntuitionBase<>nil then CloseLibrary(AOS_IntuitionBase); { amunits kludge }
  136. if AOS_UtilityBase<>nil then CloseLibrary(AOS_UtilityBase);
  137. if AOS_DOSBase<>nil then CloseLibrary(AOS_DOSBase);
  138. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  139. { If in Workbench mode, replying WBMsg }
  140. if AOS_wbMsg<>nil then begin
  141. Forbid;
  142. ReplyMsg(AOS_wbMsg);
  143. end;
  144. haltproc(ExitCode);
  145. end;
  146. { Generates correct argument array on startup }
  147. procedure GenerateArgs;
  148. var
  149. argvlen : longint;
  150. procedure allocarg(idx,len:longint);
  151. var
  152. i,oldargvlen : longint;
  153. begin
  154. if idx>=argvlen then
  155. begin
  156. oldargvlen:=argvlen;
  157. argvlen:=(idx+8) and (not 7);
  158. sysreallocmem(argv,argvlen*sizeof(pointer));
  159. for i:=oldargvlen to argvlen-1 do
  160. argv[i]:=nil;
  161. end;
  162. ArgV [Idx] := SysAllocMem (Succ (Len));
  163. end;
  164. var
  165. count: word;
  166. start: word;
  167. localindex: word;
  168. p : pchar;
  169. temp : string;
  170. begin
  171. p:=GetArgStr;
  172. argvlen:=0;
  173. { Set argv[0] }
  174. temp:=paramstr(0);
  175. allocarg(0,length(temp));
  176. move(temp[1],argv[0]^,length(temp));
  177. argv[0][length(temp)]:=#0;
  178. { check if we're started from Ambient }
  179. if AOS_wbMsg<>nil then
  180. begin
  181. argc:=0;
  182. exit;
  183. end;
  184. { Handle the other args }
  185. count:=0;
  186. { first index is one }
  187. localindex:=1;
  188. while (p[count]<>#0) do
  189. begin
  190. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  191. start:=count;
  192. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  193. if (count-start>0) then
  194. begin
  195. allocarg(localindex,count-start);
  196. move(p[start],argv[localindex]^,count-start);
  197. argv[localindex][count-start]:=#0;
  198. inc(localindex);
  199. end;
  200. end;
  201. argc:=localindex;
  202. end;
  203. function GetProgDir: String;
  204. var
  205. s1 : String;
  206. alock : LongInt;
  207. counter: Byte;
  208. begin
  209. GetProgDir:='';
  210. FillChar(s1,255,#0);
  211. { GetLock of program directory }
  212. alock:=GetProgramDir;
  213. if alock<>0 then begin
  214. if NameFromLock(alock,@s1[1],255) then begin
  215. counter:=1;
  216. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  217. s1[0]:=Char(counter-1);
  218. GetProgDir:=s1;
  219. end;
  220. end;
  221. end;
  222. function GetProgramName: String;
  223. { Returns ONLY the program name }
  224. var
  225. s1 : String;
  226. counter: Byte;
  227. begin
  228. GetProgramName:='';
  229. FillChar(s1,255,#0);
  230. if GetProgramName(@s1[1],255) then begin
  231. { now check out and assign the length of the string }
  232. counter := 1;
  233. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  234. s1[0]:=Char(counter-1);
  235. { now remove any component path which should not be there }
  236. for counter:=length(s1) downto 1 do
  237. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  238. { readjust counterv to point to character }
  239. if counter<>1 then Inc(counter);
  240. GetProgramName:=copy(s1,counter,length(s1));
  241. end;
  242. end;
  243. {*****************************************************************************
  244. ParamStr/Randomize
  245. *****************************************************************************}
  246. { number of args }
  247. function paramcount : longint;
  248. begin
  249. if AOS_wbMsg<>nil then
  250. paramcount:=0
  251. else
  252. paramcount:=argc-1;
  253. end;
  254. { argument number l }
  255. function paramstr(l : longint) : string;
  256. var
  257. s1: String;
  258. begin
  259. paramstr:='';
  260. if AOS_wbMsg<>nil then exit;
  261. if l=0 then begin
  262. s1:=GetProgDir;
  263. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  264. else paramstr:=s1+'/'+GetProgramName;
  265. end else begin
  266. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  267. end;
  268. end;
  269. { set randseed to a new pseudo random value }
  270. procedure randomize;
  271. var tmpTime: TDateStamp;
  272. begin
  273. DateStamp(@tmpTime);
  274. randseed:=tmpTime.ds_tick;
  275. end;
  276. { AmigaOS specific startup }
  277. procedure SysInitAmigaOS;
  278. var self: PProcess;
  279. begin
  280. self:=PProcess(FindTask(nil));
  281. if self^.pr_CLI=0 then begin
  282. { if we're running from Ambient/Workbench, we catch its message }
  283. WaitPort(@self^.pr_MsgPort);
  284. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  285. end;
  286. AOS_DOSBase:=OpenLibrary('dos.library',37);
  287. if AOS_DOSBase=nil then Halt(1);
  288. AOS_UtilityBase:=OpenLibrary('utility.library',37);
  289. if AOS_UtilityBase=nil then Halt(1);
  290. AOS_IntuitionBase:=OpenLibrary('intuition.library',37); { amunits support kludge }
  291. if AOS_IntuitionBase=nil then Halt(1);
  292. {$IFDEF AMIGAOS4}
  293. { Open main interfaces on OS4 }
  294. iDOS := GetInterface(AOS_DOSBase,'main',1,nil);
  295. iUtility := GetInterface(AOS_UtilityBase,'main',1,nil);
  296. {$ENDIF}
  297. { Creating the memory pool for growing heap }
  298. ASYS_heapPool:=CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  299. if ASYS_heapPool=nil then Halt(1);
  300. ASYS_heapSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  301. if ASYS_heapSemaphore = nil then Halt(1);
  302. InitSemaphore(ASYS_heapSemaphore);
  303. { Initialize semaphore for filelist access arbitration }
  304. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  305. if ASYS_fileSemaphore = nil then Halt(1);
  306. InitSemaphore(ASYS_fileSemaphore);
  307. if AOS_wbMsg=nil then begin
  308. StdInputHandle:=dosInput;
  309. StdOutputHandle:=dosOutput;
  310. StdErrorHandle:=StdOutputHandle;
  311. end else begin
  312. AOS_ConHandle:=Open(AOS_ConName,MODE_OLDFILE);
  313. if AOS_ConHandle<>0 then begin
  314. StdInputHandle:=AOS_ConHandle;
  315. StdOutputHandle:=AOS_ConHandle;
  316. StdErrorHandle:=AOS_ConHandle;
  317. end else
  318. Halt(1);
  319. end;
  320. end;
  321. procedure SysInitStdIO;
  322. begin
  323. OpenStdIO(Input,fmInput,StdInputHandle);
  324. OpenStdIO(Output,fmOutput,StdOutputHandle);
  325. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  326. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  327. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  328. end;
  329. function GetProcessID: SizeUInt;
  330. begin
  331. GetProcessID:=SizeUInt(FindTask(NIL));
  332. end;
  333. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  334. begin
  335. result := stklen;
  336. end;
  337. begin
  338. IsConsole := TRUE;
  339. SysResetFPU;
  340. if not(IsLibrary) then
  341. SysInitFPU;
  342. StackLength := CheckInitialStkLen(InitialStkLen);
  343. StackBottom := Sptr - StackLength;
  344. { OS specific startup }
  345. AOS_wbMsg:=nil;
  346. ASYS_origDir:=0;
  347. ASYS_fileList:=nil;
  348. envp:=nil;
  349. SysInitAmigaOS;
  350. { Set up signals handlers }
  351. // InstallSignals;
  352. { Setup heap }
  353. InitHeap;
  354. SysInitExceptions;
  355. initunicodestringmanager;
  356. { Setup stdin, stdout and stderr }
  357. SysInitStdIO;
  358. { Reset IO Error }
  359. InOutRes:=0;
  360. { Arguments }
  361. GenerateArgs;
  362. InitSystemThreads;
  363. InitSystemDynLibs;
  364. end.