system.pp 7.9 KB

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