summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/9p.zig2
-rw-r--r--src/9p_io.zig96
-rw-r--r--src/CHANGELOG.md59
-rw-r--r--src/File.zig36
-rw-r--r--src/Messages.zig16
-rw-r--r--src/builtins.zig23
-rw-r--r--src/dump.zig46
-rw-r--r--src/exec.zig50
-rw-r--r--src/fs.zig25
-rw-r--r--src/look.zig89
-rw-r--r--src/main.zig123
-rw-r--r--src/ninep/addr.zig10
-rw-r--r--src/ninep/cols.zig5
-rw-r--r--src/ninep/ctl.zig344
-rw-r--r--src/ninep/events.zig37
-rw-r--r--src/ninep/pane.zig141
-rw-r--r--src/ninep/pty.zig39
-rw-r--r--src/ninep/testing.zig7
-rw-r--r--src/ninep/tree.zig208
-rw-r--r--src/pardes.zig27
-rw-r--r--src/regexp.zig250
-rw-r--r--src/sam_edit.zig37
22 files changed, 1519 insertions, 151 deletions
diff --git a/src/9p.zig b/src/9p.zig
index 5ffe9058..837525cb 100644
--- a/src/9p.zig
+++ b/src/9p.zig
@@ -53,7 +53,7 @@ pub const msize_min = cloud9.fs.msize_min;
/// The smallest msize a native listener offers (src/9p_io.zig).
pub const min_msize: u32 = 4096;
/// What a native listener negotiates.
-pub const msize: u32 = 8192;
+pub const msize: u32 = 65536;
/// The editor's engine: native fid count, native file names, and room to
/// park a write of any size a frame can carry -- a request that would change
diff --git a/src/9p_io.zig b/src/9p_io.zig
index 91240b57..78ff5d59 100644
--- a/src/9p_io.zig
+++ b/src/9p_io.zig
@@ -261,6 +261,7 @@ pub const Listener = struct {
return conn.reply(&later, "");
}
const core = l.core;
+ core.fs.write_room = if (conn.engine.protocol.msize > 24) conn.engine.protocol.msize - 24 else 0;
const epoch = pardes.turn.epoch;
const restores = pardes.turn.restores;
// Asked first: a release that runs a held line has none after.
@@ -311,11 +312,14 @@ pub const Listener = struct {
if (restoring) l.restore_writer.store(true, .release);
defer if (restoring) l.restore_writer.store(false, .release);
core.fs.late_failure_len = 0;
+ // What fails while this waits is this write's err, never a msg too.
+ core.fs.write_waits = true;
if (core.effects_len != 0) pardes.turn.awaitSettled(epoch);
if (core.fs.lsp_answer_at) |n| {
core.fs.lsp_answer_at = null;
pardes.turn.awaitLsp(n);
}
+ core.fs.write_waits = false;
// ponytail: one slot, so a failure of another client's effects that
// settle in the same wait is told to this write too.
if (l.core == core and core.fs.late_failure_len != 0) {
@@ -326,9 +330,8 @@ pub const Listener = struct {
pardes.ctlfs.E.NOENT
else if (std.mem.indexOf(u8, late, "no space") != null) pardes.ctlfs.E.NOSPC else pardes.ctlfs.E.IO;
const failed = pardes.ctlfs.failText(req.tag, errno, late);
- // Its err record says it (the path in it); the msg the failure
- // was also said as goes, as a builtin's failing a write does.
- pardes.ctlfs.events.dropMessage(core, core.fs.late_failure[0..core.fs.late_failure_len]);
+ // Its err record says it (the path in it), the one record: it
+ // was said with no msg while this waited (fs.write_waits).
pardes.ctlfs.events.noteError(core, req, failed);
return conn.reply(&failed, "");
}
@@ -506,6 +509,7 @@ pub const Listener = struct {
/// Answers one QUIC request, on the editor's thread.
fn step(l: *Listener, srv: *Srv, req: pardes.ctlfs.Req) void {
+ l.core.fs.write_room = if (srv.protocol.msize > 24) srv.protocol.msize - 24 else 0;
const reply = l.core.serveFs(req);
srv.reply(&reply, l.core.fsPayload(reply));
if (req.op == .release) l.collectOs();
@@ -1943,14 +1947,8 @@ pub const Client = struct {
}
};
- fn transact(
- s: *Session,
- names: []const []const u8,
- out: *std.Io.Writer.Allocating,
- remote: *RemoteError,
- write_bytes: ?[]const u8,
- read_limit: usize,
- ) !void {
+ /// Version, attach and a walk to `names`: the fid there and its qid.
+ fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !struct { fid: u32, qid: ninep.Qid } {
_ = try s.ask(.{ .version = .{} }, remote);
if (s.cl.msize == 0) return Error.Botch;
@@ -1974,6 +1972,20 @@ pub const Client = struct {
next = if (next == 1) 2 else 1;
i += n;
}
+ return .{ .fid = cur, .qid = here };
+ }
+
+ fn transact(
+ s: *Session,
+ names: []const []const u8,
+ out: *std.Io.Writer.Allocating,
+ remote: *RemoteError,
+ write_bytes: ?[]const u8,
+ read_limit: usize,
+ ) !void {
+ const at = try walkTo(s, names, remote);
+ const cur = at.fid;
+ const here = at.qid;
defer s.dropNoWait(cur);
const directory = here.type & ninep.qtdir != 0;
@@ -2032,15 +2044,20 @@ pub const Client = struct {
read_limit: usize,
) ![]u8 {
if (comptime !supported) return Error.Dial;
+ const s = try startSession(gpa, sock, display_path);
+ defer endSession(gpa, s);
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ errdefer out.deinit();
+ try transact(s, names, &out, remote, write_bytes, read_limit);
+ return out.toOwnedSlice();
+ }
+
+ /// A connected session whose requests have the usual deadline.
+ fn startSession(gpa: std.mem.Allocator, sock: Dial, display_path: []const u8) !*Session {
const deadline = nowMs() +| budget_ms;
const s = try gpa.create(Session);
s.* = .{ .fd = -1, .deadline = deadline, .display_path = display_path };
- defer {
- if (quic_enabled and s.quic != null) {
- s.quic.?.deinit();
- } else if (s.fd >= 0) _ = libc.close(s.fd);
- gpa.destroy(s);
- }
+ errdefer endSession(gpa, s);
if (sock == .quic) {
if (comptime quic_enabled) {
s.quic = quic.Connection.dial(sock.quic) catch return Error.Dial;
@@ -2048,11 +2065,48 @@ pub const Client = struct {
} else return Error.QuicUnavailable;
} else s.fd = try connect(sock, deadline);
s.cl = .init(.{ .in = &s.in, .out = &s.out });
+ return s;
+ }
- var out: std.Io.Writer.Allocating = .init(gpa);
- errdefer out.deinit();
- try transact(s, names, &out, remote, write_bytes, read_limit);
- return out.toOwnedSlice();
+ fn endSession(gpa: std.mem.Allocator, s: *Session) void {
+ if (quic_enabled and s.quic != null) {
+ s.quic.?.deinit();
+ } else if (s.fd >= 0) _ = libc.close(s.fd);
+ gpa.destroy(s);
+ }
+
+ /// Opens `path` on one connection and writes `first` to it (/log's
+ /// `follow new`), all with the usual deadline; then, once `ready(ctx)`
+ /// has said it is not done already, reads it with no deadline, a read at
+ /// a time, until `record(ctx, bytes)` says done. An end of file or a
+ /// dropped connection is Hangup. What `pardes --wait` blocks on.
+ pub fn follow(
+ gpa: std.mem.Allocator,
+ dial: []const u8,
+ path: []const u8,
+ first: []const u8,
+ ctx: anytype,
+ comptime ready: fn (@TypeOf(ctx)) bool,
+ comptime record: fn (@TypeOf(ctx), []const u8) bool,
+ ) !void {
+ if (comptime !supported) return error.Unsupported;
+ var names: [max_depth][]const u8 = undefined;
+ const n = try elements(path, &names);
+ var sock_buf: [sun_path_len]u8 = undefined;
+ const sock = try resolve(&sock_buf, dial);
+ var remote: RemoteError = .{};
+ const s = try startSession(gpa, sock, path);
+ defer endSession(gpa, s);
+ const fid = (try walkTo(s, names[0..n], &remote)).fid;
+ _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote);
+ _ = try s.ask(.{ .write = .{ .fid = fid, .offset = 0, .data = first } }, &remote);
+ if (ready(ctx)) return;
+ s.deadline = std.math.maxInt(i64);
+ while (true) {
+ const data = (try s.ask(.{ .read = .{ .fid = fid, .offset = 0, .count = s.cl.maxRead() } }, &remote)).read;
+ if (data.len == 0) return Error.Hangup;
+ if (record(ctx, data)) return;
+ }
}
fn connect(sock: Dial, deadline: i64) Error!c_int {
diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md
index 142287b2..d2641aa9 100644
--- a/src/CHANGELOG.md
+++ b/src/CHANGELOG.md
@@ -1,6 +1,60 @@
# Changelog
-## 0.0.3
+## 0.0.4
+
+- Drive the editor over 9P the way acme is driven. Panes take sam
+ addresses (`/re/`, `#n`, `$-1`, `0/re/`), `L:C` and inclusive `L:C-L2:C2`
+ ranges on `addr`, with `data` and `xdata`; `Edit` runs the sam command
+ language as one undo step. `exec` and `ctl` buffer lines per open, and a
+ line without a newline runs with its write. One rule for failure: a
+ builtin that fails fails the write and logs one `err`, no `msg`.
+- New 9P files: `/layout`, `/tag`, `/tagexec`, `col/<serial>/{tag,ctl,exec}`,
+ `pane/<n>/tagexec`, `/recent`, ask/answer records, and a `size` ctl for
+ detached sessions (160x50 by default). Listings freeze at their open, so
+ a read in several chunks never splices two moments.
+- acme's ctl words (`name`, `put`, `clean`, `dirty`, `del`, `delete`,
+ `dot=addr`, `addr=dot`, `limit=addr`, `mark`, `nomark`, `show`,
+ `cleartag`) are accepted again, done by the files that replace them; a
+ pane's ctl reads acme's fields in acme's order. `Put` saves and `Delete`
+ closes; acme builtins pardes lacks are refused, never run as shell
+ commands. plan9port's `9p write` always truncates, so piping into `body`
+ replaces it: append with `>>` through a mount.
+- Command panes and a REPL binding; `pty/run` runs one line and answers its
+ exit status. Every child starts with default signal dispositions.
+- `Recent` lists recently opened files, closed ones included, and persists
+ in `$XDG_STATE_HOME/pardes/recent`. A Jumplist row for a closed file says
+ `(closed)`. `+Unsaved` lists dirty panes. `ThemeSel` and `FontSel` are now
+ `Themes` and `Fonts`; `Exit`'s refusal opens in an output pane.
+- `pardes --wait FILE` (`-w`) run inside a pane opens FILE there and returns
+ only when its pane is deleted, as plan9port's `E` does: set
+ `EDITOR='pardes --wait'` for fish's Ctrl-O, git and the rest. Without it,
+ `pardes FILE` still hands the file over and returns at once.
+- Only a write logs an `err`: a refused open, create or remove fails with
+ Plan 9 words (`permission denied`, `file does not exist`) and logs nothing.
+- The tag is a full text buffer: multi-line, with undo.
+- Helix keys: the skipped non-conflicting keys, full registers and per-range
+ anchors, checked against the latest helix.
+- Deleting the last column leaves the window empty but for its tag, as in
+ acme. A buffer whose file was deleted on disk counts as dirty.
+- 62 themes in 13 families, each as close as possible to its original,
+ grouped by family in `Themes`; an `acme` theme taken from plan9port's
+ source, with acme's grips, 2 px rules and a 1 px tag rule. Sizes follow
+ display density.
+- GUI motion: `Motion` flavours (crisp, smooth, bouncy, playful, off), `Lift`,
+ a cursor that glides and moves with its pane, smooth scroll, and optional
+ Bloom, Vignette, Grain, SelectionGlow, HoverGlow and Shadertoy post passes
+ that recompile on save. Still passes let the GUI rest; Crt keeps drawing.
+- The terminal: replies to capability queries, malformed escape sequences
+ and lone modifier keys never become typed text; function keys, Insert and
+ the keypad reach terminal panes; a lone Escape is Escape at once; a focus
+ change no longer resends every blank cell.
+- Speed: the theme's chrome is worked out once, not per grapheme; typing
+ tells `g.` where its edit ended; large body and data writes are linear;
+ a pattern that opens with a literal jumps to it. `zig build perf-gate`
+ fails any gesture more than 3x its recorded baseline.
+- Robustness: over-long paths, non-UTF-8 bytes and odd 9P input are refused,
+ never a panic; seeded GUI, core and 9P monkeys (`zig build monkey-9p`)
+ replay and shrink what they find.
- A column can be empty, as in acme. `Newcol` makes an empty column and
gives its tag the keyboard; closing a column's last pane, or dragging it
@@ -63,6 +117,9 @@
carry real lengths, modes and mtimes. Sources are served under `/src` only
with `-Dembed-sources=true`. The tree code moved from `src/fs.zig` to
`src/ninep/`.
+
+## 0.0.3
+
- Add pinned tree-sitter declaration context, with optional compact tagline
styling and separators between discontinuous source lines.
- Configure search and LSP result context with `LocationsConfig`. Results use
diff --git a/src/File.zig b/src/File.zig
index 1d838c4f..9f0f329c 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -266,7 +266,36 @@ test "stacked location metadata requires matching adjacent preview ownership" {
pub fn graphemeDisplayWidth(grapheme: []const u8) usize {
if (std.mem.eql(u8, grapheme, "\t")) return config.tab_width;
if (grapheme.len == 1 and grapheme[0] >= 0x20 and grapheme[0] < 0x7f) return 1;
- return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode)));
+ if (std.unicode.utf8ValidateSlice(grapheme)) return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode)));
+ // vaxis's gwidth counts back a replacement rune's 3 bytes over the one
+ // invalid byte it stood for (`\xee` before `e\u{301}` overflowed it):
+ // each invalid byte is a cell, as surface.zig draws it, and each valid
+ // run between is measured alone.
+ var width: usize = 0;
+ var run: usize = 0;
+ var i: usize = 0;
+ while (i < grapheme.len) {
+ const n = std.unicode.utf8ByteSequenceLength(grapheme[i]) catch 0;
+ if (n > 0 and i + n <= grapheme.len and std.unicode.utf8ValidateSlice(grapheme[i .. i + n])) {
+ i += n;
+ continue;
+ }
+ if (run < i) width += vaxis.gwidth.gwidth(grapheme[run..i], .unicode);
+ width += 1;
+ i += 1;
+ run = i;
+ }
+ if (run < grapheme.len) width += vaxis.gwidth.gwidth(grapheme[run..], .unicode);
+ return @max(1, width);
+}
+
+test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width" {
+ try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xeee\xcc\x81"));
+ try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\xee\xffe"));
+ try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\u{4e16}\x80"));
+ // The whole path the 9P fuzzer took: a body holding it, fitted to a width.
+ const text = "\xeee\xcc\x81";
+ try std.testing.expectEqual(@as(usize, 4), fitEnd(text, 0, 10));
}
pub fn byteDisplayWidth(byte: u8) usize {
@@ -433,8 +462,11 @@ test "the ASCII arm of graphemeDisplayWidth matches the gwidth it skips" {
}
for ([_][]const u8{
"e\u{301}", "a\u{903}", "1\u{fe0f}\u{20e3}", "\u{4e16}",
- "\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff", "\xe4\xb8",
+ "\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff",
}) |g| try std.testing.expectEqual(ref(g), graphemeDisplayWidth(g));
+ // Two invalid bytes are two cells, as surface.zig draws them, where
+ // vaxis's own count gives the pair one.
+ try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xe4\xb8"));
}
test "the ASCII run in fitEnd survives an exhaustive byte sweep" {
diff --git a/src/Messages.zig b/src/Messages.zig
index aebaff0c..42200863 100644
--- a/src/Messages.zig
+++ b/src/Messages.zig
@@ -503,18 +503,14 @@ pub fn clip(text: []const u8, max: usize) []const u8 {
/// reportError with the words already chosen.
pub fn reportFailure(p: *Pardes, id: usize, text: []const u8) void {
p.fs.failures +%= 1;
- // A builtin a ctl write runs: its first error is also the write's, cut
- // between words.
- const failing_write = p.fs.no_prompt or p.fs.capturing;
+ // A builtin a ctl write runs: its first error is also the write's, its
+ // path shortened in the middle if it must be, never its reason.
+ const failing_write = p.fs.no_prompt or p.fs.capturing or p.fs.write_waits;
if (failing_write and p.fs.failure_len == 0) {
- const kept = if (text.len > p.fs.failure.len) clip(text, p.fs.failure.len - 3) else text;
+ var room: @TypeOf(p.fs.failure) = undefined;
+ const kept = pardes.ctlfs.fitErr(text, &room);
@memcpy(p.fs.failure[0..kept.len], kept);
- var n = kept.len;
- if (kept.len < text.len) {
- @memcpy(p.fs.failure[n..][0..3], "...");
- n += 3;
- }
- p.fs.failure_len = @intCast(n);
+ p.fs.failure_len = @intCast(kept.len);
}
// The write fails with it, and its err record says it: no msg for it,
// so the same failure again is the same record again, counted
diff --git a/src/builtins.zig b/src/builtins.zig
index 8788e65f..8a01b3d6 100644
--- a/src/builtins.zig
+++ b/src/builtins.zig
@@ -562,7 +562,9 @@ fn warnModifiedIn(c: Ctx, asking: Pane.Discarding, which: Asked) bool {
}
if (count > 0) {
listed.print(arena, "\n{s} again to discard\n", .{@tagName(asking)}) catch {};
- if (c.p.gpa.dupe(u8, listed.items)) |content| panes.Output.openUnsaved(c.p, c.id, content) else |_| {}
+ // A refused Delcol changes nothing, the layout included: its records
+ // and its notice say it, and no +Unsaved opens.
+ if (asking != .Delcol) if (c.p.gpa.dupe(u8, listed.items)) |content| panes.Output.openUnsaved(c.p, c.id, content) else |_| {};
var short: [96]u8 = undefined;
c.p.reportFailure(c.id, std.fmt.bufPrint(&short, "{d} unsaved pane{s} \u{2014} {s} again to discard", .{ count, if (count == 1) "" else "s", @tagName(asking) }) catch "unsaved panes");
}
@@ -572,7 +574,8 @@ fn warnModifiedIn(c: Ctx, asking: Pane.Discarding, which: Asked) bool {
std.fmt.bufPrint(&said_buf, "{s}: Modified ({s} again to discard)", .{ one, @tagName(asking) })
else
std.fmt.bufPrint(&said_buf, "{d} unsaved panes: Modified ({s} again to discard)", .{ count, @tagName(asking) })) catch "unsaved panes: Modified";
- const kept = @import("Messages.zig").clip(said, c.p.fs.failure.len);
+ var room: @TypeOf(c.p.fs.failure) = undefined;
+ const kept = pardes.ctlfs.fitErr(said, &room);
@memcpy(c.p.fs.failure[0..kept.len], kept);
c.p.fs.failure_len = @intCast(kept.len);
}
@@ -1372,11 +1375,12 @@ pub const Pet = struct {
/// Open a minimap of the file in braille dots.
pub const Mini = struct {
pub const takes_arg = true;
+ pub const requires_arg = true; // a file to map: bare, wrong #args (EINVAL)
pub const output: OutputTraits = .{ .name = "Mini", .doc = true };
pub fn run(c: Ctx) void {
panes.Mini.open(c.p, c.id, c.arg orelse "") catch |err|
- c.p.reportError(c.id, "mini", err);
+ c.p.reportError(c.id, "Mini", err);
}
};
@@ -1384,8 +1388,8 @@ pub const Mini = struct {
/// A Find or Grep that finds nothing fails as one, in words 9ns reads as
/// ENOENT (`not found`), the same for both.
-const e_search_miss_find = "find: name not found";
-const e_search_miss_grep = "grep: pattern not found";
+const e_search_miss_find = "Find: name not found";
+const e_search_miss_grep = "Grep: pattern not found";
/// List the files below this pane's directory whose names hold its argument, in a +Search.
pub const Find = struct {
@@ -1395,7 +1399,7 @@ pub const Find = struct {
pub fn run(c: Ctx) void {
const pat = std.mem.trim(u8, c.arg orelse "", " \t\r\n");
if (pat.len > 0) {
- const rows = look.runSearch(c.p, c.id, pat, .find, .top, false) catch |err| return c.p.reportError(c.id, "find", err);
+ const rows = look.runSearch(c.p, c.id, pat, .find, .top, false) catch |err| return c.p.reportError(c.id, "Find", err);
if (rows == 0) c.p.reportFailure(c.id, e_search_miss_find);
return;
}
@@ -1412,7 +1416,7 @@ pub const Grep = struct {
pub fn run(c: Ctx) void {
const pat = std.mem.trim(u8, c.arg orelse "", " \t\r\n");
if (pat.len > 0) {
- const rows = look.runSearch(c.p, c.id, pat, .grep, .top, false) catch |err| return c.p.reportError(c.id, "grep", err);
+ const rows = look.runSearch(c.p, c.id, pat, .grep, .top, false) catch |err| return c.p.reportError(c.id, "Grep", err);
if (rows == 0) c.p.reportFailure(c.id, e_search_miss_grep);
return;
}
@@ -2002,7 +2006,7 @@ test "Exit and Restore name a modified +New scratch as well as files" {
// Delcol, Restore and Exit name it the same way.
p.fs.failure_len = 0;
try std.testing.expect(p.executeBuiltinLine(p.active, "Delcol"));
- try std.testing.expect(std.mem.indexOf(u8, unsavedList(p), "Delcol again to discard") != null);
+ try std.testing.expect(std.mem.indexOf(u8, unsavedList(p), "Delcol again to discard") == null);
try std.testing.expect(std.mem.indexOf(u8, unsavedList(p), named[0 .. config.scratch_buffer.len + 7]) != null);
}
@@ -2015,7 +2019,8 @@ test "Del and Delcol refuse a pane with unsaved text once, as acme's do, and clo
const serial = pane.serial;
try std.testing.expect(p.executeBuiltinLine(p.active, "Delcol"));
try std.testing.expect(p.paneBySerial(serial) != null);
- try std.testing.expect(std.mem.endsWith(u8, unsavedList(p), ": Modified\n\nDelcol again to discard\n"));
+ // A refused Delcol changes nothing: no +Unsaved (its records say it).
+ try std.testing.expect(std.mem.indexOf(u8, unsavedList(p), "Delcol") == null);
// Each word is warned on its own: Del asks too, then closes.
try std.testing.expect(p.executeBuiltinLine(p.active, "Del"));
try std.testing.expect(p.paneBySerial(serial) != null);
diff --git a/src/dump.zig b/src/dump.zig
index fca88a2d..a2efb95d 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -31,7 +31,8 @@ const Pardes = pardes.Pardes;
/// Where the next dump goes. $PARDES_DUMP wins verbatim (the snapshot harness
/// pins it for deterministic goldens); else $XDG_DATA_HOME|~/.local/share
/// /pardes/pardes-<utc>.zon — timestamped so dumps never overwrite each other.
-/// Creates the pardes dir (parents assumed; a failure surfaces at open).
+/// Creates the directory and any parent missing (a fresh HOME has no
+/// ~/.local/share); a failure surfaces at open.
pub fn outPath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 {
if (std.c.getenv("PARDES_DUMP")) |p|
return std.fmt.bufPrintSentinel(buf, "{s}", .{std.mem.span(p)}, 0) catch null;
@@ -40,7 +41,13 @@ pub fn outPath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 {
var dz: [901:0]u8 = undefined;
@memcpy(dz[0..dir.len], dir);
dz[dir.len] = 0;
- _ = std.c.mkdir(dz[0..dir.len :0], 0o755); // EEXIST is fine
+ // Each missing directory on the way, as `mkdir -p`: EEXIST is fine.
+ for (dz[1..dir.len], 1..) |c, i| if (c == '/') {
+ dz[i] = 0;
+ _ = std.c.mkdir(dz[0..i :0], 0o755);
+ dz[i] = '/';
+ };
+ _ = std.c.mkdir(dz[0..dir.len :0], 0o755);
var ts: std.c.timespec = undefined;
_ = std.c.clock_gettime(.REALTIME, &ts);
const es: std.time.epoch.EpochSeconds = .{ .secs = @intCast(@max(0, ts.sec)) };
@@ -109,6 +116,21 @@ pub fn directory(buf: []u8, dir_setting: []const u8) ?[]const u8 {
return std.fmt.bufPrint(buf, "{s}", .{resolved}) catch null;
}
+test "a dump makes every directory missing on the way to its DumpDir" {
+ if (std.c.getenv("PARDES_DUMP") != null) return error.SkipZigTest;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var at: [4096]u8 = undefined;
+ const base = at[0..try tmp.dir.realPath(std.testing.io, &at)];
+ var setting: [4200]u8 = undefined;
+ const dir = try std.fmt.bufPrint(&setting, "{s}/fresh/.local/share/pardes", .{base});
+ var out: [1024:0]u8 = undefined;
+ const path = outPath(&out, dir).?;
+ try std.testing.expect(std.mem.startsWith(u8, path, dir));
+ var made = try tmp.dir.openDir(std.testing.io, "fresh/.local/share/pardes", .{});
+ made.close(std.testing.io);
+}
+
test "a DumpDir reads back absolute, however it was written" {
var buf: [4096]u8 = undefined;
try std.testing.expectEqualStrings("/tmp/dumps", directory(&buf, "/tmp/dumps/").?);
@@ -129,7 +151,7 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 {
pub const magic = "pardes-dump";
pub const version: u32 = 1;
pub const max_panes: usize = MAX_PANES;
-pub const max_cols: usize = 6;
+pub const max_cols: usize = MAX_COLS;
/// Output arguments are typed in the same bounded one-line tag storage. Keep
/// the schema limit named independently so a dump reader can validate it
/// without importing the output-pane implementation. The bound itself is
@@ -987,6 +1009,24 @@ test "a dump keeps the settings that differ from a fresh session's, and a restor
try std.testing.expect(!restored.settings.verbose);
}
+test "a session with every column it may hold dumps and restores them all" {
+ const gpa = std.testing.allocator;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 1200, .rows = 24 });
+ defer p.deinit();
+ while (p.ncol < MAX_COLS) {
+ layout.compute(p);
+ var widest: usize = 0;
+ for (0..p.ncol) |c| if (p.col_w[c] > p.col_w[widest]) {
+ widest = c;
+ };
+ _ = layout.insertColumn(p, widest, false) orelse return error.NoColumn;
+ }
+ try dumpState(p);
+ const restored = try initFromDump(gpa, .{ .tty_only = true, .cols = 1200, .rows = 24 }, p.dump_out.?);
+ defer restored.deinit();
+ try std.testing.expectEqual(@as(usize, MAX_COLS), restored.ncol);
+}
+
test "a restored pane keeps its dot, as acme's dump keeps a window's" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true });
diff --git a/src/exec.zig b/src/exec.zig
index 06e5d375..2c42389c 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -811,9 +811,42 @@ pub fn namesBuiltin(txt: []const u8) bool {
return std.meta.stringToEnum(Builtin, cmd[0..end]) != null;
}
+/// acme's words, run by their pardes names (Put is Save; Delete is a Del
+/// that does not ask), or refused, never run as a shell command: a Put that
+/// exited 127 looked like a save that worked.
+pub const acme_words = [_]struct { acme: []const u8, pardes: ?[]const u8, hint: []const u8 = "" }{
+ .{ .acme = "Put", .pardes = "Save" },
+ .{ .acme = "Delete", .pardes = "Del" },
+ .{ .acme = "Get", .pardes = null, .hint = ": write get to the pane's ctl" },
+ .{ .acme = "Putall", .pardes = null, .hint = ": Save each pane" },
+ .{ .acme = "Snarf", .pardes = null, .hint = ": y yanks the selection" },
+ .{ .acme = "Cut", .pardes = null, .hint = ": d deletes the selection into a register" },
+ .{ .acme = "Paste", .pardes = null, .hint = ": p puts a register" },
+ .{ .acme = "Zerox", .pardes = null },
+ .{ .acme = "Sort", .pardes = null },
+ .{ .acme = "Load", .pardes = null, .hint = ": Restore loads a dump" },
+ .{ .acme = "ID", .pardes = null },
+ .{ .acme = "Send", .pardes = null },
+};
+
pub fn executeBuiltinLine(p: *Pardes, id: usize, txt: []const u8) bool {
- const cmd = commandText(txt);
- if (cmd.len == 0 or p.exec_depth >= max_exec_depth) return false;
+ const raw = commandText(txt);
+ if (raw.len == 0 or p.exec_depth >= max_exec_depth) return false;
+ const word_end = std.mem.indexOfAny(u8, raw, " \t") orelse raw.len;
+ var renamed: [1024 + 16]u8 = undefined;
+ const cmd = for (acme_words) |w| {
+ if (!std.mem.eql(u8, raw[0..word_end], w.acme)) continue;
+ const now = w.pardes orelse {
+ var said: [128]u8 = undefined;
+ p.reportFailure(id, std.fmt.bufPrint(&said, "invalid: acme's {s} is not a pardes builtin{s}", .{ w.acme, w.hint }) catch "invalid: an acme word pardes has not");
+ return true;
+ };
+ // Delete: Del, the text thrown away unasked.
+ if (std.mem.eql(u8, w.acme, "Delete")) if (p.panes[id]) |pane| if (pane.file) |f| {
+ pane.discard_warned = .{ .revision = f.revision, .by = .Del };
+ };
+ break std.fmt.bufPrint(&renamed, "{s}{s}", .{ now, raw[word_end..] }) catch raw;
+ } else raw;
// `Word arg`, or for a word that says so, `Word+arg` (`Tty+fish`).
const plus = std.mem.indexOfScalar(u8, cmd, '+');
var sp = std.mem.indexOfAny(u8, cmd, " \t");
@@ -1117,7 +1150,7 @@ fn pardesPlace(p: *Pardes, from: usize, id: usize, kind: NewPane) void {
.scratch, .fs => if (layout.findPane(p, from)) |sf| {
layout.insert(p, sf.col, sf.idx + 1, id);
layout.splitBelow(p, from, nt);
- },
+ } else atFoot(p, id),
.shell => belowParent(p, from, id),
.doc, .errors => pardesDoc(p, from, id),
.command => {
@@ -1127,9 +1160,18 @@ fn pardesPlace(p: *Pardes, from: usize, id: usize, kind: NewPane) void {
}
}
+/// Where a pane goes when the one it would go under is in no column (Delcol
+/// took them all, or it was never placed): the foot of the active column,
+/// else of the last.
+fn atFoot(p: *Pardes, id: usize) void {
+ const c = p.activeColumn() orelse p.ncol - 1;
+ layout.insert(p, c, p.col_n[c], id);
+ if (p.col_n[c] == 1) p.panes[id].?.vweight = 1;
+}
+
fn belowParent(p: *Pardes, from: usize, id: usize) void {
const parent = layout.splitParent(p, from);
- const f = layout.findPane(p, parent).?;
+ const f = layout.findPane(p, parent) orelse return atFoot(p, id);
layout.insert(p, f.col, f.idx + 1, id);
layout.splitBelow(p, parent, p.panes[id].?);
}
diff --git a/src/fs.zig b/src/fs.zig
index 753a4446..edead0ff 100644
--- a/src/fs.zig
+++ b/src/fs.zig
@@ -1326,6 +1326,31 @@ pub const Namespace = struct {
/// A 9P write asked a language server something: the answer count it
/// waits to change (`turn.lsp_answers`), so its failure is the write's.
lsp_answer_at: ?u64 = null,
+ /// The most a Twrite of the request being served can carry (msize less
+ /// its header), 0 when unknown: a write shorter than it is the whole of
+ /// what its client wrote, one that fills it may be cut and go on.
+ write_room: u32 = 0,
+ /// A 9P write waits on what the editor performs for it (a Save's disk
+ /// write, a shell, a language server): a failure said meanwhile is that
+ /// write's err, so it posts no msg (Messages.reportFailure).
+ write_waits: bool = false,
+ /// The whole text of the newest counted log record, hashed: its record
+ /// may be cut, and a repeat is only the same whole text again.
+ log_full_hash: u64 = 0,
+ /// Writes one open made in a row into one place of a pane's body (an
+ /// append to `body`, an insert at `data`'s address), held and put in
+ /// as one edit when anything else comes (ninep/pane.zig flushBatch): a
+ /// 10 MB write in 8 KB pieces is one copy and one undo step, not 1280.
+ batch: struct {
+ serial: u32 = 0,
+ node: u64 = 0,
+ handle: u32 = 0,
+ at: usize = 0,
+ bytes: std.ArrayList(u8) = .empty,
+ /// The editor's clock at the last write held: its step puts them in
+ /// once the writes have paused (`batch_pause_ns`).
+ last_ns: u64 = 0,
+ } = .{},
/// The serial of the pane the root's exec or look last ran a line at
/// (the keyboard's), 0 before one: its err record names that pane.
ran_at: u32 = 0,
diff --git a/src/look.zig b/src/look.zig
index 79cbd50d..fe5a3ac1 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -1302,6 +1302,41 @@ fn addressed(tok: []const u8) ?struct { path: []const u8, expr: []const u8 } {
return .{ .path = tok[0..colon], .expr = expr };
}
+/// A look at text that names nothing to open: a `./` name is a missing
+/// file, anything else a search for the text as written.
+fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8) void {
+ // A `./` or `../` name opened nothing: said, ENOENT, not searched
+ // for as a word. What is missing is the pane's directory when
+ // that is gone, else the file named.
+ if (filesystem.dotRelative(trimmed)) {
+ const dir = Pardes.paneDir(pane);
+ var said: [4200]u8 = undefined;
+ const gone = if (comptime pardes.hosted) if (filesystem.localPath(dir)) |local| !exec.isDirectory(local) else false else true;
+ if (gone) return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such directory", .{dir[0..@min(dir.len, 4096)]}) catch "look: no such directory");
+ var cut: [miss_path_cap + "…".len]u8 = undefined;
+ return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such file", .{missText(trimmed, &cut)}) catch "look: no such file");
+ }
+ // A raw terminal's clicks are its program's; a look written over
+ // 9P is no keyboard input, and searches whatever the key mode.
+ if (pane.image != null or (pane.body.mode == .tty and !p.fs.serving)) return;
+ if (p.settings.look_word == .search and pane.file != null) return lookWord(p, id, pane, trimmed);
+ const rows = runSearch(p, id, trimmed, .text, .cursor, false) catch |err| {
+ p.reportError(id, "search", err);
+ return;
+ };
+ if (rows == 0) {
+ var cut: [miss_path_cap + "…".len]u8 = undefined;
+ return missed(p, id, "no match for \"{s}\"", .{missText(trimmed, &cut)});
+ }
+ clearNavigationSelection(p, pane);
+ const at = pane.search_row;
+ _ = searchStep(p, id, 1);
+ if (at != null and pane.search_row == at) {
+ pane.search_row = null;
+ _ = searchStep(p, id, 1);
+ }
+}
+
/// Opens (or finds) `path` as Look would, `id`'s own pane when it is empty,
/// and selects what `expr` addresses there from its dot. An address that
/// does not evaluate moves nothing and says why (acme: no jump).
@@ -1321,9 +1356,18 @@ fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void {
const by_serial = std.mem.startsWith(u8, path, config.pane_addr);
if (by_serial and open == null) return missed(p, id, "no pane {s}", .{path[0..@min(path.len, 32)]});
if (open) |o| target = o else if (path.len > 0) {
+ // A path that names nothing to open: the whole of what was written
+ // is looked for, as `zzq:2` is, and a miss quotes it as written.
+ if (!by_serial and resolveLookTarget(p, id, path, &realbuf) == .none)
+ return lookUnresolved(p, id, p.panes[id] orelse return, path.ptr[0 .. path.len + 1 + expr.len]);
const before = p.active;
+ const failures = p.fs.failures;
+ const was_missed = p.look_missed;
lookAt(p, id, path);
target = p.active;
+ // Look said why it opened nothing (its placement refused, say):
+ // that one failure is the look's, no miss of the address after it.
+ if (p.fs.failures != failures or (p.look_missed and !was_missed)) return;
// The path opened nothing (a search, or no such file): Look said so.
if (target == before and target != id) return;
}
@@ -1334,11 +1378,14 @@ fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void {
};
if (!by_serial and path.len > 0 and (f.output != null or !std.mem.endsWith(u8, f.path, std.fs.path.basename(path)))) return;
var a: Addr = .{ .text = f.content, .lim = null, .expr = expr };
+ // A miss names the file it was looked for in, and is that file's pane's.
+ var cut: [miss_path_cap + "…".len]u8 = undefined;
+ const name = missText(f.path, &cut);
const r = a.address(ninep_pane.dotOf(pane)) orelse {
- return missed(p, id, "{s}", .{a.err});
+ return missed(p, target, "{s}: {s}", .{ name, a.err });
};
if (a.i < expr.len or r.q0 > r.q1) {
- return missed(p, id, "{s}", .{if (r.q0 > r.q1) "addresses out of order" else "bad address syntax"});
+ return missed(p, target, "{s}: {s}", .{ name, if (r.q0 > r.q1) "addresses out of order" else "bad address syntax" });
}
clearNavigationSelection(p, pane);
ninep_pane.setDot(pane, r);
@@ -1481,39 +1528,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand: []const u8) void {
pane.sel = @splat(.{});
}
switch (found) {
- .none => {
- // A `./` or `../` name opened nothing: said, ENOENT, not searched
- // for as a word. What is missing is the pane's directory when
- // that is gone, else the file named.
- if (filesystem.dotRelative(trimmed)) {
- const dir = Pardes.paneDir(pane);
- var said: [4200]u8 = undefined;
- const gone = if (comptime pardes.hosted) if (filesystem.localPath(dir)) |local| !exec.isDirectory(local) else false else true;
- if (gone) return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such directory", .{dir[0..@min(dir.len, 4096)]}) catch "look: no such directory");
- var cut: [miss_path_cap + "…".len]u8 = undefined;
- return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such file", .{missText(trimmed, &cut)}) catch "look: no such file");
- }
- // A raw terminal's clicks are its program's; a look written over
- // 9P is no keyboard input, and searches whatever the key mode.
- if (pane.image != null or (pane.body.mode == .tty and !p.fs.serving)) return;
- if (p.settings.look_word == .search and pane.file != null) return lookWord(p, id, pane, trimmed);
- const rows = runSearch(p, id, trimmed, .text, .cursor, false) catch |err| {
- p.reportError(id, "search", err);
- return;
- };
- if (rows == 0) {
- var cut: [miss_path_cap + "…".len]u8 = undefined;
- return missed(p, id, "no match for \"{s}\"", .{missText(trimmed, &cut)});
- }
- clearNavigationSelection(p, pane);
- const at = pane.search_row;
- _ = searchStep(p, id, 1);
- if (at != null and pane.search_row == at) {
- pane.search_row = null;
- _ = searchStep(p, id, 1);
- }
-
- },
+ .none => lookUnresolved(p, id, pane, trimmed),
.pane => |t| {
// `@p<serial>` names no open pane: a miss, said.
if (t.id >= MAX_PANES or p.panes[t.id] == null) return missed(p, id, "no pane {s}", .{pl.path[0..@min(pl.path.len, 32)]});
@@ -1570,7 +1585,11 @@ pub fn lookAt(p: *Pardes, id: usize, operand: []const u8) void {
const nt = panes.File.open(p, free, target.path, target.at.line) catch |err|
return p.reportError(id, if (@import("Messages.zig").dialReason(err) != null) target.path else "look", err);
if (target.at.col > 0) nt.body.cur_col = @intCast(target.at.col - 1);
+ const failures = p.fs.failures;
exec.placeNew(p, id, id, free, .doc);
+ // Refused a place: that is the look's one failure, not a
+ // miss of its line too.
+ if (p.fs.failures != failures) return;
layout.compute(p);
nt.file.?.scroll -|= @max(1, p.rects[free].h -| BOX_H) / 2;
if (target.at.end_line != 0) {
diff --git a/src/main.zig b/src/main.zig
index aaa237f6..ab6f1527 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -58,6 +58,12 @@ const help_text =
\\ A FILE argument boots just that file instead.
\\ --tty-toggle <key> use Ctrl-<key> to toggle tty/editor mode
\\ -l <dump.zon> load a dump of another instance (see Dump)
+ \\ -w, --wait inside a pardes pane, hand FILE to the outer
+ \\ session and return only once the pane it opened
+ \\ (or already had) is deleted: 0 then, 1 if the
+ \\ session goes away. For $EDITOR (acme's E):
+ \\ EDITOR='pardes --wait', which GIT_EDITOR follows.
+ \\ Outside pardes a session blocks anyway.
\\ --nested run a full session even inside another pardes.
\\ Without it, a pardes started inside a pardes
\\ hands its FILE argument to the outer one. This
@@ -92,6 +98,81 @@ const help_text =
\\
;
+/// `--wait` after a forwarded look, as plan9port's E reads acme's log: the
+/// pane `path` landed in (the one it already had, if open), then /log
+/// followed on one connection until that pane's `del` -- exit 0 -- or the
+/// connection ends with the session -- exit 1.
+fn waitForDel(io: std.Io, gpa: std.mem.Allocator, dial: []const u8, path: []const u8) noreturn {
+ const first = ninep_io.Client.read(gpa, dial, "/index", "/index") catch std.process.exit(1);
+ const serial = paneShowing(first, path) orelse {
+ std.Io.File.stderr().writeStreamingAll(io, "pardes: --wait: no pane shows that file\n") catch {};
+ std.process.exit(1);
+ };
+ gpa.free(first);
+ const Wait = struct {
+ gpa: std.mem.Allocator,
+ dial: []const u8,
+ serial: u32,
+ /// Deleted between the look and the follow: /index, read once the
+ /// follow stands, says so, and a `del` after it is followed.
+ fn gone(w: *const @This()) bool {
+ const index = ninep_io.Client.read(w.gpa, w.dial, "/index", "/index") catch return false;
+ defer w.gpa.free(index);
+ return !hasPane(index, w.serial);
+ }
+ fn deleted(w: *const @This(), bytes: []const u8) bool {
+ return isDel(bytes, w.serial);
+ }
+ };
+ const w: Wait = .{ .gpa = gpa, .dial = dial, .serial = serial };
+ ninep_io.Client.follow(gpa, dial, "/log", "follow new", &w, Wait.gone, Wait.deleted) catch std.process.exit(1);
+ std.process.exit(0);
+}
+
+/// Whether log records `bytes` hold `del <serial> ...`.
+fn isDel(bytes: []const u8, serial: u32) bool {
+ var records = std.mem.splitScalar(u8, bytes, '\n');
+ while (records.next()) |rec| {
+ if (!std.mem.startsWith(u8, rec, "del ")) continue;
+ const word = rec[4 .. std.mem.indexOfScalarPos(u8, rec, 4, ' ') orelse rec.len];
+ if ((std.fmt.parseInt(u32, word, 10) catch continue) == serial) return true;
+ }
+ return false;
+}
+
+/// The serial of /index's first row named `path` (`serial kind dirty name col`).
+fn paneShowing(index: []const u8, path: []const u8) ?u32 {
+ var rows = std.mem.splitScalar(u8, index, '\n');
+ while (rows.next()) |row| {
+ var words = std.mem.splitScalar(u8, row, ' ');
+ const serial = std.fmt.parseInt(u32, words.first(), 10) catch continue;
+ _ = words.next() orelse continue; // kind
+ _ = words.next() orelse continue; // dirty
+ const rest = words.rest();
+ const end = std.mem.lastIndexOfScalar(u8, rest, ' ') orelse continue;
+ if (std.mem.eql(u8, rest[0..end], path)) return serial;
+ }
+ return null;
+}
+
+fn hasPane(index: []const u8, serial: u32) bool {
+ var rows = std.mem.splitScalar(u8, index, '\n');
+ while (rows.next()) |row| {
+ const first = row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len];
+ if ((std.fmt.parseInt(u32, first, 10) catch continue) == serial) return true;
+ }
+ return false;
+}
+
+test "--wait finds the pane /index names a file by, spaces and all, and sees it gone" {
+ const index = "1 term 0 /home/u 1\n4 text 1 /tmp/a b.txt 2\n";
+ try std.testing.expectEqual(@as(?u32, 4), paneShowing(index, "/tmp/a b.txt"));
+ try std.testing.expectEqual(@as(?u32, null), paneShowing(index, "/tmp/a"));
+ try std.testing.expect(hasPane(index, 4) and !hasPane(index, 2));
+ try std.testing.expect(isDel("new 5 /x\ndel 4 /tmp/a b.txt\n", 4));
+ try std.testing.expect(!isDel("del 41 /x\n", 4) and !isDel("undel 4\n", 4));
+}
+
fn badArgs(io: std.Io, comptime fmt: []const u8, args: anytype) noreturn {
var buf: [1024]u8 = undefined;
const line = std.fmt.bufPrint(&buf, "pardes: " ++ fmt ++ "\nTry 'pardes --help'.\n", args) catch
@@ -159,6 +240,7 @@ fn nativeMain(init: std.process.Init) !void {
var detach: ?[]const u8 = null;
var attach: ?[]const u8 = null;
var positional: ?[:0]const u8 = null;
+ var wait = false;
var i: usize = 1;
while (i < args.len) : (i += 1) {
const a = args[i];
@@ -239,6 +321,8 @@ fn nativeMain(init: std.process.Init) !void {
mounts_len += 1;
} else if (std.mem.eql(u8, a, "--nested")) {
opts.nested = true;
+ } else if (std.mem.eql(u8, a, "-w") or std.mem.eql(u8, a, "--wait")) {
+ wait = true;
} else if (std.mem.eql(u8, a, "--detach")) {
detach = "";
} else if (std.mem.startsWith(u8, a, "--detach=")) {
@@ -324,10 +408,31 @@ fn nativeMain(init: std.process.Init) !void {
if (std.mem.indexOfAny(u8, word, "\r\n") != null) break :forwarding;
const target = @import("look.zig").parsePathLine(word);
var realbuf: [4096]u8 = undefined;
- const path = if (pardes.filesystem.isVirtual(target.path)) target.path else (pardes.filesystem.resolveOs(target.path, &realbuf) orelse break :forwarding).path;
- var command_buf: [8192]u8 = undefined;
- const command = std.fmt.bufPrint(&command_buf, "{s}{s}\n", .{ path, word[target.path.len..] }) catch break :forwarding;
- ninep_io.Client.write(arena, parent.dial, look, command) catch break :forwarding;
+ var newbuf: [4096]u8 = undefined;
+ const found_path: ?[]const u8 = if (pardes.filesystem.isVirtual(target.path)) target.path else if (pardes.filesystem.resolveOs(target.path, &realbuf)) |r| r.path else null;
+ const path = found_path orelse named: {
+ // A name not there yet, as acme's B takes one: its directory
+ // resolved and the name kept, a pane made for it that Save
+ // creates the file from.
+ const base = std.fs.path.basename(target.path);
+ if (base.len == 0 or std.mem.eql(u8, base, ".") or std.mem.eql(u8, base, "..")) break :forwarding;
+ const dir = pardes.filesystem.resolveOs(std.fs.path.dirname(target.path) orelse ".", &realbuf) orelse break :forwarding;
+ break :named std.fmt.bufPrint(&newbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir.path, "/"), base }) catch break :forwarding;
+ };
+ if (found_path != null) {
+ var command_buf: [8192]u8 = undefined;
+ const command = std.fmt.bufPrint(&command_buf, "{s}{s}\n", .{ path, word[target.path.len..] }) catch break :forwarding;
+ ninep_io.Client.write(arena, parent.dial, look, command) catch break :forwarding;
+ } else {
+ const made = ninep_io.Client.read(arena, parent.dial, "/pane/new", "/pane/new") catch break :forwarding;
+ const serial = std.fmt.parseInt(u32, std.mem.trim(u8, made, " \n"), 10) catch break :forwarding;
+ var name_buf: [64]u8 = undefined;
+ const name = try std.fmt.bufPrint(&name_buf, "/pane/{d}/name", .{serial});
+ var line_buf: [4200]u8 = undefined;
+ const line = std.fmt.bufPrint(&line_buf, "{s}\n", .{path}) catch break :forwarding;
+ ninep_io.Client.write(arena, parent.dial, name, line) catch break :forwarding;
+ }
+ if (wait) waitForDel(init.io, arena, parent.dial, path);
return;
}
if (positional) |a| {
@@ -383,7 +488,15 @@ fn nativeMain(init: std.process.Init) !void {
std.process.exit(1);
}
switch (pardes.platform) {
- .tty => try @import("tty/tty.zig").run(init, opts, attach),
+ .tty => @import("tty/tty.zig").run(init, opts, attach) catch |err| switch (err) {
+ // No controlling terminal (a detached pty, a daemon): said, not
+ // an error trace.
+ error.NoDevice => {
+ std.Io.File.stderr().writeStreamingAll(init.io, "pardes: no terminal to draw on: /dev/tty will not open; run it in a terminal, or --detach\n") catch {};
+ std.process.exit(1);
+ },
+ else => return err,
+ },
.gui => try @import("gui/gui.zig").run(init, opts, attach),
.web, .macos, .esp32p4 => unreachable,
}
diff --git a/src/ninep/addr.zig b/src/ninep/addr.zig
index f60c07b1..33a3f646 100644
--- a/src/ninep/addr.zig
+++ b/src/ninep/addr.zig
@@ -17,7 +17,7 @@ pub const e_col_zero = "address out of range: a column counts from 1";
pub const e_regexp = "bad regular expression";
/// Not "took too long": 9ns reads errors by their words, and that would be
/// ENAMETOOLONG.
-pub const e_slow = "regular expression search gave up, backtracking past its step budget";
+pub const e_slow = "regular expression search took too much time, gave up";
pub const e_syntax = "bad address syntax";
pub const e_order = "addresses out of order";
@@ -299,7 +299,13 @@ pub const Addr = struct {
return null;
}
var rx = regexp_.Regex.compile(pat) catch |err| {
- a.err = if (err == error.Anchor) regexp_.Regex.e_anchor else e_regexp;
+ a.err = switch (err) {
+ error.Anchor => regexp_.Regex.e_anchor,
+ error.TooLong => regexp_.Regex.e_long,
+ error.WideRange => regexp_.Regex.e_wide,
+ error.NegatedRunes => regexp_.Regex.e_negated,
+ error.Bad => e_regexp,
+ };
return null;
};
// sam's nextmatch (editors/sam/address.c:97-119): an empty match
diff --git a/src/ninep/cols.zig b/src/ninep/cols.zig
index 6b59f9e5..cc583c93 100644
--- a/src/ninep/cols.zig
+++ b/src/ninep/cols.zig
@@ -368,10 +368,11 @@ test "a column's ctl and exec act on it as its tag would, and rmdir closes it on
try testing.expectEqual(E.INVAL, th.wr(p, Node.ofCol(empty, .ctl), "Exit\n").errno());
var rec: [64]u8 = undefined;
try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "err - col/{d}/ctl: unknown control message \"Exit\"", .{empty})));
- // rmdir refuses a column with a pane, and says why.
+ // rmdir refuses a column with a pane, and says why, in its error alone.
const full = th.call(p, .{ .tag = 1, .op = .release, .node = Node.ofCol(empty, .dir), .remove = true });
try testing.expectEqual(E.NOTEMPTY, full.errno());
- try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "err - col/{d}: column not empty", .{empty})));
+ try testing.expect(std.mem.startsWith(u8, full.reply.ename, "column not empty"));
+ try testing.expect(!th.logHas(p, try std.fmt.bufPrint(&rec, "err - col/{d}: column not empty", .{empty})));
// exec runs a word as a click in that column's tag: Delcol closes it.
try testing.expectEqual(tree.Status.ok, th.wr(p, Node.ofCol(empty, .exec), "Delcol\n").reply.status);
try testing.expectEqual(@as(?usize, null), layout.columnBySerial(p, empty));
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index 397f3339..5bf354eb 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -251,8 +251,9 @@ pub fn captured(p: *Pardes, req: Req, id: usize, exec: bool, text: []const u8, i
/// EIO.
fn failureErrno(failure: []const u8) u16 {
if (std.mem.indexOf(u8, failure, "no such") != null or std.mem.indexOf(u8, failure, "not found") != null) return E.NOENT;
- // A pattern refused (Edit's too) is malformed input, as 9ns reads it.
- if (std.mem.indexOf(u8, failure, "bad regular expression") != null) return E.INVAL;
+ // A pattern refused (Edit's too) is malformed input, as 9ns reads it;
+ // so is a word this build has not (`invalid: acme's Put ...`).
+ if (std.mem.indexOf(u8, failure, "bad regular expression") != null or std.mem.startsWith(u8, failure, "invalid")) return E.INVAL;
return E.IO;
}
@@ -330,13 +331,13 @@ fn writeQuoted(w: *std.Io.Writer, s: []const u8) !void {
try w.writeByte('\'');
}
-pub const pane_text_cap = 7 * 12 + 2 * 256 + 4 + "notcurrent ".len + 48;
+pub const pane_text_cap = 9 * 12 + 2 * 256 + 4 + "notcurrent ".len + 48;
-/// acme's winctlprint: serial, tag length, body length, a reserved zero, the
-/// dirty flag, then the width in cells, the font and the tab width. Then
-/// rio's word for whether the window has the keyboard, `current` or
-/// `notcurrent` (rio(4), wctl), which acme's line has no room for because
-/// acme has one window for input at a time only by the mouse.
+/// acme's winctlprint (plan9port wind.c), field for field: serial, tag
+/// length, body length, isdir (0), the dirty flag, the width in cells, the
+/// font, the tab width, whether Undo has a step, whether Redo has one. Then
+/// pardes's own: rio's word for whether the window has the keyboard,
+/// `current` or `notcurrent` (rio(4), wctl), and a REPL's id.
pub fn paneText(p: *Pardes, pane: *Pane, buf: []u8) []const u8 {
var w = std.Io.Writer.fixed(buf);
w.print("{d:>11} {d:>11} {d:>11} {d:>11} {d:>11} {d:>11} ", .{
@@ -352,7 +353,10 @@ pub fn paneText(p: *Pardes, pane: *Pane, buf: []u8) []const u8 {
writeQuoted(&w, if (font.len == 0) "default" else font) catch return w.buffered();
// While a column's or the workspace's tag has the keyboard, no pane does.
const current = p.panes[p.active] == pane and !p.header_focus;
- w.print(" {d:>11} {s} ", .{ config.tab_width, if (current) "current" else "notcurrent" }) catch {};
+ const f = pane_files.fileOf(pane);
+ const undo = if (f) |file| file.history.undo_len > 0 else false;
+ const redo = if (f) |file| file.history.redo_len > 0 else false;
+ w.print(" {d:>11} {d:>11} {d:>11} {s} ", .{ config.tab_width, @intFromBool(undo), @intFromBool(redo), if (current) "current" else "notcurrent" }) catch {};
// A terminal bound as a REPL ends with its id, `python-a`.
var idbuf: [48]u8 = undefined;
const id = exec_line.replId(&idbuf, pane);
@@ -387,7 +391,13 @@ pub fn refuseTo(p: *Pardes, req: Req, why: []const u8, line: []const u8, ctl: []
/// as on a tag; acme's lowercase verbs are the pane ctl's and alias none.
fn checkBuiltin(p: *Pardes, req: Req, line: []const u8, scope: builtins.Scope) ?Reply {
const sp = std.mem.indexOfAny(u8, line, " \t");
- const word = line[0 .. sp orelse line.len];
+ const typed = line[0 .. sp orelse line.len];
+ // acme's words: checked as the pardes word they run as, or refused.
+ const word = for (pardes.exec.acme_words) |w| {
+ if (!std.mem.eql(u8, typed, w.acme)) continue;
+ const now = w.pardes orelse return tree.failText(req.tag, E.INVAL, std.fmt.bufPrint(&p.fs.ename, "invalid: acme's {s} is not a pardes builtin{s}", .{ w.acme, w.hint }) catch "invalid: an acme word pardes has not");
+ break now;
+ } else typed;
const arg = if (sp) |s| std.mem.trim(u8, line[s + 1 ..], " \t") else "";
const b = std.meta.stringToEnum(Builtin, word) orelse {
// A setting this build's frontend cannot show (Lift, GripWidth on a
@@ -777,10 +787,19 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply {
if (apply) p.fs.results_len = 0;
var held = !other and pane.fs.lock != null;
var it = Messages.init(p, req.data);
- while (it.next()) |line| {
- if (line.len == 0) continue;
+ while (it.next()) |line_in| {
+ if (line_in.len == 0) continue;
// A line before this one may have closed the pane (`Del`).
if (apply and p.paneBySerial(serial) == null) return Reply.fail(req.tag, E.NOENT);
+ // acme's ctl words: done by the file that replaces each, or the
+ // builtin that does it, checked with the rest before any runs.
+ var line = line_in;
+ switch (acmeCtl(p, req, serial, line_in, apply)) {
+ .no => {},
+ .done => continue,
+ .refused => |r| return r,
+ .builtin => |word| line = word,
+ }
if (std.mem.eql(u8, line, "get")) {
if (!apply) continue;
// Quoting the line, as a builtin's refusal (Save's) does.
@@ -854,6 +873,72 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply {
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
}
+/// acme's pane ctl vocabulary (editors/acme/xfid.c xfidctlwrite): each word
+/// done here by the file that replaces it, or run as the builtin that does
+/// it; those with no place here refused with why.
+const AcmeCtl = union(enum) { no, done, refused: Reply, builtin: []const u8 };
+
+fn acmeCtl(p: *Pardes, req: Req, serial: u32, line: []const u8, apply: bool) AcmeCtl {
+ const id = p.paneBySerial(serial) orelse return .no;
+ const pane = p.panes[id].?;
+ const word = line[0 .. std.mem.indexOfScalar(u8, line, ' ') orelse line.len];
+ const rest = std.mem.trim(u8, line[word.len..], " ");
+ const File = tree.PaneFile;
+ // A word written to one of the pane's own files, as acme's is to its.
+ const Into = struct { file: File, data: []const u8 };
+ const into: ?Into = if (std.mem.eql(u8, word, "name") and rest.len > 0)
+ .{ .file = .name, .data = rest }
+ else if (std.mem.eql(u8, line, "clean"))
+ .{ .file = .dirty, .data = "0" }
+ else if (std.mem.eql(u8, line, "dirty"))
+ .{ .file = .dirty, .data = "1" }
+ else if (std.mem.eql(u8, line, "mark"))
+ .{ .file = .mark, .data = "1" }
+ else if (std.mem.eql(u8, line, "nomark"))
+ .{ .file = .mark, .data = "0" }
+ else
+ null;
+ if (into) |w| {
+ if (!apply) return .done;
+ const r = pane_files.write(p, .{ .tag = req.tag, .op = .write, .node = tree.Node.of(serial, w.file), .data = w.data }, id, pane, w.file);
+ return if (r.status == .err) .{ .refused = r } else .done;
+ }
+ if (std.mem.eql(u8, line, "put")) return .{ .builtin = "Save" };
+ if (std.mem.eql(u8, line, "del")) return .{ .builtin = "Del" };
+ if (std.mem.eql(u8, line, "delete")) return .{ .builtin = "Delete" };
+ const pf = &pane.fs;
+ if (std.mem.eql(u8, line, "dot=addr")) {
+ if (apply) pane_files.setDot(pane, pf.addr);
+ return .done;
+ }
+ if (std.mem.eql(u8, line, "addr=dot")) {
+ if (apply) pf.addr = pane_files.dotOf(pane);
+ return .done;
+ }
+ if (std.mem.eql(u8, line, "limit=addr")) {
+ if (apply) pf.limit = pf.addr;
+ return .done;
+ }
+ if (std.mem.eql(u8, line, "show")) {
+ if (apply) pane_files.showOffset(pane, pane_files.dotOf(pane).q0);
+ return .done;
+ }
+ if (std.mem.eql(u8, line, "cleartag")) {
+ if (apply) _ = pane_files.truncate(p, pane, .tag);
+ return .done;
+ }
+ const none: ?[]const u8 = if (std.mem.eql(u8, word, "dump") or std.mem.eql(u8, word, "dumpdir"))
+ "a pane is dumped with the session: Dump and DumpDir on /ctl"
+ else if (std.mem.eql(u8, word, "font"))
+ "the font is the session's: Font on /ctl"
+ else if (std.mem.eql(u8, line, "menu") or std.mem.eql(u8, line, "nomenu"))
+ "pardes has no menu"
+ else
+ null;
+ if (none) |why| return .{ .refused = tree.failText(req.tag, E.INVAL, std.fmt.bufPrint(&p.fs.ename, "invalid: acme's {s}: {s}", .{ word, why }) catch "invalid: acme's ctl word") };
+ return .no;
+}
+
const e_not_locked = "pane not locked by this open";
/// Who holds it is another open of this ctl, whose client pardes cannot name.
const e_locked = tree.e_in_use ++ ": another open of this ctl holds the lock";
@@ -922,7 +1007,7 @@ const root_status = @intFromEnum(tree.TopFile.status);
const root_look = @intFromEnum(tree.TopFile.look);
const root_exec = @intFromEnum(tree.TopFile.exec);
-test "pane ctl read is index's five fields plus width in cells, font, tab width and whether it is current" {
+test "pane ctl read is acme's fields -- index's five, width in cells, font, tab width, undo, redo -- then whether it is current" {
const gpa = testing.allocator;
const p = try withFile(gpa, "x\n");
defer p.deinit();
@@ -932,8 +1017,8 @@ test "pane ctl read is index's five fields plus width in cells, font, tab width
try testing.expectEqual(Status.ok, a.reply.status);
var want: std.ArrayList(u8) = .empty;
defer want.deinit(gpa);
- try want.print(gpa, "{d:>11} {d:>11} {d:>11} {d:>11} {d:>11} {d:>11} {s} {d:>11} current ", .{
- pane.serial, pane_files.tagOf(p, pane).len, @as(usize, 2), 0, 0, pane.cols, "default", config.tab_width,
+ try want.print(gpa, "{d:>11} {d:>11} {d:>11} {d:>11} {d:>11} {d:>11} {s} {d:>11} {d:>11} {d:>11} current ", .{
+ pane.serial, pane_files.tagOf(p, pane).len, @as(usize, 2), 0, 0, pane.cols, "default", config.tab_width, 0, 0,
});
try testing.expectEqualStrings(want.items, a.bytes);
try testing.expectEqual(@as(u64, a.bytes.len), call(p, .{ .tag = 1, .op = .getattr, .node = Node.of(pane.serial, .ctl) }).reply.attr.size);
@@ -1034,13 +1119,9 @@ test "the pane ctl takes acme's verbs and the pane's builtins, and refuses the r
defer p.deinit();
const serial = serialOf(p);
const ctl_node = Node.of(serial, .ctl);
- // acme's other ctl words have files of their own here, and a builtin is
- // its own capitalised word: `Get`, `DEL` and `del` are none of them.
- for ([_][]const u8{
- "menu", "nomenu", "dump echo hi", "font Go Mono", "lock x", "bogus", "DEL",
- "put", "del", "delete", "clean", "dirty", "show", "mark",
- "Get", "limit=addr", "get x", "look /tmp", "exec Del",
- }) |bad| {
+ // A builtin is its own capitalised word: `GET` and `DEL` are none;
+ // acme's own words are done or refused as acme's (the test below).
+ for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| {
const refused = wr(p, ctl_node, bad);
try testing.expectEqual(E.INVAL, refused.errno());
var want: [64]u8 = undefined;
@@ -1671,6 +1752,59 @@ test "a look after Newcol fills the empty column the keyboard is on" {
try testing.expectEqual(p.active, p.col_panes[p.ncol - 1][0]);
}
+test "a look at file:addr whose file is not there is one miss, quoting what was written, in a terminal too" {
+ const p = try th.withTerm(testing.allocator);
+ defer p.deinit();
+ const looked = Node.of(serialOf(p), .look);
+ for ([_][]const u8{ "zzq:#3", "zzq:2", "zzq:/re/" }) |written| {
+ var line: [64]u8 = undefined;
+ _ = wr(p, looked, try std.fmt.bufPrint(&line, "{s}\n", .{written}));
+ var want: [64]u8 = undefined;
+ try testing.expectEqual(@as(usize, 1), th.logCount(p, try std.fmt.bufPrint(&want, "look: no match for \"{s}\"\n", .{written})));
+ }
+ try testing.expectEqual(@as(usize, 3), th.logCount(p, "\nerr "));
+}
+
+test "a look whose pane is refused a place fails with that alone, not a missing line too" {
+ const gpa = testing.allocator;
+ // A screen with rows for one pane only.
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 5 });
+ defer p.deinit();
+ while (p.nextEffect()) |_| {}
+ _ = try p.setTestFile("x\n");
+ while (p.nextEffect()) |_| {}
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "one\ntwo\n" });
+ var dir: [4096]u8 = undefined;
+ const at = dir[0..try tmp.dir.realPath(testing.io, &dir)];
+ var line: [4200]u8 = undefined;
+ const r = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/f.txt:2\n", .{at}));
+ try testing.expectEqual(Status.err, r.reply.status);
+ try testing.expect(std.mem.indexOf(u8, r.reply.ename, "no space for a pane") != null);
+ try testing.expectEqual(@as(usize, 1), th.logCount(p, "\nerr "));
+}
+
+test "a look under pardes's placement, from a pane pane/new made after Delcol, opens its file" {
+ const gpa = testing.allocator;
+ const p = try withFile(gpa, "x\n");
+ defer p.deinit();
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "file\n" });
+ var dir: [4096]u8 = undefined;
+ const at = dir[0..try tmp.dir.realPath(testing.io, &dir)];
+ var line: [4200]u8 = undefined;
+ const first = serialOf(p);
+ try testing.expectEqual(tree.Status.ok, wr(p, Node.of(first, .exec), "Placement\n").reply.status);
+ try testing.expectEqual(tree.Status.ok, wr(p, Node.of(first, .exec), "Delcol\n").reply.status);
+ const made = try th.newPane(p);
+ try testing.expectEqual(tree.Status.ok, wr(p, Node.of(made, .look), try std.fmt.bufPrint(&line, "{s}/f.txt:#4\n", .{at})).reply.status);
+ // The pane pane/new made is placed, and so is the one its look opened.
+ for (p.panes, 0..) |slot, i| if (slot != null) try testing.expect(layout.findPane(p, i) != null);
+ try testing.expect(p.panes[p.active].?.serial != made);
+}
+
test "a Restore of a file that is no dump fails the write before any warning, and a failed Dump fails its write" {
if (comptime !pardes.hosted) return error.SkipZigTest;
const p = try withFile(testing.allocator, "x\n");
@@ -1924,10 +2058,10 @@ test "Grep with no hit says grep: no match and opens nothing, a missing pane dir
for (p.panes) |slot| count += @intFromBool(slot != null);
const missed = wr(p, Node.of(serialOf(p), .exec), "Grep zzqqnomatchzz\n");
try testing.expectEqual(E.NOENT, missed.errno());
- try testing.expectEqualStrings("grep: pattern not found", missed.reply.ename);
+ try testing.expectEqualStrings("Grep: pattern not found", missed.reply.ename);
const unfound = wr(p, Node.of(serialOf(p), .exec), "Find zzqqnomatchzz\n");
try testing.expectEqual(E.NOENT, unfound.errno());
- try testing.expectEqualStrings("find: name not found", unfound.reply.ename);
+ try testing.expectEqualStrings("Find: name not found", unfound.reply.ename);
var after: usize = 0;
for (p.panes) |slot| after += @intFromBool(slot != null);
try testing.expectEqual(count, after);
@@ -2234,6 +2368,133 @@ test "a buffer whose file was deleted on disk is dirty, and Del asks first" {
try testing.expect(p.paneBySerial(serial) == null);
}
+test "a failure said while a 9P write waits on it posts no msg, only the write's err" {
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ p.fs.write_waits = true;
+ p.saveFailed(@intCast(p.active), "/nowhere/zz.txt", error.FileNotFound);
+ p.fs.write_waits = false;
+ try testing.expect(!th.logHas(p, "msg "));
+ try testing.expect(p.fs.late_failure_len > 0);
+}
+
+test "Mini with no argument is wrong #args, EINVAL, as every word that needs one" {
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ const r = wr(p, Node.of(serialOf(p), .ctl), "Mini\n");
+ try testing.expectEqual(E.INVAL, r.errno());
+ try testing.expectEqualStrings("wrong #args in control message \"Mini\"", r.reply.ename);
+}
+
+test "a look that misses file:/re/ names the file, and is logged as its pane's" {
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "alpha\n" });
+ var dir_buf: [4096]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
+ var line: [4200]u8 = undefined;
+ _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/f.txt\n", .{dir}));
+ p.sync();
+ const f_serial = p.panes[p.active].?.serial;
+ _ = wr(p, Node.of(serialOf(p), .look), try std.fmt.bufPrint(&line, "{s}/f.txt:/zzqq/\n", .{dir}));
+ var want: [4300]u8 = undefined;
+ try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "err {d} look: {s}/f.txt: no match for regexp", .{ f_serial, dir })));
+}
+
+test "acme's words run as pardes's (Put is Save, Delete a Del that does not ask) or are refused, never run as commands" {
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ for ([_][]const u8{ "Snarf\n", "Putall\n", "Zerox\n", "Get\n" }) |line| {
+ for ([_]u64{ Node.of(serial, .ctl), Node.of(serial, .exec) }) |node| {
+ const r = wr(p, node, line);
+ try testing.expectEqual(E.INVAL, r.errno());
+ try testing.expect(std.mem.indexOf(u8, r.reply.ename, "is not a pardes builtin") != null);
+ }
+ }
+ // Put saves, as Save does.
+ _ = wr(p, Node.of(serial, .name), "/tmp/pardes-put.txt\n");
+ const put = wr(p, Node.of(serial, .exec), "Put\n");
+ try testing.expectEqual(Status.ok, put.reply.status);
+ try testing.expect(put.saved);
+ // Delete closes an edited pane without asking.
+ const made = try th.newPane(p);
+ _ = wr(p, Node.of(made, .name), "/tmp/pardes-delete.txt\n");
+ _ = wr(p, Node.of(made, .body), "edited\n");
+ try testing.expectEqual(Status.ok, wr(p, Node.of(made, .ctl), "Delete\n").reply.status);
+ p.sync();
+ try testing.expect(p.paneBySerial(made) == null);
+}
+
+test "a pane's ctl takes acme's words: done by the files that replace them, or refused with why" {
+ const p = try withFile(testing.allocator, "one two\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ const ctl_node = Node.of(serial, .ctl);
+ const pane = p.panes[p.paneBySerial(serial).?].?;
+ try testing.expectEqual(Status.ok, wr(p, ctl_node, "name /tmp/pardes-acme-ctl.txt\n").reply.status);
+ try testing.expectEqualStrings("/tmp/pardes-acme-ctl.txt", pane.file.?.path);
+ try testing.expectEqual(Status.ok, wr(p, ctl_node, "dirty\n").reply.status);
+ try testing.expectEqualStrings("1", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n"));
+ try testing.expectEqual(Status.ok, wr(p, ctl_node, "clean\n").reply.status);
+ try testing.expectEqualStrings("0", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n"));
+ _ = wr(p, Node.of(serial, .addr), "/two/");
+ try testing.expectEqual(Status.ok, wr(p, ctl_node, "dot=addr\nshow\nnomark\nmark\nlimit=addr\n").reply.status);
+ try testing.expectEqual(@as(u32, 4), pane_files.dotOf(pane).q0);
+ try testing.expect(pane.fs.limit != null);
+ try testing.expectEqual(Status.ok, wr(p, ctl_node, "cleartag\n").reply.status);
+ for ([_][]const u8{ "menu", "nomenu", "dump echo hi", "dumpdir /tmp", "font Go Mono" }) |line| {
+ const r = wr(p, ctl_node, line);
+ try testing.expectEqual(E.INVAL, r.errno());
+ try testing.expect(std.mem.startsWith(u8, r.reply.ename, "invalid: acme's "));
+ }
+ // Checked whole: a refused word anywhere and nothing ran.
+ try testing.expectEqual(E.INVAL, wr(p, ctl_node, "dirty\nmenu\n").errno());
+ try testing.expectEqualStrings("0", std.mem.trimEnd(u8, rd(p, Node.of(serial, .dirty), 0, 8).bytes, "\n"));
+}
+
+test "ctl reads acme's fields in acme's order, undo and redo flags included, then pardes's own" {
+ const p = try withFile(testing.allocator, "one\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ const ctl_node = Node.of(serial, .ctl);
+ const fields = struct {
+ fn of(text: []const u8, buf: *[16][]const u8) [][]const u8 {
+ var it = std.mem.tokenizeScalar(u8, text, ' ');
+ var n: usize = 0;
+ while (it.next()) |t| : (n += 1) buf[n] = t;
+ return buf[0..n];
+ }
+ };
+ var buf: [16][]const u8 = undefined;
+ var f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf);
+ try testing.expectEqualStrings("0", f[8]); // nothing to undo
+ try testing.expectEqualStrings("0", f[9]); // nothing to redo
+ try testing.expect(std.mem.eql(u8, f[10], "current") or std.mem.eql(u8, f[10], "notcurrent"));
+ _ = wr(p, Node.of(serial, .body), "two\n");
+ f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf);
+ try testing.expectEqualStrings("1", f[8]);
+ _ = wr(p, ctl_node, "Undo\n");
+ f = fields.of(rd(p, ctl_node, 0, 4096).bytes, &buf);
+ try testing.expectEqualStrings("1", f[9]);
+}
+
+test "a write to addr, limit or mark asks for no frame; one to body does" {
+ const p = try withFile(testing.allocator, "one\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ for ([_]tree.PaneFile{ .addr, .limit, .mark }) |file| {
+ p.needs_frame = false;
+ _ = p.serveFs(.{ .tag = 1, .op = .write, .node = Node.of(serial, file), .data = if (file == .mark) "1" else "#0" });
+ try testing.expect(!p.needs_frame);
+ }
+ p.needs_frame = false;
+ _ = p.serveFs(.{ .tag = 1, .op = .write, .node = Node.of(serial, .body), .data = "two\n" });
+ try testing.expect(p.needs_frame);
+}
+
test "every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
@@ -2960,7 +3221,7 @@ test "@p<serial>:<address> takes any sam address; a miss and an unknown serial a
}
var line: [64]u8 = undefined;
_ = wr(p, root, try std.fmt.bufPrint(&line, "@p{d}:/zzqq/\n", .{serial}));
- try testing.expect(th.logHas(p, "look: no match for regexp"));
+ try testing.expect(th.logHas(p, ": no match for regexp"));
try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes);
_ = wr(p, root, "@p999:/foo/\n");
try testing.expect(th.logHas(p, "look: no pane @p999"));
@@ -3020,3 +3281,36 @@ test "@p<terminal>:<address> takes any address over the terminal's lines, select
try testing.expectEqual(row_before, pane.body.cur_row);
try testing.expectEqualStrings("", rd(p, root, 0, 64).bytes);
}
+
+test "a bad command inside an Edit block written whole is named, not the } after it" {
+ const p = try withFile(testing.allocator, "abc\n");
+ defer p.deinit();
+ const r = wr(p, Node.of(serialOf(p), .ctl), "Edit {\n,s/a/b/\nzq\n}\n");
+ try testing.expectEqual(Status.err, r.reply.status);
+ try testing.expect(std.mem.indexOf(u8, r.reply.ename, "\"}\"") == null);
+ try testing.expect(std.mem.indexOf(u8, r.reply.ename, "z") != null);
+ try testing.expectEqualStrings("abc\n", p.panes[0].?.file.?.content);
+}
+
+test "a refused Delcol changes nothing: no pane, no focus" {
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ _ = wr(p, @intFromEnum(tree.TopFile.ctl), "Newcol\n");
+ const made = try th.newPane(p);
+ _ = wr(p, Node.of(made, .name), "/tmp/pardes-delcol-refused.txt\n");
+ _ = wr(p, Node.of(made, .body), "edit\n");
+ p.sync();
+ const col = layout.findPane(p, p.paneBySerial(made).?).?.col;
+ const active = p.active;
+ const active_column = p.active_column;
+ var count: usize = 0;
+ for (p.panes) |slot| count += @intFromBool(slot != null);
+ try testing.expectEqual(Status.err, wr(p, Node.ofCol(layout.columnSerial(p, col), .ctl), "Delcol\n").reply.status);
+ p.sync();
+ var after: usize = 0;
+ for (p.panes) |slot| after += @intFromBool(slot != null);
+ try testing.expectEqual(count, after);
+ try testing.expectEqual(active, p.active);
+ try testing.expectEqual(active_column, p.active_column);
+ try testing.expect(th.logHas(p, "unsaved "));
+}
diff --git a/src/ninep/events.zig b/src/ninep/events.zig
index 570cae78..fe7af77e 100644
--- a/src/ninep/events.zig
+++ b/src/ninep/events.zig
@@ -171,11 +171,11 @@ pub fn noteMessage(p: *Pardes, serial: u32, said: []const u8) void {
pushCounting(p, (if (serial == 0)
std.fmt.bufPrint(&buf, "msg - {s}{s}\n", .{ kept, cut })
else
- std.fmt.bufPrint(&buf, "msg {d} {s}{s}\n", .{ serial, kept, cut })) catch return);
+ std.fmt.bufPrint(&buf, "msg {d} {s}{s}\n", .{ serial, kept, cut })) catch return, said);
}
-/// Records `err <serial|-> <file>: <why>` for a write or truncation that was
-/// refused or failed: through a mount a shell sees only the errno the
+/// Records `err <serial|-> <file>: <why>` for a write that was refused or
+/// failed: through a mount a shell sees only the errno the
/// kernel mapped the reply to (`Invalid argument`), and here is the reason.
/// The log is the one place for it, as acme's `errors` file takes text and
/// answers nothing: a per-pane readable error file would be a second.
@@ -186,7 +186,7 @@ pub fn noteMessage(p: *Pardes, serial: u32, said: []const u8) void {
pub fn noteErrCounted(p: *Pardes, text: []const u8) void {
bootFirst(p);
var buf: [pardes.memory.limits.host_path_cap + 16]u8 = undefined;
- pushCounting(p, std.fmt.bufPrint(&buf, "err {s}\n", .{text}) catch return);
+ pushCounting(p, std.fmt.bufPrint(&buf, "err {s}\n", .{text}) catch return, text);
}
pub fn notePath(p: *Pardes, what: []const u8, path: []const u8) void {
@@ -239,7 +239,7 @@ pub fn noteError(p: *Pardes, req: Req, reply: Reply) void {
for (record[0 .. record.len - 1]) |*c| if (c.* < ' ') {
c.* = ' ';
};
- pushCounting(p, record);
+ pushCounting(p, record, reason);
}
/// Drops the newest record when it is a msg saying `text` (of any pane)
@@ -256,7 +256,11 @@ pub fn dropMessage(p: *Pardes, text: []const u8) void {
/// counts that one instead: `<record> (x2)`, the count being every time it
/// was said. One a follower has already read is not rewritten: the repeat
/// is a new line carrying the running count, `(x3)`.
-fn pushCounting(p: *Pardes, raw: []u8) void {
+/// `full` is the text before any cut: two records that agree only in what
+/// was cut to fit are no repeat (`fs.log_full_hash`).
+fn pushCounting(p: *Pardes, raw: []u8, full: []const u8) void {
+ const full_hash = std.hash.Wyhash.hash(0, full);
+ defer p.fs.log_full_hash = full_hash;
// A client retrying a write that fails the same way would fill the ring
// with one line, so a repeat of the newest record is that record counted,
// `(x3)`, as +Messages counts its repeats; unless a follower has read it
@@ -272,7 +276,7 @@ fn pushCounting(p: *Pardes, raw: []u8) void {
said = said[0..x];
} else |_| {}
};
- if (!std.mem.eql(u8, said, record[0 .. record.len - 1])) return pushLog(p, record);
+ if (!std.mem.eql(u8, said, record[0 .. record.len - 1]) or p.fs.log_full_hash != full_hash) return pushLog(p, record);
var counted: [pardes.Messages.LoggedMessage.cap + 64]u8 = undefined;
const again = std.fmt.bufPrint(&counted, "{s} (x{d})\n", .{ said, times +| 1 }) catch return;
if (!followerRead(p, last.seq)) p.fs.log.buf.shrinkRetainingCapacity(last.at);
@@ -373,6 +377,8 @@ fn shownAs(text: []const u8, out: []u8, escape_newline: bool) []u8 {
}
fn pushLog(p: *Pardes, raw: []u8) void {
+ // A record not counted: the next one is no repeat of a counted one.
+ p.fs.log_full_hash = 0;
var clean: [4 * 4096 + 256]u8 = undefined;
const record = sanitize(raw, &clean);
// One record larger than the ring would push every other out and then
@@ -1283,7 +1289,7 @@ test "a record is one line of UTF-8: DEL and C1 are spaces, bytes not UTF-8 are
try testing.expectEqualStrings("msg - caf\xc3\xa9\n", sanitize("msg - caf\xc3\xa9\n", &out));
}
-test "a long err record is cut between words, with an ellipsis" {
+test "a long err record keeps its end, its middle given up to an ellipsis" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
_ = wr(p, @intFromEnum(tree.TopFile.ctl), "Bogus " ++ "abcdefgh " ** 40 ++ "\n");
@@ -1292,6 +1298,19 @@ test "a long err record is cut between words, with an ellipsis" {
const text = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = h, .size = 1 << 16 }).bytes;
const at = std.mem.lastIndexOf(u8, text, "err - ctl: ").?;
const record = text[at .. std.mem.indexOfScalarPos(u8, text, at, '\n').? + 1];
- try testing.expect(std.mem.endsWith(u8, record, "abcdefgh…\n"));
+ try testing.expect(std.mem.indexOf(u8, record, "…") != null);
+ try testing.expect(std.mem.startsWith(u8, record, "err - ctl: "));
+ try testing.expect(std.mem.endsWith(u8, record, "abcdefgh\"\n") or std.mem.endsWith(u8, record, "abcdefgh \"\n"));
_ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h });
}
+
+test "two failures that agree only in what their records cut are two records, not one counted" {
+ const p = try th.withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ const long = "why " ** 80;
+ p.setMessage(0, long ++ "first");
+ p.setMessage(0, long ++ "second");
+ try testing.expect(!th.logHas(p, "(x2)"));
+ p.setMessage(0, long ++ "second");
+ try testing.expect(th.logHas(p, "(x2)"));
+}
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig
index 9f85a75f..3c1e181e 100644
--- a/src/ninep/pane.zig
+++ b/src/ninep/pane.zig
@@ -475,6 +475,55 @@ fn joinOnly(pane: *Pane, handle: u32) void {
if (pane.fs.joined_by != 0 and pane.fs.joined_by != handle) pane.fs.joined = null;
}
+/// How long writes in a row may pause before the editor's own step puts
+/// them in: long enough that a stream of 8 KB writes is one edit.
+pub const batch_pause_ns: u64 = 20 * std.time.ns_per_ms;
+
+/// The editor's step: the held writes go in once they have paused.
+pub fn flushPausedBatch(p: *Pardes) void {
+ const b = &p.fs.batch;
+ if (b.bytes.items.len == 0) return;
+ if (p.now_ns != 0 and p.now_ns -| b.last_ns < batch_pause_ns) return;
+ flushBatch(p);
+}
+
+/// Puts the held writes (`fs.batch`) in as one edit: before any other
+/// request, the open's release, or the editor's own step.
+pub fn flushBatch(p: *Pardes) void {
+ const b = &p.fs.batch;
+ if (b.bytes.items.len == 0) return;
+ defer b.bytes.clearRetainingCapacity();
+ const id = p.paneBySerial(b.serial) orelse return;
+ const pane = p.panes[id].?;
+ if (fileOf(pane) == null) return;
+ const before = dotOf(pane);
+ joinOnly(pane, b.handle);
+ const take = spliceBody(p, pane, b.at, b.at, b.bytes.items) orelse return;
+ pane.fs.joined = fileOf(pane).?.revision;
+ pane.fs.joined_by = b.handle;
+ setDot(pane, .{
+ .q0 = shiftOne(before.q0, clip(b.at), 0, clip(take)),
+ .q1 = shiftOne(before.q1, clip(b.at), 0, clip(take)),
+ });
+ if (!pane.fs.noscroll) showOffset(pane, b.at + take);
+}
+
+/// The held bytes a write at `at` on this open can go on from: its own
+/// batch, when it ends right at `at`; any other is put in first.
+fn batchFor(p: *Pardes, req: Req, pane: *Pane, at: usize) ?*@TypeOf(p.fs.batch) {
+ const b = &p.fs.batch;
+ const mine = b.serial == pane.serial and b.node == req.node and b.handle == req.handle;
+ if (b.bytes.items.len > 0 and !(mine and b.at + b.bytes.items.len == at)) flushBatch(p);
+ // Only an open's writes are held: one with no record (a test's, handle
+ // 0) is put in at once. A hold past 64 MiB is put in first.
+ if (req.handle == 0 or b.bytes.items.len > 64 << 20) {
+ flushBatch(p);
+ return null;
+ }
+ if (b.bytes.items.len == 0) b.* = .{ .serial = pane.serial, .node = req.node, .handle = req.handle, .at = at, .bytes = b.bytes };
+ return b;
+}
+
fn writeBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
if (req.data.len == 0) return .{ .tag = req.tag, .written = 0 };
if (pane.file == null) {
@@ -484,7 +533,14 @@ fn writeBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
p.emitWrite(id, req.data[0..take]);
return .{ .tag = req.tag, .written = @intCast(take) };
}
- const at = bodyOf(pane).len;
+ const held = if (p.fs.batch.serial == pane.serial and p.fs.batch.node == req.node and p.fs.batch.handle == req.handle) p.fs.batch.bytes.items.len else 0;
+ const at = bodyOf(pane).len + held;
+ if (batchFor(p, req, pane, at)) |b| {
+ const take = wholeUtf8(req.data);
+ b.bytes.appendSlice(p.gpa, req.data[0..take]) catch return Reply.fail(req.tag, E.NOMEM);
+ b.last_ns = p.now_ns;
+ return .{ .tag = req.tag, .written = @intCast(take) };
+ }
joinOnly(pane, req.handle);
const take = spliceBody(p, pane, at, at, req.data) orelse
return Reply.fail(req.tag, E.NOMEM);
@@ -514,8 +570,9 @@ fn writeTag(req: Req, pane: *Pane) Reply {
const pf = &pane.fs;
const had = tagline.curTail(pane).len;
const room = limits.max_tag_tail -| had -| @intFromBool(pf.tag_held_newline);
- if (room == 0) return Reply.fail(req.tag, E.NOSPC);
- const take = wholeUtf8(req.data[0..@min(req.data.len, room)]);
+ // Whole or not at all: a write that would pass the limit changes nothing.
+ if (req.data.len > room) return tree.failText(req.tag, E.NOSPC, std.fmt.comptimePrint("tag: no space: over {d} bytes", .{limits.max_tag_tail}));
+ const take = wholeUtf8(req.data);
// A truncating write drops ONE trailing newline, its whole text's: a
// newline held from a write before goes in once more text follows it.
if (pf.tag_held_newline) {
@@ -538,9 +595,19 @@ fn writeData(p: *Pardes, req: Req, pane: *Pane) Reply {
if (fileOf(pane) == null) return tree.failText(req.tag, E.INVAL, e_no_text);
const pf = &pane.fs;
if (pf.addr_failed) return tree.failText(req.tag, E.INVAL, e_addr_failed);
- clampAddr(pf, bodyOf(pane).len);
+ const held = if (p.fs.batch.serial == pane.serial and p.fs.batch.node == req.node and p.fs.batch.handle == req.handle) p.fs.batch.bytes.items.len else 0;
+ clampAddr(pf, bodyOf(pane).len + held);
const q0: usize = pf.addr.q0;
const q1: usize = @max(q0, @as(usize, pf.addr.q1));
+ // An insert (an empty address) goes on from the open's held bytes.
+ if (q1 == q0) if (batchFor(p, req, pane, q0)) |b| {
+ const take = wholeUtf8(req.data);
+ b.bytes.appendSlice(p.gpa, req.data[0..take]) catch return Reply.fail(req.tag, E.NOMEM);
+ b.last_ns = p.now_ns;
+ pf.addr = .{ .q0 = clip(q0 + take), .q1 = clip(q0 + take) };
+ return .{ .tag = req.tag, .written = @intCast(take) };
+ };
+ flushBatch(p);
const before = dotOf(pane);
joinOnly(pane, req.handle);
const take = spliceBody(p, pane, q0, q1, req.data) orelse
@@ -1097,6 +1164,62 @@ test "the writes of one open are one undo step, as bash's line-buffered printf m
}
}
+test "an open's writes in a row are held and go in as one edit, seen by the next request" {
+ const p = try withFile(testing.allocator, "one\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ const body = Node.of(serial, .body);
+ const h = call(p, .{ .tag = 1, .op = .open, .node = body, .omode = 1 }).reply.handle;
+ const revision = p.panes[0].?.file.?.revision;
+ for (0..100) |_| _ = call(p, .{ .tag = 2, .op = .write, .node = body, .handle = h, .data = "0123456789\n" });
+ // Held: nothing edited yet.
+ try testing.expectEqual(revision, p.panes[0].?.file.?.revision);
+ // Any other request puts them in, once.
+ try testing.expectEqual(@as(usize, 4 + 1100), rd(p, body, 0, 4096).bytes.len);
+ try testing.expectEqual(revision + 1, p.panes[0].?.file.?.revision);
+ _ = call(p, .{ .tag = 3, .op = .release, .node = body, .handle = h, .opened = true });
+ try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Undo").reply.status);
+ try testing.expectEqualStrings("one\n", rd(p, body, 0, 64).bytes);
+ // data inserts at its address go on the same way.
+ const data = Node.of(serial, .data);
+ _ = wr(p, Node.of(serial, .addr), "#0");
+ const d = call(p, .{ .tag = 4, .op = .open, .node = data, .omode = 1 }).reply.handle;
+ _ = call(p, .{ .tag = 5, .op = .write, .node = data, .handle = d, .data = "a" });
+ _ = call(p, .{ .tag = 5, .op = .write, .node = data, .handle = d, .data = "b" });
+ _ = call(p, .{ .tag = 6, .op = .release, .node = data, .handle = d, .opened = true });
+ try testing.expectEqualStrings("abone\n", rd(p, body, 0, 64).bytes);
+}
+
+test "writes held before the shell's clock is first seen go in at its first step" {
+ const p = try withFile(testing.allocator, "one\n");
+ defer p.deinit();
+ const body = Node.of(serialOf(p), .body);
+ try testing.expect(!p.clock_started);
+ const h = call(p, .{ .tag = 1, .op = .open, .node = body, .omode = 1 }).reply.handle;
+ const revision = p.panes[0].?.file.?.revision;
+ _ = call(p, .{ .tag = 2, .op = .write, .node = body, .handle = h, .data = "x\n" });
+ try testing.expectEqual(@as(u64, 0), p.fs.batch.last_ns);
+ // A detached host's first step: its clock is days into the machine's.
+ p.advance(700_000 * std.time.ns_per_s);
+ flushPausedBatch(p);
+ try testing.expectEqual(revision + 1, p.panes[0].?.file.?.revision);
+}
+
+test "a tag write past the limit is refused whole, naming the limit" {
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ const tag = Node.of(serialOf(p), .tag);
+ const before = try testing.allocator.dupe(u8, rd(p, tag, 0, 1 << 16).bytes);
+ defer testing.allocator.free(before);
+ const big = "w" ** (limits.max_tag_tail + 10);
+ const r = wr(p, tag, big);
+ try testing.expectEqual(E.NOSPC, r.errno());
+ // `no space` is what 9ns maps to ENOSPC, so a shell through a mount
+ // sees the errno the tree gives.
+ try testing.expect(std.mem.startsWith(u8, r.reply.ename, "tag: no space: over "));
+ try testing.expectEqualStrings(before, rd(p, tag, 0, 1 << 16).bytes);
+}
+
test "two opens writing one body are two undo steps, however their writes interleave" {
const p = try withFile(testing.allocator, "one\n");
defer p.deinit();
@@ -1153,13 +1276,19 @@ test "a name cut across writes is one name, applied once at its newline or its c
const node = Node.of(serial, .name);
const h = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle;
try testing.expect(h != 0);
+ // A piece that fills its Twrite goes on in the next.
+ p.fs.write_room = "/tmp/pardes-na".len;
_ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "/tmp/pardes-na" });
+ p.fs.write_room = 0;
try testing.expectEqualStrings("/test.txt", nameOf(p.panes[p.paneBySerial(serial).?].?));
_ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "me.txt\n" });
try testing.expectEqualStrings("/tmp/pardes-name.txt", nameOf(p.panes[p.paneBySerial(serial).?].?));
- // No newline: the close applies it.
- _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "/tmp/pardes-closed.txt" });
_ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h, .opened = true });
+ // No newline, the whole write: applied at once, on its own open.
+ const h3 = call(p, .{ .tag = 1, .op = .open, .node = node, .omode = 1 }).reply.handle;
+ _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h3, .data = "/tmp/pardes-closed.txt" });
+ try testing.expectEqualStrings("/tmp/pardes-closed.txt", nameOf(p.panes[p.paneBySerial(serial).?].?));
+ _ = call(p, .{ .tag = 3, .op = .release, .node = node, .handle = h3, .opened = true });
try testing.expectEqualStrings("/tmp/pardes-closed.txt", nameOf(p.panes[p.paneBySerial(serial).?].?));
}
diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig
index d32c35fd..8ba9f4df 100644
--- a/src/ninep/pty.zig
+++ b/src/ninep/pty.zig
@@ -61,6 +61,10 @@ pub fn writeCtl(p: *Pardes, req: Req, id: usize) Reply {
while (it.next()) |raw| {
const line = std.mem.trim(u8, raw, " \t\r");
if (line.len == 0) continue;
+ // A command pane's child is its command, done or running: exec
+ // would run its line again with no header, footer or records.
+ if (!apply and std.mem.eql(u8, line, "exec")) if (p.panes[id]) |pane| if (pane.command != null)
+ return tree.failText(req.tag, E.INVAL, "invalid exec: a command pane does not restart");
// A shell restarted where the directory is gone would not start:
// refused before anything runs, as Tty there is.
if (!apply and std.mem.eql(u8, line, "exec")) if (p.panes[id]) |pane| if (pardes.exec.missingDir(p, pane.cwdSlice())) {
@@ -204,6 +208,12 @@ fn answer(p: *Pardes, slot: *Run, comptime fmt: []const u8, args: anytype) void
pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
const slot = &(tree.openOf(p, req) orelse return Reply.fail(req.tag, E.INVAL)).what.run;
if (slot.phase == .sent or slot.phase == .waiting) return tree.failText(req.tag, E.BUSY, tree.e_in_use);
+ // One line a run, its answer read before the next: a second line (bash
+ // writes printf 'a\nb\n' a line at a time) would run and lose the
+ // first's answer.
+ const one = std.mem.trimEnd(u8, req.data, "\r\n");
+ if (std.mem.indexOfScalar(u8, one, '\n') != null or (slot.phase == .done and slot.read == 0))
+ return tree.failText(req.tag, E.INVAL, e_one_line);
// A new line on this open: a new answer.
p.gpa.free(slot.output);
p.gpa.free(slot.line);
@@ -421,6 +431,7 @@ fn waitingRun(p: *Pardes, pane: *Pane) ?*Run {
}
const e_bad_line = "bad command line";
+const e_one_line = "invalid run: one line a run, its answer read before the next";
const e_bad_pty_ctl = tree.e_bad_ctl ++ "; takes winsize COLS ROWS, sig INT|TERM|HUP|QUIT|KILL, or exec";
pub fn readData(p: *Pardes, req: Req, pf: *pane_files.State) Reply {
@@ -1087,3 +1098,31 @@ test "a run's answer longer than its room is cut, its newline kept" {
try testing.expectEqual(slot.answer.len, slot.len);
try testing.expectEqual(@as(u8, '\n'), slot.answer[slot.len - 1]);
}
+
+test "exec on a command pane is refused: a command pane does not restart" {
+ const p = try th.withTerm(testing.allocator);
+ defer p.deinit();
+ const serial = th.serialOf(p);
+ const pane = p.panes[p.paneBySerial(serial).?].?;
+ pane.command = try p.gpa.dupe(u8, "echo hi");
+ const r = th.wr(p, tree.Node.of(serial, .pty_ctl), "exec\n");
+ try testing.expectEqual(tree.E.INVAL, r.errno());
+ try testing.expectEqualStrings("invalid exec: a command pane does not restart", r.reply.ename);
+}
+
+test "pty/run takes one line a run: two in one write, or a second before the first's answer is read, are refused" {
+ const p = try th.withTerm(testing.allocator);
+ defer p.deinit();
+ const serial = th.serialOf(p);
+ const run = tree.Node.of(serial, .pty_run);
+ const h = th.call(p, .{ .tag = 1, .op = .open, .node = run, .omode = 2 }).reply.handle;
+ const two = th.call(p, .{ .tag = 2, .op = .write, .node = run, .handle = h, .data = "true\nfalse\n" });
+ try testing.expectEqual(tree.E.INVAL, two.errno());
+ try testing.expectEqualStrings(e_one_line, two.reply.ename);
+ // A finished run whose answer nobody read takes no next line.
+ const slot = &tree.openOf(p, .{ .tag = 0, .op = .read, .node = run, .handle = h }).?.what.run;
+ slot.phase = .done;
+ slot.len = 7;
+ try testing.expectEqual(tree.E.INVAL, th.call(p, .{ .tag = 3, .op = .write, .node = run, .handle = h, .data = "true\n" }).errno());
+ _ = th.call(p, .{ .tag = 4, .op = .release, .node = run, .handle = h, .opened = true });
+}
diff --git a/src/ninep/testing.zig b/src/ninep/testing.zig
index 927552e2..b8eaa02b 100644
--- a/src/ninep/testing.zig
+++ b/src/ninep/testing.zig
@@ -58,11 +58,16 @@ pub fn rd(p: *Pardes, node: u64, off: u64, size: u32) Answer {
/// Whether the log, read whole from a fresh open, holds `needle`.
pub fn logHas(p: *Pardes, needle: []const u8) bool {
+ return logCount(p, needle) > 0;
+}
+
+/// How many times the log, read whole from a fresh open, holds `needle`.
+pub fn logCount(p: *Pardes, needle: []const u8) usize {
const log = @intFromEnum(tree.TopFile.log);
const h = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle;
defer _ = call(p, .{ .tag = 3, .op = .release, .node = log, .handle = h });
const text = call(p, .{ .tag = 2, .op = .read, .node = log, .handle = h, .size = 1 << 16 }).bytes;
- return std.mem.indexOf(u8, text, needle) != null;
+ return std.mem.count(u8, text, needle);
}
pub fn wr(p: *Pardes, node: u64, data: []const u8) Answer {
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index d6f4211b..5f3ffc9c 100644
--- a/src/ninep/tree.zig
+++ b/src/ninep/tree.zig
@@ -454,6 +454,86 @@ pub fn stagedReply(p: *Pardes, req: Req) Reply {
// ---- dispatch ----
pub fn handle(p: *Pardes, req: Req) Reply {
+ var reply = serve(p, req);
+ if (reply.status == .err and reply.ename.len == 0) reply.ename = errWords(reply.errno);
+ if (reply.status == .err and reply.ename.len > cloud9.fs.errmax)
+ if (p.scratch.allocator().alloc(u8, cloud9.fs.errmax)) |room| {
+ reply.ename = fitErr(reply.ename, room);
+ } else |_| {};
+ return reply;
+}
+
+/// A reason longer than an Rerror carries (Plan 9's ERRMAX, 128 bytes)
+/// keeps its end, which says why: the longest path in it -- else the whole
+/// text -- gives up its middle to `…`.
+pub fn fitErr(text: []const u8, out: []u8) []const u8 {
+ const ell = "…";
+ if (text.len <= out.len) return text;
+ const over = text.len - out.len + ell.len;
+ var from: usize = 0;
+ var to: usize = text.len;
+ var words = std.mem.tokenizeAny(u8, text, " \t");
+ var longest: usize = 0;
+ while (words.next()) |w| if (std.mem.indexOfScalar(u8, w, '/') != null and w.len > longest and w.len > over + 2) {
+ longest = w.len;
+ from = @intFromPtr(w.ptr) - @intFromPtr(text.ptr);
+ to = from + w.len;
+ };
+ var a = from + (to - from - over) / 2;
+ var b = a + over;
+ while (a > from and text[a] & 0xC0 == 0x80) a -= 1;
+ while (b < to and text[b] & 0xC0 == 0x80) b += 1;
+ return std.fmt.bufPrint(out, "{s}" ++ ell ++ "{s}", .{ text[0..a], text[b..] }) catch text[0..out.len];
+}
+
+test "a reason past 128 bytes keeps its end: the path in it gives up its middle" {
+ var out: [128]u8 = undefined;
+ const long = "Save /home/someone/projects/" ++ "deep/" ** 30 ++ "file.txt: no such directory";
+ const fit = fitErr(long, &out);
+ try testing.expect(fit.len <= 128);
+ try testing.expect(std.mem.startsWith(u8, fit, "Save /home/someone/"));
+ try testing.expect(std.mem.endsWith(u8, fit, "file.txt: no such directory"));
+ try testing.expect(std.mem.indexOf(u8, fit, "…") != null);
+ try testing.expect(std.unicode.utf8ValidateSlice(fit));
+ // No path: the whole text's middle goes, the end stays.
+ const words = "why " ** 40 ++ "the reason";
+ try testing.expect(std.mem.endsWith(u8, fitErr(words, &out), "the reason"));
+ try testing.expectEqualStrings("short", fitErr("short", &out));
+ // Through the tree: a look at a long ./ name not there says why at the end.
+ const p = try th.withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ var line: [512]u8 = undefined;
+ const r = th.wr(p, Node.of(serialOf(p), .look), try std.fmt.bufPrint(&line, "./{s}x.txt\n", .{"no-such-dir/" ** 20}));
+ try testing.expectEqual(Status.err, r.reply.status);
+ try testing.expect(r.reply.ename.len <= 128);
+ try testing.expect(std.mem.endsWith(u8, r.reply.ename, "no such file") or std.mem.endsWith(u8, r.reply.ename, "no such directory"));
+}
+
+/// A refusal with no reason of its own said in Plan 9's words, not the C
+/// library's (`Operation not permitted`); each maps back to its errno in
+/// 9ns (enameToErrno), EPERM's to EACCES as Plan 9's does.
+pub fn errWords(errno: u16) []const u8 {
+ return switch (errno) {
+ E.PERM => "permission denied",
+ E.NOENT => "file does not exist",
+ E.NOMEM => "out of memory",
+ E.BUSY => "file in use",
+ E.EXIST => "file already exists",
+ E.NOTDIR => "not a directory",
+ E.ISDIR => "is a directory",
+ E.INVAL => "bad argument",
+ E.NFILE => "too many open files",
+ E.NOSPC => "no space left",
+ E.NOSYS => "not supported",
+ E.NOTEMPTY => "directory not empty",
+ else => "i/o error",
+ };
+}
+
+fn serve(p: *Pardes, req: Req) Reply {
+ // Held writes go in before anything but the next write of their open.
+ if (p.fs.batch.bytes.items.len > 0 and !(req.op == .write and req.node == p.fs.batch.node and req.handle == p.fs.batch.handle))
+ pane.flushBatch(p);
if (req.op == .open and req.create) {
if (Node.target(req.node)) |t| if (t == .pane and p.paneBySerial(t.pane.serial) == null)
return Reply.fail(req.tag, E.NOENT);
@@ -1036,7 +1116,7 @@ fn linesFile(target: Target) bool {
.top => |f| f == .look or f == .exec or f == .ctl or f == .tagexec,
.col => |c| c.file == .ctl or c.file == .exec,
// `name` too: one name, however its writes cut it, applied once
- // its newline or its close comes.
+ // 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,
};
}
@@ -1073,7 +1153,18 @@ fn write(p: *Pardes, req: Req, target: Target) Reply {
// to fail unseen at the close.
if (resultsFile(target)) for (req.data) |c| if ((c < ' ' and c != '\t' and c != '\n' and c != '\r') or c == 0x7f) return failText(req.tag, E.INVAL, ctl.e_control);
o.pending.appendSlice(p.gpa, req.data) catch return Reply.fail(req.tag, E.NOMEM);
- const end = ctl.completeEnd(p, o.pending.items);
+ var end = ctl.completeEnd(p, o.pending.items);
+ // A tail with no newline is a whole line when the write is the whole of
+ // what its client wrote (shorter than a Twrite can carry): it runs now,
+ // and its failure is this write's, as acme takes each write whole. Held
+ // only when it is not whole yet -- an open `{` block, an `a`/`c`/`i`
+ // text awaiting its `.` -- or the write filled its Twrite and may go on.
+ const cut = p.fs.write_room != 0 and req.data.len >= p.fs.write_room;
+ if (end < o.pending.items.len and !cut) {
+ o.pending.append(p.gpa, '\n') catch return Reply.fail(req.tag, E.NOMEM);
+ const whole = ctl.completeEnd(p, o.pending.items);
+ if (whole == o.pending.items.len) end = whole else o.pending.shrinkRetainingCapacity(o.pending.items.len - 1);
+ }
if (end == 0) {
if (o.pending.items.len <= pending_cap) return .{ .tag = req.tag, .written = @intCast(req.data.len) };
o.pending.clearRetainingCapacity();
@@ -1537,6 +1628,55 @@ test "editor paths resolve to the same nodes the wire serves" {
try testing.expectEqual(before, p.next_serial);
}
+test "random placements, refused or not, leave no pane under its tag and two rows" {
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "f.txt", .data = "one\ntwo\n" });
+ var dir_buf: [4096]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
+ var look_line: [4200]u8 = undefined;
+ const looked = try std.fmt.bufPrint(&look_line, "{s}/f.txt\n", .{dir});
+ var seed: u64 = 0;
+ while (seed < 300) : (seed += 1) {
+ var prng = std.Random.DefaultPrng.init(seed);
+ const r = prng.random();
+ const rows = r.intRangeAtMost(u16, 6, 40);
+ const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true, .cols = r.intRangeAtMost(u16, 60, 240), .rows = rows });
+ defer p.deinit();
+ while (p.nextEffect()) |_| {}
+ _ = try p.setTestFile("x\n");
+ while (p.nextEffect()) |_| {}
+ for (0..r.uintLessThan(usize, 3)) |_| _ = th.wr(p, Node.of(serialOf(p), .exec), "Newcol\n");
+ if (r.boolean()) p.settings.placement = .pardes;
+ for (0..40) |step| {
+ var serials: [pardes.MAX_PANES]u32 = undefined;
+ var n: usize = 0;
+ for (p.panes) |slot| if (slot) |pn| {
+ serials[n] = pn.serial;
+ n += 1;
+ };
+ if (n == 0) break;
+ const at = serials[r.uintLessThan(usize, n)];
+ const action = r.uintLessThan(u8, 8);
+ switch (action) {
+ 0 => _ = th.wr(p, Node.of(at, .exec), "New\n"),
+ 1 => _ = call(p, .{ .tag = 1, .op = .open, .node = @intFromEnum(TopFile.new) }),
+ 2 => _ = th.wr(p, Node.of(at, .exec), "Newcol\n"),
+ 3 => _ = th.wr(p, Node.of(at, .exec), "Edit =\n"),
+ 4 => _ = th.wr(p, Node.of(at, .exec), "Tty\n"),
+ 5 => _ = th.wr(p, Node.of(at, .look), looked),
+ 6 => _ = th.wr(p, Node.of(at, .exec), "Delcol\n"),
+ else => _ = th.wr(p, Node.of(at, .ctl), "delete\n"),
+ }
+ for (0..p.ncol) |c| if (!layout.columnAtMinimums(p, c)) {
+ std.debug.print("seed {d} rows {d} step {d} action {d}: column {d} under its minimums\n", .{ seed, rows, step, action, c });
+ for (p.col_panes[c][0..p.col_n[c]]) |id| std.debug.print(" pane {d} h {d} min {d}\n", .{ id, p.rects[id].h, layout.minRows(p, id) });
+ return error.TestUnexpectedResult;
+ };
+ }
+ }
+}
+
test "at the pane cap, pane/new, look and New each say so, and look reads back empty" {
const p = try th.withFile(testing.allocator, "x\n");
defer p.deinit();
@@ -1550,8 +1690,8 @@ test "at the pane cap, pane/new, look and New each say so, and look reads back e
const refused = call(p, .{ .tag = 2, .op = .open, .node = new });
try testing.expectEqual(E.NOSPC, refused.errno());
try testing.expect(std.mem.endsWith(u8, refused.reply.ename, said));
- try testing.expect(th.logHas(p, said));
- // Its err alone says it: no msg besides (the one rule).
+ // A refused open is its error alone: no err, and no msg besides.
+ try testing.expect(!th.logHas(p, said));
try testing.expect(!th.logHas(p, "New: no space"));
// A look that would open a pane fails its write, and says the same.
var tmp = testing.tmpDir(.{});
@@ -1580,8 +1720,12 @@ test "a command line cut across writes runs once whole, and the last runs at rel
const exec = @intFromEnum(TopFile.exec);
const h = call(p, .{ .tag = 1, .op = .open, .node = exec, .omode = 1 }).reply.handle;
try testing.expect(h != 0);
- for ([_][]const u8{ "Msg he", "llo\nMsg wor", "ld\nMsg tail" }) |piece|
+ // Each piece fills its Twrite (a client's one write cut at msize).
+ for ([_][]const u8{ "Msg he", "llo\nMsg wor", "ld\nMsg tail" }) |piece| {
+ p.fs.write_room = @intCast(piece.len);
try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = exec, .handle = h, .data = piece }).reply.status);
+ }
+ p.fs.write_room = 0;
try testing.expect(th.logHas(p, " hello\n") and th.logHas(p, " world\n"));
try testing.expect(!th.logHas(p, " he\n") and !th.logHas(p, " tail\n"));
_ = call(p, .{ .tag = 3, .op = .release, .node = exec, .handle = h, .opened = true });
@@ -1596,6 +1740,60 @@ test "a command line cut across writes runs once whole, and the last runs at rel
try testing.expectEqualStrings("x\none\ntwo\n", p.panes[0].?.file.?.content);
}
+test "a refused open, create or remove says why in words and logs no err; a refused write logs one" {
+ const p = try th.withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ // pane/new is read, never written: a write is refused, and is no
+ // failure the log tells of.
+ const new = th.wr(p, @intFromEnum(TopFile.new), "x\n");
+ try testing.expectEqual(Status.err, new.reply.status);
+ try testing.expectEqualStrings("permission denied", new.reply.ename);
+ const made = call(p, .{ .tag = 2, .op = .open, .node = Node.of(serial, .body), .create = true });
+ try testing.expectEqualStrings("permission denied", made.reply.ename);
+ const gone = th.rmdir(p, @intFromEnum(TopFile.index));
+ try testing.expectEqual(Status.err, gone.reply.status);
+ try testing.expect(gone.reply.ename.len > 0 and !std.ascii.isUpper(gone.reply.ename[0]));
+ // An OTRUNC open's truncation of data after a failed addr: refused, no err.
+ _ = th.wr(p, Node.of(serial, .addr), "/nothere/");
+ const errs = th.logCount(p, "\nerr ");
+ try testing.expectEqual(Status.err, call(p, .{ .tag = 3, .op = .setattr, .node = Node.of(serial, .data), .truncate = true }).reply.status);
+ try testing.expectEqual(errs, th.logCount(p, "\nerr "));
+ try testing.expectEqual(@as(usize, 1), errs); // the addr write's own
+ // A write is the rule's: refused, it logs its one err, in words too.
+ try testing.expectEqual(Status.err, th.wr(p, Node.of(serial, .ctl), "bogus\n").reply.status);
+ try testing.expectEqual(@as(usize, 2), th.logCount(p, "\nerr "));
+}
+
+test "a write with no newline, whole in its Twrite, runs then and fails the write; one needing more waits" {
+ const p = try th.withFile(testing.allocator, "abc\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ const ctl_node = Node.of(serial, .ctl);
+ const c = call(p, .{ .tag = 1, .op = .open, .node = ctl_node, .omode = 1 }).reply.handle;
+ p.fs.write_room = 8192;
+ defer p.fs.write_room = 0;
+ try testing.expectEqual(Status.err, call(p, .{ .tag = 2, .op = .write, .node = ctl_node, .handle = c, .data = "bogus" }).reply.status);
+ try testing.expectEqual(Status.err, call(p, .{ .tag = 3, .op = .write, .node = ctl_node, .handle = c, .data = "Edit ,s/zzz/y/" }).reply.status);
+ // An `a` needs its text and `.`: held, not run, not failed.
+ try testing.expectEqual(Status.ok, call(p, .{ .tag = 4, .op = .write, .node = ctl_node, .handle = c, .data = "Edit $a" }).reply.status);
+ try testing.expectEqual(Status.ok, call(p, .{ .tag = 5, .op = .write, .node = ctl_node, .handle = c, .data = "\nmore\n.\n" }).reply.status);
+ try testing.expectEqualStrings("abc\nmore\n", p.panes[0].?.file.?.content);
+ _ = call(p, .{ .tag = 6, .op = .release, .node = ctl_node, .handle = c, .opened = true });
+ // The root's ctl too.
+ const root_ctl = @intFromEnum(TopFile.ctl);
+ const r = call(p, .{ .tag = 7, .op = .open, .node = root_ctl, .omode = 1 }).reply.handle;
+ try testing.expectEqual(Status.err, call(p, .{ .tag = 8, .op = .write, .node = root_ctl, .handle = r, .data = "bogus" }).reply.status);
+ _ = call(p, .{ .tag = 9, .op = .release, .node = root_ctl, .handle = r, .opened = true });
+ // And a name: a bad one fails its write, not the close after it.
+ const name = Node.of(serial, .name);
+ const n = call(p, .{ .tag = 10, .op = .open, .node = name, .omode = 1 }).reply.handle;
+ const bad = call(p, .{ .tag = 11, .op = .write, .node = name, .handle = n, .data = "delete " });
+ try testing.expectEqual(Status.err, bad.reply.status);
+ try testing.expect(std.mem.indexOf(u8, bad.reply.ename, "a blank at its end") != null);
+ try testing.expectEqual(Status.ok, call(p, .{ .tag = 12, .op = .release, .node = name, .handle = n, .opened = true }).reply.status);
+}
+
test "look and exec read back what their own open's write touched; a fresh open reads the session's last" {
const p = try th.withFile(testing.allocator, "x\n");
defer p.deinit();
diff --git a/src/pardes.zig b/src/pardes.zig
index dc479909..22d5f7a1 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -4792,6 +4792,7 @@ pub const Pardes = struct {
for (p.panes) |slot| if (slot) |pane| p.noteClosed(pane);
p.saveRecent();
p.recent.deinit(p.recentGpa());
+ p.fs.batch.bytes.deinit(p.gpa);
if (p.stand_in) |pane| p.teardownPane(pane);
look.cancelLookHover(p);
if (p.repl_ask) |ask| p.gpa.free(ask.text);
@@ -5081,6 +5082,15 @@ pub const Pardes = struct {
else
"Newcol: no space for a column: this one is too narrow to split");
};
+ // A narrower column wraps long tags onto more rows, which can leave
+ // a pane under its tag and two rows: its column's rows are shared
+ // out again, and where they cannot hold every minimum there is no
+ // new column, as a size too small is refused.
+ for (0..p.ncol) |k| if (!layout.columnAtMinimums(p, k) and !layout.shareColumn(p, k)) {
+ layout.dropColumn(p, c);
+ layout.compute(p);
+ return p.reportFailure(from_id, "Newcol: no space for a column: the panes' tags would not fit");
+ };
tagline.enterHeader(p, c);
}
@@ -5686,6 +5696,8 @@ pub const Pardes = struct {
f(h.ctx, timeout);
}
if (v.now) |clock| p.advance(clock(h.ctx));
+ // What a 9P open wrote in a row goes in once the writes pause.
+ ctlfs.pane.flushPausedBatch(p);
while (p.nextQueued()) |ev| p.update(ev);
while (p.nextEffect()) |e| p.perform(e);
turn.settle();
@@ -5716,14 +5728,21 @@ pub const Pardes = struct {
/// local socket costs microseconds and not a vsync.
pub fn serveFs(p: *Pardes, req: ctlfs.Req) ctlfs.Reply {
if (!ctlfs.changesPane(p, req)) return ctlfs.handle(p, req);
- p.needs_frame = true;
+ // An address, a limit or the mark flag is nothing drawn: a script's
+ // addr write then its close costs no frame between them.
+ const drawn = if (ctlfs.Node.target(req.node)) |t| !(t == .pane and (t.pane.file == .addr or t.pane.file == .limit or t.pane.file == .mark)) else true;
+ if (drawn) p.needs_frame = true;
p.raw_hover_intent = false;
look.cancelLookHover(p);
p.fs.serving = true;
p.fs.lsp_answer_at = null;
const reply = ctlfs.handle(p, req);
p.fs.serving = false;
- if (reply.status == .err and (req.op == .write or req.op == .setattr or (req.op == .open and reply.errno == ctlfs.E.NOSPC) or (req.op == .release and req.remove))) ctlfs.events.noteError(p, req, reply);
+ // The one-failure rule is a write's: a refused open (its OTRUNC's
+ // truncation, a setattr, too), create or remove is its Rerror alone,
+ // no err, as is a write to pane/new, which is only ever read.
+ const ruled = req.op == .write and req.node != @intFromEnum(ctlfs.TopFile.new);
+ if (reply.status == .err and ruled) ctlfs.events.noteError(p, req, reply);
// The request was a whole step of its own, so it settles the way a
// step does: the cursor and scroll reconciled, the scripted panes
// told, and the panes it made announced to /log now rather than at
@@ -7123,6 +7142,10 @@ pub const Pardes = struct {
/// sleep exactly as long as nothing changes.
pub fn nextWake(p: *const Pardes) ?u64 {
const next = p.stepped_ns + animation.frame_ns;
+ // Held 9P writes go in when they pause: wake for it, never before
+ // the next frame (writes served before the shell's clock was first
+ // seen are stamped 0, which `advance` would count back from).
+ if (p.fs.batch.bytes.items.len > 0) return @max(next, p.fs.batch.last_ns + ctlfs.pane.batch_pause_ns);
if (p.chrome_animation.isActive() or p.presentation.animating()) return next;
var frames: ?u64 = null;
if (p.look_hover_wait) |waiting| {
diff --git a/src/regexp.zig b/src/regexp.zig
index ca396ff1..ddb5eb28 100644
--- a/src/regexp.zig
+++ b/src/regexp.zig
@@ -27,8 +27,19 @@ const mvzr = @import("mvzr");
/// enough line (`\s*(\w+)\s*=` over 20 KB of letters) runs out of budget. A
/// regex engine of sam's own would lift these; the user chose not to have
/// one.
+/// mvzr's own `Regex` holds 64 operations, some 64 pattern characters; a
+/// search pattern is often longer. Past these a pattern is refused naming
+/// the limit (`e_long`).
+pub const max_ops = 512;
+const Compiled = mvzr.SizedRegex(max_ops, 64);
+
pub const Regex = struct {
- re: mvzr.Regex,
+ re: Compiled,
+ /// The literal every match starts with, when the pattern opens with one
+ /// (`line 049`, `foo` in `foo.*bar`): a search goes straight to where it
+ /// occurs rather than trying mvzr at every line.
+ lit: [32]u8 = undefined,
+ lit_len: u8 = 0,
/// The pattern names a newline: it runs over the whole text.
spans: bool,
/// The pattern starts with `^`: a search begun mid-line skips the line.
@@ -41,19 +52,48 @@ pub const Regex = struct {
/// patterns take 45-80 ms, and a Debug build is ten times slower.
pub const budget: u64 = if (builtin.mode == .Debug) 4_000_000 else 32_000_000;
+ pub const e_long = std.fmt.comptimePrint("bad regular expression: longer than mvzr's {d} operations (about {d} pattern characters)", .{ max_ops, max_ops });
pub const e_anchor = "bad regular expression: in a pattern with \\n, ^ can only come first and $ only just before a \\n";
+ pub const e_wide = std.fmt.comptimePrint("bad regular expression: a range of runes wider than {d} in [...] is not supported", .{max_range});
+ pub const e_negated = "bad regular expression: a [^...] with non-ASCII runes is not supported (mvzr's classes hold bytes)";
+
+ /// The most runes a non-ASCII range in a class is spelled out as.
+ pub const max_range = 256;
/// `Anchor`: a pattern that names a newline has `^` other than first,
/// or `$` other than just before a `\n`, which mvzr would read as the
/// ends of the whole text and so never match where sam would.
- pub fn compile(pat: []const u8) error{ Bad, Anchor }!Regex {
+ /// The plain characters a pattern opens with, each of which every match
+ /// must start with: up to the first metacharacter, less the last one
+ /// when a quantifier makes it optional or repeated.
+ fn literalPrefix(pat: []const u8, out: *[32]u8) u8 {
+ // ponytail: any `|` and there is none (a match may start with
+ // another branch); a top-level-only check would keep `a(b|c)`'s.
+ if (std.mem.indexOfScalar(u8, pat, '|') != null) return 0;
+ var n: usize = 0;
+ while (n < pat.len and n < out.len) : (n += 1) {
+ if (std.mem.indexOfScalar(u8, "\\^$.[]()|*+?{}", pat[n]) != null) break;
+ out[n] = pat[n];
+ }
+ if (n < pat.len and std.mem.indexOfScalar(u8, "*?{", pat[n]) != null) n -|= 1;
+ return @intCast(n);
+ }
+
+ pub const Error = error{ Bad, Anchor, TooLong, WideRange, NegatedRunes };
+
+ pub fn compile(pat: []const u8) Error!Regex {
if (pat.len == 0) return error.Bad;
+ // mvzr's classes hold bytes: `[éa-z]` is written `(é|[a-z])` for it.
+ // The limits apply to the pattern as rewritten.
+ var runes: [5 * max_ops + 2]u8 = undefined;
+ if (try runeClasses(pat, &runes)) |whole| return compile(whole);
// mvzr takes `^` only at its pattern's start, so `^def|^ ` (a `^`
// after a `|`) is written `^(def| )` for it: the same lines. A mix,
// `^a|b`, has no such spelling and is refused rather than wrong.
- var joined: [258]u8 = undefined;
+ var joined: [5 * max_ops + 2]u8 = undefined;
if (try anchoredAlternation(pat, &joined)) |whole| return compile(whole);
- var buf: [256]u8 = undefined;
+ // `.` may become `[^\n]`: five bytes for one.
+ var buf: [5 * max_ops]u8 = undefined;
var len: usize = 0;
var spans = false;
// Twice over the pattern: the first pass learns whether it names a
@@ -90,22 +130,150 @@ pub const Regex = struct {
return error.Anchor;
}
if (!emit) continue;
- if (len + piece.len > buf.len) return error.Bad;
+ if (len + piece.len > buf.len) return error.TooLong;
@memcpy(buf[len..][0..piece.len], piece);
len += piece.len;
}
}
+ var lit: [32]u8 = undefined;
+ const lit_len = if (spans) 0 else literalPrefix(pat, &lit);
return .{
- .re = mvzr.compile(buf[0..len]) orelse return error.Bad,
+ .lit = lit,
+ .lit_len = lit_len,
+ .re = Compiled.compile(buf[0..len]) orelse {
+ // Too long, or malformed: told apart by trying it with room.
+ if (mvzr.SizedRegex(4 * max_ops, 256).compile(buf[0..len]) != null) return error.TooLong;
+ return error.Bad;
+ },
.spans = spans,
.bol = pat[0] == '^',
};
}
+ /// `pat` with each class that holds a non-ASCII rune written as an
+ /// alternation of its runes and a class of the rest (`[éa-z]` is
+ /// `(é|[a-z])`), a range spelled out rune by rune; null when no class
+ /// holds one.
+ fn runeClasses(pat: []const u8, out: *[5 * max_ops + 2]u8) Error!?[]const u8 {
+ var w = std.Io.Writer.fixed(out);
+ var any = false;
+ var i: usize = 0;
+ while (i < pat.len) : (i += 1) {
+ const c = pat[i];
+ if (c == '\\') {
+ w.writeAll(pat[i..@min(i + 2, pat.len)]) catch return error.TooLong;
+ i += 1;
+ continue;
+ }
+ if (c != '[') {
+ w.writeByte(c) catch return error.TooLong;
+ continue;
+ }
+ // The class's end, as mvzr finds it: the first unescaped `]`.
+ var end = i + 1;
+ while (end < pat.len and pat[end] != ']') : (end += 1) {
+ if (pat[end] == '\\') end += 1;
+ }
+ if (end >= pat.len) return error.Bad;
+ const body = pat[i + 1 .. end];
+ if (for (body) |b| {
+ if (b >= 0x80) break false;
+ } else true) {
+ w.writeAll(pat[i .. end + 1]) catch return error.TooLong;
+ i = end;
+ continue;
+ }
+ if (body[0] == '^') return error.NegatedRunes;
+ any = true;
+ try runeClass(body, &w);
+ i = end;
+ }
+ return if (any) w.buffered() else null;
+ }
+
+ fn runeClass(body: []const u8, w: *std.Io.Writer) Error!void {
+ var ascii: [5 * max_ops]u8 = undefined;
+ var n: usize = 0;
+ w.writeByte('(') catch return error.TooLong;
+ var alts: usize = 0;
+ var j: usize = 0;
+ while (j < body.len) {
+ if (body[j] == '\\') {
+ const len: usize = if (j + 1 < body.len and body[j + 1] == 'x') 4 else 2;
+ if (j + len > body.len) return error.Bad;
+ // An escape at one end of a range whose other end is a rune.
+ if (j + len + 1 < body.len and body[j + len] == '-' and body[j + len + 1] >= 0x80) return error.Bad;
+ if (n + len > ascii.len) return error.TooLong;
+ @memcpy(ascii[n..][0..len], body[j..][0..len]);
+ n += len;
+ j += len;
+ continue;
+ }
+ const lo, const lo_len = try rune(body[j..]);
+ j += lo_len;
+ var hi = lo;
+ if (j + 1 < body.len and body[j] == '-') {
+ if (body[j + 1] == '\\' and lo >= 0x80) return error.Bad;
+ if (body[j + 1] != '\\') {
+ hi, const hi_len = try rune(body[j + 1 ..]);
+ j += 1 + hi_len;
+ }
+ }
+ if (hi < lo) return error.Bad;
+ if (hi - lo + 1 > max_range) return error.WideRange;
+ // Runes that differ only in their last byte go as one
+ // alternative, `\xc3[\xa0-\xbf]` for `[à-ÿ]`: 256 runes one a
+ // time would pass `max_ops`.
+ var run: [4]u8 = undefined;
+ var run_len: usize = 0;
+ var run_hi: u8 = 0;
+ var cp = lo;
+ while (cp <= hi + 1) : (cp += 1) {
+ var enc: [4]u8 = undefined;
+ const len = if (cp > hi) 0 else std.unicode.utf8Encode(cp, &enc) catch continue; // a surrogate
+ if (run_len > 0 and (len != run_len or enc[len - 1] != run_hi + 1 or
+ !std.mem.eql(u8, enc[0 .. len - 1], run[0 .. len - 1])))
+ {
+ if (alts > 0) w.writeByte('|') catch return error.TooLong;
+ w.writeAll(run[0 .. run_len - 1]) catch return error.TooLong;
+ if (run_hi == run[run_len - 1])
+ w.writeByte(run_hi) catch return error.TooLong
+ else
+ w.print("[\\x{x:0>2}-\\x{x:0>2}]", .{ run[run_len - 1], run_hi }) catch return error.TooLong;
+ alts += 1;
+ run_len = 0;
+ }
+ if (cp > hi) break;
+ if (cp < 0x80) {
+ // `\xHH` rather than the byte: `]`, `^`, `-` and `\`
+ // would otherwise mean something in the class.
+ if (n + 4 > ascii.len) return error.TooLong;
+ _ = std.fmt.bufPrint(ascii[n..][0..4], "\\x{x:0>2}", .{cp}) catch unreachable;
+ n += 4;
+ continue;
+ }
+ if (run_len == 0) {
+ run = enc;
+ run_len = len;
+ }
+ run_hi = enc[len - 1];
+ }
+ }
+ if (n > 0) w.print("|[{s}]", .{ascii[0..n]}) catch return error.TooLong;
+ w.writeByte(')') catch return error.TooLong;
+ }
+
+ /// The rune `s` opens with and its length in bytes.
+ fn rune(s: []const u8) error{Bad}!struct { u21, usize } {
+ const len = std.unicode.utf8ByteSequenceLength(s[0]) catch return error.Bad;
+ if (len > s.len) return error.Bad;
+ return .{ std.unicode.utf8Decode(s[0..len]) catch return error.Bad, len };
+ }
+
/// `^a|^b` as `^(a|b)` in `out`, when the pattern is an alternation at
/// its top level and every branch starts with `^`; null when it is not
/// one, or no branch does.
- fn anchoredAlternation(pat: []const u8, out: *[258]u8) error{Bad}!?[]const u8 {
+ fn anchoredAlternation(pat: []const u8, out: *[5 * max_ops + 2]u8) error{Bad}!?[]const u8 {
var bars: [16]usize = undefined;
var n: usize = 0;
var depth: usize = 0;
@@ -183,6 +351,24 @@ pub const Regex = struct {
at = 0;
}
while (start <= hi and start <= last) {
+ // A pattern that opens with a literal matches only where it
+ // occurs: on to the line where it next does.
+ if (rx.lit_len > 0 and !rx.spans) {
+ const q = std.mem.indexOfPos(u8, text[0..hi], start + at, rx.lit[0..rx.lit_len]) orelse return null;
+ if (q > last) return null;
+ if (q > start + at) {
+ const line_start = if (std.mem.lastIndexOfScalar(u8, text[0..q], '\n')) |nl| nl + 1 else 0;
+ if (line_start > start) {
+ start = line_start;
+ at = q - line_start;
+ } else at = q - start;
+ if (rx.bol and at > 0) {
+ start = (std.mem.indexOfScalarPos(u8, text[0..hi], q, '\n') orelse return null) + 1;
+ at = 0;
+ continue;
+ }
+ }
+ }
const end = if (rx.spans) hi else std.mem.indexOfScalarPos(u8, text[0..hi], start, '\n') orelse hi;
const line = text[start..end];
// matchPos finds nothing at a haystack's very end, where `$` or
@@ -240,6 +426,56 @@ test "lines are haystacks: ^ and $ at each line, . never a newline, \\n spans li
try std.testing.expectError(error.Bad, Regex.compile("a\\"));
}
+test "a pattern opening with a literal finds what a search from each line finds" {
+ const text = "alpha beta\nbeta gamma\ngamma alpha\nfoo line 049999 x\n";
+ for ([_][]const u8{ "beta", "gam+a", "line 049999", "alph?a", "a.*a", "^gamma", "o+", "h$|zz", "beta|x" }) |pat| {
+ var fast = try Regex.compile(pat);
+ var slow = try Regex.compile(pat);
+ slow.lit_len = 0;
+ var from: usize = 0;
+ while (from < text.len) : (from += 1) {
+ const a = try fast.find(text, from, text.len, text.len);
+ const b = try slow.find(text, from, text.len, text.len);
+ try std.testing.expectEqual(b == null, a == null);
+ if (a) |m| try std.testing.expectEqual(b.?.start, m.start);
+ }
+ }
+}
+
+test "a class with non-ASCII runes matches those runes: [éa-z], a range of them, and refuses a wide range or [^é]" {
+ const text = "1 é 2 b 3 ü 4 ñ\n";
+ var mixed = try Regex.compile("[éa-z]+");
+ try std.testing.expectEqual(@as(usize, 2), (try mixed.find(text, 0, text.len, text.len)).?.start);
+ const m = (try mixed.find(text, 5, text.len, text.len)).?;
+ try std.testing.expectEqualStrings("b", text[m.start..m.end]);
+ var range = try Regex.compile("3 [à-ÿ]");
+ const r = (try range.find(text, 0, text.len, text.len)).?;
+ try std.testing.expectEqualStrings("3 ü", text[r.start..r.end]);
+ // An ASCII member that means something in a class stays a member.
+ var odd = try Regex.compile("[ñ\\]^-]");
+ try std.testing.expectEqual(@as(usize, 16), (try odd.find(text, 0, text.len, text.len)).?.start);
+ var none = try Regex.compile("[ö]");
+ try std.testing.expect(try none.find(text, 0, text.len, text.len) == null);
+ try std.testing.expectError(error.WideRange, Regex.compile("[ā-ӿ]"));
+ try std.testing.expectError(error.NegatedRunes, Regex.compile("[^é]"));
+ try std.testing.expectError(error.Bad, Regex.compile("[é"));
+ try std.testing.expectError(error.Bad, Regex.compile("[ÿ-à]"));
+ // A full 256-rune range fits the limit as rewritten.
+ var wide = try Regex.compile("x[Ā-ǿ]");
+ try std.testing.expectEqual(@as(usize, 2), (try wide.find("ǿxǿ", 0, 5, 5)).?.start);
+ var kana = try Regex.compile("[ぁ-ゟ]"); // 3 bytes, across a last-byte wrap
+ try std.testing.expectEqual(@as(usize, 1), (try kana.find("aゞ", 0, 4, 4)).?.start);
+ try std.testing.expectError(error.WideRange, Regex.compile("[Ā-Ȁ]"));
+}
+
+test "a pattern past 64 characters compiles, and one past the limit says so" {
+ var long = try Regex.compile("a" ** 200);
+ const text = "x" ++ "a" ** 200 ++ "\n";
+ try std.testing.expectEqual(@as(usize, 1), (try long.find(text, 0, text.len, text.len)).?.start);
+ try std.testing.expectError(error.TooLong, Regex.compile("a" ** (max_ops + 8)));
+ try std.testing.expectError(error.Bad, Regex.compile("a[b"));
+}
+
test "a ^ after | anchors that branch: ^def|^ finds a line that starts either way, and a mix is refused" {
const text = "x def\n a\ndef b\n";
var both = try Regex.compile("^def|^ ");
diff --git a/src/sam_edit.zig b/src/sam_edit.zig
index a88728aa..0f317a83 100644
--- a/src/sam_edit.zig
+++ b/src/sam_edit.zig
@@ -111,11 +111,26 @@ pub fn waitsFor(arena: std.mem.Allocator, command: []const u8) Wait {
var ps: Parser = .{ .arena = arena, .src = src, .why = &why };
while (true) {
const c = ps.parse(0) catch
- return if (ps.open_text) .text else if (std.mem.eql(u8, why.text(), "unmatched `{'")) .group else .none;
+ return if (ps.open_text) .text else if (std.mem.eql(u8, why.text(), "unmatched `{'") or groupOpen(command)) .group else .none;
if (c == null) return if (ps.open_text) .text else .none;
}
}
+/// A `{` block not closed yet by its `}` line: a command in it that fails
+/// to parse still leaves the block to end at its `}`, so the error is that
+/// command's, not the `}`'s taken as a line of its own.
+fn groupOpen(command: []const u8) bool {
+ var depth: usize = 0;
+ var lines = std.mem.splitScalar(u8, command, '\n');
+ while (lines.next()) |raw| {
+ const line = std.mem.trim(u8, raw, " \t\r");
+ if (std.mem.eql(u8, line, "}")) {
+ depth -|= 1;
+ } else if (std.mem.endsWith(u8, line, "{")) depth += 1;
+ }
+ return depth > 0;
+}
+
pub fn needsMore(arena: std.mem.Allocator, command: []const u8) bool {
var why: Why = .{};
const src = std.fmt.allocPrint(arena, "{s}\n", .{command}) catch return false;
@@ -445,6 +460,9 @@ const Exec = struct {
fn compile(ex: *Exec, pat: []const u8, c: u8) Failure!regexp.Regex {
return regexp.Regex.compile(pat) catch |err| switch (err) {
error.Anchor => fail(ex.why, "{s}", .{regexp.Regex.e_anchor}),
+ error.TooLong => fail(ex.why, "{s}", .{regexp.Regex.e_long}),
+ error.WideRange => fail(ex.why, "{s}", .{regexp.Regex.e_wide}),
+ error.NegatedRunes => fail(ex.why, "{s}", .{regexp.Regex.e_negated}),
error.Bad => fail(ex.why, "bad regexp in {c} command", .{c}),
};
}
@@ -455,6 +473,9 @@ const Exec = struct {
/// reaches back into what the last one took.
fn find(ex: *Exec, rx: *regexp.Regex, from: usize, hi: usize) Failure!?regexp.Regex.Match {
if (from > hi) return null;
+ // The budget is each search's own: an x over 100k lines makes 100k
+ // searches, none of which is the slow one.
+ rx.steps = regexp.Regex.budget;
const m = (rx.find(ex.text, from, hi, hi) catch return fail(ex.why, "{s}", .{addr_lang.e_slow})) orelse return null;
var start = modal.runeStart(ex.text, m.start);
if (start < from) start = modal.runeEnd(ex.text, m.start);
@@ -693,6 +714,20 @@ fn expectEdit(text: []const u8, command: []const u8, want: []const u8) !void {
try std.testing.expectEqualStrings(want, got);
}
+test "an x over 100k lines is 100k searches, each with its own step budget" {
+ const th = @import("ninep/testing.zig");
+ const line = "x" ** 80 ++ "\n";
+ const text = try std.testing.allocator.alloc(u8, line.len * 100_000);
+ defer std.testing.allocator.free(text);
+ for (0..100_000) |k| @memcpy(text[line.len * k ..][0..line.len], line);
+ const p = try th.withFile(std.testing.allocator, text);
+ defer p.deinit();
+ const tree = @import("ninep/tree.zig");
+ const r = th.wr(p, tree.Node.of(th.serialOf(p), .ctl), "Edit ,x/x+/c/z/\n");
+ try std.testing.expectEqual(tree.Status.ok, r.reply.status);
+ try std.testing.expect(std.mem.startsWith(u8, p.panes[0].?.file.?.content, "z\nz\n"));
+}
+
test "sam's classic commands" {
try expectEdit("foo x foo y foo\n", ",x/foo/c/foobar/", "foobar x foobar y foobar\n");
try expectEdit("a b\nc d\n", ",x/ /c/_/", "a_b\nc_d\n");