Refactor builtin commands dispatcher

This commit is contained in:
2026-08-23 11:43:28 +03:00
parent 0548f8de26
commit 35f5d5777e
9 changed files with 78 additions and 75 deletions
+21 -30
View File
@@ -8,16 +8,22 @@ const exit = @import("cmds/exit.zig");
const pwd = @import("cmds/pwd.zig");
const run = @import("cmds/run.zig");
pub const Shell = struct {
running: bool = true,
interrupted: bool = false,
environ: *const std.process.Environ.Map,
};
const types = @import("types.zig");
const CommandFn = *const fn (types.BuiltinCommandContext) anyerror!void;
const commands = std.StaticStringMap(CommandFn).initComptime(.{
.{ "cd", cd.execute },
.{ "pwd", pwd.execute },
.{ "run", run.execute },
.{ "echo", echo.execute },
.{ "exit", exit.execute },
});
pub fn execute(
io: std.Io,
allocator: std.mem.Allocator,
shell: *Shell,
shell: *types.Shell,
command: parser.Command,
) !bool {
if (command.argv.len == 0)
@@ -25,30 +31,15 @@ pub fn execute(
const name = command.argv[0];
if (std.mem.eql(u8, name, "cd")) {
try cd.execute(io, shell.environ, command.argv);
return true;
}
const execute_fn = commands.get(name) orelse
return false;
if (std.mem.eql(u8, name, "pwd")) {
try pwd.execute(io, allocator);
return true;
}
try execute_fn(.{
.io = io,
.allocator = allocator,
.shell = shell,
.argv = command.argv,
});
if (std.mem.eql(u8, name, "run")) {
try run.execute(io, allocator, command.argv);
return true;
}
if (std.mem.eql(u8, name, "echo")) {
try echo.execute(io, command.argv);
return true;
}
if (std.mem.eql(u8, name, "exit")) {
exit.execute(&shell.running);
return true;
}
return false;
return true;
}