diff options
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/src/main.zig b/src/main.zig index f9118dfc..a07991f5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -108,12 +108,16 @@ const help_text = \\ --detach=<name> ...named <name> rather than this process's pid, \\ as --9p names it. One path component: no '/' \\ and nothing empty - \\ --attach show the one detached session here: this - \\ terminal for `pardes`, a window again for - \\ `pardes-gui`, each of its own kind. Returns when - \\ the session detaches or ends + \\ --attach show the one detached session here (with + \\ none, the one session): in this terminal for + \\ `pardes`, a window for `pardes-gui`. One + \\ attached already gets another view; one of the + \\ other kind (a window's session in a terminal, + \\ or the reverse) is joined as a client, as + \\ --attach=<sock>:<name>. Returns when the + \\ session detaches or ends \\ --attach=<name> ...the session called <name> (its --9p or - \\ --detach name, else its pid) + \\ --detach name, else its pid; see --ps) \\ --attach=<host>:<name> a client of session <name> on another machine, \\ a terminal's or a window's (either kind's \\ session), over `ssh -L` to its pardes-replica.sock @@ -811,7 +815,7 @@ fn nativeMain(std_init: std.process.Init) !void { if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| { remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]); }; - if (attach) |name| if (!resuming) attachTo(init.io, name); + if (attach) |name| if (!resuming) attachTo(init, opts, name); if (detach) |name| { // Said plainly, never a Zig error trace: a name a live session's // socket holds already (a plain session is named by its pid too). @@ -1099,8 +1103,11 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, /// `pardes --attach[=name]` in a terminal: this terminal handed (through /// the daemon) to the detached session of that name, or the only one, and /// this process its stub until the session leaves it. `pardes-gui -/// --attach`: the session asked to open its window again, and done. -fn attachTo(io: std.Io, name: []const u8) noreturn { +/// --attach`: the session asked to open its window again, and done. A +/// session of the other kind is joined as a replica client, through this +/// machine's replica socket. +fn attachTo(init: std.process.Init, opts: pardes.Options, name: []const u8) noreturn { + const io = init.io; var words: [600]u8 = undefined; const Say = struct { fn no(o: std.Io, why: []const u8) noreturn { @@ -1113,6 +1120,7 @@ fn attachTo(io: std.Io, name: []const u8) noreturn { if (pardes.platform == .gui) switch (daemon.attach(.gui, name, &.{}, &words)) { .ok => std.process.exit(0), .no => |why| Say.no(io, why), + .other => |named| crossAttach(init, opts, named), }; const fd = std.c.open("/dev/tty", .{ .ACCMODE = .RDWR, .CLOEXEC = true, .NOCTTY = true }, @as(std.c.mode_t, 0)); if (fd < 0) Say.no(io, "no terminal to attach: /dev/tty will not open"); @@ -1128,9 +1136,25 @@ fn attachTo(io: std.Io, name: []const u8) noreturn { daemon.stubWait(pid, false, pipe[0]); }, .no => |why| Say.no(io, why), + .other => |named| { + _ = std.c.close(fd); + _ = std.c.close(pipe[0]); + _ = std.c.close(pipe[1]); + crossAttach(init, opts, named); + }, } } +/// A session of the other kind (`--attach`'s): a replica client of it here. +fn crossAttach(init: std.process.Init, opts: pardes.Options, name: []const u8) noreturn { + var sock_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const sock = daemon.replicaSocket(&sock_buf) orelse { + std.Io.File.stderr().writeStreamingAll(init.io, "pardes: --attach: no runtime directory for the replica socket\n") catch {}; + std.process.exit(1); + }; + remoteAttach(init, opts, sock, name); +} + fn parseCtrlKey(raw: []const u8) ?u21 { var value = std.mem.trim(u8, raw, " \t\r\n"); if (value.len >= 5 and std.ascii.eqlIgnoreCase(value[0..5], "ctrl-")) { |
