system.pp 10 KB

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