From d06385337b1f2d53da7689c17deaa2a910178c51 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sat, 16 Aug 2025 21:30:46 +0200 Subject: redirect: Replace std.debug.print() with std.io.getStdOut().writeAll(). --- src/cmd/pipe.zig | 1 - src/cmd/redirect.zig | 45 +++++++++++++++++++++++---------------------- src/eval.zig | 1 - 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/cmd/pipe.zig b/src/cmd/pipe.zig index c3d08e4..90d1e13 100644 --- a/src/cmd/pipe.zig +++ b/src/cmd/pipe.zig @@ -1,6 +1,5 @@ const std = @import("std"); const Allocator = std.mem.Allocator; -const print = std.debug.print; const types = @import("./lib/types.zig"); const CommandStatus = types.CommandStatus; diff --git a/src/cmd/redirect.zig b/src/cmd/redirect.zig index 92a666a..ef37c3e 100644 --- a/src/cmd/redirect.zig +++ b/src/cmd/redirect.zig @@ -1,7 +1,6 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; -const print = std.debug.print; const types = @import("./lib/types.zig"); const CommandStatus = types.CommandStatus; @@ -22,6 +21,8 @@ pub const RedirectCommand = struct { pub fn eval(redirect: RedirectCommand, ctx: CommandContext) !CommandStatus { const allocator = ctx.allocator; + const stdout = std.io.getStdOut(); + const stderr = std.io.getStdErr(); // Extract output_capture from the output_writer for redirection logic const output_capture = switch (ctx.output_writer) { .capture => |capture| capture, @@ -51,11 +52,11 @@ pub const RedirectCommand = struct { if (redir.redirect_type == .InputFrom) { const file_path = switch (redir.target) { .Con => { - print("Input redirection from CON not supported\n", .{}); + try stderr.writeAll("Input redirection from CON not supported\n"); return CommandStatus{ .Code = 1 }; }, .Lpt1, .Lpt2, .Lpt3, .Prn => { - print("Cannot redirect input from device\n", .{}); + try stderr.writeAll("Cannot redirect input from device\n"); return CommandStatus{ .Code = 1 }; }, .Path => |path| path, @@ -64,9 +65,9 @@ pub const RedirectCommand = struct { // Read input file const file = std.fs.cwd().openFile(file_path, .{}) catch |err| { switch (err) { - error.FileNotFound => print("The system cannot find the file specified.\n", .{}), - error.AccessDenied => print("Access is denied.\n", .{}), - else => print("Cannot open input file.\n", .{}), + error.FileNotFound => stderr.writeAll("The system cannot find the file specified.\n") catch {}, + error.AccessDenied => stderr.writeAll("Access is denied.\n") catch {}, + else => stderr.writeAll("Cannot open input file.\n") catch {}, } return CommandStatus{ .Code = 1 }; }; @@ -74,8 +75,8 @@ pub const RedirectCommand = struct { input_data = file.readToEndAlloc(allocator, std.math.maxInt(usize)) catch |err| { switch (err) { - error.AccessDenied => print("Access is denied.\n", .{}), - else => print("Cannot read input file.\n", .{}), + error.AccessDenied => stderr.writeAll("Access is denied.\n") catch {}, + else => stderr.writeAll("Cannot read input file.\n") catch {}, } return CommandStatus{ .Code = 1 }; }; @@ -94,11 +95,11 @@ pub const RedirectCommand = struct { const file_path = switch (redir.target) { .Con => { // Redirect to console - just print normally - print("{s}", .{captured_output.getContents()}); + try stdout.writeAll(captured_output.getContents()); continue; }, .Lpt1, .Lpt2, .Lpt3, .Prn => { - print("Cannot redirect to device\n", .{}); + try stderr.writeAll("Cannot redirect to device\n"); return CommandStatus{ .Code = 1 }; }, .Path => |path| path, @@ -110,8 +111,8 @@ pub const RedirectCommand = struct { // Write to file, overwriting existing content const file = std.fs.cwd().createFile(file_path, .{}) catch |err| { switch (err) { - error.AccessDenied => print("Access is denied.\n", .{}), - else => print("Cannot create file.\n", .{}), + error.AccessDenied => stderr.writeAll("Access is denied.\n") catch {}, + else => stderr.writeAll("Cannot create file.\n") catch {}, } return CommandStatus{ .Code = 1 }; }; @@ -119,8 +120,8 @@ pub const RedirectCommand = struct { file.writeAll(captured_output.getContents()) catch |err| { switch (err) { - error.AccessDenied => print("Access is denied.\n", .{}), - else => print("Cannot write to file.\n", .{}), + error.AccessDenied => stderr.writeAll("Access is denied.\n") catch {}, + else => stderr.writeAll("Cannot write to file.\n") catch {}, } return CommandStatus{ .Code = 1 }; }; @@ -133,25 +134,25 @@ pub const RedirectCommand = struct { // Create new file if it doesn't exist const new_file = std.fs.cwd().createFile(file_path, .{}) catch |create_err| { switch (create_err) { - error.AccessDenied => print("Access is denied.\n", .{}), - else => print("Cannot create file.\n", .{}), + error.AccessDenied => stderr.writeAll("Access is denied.\n") catch {}, + else => stderr.writeAll("Cannot create file.\n") catch {}, } return CommandStatus{ .Code = 1 }; }; defer new_file.close(); new_file.writeAll(captured_output.getContents()) catch { - print("Cannot write to file.\n", .{}); + stderr.writeAll("Cannot write to file.\n") catch {}; return CommandStatus{ .Code = 1 }; }; continue; }, error.AccessDenied => { - print("Access is denied.\n", .{}); + stderr.writeAll("Access is denied.\n") catch {}; return CommandStatus{ .Code = 1 }; }, else => { - print("Cannot open file.\n", .{}); + stderr.writeAll("Cannot open file.\n") catch {}; return CommandStatus{ .Code = 1 }; }, } @@ -160,14 +161,14 @@ pub const RedirectCommand = struct { // Seek to end for append file.seekFromEnd(0) catch { - print("Cannot seek to end of file.\n", .{}); + stderr.writeAll("Cannot seek to end of file.\n") catch {}; return CommandStatus{ .Code = 1 }; }; file.writeAll(captured_output.getContents()) catch |err| { switch (err) { - error.AccessDenied => print("Access is denied.\n", .{}), - else => print("Cannot write to file.\n", .{}), + error.AccessDenied => stderr.writeAll("Access is denied.\n") catch {}, + else => stderr.writeAll("Cannot write to file.\n") catch {}, } return CommandStatus{ .Code = 1 }; }; diff --git a/src/eval.zig b/src/eval.zig index 1b9858e..b836c48 100644 --- a/src/eval.zig +++ b/src/eval.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const print = std.debug.print; const ArrayList = std.ArrayList; const Allocator = std.mem.Allocator; const Thread = std.Thread; -- cgit v1.2.1