system.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. {$define FPC_ANSI_TEXTFILEREC}
  17. {$if defined(AMIGA_V1_0_ONLY) or defined(AMIGA_V1_2_ONLY)}
  18. {$define AMIGA_LEGACY}
  19. {$endif}
  20. {$if defined(AMIGA_LEGACY) or defined(AMIGA_USE_OSHEAP)}
  21. {$define FPC_AMIGA_USE_OSHEAP}
  22. {$endif}
  23. {$ifdef FPC_AMIGA_USE_OSHEAP}
  24. {$define HAS_MEMORYMANAGER}
  25. {$endif FPC_AMIGA_USE_OSHEAP}
  26. {$I systemh.inc}
  27. {$I osdebugh.inc}
  28. {$if defined(cpum68k) and defined(fpusoft)}
  29. {$define fpc_softfpu_interface}
  30. {$i softfpu.pp}
  31. {$undef fpc_softfpu_interface}
  32. {$endif defined(cpum68k) and defined(fpusoft)}
  33. const
  34. {$if defined(AMIGA_V1_0_ONLY)}
  35. AMIGA_OS_MINVERSION = 0;
  36. {$else}
  37. {$if defined(AMIGA_V1_2_ONLY)}
  38. AMIGA_OS_MINVERSION = 33;
  39. {$else}
  40. {$if defined(AMIGA_V2_0_ONLY)}
  41. AMIGA_OS_MINVERSION = 37;
  42. {$else}
  43. {$ifndef cpupowerpc}
  44. AMIGA_OS_MINVERSION = 39;
  45. {$else}
  46. AMIGA_OS_MINVERSION = 50;
  47. {$endif}
  48. {$endif}
  49. {$endif}
  50. {$endif}
  51. const
  52. LineEnding = #10;
  53. LFNSupport = True;
  54. DirectorySeparator = '/';
  55. DriveSeparator = ':';
  56. ExtensionSeparator = '.';
  57. PathSeparator = ';';
  58. AllowDirectorySeparators : set of char = ['\','/'];
  59. AllowDriveSeparators : set of char = [':'];
  60. maxExitCode = 255;
  61. MaxPathLen = 256;
  62. AllFilesMask = '#?';
  63. const
  64. UnusedHandle : LongInt = -1;
  65. StdInputHandle : LongInt = 0;
  66. StdOutputHandle : LongInt = 0;
  67. StdErrorHandle : LongInt = 0;
  68. FileNameCaseSensitive : Boolean = False;
  69. FileNameCasePreserving: boolean = True;
  70. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  71. sLineBreak = LineEnding;
  72. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  73. BreakOn : Boolean = True;
  74. { FIX ME: remove the kludge required by amunits package, which needs a huge rework }
  75. var
  76. AOS_ExecBase : Pointer; external name '_ExecBase';
  77. _ExecBase: Pointer; external name '_ExecBase'; { amunits compatibility kludge }
  78. AOS_DOSBase : Pointer; public name '_DOSBase'; { the "public" part is amunits compatibility kludge }
  79. _DOSBase: Pointer; external name '_DOSBase'; { amunits compatibility kludge }
  80. AOS_UtilityBase: Pointer; public name '_UtilityBase'; { the "public" part is amunits compatibility kludge }
  81. _UtilityBase: Pointer; external name '_UtilityBase'; { amunits compatibility kludge }
  82. AOS_IntuitionBase: Pointer; public name '_IntuitionBase'; { amunits compatibility kludge }
  83. _IntuitionBase: Pointer; external name '_IntuitionBase'; { amunits compatibility kludge }
  84. {$IFDEF AMIGAOS4}
  85. {$WARNING iExec, iDOS and iUtility should be typed pointer later}
  86. var
  87. IExec : Pointer; external name '_IExec';
  88. IDOS : Pointer;
  89. IUtility : Pointer;
  90. {$ENDIF}
  91. ASYS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  92. ASYS_heapSemaphore: Pointer; { 68k OS from 3.x has no MEMF_SEM_PROTECTED for pools, have to do it ourselves }
  93. ASYS_fileSemaphore: Pointer; { mutex semaphore for filelist access arbitration }
  94. ASYS_origDir : LongInt; { original directory on startup }
  95. AOS_wbMsg : Pointer; public name '_WBenchMsg'; { the "public" part is amunits compatibility kludge }
  96. _WBenchMsg : Pointer; external name '_WBenchMsg'; { amunits compatibility kludge }
  97. AOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  98. AOS_ConHandle: LongInt;
  99. argc: LongInt;
  100. argv: PPChar;
  101. envp: PPChar;
  102. implementation
  103. {$if defined(cpum68k) and defined(fpusoft)}
  104. {$define fpc_softfpu_implementation}
  105. {$define softfpu_compiler_mul32to64}
  106. {$define softfpu_inline}
  107. {$i softfpu.pp}
  108. {$undef fpc_softfpu_implementation}
  109. { we get these functions and types from the softfpu code }
  110. {$define FPC_SYSTEM_HAS_float64}
  111. {$define FPC_SYSTEM_HAS_float32}
  112. {$define FPC_SYSTEM_HAS_flag}
  113. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  114. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  115. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  116. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  117. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  118. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  119. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  120. {$endif defined(cpum68k) and defined(fpusoft)}
  121. {$I system.inc}
  122. {$ifdef FPC_AMIGA_USE_OSHEAP}
  123. {$i osheap.inc}
  124. {$endif FPC_AMIGA_USE_OSHEAP}
  125. {$I osdebug.inc}
  126. {$IFDEF AMIGAOS4}
  127. // Required to allow opening of utility library interface...
  128. {$include utilf.inc}
  129. {$ENDIF}
  130. {$IFDEF ASYS_FPC_FILEDEBUG}
  131. {$WARNING Compiling with file debug enabled!}
  132. {$ENDIF}
  133. {$IFDEF ASYS_FPC_MEMDEBUG}
  134. {$WARNING Compiling with memory debug enabled!}
  135. {$ENDIF}
  136. type
  137. PWBArg = ^TWBArg;
  138. TWBArg = record
  139. wa_Lock : LongInt; { a lock descriptor }
  140. wa_Name : PChar; { a string relative to that lock }
  141. end;
  142. WBArgList = array[1..MaxInt] of TWBArg; { Only 1..smNumArgs are valid }
  143. PWBArgList = ^WBArgList;
  144. PWBStartup = ^TWBStartup;
  145. TWBStartup = record
  146. sm_Message : TMessage; { a standard message structure }
  147. sm_Process : Pointer; { the process descriptor for you }
  148. sm_Segment : Pointer; { a descriptor for your code }
  149. sm_NumArgs : Longint; { the number of elements in ArgList }
  150. sm_ToolWindow : Pointer; { description of window }
  151. sm_ArgList : PWBArgList; { the arguments themselves }
  152. end;
  153. {*****************************************************************************
  154. Misc. System Dependent Functions
  155. *****************************************************************************}
  156. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  157. procedure System_exit;
  158. var
  159. oldDirLock: LongInt;
  160. begin
  161. { Dispose the thread init/exit chains }
  162. CleanupThreadProcChain(threadInitProcList);
  163. CleanupThreadProcChain(threadExitProcList);
  164. { We must remove the CTRL-C FLAG here because halt }
  165. { may call I/O routines, which in turn might call }
  166. { halt, so a recursive stack crash }
  167. if BreakOn then begin
  168. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  169. SetSignal(0,SIGBREAKF_CTRL_C);
  170. end;
  171. { Closing opened files }
  172. CloseList(ASYS_fileList);
  173. { Changing back to original directory if changed }
  174. if ASYS_origDir<>0 then begin
  175. oldDirLock:=CurrentDir(ASYS_origDir);
  176. { unlock our lock if its safe, so we won't leak the lock }
  177. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  178. Unlock(oldDirLock);
  179. end;
  180. {$IFDEF AMIGAOS4}
  181. if iDOS<>nil then DropInterface(iDOS);
  182. if iUtility<>nil then DropInterface(iUtility);
  183. {$ENDIF}
  184. if AOS_IntuitionBase<>nil then CloseLibrary(AOS_IntuitionBase); { amunits kludge }
  185. if AOS_UtilityBase<>nil then CloseLibrary(AOS_UtilityBase);
  186. if AOS_DOSBase<>nil then CloseLibrary(AOS_DOSBase);
  187. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  188. { If in Workbench mode, replying WBMsg }
  189. if AOS_wbMsg<>nil then begin
  190. Forbid;
  191. ReplyMsg(AOS_wbMsg);
  192. end;
  193. haltproc(ExitCode);
  194. end;
  195. {*****************************************************************************
  196. Parameterhandling
  197. as include in amicommon
  198. *****************************************************************************}
  199. {$I paramhandling.inc}
  200. {*****************************************************************************
  201. Randomize
  202. *****************************************************************************}
  203. { set randseed to a new pseudo random value }
  204. procedure randomize;
  205. var tmpTime: TDateStamp;
  206. begin
  207. DateStamp(@tmpTime);
  208. randseed:=tmpTime.ds_tick;
  209. end;
  210. {$IFDEF FPC_AMIGA_USE_OSHEAP}
  211. var
  212. { generated by the compiler based on the $MEMORY directive }
  213. heapsize : PtrInt; external name '__heapsize';
  214. {$ENDIF FPC_AMIGA_USE_OSHEAP}
  215. { AmigaOS specific startup }
  216. procedure SysInitAmigaOS;
  217. var self: PProcess;
  218. begin
  219. self:=PProcess(FindTask(nil));
  220. if self^.pr_CLI=0 then begin
  221. { if we're running from Ambient/Workbench, we catch its message }
  222. WaitPort(@self^.pr_MsgPort);
  223. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  224. end;
  225. AOS_DOSBase:=OpenLibrary('dos.library',AMIGA_OS_MINVERSION);
  226. if AOS_DOSBase=nil then Halt(1);
  227. {$ifndef AMIGA_LEGACY}
  228. AOS_UtilityBase:=OpenLibrary('utility.library',AMIGA_OS_MINVERSION);
  229. if AOS_UtilityBase=nil then Halt(1);
  230. {$endif}
  231. AOS_IntuitionBase:=OpenLibrary('intuition.library',AMIGA_OS_MINVERSION); { amunits support kludge }
  232. if AOS_IntuitionBase=nil then Halt(1);
  233. {$IFDEF AMIGAOS4}
  234. { Open main interfaces on OS4 }
  235. iDOS := GetInterface(AOS_DOSBase,'main',1,nil);
  236. iUtility := GetInterface(AOS_UtilityBase,'main',1,nil);
  237. {$ENDIF}
  238. { Creating the memory pool for growing heap }
  239. {$IFNDEF FPC_AMIGA_USE_OSHEAP}
  240. ASYS_heapPool:=CreatePool(MEMF_ANY,growheapsize2,growheapsize1);
  241. {$ELSE FPC_AMIGA_USE_OSHEAP}
  242. ASYS_heapPool:=CreatePool(MEMF_ANY,min(heapsize,1024),min(heapsize div 2,1024));
  243. {$ENDIF FPC_AMIGA_USE_OSHEAP}
  244. if ASYS_heapPool=nil then Halt(1);
  245. ASYS_heapSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  246. if ASYS_heapSemaphore = nil then Halt(1);
  247. InitSemaphore(ASYS_heapSemaphore);
  248. { Initialize semaphore for filelist access arbitration }
  249. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  250. if ASYS_fileSemaphore = nil then Halt(1);
  251. InitSemaphore(ASYS_fileSemaphore);
  252. if AOS_wbMsg=nil then begin
  253. StdInputHandle:=dosInput;
  254. StdOutputHandle:=dosOutput;
  255. StdErrorHandle:=StdOutputHandle;
  256. end else begin
  257. AOS_ConHandle:=Open(AOS_ConName,MODE_OLDFILE);
  258. if AOS_ConHandle<>0 then begin
  259. StdInputHandle:=AOS_ConHandle;
  260. StdOutputHandle:=AOS_ConHandle;
  261. StdErrorHandle:=AOS_ConHandle;
  262. end else
  263. Halt(1);
  264. end;
  265. end;
  266. procedure SysInitStdIO;
  267. begin
  268. OpenStdIO(Input,fmInput,StdInputHandle);
  269. OpenStdIO(Output,fmOutput,StdOutputHandle);
  270. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  271. {$ifndef FPC_STDOUT_TRUE_ALIAS}
  272. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  273. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  274. {$endif FPC_STDOUT_TRUE_ALIAS}
  275. end;
  276. function GetProcessID: SizeUInt;
  277. begin
  278. GetProcessID:=SizeUInt(FindTask(NIL));
  279. end;
  280. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  281. begin
  282. result := stklen;
  283. end;
  284. begin
  285. IsConsole := TRUE;
  286. StackLength := CheckInitialStkLen(InitialStkLen);
  287. StackBottom := StackTop - StackLength;
  288. { OS specific startup }
  289. AOS_wbMsg:=nil;
  290. ASYS_origDir:=0;
  291. ASYS_fileList:=nil;
  292. envp:=nil;
  293. SysInitAmigaOS;
  294. { Set up signals handlers }
  295. // InstallSignals;
  296. {$ifndef FPC_AMIGA_USE_OSHEAP}
  297. { Setup heap }
  298. InitHeap;
  299. {$endif FPC_AMIGA_USE_OSHEAP}
  300. SysInitExceptions;
  301. {$ifdef cpum68k}
  302. fpc_cpucodeinit;
  303. {$endif}
  304. initunicodestringmanager;
  305. { Setup stdin, stdout and stderr }
  306. SysInitStdIO;
  307. { Reset IO Error }
  308. InOutRes:=0;
  309. { Arguments }
  310. GenerateArgs;
  311. InitSystemThreads;
  312. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  313. InitSystemDynLibs;
  314. {$endif}
  315. end.