system.pp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004 by Karoly Balogh for Genesi S.a.r.l.
  4. System unit for MorphOS/PowerPC
  5. Uses parts of the Commodore Amiga/68k port by Carl Eric Codere
  6. and Nils Sjoholm
  7. MorphOS port was done on a free Pegasos II/G4 machine
  8. provided by Genesi S.a.r.l. <www.genesi.lu>
  9. See the file COPYING.FPC, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. **********************************************************************}
  15. unit System;
  16. interface
  17. {$define FPC_IS_SYSTEM}
  18. {.$define FPC_MORPHOS_USE_OSHEAP}
  19. {$ifdef FPC_MORPHOS_USE_OSHEAP}
  20. {$define HAS_MEMORYMANAGER}
  21. {$endif FPC_MORPHOS_USE_OSHEAP}
  22. {$I systemh.inc}
  23. {$I osdebugh.inc}
  24. const
  25. LineEnding = #10;
  26. LFNSupport = True;
  27. DirectorySeparator = '/';
  28. DriveSeparator = ':';
  29. ExtensionSeparator = '.';
  30. PathSeparator = ';';
  31. AllowDirectorySeparators : set of AnsiChar = ['\','/'];
  32. AllowDriveSeparators : set of AnsiChar = [':'];
  33. maxExitCode = 255;
  34. MaxPathLen = 256;
  35. AllFilesMask = '#?';
  36. const
  37. UnusedHandle : LongInt = -1;
  38. StdInputHandle : LongInt = 0;
  39. StdOutputHandle : LongInt = 0;
  40. StdErrorHandle : LongInt = 0;
  41. FileNameCaseSensitive : Boolean = False;
  42. FileNameCasePreserving: boolean = true;
  43. CtrlZMarksEOF: boolean = false; { #26 not considered as end of file }
  44. sLineBreak = LineEnding;
  45. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  46. BreakOn : Boolean = True;
  47. var
  48. MOS_ExecBase : Pointer; external name '_ExecBase';
  49. MOS_DOSBase : Pointer; public name 'AOS_DOSBASE';
  50. AOS_DOSBase : Pointer; external name 'AOS_DOSBASE'; { common Amiga code compatibility kludge }
  51. MOS_UtilityBase: Pointer;
  52. ASYS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  53. ASYS_fileSemaphore: Pointer; { mutex semaphore for filelist access arbitration }
  54. ASYS_origDir : LongInt; { original directory on startup }
  55. MOS_ambMsg : Pointer;
  56. MOS_ConName : PAnsiChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  57. MOS_ConHandle: LongInt;
  58. AOS_wbMsg: Pointer absolute MOS_ambMsg; { common Amiga code compatibility kludge }
  59. argc: LongInt;
  60. argv: PPAnsiChar;
  61. envp: PPAnsiChar;
  62. implementation
  63. {$define FPC_SYSTEM_HAS_STACKTOP}
  64. {$I system.inc}
  65. {$ifdef FPC_MORPHOS_USE_OSHEAP}
  66. {$i osheap.inc}
  67. {$endif FPC_MORPHOS_USE_OSHEAP}
  68. {$I osdebug.inc}
  69. function StackTop: pointer;
  70. begin
  71. StackTop:=PETask(FindTask(nil)^.tc_ETask)^.PPCSPUpper;
  72. end;
  73. {$IFDEF MOSFPC_FILEDEBUG}
  74. {$WARNING Compiling with file debug enabled!}
  75. {$ENDIF}
  76. {$IFDEF MOSFPC_MEMDEBUG}
  77. {$WARNING Compiling with memory debug enabled!}
  78. {$ENDIF}
  79. type
  80. pWBArg = ^tWBArg;
  81. tWBArg = record
  82. wa_Lock: longint;
  83. wa_Name: PAnsiChar;
  84. end;
  85. WBArgList = array[1..MaxInt] of TWBArg; { Only 1..smNumArgs are valid }
  86. PWBArgList = ^WBArgList;
  87. pWBStartup = ^tWBStartup;
  88. tWBStartup = packed record
  89. sm_Message : tMessage;
  90. sm_Process : pMsgPort;
  91. sm_Segment : longint;
  92. sm_NumArgs : longint;
  93. sm_ToolWindow: PAnsiChar;
  94. sm_ArgList : PWBArgList;
  95. end;
  96. {*****************************************************************************
  97. Misc. System Dependent Functions
  98. *****************************************************************************}
  99. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  100. procedure System_exit;
  101. var
  102. oldDirLock: LongInt;
  103. begin
  104. { Dispose the thread init/exit chains }
  105. CleanupThreadProcChain(threadInitProcList);
  106. CleanupThreadProcChain(threadExitProcList);
  107. { We must remove the CTRL-C FLAG here because halt }
  108. { may call I/O routines, which in turn might call }
  109. { halt, so a recursive stack crash }
  110. if BreakOn then begin
  111. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  112. SetSignal(0,SIGBREAKF_CTRL_C);
  113. end;
  114. { Closing opened files }
  115. CloseList(ASYS_fileList);
  116. { Changing back to original directory if changed }
  117. if ASYS_origDir<>0 then begin
  118. oldDirLock:=CurrentDir(ASYS_origDir);
  119. { unlock our lock if its safe, so we won't leak the lock }
  120. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  121. Unlock(oldDirLock);
  122. end;
  123. { Closing CON: when in Ambient mode }
  124. if MOS_ConHandle<>0 then dosClose(MOS_ConHandle);
  125. if MOS_UtilityBase<>nil then CloseLibrary(MOS_UtilityBase);
  126. if MOS_DOSBase<>nil then CloseLibrary(MOS_DOSBase);
  127. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  128. { If in Ambient mode, replying WBMsg }
  129. if MOS_ambMsg<>nil then begin
  130. Forbid;
  131. ReplyMsg(MOS_ambMsg);
  132. end;
  133. haltproc(ExitCode);
  134. end;
  135. {*****************************************************************************
  136. Parameterhandling
  137. as include in amicommon
  138. *****************************************************************************}
  139. {$I paramhandling.inc}
  140. {*****************************************************************************
  141. Randomize
  142. *****************************************************************************}
  143. { set randseed to a new pseudo random value }
  144. procedure randomize;
  145. var tmpTime: TDateStamp;
  146. begin
  147. DateStamp(@tmpTime);
  148. randseed:=tmpTime.ds_tick;
  149. end;
  150. {$IFDEF FPC_MORPHOS_USE_OSHEAP}
  151. var
  152. { generated by the compiler based on the $MEMORY directive }
  153. heapsize : PtrInt; external name '__heapsize';
  154. {$ENDIF FPC_MORPHOS_USE_OSHEAP}
  155. { MorphOS specific startup }
  156. procedure SysInitMorphOS;
  157. var self: PProcess;
  158. begin
  159. self:=PProcess(FindTask(nil));
  160. if self^.pr_CLI=0 then begin
  161. { if we're running from Ambient/Workbench, we catch its message }
  162. WaitPort(@self^.pr_MsgPort);
  163. MOS_ambMsg:=GetMsg(@self^.pr_MsgPort);
  164. end;
  165. MOS_DOSBase:=OpenLibrary('dos.library',50);
  166. if MOS_DOSBase=nil then Halt(1);
  167. MOS_UtilityBase:=OpenLibrary('utility.library',50);
  168. if MOS_UtilityBase=nil then Halt(1);
  169. { Creating the memory pool for growing heap }
  170. {$IFNDEF FPC_MORPHOS_USE_OSHEAP}
  171. ASYS_heapPool:=CreatePool(MEMF_FAST or MEMF_SEM_PROTECTED,growheapsize2,growheapsize2 div 4);
  172. {$ELSE FPC_MORPHOS_USE_OSHEAP}
  173. ASYS_heapPool:=CreatePool(MEMF_FAST or MEMF_SEM_PROTECTED,min(heapsize,1024),min(heapsize div 2,1024));
  174. {$ENDIF FPC_MORPHOS_USE_OSHEAP}
  175. if ASYS_heapPool=nil then Halt(1);
  176. { Initialize semaphore for filelist access arbitration }
  177. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  178. if ASYS_fileSemaphore = nil then Halt(1);
  179. InitSemaphore(ASYS_fileSemaphore);
  180. if MOS_ambMsg=nil then begin
  181. MOS_ConHandle:=0;
  182. StdInputHandle:=dosInput;
  183. StdOutputHandle:=dosOutput;
  184. StdErrorHandle:=StdOutputHandle;
  185. end else begin
  186. MOS_ConHandle:=Open(MOS_ConName,MODE_OLDFILE);
  187. if MOS_ConHandle<>0 then begin
  188. StdInputHandle:=MOS_ConHandle;
  189. StdOutputHandle:=MOS_ConHandle;
  190. StdErrorHandle:=MOS_ConHandle;
  191. end else
  192. Halt(1);
  193. end;
  194. end;
  195. procedure SysInitStdIO;
  196. begin
  197. OpenStdIO(Input,fmInput,StdInputHandle);
  198. OpenStdIO(Output,fmOutput,StdOutputHandle);
  199. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  200. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  201. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  202. end;
  203. function GetProcessID: SizeUInt;
  204. begin
  205. GetProcessID:=SizeUInt(FindTask(NIL));
  206. end;
  207. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  208. begin
  209. result := stklen;
  210. end;
  211. begin
  212. IsConsole := TRUE;
  213. StackLength := CheckInitialStkLen(InitialStkLen);
  214. StackBottom := StackTop - StackLength;
  215. { OS specific startup }
  216. MOS_ambMsg:=nil;
  217. ASYS_origDir:=0;
  218. ASYS_fileList:=nil;
  219. envp:=nil;
  220. SysInitMorphOS;
  221. { Set up signals handlers }
  222. // InstallSignals;
  223. { Setup heap }
  224. {$ifndef FPC_MORPHOS_USE_OSHEAP}
  225. { Setup heap }
  226. InitHeap;
  227. {$endif FPC_MORPHOS_USE_OSHEAP}
  228. SysInitExceptions;
  229. initunicodestringmanager;
  230. { Setup stdin, stdout and stderr }
  231. SysInitStdIO;
  232. { Reset IO Error }
  233. InOutRes:=0;
  234. { Arguments }
  235. GenerateArgs;
  236. InitSystemThreads;
  237. InitSystemDynLibs;
  238. end.