system.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004 by Karoly Balogh for Genesi S.a.r.l.
  4. System unit for MorphOS/PowerPC
  5. Uses parts of the Commodore Amiga/68k port by Carl Eric Codere
  6. and Nils Sjoholm
  7. MorphOS port was done on a free Pegasos II/G4 machine
  8. provided by Genesi S.a.r.l. <www.genesi.lu>
  9. See the file COPYING.FPC, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. **********************************************************************}
  15. unit System;
  16. interface
  17. {$define FPC_IS_SYSTEM}
  18. {$I systemh.inc}
  19. const
  20. LineEnding = #10;
  21. LFNSupport = True;
  22. DirectorySeparator = '/';
  23. DriveSeparator = ':';
  24. PathSeparator = ';';
  25. maxExitCode = 255;
  26. MaxPathLen = 256;
  27. const
  28. UnusedHandle : LongInt = -1;
  29. StdInputHandle : LongInt = 0;
  30. StdOutputHandle : LongInt = 0;
  31. StdErrorHandle : LongInt = 0;
  32. FileNameCaseSensitive : Boolean = False;
  33. CtrlZMarksEOF: boolean = false; { #26 not considered as end of file }
  34. sLineBreak : string[1] = LineEnding;
  35. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  36. BreakOn : Boolean = True;
  37. var
  38. MOS_ExecBase : Pointer; external name '_ExecBase';
  39. MOS_DOSBase : Pointer;
  40. MOS_UtilityBase: Pointer;
  41. MOS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  42. MOS_origDir : LongInt; { original directory on startup }
  43. MOS_ambMsg : Pointer;
  44. MOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  45. MOS_ConHandle: LongInt;
  46. argc: LongInt;
  47. argv: PPChar;
  48. envp: PPChar;
  49. implementation
  50. {$I system.inc}
  51. {$IFDEF MOSFPC_FILEDEBUG}
  52. {$WARNING Compiling with file debug enabled!}
  53. {$ENDIF}
  54. {$IFDEF MOSFPC_MEMDEBUG}
  55. {$WARNING Compiling with memory debug enabled!}
  56. {$ENDIF}
  57. {*****************************************************************************
  58. Misc. System Dependent Functions
  59. *****************************************************************************}
  60. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  61. procedure System_exit;
  62. begin
  63. { We must remove the CTRL-C FLAG here because halt }
  64. { may call I/O routines, which in turn might call }
  65. { halt, so a recursive stack crash }
  66. if BreakOn then begin
  67. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  68. SetSignal(0,SIGBREAKF_CTRL_C);
  69. end;
  70. { Closing opened files }
  71. CloseList(MOS_fileList);
  72. { Changing back to original directory if changed }
  73. if MOS_origDir<>0 then begin
  74. CurrentDir(MOS_origDir);
  75. end;
  76. { Closing CON: when in Ambient mode }
  77. if MOS_ConHandle<>0 then dosClose(MOS_ConHandle);
  78. if MOS_UtilityBase<>nil then CloseLibrary(MOS_UtilityBase);
  79. if MOS_DOSBase<>nil then CloseLibrary(MOS_DOSBase);
  80. if MOS_heapPool<>nil then DeletePool(MOS_heapPool);
  81. { If in Ambient mode, replying WBMsg }
  82. if MOS_ambMsg<>nil then begin
  83. Forbid;
  84. ReplyMsg(MOS_ambMsg);
  85. end;
  86. haltproc(ExitCode);
  87. end;
  88. { Generates correct argument array on startup }
  89. procedure GenerateArgs;
  90. var
  91. argvlen : longint;
  92. procedure allocarg(idx,len:longint);
  93. var
  94. i,oldargvlen : longint;
  95. begin
  96. if idx>=argvlen then
  97. begin
  98. oldargvlen:=argvlen;
  99. argvlen:=(idx+8) and (not 7);
  100. sysreallocmem(argv,argvlen*sizeof(pointer));
  101. for i:=oldargvlen to argvlen-1 do
  102. argv[i]:=nil;
  103. end;
  104. ArgV [Idx] := SysAllocMem (Succ (Len));
  105. end;
  106. var
  107. count: word;
  108. start: word;
  109. localindex: word;
  110. p : pchar;
  111. temp : string;
  112. begin
  113. p:=GetArgStr;
  114. argvlen:=0;
  115. { Set argv[0] }
  116. temp:=paramstr(0);
  117. allocarg(0,length(temp));
  118. move(temp[1],argv[0]^,length(temp));
  119. argv[0][length(temp)]:=#0;
  120. { check if we're started from Ambient }
  121. if MOS_ambMsg<>nil then begin
  122. argc:=0;
  123. exit;
  124. end;
  125. { Handle the other args }
  126. count:=0;
  127. { first index is one }
  128. localindex:=1;
  129. while (p[count]<>#0) do
  130. begin
  131. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  132. start:=count;
  133. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  134. if (count-start>0) then
  135. begin
  136. allocarg(localindex,count-start);
  137. move(p[start],argv[localindex]^,count-start);
  138. argv[localindex][count-start]:=#0;
  139. inc(localindex);
  140. end;
  141. end;
  142. argc:=localindex;
  143. end;
  144. function GetProgDir: String;
  145. var
  146. s1 : String;
  147. alock : LongInt;
  148. counter: Byte;
  149. begin
  150. GetProgDir:='';
  151. FillChar(s1,255,#0);
  152. { GetLock of program directory }
  153. alock:=GetProgramDir;
  154. if alock<>0 then begin
  155. if NameFromLock(alock,@s1[1],255) then begin
  156. counter:=1;
  157. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  158. s1[0]:=Char(counter-1);
  159. GetProgDir:=s1;
  160. end;
  161. end;
  162. end;
  163. function GetProgramName: String;
  164. { Returns ONLY the program name }
  165. var
  166. s1 : String;
  167. counter: Byte;
  168. begin
  169. GetProgramName:='';
  170. FillChar(s1,255,#0);
  171. if GetProgramName(@s1[1],255) then begin
  172. { now check out and assign the length of the string }
  173. counter := 1;
  174. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  175. s1[0]:=Char(counter-1);
  176. { now remove any component path which should not be there }
  177. for counter:=length(s1) downto 1 do
  178. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  179. { readjust counterv to point to character }
  180. if counter<>1 then Inc(counter);
  181. GetProgramName:=copy(s1,counter,length(s1));
  182. end;
  183. end;
  184. function GetArgv0Ambient: String;
  185. { Returns program full path+name, when in Ambient mode }
  186. { Required for paramstr(0) support in Ambient mode }
  187. type
  188. pWBArg = ^tWBArg;
  189. tWBArg = record
  190. wa_Lock: longint;
  191. wa_Name: PChar;
  192. end;
  193. pWBStartup = ^tWBStartup;
  194. tWBStartup = packed record
  195. sm_Message : tMessage;
  196. sm_Process : pMsgPort;
  197. sm_Segment : longint;
  198. sm_NumArgs : longint;
  199. sm_ToolWindow: PChar;
  200. sm_ArgList : pWBArg;
  201. end;
  202. var
  203. tmpbuf : String;
  204. counter : longint;
  205. progname: PChar;
  206. dlock : longint;
  207. begin
  208. GetArgv0Ambient:='';
  209. if MOS_ambMsg<>nil then begin
  210. dlock:=pWBStartup(MOS_ambMsg)^.sm_argList^.wa_Lock;
  211. if dlock<>0 then begin
  212. FillDWord(tmpbuf,256 div 4,0);
  213. if NameFromLock(dlock,@tmpbuf[1],255) then begin
  214. counter:=1;
  215. while tmpbuf[counter]<>#0 do counter+=1;
  216. tmpbuf[0]:=Char(counter-1);
  217. GetArgv0Ambient:=tmpbuf;
  218. { Append slash,if we're not in root directory of a volume }
  219. if tmpbuf[counter-1]<>':' then GetArgv0Ambient+='/';
  220. end;
  221. end;
  222. { Fetch the progname, and copy it to the buffer }
  223. progname:=pWBStartup(MOS_ambMsg)^.sm_argList^.wa_Name;
  224. if progname<>nil then begin
  225. FillDWord(tmpbuf,256 div 4,0);
  226. counter:=0;
  227. while (progname[counter]<>#0) do begin
  228. tmpbuf[counter+1]:=progname[counter];
  229. counter+=1;
  230. end;
  231. tmpbuf[0]:=Char(counter);
  232. GetArgv0Ambient+=tmpbuf;
  233. end;
  234. end;
  235. end;
  236. {*****************************************************************************
  237. ParamStr/Randomize
  238. *****************************************************************************}
  239. { number of args }
  240. function paramcount : longint;
  241. begin
  242. if MOS_ambMsg<>nil then
  243. paramcount:=0
  244. else
  245. paramcount:=argc-1;
  246. end;
  247. { argument number l }
  248. function paramstr(l : longint) : string;
  249. var
  250. s1: String;
  251. begin
  252. paramstr:='';
  253. if MOS_ambMsg<>nil then begin
  254. if l=0 then begin
  255. paramstr:=GetArgv0Ambient;
  256. exit;
  257. end else
  258. exit;
  259. end;
  260. if l=0 then begin
  261. s1:=GetProgDir;
  262. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  263. else paramstr:=s1+'/'+GetProgramName;
  264. end else begin
  265. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  266. end;
  267. end;
  268. { set randseed to a new pseudo random value }
  269. procedure randomize;
  270. var tmpTime: TDateStamp;
  271. begin
  272. DateStamp(@tmpTime);
  273. randseed:=tmpTime.ds_tick;
  274. end;
  275. { MorphOS specific startup }
  276. procedure SysInitMorphOS;
  277. var self: PProcess;
  278. begin
  279. self:=PProcess(FindTask(nil));
  280. if self^.pr_CLI=0 then begin
  281. { if we're running from Ambient/Workbench, we catch its message }
  282. WaitPort(@self^.pr_MsgPort);
  283. MOS_ambMsg:=GetMsg(@self^.pr_MsgPort);
  284. end;
  285. MOS_DOSBase:=OpenLibrary('dos.library',50);
  286. if MOS_DOSBase=nil then Halt(1);
  287. MOS_UtilityBase:=OpenLibrary('utility.library',50);
  288. if MOS_UtilityBase=nil then Halt(1);
  289. { Creating the memory pool for growing heap }
  290. MOS_heapPool:=CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  291. if MOS_heapPool=nil then Halt(1);
  292. if MOS_ambMsg=nil then begin
  293. MOS_ConHandle:=0;
  294. StdInputHandle:=dosInput;
  295. StdOutputHandle:=dosOutput;
  296. end else begin
  297. MOS_ConHandle:=Open(MOS_ConName,MODE_OLDFILE);
  298. if MOS_ConHandle<>0 then begin
  299. StdInputHandle:=MOS_ConHandle;
  300. StdOutputHandle:=MOS_ConHandle;
  301. end else
  302. Halt(1);
  303. end;
  304. end;
  305. procedure SysInitStdIO;
  306. begin
  307. OpenStdIO(Input,fmInput,StdInputHandle);
  308. OpenStdIO(Output,fmOutput,StdOutputHandle);
  309. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  310. { * MorphOS doesn't have a separate stderr, just like AmigaOS (???) * }
  311. StdErrorHandle:=StdOutputHandle;
  312. // OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  313. // OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  314. end;
  315. function GetProcessID: SizeUInt;
  316. begin
  317. GetProcessID:=SizeUInt(FindTask(NIL));
  318. end;
  319. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  320. begin
  321. result := stklen;
  322. end;
  323. begin
  324. SysResetFPU;
  325. IsConsole := TRUE;
  326. IsLibrary := FALSE;
  327. StackLength := CheckInitialStkLen(InitialStkLen);
  328. StackBottom := Sptr - StackLength;
  329. { OS specific startup }
  330. MOS_ambMsg:=nil;
  331. MOS_origDir:=0;
  332. MOS_fileList:=nil;
  333. envp:=nil;
  334. SysInitMorphOS;
  335. { Set up signals handlers }
  336. // InstallSignals;
  337. { Setup heap }
  338. InitHeap;
  339. SysInitExceptions;
  340. { Setup stdin, stdout and stderr }
  341. SysInitStdIO;
  342. { Reset IO Error }
  343. InOutRes:=0;
  344. { Arguments }
  345. GenerateArgs;
  346. InitSystemThreads;
  347. initvariantmanager;
  348. initwidestringmanager;
  349. end.