system.pp 11 KB

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