system.pp 9.1 KB

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