system.pp 7.5 KB

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