Files
xsh/src/builtin/builtin.zig
T

46 lines
1.0 KiB
Zig
Raw Normal View History

2026-08-23 11:11:36 +03:00
const std = @import("std");
const parser = @import("../shell/parser.zig");
const cd = @import("cmds/cd.zig");
const echo = @import("cmds/echo.zig");
const exit = @import("cmds/exit.zig");
const pwd = @import("cmds/pwd.zig");
const run = @import("cmds/run.zig");
2026-08-23 11:43:28 +03:00
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 },
});
2026-08-23 11:11:36 +03:00
pub fn execute(
io: std.Io,
allocator: std.mem.Allocator,
2026-08-23 11:43:28 +03:00
shell: *types.Shell,
2026-08-23 11:11:36 +03:00
command: parser.Command,
) !bool {
if (command.argv.len == 0)
return true;
const name = command.argv[0];
2026-08-23 11:43:28 +03:00
const execute_fn = commands.get(name) orelse
return false;
2026-08-23 11:11:36 +03:00
2026-08-23 11:43:28 +03:00
try execute_fn(.{
.io = io,
.allocator = allocator,
.shell = shell,
.argv = command.argv,
});
2026-08-23 11:11:36 +03:00
2026-08-23 11:43:28 +03:00
return true;
2026-08-23 11:11:36 +03:00
}