From 15bd47670102ceae36565ee299fd8a40625c3b21 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 31 Oct 2026 21:12:16 -0300 Subject: daemon: --attach= to a session of the other kind (pardes in a terminal for a window's session, pardes-gui for a terminal's) joins it as a replica client through this machine's pardes-replica.sock, where it was refused in words; the daemon answers `other ` (a named one, or with none of the client's kind the one session there is) and the client goes on as --attach=:; --help says attach takes an attached session too; test/daemon.py's window test attaches a terminal to the window session by name (the old build refused it, 'is a window') Co-Authored-By: Claude Opus 5.5 --- src/daemon.zig | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/daemon.zig') diff --git a/src/daemon.zig b/src/daemon.zig index b93f5d43..2a70c772 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -473,7 +473,15 @@ pub fn quit() void { // ---- `pardes --attach`: a terminal, or a window, for a detached session ---- -pub const Answer = union(enum) { ok: libc.pid_t, no: []const u8 }; +/// `other`: the session is the other kind (a window's for a terminal, or a +/// terminal's for a window), named so: joined as a replica client instead +/// (`replicaSocket`). +pub const Answer = union(enum) { ok: libc.pid_t, no: []const u8, other: []const u8 }; + +/// This machine's replica socket, which joins a session of either kind. +pub fn replicaSocket(buf: *[sun_path_len:0]u8) ?[:0]const u8 { + return place(buf, "pardes-replica.sock"); +} /// Asks the daemon to hand `fds` (a terminal and a stub's pipe, or none for /// a window) to the detached session `named` (any one, when empty), and @@ -490,6 +498,7 @@ pub fn attach(k: Kind, named: []const u8, fds: []const c_int, buf: []u8) Answer const pid = std.fmt.parseInt(libc.pid_t, got.words[3..], 10) catch return .{ .no = "the daemon answered nonsense" }; return .{ .ok = pid }; } + if (std.mem.startsWith(u8, got.words, "other ")) return .{ .other = got.words["other ".len..] }; return .{ .no = if (std.mem.startsWith(u8, got.words, "no ")) got.words[3..] else got.words }; } @@ -860,7 +869,8 @@ const Daemon = struct { const wanted_name = std.mem.trim(u8, words.rest(), " "); var why: [512]u8 = undefined; const target = d.find(k, wanted_name, &why) catch |err| return d.reply(i, switch (err) { - error.Said => std.mem.sliceTo(&why, 0), + // "other ": the client joins it through the replica socket. + error.Said, error.Other => std.mem.sliceTo(&why, 0), }, &.{}); const t = &d.conns[target]; if (!send(t.fd, "attach", got.fds[0..got.nfds])) { @@ -875,7 +885,8 @@ const Daemon = struct { /// The detached session of kind `k` called `wanted_name`, or the one /// there is when it is empty; else why not, in `why` (0-terminated). - fn find(d: *Daemon, k: Kind, wanted_name: []const u8, why: *[512]u8) error{Said}!u8 { + /// One of the other kind is error.Other, `why` "other ". + fn find(d: *Daemon, k: Kind, wanted_name: []const u8, why: *[512]u8) error{ Said, Other }!u8 { @memset(why, 0); const other: []const u8 = switch (k) { .tty => "a window: attach it with `pardes-gui --attach", @@ -884,8 +895,8 @@ const Daemon = struct { if (wanted_name.len != 0) { for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.kind != null and std.mem.eql(u8, s.named(), wanted_name)) { if (s.kind.? != k) { - _ = std.fmt.bufPrint(why, "no '{s}' is {s}={s}`", .{ wanted_name, other, wanted_name }) catch {}; - return error.Said; + _ = std.fmt.bufPrint(why, "other {s}", .{wanted_name}) catch {}; + return error.Other; } // Attached already: the session takes another view. return @intCast(i); @@ -914,6 +925,17 @@ const Daemon = struct { }; if (count == 1) return found.?; count = 0; + // None of this kind at all: the one session there is, the other kind's. + var only: ?*Conn = null; + var sessions: usize = 0; + for (&d.conns) |*s| if (s.fd >= 0 and s.kind != null) { + sessions += 1; + only = s; + }; + if (found == null and sessions == 1) { + _ = std.fmt.bufPrint(why, "other {s}", .{only.?.named()}) catch {}; + return error.Other; + } } if (count == 0 and others == 0) { w.writeAll("no no detached session is running") catch {}; -- cgit v1.3