system.pp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. {$ifdef FPC_HAS_SETSYSNR_INC}
  55. {$I setsysnr.inc}
  56. {$endif FPC_HAS_SETSYSNR_INC}
  57. {*****************************************************************************
  58. Misc. System Dependent Functions
  59. *****************************************************************************}
  60. {$ifdef darwin}
  61. procedure normalexit(status: cint); cdecl; external 'c' name 'exit';
  62. {$endif}
  63. procedure System_exit;
  64. {$ifndef darwin}
  65. begin
  66. Fpexit(cint(ExitCode));
  67. end;
  68. {$else darwin}
  69. begin
  70. { make sure the libc atexit handlers are called, needed for e.g. profiling }
  71. normalexit(cint(ExitCode));
  72. end;
  73. {$endif darwin}
  74. Function ParamCount: Longint;
  75. Begin
  76. Paramcount:=argc-1
  77. End;
  78. function BackPos(c:char; const s: shortstring): integer;
  79. var
  80. i: integer;
  81. Begin
  82. for i:=length(s) downto 0 do
  83. if s[i] = c then break;
  84. if i=0 then
  85. BackPos := 0
  86. else
  87. BackPos := i;
  88. end;
  89. { variable where full path and filename and executable is stored }
  90. { is setup by the startup of the system unit. }
  91. //var
  92. // execpathstr : shortstring;
  93. function paramstr(l: longint) : string;
  94. begin
  95. { stricly conforming POSIX applications }
  96. { have the executing filename as argv[0] }
  97. // if l=0 then
  98. // begin
  99. // paramstr := execpathstr;
  100. // end
  101. // else
  102. if (l < argc) then
  103. paramstr:=strpas(argv[l])
  104. else
  105. paramstr:='';
  106. end;
  107. Procedure Randomize;
  108. Begin
  109. randseed:=longint(Fptime(nil));
  110. End;
  111. {*****************************************************************************
  112. System Unit Initialization
  113. *****************************************************************************}
  114. function reenable_signal(sig : longint) : boolean;
  115. var
  116. e,oe : TSigSet;
  117. i,j : byte;
  118. olderrno: cint;
  119. begin
  120. fillchar(e,sizeof(e),#0);
  121. fillchar(oe,sizeof(oe),#0);
  122. { set is 1 based PM }
  123. dec(sig);
  124. i:=sig mod 32;
  125. j:=sig div 32;
  126. e[j]:=1 shl i;
  127. { this routine is called from a signal handler, so must not change errno }
  128. olderrno:=geterrno;
  129. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  130. reenable_signal:=geterrno=0;
  131. seterrno(olderrno);
  132. end;
  133. {$ifdef DEBUG}
  134. { Declare InstallDefaultSignalHandler as forward to be able
  135. to test aclling fpsigaction again within SignalToRunError
  136. function implemented within sighnd.inc inlcude file }
  137. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); forward;
  138. {$endif}
  139. {$i sighnd.inc}
  140. procedure InstallDefaultSignalHandler(signum: longint; out oldact: SigActionRec); public name '_FPC_INSTALLDEFAULTSIGHANDLER';
  141. var
  142. act: SigActionRec;
  143. begin
  144. { Initialize the sigaction structure }
  145. { all flags and information set to zero }
  146. FillChar(act,sizeof(SigActionRec),0);
  147. { initialize handler }
  148. act.sa_handler:=@SignalToRunError;
  149. {$if defined(darwin) and defined(cpu64)}
  150. act.sa_flags:=SA_SIGINFO or SA_64REGSET;
  151. {$else}
  152. act.sa_flags:=SA_SIGINFO;
  153. {$endif}
  154. FpSigAction(signum,@act,@oldact);
  155. end;
  156. var
  157. oldsigfpe: SigActionRec; public name '_FPC_OLDSIGFPE';
  158. oldsigsegv: SigActionRec; public name '_FPC_OLDSIGSEGV';
  159. oldsigbus: SigActionRec; public name '_FPC_OLDSIGBUS';
  160. oldsigill: SigActionRec; public name '_FPC_OLDSIGILL';
  161. Procedure InstallSignals;
  162. begin
  163. InstallDefaultSignalHandler(SIGFPE,oldsigfpe);
  164. InstallDefaultSignalHandler(SIGSEGV,oldsigsegv);
  165. InstallDefaultSignalHandler(SIGBUS,oldsigbus);
  166. InstallDefaultSignalHandler(SIGILL,oldsigill);
  167. end;
  168. Procedure RestoreOldSignalHandlers;
  169. begin
  170. FpSigAction(SIGFPE,@oldsigfpe,nil);
  171. FpSigAction(SIGSEGV,@oldsigsegv,nil);
  172. FpSigAction(SIGBUS,@oldsigbus,nil);
  173. FpSigAction(SIGILL,@oldsigill,nil);
  174. end;
  175. procedure SetupCmdLine;
  176. var
  177. bufsize,
  178. len,j,
  179. size,i : longint;
  180. found : boolean;
  181. buf : pchar;
  182. procedure AddBuf;
  183. begin
  184. reallocmem(cmdline,size+bufsize);
  185. move(buf^,cmdline[size],bufsize);
  186. inc(size,bufsize);
  187. bufsize:=0;
  188. end;
  189. begin
  190. GetMem(buf,ARG_MAX);
  191. size:=0;
  192. bufsize:=0;
  193. i:=0;
  194. while (i<argc) do
  195. begin
  196. len:=strlen(argv[i]);
  197. if len>ARG_MAX-2 then
  198. len:=ARG_MAX-2;
  199. found:=false;
  200. for j:=1 to len do
  201. if argv[i][j]=' ' then
  202. begin
  203. found:=true;
  204. break;
  205. end;
  206. if bufsize+len>=ARG_MAX-2 then
  207. AddBuf;
  208. if found then
  209. begin
  210. buf[bufsize]:='"';
  211. inc(bufsize);
  212. end;
  213. move(argv[i]^,buf[bufsize],len);
  214. inc(bufsize,len);
  215. if found then
  216. begin
  217. buf[bufsize]:='"';
  218. inc(bufsize);
  219. end;
  220. if i<argc-1 then
  221. buf[bufsize]:=' '
  222. else
  223. buf[bufsize]:=#0;
  224. inc(bufsize);
  225. inc(i);
  226. end;
  227. AddBuf;
  228. FreeMem(buf,ARG_MAX);
  229. end;
  230. procedure SysInitStdIO;
  231. begin
  232. OpenStdIO(Input,fmInput,StdInputHandle);
  233. OpenStdIO(Output,fmOutput,StdOutputHandle);
  234. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  235. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  236. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  237. end;
  238. {$ifdef FPC_USE_LIBC}
  239. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  240. {$ifdef Darwin}
  241. procedure pascalmain;cdecl;external name 'PASCALMAIN';
  242. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  243. begin
  244. argc:= argcparam;
  245. argv:= argvparam;
  246. envp:= envpparam;
  247. {$ifdef cpui386}
  248. Set8087CW(Default8087CW);
  249. {$endif cpui386}
  250. pascalmain; {run the pascal main program}
  251. end;
  252. {$endif Darwin}
  253. {$endif FPC_USE_LIBC}
  254. function GetProcessID: SizeUInt;
  255. begin
  256. GetProcessID := SizeUInt (fpGetPID);
  257. end;
  258. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  259. var
  260. stackpointer: ptruint;
  261. begin
  262. stackpointer := (ptruint(sptr) + 4095) and not(4095);
  263. if stklen > stackpointer then
  264. stklen := stackpointer-4096;
  265. result := stklen;
  266. end;
  267. Begin
  268. IsConsole := TRUE;
  269. StackLength := CheckInitialStkLen(InitialStkLen);
  270. StackBottom := Sptr - StackLength;
  271. {$ifdef FPC_HAS_SETSYSNR_INC}
  272. { This procedure is needed for openbsd system which re-uses
  273. the same syscall numbers depending on OS version }
  274. SetSyscallNumbers;
  275. {$endif FPC_HAS_SETSYSNR_INC}
  276. { Set up signals handlers (may be needed by init code to test cpu features) }
  277. InstallSignals;
  278. {$if defined(cpui386) or defined(cpuarm)}
  279. fpc_cpucodeinit;
  280. {$endif cpui386}
  281. { Setup heap }
  282. InitHeap;
  283. SysInitExceptions;
  284. initunicodestringmanager;
  285. { Setup stdin, stdout and stderr }
  286. SysInitStdIO;
  287. { Reset IO Error }
  288. InOutRes:=0;
  289. { Arguments }
  290. SetupCmdLine;
  291. { threading }
  292. InitSystemThreads;
  293. InitSystemDynLibs;
  294. { restore original signal handlers in case this is a library }
  295. if IsLibrary then
  296. RestoreOldSignalHandlers;
  297. End.