system.pp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. StdErrorHandle := THandle(DosError1);
  179. end else begin
  180. AOS_ConHandle := Open(AOS_ConName, MODE_OLDFILE);
  181. if AOS_ConHandle <> 0 then begin
  182. StdInputHandle := AOS_ConHandle;
  183. StdOutputHandle := AOS_ConHandle;
  184. StdErrorHandle := AOS_ConHandle;
  185. end else
  186. Halt(1);
  187. end;
  188. end;
  189. function AROSBackTraceStr(Addr: CodePointer): ShortString;
  190. const
  191. DL_Dummy = TAG_USER + $03e00000;
  192. DL_ModuleName = DL_Dummy + 1;
  193. DL_SymbolName = DL_Dummy + 7;
  194. var
  195. SymName, ModName: PChar;
  196. Tags: array[0..5] of PtrUInt;
  197. s: AnsiString;
  198. Res: AnsiString;
  199. begin
  200. if Assigned(SysDebugBase) then
  201. begin
  202. ModName := nil;
  203. SymName := nil;
  204. Tags[0] := DL_Modulename;
  205. Tags[1] := PtrUInt(@ModName);
  206. Tags[2] := DL_SymbolName;
  207. Tags[3] := PtrUInt(@SymName);
  208. Tags[4] := 0;
  209. Tags[5] := 0;
  210. DecodeLocation(Addr, @Tags[0]);
  211. s := '-';
  212. if not Assigned(ModName) then
  213. ModName := @S[1];
  214. if not Assigned(SymName) then
  215. SymName := @S[1];
  216. Res := ' $' + HexStr(Addr) + ' ' + ModName + ' ' + SymName;
  217. AROSBackTraceStr := Copy(Res, 1, 254);
  218. end
  219. else
  220. begin
  221. AROSBackTraceStr := ' $' + HexStr(Addr) + ' - ';
  222. end;
  223. end;
  224. procedure EnableBackTraceStr;
  225. begin
  226. if not Assigned(SysDebugBase) then
  227. begin
  228. SysDebugBase := OpenLibrary('debug.library', 0);
  229. if Assigned(SysDebugBase) then
  230. BackTraceStrFunc := @AROSBackTraceStr;
  231. end;
  232. end;
  233. procedure SysInitStdIO;
  234. begin
  235. OpenStdIO(Input,fmInput,StdInputHandle);
  236. OpenStdIO(Output,fmOutput,StdOutputHandle);
  237. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  238. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  239. end;
  240. function GetProcessID: SizeUInt;
  241. begin
  242. GetProcessID := SizeUInt(FindTask(NIL));
  243. end;
  244. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  245. begin
  246. result := stklen;
  247. end;
  248. begin
  249. IsConsole := TRUE;
  250. SysResetFPU;
  251. if not (IsLibrary) then
  252. SysInitFPU;
  253. StackLength := CheckInitialStkLen(InitialStkLen);
  254. StackBottom := Sptr - 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. { Setup heap }
  264. InitHeap;
  265. SysInitExceptions;
  266. initunicodestringmanager;
  267. { Setup stdin, stdout and stderr }
  268. SysInitStdIO;
  269. { Reset IO Error }
  270. InOutRes:=0;
  271. { Arguments }
  272. GenerateArgs;
  273. InitSystemThreads;
  274. InitSystemDynLibs;
  275. end.