system.pp 7.3 KB

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