system.pp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.24 2005-02-13 21:47:56 peter
  223. * include file cleanup part 2
  224. Revision 1.23 2005/02/06 12:16:52 peter
  225. * bsd thread updates
  226. Revision 1.22 2005/02/01 20:22:49 florian
  227. * improved widestring infrastructure manager
  228. Revision 1.21 2004/12/05 14:36:37 hajny
  229. + GetProcessID added
  230. Revision 1.20 2004/11/04 09:32:31 peter
  231. ErrOutput added
  232. Revision 1.19 2004/07/17 15:31:03 jonas
  233. * initialise StackLength (fixes stack checking in general, and tw2897 in
  234. particular)
  235. Revision 1.18 2004/07/03 22:49:34 daniel
  236. * Moved declarations downwards
  237. Revision 1.17 2004/07/03 22:44:37 daniel
  238. * Declared envp,argc,argv in interface for Darwin
  239. Revision 1.16 2004/06/19 08:06:04 marco
  240. * moved heap.inc and text.inc before sysalloc as suggested. Why wasn't this
  241. done directly?
  242. Revision 1.15 2004/06/17 16:16:13 peter
  243. * New heapmanager that releases memory back to the OS, donated
  244. by Micha Nelissen
  245. Revision 1.14 2004/01/22 13:46:14 marco
  246. bsd
  247. Revision 1.13 2004/01/20 23:09:14 hajny
  248. * ExecuteProcess fixes, ProcessID and ThreadID added
  249. Revision 1.12 2004/01/04 20:32:05 jonas
  250. + geterrnolocation for Darwin
  251. + C-style main for Darwin (generic, can be used for anything)
  252. Revision 1.11 2003/12/30 12:26:21 marco
  253. * FPC_USE_LIBC
  254. Revision 1.10 2003/10/26 17:01:04 marco
  255. * moved sigprocmask to system
  256. Revision 1.9 2003/10/26 16:42:22 marco
  257. * texception4 fix merge
  258. Revision 1.8 2003/01/05 19:01:28 marco
  259. * FreeBSD compiles now with baseunix mods.
  260. Revision 1.7 2002/11/12 14:57:48 marco
  261. * Ugly hack to temporarily be able to use system.pp for Linux too
  262. Revision 1.6 2002/10/27 11:58:30 marco
  263. * Modifications from Saturday.
  264. Revision 1.5 2002/10/26 18:27:51 marco
  265. * First series POSIX calls commits. Including getcwd.
  266. Revision 1.4 2002/10/18 12:19:58 marco
  267. * Fixes to get the generic *BSD RTL compiling again + fixes for thread
  268. support. Still problems left in fexpand. (inoutres?) Therefore fixed
  269. sysposix not yet commited
  270. Revision 1.3 2002/10/13 09:25:39 florian
  271. + call to initvariantmanager inserted
  272. Revision 1.2 2002/09/07 16:01:17 peter
  273. * old logs removed and tabs fixed
  274. Revision 1.1 2002/08/19 12:29:11 marco
  275. * First working POSIX *BSD system unit.
  276. }