system.pp 11 KB

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