diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 12:40:11 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 14:56:36 -0300 |
| commit | a66a9eb6e0c69cfcb63717bc27704cdc96f382b4 (patch) | |
| tree | 400e3034a6c71c1d15d0ad302e1293a4290071b9 /src/exec.zig | |
| parent | 36a51ddc67d0beb7d9851fb2425cf780a47fed36 (diff) | |
| download | pardes-a66a9eb6e0c69cfcb63717bc27704cdc96f382b4.tar.gz pardes-a66a9eb6e0c69cfcb63717bc27704cdc96f382b4.zip | |
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/exec.zig')
| -rw-r--r-- | src/exec.zig | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/src/exec.zig b/src/exec.zig index eb42424f..f90a2315 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -332,7 +332,7 @@ pub fn saveFile(p: *Pardes, id: usize) void { if (!f.disk_newer_warned) { f.disk_newer_warned = true; var said: [Pardes.SavePath.cap + 64]u8 = undefined; - return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} modified on disk since read (Save again to overwrite)", .{f.path}) catch "modified on disk since read (Save again to overwrite)"); + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} modified on disk since read (Save again to overwrite)", .{f.path[0..@min(f.path.len, Pardes.SavePath.cap)]}) catch unreachable); // cap and 55 fit } f.disk_newer = null; } @@ -345,11 +345,11 @@ pub fn saveTo(p: *Pardes, id: usize, path: []const u8) void { std.mem.indexOfScalar(u8, path, '\n') != null) return p.reportError(id, "save", error.NoFilename); var home_buf: [4096]u8 = undefined; - const full = std.fs.path.resolvePosix( + const full = std.Io.Dir.path.resolvePosix( p.scratch.allocator(), &.{ Pardes.paneDir(pane), @import("fs.zig").expandHome(path, &home_buf) }, - ) catch return; - if (!std.fs.path.isAbsolute(full)) return p.reportError(id, "save", error.NoDirectory); + ) catch |err| return p.reportError(id, "save", err); + if (!std.Io.Dir.path.isAbsolute(full)) return p.reportError(id, "save", error.NoDirectory); if (full.len > Pardes.SavePath.cap) return p.reportError(id, "save", error.PathTooLong); const f = if (pane.file) |*file| file else { // a terminal writes its scrollback; an image and a PDF have nothing @@ -392,7 +392,7 @@ pub fn spawnTty(p: *Pardes, from: usize) ?*Pane { const dir = p.execDir(from); if (missingDir(p, dir)) { var said: [pardes.memory.limits.host_path_cap + 48]u8 = undefined; - p.reportFailure(from, std.fmt.bufPrint(&said, "Term: {s}: no such directory", .{dir}) catch "Term: no such directory"); + p.reportFailure(from, std.fmt.bufPrint(&said, "Term: {s}: no such directory", .{dir[0..@min(dir.len, pardes.memory.limits.host_path_cap)]}) catch unreachable); // cap and 27 fit return null; } const free = p.freeSlot() orelse { @@ -473,9 +473,9 @@ pub fn replLive(p: *const Pardes, id: usize) bool { } /// `python-a`. -pub fn replId(buf: []u8, pane: *const Pane) []const u8 { +pub fn replId(buf: *[48]u8, pane: *const Pane) []const u8 { const r = pane.repl orelse return ""; - return std.fmt.bufPrint(buf, "{s}-{c}", .{ grammars[r.lang].name, r.letter }) catch ""; + return std.fmt.bufPrint(buf, "{s}-{c}", .{ grammars[r.lang].name, r.letter }) catch unreachable; // a grammar's name is short } const Routed = union(enum) { none, sent: usize, asked }; @@ -485,7 +485,7 @@ const Routed = union(enum) { none, sent: usize, asked }; pub fn replGone(p: *Pardes, from: usize, repl: *const Pane) Routed { var idbuf: [48]u8 = undefined; var said: [128]u8 = undefined; - p.setMessage(from, std.fmt.bufPrint(&said, "Repl: {s} has no program running; nothing sent", .{replId(&idbuf, repl)}) catch "Repl: no program running"); + p.setMessage(from, std.fmt.bufPrint(&said, "Repl: {s} has no program running; nothing sent", .{replId(&idbuf, repl)}) catch unreachable); // 48 and 44 fit 128 return .asked; } @@ -586,7 +586,7 @@ pub fn sendToRepl(p: *Pardes, from: usize, to: usize, text: []const u8) void { // Lines end in \n alone (a \r\n would give Python a blank line that // ends its block), and no ESC or other control byte but a tab goes: an // ESC[201~ in the text would end a bracketed paste early. - const clean = p.scratch.allocator().alloc(u8, text.len) catch return; + const clean = p.scratch.allocator().alloc(u8, text.len) catch |err| return p.reportError(from, "Repl", err); var n: usize = 0; for (text, 0..) |c, i| { if (c == '\r') { @@ -599,7 +599,7 @@ pub fn sendToRepl(p: *Pardes, from: usize, to: usize, text: []const u8) void { // paste, or with half a block. if (n + 16 > p.writeRoom(to)) { var said: [128]u8 = undefined; - return p.setMessage(from, std.fmt.bufPrint(&said, "Repl: {d} bytes is more than can be sent at once; send less", .{n}) catch "Repl: too long to send"); + return p.setMessage(from, std.fmt.bufPrint(&said, "Repl: {d} bytes is more than can be sent at once; send less", .{n}) catch unreachable); // a usize and 56 fit 128 } const pasted = panes.terminal.bracketedPaste(dst); edit.typeToTty(p, to, dst, clean[0..n]); @@ -629,10 +629,10 @@ pub fn sendToRepl(p: *Pardes, from: usize, to: usize, text: []const u8) void { { dst.repl_warned = true; var warn: [128]u8 = undefined; - p.setMessage(from, std.fmt.bufPrint(&warn, "Repl: {s} takes no bracketed paste; a blank line in a block ends it", .{id}) catch "Repl: no bracketed paste"); + p.setMessage(from, std.fmt.bufPrint(&warn, "Repl: {s} takes no bracketed paste; a blank line in a block ends it", .{id}) catch unreachable); // a 48-byte id and 66 fit 128 } var said: [64]u8 = undefined; - p.setMessage(from, std.fmt.bufPrint(&said, "\u{2192} {s}", .{id}) catch "sent"); + p.setMessage(from, std.fmt.bufPrint(&said, "\u{2192} {s}", .{id}) catch unreachable); // a 48-byte id and 4 fit 64 src.msg_sent = true; var rec: [96]u8 = undefined; pardes.ctlfs.events.notePath(p, "send", std.fmt.bufPrint(&rec, "{d} {d} {s}", .{ src.serial, dst.serial, id }) catch return); @@ -690,7 +690,7 @@ pub fn executeFrom(p: *Pardes, id: usize, txt: []const u8, from_body: bool) ?usi const word = cmd[0 .. std.mem.indexOfAny(u8, cmd, " \t") orelse cmd.len]; if (builtins.absentWhy(word) orelse if (config.Runtime.find(word) != null) @as([]const u8, "GUI-only") else null) |why| { var said: [96]u8 = undefined; - p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is {s}, invalid here", .{ word, why }) catch "GUI-only, invalid here"); + p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is {s}, invalid here", .{ word, why }) catch unreachable); // a known word (15 at most) and 17 fit 96 return null; } // A command from a column's or the workspace's tag is that tag's: a @@ -726,7 +726,7 @@ fn runCommand(p: *Pardes, from: usize, line: []const u8) ?usize { // there: no pane, and the write fails ENOENT. if (missingDir(p, dir)) { var said: [pardes.memory.limits.host_path_cap + 48]u8 = undefined; - p.reportFailure(from, std.fmt.bufPrint(&said, "exec: {s}: no such directory", .{dir}) catch "exec: no such directory"); + p.reportFailure(from, std.fmt.bufPrint(&said, "exec: {s}: no such directory", .{dir[0..@min(dir.len, pardes.memory.limits.host_path_cap)]}) catch unreachable); // cap and 27 fit return null; } // Not one whose pty a job it left behind still prints to: reusing it @@ -806,11 +806,12 @@ fn winid(p: *const Pardes, src: ?*Pane) u32 { /// what it was given; its newlines the terminal's, so a multi-line command /// does not staircase. fn echoCommand(p: *Pardes, pane: *Pane, line: []const u8) void { + // unreachable: runCommand takes no line over command_max, each byte two at most. var buf: [2 * command_max + 8]u8 = undefined; var w = std.Io.Writer.fixed(&buf); - w.writeAll(if (panes.terminal.gridCursor(pane).x != 0) "\r\n% " else "% ") catch {}; - for (line) |c| (if (c == '\n') w.writeAll("\r\n") else w.writeByte(c)) catch {}; - w.writeAll("\r\n") catch {}; + w.writeAll(if (panes.terminal.gridCursor(pane).x != 0) "\r\n% " else "% ") catch unreachable; + for (line) |c| (if (c == '\n') w.writeAll("\r\n") else w.writeByte(c)) catch unreachable; + w.writeAll("\r\n") catch unreachable; panes.terminal.feedOutput(p, pane, w.buffered()); } @@ -821,7 +822,7 @@ fn echoCommand(p: *Pardes, pane: *Pane, line: []const u8) void { pub fn noteRun(p: *Pardes, pane: *Pane, what: []const u8, said: []const u8) void { pardes.ctlfs.events.announce(p); var buf: [command_max + 16]u8 = undefined; - pardes.ctlfs.events.notePath(p, what, std.fmt.bufPrint(&buf, "{d} {s}", .{ pane.serial, std.mem.trim(u8, said, " \t\r\n") }) catch return); + pardes.ctlfs.events.notePath(p, what, std.fmt.bufPrint(&buf, "{d} {s}", .{ pane.serial, std.mem.trim(u8, said, " \t\r\n") }) catch unreachable); // a run's line is at most command_max } /// A command pane's child is gone: the pane stays, showing what it said @@ -840,10 +841,10 @@ pub fn commandDone(p: *Pardes, id: usize, status: ?u8) void { if (status == 0) return p.removePane(id, null) catch {}; } var buf: [16]u8 = undefined; - const code = if (status) |n| std.fmt.bufPrint(&buf, "{d}", .{n}) catch "?" else "?"; + const code = if (status) |n| std.fmt.bufPrint(&buf, "{d}", .{n}) catch unreachable else "?"; // a status fits 16 var said: [32]u8 = undefined; const lead = if (panes.terminal.gridCursor(pane).x != 0) "\r\n" else ""; - panes.terminal.feedOutput(p, pane, std.fmt.bufPrint(&said, "{s}exit {s}\r\n", .{ lead, code }) catch "exit ?\r\n"); + panes.terminal.feedOutput(p, pane, std.fmt.bufPrint(&said, "{s}exit {s}\r\n", .{ lead, code }) catch unreachable); // 16 and 9 fit 32 noteRun(p, pane, "exit", code); p.needs_frame = true; } @@ -896,7 +897,7 @@ pub fn executeBuiltinLine(p: *Pardes, id: usize, txt: []const u8) bool { 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"); + p.reportFailure(id, std.fmt.bufPrint(&said, "invalid: acme's {s} is not a pardes builtin{s}", .{ w.acme, w.hint }) catch unreachable); // acme_words holds short literals return true; }; // Delete: Del, the text thrown away unasked. @@ -963,10 +964,10 @@ fn applyInitText(p: *Pardes, text: []const u8, settings_only: bool) void { // on the message row and an err in the log, with its line. var why_buf: [320]u8 = undefined; const why: []const u8 = if (!known) - std.fmt.bufPrint(&why_buf, "unknown word \"{s}\"", .{word[0..@min(word.len, 64)]}) catch "unknown word" + std.fmt.bufPrint(&why_buf, "unknown word \"{s}\"", .{word[0..@min(word.len, 64)]}) catch unreachable // 64 fit 320 else if (p.fs.said_len > 0) p.fs.said[0..p.fs.said_len] else "failed"; var said: [400]u8 = undefined; - const line_said = std.fmt.bufPrint(&said, "init file line {d}: {s}", .{ number, why }) catch "init file: a line failed"; + const line_said = std.fmt.bufPrint(&said, "init file line {d}: {s}", .{ number, why }) catch unreachable; // why is 320 at most if (!known) p.reportFailure(p.view().active, line_said); pardes.ctlfs.events.notePath(p, "err -", line_said); } @@ -983,7 +984,7 @@ pub fn runBuiltinFrom(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ? // word a build lacks is, and never run. if (!builtins.registry.here(p, b)) { var said: [96]u8 = undefined; - return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is GUI-only, invalid here", .{@tagName(b)}) catch "GUI-only, invalid here"); + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is GUI-only, invalid here", .{@tagName(b)}) catch unreachable); // a builtin's name is 15 at most } // No pane at all (the window Delcol emptied): a word runs all the same, // with a stand-in that is no pane (New, Newcol, Exit are what make sense). @@ -1002,7 +1003,7 @@ pub fn runBuiltinFrom(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ? // silent nothing (Palette on a file, Filter on a PDF). if (builtins.registry.needs(b)) |kind| if (p.panes[id] == null or !kind.has(pane)) { var said: [96]u8 = undefined; - return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: only {s} pane takes it", .{ @tagName(b), kind.noun() }) catch "only another kind of pane takes it"); + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: only {s} pane takes it", .{ @tagName(b), kind.noun() }) catch unreachable); // short names }; const c: builtins.Ctx = .{ .p = p, .pane = pane, .id = id, .txt = txt, .arg = arg, .from_body = from_body }; builtins.registry.dispatch(b, c); @@ -1023,7 +1024,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[] const room = 128 -| ("Theme: no theme \"\"; like it: ; Themes lists all".len + shown.len); var near: [128]u8 = undefined; var text: [256]u8 = undefined; - return p.reportFailure(v.active, std.fmt.bufPrint(&text, "Theme: no theme \"{s}\"; like it: {s}; Themes lists all", .{ shown, colors.themesNear(near[0..@min(room, near.len)], want) }) catch "Theme: no such theme"); + return p.reportFailure(v.active, std.fmt.bufPrint(&text, "Theme: no theme \"{s}\"; like it: {s}; Themes lists all", .{ shown, colors.themesNear(near[0..@min(room, near.len)], want) }) catch unreachable); // 40 and 128 of names fit 256 }, .font => { if (comptime !font_picker) return; @@ -1042,30 +1043,32 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[] p.settings.shell.pending = true; if (!p.announce) return; var said: [320]u8 = undefined; - return p.setMessage(v.active, std.fmt.bufPrint(&said, "Shell: the default again, {s} ($SHELL, else /bin/sh)", .{config.defaultShell()}) catch "Shell: the default again"); + return p.setMessage(v.active, std.fmt.bufPrint(&said, "Shell: the default again, {s} ($SHELL, else /bin/sh)", .{config.defaultShell()[0..@min(config.defaultShell().len, 256)]}) catch unreachable); // 256 and 53 fit 320 } var why: [320]u8 = undefined; if (pardes.tape.value(.shell, @import("host_io.zig").Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| { var text: [340]u8 = undefined; - return p.reportFailure(v.active, std.fmt.bufPrint(&text, "Shell: {s}", .{refused}) catch "Shell: no such shell"); + return p.reportFailure(v.active, std.fmt.bufPrint(&text, "Shell: {s}", .{refused}) catch unreachable); // refused is at most why's 320 } if (!p.settings.apply(setting, want)) p.reportFailure(v.active, "Shell: takes " ++ comptime config.Runtime.takes(.shell).?); }, // Bare, each says its value, as acme's Tab says the width. inline .tab, .callstack_depth => |_, tag| if (arg == null) { var said: [40]u8 = undefined; - return p.setMessage(v.active, std.fmt.bufPrint(&said, "{s} {d}", .{ setting.word, @field(p.settings, @tagName(tag)) }) catch setting.word); + return p.setMessage(v.active, std.fmt.bufPrint(&said, "{s} {d}", .{ setting.word, @field(p.settings, @tagName(tag)) }) catch unreachable); // a setting's word is 14 at most } else if (!p.settings.apply(setting, arg)) { var said: [96]u8 = undefined; - p.reportFailure(v.active, std.fmt.bufPrint(&said, "{s}{s}: takes {s}", .{ setting.word, if (tag == .tab) ": invalid tab width" else "", comptime config.Runtime.takes(tag).? }) catch setting.word); + p.reportFailure(v.active, std.fmt.bufPrint(&said, "{s}{s}: takes {s}", .{ setting.word, if (tag == .tab) ": invalid tab width" else "", comptime config.Runtime.takes(tag).? }) catch unreachable); // short literals } else if (tag == .tab) { config.tab_now = p.settings.tab; }, // Said with what it takes: its words, its range. else => if (!p.settings.apply(setting, arg)) { - var text: [160]u8 = undefined; - const why = if (config.Runtime.takes(setting.action)) |values| std.fmt.bufPrint(&text, "{s}: takes {s}", .{ setting.word, values }) else std.fmt.bufPrint(&text, "{s}: does not take that value", .{setting.word}); - p.reportFailure(v.active, why catch "does not take that value"); + var buf: [160]u8 = undefined; + var text: std.Io.Writer = .fixed(&buf); + // A list of values too long for the row is cut. + (if (config.Runtime.takes(setting.action)) |values| text.print("{s}: takes {s}", .{ setting.word, values }) else text.print("{s}: does not take that value", .{setting.word})) catch {}; + p.reportFailure(v.active, text.buffered()); }, } if (setting.action == .tab) { @@ -1257,7 +1260,7 @@ pub fn runWord(p: *Pardes, id: usize, w: *const pardes.Kind.Word, line: []const if (w.opens != 0) { const free = p.freeSlot() orelse return p.reportError(id, name, error.NoPaneSlots); var path_buf: [4096]u8 = undefined; - const path = if (arg.len == 0 or std.fs.path.isAbsolute(arg)) arg else std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"), arg }) catch arg; + const path = if (arg.len == 0 or std.Io.Dir.path.isAbsolute(arg)) arg else std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"), arg }) catch arg; const made = (if (path.len > 0) pardes.Kind.openFile(p, free, w.kind, path) else pardes.Kind.open(p, free, w.kind, null, null)) catch |err| return p.reportError(id, name, err); if (made.addon.?.refused.len > 0) { made.vweight = 0; |
