system.pp 8.3 KB

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