summaryrefslogtreecommitdiff
path: root/src/cmd/copy.zig
blob: 733dc47a48bd5c8b48a50c2523cccbe32194c8c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const std = @import("std");
const Allocator = std.mem.Allocator;

const syntax = @import("../syntax.zig");
const FileSpec = syntax.FileSpec;

const types = @import("./lib/types.zig");
const CommandStatus = types.CommandStatus;
const CommandContext = types.CommandContext;

pub const Copy = struct {
    from: FileSpec,
    to: FileSpec,

    pub fn eval(copy: Copy, ctx: CommandContext) !CommandStatus {
        // Handle source file
        const source_path = switch (copy.from) {
            .Con => {
                // COPY CON <file> - copy from console to file
                const dest_path = switch (copy.to) {
                    .Con => {
                        const error_msg = "Cannot copy from CON to CON\n";
                        var writer = ctx.output_writer;
                        try writer.write(error_msg);
                        return CommandStatus{ .Code = 1 };
                    },
                    .Lpt1, .Lpt2, .Lpt3, .Prn => {
                        const error_msg = "Cannot copy to device\n";
                        var writer = ctx.output_writer;
                        try writer.write(error_msg);
                        return CommandStatus{ .Code = 1 };
                    },
                    .Path => |path| path,
                };

                // Create the destination file
                const dest_file = std.fs.cwd().createFile(dest_path, .{}) catch |err| {
                    const error_msg = switch (err) {
                        error.AccessDenied => "Access denied\n",
                        error.PathAlreadyExists => "File already exists - use different name\n",
                        else => "Cannot create file\n",
                    };
                    var writer = ctx.output_writer;
                    try writer.write(error_msg);
                    return CommandStatus{ .Code = 1 };
                };
                defer dest_file.close();

                // Read from input source and write to file until EOF (Ctrl+Z on DOS)
                var line_count: u32 = 0;
                var reader = ctx.input_reader;

                while (true) {
                    if (try reader.readLine(ctx.allocator)) |line| {
                        defer ctx.allocator.free(line);

                        // Write line to file with DOS line ending
                        try dest_file.writeAll(line);
                        try dest_file.writeAll("\r\n");
                        line_count += 1;
                    } else {
                        // EOF reached
                        break;
                    }
                }

                const msg = try std.fmt.allocPrint(ctx.allocator, "        1 File(s) copied\n", .{});
                defer ctx.allocator.free(msg);
                var writer = ctx.output_writer;
                try writer.write(msg);
                return CommandStatus{ .Code = 0 };
            },
            .Lpt1, .Lpt2, .Lpt3, .Prn => {
                const error_msg = "Cannot copy from device\n";
                var writer = ctx.output_writer;
                try writer.write(error_msg);
                return CommandStatus{ .Code = 1 };
            },
            .Path => |path| path,
        };

        // Handle destination
        const dest_path = switch (copy.to) {
            .Con => {
                // Copy to console (display file contents)
                const source_file = std.fs.cwd().openFile(source_path, .{}) catch |err| {
                    const error_msg = switch (err) {
                        error.FileNotFound => "File not found\n",
                        error.AccessDenied => "Access denied\n",
                        else => "Cannot access source file\n",
                    };
                    var writer = ctx.output_writer;
                    try writer.write(error_msg);
                    return CommandStatus{ .Code = 1 };
                };
                defer source_file.close();

                // Read and display file contents
                var buffer: [4096]u8 = undefined;
                while (true) {
                    const bytes_read = source_file.readAll(&buffer) catch {
                        const error_msg = "Error reading file\n";
                        var writer = ctx.output_writer;
                        try writer.write(error_msg);
                        return CommandStatus{ .Code = 1 };
                    };
                    if (bytes_read == 0) break;

                    var writer = ctx.output_writer;
                    try writer.write(buffer[0..bytes_read]);

                    if (bytes_read < buffer.len) break;
                }

                const msg = "        1 File(s) copied\n";
                var writer = ctx.output_writer;
                try writer.write(msg);
                return CommandStatus{ .Code = 0 };
            },
            .Lpt1, .Lpt2, .Lpt3, .Prn => {
                const error_msg = "Cannot copy to device\n";
                var writer = ctx.output_writer;
                try writer.write(error_msg);
                return CommandStatus{ .Code = 1 };
            },
            .Path => |path| path,
        };

        // Regular file-to-file copy
        std.fs.cwd().copyFile(source_path, std.fs.cwd(), dest_path, .{}) catch |err| {
            const error_msg = switch (err) {
                error.FileNotFound => "File not found\n",
                error.AccessDenied => "Access denied\n",
                error.PathAlreadyExists => "File already exists\n",
                else => "Cannot copy file\n",
            };
            var writer = ctx.output_writer;
            try writer.write(error_msg);
            return CommandStatus{ .Code = 1 };
        };

        const msg = "        1 File(s) copied\n";
        var writer = ctx.output_writer;
        try writer.write(msg);
        return CommandStatus{ .Code = 0 };
    }
};