system.pp 10 KB

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