~ntgg/zosh

3ff75641f15d106e9406028cf30ab87668eb7baa — Noah Graff 4 years ago f82c385
added basic shell state struct.
1 files changed, 48 insertions(+), 0 deletions(-)

A src/state.zig
A src/state.zig => src/state.zig +48 -0
@@ 0,0 1,48 @@
const std = @import("std");
const Allocator = std.mem.Allocator;

pub const State = struct {
    exit: ?u8,
    last_status: u8,
    is_interactive: bool,

    variables: std.StringHashMap(Variable),

    pub fn init(allocator: *Allocator) State {
        return State{
            .exit = null,
            .last_status = 0,
            .is_interactive = true,
            .variables = std.StringHashMap(Variable).init(allocator),
        };
    }

    pub fn deinit(state: State) void {
        state.variables.deinit();
    }

    pub fn getEnv(state: State, variable_name: []const u8) ?Variable {
        return state.variables.getValue(variable_name);
    }

    pub fn getPs1(state: State) []const u8 {
        if (state.getEnv("PS1")) |ps1| {
            // TODO: Evaluate this appropriately.
            return ps1.value;
        }
        // TODO: use a more abstracted interface once it exists. The current
        // std.os.linux.getuid is broken, and hopefully at some point there
        // will be a std.os.getuid(), so I don't have to do switch cases for
        // all the other platforms.
        return switch (std.os.linux.syscall0(std.os.linux.SYS_getuid)) {
            0 => "# ", // a uid of 0 is root
            else => "$ ",
        };
    }
};

pub const Variable = struct {
    value: []const u8,
    is_readonly: bool,
    is_export: bool,
};