system.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. olderrno: cint;
  79. begin
  80. fillchar(e,sizeof(e),#0);
  81. fillchar(oe,sizeof(oe),#0);
  82. { set is 1 based PM }
  83. dec(sig);
  84. i:=sig mod 32;
  85. j:=sig div 32;
  86. e[j]:=1 shl i;
  87. { this routine is called from a signal handler, so must not change errno }
  88. olderrno:=geterrno;
  89. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  90. reenable_signal:=geterrno=0;
  91. seterrno(olderrno);
  92. end;
  93. {$i sighnd.inc}
  94. var
  95. act: SigActionRec;
  96. Procedure InstallSignals;
  97. var
  98. oldact: SigActionRec;
  99. begin
  100. { Initialize the sigaction structure }
  101. { all flags and information set to zero }
  102. FillChar(act, sizeof(SigActionRec),0);
  103. { initialize handler }
  104. act.sa_handler :=@SignalToRunError;
  105. act.sa_flags:=SA_SIGINFO;
  106. FpSigAction(SIGFPE,act,oldact);
  107. FpSigAction(SIGSEGV,act,oldact);
  108. FpSigAction(SIGBUS,act,oldact);
  109. FpSigAction(SIGILL,act,oldact);
  110. end;
  111. procedure SetupCmdLine;
  112. var
  113. bufsize,
  114. len,j,
  115. size,i : longint;
  116. found : boolean;
  117. buf : pchar;
  118. procedure AddBuf;
  119. begin
  120. reallocmem(cmdline,size+bufsize);
  121. move(buf^,cmdline[size],bufsize);
  122. inc(size,bufsize);
  123. bufsize:=0;
  124. end;
  125. begin
  126. GetMem(buf,ARG_MAX);
  127. size:=0;
  128. bufsize:=0;
  129. i:=0;
  130. while (i<argc) do
  131. begin
  132. len:=strlen(argv[i]);
  133. if len>ARG_MAX-2 then
  134. len:=ARG_MAX-2;
  135. found:=false;
  136. for j:=1 to len do
  137. if argv[i][j]=' ' then
  138. begin
  139. found:=true;
  140. break;
  141. end;
  142. if bufsize+len>=ARG_MAX-2 then
  143. AddBuf;
  144. if found then
  145. begin
  146. buf[bufsize]:='"';
  147. inc(bufsize);
  148. end;
  149. move(argv[i]^,buf[bufsize],len);
  150. inc(bufsize,len);
  151. if found then
  152. begin
  153. buf[bufsize]:='"';
  154. inc(bufsize);
  155. end;
  156. if i<argc then
  157. buf[bufsize]:=' '
  158. else
  159. buf[bufsize]:=#0;
  160. inc(bufsize);
  161. inc(i);
  162. end;
  163. AddBuf;
  164. FreeMem(buf,ARG_MAX);
  165. end;
  166. procedure SysInitStdIO;
  167. begin
  168. OpenStdIO(Input,fmInput,StdInputHandle);
  169. OpenStdIO(Output,fmOutput,StdOutputHandle);
  170. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  171. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  172. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  173. end;
  174. function GetProcessID: SizeUInt;
  175. begin
  176. GetProcessID := SizeUInt (fpGetPID);
  177. end;
  178. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  179. begin
  180. result := stklen;
  181. end;
  182. Begin
  183. IsConsole := TRUE;
  184. StackLength := CheckInitialStkLen(InitialStkLen);
  185. StackBottom := Sptr - StackLength;
  186. { Set up signals handlers }
  187. InstallSignals;
  188. { Setup heap }
  189. InitHeap;
  190. SysInitExceptions;
  191. { Setup stdin, stdout and stderr }
  192. SysInitStdIO;
  193. { Reset IO Error }
  194. InOutRes:=0;
  195. { Arguments }
  196. SetupCmdLine;
  197. InitSystemThreads;
  198. initvariantmanager;
  199. {$ifdef VER2_2}
  200. initwidestringmanager;
  201. {$else VER2_2}
  202. initunicodestringmanager;
  203. {$endif VER2_2}
  204. End.