From d9ca9db5a49f0af2f6bef6f198b28a8d29808e6d Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Tue, 26 May 2020 22:55:07 +0200 Subject: Rework commands to be string based This allows for significantly more flexibility and should make implementing the bind command possible. --- src/command/spawn.zig | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'src/command/spawn.zig') diff --git a/src/command/spawn.zig b/src/command/spawn.zig index 61aedab..880483a 100644 --- a/src/command/spawn.zig +++ b/src/command/spawn.zig @@ -17,23 +17,31 @@ const std = @import("std"); -const c = @import("../c.zig"); - -const Arg = @import("../Command.zig").Arg; -const Log = @import("../log.zig").Log; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Spawn a program. -pub fn spawn(seat: *Seat, arg: Arg) void { - const cmd = arg.str; +pub fn spawn( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + + const cmd = try std.mem.join(allocator, " ", args[1..]); + defer allocator.free(cmd); + + const child_args = [_][]const u8{ "/bin/sh", "-c", cmd }; + const child = try std.ChildProcess.init(&child_args, allocator); + defer child.deinit(); - const argv = [_][]const u8{ "/bin/sh", "-c", cmd }; - const child = std.ChildProcess.init(&argv, std.heap.c_allocator) catch |err| { - Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); - return; - }; std.ChildProcess.spawn(child) catch |err| { - Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); - return; + failure_message.* = try std.fmt.allocPrint( + allocator, + "failed to spawn {}: {}.", + .{ cmd, err }, + ); + return Error.CommandFailed; }; } -- cgit v1.2.3