system.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. {
  2. This file is part of the Free Pascal run time librar~y.
  3. Copyright (c) 2000 by Marco van de Voort
  4. member of the Free Pascal development team.
  5. System unit for the *BSD's.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { These things are set in the makefile, }
  13. { But you can override them here.}
  14. { If you use an aout system, set the conditional AOUT}
  15. { $Define AOUT}
  16. Unit System;
  17. Interface
  18. {$define FPC_USE_SIGPROCMASK}
  19. {$define FPC_USE_SIGALTSTACK}
  20. {$ifndef FPC_USE_LIBC}
  21. {$define FPC_USE_SYSCALL}
  22. {$endif}
  23. {$define FPC_IS_SYSTEM}
  24. {$I sysunixh.inc}
  25. {$ifdef Darwin}
  26. var argc:cardinal;
  27. argv:PPchar;
  28. envp:PPchar;
  29. {$endif}
  30. CONST SIGSTKSZ = 40960;
  31. Implementation
  32. {$I system.inc}
  33. {*****************************************************************************
  34. Misc. System Dependent Functions
  35. *****************************************************************************}
  36. procedure System_exit;
  37. begin
  38. Fpexit(cint(ExitCode));
  39. End;
  40. Function ParamCount: Longint;
  41. Begin
  42. Paramcount:=argc-1
  43. End;
  44. function BackPos(c:char; const s: shortstring): integer;
  45. var
  46. i: integer;
  47. Begin
  48. for i:=length(s) downto 0 do
  49. if s[i] = c then break;
  50. if i=0 then
  51. BackPos := 0
  52. else
  53. BackPos := i;
  54. end;
  55. { variable where full path and filename and executable is stored }
  56. { is setup by the startup of the system unit. }
  57. var
  58. execpathstr : shortstring;
  59. function paramstr(l: longint) : string;
  60. begin
  61. { stricly conforming POSIX applications }
  62. { have the executing filename as argv[0] }
  63. // if l=0 then
  64. // begin
  65. // paramstr := execpathstr;
  66. // end
  67. // else
  68. paramstr:=strpas(argv[l]);
  69. end;
  70. Procedure Randomize;
  71. Begin
  72. randseed:=longint(Fptime(nil));
  73. End;
  74. {*****************************************************************************
  75. SystemUnit Initialization
  76. *****************************************************************************}
  77. function reenable_signal(sig : longint) : boolean;
  78. var
  79. e,oe : TSigSet;
  80. i,j : byte;
  81. begin
  82. fillchar(e,sizeof(e),#0);
  83. fillchar(oe,sizeof(oe),#0);
  84. { set is 1 based PM }
  85. dec(sig);
  86. i:=sig mod 32;
  87. j:=sig div 32;
  88. e[j]:=1 shl i;
  89. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  90. reenable_signal:=geterrno=0;
  91. end;
  92. {$i sighnd.inc}
  93. var
  94. act: SigActionRec;
  95. Procedure InstallSignals;
  96. var
  97. oldact: SigActionRec;
  98. begin
  99. { Initialize the sigaction structure }
  100. { all flags and information set to zero }
  101. FillChar(act, sizeof(SigActionRec),0);
  102. { initialize handler }
  103. act.sa_handler :=@SignalToRunError;
  104. act.sa_flags:=SA_SIGINFO;
  105. FpSigAction(SIGFPE,act,oldact);
  106. FpSigAction(SIGSEGV,act,oldact);
  107. FpSigAction(SIGBUS,act,oldact);
  108. FpSigAction(SIGILL,act,oldact);
  109. end;
  110. procedure SetupCmdLine;
  111. var
  112. bufsize,
  113. len,j,
  114. size,i : longint;
  115. found : boolean;
  116. buf : pchar;
  117. procedure AddBuf;
  118. begin
  119. reallocmem(cmdline,size+bufsize);
  120. move(buf^,cmdline[size],bufsize);
  121. inc(size,bufsize);
  122. bufsize:=0;
  123. end;
  124. begin
  125. GetMem(buf,ARG_MAX);
  126. size:=0;
  127. bufsize:=0;
  128. i:=0;
  129. while (i<argc) do
  130. begin
  131. len:=strlen(argv[i]);
  132. if len>ARG_MAX-2 then
  133. len:=ARG_MAX-2;
  134. found:=false;
  135. for j:=1 to len do
  136. if argv[i][j]=' ' then
  137. begin
  138. found:=true;
  139. break;
  140. end;
  141. if bufsize+len>=ARG_MAX-2 then
  142. AddBuf;
  143. if found then
  144. begin
  145. buf[bufsize]:='"';
  146. inc(bufsize);
  147. end;
  148. move(argv[i]^,buf[bufsize],len);
  149. inc(bufsize,len);
  150. if found then
  151. begin
  152. buf[bufsize]:='"';
  153. inc(bufsize);
  154. end;
  155. if i<argc then
  156. buf[bufsize]:=' '
  157. else
  158. buf[bufsize]:=#0;
  159. inc(bufsize);
  160. inc(i);
  161. end;
  162. AddBuf;
  163. FreeMem(buf,ARG_MAX);
  164. end;
  165. procedure SysInitStdIO;
  166. begin
  167. OpenStdIO(Input,fmInput,StdInputHandle);
  168. OpenStdIO(Output,fmOutput,StdOutputHandle);
  169. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  170. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  171. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  172. end;
  173. {$ifdef FPC_USE_LIBC}
  174. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  175. {$ifdef Darwin}
  176. {$ifndef FPC_DARWIN_PASCALMAIN}
  177. procedure pascalmain;external name 'PASCALMAIN';
  178. { Main entry point in C style, needed to capture program parameters. }
  179. procedure main(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  180. {$else FPC_DARWIN_PASCALMAIN}
  181. {$ifdef FPC_DARWIN_JMP_MAIN}
  182. procedure pascalmain;cdecl;external name 'PASCALMAIN';
  183. {$endif}
  184. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  185. {$endif FPC_DARWIN_PASCALMAIN}
  186. begin
  187. argc:= argcparam;
  188. argv:= argvparam;
  189. envp:= envpparam;
  190. {$ifdef cpui386}
  191. Set8087CW(Default8087CW);
  192. {$endif cpui386}
  193. {$if not defined(FPC_DARWIN_PASCALMAIN) or defined(FPC_DARWIN_JMP_MAIN)}
  194. pascalmain; {run the pascal main program}
  195. {$endif}
  196. end;
  197. {$endif Darwin}
  198. {$endif FPC_USE_LIBC}
  199. function GetProcessID: SizeUInt;
  200. begin
  201. GetProcessID := SizeUInt (fpGetPID);
  202. end;
  203. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  204. begin
  205. result := stklen;
  206. end;
  207. Begin
  208. IsConsole := TRUE;
  209. IsLibrary := FALSE;
  210. StackLength := CheckInitialStkLen(InitialStkLen);
  211. StackBottom := Sptr - StackLength;
  212. { Set up signals handlers }
  213. InstallSignals;
  214. { Setup heap }
  215. InitHeap;
  216. SysInitExceptions;
  217. { Setup stdin, stdout and stderr }
  218. SysInitStdIO;
  219. { Reset IO Error }
  220. InOutRes:=0;
  221. { Arguments }
  222. SetupCmdLine;
  223. { threading }
  224. InitSystemThreads;
  225. initvariantmanager;
  226. initwidestringmanager;
  227. End.