system.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if (l < argc) then
  63. paramstr:=strpas(argv[l])
  64. else
  65. paramstr:='';
  66. end;
  67. Procedure Randomize;
  68. Begin
  69. randseed:=longint(Fptime(nil));
  70. End;
  71. {*****************************************************************************
  72. SystemUnit Initialization
  73. *****************************************************************************}
  74. function reenable_signal(sig : longint) : boolean;
  75. var
  76. e,oe : TSigSet;
  77. i,j : byte;
  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 32;
  84. j:=sig div 32;
  85. e[j]:=1 shl i;
  86. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  87. reenable_signal:=geterrno=0;
  88. end;
  89. {$i sighnd.inc}
  90. var
  91. act: SigActionRec;
  92. Procedure InstallSignals;
  93. var
  94. oldact: 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(SIGFPE,act,oldact);
  103. FpSigAction(SIGSEGV,act,oldact);
  104. FpSigAction(SIGBUS,act,oldact);
  105. FpSigAction(SIGILL,act,oldact);
  106. end;
  107. procedure SetupCmdLine;
  108. var
  109. bufsize,
  110. len,j,
  111. size,i : longint;
  112. found : boolean;
  113. buf : pchar;
  114. procedure AddBuf;
  115. begin
  116. reallocmem(cmdline,size+bufsize);
  117. move(buf^,cmdline[size],bufsize);
  118. inc(size,bufsize);
  119. bufsize:=0;
  120. end;
  121. begin
  122. GetMem(buf,ARG_MAX);
  123. size:=0;
  124. bufsize:=0;
  125. i:=0;
  126. while (i<argc) do
  127. begin
  128. len:=strlen(argv[i]);
  129. if len>ARG_MAX-2 then
  130. len:=ARG_MAX-2;
  131. found:=false;
  132. for j:=1 to len do
  133. if argv[i][j]=' ' then
  134. begin
  135. found:=true;
  136. break;
  137. end;
  138. if bufsize+len>=ARG_MAX-2 then
  139. AddBuf;
  140. if found then
  141. begin
  142. buf[bufsize]:='"';
  143. inc(bufsize);
  144. end;
  145. move(argv[i]^,buf[bufsize],len);
  146. inc(bufsize,len);
  147. if found then
  148. begin
  149. buf[bufsize]:='"';
  150. inc(bufsize);
  151. end;
  152. if i<argc then
  153. buf[bufsize]:=' '
  154. else
  155. buf[bufsize]:=#0;
  156. inc(bufsize);
  157. inc(i);
  158. end;
  159. AddBuf;
  160. FreeMem(buf,ARG_MAX);
  161. end;
  162. procedure SysInitStdIO;
  163. begin
  164. OpenStdIO(Input,fmInput,StdInputHandle);
  165. OpenStdIO(Output,fmOutput,StdOutputHandle);
  166. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  167. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  168. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  169. end;
  170. function GetProcessID: SizeUInt;
  171. begin
  172. GetProcessID := SizeUInt (fpGetPID);
  173. end;
  174. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  175. begin
  176. result := stklen;
  177. end;
  178. Begin
  179. IsConsole := TRUE;
  180. IsLibrary := FALSE;
  181. StackLength := CheckInitialStkLen(InitialStkLen);
  182. StackBottom := Sptr - StackLength;
  183. { Set up signals handlers }
  184. InstallSignals;
  185. { Setup heap }
  186. InitHeap;
  187. SysInitExceptions;
  188. { Setup stdin, stdout and stderr }
  189. SysInitStdIO;
  190. { Reset IO Error }
  191. InOutRes:=0;
  192. { Arguments }
  193. SetupCmdLine;
  194. InitSystemThreads;
  195. initvariantmanager;
  196. {$ifdef VER2_2}
  197. initwidestringmanager;
  198. {$else VER2_2}
  199. initunicodestringmanager;
  200. {$endif VER2_2}
  201. End.