system.pp 9.0 KB

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