system.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. {
  2. $Id: system.pp,v 1.25 2005/04/24 21:19:22 peter 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 Linux.
  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_IS_SYSTEM}
  20. {$i osdefs.inc}
  21. {$I sysunixh.inc}
  22. Implementation
  23. {$I system.inc}
  24. {*****************************************************************************
  25. Misc. System Dependent Functions
  26. *****************************************************************************}
  27. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  28. procedure System_exit;
  29. begin
  30. haltproc(ExitCode);
  31. End;
  32. Function ParamCount: Longint;
  33. Begin
  34. Paramcount:=argc-1
  35. End;
  36. function BackPos(c:char; const s: shortstring): integer;
  37. var
  38. i: integer;
  39. Begin
  40. for i:=length(s) downto 0 do
  41. if s[i] = c then break;
  42. if i=0 then
  43. BackPos := 0
  44. else
  45. BackPos := i;
  46. end;
  47. { variable where full path and filename and executable is stored }
  48. { is setup by the startup of the system unit. }
  49. var
  50. execpathstr : shortstring;
  51. function paramstr(l: longint) : string;
  52. begin
  53. { stricly conforming POSIX applications }
  54. { have the executing filename as argv[0] }
  55. if l=0 then
  56. begin
  57. paramstr := execpathstr;
  58. end
  59. else
  60. paramstr:=strpas(argv[l]);
  61. end;
  62. Procedure Randomize;
  63. Begin
  64. randseed:=longint(Fptime(nil));
  65. End;
  66. {*****************************************************************************
  67. SystemUnit Initialization
  68. *****************************************************************************}
  69. function reenable_signal(sig : longint) : boolean;
  70. var
  71. e : TSigSet;
  72. i,j : byte;
  73. begin
  74. fillchar(e,sizeof(e),#0);
  75. { set is 1 based PM }
  76. dec(sig);
  77. i:=sig mod 32;
  78. j:=sig div 32;
  79. e[j]:=1 shl i;
  80. fpsigprocmask(SIG_UNBLOCK,@e,nil);
  81. reenable_signal:=geterrno=0;
  82. end;
  83. // signal handler is arch dependant due to processorexception to language
  84. // exception translation
  85. {$i sighnd.inc}
  86. var
  87. act: SigActionRec;
  88. Procedure InstallSignals;
  89. begin
  90. { Initialize the sigaction structure }
  91. { all flags and information set to zero }
  92. FillChar(act, sizeof(SigActionRec),0);
  93. { initialize handler }
  94. act.sa_handler := SigActionHandler(@SignalToRunError);
  95. act.sa_flags:=SA_SIGINFO
  96. {$ifdef cpux86_64}
  97. or $4000000
  98. {$endif cpux86_64}
  99. ;
  100. FpSigAction(SIGFPE,@act,nil);
  101. FpSigAction(SIGSEGV,@act,nil);
  102. FpSigAction(SIGBUS,@act,nil);
  103. FpSigAction(SIGILL,@act,nil);
  104. end;
  105. procedure SetupCmdLine;
  106. var
  107. bufsize,
  108. len,j,
  109. size,i : longint;
  110. found : boolean;
  111. buf : pchar;
  112. procedure AddBuf;
  113. begin
  114. reallocmem(cmdline,size+bufsize);
  115. move(buf^,cmdline[size],bufsize);
  116. inc(size,bufsize);
  117. bufsize:=0;
  118. end;
  119. begin
  120. GetMem(buf,ARG_MAX);
  121. size:=0;
  122. bufsize:=0;
  123. i:=0;
  124. while (i<argc) do
  125. begin
  126. len:=strlen(argv[i]);
  127. if len>ARG_MAX-2 then
  128. len:=ARG_MAX-2;
  129. found:=false;
  130. for j:=1 to len do
  131. if argv[i][j]=' ' then
  132. begin
  133. found:=true;
  134. break;
  135. end;
  136. if bufsize+len>=ARG_MAX-2 then
  137. AddBuf;
  138. if found then
  139. begin
  140. buf[bufsize]:='"';
  141. inc(bufsize);
  142. end;
  143. move(argv[i]^,buf[bufsize],len);
  144. inc(bufsize,len);
  145. if found then
  146. begin
  147. buf[bufsize]:='"';
  148. inc(bufsize);
  149. end;
  150. if i<argc then
  151. buf[bufsize]:=' '
  152. else
  153. buf[bufsize]:=#0;
  154. inc(bufsize);
  155. inc(i);
  156. end;
  157. AddBuf;
  158. FreeMem(buf,ARG_MAX);
  159. end;
  160. procedure SysInitStdIO;
  161. begin
  162. OpenStdIO(Input,fmInput,StdInputHandle);
  163. OpenStdIO(Output,fmOutput,StdOutputHandle);
  164. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  165. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  166. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  167. end;
  168. procedure SysInitExecPath;
  169. var
  170. i : longint;
  171. begin
  172. execpathstr[0]:=#0;
  173. i:=Fpreadlink('/proc/self/exe',@execpathstr[1],high(execpathstr));
  174. { it must also be an absolute filename, linux 2.0 points to a memory
  175. location so this will skip that }
  176. if (i>0) and (execpathstr[1]='/') then
  177. execpathstr[0]:=char(i);
  178. end;
  179. function GetProcessID: SizeUInt;
  180. begin
  181. GetProcessID := SizeUInt (fpGetPID);
  182. end;
  183. Begin
  184. IsConsole := TRUE;
  185. IsLibrary := FALSE;
  186. StackLength := InitialStkLen;
  187. StackBottom := Sptr - StackLength;
  188. { Set up signals handlers }
  189. InstallSignals;
  190. { Setup heap }
  191. InitHeap;
  192. SysInitExceptions;
  193. { Arguments }
  194. SetupCmdLine;
  195. SysInitExecPath;
  196. { Setup stdin, stdout and stderr }
  197. SysInitStdIO;
  198. { Reset IO Error }
  199. InOutRes:=0;
  200. { threading }
  201. InitSystemThreads;
  202. {$ifdef HASVARIANT}
  203. initvariantmanager;
  204. {$endif HASVARIANT}
  205. {$ifdef HASWIDESTRING}
  206. initwidestringmanager;
  207. {$endif HASWIDESTRING}
  208. End.
  209. {
  210. $Log: system.pp,v $
  211. Revision 1.25 2005/04/24 21:19:22 peter
  212. * unblock signal in signalhandler, remove the sigprocmask call
  213. from setjmp
  214. Revision 1.24 2005/02/14 17:13:30 peter
  215. * truncate log
  216. Revision 1.23 2005/02/13 21:47:56 peter
  217. * include file cleanup part 2
  218. Revision 1.22 2005/02/06 11:20:52 peter
  219. * threading in system unit
  220. * removed systhrds unit
  221. Revision 1.21 2005/02/01 20:22:49 florian
  222. * improved widestring infrastructure manager
  223. }