system.pp 11 KB

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