system.pp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. {$I system.inc}
  60. {$I osdebug.inc}
  61. {$IFDEF MOSFPC_FILEDEBUG}
  62. {$WARNING Compiling with file debug enabled!}
  63. {$ENDIF}
  64. {$IFDEF MOSFPC_MEMDEBUG}
  65. {$WARNING Compiling with memory debug enabled!}
  66. {$ENDIF}
  67. type
  68. pWBArg = ^tWBArg;
  69. tWBArg = record
  70. wa_Lock: longint;
  71. wa_Name: PChar;
  72. end;
  73. WBArgList = array[1..MaxInt] of TWBArg; { Only 1..smNumArgs are valid }
  74. PWBArgList = ^WBArgList;
  75. pWBStartup = ^tWBStartup;
  76. tWBStartup = packed record
  77. sm_Message : tMessage;
  78. sm_Process : pMsgPort;
  79. sm_Segment : longint;
  80. sm_NumArgs : longint;
  81. sm_ToolWindow: PChar;
  82. sm_ArgList : PWBArgList;
  83. end;
  84. {*****************************************************************************
  85. Misc. System Dependent Functions
  86. *****************************************************************************}
  87. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  88. procedure System_exit;
  89. var
  90. oldDirLock: LongInt;
  91. begin
  92. { Dispose the thread init/exit chains }
  93. CleanupThreadProcChain(threadInitProcList);
  94. CleanupThreadProcChain(threadExitProcList);
  95. { We must remove the CTRL-C FLAG here because halt }
  96. { may call I/O routines, which in turn might call }
  97. { halt, so a recursive stack crash }
  98. if BreakOn then begin
  99. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  100. SetSignal(0,SIGBREAKF_CTRL_C);
  101. end;
  102. { Closing opened files }
  103. CloseList(ASYS_fileList);
  104. { Changing back to original directory if changed }
  105. if ASYS_origDir<>0 then begin
  106. oldDirLock:=CurrentDir(ASYS_origDir);
  107. { unlock our lock if its safe, so we won't leak the lock }
  108. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  109. Unlock(oldDirLock);
  110. end;
  111. { Closing CON: when in Ambient mode }
  112. if MOS_ConHandle<>0 then dosClose(MOS_ConHandle);
  113. if MOS_UtilityBase<>nil then CloseLibrary(MOS_UtilityBase);
  114. if MOS_DOSBase<>nil then CloseLibrary(MOS_DOSBase);
  115. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  116. { If in Ambient mode, replying WBMsg }
  117. if MOS_ambMsg<>nil then begin
  118. Forbid;
  119. ReplyMsg(MOS_ambMsg);
  120. end;
  121. haltproc(ExitCode);
  122. end;
  123. {*****************************************************************************
  124. Parameterhandling
  125. as include in amicommon
  126. *****************************************************************************}
  127. {$I paramhandling.inc}
  128. {*****************************************************************************
  129. Randomize
  130. *****************************************************************************}
  131. { set randseed to a new pseudo random value }
  132. procedure randomize;
  133. var tmpTime: TDateStamp;
  134. begin
  135. DateStamp(@tmpTime);
  136. randseed:=tmpTime.ds_tick;
  137. end;
  138. { MorphOS specific startup }
  139. procedure SysInitMorphOS;
  140. var self: PProcess;
  141. begin
  142. self:=PProcess(FindTask(nil));
  143. if self^.pr_CLI=0 then begin
  144. { if we're running from Ambient/Workbench, we catch its message }
  145. WaitPort(@self^.pr_MsgPort);
  146. MOS_ambMsg:=GetMsg(@self^.pr_MsgPort);
  147. end;
  148. MOS_DOSBase:=OpenLibrary('dos.library',50);
  149. if MOS_DOSBase=nil then Halt(1);
  150. MOS_UtilityBase:=OpenLibrary('utility.library',50);
  151. if MOS_UtilityBase=nil then Halt(1);
  152. { Creating the memory pool for growing heap }
  153. ASYS_heapPool:=CreatePool(MEMF_FAST or MEMF_SEM_PROTECTED,growheapsize2,growheapsize1);
  154. if ASYS_heapPool=nil then Halt(1);
  155. { Initialize semaphore for filelist access arbitration }
  156. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  157. if ASYS_fileSemaphore = nil then Halt(1);
  158. InitSemaphore(ASYS_fileSemaphore);
  159. if MOS_ambMsg=nil then begin
  160. MOS_ConHandle:=0;
  161. StdInputHandle:=dosInput;
  162. StdOutputHandle:=dosOutput;
  163. StdErrorHandle:=StdOutputHandle;
  164. end else begin
  165. MOS_ConHandle:=Open(MOS_ConName,MODE_OLDFILE);
  166. if MOS_ConHandle<>0 then begin
  167. StdInputHandle:=MOS_ConHandle;
  168. StdOutputHandle:=MOS_ConHandle;
  169. StdErrorHandle:=MOS_ConHandle;
  170. end else
  171. Halt(1);
  172. end;
  173. end;
  174. procedure SysInitStdIO;
  175. begin
  176. OpenStdIO(Input,fmInput,StdInputHandle);
  177. OpenStdIO(Output,fmOutput,StdOutputHandle);
  178. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  179. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  180. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  181. end;
  182. function GetProcessID: SizeUInt;
  183. begin
  184. GetProcessID:=SizeUInt(FindTask(NIL));
  185. end;
  186. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  187. begin
  188. result := stklen;
  189. end;
  190. begin
  191. IsConsole := TRUE;
  192. StackLength := CheckInitialStkLen(InitialStkLen);
  193. StackBottom := Sptr - StackLength;
  194. { OS specific startup }
  195. MOS_ambMsg:=nil;
  196. ASYS_origDir:=0;
  197. ASYS_fileList:=nil;
  198. envp:=nil;
  199. SysInitMorphOS;
  200. { Set up signals handlers }
  201. // InstallSignals;
  202. { Setup heap }
  203. InitHeap;
  204. SysInitExceptions;
  205. initunicodestringmanager;
  206. { Setup stdin, stdout and stderr }
  207. SysInitStdIO;
  208. { Reset IO Error }
  209. InOutRes:=0;
  210. { Arguments }
  211. GenerateArgs;
  212. InitSystemThreads;
  213. InitSystemDynLibs;
  214. end.