system.pp 9.3 KB

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