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

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

pub const PathGet = struct {
    pub fn eval(path_get: PathGet, ctx: CommandContext) !CommandStatus {
        _ = path_get;
        _ = ctx.input_source;

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

        const output = try std.fmt.allocPrint(ctx.allocator, "PATH={s}\n", .{current_path});
        defer ctx.allocator.free(output);
        var writer = ctx.output_writer;
        try writer.write(output);
        return CommandStatus{ .Code = 0 };
    }
};

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

    pub fn eval(path_set: PathSet, ctx: CommandContext) !CommandStatus {
        // 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(ctx.allocator, "PATH would be set to: {s}\n(Note: Environment variable setting not implemented in this shell)\n", .{path_set.value});
        defer ctx.allocator.free(output);
        var writer = ctx.output_writer;
        try writer.write(output);
        return CommandStatus{ .Code = 0 };
    }
};