summaryrefslogtreecommitdiff
path: root/src/cmd/type.zig
blob: 5909f4aa6b3ec7da727bf749fcbb434ab1c7ac2b (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
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;

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 Type = struct {
    file: FileSpec,

    pub fn eval(type_cmd: Type, ctx: CommandContext) !CommandStatus {
        const file_path = switch (type_cmd.file) {
            .Con => {
                const error_msg = "Cannot TYPE from CON\n";
                var writer = ctx.output_writer;
                try writer.write(error_msg);
                return CommandStatus{ .Code = 1 };
            },
            .Lpt1, .Lpt2, .Lpt3, .Prn => {
                const error_msg = "Cannot TYPE from device\n";
                var writer = ctx.output_writer;
                try writer.write(error_msg);
                return CommandStatus{ .Code = 1 };
            },
            .Path => |path| path,
        };

        const file = std.fs.cwd().openFile(file_path, .{}) catch |err| {
            const error_msg = switch (err) {
                error.FileNotFound => "The system cannot find the file specified.\n",
                error.IsDir => "Access is denied.\n",
                error.AccessDenied => "Access is denied.\n",
                else => "Cannot access file.\n",
            };
            var writer = ctx.output_writer;
            try writer.write(error_msg);
            return CommandStatus{ .Code = 1 };
        };
        defer file.close();

        // Read and display file contents
        var buffer: [4096]u8 = undefined;
        while (true) {
            const bytes_read = file.readAll(&buffer) catch |err| {
                const error_msg = switch (err) {
                    error.AccessDenied => "Access is denied.\n",
                    else => "Error reading file.\n",
                };
                var writer = ctx.output_writer;
                try writer.write(error_msg);
                return CommandStatus{ .Code = 1 };
            };

            if (bytes_read == 0) break;

            // Process buffer contents for output
            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(ctx.allocator, byte);
                } else if (byte == '\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(ctx.allocator, '\t');
                } else {
                    // Replace non-printable characters with '?'
                    try processed_output.append(ctx.allocator, '?');
                }
            }

            var writer = ctx.output_writer;
            try writer.write(processed_output.items);

            // If we read less than the buffer size, we're done
            if (bytes_read < buffer.len) break;
        }

        return CommandStatus{ .Code = 0 };
    }
};