diff options
Diffstat (limited to 'runtime/main.zig')
| -rw-r--r-- | runtime/main.zig | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/runtime/main.zig b/runtime/main.zig index 448dfa57..563461ed 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -1595,7 +1595,6 @@ fn nativeMain(std_init: std.process.Init) !void { } } defer daemon.quit(); - errdefer daemon.tellStubExit(1); switch (pardes.platform) { .tui => @import("dev").tui.run(init, opts, term) catch |err| switch (err) { // No controlling terminal (a detached pty, a daemon): said, not @@ -1609,7 +1608,6 @@ fn nativeMain(std_init: std.process.Init) !void { .gui => try @import("dev").gui.run(init, opts, detached), .macos => unreachable, } - daemon.tellStubExit(0); } extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; @@ -1803,50 +1801,61 @@ fn attachClient(init: std.process.Init, opts: pardes.Options, where: []const u8, std.process.exit(0); } -/// `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. A -/// session attached already, or of the other kind, is joined as a replica -/// client, through this machine's replica socket. +/// `pardes attach [NAME]` (`pardes-gui` too): a client of the session of +/// that name on this machine, through its replica socket (the daemon +/// refuses a name no session has); bare, of the one `bareAttach` finds. fn attachTo(init: std.process.Init, opts: pardes.Options, name: []const u8) noreturn { const io = init.io; - var words: [600]u8 = undefined; if (comptime !daemon.supported) die(io, "--attach: this system has no pardes daemon to attach through", .{}); - if (pardes.platform == .gui) switch (daemon.attach(.gui, name, &.{}, &words)) { - .ok => std.process.exit(0), - .no => |why| die(io, "--attach: {s}", .{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) die(io, "--attach: no terminal to attach: /dev/tty will not open", .{}); - if (std.posix.tcgetattr(fd)) |tio| std.posix.tcsetattr(fd, .NOW, tio) catch {} else |_| {} - var pipe: [2]std.c.fd_t = undefined; - if (std.c.pipe(&pipe) != 0) die(io, "--attach: no pipe for the session to say when it leaves", .{}); - ninep_io.setCloexec(pipe[0]); - ninep_io.setCloexec(pipe[1]); - switch (daemon.attach(.tty, name, &.{ fd, pipe[1] }, &words)) { - .ok => |pid| { - _ = std.c.close(fd); - _ = std.c.close(pipe[1]); - daemon.stubWait(pid, false, pipe[0]); - }, - .no => |why| die(io, "--attach: {s}", .{why}), - .other => |named| { - _ = std.c.close(fd); - _ = std.c.close(pipe[0]); - _ = std.c.close(pipe[1]); - crossAttach(init, opts, named); - }, + var list: [4096]u8 = undefined; + var names_buf: [600]u8 = undefined; + var names: std.Io.Writer = .fixed(&names_buf); + const said = daemon.ask("list", &list) orelse die(io, "--attach: no pardes daemon is running", .{}); + const named = if (name.len != 0) name else bareAttach(said, &names) orelse if (names.end == 0) + die(io, "--attach: no session is running", .{}) + else + die(io, "--attach: say which session:{s}", .{names.buffered()}); + var sock_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const sock = daemon.replicaSocket(&sock_buf) orelse die(io, "--attach: no runtime directory for the replica socket", .{}); + attachClient(init, opts, sock, named); +} + +/// The session a bare attach takes, from the daemon's `list`: the one +/// detached, else the one there is; null with none or several, every +/// session's name in `names`. +fn bareAttach(list: []const u8, names: *std.Io.Writer) ?[]const u8 { + var one: ?[]const u8 = null; + var detached: ?[]const u8 = null; + var n: usize = 0; + var n_detached: usize = 0; + var lines = std.mem.tokenizeScalar(u8, list, '\n'); + while (lines.next()) |line| { + var words = std.mem.tokenizeScalar(u8, line, ' '); + if (!std.mem.eql(u8, words.next() orelse "", "session")) continue; + _ = words.next(); // its pid + _ = words.next(); // its kind + const state = words.next() orelse continue; + one = std.mem.trim(u8, words.rest(), " "); + n += 1; + names.print(" {s}", .{one.?}) catch {}; // cut short + if (!std.mem.eql(u8, state, "detached")) continue; + detached = one; + n_detached += 1; } + if (n_detached == 1) return detached; + return if (n == 1) one else null; } -/// A session attached already or 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 die(init.io, "--attach: no runtime directory for the replica socket", .{}); - attachClient(init, opts, sock, name); +test "a bare attach takes the one detached session, else the one there is, else names them" { + var buf: [600]u8 = undefined; + var names: std.Io.Writer = .fixed(&buf); + try std.testing.expectEqualStrings("b", bareAttach("daemon 1\nsession 2 tty attached a\nshell 9\nsession 3 gui detached b\n", &names).?); + try std.testing.expectEqualStrings("a", bareAttach("daemon 1\nsession 2 tty attached a\n", &names).?); + names = .fixed(&buf); + try std.testing.expect(bareAttach("daemon 1\norphan 4\n", &names) == null); + try std.testing.expectEqual(@as(usize, 0), names.end); + try std.testing.expect(bareAttach("session 2 tty detached a\nsession 3 tty detached b c\n", &names) == null); + try std.testing.expectEqualStrings(" a b c", names.buffered()); } /// Where Recent's file (0.24) and the store before it were kept: |
