system.pp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. CtrlZMarksEOF: boolean = true; (* #26 not considered as end of file *)
  41. sLineBreak = LineEnding;
  42. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  43. var
  44. argc: LongInt = 0;
  45. argv: PPChar;
  46. envp: PPChar;
  47. // errno: integer;
  48. function get_cmdline:Pchar;
  49. property cmdline:Pchar read get_cmdline;
  50. implementation
  51. const
  52. calculated_cmdline: Pchar = nil;
  53. { System limits, POSIX value in parentheses, used for buffer and stack allocation }
  54. ARG_MAX = 65536; {4096} { Maximum number of argument size }
  55. PATH_MAX = 1024; {255} { Maximum number of bytes in pathname }
  56. {$i system.inc}
  57. {$i libc.inc}
  58. {$i wii.inc}
  59. {$ifdef FPC_HAS_FEATURE_PROCESSES}
  60. function GetProcessID: SizeUInt;
  61. begin
  62. GetProcessID := 0;
  63. end;
  64. {$endif}
  65. {*****************************************************************************
  66. Misc. System Dependent Functions
  67. *****************************************************************************}
  68. procedure System_exit;
  69. begin
  70. // Boo!
  71. end;
  72. {*****************************************************************************
  73. ParamStr/Randomize
  74. *****************************************************************************}
  75. const
  76. QRAN_SHIFT = 15;
  77. QRAN_MASK = ((1 shl QRAN_SHIFT) - 1);
  78. QRAN_MAX = QRAN_MASK;
  79. QRAN_A = 1664525;
  80. QRAN_C = 1013904223;
  81. { set randseed to a new pseudo random value }
  82. procedure randomize;
  83. begin
  84. end;
  85. function random(): integer;
  86. begin
  87. RandSeed := QRAN_A * RandSeed + QRAN_C;
  88. random := (RandSeed shr 16) and QRAN_MAX;
  89. end;
  90. function random(value: integer): integer;
  91. var
  92. a: integer;
  93. begin
  94. RandSeed := QRAN_A * RandSeed + QRAN_C;
  95. a := (RandSeed shr 16) and QRAN_MAX;
  96. random := (a * value) shr 15;
  97. end;
  98. Function ParamCount: Longint;
  99. Begin
  100. Paramcount:=argc-1
  101. End;
  102. { variable where full path and filename and executable is stored }
  103. { is setup by the startup of the system unit. }
  104. var
  105. execpathstr : shortstring;
  106. function paramstr(l: longint) : string;
  107. begin
  108. { stricly conforming POSIX applications }
  109. { have the executing filename as argv[0] }
  110. if l=0 then
  111. begin
  112. paramstr := execpathstr;
  113. end
  114. else
  115. paramstr:=strpas(argv[l]);
  116. end;
  117. {*****************************************************************************
  118. cmdline
  119. *****************************************************************************}
  120. procedure SetupCmdLine;
  121. var
  122. bufsize,
  123. len,j,
  124. size,i : longint;
  125. found : boolean;
  126. buf : pchar;
  127. procedure AddBuf;
  128. begin
  129. reallocmem(calculated_cmdline,size+bufsize);
  130. move(buf^,calculated_cmdline[size],bufsize);
  131. inc(size,bufsize);
  132. bufsize:=0;
  133. end;
  134. begin
  135. if argc<=0 then
  136. exit;
  137. GetMem(buf,ARG_MAX);
  138. size:=0;
  139. bufsize:=0;
  140. i:=0;
  141. while (i<argc) do
  142. begin
  143. len:=strlen(argv[i]);
  144. if len>ARG_MAX-2 then
  145. len:=ARG_MAX-2;
  146. found:=false;
  147. for j:=1 to len do
  148. if argv[i][j]=' ' then
  149. begin
  150. found:=true;
  151. break;
  152. end;
  153. if bufsize+len>=ARG_MAX-2 then
  154. AddBuf;
  155. if found then
  156. begin
  157. buf[bufsize]:='"';
  158. inc(bufsize);
  159. end;
  160. move(argv[i]^,buf[bufsize],len);
  161. inc(bufsize,len);
  162. if found then
  163. begin
  164. buf[bufsize]:='"';
  165. inc(bufsize);
  166. end;
  167. if i<argc then
  168. buf[bufsize]:=' '
  169. else
  170. buf[bufsize]:=#0;
  171. inc(bufsize);
  172. inc(i);
  173. end;
  174. AddBuf;
  175. FreeMem(buf,ARG_MAX);
  176. end;
  177. function get_cmdline:Pchar;
  178. begin
  179. if calculated_cmdline=nil then
  180. setupcmdline;
  181. get_cmdline:=calculated_cmdline;
  182. end;
  183. procedure SysInitStdIO;
  184. begin
  185. OpenStdIO(Input,fmInput,0);
  186. OpenStdIO(Output,fmOutput,0);
  187. OpenStdIO(ErrOutput,fmOutput,0);
  188. OpenStdIO(StdOut,fmOutput,0);
  189. OpenStdIO(StdErr,fmOutput,0);
  190. end;
  191. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  192. begin
  193. result := stklen;
  194. end;
  195. begin
  196. StackLength := CheckInitialStkLen(InitialStkLen);
  197. StackBottom := StackTop - StackLength;
  198. { OS specific startup }
  199. { Set up signals handlers }
  200. // fpc_cpucodeinit;
  201. { Setup heap }
  202. InitHeap;
  203. SysInitExceptions;
  204. SetupCmdLine;
  205. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  206. { Setup stdin, stdout and stderr }
  207. SysInitStdIO;
  208. { Reset IO Error }
  209. InOutRes:=0;
  210. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  211. { Arguments }
  212. {$ifdef FPC_HAS_FEATURE_THREADING}
  213. { threading }
  214. InitSystemThreads;
  215. {$endif FPC_HAS_FEATURE_THREADING}
  216. initvariantmanager;
  217. end.