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

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

pub const PathGet = struct {
    pub fn eval(path_get: PathGet, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
        _ = path_get;
        _ = input_source;

        const current_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) {
            error.EnvironmentVariableNotFound => {
                // PATH not set, show empty
                const output = "PATH=(not set)\n";
                if (output_capture) |capture| {
                    try capture.write(output);
                } else {
                    print("{s}", .{output});
                }
                return CommandStatus{ .Code = 0 };
            },
            else => {
                const error_msg = "Cannot access PATH environment variable\n";
                if (output_capture) |capture| {
                    try capture.write(error_msg);
                } else {
                    print("{s}", .{error_msg});
                }
                return CommandStatus{ .Code = 1 };
            },
        };
        defer allocator.free(current_path);

        const output = try std.fmt.allocPrint(allocator, "PATH={s}\n", .{current_path});
        defer allocator.free(output);
        if (output_capture) |capture| {
            try capture.write(output);
        } else {
            print("{s}", .{output});
        }
        return CommandStatus{ .Code = 0 };
    }
};

pub const PathSet = struct {
    value: []const u8,

    pub fn eval(path_set: PathSet, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
        _ = input_source;

        // Note: In a real DOS system, this would persist for the session
        // Here we just show what would be set but don't actually set it
        // since Zig's std.process doesn't provide a simple way to set env vars
        const output = try std.fmt.allocPrint(allocator, "PATH would be set to: {s}\n(Note: Environment variable setting not implemented in this shell)\n", .{path_set.value});
        defer allocator.free(output);
        if (output_capture) |capture| {
            try capture.write(output);
        } else {
            print("{s}", .{output});
        }
        return CommandStatus{ .Code = 0 };
    }
};