system.pp 6.9 KB

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