summaryrefslogtreecommitdiff
path: root/src/ninep
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep')
-rw-r--r--src/ninep/cols.zig4
-rw-r--r--src/ninep/ctl.zig31
-rw-r--r--src/ninep/events.zig24
-rw-r--r--src/ninep/pane.zig102
-rw-r--r--src/ninep/pty.zig51
-rw-r--r--src/ninep/screen.zig4
-rw-r--r--src/ninep/testing.zig1
-rw-r--r--src/ninep/tree.zig198
8 files changed, 331 insertions, 84 deletions
diff --git a/src/ninep/cols.zig b/src/ninep/cols.zig
index c8d84e5d..d953bcc7 100644
--- a/src/ninep/cols.zig
+++ b/src/ninep/cols.zig
@@ -435,6 +435,7 @@ test "Help and Find at /tagexec act in the session's directory, not the keyboard
}
test "a shell command at /tagexec or a column's exec is a command pane in the session's directory, never typed into the keyboard's terminal" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 30 });
defer p.deinit();
while (p.nextEffect()) |_| {}
@@ -455,6 +456,7 @@ test "a shell command at /tagexec or a column's exec is a command pane in the se
}
test "New and Term run at /tagexec and a column's exec, with no pane at all too" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const p = try th.withFile(testing.allocator, "x\n");
defer p.deinit();
const tagexec = @backingInt(tree.TopFile.tagexec);
@@ -671,6 +673,7 @@ test "tagexec runs a word as a click in the workspace tag; exec files read back
}
test "a word run from the workspace's or a column's tag runs in the session's directory, a pane's in its own" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
const p = try th.withFile(testing.allocator, "x\n");
defer p.deinit();
const session = p.sessionDir();
@@ -689,6 +692,7 @@ test "a word run from the workspace's or a column's tag runs in the session's di
}
test "a command run from a column's tag gets its pane in that column, not the last" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
const p = try th.withFile(testing.allocator, "x\n");
defer p.deinit();
layout.newColumn(p, p.view().active);
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index 699b7833..49fe661d 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -366,7 +366,7 @@ pub fn readResults(p: *Pardes, req: Req) Reply {
const looking = if (tree.Node.target(req.node)) |t| switch (t) {
.top => |f| f == .look,
.pane => |pt| pt.file == .look,
- .col => false,
+ .col, .port => false,
} else false;
for (kept) |serial| if (!looking or p.paneBySerial(serial) != null)
out.print(p.gpa, "{d}\n", .{serial}) catch return Reply.fail(req.tag, E.NOMEM);
@@ -426,7 +426,7 @@ pub fn paneText(p: *Pardes, pane: *Pane, buf: *[pane_text_cap]u8) []const u8 {
pane.serial,
if (pane_files.tagOf(p, pane)) |tag| tag.len else |_| 0, // a length, as a stat's: none when the tag cannot be made
// A terminal's body is its history, read whole on an open's first read.
- if (pane.isTerminal()) panes.terminal.screenTextLen(pane) else pane_files.bodyOf(pane).len,
+ if (pane.isTerminal()) pardes.Term.scrollbackLen(pane.terminal) else pane_files.bodyOf(pane).len,
// A directory pane is one, as acme's isdir says.
@intFromBool(if (pane.file) |f| f.listing != null else false),
@intFromBool(pane_files.dirtyOf(pane)),
@@ -699,7 +699,7 @@ pub fn writePager(p: *Pardes, req: Req) Reply {
/// The text a /pager open wrote after its directory (`held`), a program's
/// output for a terminal, put in the +Pager `serial` as `pardes -` once
-/// wrote it to the body: through ghostty-vt (pager.zig), its colours kept
+/// wrote it to the body: through the terminal's parse (Term.page), its colours kept
/// as the pane's spans, one undo step, and left
/// clean. Reads of the pane are its plain text. The held bytes go either way.
pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfMemory}!void {
@@ -708,7 +708,7 @@ pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfM
const id = p.paneBySerial(serial) orelse return;
const pane = p.panes[id].?;
const f = if (pane.file) |*file| file else return;
- const page = try pardes.pager.parse(p.gpa, held.items);
+ const page = try pardes.Term.page(p.gpa, held.items);
held.clearAndFree(p.gpa);
if (page.text.len == 0 and f.content.len == 0) return page.deinit(p.gpa);
if (!pane.fs.nomark) panes.File.pushUndo(p, pane) catch |err| {
@@ -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;
- }
+ pardes.Plumb.paged(p, id);
}
/// The editor's step: the text of /pager opens closed unread goes in.
@@ -1889,6 +1879,7 @@ test "Mount dials its peer and fails, mounting nothing, when none answers; a loo
}
test "Term in a directory that is not there fails and makes no pane; a shell that never started answers a run" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
const serial = serialOf(p);
@@ -1981,6 +1972,7 @@ test "a script's Grep and Find fill the pane's one query pane, a row a line unde
}
test "a terminal told the directory it already goes by logs no rename" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
_ = wr(p, Node.of(serialOf(p), .name), "/tmp/f.txt\n");
@@ -2029,6 +2021,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" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
p.host = .{ .vtable = &th.starting };
@@ -2188,6 +2181,7 @@ test "a failure said while a 9P write waits on it posts no msg, only the write's
}
test "with no pane at all, the root look opens its file and the root exec runs in the session's directory" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
var tmp = testing.tmpDir(.{});
@@ -2248,6 +2242,7 @@ test "a restored pane whose file is gone from disk comes back dirty, and Del ask
test "a focus moved over 9P tells the programs that asked for focus reports, as a click does" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const p = try Pardes.init(testing.allocator, .{ .cols = 90, .rows = 30, .shells = 2 });
defer p.deinit();
while (p.nextEffect()) |_| {}
@@ -2376,6 +2371,7 @@ test "/recent shows a name as /index does, a newline in it escaped" {
}
test "size is monotonic: growing is never refused, and a size once taken is taken again" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 60 });
defer p.deinit();
while (p.nextEffect()) |_| {}
@@ -2612,6 +2608,7 @@ test "@p<terminal>:<address> takes any address over the terminal's lines, select
}
test "what a /pager open writes after its directory is the text: plain in the body, its colours the pane's, clean, in by the read" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // the emulator's parse of colours
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
const pager = @backingInt(tree.TopFile.pager);
@@ -2648,7 +2645,7 @@ test "what a /pager open writes after its directory is the text: plain in the bo
try testing.expectEqual(@as(usize, 0), e.off);
try testing.expectEqual(@as(usize, 0), e.removed.len);
try testing.expectEqual(f.content.len, e.ins);
- // An edit drops the colours (pager.zig's ponytail).
+ // An edit drops the colours (Term.page's ponytail).
try testing.expectEqual(Status.ok, call(p, .{ .tag = 6, .op = .write, .node = Node.of(pn.serial, .body), .data = "more\n" }).reply.status);
try testing.expectEqual(@as(usize, 0), f.pager_spans.len);
// Undone back to the paged text: the text comes back, its colours
@@ -2680,6 +2677,7 @@ test "what a /pager open writes after its directory is the text: plain in the bo
}
test "a command pane an exec open was answered is leased to it while it stays open: another client's command gets a pane of its own" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
const exec = Node.of(serialOf(p), .exec);
@@ -2727,6 +2725,7 @@ test "a body write to an image pane says its body takes none, not that terminals
}
test "Term+bash on a pane ctl is Term with its argument, as exec and the tag take it" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
// Tty+sh too: Term's old name, as a saved tag holds it.
diff --git a/src/ninep/events.zig b/src/ninep/events.zig
index 77a4e35f..9e6df93d 100644
--- a/src/ninep/events.zig
+++ b/src/ninep/events.zig
@@ -233,6 +233,10 @@ pub fn noteError(p: *Pardes, req: Req, reply: Reply) void {
if ((f == .exec or f == .look) and p.fs.ran_at != 0)
serial = name.print("{d}", .{p.fs.ran_at});
},
+ .port => |i| {
+ serial = "-";
+ file = name.print("plumb/{s}", .{pardes.Plumb.portName(p, i) orelse "?"});
+ },
.col => |c| {
serial = "-";
file = if (c.file == .dir)
@@ -677,22 +681,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 "";
@@ -1303,6 +1306,7 @@ test "after a Restore a new pane or column takes a serial past every one the old
}
test "a pane named before it is announced has its new under that name, and no rename before it" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const p = try withFile(testing.allocator, "one\n");
defer p.deinit();
const pane = p.panes[0].?;
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig
index bb15b016..93759aa5 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");
@@ -317,10 +318,10 @@ pub fn fileSize(p: *Pardes, id: usize, f: PaneFile) u64 {
const pf = &pane.fs;
return switch (f) {
// A terminal's body reads its screen and history, as text.
- .body => if (pane.file == null and pane.isTerminal()) panes.terminal.screenTextLen(pane) else if (kindText(p, pane)) |t| t.len else bodyOf(pane).len,
+ .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),
@@ -441,7 +448,7 @@ fn readBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
// A kind's body is its text (a PDF's, the page shown's): read-only.
if (kindText(p, pane)) |text| return tree.window(p, req, text);
if (req.handle != 0 and pane.isTerminal()) return screen.readSnapshot(p, req, pane);
- const text = panes.terminal.screenTextAlloc(pane, p.gpa) catch
+ const text = pardes.Term.scrollback(pane.terminal, p.gpa) catch
return Reply.fail(req.tag, E.NOMEM);
defer p.gpa.free(text);
return tree.window(p, req, text);
@@ -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
@@ -900,7 +938,7 @@ pub fn nameBuffer(p: *Pardes, id: usize, full: []const u8, preserve_tag: bool) !
f.highlights = &.{};
f.highlight_start = 0;
f.syntax_dirty = true;
- f.context_revision = null;
+ f.context.revision = null;
pane.geometry_key = 0;
look.invalidateLookHover(p, id);
p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } });
@@ -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;
@@ -1016,6 +1056,7 @@ test "rows through the line index are the ones counting newlines gives, at every
}
test "a terminal's body stats as long as it reads" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const gpa = testing.allocator;
const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
@@ -1030,6 +1071,7 @@ test "a terminal's body stats as long as it reads" {
}
test "a body write to a terminal pane types at its shell" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const gpa = testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 10 });
defer p.deinit();
@@ -1240,6 +1282,7 @@ test "a limit write that fails keeps the limit a truncation cleared before it" {
}
test "index names a command's pane cmd, a shell's term" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a command pane
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .exec), "true\n").reply.status);
@@ -1248,3 +1291,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 7333fac7..be85a497 100644
--- a/src/ninep/pty.zig
+++ b/src/ninep/pty.zig
@@ -137,9 +137,9 @@ pub fn readStatus(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
// Busy is what a run written now would be told: a command is running
// or a line is typed at the prompt. Before the shell's first prompt a
// run waits for it rather than answer busy, so that is not busy.
- const marks = if (pane.terminal) |state| &state.stream.handler else null;
+ const marks = pardes.Term.prompt(pane.terminal);
const busy = p.hostTtyTaken(id) or (!pane.fs.unmarked and (waitingRun(p, pane) != null or
- (if (marks) |m| m.prompts > 0 and (m.phase != .input or !pardes.panes.terminal.promptInputEmpty(pane)) else false)));
+ (marks.prompts > 0 and (marks.phase != .input or !marks.input_empty))));
const out = p.fs.stage(p.gpa);
const size = winsize(pane);
out.print(p.gpa, "{d:>11} {d:>11} {d:>11}\n", .{ size[0], size[1], @intFromBool(busy) }) catch return Reply.fail(req.tag, E.NOMEM);
@@ -219,8 +219,8 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
if (std.mem.trim(u8, line, " \t").len == 0) return tree.failText(req.tag, E.INVAL, e_bad_line);
for (line) |c| if (c < ' ' and c != '\t') return tree.failText(req.tag, E.INVAL, e_bad_line);
const pf = &pane.fs;
- const state = pane.terminal orelse return tree.failText(req.tag, E.INVAL, e_bad_line);
- const marks = &state.stream.handler;
+ if (pane.terminal == null) return tree.failText(req.tag, E.INVAL, e_bad_line);
+ const marks = pardes.Term.prompt(pane.terminal);
if (pane.shell_failed) {
// Its shell never started: no prompt is coming to wait for.
answer(p, slot, "error shell gone", .{});
@@ -230,18 +230,18 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
} else if (pf.unmarked) {
answer(p, slot, "error no prompt marks", .{});
} else if (waitingRun(p, pane) != null or (marks.prompts > 0 and (marks.phase != .input or
- !pardes.panes.terminal.promptInputEmpty(pane) or p.hostTtyTaken(id))))
+ !marks.input_empty or p.hostTtyTaken(id))))
{
// Something is running or someone has typed at the prompt: sending
// now would type into the middle of it. The phase is pardes's own
// marks: a nested shell's prompt (ssh, a shell with its own
- // integration) looks like an empty prompt to ghostty but is not the
+ // integration) looks like an empty prompt to the emulator but is not the
// shell this run knows.
// Naming what runs, where the host can tell (Linux).
var name: [64]u8 = undefined;
if (p.hostForegroundName(id, &name)) |program|
answer(p, slot, "busy: {s} is running", .{program})
- else if (pardes.panes.terminal.onAlternateScreen(pane))
+ else if (pardes.Term.modes(pane.terminal).alternate)
answer(p, slot, "busy alternate screen", .{})
else
answer(p, slot, "busy", .{});
@@ -285,27 +285,27 @@ pub fn readRun(p: *Pardes, req: Req) Reply {
/// for the prompt means whoever reads the answer can send the next command
/// at once instead of finding the shell still drawing it.
pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void {
- const state = pane.terminal orelse return;
+ if (pane.terminal == null) return;
const pf = &pane.fs;
const slot = waitingRun(p, pane) orelse return;
- const marks = &state.stream.handler;
+ const marks = pardes.Term.prompt(pane.terminal);
if (slot.phase == .waiting) {
if (pf.unmarked) {
answer(p, slot, "error no prompt marks", .{});
pf.run = null;
return;
}
- if (marks.phase != .input or !pardes.panes.terminal.promptInputEmpty(pane) or p.hostTtyTaken(id)) return;
+ if (marks.phase != .input or !marks.input_empty or p.hostTtyTaken(id)) return;
slot.want = marks.started +% 1;
slot.prompts = marks.prompts;
slot.declined = marks.declined;
// A prompt on the alternate screen (its program killed there): the
// emulator leaves it first, as a reused command pane does, so the
// run's output lands on the primary screen and is read whole.
- if (pardes.panes.terminal.onAlternateScreen(pane)) pardes.panes.terminal.feedOutput(p, pane, "\x1b[?1049l");
+ if (pardes.Term.modes(pane.terminal).alternate) pane.feedOutput(p, "\x1b[?1049l");
p.emitWrite(id, slot.line);
p.emitWrite(id, "\r");
- pardes.panes.terminal.noteCommand(pane, slot.line);
+ pane.noteCommand(slot.line);
// The run is the shell's first command: a greeting `ls` typed at the
// same prompt would run after it, and its C would be taken for ours.
pane.greet = false;
@@ -322,7 +322,7 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void {
// typed into an empty prompt, so throw it away with Ctrl-C, which
// drops the whole pending command, and answer at the prompt after
// that, so the next line finds the shell clear.
- const empty = pardes.panes.terminal.promptInputEmpty(pane);
+ const empty = marks.input_empty;
switch (slot.cleared) {
.no => {
if (marks.continuation) {
@@ -365,7 +365,7 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void {
fn finish(p: *Pardes, slot: *Run, pane: *Pane, status_code: ?i32) void {
// As the authority's emulator has it (taped): a screen-state replica's
// is its deltas', which a lagging client's can be states behind, its
- // pins in pages it never had (ghostty's leftWrap overflowed on one).
+ // pins in pages it never had (the emulator's leftWrap overflowed on one).
const printed = pardes.tape.value(.prompt, printedBy, .{ pane, p.scratch.allocator() }) orelse
return answer(p, slot, "error out of memory", .{});
// Keep the tail, cut at a line start so the first line kept is whole.
@@ -398,9 +398,9 @@ fn finish(p: *Pardes, slot: *Run, pane: *Pane, status_code: ?i32) void {
pub const Printed = struct { text: []const u8, whole: bool };
-/// What pane `pane`'s command printed (terminal.commandOutput), in `arena`.
+/// What pane `pane`'s command printed (Term.commandOutput), in `arena`.
fn printedBy(pane: *Pane, arena: std.mem.Allocator) ?Printed {
- const out = pardes.panes.terminal.commandOutput(pane, arena, output_rows) catch return null;
+ const out = pardes.Term.commandOutput(pane.terminal, arena, output_rows) catch return null;
return .{ .text = out.text, .whole = out.whole };
}
@@ -413,7 +413,7 @@ pub fn shellExited(p: *Pardes, pane: *Pane, status: ?u8) void {
const code = status orelse return;
if (waitingRun(p, pane)) |slot| if (slot.phase == .sent) {
pane.fs.run = null;
- pardes.panes.terminal.endOutputHere(pane);
+ if (pane.terminal) |state| pardes.Term.endOutputHere(state);
finish(p, slot, pane, code);
};
var buf: [4]u8 = undefined;
@@ -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);
@@ -962,6 +962,7 @@ test "run on a command pane says no shell is there" {
test "a run's answer says cut when its output's start is gone, and reads a bounded tail" {
const gpa = testing.allocator;
+ if (pardes.Term.ops == null) return error.SkipZigTest; // it reads the emulator
const p = try withTerm(gpa);
defer p.deinit();
const node = Node.of(serialOf(p), .pty_run);
@@ -1005,10 +1006,10 @@ test "a run's answer says cut when its output's start is gone, and reads a bound
// A resize between its start and end reflows the pins with the text.
h = sh.send(p, node);
sh.put(p, c ++ "one\r\ntwo\r\n");
- pardes.panes.terminal.resizeGrid(pane, 50, 20);
+ pane.resizeGrid(50, 20);
sh.put(p, d ++ prompt);
try testing.expectEqualStrings("exit 0\none\ntwo\n", sh.answered(p, node, h, into));
- pardes.panes.terminal.resizeGrid(pane, pane.cols, pane.rows);
+ pane.resizeGrid(pane.cols, pane.rows);
// A full-screen program leaves nothing on the primary screen, and a D
// with no status is not a success.
@@ -1023,7 +1024,7 @@ test "a run's answer says cut when its output's start is gone, and reads a bound
sh.put(p, c ++ "\x1b[?1049hhtop's screen\r\n");
_ = call(p, .{ .tag = 4, .op = .release, .node = node, .handle = h });
sh.put(p, "\x1b]133;D;137;aid=pardes\x07" ++ prompt);
- try testing.expect(pardes.panes.terminal.onAlternateScreen(pane));
+ try testing.expect(pardes.Term.modes(pane.terminal).alternate);
try testing.expect(std.mem.endsWith(u8, rd(p, Node.of(serialOf(p), .pty_status), 0, 64).bytes, " 0\n"));
h = sh.send(p, node);
sh.put(p, c ++ "after htop\r\n" ++ d ++ prompt);
@@ -1048,7 +1049,7 @@ test "a run's answer says cut when its output's start is gone, and reads a bound
_ = std.fmt.bufPrint(&line, "{d:0>8}", .{i}) catch unreachable;
sh.put(p, &line);
}
- try testing.expect(!pane.terminal.?.stream.handler.out_start.?.garbage);
+ try testing.expect(!pardes.Term.emulatorOf(pane.terminal.?).stream.handler.out_start.?.garbage);
sh.put(p, d ++ prompt);
got = sh.answered(p, node, h, into);
try testing.expect(std.mem.startsWith(u8, got, "exit 0 cut\n"));
@@ -1061,14 +1062,14 @@ test "a run's answer says cut when its output's start is gone, and reads a bound
const q = try withTerm(gpa);
defer q.deinit();
const kept = q.panes[0].?;
- kept.terminal.?.vt.screens.get(.primary).?.pages.limits.set(.bytes, 1);
+ pardes.Term.emulatorOf(kept.terminal.?).vt.screens.get(.primary).?.pages.limits.set(.bytes, 1);
const run = Node.of(serialOf(q), .pty_run);
sh.put(q, prompt);
h = sh.send(q, run);
sh.put(q, c);
const lines = std.mem.asBytes(&@as([64][8]u8, @splat("pruned\r\n".*)));
var fed: usize = 0;
- while (!kept.terminal.?.stream.handler.out_start.?.garbage) : (fed += 1) {
+ while (!pardes.Term.emulatorOf(kept.terminal.?).stream.handler.out_start.?.garbage) : (fed += 1) {
if (fed == 1000) return error.TestUnexpectedResult;
sh.put(q, lines);
}
diff --git a/src/ninep/screen.zig b/src/ninep/screen.zig
index 634ef640..1bfd9b63 100644
--- a/src/ninep/screen.zig
+++ b/src/ninep/screen.zig
@@ -64,7 +64,7 @@ pub fn readSnapshot(p: *Pardes, req: Req, pane: ?*Pane) Reply {
const snapshot = &(tree.openOf(p, req) orelse return Reply.fail(req.tag, E.INVAL)).what.snapshot;
if (snapshot.* == null) {
const terminal = pane orelse return Reply.fail(req.tag, E.INVAL);
- snapshot.* = panes.terminal.screenTextAlloc(terminal, p.gpa) catch return Reply.fail(req.tag, E.NOMEM);
+ snapshot.* = pardes.Term.scrollback(terminal.terminal, p.gpa) catch return Reply.fail(req.tag, E.NOMEM);
}
return tree.window(p, req, snapshot.*.?);
}
@@ -126,7 +126,7 @@ test "terminal body handles keep one history snapshot across fragmented reads" {
const node = Node.of(serialOf(p), .body);
p.update(.{ .output = .{ .pane = 0, .bytes = "old caf\xc3\xa9\r\nold tail" } });
while (p.nextEffect()) |_| {}
- const original = try panes.terminal.screenTextAlloc(p.panes[0].?, gpa);
+ const original = try pardes.Term.scrollback(p.panes[0].?.terminal, gpa);
defer gpa.free(original);
const opened = call(p, .{ .tag = 1, .op = .open, .node = node });
try testing.expectEqual(Status.ok, opened.reply.status);
diff --git a/src/ninep/testing.zig b/src/ninep/testing.zig
index 14aeffea..a9f6d5de 100644
--- a/src/ninep/testing.zig
+++ b/src/ninep/testing.zig
@@ -114,6 +114,7 @@ pub fn withFile(gpa: std.mem.Allocator, text: []const u8) !*Pardes {
}
pub fn withTerm(gpa: std.mem.Allocator) !*Pardes {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
errdefer p.deinit();
while (p.nextEffect()) |_| {}
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index bca89466..73468ef8 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,8 @@ 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,
+ .port => false,
};
}
@@ -151,6 +153,13 @@ 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,
+ /// 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
@@ -207,7 +216,8 @@ pub const Open = struct {
gpa.free(run.line);
},
.frozen => |bytes| gpa.free(bytes),
- .event, .pty_data, .ctl, .lines, .writes, .pager => {},
+ .port => |*port| port.queue.deinit(gpa),
+ .event, .pty_data, .ctl, .lines, .writes, .pager, .plumb => {},
}
o.pending.deinit(gpa);
if (o.replay) |r| gpa.free(r.bytes);
@@ -269,6 +279,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 +293,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 +330,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,
@@ -365,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,
@@ -379,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,
@@ -399,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
@@ -559,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),
@@ -581,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| {
@@ -593,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),
@@ -658,7 +694,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,
@@ -689,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 });
}
@@ -697,8 +734,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,8 +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 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);
@@ -752,7 +792,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 +813,16 @@ fn stageDir(p: *Pardes, out: *std.ArrayList(u8), target: Target, off: u64) error
try stageDirent(out, p.gpa, Node.of(serial, .dir), true, name);
}
},
+ // 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) {
skip -= 1;
@@ -792,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();
@@ -842,11 +893,19 @@ 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)
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);
@@ -904,7 +963,8 @@ 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 => {},
+ .port => |*port| Plumb.opened(p, port.index, &port.queue),
+ .run, .ctl, .lines, .writes, .pager, .plumb => {},
}
return .{ .tag = req.tag, .handle = @intCast(i + 1) };
}
@@ -915,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);
@@ -967,6 +1027,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 +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 => {},
+ .snapshot, .log, .lines, .writes, .frozen, .pager, .plumb, .port => {},
};
o.deinit(p.gpa);
}
@@ -1017,7 +1080,8 @@ 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),
+ .port => return Reply.fail(req.tag, E.PERM),
};
return .{ .tag = req.tag, .attr = attrOf(p, target) orelse return Reply.fail(req.tag, E.NOENT) };
}
@@ -1028,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,
@@ -1055,8 +1121,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 +1195,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.
@@ -1153,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),
}
}
@@ -1163,7 +1232,8 @@ 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,
+ .port => false,
};
}
@@ -1173,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,
};
}
@@ -1219,6 +1290,7 @@ fn ctlFile(target: Target) bool {
.top => |f| f == .ctl,
.col => |c| c.file == .ctl,
.pane => |t| t.file == .ctl,
+ .port => false,
};
}
@@ -1326,6 +1398,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 +1425,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| {
@@ -1364,6 +1441,64 @@ 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),
+ }
+}
+
+/// 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, "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}));
+ }
+ 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, "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..]);
+ 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();
+ 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) };
+ },
}
}
@@ -1566,13 +1701,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);
+ // 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);
@@ -1611,10 +1756,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);
@@ -2001,6 +2146,7 @@ test "a write with no newline waits for its newline or the close, whatever its s
}
test "look and exec read back what their own open's write touched; a fresh open reads the session's last" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell, by Term
const p = try th.withFile(testing.allocator, "x\n");
defer p.deinit();
const exec = @backingInt(TopFile.exec);