summaryrefslogtreecommitdiff
path: root/src/ninep/ctl.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
committerGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
commita479792a1ab2b593642d7f648194bfa23ce01dba (patch)
tree96ddee7d879a9084daa07db43b0b7910b072a785 /src/ninep/ctl.zig
parente179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff)
parent4c761893e5b3fdae422622cfa02104ab36633045 (diff)
downloadpardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz
pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src/ninep/ctl.zig')
-rw-r--r--src/ninep/ctl.zig118
1 files changed, 38 insertions, 80 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index d0d9b0b4..2859304a 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -524,10 +524,12 @@ fn checkBuiltin(p: *Pardes, req: Req, line: []const u8, scope: builtins.Scope) ?
break now;
} else typed;
const arg = if (sp) |s| std.mem.trim(u8, line[s + 1 ..], " \t") else "";
- const b = std.meta.stringToEnum(Builtin, word) orelse {
+ const here = if (std.meta.stringToEnum(Builtin, word)) |b| builtins.registry.here(p, b) else false;
+ const b = (if (here) std.meta.stringToEnum(Builtin, word) else null) orelse {
// A setting this build's frontend cannot show (Lift, GripWidth on a
- // terminal) is known, and says so rather than sound misspelt.
- if (builtins.absentWhy(word) orelse if (config.Runtime.find(word) != null) @as([]const u8, "GUI-only") else null) |absent| {
+ // terminal, the window's own words in a terminal's view) is known,
+ // and says so rather than sound misspelt.
+ if (builtins.absentWhy(word) orelse if (config.Runtime.find(word) != null or std.meta.stringToEnum(Builtin, word) != null) @as([]const u8, "GUI-only") else null) |absent| {
var why: [64]u8 = undefined;
// EINVAL, a request this build cannot take, in words 9ns reads
// as that: `invalid`.
@@ -618,18 +620,24 @@ const e_prompt = "control message needs its argument";
/// What is not a setting -- platform, themes, where the config came from --
/// is /status's or the DumpConfig window's.
fn writeSettings(p: *Pardes, w: *std.Io.Writer) !void {
- try writeSettingsOf(p, &p.settings, w);
+ try writeSettingsFor(p, &p.settings, w, builtins.viewCapabilities(p));
try w.writeAll("LocationsConfig ");
try p.locations_config.write(w);
try w.writeByte('\n');
- if (comptime builtins.Pet.enabled) try w.print("Pet {s}\n", .{@tagName(p.settings.pet)});
+ if (comptime builtins.Pet.enabled) if (builtins.registry.here(p, .Pet)) try w.print("Pet {s}\n", .{@tagName(p.settings.pet)});
}
/// The setting words of `s`, one a line, as the root ctl reads them; a Dump
/// keeps those that differ from a fresh session's (dump.zig).
pub fn writeSettingsOf(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer) !void {
- inline for (config.Runtime.settings) |setting| {
+ return writeSettingsFor(p, s, w, builtins.capabilities);
+}
+
+/// ...those a frontend of `caps` takes (the root ctl's: the live view's).
+fn writeSettingsFor(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer, caps: config.Runtime.Capabilities) !void {
+ inline for (config.Runtime.settings) |setting| skip: {
if (comptime !setting.enabled(builtins.capabilities)) continue;
+ if (!setting.enabled(caps)) break :skip;
const word = setting.word;
switch (setting.action) {
.toggle => |field| try w.print("{s} {s}\n", .{ word, if (@field(s, @tagName(field))) "on" else "off" }),
@@ -909,8 +917,10 @@ fn stripCitations(text: []u8) []u8 {
return text[0..out];
}
-fn writeCommands(w: *std.Io.Writer) !void {
+fn writeCommands(p: *const Pardes, w: *std.Io.Writer) !void {
for (std.enums.values(Builtin)) |b| {
+ // A window's own word is none in a terminal's view.
+ if (!builtins.registry.here(p, b)) continue;
// Edit is a pane's word the root's ctl takes too, at the active pane.
const scope: []const u8 = if (b == .Edit) "both" else if (builtins.registry.scope(b) == .session) "root" else "pane";
try w.print("{s}{s} {s}", .{
@@ -938,16 +948,16 @@ fn writeCommands(w: *std.Io.Writer) !void {
}
}
-pub fn commandsLen() u64 {
+pub fn commandsLen(p: *const Pardes) u64 {
var buf: [256]u8 = undefined;
var counting: std.Io.Writer.Discarding = .init(&buf);
- writeCommands(&counting.writer) catch return 0;
+ writeCommands(p, &counting.writer) catch return 0;
return counting.fullCount();
}
pub fn readCommands(p: *Pardes, req: Req) Reply {
var text: std.Io.Writer.Allocating = .init(p.scratch.allocator());
- writeCommands(&text.writer) catch return Reply.fail(req.tag, E.NOMEM);
+ 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);
}
@@ -1362,7 +1372,9 @@ test "commands lists every builtin with its argument and the ctl that takes it"
// Then, where the source has one, ` -- ` and what it does.
if (words.next()) |dashes| try testing.expectEqualStrings("--", dashes);
}
- try testing.expectEqual(std.enums.values(Builtin).len, lines);
+ var here: usize = 0;
+ for (std.enums.values(Builtin)) |b| here += @intFromBool(builtins.registry.here(p, b));
+ try testing.expectEqual(here, lines);
for ([_][]const u8{ "\nDel arg pane -- ", "\nNewcol root -- ", "\nSave arg pane", "\nVerbose arg root on,off", "\nPlacement arg root acme,pardes", "\nUndo pane -- ", "\nEdit arg both -- acme's Edit" }) |row|
try testing.expect(std.mem.indexOf(u8, listed.bytes, row) != null);
}
@@ -1738,6 +1750,8 @@ test "status reads the editor, and look and exec validate whole batches" {
const gpa = testing.allocator;
const p = try withFile(gpa, "Msg root\n");
defer p.deinit();
+ // A terminal's view first (a window's build starts windowed).
+ p.windowed = false;
const status = rd(p, root_status, 0, 4096);
try testing.expectEqual(Status.ok, status.reply.status);
var lines = std.mem.tokenizeScalar(u8, status.bytes, '\n');
@@ -2075,8 +2089,7 @@ test "Zerox twins dump their text once and come back on one buffer from a Restor
try pardes.dump.dumpState(p);
const text = p.dump_out.?;
// The text is kept once though three panes show it.
- const encoded = try pardes.dump.encodeBytes(p.scratch.allocator(), p.panes[0].?.file.?.content);
- try testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, encoded));
+ try testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, p.panes[0].?.file.?.content));
const restored = try pardes.dump.restore(p, text, "/tmp/pardes.dump.zon");
defer restored.deinit();
var files: [3]*pardes.panes.File.State = undefined;
@@ -2838,7 +2851,7 @@ test "a Restore of a file that is no dump fails the write before any warning, an
const root_ctl = @backingInt(tree.TopFile.ctl);
const refused = wr(p, root_ctl, line);
try testing.expectEqual(E.IO, refused.errno());
- try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "not a pardes dump: line 1: ") != null);
+ try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "not a pardes dump: it does not begin as one") != null);
try testing.expect(p.restore_req == null);
try testing.expect(p.panes[0].?.discard_warned == null);
@@ -2856,7 +2869,7 @@ test "a setting this frontend cannot show says GUI-only, and DumpDir reads back
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
const root_ctl = @backingInt(tree.TopFile.ctl);
- if (!builtins.capabilities.lift) {
+ if (!builtins.viewCapabilities(p).lift) {
try testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", wr(p, root_ctl, "Lift on\n").reply.ename);
try testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", wr(p, root_ctl, "GripWidth 120\n").reply.ename);
// A builtin the GUI alone has, written to a ctl or an exec, never
@@ -4676,9 +4689,8 @@ test "pager answers its +Pager on the open that asked, whatever another client e
try testing.expectEqualStrings("/tmp/+Pager", second_pane.file.?.path);
try testing.expect(p.paneBySerial(paged) != null);
_ = call(p, .{ .tag = 14, .op = .release, .node = pager, .handle = resolved, .opened = true });
- // One that may not be written is refused, permission denied.
- if (comptime @import("builtin").os.tag == .linux)
- try testing.expectEqual(E.PERM, wr(p, pager, "/proc/1\n").errno());
+ // One that may not be written is refused, permission denied (Linux's /proc/1).
+ if (comptime @import("builtin").os.tag == .linux) try testing.expectEqual(E.PERM, wr(p, pager, "/proc/1\n").errno());
// One directory a write, absolute, and there.
try testing.expectEqual(E.INVAL, wr(p, pager, "/a\n/b\n").errno());
// One directory an open: one refused is not followed by another, as a
@@ -4787,8 +4799,11 @@ test "a command pane an exec open was answered is leased to it while it stays op
test "a Save whose directory is missing because one above refuses says permission denied, not no such directory" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- p.saveFailed(0, "/proc/1/root/x/f.txt", error.FileNotFound);
- try testing.expect(th.logHas(p, "Save /proc/1/root/x/f.txt: permission denied"));
+ // The refusing directory is Linux's /proc/1/root.
+ if (comptime @import("builtin").os.tag == .linux) {
+ p.saveFailed(0, "/proc/1/root/x/f.txt", error.FileNotFound);
+ try testing.expect(th.logHas(p, "Save /proc/1/root/x/f.txt: permission denied"));
+ }
p.saveFailed(0, "/tmp/pardes-surely-missing-dir/f.txt", error.FileNotFound);
try testing.expect(th.logHas(p, "no such directory"));
}
@@ -4897,46 +4912,6 @@ test "Petscii, Palette and Ascii take the state their tag word names, set rather
try testing.expect(th.logHas(p, "Petscii takes on or off"));
}
-test "a Restore reads a clean file from disk; an unsaved one whose file changed since is said so, and its first Save asks" {
- if (comptime !pardes.hosted) return;
- const gpa = testing.allocator;
- var tmp = testing.tmpDir(.{});
- defer tmp.cleanup();
- try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\n" });
- var dir_buf: [4096]u8 = undefined;
- const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
- var path_buf: [4200]u8 = undefined;
- const path = try std.fmt.bufPrint(&path_buf, "{s}/a.txt", .{dir});
- const p = try Pardes.init(gpa, .{ .file = path, .tty_only = true, .cols = 80, .rows = 24 });
- defer p.deinit();
- // Clean: the dump keeps no text, and the newer disk is what comes back.
- try pardes.dump.dumpState(p);
- try testing.expect(std.mem.indexOf(u8, p.dump_out.?, "b25lCg==") == null); // "one\n" in base64
- try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "two\n" });
- {
- const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon");
- defer restored.deinit();
- const f = pane_files.fileOf(restored.panes[0].?).?;
- try testing.expectEqualStrings("two\n", f.content);
- try testing.expectEqual(f.saved_revision, f.revision);
- }
- // Unsaved: its text comes back, the disk changed under it is said, and
- // Save asks once before overwriting it.
- const f = pane_files.fileOf(p.panes[0].?).?;
- const edited = try gpa.dupe(u8, "edited\n");
- pardes.panes.File.setContent(p, f, edited);
- try testing.expect(f.revision != f.saved_revision);
- try pardes.dump.dumpState(p);
- try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "three\n" });
- const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon");
- defer restored.deinit();
- const rf = pane_files.fileOf(restored.panes[0].?).?;
- try testing.expectEqualStrings("edited\n", rf.content);
- try testing.expect(rf.disk_newer != null);
- pardes.exec.saveFile(restored, 0);
- try testing.expect(th.logHas(restored, "modified on disk since read (Save again to overwrite)"));
-}
-
test "a Rename the server answered for this file alone, with other open files saying the name, is previewed and says so" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
@@ -5037,8 +5012,10 @@ test "DumpDir refuses a relative directory and one that may not be written, up f
defer p.deinit();
const root_ctl = @backingInt(tree.TopFile.ctl);
try testing.expectEqual(E.INVAL, wr(p, root_ctl, "DumpDir dumps\n").errno());
- try testing.expectEqual(E.INVAL, wr(p, root_ctl, "DumpDir /proc/1/root/dumps\n").errno());
try testing.expectEqual(Status.ok, wr(p, root_ctl, "DumpDir /tmp/pardes-dumps-test\n").reply.status);
+ // The refusing directory is Linux's /proc/1/root.
+ if (comptime @import("builtin").os.tag != .linux) return;
+ try testing.expectEqual(E.INVAL, wr(p, root_ctl, "DumpDir /proc/1/root/dumps\n").errno());
p.fs.late_failure_len = 0;
p.dumpFailed("/proc/1/root/x/pardes.dump.zon", error.FileNotFound);
try testing.expect(std.mem.endsWith(u8, p.fs.late_failure[0..p.fs.late_failure_len], ": permission denied"));
@@ -5082,25 +5059,6 @@ test "a command or a terminal refused for room is refused before anything is log
}
}
-test "an unsaved pane from a dump that says nothing of its disk asks at its first Save" {
- if (comptime !pardes.hosted) return;
- const gpa = testing.allocator;
- var tmp = testing.tmpDir(.{});
- defer tmp.cleanup();
- try tmp.dir.writeFile(testing.io, .{ .sub_path = "old.txt", .data = "disk\n" });
- var dir_buf: [4096]u8 = undefined;
- const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
- var path_buf: [4200]u8 = undefined;
- const path = try std.fmt.bufPrint(&path_buf, "{s}/old.txt", .{dir});
- const p = try withFile(gpa, "x\n");
- defer p.deinit();
- const id = p.freeSlot().?;
- const pane = try pardes.panes.File.restore(p, id, .{ .kind = .file, .tag = "", .body = "", .file = .{ .path = path, .content = "unsaved\n", .dirty = true } });
- try testing.expect(pane.file.?.disk_newer != null);
- pardes.exec.saveFile(p, id);
- try testing.expect(th.logHas(p, "modified on disk since read (Save again to overwrite)"));
-}
-
test "a Mount of a socket path that is not there says no such socket, ENOENT" {
if (comptime !pardes.hosted) return;
const p = try withFile(testing.allocator, "x\n");