system.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. {$I systemh.inc}
  19. {$I osdebugh.inc}
  20. const
  21. LineEnding = #10;
  22. LFNSupport = True;
  23. DirectorySeparator = '/';
  24. DriveSeparator = ':';
  25. ExtensionSeparator = '.';
  26. PathSeparator = ';';
  27. AllowDirectorySeparators : set of char = ['\','/'];
  28. AllowDriveSeparators : set of char = [':'];
  29. maxExitCode = 255;
  30. MaxPathLen = 256;
  31. AllFilesMask = '#?';
  32. const
  33. UnusedHandle : LongInt = -1;
  34. StdInputHandle : LongInt = 0;
  35. StdOutputHandle : LongInt = 0;
  36. StdErrorHandle : LongInt = 0;
  37. FileNameCaseSensitive : Boolean = False;
  38. FileNameCasePreserving: boolean = true;
  39. CtrlZMarksEOF: boolean = false; { #26 not considered as end of file }
  40. sLineBreak = LineEnding;
  41. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  42. BreakOn : Boolean = True;
  43. var
  44. MOS_ExecBase : Pointer; external name '_ExecBase';
  45. MOS_DOSBase : Pointer; public name 'AOS_DOSBASE';
  46. AOS_DOSBase : Pointer; external name 'AOS_DOSBASE'; { common Amiga code compatibility kludge }
  47. MOS_UtilityBase: Pointer;
  48. ASYS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  49. ASYS_fileSemaphore: Pointer; { mutex semaphore for filelist access arbitration }
  50. ASYS_origDir : LongInt; { original directory on startup }
  51. MOS_ambMsg : Pointer;
  52. MOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  53. MOS_ConHandle: LongInt;
  54. AOS_wbMsg: Pointer absolute MOS_ambMsg; { common Amiga code compatibility kludge }
  55. argc: LongInt;
  56. argv: PPChar;
  57. envp: PPChar;
  58. implementation
  59. {$define FPC_SYSTEM_HAS_STACKTOP}
  60. {$I system.inc}
  61. {$I osdebug.inc}
  62. function StackTop: pointer;
  63. begin
  64. StackTop:=PETask(FindTask(nil)^.tc_ETask)^.PPCSPUpper;
  65. end;
  66. {$IFDEF MOSFPC_FILEDEBUG}
  67. {$WARNING Compiling with file debug enabled!}
  68. {$ENDIF}
  69. {$IFDEF MOSFPC_MEMDEBUG}
  70. {$WARNING Compiling with memory debug enabled!}
  71. {$ENDIF}
  72. type
  73. pWBArg = ^tWBArg;
  74. tWBArg = record
  75. wa_Lock: longint;
  76. wa_Name: PChar;
  77. end;
  78. WBArgList = array[1..MaxInt] of TWBArg; { Only 1..smNumArgs are valid }
  79. PWBArgList = ^WBArgList;
  80. pWBStartup = ^tWBStartup;
  81. tWBStartup = packed record
  82. sm_Message : tMessage;
  83. sm_Process : pMsgPort;
  84. sm_Segment : longint;
  85. sm_NumArgs : longint;
  86. sm_ToolWindow: PChar;
  87. sm_ArgList : PWBArgList;
  88. end;
  89. {*****************************************************************************
  90. Misc. System Dependent Functions
  91. *****************************************************************************}
  92. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  93. procedure System_exit;
  94. var
  95. oldDirLock: LongInt;
  96. begin
  97. { Dispose the thread init/exit chains }
  98. CleanupThreadProcChain(threadInitProcList);
  99. CleanupThreadProcChain(threadExitProcList);
  100. { We must remove the CTRL-C FLAG here because halt }
  101. { may call I/O routines, which in turn might call }
  102. { halt, so a recursive stack crash }
  103. if BreakOn then begin
  104. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  105. SetSignal(0,SIGBREAKF_CTRL_C);
  106. end;
  107. { Closing opened files }
  108. CloseList(ASYS_fileList);
  109. { Changing back to original directory if changed }
  110. if ASYS_origDir<>0 then begin
  111. oldDirLock:=CurrentDir(ASYS_origDir);
  112. { unlock our lock if its safe, so we won't leak the lock }
  113. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  114. Unlock(oldDirLock);
  115. end;
  116. { Closing CON: when in Ambient mode }
  117. if MOS_ConHandle<>0 then dosClose(MOS_ConHandle);
  118. if MOS_UtilityBase<>nil then CloseLibrary(MOS_UtilityBase);
  119. if MOS_DOSBase<>nil then CloseLibrary(MOS_DOSBase);
  120. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  121. { If in Ambient mode, replying WBMsg }
  122. if MOS_ambMsg<>nil then begin
  123. Forbid;
  124. ReplyMsg(MOS_ambMsg);
  125. end;
  126. haltproc(ExitCode);
  127. end;
  128. {*****************************************************************************
  129. Parameterhandling
  130. as include in amicommon
  131. *****************************************************************************}
  132. {$I paramhandling.inc}
  133. {*****************************************************************************
  134. Randomize
  135. *****************************************************************************}
  136. { set randseed to a new pseudo random value }
  137. procedure randomize;
  138. var tmpTime: TDateStamp;
  139. begin
  140. DateStamp(@tmpTime);
  141. randseed:=tmpTime.ds_tick;
  142. end;
  143. { MorphOS specific startup }
  144. procedure SysInitMorphOS;
  145. var self: PProcess;
  146. begin
  147. self:=PProcess(FindTask(nil));
  148. if self^.pr_CLI=0 then begin
  149. { if we're running from Ambient/Workbench, we catch its message }
  150. WaitPort(@self^.pr_MsgPort);
  151. MOS_ambMsg:=GetMsg(@self^.pr_MsgPort);
  152. end;
  153. MOS_DOSBase:=OpenLibrary('dos.library',50);
  154. if MOS_DOSBase=nil then Halt(1);
  155. MOS_UtilityBase:=OpenLibrary('utility.library',50);
  156. if MOS_UtilityBase=nil then Halt(1);
  157. { Creating the memory pool for growing heap }
  158. ASYS_heapPool:=CreatePool(MEMF_FAST or MEMF_SEM_PROTECTED,growheapsize2,growheapsize1);
  159. if ASYS_heapPool=nil then Halt(1);
  160. { Initialize semaphore for filelist access arbitration }
  161. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  162. if ASYS_fileSemaphore = nil then Halt(1);
  163. InitSemaphore(ASYS_fileSemaphore);
  164. if MOS_ambMsg=nil then begin
  165. MOS_ConHandle:=0;
  166. StdInputHandle:=dosInput;
  167. StdOutputHandle:=dosOutput;
  168. StdErrorHandle:=StdOutputHandle;
  169. end else begin
  170. MOS_ConHandle:=Open(MOS_ConName,MODE_OLDFILE);
  171. if MOS_ConHandle<>0 then begin
  172. StdInputHandle:=MOS_ConHandle;
  173. StdOutputHandle:=MOS_ConHandle;
  174. StdErrorHandle:=MOS_ConHandle;
  175. end else
  176. Halt(1);
  177. end;
  178. end;
  179. procedure SysInitStdIO;
  180. begin
  181. OpenStdIO(Input,fmInput,StdInputHandle);
  182. OpenStdIO(Output,fmOutput,StdOutputHandle);
  183. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  184. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  185. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  186. end;
  187. function GetProcessID: SizeUInt;
  188. begin
  189. GetProcessID:=SizeUInt(FindTask(NIL));
  190. end;
  191. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  192. begin
  193. result := stklen;
  194. end;
  195. begin
  196. IsConsole := TRUE;
  197. StackLength := CheckInitialStkLen(InitialStkLen);
  198. StackBottom := StackTop - StackLength;
  199. { OS specific startup }
  200. MOS_ambMsg:=nil;
  201. ASYS_origDir:=0;
  202. ASYS_fileList:=nil;
  203. envp:=nil;
  204. SysInitMorphOS;
  205. { Set up signals handlers }
  206. // InstallSignals;
  207. { Setup heap }
  208. InitHeap;
  209. SysInitExceptions;
  210. initunicodestringmanager;
  211. { Setup stdin, stdout and stderr }
  212. SysInitStdIO;
  213. { Reset IO Error }
  214. InOutRes:=0;
  215. { Arguments }
  216. GenerateArgs;
  217. InitSystemThreads;
  218. InitSystemDynLibs;
  219. end.