From 99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 22:03:14 -0300 Subject: core: src/ becomes core/, pardes.zig its root.zig (the reorganisation's tree, step 2d), and every mention of a src/ path follows: the build's paths, the core's imports of its root, the tests that read the repository's files by path (lspbench's, look.zig's spans over core/look.zig), the comments that cite files, the documents and the manual (the typst corpus reads core/tutor.txt and core/plumbing); left as they were: the /src tree's served names (src/pardes.zig and the rest are the 9P tree's, not the repository's), the CHANGELOG's history, fixtures that are only strings; fs-unit-test runs from the core's root (a module of the core rooted at fs.zig had the runtime's and the devices' `core` be fs.zig); nothing else changes: the ledger (pardes.test.* now root.test.*), frames byte-identical, replicas and process-test the base's Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/ninep/tree.zig | 2273 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 2273 insertions(+) create mode 100644 core/ninep/tree.zig (limited to 'core/ninep/tree.zig') diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig new file mode 100644 index 00000000..d13a1b19 --- /dev/null +++ b/core/ninep/tree.zig @@ -0,0 +1,2273 @@ +//! The control tree served over 9P: its nodes, lookup, readdir and the +//! dispatch that answers the engine (cloud9.fs.Server, configured in +//! runtime/9p.zig). The backend contract (Req, Reply, Op, Status, E) is cloud9's, +//! extended here with the editor's reply payload; fs.zig keeps host access +//! and mounts. +//! +//! /README /index /status /look /exec /log /screen /listeners /sessions +//! /pane/new an open makes a pane, a read names it +//! /pane/ rmdir closes that pane +//! /pane//{name,body,tag,ctl,addr,dot,limit,data,xdata,sel, +//! dirty,mark,scroll,errors,event,look,exec,pty/} +//! /os/... the host filesystem /src/... embedded sources (opt-in) +const std = @import("std"); +const cloud9 = @import("cloud9"); +const pardes = @import("../root.zig"); +const fs = @import("../fs.zig"); +pub const events = @import("events.zig"); +pub const pane = @import("pane.zig"); +pub const addr = @import("addr.zig"); +pub const ctl = @import("ctl.zig"); +pub const cols = @import("cols.zig"); +const layout = pardes.layout; +pub const pty = @import("pty.zig"); +pub const sources = @import("sources.zig"); +pub const screen = @import("screen.zig"); +const daemon = @import("runtime").daemon; +const panes = @import("../panes.zig"); +const Plumb = @import("../Plumb.zig"); + +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; +const MAX_PANES = pardes.MAX_PANES; + +// ---- the backend contract: cloud9.fs, with the editor's payload locator ---- + +pub const Op = cloud9.fs.Op; +pub const Status = cloud9.fs.Status; +pub const Req = cloud9.fs.Req; +pub const E = cloud9.fs.E; + +/// Where a reply's bytes live. The engine ignores it; `Pardes.fsPayload` +/// resolves it into the slice that travels beside the reply. +pub const Payload = union(enum) { + none, + staged: u32, + region: struct { pane: u8, serial: u32, off: u32, len: u32 }, +}; + +pub const Reply = cloud9.fs.ReplyWith(Payload); + +/// 9P answers a failure with a string, not a number (man 5 error): the errno +/// still travels, for clients that map it, but the string is what a person +/// reads. acme names its refusals the same way, and these are its spellings +/// (editors/acme/xfid.c:19) for the ones this tree shares. +pub fn failText(tag: u64, errno: u16, text: []const u8) Reply { + return .{ .tag = tag, .status = .err, .errno = errno, .ename = text }; +} + +pub const e_bad_ctl = "ill-formed control message"; +pub const e_bad_event = "bad event syntax"; +/// A second open reading a file whose reads consume: rio's word for it +/// (windows/rio/xfid.c:25), which 9ns turns into EBUSY. +pub const e_in_use = "file in use"; +/// A read held on a pane's event or pty/data that the pane closed under. +/// acme says "window shut down" (xfid.c:1005), which 9ns reads as EIO; +/// these words read as ENOENT, what every other file of a gone pane gives. +pub const e_shut_down = "no such pane: its window shut"; + +/// Tremove closes a pane; nothing else in the tree is created or destroyed by +/// the protocol, and wstat stays a truncation. Making a pane is an open of +/// /pane/new, so Tcreate is refused everywhere, as it is in acme +/// (editors/acme/fsys.c, fsyscreate) -- here rather than by the engine, so +/// a create in a pane that has closed says the pane is gone (a shell's `>` +/// to a closed pane's file creates when its lookup fails). +pub const features: cloud9.fs.Features = .{ .remove = true, .create = true }; + +/// Whether a request must wait until no step is out in a syscall +/// (`pardes.turn`): what would change a pane such a step still points into. +/// Making a pane and rendering a screen do not -- every yield sits before +/// the mutation of its own step, so the layout and the surface are whole +/// under it -- and a step out there is never waiting on a write of its own, +/// so what parks here is never the syscall's own request. +pub fn needsQuiet(p: *Pardes, req: Req) bool { + return switch (req.op) { + .write => true, + .setattr => truncates(req), + .release => req.remove, + .read => holdsPage(p, req), + else => false, + }; +} + +/// A setattr that changes something: a truncate of a pane's file or a +/// tag. `> exec` (a shell's truncating open) changes nothing, so it waits +/// on nothing: through a mount it hung, as 9ns answers nothing else on the +/// mount while it is out, and the editor's step may be out reading a pane's +/// file through that mount, the quiet it waited for never coming. +fn truncates(req: Req) bool { + if (!req.truncate) return false; + return switch (Node.target(req.node) orelse return false) { + .pane => true, + .col => |c| c.file == .tag, + .top => |f| f == .tag or f == .rules, + .port => false, + }; +} + +/// `needsQuiet`, plus the open that makes a pane and a close that hands +/// held text on: what costs a frame and a wake of the editor. +pub fn changesPane(p: *Pardes, req: Req) bool { + return needsQuiet(p, req) or switch (req.op) { + .open => req.node == @backingInt(TopFile.new), + .release => holdsLines(p, req) or holdsPage(p, req), + else => false, + }; +} + +pub const out_reserve = 4 * 1024; + +// ---- opens ---- + +/// What one open holds from its open to its release, the way lib9p hangs a +/// server's own state off each fid (`Fid.aux`) and acme keeps it on its Fid +/// (editors/acme/dat.h:373-385). An open that holds something answers its +/// record's index plus one as the handle; one that holds nothing answers 0. +pub const Open = struct { + /// The node it was opened on, which every use checks; 0 is a free record. + node: u64 = 0, + what: union(enum) { + /// /screen's frame from the open, a terminal body's history from its + /// first read. + snapshot: ?[]const u8, + log: events.LogOpen, + run: pty.Run, + /// An open of a pane's event, holding the pane scripted; `true` for + /// the one that reads it. + event: bool, + /// The one open reading a pane's pty/data. + pty_data, + /// An open that may write a pane's ctl, and so hold its lock. + ctl, + /// An open of a file that takes lines of commands (look, exec, a + /// ctl, tagexec): its `pending` and `results`. + lines, + /// An open writing a file pane's body, data or xdata: its writes are + /// one undo step (pane.zig `joined_by`), another open's its own. + writes, + /// A listing (`/index`, `/layout`, `/recent`, `/commands`, `/status`, + /// `/listeners`, a ctl opened to read) frozen at the open, as + /// `/screen` is: reads in several chunks never splice two moments. + frozen: []const u8, + /// A /pager open past its directory line: the +Pager's serial, the + /// text after that line held in `pending` until its read puts it + /// in (ctl.fillPager), or its close hands it to the editor's step. + pager: u32, + /// An open writing plumb/rules or plumb/send: `pending` holds what + /// is not whole yet (rule sets with no blank line after them, a + /// message short of its data). + plumb, + /// An open reading a plumb port (Plumb.zig's ports, plumber(4)'s): + /// the messages sent to it since the open, packed, a read each. + port: struct { index: u8, queue: events.Queue = .{} }, + } = .{ .snapshot = null }, + /// What a write to a file of command lines left unfinished: a last line + /// with no newline yet, or an Edit block still open. A write through a + /// mount is cut at the 9P message size, so one `echo` of many lines may + /// come as several writes, cut anywhere; the rest waits for the open's + /// next write, or its release, and never runs as a fragment. + pending: std.ArrayList(u8) = .empty, + /// The panes this open's last write touched, which a read of look or + /// exec on it answers, as /net/tcp/clone answers on the open that + /// asked; a fresh open holds the session's last, from before it opened. + results: Results = .{}, + /// A read that found nothing yet (`.again`), kept to be answered when + /// this open's file has something, the way factotum keeps its log's + /// waiting reads and answers them on append (security/auth/factotum/ + /// log.c:4-52) and acme an event read (editors/acme/xfid.c:994). One at a + /// time, as acme's window keeps one `eventx`: a newer read takes the + /// place of the older. The listener fills it in and answers it + /// (runtime/9p_io.zig, `answerHeld`). + held: ?Held = null, + /// The last answer a stream open (a followed log, event, pty/data) gave, + /// and the offset it was read at. A read at an offset inside it is + /// answered from it again: bash's `read` takes a chunk, keeps a line and + /// lseeks back to just past it, and a stream has already moved on. + replay: ?Replay = null, + /// A `name` open has named its pane: one name an open, so a second line + /// on it (bash writes `printf 'a\nb\n'` a line at a time) is refused. + named: bool = false, + /// A line over 1 MiB was refused: what follows of it, through its + /// newline, is dropped rather than run as a line of its own. + discarding: bool = false, + + pub const Replay = struct { off: u64, bytes: []u8 }; + + pub const Results = struct { + list: [pardes.MAX_PANES]u32 = undefined, + len: u8 = 0, + /// Written on: reads are a stream from the write on, whatever + /// offset they come at, since a shell's `exec 3<>look` shares one + /// offset between the write and the read (as pty/run does). + wrote: bool = false, + read: usize = 0, + }; + + /// `asker` is the connection the read came on, which only the listener + /// knows. + pub const Held = struct { asker: *anyopaque, req: Req, ticket: cloud9.fs.Ticket }; + + pub fn deinit(o: *Open, gpa: std.mem.Allocator) void { + switch (o.what) { + .snapshot => |bytes| if (bytes) |b| gpa.free(b), + .log => |log| gpa.free(log.bytes), + .run => |run| { + gpa.free(run.output); + gpa.free(run.line); + }, + .frozen => |bytes| gpa.free(bytes), + .port => |*port| port.queue.deinit(gpa), + .event, .pty_data, .ctl, .lines, .writes, .pager, .plumb => {}, + } + o.pending.deinit(gpa); + if (o.replay) |r| gpa.free(r.bytes); + o.* = .{}; + } +}; + +/// Records for 64 opens at once; one more that would hold something fails. +pub const open_slots = 64; + +/// The record a request's handle names, when it was opened on that node. +pub fn openOf(p: *Pardes, req: Req) ?*Open { + if (req.handle == 0 or req.handle > p.fs.opens.len) return null; + const o = &p.fs.opens[req.handle - 1]; + return if (o.node == req.node) o else null; +} + +// ---- nodes ---- + +pub const TopFile = enum(u5) { + root = 1, + README, + index, + status, + look, + exec, + log, + screen, + listeners, + pane, + new, + focus, + ctl, + commands, + layout, + tag, + col, + tagexec, + /// The files opened lately, closed ones too, a line each: + /// `open|closed `, most recent first. + recent, + /// `pardes -`'s: a directory written, a new +Pager made there; a + /// read of the same open answers its serial. + pager, + /// The pardes daemon's sessions and shells, as `pardes --ps` lists + /// them: `daemon `, `session tty|gui attached|detached + /// `, `shell ` (the session above's), `orphan `. + sessions, + /// rio's kbdin (windows/rio/xfid.c:515): each rune written is a key + /// typed at the pane with the keyboard, done before the write answers. + kbdin, + /// devmouse's mousein: `m col row buttons`, the mouse as it now is. + mousein, + /// The dumps the state file keeps (core/Db.zig), a line each, the newest + /// first: `