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