From 65612a1cc13dd2a7e866f88176dbf6db5dd83467 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:41:40 -0300 Subject: plumber: Plan 9's plumber in the core, its language and its files (docs/next-plumber.md, phase 1): src/Plumb.zig owns the rules, kept as their text (Core.plumber, digested and dumped; `include basic`'s, the embedded src/plumbing, until any are written, so a session and a dump from before it start with the defaults) and parsed again for each message, and a matcher over them read from rules.c and match.c: the message's pack and unpack (plumbunpackpartial's whole, short of its data, malformed), packed attributes read and quoted rc's way, rule sets of `object verb arg` lines, variables, include (basic, the configuration directory's, an absolute path, through the taped read site), rc's quoting and `$` expansion at parse and at match, every parse refusal in rules.c's words with its file and line, `$1`-`$9` refused in words (the regular expressions have no capture groups), `matches` over regexp.zig whole (`^(...)$`) or around a click counted in runes with Pike's two patterns, `is`, `set` rewriting for good, `add`, `delete` failing on an attribute not there as match.c's code does, `isfile` and `isdir` finding a name as Look does (past its place, the message's directory, a look's jump list's, Incl's) at the taped resolve sites, once a send; dispose to a port pardes reads (edit: opened as a look opens it, at addr=; web: the host's link opener; a loaded kind by its name), to a rule set's start or client run as exec runs a line (a builtin in place, else a command pane in the message's wdir: Core.exec_dir, exec.runCommand's reuse skipping the sender), or Plan 9's words for what went nowhere; rules print back in the order they were written, a variable where it was set, so a printed text reads back the same; plumb/rules (a write adds rule sets as each is whole, the rest at the close, `>` replaces them, one writer at a time) and plumb/send (a message put together across writes) in the tree, with fs-help's line, the reference's section and tree, and the fstree test's paths; nothing calls the plumber from Look yet; test: Plumb.zig's eleven (every parse error, print and read back, plan9port's basic less its submatch rule sets, its ¡-￿ classes UTF-8's bytes, whole and click matches with horse.gift, quoting, a set's rewrite kept, delete of an absent attribute, dst filtering, isfile and isdir, dispose, a dump keeping the rules), test/behaviour/plumber.zig's two through the tree (Session records the link a host would open); the root's listings in tree.zig's and the process tests gain plumb, and the README folds a blank line to stay 45 lines; the changelog says so MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/ninep/tree.zig | 130 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 111 insertions(+), 19 deletions(-) (limited to 'src/ninep') diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index bca89466..e280ecbb 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -25,6 +25,7 @@ pub const sources = @import("sources.zig"); pub const screen = @import("screen.zig"); const daemon = @import("../daemon.zig"); const panes = @import("../panes.zig"); +const Plumb = @import("../Plumb.zig"); const Pardes = pardes.Pardes; const Pane = pardes.Pane; @@ -99,7 +100,7 @@ fn truncates(req: Req) bool { return switch (Node.target(req.node) orelse return false) { .pane => true, .col => |c| c.file == .tag, - .top => |f| f == .tag, + .top => |f| f == .tag or f == .rules, }; } @@ -151,6 +152,10 @@ pub const Open = struct { /// text after that line held in `pending` until its read puts it /// in (ctl.fillPager), or its close hands it to the editor's step. pager: u32, + /// An open writing plumb/rules or plumb/send: `pending` holds what + /// is not whole yet (rule sets with no blank line after them, a + /// message short of its data). + plumb, } = .{ .snapshot = null }, /// What a write to a file of command lines left unfinished: a last line /// with no newline yet, or an Edit block still open. A write through a @@ -207,7 +212,7 @@ pub const Open = struct { gpa.free(run.line); }, .frozen => |bytes| gpa.free(bytes), - .event, .pty_data, .ctl, .lines, .writes, .pager => {}, + .event, .pty_data, .ctl, .lines, .writes, .pager, .plumb => {}, } o.pending.deinit(gpa); if (o.replay) |r| gpa.free(r.bytes); @@ -269,6 +274,13 @@ pub const TopFile = enum(u5) { /// the addons refused, a line each: ` /// static|loaded|refused: why [path]`. addons, + /// The plumber's (Plumb.zig, Plan 9's plumber(4)): `rules` and `send`. + plumb, + /// plumb/rules: the rules in force, plumb(6)'s; a write adds rule + /// sets, a `>` replaces them. + rules, + /// plumb/send: a write is a message, plumb(6)'s six lines and data. + send, pub fn fileName(f: TopFile) []const u8 { return if (f == .root) "/" else @tagName(f); @@ -276,15 +288,15 @@ pub const TopFile = enum(u5) { pub fn mode(f: TopFile) u16 { return switch (f) { - .root, .pane, .col => 0o755, - .look, .exec, .log, .focus, .ctl, .tag, .tagexec, .pager => 0o666, + .root, .pane, .col, .plumb => 0o755, + .look, .exec, .log, .focus, .ctl, .tag, .tagexec, .pager, .rules => 0o666, .README, .index, .status, .screen, .listeners, .new, .commands, .layout, .recent, .dumps, .sessions, .addons => 0o444, - .kbdin, .mousein => 0o222, + .kbdin, .mousein, .send => 0o222, }; } pub fn dir(f: TopFile) bool { - return f == .root or f == .pane or f == .col; + return f == .root or f == .pane or f == .col or f == .plumb; } }; @@ -658,7 +670,7 @@ fn topSize(p: *Pardes, f: TopFile) u64 { // frame, /pane/new until an open has a pane to name, and the index, // layout, recent list and listeners, which a stat would otherwise // have to build to count. - .root, .pane, .col, .screen, .new, .layout, .index, .recent, .dumps, .addons, .listeners, .sessions, .kbdin, .mousein => 0, + .root, .pane, .col, .screen, .new, .layout, .index, .recent, .dumps, .addons, .listeners, .sessions, .kbdin, .mousein, .plumb, .rules, .send => 0, .tag => cols.headerText(p, null).?.len, .README => fs.help.len, .status => if (ctl.statusText(p)) |text| text.len else |_| 0, @@ -697,8 +709,9 @@ fn lookup(p: *Pardes, req: Req, target: Target) Reply { .top => |f| switch (f) { .root => root: { if (std.mem.eql(u8, name, "os")) return fs.osHandle(p, .{ .tag = req.tag, .op = .getattr, .node = fs.os_root }); - // `new` is reached in /pane, where the panes it makes are. - if (std.meta.stringToEnum(TopFile, name)) |t| if (t != .root and t != .new) break :root @backingInt(t); + // `new` is reached in /pane, where the panes it makes are, + // `rules` and `send` in /plumb. + if (std.meta.stringToEnum(TopFile, name)) |t| if (t != .root and t != .new and t != .rules and t != .send) break :root @backingInt(t); if (sources.node(name)) |n| return sources.handle(p, .{ .tag = req.tag, .op = .getattr, .node = n }); return Reply.fail(req.tag, E.NOENT); }, @@ -712,6 +725,7 @@ fn lookup(p: *Pardes, req: Req, target: Target) Reply { if (serial == 0) return Reply.fail(req.tag, E.NOENT); break :dir if (f == .pane) Node.of(serial, .dir) else Node.ofCol(serial, .dir); }, + .plumb => if (std.mem.eql(u8, name, "rules")) @backingInt(TopFile.rules) else if (std.mem.eql(u8, name, "send")) @backingInt(TopFile.send) else return Reply.fail(req.tag, E.NOENT), else => return Reply.fail(req.tag, E.NOTDIR), }, .col => |c| col: { @@ -752,7 +766,7 @@ fn stageDir(p: *Pardes, out: *std.ArrayList(u8), target: Target, off: u64) error .root => { // The guide, then what describes the session, then the // files that act, then the directories. - inline for (.{ TopFile.README, TopFile.index, TopFile.status, TopFile.focus, TopFile.ctl, TopFile.commands, TopFile.recent, TopFile.dumps, TopFile.addons, TopFile.look, TopFile.exec, TopFile.pager, TopFile.log, TopFile.screen, TopFile.listeners, TopFile.sessions, TopFile.layout, TopFile.tag, TopFile.tagexec, TopFile.kbdin, TopFile.mousein, TopFile.pane, TopFile.col }) |t| { + inline for (.{ TopFile.README, TopFile.index, TopFile.status, TopFile.focus, TopFile.ctl, TopFile.commands, TopFile.recent, TopFile.dumps, TopFile.addons, TopFile.look, TopFile.exec, TopFile.pager, TopFile.log, TopFile.screen, TopFile.listeners, TopFile.sessions, TopFile.layout, TopFile.tag, TopFile.tagexec, TopFile.kbdin, TopFile.mousein, TopFile.plumb, TopFile.pane, TopFile.col }) |t| { if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, @backingInt(t), t.dir(), t.fileName()); } if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, fs.os_root, true, "os"); @@ -773,6 +787,9 @@ fn stageDir(p: *Pardes, out: *std.ArrayList(u8), target: Target, off: u64) error try stageDirent(out, p.gpa, Node.of(serial, .dir), true, name); } }, + .plumb => inline for (.{ TopFile.rules, TopFile.send }) |t| { + if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, @backingInt(t), false, t.fileName()); + }, .col => for (0..p.view().ncol) |c| { if (skip > 0) { skip -= 1; @@ -842,6 +859,11 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { .index, .layout, .recent, .dumps, .addons, .commands, .status, .listeners, .sessions => .{ .frozen = &.{} }, // The root's ctl opened only to read: its settings, frozen. .ctl => if (linesOpen(target, req)) .lines else .{ .frozen = &.{} }, + // One writer of the rules at a time, as plumber(4)'s. + .rules, .send => if (req.omode & 3 == 0) return .{ .tag = req.tag } else plumb: { + if (f == .rules) for (p.fs.opens) |o| if (o.node == req.node and o.what == .plumb) return failText(req.tag, E.BUSY, e_in_use); + break :plumb .plumb; + }, else => if (linesOpen(target, req)) .lines else return .{ .tag = req.tag }, }, .col => |c| if (layout.columnBySerial(p, c.serial) == null) @@ -904,7 +926,7 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { pn.fs.event_reader = pn.fs.event_reader or reader; }, .pty_data => pane_of.?.fs.pty_reader = true, - .run, .ctl, .lines, .writes, .pager => {}, + .run, .ctl, .lines, .writes, .pager, .plumb => {}, } return .{ .tag = req.tag, .handle = @intCast(i + 1) }; } @@ -967,6 +989,9 @@ fn releaseHandle(p: *Pardes, req: Req) void { if (t == .col and t.col.file == .tag) cols.released(p, t.col.serial); if (t == .top and t.top == .tag) cols.released(p, null); const o = openOf(p, req) orelse return; + // A message short of its data is dropped with its open, as plumber(4) + // drops a fid's writebuf; rule text left goes in at the editor's step. + if (o.what == .plumb and req.node == @backingInt(TopFile.send)) o.pending.clearRetainingCapacity(); // A /pager open's text, not read for, goes in at the editor's next // step (ctl.fillClosedPagers): this close need not wait for quiet. if (o.what == .pager and o.pending.items.len > 0) { @@ -999,7 +1024,7 @@ fn releaseHandle(p: *Pardes, req: Req) void { .ctl => if (pn.fs.lock == req.handle) { pn.fs.lock = null; }, - .snapshot, .log, .lines, .writes, .frozen, .pager => {}, + .snapshot, .log, .lines, .writes, .frozen, .pager, .plumb => {}, }; o.deinit(p.gpa); } @@ -1017,7 +1042,7 @@ fn setattr(p: *Pardes, req: Req, target: Target) Reply { if (pane.truncate(p, p.panes[id].?, t.file) != .ok) return Reply.fail(req.tag, E.NOMEM); }, .col => |c| if (c.file == .tag and cols.truncate(p, c.serial) != .ok) return Reply.fail(req.tag, E.NOENT), - .top => |f| if (f == .tag and cols.truncate(p, null) != .ok) return Reply.fail(req.tag, E.NOMEM), + .top => |f| if (f == .tag and cols.truncate(p, null) != .ok) return Reply.fail(req.tag, E.NOMEM) else if (f == .rules) Plumb.clear(p), }; return .{ .tag = req.tag, .attr = attrOf(p, target) orelse return Reply.fail(req.tag, E.NOENT) }; } @@ -1055,8 +1080,9 @@ fn read(p: *Pardes, req: Req, target: Target) Reply { fn readFile(p: *Pardes, req: Req, target: Target) Reply { switch (target) { .top => |f| return switch (f) { - .kbdin, .mousein => Reply.fail(req.tag, E.PERM), + .kbdin, .mousein, .send => Reply.fail(req.tag, E.PERM), .README => window(p, req, fs.help), + .rules => window(p, req, Plumb.inForce(p)), .index => index: { const out = p.fs.stage(p.gpa); var last: u32 = 0; @@ -1128,7 +1154,7 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply { break :serial Reply.fail(req.tag, E.NOMEM); break :serial stagedReply(p, req); }, - .root, .pane, .col => Reply.fail(req.tag, E.PERM), + .root, .pane, .col, .plumb => Reply.fail(req.tag, E.PERM), }, .pane => |t| { // A run's answer outlives the pane it ran in. @@ -1350,6 +1376,8 @@ fn writeNow(p: *Pardes, req: Req, target: Target) Reply { .pager => ctl.writePager(p, req), .tag => cols.write(p, req, null), .tagexec => cols.writeExec(p, req, null), + .rules => writeRules(p, req), + .send => writeSend(p, req), else => Reply.fail(req.tag, E.PERM), }, .pane => |t| { @@ -1367,6 +1395,60 @@ fn writeNow(p: *Pardes, req: Req, target: Target) Reply { } } +/// plumb/rules: rule text added to the rules in force a rule set at a time, +/// as each is whole (a blank line after it, rules.c's morerules), the rest +/// at the open's close; a refusal is the write's, in plumb(6)'s words, the +/// rules as they were and the text it held dropped. +fn writeRules(p: *Pardes, req: Req) Reply { + const done: Reply = .{ .tag = req.tag, .written = @intCast(req.data.len) }; + // A close's rest (runClosedLines), or a write with no open: whole. + const o = openOf(p, req) orelse return if (Plumb.apply(p, true, req.data)) |why| failText(req.tag, E.INVAL, why) else done; + if (o.pending.items.len + req.data.len > Plumb.max_text) { + o.pending.clearRetainingCapacity(); + return failText(req.tag, E.INVAL, std.fmt.comptimePrint("rules too long; max {d} KiB", .{Plumb.max_text / 1024})); + } + o.pending.appendSlice(p.gpa, req.data) catch return Reply.fail(req.tag, E.NOMEM); + const end = Plumb.wholeSets(o.pending.items); + if (end == 0) return done; + const why = Plumb.apply(p, true, o.pending.items[0..end]); + if (why != null) o.pending.clearRetainingCapacity() else { + const rest = o.pending.items.len - end; + std.mem.copyForwards(u8, o.pending.items[0..rest], o.pending.items[end..]); + o.pending.shrinkRetainingCapacity(rest); + } + return if (why) |w| failText(req.tag, E.INVAL, w) else done; +} + +/// plumb/send: a message, its writes put together until its data is all +/// there (plumber(4)), then matched and sent: the write is answered when it +/// has gone where the rules say, or with why not, in Plan 9's words. +fn writeSend(p: *Pardes, req: Req) Reply { + const o = openOf(p, req); + const held = if (o) |op| op.pending.items.len > 0 else false; + if (held) o.?.pending.appendSlice(p.gpa, req.data) catch return Reply.fail(req.tag, E.NOMEM); + defer if (o) |op| if (op.pending.items.len > pending_cap) op.pending.clearRetainingCapacity(); + switch (Plumb.unpack(if (held) o.?.pending.items else req.data)) { + .bad => { + if (o) |op| op.pending.clearRetainingCapacity(); + return failText(req.tag, E.INVAL, Plumb.e_bad_msg); + }, + .more => |n| { + const op = o orelse return failText(req.tag, E.INVAL, Plumb.e_bad_msg); + if (op.pending.items.len + n > pending_cap) { + op.pending.clearRetainingCapacity(); + return failText(req.tag, E.INVAL, Plumb.e_bad_msg ++ ": over 1 MiB"); + } + if (!held) op.pending.appendSlice(p.gpa, req.data) catch return Reply.fail(req.tag, E.NOMEM); + return .{ .tag = req.tag, .written = @intCast(req.data.len) }; + }, + .msg => |m| { + defer if (o) |op| op.pending.clearRetainingCapacity(); + if (Plumb.send(p, null, m)) |why| return failText(req.tag, E.IO, why); + return .{ .tag = req.tag, .written = @intCast(req.data.len) }; + }, + } +} + // ---- tests ---- const testing = std.testing; @@ -1566,13 +1648,23 @@ test "readdir lists the root and a pane directory without creating anything" { @memcpy(top_bytes[0..top_reply.bytes.len], top_reply.bytes); const top = th.dirents(top_bytes[0..top_reply.bytes.len], &buf); try testing.expect(top.len >= 10); - for ([_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "addons", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" }, 0..) |name, i| { + for ([_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "addons", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "plumb", "pane", "col", "os" }, 0..) |name, i| { try testing.expectEqualStrings(name, top[i].name); try testing.expectEqual(i >= 21, top[i].dir); } - try testing.expectEqual(@backingInt(TopFile.pane), top[21].node); - try testing.expectEqual(@backingInt(TopFile.col), top[22].node); - try testing.expectEqual(fs.os_root, top[23].node); + try testing.expectEqual(@backingInt(TopFile.plumb), top[21].node); + try testing.expectEqual(@backingInt(TopFile.pane), top[22].node); + try testing.expectEqual(@backingInt(TopFile.col), top[23].node); + try testing.expectEqual(fs.os_root, top[24].node); + // The plumber's directory: its rules, then send. + const plumb_reply = rdir(p, @backingInt(TopFile.plumb), 0); + var plumb_bytes: [256]u8 = undefined; + @memcpy(plumb_bytes[0..plumb_reply.bytes.len], plumb_reply.bytes); + var plumb_buf: [4]th.Dirent = undefined; + const plumb = th.dirents(plumb_bytes[0..plumb_reply.bytes.len], &plumb_buf); + try testing.expectEqual(@as(usize, 2), plumb.len); + try testing.expectEqualStrings("rules", plumb[0].name); + try testing.expectEqualStrings("send", plumb[1].name); var idbuf: [16]u8 = undefined; try testing.expect(th.nameAt(top, try std.fmt.bufPrint(&idbuf, "{d}", .{serial})) == null); try testing.expect(th.nameAt(top, "self") == null); -- cgit v1.3