diff options
| author | Isaac Freund <ifreund@ifreund.xyz> | 2020-05-26 22:55:07 +0200 |
|---|---|---|
| committer | Isaac Freund <ifreund@ifreund.xyz> | 2020-05-31 21:04:25 +0200 |
| commit | d9ca9db5a49f0af2f6bef6f198b28a8d29808e6d (patch) | |
| tree | ebeb7de70a63bfff951e9eaa069f7885aa34a2dc /src/command/spawn.zig | |
| parent | 9cd61f75903a83e56538ae3d69e7c2f3d2e6ca10 (diff) | |
| download | river-d9ca9db5a49f0af2f6bef6f198b28a8d29808e6d.tar.gz river-d9ca9db5a49f0af2f6bef6f198b28a8d29808e6d.tar.xz | |
Rework commands to be string based
This allows for significantly more flexibility and should make
implementing the bind command possible.
Diffstat (limited to 'src/command/spawn.zig')
| -rw-r--r-- | src/command/spawn.zig | 34 |
1 files changed, 21 insertions, 13 deletions
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; }; } |
