authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-12 20:36:07-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-13 04:01:40+00:00
log0237e7a701892a0bf3c57e6868f54421782ff91d
tree0dacfcaf38543d384656b86ce846a3065a0ca1e3
parent8bae70454dabe77dfe7e5344e59ca2180d63af51

std.io.getStdOut and related fns no longer can error

Thanks to the Windows Process Environment Block, it is possible to obtain handles to the standard input, output, and error streams without possibility of failure.

17 files changed, 76 insertions(+), 115 deletions(-)

doc/langref.html.in+2-5
...@@ -202,11 +202,8 @@...@@ -202,11 +202,8 @@
202const std = @import("std");202const std = @import("std");
203203
204pub fn main() !void {204pub fn main() !void {
205 // If this program is run without stdout attached, exit with an error.205 const stdout = &std.io.getStdOut().outStream().stream;
206 const stdout_file = try std.io.getStdOut();206 try stdout.print("Hello, {}!\n", "world");
207 // If this program encounters pipe failure when printing to stdout, exit
208 // with an error.
209 try stdout_file.write("Hello, world!\n");
210}207}
211 {#code_end#}208 {#code_end#}
212 <p>209 <p>
lib/std/crypto/benchmark.zig+1-3
...@@ -131,9 +131,7 @@ fn printPad(stdout: var, s: []const u8) !void {...@@ -131,9 +131,7 @@ fn printPad(stdout: var, s: []const u8) !void {
131}131}
132132
133pub fn main() !void {133pub fn main() !void {
134 var stdout_file = try std.io.getStdOut();134 const stdout = &std.io.getStdOut().outStream().stream;
135 var stdout_out_stream = stdout_file.outStream();
136 const stdout = &stdout_out_stream.stream;
137135
138 var buffer: [1024]u8 = undefined;136 var buffer: [1024]u8 = undefined;
139 var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);137 var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
lib/std/debug.zig+7-7
...@@ -49,15 +49,15 @@ var stderr_mutex = std.Mutex.init();...@@ -49,15 +49,15 @@ var stderr_mutex = std.Mutex.init();
49pub fn warn(comptime fmt: []const u8, args: ...) void {49pub fn warn(comptime fmt: []const u8, args: ...) void {
50 const held = stderr_mutex.acquire();50 const held = stderr_mutex.acquire();
51 defer held.release();51 defer held.release();
52 const stderr = getStderrStream() catch return;52 const stderr = getStderrStream();
53 stderr.print(fmt, args) catch return;53 stderr.print(fmt, args) catch return;
54}54}
5555
56pub fn getStderrStream() !*io.OutStream(File.WriteError) {56pub fn getStderrStream() *io.OutStream(File.WriteError) {
57 if (stderr_stream) |st| {57 if (stderr_stream) |st| {
58 return st;58 return st;
59 } else {59 } else {
60 stderr_file = try io.getStdErr();60 stderr_file = io.getStdErr();
61 stderr_file_out_stream = stderr_file.outStream();61 stderr_file_out_stream = stderr_file.outStream();
62 const st = &stderr_file_out_stream.stream;62 const st = &stderr_file_out_stream.stream;
63 stderr_stream = st;63 stderr_stream = st;
...@@ -90,7 +90,7 @@ fn wantTtyColor() bool {...@@ -90,7 +90,7 @@ fn wantTtyColor() bool {
90/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.90/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.
91/// TODO multithreaded awareness91/// TODO multithreaded awareness
92pub fn dumpCurrentStackTrace(start_addr: ?usize) void {92pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
93 const stderr = getStderrStream() catch return;93 const stderr = getStderrStream();
94 if (builtin.strip_debug_info) {94 if (builtin.strip_debug_info) {
95 stderr.print("Unable to dump stack trace: debug info stripped\n") catch return;95 stderr.print("Unable to dump stack trace: debug info stripped\n") catch return;
96 return;96 return;
...@@ -109,7 +109,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {...@@ -109,7 +109,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
109/// unbuffered, and ignores any error returned.109/// unbuffered, and ignores any error returned.
110/// TODO multithreaded awareness110/// TODO multithreaded awareness
111pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {111pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
112 const stderr = getStderrStream() catch return;112 const stderr = getStderrStream();
113 if (builtin.strip_debug_info) {113 if (builtin.strip_debug_info) {
114 stderr.print("Unable to dump stack trace: debug info stripped\n") catch return;114 stderr.print("Unable to dump stack trace: debug info stripped\n") catch return;
115 return;115 return;
...@@ -182,7 +182,7 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace...@@ -182,7 +182,7 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace
182/// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned.182/// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned.
183/// TODO multithreaded awareness183/// TODO multithreaded awareness
184pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void {184pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void {
185 const stderr = getStderrStream() catch return;185 const stderr = getStderrStream();
186 if (builtin.strip_debug_info) {186 if (builtin.strip_debug_info) {
187 stderr.print("Unable to dump stack trace: debug info stripped\n") catch return;187 stderr.print("Unable to dump stack trace: debug info stripped\n") catch return;
188 return;188 return;
...@@ -237,7 +237,7 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c...@@ -237,7 +237,7 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
237 // which first called panic can finish printing a stack trace.237 // which first called panic can finish printing a stack trace.
238 os.abort();238 os.abort();
239 }239 }
240 const stderr = getStderrStream() catch os.abort();240 const stderr = getStderrStream();
241 stderr.print(format ++ "\n", args) catch os.abort();241 stderr.print(format ++ "\n", args) catch os.abort();
242 if (trace) |t| {242 if (trace) |t| {
243 dumpStackTrace(t.*);243 dumpStackTrace(t.*);
lib/std/io.zig+9-16
...@@ -34,28 +34,23 @@ else...@@ -34,28 +34,23 @@ else
34 Mode.blocking;34 Mode.blocking;
35pub const is_async = mode != .blocking;35pub const is_async = mode != .blocking;
3636
37pub const GetStdIoError = os.windows.GetStdHandleError;37pub fn getStdOut() File {
38
39pub fn getStdOut() GetStdIoError!File {
40 if (builtin.os == .windows) {38 if (builtin.os == .windows) {
41 const handle = try os.windows.GetStdHandle(os.windows.STD_OUTPUT_HANDLE);39 return File.openHandle(os.windows.peb().ProcessParameters.hStdOutput);
42 return File.openHandle(handle);
43 }40 }
44 return File.openHandle(os.STDOUT_FILENO);41 return File.openHandle(os.STDOUT_FILENO);
45}42}
4643
47pub fn getStdErr() GetStdIoError!File {44pub fn getStdErr() File {
48 if (builtin.os == .windows) {45 if (builtin.os == .windows) {
49 const handle = try os.windows.GetStdHandle(os.windows.STD_ERROR_HANDLE);46 return File.openHandle(os.windows.peb().ProcessParameters.hStdError);
50 return File.openHandle(handle);
51 }47 }
52 return File.openHandle(os.STDERR_FILENO);48 return File.openHandle(os.STDERR_FILENO);
53}49}
5450
55pub fn getStdIn() GetStdIoError!File {51pub fn getStdIn() File {
56 if (builtin.os == .windows) {52 if (builtin.os == .windows) {
57 const handle = try os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE);53 return File.openHandle(os.windows.peb().ProcessParameters.hStdInput);
58 return File.openHandle(handle);
59 }54 }
60 return File.openHandle(os.STDIN_FILENO);55 return File.openHandle(os.STDIN_FILENO);
61}56}
...@@ -598,7 +593,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr...@@ -598,7 +593,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
598 self.index = 0;593 self.index = 0;
599 }594 }
600595
601 fn writeFn(out_stream: *Stream, bytes: []const u8) !void {596 fn writeFn(out_stream: *Stream, bytes: []const u8) Error!void {
602 const self = @fieldParentPtr(Self, "stream", out_stream);597 const self = @fieldParentPtr(Self, "stream", out_stream);
603598
604 if (bytes.len >= self.buffer.len) {599 if (bytes.len >= self.buffer.len) {
...@@ -814,8 +809,7 @@ pub const BufferedAtomicFile = struct {...@@ -814,8 +809,7 @@ pub const BufferedAtomicFile = struct {
814};809};
815810
816pub fn readLine(buf: *std.Buffer) ![]u8 {811pub fn readLine(buf: *std.Buffer) ![]u8 {
817 var stdin = try getStdIn();812 var stdin_stream = getStdIn().inStream();
818 var stdin_stream = stdin.inStream();
819 return readLineFrom(&stdin_stream.stream, buf);813 return readLineFrom(&stdin_stream.stream, buf);
820}814}
821815
...@@ -856,8 +850,7 @@ test "io.readLineFrom" {...@@ -856,8 +850,7 @@ test "io.readLineFrom" {
856}850}
857851
858pub fn readLineSlice(slice: []u8) ![]u8 {852pub fn readLineSlice(slice: []u8) ![]u8 {
859 var stdin = try getStdIn();853 var stdin_stream = getStdIn().inStream();
860 var stdin_stream = stdin.inStream();
861 return readLineSliceFrom(&stdin_stream.stream, slice);854 return readLineSliceFrom(&stdin_stream.stream, slice);
862}855}
863856
lib/std/json.zig+2-2
...@@ -1015,7 +1015,7 @@ pub const Value = union(enum) {...@@ -1015,7 +1015,7 @@ pub const Value = union(enum) {
1015 var held = std.debug.getStderrMutex().acquire();1015 var held = std.debug.getStderrMutex().acquire();
1016 defer held.release();1016 defer held.release();
10171017
1018 const stderr = std.debug.getStderrStream() catch return;1018 const stderr = std.debug.getStderrStream();
1019 self.dumpStream(stderr, 1024) catch return;1019 self.dumpStream(stderr, 1024) catch return;
1020 }1020 }
10211021
...@@ -1026,7 +1026,7 @@ pub const Value = union(enum) {...@@ -1026,7 +1026,7 @@ pub const Value = union(enum) {
1026 var held = std.debug.getStderrMutex().acquire();1026 var held = std.debug.getStderrMutex().acquire();
1027 defer held.release();1027 defer held.release();
10281028
1029 const stderr = std.debug.getStderrStream() catch return;1029 const stderr = std.debug.getStderrStream();
1030 self.dumpStreamIndent(indent, stderr, 1024) catch return;1030 self.dumpStreamIndent(indent, stderr, 1024) catch return;
1031 }1031 }
1032 }1032 }
lib/std/progress.zig+2-5
...@@ -98,11 +98,8 @@ pub const Progress = struct {...@@ -98,11 +98,8 @@ pub const Progress = struct {
98 /// TODO solve https://github.com/ziglang/zig/issues/2765 and then change this98 /// TODO solve https://github.com/ziglang/zig/issues/2765 and then change this
99 /// API to return Progress rather than accept it as a parameter.99 /// API to return Progress rather than accept it as a parameter.
100 pub fn start(self: *Progress, name: []const u8, estimated_total_items: ?usize) !*Node {100 pub fn start(self: *Progress, name: []const u8, estimated_total_items: ?usize) !*Node {
101 if (std.io.getStdErr()) |stderr| {101 const stderr = std.io.getStdErr();
102 self.terminal = if (stderr.supportsAnsiEscapeCodes()) stderr else null;102 self.terminal = if (stderr.supportsAnsiEscapeCodes()) stderr else null;
103 } else |_| {
104 self.terminal = null;
105 }
106 self.root = Node{103 self.root = Node{
107 .context = self,104 .context = self,
108 .parent = null,105 .parent = null,
lib/std/special/build_runner.zig+12-23
...@@ -43,19 +43,8 @@ pub fn main() !void {...@@ -43,19 +43,8 @@ pub fn main() !void {
4343
44 var targets = ArrayList([]const u8).init(allocator);44 var targets = ArrayList([]const u8).init(allocator);
4545
46 var stderr_file = io.getStdErr();46 const stderr_stream = &io.getStdErr().outStream().stream;
47 var stderr_file_stream: File.OutStream = undefined;47 const stdout_stream = &io.getStdOut().outStream().stream;
48 var stderr_stream = if (stderr_file) |f| x: {
49 stderr_file_stream = f.outStream();
50 break :x &stderr_file_stream.stream;
51 } else |err| err;
52
53 var stdout_file = io.getStdOut();
54 var stdout_file_stream: File.OutStream = undefined;
55 var stdout_stream = if (stdout_file) |f| x: {
56 stdout_file_stream = f.outStream();
57 break :x &stdout_file_stream.stream;
58 } else |err| err;
5948
60 while (arg_it.next(allocator)) |err_or_arg| {49 while (arg_it.next(allocator)) |err_or_arg| {
61 const arg = try unwrapArg(err_or_arg);50 const arg = try unwrapArg(err_or_arg);
...@@ -63,37 +52,37 @@ pub fn main() !void {...@@ -63,37 +52,37 @@ pub fn main() !void {
63 const option_contents = arg[2..];52 const option_contents = arg[2..];
64 if (option_contents.len == 0) {53 if (option_contents.len == 0) {
65 warn("Expected option name after '-D'\n\n");54 warn("Expected option name after '-D'\n\n");
66 return usageAndErr(builder, false, try stderr_stream);55 return usageAndErr(builder, false, stderr_stream);
67 }56 }
68 if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {57 if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
69 const option_name = option_contents[0..name_end];58 const option_name = option_contents[0..name_end];
70 const option_value = option_contents[name_end + 1 ..];59 const option_value = option_contents[name_end + 1 ..];
71 if (try builder.addUserInputOption(option_name, option_value))60 if (try builder.addUserInputOption(option_name, option_value))
72 return usageAndErr(builder, false, try stderr_stream);61 return usageAndErr(builder, false, stderr_stream);
73 } else {62 } else {
74 if (try builder.addUserInputFlag(option_contents))63 if (try builder.addUserInputFlag(option_contents))
75 return usageAndErr(builder, false, try stderr_stream);64 return usageAndErr(builder, false, stderr_stream);
76 }65 }
77 } else if (mem.startsWith(u8, arg, "-")) {66 } else if (mem.startsWith(u8, arg, "-")) {
78 if (mem.eql(u8, arg, "--verbose")) {67 if (mem.eql(u8, arg, "--verbose")) {
79 builder.verbose = true;68 builder.verbose = true;
80 } else if (mem.eql(u8, arg, "--help")) {69 } else if (mem.eql(u8, arg, "--help")) {
81 return usage(builder, false, try stdout_stream);70 return usage(builder, false, stdout_stream);
82 } else if (mem.eql(u8, arg, "--prefix")) {71 } else if (mem.eql(u8, arg, "--prefix")) {
83 builder.install_prefix = try unwrapArg(arg_it.next(allocator) orelse {72 builder.install_prefix = try unwrapArg(arg_it.next(allocator) orelse {
84 warn("Expected argument after --prefix\n\n");73 warn("Expected argument after --prefix\n\n");
85 return usageAndErr(builder, false, try stderr_stream);74 return usageAndErr(builder, false, stderr_stream);
86 });75 });
87 } else if (mem.eql(u8, arg, "--search-prefix")) {76 } else if (mem.eql(u8, arg, "--search-prefix")) {
88 const search_prefix = try unwrapArg(arg_it.next(allocator) orelse {77 const search_prefix = try unwrapArg(arg_it.next(allocator) orelse {
89 warn("Expected argument after --search-prefix\n\n");78 warn("Expected argument after --search-prefix\n\n");
90 return usageAndErr(builder, false, try stderr_stream);79 return usageAndErr(builder, false, stderr_stream);
91 });80 });
92 builder.addSearchPrefix(search_prefix);81 builder.addSearchPrefix(search_prefix);
93 } else if (mem.eql(u8, arg, "--override-lib-dir")) {82 } else if (mem.eql(u8, arg, "--override-lib-dir")) {
94 builder.override_lib_dir = try unwrapArg(arg_it.next(allocator) orelse {83 builder.override_lib_dir = try unwrapArg(arg_it.next(allocator) orelse {
95 warn("Expected argument after --override-lib-dir\n\n");84 warn("Expected argument after --override-lib-dir\n\n");
96 return usageAndErr(builder, false, try stderr_stream);85 return usageAndErr(builder, false, stderr_stream);
97 });86 });
98 } else if (mem.eql(u8, arg, "--verbose-tokenize")) {87 } else if (mem.eql(u8, arg, "--verbose-tokenize")) {
99 builder.verbose_tokenize = true;88 builder.verbose_tokenize = true;
...@@ -111,7 +100,7 @@ pub fn main() !void {...@@ -111,7 +100,7 @@ pub fn main() !void {
111 builder.verbose_cc = true;100 builder.verbose_cc = true;
112 } else {101 } else {
113 warn("Unrecognized argument: {}\n\n", arg);102 warn("Unrecognized argument: {}\n\n", arg);
114 return usageAndErr(builder, false, try stderr_stream);103 return usageAndErr(builder, false, stderr_stream);
115 }104 }
116 } else {105 } else {
117 try targets.append(arg);106 try targets.append(arg);
...@@ -122,12 +111,12 @@ pub fn main() !void {...@@ -122,12 +111,12 @@ pub fn main() !void {
122 try runBuild(builder);111 try runBuild(builder);
123112
124 if (builder.validateUserInputDidItFail())113 if (builder.validateUserInputDidItFail())
125 return usageAndErr(builder, true, try stderr_stream);114 return usageAndErr(builder, true, stderr_stream);
126115
127 builder.make(targets.toSliceConst()) catch |err| {116 builder.make(targets.toSliceConst()) catch |err| {
128 switch (err) {117 switch (err) {
129 error.InvalidStepName => {118 error.InvalidStepName => {
130 return usageAndErr(builder, true, try stderr_stream);119 return usageAndErr(builder, true, stderr_stream);
131 },120 },
132 error.UncleanExit => process.exit(1),121 error.UncleanExit => process.exit(1),
133 else => return err,122 else => return err,
lib/std/unicode/throughput_test.zig+1-3
...@@ -2,9 +2,7 @@ const builtin = @import("builtin");...@@ -2,9 +2,7 @@ const builtin = @import("builtin");
2const std = @import("std");2const std = @import("std");
33
4pub fn main() !void {4pub fn main() !void {
5 var stdout_file = try std.io.getStdOut();5 const stdout = &std.io.getStdOut().outStream().stream;
6 var stdout_out_stream = stdout_file.outStream();
7 const stdout = &stdout_out_stream.stream;
86
9 const args = try std.process.argsAlloc(std.heap.direct_allocator);7 const args = try std.process.argsAlloc(std.heap.direct_allocator);
108
lib/std/zig/parser_test.zig+5-6
...@@ -1451,11 +1451,11 @@ test "zig fmt: preserve spacing" {...@@ -1451,11 +1451,11 @@ test "zig fmt: preserve spacing" {
1451 \\const std = @import("std");1451 \\const std = @import("std");
1452 \\1452 \\
1453 \\pub fn main() !void {1453 \\pub fn main() !void {
1454 \\ var stdout_file = try std.io.getStdOut;1454 \\ var stdout_file = std.io.getStdOut;
1455 \\ var stdout_file = try std.io.getStdOut;1455 \\ var stdout_file = std.io.getStdOut;
1456 \\1456 \\
1457 \\ var stdout_file = try std.io.getStdOut;1457 \\ var stdout_file = std.io.getStdOut;
1458 \\ var stdout_file = try std.io.getStdOut;1458 \\ var stdout_file = std.io.getStdOut;
1459 \\}1459 \\}
1460 \\1460 \\
1461 );1461 );
...@@ -2565,8 +2565,7 @@ const maxInt = std.math.maxInt;...@@ -2565,8 +2565,7 @@ const maxInt = std.math.maxInt;
2565var fixed_buffer_mem: [100 * 1024]u8 = undefined;2565var fixed_buffer_mem: [100 * 1024]u8 = undefined;
25662566
2567fn testParse(source: []const u8, allocator: *mem.Allocator, anything_changed: *bool) ![]u8 {2567fn testParse(source: []const u8, allocator: *mem.Allocator, anything_changed: *bool) ![]u8 {
2568 var stderr_file = try io.getStdErr();2568 const stderr = &io.getStdErr().outStream().stream;
2569 var stderr = &stderr_file.outStream().stream;
25702569
2571 const tree = try std.zig.parse(allocator, source);2570 const tree = try std.zig.parse(allocator, source);
2572 defer tree.deinit();2571 defer tree.deinit();
src-self-hosted/main.zig+4-7
...@@ -56,13 +56,10 @@ pub fn main() !void {...@@ -56,13 +56,10 @@ pub fn main() !void {
56 // libc allocator is guaranteed to have this property.56 // libc allocator is guaranteed to have this property.
57 const allocator = std.heap.c_allocator;57 const allocator = std.heap.c_allocator;
5858
59 var stdout_file = try std.io.getStdOut();59 stdout = &std.io.getStdOut().outStream().stream;
60 var stdout_out_stream = stdout_file.outStream();
61 stdout = &stdout_out_stream.stream;
6260
63 stderr_file = try std.io.getStdErr();61 stderr_file = std.io.getStdErr();
64 var stderr_out_stream = stderr_file.outStream();62 stderr = &stderr_file.outStream().stream;
65 stderr = &stderr_out_stream.stream;
6663
67 const args = try process.argsAlloc(allocator);64 const args = try process.argsAlloc(allocator);
68 // TODO I'm getting unreachable code here, which shouldn't happen65 // TODO I'm getting unreachable code here, which shouldn't happen
...@@ -619,7 +616,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {...@@ -619,7 +616,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
619 process.exit(1);616 process.exit(1);
620 }617 }
621618
622 var stdin_file = try io.getStdIn();619 var stdin_file = io.getStdIn();
623 var stdin = stdin_file.inStream();620 var stdin = stdin_file.inStream();
624621
625 const source_code = try stdin.stream.readAllAlloc(allocator, max_src_size);622 const source_code = try stdin.stream.readAllAlloc(allocator, max_src_size);
src-self-hosted/stage1.zig+4-8
...@@ -166,13 +166,9 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {...@@ -166,13 +166,9 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {
166 try args_list.append(std.mem.toSliceConst(u8, argv[arg_i]));166 try args_list.append(std.mem.toSliceConst(u8, argv[arg_i]));
167 }167 }
168168
169 var stdout_file = try std.io.getStdOut();169 stdout = &std.io.getStdOut().outStream().stream;
170 var stdout_out_stream = stdout_file.outStream();170 stderr_file = std.io.getStdErr();
171 stdout = &stdout_out_stream.stream;171 stderr = &stderr_file.outStream().stream;
172
173 stderr_file = try std.io.getStdErr();
174 var stderr_out_stream = stderr_file.outStream();
175 stderr = &stderr_out_stream.stream;
176172
177 const args = args_list.toSliceConst();173 const args = args_list.toSliceConst();
178 var flags = try Args.parse(allocator, self_hosted_main.args_fmt_spec, args[2..]);174 var flags = try Args.parse(allocator, self_hosted_main.args_fmt_spec, args[2..]);
...@@ -203,7 +199,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {...@@ -203,7 +199,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {
203 process.exit(1);199 process.exit(1);
204 }200 }
205201
206 var stdin_file = try io.getStdIn();202 const stdin_file = io.getStdIn();
207 var stdin = stdin_file.inStream();203 var stdin = stdin_file.inStream();
208204
209 const source_code = try stdin.stream.readAllAlloc(allocator, self_hosted_main.max_src_size);205 const source_code = try stdin.stream.readAllAlloc(allocator, self_hosted_main.max_src_size);
src-self-hosted/test.zig+2-2
...@@ -181,7 +181,7 @@ pub const TestContext = struct {...@@ -181,7 +181,7 @@ pub const TestContext = struct {
181 },181 },
182 Compilation.Event.Error => |err| return err,182 Compilation.Event.Error => |err| return err,
183 Compilation.Event.Fail => |msgs| {183 Compilation.Event.Fail => |msgs| {
184 var stderr = try std.io.getStdErr();184 const stderr = std.io.getStdErr();
185 try stderr.write("build incorrectly failed:\n");185 try stderr.write("build incorrectly failed:\n");
186 for (msgs) |msg| {186 for (msgs) |msg| {
187 defer msg.destroy();187 defer msg.destroy();
...@@ -231,7 +231,7 @@ pub const TestContext = struct {...@@ -231,7 +231,7 @@ pub const TestContext = struct {
231 text,231 text,
232 );232 );
233 std.debug.warn("\n====found:========\n");233 std.debug.warn("\n====found:========\n");
234 var stderr = try std.io.getStdErr();234 const stderr = std.io.getStdErr();
235 for (msgs) |msg| {235 for (msgs) |msg| {
236 defer msg.destroy();236 defer msg.destroy();
237 try msg.printToFile(stderr, errmsg.Color.Auto);237 try msg.printToFile(stderr, errmsg.Color.Auto);
test/compare_output.zig+15-15
...@@ -19,7 +19,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -19,7 +19,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
19 \\19 \\
20 \\pub fn main() void {20 \\pub fn main() void {
21 \\ privateFunction();21 \\ privateFunction();
22 \\ const stdout = &(getStdOut() catch unreachable).outStream().stream;22 \\ const stdout = &getStdOut().outStream().stream;
23 \\ stdout.print("OK 2\n") catch unreachable;23 \\ stdout.print("OK 2\n") catch unreachable;
24 \\}24 \\}
25 \\25 \\
...@@ -34,7 +34,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -34,7 +34,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
34 \\// purposefully conflicting function with main.zig34 \\// purposefully conflicting function with main.zig
35 \\// but it's private so it should be OK35 \\// but it's private so it should be OK
36 \\fn privateFunction() void {36 \\fn privateFunction() void {
37 \\ const stdout = &(getStdOut() catch unreachable).outStream().stream;37 \\ const stdout = &getStdOut().outStream().stream;
38 \\ stdout.print("OK 1\n") catch unreachable;38 \\ stdout.print("OK 1\n") catch unreachable;
39 \\}39 \\}
40 \\40 \\
...@@ -60,7 +60,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -60,7 +60,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
60 tc.addSourceFile("foo.zig",60 tc.addSourceFile("foo.zig",
61 \\usingnamespace @import("std").io;61 \\usingnamespace @import("std").io;
62 \\pub fn foo_function() void {62 \\pub fn foo_function() void {
63 \\ const stdout = &(getStdOut() catch unreachable).outStream().stream;63 \\ const stdout = &getStdOut().outStream().stream;
64 \\ stdout.print("OK\n") catch unreachable;64 \\ stdout.print("OK\n") catch unreachable;
65 \\}65 \\}
66 );66 );
...@@ -71,7 +71,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -71,7 +71,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
71 \\71 \\
72 \\pub fn bar_function() void {72 \\pub fn bar_function() void {
73 \\ if (foo_function()) {73 \\ if (foo_function()) {
74 \\ const stdout = &(getStdOut() catch unreachable).outStream().stream;74 \\ const stdout = &getStdOut().outStream().stream;
75 \\ stdout.print("OK\n") catch unreachable;75 \\ stdout.print("OK\n") catch unreachable;
76 \\ }76 \\ }
77 \\}77 \\}
...@@ -103,7 +103,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -103,7 +103,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
103 \\pub const a_text = "OK\n";103 \\pub const a_text = "OK\n";
104 \\104 \\
105 \\pub fn ok() void {105 \\pub fn ok() void {
106 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;106 \\ const stdout = &io.getStdOut().outStream().stream;
107 \\ stdout.print(b_text) catch unreachable;107 \\ stdout.print(b_text) catch unreachable;
108 \\}108 \\}
109 );109 );
...@@ -121,7 +121,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -121,7 +121,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
121 \\const io = @import("std").io;121 \\const io = @import("std").io;
122 \\122 \\
123 \\pub fn main() void {123 \\pub fn main() void {
124 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;124 \\ const stdout = &io.getStdOut().outStream().stream;
125 \\ stdout.print("Hello, world!\n{d:4} {x:3} {c}\n", @as(u32, 12), @as(u16, 0x12), @as(u8, 'a')) catch unreachable;125 \\ stdout.print("Hello, world!\n{d:4} {x:3} {c}\n", @as(u32, 12), @as(u16, 0x12), @as(u8, 'a')) catch unreachable;
126 \\}126 \\}
127 , "Hello, world!\n 12 12 a\n");127 , "Hello, world!\n 12 12 a\n");
...@@ -264,7 +264,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -264,7 +264,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
264 \\ var x_local : i32 = print_ok(x);264 \\ var x_local : i32 = print_ok(x);
265 \\}265 \\}
266 \\fn print_ok(val: @typeOf(x)) @typeOf(foo) {266 \\fn print_ok(val: @typeOf(x)) @typeOf(foo) {
267 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;267 \\ const stdout = &io.getStdOut().outStream().stream;
268 \\ stdout.print("OK\n") catch unreachable;268 \\ stdout.print("OK\n") catch unreachable;
269 \\ return 0;269 \\ return 0;
270 \\}270 \\}
...@@ -346,7 +346,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -346,7 +346,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
346 \\pub fn main() void {346 \\pub fn main() void {
347 \\ const bar = Bar {.field2 = 13,};347 \\ const bar = Bar {.field2 = 13,};
348 \\ const foo = Foo {.field1 = bar,};348 \\ const foo = Foo {.field1 = bar,};
349 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;349 \\ const stdout = &io.getStdOut().outStream().stream;
350 \\ if (!foo.method()) {350 \\ if (!foo.method()) {
351 \\ stdout.print("BAD\n") catch unreachable;351 \\ stdout.print("BAD\n") catch unreachable;
352 \\ }352 \\ }
...@@ -360,7 +360,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -360,7 +360,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
360 cases.add("defer with only fallthrough",360 cases.add("defer with only fallthrough",
361 \\const io = @import("std").io;361 \\const io = @import("std").io;
362 \\pub fn main() void {362 \\pub fn main() void {
363 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;363 \\ const stdout = &io.getStdOut().outStream().stream;
364 \\ stdout.print("before\n") catch unreachable;364 \\ stdout.print("before\n") catch unreachable;
365 \\ defer stdout.print("defer1\n") catch unreachable;365 \\ defer stdout.print("defer1\n") catch unreachable;
366 \\ defer stdout.print("defer2\n") catch unreachable;366 \\ defer stdout.print("defer2\n") catch unreachable;
...@@ -373,7 +373,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -373,7 +373,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
373 \\const io = @import("std").io;373 \\const io = @import("std").io;
374 \\const os = @import("std").os;374 \\const os = @import("std").os;
375 \\pub fn main() void {375 \\pub fn main() void {
376 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;376 \\ const stdout = &io.getStdOut().outStream().stream;
377 \\ stdout.print("before\n") catch unreachable;377 \\ stdout.print("before\n") catch unreachable;
378 \\ defer stdout.print("defer1\n") catch unreachable;378 \\ defer stdout.print("defer1\n") catch unreachable;
379 \\ defer stdout.print("defer2\n") catch unreachable;379 \\ defer stdout.print("defer2\n") catch unreachable;
...@@ -390,7 +390,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -390,7 +390,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
390 \\ do_test() catch return;390 \\ do_test() catch return;
391 \\}391 \\}
392 \\fn do_test() !void {392 \\fn do_test() !void {
393 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;393 \\ const stdout = &io.getStdOut().outStream().stream;
394 \\ stdout.print("before\n") catch unreachable;394 \\ stdout.print("before\n") catch unreachable;
395 \\ defer stdout.print("defer1\n") catch unreachable;395 \\ defer stdout.print("defer1\n") catch unreachable;
396 \\ errdefer stdout.print("deferErr\n") catch unreachable;396 \\ errdefer stdout.print("deferErr\n") catch unreachable;
...@@ -409,7 +409,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -409,7 +409,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
409 \\ do_test() catch return;409 \\ do_test() catch return;
410 \\}410 \\}
411 \\fn do_test() !void {411 \\fn do_test() !void {
412 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;412 \\ const stdout = &io.getStdOut().outStream().stream;
413 \\ stdout.print("before\n") catch unreachable;413 \\ stdout.print("before\n") catch unreachable;
414 \\ defer stdout.print("defer1\n") catch unreachable;414 \\ defer stdout.print("defer1\n") catch unreachable;
415 \\ errdefer stdout.print("deferErr\n") catch unreachable;415 \\ errdefer stdout.print("deferErr\n") catch unreachable;
...@@ -426,7 +426,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -426,7 +426,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
426 \\const io = @import("std").io;426 \\const io = @import("std").io;
427 \\427 \\
428 \\pub fn main() void {428 \\pub fn main() void {
429 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;429 \\ const stdout = &io.getStdOut().outStream().stream;
430 \\ stdout.print(foo_txt) catch unreachable;430 \\ stdout.print(foo_txt) catch unreachable;
431 \\}431 \\}
432 , "1234\nabcd\n");432 , "1234\nabcd\n");
...@@ -445,7 +445,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -445,7 +445,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
445 \\445 \\
446 \\pub fn main() !void {446 \\pub fn main() !void {
447 \\ var args_it = std.process.args();447 \\ var args_it = std.process.args();
448 \\ var stdout_file = try io.getStdOut();448 \\ var stdout_file = io.getStdOut();
449 \\ var stdout_adapter = stdout_file.outStream();449 \\ var stdout_adapter = stdout_file.outStream();
450 \\ const stdout = &stdout_adapter.stream;450 \\ const stdout = &stdout_adapter.stream;
451 \\ var index: usize = 0;451 \\ var index: usize = 0;
...@@ -486,7 +486,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {...@@ -486,7 +486,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
486 \\486 \\
487 \\pub fn main() !void {487 \\pub fn main() !void {
488 \\ var args_it = std.process.args();488 \\ var args_it = std.process.args();
489 \\ var stdout_file = try io.getStdOut();489 \\ var stdout_file = io.getStdOut();
490 \\ var stdout_adapter = stdout_file.outStream();490 \\ var stdout_adapter = stdout_file.outStream();
491 \\ const stdout = &stdout_adapter.stream;491 \\ const stdout = &stdout_adapter.stream;
492 \\ var index: usize = 0;492 \\ var index: usize = 0;
test/standalone/brace_expansion/main.zig+2-2
...@@ -179,8 +179,8 @@ fn expandNode(node: Node, output: *ArrayList(Buffer)) ExpandNodeError!void {...@@ -179,8 +179,8 @@ fn expandNode(node: Node, output: *ArrayList(Buffer)) ExpandNodeError!void {
179}179}
180180
181pub fn main() !void {181pub fn main() !void {
182 var stdin_file = try io.getStdIn();182 const stdin_file = io.getStdIn();
183 var stdout_file = try io.getStdOut();183 const stdout_file = io.getStdOut();
184184
185 var arena = std.heap.ArenaAllocator.init(std.heap.direct_allocator);185 var arena = std.heap.ArenaAllocator.init(std.heap.direct_allocator);
186 defer arena.deinit();186 defer arena.deinit();
test/standalone/cat/main.zig+6-8
...@@ -10,30 +10,28 @@ pub fn main() !void {...@@ -10,30 +10,28 @@ pub fn main() !void {
10 var args_it = process.args();10 var args_it = process.args();
11 const exe = try unwrapArg(args_it.next(allocator).?);11 const exe = try unwrapArg(args_it.next(allocator).?);
12 var catted_anything = false;12 var catted_anything = false;
13 var stdout_file = try io.getStdOut();13 const stdout_file = io.getStdOut();
1414
15 while (args_it.next(allocator)) |arg_or_err| {15 while (args_it.next(allocator)) |arg_or_err| {
16 const arg = try unwrapArg(arg_or_err);16 const arg = try unwrapArg(arg_or_err);
17 if (mem.eql(u8, arg, "-")) {17 if (mem.eql(u8, arg, "-")) {
18 catted_anything = true;18 catted_anything = true;
19 var stdin_file = try io.getStdIn();19 try cat_file(stdout_file, io.getStdIn());
20 try cat_file(&stdout_file, &stdin_file);
21 } else if (arg[0] == '-') {20 } else if (arg[0] == '-') {
22 return usage(exe);21 return usage(exe);
23 } else {22 } else {
24 var file = File.openRead(arg) catch |err| {23 const file = File.openRead(arg) catch |err| {
25 warn("Unable to open file: {}\n", @errorName(err));24 warn("Unable to open file: {}\n", @errorName(err));
26 return err;25 return err;
27 };26 };
28 defer file.close();27 defer file.close();
2928
30 catted_anything = true;29 catted_anything = true;
31 try cat_file(&stdout_file, &file);30 try cat_file(stdout_file, file);
32 }31 }
33 }32 }
34 if (!catted_anything) {33 if (!catted_anything) {
35 var stdin_file = try io.getStdIn();34 try cat_file(stdout_file, io.getStdIn());
36 try cat_file(&stdout_file, &stdin_file);
37 }35 }
38}36}
3937
...@@ -42,7 +40,7 @@ fn usage(exe: []const u8) !void {...@@ -42,7 +40,7 @@ fn usage(exe: []const u8) !void {
42 return error.Invalid;40 return error.Invalid;
43}41}
4442
45fn cat_file(stdout: *File, file: *File) !void {43fn cat_file(stdout: File, file: File) !void {
46 var buf: [1024 * 4]u8 = undefined;44 var buf: [1024 * 4]u8 = undefined;
4745
48 while (true) {46 while (true) {
test/standalone/guess_number/main.zig+1-2
...@@ -4,8 +4,7 @@ const io = std.io;...@@ -4,8 +4,7 @@ const io = std.io;
4const fmt = std.fmt;4const fmt = std.fmt;
55
6pub fn main() !void {6pub fn main() !void {
7 var stdout_file = try io.getStdOut();7 const stdout = &io.getStdOut().outStream().stream;
8 const stdout = &stdout_file.outStream().stream;
98
10 try stdout.print("Welcome to the Guess Number Game in Zig.\n");9 try stdout.print("Welcome to the Guess Number Game in Zig.\n");
1110
test/standalone/hello_world/hello.zig+1-1
...@@ -2,7 +2,7 @@ const std = @import("std");...@@ -2,7 +2,7 @@ const std = @import("std");
22
3pub fn main() !void {3pub fn main() !void {
4 // If this program is run without stdout attached, exit with an error.4 // If this program is run without stdout attached, exit with an error.
5 const stdout_file = try std.io.getStdOut();5 const stdout_file = std.io.getStdOut();
6 // If this program encounters pipe failure when printing to stdout, exit6 // If this program encounters pipe failure when printing to stdout, exit
7 // with an error.7 // with an error.
8 try stdout_file.write("Hello, world!\n");8 try stdout_file.write("Hello, world!\n");