diff options
Diffstat (limited to 'src/shell.zig')
| -rw-r--r-- | src/shell.zig | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/src/shell.zig b/src/shell.zig index f933777..be9fb0d 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -6,7 +6,7 @@ const fs = @import("fs.zig"); const conf = @import("conf.zig"); const time = @import("time.zig"); const Env = @import("env.zig"); -const fatal = @import("cli.zig").fatal; +const fatal = cli.fatal; pub const Exec_Args = struct { env: *const Env, @@ -19,12 +19,27 @@ pub const Exec_Result = union(enum) { failure: ?[]const u8, }; +/// Execute a binary and return its result. Both the success and the failure +/// messages are the stdout and stderr output of the binary and the caller is +/// responsible to deallocate this output. pub fn exec(args: Exec_Args) Exec_Result { return spawn_proc(args, true); } -pub fn run(args: Exec_Args) void { - _ = spawn_proc(args, false); +/// Run a binary and forward all its output to stdout/stderr directly. It +/// returns if the execution was successful or not. +pub fn run(args: Exec_Args) bool { + const res = spawn_proc(args, false); + switch (res) { + .success => |msg| { + assert(msg == null); + return true; + }, + .failure => |msg| { + assert(msg == null); + return false; + }, + } } pub fn edit_config_file(env: *const Env, file: *const fs.Path, comptime verify: fn (env: *const Env, vars: conf.Config) bool) !void { @@ -113,12 +128,14 @@ fn exec_editor(env: *const Env, file: *const fs.Path) !void { .argv = &.{ editor, file.name() }, }; const res = spawn_proc(args, false); - if (res.failure) |failure| { - if (failure.len == 0) { + switch (res) { + .success => |msg| { + assert(msg == null); + }, + .failure => |msg| { + assert(msg == null); fatal("failed to open editor", .{}); - } else { - fatal("{s}", .{failure}); - } + }, } } @@ -129,7 +146,7 @@ fn tmp_edit_file(env: *const Env, file: *const fs.Path) fs.Path { const etc = env.etc_path(.{}); const relname = file.relname(&etc).?; - var tmp_filename_buf: [std.fs.max_name_bytes]u8 = undefined; + var tmp_filename_buf: [std.Io.Dir.max_name_bytes]u8 = undefined; var tmp_filename_len: usize = 0; tmp_filename_len += std.fmt.printInt(tmp_filename_buf[tmp_filename_len..], time.now(), 16, .lower, .{}); @@ -282,3 +299,10 @@ fn spawn_proc(args: Exec_Args, need_stdout: bool) Exec_Result { }, } } + +fn without_trailing_newline(s: []const u8) []const u8 { + return if (s.len > 0 and s[s.len - 1] == '\n') + s[0 .. s.len - 1] + else + s; +} |