From f15ec9a59b777a2b7998518858f11f5da0dfcb2d Mon Sep 17 00:00:00 2001 From: emekoi Date: Thu, 1 Aug 2019 17:51:53 -0500 Subject: implemented runtime abi detetction for windows --- src/os.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/os.cpp') diff --git a/src/os.cpp b/src/os.cpp index 5fa70bd260..5844070609 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1125,29 +1125,27 @@ Error os_get_cwd(Buf *out_cwd) { #endif } -#if defined(ZIG_OS_WINDOWS) #define is_wprefix(s, prefix) \ (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0) -static bool is_stderr_cyg_pty(void) { - HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE); - if (stderr_handle == INVALID_HANDLE_VALUE) +bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { +#if defined(ZIG_OS_WINDOWS) + HANDLE handle = (HANDLE)_get_osfhandle(fd); + + // Cygwin/msys's pty is a pipe. + if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) != FILE_TYPE_PIPE) { return false; + } int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH; - FILE_NAME_INFO *nameinfo; WCHAR *p = NULL; - - // Cygwin/msys's pty is a pipe. - if (GetFileType(stderr_handle) != FILE_TYPE_PIPE) { - return 0; - } - nameinfo = (FILE_NAME_INFO *)allocate(size); + + FILE_NAME_INFO *nameinfo = (FILE_NAME_INFO *)allocate(size); if (nameinfo == NULL) { - return 0; + return false; } // Check the name of the pipe: // '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master' - if (GetFileInformationByHandleEx(stderr_handle, FileNameInfo, nameinfo, size)) { + if (GetFileInformationByHandleEx(handle, FileNameInfo, nameinfo, size)) { nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0'; p = nameinfo->FileName; if (is_wprefix(p, L"\\cygwin-")) { /* Cygwin */ @@ -1180,12 +1178,14 @@ static bool is_stderr_cyg_pty(void) { } free(nameinfo); return (p != NULL); -} +#else + return false #endif +} bool os_stderr_tty(void) { #if defined(ZIG_OS_WINDOWS) - return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty(); + return _isatty(_fileno(stderr)) != 0 || os_is_cygwin_pty(_fileno(stderr)); #elif defined(ZIG_OS_POSIX) return isatty(STDERR_FILENO) != 0; #else @@ -1486,7 +1486,7 @@ WORD original_console_attributes = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BL void os_stderr_set_color(TermColor color) { #if defined(ZIG_OS_WINDOWS) - if (is_stderr_cyg_pty()) { + if (os_stderr_tty()) { set_color_posix(color); return; } -- cgit v1.2.3 From 102d3f30c406e2818c6320a48f2405fa7e1e4237 Mon Sep 17 00:00:00 2001 From: emekoi Date: Thu, 1 Aug 2019 18:27:39 -0500 Subject: accept unix style paths on windows-gnu --- src/os.cpp | 8 ++++---- src/os.hpp | 5 +++++ src/target.cpp | 2 +- std/os/windows.zig | 7 +++---- 4 files changed, 13 insertions(+), 9 deletions(-) (limited to 'src/os.cpp') diff --git a/src/os.cpp b/src/os.cpp index 5844070609..6c1a2581df 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1130,7 +1130,7 @@ Error os_get_cwd(Buf *out_cwd) { bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { #if defined(ZIG_OS_WINDOWS) HANDLE handle = (HANDLE)_get_osfhandle(fd); - + // Cygwin/msys's pty is a pipe. if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) != FILE_TYPE_PIPE) { return false; @@ -1138,7 +1138,7 @@ bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH; WCHAR *p = NULL; - + FILE_NAME_INFO *nameinfo = (FILE_NAME_INFO *)allocate(size); if (nameinfo == NULL) { return false; @@ -1179,13 +1179,13 @@ bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { free(nameinfo); return (p != NULL); #else - return false + return false; #endif } bool os_stderr_tty(void) { #if defined(ZIG_OS_WINDOWS) - return _isatty(_fileno(stderr)) != 0 || os_is_cygwin_pty(_fileno(stderr)); + return _isatty(fileno(stderr)) != 0 || os_is_cygwin_pty(fileno(stderr)); #elif defined(ZIG_OS_POSIX) return isatty(STDERR_FILENO) != 0; #else diff --git a/src/os.hpp b/src/os.hpp index fc0e1929f2..7354528c34 100644 --- a/src/os.hpp +++ b/src/os.hpp @@ -89,6 +89,11 @@ struct Termination { #define OsFile int #endif +#if defined(ZIG_OS_WINDOWS) +#undef fileno +#define fileno _fileno +#endif + struct OsTimeStamp { uint64_t sec; uint64_t nsec; diff --git a/src/target.cpp b/src/target.cpp index d34254f1d1..695a810e39 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -1507,7 +1507,7 @@ bool target_is_single_threaded(const ZigTarget *target) { static ZigLLVM_EnvironmentType target_get_win32_abi() { FILE* files[] = { stdin, stdout, stderr, nullptr }; for (int i = 0; files[i] != nullptr; i++) { - if (os_is_cygwin_pty(_fileno(files[i]))) { + if (os_is_cygwin_pty(fileno(files[i]))) { return ZigLLVM_GNU; } } diff --git a/std/os/windows.zig b/std/os/windows.zig index ac76e8f58f..954e56443f 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -65,7 +65,7 @@ pub const CreateFileError = error{ InvalidUtf8, /// On Windows, file paths cannot contain these characters: - /// '/', '*', '?', '"', '<', '>', '|' + /// '*', '?', '"', '<', '>', '|', and '/' (when the ABI is not GNU) BadPathName, Unexpected, @@ -831,8 +831,8 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16) // disallow forward slashes in zig std lib file functions on Windows. for (s) |byte| { switch (byte) { - '/', '*', '?', '"', '<', '>', '|' => return error.BadPathName, - else => {}, + '*', '?', '"', '<', '>', '|' => return error.BadPathName, + else => if (builtin.abi == .msvc and byte == '/') return error.BadPathName, } } const start_index = if (mem.startsWith(u8, s, "\\\\") or !std.fs.path.isAbsolute(s)) 0 else blk: { @@ -866,7 +866,6 @@ pub fn unexpectedError(err: DWORD) std.os.UnexpectedError { return error.Unexpected; } - /// Call this when you made a windows NtDll call /// and you get an unexpected status. pub fn unexpectedStatus(status: NTSTATUS) std.os.UnexpectedError { -- cgit v1.2.3