system.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. {
  2. $Id: system.pp,v 1.26 2005/03/25 22:53:39 jonas Exp $
  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 System;
  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. begin
  62. { stricly conforming POSIX applications }
  63. { have the executing filename as argv[0] }
  64. // if l=0 then
  65. // begin
  66. // paramstr := execpathstr;
  67. // end
  68. // else
  69. paramstr:=strpas(argv[l]);
  70. end;
  71. Procedure Randomize;
  72. Begin
  73. randseed:=longint(Fptime(nil));
  74. End;
  75. {*****************************************************************************
  76. SystemUnit Initialization
  77. *****************************************************************************}
  78. function reenable_signal(sig : longint) : boolean;
  79. var
  80. e,oe : TSigSet;
  81. i,j : byte;
  82. begin
  83. fillchar(e,sizeof(e),#0);
  84. fillchar(oe,sizeof(oe),#0);
  85. { set is 1 based PM }
  86. dec(sig);
  87. i:=sig mod 32;
  88. j:=sig div 32;
  89. e[j]:=1 shl i;
  90. fpsigprocmask(SIG_UNBLOCK,@e,@oe);
  91. reenable_signal:=geterrno=0;
  92. end;
  93. {$i sighnd.inc}
  94. var
  95. act: SigActionRec;
  96. Procedure InstallSignals;
  97. var
  98. oldact: SigActionRec;
  99. begin
  100. { Initialize the sigaction structure }
  101. { all flags and information set to zero }
  102. FillChar(act, sizeof(SigActionRec),0);
  103. { initialize handler }
  104. act.sa_handler :=@SignalToRunError;
  105. act.sa_flags:=SA_SIGINFO;
  106. FpSigAction(SIGFPE,act,oldact);
  107. FpSigAction(SIGSEGV,act,oldact);
  108. FpSigAction(SIGBUS,act,oldact);
  109. FpSigAction(SIGILL,act,oldact);
  110. end;
  111. procedure SetupCmdLine;
  112. var
  113. bufsize,
  114. len,j,
  115. size,i : longint;
  116. found : boolean;
  117. buf : pchar;
  118. procedure AddBuf;
  119. begin
  120. reallocmem(cmdline,size+bufsize);
  121. move(buf^,cmdline[size],bufsize);
  122. inc(size,bufsize);
  123. bufsize:=0;
  124. end;
  125. begin
  126. GetMem(buf,ARG_MAX);
  127. size:=0;
  128. bufsize:=0;
  129. i:=0;
  130. while (i<argc) do
  131. begin
  132. len:=strlen(argv[i]);
  133. if len>ARG_MAX-2 then
  134. len:=ARG_MAX-2;
  135. found:=false;
  136. for j:=1 to len do
  137. if argv[i][j]=' ' then
  138. begin
  139. found:=true;
  140. break;
  141. end;
  142. if bufsize+len>=ARG_MAX-2 then
  143. AddBuf;
  144. if found then
  145. begin
  146. buf[bufsize]:='"';
  147. inc(bufsize);
  148. end;
  149. move(argv[i]^,buf[bufsize],len);
  150. inc(bufsize,len);
  151. if found then
  152. begin
  153. buf[bufsize]:='"';
  154. inc(bufsize);
  155. end;
  156. if i<argc then
  157. buf[bufsize]:=' '
  158. else
  159. buf[bufsize]:=#0;
  160. inc(bufsize);
  161. inc(i);
  162. end;
  163. AddBuf;
  164. FreeMem(buf,ARG_MAX);
  165. end;
  166. procedure SysInitStdIO;
  167. begin
  168. OpenStdIO(Input,fmInput,StdInputHandle);
  169. OpenStdIO(Output,fmOutput,StdOutputHandle);
  170. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  171. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  172. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  173. end;
  174. {$ifdef FPC_USE_LIBC}
  175. { can also be used with other BSD's if they use the system's crtX instead of prtX }
  176. {$ifdef Darwin}
  177. procedure pascalmain; external name 'PASCALMAIN';
  178. { Main entry point in C style, needed to capture program parameters. }
  179. procedure main(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  180. begin
  181. argc:= argcparam;
  182. argv:= argvparam;
  183. envp:= envpparam;
  184. pascalmain; {run the pascal main program}
  185. end;
  186. {$endif Darwin}
  187. {$endif FPC_USE_LIBC}
  188. function GetProcessID: SizeUInt;
  189. begin
  190. GetProcessID := SizeUInt (fpGetPID);
  191. end;
  192. Begin
  193. IsConsole := TRUE;
  194. IsLibrary := FALSE;
  195. StackLength := InitialStkLen;
  196. StackBottom := Sptr - StackLength;
  197. { Set up signals handlers }
  198. InstallSignals;
  199. { Setup heap }
  200. InitHeap;
  201. SysInitExceptions;
  202. { Arguments }
  203. SetupCmdLine;
  204. { Setup stdin, stdout and stderr }
  205. SysInitStdIO;
  206. { Reset IO Error }
  207. InOutRes:=0;
  208. { threading }
  209. InitSystemThreads;
  210. {$ifdef HASVARIANT}
  211. initvariantmanager;
  212. {$endif HASVARIANT}
  213. {$ifdef HASWIDESTRING}
  214. initwidestringmanager;
  215. {$endif HASWIDESTRING}
  216. End.
  217. {
  218. $Log: system.pp,v $
  219. Revision 1.26 2005/03/25 22:53:39 jonas
  220. * fixed several warnings and notes about unused variables (mainly) or
  221. uninitialised use of variables/function results (a few)
  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. }