diff options
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/dir.zig | 28 | ||||
-rw-r--r-- | src/cmd/external.zig | 13 | ||||
-rw-r--r-- | src/cmd/lib/flags.zig | 30 | ||||
-rw-r--r-- | src/cmd/lib/types.zig | 63 | ||||
-rw-r--r-- | src/cmd/redirect.zig | 8 | ||||
-rw-r--r-- | src/cmd/sort.zig | 12 | ||||
-rw-r--r-- | src/cmd/type.zig | 12 |
7 files changed, 88 insertions, 78 deletions
diff --git a/src/cmd/dir.zig b/src/cmd/dir.zig index 04f74dc..96c6319 100644 --- a/src/cmd/dir.zig +++ b/src/cmd/dir.zig @@ -120,8 +120,8 @@ pub const Dir = struct { subdirs: bool = false, // /s flag pub fn eval(dir: Dir, ctx: CommandContext) !CommandStatus { - var output_buffer = ArrayList(u8).init(ctx.allocator); - defer output_buffer.deinit(); + var output_buffer = ArrayList(u8){}; + defer output_buffer.deinit(ctx.allocator); // Format path in DOS style with backslashes and uppercase drive letter const formatted_path = try formatDosPath(ctx.allocator, dir.path); @@ -134,9 +134,9 @@ pub const Dir = struct { formatted_path[0] else 'C'; - try output_buffer.writer().print(" Volume in drive {c} has no label\n", .{drive_letter}); - try output_buffer.writer().print(" Volume Serial Number is 1234-5678\n", .{}); - try output_buffer.writer().print("\n Directory of {s}\n\n", .{formatted_path}); + try output_buffer.writer(ctx.allocator).print(" Volume in drive {c} has no label\n", .{drive_letter}); + try output_buffer.writer(ctx.allocator).print(" Volume Serial Number is 1234-5678\n", .{}); + try output_buffer.writer(ctx.allocator).print("\n Directory of {s}\n\n", .{formatted_path}); } var dir_iterator = std.fs.cwd().openDir(dir.path, .{ .iterate = true }) catch { @@ -172,23 +172,23 @@ pub const Dir = struct { switch (entry.kind) { .directory => { if (dir.bare_format) { - try output_buffer.writer().print("{s}\n", .{entry.name}); + try output_buffer.writer(ctx.allocator).print("{s}\n", .{entry.name}); } else if (dir.wide_format) { - try output_buffer.writer().print("{s:<12} ", .{entry.name}); + try output_buffer.writer(ctx.allocator).print("{s:<12} ", .{entry.name}); } else { - try output_buffer.writer().print("{s:<8} {s:<3} <DIR> {s}\n", .{ name, ext, date_time }); + try output_buffer.writer(ctx.allocator).print("{s:<8} {s:<3} <DIR> {s}\n", .{ name, ext, date_time }); } dir_count += 1; }, .file => { if (dir.bare_format) { - try output_buffer.writer().print("{s}\n", .{entry.name}); + try output_buffer.writer(ctx.allocator).print("{s}\n", .{entry.name}); } else if (dir.wide_format) { - try output_buffer.writer().print("{s:<12} ", .{entry.name}); + try output_buffer.writer(ctx.allocator).print("{s:<12} ", .{entry.name}); } else { const formatted_size = try formatWithCommas(ctx.allocator, stat.size); defer ctx.allocator.free(formatted_size); - try output_buffer.writer().print("{s:<8} {s:<3} {s:>14} {s}\n", .{ name, ext, formatted_size, date_time }); + try output_buffer.writer(ctx.allocator).print("{s:<8} {s:<3} {s:>14} {s}\n", .{ name, ext, formatted_size, date_time }); } file_count += 1; total_file_bytes += stat.size; @@ -199,7 +199,7 @@ pub const Dir = struct { // Add newline after wide format listing if (dir.wide_format and !dir.bare_format) { - try output_buffer.writer().print("\n", .{}); + try output_buffer.writer(ctx.allocator).print("\n", .{}); } // Only show footer for non-bare format @@ -218,8 +218,8 @@ pub const Dir = struct { const formatted_free_bytes = try formatWithCommas(ctx.allocator, bytes_free); defer ctx.allocator.free(formatted_free_bytes); - try output_buffer.writer().print(" {d} File(s) {s:>14} bytes\n", .{ file_count, formatted_total_bytes }); - try output_buffer.writer().print(" {d} Dir(s) {s:>14} bytes free\n", .{ dir_count, formatted_free_bytes }); + try output_buffer.writer(ctx.allocator).print(" {d} File(s) {s:>14} bytes\n", .{ file_count, formatted_total_bytes }); + try output_buffer.writer(ctx.allocator).print(" {d} Dir(s) {s:>14} bytes free\n", .{ dir_count, formatted_free_bytes }); } var writer = ctx.output_writer; diff --git a/src/cmd/external.zig b/src/cmd/external.zig index 3aaa7af..befd746 100644 --- a/src/cmd/external.zig +++ b/src/cmd/external.zig @@ -26,12 +26,12 @@ pub const External = struct { .capture => true, }; // Try to execute external command - var child_args = ArrayList([]const u8).init(allocator); - defer child_args.deinit(); + var child_args = ArrayList([]const u8){}; + defer child_args.deinit(allocator); - try child_args.append(external.program); + try child_args.append(allocator, external.program); for (external.args.items) |arg| { - try child_args.append(arg); + try child_args.append(allocator, arg); } var child = std.process.Child.init(child_args.items, allocator); @@ -72,7 +72,9 @@ pub const External = struct { // Handle input redirection if (input_source) |source| { if (child.stdin) |stdin| { - const writer = stdin.writer(); + var writer_buffer: [1024]u8 = undefined; + var file_writer = stdin.writer(&writer_buffer); + const writer = &file_writer.interface; // Reset source position for reading var temp_source = source.*; @@ -82,6 +84,7 @@ pub const External = struct { defer allocator.free(line); try writer.print("{s}\n", .{line}); } + try writer.flush(); child.stdin.?.close(); child.stdin = null; } diff --git a/src/cmd/lib/flags.zig b/src/cmd/lib/flags.zig index 637ea83..266ddfe 100644 --- a/src/cmd/lib/flags.zig +++ b/src/cmd/lib/flags.zig @@ -155,34 +155,34 @@ pub const CommandFlags = struct { } pub fn getHelp(self: *const CommandFlags, command_name: []const u8, allocator: Allocator) ![]const u8 { - var help = ArrayList(u8).init(allocator); - defer help.deinit(); + var help = ArrayList(u8){}; + defer help.deinit(allocator); - try help.appendSlice(command_name); - try help.appendSlice(" - Available flags:\n\n"); + try help.appendSlice(allocator, command_name); + try help.appendSlice(allocator, " - Available flags:\n\n"); for (self.flags) |flag_def| { - try help.appendSlice(" /"); - try help.appendSlice(flag_def.name); + try help.appendSlice(allocator, " /"); + try help.appendSlice(allocator, flag_def.name); if (flag_def.aliases.len > 0) { - try help.appendSlice(" ("); + try help.appendSlice(allocator, " ("); for (flag_def.aliases, 0..) |alias, i| { - if (i > 0) try help.appendSlice(", "); - try help.appendSlice(alias); + if (i > 0) try help.appendSlice(allocator, ", "); + try help.appendSlice(allocator, alias); } - try help.appendSlice(")"); + try help.appendSlice(allocator, ")"); } switch (flag_def.flag_type) { - .String => try help.appendSlice(":value"), - .Number => try help.appendSlice(":number"), + .String => try help.appendSlice(allocator, ":value"), + .Number => try help.appendSlice(allocator, ":number"), .Boolean => {}, } - try help.appendSlice(" - "); - try help.appendSlice(flag_def.description); - try help.appendSlice("\n"); + try help.appendSlice(allocator, " - "); + try help.appendSlice(allocator, flag_def.description); + try help.appendSlice(allocator, "\n"); } return help.toOwnedSlice(); diff --git a/src/cmd/lib/types.zig b/src/cmd/lib/types.zig index 57d370d..2dfb0fa 100644 --- a/src/cmd/lib/types.zig +++ b/src/cmd/lib/types.zig @@ -12,19 +12,21 @@ pub const CommandStatus = union(enum) { pub const OutputCapture = struct { buffer: ArrayList(u8), + allocator: Allocator, pub fn init(allocator: Allocator) OutputCapture { return OutputCapture{ - .buffer = ArrayList(u8).init(allocator), + .buffer = ArrayList(u8){}, + .allocator = allocator, }; } pub fn deinit(self: *OutputCapture) void { - self.buffer.deinit(); + self.buffer.deinit(self.allocator); } pub fn write(self: *OutputCapture, data: []const u8) !void { - try self.buffer.appendSlice(data); + try self.buffer.appendSlice(self.allocator, data); } pub fn getContents(self: *const OutputCapture) []const u8 { @@ -35,7 +37,11 @@ pub const OutputCapture = struct { pub const StdoutOutputCapture = struct { pub fn write(self: *StdoutOutputCapture, data: []const u8) !void { _ = self; - try std.io.getStdOut().writeAll(data); + var stdout_buffer: [1024]u8 = undefined; + var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); + const stdout = &stdout_writer.interface; + try stdout.writeAll(data); + try stdout.flush(); } }; @@ -73,41 +79,32 @@ pub const InputSource = struct { }; pub const StdinInputReader = struct { - pub fn readLine(self: *StdinInputReader, allocator: Allocator) !?[]const u8 { + pub fn readLine(self: *StdinInputReader, buf: []u8) !?[]const u8 { _ = self; - const stdin = std.io.getStdIn().reader(); + const stdin = std.fs.File.stdin(); // Read line from stdin with proper DOS handling - const line = stdin.readUntilDelimiterOrEofAlloc(allocator, '\n', 4096) catch |err| switch (err) { + var reader = stdin.readerStreaming(buf); + const input = std.io.Reader.takeDelimiterExclusive(&reader.interface, '\n') catch |err| switch (err) { + error.EndOfStream => return null, error.StreamTooLong => { - // Line too long, skip to next line - while (true) { - const ch = stdin.readByte() catch return null; - if (ch == '\n') break; - } - return try allocator.dupe(u8, ""); + // Line too long - return empty string + return ""; }, else => return null, }; - if (line) |input| { - // Check for Ctrl+Z (EOF marker) - ASCII 26 - if (input.len == 1 and input[0] == 26) { - allocator.free(input); - return null; // EOF - } - - // Remove trailing \r if present (DOS line endings) - if (input.len > 0 and input[input.len - 1] == '\r') { - const trimmed = try allocator.dupe(u8, input[0 .. input.len - 1]); - allocator.free(input); - return trimmed; - } else { - return input; - } - } else { + // Check for Ctrl+Z (EOF marker) - ASCII 26 + if (input.len == 1 and input[0] == 26) { return null; // EOF } + + // Remove trailing \r if present (DOS line endings) + if (input.len > 0 and input[input.len - 1] == '\r') { + return input[0 .. input.len - 1]; + } else { + return input; + } } }; @@ -118,7 +115,13 @@ pub const InputReader = union(enum) { pub fn readLine(self: *InputReader, allocator: Allocator) !?[]const u8 { switch (self.*) { .source => |source| return source.readLine(allocator), - .stdin => |stdin| return stdin.readLine(allocator), + .stdin => |stdin| { + var buf: [4096]u8 = undefined; + if (try stdin.readLine(&buf)) |line| { + return try allocator.dupe(u8, line); + } + return null; + }, } } }; diff --git a/src/cmd/redirect.zig b/src/cmd/redirect.zig index ef37c3e..3921ef0 100644 --- a/src/cmd/redirect.zig +++ b/src/cmd/redirect.zig @@ -21,8 +21,12 @@ 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(); + var stdout_buffer: [1024]u8 = undefined; + var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); + const stdout = &stdout_writer.interface; + var stderr_buffer: [1024]u8 = undefined; + var stderr_writer = std.fs.File.stderr().writer(&stderr_buffer); + const stderr = &stderr_writer.interface; // Extract output_capture from the output_writer for redirection logic const output_capture = switch (ctx.output_writer) { .capture => |capture| capture, diff --git a/src/cmd/sort.zig b/src/cmd/sort.zig index 79616e9..5f75734 100644 --- a/src/cmd/sort.zig +++ b/src/cmd/sort.zig @@ -10,12 +10,12 @@ pub const Sort = struct { pub fn eval(sort: Sort, ctx: CommandContext) !CommandStatus { _ = sort; - var lines = ArrayList([]const u8).init(ctx.allocator); + var lines = ArrayList([]const u8){}; defer { for (lines.items) |line| { ctx.allocator.free(line); } - lines.deinit(); + lines.deinit(ctx.allocator); } // Read input lines from unified input reader @@ -33,7 +33,7 @@ pub const Sort = struct { .source => { // Read from input redirection while (try reader.readLine(ctx.allocator)) |line| { - try lines.append(line); + try lines.append(ctx.allocator, line); } }, } @@ -46,11 +46,11 @@ pub const Sort = struct { }.lessThan); // Output sorted lines - var output_buffer = ArrayList(u8).init(ctx.allocator); - defer output_buffer.deinit(); + var output_buffer = ArrayList(u8){}; + defer output_buffer.deinit(ctx.allocator); for (lines.items) |line| { - try output_buffer.writer().print("{s}\n", .{line}); + try output_buffer.writer(ctx.allocator).print("{s}\n", .{line}); } var writer = ctx.output_writer; diff --git a/src/cmd/type.zig b/src/cmd/type.zig index 147c2a9..5909f4a 100644 --- a/src/cmd/type.zig +++ b/src/cmd/type.zig @@ -58,23 +58,23 @@ pub const Type = struct { if (bytes_read == 0) break; // Process buffer contents for output - var processed_output = ArrayList(u8).init(ctx.allocator); - defer processed_output.deinit(); + var processed_output = ArrayList(u8){}; + defer processed_output.deinit(ctx.allocator); for (buffer[0..bytes_read]) |byte| { // Convert to printable characters, similar to DOS TYPE behavior if (byte >= 32 and byte <= 126) { - try processed_output.append(byte); + try processed_output.append(ctx.allocator, byte); } else if (byte == '\n') { - try processed_output.append('\n'); + try processed_output.append(ctx.allocator, '\n'); } else if (byte == '\r') { // Skip carriage return in DOS-style line endings continue; } else if (byte == '\t') { - try processed_output.append('\t'); + try processed_output.append(ctx.allocator, '\t'); } else { // Replace non-printable characters with '?' - try processed_output.append('?'); + try processed_output.append(ctx.allocator, '?'); } } |