123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- {
- This file is part of the Free Pascal run time library.
- Copyright (c) 1999-2021 by the Free Pascal development team.
- Sysutils unit for The WebAssembly System Interface (WASI).
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- {$inline on}
- unit sysutils;
- interface
- {$MODE objfpc}
- {$MODESWITCH out}
- { force ansistrings }
- {$H+}
- {$modeswitch typehelpers}
- {$modeswitch advancedrecords}
- uses
- wasiapi;
- {$DEFINE HAS_SLEEP}
- {$DEFINE HAS_GETTICKCOUNT64}
- { used OS file system APIs use ansistring }
- {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
- { OS has an ansistring/single byte environment variable API }
- {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
- { Include platform independent interface part }
- {$i sysutilh.inc}
- implementation
- uses
- sysconst;
- {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
- {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
- {$DEFINE HAS_LOCALTIMEZONEOFFSET}
- {$DEFINE executeprocuni} (* Only 1 byte version of ExecuteProcess is provided by the OS *)
- { Include platform independent implementation part }
- {$i sysutils.inc}
- function GetTickCount64: QWord;
- var
- NanoSecsPast: __wasi_timestamp_t;
- begin
- if __wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC,1000000,@NanoSecsPast)=__WASI_ERRNO_SUCCESS then
- Result:=NanoSecsPast div 1000000
- else
- Result:=0;
- end;
- {****************************************************************************
- File Functions
- ****************************************************************************}
- Function FileOpen (Const FileName : RawByteString; Mode : Integer) : THandle;
- Var
- SystemFileName: RawByteString;
- fs_rights_base: __wasi_rights_t = 0;
- ourfd: __wasi_fd_t;
- res: __wasi_errno_t;
- pr: RawByteString;
- fd: __wasi_fd_t;
- Begin
- SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
- case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of
- fmOpenRead:
- fs_rights_base :=__WASI_RIGHTS_FD_READ or
- __WASI_RIGHTS_FD_FILESTAT_GET or
- __WASI_RIGHTS_FD_SEEK or
- __WASI_RIGHTS_FD_TELL or
- __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
- __WASI_RIGHTS_FD_ADVISE or
- __WASI_RIGHTS_POLL_FD_READWRITE;
- fmOpenWrite:
- fs_rights_base :=__WASI_RIGHTS_FD_WRITE or
- __WASI_RIGHTS_FD_FILESTAT_GET or
- __WASI_RIGHTS_FD_SEEK or
- __WASI_RIGHTS_FD_TELL or
- __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
- __WASI_RIGHTS_FD_ADVISE or
- __WASI_RIGHTS_POLL_FD_READWRITE or
- __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
- __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
- __WASI_RIGHTS_FD_ALLOCATE or
- __WASI_RIGHTS_FD_DATASYNC or
- __WASI_RIGHTS_FD_SYNC;
- fmOpenReadWrite:
- fs_rights_base :=__WASI_RIGHTS_FD_READ or
- __WASI_RIGHTS_FD_WRITE or
- __WASI_RIGHTS_FD_FILESTAT_GET or
- __WASI_RIGHTS_FD_SEEK or
- __WASI_RIGHTS_FD_TELL or
- __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
- __WASI_RIGHTS_FD_ADVISE or
- __WASI_RIGHTS_POLL_FD_READWRITE or
- __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
- __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
- __WASI_RIGHTS_FD_ALLOCATE or
- __WASI_RIGHTS_FD_DATASYNC or
- __WASI_RIGHTS_FD_SYNC;
- end;
- if not ConvertToFdRelativePath(SystemFileName,fd,pr) then
- begin
- result:=-1;
- exit;
- end;
- repeat
- res:=__wasi_path_open(fd,
- 0,
- PChar(pr),
- length(pr),
- 0,
- fs_rights_base,
- fs_rights_base,
- 0,
- @ourfd);
- until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
- If res=__WASI_ERRNO_SUCCESS Then
- Result:=ourfd
- else
- Result:=-1;
- end;
- Function FileCreate (Const FileName : RawByteString) : THandle;
- Const
- fs_rights_base: __wasi_rights_t =
- __WASI_RIGHTS_FD_READ or
- __WASI_RIGHTS_FD_WRITE or
- __WASI_RIGHTS_FD_FILESTAT_GET or
- __WASI_RIGHTS_FD_SEEK or
- __WASI_RIGHTS_FD_TELL or
- __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
- __WASI_RIGHTS_FD_ADVISE or
- __WASI_RIGHTS_POLL_FD_READWRITE or
- __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
- __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
- __WASI_RIGHTS_FD_ALLOCATE or
- __WASI_RIGHTS_FD_DATASYNC or
- __WASI_RIGHTS_FD_SYNC;
- Var
- SystemFileName: RawByteString;
- ourfd: __wasi_fd_t;
- res: __wasi_errno_t;
- pr: RawByteString;
- fd: __wasi_fd_t;
- Begin
- SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
- if not ConvertToFdRelativePath(SystemFileName,fd,pr) then
- begin
- result:=-1;
- exit;
- end;
- repeat
- res:=__wasi_path_open(fd,
- 0,
- PChar(pr),
- length(pr),
- __WASI_OFLAGS_CREAT or __WASI_OFLAGS_TRUNC,
- fs_rights_base,
- fs_rights_base,
- 0,
- @ourfd);
- until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
- If res=__WASI_ERRNO_SUCCESS Then
- Result:=ourfd
- else
- Result:=-1;
- end;
- Function FileCreate (Const FileName : RawByteString; ShareMode:integer; Rights : integer) : THandle;
- begin
- FileCreate:=FileCreate(FileName);
- end;
- Function FileCreate (Const FileName : RawByteString; Rights:integer) : THandle;
- begin
- FileCreate:=FileCreate(FileName);
- end;
- Function FileRead (Handle : THandle; Out Buffer; Count : longint) : Longint;
- var
- our_iov: __wasi_iovec_t;
- our_nread: __wasi_size_t;
- res: __wasi_errno_t;
- begin
- repeat
- our_iov.buf:=@Buffer;
- our_iov.buf_len:=Count;
- res:=__wasi_fd_read(Handle,@our_iov,1,@our_nread);
- until (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
- if res=__WASI_ERRNO_SUCCESS then
- Result:=our_nread
- else
- Result:=-1;
- end;
- Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
- var
- our_iov: __wasi_ciovec_t;
- our_nwritten: longint;
- res: __wasi_errno_t;
- begin
- repeat
- our_iov.buf:=@Buffer;
- our_iov.buf_len:=Count;
- res:=__wasi_fd_write(Handle,@our_iov,1,@our_nwritten);
- until (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
- if res=__WASI_ERRNO_SUCCESS then
- Result:=our_nwritten
- else
- Result:=-1;
- end;
- Function FileSeek (Handle : THandle; FOffset, Origin : Longint) : Longint;
- begin
- result:=longint(FileSeek(Handle,int64(FOffset),Origin));
- end;
- Function FileSeek (Handle : THandle; FOffset: Int64; Origin: Longint) : Int64;
- var
- res: __wasi_errno_t;
- newoffset: __wasi_filesize_t;
- whence: __wasi_whence_t;
- begin
- case Origin of
- fsFromBeginning:
- whence:=__WASI_WHENCE_SET;
- fsFromCurrent:
- whence:=__WASI_WHENCE_CUR;
- fsFromEnd:
- whence:=__WASI_WHENCE_END;
- else
- begin
- Result:=-1;
- exit;
- end;
- end;
- res:=__wasi_fd_seek(Handle,FOffset,whence,@newoffset);
- if res=__WASI_ERRNO_SUCCESS then
- Result:=newoffset
- else
- Result:=-1;
- end;
- Procedure FileClose (Handle : THandle);
- var
- res: __wasi_errno_t;
- begin
- repeat
- res:=__wasi_fd_close(Handle);
- until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
- end;
- Function FileTruncate (Handle: THandle; Size: Int64) : boolean;
- var
- res: __wasi_errno_t;
- begin
- Result:=__wasi_fd_filestat_set_size(handle,Size)=__WASI_ERRNO_SUCCESS;
- end;
- Function FileAge (Const FileName : RawByteString): Int64;
- begin
- end;
- function FileGetSymLinkTarget(const FileName: RawByteString; out SymLinkRec: TRawbyteSymLinkRec): Boolean;
- begin
- Result := False;
- end;
- function FileExists (const FileName: RawByteString; FollowLink : Boolean): boolean;
- begin
- end;
- Function DirectoryExists (Const Directory : RawByteString; FollowLink : Boolean) : Boolean;
- begin
- end;
- Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
- begin
- { not yet implemented }
- Result := -1;
- end;
- Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
- begin
- { not yet implemented }
- Result := -1;
- end;
- Procedure InternalFindClose(var Handle: THandle);
- begin
- end;
- Function FileGetDate (Handle : THandle) : Int64;
- begin
- end;
- Function FileSetDate (Handle : THandle; Age : Int64) : Longint;
- begin
- end;
- Function FileGetAttr (Const FileName : RawByteString) : Longint;
- begin
- end;
- Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
- begin
- end;
- Function DeleteFile (Const FileName : RawByteString) : Boolean;
- begin
- end;
- Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
- var
- fd1,fd2: __wasi_fd_t;
- pr1,pr2: RawByteString;
- res: __wasi_errno_t;
- begin
- result:=false;
- if not ConvertToFdRelativePath(OldName,fd1,pr1) then
- exit;
- if not ConvertToFdRelativePath(NewName,fd2,pr2) then
- exit;
- result:=__wasi_path_rename(fd1,PChar(pr1),Length(pr1),fd2,PChar(pr2),Length(pr2))=__WASI_ERRNO_SUCCESS;
- end;
- {****************************************************************************
- Disk Functions
- ****************************************************************************}
- function diskfree(drive : byte) : int64;
- begin
- end;
- function disksize(drive : byte) : int64;
- begin
- end;
- {****************************************************************************
- Time Functions
- ****************************************************************************}
- {$I tzenv.inc}
- Procedure GetLocalTime(var SystemTime: TSystemTime);
- begin
- end ;
- {****************************************************************************
- Misc Functions
- ****************************************************************************}
- procedure sysBeep;
- begin
- end;
- {****************************************************************************
- Locale Functions
- ****************************************************************************}
- procedure InitAnsi;
- begin
- end;
- Procedure InitInternational;
- begin
- InitInternationalGeneric;
- InitAnsi;
- end;
- function SysErrorMessage(ErrorCode: Integer): String;
- begin
- Result:=Format(SUnknownErrorCode,[ErrorCode]);
- end;
- {****************************************************************************
- Os utils
- ****************************************************************************}
- Function GetEnvironmentVariable(Const EnvVar : String) : String;
- var
- hp : ppchar;
- hs : string;
- eqpos : longint;
- begin
- result:='';
- hp:=envp;
- if hp<>nil then
- while assigned(hp^) do
- begin
- hs:=strpas(hp^);
- eqpos:=pos('=',hs);
- if copy(hs,1,eqpos-1)=envvar then
- begin
- result:=copy(hs,eqpos+1,length(hs)-eqpos);
- break;
- end;
- inc(hp);
- end;
- end;
- Function GetEnvironmentVariableCount : Integer;
- var
- p: ppchar;
- begin
- result:=0;
- p:=envp; {defined in system}
- if p<>nil then
- while p^<>nil do
- begin
- inc(result);
- inc(p);
- end;
- end;
- Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
- Var
- i : longint;
- p : ppchar;
- begin
- if (Index <= 0) or (envp=nil) then
- result:=''
- else
- begin
- p:=envp; {defined in system}
- i:=1;
- while (i<Index) and (p^<>nil) do
- begin
- inc(i);
- inc(p);
- end;
- if p^=nil then
- result:=''
- else
- result:=strpas(p^)
- end;
- end;
- function ExecuteProcess(Const Path: RawByteString; Const ComLine: RawByteString;Flags:TExecuteFlags=[]):integer;
- begin
- end;
- function ExecuteProcess (const Path: RawByteString;
- const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
- begin
- end;
- {*************************************************************************
- Sleep
- *************************************************************************}
- procedure Sleep (MilliSeconds: Cardinal);
- var
- subscription: __wasi_subscription_t;
- event: __wasi_event_t;
- nevents: __wasi_size_t;
- begin
- FillChar(subscription,SizeOf(subscription),0);
- subscription.u.tag:=__WASI_EVENTTYPE_CLOCK;
- subscription.u.u.clock.id:=__WASI_CLOCKID_MONOTONIC;
- subscription.u.u.clock.timeout:=MilliSeconds*1000000;
- subscription.u.u.clock.precision:=1000000;
- subscription.u.u.clock.flags:=0; { timeout value is relative }
- __wasi_poll_oneoff(@subscription,@event,1,@nevents);
- end;
- {****************************************************************************
- Initialization code
- ****************************************************************************}
- Initialization
- InitExceptions; { Initialize exceptions. OS independent }
- InitInternational; { Initialize internationalization settings }
- OnBeep:=@SysBeep;
- InitTZ;
- Finalization
- FreeTerminateProcs;
- DoneExceptions;
- end.
|