system.pp 7.4 KB

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