system.pp 9.3 KB

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