system.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  95. var
  96. act: SigActionRec;
  97. begin
  98. { Initialize the sigaction structure }
  99. { all flags and information set to zero }
  100. FillChar(act, sizeof(SigActionRec),0);
  101. { initialize handler }
  102. act.sa_handler :=@SignalToRunError;
  103. act.sa_flags:=SA_SIGINFO;
  104. FpSigAction(signum,act,oldact);
  105. end;
  106. var
  107. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  108. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  109. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  110. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  111. Procedure InstallSignals;
  112. begin
  113. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  114. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  115. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  116. InstallDefaultSignalHandler(SIGILL,oldsigill);
  117. end;
  118. Procedure RestoreOldSignalHandlers;
  119. begin
  120. FpSigAction(SIGFPE,@oldsigfpe,nil);
  121. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  122. FpSigAction(SIGBUS,@oldsigbus,nil);
  123. FpSigAction(SIGILL,@oldsigill,nil);
  124. end;
  125. procedure SetupCmdLine;
  126. var
  127. bufsize,
  128. len,j,
  129. size,i : longint;
  130. found : boolean;
  131. buf : pchar;
  132. procedure AddBuf;
  133. begin
  134. reallocmem(cmdline,size+bufsize);
  135. move(buf^,cmdline[size],bufsize);
  136. inc(size,bufsize);
  137. bufsize:=0;
  138. end;
  139. begin
  140. GetMem(buf,ARG_MAX);
  141. size:=0;
  142. bufsize:=0;
  143. i:=0;
  144. while (i<argc) do
  145. begin
  146. len:=strlen(argv[i]);
  147. if len>ARG_MAX-2 then
  148. len:=ARG_MAX-2;
  149. found:=false;
  150. for j:=1 to len do
  151. if argv[i][j]=' ' then
  152. begin
  153. found:=true;
  154. break;
  155. end;
  156. if bufsize+len>=ARG_MAX-2 then
  157. AddBuf;
  158. if found then
  159. begin
  160. buf[bufsize]:='"';
  161. inc(bufsize);
  162. end;
  163. move(argv[i]^,buf[bufsize],len);
  164. inc(bufsize,len);
  165. if found then
  166. begin
  167. buf[bufsize]:='"';
  168. inc(bufsize);
  169. end;
  170. if i<argc then
  171. buf[bufsize]:=' '
  172. else
  173. buf[bufsize]:=#0;
  174. inc(bufsize);
  175. inc(i);
  176. end;
  177. AddBuf;
  178. FreeMem(buf,ARG_MAX);
  179. end;
  180. procedure SysInitStdIO;
  181. begin
  182. OpenStdIO(Input,fmInput,StdInputHandle);
  183. OpenStdIO(Output,fmOutput,StdOutputHandle);
  184. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  185. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  186. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  187. end;
  188. function GetProcessID: SizeUInt;
  189. begin
  190. GetProcessID := SizeUInt (fpGetPID);
  191. end;
  192. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  193. begin
  194. result := stklen;
  195. end;
  196. Begin
  197. IsConsole := TRUE;
  198. StackLength := CheckInitialStkLen(InitialStkLen);
  199. StackBottom := Sptr - StackLength;
  200. { Set up signals handlers (may be needed by init code to test cpu features) }
  201. InstallSignals;
  202. { Setup heap }
  203. InitHeap;
  204. SysInitExceptions;
  205. { Setup stdin, stdout and stderr }
  206. SysInitStdIO;
  207. { Reset IO Error }
  208. InOutRes:=0;
  209. { Arguments }
  210. SetupCmdLine;
  211. InitSystemThreads;
  212. initvariantmanager;
  213. {$ifdef VER2_2}
  214. initwidestringmanager;
  215. {$else VER2_2}
  216. initunicodestringmanager;
  217. {$endif VER2_2}
  218. { restore original signal handlers in case this is a library }
  219. if IsLibrary then
  220. RestoreOldSignalHandlers;
  221. End.