system.pp 6.7 KB

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