system.pp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. System Unit Initialization
  110. *****************************************************************************}
  111. function reenable_signal(sig : longint) : boolean;
  112. var
  113. e,oe : TSigSet;
  114. i,j : byte;
  115. olderrno: cint;
  116. begin
  117. fillchar(e,sizeof(e),#0);
  118. fillchar(oe,sizeof(oe),#0);
  119. { set is 1 based PM }
  120. dec(sig);
  121. i:=sig mod 32;
  122. j:=sig div 32;
  123. e[j]:=1 shl i;
  124. { this routine is called from a signal handler, so must not change errno }
  125. olderrno:=geterrno;
  126. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  127. reenable_signal:=geterrno=0;
  128. seterrno(olderrno);
  129. end;
  130. {$i sighnd.inc}
  131. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  132. var
  133. act: SigActionRec;
  134. begin
  135. { Initialize the sigaction structure }
  136. { all flags and information set to zero }
  137. FillChar(act,sizeof(SigActionRec),0);
  138. { initialize handler }
  139. act.sa_handler:=@SignalToRunError;
  140. {$if defined(darwin) and defined(cpu64)}
  141. act.sa_flags:=SA_SIGINFO or SA_64REGSET;
  142. {$else}
  143. act.sa_flags:=SA_SIGINFO;
  144. {$endif}
  145. FpSigAction(signum,@act,@oldact);
  146. end;
  147. var
  148. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  149. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  150. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  151. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  152. Procedure InstallSignals;
  153. begin
  154. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  155. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  156. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  157. InstallDefaultSignalHandler(SIGILL,oldsigill);
  158. end;
  159. Procedure RestoreOldSignalHandlers;
  160. begin
  161. FpSigAction(SIGFPE,@oldsigfpe,nil);
  162. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  163. FpSigAction(SIGBUS,@oldsigbus,nil);
  164. FpSigAction(SIGILL,@oldsigill,nil);
  165. end;
  166. procedure SetupCmdLine;
  167. var
  168. bufsize,
  169. len,j,
  170. size,i : longint;
  171. found : boolean;
  172. buf : pchar;
  173. procedure AddBuf;
  174. begin
  175. reallocmem(cmdline,size+bufsize);
  176. move(buf^,cmdline[size],bufsize);
  177. inc(size,bufsize);
  178. bufsize:=0;
  179. end;
  180. begin
  181. GetMem(buf,ARG_MAX);
  182. size:=0;
  183. bufsize:=0;
  184. i:=0;
  185. while (i<argc) do
  186. begin
  187. len:=strlen(argv[i]);
  188. if len>ARG_MAX-2 then
  189. len:=ARG_MAX-2;
  190. found:=false;
  191. for j:=1 to len do
  192. if argv[i][j]=' ' then
  193. begin
  194. found:=true;
  195. break;
  196. end;
  197. if bufsize+len>=ARG_MAX-2 then
  198. AddBuf;
  199. if found then
  200. begin
  201. buf[bufsize]:='"';
  202. inc(bufsize);
  203. end;
  204. move(argv[i]^,buf[bufsize],len);
  205. inc(bufsize,len);
  206. if found then
  207. begin
  208. buf[bufsize]:='"';
  209. inc(bufsize);
  210. end;
  211. if i<argc then
  212. buf[bufsize]:=' '
  213. else
  214. buf[bufsize]:=#0;
  215. inc(bufsize);
  216. inc(i);
  217. end;
  218. AddBuf;
  219. FreeMem(buf,ARG_MAX);
  220. end;
  221. procedure SysInitStdIO;
  222. begin
  223. OpenStdIO(Input,fmInput,StdInputHandle);
  224. OpenStdIO(Output,fmOutput,StdOutputHandle);
  225. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  226. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  227. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  228. end;
  229. {$ifdef FPC_USE_LIBC}
  230. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  231. {$ifdef Darwin}
  232. procedure pascalmain;cdecl;external name 'PASCALMAIN';
  233. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  234. begin
  235. argc:= argcparam;
  236. argv:= argvparam;
  237. envp:= envpparam;
  238. {$ifdef cpui386}
  239. Set8087CW(Default8087CW);
  240. {$endif cpui386}
  241. pascalmain; {run the pascal main program}
  242. end;
  243. {$endif Darwin}
  244. {$endif FPC_USE_LIBC}
  245. function GetProcessID: SizeUInt;
  246. begin
  247. GetProcessID := SizeUInt (fpGetPID);
  248. end;
  249. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  250. begin
  251. result := stklen;
  252. end;
  253. Begin
  254. IsConsole := TRUE;
  255. StackLength := CheckInitialStkLen(InitialStkLen);
  256. StackBottom := Sptr - StackLength;
  257. { Set up signals handlers (may be needed by init code to test cpu features) }
  258. InstallSignals;
  259. SysResetFPU;
  260. if not(IsLibrary) then
  261. SysInitFPU;
  262. {$if defined(cpui386) or defined(cpuarm)}
  263. fpc_cpucodeinit;
  264. {$endif cpui386}
  265. { Setup heap }
  266. InitHeap;
  267. SysInitExceptions;
  268. { Setup stdin, stdout and stderr }
  269. SysInitStdIO;
  270. { Reset IO Error }
  271. InOutRes:=0;
  272. { Arguments }
  273. SetupCmdLine;
  274. { threading }
  275. InitSystemThreads;
  276. initvariantmanager;
  277. {$ifdef VER2_2}
  278. initwidestringmanager;
  279. {$else VER2_2}
  280. initunicodestringmanager;
  281. {$endif VER2_2}
  282. { restore original signal handlers in case this is a library }
  283. if IsLibrary then
  284. RestoreOldSignalHandlers;
  285. End.