summaryrefslogtreecommitdiff
path: root/src/cmd/copy.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/copy.zig')
-rw-r--r--src/cmd/copy.zig41
1 files changed, 20 insertions, 21 deletions
diff --git a/src/cmd/copy.zig b/src/cmd/copy.zig
index c99560f..9f9d4bf 100644
--- a/src/cmd/copy.zig
+++ b/src/cmd/copy.zig
@@ -7,15 +7,14 @@ const FileSpec = syntax.FileSpec;
const types = @import("./lib/types.zig");
const CommandStatus = types.CommandStatus;
-const OutputCapture = types.OutputCapture;
-const InputSource = types.InputSource;
+const CommandContext = types.CommandContext;
pub const Copy = struct {
from: FileSpec,
to: FileSpec,
- pub fn eval(copy: Copy, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
- _ = input_source;
+ pub fn eval(copy: Copy, ctx: CommandContext) !CommandStatus {
+ _ = ctx.input_source;
// Handle source file
const source_path = switch (copy.from) {
@@ -24,7 +23,7 @@ pub const Copy = struct {
const dest_path = switch (copy.to) {
.Con => {
const error_msg = "Cannot copy from CON to CON\n";
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -33,7 +32,7 @@ pub const Copy = struct {
},
.Lpt1, .Lpt2, .Lpt3, .Prn => {
const error_msg = "Cannot copy to device\n";
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -50,7 +49,7 @@ pub const Copy = struct {
error.PathAlreadyExists => "File already exists - use different name\n",
else => "Cannot create file\n",
};
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -64,14 +63,14 @@ pub const Copy = struct {
var line_count: u32 = 0;
// Skip output redirection since we're doing interactive input
- if (output_capture == null) {
+ if (ctx.output_capture == null) {
// In interactive mode, show no prompt (DOS behavior)
}
while (true) {
- if (stdin.readUntilDelimiterOrEofAlloc(allocator, '\n', 4096)) |maybe_line| {
+ if (stdin.readUntilDelimiterOrEofAlloc(ctx.allocator, '\n', 4096)) |maybe_line| {
if (maybe_line) |line| {
- defer allocator.free(line);
+ defer ctx.allocator.free(line);
// Check for Ctrl+Z (EOF marker)
if (line.len == 1 and line[0] == 26) { // ASCII 26 = Ctrl+Z
@@ -98,9 +97,9 @@ pub const Copy = struct {
}
}
- const msg = try std.fmt.allocPrint(allocator, " 1 File(s) copied\n", .{});
- defer allocator.free(msg);
- if (output_capture) |capture| {
+ const msg = try std.fmt.allocPrint(ctx.allocator, " 1 File(s) copied\n", .{});
+ defer ctx.allocator.free(msg);
+ if (ctx.output_capture) |capture| {
try capture.write(msg);
} else {
print("{s}", .{msg});
@@ -109,7 +108,7 @@ pub const Copy = struct {
},
.Lpt1, .Lpt2, .Lpt3, .Prn => {
const error_msg = "Cannot copy from device\n";
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -129,7 +128,7 @@ pub const Copy = struct {
error.AccessDenied => "Access denied\n",
else => "Cannot access source file\n",
};
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -143,7 +142,7 @@ pub const Copy = struct {
while (true) {
const bytes_read = source_file.readAll(&buffer) catch {
const error_msg = "Error reading file\n";
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -152,7 +151,7 @@ pub const Copy = struct {
};
if (bytes_read == 0) break;
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(buffer[0..bytes_read]);
} else {
print("{s}", .{buffer[0..bytes_read]});
@@ -162,7 +161,7 @@ pub const Copy = struct {
}
const msg = " 1 File(s) copied\n";
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(msg);
} else {
print("{s}", .{msg});
@@ -171,7 +170,7 @@ pub const Copy = struct {
},
.Lpt1, .Lpt2, .Lpt3, .Prn => {
const error_msg = "Cannot copy to device\n";
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -189,7 +188,7 @@ pub const Copy = struct {
error.PathAlreadyExists => "File already exists\n",
else => "Cannot copy file\n",
};
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -198,7 +197,7 @@ pub const Copy = struct {
};
const msg = " 1 File(s) copied\n";
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(msg);
} else {
print("{s}", .{msg});