From 84c8209d02f6830db861c3a8dde0c710c88075f0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 9 Nov 2026 03:29:30 -0300 Subject: idioms: the edit surface fails by name: the undo history (Journal.mark, Text.remember and step, File's, terminal's and edit's pushUndo and undoStep) returns error{OutOfMemory} instead of dropping an undo point, setEditText, enterInsert and insertTab return edit.Error with try where they reported each failure, Registers.put and setYank return error{OutOfMemory} instead of losing a yank, and their other-file callers decide (try under edit.Error, said on the pane by Undo, Redo, Pipe, Edit, Query, keys, look, completion, Incl and Plumb, ENOMEM or null where a 9P write answers, Ask's false); ctl.get and getFile return GetError (fs.ReadError, now named at fs.zig's surface, and InvalidName) for the *anyerror out-parameter and errno, the 9P get, Get and Edit's e mapping it as before; the OOM undo test pins the error; perf.zig's try relabels its harness hash, the three baselines re-keyed to it (their numbers the base's) Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/cols.zig | 4 +-- src/ninep/ctl.zig | 77 +++++++++++++++++++++++++----------------------------- src/ninep/pane.zig | 9 ++++--- 3 files changed, 43 insertions(+), 47 deletions(-) (limited to 'src/ninep') diff --git a/src/ninep/cols.zig b/src/ninep/cols.zig index 5218b937..c8d84e5d 100644 --- a/src/ninep/cols.zig +++ b/src/ninep/cols.zig @@ -124,8 +124,8 @@ pub fn applyTruncation(p: *Pardes, serial: ?u32) void { fn clear(p: *Pardes, serial: ?u32) tree.Status { const t = (header(p, serial) orelse return .err).text; - const empty = p.gpa.alloc(u8, 0) catch return .err; - t.remember(p.gpa, if (t.own) |own| .{ .text = own } else null); + t.remember(p.gpa, if (t.own) |own| .{ .text = own } else null) catch return .err; + const empty = p.gpa.alloc(u8, 0) catch unreachable; // no byte: the allocator is not asked if (t.own) |own| p.gpa.free(own); t.own = empty; t.cur_row = 0; diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index ce92ba30..dc66663e 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -711,7 +711,10 @@ pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfM const page = try pardes.pager.parse(p.gpa, held.items); held.clearAndFree(p.gpa); if (page.text.len == 0 and f.content.len == 0) return page.deinit(p.gpa); - if (!pane.fs.nomark) panes.File.pushUndo(p, pane); + if (!pane.fs.nomark) panes.File.pushUndo(p, pane) catch |err| { + page.deinit(p.gpa); + return err; + }; panes.File.setContent(p, f, page.text); f.pager_spans = page.spans; f.saved_revision = f.revision; @@ -1101,18 +1104,16 @@ pub fn writePane(p: *Pardes, req: Req, pane: *Pane) Reply { // It ran and was refused: its words (in ename), not the line // quoted, as a builtin's refusal (Save's) says it. if (getRefused(p, pane, "get")) |said| return tree.failText(req.tag, E.IO, said); - var failed: anyerror = error.Unexpected; - const errno = get(p, pane, &failed); // Naming the path, in words 9ns reads as the errno; a peer // out of reach as a look or a Save says it. - if (errno != 0) { + get(p, pane) catch |err| { const path = if (pane_files.fileOf(pane)) |f| f.path else ""; var why_buf: pardes.Kind.Buf(48) = .{}; - const why = if (pardes.Messages.dialReason(failed)) |r| + const why = if (pardes.Messages.dialReason(err)) |r| why_buf.print("dial failed: {s}", .{r}) - else if (errno == E.NOENT) "no such file" else "cannot be read"; - return tree.failText(req.tag, errno, std.fmt.bufPrint(&p.fs.ename, "get: {s}: {s}", .{ path[0..@min(path.len, 256)], why[0..@min(why.len, 48)] }) catch &p.fs.ename); // cut to ename's 320 - } + else if (err == error.FileNotFound) "no such file" else "cannot be read"; + return tree.failText(req.tag, if (err == error.FileNotFound) E.NOENT else E.IO, std.fmt.bufPrint(&p.fs.ename, "get: {s}: {s}", .{ path[0..@min(path.len, 256)], why[0..@min(why.len, 48)] }) catch &p.fs.ename); // cut to ename's 320 + }; }, .lock => { if (mine == null) return tree.failText(req.tag, E.INVAL, tree.e_bad_ctl); @@ -1270,22 +1271,18 @@ pub fn getRefused(p: *Pardes, pane: *Pane, word: []const u8) ?[]const u8 { /// loaded into the pane, which takes its name, in one undo step that puts /// the old name back with the old text. Read first, so one that cannot be /// read changes nothing; loaded, the pane is clean. Asking about unsaved -/// edits is the caller's (getRefused). Returns an errno, 0 when loaded. -pub fn getFile(p: *Pardes, pane: *Pane, path: []const u8, failed: *anyerror) u16 { - const f = pane_files.fileOf(pane) orelse return E.INVAL; - const id = p.paneBySerial(pane.serial) orelse return E.NOENT; - const bytes = fs.read(p, path) catch |err| { - failed.* = err; - return switch (err) { - error.FileNotFound => E.NOENT, - else => E.IO, - }; +/// edits is the caller's (getRefused). +pub fn getFile(p: *Pardes, pane: *Pane, path: []const u8) GetError!void { + const f = pane_files.fileOf(pane) orelse return error.NotAFile; + const id = p.paneBySerial(pane.serial) orelse return error.FileNotFound; + const bytes = try fs.read(p, path); + panes.File.pushUndo(p, pane) catch |err| { + p.gpa.free(bytes); + return err; }; - panes.File.pushUndo(p, pane); pane_files.nameBuffer(p, id, path, false) catch |err| { p.gpa.free(bytes); - failed.* = err; - return E.INVAL; + return if (err == error.OutOfMemory) error.OutOfMemory else error.InvalidName; }; panes.File.setContent(p, f, bytes); f.saved_revision = f.revision; @@ -1295,31 +1292,30 @@ pub fn getFile(p: *Pardes, pane: *Pane, path: []const u8, failed: *anyerror) u16 f.disk_newer_warned = false; f.disk_gone = false; if (fs.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); - return 0; } -pub fn get(p: *Pardes, pane: *Pane, failed: *anyerror) u16 { - const f = pane_files.fileOf(pane) orelse return 0; +/// What Get and Edit's `e` fail with, as their callers say it (the 9P +/// reply's errno, FileNotFound ENOENT and the rest EIO; a peer's dial, +/// Messages.dialReason; Get's words, Messages.errorWords): a read's, and a +/// name the pane cannot take. +pub const GetError = fs.ReadError || error{InvalidName}; + +pub fn get(p: *Pardes, pane: *Pane) GetError!void { + const f = pane_files.fileOf(pane) orelse return; // A directory pane: the directory read again (Dir.zig). - if (f.listing != null) { - panes.Dir.refresh(p, p.paneBySerial(pane.serial).?) catch |err| { - failed.* = err; - return if (err == error.FileNotFound) E.NOENT else E.IO; - }; - return 0; - } - if (!panes.Output.fileTraits(f.output).saves) return 0; - const bytes = fs.read(p, f.path) catch |err| { - failed.* = err; - return switch (err) { - error.FileNotFound => E.NOENT, - else => E.IO, - }; + if (f.listing != null) return panes.Dir.refresh(p, p.paneBySerial(pane.serial).?) catch |err| switch (err) { + error.FileNotFound, error.OutOfMemory => |e| e, + else => error.ReadFailed, }; + if (!panes.Output.fileTraits(f.output).saves) return; + const bytes = try fs.read(p, f.path); // Throwing unsaved edits away for it is a reload the log hears of, as a // clean one from the watch is. const discarded = f.revision != f.saved_revision; - panes.File.pushUndo(p, pane); + panes.File.pushUndo(p, pane) catch |err| { + p.gpa.free(bytes); + return err; + }; panes.File.setContent(p, f, bytes); f.saved_revision = f.revision; f.saved_hash = std.hash.Wyhash.hash(0, f.content); @@ -1330,7 +1326,6 @@ pub fn get(p: *Pardes, pane: *Pane, failed: *anyerror) u16 { var rec: [32]u8 = undefined; pardes.ctlfs.events.notePath(p, "changed", std.fmt.bufPrint(&rec, "{d} reloaded", .{pane.serial}) catch unreachable); // a u32 fits 32 } - return 0; } // ---- tests ---- @@ -2658,7 +2653,7 @@ test "what a /pager open writes after its directory is the text: plain in the bo try testing.expectEqual(@as(usize, 0), f.pager_spans.len); // Undone back to the paged text: the text comes back, its colours // stay dropped (they were the program's, for the text it printed). - panes.File.undoStep(p, pn, true); + try panes.File.undoStep(p, pn, true); try testing.expectEqualStrings("red /a\nNext\n", f.content); try testing.expectEqual(@as(usize, 0), f.pager_spans.len); diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index ce246465..b77d72d7 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -300,7 +300,7 @@ pub fn spliceBody(p: *Pardes, pane: *Pane, q0: usize, q1: usize, bytes: []const if (std.mem.eql(u8, f.content[lo..hi], bytes[0..take])) return take; const join = pane.fs.joined == f.revision; pane.fs.joined = null; - if (!pane.fs.nomark and !join) panes.File.pushUndo(p, pane); + if (!pane.fs.nomark and !join) panes.File.pushUndo(p, pane) catch return null; if (!panes.File.replace(p, f, lo, hi - lo, bytes[0..take])) return null; return take; } @@ -652,9 +652,10 @@ pub fn applyTagTruncation(p: *Pardes, pane: *Pane) void { const pf = &pane.fs; if (!pf.tag_trunc_pending) return; pf.tag_trunc_pending = false; - const empty = p.gpa.alloc(u8, 0) catch return; // An edit like a keyboard one: `u` in the tag brings it back. - pane.tag.remember(p.gpa, if (pane.tag.own) |own| .{ .text = own } else null); + pane.tag.remember(p.gpa, if (pane.tag.own) |own| .{ .text = own } else null) catch |err| + return p.reportError(p.paneBySerial(pane.serial) orelse return, "tag", err); + const empty = p.gpa.alloc(u8, 0) catch unreachable; // no byte: the allocator is not asked if (pane.tag.own) |own| p.gpa.free(own); pane.tag.own = empty; // what is left is the prefix; the cursor goes after it @@ -800,7 +801,7 @@ fn writeFlag(p: *Pardes, req: Req, pane: *Pane, file: PaneFile) Reply { // step starts here: the text as it is now is what Undo brings back, // and turning mark on again only ends it. .mark => { - if (!on and !pf.nomark) panes.File.pushUndo(p, pane); + if (!on and !pf.nomark) panes.File.pushUndo(p, pane) catch return Reply.fail(req.tag, E.NOMEM); pf.nomark = !on; }, .scroll => pf.noscroll = !on, -- cgit v1.3