system.pp 10 KB

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