system.pp 6.4 KB

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