diff options
Diffstat (limited to 'src/ninep')
| -rw-r--r-- | src/ninep/ctl.zig | 12 | ||||
| -rw-r--r-- | src/ninep/events.zig | 19 | ||||
| -rw-r--r-- | src/ninep/pane.zig | 93 | ||||
| -rw-r--r-- | src/ninep/pty.zig | 4 | ||||
| -rw-r--r-- | src/ninep/tree.zig | 144 |
5 files changed, 224 insertions, 48 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 2fa24173..c4947504 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -718,17 +718,7 @@ pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfM panes.File.setContent(p, f, page.text); f.pager_spans = page.spans; f.saved_revision = f.revision; - // A diff paged (`git show`, difftui's patches): its hunks stacked in a - // +Diff in the +Pager's place, the +Pager closed. The keyboard stays - // where it was, as a page's. - if (f.pager_diff) { - const keyboard = p.view().active; - if (panes.Output.openDiff(p, id, f.path, f.content)) |hid| { - // ponytail: the text is gone with it; Diff on the +Diff says no such file. - p.removePane(id, hid) catch |err| p.reportError(id, "pager", err); - } else |_| {} - if (p.panes[keyboard] != null) p.view().active = keyboard; - } + if (f.pager_diff) @import("../hunks.zig").paged(p, id); } /// The editor's step: the text of /pager opens closed unread goes in. diff --git a/src/ninep/events.zig b/src/ninep/events.zig index 77a4e35f..dd9b488c 100644 --- a/src/ninep/events.zig +++ b/src/ninep/events.zig @@ -677,22 +677,21 @@ fn scanNumber(data: []const u8, i: *usize, overflow: *bool) ?u32 { /// What a click at `at` takes, as the mouse's does (exec.operandAt), /// for an empty range written back: the file name or word under it on its -/// line (look.expandedWord), else nothing; in a diff pane's body, for a -/// look alone (`for_look`, exec.operandAt), look.diffClick's answer, the -/// line's address or its code's words. +/// line (look.expandedWord), else nothing; in a body whose rows name +/// addresses, for a look alone (`for_look`, exec.operandAt), +/// look.rowAddressAt's answer, the row's address or its code's words. pub fn expandAt(p: *Pardes, pane: *Pane, whole: []const u8, at: usize, in_body: bool, for_look: bool) []const u8 { const start = if (std.mem.lastIndexOfScalar(u8, whole[0..at], '\n')) |nl| nl + 1 else 0; const end = std.mem.indexOfScalarPos(u8, whole, at, '\n') orelse whole.len; var word_from: usize = 0; - if (in_body and for_look) diff: { + if (in_body and for_look) row_address: { const row = std.mem.count(u8, whole[0..start], "\n"); - // Only when the served line is the one the diff reads there. - const lines = look.diffLines(p, pane) orelse break :diff; - if (row >= lines.len or !std.mem.eql(u8, std.mem.trimEnd(u8, lines[row], "\r"), std.mem.trimEnd(u8, whole[start..end], "\r"))) break :diff; var buf: [4096 + 32]u8 = undefined; - const click = look.diffClick(p, pane, row, at - start, &buf) orelse break :diff; - if (click.address) |address| return p.scratch.allocator().dupe(u8, address) catch ""; - word_from = click.word_from; + const got = look.rowAddressAt(p, pane, row, at - start, &buf) orelse break :row_address; + // Only when the served line is the one the address was read from. + if (!std.mem.eql(u8, std.mem.trimEnd(u8, got.line, "\r"), std.mem.trimEnd(u8, whole[start..end], "\r"))) break :row_address; + if (got.address) |address| return p.scratch.allocator().dupe(u8, address) catch ""; + word_from = got.word_from; } const line = whole[start..end]; if (at - start < word_from or word_from > line.len) return ""; diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index 13a15206..6c86053f 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -12,6 +12,7 @@ const modal = @import("../modal.zig"); const config = @import("../config.zig"); const fs = @import("../fs.zig"); const tree = @import("tree.zig"); +const locations = @import("../locations.zig"); const events = @import("events.zig"); const addressing = @import("addr.zig"); const ctl = @import("ctl.zig"); @@ -319,8 +320,8 @@ pub fn fileSize(p: *Pardes, id: usize, f: PaneFile) u64 { // A terminal's body reads its screen and history, as text. .body => if (pane.file == null and pane.isTerminal()) pardes.Term.scrollbackLen(pane.terminal) else if (kindText(p, pane)) |t| t.len else bodyOf(pane).len, // What addr selects, generated by each read: no length, as acme's - // data and xdata have none. - .data, .xdata => 0, + // data and xdata have none; and rows, made by each read. + .data, .xdata, .rows => 0, .tag => if (tagOf(p, pane)) |tag| tag.len else |_| 0, // a stat's length is a hint: none when the tag cannot be made .name => nameOf(p, pane).len + 1, .ctl => ctl: { @@ -396,6 +397,12 @@ pub fn read(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply .mark => readFlag(p, req, !pf.nomark), .scroll => readFlag(p, req, !pf.noscroll), .look, .exec, .tagexec => ctl.readResults(p, req), + .rows => rows: { + const f = fileOf(pane) orelse break :rows tree.window(p, req, ""); + var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); + locations.formatRows(f.location_rows, &text.writer) catch break :rows Reply.fail(req.tag, E.NOMEM); + break :rows tree.window(p, req, text.written()); + }, .event => events.readQueue(p, req, &pf.events), .pty_status => pty.readStatus(p, req, id, pane), .pty_data => pty.readData(p, req, pf), @@ -498,6 +505,7 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply // A click in its tag: an event reader holding the pane hears it as // an `x` record (ctl.run), as acme's tag clicks are. .tagexec => ctl.command(p, req, pane.serial, true, true), + .rows => writeRows(p, req, pane), .event => events.writeEvent(p, req, id), .errors => if (appendErrors(p, id, req.data)) |took| .{ .tag = req.tag, .written = @intCast(took) } @@ -510,6 +518,36 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply }; } +pub const e_rows_line = "invalid rows: a row is a line, ending in a newline"; +pub const e_rows_text = "invalid rows: a line is no row (the README's rows line says what one is)"; +pub const e_rows_fit = "invalid rows: more rows than the text has lines, or one past its line's end"; + +/// Rows, whole lines of them (a write through a mount is cut anywhere: the +/// rest of a line waits for the open's next write, tree.writeLines): at +/// offset 0 they replace the pane's, at a later one they follow those it +/// holds. Parsed (locations.parseRows) and handed to File.setRows, which +/// refuses what the text cannot hold. +fn writeRows(p: *Pardes, req: Req, pane: *Pane) Reply { + const f = fileOf(pane) orelse return Reply.fail(req.tag, E.PERM); + if (req.data.len == 0) return .{ .tag = req.tag, .written = 0 }; + if (req.data[req.data.len - 1] != '\n') return tree.failText(req.tag, E.INVAL, e_rows_line); + const arena = p.scratch.allocator(); + const written = locations.parseRows(arena, req.data) catch |err| return switch (err) { + error.OutOfMemory => Reply.fail(req.tag, E.NOMEM), + error.BadRows => tree.failText(req.tag, E.INVAL, e_rows_text), + }; + const kept = if (req.off == 0) 0 else f.location_rows.len; + const all = arena.alloc(locations.Row, kept + written.len) catch return Reply.fail(req.tag, E.NOMEM); + @memcpy(all[0..kept], f.location_rows[0..kept]); + @memcpy(all[kept..], written); + const rows = locations.cloneRows(p.gpa, all) catch return Reply.fail(req.tag, E.NOMEM); + panes.File.setRows(p, f, rows) catch { + locations.freeRows(p.gpa, rows); + return tree.failText(req.tag, E.INVAL, e_rows_fit); + }; + return .{ .tag = req.tag, .written = @intCast(req.data.len) }; +} + /// spliceBody for a write of the open `handle`, in the undo step its open /// is making: it joins the step being made only when it is the same open's /// (or the step is anyone's, `joined_by` 0), another open's starting its @@ -981,6 +1019,8 @@ pub fn truncate(p: *Pardes, pane: *Pane, file: PaneFile) tree.Status { pf.limit = null; }, .dot => pf.dot_truncated = fileOf(pane) != null, + // `>` starts the rows over, as a write at 0 does. + .rows => if (fileOf(pane)) |f| panes.File.setRows(p, f, &.{}) catch unreachable, else => {}, } return .ok; @@ -1248,3 +1288,52 @@ test "index names a command's pane cmd, a shell's term" { const shell = try p.newShell(p.freeSlot().?, "/tmp"); try testing.expectEqualStrings("term", kindOf(shell)); } + +test "rows: a pane's rows read as lines, written whole or across writes, refused when its text cannot hold them" { + const gpa = testing.allocator; + const p = try withFile(gpa, "a.txt:3 one\n two\n"); + defer p.deinit(); + const rows = Node.of(serialOf(p), .rows); + try testing.expectEqualStrings("", rd(p, rows, 0, 4096).bytes); + const text = "m 3 0 0 0 0 7 8 3:0+3=keyword.add a.txt\ncd 0 0 0 0 0 0 0 - \n"; + try testing.expectEqual(Status.ok, wr(p, rows, text).reply.status); + try testing.expectEqualStrings(text, rd(p, rows, 0, 4096).bytes); + const f = &p.panes[0].?.file.?; + try testing.expectEqual(@as(usize, 2), f.location_rows.len); + try testing.expectEqualStrings("a.txt", f.location_rows[0].path); + try testing.expect(f.location_rows[1].declaration and f.location_rows[1].kind == .context); + // A write at 0 starts them over; one open's writes, cut mid-line, + // make one set of rows, its later writes following its first. + const h = call(p, .{ .tag = 1, .op = .open, .node = rows, .omode = 1 }).reply.handle; + try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = rows, .handle = h, .off = 0, .data = "m 3 0 0 0 0 7 8 - a.t" }).reply.status); + try testing.expectEqual(Status.ok, call(p, .{ .tag = 3, .op = .write, .node = rows, .handle = h, .off = 21, .data = "xt\np 4 0 0 0 0 0 0 - a.txt\n" }).reply.status); + _ = call(p, .{ .tag = 4, .op = .release, .node = rows, .handle = h }); + try testing.expectEqualStrings("m 3 0 0 0 0 7 8 - a.txt\np 4 0 0 0 0 0 0 - a.txt\n", rd(p, rows, 0, 4096).bytes); + // More rows than lines, an offset or styles past a line's end, a line + // that is no row: refused, the rows as they were. + for ([_][]const u8{ "m 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\n", "m 1 0 0 0 0 12 0 - a\n", "m 1 0 0 0 0 0 8 9:0+1=add a\n", "q 1 0 0 0 0 0 0 - a\n", "m 1 0 0 0 0 0 0 - a" }) |bad| + try testing.expectEqual(Status.err, wr(p, rows, bad).reply.status); + try testing.expectEqual(@as(usize, 2), f.location_rows.len); + // `>` truncates: no rows. + _ = call(p, .{ .tag = 5, .op = .setattr, .node = rows, .truncate = true }); + try testing.expectEqualStrings("", rd(p, rows, 0, 4096).bytes); + // An edit drops them. + try testing.expectEqual(Status.ok, wr(p, rows, text).reply.status); + try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .body), "more\n").reply.status); + try testing.expectEqual(@as(usize, 0), f.location_rows.len); +} + +test "rows: a +Search's rows read back as the addresses its text shows" { + const gpa = testing.allocator; + const p = try withFile(gpa, "x\n"); + defer p.deinit(); + try panes.Output.fillResults(p, 0, "/tmp", .search, "foo", try p.gpa.dupe(u8, "a.txt:1:1 foo\nb.txt:2:3 foo\n"), null); + const serial = for (p.panes) |slot| { + const pane = slot orelse continue; + const f = pane.file orelse continue; + if (f.output != null and f.location_rows.len > 0) break pane.serial; + } else return error.NoSearch; + const text = rd(p, Node.of(serial, .rows), 0, 4096).bytes; + try testing.expect(std.mem.indexOf(u8, text, "m 1 1 ") == 0); + try testing.expect(std.mem.indexOf(u8, text, " a.txt\n") != null and std.mem.indexOf(u8, text, "\nm 2 3 ") != null and std.mem.endsWith(u8, text, " b.txt\n")); +} diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig index 40ae179c..6e7fe33e 100644 --- a/src/ninep/pty.zig +++ b/src/ninep/pty.zig @@ -507,7 +507,7 @@ test "a pane that is not a terminal has no pty/ at all" { var buf: [32]th.Dirent = undefined; const files = th.dirents(rdir(p, dir, 0).bytes, &buf); - try testing.expectEqual(@as(usize, 18), files.len); + try testing.expectEqual(@as(usize, 19), files.len); try testing.expect(th.nameAt(files, "pty") == null); try testing.expectEqual(E.NOENT, look_up(p, dir, "pty_ctl").errno()); @@ -528,7 +528,7 @@ test "a terminal pane's pty/ holds exactly ctl, status and data" { var buf: [32]th.Dirent = undefined; const files = th.dirents(rdir(p, dir, 0).bytes, &buf); - try testing.expectEqual(@as(usize, 19), files.len); + try testing.expectEqual(@as(usize, 20), files.len); try testing.expect(th.nameAt(files, "pty").?.dir); const inside = th.dirents(rdir(p, Node.of(serial, .pty), 0).bytes, &buf); diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index bca89466..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; } }; @@ -313,6 +325,9 @@ pub const PaneFile = enum(u5) { /// A word run as a click in the pane's tag, as /tagexec is in the /// workspace's and col/<n>/exec in a column's. tagexec, + /// A line a body row: where it points and how it is coloured + /// (locations.formatRows), a results pane's; written, the pane's. + rows, pty, pty_ctl, pty_status, @@ -658,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, @@ -697,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); }, @@ -712,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: { @@ -752,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"); @@ -773,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; @@ -842,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) @@ -904,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) }; } @@ -967,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) { @@ -999,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); } @@ -1017,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) }; } @@ -1055,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; @@ -1128,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. @@ -1163,7 +1192,7 @@ fn linesFile(target: Target) bool { .col => |c| c.file == .ctl or c.file == .exec, // `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, + .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, }; } @@ -1326,6 +1355,9 @@ fn writeLines(p: *Pardes, req: Req, target: Target) Reply { o.pending.shrinkRetainingCapacity(rest); var w = req; w.data = whole; + // At the offset its first byte was written at, an earlier write's + // held line included (a rows write at 0 starts the rows over). + w.off = (req.off + (req.data.len - data.len)) -| (end + rest - data.len); // The command pane this open was answered holds no lease against its // own next command (exec.leased): that one may run there again. if (resultsFile(target)) o.results.len = 0; @@ -1350,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| { @@ -1367,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; @@ -1566,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); @@ -1611,10 +1709,10 @@ test "readdir lists the root and a pane directory without creating anything" { const dir = rdir(p, Node.of(serial, .dir), 0); const files = th.dirents(dir.bytes, &buf); - try testing.expectEqual(@as(usize, 18), files.len); + try testing.expectEqual(@as(usize, 19), files.len); for ([_][]const u8{ - "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", - "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", + "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", + "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "rows", }, 0..) |name, i| try testing.expectEqualStrings(name, files[i].name); try testing.expect(th.nameAt(files, ".") == null); |
