system.pp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2004 by Karoly Balogh for Genesi S.a.r.l.
  5. System unit for MorphOS/PowerPC
  6. Uses parts of the Commodore Amiga/68k port by Carl Eric Codere
  7. and Nils Sjoholm
  8. MorphOS port was done on a free Pegasos II/G4 machine
  9. provided by Genesi S.a.r.l. <www.genesi.lu>
  10. See the file COPYING.FPC, included in this distribution,
  11. for details about the copyright.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. **********************************************************************}
  16. unit {$ifdef VER1_0}SysMorph{$else}System{$endif};
  17. interface
  18. {$define FPC_IS_SYSTEM}
  19. {$I systemh.inc}
  20. const
  21. LineEnding = #10;
  22. LFNSupport = True;
  23. DirectorySeparator = '/';
  24. DriveSeparator = ':';
  25. PathSeparator = ';';
  26. maxExitCode = 255;
  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. { use realloc to reuse already existing memory }
  92. sysreallocmem(argv[idx],len+1);
  93. end;
  94. var
  95. count: word;
  96. start: word;
  97. localindex: word;
  98. p : pchar;
  99. temp : string;
  100. begin
  101. p:=GetArgStr;
  102. argvlen:=0;
  103. { Set argv[0] }
  104. temp:=paramstr(0);
  105. allocarg(0,length(temp));
  106. move(temp[1],argv[0]^,length(temp));
  107. argv[0][length(temp)]:=#0;
  108. { check if we're started from Ambient }
  109. if MOS_ambMsg<>nil then
  110. begin
  111. argc:=0;
  112. exit;
  113. end;
  114. { Handle the other args }
  115. count:=0;
  116. { first index is one }
  117. localindex:=1;
  118. while (p[count]<>#0) do
  119. begin
  120. while (p[count]=' ') or (p[count]=#9) or (p[count]=LineEnding) do inc(count);
  121. start:=count;
  122. while (p[count]<>#0) and (p[count]<>' ') and (p[count]<>#9) and (p[count]<>LineEnding) do inc(count);
  123. if (count-start>0) then
  124. begin
  125. allocarg(localindex,count-start);
  126. move(p[start],argv[localindex]^,count-start);
  127. argv[localindex][count-start]:=#0;
  128. inc(localindex);
  129. end;
  130. end;
  131. argc:=localindex;
  132. end;
  133. function GetProgDir: String;
  134. var
  135. s1 : String;
  136. alock : LongInt;
  137. counter: Byte;
  138. begin
  139. GetProgDir:='';
  140. FillChar(s1,255,#0);
  141. { GetLock of program directory }
  142. alock:=GetProgramDir;
  143. if alock<>0 then begin
  144. if NameFromLock(alock,@s1[1],255) then begin
  145. counter:=1;
  146. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  147. s1[0]:=Char(counter-1);
  148. GetProgDir:=s1;
  149. end;
  150. end;
  151. end;
  152. function GetProgramName: String;
  153. { Returns ONLY the program name }
  154. var
  155. s1 : String;
  156. counter: Byte;
  157. begin
  158. GetProgramName:='';
  159. FillChar(s1,255,#0);
  160. if GetProgramName(@s1[1],255) then begin
  161. { now check out and assign the length of the string }
  162. counter := 1;
  163. while (s1[counter]<>#0) and (counter<>0) do Inc(counter);
  164. s1[0]:=Char(counter-1);
  165. { now remove any component path which should not be there }
  166. for counter:=length(s1) downto 1 do
  167. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  168. { readjust counterv to point to character }
  169. if counter<>1 then Inc(counter);
  170. GetProgramName:=copy(s1,counter,length(s1));
  171. end;
  172. end;
  173. {*****************************************************************************
  174. ParamStr/Randomize
  175. *****************************************************************************}
  176. { number of args }
  177. function paramcount : longint;
  178. begin
  179. if MOS_ambMsg<>nil then
  180. paramcount:=0
  181. else
  182. paramcount:=argc-1;
  183. end;
  184. { argument number l }
  185. function paramstr(l : longint) : string;
  186. var
  187. s1: String;
  188. begin
  189. paramstr:='';
  190. if MOS_ambMsg<>nil then exit;
  191. if l=0 then begin
  192. s1:=GetProgDir;
  193. if s1[length(s1)]=':' then paramstr:=s1+GetProgramName
  194. else paramstr:=s1+'/'+GetProgramName;
  195. end else begin
  196. if (l>0) and (l+1<=argc) then paramstr:=strpas(argv[l]);
  197. end;
  198. end;
  199. { set randseed to a new pseudo random value }
  200. procedure randomize;
  201. var tmpTime: TDateStamp;
  202. begin
  203. DateStamp(@tmpTime);
  204. randseed:=tmpTime.ds_tick;
  205. end;
  206. { MorphOS specific startup }
  207. procedure SysInitMorphOS;
  208. var self: PProcess;
  209. begin
  210. self:=PProcess(FindTask(nil));
  211. if self^.pr_CLI=0 then begin
  212. { if we're running from Ambient/Workbench, we catch its message }
  213. WaitPort(@self^.pr_MsgPort);
  214. MOS_ambMsg:=GetMsg(@self^.pr_MsgPort);
  215. end;
  216. MOS_DOSBase:=OpenLibrary('dos.library',50);
  217. if MOS_DOSBase=nil then Halt(1);
  218. MOS_UtilityBase:=OpenLibrary('utility.library',50);
  219. if MOS_UtilityBase=nil then Halt(1);
  220. { Creating the memory pool for growing heap }
  221. MOS_heapPool:=CreatePool(MEMF_FAST,growheapsize2,growheapsize1);
  222. if MOS_heapPool=nil then Halt(1);
  223. if MOS_ambMsg=nil then begin
  224. StdInputHandle:=dosInput;
  225. StdOutputHandle:=dosOutput;
  226. end else begin
  227. MOS_ConHandle:=Open(MOS_ConName,MODE_OLDFILE);
  228. if MOS_ConHandle<>0 then begin
  229. StdInputHandle:=MOS_ConHandle;
  230. StdOutputHandle:=MOS_ConHandle;
  231. end else
  232. Halt(1);
  233. end;
  234. end;
  235. procedure SysInitStdIO;
  236. begin
  237. OpenStdIO(Input,fmInput,StdInputHandle);
  238. OpenStdIO(Output,fmOutput,StdOutputHandle);
  239. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  240. { * MorphOS doesn't have a separate stderr, just like AmigaOS (???) * }
  241. StdErrorHandle:=StdOutputHandle;
  242. // OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  243. // OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  244. end;
  245. function GetProcessID: SizeUInt;
  246. begin
  247. GetProcessID:=SizeUInt(FindTask(NIL));
  248. end;
  249. begin
  250. IsConsole := TRUE;
  251. IsLibrary := FALSE;
  252. StackLength := InitialStkLen;
  253. StackBottom := Sptr - StackLength;
  254. { OS specific startup }
  255. MOS_ambMsg:=nil;
  256. MOS_origDir:=0;
  257. MOS_fileList:=nil;
  258. envp:=nil;
  259. SysInitMorphOS;
  260. { Set up signals handlers }
  261. // InstallSignals;
  262. { Setup heap }
  263. InitHeap;
  264. SysInitExceptions;
  265. { Setup stdin, stdout and stderr }
  266. SysInitStdIO;
  267. { Reset IO Error }
  268. InOutRes:=0;
  269. { Arguments }
  270. GenerateArgs;
  271. InitSystemThreads;
  272. {$ifdef HASVARIANT}
  273. initvariantmanager;
  274. {$endif HASVARIANT}
  275. {$ifdef HASWIDESTRING}
  276. initwidestringmanager;
  277. {$endif HASWIDESTRING}
  278. end.
  279. {
  280. $Log$
  281. Revision 1.33 2005-04-03 21:10:59 hajny
  282. * EOF_CTRLZ conditional define replaced with CtrlZMarksEOF, #26 handling made more consistent (fix for bug 2453)
  283. Revision 1.32 2005/02/14 17:13:30 peter
  284. * truncate log
  285. Revision 1.31 2005/02/07 21:30:12 peter
  286. * system unit updated
  287. Revision 1.30 2005/02/01 20:22:49 florian
  288. * improved widestring infrastructure manager
  289. Revision 1.29 2005/01/12 08:03:42 karoly
  290. * Few more Sysutils functions implemented
  291. Revision 1.28 2005/01/11 17:43:14 karoly
  292. * some cleanup, more sanity checks and updates for sysutils
  293. }