system.pp 6.2 KB

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