system.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004-2006 by Karoly Balogh
  4. System unit for AmigaOS 3.x/4.x
  5. Uses parts of the Free Pascal 1.0.x for Commodore Amiga/68k port
  6. by Carl Eric Codere and Nils Sjoholm
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit System;
  14. interface
  15. {$define FPC_IS_SYSTEM}
  16. {$I systemh.inc}
  17. {$I osdebugh.inc}
  18. {$ifdef cpum68k}
  19. {$define fpc_softfpu_interface}
  20. {$i softfpu.pp}
  21. {$undef fpc_softfpu_interface}
  22. {$endif cpum68k}
  23. const
  24. LineEnding = #10;
  25. LFNSupport = True;
  26. DirectorySeparator = '/';
  27. DriveSeparator = ':';
  28. ExtensionSeparator = '.';
  29. PathSeparator = ';';
  30. AllowDirectorySeparators : set of char = ['\','/'];
  31. AllowDriveSeparators : set of char = [':'];
  32. maxExitCode = 255;
  33. MaxPathLen = 256;
  34. AllFilesMask = '#?';
  35. const
  36. UnusedHandle : LongInt = -1;
  37. StdInputHandle : LongInt = 0;
  38. StdOutputHandle : LongInt = 0;
  39. StdErrorHandle : LongInt = 0;
  40. FileNameCaseSensitive : Boolean = False;
  41. FileNameCasePreserving: boolean = True;
  42. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  43. sLineBreak = LineEnding;
  44. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  45. BreakOn : Boolean = True;
  46. { FIX ME: remove the kludge required by amunits package, which needs a huge rework }
  47. var
  48. AOS_ExecBase : Pointer; external name '_ExecBase';
  49. _ExecBase: Pointer; external name '_ExecBase'; { amunits compatibility kludge }
  50. AOS_DOSBase : Pointer; public name '_DOSBase'; { the "public" part is amunits compatibility kludge }
  51. _DOSBase: Pointer; external name '_DOSBase'; { amunits compatibility kludge }
  52. AOS_UtilityBase: Pointer; public name '_UtilityBase'; { the "public" part is amunits compatibility kludge }
  53. _UtilityBase: Pointer; external name '_UtilityBase'; { amunits compatibility kludge }
  54. AOS_IntuitionBase: Pointer; public name '_IntuitionBase'; { amunits compatibility kludge }
  55. _IntuitionBase: Pointer; external name '_IntuitionBase'; { amunits compatibility kludge }
  56. {$IFDEF AMIGAOS4}
  57. {$WARNING iExec, iDOS and iUtility should be typed pointer later}
  58. var
  59. IExec : Pointer; external name '_IExec';
  60. IDOS : Pointer;
  61. IUtility : Pointer;
  62. {$ENDIF}
  63. ASYS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  64. ASYS_heapSemaphore: Pointer; { 68k OS from 3.x has no MEMF_SEM_PROTECTED for pools, have to do it ourselves }
  65. ASYS_origDir : LongInt; { original directory on startup }
  66. AOS_wbMsg : Pointer; public name '_WBenchMsg'; { the "public" part is amunits compatibility kludge }
  67. _WBenchMsg : Pointer; external name '_WBenchMsg'; { amunits compatibility kludge }
  68. AOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  69. AOS_ConHandle: LongInt;
  70. argc: LongInt;
  71. argv: PPChar;
  72. envp: PPChar;
  73. implementation
  74. {$ifdef cpum68k}
  75. {$define fpc_softfpu_implementation}
  76. {$i softfpu.pp}
  77. {$undef fpc_softfpu_implementation}
  78. { we get these functions and types from the softfpu code }
  79. {$define FPC_SYSTEM_HAS_float64}
  80. {$define FPC_SYSTEM_HAS_float32}
  81. {$define FPC_SYSTEM_HAS_flag}
  82. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  83. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  84. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  85. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  86. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  87. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  88. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  89. {$endif cpum68k}
  90. {$I system.inc}
  91. {$I osdebug.inc}
  92. {$IFDEF AMIGAOS4}
  93. // Required to allow opening of utility library interface...
  94. {$include utilf.inc}
  95. {$ENDIF}
  96. {$IFDEF ASYS_FPC_FILEDEBUG}
  97. {$WARNING Compiling with file debug enabled!}
  98. {$ENDIF}
  99. {$IFDEF ASYS_FPC_MEMDEBUG}
  100. {$WARNING Compiling with memory debug enabled!}
  101. {$ENDIF}
  102. {*****************************************************************************
  103. Misc. System Dependent Functions
  104. *****************************************************************************}
  105. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  106. procedure System_exit;
  107. var
  108. oldDirLock: LongInt;
  109. begin
  110. { We must remove the CTRL-C FLAG here because halt }
  111. { may call I/O routines, which in turn might call }
  112. { halt, so a recursive stack crash }
  113. if BreakOn then begin
  114. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  115. SetSignal(0,SIGBREAKF_CTRL_C);
  116. end;
  117. { Closing opened files }
  118. CloseList(ASYS_fileList);
  119. { Changing back to original directory if changed }
  120. if ASYS_origDir<>0 then begin
  121. oldDirLock:=CurrentDir(ASYS_origDir);
  122. { unlock our lock if its safe, so we won't leak the lock }
  123. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  124. Unlock(oldDirLock);
  125. end;
  126. {$IFDEF AMIGAOS4}
  127. if iDOS<>nil then DropInterface(iDOS);
  128. if iUtility<>nil then DropInterface(iUtility);
  129. {$ENDIF}
  130. if AOS_IntuitionBase<>nil then CloseLibrary(AOS_IntuitionBase); { amunits kludge }
  131. if AOS_UtilityBase<>nil then CloseLibrary(AOS_UtilityBase);
  132. if AOS_DOSBase<>nil then CloseLibrary(AOS_DOSBase);
  133. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  134. { If in Workbench mode, replying WBMsg }
  135. if AOS_wbMsg<>nil then begin
  136. Forbid;
  137. ReplyMsg(AOS_wbMsg);
  138. end;
  139. haltproc(ExitCode);
  140. end;
  141. { Generates correct argument array on startup }
  142. procedure GenerateArgs;
  143. var
  144. argvlen : longint;
  145. procedure allocarg(idx,len:longint);
  146. var
  147. i,oldargvlen : longint;
  148. begin
  149. if idx>=argvlen then
  150. begin
  151. oldargvlen:=argvlen;
  152. argvlen:=(idx+8) and (not 7);
  153. sysreallocmem(argv,argvlen*sizeof(pointer));
  154. for i:=oldargvlen to argvlen-1 do
  155. argv[i]:=nil;
  156. end;
  157. ArgV [Idx] := SysAllocMem (Succ (Len));
  158. end;
  159. var
  160. count: word;
  161. start: word;
  162. localindex: word;
  163. p : pchar;
  164. temp : string;
  165. begin
  166. p:=GetArgStr;
  167. argvlen:=0;
  168. { Set argv[0] }
  169. temp:=paramstr(0);
  170. allocarg(0,length(temp));
  171. move(temp[1],argv[0]^,length(temp));
  172. argv[0][length(temp)]:=#0;
  173. { check if we're started from Ambient }
  174. if AOS_wbMsg<>nil then
  175. begin
  176. argc:=0;
  177. exit;
  178. end;
  179. { Handle the other args }
  180. count:=0;
  181. { first index is one }
  182. localindex:=1;
  183. while (p[count]<>#0) do
  184. begin
  185. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  186. start:=count;
  187. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  188. if (count-start>0) then
  189. begin
  190. allocarg(localindex,count-start);
  191. move(p[start],argv[localindex]^,count-start);
  192. argv[localindex][count-start]:=#0;
  193. inc(localindex);
  194. end;
  195. end;
  196. argc:=localindex;
  197. end;
  198. function GetProgDir: String;
  199. var
  200. s1 : String;
  201. alock : LongInt;
  202. counter: Byte;
  203. begin
  204. GetProgDir:='';
  205. FillChar(s1,255,#0);
  206. { GetLock of program directory }
  207. alock:=GetProgramDir;
  208. if alock<>0 then begin
  209. if NameFromLock(alock,@s1[1],255) then begin
  210. counter:=1;
  211. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  212. s1[0]:=Char(counter-1);
  213. GetProgDir:=s1;
  214. end;
  215. end;
  216. end;
  217. function GetProgramName: String;
  218. { Returns ONLY the program name }
  219. var
  220. s1 : String;
  221. counter: Byte;
  222. begin
  223. GetProgramName:='';
  224. FillChar(s1,255,#0);
  225. if GetProgramName(@s1[1],255) then begin
  226. { now check out and assign the length of the string }
  227. counter := 1;
  228. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  229. s1[0]:=Char(counter-1);
  230. { now remove any component path which should not be there }
  231. for counter:=length(s1) downto 1 do
  232. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  233. { readjust counterv to point to character }
  234. if counter<>1 then Inc(counter);
  235. GetProgramName:=copy(s1,counter,length(s1));
  236. end;
  237. end;
  238. {*****************************************************************************
  239. ParamStr/Randomize
  240. *****************************************************************************}
  241. { number of args }
  242. function paramcount : longint;
  243. begin
  244. if AOS_wbMsg<>nil then
  245. paramcount:=0
  246. else
  247. paramcount:=argc-1;
  248. end;
  249. { argument number l }
  250. function paramstr(l : longint) : string;
  251. var
  252. s1: String;
  253. begin
  254. paramstr:='';
  255. if AOS_wbMsg<>nil then exit;
  256. if l=0 then begin
  257. s1:=GetProgDir;
  258. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  259. else paramstr:=s1+'/'+GetProgramName;
  260. end else begin
  261. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  262. end;
  263. end;
  264. { set randseed to a new pseudo random value }
  265. procedure randomize;
  266. var tmpTime: TDateStamp;
  267. begin
  268. DateStamp(@tmpTime);
  269. randseed:=tmpTime.ds_tick;
  270. end;
  271. { AmigaOS specific startup }
  272. procedure SysInitAmigaOS;
  273. var self: PProcess;
  274. begin
  275. self:=PProcess(FindTask(nil));
  276. if self^.pr_CLI=0 then begin
  277. { if we're running from Ambient/Workbench, we catch its message }
  278. WaitPort(@self^.pr_MsgPort);
  279. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  280. end;
  281. AOS_DOSBase:=OpenLibrary('dos.library',37);
  282. if AOS_DOSBase=nil then Halt(1);
  283. AOS_UtilityBase:=OpenLibrary('utility.library',37);
  284. if AOS_UtilityBase=nil then Halt(1);
  285. AOS_IntuitionBase:=OpenLibrary('intuition.library',37); { amunits support kludge }
  286. if AOS_IntuitionBase=nil then Halt(1);
  287. {$IFDEF AMIGAOS4}
  288. { Open main interfaces on OS4 }
  289. iDOS := GetInterface(AOS_DOSBase,'main',1,nil);
  290. iUtility := GetInterface(AOS_UtilityBase,'main',1,nil);
  291. {$ENDIF}
  292. { Creating the memory pool for growing heap }
  293. ASYS_heapPool:=CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  294. if ASYS_heapPool=nil then Halt(1);
  295. ASYS_heapSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  296. InitSemaphore(ASYS_heapSemaphore);
  297. if AOS_wbMsg=nil then begin
  298. StdInputHandle:=dosInput;
  299. StdOutputHandle:=dosOutput;
  300. StdErrorHandle:=StdOutputHandle;
  301. end else begin
  302. AOS_ConHandle:=Open(AOS_ConName,MODE_OLDFILE);
  303. if AOS_ConHandle<>0 then begin
  304. StdInputHandle:=AOS_ConHandle;
  305. StdOutputHandle:=AOS_ConHandle;
  306. StdErrorHandle:=AOS_ConHandle;
  307. end else
  308. Halt(1);
  309. end;
  310. end;
  311. procedure SysInitStdIO;
  312. begin
  313. OpenStdIO(Input,fmInput,StdInputHandle);
  314. OpenStdIO(Output,fmOutput,StdOutputHandle);
  315. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  316. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  317. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  318. end;
  319. function GetProcessID: SizeUInt;
  320. begin
  321. GetProcessID:=SizeUInt(FindTask(NIL));
  322. end;
  323. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  324. begin
  325. result := stklen;
  326. end;
  327. begin
  328. IsConsole := TRUE;
  329. SysResetFPU;
  330. if not(IsLibrary) then
  331. SysInitFPU;
  332. StackLength := CheckInitialStkLen(InitialStkLen);
  333. StackBottom := Sptr - StackLength;
  334. { OS specific startup }
  335. AOS_wbMsg:=nil;
  336. ASYS_origDir:=0;
  337. ASYS_fileList:=nil;
  338. envp:=nil;
  339. SysInitAmigaOS;
  340. { Set up signals handlers }
  341. // InstallSignals;
  342. { Setup heap }
  343. InitHeap;
  344. SysInitExceptions;
  345. initunicodestringmanager;
  346. { Setup stdin, stdout and stderr }
  347. SysInitStdIO;
  348. { Reset IO Error }
  349. InOutRes:=0;
  350. { Arguments }
  351. GenerateArgs;
  352. InitSystemThreads;
  353. end.