Initial commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn execute(
|
||||
io: std.Io,
|
||||
environ: *const std.process.Environ.Map,
|
||||
argv: []const []const u8,
|
||||
) !void {
|
||||
const path = if (argv.len >= 2)
|
||||
argv[1]
|
||||
else
|
||||
environ.get("USERPROFILE") orelse environ.get("HOME") orelse {
|
||||
std.debug.print("xsh: cd: cannot find home directory\n", .{});
|
||||
return;
|
||||
};
|
||||
|
||||
var dir = try std.Io.Dir.cwd().openDir(io, path, .{});
|
||||
defer dir.close(io);
|
||||
|
||||
try std.process.setCurrentDir(io, dir);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn execute(
|
||||
io: std.Io,
|
||||
argv: []const []const u8,
|
||||
) !void {
|
||||
for (argv[1..], 0..) |argument, index| {
|
||||
if (index != 0) {
|
||||
try std.Io.File.stdout().writeStreamingAll(io, " ");
|
||||
}
|
||||
|
||||
try std.Io.File.stdout().writeStreamingAll(io, argument);
|
||||
}
|
||||
|
||||
try std.Io.File.stdout().writeStreamingAll(io, "\n");
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub fn execute(running: *bool) void {
|
||||
running.* = false;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn execute(
|
||||
io: std.Io,
|
||||
allocator: std.mem.Allocator,
|
||||
) !void {
|
||||
const path = try std.process.currentPathAlloc(io, allocator);
|
||||
defer allocator.free(path);
|
||||
|
||||
try std.Io.File.stdout().writeStreamingAll(io, path);
|
||||
try std.Io.File.stdout().writeStreamingAll(io, "\n");
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
const std = @import("std");
|
||||
|
||||
const script = @import("../../script/script.zig");
|
||||
|
||||
pub fn execute(
|
||||
io: std.Io,
|
||||
allocator: std.mem.Allocator,
|
||||
argv: []const []const u8,
|
||||
) !void {
|
||||
if (argv.len < 2) {
|
||||
std.debug.print("xsh: run: missing script path\n", .{});
|
||||
return;
|
||||
}
|
||||
|
||||
var file = try std.Io.Dir.cwd().openFile(io, argv[1], .{});
|
||||
defer file.close(io);
|
||||
|
||||
var file_reader = file.reader(io, &.{});
|
||||
|
||||
const source = try file_reader.interface.allocRemaining(
|
||||
allocator,
|
||||
.limited(16 * 1024 * 1024),
|
||||
);
|
||||
|
||||
const name_z = try allocator.dupeZ(u8, argv[1]);
|
||||
|
||||
try script.run(
|
||||
allocator,
|
||||
io,
|
||||
name_z,
|
||||
source,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user