system.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. { include system-independent routine headers }
  15. {$I sysunixh.inc}
  16. var argc:longint;
  17. argv:PPchar;
  18. envp:PPchar;
  19. implementation
  20. { OS independant parts}
  21. {$I system.inc}
  22. {*****************************************************************************
  23. Misc. System Dependent Functions
  24. *****************************************************************************}
  25. {$i start.inc}
  26. procedure System_exit;
  27. begin
  28. Fpexit(cint(ExitCode));
  29. End;
  30. Function ParamCount: Longint;
  31. Begin
  32. Paramcount:=argc-1
  33. End;
  34. function BackPos(c:char; const s: shortstring): integer;
  35. var
  36. i: integer;
  37. Begin
  38. for i:=length(s) downto 0 do
  39. if s[i] = c then break;
  40. if i=0 then
  41. BackPos := 0
  42. else
  43. BackPos := i;
  44. end;
  45. { variable where full path and filename and executable is stored }
  46. { is setup by the startup of the system unit. }
  47. var
  48. execpathstr : shortstring;
  49. function paramstr(l: longint) : string;
  50. var
  51. s: string;
  52. s1: string;
  53. begin
  54. { stricly conforming POSIX applications }
  55. { have the executing filename as argv[0] }
  56. // if l=0 then
  57. // begin
  58. // paramstr := execpathstr;
  59. // end
  60. // else
  61. paramstr:=strpas(argv[l]);
  62. end;
  63. Procedure Randomize;
  64. Begin
  65. randseed:=longint(Fptime(nil));
  66. End;
  67. {*****************************************************************************
  68. SystemUnit Initialization
  69. *****************************************************************************}
  70. function reenable_signal(sig : longint) : boolean;
  71. var
  72. e,oe : TSigSet;
  73. i,j : byte;
  74. begin
  75. fillchar(e,sizeof(e),#0);
  76. fillchar(oe,sizeof(oe),#0);
  77. { set is 1 based PM }
  78. dec(sig);
  79. i:=sig mod 32;
  80. j:=sig div 32;
  81. e[j]:=1 shl i;
  82. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  83. reenable_signal:=geterrno=0;
  84. end;
  85. {$i sighnd.inc}
  86. var
  87. act: SigActionRec;
  88. Procedure InstallSignals;
  89. var
  90. oldact: SigActionRec;
  91. begin
  92. { Initialize the sigaction structure }
  93. { all flags and information set to zero }
  94. FillChar(act, sizeof(SigActionRec),0);
  95. { initialize handler }
  96. act.sa_handler :=@SignalToRunError;
  97. act.sa_flags:=SA_SIGINFO;
  98. FpSigAction(SIGFPE,act,oldact);
  99. FpSigAction(SIGSEGV,act,oldact);
  100. FpSigAction(SIGBUS,act,oldact);
  101. FpSigAction(SIGILL,act,oldact);
  102. end;
  103. procedure SetupCmdLine;
  104. var
  105. bufsize,
  106. len,j,
  107. size,i : longint;
  108. found : boolean;
  109. buf : pchar;
  110. procedure AddBuf;
  111. begin
  112. reallocmem(cmdline,size+bufsize);
  113. move(buf^,cmdline[size],bufsize);
  114. inc(size,bufsize);
  115. bufsize:=0;
  116. end;
  117. begin
  118. GetMem(buf,ARG_MAX);
  119. size:=0;
  120. bufsize:=0;
  121. i:=0;
  122. while (i<argc) do
  123. begin
  124. len:=strlen(argv[i]);
  125. if len>ARG_MAX-2 then
  126. len:=ARG_MAX-2;
  127. found:=false;
  128. for j:=1 to len do
  129. if argv[i][j]=' ' then
  130. begin
  131. found:=true;
  132. break;
  133. end;
  134. if bufsize+len>=ARG_MAX-2 then
  135. AddBuf;
  136. if found then
  137. begin
  138. buf[bufsize]:='"';
  139. inc(bufsize);
  140. end;
  141. move(argv[i]^,buf[bufsize],len);
  142. inc(bufsize,len);
  143. if found then
  144. begin
  145. buf[bufsize]:='"';
  146. inc(bufsize);
  147. end;
  148. if i<argc then
  149. buf[bufsize]:=' '
  150. else
  151. buf[bufsize]:=#0;
  152. inc(bufsize);
  153. inc(i);
  154. end;
  155. AddBuf;
  156. FreeMem(buf,ARG_MAX);
  157. end;
  158. procedure SysInitStdIO;
  159. begin
  160. OpenStdIO(Input,fmInput,StdInputHandle);
  161. OpenStdIO(Output,fmOutput,StdOutputHandle);
  162. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  163. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  164. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  165. end;
  166. function GetProcessID: SizeUInt;
  167. begin
  168. GetProcessID := SizeUInt (fpGetPID);
  169. end;
  170. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  171. begin
  172. result := stklen;
  173. end;
  174. Begin
  175. IsConsole := TRUE;
  176. IsLibrary := FALSE;
  177. StackLength := CheckInitialStkLen(InitialStkLen);
  178. StackBottom := Sptr - StackLength;
  179. { Set up signals handlers }
  180. InstallSignals;
  181. { Setup heap }
  182. InitHeap;
  183. SysInitExceptions;
  184. { Setup stdin, stdout and stderr }
  185. SysInitStdIO;
  186. { Reset IO Error }
  187. InOutRes:=0;
  188. { Arguments }
  189. SetupCmdLine;
  190. InitSystemThreads;
  191. initvariantmanager;
  192. initwidestringmanager;
  193. End.