system.pp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. PathSeparator = ';';
  25. maxExitCode = 255;
  26. MaxPathLen = 256;
  27. const
  28. UnusedHandle : LongInt = -1;
  29. StdInputHandle : LongInt = 0;
  30. StdOutputHandle : LongInt = 0;
  31. StdErrorHandle : LongInt = 0;
  32. FileNameCaseSensitive : Boolean = False;
  33. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  34. sLineBreak : string[1] = LineEnding;
  35. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  36. BreakOn : Boolean = True;
  37. var
  38. MOS_ExecBase : Pointer; external name '_ExecBase';
  39. MOS_DOSBase : Pointer;
  40. MOS_UtilityBase: Pointer;
  41. MOS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  42. MOS_origDir : LongInt; { original directory on startup }
  43. MOS_ambMsg : Pointer;
  44. MOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  45. MOS_ConHandle: LongInt;
  46. argc: LongInt;
  47. argv: PPChar;
  48. envp: PPChar;
  49. implementation
  50. {$I system.inc}
  51. {*****************************************************************************
  52. Misc. System Dependent Functions
  53. *****************************************************************************}
  54. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  55. procedure System_exit;
  56. begin
  57. { We must remove the CTRL-C FLAG here because halt }
  58. { may call I/O routines, which in turn might call }
  59. { halt, so a recursive stack crash }
  60. if BreakOn then begin
  61. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  62. SetSignal(0,SIGBREAKF_CTRL_C);
  63. end;
  64. { Closing opened files }
  65. CloseList(MOS_fileList);
  66. { Changing back to original directory if changed }
  67. if MOS_origDir<>0 then begin
  68. CurrentDir(MOS_origDir);
  69. end;
  70. if MOS_UtilityBase<>nil then CloseLibrary(MOS_UtilityBase);
  71. if MOS_DOSBase<>nil then CloseLibrary(MOS_DOSBase);
  72. if MOS_heapPool<>nil then DeletePool(MOS_heapPool);
  73. haltproc(ExitCode);
  74. end;
  75. { Generates correct argument array on startup }
  76. procedure GenerateArgs;
  77. var
  78. argvlen : longint;
  79. procedure allocarg(idx,len:longint);
  80. var
  81. i,oldargvlen : longint;
  82. begin
  83. if idx>=argvlen then
  84. begin
  85. oldargvlen:=argvlen;
  86. argvlen:=(idx+8) and (not 7);
  87. sysreallocmem(argv,argvlen*sizeof(pointer));
  88. for i:=oldargvlen to argvlen-1 do
  89. argv[i]:=nil;
  90. end;
  91. ArgV [Idx] := SysAllocMem (Succ (Len));
  92. end;
  93. var
  94. count: word;
  95. start: word;
  96. localindex: word;
  97. p : pchar;
  98. temp : string;
  99. begin
  100. p:=GetArgStr;
  101. argvlen:=0;
  102. { Set argv[0] }
  103. temp:=paramstr(0);
  104. allocarg(0,length(temp));
  105. move(temp[1],argv[0]^,length(temp));
  106. argv[0][length(temp)]:=#0;
  107. { check if we're started from Ambient }
  108. if MOS_ambMsg<>nil then
  109. begin
  110. argc:=0;
  111. exit;
  112. end;
  113. { Handle the other args }
  114. count:=0;
  115. { first index is one }
  116. localindex:=1;
  117. while (p[count]<>#0) do
  118. begin
  119. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  120. start:=count;
  121. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  122. if (count-start>0) then
  123. begin
  124. allocarg(localindex,count-start);
  125. move(p[start],argv[localindex]^,count-start);
  126. argv[localindex][count-start]:=#0;
  127. inc(localindex);
  128. end;
  129. end;
  130. argc:=localindex;
  131. end;
  132. function GetProgDir: String;
  133. var
  134. s1 : String;
  135. alock : LongInt;
  136. counter: Byte;
  137. begin
  138. GetProgDir:='';
  139. FillChar(s1,255,#0);
  140. { GetLock of program directory }
  141. alock:=GetProgramDir;
  142. if alock<>0 then begin
  143. if NameFromLock(alock,@s1[1],255) then begin
  144. counter:=1;
  145. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  146. s1[0]:=Char(counter-1);
  147. GetProgDir:=s1;
  148. end;
  149. end;
  150. end;
  151. function GetProgramName: String;
  152. { Returns ONLY the program name }
  153. var
  154. s1 : String;
  155. counter: Byte;
  156. begin
  157. GetProgramName:='';
  158. FillChar(s1,255,#0);
  159. if GetProgramName(@s1[1],255) then begin
  160. { now check out and assign the length of the string }
  161. counter := 1;
  162. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  163. s1[0]:=Char(counter-1);
  164. { now remove any component path which should not be there }
  165. for counter:=length(s1) downto 1 do
  166. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  167. { readjust counterv to point to character }
  168. if counter<>1 then Inc(counter);
  169. GetProgramName:=copy(s1,counter,length(s1));
  170. end;
  171. end;
  172. {*****************************************************************************
  173. ParamStr/Randomize
  174. *****************************************************************************}
  175. { number of args }
  176. function paramcount : longint;
  177. begin
  178. if MOS_ambMsg<>nil then
  179. paramcount:=0
  180. else
  181. paramcount:=argc-1;
  182. end;
  183. { argument number l }
  184. function paramstr(l : longint) : string;
  185. var
  186. s1: String;
  187. begin
  188. paramstr:='';
  189. if MOS_ambMsg<>nil then exit;
  190. if l=0 then begin
  191. s1:=GetProgDir;
  192. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  193. else paramstr:=s1+'/'+GetProgramName;
  194. end else begin
  195. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  196. end;
  197. end;
  198. { set randseed to a new pseudo random value }
  199. procedure randomize;
  200. var tmpTime: TDateStamp;
  201. begin
  202. DateStamp(@tmpTime);
  203. randseed:=tmpTime.ds_tick;
  204. end;
  205. { MorphOS specific startup }
  206. procedure SysInitMorphOS;
  207. var self: PProcess;
  208. begin
  209. self:=PProcess(FindTask(nil));
  210. if self^.pr_CLI=0 then begin
  211. { if we're running from Ambient/Workbench, we catch its message }
  212. WaitPort(@self^.pr_MsgPort);
  213. MOS_ambMsg:=GetMsg(@self^.pr_MsgPort);
  214. end;
  215. MOS_DOSBase:=OpenLibrary('dos.library',50);
  216. if MOS_DOSBase=nil then Halt(1);
  217. MOS_UtilityBase:=OpenLibrary('utility.library',50);
  218. if MOS_UtilityBase=nil then Halt(1);
  219. { Creating the memory pool for growing heap }
  220. MOS_heapPool:=CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  221. if MOS_heapPool=nil then Halt(1);
  222. if MOS_ambMsg=nil then begin
  223. StdInputHandle:=dosInput;
  224. StdOutputHandle:=dosOutput;
  225. end else begin
  226. MOS_ConHandle:=Open(MOS_ConName,MODE_OLDFILE);
  227. if MOS_ConHandle<>0 then begin
  228. StdInputHandle:=MOS_ConHandle;
  229. StdOutputHandle:=MOS_ConHandle;
  230. end else
  231. Halt(1);
  232. end;
  233. end;
  234. procedure SysInitStdIO;
  235. begin
  236. OpenStdIO(Input,fmInput,StdInputHandle);
  237. OpenStdIO(Output,fmOutput,StdOutputHandle);
  238. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  239. { * MorphOS doesn't have a separate stderr, just like AmigaOS (???) * }
  240. StdErrorHandle:=StdOutputHandle;
  241. // OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  242. // OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  243. end;
  244. function GetProcessID: SizeUInt;
  245. begin
  246. GetProcessID:=SizeUInt(FindTask(NIL));
  247. end;
  248. begin
  249. IsConsole := TRUE;
  250. IsLibrary := FALSE;
  251. StackLength := InitialStkLen;
  252. StackBottom := Sptr - StackLength;
  253. { OS specific startup }
  254. MOS_ambMsg:=nil;
  255. MOS_origDir:=0;
  256. MOS_fileList:=nil;
  257. envp:=nil;
  258. SysInitMorphOS;
  259. { Set up signals handlers }
  260. // InstallSignals;
  261. { Setup heap }
  262. InitHeap;
  263. SysInitExceptions;
  264. { Setup stdin, stdout and stderr }
  265. SysInitStdIO;
  266. { Reset IO Error }
  267. InOutRes:=0;
  268. { Arguments }
  269. GenerateArgs;
  270. InitSystemThreads;
  271. initvariantmanager;
  272. initwidestringmanager;
  273. end.