system.pp 8.1 KB

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