system.pp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2020,2021 by the Free Pascal development team.
  4. System unit for The WebAssembly System Interface (WASI).
  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. {$I systemh.inc}
  15. const
  16. LineEnding = #10;
  17. LFNSupport = true;
  18. DirectorySeparator = '/';
  19. DriveSeparator = '';
  20. ExtensionSeparator = '.';
  21. PathSeparator = ':';
  22. AllowDirectorySeparators : set of char = ['\','/'];
  23. AllowDriveSeparators : set of char = [];
  24. { FileNameCaseSensitive and FileNameCasePreserving are defined below! }
  25. maxExitCode = 65535;
  26. MaxPathLen = 4096;
  27. AllFilesMask = '*';
  28. const
  29. UnusedHandle = -1;
  30. StdInputHandle = 0;
  31. StdOutputHandle = 1;
  32. StdErrorHandle = 2;
  33. FileNameCaseSensitive : boolean = true;
  34. FileNameCasePreserving: boolean = true;
  35. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  36. sLineBreak = LineEnding;
  37. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsLF;
  38. var
  39. argc: longint;
  40. argv: PPChar;
  41. procedure DebugWrite(const P: PChar);
  42. procedure DebugWriteLn(const P: PChar);
  43. procedure DebugWriteChar(Ch: Char);
  44. procedure DebugWriteHexDigit(d: Byte);
  45. procedure DebugWriteHexByte(b: Byte);
  46. procedure DebugWriteHexWord(w: Word);
  47. procedure DebugWriteHexLongWord(lw: LongWord);
  48. implementation
  49. {$I wasitypes.inc}
  50. {$I wasiprocs.inc}
  51. {$I system.inc}
  52. var
  53. argv_size,
  54. argv_buf_size: __wasi_size_t;
  55. argv_buf: Pointer;
  56. function GetProcessID: SizeUInt;
  57. begin
  58. end;
  59. Procedure Randomize;
  60. Begin
  61. End;
  62. procedure System_exit;
  63. begin
  64. __wasi_proc_exit(ExitCode);
  65. End;
  66. procedure Setup_PreopenedDirs;
  67. var
  68. fd: __wasi_fd_t;
  69. prestat: __wasi_prestat_t;
  70. res: __wasi_errno_t;
  71. prestat_dir_name: PChar;
  72. begin
  73. fd:=3;
  74. repeat
  75. res:=__wasi_fd_prestat_get(fd, @prestat);
  76. if res=__WASI_ERRNO_SUCCESS then
  77. begin
  78. if (prestat.tag=__WASI_PREOPENTYPE_DIR) and (prestat.u.dir.pr_name_len>0) then
  79. begin
  80. //GetMem(prestat_dir_name,prestat.u.dir.pr_name_len+1);
  81. //if __wasi_fd_prestat_dir_name(fd,PByte(prestat_dir_name),prestat.u.dir.pr_name_len)=__WASI_ERRNO_SUCCESS then
  82. //begin
  83. // prestat_dir_name[prestat.u.dir.pr_name_len]:=#0;
  84. // //Writeln(prestat_dir_name);
  85. //end
  86. //else
  87. // FreeMem(prestat_dir_name,prestat.u.dir.pr_name_len+1);
  88. end;
  89. end;
  90. Inc(fd);
  91. until res<>__WASI_ERRNO_SUCCESS;
  92. end;
  93. procedure setup_arguments;
  94. begin
  95. if argv<>nil then
  96. exit;
  97. if __wasi_args_sizes_get(@argc, @argv_buf_size)<>__WASI_ERRNO_SUCCESS then
  98. begin
  99. argc:=0;
  100. argv:=nil;
  101. exit;
  102. end;
  103. argv_size:=(argc+1)*SizeOf(PChar);
  104. GetMem(argv, argv_size);
  105. GetMem(argv_buf, argv_buf_size);
  106. if __wasi_args_get(Pointer(argv), argv_buf)<>__WASI_ERRNO_SUCCESS then
  107. begin
  108. FreeMem(argv, argv_size);
  109. FreeMem(argv_buf, argv_buf_size);
  110. argc:=0;
  111. argv:=nil;
  112. end;
  113. end;
  114. Function ParamCount: Longint;
  115. Begin
  116. if argv=nil then
  117. setup_arguments;
  118. paramcount := argc - 1;
  119. End;
  120. function paramstr(l: longint) : string;
  121. begin
  122. if argv=nil then
  123. setup_arguments;
  124. if (l>=0) and (l+1<=argc) then
  125. paramstr:=strpas(argv[l])
  126. else
  127. paramstr:='';
  128. end;
  129. procedure SysInitStdIO;
  130. begin
  131. OpenStdIO(Input,fmInput,StdInputHandle);
  132. OpenStdIO(Output,fmOutput,StdOutputHandle);
  133. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  134. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  135. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  136. end;
  137. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  138. begin
  139. end;
  140. procedure DebugWrite(const P: PChar);
  141. var
  142. our_iov: __wasi_ciovec_t;
  143. our_nwritten: longint;
  144. begin
  145. our_iov.buf := PByte(P);
  146. our_iov.buf_len := StrLen(P);
  147. __wasi_fd_write(1, @our_iov, 1, @our_nwritten);
  148. end;
  149. procedure DebugWriteLn(const P: PChar);
  150. begin
  151. DebugWrite(P);
  152. DebugWriteChar(#10);
  153. end;
  154. procedure DebugWriteChar(Ch: Char);
  155. var
  156. CharArr: array [0..1] of Char;
  157. begin
  158. CharArr[0] := Ch;
  159. CharArr[1] := #0;
  160. DebugWrite(@CharArr);
  161. end;
  162. procedure DebugWriteHexDigit(d: Byte);
  163. const
  164. HexDigits: array [0..15] of Char = '0123456789ABCDEF';
  165. begin
  166. DebugWriteChar(HexDigits[d]);
  167. end;
  168. procedure DebugWriteHexByte(b: Byte);
  169. begin
  170. DebugWriteHexDigit(b shr 4);
  171. DebugWriteHexDigit(b and 15);
  172. end;
  173. procedure DebugWriteHexWord(w: Word);
  174. begin
  175. DebugWriteHexByte(w shr 8);
  176. DebugWriteHexByte(Byte(w));
  177. end;
  178. procedure DebugWriteHexLongWord(lw: LongWord);
  179. begin
  180. DebugWriteHexWord(lw shr 16);
  181. DebugWriteHexWord(Word(lw));
  182. end;
  183. begin
  184. { To be set if this is a GUI or console application }
  185. IsConsole := TRUE;
  186. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  187. { If dynlibs feature is disabled,
  188. IsLibrary is a constant, which can thus not be set to a value }
  189. { To be set if this is a library and not a program }
  190. IsLibrary := FALSE;
  191. {$endif def FPC_HAS_FEATURE_DYNLIBS}
  192. { Setup heap }
  193. InitHeap;
  194. SysInitExceptions;
  195. initunicodestringmanager;
  196. { Setup stdin, stdout and stderr }
  197. SysInitStdIO;
  198. { Reset IO Error }
  199. InOutRes:=0;
  200. {$ifdef FPC_HAS_FEATURE_THREADING}
  201. InitSystemThreads;
  202. {$endif}
  203. Setup_PreopenedDirs
  204. end.