diff options
-rw-r--r-- | src/cmd/lib/types.zig | 9 | ||||
-rw-r--r-- | src/cmd/pipe.zig | 11 | ||||
-rw-r--r-- | src/cmd/redirect.zig | 11 | ||||
-rw-r--r-- | src/eval.zig | 6 |
4 files changed, 18 insertions, 19 deletions
diff --git a/src/cmd/lib/types.zig b/src/cmd/lib/types.zig index 5990511..c1ce3c7 100644 --- a/src/cmd/lib/types.zig +++ b/src/cmd/lib/types.zig @@ -2,6 +2,9 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; +const cmd = @import("../../cmd.zig"); +const Command = cmd.Command; + pub const CommandStatus = union(enum) { Code: u16, ExitShell, @@ -62,16 +65,20 @@ pub const InputSource = struct { } }; +pub const ExecuteCommandFn = *const fn (Command, Allocator, ?*OutputCapture, ?*InputSource) anyerror!CommandStatus; + pub const CommandContext = struct { allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource, + execute_command: ExecuteCommandFn, - pub fn init(allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) CommandContext { + pub fn init(allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource, execute_command: ExecuteCommandFn) CommandContext { return CommandContext{ .allocator = allocator, .output_capture = output_capture, .input_source = input_source, + .execute_command = execute_command, }; } }; diff --git a/src/cmd/pipe.zig b/src/cmd/pipe.zig index ad0d0e4..39bee8c 100644 --- a/src/cmd/pipe.zig +++ b/src/cmd/pipe.zig @@ -11,15 +11,12 @@ const InputSource = types.InputSource; const cmd = @import("../cmd.zig"); const Command = cmd.Command; -// Function type for executing commands with output capture -const ExecuteCommandFn = *const fn (Command, Allocator, ?*OutputCapture, ?*InputSource) anyerror!CommandStatus; - pub const PipeCommand = struct { left: *Command, right: *Command, - pub fn eval(pipe: PipeCommand, ctx: CommandContext, executeCommandWithOutput: ExecuteCommandFn) !CommandStatus { - // Cast the execute function back to its proper type + pub fn eval(pipe: PipeCommand, ctx: CommandContext) !CommandStatus { + const execute_command = ctx.execute_command; const allocator = ctx.allocator; const output_capture = ctx.output_capture; @@ -28,7 +25,7 @@ pub const PipeCommand = struct { defer left_output.deinit(); // Execute the left command and capture its output - const left_status = try executeCommandWithOutput(pipe.left.*, allocator, &left_output, null); + const left_status = try execute_command(pipe.left.*, allocator, &left_output, null); // If the left command failed, return its status if (left_status != .Code or left_status.Code != 0) { @@ -40,7 +37,7 @@ pub const PipeCommand = struct { var right_input = InputSource.init(left_output_data); // Execute the right command with the left command's output as input - const right_status = try executeCommandWithOutput(pipe.right.*, allocator, output_capture, &right_input); + const right_status = try execute_command(pipe.right.*, allocator, output_capture, &right_input); return right_status; } diff --git a/src/cmd/redirect.zig b/src/cmd/redirect.zig index 969f6c4..10218f6 100644 --- a/src/cmd/redirect.zig +++ b/src/cmd/redirect.zig @@ -16,19 +16,14 @@ const Redirect = syntax.Redirect; const cmd = @import("../cmd.zig"); const Command = cmd.Command; -// Function type for executing commands with output capture -const ExecuteCommandFn = *const fn (Command, Allocator, ?*OutputCapture, ?*InputSource) anyerror!CommandStatus; - pub const RedirectCommand = struct { command: *Command, redirects: ArrayList(Redirect), - pub fn eval(redirect: RedirectCommand, ctx: CommandContext, executeCommandWithOutput: ExecuteCommandFn) !CommandStatus { - _ = ctx.input_source; // Redirect handles its own input source - - // Cast the execute function back to its proper type + pub fn eval(redirect: RedirectCommand, ctx: CommandContext) !CommandStatus { const allocator = ctx.allocator; const output_capture = ctx.output_capture; + const execute_command = ctx.execute_command; // Check if we have any output redirections var has_output_redirect = false; @@ -87,7 +82,7 @@ pub const RedirectCommand = struct { } // Execute the command with input and output capture (only capture output if needed) - const status = try executeCommandWithOutput(redirect.command.*, allocator, if (has_output_redirect) &captured_output else output_capture, if (redirect_input_source) |*source| source else null); + const status = try execute_command(redirect.command.*, allocator, if (has_output_redirect) &captured_output else output_capture, if (redirect_input_source) |*source| source else null); // Handle output redirections for (redirect.redirects.items) |redir| { diff --git a/src/eval.zig b/src/eval.zig index 7bd7da7..17486aa 100644 --- a/src/eval.zig +++ b/src/eval.zig @@ -36,7 +36,7 @@ pub fn executeCommand(command: Command, allocator: Allocator) !CommandStatus { } pub fn executeCommandWithOutput(command: Command, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus { - const ctx = CommandContext.init(allocator, output_capture, input_source); + const ctx = CommandContext.init(allocator, output_capture, input_source, executeCommandWithOutput); switch (command) { .Empty => return CommandStatus{ .Code = 0 }, @@ -124,11 +124,11 @@ pub fn executeCommandWithOutput(command: Command, allocator: Allocator, output_c }, .Redirect => |redirect| { - return redirect.eval(ctx, executeCommandWithOutput); + return redirect.eval(ctx); }, .Pipe => |pipe| { - return pipe.eval(ctx, executeCommandWithOutput); + return pipe.eval(ctx); }, } } |