system.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_fileSemaphore: Pointer; { mutex semaphore for filelist access arbitration }
  66. ASYS_origDir : LongInt; { original directory on startup }
  67. AOS_wbMsg : Pointer; public name '_WBenchMsg'; { the "public" part is amunits compatibility kludge }
  68. _WBenchMsg : Pointer; external name '_WBenchMsg'; { amunits compatibility kludge }
  69. AOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  70. AOS_ConHandle: LongInt;
  71. argc: LongInt;
  72. argv: PPChar;
  73. envp: PPChar;
  74. implementation
  75. {$ifdef cpum68k}
  76. {$define fpc_softfpu_implementation}
  77. {$i softfpu.pp}
  78. {$undef fpc_softfpu_implementation}
  79. { we get these functions and types from the softfpu code }
  80. {$define FPC_SYSTEM_HAS_float64}
  81. {$define FPC_SYSTEM_HAS_float32}
  82. {$define FPC_SYSTEM_HAS_flag}
  83. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  84. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  85. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  86. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  87. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  88. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  89. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  90. {$endif cpum68k}
  91. {$I system.inc}
  92. {$I osdebug.inc}
  93. {$I m68kamiga.inc}
  94. {$IFDEF AMIGAOS4}
  95. // Required to allow opening of utility library interface...
  96. {$include utilf.inc}
  97. {$ENDIF}
  98. {$IFDEF ASYS_FPC_FILEDEBUG}
  99. {$WARNING Compiling with file debug enabled!}
  100. {$ENDIF}
  101. {$IFDEF ASYS_FPC_MEMDEBUG}
  102. {$WARNING Compiling with memory debug enabled!}
  103. {$ENDIF}
  104. {*****************************************************************************
  105. Misc. System Dependent Functions
  106. *****************************************************************************}
  107. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  108. procedure System_exit;
  109. var
  110. oldDirLock: LongInt;
  111. begin
  112. { We must remove the CTRL-C FLAG here because halt }
  113. { may call I/O routines, which in turn might call }
  114. { halt, so a recursive stack crash }
  115. if BreakOn then begin
  116. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  117. SetSignal(0,SIGBREAKF_CTRL_C);
  118. end;
  119. { Closing opened files }
  120. CloseList(ASYS_fileList);
  121. { Changing back to original directory if changed }
  122. if ASYS_origDir<>0 then begin
  123. oldDirLock:=CurrentDir(ASYS_origDir);
  124. { unlock our lock if its safe, so we won't leak the lock }
  125. if (oldDirLock<>0) and (oldDirLock<>ASYS_origDir) then
  126. Unlock(oldDirLock);
  127. end;
  128. {$IFDEF AMIGAOS4}
  129. if iDOS<>nil then DropInterface(iDOS);
  130. if iUtility<>nil then DropInterface(iUtility);
  131. {$ENDIF}
  132. if AOS_IntuitionBase<>nil then CloseLibrary(AOS_IntuitionBase); { amunits kludge }
  133. if AOS_UtilityBase<>nil then CloseLibrary(AOS_UtilityBase);
  134. if AOS_DOSBase<>nil then CloseLibrary(AOS_DOSBase);
  135. if ASYS_heapPool<>nil then DeletePool(ASYS_heapPool);
  136. { If in Workbench mode, replying WBMsg }
  137. if AOS_wbMsg<>nil then begin
  138. Forbid;
  139. ReplyMsg(AOS_wbMsg);
  140. end;
  141. haltproc(ExitCode);
  142. end;
  143. { Generates correct argument array on startup }
  144. procedure GenerateArgs;
  145. var
  146. argvlen : longint;
  147. procedure allocarg(idx,len:longint);
  148. var
  149. i,oldargvlen : longint;
  150. begin
  151. if idx>=argvlen then
  152. begin
  153. oldargvlen:=argvlen;
  154. argvlen:=(idx+8) and (not 7);
  155. sysreallocmem(argv,argvlen*sizeof(pointer));
  156. for i:=oldargvlen to argvlen-1 do
  157. argv[i]:=nil;
  158. end;
  159. ArgV [Idx] := SysAllocMem (Succ (Len));
  160. end;
  161. var
  162. count: word;
  163. start: word;
  164. localindex: word;
  165. p : pchar;
  166. temp : string;
  167. begin
  168. p:=GetArgStr;
  169. argvlen:=0;
  170. { Set argv[0] }
  171. temp:=paramstr(0);
  172. allocarg(0,length(temp));
  173. move(temp[1],argv[0]^,length(temp));
  174. argv[0][length(temp)]:=#0;
  175. { check if we're started from Ambient }
  176. if AOS_wbMsg<>nil then
  177. begin
  178. argc:=0;
  179. exit;
  180. end;
  181. { Handle the other args }
  182. count:=0;
  183. { first index is one }
  184. localindex:=1;
  185. while (p[count]<>#0) do
  186. begin
  187. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  188. start:=count;
  189. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  190. if (count-start>0) then
  191. begin
  192. allocarg(localindex,count-start);
  193. move(p[start],argv[localindex]^,count-start);
  194. argv[localindex][count-start]:=#0;
  195. inc(localindex);
  196. end;
  197. end;
  198. argc:=localindex;
  199. end;
  200. function GetProgDir: String;
  201. var
  202. s1 : String;
  203. alock : LongInt;
  204. counter: Byte;
  205. begin
  206. GetProgDir:='';
  207. FillChar(s1,255,#0);
  208. { GetLock of program directory }
  209. alock:=GetProgramDir;
  210. if alock<>0 then begin
  211. if NameFromLock(alock,@s1[1],255) then begin
  212. counter:=1;
  213. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  214. s1[0]:=Char(counter-1);
  215. GetProgDir:=s1;
  216. end;
  217. end;
  218. end;
  219. function GetProgramName: String;
  220. { Returns ONLY the program name }
  221. var
  222. s1 : String;
  223. counter: Byte;
  224. begin
  225. GetProgramName:='';
  226. FillChar(s1,255,#0);
  227. if GetProgramName(@s1[1],255) then begin
  228. { now check out and assign the length of the string }
  229. counter := 1;
  230. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  231. s1[0]:=Char(counter-1);
  232. { now remove any component path which should not be there }
  233. for counter:=length(s1) downto 1 do
  234. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  235. { readjust counterv to point to character }
  236. if counter<>1 then Inc(counter);
  237. GetProgramName:=copy(s1,counter,length(s1));
  238. end;
  239. end;
  240. {*****************************************************************************
  241. ParamStr/Randomize
  242. *****************************************************************************}
  243. { number of args }
  244. function paramcount : longint;
  245. begin
  246. if AOS_wbMsg<>nil then
  247. paramcount:=0
  248. else
  249. paramcount:=argc-1;
  250. end;
  251. { argument number l }
  252. function paramstr(l : longint) : string;
  253. var
  254. s1: String;
  255. begin
  256. paramstr:='';
  257. if AOS_wbMsg<>nil then exit;
  258. if l=0 then begin
  259. s1:=GetProgDir;
  260. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  261. else paramstr:=s1+'/'+GetProgramName;
  262. end else begin
  263. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  264. end;
  265. end;
  266. { set randseed to a new pseudo random value }
  267. procedure randomize;
  268. var tmpTime: TDateStamp;
  269. begin
  270. DateStamp(@tmpTime);
  271. randseed:=tmpTime.ds_tick;
  272. end;
  273. { AmigaOS specific startup }
  274. procedure SysInitAmigaOS;
  275. var self: PProcess;
  276. begin
  277. self:=PProcess(FindTask(nil));
  278. if self^.pr_CLI=0 then begin
  279. { if we're running from Ambient/Workbench, we catch its message }
  280. WaitPort(@self^.pr_MsgPort);
  281. AOS_wbMsg:=GetMsg(@self^.pr_MsgPort);
  282. end;
  283. AOS_DOSBase:=OpenLibrary('dos.library',37);
  284. if AOS_DOSBase=nil then Halt(1);
  285. AOS_UtilityBase:=OpenLibrary('utility.library',37);
  286. if AOS_UtilityBase=nil then Halt(1);
  287. AOS_IntuitionBase:=OpenLibrary('intuition.library',37); { amunits support kludge }
  288. if AOS_IntuitionBase=nil then Halt(1);
  289. {$IFDEF AMIGAOS4}
  290. { Open main interfaces on OS4 }
  291. iDOS := GetInterface(AOS_DOSBase,'main',1,nil);
  292. iUtility := GetInterface(AOS_UtilityBase,'main',1,nil);
  293. {$ENDIF}
  294. { Creating the memory pool for growing heap }
  295. ASYS_heapPool:=CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  296. if ASYS_heapPool=nil then Halt(1);
  297. ASYS_heapSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  298. if ASYS_heapSemaphore = nil then Halt(1);
  299. InitSemaphore(ASYS_heapSemaphore);
  300. { Initialize semaphore for filelist access arbitration }
  301. ASYS_fileSemaphore:=AllocPooled(ASYS_heapPool,sizeof(TSignalSemaphore));
  302. if ASYS_fileSemaphore = nil then Halt(1);
  303. InitSemaphore(ASYS_fileSemaphore);
  304. if AOS_wbMsg=nil then begin
  305. StdInputHandle:=dosInput;
  306. StdOutputHandle:=dosOutput;
  307. StdErrorHandle:=StdOutputHandle;
  308. end else begin
  309. AOS_ConHandle:=Open(AOS_ConName,MODE_OLDFILE);
  310. if AOS_ConHandle<>0 then begin
  311. StdInputHandle:=AOS_ConHandle;
  312. StdOutputHandle:=AOS_ConHandle;
  313. StdErrorHandle:=AOS_ConHandle;
  314. end else
  315. Halt(1);
  316. end;
  317. end;
  318. procedure SysInitStdIO;
  319. begin
  320. OpenStdIO(Input,fmInput,StdInputHandle);
  321. OpenStdIO(Output,fmOutput,StdOutputHandle);
  322. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  323. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  324. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  325. end;
  326. function GetProcessID: SizeUInt;
  327. begin
  328. GetProcessID:=SizeUInt(FindTask(NIL));
  329. end;
  330. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  331. begin
  332. result := stklen;
  333. end;
  334. begin
  335. IsConsole := TRUE;
  336. SysResetFPU;
  337. if not(IsLibrary) then
  338. SysInitFPU;
  339. StackLength := CheckInitialStkLen(InitialStkLen);
  340. StackBottom := Sptr - StackLength;
  341. { OS specific startup }
  342. AOS_wbMsg:=nil;
  343. ASYS_origDir:=0;
  344. ASYS_fileList:=nil;
  345. envp:=nil;
  346. SysInitAmigaOS;
  347. { Set up signals handlers }
  348. // InstallSignals;
  349. { Setup heap }
  350. InitHeap;
  351. SysInitExceptions;
  352. initunicodestringmanager;
  353. { Setup stdin, stdout and stderr }
  354. SysInitStdIO;
  355. { Reset IO Error }
  356. InOutRes:=0;
  357. { Arguments }
  358. GenerateArgs;
  359. InitSystemThreads;
  360. end.