system.pp 10 KB

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