system.pp 7.1 KB

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