system.pp 10 KB

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