2
0

system.pp 6.4 KB

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