system.pp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. sLineBreak : string[1] = LineEnding;
  34. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  35. BreakOn : Boolean = True;
  36. var
  37. MOS_ExecBase : Pointer; external name '_ExecBase';
  38. MOS_DOSBase : Pointer;
  39. MOS_UtilityBase: Pointer;
  40. MOS_heapPool : Pointer; { pointer for the OS pool for growing the heap }
  41. MOS_origDir : LongInt; { original directory on startup }
  42. MOS_ambMsg : Pointer;
  43. MOS_ConName : PChar ='CON:10/30/620/100/FPC Console Output/AUTO/CLOSE/WAIT';
  44. MOS_ConHandle: LongInt;
  45. argc: LongInt;
  46. argv: PPChar;
  47. envp: PPChar;
  48. implementation
  49. {$I system.inc}
  50. {*****************************************************************************
  51. Misc. System Dependent Functions
  52. *****************************************************************************}
  53. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  54. procedure System_exit;
  55. begin
  56. { We must remove the CTRL-C FLAG here because halt }
  57. { may call I/O routines, which in turn might call }
  58. { halt, so a recursive stack crash }
  59. if BreakOn then begin
  60. if (SetSignal(0,0) and SIGBREAKF_CTRL_C)<>0 then
  61. SetSignal(0,SIGBREAKF_CTRL_C);
  62. end;
  63. { Closing opened files }
  64. CloseList(MOS_fileList);
  65. { Changing back to original directory if changed }
  66. if MOS_origDir<>0 then begin
  67. CurrentDir(MOS_origDir);
  68. end;
  69. if MOS_UtilityBase<>nil then CloseLibrary(MOS_UtilityBase);
  70. if MOS_DOSBase<>nil then CloseLibrary(MOS_DOSBase);
  71. if MOS_heapPool<>nil then DeletePool(MOS_heapPool);
  72. haltproc(ExitCode);
  73. end;
  74. { Generates correct argument array on startup }
  75. procedure GenerateArgs;
  76. var
  77. argvlen : longint;
  78. procedure allocarg(idx,len:longint);
  79. var
  80. i,oldargvlen : longint;
  81. begin
  82. if idx>=argvlen then
  83. begin
  84. oldargvlen:=argvlen;
  85. argvlen:=(idx+8) and (not 7);
  86. sysreallocmem(argv,argvlen*sizeof(pointer));
  87. for i:=oldargvlen to argvlen-1 do
  88. argv[i]:=nil;
  89. end;
  90. { use realloc to reuse already existing memory }
  91. sysreallocmem(argv[idx],len+1);
  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. {$ifdef HASVARIANT}
  272. initvariantmanager;
  273. {$endif HASVARIANT}
  274. {$ifdef HASWIDESTRING}
  275. initwidestringmanager;
  276. {$endif HASWIDESTRING}
  277. end.
  278. {
  279. $Log$
  280. Revision 1.32 2005-02-14 17:13:30 peter
  281. * truncate log
  282. Revision 1.31 2005/02/07 21:30:12 peter
  283. * system unit updated
  284. Revision 1.30 2005/02/01 20:22:49 florian
  285. * improved widestring infrastructure manager
  286. Revision 1.29 2005/01/12 08:03:42 karoly
  287. * Few more Sysutils functions implemented
  288. Revision 1.28 2005/01/11 17:43:14 karoly
  289. * some cleanup, more sanity checks and updates for sysutils
  290. }