From 66c4095e140694376fd07c3afa0fee721558bdb3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:38:29 -0300 Subject: ninep: one tree.window answers a read with the part of a text its offset and size ask for, where ten places (a frozen listing, a stream's replay, README, /screen, the sources, a PDF's or a terminal's body, a tag, a column's) windowed by hand; /status, the root ctl, /commands and /listeners are each one text in the step's scratch, read and counted from it, so the xLen and readX pairs (statusLen, rootLen, commandsLen, readStatus, readRoot, readCommands, readPane, readListeners) go; tree.read asks openOf once Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/ninep/cols.zig | 5 +---- src/ninep/ctl.zig | 62 ++++++++++++--------------------------------------- src/ninep/pane.zig | 18 ++++++--------- src/ninep/screen.zig | 30 +++++++------------------ src/ninep/sources.zig | 6 +---- src/ninep/tree.zig | 60 ++++++++++++++++++++++--------------------------- 6 files changed, 58 insertions(+), 123 deletions(-) (limited to 'src/ninep') diff --git a/src/ninep/cols.zig b/src/ninep/cols.zig index 833b25c5..1445a3c3 100644 --- a/src/ninep/cols.zig +++ b/src/ninep/cols.zig @@ -60,11 +60,8 @@ pub fn headerText(p: *Pardes, serial: ?u32) ?[]const u8 { pub fn read(p: *Pardes, req: Req, serial: ?u32) Reply { applyTruncation(p, serial); - const text = headerText(p, serial) orelse return Reply.fail(req.tag, E.NOENT); - const out = p.fs.stage(p.gpa); // As a pane tag reads: the text alone, no newline after it. - out.appendSlice(p.gpa, text) catch return Reply.fail(req.tag, E.NOMEM); - return tree.stagedReply(p, req); + return tree.window(p, req, headerText(p, serial) orelse return Reply.fail(req.tag, E.NOENT)); } /// A write appends, as a pane tag's does; the tag is one line, so a newline diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index c3658df4..30ba8bf7 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -374,31 +374,18 @@ pub fn readResults(p: *Pardes, req: Req) Reply { // ---- /status ---- -fn statusText(p: *Pardes, buf: []u8) []const u8 { - var w = std.Io.Writer.fixed(buf); +/// /status's text, in the step's scratch. +pub fn statusText(p: *Pardes) []const u8 { + var w: std.Io.Writer.Allocating = .init(p.scratch.allocator()); var count: usize = 0; for (p.panes) |slot| count += @intFromBool(slot != null); const pid: u32 = if (comptime pardes.hosted) @intCast(std.c.getpid()) else 0; - w.print("pid {d}\nversion {s}\npanes {d}\n", .{ pid, pardes.version, count }) catch {}; + w.writer.print("pid {d}\nversion {s}\npanes {d}\n", .{ pid, pardes.version, count }) catch {}; // Whether the pointer is over this window: what a grip let go past // another window's edge asks of every window, to find the one it was // let go over (Handover.zig), where no window may know another's place. - if (p.windowed) w.print("pointer {s}\n", .{if (p.pointer_inside) "in" else "out"}) catch {}; - return w.buffered(); -} - -pub const status_text_cap = 80 + pardes.version.len; - -pub fn statusLen(p: *Pardes) u64 { - var buf: [status_text_cap]u8 = undefined; - return statusText(p, &buf).len; -} - -pub fn readStatus(p: *Pardes, req: Req) Reply { - var buf: [status_text_cap]u8 = undefined; - const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, statusText(p, &buf)) catch return Reply.fail(req.tag, E.NOMEM); - return tree.stagedReply(p, req); + if (p.windowed) w.writer.print("pointer {s}\n", .{if (p.pointer_inside) "in" else "out"}) catch {}; + return w.written(); } // ---- /pane//ctl ---- @@ -656,18 +643,11 @@ fn writeSettingsFor(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer, cap }); } -pub fn rootLen(p: *Pardes) u64 { - var buf: [256]u8 = undefined; - var counting: std.Io.Writer.Discarding = .init(&buf); - writeSettings(p, &counting.writer) catch return 0; - return counting.fullCount(); -} - -pub fn readRoot(p: *Pardes, req: Req) Reply { +/// The root ctl's text (`writeSettings`), in the step's scratch. +pub fn settingsText(p: *Pardes) ![]const u8 { var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); - writeSettings(p, &text.writer) catch return Reply.fail(req.tag, E.NOMEM); - p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch return Reply.fail(req.tag, E.NOMEM); - return tree.stagedReply(p, req); + try writeSettings(p, &text.writer); + return text.written(); } /// /pager, `pardes -`'s: the directory written (empty, or not absolute: the @@ -919,18 +899,11 @@ fn writeCommands(p: *const Pardes, w: *std.Io.Writer) !void { } } -pub fn commandsLen(p: *const Pardes) u64 { - var buf: [256]u8 = undefined; - var counting: std.Io.Writer.Discarding = .init(&buf); - writeCommands(p, &counting.writer) catch return 0; - return counting.fullCount(); -} - -pub fn readCommands(p: *Pardes, req: Req) Reply { +/// /commands' text (`writeCommands`), in the step's scratch. +pub fn commandsText(p: *Pardes) ![]const u8 { var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); - writeCommands(p, &text.writer) catch return Reply.fail(req.tag, E.NOMEM); - p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch return Reply.fail(req.tag, E.NOMEM); - return tree.stagedReply(p, req); + try writeCommands(p, &text.writer); + return text.written(); } // ---- /focus ---- @@ -1062,13 +1035,6 @@ pub fn writeMousein(p: *Pardes, req: Req) Reply { return .{ .tag = req.tag, .written = @intCast(req.data.len) }; } -pub fn readPane(p: *Pardes, req: Req, pane: *Pane) Reply { - var buf: [pane_text_cap]u8 = undefined; - const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, paneText(p, pane, &buf)) catch return Reply.fail(req.tag, E.NOMEM); - return tree.stagedReply(p, req); -} - /// `get` reloads the buffer from the name it carries, wherever that name /// resolves. Any other line is a builtin that acts on a pane (`Del`, /// `Save f`), run at this one; a session's builtin is the root ctl's. diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index c12b2e80..48095c46 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -413,11 +413,12 @@ pub fn read(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply .body => readBody(p, req, id, pane), .tag => tag: { applyTagTruncation(p, pane); - const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, tagOf(p, pane)) catch {}; - break :tag tree.stagedReply(p, req); + break :tag tree.window(p, req, tagOf(p, pane)); + }, + .ctl => ctl: { + var buf: [ctl.pane_text_cap]u8 = undefined; + break :ctl tree.window(p, req, ctl.paneText(p, pane, &buf)); }, - .ctl => ctl.readPane(p, req, pane), // A failed address reads empty, as an unset limit does: there is // none. data and xdata are what refuse. .addr => readRange(p, req, if (pf.addr_failed) null else addr: { @@ -477,17 +478,12 @@ fn readBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply { } } }; } // A PDF's body is its text layer, the page shown's: read-only. - if (pdfText(p, pane)) |text| { - p.fs.stage(p.gpa).appendSlice(p.gpa, text) catch return Reply.fail(req.tag, E.NOMEM); - return tree.stagedReply(p, req); - } + if (pdfText(p, pane)) |text| return tree.window(p, req, text); if (req.handle != 0 and pane.isTerminal()) return screen.readSnapshot(p, req, pane); const text = panes.terminal.screenTextAlloc(pane, p.gpa) catch return Reply.fail(req.tag, E.NOMEM); defer p.gpa.free(text); - const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, text) catch return Reply.fail(req.tag, E.NOMEM); - return tree.stagedReply(p, req); + return tree.window(p, req, text); } fn readData(req: Req, id: usize, pane: *Pane, pf: *State, stop_at_end: bool) Reply { diff --git a/src/ninep/screen.zig b/src/ninep/screen.zig index 22129546..c0251d80 100644 --- a/src/ninep/screen.zig +++ b/src/ninep/screen.zig @@ -49,32 +49,18 @@ pub fn readSnapshot(p: *Pardes, req: Req, pane: ?*Pane) Reply { const terminal = pane orelse return Reply.fail(req.tag, E.INVAL); snapshot.* = panes.terminal.screenTextAlloc(terminal, p.gpa) catch return Reply.fail(req.tag, E.NOMEM); } - const bytes = snapshot.*.?; - const off = @min(req.off, bytes.len); - const len = @min(bytes.len - off, req.size); - const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, bytes[off..][0..len]) catch return Reply.fail(req.tag, E.NOMEM); - return .{ .tag = req.tag, .payload = .{ .staged = @intCast(len) } }; + return tree.window(p, req, snapshot.*.?); } -pub const listeners_cap = 512; - -pub fn listenersText(p: *Pardes, buf: []u8) []const u8 { - var text = std.Io.Writer.fixed(buf); - if (p.fs.socket_path.len != 0) - text.print("unix!{s}\n", .{p.fs.socket_path}) catch return text.buffered(); +/// /listeners' text, a dial a line, in the step's scratch. +pub fn listenersText(p: *Pardes) []const u8 { + var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); + if (p.fs.socket_path.len != 0) text.writer.print("unix!{s}\n", .{p.fs.socket_path}) catch {}; if (p.fs.tcp_address) |address| switch (address) { - .ip4 => |ip| text.print("tcp!{d}.{d}.{d}.{d}!{d}\n", .{ ip.bytes[0], ip.bytes[1], ip.bytes[2], ip.bytes[3], ip.port }) catch {}, - .ip6 => |ip| text.print("tcp!{f}!{d}\n", .{ std.Io.net.Ip6Address.Unresolved{ .bytes = ip.bytes, .interface_name = null }, ip.port }) catch {}, + .ip4 => |ip| text.writer.print("tcp!{d}.{d}.{d}.{d}!{d}\n", .{ ip.bytes[0], ip.bytes[1], ip.bytes[2], ip.bytes[3], ip.port }) catch {}, + .ip6 => |ip| text.writer.print("tcp!{f}!{d}\n", .{ std.Io.net.Ip6Address.Unresolved{ .bytes = ip.bytes, .interface_name = null }, ip.port }) catch {}, }; - return text.buffered(); -} - -pub fn readListeners(p: *Pardes, req: Req) Reply { - var buf: [listeners_cap]u8 = undefined; - const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, listenersText(p, &buf)) catch return Reply.fail(req.tag, E.NOMEM); - return tree.stagedReply(p, req); + return text.written(); } // ---- tests ---- diff --git a/src/ninep/sources.zig b/src/ninep/sources.zig index b0408594..846ab758 100644 --- a/src/ninep/sources.zig +++ b/src/ninep/sources.zig @@ -102,11 +102,7 @@ pub fn handle(p: *Pardes, req: Req) Reply { }, .read => { if (found.dir) return Reply.fail(req.tag, E.PERM); - const off: usize = @intCast(@min(req.off, found.contents.len)); - const bytes = found.contents[off..][0..@min(req.size, found.contents.len - off)]; - const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, bytes) catch return Reply.fail(req.tag, E.NOMEM); - return .{ .tag = req.tag, .payload = .{ .staged = @intCast(bytes.len) } }; + return tree.window(p, req, found.contents); }, else => return Reply.fail(req.tag, E.PERM), } diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index 24de3e76..a5444caa 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -473,7 +473,6 @@ pub fn stageDirent(out: *std.ArrayList(u8), gpa: std.mem.Allocator, node: u64, d out.appendSlice(gpa, name) catch return; } -/// Answers a read from whatever the handler staged, windowed by offset and size. /// The daemon's registry (/sessions), its `list` answer as it gave it. fn daemonList(gpa: std.mem.Allocator) anyerror![]u8 { var buf: [4096]u8 = undefined; @@ -481,6 +480,7 @@ fn daemonList(gpa: std.mem.Allocator) anyerror![]u8 { return gpa.dupe(u8, said); } +/// Answers a read from whatever the handler staged, windowed by offset and size. pub fn stagedReply(p: *Pardes, req: Req) Reply { const out = &p.fs.out; const off = @min(req.off, out.items.len); @@ -490,6 +490,14 @@ pub fn stagedReply(p: *Pardes, req: Req) Reply { return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } }; } +/// Answers a read with the window of `bytes` its offset and size ask for. +pub fn window(p: *Pardes, req: Req, bytes: []const u8) Reply { + const off = @min(req.off, bytes.len); + const n = @min(bytes.len - off, req.size); + p.fs.stage(p.gpa).appendSlice(p.gpa, bytes[off..][0..n]) catch return Reply.fail(req.tag, E.NOMEM); + return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } }; +} + // ---- dispatch ---- pub fn handle(p: *Pardes, req: Req) Reply { @@ -690,13 +698,13 @@ fn topSize(p: *Pardes, f: TopFile) u64 { .root, .pane, .col, .screen, .new, .layout, .index, .recent, .listeners, .sessions, .kbdin, .mousein => 0, .tag => cols.headerText(p, null).?.len, .README => fs.help.len, - .status => ctl.statusLen(p), + .status => ctl.statusText(p).len, .look, .exec, .tagexec, .pager => ctl.resultsLen(p), // A stream: a length would read as its end to a kernel that caches // one (a 9ns mount's splice), and a follower's reads go past it. .log => 0, - .ctl => ctl.rootLen(p), - .commands => ctl.commandsLen(p), + .ctl => if (ctl.settingsText(p)) |text| text.len else |_| 0, + .commands => if (ctl.commandsText(p)) |text| text.len else |_| 0, .focus => focus: { if (p.header_focus) break :focus 0; const pn = p.panes[p.active] orelse break :focus 0; @@ -1060,17 +1068,12 @@ fn setattr(p: *Pardes, req: Req, target: Target) Reply { } fn read(p: *Pardes, req: Req, target: Target) Reply { + const open_record = openOf(p, req); // A /pager open's text goes in before its read answers the pane. - if (openOf(p, req)) |o| if (o.what == .pager) ctl.fillPager(p, o.what.pager, &o.pending) catch return Reply.fail(req.tag, E.NOMEM); + if (open_record) |o| if (o.what == .pager) ctl.fillPager(p, o.what.pager, &o.pending) catch return Reply.fail(req.tag, E.NOMEM); // A listing reads what it was at the open. - if (openOf(p, req)) |o| if (o.what == .frozen) { - const bytes = o.what.frozen; - const off = @min(req.off, bytes.len); - const n = @min(bytes.len - off, req.size); - p.fs.stage(p.gpa).appendSlice(p.gpa, bytes[off..][0..n]) catch return Reply.fail(req.tag, E.NOMEM); - return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } }; - }; - const stream = if (openOf(p, req)) |o| switch (o.what) { + if (open_record) |o| if (o.what == .frozen) return window(p, req, o.what.frozen); + const stream = if (open_record) |o| switch (o.what) { .log => o, .event => |reader| if (reader) o else null, .pty_data => o, @@ -1080,10 +1083,9 @@ fn read(p: *Pardes, req: Req, target: Target) Reply { // Strictly inside: a client that reads every record at one offset (the // offset is ignored on a stream, as in Plan 9) always gets new ones. if (s.replay) |r| if (req.off > r.off and req.off < r.off + r.bytes.len) { - const from = r.bytes[@intCast(req.off - r.off)..]; - const n = @min(from.len, req.size); - p.fs.stage(p.gpa).appendSlice(p.gpa, from[0..n]) catch return Reply.fail(req.tag, E.NOMEM); - return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } }; + var at = req; + at.off = req.off - r.off; + return window(p, at, r.bytes); }; const reply = readFile(p, req, target); if (reply.status != .ok) return reply; @@ -1099,12 +1101,7 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply { switch (target) { .top => |f| return switch (f) { .kbdin, .mousein => Reply.fail(req.tag, E.PERM), - .README => help: { - const off = @min(req.off, fs.help.len); - const bytes = fs.help[off..][0..@min(req.size, fs.help.len - off)]; - p.fs.stage(p.gpa).appendSlice(p.gpa, bytes) catch break :help Reply.fail(req.tag, E.NOMEM); - break :help .{ .tag = req.tag, .payload = .{ .staged = @intCast(bytes.len) } }; - }, + .README => window(p, req, fs.help), .index => index: { const out = p.fs.stage(p.gpa); var last: u32 = 0; @@ -1120,11 +1117,11 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply { } break :index stagedReply(p, req); }, - .status => ctl.readStatus(p, req), + .status => window(p, req, ctl.statusText(p)), .look, .exec, .tagexec, .pager => ctl.readResults(p, req), .log => events.readLog(p, req), .screen => screen.readSnapshot(p, req, null), - .listeners => screen.readListeners(p, req), + .listeners => window(p, req, screen.listenersText(p)), .sessions => sessions: { if (comptime !(pardes.hosted and daemon.supported)) break :sessions stagedReply(p, req); // Out of the turn: the daemon answers within 2 s, or not at all. @@ -1133,12 +1130,11 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply { else => failText(req.tag, E.NOENT, "no pardes daemon answers"), }; defer p.gpa.free(said); - p.fs.stage(p.gpa).appendSlice(p.gpa, said) catch break :sessions Reply.fail(req.tag, E.NOMEM); - break :sessions stagedReply(p, req); + break :sessions window(p, req, said); }, .focus => ctl.readFocus(p, req), - .ctl => ctl.readRoot(p, req), - .commands => ctl.readCommands(p, req), + .ctl => window(p, req, ctl.settingsText(p) catch return Reply.fail(req.tag, E.NOMEM)), + .commands => window(p, req, ctl.commandsText(p) catch return Reply.fail(req.tag, E.NOMEM)), .recent => recent: { // Every window's files: what the others wrote since, read, // unless a step is out mid-syscall (nothing may change the @@ -1146,14 +1142,12 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply { if (pardes.turn.out == 0) p.store.refresh(p); var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); pardes.panes.Output.recentText(p, &text.writer, false) catch break :recent Reply.fail(req.tag, E.NOMEM); - p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch break :recent Reply.fail(req.tag, E.NOMEM); - break :recent stagedReply(p, req); + break :recent window(p, req, text.written()); }, .layout => layout: { var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); cols.writeLayout(p, &text.writer) catch break :layout Reply.fail(req.tag, E.NOMEM); - p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch break :layout Reply.fail(req.tag, E.NOMEM); - break :layout stagedReply(p, req); + break :layout window(p, req, text.written()); }, .tag => cols.read(p, req, null), // The serial the open handed this fid, so that two reads of one -- cgit v1.3