From a66a9eb6e0c69cfcb63717bc27704cdc96f382b4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 12:40:11 -0300 Subject: idioms #11, the command layer: exec, builtins, edit_cmd, Messages, Dir, Output, Jump and Pipe say their messages through buffers proven big enough (or the long argument cut) instead of placeholder text, replId takes its 48-byte buffer, a failed Save path, Get or Incl path, Repl send, Tutor, +Unsaved, pipe or Edit output on OOM is reported (reportError) rather than silently dropped, Edit's why and zonWhy cut a long text instead of losing it, and writeFailure names its set Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/builtins.zig | 76 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) (limited to 'src/builtins.zig') diff --git a/src/builtins.zig b/src/builtins.zig index ada02e0c..76fb959d 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -594,14 +594,14 @@ pub fn warnModifiedIn(c: Ctx, asking: Pane.Discarding, which: Asked) bool { // 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"); + 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 unreachable); // a u32 and 48 fit 96 } if (count > 0 and (c.p.fs.no_prompt or c.p.fs.capturing)) { var said_buf: [limits.host_path_cap + 96]u8 = undefined; const said = (if (count == 1) 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"; + std.fmt.bufPrint(&said_buf, "{d} unsaved panes: Modified ({s} again to discard)", .{ count, @tagName(asking) })) catch unreachable; // one is one_buf's at most var room: @TypeOf(c.p.fs.failure) = undefined; const kept = pardes.ctlfs.fitErr(said, &room); @memcpy(c.p.fs.failure[0..kept.len], kept); @@ -707,7 +707,7 @@ pub const Restore = struct { const file = if (typed) |t| dump.restoreFile(t, c.p.settings.dump_dir.get(), &full_buf) else null; const name = if (file) |path| path else pardes.tape.authority(pardes.Db.labelled, .{ &c.p.db, c.p, typed, @as([]u8, &label_buf) }) orelse { var buf: [320]u8 = undefined; - return c.p.reportFailure(c.id, if (typed) |t| std.fmt.bufPrint(&buf, "Restore: no dump labelled {s}", .{t[0..@min(t.len, 256)]}) catch "Restore: no dump of that label" else "Restore: no dump yet; name one"); + return c.p.reportFailure(c.id, if (typed) |t| std.fmt.bufPrint(&buf, "Restore: no dump labelled {s}", .{t[0..@min(t.len, 256)]}) catch unreachable else "Restore: no dump yet; name one"); // 256 fit 320 }; if (name.len > c.p.restore_buf.len) return c.p.reportFailure(c.id, "Restore: that name is too long"); // A file that cannot be read is said now, and fails a ctl write: @@ -715,7 +715,7 @@ pub const Restore = struct { // First, so a Restore that could never happen spends no warning. if (file) |path| { const bytes = @import("fs.zig").readFile(c.p.gpa, path) catch |err| { - var buf: [limits.host_path_cap + 64]u8 = undefined; + var buf: [limits.host_path_cap + 160]u8 = undefined; var words: [128]u8 = undefined; const why = switch (err) { error.FileNotFound => "no such file", @@ -723,7 +723,7 @@ pub const Restore = struct { error.IsDirectory => "a directory, not a dump", else => pardes.Messages.errorWords(err, &words), }; - return c.p.reportFailure(c.id, std.fmt.bufPrint(&buf, "Restore: {s}: {s}", .{ path, why }) catch "Restore: cannot read the dump"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&buf, "Restore: {s}: {s}", .{ path[0..@min(path.len, limits.host_path_cap)], why }) catch unreachable); // cap and 128 fit }; defer c.p.gpa.free(bytes); // Nor does a file that is not a dump, before any warning. @@ -731,7 +731,7 @@ pub const Restore = struct { pardes.dump.check(c.p.gpa, bytes, &why_buf) catch |err| { var buf: [limits.host_path_cap + 320]u8 = undefined; const why = if (err == error.OutOfMemory) pardes.Messages.errorWords(err, &why_buf) else why_buf[0..std.mem.indexOfScalar(u8, &why_buf, 0).?]; - return c.p.reportFailure(c.id, std.fmt.bufPrint(&buf, "Restore: {s}: not a pardes dump: {s}", .{ path, why }) catch "Restore: not a pardes dump"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&buf, "Restore: {s}: not a pardes dump: {s}", .{ path[0..@min(path.len, limits.host_path_cap)], why }) catch unreachable); // cap and 256 fit }; } // acme's Load adds a dump's windows to the ones there; a Restore @@ -788,7 +788,7 @@ pub const Mount = struct { if (dial.len == 0) return c.p.reportFailure(c.id, e_args); c.p.fs.mount(c.p.gpa, name, dial) catch |err| { var said: [200]u8 = undefined; - if (err == error.BadDial) return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Mount: bad dial address: {s} (unix!/path, /path, tcp!!, or a session name)", .{dial[0..@min(dial.len, 100)]}) catch "Mount: bad dial address"); + if (err == error.BadDial) return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Mount: bad dial address: {s} (unix!/path, /path, tcp!!, or a session name)", .{dial[0..@min(dial.len, 100)]}) catch unreachable); // 100 and 92 fit 200 return c.p.reportError(c.id, "Mount", err); }; // A socket path that is not there: said so (ENOENT), not a dial @@ -800,7 +800,7 @@ pub const Mount = struct { c.p.gpa.free(gone.dial); } var said: [200]u8 = undefined; - return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Mount {s}: {s}: no such socket", .{ name, path[0..@min(path.len, 100)] }) catch "Mount: no such socket"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Mount {s}: {s}: no such socket", .{ name[0..@min(name.len, 32)], path[0..@min(path.len, 100)] }) catch unreachable); // 132 and 25 fit 200 }; // A mount that cannot be reached is said now, and taken back, not // found out by the first look through it. @@ -811,7 +811,7 @@ pub const Mount = struct { c.p.gpa.free(gone.dial); } var what: [160]u8 = undefined; - return c.p.reportError(c.id, std.fmt.bufPrint(&what, "Mount {s} {s}", .{ name, dial[0..@min(dial.len, 100)] }) catch "Mount", err); + return c.p.reportError(c.id, std.fmt.bufPrint(&what, "Mount {s} {s}", .{ name[0..@min(name.len, 32)], dial[0..@min(dial.len, 100)] }) catch unreachable, err); // 132 and 7 fit 160 }; } }; @@ -829,7 +829,7 @@ pub const Unmount = struct { // "no such": the words 9ns reads as ENOENT. if (err == error.NotMounted) { var said: [96]u8 = undefined; - return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Unmount: no such mount {s}", .{name[0..@min(name.len, 32)]}) catch "Unmount: no such mount"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Unmount: no such mount {s}", .{name[0..@min(name.len, 32)]}) catch unreachable); // 32 and 23 fit 96 } return c.p.reportError(c.id, "Unmount", err); }; @@ -879,7 +879,7 @@ pub const ThemeFile = struct { // Said with why, not "invalid theme path". if (!std.mem.endsWith(u8, arg, ".zon")) { var said: [320]u8 = undefined; - return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "ThemeFile: {s}: invalid theme path: a theme file is a .zon (DumpThemes writes some to start from)", .{arg[0..@min(arg.len, 200)]}) catch "ThemeFile: invalid theme path: a theme file is a .zon"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "ThemeFile: {s}: invalid theme path: a theme file is a .zon (DumpThemes writes some to start from)", .{arg[0..@min(arg.len, 200)]}) catch unreachable); // 200 and 85 fit 320 } colors.requestThemeFile(c.p, c.id, arg); } @@ -935,8 +935,8 @@ pub const TreeContext = struct { fn stateArg(c: Ctx, comptime T: type, comptime builtin_word: []const u8, comptime takes: []const u8) ?T { const a = std.mem.trim(u8, c.arg orelse return null, " \t"); return std.meta.stringToEnum(T, a) orelse { - var said: [96]u8 = undefined; - c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "invalid argument: " ++ builtin_word ++ " takes " ++ takes ++ ", not \"{s}\"", .{a[0..@min(a.len, 32)]}) catch "invalid argument"); + var said: [128]u8 = undefined; + c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "invalid argument: " ++ builtin_word ++ " takes " ++ takes ++ ", not \"{s}\"", .{a[0..@min(a.len, 32)]}) catch unreachable); // its callers' words and 32 fit 128 return null; }; } @@ -1057,22 +1057,22 @@ pub const Get = struct { if (!panes.Output.fileTraits(f.output).saves) return c.p.reportFailure(c.id, "Get: only a file pane takes it"); var failed: anyerror = error.Unexpected; var home_buf: [4096]u8 = undefined; - const path = if (typed.len == 0) c.pane.file.?.path else std.fs.path.resolvePosix(c.p.scratch.allocator(), &.{ Pardes.paneDir(c.pane), @import("fs.zig").expandHome(typed, &home_buf) }) catch return; + const path = if (typed.len == 0) c.pane.file.?.path else std.Io.Dir.path.resolvePosix(c.p.scratch.allocator(), &.{ Pardes.paneDir(c.pane), @import("fs.zig").expandHome(typed, &home_buf) }) catch |err| return c.p.reportError(c.id, "Get", err); // A file that is not there, or a directory, is said before unsaved // edits are asked about: there would be nothing to load over them. if (typed.len > 0) if (@import("fs.zig").localPath(path)) |local| { var said: [320]u8 = undefined; const kind = exec.kindOf(local); if (kind == null or kind.? == .directory) - return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Get: {s}: {s}", .{ path[0..@min(path.len, 256)], if (kind == null) "no such file" else "is a directory" }) catch "Get: no such file"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Get: {s}: {s}", .{ path[0..@min(path.len, 256)], if (kind == null) "no such file" else "is a directory" }) catch unreachable); // 256 fit 320 }; if (ctl.getRefused(c.p, c.pane, "Get")) |said| return c.p.reportFailure(c.id, said); const errno = if (typed.len == 0) ctl.get(c.p, c.pane, &failed) else ctl.getFile(c.p, c.pane, path, &failed); if (errno != 0) { - var said: [320]u8 = undefined; + var said: [400]u8 = undefined; var words: [128]u8 = undefined; const why = if (failed == error.FileNotFound) "no such file" else pardes.Messages.errorWords(failed, &words); - c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Get: {s}: {s}", .{ path[0..@min(path.len, 256)], why }) catch "Get: cannot be read"); + c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Get: {s}: {s}", .{ path[0..@min(path.len, 256)], why }) catch unreachable); // 256 and 128 fit 400 } } }; @@ -1118,20 +1118,20 @@ pub const Incl = struct { const shown = dir_word[0..@min(dir_word.len, 200)]; if (std.mem.eql(u8, dir_word, "-")) return p.reportFailure(c.id, "Incl: invalid: - clears the list, alone"); if (dir_word[0] != '/' and dir_word[0] != '~') - return p.reportFailure(c.id, std.fmt.bufPrint(&said, "Incl: invalid directory: {s} is relative", .{shown}) catch "Incl: invalid directory"); + return p.reportFailure(c.id, std.fmt.bufPrint(&said, "Incl: invalid directory: {s} is relative", .{shown}) catch unreachable); // 200 fit 320 var home_buf: [4096]u8 = undefined; const expanded = @import("fs.zig").expandHome(dir_word, &home_buf); if (@import("fs.zig").localPath(expanded)) |local| if (exec.kindOf(local) != .directory) return p.reportFailure(c.id, if (exec.kindOf(local) != null) - std.fmt.bufPrint(&said, "Incl: not a directory: {s}", .{shown}) catch "Incl: not a directory" + std.fmt.bufPrint(&said, "Incl: not a directory: {s}", .{shown}) catch unreachable // 200 fit 320 else - std.fmt.bufPrint(&said, "Incl: no such directory: {s}", .{shown}) catch "Incl: no such directory"); + std.fmt.bufPrint(&said, "Incl: no such directory: {s}", .{shown}) catch unreachable); // 200 fit 320 } var words = std.mem.tokenizeAny(u8, typed, " \t"); while (words.next()) |dir_word| { var home_buf: [4096]u8 = undefined; - const full = std.fs.path.resolvePosix(p.scratch.allocator(), &.{ Pardes.paneDir(c.pane), @import("fs.zig").expandHome(dir_word, &home_buf) }) catch return; - if (!std.fs.path.isAbsolute(full)) return p.reportFailure(c.id, "Incl: invalid directory"); + const full = std.Io.Dir.path.resolvePosix(p.scratch.allocator(), &.{ Pardes.paneDir(c.pane), @import("fs.zig").expandHome(dir_word, &home_buf) }) catch |err| return p.reportError(c.id, "Incl", err); + if (!std.Io.Dir.path.isAbsolute(full)) return p.reportFailure(c.id, "Incl: invalid directory"); for (p.incl.items) |dir| { if (std.mem.eql(u8, dir, full)) break; } else { @@ -1141,9 +1141,9 @@ pub const Incl = struct { } if (typed.len > 0) return; var list: std.Io.Writer.Allocating = .init(p.scratch.allocator()); - list.writer.writeAll("Incl:") catch return; - for (p.incl.items) |dir| list.writer.print(" {s}", .{dir}) catch return; - if (p.incl.items.len == 0) list.writer.writeAll(" none") catch return; + list.writer.writeAll("Incl:") catch |err| return p.reportError(c.id, "Incl", err); + for (p.incl.items) |dir| list.writer.print(" {s}", .{dir}) catch |err| return p.reportError(c.id, "Incl", err); + if (p.incl.items.len == 0) list.writer.writeAll(" none") catch |err| return p.reportError(c.id, "Incl", err); p.setMessage(c.id, list.written()); } }; @@ -1210,19 +1210,19 @@ pub const Save = struct { if (c.pane.file == null and !c.pane.isTerminal()) { const kind = if (c.pane.addon) |k| k.nameOf() else ""; var said: [64]u8 = undefined; - return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Save: a{s} {s} pane has no text to save", .{ if (kind.len > 0 and std.mem.indexOfScalar(u8, "aeiou", kind[0]) != null) "n" else "", kind }) catch "Save: no text to save"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Save: a{s} {s} pane has no text to save", .{ if (kind.len > 0 and std.mem.indexOfScalar(u8, "aeiou", kind[0]) != null) "n" else "", kind }) catch unreachable); // a kind's 16-byte name and 35 fit 64 } // A directory's listing is no file to write, bare or to a path, as // acme's Put does nothing in a directory window. if (c.pane.file) |file| if (file.listing != null) { var said: [limits.host_path_cap + 48]u8 = undefined; - return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Save: {s} is a directory", .{file.path[0..@min(file.path.len, limits.host_path_cap)]}) catch "Save: a directory pane has no file to save"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Save: {s} is a directory", .{file.path[0..@min(file.path.len, limits.host_path_cap)]}) catch unreachable); // cap and 21 fit }; const path = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); if (path.len > 0) return exec.saveTo(c.p, c.id, path); // A `+` name is a generated pane's (+Tutor), no file of its own: // a bare Save asks for a path, as a +New scratch's does. - if (c.pane.file) |file| if (file.output == null and !std.mem.startsWith(u8, std.fs.path.basename(file.path), "+")) return exec.saveFile(c.p, c.id); + if (c.pane.file) |file| if (file.output == null and !std.mem.startsWith(u8, std.Io.Dir.path.basename(file.path), "+")) return exec.saveFile(c.p, c.id); if (c.pane.file != null or c.pane.isTerminal()) panes.startPrompt(c.p, c.pane, &c.pane.body, .save); } }; @@ -1280,7 +1280,7 @@ pub const Del = struct { c.pane.question_due_ns = exec.clockNow(c.p) + config.del_question_delay_ns; c.p.view().active = c.id; var rec: [32]u8 = undefined; - pardes.ctlfs.events.notePath(c.p, "ask", std.fmt.bufPrint(&rec, "{d} del k j", .{c.pane.serial}) catch ""); + pardes.ctlfs.events.notePath(c.p, "ask", std.fmt.bufPrint(&rec, "{d} del k j", .{c.pane.serial}) catch unreachable); // a u32 fits 32 // The question says Del already. if (c.pane.msg_announcement) c.pane.msg_len = 0; return; @@ -1422,7 +1422,7 @@ pub const Term = struct { // The authority's lookup, taped, as Shell's (exec.zig). if (pardes.tape.value(.shell, @import("host_io.zig").Shell.refusal, .{ arg[0..@min(arg.len, 200)], &why })) |refused| { var said: [340]u8 = undefined; - return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Term: {s}", .{refused}) catch "Term: no such shell"); + return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Term: {s}", .{refused}) catch unreachable); // refused is at most why's 320 } } const pane = exec.spawnTty(c.p, c.id) orelse return; @@ -1455,7 +1455,7 @@ pub const Repl = struct { var idbuf: [48]u8 = undefined; if (arg.len == 0) { if (pane.repl == null) return c.p.setMessage(c.id, "Repl: not bound"); - return c.p.setMessage(c.id, std.fmt.bufPrint(&buf, "Repl: {s}", .{exec.replId(&idbuf, pane)}) catch "Repl"); + return c.p.setMessage(c.id, std.fmt.bufPrint(&buf, "Repl: {s}", .{exec.replId(&idbuf, pane)}) catch unreachable); // 48 fit 320 } if (std.mem.eql(u8, arg, "-")) { pane.repl = null; @@ -1467,9 +1467,9 @@ pub const Repl = struct { const named: ?u8 = if (manifest.byName(arg)) |i| if (grammars[i].exts.len > 0) @intCast(i) else null else null; const lang: u8 = named orelse { // A few, whole, and where the rest are: a list cut to fit the - // row would end mid-word. + // row would end mid-word. unreachable: 24, six names and 60 fit 320. var w = std.Io.Writer.fixed(&buf); - w.print("Repl: invalid language \"{s}\"; - or one like", .{@import("Messages.zig").clip(arg, 24)}) catch {}; + w.print("Repl: invalid language \"{s}\"; - or one like", .{@import("Messages.zig").clip(arg, 24)}) catch unreachable; // Those that start as the word does first, a misspelling's // likeliest meaning (pithon: python). var shown: usize = 0; @@ -1479,15 +1479,15 @@ pub const Repl = struct { if (starts != alike) continue; known += 1; if (shown < 6) { - w.print(" {s}", .{g.name}) catch {}; + w.print(" {s}", .{g.name}) catch unreachable; shown += 1; } }; - w.print(" ({d} more)", .{known - shown}) catch {}; + w.print(" ({d} more)", .{known - shown}) catch unreachable; return c.p.reportFailure(c.id, w.buffered()); }; if (pane.repl) |r| if (r.lang == lang) - return c.p.setMessage(c.id, std.fmt.bufPrint(&buf, "Repl: {s}", .{exec.replId(&idbuf, pane)}) catch "Repl"); + return c.p.setMessage(c.id, std.fmt.bufPrint(&buf, "Repl: {s}", .{exec.replId(&idbuf, pane)}) catch unreachable); // 48 fit 320 // The lowest letter no other REPL of the language has: freed ones // come back. var letter: u8 = 'a'; @@ -1501,7 +1501,7 @@ pub const Repl = struct { if (letter > 'z') return c.p.reportFailure(c.id, "Repl: every letter of the language is taken"); pane.repl = .{ .lang = lang, .letter = letter }; pane.repl_warned = false; - c.p.setMessage(c.id, std.fmt.bufPrint(&buf, "Repl: {s}", .{exec.replId(&idbuf, pane)}) catch "Repl"); + c.p.setMessage(c.id, std.fmt.bufPrint(&buf, "Repl: {s}", .{exec.replId(&idbuf, pane)}) catch unreachable); // 48 fit 320 } }; @@ -1525,7 +1525,7 @@ pub const Tutor = struct { pub const scope: Scope = .session; pub fn run(c: Ctx) void { const free = c.p.freeSlot() orelse return c.p.reportError(c.id, "Tutor", error.NoPaneSlots); - _ = panes.openTutorView(c.p, free) catch return; + _ = panes.openTutorView(c.p, free) catch |err| return c.p.reportError(c.id, "Tutor", err); exec.placeNew(c.p, c.id, c.id, free, .doc); // a doc like any other } }; -- cgit v1.3