system.pp 12 KB

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