system.pp 8.2 KB

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