system.pp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. Solaris system unit
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit System;
  12. interface
  13. {$IFNDEF FPC_DISABLE_MONITOR}
  14. {$DEFINE SYSTEM_HAS_FEATURE_MONITOR}
  15. {$ENDIF}
  16. {$define FPC_IS_SYSTEM}
  17. {$linklib m}
  18. { include system-independent routine headers }
  19. {$I sysunixh.inc}
  20. var argc:longint;
  21. argv:PPAnsiChar;
  22. envp:PPAnsiChar;
  23. {$define FPC_SYSTEM_HAS_STACKTOP}
  24. var
  25. StackTopPtr : pointer;
  26. {$if defined(CPUARM) or defined(CPUM68K)}
  27. {$define fpc_softfpu_interface}
  28. {$i softfpu.pp}
  29. {$undef fpc_softfpu_interface}
  30. {$endif defined(CPUARM) or defined(CPUM68K)}
  31. implementation
  32. {$if defined(CPUARM) or defined(CPUM68K)}
  33. {$define fpc_softfpu_implementation}
  34. {$i softfpu.pp}
  35. {$undef fpc_softfpu_implementation}
  36. { we get these functions and types from the softfpu code }
  37. {$define FPC_SYSTEM_HAS_float64}
  38. {$define FPC_SYSTEM_HAS_float32}
  39. {$define FPC_SYSTEM_HAS_flag}
  40. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  41. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  42. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  43. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  44. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  45. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  46. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  47. {$endif defined(CPUARM) or defined(CPUM68K)}
  48. { OS independant parts}
  49. {$I system.inc}
  50. {*****************************************************************************
  51. Misc. System Dependent Functions
  52. *****************************************************************************}
  53. {$i start.inc}
  54. procedure System_exit;
  55. begin
  56. Fpexit(cint(ExitCode));
  57. End;
  58. Function ParamCount: Longint;
  59. Begin
  60. Paramcount:=argc-1
  61. End;
  62. function BackPos(c:AnsiChar; const s: shortstring): integer;
  63. var
  64. i: integer;
  65. Begin
  66. for i:=length(s) downto 0 do
  67. if s[i] = c then break;
  68. if i=0 then
  69. BackPos := 0
  70. else
  71. BackPos := i;
  72. end;
  73. { variable where full path and filename and executable is stored }
  74. { is setup by the startup of the system unit. }
  75. var
  76. execpathstr : shortstring;
  77. function paramstr(l: longint) : shortstring;
  78. var
  79. s: shortstring;
  80. s1: shortstring;
  81. begin
  82. { stricly conforming POSIX applications }
  83. { have the executing filename as argv[0] }
  84. // if l=0 then
  85. // begin
  86. // paramstr := execpathstr;
  87. // end
  88. // else
  89. if (l >= 0) and (l < argc) then
  90. paramstr:=strpas(argv[l])
  91. else
  92. paramstr:='';
  93. end;
  94. Procedure Randomize;
  95. Begin
  96. randseed:=longint(Fptime(nil));
  97. End;
  98. {*****************************************************************************
  99. SystemUnit Initialization
  100. *****************************************************************************}
  101. function reenable_signal(sig : longint) : boolean;
  102. var
  103. e,oe : TSigSet;
  104. i,j : byte;
  105. olderrno: cint;
  106. begin
  107. fillchar(e,sizeof(e),#0);
  108. fillchar(oe,sizeof(oe),#0);
  109. { set is 1 based PM }
  110. dec(sig);
  111. i:=sig mod 32;
  112. j:=sig div 32;
  113. e[j]:=1 shl i;
  114. { this routine is called from a signal handler, so must not change errno }
  115. olderrno:=geterrno;
  116. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  117. reenable_signal:=geterrno=0;
  118. seterrno(olderrno);
  119. end;
  120. {$i sighnd.inc}
  121. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  122. var
  123. act: SigActionRec;
  124. begin
  125. { Initialize the sigaction structure }
  126. { all flags and information set to zero }
  127. FillChar(act, sizeof(SigActionRec),0);
  128. { initialize handler }
  129. act.sa_handler :=@SignalToRunError;
  130. act.sa_flags:=SA_SIGINFO;
  131. FpSigAction(signum,act,oldact);
  132. end;
  133. var
  134. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  135. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  136. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  137. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  138. Procedure InstallSignals;
  139. begin
  140. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  141. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  142. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  143. InstallDefaultSignalHandler(SIGILL,oldsigill);
  144. end;
  145. Procedure RestoreOldSignalHandlers;
  146. begin
  147. FpSigAction(SIGFPE,@oldsigfpe,nil);
  148. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  149. FpSigAction(SIGBUS,@oldsigbus,nil);
  150. FpSigAction(SIGILL,@oldsigill,nil);
  151. end;
  152. procedure SetupCmdLine;
  153. var
  154. bufsize,
  155. len,j,
  156. size,i : longint;
  157. found : boolean;
  158. buf : PAnsiChar;
  159. procedure AddBuf;
  160. begin
  161. sysreallocmem(cmdline,size+bufsize);
  162. move(buf^,cmdline[size],bufsize);
  163. inc(size,bufsize);
  164. bufsize:=0;
  165. end;
  166. begin
  167. GetMem(buf,ARG_MAX);
  168. size:=0;
  169. bufsize:=0;
  170. i:=0;
  171. while (i<argc) do
  172. begin
  173. len:=strlen(argv[i]);
  174. if len>ARG_MAX-2 then
  175. len:=ARG_MAX-2;
  176. found:=false;
  177. for j:=1 to len do
  178. if argv[i][j]=' ' then
  179. begin
  180. found:=true;
  181. break;
  182. end;
  183. if bufsize+len>=ARG_MAX-2 then
  184. AddBuf;
  185. if found then
  186. begin
  187. buf[bufsize]:='"';
  188. inc(bufsize);
  189. end;
  190. move(argv[i]^,buf[bufsize],len);
  191. inc(bufsize,len);
  192. if found then
  193. begin
  194. buf[bufsize]:='"';
  195. inc(bufsize);
  196. end;
  197. if i<argc-1 then
  198. buf[bufsize]:=' '
  199. else
  200. buf[bufsize]:=#0;
  201. inc(bufsize);
  202. inc(i);
  203. end;
  204. AddBuf;
  205. FreeMem(buf,ARG_MAX);
  206. end;
  207. procedure SysInitStdIO;
  208. begin
  209. OpenStdIO(Input,fmInput,StdInputHandle);
  210. OpenStdIO(Output,fmOutput,StdOutputHandle);
  211. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  212. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  213. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  214. end;
  215. function GetProcessID: SizeUInt;
  216. begin
  217. GetProcessID := SizeUInt (fpGetPID);
  218. end;
  219. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  220. begin
  221. result := stklen;
  222. end;
  223. function StackTop : pointer;
  224. begin
  225. if assigned(StackTopPtr) then
  226. StackTop:=StackTopPtr
  227. else
  228. StackTop:=StackBottom + StackLength;
  229. end;
  230. Begin
  231. IsConsole := TRUE;
  232. StackLength := CheckInitialStkLen(InitialStkLen);
  233. if assigned(StackTopPtr) then
  234. StackBottom:=StackTopPtr - StackLength
  235. else
  236. StackBottom := Sptr - StackLength;
  237. { Set up signals handlers (may be needed by init code to test cpu features) }
  238. InstallSignals;
  239. { Setup heap }
  240. InitHeap;
  241. SysInitExceptions;
  242. {$if defined(cpui386) or defined(cpuarm)}
  243. fpc_cpucodeinit;
  244. {$endif cpui386}
  245. initunicodestringmanager;
  246. { Setup stdin, stdout and stderr }
  247. SysInitStdIO;
  248. { Reset IO Error }
  249. InOutRes:=0;
  250. { Arguments }
  251. SetupCmdLine;
  252. InitSystemThreads;
  253. InitSystemDynLibs;
  254. { restore original signal handlers in case this is a library }
  255. if IsLibrary then
  256. RestoreOldSignalHandlers;
  257. End.