2
0

system.pp 6.8 KB

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