2
0

system.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2011 by Francesco Lombardi.
  4. System unit for Nintendo Wii
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit System;
  12. interface
  13. {$define FPC_IS_SYSTEM}
  14. {$define HAS_CMDLINE}
  15. {$define FPC_HAS_FEATURE_THREADING}
  16. {$define FPC_HAS_FEATURE_CONSOLEIO}
  17. {$define FPC_HAS_FEATURE_COMMANDARGS}
  18. {$define FPC_HAS_FEATURE_TEXTIO}
  19. {$define FPC_HAS_FEATURE_FILEIO}
  20. {$i systemh.inc}
  21. {$i libch.inc}
  22. {$i wiih.inc}
  23. const
  24. LineEnding = #10;
  25. LFNSupport = true;
  26. DirectorySeparator = '/';
  27. DriveSeparator = ':';
  28. ExtensionSeparator = '.';
  29. PathSeparator = ':';
  30. AllowDirectorySeparators : set of AnsiChar = ['\','/'];
  31. AllowDriveSeparators : set of AnsiChar = [':'];
  32. maxExitCode = 255;
  33. MaxPathLen = 1024; // BSDs since 1993, Solaris 10, Darwin
  34. AllFilesMask = '*';
  35. UnusedHandle = -1;
  36. StdInputHandle = 0;
  37. StdOutputHandle = 1;
  38. StdErrorHandle = 2;
  39. FileNameCaseSensitive : boolean = true;
  40. FileNameCasePreserving: boolean = true;
  41. CtrlZMarksEOF: boolean = true; (* #26 not considered as end of file *)
  42. sLineBreak = LineEnding;
  43. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  44. var
  45. argc: LongInt = 0;
  46. argv: PPAnsiChar;
  47. envp: PPAnsiChar;
  48. // errno: integer;
  49. function get_cmdline:PAnsiChar;
  50. property cmdline:PAnsiChar read get_cmdline;
  51. implementation
  52. const
  53. calculated_cmdline: PAnsiChar = nil;
  54. { System limits, POSIX value in parentheses, used for buffer and stack allocation }
  55. ARG_MAX = 65536; {4096} { Maximum number of argument size }
  56. PATH_MAX = 1024; {255} { Maximum number of bytes in pathname }
  57. {$i system.inc}
  58. {$i libc.inc}
  59. {$i wii.inc}
  60. {$ifdef FPC_HAS_FEATURE_PROCESSES}
  61. function GetProcessID: SizeUInt;
  62. begin
  63. GetProcessID := 0;
  64. end;
  65. {$endif}
  66. {*****************************************************************************
  67. Misc. System Dependent Functions
  68. *****************************************************************************}
  69. procedure System_exit;
  70. begin
  71. // Boo!
  72. end;
  73. {*****************************************************************************
  74. ParamStr/Randomize
  75. *****************************************************************************}
  76. { set randseed to a new pseudo random value }
  77. procedure randomize;
  78. begin
  79. end;
  80. Function ParamCount: Longint;
  81. Begin
  82. Paramcount:=argc-1
  83. End;
  84. { variable where full path and filename and executable is stored }
  85. { is setup by the startup of the system unit. }
  86. var
  87. execpathstr : shortstring;
  88. function paramstr(l: longint) : shortstring;
  89. begin
  90. { stricly conforming POSIX applications }
  91. { have the executing filename as argv[0] }
  92. if l=0 then
  93. begin
  94. paramstr := execpathstr;
  95. end
  96. else if (l > 0) and ( l < argc ) then
  97. paramstr:=strpas(argv[l])
  98. else
  99. paramstr:='';
  100. end;
  101. {*****************************************************************************
  102. cmdline
  103. *****************************************************************************}
  104. procedure SetupCmdLine;
  105. var
  106. bufsize,
  107. len,j,
  108. size,i : longint;
  109. found : boolean;
  110. buf : PAnsiChar;
  111. procedure AddBuf;
  112. begin
  113. reallocmem(calculated_cmdline,size+bufsize);
  114. move(buf^,calculated_cmdline[size],bufsize);
  115. inc(size,bufsize);
  116. bufsize:=0;
  117. end;
  118. begin
  119. if argc<=0 then
  120. exit;
  121. GetMem(buf,ARG_MAX);
  122. size:=0;
  123. bufsize:=0;
  124. i:=0;
  125. while (i<argc) do
  126. begin
  127. len:=strlen(argv[i]);
  128. if len>ARG_MAX-2 then
  129. len:=ARG_MAX-2;
  130. found:=false;
  131. for j:=1 to len do
  132. if argv[i][j]=' ' then
  133. begin
  134. found:=true;
  135. break;
  136. end;
  137. if bufsize+len>=ARG_MAX-2 then
  138. AddBuf;
  139. if found then
  140. begin
  141. buf[bufsize]:='"';
  142. inc(bufsize);
  143. end;
  144. move(argv[i]^,buf[bufsize],len);
  145. inc(bufsize,len);
  146. if found then
  147. begin
  148. buf[bufsize]:='"';
  149. inc(bufsize);
  150. end;
  151. if i<argc-1 then
  152. buf[bufsize]:=' '
  153. else
  154. buf[bufsize]:=#0;
  155. inc(bufsize);
  156. inc(i);
  157. end;
  158. AddBuf;
  159. FreeMem(buf,ARG_MAX);
  160. end;
  161. function get_cmdline:PAnsiChar;
  162. begin
  163. if calculated_cmdline=nil then
  164. setupcmdline;
  165. get_cmdline:=calculated_cmdline;
  166. end;
  167. procedure SysInitStdIO;
  168. begin
  169. OpenStdIO(Input,fmInput,StdInputHandle);
  170. OpenStdIO(Output,fmOutput,StdOutputHandle);
  171. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  172. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  173. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  174. end;
  175. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  176. begin
  177. result := stklen;
  178. end;
  179. begin
  180. StackLength := CheckInitialStkLen(InitialStkLen);
  181. StackBottom := Sptr - StackLength;
  182. { OS specific startup }
  183. { Set up signals handlers }
  184. { Setup heap }
  185. InitHeap;
  186. SysInitExceptions;
  187. initunicodestringmanager;
  188. SetupCmdLine;
  189. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  190. { Setup stdin, stdout and stderr }
  191. SysInitStdIO;
  192. { Reset IO Error }
  193. InOutRes:=0;
  194. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  195. { Arguments }
  196. {$ifdef FPC_HAS_FEATURE_THREADING}
  197. { threading }
  198. InitSystemThreads;
  199. {$endif FPC_HAS_FEATURE_THREADING}
  200. end.