summaryrefslogtreecommitdiff
path: root/src/ninep/tree.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 03:51:15 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit79a1e816ceb13ef81b712007716c8add2de75434 (patch)
tree1714b1bf19da3a58be2623ffe29e3f8be1b21fe3 /src/ninep/tree.zig
parenta22b579ad2df8d1811f0e194fa1bbc06e2282cf8 (diff)
parentf1af015f01e4e8d42f6224c4d4063f3aea218f5a (diff)
downloadpardes-79a1e816ceb13ef81b712007716c8add2de75434.tar.gz
pardes-79a1e816ceb13ef81b712007716c8add2de75434.zip
integration: the terminal a module of its own, with the plumber whole and the walker's cheaper compile
A merge of term-phase1a and pardes-next, no conflicts. core-test (the core with no terminal) 411 passed, 535 skipped of 946; shell-test (the same tests, the terminal compiled in) 989/989, on tui. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/ninep/tree.zig')
-rw-r--r--src/ninep/tree.zig73
1 files changed, 60 insertions, 13 deletions
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index 8902f11e..e5e521c3 100644
--- a/src/ninep/tree.zig
+++ b/src/ninep/tree.zig
@@ -101,6 +101,7 @@ fn truncates(req: Req) bool {
.pane => true,
.col => |c| c.file == .tag,
.top => |f| f == .tag or f == .rules,
+ .port => false,
};
}
@@ -156,6 +157,9 @@ pub const Open = struct {
/// is not whole yet (rule sets with no blank line after them, a
/// message short of its data).
plumb,
+ /// An open reading a plumb port (Plumb.zig's ports, plumber(4)'s):
+ /// the messages sent to it since the open, packed, a read each.
+ port: struct { index: u8, queue: events.Queue = .{} },
} = .{ .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
@@ -212,6 +216,7 @@ pub const Open = struct {
gpa.free(run.line);
},
.frozen => |bytes| gpa.free(bytes),
+ .port => |*port| port.queue.deinit(gpa),
.event, .pty_data, .ctl, .lines, .writes, .pager, .plumb => {},
}
o.pending.deinit(gpa);
@@ -380,6 +385,10 @@ pub const ColFile = enum(u5) {
}
};
const col_bit: u59 = 1 << 58;
+/// plumb/<port>: a port by its place in Plumb's list, which only grows;
+/// past a pane serial's 32 bits and clear of the host tree's and the
+/// archive's high bits (fs.os_node, sources.archive_node).
+const port_bit: u59 = 1 << 40;
pub const Node = packed struct(u64) {
file: u5 = 0,
@@ -394,11 +403,16 @@ pub const Node = packed struct(u64) {
return @bitCast(Node{ .file = @backingInt(file), .serial = col_bit | @as(u59, serial) });
}
+ pub fn ofPort(index: u8) u64 {
+ return @bitCast(Node{ .serial = port_bit | @as(u59, index) });
+ }
+
pub fn target(node: u64) ?Target {
const n: Node = @bitCast(node);
if (n.serial == 0) {
return .{ .top = std.enums.fromInt(TopFile, n.file) orelse return null };
}
+ if (n.serial & port_bit != 0) return .{ .port = std.math.cast(u8, n.serial & ~port_bit) orelse return null };
if (n.serial & col_bit != 0) return .{ .col = .{
.serial = std.math.cast(u32, n.serial & ~col_bit) orelse return null,
.file = std.enums.fromInt(ColFile, n.file) orelse return null,
@@ -414,6 +428,7 @@ pub const Target = union(enum) {
top: TopFile,
pane: struct { serial: u32, file: PaneFile },
col: struct { serial: u32, file: ColFile },
+ port: u8,
};
/// Opening /pane/new makes a pane and reading the open fid answers its
@@ -574,7 +589,7 @@ fn serve(p: *Pardes, req: Req) Reply {
const target = Node.target(req.node) orelse return Reply.fail(req.tag, E.NOENT);
if (req.op == .write or req.op == .setattr) p.fs.origin = switch (target) {
.pane => |t| @as(u8, if (t.file == .body or t.file == .tag) 'E' else 'F'),
- .top, .col => 'F',
+ .top, .col, .port => 'F',
};
return switch (req.op) {
.lookup => lookup(p, req, target),
@@ -596,7 +611,7 @@ fn serve(p: *Pardes, req: Req) Reply {
fn runAttr(p: *Pardes, req: Req, target: Target) ?Reply.Attr {
const t = switch (target) {
.pane => |t| t,
- .top, .col => return null,
+ .top, .col, .port => return null,
};
if (t.file != .pty_run) return null;
const run = if (req.handle != 0) openOf(p, req) else for (&p.fs.opens) |*o| {
@@ -608,6 +623,12 @@ fn runAttr(p: *Pardes, req: Req, target: Target) ?Reply.Attr {
fn attrOf(p: *Pardes, target: Target) ?Reply.Attr {
switch (target) {
+ .port => |i| return .{
+ .name = Plumb.portName(p, i) orelse return null,
+ .node = Node.ofPort(i),
+ .mode = 0o444,
+ .mtime = p.fs.started,
+ },
.top => |f| return .{
.name = f.fileName(),
.node = @backingInt(f),
@@ -704,6 +725,7 @@ fn lookup(p: *Pardes, req: Req, target: Target) Reply {
else => return Reply.fail(req.tag, E.NOTDIR),
},
.col => |c| if (c.file == .dir) @backingInt(TopFile.col) else return Reply.fail(req.tag, E.NOTDIR),
+ .port => return Reply.fail(req.tag, E.NOTDIR),
};
return handle(p, .{ .tag = req.tag, .op = .getattr, .node = parent });
}
@@ -728,9 +750,10 @@ 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),
+ .plumb => if (std.mem.eql(u8, name, "rules")) @backingInt(TopFile.rules) else if (std.mem.eql(u8, name, "send")) @backingInt(TopFile.send) else if (Plumb.portIndex(p, name)) |i| Node.ofPort(i) else return Reply.fail(req.tag, E.NOENT),
else => return Reply.fail(req.tag, E.NOTDIR),
},
+ .port => return Reply.fail(req.tag, E.NOTDIR),
.col => |c| col: {
if (c.file != .dir) return Reply.fail(req.tag, E.NOTDIR);
const f = std.meta.stringToEnum(ColFile, name) orelse return Reply.fail(req.tag, E.NOENT);
@@ -790,8 +813,15 @@ 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());
+ // plumber(4)'s order: rules, send, then the ports as rules
+ // named them.
+ .plumb => {
+ inline for (.{ TopFile.rules, TopFile.send }) |t| {
+ if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, @backingInt(t), false, t.fileName());
+ }
+ for (p.plumber.ports.items, 0..) |name, i| {
+ if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, Node.ofPort(@intCast(i)), false, name);
+ }
},
.col => for (0..p.view().ncol) |c| {
if (skip > 0) {
@@ -812,6 +842,7 @@ fn stageDir(p: *Pardes, out: *std.ArrayList(u8), target: Target, off: u64) error
if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, Node.ofCol(c.serial, f), false, f.fileName());
}
},
+ .port => return error.NotDir,
.pane => |t| {
const id = p.paneBySerial(t.serial) orelse return error.FileNotFound;
const terminal = p.panes[id].?.isTerminal();
@@ -872,6 +903,9 @@ fn open(p: *Pardes, req: Req, target: Target) Reply {
.col => |c| if (layout.columnBySerial(p, c.serial) == null)
return Reply.fail(req.tag, E.NOENT)
else if (linesOpen(target, req)) .lines else if (c.file == .ctl) .{ .frozen = &.{} } else return .{ .tag = req.tag },
+ // A reader of the port: what is sent to it from now on, and what a
+ // `client` rule held for its first.
+ .port => |i| if (req.omode & 3 != 0) return Reply.fail(req.tag, E.PERM) else .{ .port = .{ .index = i } },
.pane => |t| what: {
const pn = p.panes[p.paneBySerial(t.serial) orelse return Reply.fail(req.tag, E.NOENT)].?;
if (t.file.inPty() and !pn.isTerminal()) return Reply.fail(req.tag, E.NOENT);
@@ -929,6 +963,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,
+ .port => |*port| Plumb.opened(p, port.index, &port.queue),
.run, .ctl, .lines, .writes, .pager, .plumb => {},
}
return .{ .tag = req.tag, .handle = @intCast(i + 1) };
@@ -940,7 +975,7 @@ fn remove(p: *Pardes, req: Req) Reply {
const target = Node.target(req.node) orelse return Reply.fail(req.tag, E.NOENT);
const t = switch (target) {
.pane => |t| t,
- .top => return Reply.fail(req.tag, E.PERM),
+ .top, .port => return Reply.fail(req.tag, E.PERM),
.col => |c| return cols.remove(p, req, c.serial, c.file),
};
if (t.file != .dir) return Reply.fail(req.tag, E.PERM);
@@ -1027,7 +1062,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, .plumb => {},
+ .snapshot, .log, .lines, .writes, .frozen, .pager, .plumb, .port => {},
};
o.deinit(p.gpa);
}
@@ -1046,6 +1081,7 @@ fn setattr(p: *Pardes, req: Req, target: Target) Reply {
},
.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) else if (f == .rules) Plumb.clear(p),
+ .port => return Reply.fail(req.tag, E.PERM),
};
return .{ .tag = req.tag, .attr = attrOf(p, target) orelse return Reply.fail(req.tag, E.NOENT) };
}
@@ -1056,6 +1092,8 @@ fn read(p: *Pardes, req: Req, target: Target) Reply {
if (open_record) |o| if (o.what == .pager) ctl.fillPager(p, o.what.pager, &o.pending) catch return Reply.fail(req.tag, E.NOMEM);
// A listing reads what it was at the open.
if (open_record) |o| if (o.what == .frozen) return window(p, req, o.what.frozen);
+ // A port: a message a read, held until one is sent.
+ if (open_record) |o| if (o.what == .port) return events.readQueue(p, req, &o.what.port.queue);
const stream = if (open_record) |o| switch (o.what) {
.log => o,
.event => |reader| if (reader) o else null,
@@ -1182,6 +1220,8 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply {
.exec => ctl.readResults(p, req),
else => Reply.fail(req.tag, E.PERM),
},
+ // Read through its open (read), never without one.
+ .port => return Reply.fail(req.tag, E.PERM),
}
}
@@ -1193,6 +1233,7 @@ fn linesFile(target: Target) bool {
// `name` too: one name, however its writes cut it, applied once
// its newline comes, or with its write when that is whole.
.pane => |t| t.file == .look or t.file == .exec or t.file == .ctl or t.file == .tagexec or t.file == .name or t.file == .rows,
+ .port => false,
};
}
@@ -1202,6 +1243,7 @@ fn resultsFile(target: Target) bool {
.top => |f| f == .look or f == .exec or f == .tagexec or f == .pager,
.col => |c| c.file == .exec,
.pane => |t| t.file == .look or t.file == .exec or t.file == .tagexec,
+ .port => false,
};
}
@@ -1248,6 +1290,7 @@ fn ctlFile(target: Target) bool {
.top => |f| f == .ctl,
.col => |c| c.file == .ctl,
.pane => |t| t.file == .ctl,
+ .port => false,
};
}
@@ -1398,6 +1441,7 @@ fn writeNow(p: *Pardes, req: Req, target: Target) Reply {
.exec => cols.writeExec(p, req, c.serial),
.dir => Reply.fail(req.tag, E.PERM),
},
+ .port => return Reply.fail(req.tag, E.PERM),
}
}
@@ -1408,7 +1452,7 @@ fn writeNow(p: *Pardes, req: Req, target: Target) Reply {
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;
+ const o = openOf(p, req) orelse return if (Plumb.apply(p, true, "plumb/rules", 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}));
@@ -1416,7 +1460,7 @@ fn writeRules(p: *Pardes, req: Req) Reply {
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]);
+ const why = Plumb.apply(p, true, "plumb/rules", 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..]);
@@ -1449,7 +1493,10 @@ fn writeSend(p: *Pardes, req: Req) Reply {
},
.msg => |m| {
defer if (o) |op| op.pending.clearRetainingCapacity();
- if (Plumb.send(p, null, m)) |why| return failText(req.tag, E.IO, why);
+ switch (Plumb.send(p, null, m, null)) {
+ .refused => |why| return failText(req.tag, E.IO, why),
+ .done, .look => {},
+ }
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
},
}
@@ -1668,9 +1715,9 @@ test "readdir lists the root and a pane directory without creating anything" {
@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);
+ // Then the ports the default rules name, in their order.
+ try testing.expectEqual(@as(usize, 4), plumb.len);
+ for ([_][]const u8{ "rules", "send", "web", "edit" }, plumb) |want, got| try testing.expectEqualStrings(want, got.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);