summaryrefslogtreecommitdiff
path: root/src/host_io.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 03:51:15 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit79a1e816ceb13ef81b712007716c8add2de75434 (patch)
tree1714b1bf19da3a58be2623ffe29e3f8be1b21fe3 /src/host_io.zig
parenta22b579ad2df8d1811f0e194fa1bbc06e2282cf8 (diff)
parentf1af015f01e4e8d42f6224c4d4063f3aea218f5a (diff)
downloadpardes-79a1e816ceb13ef81b712007716c8add2de75434.tar.gz
pardes-79a1e816ceb13ef81b712007716c8add2de75434.zip
integration: the terminal a module of its own, with the plumber whole and the walker's cheaper compile
A merge of term-phase1a and pardes-next, no conflicts. core-test (the core with no terminal) 411 passed, 535 skipped of 946; shell-test (the same tests, the terminal compiled in) 989/989, on tui. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/host_io.zig')
-rw-r--r--src/host_io.zig126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/host_io.zig b/src/host_io.zig
index 0e1d29fd..206341f9 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -59,6 +59,9 @@ pub const Host = struct {
set_clipboard: ?*const fn (ctx: ?*anyopaque, text: []const u8) void = null,
read_clipboard: ?*const fn (ctx: ?*anyopaque) void = null,
open_link: ?*const fn (ctx: ?*anyopaque, url: []const u8) void = null,
+ /// A plumbing rule's program (Effect.start): every host's is
+ /// startDetached. Null once it runs, else why not.
+ start: ?*const fn (ctx: ?*anyopaque, line: []const u8, why: []u8) ?[]const u8 = null,
lsp: ?*const fn (ctx: ?*anyopaque, req: Lsp.Request) void = null,
pipe: ?*const fn (ctx: ?*anyopaque, id: u32) void = null,
/// Start the streaming search of this generation on a worker
@@ -268,6 +271,9 @@ pub const Fallback = struct {
files: std.StringHashMapUnmanaged([]u8) = .empty,
clipboard: std.ArrayListUnmanaged(u8) = .empty,
link: std.ArrayListUnmanaged(u8) = .empty,
+ /// The programs plumbing rules started, as Effect.start packs them,
+ /// one a line.
+ started: std.ArrayListUnmanaged(u8) = .empty,
spawned: [pardes.MAX_PANES]bool = @splat(false),
watched: [pardes.MAX_PANES]bool = @splat(false),
@@ -280,6 +286,7 @@ pub const Fallback = struct {
f.files.deinit(f.gpa);
f.clipboard.deinit(f.gpa);
f.link.deinit(f.gpa);
+ f.started.deinit(f.gpa);
}
pub fn writeFile(f: *Fallback, path: []const u8, bytes: []const u8) bool {
@@ -315,6 +322,12 @@ pub const Fallback = struct {
f.link.clearRetainingCapacity();
f.link.appendSlice(f.gpa, url) catch {};
}
+
+ pub fn start(f: *Fallback, line: []const u8) ?[]const u8 {
+ f.started.appendSlice(f.gpa, line) catch return "out of memory";
+ f.started.append(f.gpa, '\n') catch return "out of memory";
+ return null;
+ }
};
/// What a pixel host's loop takes from its workers, in order: each pane's
@@ -2456,6 +2469,108 @@ const darwin = struct {
}
};
+/// A program a plumbing rule starts (Effect.start), as Plan 9's plumber
+/// starts one (match.c's startup): out of the editor's way, a session of
+/// its own reaped by init, stdin, stdout and stderr /dev/null and no other
+/// fd of the editor's, its argv exec'd (no shell: a rule that wants one
+/// says `sh -c`) in its directory. `line` is its directory, then its argv,
+/// each ended by a NUL. Null once it runs, else why not, as errno says it,
+/// in `why` (as the tape replays a host's words): a pipe closed at the
+/// exec carries the child's errno back.
+pub fn startDetached(_: ?*anyopaque, line: []const u8, why: []u8) ?[]const u8 {
+ const words = startWords(line) orelse return null;
+ const n = @min(words.len, why.len);
+ @memcpy(why[0..n], words[0..n]);
+ return why[0..n];
+}
+
+fn startWords(line: []const u8) ?[]const u8 {
+ var buf: [pardes.effect_path_cap]u8 = undefined;
+ if (line.len == 0 or line.len > buf.len or line[line.len - 1] != 0) return "malformed";
+ @memcpy(buf[0..line.len], line);
+ var argv: [64:null]?[*:0]const u8 = @splat(null);
+ var n: usize = 0;
+ var at: usize = 0;
+ var dir: [*:0]const u8 = undefined;
+ while (at < line.len) {
+ const end = std.mem.indexOfScalarPos(u8, line, at, 0).?;
+ const word: [*:0]const u8 = @ptrCast(&buf[at]);
+ if (at == 0) dir = word else if (n < argv.len) {
+ argv[n] = word;
+ n += 1;
+ } else return "more than 64 words";
+ at = end + 1;
+ }
+ if (n == 0) return "no program";
+ var fds: [2]c_int = undefined;
+ if (libc.pipe(&fds) != 0) return "no pipe";
+ for (fds) |fd| _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, libc.FD_CLOEXEC));
+ const first = libc.fork();
+ if (first < 0) {
+ _ = libc.close(fds[0]);
+ _ = libc.close(fds[1]);
+ return "no fork";
+ }
+ if (first == 0) {
+ // Only async-signal-safe calls between fork and exec.
+ _ = libc.setsid();
+ if (libc.fork() != 0) libc._exit(0);
+ resetChildSignals();
+ // What failed, then errno: 'd' the directory, 'x' the exec.
+ const Fail = struct {
+ fn say(fd: c_int, what: u8) noreturn {
+ var said: [5]u8 = undefined;
+ said[0] = what;
+ std.mem.writeInt(u32, said[1..5], @intFromEnum(libc.errno(-1)), .little);
+ _ = libc.write(fd, &said, said.len);
+ libc._exit(127);
+ }
+ };
+ const err_fd = libc.fcntl(fds[1], libc.F.DUPFD_CLOEXEC, @as(c_int, 3));
+ if (err_fd < 0) libc._exit(127);
+ if (dir[0] != 0 and libc.chdir(dir) != 0) Fail.say(err_fd, 'd');
+ const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0));
+ if (null_fd >= 0) for (0..3) |i| {
+ _ = libc.dup2(null_fd, @intCast(i));
+ };
+ // Every fd of the editor's but the errno pipe's gone: a pty's
+ // master held open would keep its terminal alive.
+ if (libc.dup2(err_fd, 3) < 0) libc._exit(127);
+ _ = libc.fcntl(3, libc.F.SETFD, @as(c_int, libc.FD_CLOEXEC));
+ const shut = if (comptime builtin.os.tag == .linux)
+ std.os.linux.close_range(4, std.math.maxInt(i32), .{ .UNSHARE = false, .CLOEXEC = false }) == 0
+ else
+ false;
+ if (!shut) {
+ var fd: c_int = 4;
+ while (fd < 1024) : (fd += 1) _ = libc.close(fd);
+ }
+ _ = execvp(argv[0].?, &argv);
+ Fail.say(3, 'x');
+ }
+ _ = libc.close(fds[1]);
+ while (libc.waitpid(first, null, 0) < 0 and libc.errno(-1) == .INTR) {}
+ var said: [5]u8 = undefined;
+ var got: usize = 0;
+ while (got < said.len) {
+ const r = libc.read(fds[0], said[got..].ptr, said.len - got);
+ if (r < 0 and libc.errno(r) == .INTR) continue;
+ if (r <= 0) break;
+ got += @intCast(r);
+ }
+ _ = libc.close(fds[0]);
+ if (got < said.len) return null;
+ const code = std.mem.readInt(u32, said[1..5], .little);
+ const E = libc.E;
+ if (code == @intFromEnum(E.NOENT)) return if (said[0] == 'd') "its directory: no such directory" else "not found";
+ if (code == @intFromEnum(E.ACCES)) return "permission denied";
+ if (code == @intFromEnum(E.NOTDIR)) return "not a directory";
+ if (code == @intFromEnum(E.NOEXEC)) return "not an executable";
+ return if (said[0] == 'd') "its directory: cannot go there" else "cannot start it";
+}
+
+extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
+
/// A terminal pane's shell is done with (its pane gone, its pty closed or at
/// end of file): say hangup, for a shell that ignores the tty's, and see it
/// reaped, killed if it is still there 100 ms on. A thread does the waiting
@@ -3004,6 +3119,8 @@ pub fn writeFile(core: *pardes.Pardes, watches: *file_watch.Table, pane: u8, pat
core.setMessage(pane, pardes.Messages.Message.stamp(&mbuf, "saved", path));
// The init file, saved here (Config): its settings take effect now.
pardes.exec.reapplyInitFile(core, path, bytes);
+ // The plumbing file (Plumb): its rules in force now.
+ pardes.Plumb.saved(core, pane, path, bytes);
}
/// A Dump's write: into the state file under a label of its own
@@ -3863,3 +3980,12 @@ test "LSP query answers, and a newer one cancels it" {
core.update(.{ .lsp_resp = .{ .id = last, .rows = batch.items[0].lsp.rows } });
try std.testing.expect(core.lsp_ask.wait == null);
}
+
+test "a plumbing rule's program starts detached, or says why not: a program not found, a directory not there" {
+ if (comptime builtin.os.tag != .linux) return;
+ var why: [64]u8 = undefined;
+ try std.testing.expect(startDetached(null, "/\x00true\x00", &why) == null);
+ try std.testing.expectEqualStrings("not found", startDetached(null, "/\x00no-such-program-x\x00", &why).?);
+ try std.testing.expectEqualStrings("its directory: no such directory", startDetached(null, "/no/such/dir\x00true\x00", &why).?);
+ try std.testing.expectEqualStrings("malformed", startDetached(null, "/\x00true", &why).?);
+}