system.pp 5.0 KB

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