system.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 char = ['\','/'];
  31. AllowDriveSeparators : set of char = [':'];
  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: PPChar;
  47. envp: PPChar;
  48. // errno: integer;
  49. function get_cmdline:Pchar;
  50. property cmdline:Pchar read get_cmdline;
  51. implementation
  52. const
  53. calculated_cmdline: Pchar = 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. const
  77. QRAN_SHIFT = 15;
  78. QRAN_MASK = ((1 shl QRAN_SHIFT) - 1);
  79. QRAN_MAX = QRAN_MASK;
  80. QRAN_A = 1664525;
  81. QRAN_C = 1013904223;
  82. { set randseed to a new pseudo random value }
  83. procedure randomize;
  84. begin
  85. end;
  86. function random(): integer;
  87. begin
  88. RandSeed := QRAN_A * RandSeed + QRAN_C;
  89. random := (RandSeed shr 16) and QRAN_MAX;
  90. end;
  91. function random(value: integer): integer;
  92. var
  93. a: integer;
  94. begin
  95. RandSeed := QRAN_A * RandSeed + QRAN_C;
  96. a := (RandSeed shr 16) and QRAN_MAX;
  97. random := (a * value) shr 15;
  98. end;
  99. Function ParamCount: Longint;
  100. Begin
  101. Paramcount:=argc-1
  102. End;
  103. { variable where full path and filename and executable is stored }
  104. { is setup by the startup of the system unit. }
  105. var
  106. execpathstr : shortstring;
  107. function paramstr(l: longint) : string;
  108. begin
  109. { stricly conforming POSIX applications }
  110. { have the executing filename as argv[0] }
  111. if l=0 then
  112. begin
  113. paramstr := execpathstr;
  114. end
  115. else
  116. paramstr:=strpas(argv[l]);
  117. end;
  118. {*****************************************************************************
  119. cmdline
  120. *****************************************************************************}
  121. procedure SetupCmdLine;
  122. var
  123. bufsize,
  124. len,j,
  125. size,i : longint;
  126. found : boolean;
  127. buf : pchar;
  128. procedure AddBuf;
  129. begin
  130. reallocmem(calculated_cmdline,size+bufsize);
  131. move(buf^,calculated_cmdline[size],bufsize);
  132. inc(size,bufsize);
  133. bufsize:=0;
  134. end;
  135. begin
  136. if argc<=0 then
  137. exit;
  138. GetMem(buf,ARG_MAX);
  139. size:=0;
  140. bufsize:=0;
  141. i:=0;
  142. while (i<argc) do
  143. begin
  144. len:=strlen(argv[i]);
  145. if len>ARG_MAX-2 then
  146. len:=ARG_MAX-2;
  147. found:=false;
  148. for j:=1 to len do
  149. if argv[i][j]=' ' then
  150. begin
  151. found:=true;
  152. break;
  153. end;
  154. if bufsize+len>=ARG_MAX-2 then
  155. AddBuf;
  156. if found then
  157. begin
  158. buf[bufsize]:='"';
  159. inc(bufsize);
  160. end;
  161. move(argv[i]^,buf[bufsize],len);
  162. inc(bufsize,len);
  163. if found then
  164. begin
  165. buf[bufsize]:='"';
  166. inc(bufsize);
  167. end;
  168. if i<argc-1 then
  169. buf[bufsize]:=' '
  170. else
  171. buf[bufsize]:=#0;
  172. inc(bufsize);
  173. inc(i);
  174. end;
  175. AddBuf;
  176. FreeMem(buf,ARG_MAX);
  177. end;
  178. function get_cmdline:Pchar;
  179. begin
  180. if calculated_cmdline=nil then
  181. setupcmdline;
  182. get_cmdline:=calculated_cmdline;
  183. end;
  184. procedure SysInitStdIO;
  185. begin
  186. OpenStdIO(Input,fmInput,StdInputHandle);
  187. OpenStdIO(Output,fmOutput,StdOutputHandle);
  188. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  189. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  190. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  191. end;
  192. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  193. begin
  194. result := stklen;
  195. end;
  196. begin
  197. StackLength := CheckInitialStkLen(InitialStkLen);
  198. StackBottom := Sptr - StackLength;
  199. { OS specific startup }
  200. { Set up signals handlers }
  201. { Setup heap }
  202. InitHeap;
  203. SysInitExceptions;
  204. initunicodestringmanager;
  205. SetupCmdLine;
  206. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  207. { Setup stdin, stdout and stderr }
  208. SysInitStdIO;
  209. { Reset IO Error }
  210. InOutRes:=0;
  211. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  212. { Arguments }
  213. {$ifdef FPC_HAS_FEATURE_THREADING}
  214. { threading }
  215. InitSystemThreads;
  216. {$endif FPC_HAS_FEATURE_THREADING}
  217. end.