diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:52:39 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 00:58:29 -0300 |
| commit | 1c7fd4c48f61695f396765c2f273b560a2e19963 (patch) | |
| tree | 4b76acd5b742fb2d7cf90dc3c03d6ac26dbbcf2a | |
| parent | f88ed53afea4fb38bf0b78ecbfbd49edab6c252b (diff) | |
| download | pardes-1c7fd4c48f61695f396765c2f273b560a2e19963.tar.gz pardes-1c7fd4c48f61695f396765c2f273b560a2e19963.zip | |
ninep ctl: a control message's refusal is one refuseQuoting with a tail, so refuseTo and the column ctl's two hand-built refusals, each cut at its own length, go; a captured run's failure is one failedWrite, said three times before; get returns getRefused's words, already in ename, where it copied them out and back; a constant was formatted into a throwaway buffer; writeSettingsOf had no caller; the tests' no-op spawn host is testing.zig's starting, not four copies
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/ninep/cols.zig | 6 | ||||
| -rw-r--r-- | src/ninep/ctl.zig | 104 | ||||
| -rw-r--r-- | src/ninep/events.zig | 6 | ||||
| -rw-r--r-- | src/ninep/testing.zig | 6 |
4 files changed, 49 insertions, 73 deletions
diff --git a/src/ninep/cols.zig b/src/ninep/cols.zig index 1445a3c3..d74c044d 100644 --- a/src/ninep/cols.zig +++ b/src/ninep/cols.zig @@ -164,10 +164,10 @@ pub fn writeCtl(p: *Pardes, req: Req, serial: u32) Reply { } else false; // Plan 9's words for it, so a mount's errno is EINVAL as the root's // and a pane's ctl give (9ns reads "control message"). - if (!takes) return tree.failText(req.tag, E.INVAL, std.fmt.bufPrint(&p.fs.ename, "unknown control message \"{s}\": a column's ctl takes Delcol, Joincol, New or Term", .{pardes.ctlfs.ctl.firstWord(line)[0..@min(pardes.ctlfs.ctl.firstWord(line).len, 64)]}) catch "unknown control message"); + if (!takes) return pardes.ctlfs.ctl.refuseQuoting(p, req, "unknown control message", word, ": a column's ctl takes Delcol, Joincol, New or Term"); // Only Term takes a word (its shell); `New extra` is no New. if (!std.mem.eql(u8, word, "Term") and word.len != line.len) - return tree.failText(req.tag, E.INVAL, std.fmt.bufPrint(&p.fs.ename, "wrong #args in control message \"{s}\"", .{pardes.ctlfs.ctl.firstWord(line)[0..@min(pardes.ctlfs.ctl.firstWord(line).len, 64)]}) catch "wrong #args in control message"); + return pardes.ctlfs.ctl.refuseQuoting(p, req, "wrong #args in control message", word, ""); const col = layout.columnBySerial(p, serial) orelse return Reply.fail(req.tag, E.NOENT); p.exec_column = col; defer p.exec_column = null; @@ -197,7 +197,7 @@ pub fn writeExec(p: *Pardes, req: Req, serial: ?u32) Reply { // pane has the keyboard. The tag's own words (New, Term, Find, Grep // in a column's) are its to run. if (paneWord(line, if (serial == null) pardes.config.topbar_str else tagline.column_tag_default)) - return pardes.ctlfs.ctl.refuseTo(p, req, "not a session control message", line, "pane/<n>/ctl"); + return pardes.ctlfs.ctl.refuseQuoting(p, req, "not a session control message", pardes.ctlfs.ctl.firstWord(line), ": write it to pane/<n>/ctl"); const col = if (serial) |s| layout.columnBySerial(p, s) orelse return Reply.fail(req.tag, E.NOENT) else null; // With no pane at all, a word runs at the stand-in, as a click in // the empty window's tag does (runBuiltinFrom). diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 30ba8bf7..53753b42 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -308,12 +308,19 @@ pub fn captured(p: *Pardes, req: Req, id: usize, exec: bool, text: []const u8, i p.fs.failure_len = 0; defer p.fs.capturing = false; run(p, id, exec, text, in_tag); - if (p.fs.failure_len == 0) return null; + const refusal = failedWrite(p, req.tag) orelse return null; // A failed click answers nothing: look reads back empty. p.fs.results_len = 0; + return refusal; +} + +/// The failure a run said while it was captured, as the refusal of the +/// write that ran it: its words, and the errno 9ns reads in them (a slot or +/// a place refused keeps its, no space: ENOSPC); null when it said none. +pub fn failedWrite(p: *Pardes, tag: u64) ?Reply { + if (p.fs.failure_len == 0) return null; const failure = p.fs.failure[0..p.fs.failure_len]; - // A slot or a place refused keeps its errno words (no space: ENOSPC). - return tree.failText(req.tag, failureErrno(failure), std.fmt.bufPrint(&p.fs.ename, "{s}", .{failure}) catch failure); + return tree.failText(tag, failureErrno(failure), std.fmt.bufPrint(&p.fs.ename, "{s}", .{failure}) catch failure); } /// The errno a builtin's failure answers with: ENOENT for what is not @@ -452,16 +459,18 @@ const Builtin = builtins.registry.Builtin(); /// Refuses a control message, quoting it the way Plan 9's cmderror does /// (kernel/misc/parse.c:82): `unknown control message "Bogus 3"`. fn refuse(p: *Pardes, req: Req, why: []const u8, line: []const u8) Reply { - return refuseQuoting(p, req, why, firstWord(line)); + return refuseQuoting(p, req, why, firstWord(line), ""); } -/// `refuse` quoting `word` as given: a setting's bad value, blanks and all. -fn refuseQuoting(p: *Pardes, req: Req, why: []const u8, word: []const u8) Reply { +/// `refuse` quoting `word` as given (a setting's bad value, blanks and +/// all), then `tail`: which ctl takes the message instead, or what this one +/// takes. +pub fn refuseQuoting(p: *Pardes, req: Req, why: []const u8, word: []const u8, tail: []const u8) Reply { // A word too long to quote whole gives up its middle (fitErr), so the // refusal keeps both its ends. var shown: [4096]u8 = undefined; var whole: [4096 + 320]u8 = undefined; - const full = std.fmt.bufPrint(&whole, "{s} \"{s}\"", .{ why, blanked(word[0..@min(word.len, shown.len)], &shown) }) catch why; + const full = std.fmt.bufPrint(&whole, "{s} \"{s}\"{s}", .{ why, blanked(word[0..@min(word.len, shown.len)], &shown), tail }) catch why; // Into the session's own buffer: `whole` is this frame's. if (full.len <= p.fs.ename.len) { @memcpy(p.fs.ename[0..full.len], full); @@ -478,14 +487,6 @@ pub fn firstWord(line: []const u8) []const u8 { return trimmed[0 .. std.mem.indexOfAny(u8, trimmed, " \t") orelse trimmed.len]; } -/// `refuse`, saying which ctl takes the message instead. -pub fn refuseTo(p: *Pardes, req: Req, why: []const u8, line: []const u8, ctl: []const u8) Reply { - const room = p.fs.ename.len -| (why.len + ctl.len + 18); - var shown: [320]u8 = undefined; - const text = std.fmt.bufPrint(&p.fs.ename, "{s} \"{s}\": write it to {s}", .{ why, blanked(firstWord(line)[0..@min(firstWord(line).len, room)], &shown), ctl }) catch why; - return tree.failText(req.tag, E.INVAL, text); -} - /// A quoted line as a refusal shows it: a control byte (a tab, the one a /// line may hold) is a blank, so the reason reads as one line of words. fn blanked(line: []const u8, buf: []u8) []const u8 { @@ -535,12 +536,12 @@ fn checkBuiltin(p: *Pardes, req: Req, line: []const u8, scope: builtins.Scope) ? }; // Edit is the one pane word the root takes too: at the active pane. if (builtins.registry.scope(b) != scope and b != .Edit) - return refuseTo(p, req, if (scope == .pane) "not a window control message" else "not a session control message", line, if (scope == .pane) - "/ctl" + return refuseQuoting(p, req, if (scope == .pane) "not a window control message" else "not a session control message", firstWord(line), if (scope == .pane) + ": write it to /ctl" else if (b == .Delcol or b == .Joincol) - "col/<serial>/ctl" // a column's own ctl, the one /layout names + ": write it to col/<serial>/ctl" // a column's own ctl, the one /layout names else - "pane/<n>/ctl"); + ": write it to pane/<n>/ctl"); if (arg.len > 0 and !builtins.registry.takesArg(b)) return refuse(p, req, "wrong #args in control message", line); if (arg.len == 0 and builtins.registry.requiresArg(b)) return refuse(p, req, "wrong #args in control message", line); const setting = config.Runtime.find(word) orelse return null; @@ -572,7 +573,7 @@ fn checkBuiltin(p: *Pardes, req: Req, line: []const u8, scope: builtins.Scope) ? return refuseQuoting(p, req, if (config.Runtime.takes(setting.action)) |values| std.fmt.bufPrint(&why, "bad value in control message; takes {s}", .{values}) catch "bad value in control message" else - "bad value in control message", arg); + "bad value in control message", arg, ""); } /// Runs a checked control message at pane `id`, as a click on its word @@ -597,14 +598,12 @@ pub fn runBuiltin(p: *Pardes, req: Req, id: usize, line: []const u8) ?Reply { }; if (!created) noteResult(p, context); if (p.fs.refused) return refuse(p, req, e_prompt, line); - if (p.fs.failure_len == 0) return null; // A line that ran and failed says why in the builtin's own words, which // name what failed (`Mount peer /tmp/s: dial failed: ...`): the line is // not quoted after them, as an exec's is not. Only a line refused before // it runs, as no message at all, quotes it (`unknown control message // "X"`). - const failure = p.fs.failure[0..p.fs.failure_len]; - return tree.failText(req.tag, failureErrno(failure), std.fmt.bufPrint(&p.fs.ename, "{s}", .{failure}) catch failure); + return failedWrite(p, req.tag); } const e_prompt = "control message needs its argument"; @@ -623,13 +622,8 @@ fn writeSettings(p: *Pardes, w: *std.Io.Writer) !void { if (comptime builtins.Pet.enabled) if (builtins.registry.here(p, .Pet)) try w.print("Pet {s}\n", .{@tagName(p.settings.pet)}); } -/// The setting words of `s`, one a line, as the root ctl reads them; a Dump -/// keeps those that differ from a fresh session's (dump.zig). -pub fn writeSettingsOf(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer) !void { - return writeSettingsFor(p, s, w, builtins.capabilities); -} - -/// ...those a frontend of `caps` takes (the root ctl's: the live view's). +/// The setting words of `s`, one a line, those a frontend of `caps` takes +/// (the root ctl's: the live view's). fn writeSettingsFor(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer, caps: config.Runtime.Capabilities) !void { // DumpConfig's words: a `# Word unsupported` comment for what `caps` lacks. var dump_dir: [1024]u8 = undefined; @@ -744,10 +738,10 @@ pub fn writeRoot(p: *Pardes, req: Req) Reply { if (std.mem.startsWith(u8, line, "size ") or std.mem.eql(u8, line, "size")) { // The offending word is the value: `size 5 5` quotes "5 5". const value = std.mem.trim(u8, line["size".len..], " \t"); - const size = sizeOf(line) orelse return refuseQuoting(p, req, e_size, value); + const size = sizeOf(line) orelse return refuseQuoting(p, req, e_size, value, ""); // A frontend's window is the size; only a session none is // attached to (--detach, unattended) takes one written. - if (!p.unattended) return refuseQuoting(p, req, "size: a frontend is attached and owns the size", value); + if (!p.unattended) return refuseQuoting(p, req, "size: a frontend is attached and owns the size", value, ""); if (apply) { const was: [2]u16 = .{ p.screen_w, p.screen_h }; const before = p.rects; @@ -760,7 +754,7 @@ pub fn writeRoot(p: *Pardes, req: Req) Reply { if (!pardes.layout.minimaFit(p)) { p.update(.{ .resize = .{ .cols = was[0], .rows = was[1] } }); p.sync(); - return refuseQuoting(p, req, "size: too small for the panes, each its tag and 2 rows", value); + return refuseQuoting(p, req, "size: too small for the panes, each its tag and 2 rows", value, ""); } pardes.layout.restoreMinima(p, &before); p.sync(); @@ -775,9 +769,9 @@ pub fn writeRoot(p: *Pardes, req: Req) Reply { if (!apply) continue; // ponytail: read on the 9P task, a small local file; a big // one would want pardes.turn's yield. - const bytes = fs.readFile(p.gpa, path) catch |err| return refuseQuoting(p, req, @errorName(err), path); + const bytes = fs.readFile(p.gpa, path) catch |err| return refuseQuoting(p, req, @errorName(err), path, ""); defer p.gpa.free(bytes); - _ = pardes.Handover.adopt(p, bytes) catch |err| return refuseQuoting(p, req, @errorName(err), path); + _ = pardes.Handover.adopt(p, bytes) catch |err| return refuseQuoting(p, req, @errorName(err), path, ""); continue; } if (!apply) { @@ -1084,15 +1078,9 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply { if (std.mem.eql(u8, line, "get")) { if (!apply) continue; if (@import("../edit_cmd.zig").busyOn(p, p.paneBySerial(serial).?)) return tree.failText(req.tag, E.BUSY, "get: " ++ @import("../edit_cmd.zig").e_busy); - // Quoting the line, as a builtin's refusal (Save's) does. - if (getRefused(p, pane, "get")) |said_in_ename| { - // refuse writes ename, where the words are: copy them out. - var said_buf: [320]u8 = undefined; - const said = said_buf[0..said_in_ename.len]; - @memcpy(said, said_in_ename); - // It ran and was refused: its words, not the line quoted. - return tree.failText(req.tag, E.IO, std.fmt.bufPrint(&p.fs.ename, "{s}", .{said}) catch said); - } + // It ran and was refused: its words (in ename), not the line + // quoted, as a builtin's refusal (Save's) says it. + if (getRefused(p, pane, "get")) |said| return tree.failText(req.tag, E.IO, said); var failed: anyerror = error.Unexpected; const errno = get(p, pane, &failed); // Naming the path, in words 9ns reads as the errno; a peer @@ -1117,10 +1105,8 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply { } else if (std.mem.eql(u8, line, "answer")) { // With a question up, what it offers. var offered: [64]u8 = undefined; - if (p.questionChoices(p.paneBySerial(serial).?, &offered)) |choices| { - var why: [96]u8 = undefined; - return tree.failText(req.tag, E.INVAL, std.fmt.bufPrint(&p.fs.ename, "answer takes {s} or -", .{choices}) catch std.fmt.bufPrint(&why, "answer takes a choice or -", .{}) catch "answer takes a choice or -"); - } + if (p.questionChoices(p.paneBySerial(serial).?, &offered)) |choices| + return tree.failText(req.tag, E.INVAL, std.fmt.bufPrint(&p.fs.ename, "answer takes {s} or -", .{choices}) catch "answer takes a choice or -"); return tree.failText(req.tag, E.INVAL, "answer takes a choice or -"); } else if (std.mem.startsWith(u8, line, "answer ") and p.panes[p.paneBySerial(serial).?].?.prompt == .save) { // Save's path, asked over 9P (`ask <serial> save path`). @@ -1143,7 +1129,7 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply { if (!std.mem.eql(u8, choice, "-") and (choice.len != 1 or std.mem.indexOfScalar(u8, choices, choice[0]) == null)) { var why: [96]u8 = undefined; // The choice refused is what is quoted: `answer takes a b or - "z"`. - return refuseQuoting(p, req, std.fmt.bufPrint(&why, "answer takes {s} or -", .{choices}) catch "answer takes a choice or -", choice); + return refuseQuoting(p, req, std.fmt.bufPrint(&why, "answer takes {s} or -", .{choices}) catch "answer takes a choice or -", choice, ""); } if (apply) _ = p.answerQuestion(id, if (choice[0] == '-') null else choice[0]); } else if (!apply) { @@ -1908,10 +1894,7 @@ test "Term in a directory that is not there fails and makes no pane; a shell tha _ = wr(p, Node.of(serial, .name), "/tmp/pardes-no-such-dir-zz/f.txt\n"); // A host that starts shells (the core alone starts none, and asks // nothing of a directory). - const Starting = struct { - fn spawn(_: ?*anyopaque, _: u8, _: []const u8) void {} - }; - p.host = .{ .vtable = &.{ .spawn = Starting.spawn } }; + p.host = .{ .vtable = &th.starting }; defer p.host = .{}; var count: usize = 0; for (p.panes) |slot| count += @intFromBool(slot != null); @@ -2047,10 +2030,7 @@ test "a terminal with text typed at its prompt is never typed into: a look at it test "a command line from a missing directory fails ENOENT and makes no pane; a command whose shell never started ends 127" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); - const Starting = struct { - fn spawn(_: ?*anyopaque, _: u8, _: []const u8) void {} - }; - p.host = .{ .vtable = &.{ .spawn = Starting.spawn } }; + p.host = .{ .vtable = &th.starting }; defer p.host = .{}; const serial = serialOf(p); _ = wr(p, Node.of(serial, .name), "/tmp/pardes-no-such-dir-zz/f.txt\n"); @@ -2078,10 +2058,7 @@ test "a command line from a missing directory fails ENOENT and makes no pane; a test "pty/ctl exec in a directory that is gone fails ENOENT; a shell that cannot start fails the write" { const p = try th.withTerm(testing.allocator); defer p.deinit(); - const Starting = struct { - fn spawn(_: ?*anyopaque, _: u8, _: []const u8) void {} - }; - p.host = .{ .vtable = &.{ .spawn = Starting.spawn } }; + p.host = .{ .vtable = &th.starting }; defer p.host = .{}; const serial = serialOf(p); p.setCwd(p.paneBySerial(serial).?, "/tmp/pardes-no-such-dir-zz"); @@ -2097,10 +2074,7 @@ test "pty/ctl exec in a directory that is gone fails ENOENT; a shell that cannot test "a script whose interpreter is not there: Term refuses it up front, only an err logged, and pty/ctl exec keeps the running shell" { const p = try th.withTerm(testing.allocator); defer p.deinit(); - const Starting = struct { - fn spawn(_: ?*anyopaque, _: u8, _: []const u8) void {} - }; - p.host = .{ .vtable = &.{ .spawn = Starting.spawn } }; + p.host = .{ .vtable = &th.starting }; defer p.host = .{}; var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); diff --git a/src/ninep/events.zig b/src/ninep/events.zig index 68f9f3e7..bc43dbfd 100644 --- a/src/ninep/events.zig +++ b/src/ninep/events.zig @@ -865,11 +865,7 @@ pub fn writeEvent(p: *Pardes, asked: Req, id: usize) Reply { else => unreachable, } } - if (p.fs.failure_len > 0) { - const failure = p.fs.failure[0..p.fs.failure_len]; - return tree.failText(req.tag, ctl.failureErrno(failure), std.fmt.bufPrint(&p.fs.ename, "{s}", .{failure}) catch failure); - } - return .{ .tag = req.tag, .written = @intCast(asked.data.len) }; + return ctl.failedWrite(p, req.tag) orelse .{ .tag = req.tag, .written = @intCast(asked.data.len) }; } // ---- tests ---- diff --git a/src/ninep/testing.zig b/src/ninep/testing.zig index ec665130..14aeffea 100644 --- a/src/ninep/testing.zig +++ b/src/ninep/testing.zig @@ -148,6 +148,12 @@ pub fn nameAt(list: []const Dirent, want: []const u8) ?Dirent { return null; } +/// A host that starts shells and does nothing else: the core alone starts +/// none. +pub const starting: pardes.Host.VTable = .{ .spawn = startNothing }; + +fn startNothing(_: ?*anyopaque, _: u8, _: []const u8) void {} + pub const FakeTty = struct { taken: bool, |
