diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:51:05 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 09:54:56 -0300 |
| commit | b0a02deff23a852e6fb3d803ce85256949419af8 (patch) | |
| tree | bccb65741cfdeca2fac169a8702c3af9fe183042 /src | |
| parent | 67c738adfb7261cdf65faf042622b1c723a456a5 (diff) | |
| download | pardes-b0a02deff23a852e6fb3d803ce85256949419af8.tar.gz pardes-b0a02deff23a852e6fb3d803ce85256949419af8.zip | |
main: every refusal on stderr (pardes: <words>, exit 1) is one die(), where badArgs, three Say structs, Refuse.with and four inline copies each formatted and wrote it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.zig | 100 |
1 files changed, 31 insertions, 69 deletions
diff --git a/src/main.zig b/src/main.zig index 2b50273c..4b0d5220 100644 --- a/src/main.zig +++ b/src/main.zig @@ -307,9 +307,13 @@ test "--wait finds the pane /index names a file by, spaces and all, and sees it } fn badArgs(io: std.Io, comptime fmt: []const u8, args: anytype) noreturn { - var buf: [1024]u8 = undefined; - const line = std.fmt.bufPrint(&buf, "pardes: " ++ fmt ++ "\nTry 'pardes --help'.\n", args) catch - "pardes: bad arguments\nTry 'pardes --help'.\n"; + die(io, fmt ++ "\nTry 'pardes --help'.", args); +} + +/// `pardes: <words>` on stderr, and exit 1. +fn die(io: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var buf: [8192]u8 = undefined; + const line = std.fmt.bufPrint(&buf, "pardes: " ++ fmt ++ "\n", args) catch "pardes: refused\n"; std.Io.File.stderr().writeStreamingAll(io, line) catch {}; std.process.exit(1); } @@ -624,22 +628,13 @@ fn nativeMain(std_init: std.process.Init) !void { ninep_io.Client.probe(arena, parent.dial) catch { // `--wait` is someone's EDITOR waiting on that session: a screen // of its own drawn in their terminal is no answer. Said, exit 1. - if (wait) { - var buf: [4300]u8 = undefined; - const text = std.fmt.bufPrint(&buf, "pardes: --wait: the session at {s} does not answer; it is gone\n", .{parent.dial[0..@min(parent.dial.len, 4096)]}) catch "pardes: --wait: the session does not answer\n"; - std.Io.File.stderr().writeStreamingAll(init.io, text) catch {}; - std.process.exit(1); - } + if (wait) die(init.io, "--wait: the session at {s} does not answer; it is gone", .{parent.dial[0..@min(parent.dial.len, 4096)]}); break :forwarding; }; const Refuse = struct { fn with(io: std.Io, what: []const u8, err: anyerror) noreturn { var words_buf: [128]u8 = undefined; - const why = if (err == error.Remote) ninep_io.Client.lastSaid() else forwardWords(err, &words_buf); - var buf: [8192]u8 = undefined; - const text = std.fmt.bufPrint(&buf, "pardes: {s}: {s}\n", .{ what[0..@min(what.len, 4096)], why }) catch "pardes: refused\n"; - std.Io.File.stderr().writeStreamingAll(io, text) catch {}; - std.process.exit(1); + die(io, "{s}: {s}", .{ what[0..@min(what.len, 4096)], if (err == error.Remote) ninep_io.Client.lastSaid() else forwardWords(err, &words_buf) }); } }; // The pane's `look` file: one line, and the line is the clicked text @@ -750,12 +745,7 @@ fn nativeMain(std_init: std.process.Init) !void { const why = why_buf[0..@min(said.len, why_buf.len)]; @memcpy(why, said[0..why.len]); ninep_io.Client.write(arena, parent.dial, ctl, "delete\n") catch {}; - if (err == error.Remote) { - var buf: [8192]u8 = undefined; - const text = std.fmt.bufPrint(&buf, "pardes: {s}: {s}\n", .{ word[0..@min(word.len, 4096)], why }) catch "pardes: refused\n"; - std.Io.File.stderr().writeStreamingAll(init.io, text) catch {}; - std.process.exit(1); - } + if (err == error.Remote) die(init.io, "{s}: {s}", .{ word[0..@min(word.len, 4096)], why }); Refuse.with(init.io, word, err); }; } @@ -819,11 +809,7 @@ fn nativeMain(std_init: std.process.Init) !void { 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). - if (name.len != 0 and !resuming and ninep_io.taken(name)) { - var said: [512]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(init.io, std.fmt.bufPrint(&said, "pardes: a session named {s} is already running\n", .{name}) catch "pardes: a session of that name is already running\n") catch {}; - std.process.exit(1); - } + if (name.len != 0 and !resuming and ninep_io.taken(name)) die(init.io, "a session named {s} is already running", .{name}); if (opts.ninep_name.len == 0) opts.ninep_name = name; // Nobody's size yet: one roomy enough for a script's panes; `size` // sets another, and an attached terminal or window its own. @@ -899,22 +885,15 @@ extern "c" fn atexit(f: *const fn () callconv(.c) void) c_int; /// and `--kill-all`: those sessions ended with their shells, a TERM and, /// for one still there 2 s later, a KILL. fn manage(io: std.Io, verb: []const u8, name: []const u8) noreturn { - const Say = struct { - fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { - var buf: [700]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: " ++ fmt ++ "\n", args) catch "pardes: failed\n") catch {}; - std.process.exit(1); - } - }; - if (comptime !daemon.supported) Say.no(io, "this system has no pardes daemon", .{}); + if (comptime !daemon.supported) die(io, "this system has no pardes daemon", .{}); var words_buf: [300]u8 = undefined; const words = if (std.mem.eql(u8, verb, "kill")) - std.fmt.bufPrint(&words_buf, "kill {s}", .{name}) catch Say.no(io, "that name is too long", .{}) + std.fmt.bufPrint(&words_buf, "kill {s}", .{name}) catch die(io, "that name is too long", .{}) else verb; var buf: [4096]u8 = undefined; - const said = daemon.ask(words, &buf) orelse Say.no(io, "no pardes daemon is running", .{}); - if (std.mem.startsWith(u8, said, "no ")) Say.no(io, "{s}", .{said[3..]}); + const said = daemon.ask(words, &buf) orelse die(io, "no pardes daemon is running", .{}); + if (std.mem.startsWith(u8, said, "no ")) die(io, "{s}", .{said[3..]}); if (std.mem.eql(u8, verb, "list")) { var out: [8192]u8 = undefined; var w: std.Io.Writer = .fixed(&out); @@ -975,7 +954,7 @@ fn manage(io: std.Io, verb: []const u8, name: []const u8) noreturn { }; if (!Gone.all(pids[0..n], 2000)) { for (pids[0..n]) |p| _ = std.c.kill(p, .KILL); - if (!Gone.all(pids[0..n], 1000)) Say.no(io, "a session would not end, even killed", .{}); + if (!Gone.all(pids[0..n], 1000)) die(io, "a session would not end, even killed", .{}); } std.process.exit(0); } @@ -1009,29 +988,22 @@ fn clientOf(init: std.process.Init, opts: pardes.Options, path: []const u8, name fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, name: []const u8) noreturn { const io = init.io; if (comptime pardes.platform != .tui and pardes.platform != .gui) unreachable; - const Say = struct { - fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { - var buf: [700]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {}; - std.process.exit(1); - } - }; if (std.mem.indexOfScalar(u8, where, '/') != null) { - clientOf(init, opts, where, name, false) catch |err| Say.no(io, "{t}", .{err}); + clientOf(init, opts, where, name, false) catch |err| die(io, "--attach: {t}", .{err}); std.process.exit(0); } // ssh -L <here>:<there> <host>, until the socket here answers. var here_buf: [ninep_io.sun_path_len:0]u8 = undefined; const tmp = std.mem.span(std.c.getenv("TMPDIR") orelse "/tmp"); - const here = std.fmt.bufPrintSentinel(&here_buf, "{s}/pardes-ssh-{d}.sock", .{ tmp, std.c.getpid() }, 0) catch Say.no(io, "TMPDIR is too long for a socket's path", .{}); + const here = std.fmt.bufPrintSentinel(&here_buf, "{s}/pardes-ssh-{d}.sock", .{ tmp, std.c.getpid() }, 0) catch die(io, "--attach: TMPDIR is too long for a socket's path", .{}); var there_buf: [ninep_io.sun_path_len + 64:0]u8 = undefined; var dir_buf: [ninep_io.sun_path_len:0]u8 = undefined; - const dir = if (std.c.getenv("PARDES_REMOTE_RUNTIME")) |d| std.mem.span(d) else ninep_io.socketDir(&dir_buf) orelse Say.no(io, "no runtime directory to name the remote socket by: set PARDES_REMOTE_RUNTIME", .{}); + const dir = if (std.c.getenv("PARDES_REMOTE_RUNTIME")) |d| std.mem.span(d) else ninep_io.socketDir(&dir_buf) orelse die(io, "--attach: no runtime directory to name the remote socket by: set PARDES_REMOTE_RUNTIME", .{}); var forward_buf: [2 * ninep_io.sun_path_len + 64]u8 = undefined; const there = std.fmt.bufPrintSentinel(&there_buf, "{s}/pardes-replica.sock", .{dir}, 0) catch unreachable; const forward = std.fmt.bufPrintSentinel(&forward_buf, "{s}:{s}", .{ here, there }, 0) catch unreachable; var host_buf: [256]u8 = undefined; - const host = std.fmt.bufPrintSentinel(&host_buf, "{s}", .{where}, 0) catch Say.no(io, "that host's name is too long", .{}); + const host = std.fmt.bufPrintSentinel(&host_buf, "{s}", .{where}, 0) catch die(io, "--attach: that host's name is too long", .{}); var argv: [48:null]?[*:0]const u8 = @splat(null); var n: usize = 0; var words_buf: [1024]u8 = undefined; @@ -1043,7 +1015,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, var words = std.mem.tokenizeScalar(u8, std.mem.span(std.c.getenv("PARDES_SSH") orelse "ssh"), ' '); var at: usize = 0; while (words.next()) |w| { - if (n >= 40 or at + w.len + 1 > words_buf.len) Say.no(io, "PARDES_SSH has too many words", .{}); + if (n >= 40 or at + w.len + 1 > words_buf.len) die(io, "--attach: PARDES_SSH has too many words", .{}); @memcpy(words_buf[at..][0..w.len], w); words_buf[at + w.len] = 0; argv[n] = @ptrCast(&words_buf[at]); @@ -1059,7 +1031,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, } _ = std.c.unlink(here); const pid = std.c.fork(); - if (pid < 0) Say.no(io, "no fork for ssh", .{}); + if (pid < 0) die(io, "--attach: no fork for ssh", .{}); if (pid == 0) { // A client killed outright takes its ssh with it. daemon.hupOnStubDeath(true); @@ -1090,13 +1062,13 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, var status: c_int = 0; if (std.c.waitpid(pid, &status, std.c.W.NOHANG) == pid) { Ssh.child = 0; - Say.no(io, "ssh to {s} ended before its forward was up", .{where}); + die(io, "--attach: ssh to {s} ended before its forward was up", .{where}); } - if (waited > 3000) Say.no(io, "ssh to {s} set up no forward in 30 s", .{where}); + if (waited > 3000) die(io, "--attach: ssh to {s} set up no forward in 30 s", .{where}); const ts: std.c.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms }; _ = std.c.nanosleep(&ts, null); } - clientOf(init, opts, here, name, true) catch |err| Say.no(io, "{t}", .{err}); + clientOf(init, opts, here, name, true) catch |err| die(io, "--attach: {t}", .{err}); std.process.exit(0); } @@ -1109,24 +1081,17 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, 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 { - var buf: [700]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: --attach: {s}\n", .{why}) catch "pardes: --attach failed\n") catch {}; - std.process.exit(1); - } - }; - if (comptime !daemon.supported) Say.no(io, "this system has no pardes daemon to attach through"); + 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| Say.no(io, why), + .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) Say.no(io, "no terminal to attach: /dev/tty will not open"); + 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) Say.no(io, "no pipe for the session to say when it leaves"); + 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)) { @@ -1135,7 +1100,7 @@ fn attachTo(init: std.process.Init, opts: pardes.Options, name: []const u8) nore _ = std.c.close(pipe[1]); daemon.stubWait(pid, false, pipe[0]); }, - .no => |why| Say.no(io, why), + .no => |why| die(io, "--attach: {s}", .{why}), .other => |named| { _ = std.c.close(fd); _ = std.c.close(pipe[0]); @@ -1148,10 +1113,7 @@ fn attachTo(init: std.process.Init, opts: pardes.Options, name: []const u8) nore /// 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); - }; + const sock = daemon.replicaSocket(&sock_buf) orelse die(init.io, "--attach: no runtime directory for the replica socket", .{}); remoteAttach(init, opts, sock, name); } |
