55 lines
1.2 KiB
Zig
55 lines
1.2 KiB
Zig
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");
|
||
|
|
|
||
|
|
pub const Shell = struct {
|
||
|
|
running: bool = true,
|
||
|
|
interrupted: bool = false,
|
||
|
|
environ: *const std.process.Environ.Map,
|
||
|
|
};
|
||
|
|
|
||
|
|
pub fn execute(
|
||
|
|
io: std.Io,
|
||
|
|
allocator: std.mem.Allocator,
|
||
|
|
shell: *Shell,
|
||
|
|
command: parser.Command,
|
||
|
|
) !bool {
|
||
|
|
if (command.argv.len == 0)
|
||
|
|
return true;
|
||
|
|
|
||
|
|
const name = command.argv[0];
|
||
|
|
|
||
|
|
if (std.mem.eql(u8, name, "cd")) {
|
||
|
|
try cd.execute(io, shell.environ, command.argv);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (std.mem.eql(u8, name, "pwd")) {
|
||
|
|
try pwd.execute(io, allocator);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|