Skip to content

Commit

Permalink
[PO] Use system/winapi.h instead of windows.h
Browse files Browse the repository at this point in the history
Signed-off-by: Shen-Ta Hsieh <[email protected]>
  • Loading branch information
ibmibmibm authored and hydai committed Jun 14, 2023
1 parent d649efa commit 2d0bd76
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
14 changes: 14 additions & 0 deletions include/system/winapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ NT_SUCCESS_(NTSTATUS_ Status) noexcept {
}
static inline constexpr const ULONG_ FILE_SEQUENTIAL_ONLY_ = 0x00000004;
static inline constexpr const ULONG_ FILE_RANDOM_ACCESS_ = 0x00000800;

static inline constexpr const DWORD_ ENABLE_VIRTUAL_TERMINAL_PROCESSING_ =
0x0004;
#endif

} // namespace WasmEdge::winapi
Expand Down Expand Up @@ -778,6 +781,11 @@ CreateHardLinkW(WasmEdge::winapi::LPCWSTR_ lpFileName,
WasmEdge::winapi::LPCWSTR_ lpExistingFileName,
WasmEdge::winapi::LPSECURITY_ATTRIBUTES_ lpSecurityAttributes);

WASMEDGE_WINAPI_SYMBOL_IMPORT
WasmEdge::winapi::BOOL_ WASMEDGE_WINAPI_WINAPI_CC
GetConsoleMode(WasmEdge::winapi::HANDLE_ hConsoleHandle,
WasmEdge::winapi::LPDWORD_ lpMode);

WASMEDGE_WINAPI_SYMBOL_IMPORT WasmEdge::winapi::BOOL_ WASMEDGE_WINAPI_WINAPI_CC
GetFileInformationByHandle(
WasmEdge::winapi::HANDLE_ hFile,
Expand Down Expand Up @@ -820,6 +828,10 @@ WASMEDGE_WINAPI_SYMBOL_IMPORT
WasmEdge::winapi::DWORD_ WASMEDGE_WINAPI_WINAPI_CC QueryDosDeviceW(
WasmEdge::winapi::LPCWSTR_ lpDeviceName,
WasmEdge::winapi::LPWSTR_ lpTargetPath, WasmEdge::winapi::DWORD_ ucchMax);

WASMEDGE_WINAPI_SYMBOL_IMPORT
WasmEdge::winapi::BOOL_ WASMEDGE_WINAPI_WINAPI_CC SetConsoleMode(
WasmEdge::winapi::HANDLE_ hConsoleHandle, WasmEdge::winapi::DWORD_ dwMode);
#endif

#if NTDDI_VERSION >= NTDDI_VISTA
Expand Down Expand Up @@ -924,13 +936,15 @@ using ::WriteFileEx;
using ::CreateFileMappingW;
using ::CreateFileW;
using ::CreateHardLinkW;
using ::GetConsoleMode;
using ::GetFileInformationByHandle;
using ::GetLogicalDriveStringsW;
using ::MapViewOfFile;
using ::NtQueryInformationFile;
using ::NtQueryObject;
using ::NtSetInformationFile;
using ::QueryDosDeviceW;
using ::SetConsoleMode;
#endif

#if NTDDI_VERSION >= NTDDI_VISTA
Expand Down
21 changes: 9 additions & 12 deletions lib/po/argument_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
#include "po/argument_parser.h"
#include "common/defines.h"
#include "common/log.h"
#include "system/winapi.h"
#include <cstdio>

// For enabling Windows PowerShell color support.
#if WASMEDGE_OS_WINDOWS
#include <windows.h>
#endif

namespace WasmEdge {
namespace PO {

Expand Down Expand Up @@ -163,13 +159,14 @@ void ArgumentParser::SubCommandDescriptor::usage(

void ArgumentParser::SubCommandDescriptor::help(std::FILE *Out) const noexcept {
// For enabling Windows PowerShell color support.
#if WASMEDGE_OS_WINDOWS
HANDLE OutputHandler = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (OutputHandler != INVALID_HANDLE_VALUE) {
DWORD ConsoleMode = 0;
if (::GetConsoleMode(OutputHandler, &ConsoleMode)) {
ConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
::SetConsoleMode(OutputHandler, ConsoleMode);
#if WASMEDGE_OS_WINDOWS && WINAPI_PARTITION_DESKTOP
winapi::HANDLE_ OutputHandler =
winapi::GetStdHandle(winapi::STD_OUTPUT_HANDLE_);
if (OutputHandler != winapi::INVALID_HANDLE_VALUE_) {
winapi::DWORD_ ConsoleMode = 0;
if (winapi::GetConsoleMode(OutputHandler, &ConsoleMode)) {
ConsoleMode |= winapi::ENABLE_VIRTUAL_TERMINAL_PROCESSING_;
winapi::SetConsoleMode(OutputHandler, ConsoleMode);
}
}
#endif
Expand Down
36 changes: 20 additions & 16 deletions tools/wasmedge/wincli_helper.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2019-2022 Second State INC
//
#include "wasmedge/wincli_helper.h"

#if WASMEDGE_WINCLI_HELPER_ENABLE
#include <Windows.h>

#include <string.h>
#include "system/winapi.h"
#include <cwchar>

static const char *UTF16ToUTF8(const wchar_t *U16Str) {
static const char *EmptyString = "";

int U16StrLen = int(wcslen(U16Str));
auto BufSize = WideCharToMultiByte(CP_UTF8, 0, U16Str, U16StrLen, nullptr, 0,
nullptr, nullptr);
auto BufSize = winapi::WideCharToMultiByte(
winapi::CP_UTF8_, 0, U16Str, U16StrLen, nullptr, 0, nullptr, nullptr);
if (BufSize == 0) {
return EmptyString;
}
// for '\0'
BufSize = BufSize + 1;
++BufSize;

char *Buf = new char[size_t(BufSize)];
::memset(Buf, 0, size_t(BufSize));
char *Buf = new char[static_cast<size_t>(BufSize)];

WideCharToMultiByte(CP_UTF8, 0, U16Str, U16StrLen, Buf, BufSize, nullptr,
nullptr);
winapi::WideCharToMultiByte(winapi::CP_UTF8_, 0, U16Str, U16StrLen, Buf,
BufSize, nullptr, nullptr);
return Buf;
}

void WasmEdge_SetConsoleOutputCPtoUTF8() { SetConsoleOutputCP(CP_UTF8); }
wchar_t *WasmEdge_GetCommandLineW() { return GetCommandLineW(); }
void WasmEdge_SetConsoleOutputCPtoUTF8() {
#if WINAPI_PARTITION_DESKTOP
winapi::SetConsoleOutputCP(CP_UTF8_);
#endif
}
wchar_t *WasmEdge_GetCommandLineW() { return winapi::GetCommandLineW(); }

const char **WasmEdge_CommandLineToUTF8ArgvW(const wchar_t *CmdLine,
int *Argc) {
auto ArgvW = CommandLineToArgvW(CmdLine, Argc);
auto ArgvW = winapi::CommandLineToArgvW(CmdLine, Argc);

if (ArgvW == nullptr)
return nullptr;

const char **Argv = new const char *[size_t(*Argc)];
const char **Argv = new const char *[static_cast<size_t>(*Argc)];
for (int I = 0; I < *Argc; I++) {
Argv[I] = UTF16ToUTF8(ArgvW[I]);
}

LocalFree(ArgvW);
winapi::LocalFree(ArgvW);
return Argv;
}
#endif

0 comments on commit 2d0bd76

Please sign in to comment.