summaryrefslogtreecommitdiff
path: root/src/ninep
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep')
-rw-r--r--src/ninep/tree.zig130
1 files changed, 111 insertions, 19 deletions
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index e0cb69b5..8902f11e 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: `<kind> <version> <abi>
/// 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;
}
};
@@ -661,7 +673,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,
@@ -700,8 +712,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);
},
@@ -715,6 +728,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: {
@@ -755,7 +769,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");
@@ -776,6 +790,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;
@@ -845,6 +862,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)
@@ -907,7 +929,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) };
}
@@ -970,6 +992,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) {
@@ -1002,7 +1027,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);
}
@@ -1020,7 +1045,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) };
}
@@ -1058,8 +1083,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;
@@ -1131,7 +1157,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.
@@ -1356,6 +1382,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| {
@@ -1373,6 +1401,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;
@@ -1572,13 +1654,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);