system.pp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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:longint;
  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. {$ifdef darwin}
  37. procedure normalexit(status: cint); cdecl; external 'c' name 'exit';
  38. {$endif}
  39. procedure System_exit;
  40. {$ifndef darwin}
  41. begin
  42. Fpexit(cint(ExitCode));
  43. end;
  44. {$else darwin}
  45. begin
  46. { make sure the libc atexit handlers are called, needed for e.g. profiling }
  47. normalexit(cint(ExitCode));
  48. end;
  49. {$endif darwin}
  50. Function ParamCount: Longint;
  51. Begin
  52. Paramcount:=argc-1
  53. End;
  54. function BackPos(c:char; const s: shortstring): integer;
  55. var
  56. i: integer;
  57. Begin
  58. for i:=length(s) downto 0 do
  59. if s[i] = c then break;
  60. if i=0 then
  61. BackPos := 0
  62. else
  63. BackPos := i;
  64. end;
  65. { variable where full path and filename and executable is stored }
  66. { is setup by the startup of the system unit. }
  67. //var
  68. // execpathstr : shortstring;
  69. function paramstr(l: longint) : string;
  70. begin
  71. { stricly conforming POSIX applications }
  72. { have the executing filename as argv[0] }
  73. // if l=0 then
  74. // begin
  75. // paramstr := execpathstr;
  76. // end
  77. // else
  78. if (l < argc) then
  79. paramstr:=strpas(argv[l])
  80. else
  81. paramstr:='';
  82. end;
  83. Procedure Randomize;
  84. Begin
  85. randseed:=longint(Fptime(nil));
  86. End;
  87. {*****************************************************************************
  88. SystemUnit Initialization
  89. *****************************************************************************}
  90. function reenable_signal(sig : longint) : boolean;
  91. var
  92. e,oe : TSigSet;
  93. i,j : byte;
  94. begin
  95. fillchar(e,sizeof(e),#0);
  96. fillchar(oe,sizeof(oe),#0);
  97. { set is 1 based PM }
  98. dec(sig);
  99. i:=sig mod 32;
  100. j:=sig div 32;
  101. e[j]:=1 shl i;
  102. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  103. reenable_signal:=geterrno=0;
  104. end;
  105. {$i sighnd.inc}
  106. var
  107. act: SigActionRec;
  108. Procedure InstallSignals;
  109. var
  110. oldact: SigActionRec;
  111. begin
  112. { Initialize the sigaction structure }
  113. { all flags and information set to zero }
  114. FillChar(act, sizeof(SigActionRec),0);
  115. { initialize handler }
  116. act.sa_handler :=@SignalToRunError;
  117. act.sa_flags:=SA_SIGINFO;
  118. {$if defined(darwin) and defined(cpu64)}
  119. act.sa_flags:=SA_SIGINFO or SA_64REGSET;
  120. {$else}
  121. act.sa_flags:=SA_SIGINFO;
  122. {$endif}
  123. FpSigAction(SIGFPE,act,oldact);
  124. FpSigAction(SIGSEGV,act,oldact);
  125. FpSigAction(SIGBUS,act,oldact);
  126. FpSigAction(SIGILL,act,oldact);
  127. end;
  128. procedure SetupCmdLine;
  129. var
  130. bufsize,
  131. len,j,
  132. size,i : longint;
  133. found : boolean;
  134. buf : pchar;
  135. procedure AddBuf;
  136. begin
  137. reallocmem(cmdline,size+bufsize);
  138. move(buf^,cmdline[size],bufsize);
  139. inc(size,bufsize);
  140. bufsize:=0;
  141. end;
  142. begin
  143. GetMem(buf,ARG_MAX);
  144. size:=0;
  145. bufsize:=0;
  146. i:=0;
  147. while (i<argc) do
  148. begin
  149. len:=strlen(argv[i]);
  150. if len>ARG_MAX-2 then
  151. len:=ARG_MAX-2;
  152. found:=false;
  153. for j:=1 to len do
  154. if argv[i][j]=' ' then
  155. begin
  156. found:=true;
  157. break;
  158. end;
  159. if bufsize+len>=ARG_MAX-2 then
  160. AddBuf;
  161. if found then
  162. begin
  163. buf[bufsize]:='"';
  164. inc(bufsize);
  165. end;
  166. move(argv[i]^,buf[bufsize],len);
  167. inc(bufsize,len);
  168. if found then
  169. begin
  170. buf[bufsize]:='"';
  171. inc(bufsize);
  172. end;
  173. if i<argc then
  174. buf[bufsize]:=' '
  175. else
  176. buf[bufsize]:=#0;
  177. inc(bufsize);
  178. inc(i);
  179. end;
  180. AddBuf;
  181. FreeMem(buf,ARG_MAX);
  182. end;
  183. procedure SysInitStdIO;
  184. begin
  185. OpenStdIO(Input,fmInput,StdInputHandle);
  186. OpenStdIO(Output,fmOutput,StdOutputHandle);
  187. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  188. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  189. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  190. end;
  191. {$ifdef FPC_USE_LIBC}
  192. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  193. {$ifdef Darwin}
  194. {$ifndef FPC_DARWIN_PASCALMAIN}
  195. procedure pascalmain;external name 'PASCALMAIN';
  196. { Main entry point in C style, needed to capture program parameters. }
  197. procedure main(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  198. {$else FPC_DARWIN_PASCALMAIN}
  199. {$ifdef FPC_DARWIN_JMP_MAIN}
  200. procedure pascalmain;cdecl;external name 'PASCALMAIN';
  201. {$endif}
  202. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  203. {$endif FPC_DARWIN_PASCALMAIN}
  204. begin
  205. argc:= argcparam;
  206. argv:= argvparam;
  207. envp:= envpparam;
  208. {$ifdef cpui386}
  209. Set8087CW(Default8087CW);
  210. {$endif cpui386}
  211. {$if not defined(FPC_DARWIN_PASCALMAIN) or defined(FPC_DARWIN_JMP_MAIN)}
  212. pascalmain; {run the pascal main program}
  213. {$endif}
  214. end;
  215. {$endif Darwin}
  216. {$endif FPC_USE_LIBC}
  217. function GetProcessID: SizeUInt;
  218. begin
  219. GetProcessID := SizeUInt (fpGetPID);
  220. end;
  221. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  222. begin
  223. result := stklen;
  224. end;
  225. Begin
  226. IsConsole := TRUE;
  227. IsLibrary := FALSE;
  228. StackLength := CheckInitialStkLen(InitialStkLen);
  229. StackBottom := Sptr - StackLength;
  230. { Set up signals handlers }
  231. InstallSignals;
  232. SysResetFPU;
  233. if not(IsLibrary) then
  234. SysInitFPU;
  235. {$if defined(cpui386) or defined(cpuarm)}
  236. fpc_cpucodeinit;
  237. {$endif cpui386}
  238. { Setup heap }
  239. InitHeap;
  240. SysInitExceptions;
  241. { Setup stdin, stdout and stderr }
  242. SysInitStdIO;
  243. { Reset IO Error }
  244. InOutRes:=0;
  245. { Arguments }
  246. SetupCmdLine;
  247. { threading }
  248. InitSystemThreads;
  249. initvariantmanager;
  250. initwidestringmanager;
  251. End.