system.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. type
  50. P__wasi_size_t = ^__wasi_size_t;
  51. __wasi_size_t = longint;
  52. P__wasi_fd_t = ^__wasi_fd_t;
  53. __wasi_fd_t = longint;
  54. size_t = longint;
  55. __wasi_errno_t = longint;
  56. const
  57. __WASI_ERRNO_SUCCESS = 0;
  58. __WASI_ERRNO_2BIG = 1;
  59. __WASI_ERRNO_ACCES = 2;
  60. __WASI_ERRNO_ADDRINUSE = 3;
  61. __WASI_ERRNO_ADDRNOTAVAIL = 4;
  62. __WASI_ERRNO_AFNOSUPPORT = 5;
  63. __WASI_ERRNO_AGAIN = 6;
  64. __WASI_ERRNO_ALREADY = 7;
  65. __WASI_ERRNO_BADF = 8;
  66. __WASI_ERRNO_BADMSG = 9;
  67. __WASI_ERRNO_BUSY = 10;
  68. __WASI_ERRNO_CANCELED = 11;
  69. __WASI_ERRNO_CHILD = 12;
  70. __WASI_ERRNO_CONNABORTED = 13;
  71. __WASI_ERRNO_CONNREFUSED = 14;
  72. __WASI_ERRNO_CONNRESET = 15;
  73. __WASI_ERRNO_DEADLK = 16;
  74. __WASI_ERRNO_DESTADDRREQ = 17;
  75. __WASI_ERRNO_DOM = 18;
  76. __WASI_ERRNO_DQUOT = 19;
  77. __WASI_ERRNO_EXIST = 20;
  78. __WASI_ERRNO_FAULT = 21;
  79. __WASI_ERRNO_FBIG = 22;
  80. __WASI_ERRNO_HOSTUNREACH = 23;
  81. __WASI_ERRNO_IDRM = 24;
  82. __WASI_ERRNO_ILSEQ = 25;
  83. __WASI_ERRNO_INPROGRESS = 26;
  84. __WASI_ERRNO_INTR = 27;
  85. __WASI_ERRNO_INVAL = 28;
  86. __WASI_ERRNO_IO = 29;
  87. __WASI_ERRNO_ISCONN = 30;
  88. __WASI_ERRNO_ISDIR = 31;
  89. __WASI_ERRNO_LOOP = 32;
  90. __WASI_ERRNO_MFILE = 33;
  91. __WASI_ERRNO_MLINK = 34;
  92. __WASI_ERRNO_MSGSIZE = 35;
  93. __WASI_ERRNO_MULTIHOP = 36;
  94. __WASI_ERRNO_NAMETOOLONG = 37;
  95. __WASI_ERRNO_NETDOWN = 38;
  96. __WASI_ERRNO_NETRESET = 39;
  97. __WASI_ERRNO_NETUNREACH = 40;
  98. __WASI_ERRNO_NFILE = 41;
  99. __WASI_ERRNO_NOBUFS = 42;
  100. __WASI_ERRNO_NODEV = 43;
  101. __WASI_ERRNO_NOENT = 44;
  102. __WASI_ERRNO_NOEXEC = 45;
  103. __WASI_ERRNO_NOLCK = 46;
  104. __WASI_ERRNO_NOLINK = 47;
  105. __WASI_ERRNO_NOMEM = 48;
  106. __WASI_ERRNO_NOMSG = 49;
  107. __WASI_ERRNO_NOPROTOOPT = 50;
  108. __WASI_ERRNO_NOSPC = 51;
  109. __WASI_ERRNO_NOSYS = 52;
  110. __WASI_ERRNO_NOTCONN = 53;
  111. __WASI_ERRNO_NOTDIR = 54;
  112. __WASI_ERRNO_NOTEMPTY = 55;
  113. __WASI_ERRNO_NOTRECOVERABLE = 56;
  114. __WASI_ERRNO_NOTSOCK = 57;
  115. __WASI_ERRNO_NOTSUP = 58;
  116. __WASI_ERRNO_NOTTY = 59;
  117. __WASI_ERRNO_NXIO = 60;
  118. __WASI_ERRNO_OVERFLOW = 61;
  119. __WASI_ERRNO_OWNERDEAD = 62;
  120. __WASI_ERRNO_PERM = 63;
  121. __WASI_ERRNO_PIPE = 64;
  122. __WASI_ERRNO_PROTO = 65;
  123. __WASI_ERRNO_PROTONOSUPPORT = 66;
  124. __WASI_ERRNO_PROTOTYPE = 67;
  125. __WASI_ERRNO_RANGE = 68;
  126. __WASI_ERRNO_ROFS = 69;
  127. __WASI_ERRNO_SPIPE = 70;
  128. __WASI_ERRNO_SRCH = 71;
  129. __WASI_ERRNO_STALE = 72;
  130. __WASI_ERRNO_TIMEDOUT = 73;
  131. __WASI_ERRNO_TXTBSY = 74;
  132. __WASI_ERRNO_XDEV = 75;
  133. __WASI_ERRNO_NOTCAPABLE = 76;
  134. type
  135. P__wasi_iovec_t = ^__wasi_iovec_t;
  136. __wasi_iovec_t = record
  137. buf: PUInt8;
  138. buf_len: __wasi_size_t;
  139. end;
  140. P__wasi_ciovec_t = ^__wasi_ciovec_t;
  141. __wasi_ciovec_t = record
  142. buf: pointer;
  143. buf_len: __wasi_size_t;
  144. end;
  145. __wasi_exitcode_t = UInt32;
  146. __wasi_filetype_t = UInt8;
  147. const
  148. __WASI_FILETYPE_UNKNOWN = 0;
  149. __WASI_FILETYPE_BLOCK_DEVICE = 1;
  150. __WASI_FILETYPE_CHARACTER_DEVICE = 2;
  151. __WASI_FILETYPE_DIRECTORY = 3;
  152. __WASI_FILETYPE_REGULAR_FILE = 4;
  153. __WASI_FILETYPE_SOCKET_DGRAM = 5;
  154. __WASI_FILETYPE_SOCKET_STREAM = 6;
  155. __WASI_FILETYPE_SYMBOLIC_LINK = 7;
  156. type
  157. __wasi_fdflags_t = UInt16;
  158. const
  159. __WASI_FDFLAGS_APPEND = 1;
  160. __WASI_FDFLAGS_DSYNC = 2;
  161. __WASI_FDFLAGS_NONBLOCK = 4;
  162. __WASI_FDFLAGS_RSYNC = 8;
  163. __WASI_FDFLAGS_SYNC = 16;
  164. type
  165. __wasi_rights_t = UInt64;
  166. const
  167. __WASI_RIGHTS_FD_DATASYNC = 1;
  168. __WASI_RIGHTS_FD_READ = 2;
  169. __WASI_RIGHTS_FD_SEEK = 4;
  170. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS = 8;
  171. __WASI_RIGHTS_FD_SYNC = 16;
  172. __WASI_RIGHTS_FD_TELL = 32;
  173. __WASI_RIGHTS_FD_WRITE = 64;
  174. __WASI_RIGHTS_FD_ADVISE = 128;
  175. __WASI_RIGHTS_FD_ALLOCATE = 256;
  176. __WASI_RIGHTS_PATH_CREATE_DIRECTORY = 512;
  177. __WASI_RIGHTS_PATH_CREATE_FILE = 1024;
  178. __WASI_RIGHTS_PATH_LINK_SOURCE = 2048;
  179. __WASI_RIGHTS_PATH_LINK_TARGET = 4096;
  180. __WASI_RIGHTS_PATH_OPEN = 8192;
  181. __WASI_RIGHTS_FD_READDIR = 16384;
  182. __WASI_RIGHTS_PATH_READLINK = 32768;
  183. __WASI_RIGHTS_PATH_RENAME_SOURCE = 65536;
  184. __WASI_RIGHTS_PATH_RENAME_TARGET = 131072;
  185. __WASI_RIGHTS_PATH_FILESTAT_GET = 262144;
  186. __WASI_RIGHTS_PATH_FILESTAT_SET_SIZE = 524288;
  187. __WASI_RIGHTS_PATH_FILESTAT_SET_TIMES = 1048576;
  188. __WASI_RIGHTS_FD_FILESTAT_GET = 2097152;
  189. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE = 4194304;
  190. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES = 8388608;
  191. __WASI_RIGHTS_PATH_SYMLINK = 16777216;
  192. __WASI_RIGHTS_PATH_REMOVE_DIRECTORY = 33554432;
  193. __WASI_RIGHTS_PATH_UNLINK_FILE = 67108864;
  194. __WASI_RIGHTS_POLL_FD_READWRITE = 134217728;
  195. __WASI_RIGHTS_SOCK_SHUTDOWN = 268435456;
  196. type
  197. P__wasi_fdstat_t = ^__wasi_fdstat_t;
  198. __wasi_fdstat_t = record
  199. fs_filetype: __wasi_filetype_t;
  200. fs_flags: __wasi_fdflags_t;
  201. fs_rights_base: __wasi_rights_t;
  202. fs_rights_inheriting: __wasi_rights_t;
  203. end;
  204. __wasi_lookupflags_t = UInt32;
  205. const
  206. __WASI_LOOKUPFLAGS_SYMLINK_FOLLOW = 1;
  207. type
  208. __wasi_oflags_t = UInt16;
  209. const
  210. __WASI_OFLAGS_CREAT = 1;
  211. __WASI_OFLAGS_DIRECTORY = 2;
  212. __WASI_OFLAGS_EXCL = 4;
  213. __WASI_OFLAGS_TRUNC = 8;
  214. function fd_write(fd: __wasi_fd_t;
  215. iovs: P__wasi_ciovec_t;
  216. iovs_len: size_t;
  217. nwritten: P__wasi_size_t): __wasi_errno_t; external 'wasi_snapshot_preview1';
  218. function fd_read(fd: __wasi_fd_t;
  219. iovs: P__wasi_iovec_t;
  220. iovs_len: size_t;
  221. nread: P__wasi_size_t): __wasi_errno_t; external 'wasi_snapshot_preview1';
  222. procedure proc_exit(rval: __wasi_exitcode_t); noreturn; external 'wasi_snapshot_preview1';
  223. function fd_fdstat_get(fd: __wasi_fd_t;
  224. stat: P__wasi_fdstat_t): __wasi_errno_t; external 'wasi_snapshot_preview1';
  225. function path_open(fd: __wasi_fd_t;
  226. dirflags: __wasi_lookupflags_t;
  227. path: PChar;
  228. path_len: size_t;
  229. oflags: __wasi_oflags_t;
  230. fs_rights_base,
  231. fs_rights_inherting: __wasi_rights_t;
  232. fdflags: __wasi_fdflags_t;
  233. opened_fd: P__wasi_fd_t): __wasi_errno_t; external 'wasi_snapshot_preview1';
  234. function fd_close(fd: __wasi_fd_t): __wasi_errno_t; external 'wasi_snapshot_preview1';
  235. {$I system.inc}
  236. function GetProcessID: SizeUInt;
  237. begin
  238. end;
  239. Procedure Randomize;
  240. Begin
  241. End;
  242. procedure System_exit;
  243. begin
  244. proc_exit(ExitCode);
  245. End;
  246. Function ParamCount: Longint;
  247. Begin
  248. End;
  249. function paramstr(l: longint) : string;
  250. begin
  251. end;
  252. procedure SysInitStdIO;
  253. begin
  254. OpenStdIO(Input,fmInput,StdInputHandle);
  255. OpenStdIO(Output,fmOutput,StdOutputHandle);
  256. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  257. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  258. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  259. end;
  260. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  261. begin
  262. end;
  263. procedure DebugWrite(const P: PChar);
  264. var
  265. our_iov: __wasi_ciovec_t;
  266. our_nwritten: longint;
  267. begin
  268. our_iov.buf := P;
  269. our_iov.buf_len := StrLen(P);
  270. fd_write(1, @our_iov, 1, @our_nwritten);
  271. end;
  272. procedure DebugWriteLn(const P: PChar);
  273. begin
  274. DebugWrite(P);
  275. DebugWriteChar(#10);
  276. end;
  277. procedure DebugWriteChar(Ch: Char);
  278. var
  279. CharArr: array [0..1] of Char;
  280. begin
  281. CharArr[0] := Ch;
  282. CharArr[1] := #0;
  283. DebugWrite(@CharArr);
  284. end;
  285. procedure DebugWriteHexDigit(d: Byte);
  286. const
  287. HexDigits: array [0..15] of Char = '0123456789ABCDEF';
  288. begin
  289. DebugWriteChar(HexDigits[d]);
  290. end;
  291. procedure DebugWriteHexByte(b: Byte);
  292. begin
  293. DebugWriteHexDigit(b shr 4);
  294. DebugWriteHexDigit(b and 15);
  295. end;
  296. procedure DebugWriteHexWord(w: Word);
  297. begin
  298. DebugWriteHexByte(w shr 8);
  299. DebugWriteHexByte(Byte(w));
  300. end;
  301. procedure DebugWriteHexLongWord(lw: LongWord);
  302. begin
  303. DebugWriteHexWord(lw shr 16);
  304. DebugWriteHexWord(Word(lw));
  305. end;
  306. begin
  307. { To be set if this is a GUI or console application }
  308. IsConsole := TRUE;
  309. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  310. { If dynlibs feature is disabled,
  311. IsLibrary is a constant, which can thus not be set to a value }
  312. { To be set if this is a library and not a program }
  313. IsLibrary := FALSE;
  314. {$endif def FPC_HAS_FEATURE_DYNLIBS}
  315. { Setup heap }
  316. InitHeap;
  317. SysInitExceptions;
  318. initunicodestringmanager;
  319. { Setup stdin, stdout and stderr }
  320. SysInitStdIO;
  321. { Reset IO Error }
  322. InOutRes:=0;
  323. {$ifdef FPC_HAS_FEATURE_THREADING}
  324. InitSystemThreads;
  325. {$endif}
  326. end.