From 9be51bb18aae4ff43ffcac649d540a7b4441e4b8 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 15 Apr 2024 21:13:40 +0200 Subject: [PATCH] feat: add subprocess.term() --- src/subprocess.zig | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/subprocess.zig b/src/subprocess.zig index ed3db6b..adbfe36 100644 --- a/src/subprocess.zig +++ b/src/subprocess.zig @@ -51,6 +51,11 @@ pub fn close(self: *Self) tp.result { if (self.pid) |pid| if (!pid.expired()) try pid.send(.{"stdin_close"}); } +pub fn term(self: *Self) tp.result { + defer self.deinit(); + if (self.pid) |pid| if (!pid.expired()) try pid.send(.{"term"}); +} + pub fn writer(self: *Self) Writer { return .{ .context = self }; } @@ -201,6 +206,9 @@ const Proc = struct { fd.close(); self.child.stderr = null; } + } else if (try m.match(.{"term"})) { + const term_ = self.child.kill() catch |e| return tp.exit_error(e); + return self.handle_term(term_); } else if (try m.match(.{ "fd", tp.any, "read_error", tp.extract(&err), tp.extract(&err_msg) })) { return tp.exit(err_msg); } @@ -239,8 +247,11 @@ const Proc = struct { } fn handle_terminate(self: *Proc) tp.result { - const term = self.child.wait() catch |e| return tp.exit_error(e); - (switch (term) { + return self.handle_term(self.child.wait() catch |e| return tp.exit_error(e)); + } + + fn handle_term(self: *Proc, term_: std.ChildProcess.Term) tp.result { + (switch (term_) { .Exited => |val| self.parent.send(.{ self.tag, "term", "exited", val }), .Signal => |val| self.parent.send(.{ self.tag, "term", "signal", val }), .Stopped => |val| self.parent.send(.{ self.tag, "term", "stop", val }),