From c8f0d65a69274dc8a072c35a6c13cb07a6770007 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 09:15:57 -0300 Subject: pdf, abi, core: the pointer and count a C struct or a C caller hands over read through one helper, `addon.cSlice` (abi/addon.zig), NULL being none whatever the count: the bridge says none with a NULL (pardes_pdf_search_page with no hit, pardes_pdf_select over no text, pardes_pdf_load_outline of no entries: each OK with a NULL and a count of 0), and Zig slices a NULL C pointer with a safety panic even at length 0; every site in pdf.zig (jobRun's page text and hits, pageText, hitsOf, selectHere, probeAt, follow, the outline's usable, resolve, sections and section, its bytes now bounded by bytes_len), abi/addon.zig's own (its private `span` was that helper for bytes alone; open's bytes), and the core's kind-ABI callbacks (Kind.zig look, say, text, exec, select, found; Addon.zig loadedOf's words), the core taking the addons' module (`addon`) for it; what holds the next site: pdf.zig's test (run by addon-test, which now runs the pdf module's tests) takes every typed pointer field of the bridge's structs from the translated header at comptime and fails on any `.field[` in pdf.zig, naming the line (shown by putting one site back: "pdf.zig:1014: pardes_pdf_text.data read bare"); the test of the change before passes, with every PDF behaviour test (8/8, Debug) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/addon.zig | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'abi') diff --git a/abi/addon.zig b/abi/addon.zig index 1fa2279a..059e016d 100644 --- a/abi/addon.zig +++ b/abi/addon.zig @@ -248,8 +248,14 @@ pub fn loadBytes(s: anytype, bytes: []const u8, version: u32) bool { pub const body_max = 4096; var body_cut = false; -fn span(p: [*c]const u8, n: u64) []const u8 { - return if (p == null) "" else p[0..n]; +/// A C pointer and its count as a slice; NULL is none, whatever the count: +/// C says none with a NULL (a page's search without a hit, a selection +/// over no text, an outline of no entries), and a NULL C pointer sliced +/// is a safety panic even at length 0 (a release build's abort). Every +/// pointer a C struct or a C caller hands with its count is read through +/// it: pdf.zig's test holds the bridge's to it. +pub fn cSlice(ptr: anytype, n: usize) []const std.meta.Child(@TypeOf(ptr)) { + return if (ptr == null) &.{} else ptr[0..n]; } /// `pardes_addon` exported, when built as a library alone: the kind is its @@ -282,7 +288,7 @@ pub fn ops(comptime T: type) c.pa_ops { fn open(path: [*c]const u8, bytes: [*c]const u8, len: u64) callconv(.c) ?*anyopaque { const s = arena.create(T) catch return null; s.* = std.mem.zeroInit(T, .{}); - if (@hasDecl(T, "open")) T.open(s, if (path == null) null else std.mem.span(path), if (bytes == null) null else bytes[0..len]); + if (@hasDecl(T, "open")) T.open(s, if (path == null) null else std.mem.span(path), if (bytes == null) null else cSlice(bytes, len)); return s; } fn close(st: ?*anyopaque) callconv(.c) void { @@ -329,7 +335,7 @@ pub fn ops(comptime T: type) c.pa_ops { return @intCast(from.len); } fn address(st: ?*anyopaque, t: [*c]const u8, n: u32) callconv(.c) c_int { - return @intFromBool(T.address(self(st) orelse return 0, span(t, n))); + return @intFromBool(T.address(self(st) orelse return 0, cSlice(t, n))); } fn where(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 { return write(T.where, self(st) orelse return 0, buf, n); @@ -337,26 +343,26 @@ pub fn ops(comptime T: type) c.pa_ops { fn ctl(st: ?*anyopaque, line: [*c]const u8, n: u32, reply: [*c]u8, cap: u32) callconv(.c) c_int { var w: std.Io.Writer = .fixed(reply[0..cap -| 1]); defer reply[w.end] = 0; - return if (T.ctl(self(st) orelse return 1, span(line, n), &w)) c.PA_OK else 1; + return if (T.ctl(self(st) orelse return 1, cSlice(line, n), &w)) c.PA_OK else 1; } fn ctl_read(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 { return write(T.ctlRead, self(st) orelse return 0, buf, n); } fn job_run(in: [*c]const u8, n: u32, out: [*c]c.pa_out) callconv(.c) u32 { const gpa: Allocator = .{ .ptr = out, .vtable = &.{ .alloc = jobAlloc, .resize = Allocator.noResize, .remap = Allocator.noRemap, .free = Allocator.noFree } }; - return @intCast((T.jobRun(span(in, n), gpa) orelse return 0).len); + return @intCast((T.jobRun(cSlice(in, n), gpa) orelse return 0).len); } fn job_done(st: ?*anyopaque, token: u32, out: [*c]const u8, n: u32) callconv(.c) void { - T.jobDone(self(st) orelse return, token, span(out, n)); + T.jobDone(self(st) orelse return, token, cSlice(out, n)); } fn dump(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 { return @intCast(T.dump(self(st) orelse return 0, buf[0..n])); } fn load(st: ?*anyopaque, buf: [*c]const u8, n: u32, version: u32) callconv(.c) c_int { - return @intFromBool(!T.load(self(st) orelse return 1, span(buf, n), version)); + return @intFromBool(!T.load(self(st) orelse return 1, cSlice(buf, n), version)); } fn search(st: ?*anyopaque, pat: [*c]const u8, n: u32) callconv(.c) void { - T.search(self(st) orelse return, span(pat, n)); + T.search(self(st) orelse return, cSlice(pat, n)); } }; if (T.head.name.len > 15) @compileError(@typeName(T) ++ "'s name " ++ T.head.name ++ " is over 15 bytes"); -- cgit v1.3 From 9a648ef09bb32f42fb6b09bebbe8c851be360c6e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 05:59:04 -0300 Subject: pty/run at a terminal's start answers the line it ran: a window session's first run (test/daemon.py `gui_upgrades`' `echo $$`, twice this week a ValueError on its second line, `b''`) answered `error not run` while its command ran, and a run written just after a new terminal's greeting is typed answered `exit 0` with the greeting's `ls` output; (a) every window session's first terminal is sized twice, its shell forked at 78x14 and sized 78x13 by the window's first frame about 20 ms later, at its first prompt, where readline draws the prompt again (`\r\e[K\r` and PS1, marks and all): a run's line sent into the first prompt shows on the redrawn one, `noteMarks` takes that newer prompt with no C for the line fish refuses and keeps (Ctrl-U, `.kept_line`), and the read after it, the line's newline before PS0's C (bash writes them apart), leaves the cursor on a fresh row in the input which `prompt()` called an empty prompt: `error not run`, the command's C a read later (the ponytail at pty.zig's refused branch named it; the same under an adoption's two sizes, which test/daemon.py `adopted_pid` waited around since ymvuluxr, then ruled a run "told plainly that it did not run": it had run); now a row begun by a newline in the input, with no prompt on it, under a typed line, is not an empty prompt (core/term/terminal.zig `prompt()`, abi/term.zig's `input_empty`): the line was taken and the run waits for its C; (b) the greeting `ls\r` is typed at a new terminal's first prompt (Core.zig's layout), and until its echo the prompt looks empty: a run sent then is typed behind it and its want (started+1) is the `ls`'s C; now the pane keeps that its greeting was typed (`Pane.greeted`, cleared at a shell's start) and until the prompt after it a run waits for that prompt (pty.zig `greeting`: writeRun, noteMarks, pty/status's busy), as for the first, bounded since `ls` always starts (the reference says so); the failing tests first (core/ninep/pty.zig, shell-test): "a prompt redrawn under a run's line, the line taken, is the line run: never `error not run`" (the bytes pty/data read off a failing ReleaseSafe window start; on the base `error not run`) and "a run written just after a new terminal's greeting is typed is not answered with the greeting's output" (on the base `exit 0\nnotes.txt\n`); a user could hit both: a script that starts pardes-gui and runs at once in its shell (a retry on `error not run` runs its command twice), and one that opens a Term and runs in it at once (another command's output for its own, exit 0); the rates (.scratch/sketch/reorg/pardes-first-run, 200 runs a row pinned to CPU 6 and to CPUs 6,7, no made load, loads 1.5-10): every one of 24 rows 200/200, the base and this, Debug and ReleaseSafe, a window session and a terminal's, plain and with a WINCH trap in the shell's .bashrc: the fault is rare on its own (2 of 108 diagnostic ReleaseSafe window starts with that trap; the coordinator's checks twice under load), Debug never reaches it (its first frame comes before bash's first prompt), and its adopted twin comes more often: test/daemon.py `--relink` with adopted_pid's wait taken out, the installed build 18 of 20 (two `error not run` on the adopted shell), this tree's ReleaseSafe 20 of 20; the gates (with the harness change after it): tui Debug core-test 924/1090 (166 skipped: the two new tests need a terminal), shell-test 1132/1135 (3 skipped), process-test 0 of 146 (loads 3.0-6.2); test/daemon.py on ReleaseSafe: plain 1/1, `--upgrade` from the installed build (wire 31) 1/1, `--upgrade --gui` from the installed build 5/5 (loads 1.1-5.8); left: the Ctrl-U a redraw that shows the line still sends (it reaches bash after the line's Enter: a bell at the next prompt), `busy` for a run written into a prompt being drawn (its A read, its B not yet), and a line a user types, or `Term CMD`'s queued line, in the instant before its echo Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/term.zig | 4 ++- core/Core.zig | 2 ++ core/ninep/pty.zig | 81 +++++++++++++++++++++++++++++++++++++++++++++----- core/panes.zig | 4 +++ core/term/terminal.zig | 12 +++++++- docs/typ/reference.typ | 3 +- 6 files changed, 96 insertions(+), 10 deletions(-) (limited to 'abi') diff --git a/abi/term.zig b/abi/term.zig index 31f155e8..e84d6998 100644 --- a/abi/term.zig +++ b/abi/term.zig @@ -78,7 +78,9 @@ pub const Prompt = struct { finished: ?Finished = null, /// The cursor is at a prompt's input, on either screen. input_ready: bool = false, - /// ...and nothing is typed on its line up to the cursor. + /// ...and nothing is typed on its line up to the cursor, nor on the + /// line above when a newline in the input began this one (a line taken + /// whose command has not started yet). input_empty: bool = false, /// The cursor is on a prompt's row or at its input, on the primary /// screen. diff --git a/core/Core.zig b/core/Core.zig index 2c6567ed..f5ed28bd 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1070,6 +1070,7 @@ fn acknowledgeShellInner(p: *Pardes, id: Pane.Id, executable: []const u8, prompt if (id.index() < MAX_PANES) if (p.panes[id.index()]) |pane| { ctlfs.pty.shellGone(p, pane, true); // a respawn replaced whatever ran pane.shell_failed = false; + pane.greeted = false; pane.had_shell = true; pane.fs.unmarked = !prompt_marks; // The tag's Term word names the shell the host ran, and a click @@ -2010,6 +2011,7 @@ pub fn sync(p: *Pardes) void { p.emit(.{ .resize_pty = .{ .pane = .at(id), .cols = pane.cols, .rows = pane.rows } }); p.emitWrite(id, "ls\r"); pane.greet = false; + pane.greeted = true; }, }; } diff --git a/core/ninep/pty.zig b/core/ninep/pty.zig index 43973491..2dd7f7f3 100644 --- a/core/ninep/pty.zig +++ b/core/ninep/pty.zig @@ -135,11 +135,12 @@ pub const status_len: u64 = 36; pub fn readStatus(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply { // Busy is what a run written now would be told: a command is running - // or a line is typed at the prompt. Before the shell's first prompt a - // run waits for it rather than answer busy, so that is not busy. + // or a line is typed at the prompt. Before the shell's first prompt, or + // the one after its greeting, a run waits for it rather than answer + // busy, so that is not busy. const marks = pardes.Term.prompt(pane.terminal); const busy = p.hostTtyTaken(id) or (!pane.fs.unmarked and (waitingRun(p, pane) != null or - (marks.prompts > 0 and (marks.phase != .input or !marks.input_empty)))); + (marks.prompts > 0 and !greeting(pane, marks) and (marks.phase != .input or !marks.input_empty)))); const out = p.fs.stage(p.gpa); const size = winsize(pane); out.print(p.gpa, "{d:>11} {d:>11} {d:>11}\n", .{ size[0], size[1], @intFromBool(busy) }) catch return Reply.fail(req.tag, E.NOMEM); @@ -230,8 +231,8 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply { answer(p, slot, "error {s}", .{if (pane.command_done) "command done; not a shell" else "a command runs here, not a shell"}); } else if (pf.unmarked) { answer(p, slot, "error no prompt marks", .{}); - } else if (waitingRun(p, pane) != null or (marks.prompts > 0 and (marks.phase != .input or - !marks.input_empty or p.hostTtyTaken(id)))) + } else if (waitingRun(p, pane) != null or (marks.prompts > 0 and !greeting(pane, marks) and + (marks.phase != .input or !marks.input_empty or p.hostTtyTaken(id)))) { // Something is running or someone has typed at the prompt: sending // now would type into the middle of it. The phase is pardes's own @@ -248,7 +249,8 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply { answer(p, slot, "busy", .{}); } else { // Sent now if the shell is at its prompt, or at its first prompt - // when it has not drawn one yet (noteMarks). + // when it has not drawn one yet, or the one after its greeting + // (noteMarks). slot.line = p.gpa.dupe(u8, line) catch return Reply.fail(req.tag, E.NOMEM); slot.phase = .waiting; pf.run = req.handle; @@ -296,7 +298,7 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void { pf.run = null; return; } - if (marks.phase != .input or !marks.input_empty or p.hostTtyTaken(id)) return; + if (greeting(pane, marks) or marks.phase != .input or !marks.input_empty or p.hostTtyTaken(id)) return; slot.want = marks.started +% 1; slot.prompts = marks.prompts; slot.declined = marks.declined; @@ -361,6 +363,15 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void { finish(p, slot, pane, done.status); } +/// The greeting typed at a new terminal's first prompt (Pane.greeted) is +/// its shell's first command until the prompt after it: its echo not back +/// yet, the prompt looks empty, and a line sent then would be typed behind +/// the `ls`, its start taken for the run's (the run answered with what ls +/// printed). `ls` always starts, so the wait ends. +fn greeting(pane: *const Pane, marks: pardes.Term.Prompt) bool { + return pane.greeted and (marks.started == 0 or (marks.started == 1 and marks.phase != .input)); +} + /// The shell's marks as the session's emulator has them (taped), as a /// run's step reads them: a screen-state replica's emulator is the last /// delta's, which comes at a frame and can be a unit behind the one it @@ -785,6 +796,62 @@ test "a run waiting for a new terminal's first prompt is the only line typed the try testing.expectEqualStrings("true\r", sent[0..len]); } +test "a prompt redrawn under a run's line, the line taken, is the line run: never `error not run`" { + const gpa = testing.allocator; + const p = try withTerm(gpa); + defer p.deinit(); + const node = Node.of(serialOf(p), .pty_run); + const out = struct { + fn put(pp: *Pardes, bytes: []const u8) void { + pp.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); + while (pp.nextEffect()) |_| {} + } + }; + const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + // A window's first terminal: bash's first prompt, then the window's + // first size (its rows a row shorter), which readline answers by + // drawing its prompt again: with the run's line on it when it had read + // it, its Enter not yet. Then the line taken: its newline, and in the + // next read PS0's C (bash writes them apart): bytes as pty/data read + // them off a ReleaseSafe window session's start. + out.put(p, "\x1b]133;D;0;aid=pardes\x07" ++ prompt); + const h = call(p, .{ .tag = 1, .op = .open, .node = node }).reply.handle; + try testing.expectEqualStrings("echo $$\r", call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "echo $$\n" }).pty()); + out.put(p, "\r\x1b[K\r" ++ prompt ++ "echo $$"); + out.put(p, "\r\n\x1b[?2004l\r"); + try testing.expectEqual(Status.again, call(p, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 }).reply.status); + out.put(p, "\x1b]133;C;aid=pardes\x074242\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt); + try testing.expectEqualStrings("exit 0\n4242\n", call(p, .{ .tag = 4, .op = .read, .node = node, .handle = h, .size = 64 }).bytes); +} + +test "a run written just after a new terminal's greeting is typed is not answered with the greeting's output" { + const gpa = testing.allocator; + const p = try withTerm(gpa); + defer p.deinit(); + p.panes[0].?.greet = true; // a Term's, in a window (resized) + p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); + while (p.nextEffect()) |_| {} + const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + // The first prompt: the greeting typed at it, its echo not back yet. + p.update(.{ .output = .{ .pane = .at(0), .bytes = prompt } }); + var greeted: [16]u8 = undefined; + var len: usize = 0; + while (p.nextEffect()) |e| if (e == .write) { + const b = e.write.bytes.slice(); + @memcpy(greeted[len..][0..b.len], b); + len += b.len; + }; + try testing.expectEqualStrings("ls\r", greeted[0..len]); + const node = Node.of(serialOf(p), .pty_run); + const h = call(p, .{ .tag = 1, .op = .open, .node = node }).reply.handle; + _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "true\n" }); + for ([_][]const u8{ "ls\r\n\x1b]133;C;aid=pardes\x07notes.txt\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt, "true\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;0;aid=pardes\x07" ++ prompt }) |bytes| { + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); + while (p.nextEffect()) |_| {} + } + try testing.expectEqualStrings("exit 0\n", call(p, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 }).bytes); +} + test "run answers how its command ended once the shell is back at a prompt" { const gpa = testing.allocator; const p = try withTerm(gpa); diff --git a/core/panes.zig b/core/panes.zig index 5bf6992a..4116c3e8 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -160,6 +160,10 @@ pub const Pane = struct { cols: u16, rows: u16, greet: bool = false, + /// Its greeting was typed (Pardes.layout): that `ls` is its shell's + /// first command, and a run written until the prompt after it waits for + /// that prompt (ninep/pty.zig `greeting`), as for the first. + greeted: bool = false, /// Its shell could not be started (Pardes.shellFailed): a run hears /// `error shell gone` at once. shell_failed: bool = false, diff --git a/core/term/terminal.zig b/core/term/terminal.zig index 45c01841..88eb1443 100644 --- a/core/term/terminal.zig +++ b/core/term/terminal.zig @@ -927,9 +927,19 @@ pub fn prompt(state: ?*State) Term.Prompt { // Nothing further right counts: that is where fish draws a right // prompt, and fish suggests nothing on an empty line. const empty = ready and empty: { + var prompted = false; const row = cursor.page_pin.cells(.all); - for (row[0..@min(cursor.x + 1, row.len)]) |cell| + for (row[0..@min(cursor.x + 1, row.len)]) |cell| { if (cell.semantic_content == .input and !cell.isEmpty()) break :empty false; + prompted = prompted or cell.semantic_content == .prompt; + } + // A row with no prompt on it, begun by a newline in the input: the + // line above was taken, its Enter echoed and the command's C still + // to come (bash writes them apart), so it is typed still. + if (!prompted and cursor.page_row.semantic_prompt == .prompt_continuation) if (cursor.page_pin.up(1)) |above| { + for (above.cells(.all)) |cell| + if (cell.semantic_content == .input and !cell.isEmpty()) break :empty false; + }; break :empty true; }; return .{ diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index ac61ca74..fac705c6 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -914,7 +914,8 @@ Terminal panes also have #file("pty/"): the clear onward). Or: `busy: is running` (bare `busy` when text is typed at the prompt, `busy alternate screen` on the alternate screen with no prompt drawn), `exit ?` (no status reported, not a success), `error not run` (the shell refused the line, e.g. a fish syntax error), `error shell gone`, `error no prompt marks`, and on a command pane `error a command runs here, not a shell` (`error command done; not a shell` once it ended). A line written - before a fresh terminal's first prompt waits for it. One line per run; a + before a fresh terminal's first prompt waits for it, and for the prompt + after the `ls` a new terminal in a directory greets with. One line per run; a second line before the answer is refused. #file("pty/run") relies on the OSC 133 marks pardes injects into bash and -- cgit v1.3