authorgravatar for me@christine.websiteChristine Dodrill <me@christine.website> 2019-12-11 18:37:52-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-11 18:37:52-05:00
logb375f6e027a159616e80906aa05e253fbe8cc9df
tree79343dbd3e23733b86811f0c79a72c2032f6c4dc
parent7c1dbfab724291b492be5e64fd93ec14e48b202c

lib/std/io: let the bring-your-own OS package handle stdio (#3887)


1 files changed, 35 insertions(+), 8 deletions(-)

lib/std/io.zig+35-8
...@@ -34,25 +34,52 @@ else...@@ -34,25 +34,52 @@ else
34 Mode.blocking;34 Mode.blocking;
35pub const is_async = mode != .blocking;35pub const is_async = mode != .blocking;
3636
37fn getStdOutHandle() os.fd_t {
38 if (builtin.os == .windows) {
39 return os.windows.peb().ProcessParameters.hStdOutput;
40 }
41
42 if (@hasDecl(root, "os") and @hasDecl(root.os, "io") and @hasDecl(root.os.io, "getStdOutHandle")) {
43 return root.os.io.getStdOutHandle();
44 }
45
46 return os.STDOUT_FILENO;
47}
48
37pub fn getStdOut() File {49pub fn getStdOut() File {
50 return File.openHandle(getStdOutHandle());
51}
52
53fn getStdErrHandle() os.fd_t {
38 if (builtin.os == .windows) {54 if (builtin.os == .windows) {
39 return File.openHandle(os.windows.peb().ProcessParameters.hStdOutput);55 return os.windows.peb().ProcessParameters.hStdError;
40 }56 }
41 return File.openHandle(os.STDOUT_FILENO);57
58 if (@hasDecl(root, "os") and @hasDecl(root.os, "io") and @hasDecl(root.os.io, "getStdErrHandle")) {
59 return root.os.io.getStdErrHandle();
60 }
61
62 return os.STDERR_FILENO;
42}63}
4364
44pub fn getStdErr() File {65pub fn getStdErr() File {
66 return File.openHandle(getStdErrHandle());
67}
68
69fn getStdInHandle() os.fd_t {
45 if (builtin.os == .windows) {70 if (builtin.os == .windows) {
46 return File.openHandle(os.windows.peb().ProcessParameters.hStdError);71 return os.windows.peb().ProcessParameters.hStdInput;
47 }72 }
48 return File.openHandle(os.STDERR_FILENO);73
74 if (@hasDecl(root, "os") and @hasDecl(root.os, "io") and @hasDecl(root.os.io, "getStdInHandle")) {
75 return root.os.io.getStdInHandle();
76 }
77
78 return os.STDIN_FILENO;
49}79}
5080
51pub fn getStdIn() File {81pub fn getStdIn() File {
52 if (builtin.os == .windows) {82 return File.openHandle(getStdInHandle());
53 return File.openHandle(os.windows.peb().ProcessParameters.hStdInput);
54 }
55 return File.openHandle(os.STDIN_FILENO);
56}83}
5784
58pub const SeekableStream = @import("io/seekable_stream.zig").SeekableStream;85pub const SeekableStream = @import("io/seekable_stream.zig").SeekableStream;