system.pp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. paramstr:=strpas(argv[l]);
  79. end;
  80. Procedure Randomize;
  81. Begin
  82. randseed:=longint(Fptime(nil));
  83. End;
  84. {*****************************************************************************
  85. SystemUnit Initialization
  86. *****************************************************************************}
  87. function reenable_signal(sig : longint) : boolean;
  88. var
  89. e,oe : TSigSet;
  90. i,j : byte;
  91. begin
  92. fillchar(e,sizeof(e),#0);
  93. fillchar(oe,sizeof(oe),#0);
  94. { set is 1 based PM }
  95. dec(sig);
  96. i:=sig mod 32;
  97. j:=sig div 32;
  98. e[j]:=1 shl i;
  99. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  100. reenable_signal:=geterrno=0;
  101. end;
  102. {$i sighnd.inc}
  103. var
  104. act: SigActionRec;
  105. Procedure InstallSignals;
  106. var
  107. oldact: SigActionRec;
  108. begin
  109. { Initialize the sigaction structure }
  110. { all flags and information set to zero }
  111. FillChar(act, sizeof(SigActionRec),0);
  112. { initialize handler }
  113. act.sa_handler :=@SignalToRunError;
  114. act.sa_flags:=SA_SIGINFO;
  115. FpSigAction(SIGFPE,act,oldact);
  116. FpSigAction(SIGSEGV,act,oldact);
  117. FpSigAction(SIGBUS,act,oldact);
  118. FpSigAction(SIGILL,act,oldact);
  119. end;
  120. procedure SetupCmdLine;
  121. var
  122. bufsize,
  123. len,j,
  124. size,i : longint;
  125. found : boolean;
  126. buf : pchar;
  127. procedure AddBuf;
  128. begin
  129. reallocmem(cmdline,size+bufsize);
  130. move(buf^,cmdline[size],bufsize);
  131. inc(size,bufsize);
  132. bufsize:=0;
  133. end;
  134. begin
  135. GetMem(buf,ARG_MAX);
  136. size:=0;
  137. bufsize:=0;
  138. i:=0;
  139. while (i<argc) do
  140. begin
  141. len:=strlen(argv[i]);
  142. if len>ARG_MAX-2 then
  143. len:=ARG_MAX-2;
  144. found:=false;
  145. for j:=1 to len do
  146. if argv[i][j]=' ' then
  147. begin
  148. found:=true;
  149. break;
  150. end;
  151. if bufsize+len>=ARG_MAX-2 then
  152. AddBuf;
  153. if found then
  154. begin
  155. buf[bufsize]:='"';
  156. inc(bufsize);
  157. end;
  158. move(argv[i]^,buf[bufsize],len);
  159. inc(bufsize,len);
  160. if found then
  161. begin
  162. buf[bufsize]:='"';
  163. inc(bufsize);
  164. end;
  165. if i<argc then
  166. buf[bufsize]:=' '
  167. else
  168. buf[bufsize]:=#0;
  169. inc(bufsize);
  170. inc(i);
  171. end;
  172. AddBuf;
  173. FreeMem(buf,ARG_MAX);
  174. end;
  175. procedure SysInitStdIO;
  176. begin
  177. OpenStdIO(Input,fmInput,StdInputHandle);
  178. OpenStdIO(Output,fmOutput,StdOutputHandle);
  179. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  180. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  181. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  182. end;
  183. {$ifdef FPC_USE_LIBC}
  184. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  185. {$ifdef Darwin}
  186. {$ifndef FPC_DARWIN_PASCALMAIN}
  187. procedure pascalmain;external name 'PASCALMAIN';
  188. { Main entry point in C style, needed to capture program parameters. }
  189. procedure main(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  190. {$else FPC_DARWIN_PASCALMAIN}
  191. {$ifdef FPC_DARWIN_JMP_MAIN}
  192. procedure pascalmain;cdecl;external name 'PASCALMAIN';
  193. {$endif}
  194. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  195. {$endif FPC_DARWIN_PASCALMAIN}
  196. begin
  197. argc:= argcparam;
  198. argv:= argvparam;
  199. envp:= envpparam;
  200. {$ifdef cpui386}
  201. Set8087CW(Default8087CW);
  202. {$endif cpui386}
  203. {$if not defined(FPC_DARWIN_PASCALMAIN) or defined(FPC_DARWIN_JMP_MAIN)}
  204. pascalmain; {run the pascal main program}
  205. {$endif}
  206. end;
  207. {$endif Darwin}
  208. {$endif FPC_USE_LIBC}
  209. function GetProcessID: SizeUInt;
  210. begin
  211. GetProcessID := SizeUInt (fpGetPID);
  212. end;
  213. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  214. begin
  215. result := stklen;
  216. end;
  217. Begin
  218. IsConsole := TRUE;
  219. IsLibrary := FALSE;
  220. StackLength := CheckInitialStkLen(InitialStkLen);
  221. StackBottom := Sptr - StackLength;
  222. { Set up signals handlers }
  223. InstallSignals;
  224. { Setup heap }
  225. InitHeap;
  226. SysInitExceptions;
  227. { Setup stdin, stdout and stderr }
  228. SysInitStdIO;
  229. { Reset IO Error }
  230. InOutRes:=0;
  231. { Arguments }
  232. SetupCmdLine;
  233. { threading }
  234. InitSystemThreads;
  235. initvariantmanager;
  236. initwidestringmanager;
  237. End.