Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.43.0

0.43.0 / src / std / os / windows.lucb

#==============================================================================================
#
#   windows - The os module's native Windows calls
#
#   DESCRIPTION:
#       Native Windows calls stay behind the portable `os` interface. Public text is UTF-8.
#
#==============================================================================================

from windows_abi import MultiByteToWideChar, WideCharToMultiByte, GetLastError, SetLastError, CloseHandle, GetCurrentProcessId, GetEnvironmentVariableW, SetEnvironmentVariableW, GetCurrentDirectoryW, SetCurrentDirectoryW
extern func GetComputerNameW(output: u16*, capacity: u32*) -> i32
extern func GetModuleFileNameW(module: void*?, output: u16*, capacity: u32) -> u32
extern func GetActiveProcessorCount(group: u16) -> u32
extern func CreateToolhelp32Snapshot(flags: u32, process: u32) -> void*
extern func Process32FirstW(snapshot: void*, entry: WinProcessEntry*) -> i32
extern func Process32NextW(snapshot: void*, entry: WinProcessEntry*) -> i32

extern struct WinProcessEntry:
    size: u32
    usage: u32
    pid: u32
    heap: usize
    module: u32
    threads: u32
    parent: u32
    priority: i32
    flags: u32
    executable: u16[260]

# Environment views last until the next env call on this thread. Fixed bounds are
# the Windows environment limit, independent of the caller's current allocator.
local var environment_wide: u16[32768]
local var environment_utf8: u8[131073]
local var environment_key: u16[32768]

func windows_env(variable: c.str) -> str?:
    if MultiByteToWideChar(65001, 8, variable, -1, &environment_key[0], 32768) == 0:
        return none
    SetLastError(0)
    let count = GetEnvironmentVariableW(&environment_key[0], &environment_wide[0], 32768)
    if count >= 32768 or (count == 0 and GetLastError() != 0):
        return none
    var size: i32 = 0
    if count > 0:
        size = WideCharToMultiByte(65001, 128, &environment_wide[0], (i32)count, &environment_utf8[0], 131072, none, none)
        if size == 0:
            return none
    environment_utf8[(usize)size] = 0
    return (str)environment_utf8[..<(usize)size]

func windows_cwd() -> str!:
    var attempt: usize = 0
    while attempt < 8:
        let capacity = GetCurrentDirectoryW(0, none)
        if capacity == 0:
            break
        let text = try new u16[(usize)capacity] ---
        defer free(text)
        let count = GetCurrentDirectoryW(capacity, text.data)
        if count == 0:
            break
        if count < capacity:
            return try windows_text.narrow(text[..<(usize)count], failed)
        attempt += 1
    error(failed, "the working directory cannot be read")

## The module path grows until the call answers a length below the capacity: a full
## buffer is a truncated path.
func windows_executable() -> str!:
    var capacity: usize = 260
    while capacity <= 32768:
        let text = try new u16[capacity] ---
        defer free(text)
        let count = GetModuleFileNameW(none, text.data, (u32)capacity)
        if count == 0:
            break
        if (usize)count < capacity:
            return try windows_text.narrow(text[..<(usize)count], failed)
        capacity *= 4
    error(failed, "the executable path cannot be read")

func windows_parent_pid() -> i32:
    let snapshot = CreateToolhelp32Snapshot(2, 0)
    if (usize)snapshot == ~(usize)0:
        return 0
    defer _ = CloseHandle(snapshot)
    var entry: WinProcessEntry
    entry.size = (u32)memory.size_of(WinProcessEntry)
    var found = Process32FirstW(snapshot, &entry)
    let current = GetCurrentProcessId()
    while found != 0:
        if entry.pid == current:
            return (i32)entry.parent
        found = Process32NextW(snapshot, &entry)
    return 0