system.pp 11 KB

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