system.pp 6.6 KB

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