2
0

system.pp 8.0 KB

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