system.pp 4.6 KB

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