system.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time librar~y.
  4. Copyright (c) 2000 by Marco van de Voort
  5. member of the Free Pascal development team.
  6. System unit for the *BSD's.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. { These things are set in the makefile, }
  14. { But you can override them here.}
  15. { If you use an aout system, set the conditional AOUT}
  16. { $Define AOUT}
  17. Unit {$ifdef VER1_0}SysBSD{$else}System{$endif};
  18. Interface
  19. {$define FPC_USE_SIGPROCMASK}
  20. {$define FPC_USE_SIGALTSTACK}
  21. {$ifndef FPC_USE_LIBC}
  22. {$define FPC_USE_SYSCALL}
  23. {$endif}
  24. {$define FPC_IS_SYSTEM}
  25. {$I sysunixh.inc}
  26. {$ifdef Darwin}
  27. var argc:cardinal;
  28. argv:PPchar;
  29. envp:PPchar;
  30. {$endif}
  31. CONST SIGSTKSZ = 40960;
  32. Implementation
  33. {$I system.inc}
  34. {*****************************************************************************
  35. Misc. System Dependent Functions
  36. *****************************************************************************}
  37. procedure System_exit;
  38. begin
  39. Fpexit(cint(ExitCode));
  40. End;
  41. Function ParamCount: Longint;
  42. Begin
  43. Paramcount:=argc-1
  44. End;
  45. function BackPos(c:char; const s: shortstring): integer;
  46. var
  47. i: integer;
  48. Begin
  49. for i:=length(s) downto 0 do
  50. if s[i] = c then break;
  51. if i=0 then
  52. BackPos := 0
  53. else
  54. BackPos := i;
  55. end;
  56. { variable where full path and filename and executable is stored }
  57. { is setup by the startup of the system unit. }
  58. var
  59. execpathstr : shortstring;
  60. function paramstr(l: longint) : string;
  61. var
  62. s: string;
  63. s1: string;
  64. begin
  65. { stricly conforming POSIX applications }
  66. { have the executing filename as argv[0] }
  67. // if l=0 then
  68. // begin
  69. // paramstr := execpathstr;
  70. // end
  71. // else
  72. paramstr:=strpas(argv[l]);
  73. end;
  74. Procedure Randomize;
  75. Begin
  76. randseed:=longint(Fptime(nil));
  77. End;
  78. {*****************************************************************************
  79. SystemUnit Initialization
  80. *****************************************************************************}
  81. function reenable_signal(sig : longint) : boolean;
  82. var
  83. e,oe : TSigSet;
  84. i,j : byte;
  85. begin
  86. fillchar(e,sizeof(e),#0);
  87. fillchar(oe,sizeof(oe),#0);
  88. { set is 1 based PM }
  89. dec(sig);
  90. i:=sig mod 32;
  91. j:=sig div 32;
  92. e[j]:=1 shl i;
  93. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  94. reenable_signal:=geterrno=0;
  95. end;
  96. {$i sighnd.inc}
  97. var
  98. act: SigActionRec;
  99. Procedure InstallSignals;
  100. var
  101. oldact: SigActionRec;
  102. begin
  103. { Initialize the sigaction structure }
  104. { all flags and information set to zero }
  105. FillChar(act, sizeof(SigActionRec),0);
  106. { initialize handler }
  107. act.sa_handler :=@SignalToRunError;
  108. act.sa_flags:=SA_SIGINFO;
  109. FpSigAction(SIGFPE,act,oldact);
  110. FpSigAction(SIGSEGV,act,oldact);
  111. FpSigAction(SIGBUS,act,oldact);
  112. FpSigAction(SIGILL,act,oldact);
  113. end;
  114. procedure SetupCmdLine;
  115. var
  116. bufsize,
  117. len,j,
  118. size,i : longint;
  119. found : boolean;
  120. buf : pchar;
  121. procedure AddBuf;
  122. begin
  123. reallocmem(cmdline,size+bufsize);
  124. move(buf^,cmdline[size],bufsize);
  125. inc(size,bufsize);
  126. bufsize:=0;
  127. end;
  128. begin
  129. GetMem(buf,ARG_MAX);
  130. size:=0;
  131. bufsize:=0;
  132. i:=0;
  133. while (i<argc) do
  134. begin
  135. len:=strlen(argv[i]);
  136. if len>ARG_MAX-2 then
  137. len:=ARG_MAX-2;
  138. found:=false;
  139. for j:=1 to len do
  140. if argv[i][j]=' ' then
  141. begin
  142. found:=true;
  143. break;
  144. end;
  145. if bufsize+len>=ARG_MAX-2 then
  146. AddBuf;
  147. if found then
  148. begin
  149. buf[bufsize]:='"';
  150. inc(bufsize);
  151. end;
  152. move(argv[i]^,buf[bufsize],len);
  153. inc(bufsize,len);
  154. if found then
  155. begin
  156. buf[bufsize]:='"';
  157. inc(bufsize);
  158. end;
  159. if i<argc then
  160. buf[bufsize]:=' '
  161. else
  162. buf[bufsize]:=#0;
  163. inc(bufsize);
  164. inc(i);
  165. end;
  166. AddBuf;
  167. FreeMem(buf,ARG_MAX);
  168. end;
  169. procedure SysInitStdIO;
  170. begin
  171. OpenStdIO(Input,fmInput,StdInputHandle);
  172. OpenStdIO(Output,fmOutput,StdOutputHandle);
  173. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  174. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  175. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  176. end;
  177. {$ifdef FPC_USE_LIBC}
  178. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  179. {$ifdef Darwin}
  180. procedure pascalmain; external name 'PASCALMAIN';
  181. { Main entry point in C style, needed to capture program parameters. }
  182. procedure main(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  183. begin
  184. argc:= argcparam;
  185. argv:= argvparam;
  186. envp:= envpparam;
  187. pascalmain; {run the pascal main program}
  188. end;
  189. {$endif Darwin}
  190. {$endif FPC_USE_LIBC}
  191. function GetProcessID: SizeUInt;
  192. begin
  193. GetProcessID := SizeUInt (fpGetPID);
  194. end;
  195. Begin
  196. IsConsole := TRUE;
  197. IsLibrary := FALSE;
  198. StackLength := InitialStkLen;
  199. StackBottom := Sptr - StackLength;
  200. { Set up signals handlers }
  201. InstallSignals;
  202. { Setup heap }
  203. InitHeap;
  204. SysInitExceptions;
  205. { Arguments }
  206. SetupCmdLine;
  207. { Setup stdin, stdout and stderr }
  208. SysInitStdIO;
  209. { Reset IO Error }
  210. InOutRes:=0;
  211. { threading }
  212. InitSystemThreads;
  213. {$ifdef HASVARIANT}
  214. initvariantmanager;
  215. {$endif HASVARIANT}
  216. {$ifdef HASWIDESTRING}
  217. initwidestringmanager;
  218. {$endif HASWIDESTRING}
  219. End.
  220. {
  221. $Log$
  222. Revision 1.25 2005-02-14 17:13:21 peter
  223. * truncate log
  224. Revision 1.24 2005/02/13 21:47:56 peter
  225. * include file cleanup part 2
  226. Revision 1.23 2005/02/06 12:16:52 peter
  227. * bsd thread updates
  228. Revision 1.22 2005/02/01 20:22:49 florian
  229. * improved widestring infrastructure manager
  230. }