Skip to content

armgit / WSM

Public Imported from GitHub
0

Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.

C++
2.4 KB
Raw Blame
#pragma once
#include <windows.h>
#include <filesystem>
#include <string>
#include <vector>

namespace wsm::runtime {
inline std::filesystem::path find_ssh_tool(const std::string& name, const std::vector<std::filesystem::path>& directories) {
    if (name != "ssh" && name != "scp" && name != "ssh-keygen") return {};
    for (const auto& directory : directories) {
        std::error_code ec;
        const auto file = directory / (name + ".exe");
        if (!directory.empty() && std::filesystem::is_regular_file(file, ec)) return std::filesystem::absolute(file);
    }
    return {};
}
inline std::vector<std::filesystem::path> ssh_tool_directories() {
    std::vector<std::filesystem::path> directories;
    wchar_t system[32768]{};
    if (GetSystemDirectoryW(system, 32768)) directories.emplace_back(std::filesystem::path(system) / "OpenSSH");
    for (auto variable : {L"ProgramW6432", L"ProgramFiles"}) {
        if (const auto value = _wgetenv(variable); value && *value) directories.emplace_back(std::filesystem::path(value) / "OpenSSH");
    }
    if (const auto value = _wgetenv(L"PATH")) {
        const std::wstring path(value);
        for (size_t start = 0; start < path.size();) {
            const auto end = path.find(L';', start);
            auto part = path.substr(start, end == std::wstring::npos ? path.size() - start : end - start);
            if (part.size() >= 2 && part.front() == L'"' && part.back() == L'"') part = part.substr(1, part.size() - 2);
            if (!part.empty()) directories.emplace_back(part);
            if (end == std::wstring::npos) break;
            start = end + 1;
        }
    }
    return directories;
}
inline std::string ssh_tool_command(const std::string& name) {
    const auto file = find_ssh_tool(name, ssh_tool_directories());
    const auto utf8 = file.u8string();
    return "\"" + std::string(reinterpret_cast<const char*>(utf8.data()), utf8.size()) + "\"";
}
inline bool ssh_tools_available() {
    const auto directories = ssh_tool_directories();
    for (auto name : {"ssh", "scp", "ssh-keygen"}) if (find_ssh_tool(name, directories).empty()) return false;
    return true;
}
inline const char* ssh_tools_missing_message() {
    return "OpenSSH tools (ssh, scp and ssh-keygen) were not found. WSM checks Windows System32/OpenSSH, Program Files/OpenSSH and PATH. Install Windows' OpenSSH Client or add a complete OpenSSH installation to PATH, then try again. Your macOS disk is kept.";
}
}