system.pp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2006 by Florian Klaempfl
  4. member of the Free Pascal development team.
  5. System unit for embedded systems
  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. Unit System;
  13. {*****************************************************************************}
  14. interface
  15. {*****************************************************************************}
  16. {$define FPC_IS_SYSTEM}
  17. {$define HAS_CMDLINE}
  18. {$define USE_NOTHREADMANAGER}
  19. {$I check.inc}
  20. {$I systemh.inc}
  21. {
  22. fix me
  23. const
  24. LineEnding = #10;
  25. LFNSupport = true;
  26. DirectorySeparator = '/';
  27. DriveSeparator = ':';
  28. PathSeparator = ':';
  29. { FileNameCaseSensitive is defined below! }
  30. maxExitCode = 255;
  31. MaxPathLen = 1024; // BSDs since 1993, Solaris 10, Darwin
  32. const
  33. UnusedHandle = -1;
  34. StdInputHandle = 0;
  35. StdOutputHandle = 1;
  36. StdErrorHandle = 2;
  37. FileNameCaseSensitive : boolean = true;
  38. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  39. sLineBreak = LineEnding;
  40. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  41. var
  42. argc:longint;external name 'operatingsystem_parameter_argc';
  43. argv:PPchar;external name 'operatingsystem_parameter_argv';
  44. envp:PPchar;external name 'operatingsystem_parameter_envp';
  45. }
  46. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  47. function get_cmdline:Pchar;
  48. property cmdline:Pchar read get_cmdline;
  49. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  50. {$ifdef FPC_HAS_FEATURE_SOFTFPU}
  51. {$define fpc_softfpu_interface}
  52. {$i softfpu.pp}
  53. {$undef fpc_softfpu_interface}
  54. {$endif FPC_HAS_FEATURE_SOFTFPU}
  55. {*****************************************************************************}
  56. implementation
  57. {*****************************************************************************}
  58. { Include ELF resources }
  59. const calculated_cmdline:Pchar=nil;
  60. {$ifdef FPC_HAS_FEATURE_SOFTFPU}
  61. {$define fpc_softfpu_implementation}
  62. {$i softfpu.pp}
  63. {$undef fpc_softfpu_implementation}
  64. { we get these functions and types from the softfpu code }
  65. {$define FPC_SYSTEM_HAS_float64}
  66. {$define FPC_SYSTEM_HAS_float32}
  67. {$define FPC_SYSTEM_HAS_flag}
  68. {$define FPC_SYSTEM_HAS_extractFloat64Frac0}
  69. {$define FPC_SYSTEM_HAS_extractFloat64Frac1}
  70. {$define FPC_SYSTEM_HAS_extractFloat64Exp}
  71. {$define FPC_SYSTEM_HAS_extractFloat64Frac}
  72. {$define FPC_SYSTEM_HAS_extractFloat64Sign}
  73. {$define FPC_SYSTEM_HAS_ExtractFloat32Frac}
  74. {$define FPC_SYSTEM_HAS_extractFloat32Exp}
  75. {$define FPC_SYSTEM_HAS_extractFloat32Sign}
  76. {$endif FPC_HAS_FEATURE_SOFTFPU}
  77. {$I system.inc}
  78. {*****************************************************************************
  79. Misc. System Dependent Functions
  80. *****************************************************************************}
  81. procedure haltproc(e:longint);cdecl;external name '_haltproc';
  82. procedure System_exit;
  83. begin
  84. haltproc(ExitCode);
  85. End;
  86. {$ifdef FPC_HAS_FEATURE_RANDOM}
  87. Procedure Randomize;
  88. Begin
  89. randseed:=longint(Fptime(nil));
  90. End;
  91. {$endif FPC_HAS_FEATURE_RANDOM}
  92. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  93. Function ParamCount: Longint;
  94. Begin
  95. Paramcount:=argc-1
  96. End;
  97. { variable where full path and filename and executable is stored }
  98. { is setup by the startup of the system unit. }
  99. var
  100. execpathstr : shortstring;
  101. function paramstr(l: longint) : string;
  102. begin
  103. { stricly conforming POSIX applications }
  104. { have the executing filename as argv[0] }
  105. if l=0 then
  106. begin
  107. paramstr := execpathstr;
  108. end
  109. else
  110. paramstr:=strpas(argv[l]);
  111. end;
  112. {*****************************************************************************
  113. cmdline
  114. *****************************************************************************}
  115. procedure SetupCmdLine;
  116. var
  117. bufsize,
  118. len,j,
  119. size,i : longint;
  120. found : boolean;
  121. buf : pchar;
  122. procedure AddBuf;
  123. begin
  124. reallocmem(calculated_cmdline,size+bufsize);
  125. move(buf^,calculated_cmdline[size],bufsize);
  126. inc(size,bufsize);
  127. bufsize:=0;
  128. end;
  129. begin
  130. if argc<=0 then
  131. exit;
  132. GetMem(buf,ARG_MAX);
  133. size:=0;
  134. bufsize:=0;
  135. i:=0;
  136. while (i<argc) do
  137. begin
  138. len:=strlen(argv[i]);
  139. if len>ARG_MAX-2 then
  140. len:=ARG_MAX-2;
  141. found:=false;
  142. for j:=1 to len do
  143. if argv[i][j]=' ' then
  144. begin
  145. found:=true;
  146. break;
  147. end;
  148. if bufsize+len>=ARG_MAX-2 then
  149. AddBuf;
  150. if found then
  151. begin
  152. buf[bufsize]:='"';
  153. inc(bufsize);
  154. end;
  155. move(argv[i]^,buf[bufsize],len);
  156. inc(bufsize,len);
  157. if found then
  158. begin
  159. buf[bufsize]:='"';
  160. inc(bufsize);
  161. end;
  162. if i<argc then
  163. buf[bufsize]:=' '
  164. else
  165. buf[bufsize]:=#0;
  166. inc(bufsize);
  167. inc(i);
  168. end;
  169. AddBuf;
  170. FreeMem(buf,ARG_MAX);
  171. end;
  172. function get_cmdline:Pchar;
  173. begin
  174. if calculated_cmdline=nil then
  175. setupcmdline;
  176. get_cmdline:=calculated_cmdline;
  177. end;
  178. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  179. {*****************************************************************************
  180. SystemUnit Initialization
  181. *****************************************************************************}
  182. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  183. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  184. var
  185. limits : TRLimit;
  186. success : boolean;
  187. begin
  188. success := false;
  189. fillchar(limits, sizeof(limits), 0);
  190. {$ifdef has_ugetrlimit}
  191. success := fpugetrlimit(RLIMIT_STACK, @limits)=0;
  192. {$endif}
  193. if (not success) then
  194. success := fpgetrlimit(RLIMIT_STACK, @limits)=0;
  195. if (success) and (limits.rlim_cur < stklen) then
  196. result := limits.rlim_cur
  197. else
  198. result := stklen;
  199. end;
  200. var
  201. initialstkptr : Pointer;external name '__stkptr';
  202. {$endif FPC_HAS_FEATURE_STACKCHECK}
  203. begin
  204. {$ifdef FPC_HAS_FEATURE_FPU}
  205. SysResetFPU;
  206. {$endif FPC_HAS_FEATURE_FPU}
  207. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  208. IsConsole := TRUE;
  209. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  210. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  211. StackLength := CheckInitialStkLen(initialStkLen);
  212. StackBottom := initialstkptr - StackLength;
  213. {$endif FPC_HAS_FEATURE_STACKCHECK}
  214. {$ifdef FPC_HAS_FEATURE_HEAP}
  215. { Setup heap }
  216. InitHeap;
  217. {$endif FPC_HAS_FEATURE_HEAP}
  218. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  219. SysInitExceptions;
  220. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  221. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  222. { Setup stdin, stdout and stderr }
  223. SysInitStdIO;
  224. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  225. {$ifdef FPC_HAS_FEATURE_COMMANDARGS}
  226. { Arguments }
  227. SysInitExecPath;
  228. SetupCmdLine;
  229. {$endif FPC_HAS_FEATURE_COMMANDARGS}
  230. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  231. { Reset IO Error }
  232. InOutRes:=0;
  233. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  234. {$ifdef FPC_HAS_FEATURE_THREADING}
  235. { threading }
  236. InitSystemThreads;
  237. {$endif FPC_HAS_FEATURE_THREADING}
  238. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  239. initvariantmanager;
  240. {$endif FPC_HAS_FEATURE_VARIANTS}
  241. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  242. initwidestringmanager;
  243. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  244. end.