system.pp 9.5 KB

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