system.pp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. AIX 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. implementation
  21. { OS independant parts}
  22. {$I system.inc}
  23. {*****************************************************************************
  24. Misc. System Dependent Functions
  25. *****************************************************************************}
  26. procedure pascalmain;external name 'PASCALMAIN';
  27. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  28. begin
  29. argc:=argcparam;
  30. argv:=argvparam;
  31. envp:=envpparam;
  32. pascalmain; {run the pascal main program}
  33. end;
  34. procedure System_exit;
  35. begin
  36. Fpexit(cint(ExitCode));
  37. End;
  38. Function ParamCount: Longint;
  39. Begin
  40. Paramcount:=argc-1
  41. End;
  42. function BackPos(c:char; const s: shortstring): integer;
  43. var
  44. i: integer;
  45. Begin
  46. for i:=length(s) downto 0 do
  47. if s[i] = c then break;
  48. if i=0 then
  49. BackPos := 0
  50. else
  51. BackPos := i;
  52. end;
  53. function paramstr(l: longint) : string;
  54. var
  55. s: string;
  56. s1: string;
  57. begin
  58. { stricly conforming POSIX applications }
  59. { have the executing filename as argv[0] }
  60. if (l < argc) then
  61. paramstr:=strpas(argv[l])
  62. else
  63. paramstr:='';
  64. end;
  65. Procedure Randomize;
  66. Begin
  67. randseed:=longint(Fptime(nil));
  68. End;
  69. {*****************************************************************************
  70. SystemUnit Initialization
  71. *****************************************************************************}
  72. function reenable_signal(sig : longint) : boolean;
  73. var
  74. e,oe : TSigSet;
  75. i,j : byte;
  76. olderrno: cint;
  77. begin
  78. fillchar(e,sizeof(e),#0);
  79. fillchar(oe,sizeof(oe),#0);
  80. { set is 1 based PM }
  81. dec(sig);
  82. i:=sig mod sizeof(clong);
  83. j:=sig div sizeof(clong);
  84. e[j]:=1 shl i;
  85. { this routine is called from a signal handler, so must not change errno }
  86. olderrno:=geterrno;
  87. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  88. reenable_signal:=geterrno=0;
  89. seterrno(olderrno);
  90. end;
  91. {$i sighnd.inc}
  92. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  93. var
  94. act: SigActionRec;
  95. begin
  96. { Initialize the sigaction structure }
  97. { all flags and information set to zero }
  98. FillChar(act, sizeof(SigActionRec),0);
  99. { initialize handler }
  100. act.sa_handler:=@SignalToRunError;
  101. act.sa_flags:=SA_SIGINFO;
  102. FpSigAction(signum,act,oldact);
  103. end;
  104. var
  105. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  106. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  107. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  108. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  109. Procedure InstallSignals;
  110. begin
  111. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  112. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  113. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  114. InstallDefaultSignalHandler(SIGILL,oldsigill);
  115. end;
  116. Procedure RestoreOldSignalHandlers;
  117. begin
  118. FpSigAction(SIGFPE,@oldsigfpe,nil);
  119. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  120. FpSigAction(SIGBUS,@oldsigbus,nil);
  121. FpSigAction(SIGILL,@oldsigill,nil);
  122. end;
  123. procedure SetupCmdLine;
  124. var
  125. bufsize,
  126. len,j,
  127. size,i : longint;
  128. found : boolean;
  129. buf : pchar;
  130. procedure AddBuf;
  131. begin
  132. reallocmem(cmdline,size+bufsize);
  133. move(buf^,cmdline[size],bufsize);
  134. inc(size,bufsize);
  135. bufsize:=0;
  136. end;
  137. begin
  138. GetMem(buf,ARG_MAX);
  139. size:=0;
  140. bufsize:=0;
  141. i:=0;
  142. while (i<argc) do
  143. begin
  144. len:=strlen(argv[i]);
  145. if len>ARG_MAX-2 then
  146. len:=ARG_MAX-2;
  147. found:=false;
  148. for j:=1 to len do
  149. if argv[i][j]=' ' then
  150. begin
  151. found:=true;
  152. break;
  153. end;
  154. if bufsize+len>=ARG_MAX-2 then
  155. AddBuf;
  156. if found then
  157. begin
  158. buf[bufsize]:='"';
  159. inc(bufsize);
  160. end;
  161. move(argv[i]^,buf[bufsize],len);
  162. inc(bufsize,len);
  163. if found then
  164. begin
  165. buf[bufsize]:='"';
  166. inc(bufsize);
  167. end;
  168. if i<argc-1 then
  169. buf[bufsize]:=' '
  170. else
  171. buf[bufsize]:=#0;
  172. inc(bufsize);
  173. inc(i);
  174. end;
  175. AddBuf;
  176. FreeMem(buf,ARG_MAX);
  177. end;
  178. procedure SysInitStdIO;
  179. begin
  180. OpenStdIO(Input,fmInput,StdInputHandle);
  181. OpenStdIO(Output,fmOutput,StdOutputHandle);
  182. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  183. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  184. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  185. end;
  186. function GetProcessID: SizeUInt;
  187. begin
  188. GetProcessID := SizeUInt (fpGetPID);
  189. end;
  190. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  191. begin
  192. result := stklen;
  193. end;
  194. const
  195. FP_TRAP_SYNC = 1; { precise fpu exceptions }
  196. FP_TRAP_OFF = 0; { disable fpu exceptions }
  197. FP_TRAP_QUERY = 2; { current fpu exception state }
  198. FP_TRAP_IMP = 3; { imprecise non-recoverable fpu exceptions }
  199. FP_TRAP_IMP_REC = 4; { imprecise recoverable fpu exceptions }
  200. FP_TRAP_FASTMODE = 128; { fastest fpu exception state }
  201. FP_TRAP_ERROR = -1;
  202. FP_TRAP_UNIMPL = -2;
  203. TRP_INVALID = $00000080;
  204. TRP_OVERFLOW = $00000040;
  205. TRP_UNDERFLOW = $00000020;
  206. TRP_DIV_BY_ZERO = $00000010;
  207. TRP_INEXACT = $00000008;
  208. function fp_trap(flag: longint): longint; cdecl; external;
  209. procedure fp_enable(Mask: DWord);cdecl;external;
  210. Begin
  211. IsConsole := TRUE;
  212. StackLength := CheckInitialStkLen(InitialStkLen);
  213. StackBottom := Sptr - StackLength;
  214. { Set up signals handlers (may be needed by init code to test cpu features) }
  215. InstallSignals;
  216. SysResetFPU;
  217. if not(IsLibrary) then
  218. begin
  219. { clear pending exceptions }
  220. feclearexcept(FE_ALL_EXCEPT);
  221. { enable floating point exceptions process-wide (try two possibilities) }
  222. if fp_trap(FP_TRAP_SYNC)=FP_TRAP_UNIMPL then
  223. fp_trap(FP_TRAP_IMP);
  224. SysInitFPU;
  225. { now enable the actual individual exceptions, except for underflow and
  226. inexact (also disabled by default on x86 and in the softfpu mask) }
  227. fp_enable(TRP_INVALID or TRP_DIV_BY_ZERO or TRP_OVERFLOW);
  228. end;
  229. { Setup heap }
  230. InitHeap;
  231. SysInitExceptions;
  232. initunicodestringmanager;
  233. { Setup stdin, stdout and stderr }
  234. SysInitStdIO;
  235. { Reset IO Error }
  236. InOutRes:=0;
  237. { Arguments }
  238. SetupCmdLine;
  239. InitSystemThreads;
  240. InitSystemDynLibs;
  241. { restore original signal handlers in case this is a library }
  242. if IsLibrary then
  243. RestoreOldSignalHandlers;
  244. End.