Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set sync or flush_on_newline for standard I/O on Windows. #9207

Merged
merged 2 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/crystal/system/unix/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,24 @@ module Crystal::System::FileDescriptor

bytes_read
end

def self.from_stdio(fd)
# If we have a TTY for stdin/out/err, it is possibly a shared terminal.
# We need to reopen it to use O_NONBLOCK without causing other programs to break

# Figure out the terminal TTY name. If ttyname fails we have a non-tty, or something strange.
# For non-tty we set flush_on_newline to true for reasons described in STDOUT and STDERR docs.
path = uninitialized UInt8[256]
ret = LibC.ttyname_r(fd, path, 256)
return IO::FileDescriptor.new(fd).tap(&.flush_on_newline=(true)) unless ret == 0

clone_fd = LibC.open(path, LibC::O_RDWR)
return IO::FileDescriptor.new(fd).tap(&.flush_on_newline=(true)) if clone_fd == -1

# We don't buffer output for TTY devices to see their output right away
io = IO::FileDescriptor.new(clone_fd)
io.close_on_exec = true
io.sync = true
io
end
end
21 changes: 21 additions & 0 deletions src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "c/io"
require "c/consoleapi"

module Crystal::System::FileDescriptor
@volatile_fd : Atomic(LibC::Int)
Expand Down Expand Up @@ -162,4 +163,24 @@ module Crystal::System::FileDescriptor

bytes_read
end

def self.from_stdio(fd)
console_handle = false
handle = LibC._get_osfhandle(fd)
if handle != -1
if LibC.GetConsoleMode(LibC::HANDLE.new(handle), out _) != 0
console_handle = true
end
end

io = IO::FileDescriptor.new(fd)
# Set sync or flush_on_newline as described in STDOUT and STDERR docs.
# See https://crystal-lang.org/api/toplevel.html#STDERR
if console_handle
io.sync = true
RX14 marked this conversation as resolved.
Show resolved Hide resolved
else
io.flush_on_newline = true
end
io
end
end
22 changes: 1 addition & 21 deletions src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,7 @@ class IO::FileDescriptor < IO

# :nodoc:
def self.from_stdio(fd)
{% if flag?(:win32) %}
new(fd)
{% else %}
# If we have a TTY for stdin/out/err, it is possibly a shared terminal.
# We need to reopen it to use O_NONBLOCK without causing other programs to break

# Figure out the terminal TTY name. If ttyname fails we have a non-tty, or something strange.
# For non-tty we set flush_on_newline to true for reasons described in STDOUT and STDERR docs.
path = uninitialized UInt8[256]
ret = LibC.ttyname_r(fd, path, 256)
return new(fd).tap(&.flush_on_newline=(true)) unless ret == 0

clone_fd = LibC.open(path, LibC::O_RDWR)
return new(fd).tap(&.flush_on_newline=(true)) if clone_fd == -1

# We don't buffer output for TTY devices to see their output right away
io = new(clone_fd)
io.close_on_exec = true
io.sync = true
io
{% end %}
Crystal::System::FileDescriptor.from_stdio(fd)
end

def blocking
Expand Down
5 changes: 5 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/consoleapi.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "c/winnt"

lib LibC
fun GetConsoleMode(hConsoleHandle : HANDLE, lpMode : DWORD*) : BOOL
end