system.pp 9.6 KB

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