system.pp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004-2006 by Karoly Balogh
  4. AROS conversion
  5. Copyright (c) 2011 by Marcus Sackrow
  6. System unit for AROS
  7. Uses parts of the Free Pascal 1.0.x for Commodore Amiga/68k port
  8. by Carl Eric Codere and Nils Sjoholm
  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 : THandle = 0;
  34. StdInputHandle : THandle = 0;
  35. StdOutputHandle : THandle = 0;
  36. StdErrorHandle : THandle = 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. AOS_ExecBase : Pointer; external name '_ExecBase';
  45. AOS_DOSBase : Pointer;
  46. AOS_UtilityBase: Pointer;
  47. AROS_ThreadLib : Pointer; public name 'AROS_THREADLIB';
  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. AOS_wbMsg : Pointer;
  52. AOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  53. AOS_ConHandle: THandle;
  54. SysDebugBase: Pointer = nil;
  55. argc: LongInt;
  56. argv: PPChar;
  57. envp: PPChar;
  58. killed : Boolean = False;
  59. function GetLibAdress(Base: Pointer; Offset: LongInt): Pointer;
  60. procedure Debug(s: string);
  61. procedure Debugln(s: string);
  62. procedure EnableBackTraceStr;
  63. implementation
  64. {$I system.inc}
  65. {$I osdebug.inc}
  66. type
  67. PWBArg = ^TWBArg;
  68. TWBArg = record
  69. wa_Lock : LongInt; { a lock descriptor }
  70. wa_Name : PChar; { a string relative to that lock }
  71. end;
  72. WBArgList = array[1..MaxInt] of TWBArg; { Only 1..smNumArgs are valid }
  73. PWBArgList = ^WBArgList;
  74. PWBStartup = ^TWBStartup;
  75. TWBStartup = record
  76. sm_Message : TMessage; { a standard message structure }
  77. sm_Process : Pointer; { the process descriptor for you }
  78. sm_Segment : Pointer; { a descriptor for your code }
  79. sm_NumArgs : Longint; { the number of elements in ArgList }
  80. sm_ToolWindow : Pointer; { description of window }
  81. sm_ArgList : PWBArgList; { the arguments themselves }
  82. end;
  83. {*****************************************************************************
  84. Misc. System Dependent Functions
  85. *****************************************************************************}
  86. procedure haltproc(e:longint); cdecl; external name '_haltproc';
  87. procedure System_exit;
  88. var
  89. oldDirLock: LongInt;
  90. begin
  91. if Killed then
  92. Exit;
  93. Killed := True;
  94. { Dispose the thread init/exit chains }
  95. CleanupThreadProcChain(threadInitProcList);
  96. CleanupThreadProcChain(threadExitProcList);
  97. { Closing opened files }
  98. CloseList(ASYS_fileList);
  99. { Changing back to original directory if changed }
  100. if ASYS_OrigDir <> 0 then begin
  101. oldDirLock:=CurrentDir(ASYS_origDir);
  102. { unlock our lock if its safe, so we won't leak the lock }
  103. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  104. Unlock(oldDirLock);
  105. end;
  106. // debug lib
  107. if SysDebugBase <> nil then
  108. CloseLibrary(SysDebugBase);
  109. SysDebugBase := nil;
  110. // utility
  111. if AOS_UtilityBase <> nil then
  112. CloseLibrary(AOS_UtilityBase);
  113. // Heap
  114. if ASYS_heapPool <> nil then
  115. DeletePool(ASYS_heapPool);
  116. AOS_UtilityBase := nil;
  117. ASYS_HeapPool := nil;
  118. // dos
  119. if AOS_DOSBase<>nil then
  120. CloseLibrary(AOS_DOSBase);
  121. AOS_DOSBase := nil;
  122. //
  123. if AOS_wbMsg <> nil then
  124. begin
  125. // forbid -> Amiga RKM Libraries Manual
  126. Forbid();
  127. // Reply WBStartupMessage
  128. ReplyMsg(AOS_wbMsg);
  129. end;
  130. //
  131. HaltProc(ExitCode);
  132. end;
  133. {*****************************************************************************
  134. Parameterhandling
  135. as include in amicommon
  136. *****************************************************************************}
  137. {$I paramhandling.inc}
  138. {*****************************************************************************
  139. Randomize
  140. *****************************************************************************}
  141. { set randseed to a new pseudo random value }
  142. procedure Randomize;
  143. var
  144. tmpTime: TDateStamp;
  145. begin
  146. DateStamp(@tmpTime);
  147. randseed := tmpTime.ds_tick;
  148. end;
  149. { AmigaOS specific startup }
  150. procedure SysInitAmigaOS;
  151. var
  152. self: PProcess;
  153. begin
  154. self := PProcess(FindTask(nil));
  155. if self^.pr_CLI = 0 then begin
  156. { if we're running from Ambient/Workbench, we catch its message }
  157. WaitPort(@self^.pr_MsgPort);
  158. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  159. end;
  160. AOS_DOSBase := OpenLibrary('dos.library', 0);
  161. if AOS_DOSBase = nil then
  162. Halt(1);
  163. AOS_UtilityBase := OpenLibrary('utility.library', 0);
  164. if AOS_UtilityBase = nil then
  165. Halt(1);
  166. { Creating the memory pool for growing heap }
  167. ASYS_heapPool := CreatePool(MEMF_ANY or MEMF_SEM_PROTECTED, growheapsize2, growheapsize1);
  168. if ASYS_heapPool = nil then
  169. Halt(1);
  170. { Initialize semaphore for filelist access arbitration }
  171. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  172. if ASYS_fileSemaphore = nil then
  173. Halt(1);
  174. InitSemaphore(ASYS_fileSemaphore);
  175. if AOS_wbMsg = nil then begin
  176. StdInputHandle := THandle(dosInput);
  177. StdOutputHandle := THandle(dosOutput);
  178. {$ifdef CPU64}
  179. StdErrorHandle := THandle(DosOutput);
  180. {$else}
  181. StdErrorHandle := THandle(DosError1);
  182. {$endif}
  183. end else begin
  184. AOS_ConHandle := Open(AOS_ConName, MODE_OLDFILE);
  185. if AOS_ConHandle <> 0 then begin
  186. StdInputHandle := AOS_ConHandle;
  187. StdOutputHandle := AOS_ConHandle;
  188. StdErrorHandle := AOS_ConHandle;
  189. end else
  190. Halt(1);
  191. end;
  192. end;
  193. function AROSBackTraceStr(Addr: CodePointer): ShortString;
  194. const
  195. DL_Dummy = TAG_USER + $03e00000;
  196. DL_ModuleName = DL_Dummy + 1;
  197. DL_SymbolName = DL_Dummy + 7;
  198. var
  199. SymName, ModName: PChar;
  200. Tags: array[0..5] of PtrUInt;
  201. s: AnsiString;
  202. Res: AnsiString;
  203. begin
  204. if Assigned(SysDebugBase) then
  205. begin
  206. ModName := nil;
  207. SymName := nil;
  208. Tags[0] := DL_Modulename;
  209. Tags[1] := PtrUInt(@ModName);
  210. Tags[2] := DL_SymbolName;
  211. Tags[3] := PtrUInt(@SymName);
  212. Tags[4] := 0;
  213. Tags[5] := 0;
  214. DecodeLocation(Addr, @Tags[0]);
  215. s := '-';
  216. if not Assigned(ModName) then
  217. ModName := @S[1];
  218. if not Assigned(SymName) then
  219. SymName := @S[1];
  220. Res := ' $' + HexStr(Addr) + ' ' + ModName + ' ' + SymName;
  221. AROSBackTraceStr := Copy(Res, 1, 254);
  222. end
  223. else
  224. begin
  225. AROSBackTraceStr := ' $' + HexStr(Addr) + ' - ';
  226. end;
  227. end;
  228. procedure EnableBackTraceStr;
  229. begin
  230. if not Assigned(SysDebugBase) then
  231. begin
  232. SysDebugBase := OpenLibrary('debug.library', 0);
  233. if Assigned(SysDebugBase) then
  234. BackTraceStrFunc := @AROSBackTraceStr;
  235. end;
  236. end;
  237. procedure SysInitStdIO;
  238. begin
  239. OpenStdIO(Input,fmInput,StdInputHandle);
  240. OpenStdIO(Output,fmOutput,StdOutputHandle);
  241. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  242. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  243. end;
  244. function GetProcessID: SizeUInt;
  245. begin
  246. GetProcessID := SizeUInt(FindTask(NIL));
  247. end;
  248. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  249. begin
  250. result := stklen;
  251. end;
  252. begin
  253. IsConsole := TRUE;
  254. SysResetFPU;
  255. if not (IsLibrary) then
  256. SysInitFPU;
  257. StackLength := CheckInitialStkLen(InitialStkLen);
  258. StackBottom := Sptr - StackLength;
  259. { OS specific startup }
  260. AOS_wbMsg := nil;
  261. ASYS_origDir := 0;
  262. ASYS_fileList := nil;
  263. envp := nil;
  264. SysInitAmigaOS;
  265. { Set up signals handlers }
  266. //InstallSignals;
  267. { Setup heap }
  268. InitHeap;
  269. SysInitExceptions;
  270. initunicodestringmanager;
  271. { Setup stdin, stdout and stderr }
  272. SysInitStdIO;
  273. { Reset IO Error }
  274. InOutRes:=0;
  275. { Arguments }
  276. GenerateArgs;
  277. InitSystemThreads;
  278. InitSystemDynLibs;
  279. end.