summaryrefslogtreecommitdiff
path: root/src/ninep/ctl.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/ctl.zig')
-rw-r--r--src/ninep/ctl.zig133
1 files changed, 108 insertions, 25 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index 31265a94..87339b70 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -296,7 +296,7 @@ pub fn captured(p: *Pardes, req: Req, id: usize, exec: bool, text: []const u8, i
/// The errno a builtin's failure answers with: ENOENT for what is not
/// there (`no such`, `not found`, the words 9ns reads as ENOENT too), else
/// EIO.
-fn failureErrno(failure: []const u8) u16 {
+pub fn failureErrno(failure: []const u8) u16 {
if (std.mem.indexOf(u8, failure, "no such") != null or std.mem.indexOf(u8, failure, "not found") != null) return E.NOENT;
// A pattern refused (Edit's too) is malformed input, as 9ns reads it;
// so is a word this build has not (`invalid: acme's Put ...`).
@@ -422,23 +422,39 @@ const Builtin = builtins.registry.Builtin();
/// Refuses a control message, quoting it the way Plan 9's cmderror does
/// (kernel/misc/parse.c:82): `unknown control message "Bogus 3"`.
fn refuse(p: *Pardes, req: Req, why: []const u8, line: []const u8) Reply {
- const room = p.fs.ename.len -| (why.len + 3);
- var shown: [320]u8 = undefined;
- const text = std.fmt.bufPrint(&p.fs.ename, "{s} \"{s}\"", .{ why, blanked(line[0..@min(line.len, room)], &shown) }) catch why;
- return tree.failText(req.tag, E.INVAL, text);
+ const word = firstWord(line);
+ // A word too long to quote whole gives up its middle (fitErr), so the
+ // refusal keeps both its ends.
+ var shown: [4096]u8 = undefined;
+ var whole: [4096 + 320]u8 = undefined;
+ const full = std.fmt.bufPrint(&whole, "{s} \"{s}\"", .{ why, blanked(word[0..@min(word.len, shown.len)], &shown) }) catch why;
+ // Into the session's own buffer: `whole` is this frame's.
+ if (full.len <= p.fs.ename.len) {
+ @memcpy(p.fs.ename[0..full.len], full);
+ return tree.failText(req.tag, E.INVAL, p.fs.ename[0..full.len]);
+ }
+ return tree.failText(req.tag, E.INVAL, tree.fitErr(full, &p.fs.ename));
+}
+
+/// The word a refusal quotes: a control message's first, its offending
+/// word, never the line with its arguments (one rule everywhere: `wrong
+/// #args in control message "Newcol"`).
+pub fn firstWord(line: []const u8) []const u8 {
+ const trimmed = std.mem.trim(u8, line, " \t\r\n");
+ return trimmed[0 .. std.mem.indexOfAny(u8, trimmed, " \t") orelse trimmed.len];
}
/// `refuse`, saying which ctl takes the message instead.
pub fn refuseTo(p: *Pardes, req: Req, why: []const u8, line: []const u8, ctl: []const u8) Reply {
const room = p.fs.ename.len -| (why.len + ctl.len + 18);
var shown: [320]u8 = undefined;
- const text = std.fmt.bufPrint(&p.fs.ename, "{s} \"{s}\": write it to {s}", .{ why, blanked(line[0..@min(line.len, room)], &shown), ctl }) catch why;
+ const text = std.fmt.bufPrint(&p.fs.ename, "{s} \"{s}\": write it to {s}", .{ why, blanked(firstWord(line)[0..@min(firstWord(line).len, room)], &shown), ctl }) catch why;
return tree.failText(req.tag, E.INVAL, text);
}
/// A quoted line as a refusal shows it: a control byte (a tab, the one a
/// line may hold) is a blank, so the reason reads as one line of words.
-fn blanked(line: []const u8, buf: *[320]u8) []const u8 {
+fn blanked(line: []const u8, buf: []u8) []const u8 {
const n = @min(line.len, buf.len);
for (line[0..n], buf[0..n]) |c, *o| o.* = if (c < ' ' or c == 0x7f) ' ' else c;
return buf[0..n];
@@ -1087,7 +1103,8 @@ test "a control character in a write to any ctl refuses the whole write, and a q
}
const quoted = wr(p, Node.of(serialOf(p), .ctl), "bogus\tword\n");
try testing.expectEqual(E.INVAL, quoted.errno());
- try testing.expect(std.mem.indexOf(u8, quoted.reply.ename, "bogus word") != null);
+ // The offending word alone is quoted: a tab ends it.
+ try testing.expect(std.mem.indexOf(u8, quoted.reply.ename, "\"bogus\"") != null);
try testing.expect(std.mem.indexOfScalar(u8, quoted.reply.ename, '\t') == null);
}
@@ -1209,11 +1226,11 @@ test "the pane ctl takes acme's verbs and the pane's builtins, and refuses the r
const refused = wr(p, ctl_node, bad);
try testing.expectEqual(E.INVAL, refused.errno());
var want: [64]u8 = undefined;
- try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "unknown control message \"{s}\"", .{bad}), refused.reply.ename);
+ try testing.expectEqualStrings(try std.fmt.bufPrint(&want, "unknown control message \"{s}\"", .{firstWord(bad)}), refused.reply.ename);
}
try testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", wr(p, ctl_node, "Newcol").reply.ename);
- try testing.expectEqualStrings("not a window control message \"Verbose off\": write it to /ctl", wr(p, ctl_node, "Verbose off").reply.ename);
- try testing.expectEqualStrings("wrong #args in control message \"Collapse now\"", wr(p, ctl_node, "Collapse now").reply.ename);
+ try testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", wr(p, ctl_node, "Verbose off").reply.ename);
+ try testing.expectEqualStrings("wrong #args in control message \"Collapse\"", wr(p, ctl_node, "Collapse now").reply.ename);
// Checked whole first: a bad line anywhere and nothing ran.
try testing.expectEqual(E.INVAL, wr(p, ctl_node, "Msg first\nbogus").errno());
try testing.expect(p.panes[0].?.msg_len == 0 or !std.mem.eql(u8, p.panes[0].?.msg[0..p.panes[0].?.msg_len], "first"));
@@ -1265,18 +1282,18 @@ test "the root ctl reads the settings as a write takes them, and takes the sessi
try testing.expectEqual(columns + 1, p.ncol);
try testing.expectEqualStrings("not a session control message \"Del\": write it to pane/<n>/ctl", wr(p, root_ctl, "Del").reply.ename);
- try testing.expectEqualStrings("unknown control message \"Nonsense 1\"", wr(p, root_ctl, "Nonsense 1").reply.ename);
- try testing.expectEqualStrings("bad value in control message; takes on, off \"Verbose maybe\"", wr(p, root_ctl, "Verbose maybe").reply.ename);
- try testing.expectEqualStrings("bad value in control message; takes acme, pardes \"Placement east\"", wr(p, root_ctl, "Placement east").reply.ename);
+ try testing.expectEqualStrings("unknown control message \"Nonsense\"", wr(p, root_ctl, "Nonsense 1").reply.ename);
+ try testing.expectEqualStrings("bad value in control message; takes on, off \"Verbose\"", wr(p, root_ctl, "Verbose maybe").reply.ename);
+ try testing.expectEqualStrings("bad value in control message; takes acme, pardes \"Placement\"", wr(p, root_ctl, "Placement east").reply.ename);
// A number's refusal names its range, a path's what it takes.
- try testing.expectEqualStrings("bad value in control message; takes 0-100 (a percentage) \"InactiveDim 200\"", wr(p, root_ctl, "InactiveDim 200").reply.ename);
- try testing.expectEqualStrings("bad value in control message; takes 0-60000 (milliseconds) \"MessageLinger x\"", wr(p, root_ctl, "MessageLinger x").reply.ename);
+ try testing.expectEqualStrings("bad value in control message; takes 0-100 (a percentage) \"InactiveDim\"", wr(p, root_ctl, "InactiveDim 200").reply.ename);
+ try testing.expectEqualStrings("bad value in control message; takes 0-60000 (milliseconds) \"MessageLinger\"", wr(p, root_ctl, "MessageLinger x").reply.ename);
for (config.Runtime.settings) |setting| if (setting.action != .theme)
try testing.expect(config.Runtime.takes(setting.action) != null);
const no_theme = wr(p, root_ctl, "Theme no-such-theme").reply.ename;
try testing.expectStringStartsWith(no_theme, "bad value in control message; like it: n");
- try testing.expect(std.mem.endsWith(u8, no_theme, "; Themes lists all \"Theme no-such-theme\""));
- try testing.expectEqualStrings("wrong #args in control message \"Newcol 2\"", wr(p, root_ctl, "Newcol 2").reply.ename);
+ try testing.expect(std.mem.endsWith(u8, no_theme, "; Themes lists all \"Theme\""));
+ try testing.expectEqualStrings("wrong #args in control message \"Newcol\"", wr(p, root_ctl, "Newcol 2").reply.ename);
try testing.expectEqualStrings("wrong #args in control message \"Theme\"", wr(p, root_ctl, "Theme").reply.ename);
// A bare required word fails the check, so the line before never runs.
try testing.expectEqual(E.INVAL, wr(p, root_ctl, "Verbose on\nMount").errno());
@@ -1760,6 +1777,71 @@ test "exec Save writes the named file through the host" {
try testing.expectEqualStrings(pane.file.?.content, p.fallback.get("/tmp/renamed.txt").?);
}
+test "a refusal's +Unsaved goes in the session's directory when the active pane's directory is not there" {
+ if (comptime !pardes.hosted) return;
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .name), "/tmp/pardes-no-such-dir-zz/x.txt\n").reply.status);
+ try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .body), "edited\n").reply.status);
+ try testing.expectEqual(Status.err, wr(p, @intFromEnum(tree.TopFile.ctl), "Exit\n").reply.status);
+ const unsaved = for (p.panes) |slot| {
+ const pane = slot orelse continue;
+ const f = pane.file orelse continue;
+ if (f.output) |o| if (o.from == .unsaved) break f.path;
+ } else return error.NoUnsavedPane;
+ try testing.expectEqualStrings(p.sessionDir(), std.fs.path.dirname(unsaved).?);
+}
+
+test "a look at a file it may not read fails the write naming the file" {
+ if (comptime !pardes.hosted) return;
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "locked.txt", .data = "secret\n", .flags = .{ .permissions = .fromMode(0o000) } });
+ var dir_buf: [4096]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
+ var line: [4200]u8 = undefined;
+ const path = try std.fmt.bufPrint(&line, "{s}/locked.txt", .{dir});
+ var want: [4200]u8 = undefined;
+ const expected = try std.fmt.bufPrint(&want, "look: {s}: permission denied", .{path});
+ var with_newline: [4300]u8 = undefined;
+ const refused = wr(p, root_look, try std.fmt.bufPrint(&with_newline, "{s}\n", .{path}));
+ if (refused.reply.status == .ok) return error.SkipZigTest; // root reads anything
+ try testing.expectEqualStrings(expected, refused.reply.ename);
+}
+
+test "Pager is a setting of the root ctl: pardes by default, off taken, anything else refused with its choices" {
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ const root_ctl = @intFromEnum(tree.TopFile.ctl);
+ try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, "Pager pardes\n") != null);
+ try testing.expectEqual(Status.ok, wr(p, root_ctl, "Pager off\n").reply.status);
+ try testing.expectEqual(config.Runtime.Pager.off, p.settings.pager);
+ try testing.expect(std.mem.indexOf(u8, rd(p, root_ctl, 0, 1 << 16).bytes, "Pager off\n") != null);
+ try testing.expectEqualStrings("bad value in control message; takes pardes, off \"Pager\"", wr(p, root_ctl, "Pager less\n").reply.ename);
+}
+
+test "a refused Del written back through event fails that write, EIO, with its err" {
+ const gpa = testing.allocator;
+ const p = try withFile(gpa, "text\n");
+ defer p.deinit();
+ const serial = serialOf(p);
+ try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .body), "more\n").reply.status); // unsaved
+ const event = Node.of(serial, .event);
+ const h = call(p, .{ .tag = 1, .op = .open, .node = event }).reply.handle;
+ defer _ = call(p, .{ .tag = 9, .op = .release, .node = event, .handle = h });
+ try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Del\n").reply.status);
+ const record = call(p, .{ .tag = 2, .op = .read, .node = event, .handle = h, .size = 256 }).bytes;
+ var copy: [256]u8 = undefined;
+ @memcpy(copy[0..record.len], record);
+ const back = wr(p, event, copy[0..record.len]);
+ try testing.expectEqual(E.IO, back.errno());
+ try testing.expect(std.mem.indexOf(u8, back.reply.ename, "Modified") != null);
+ try testing.expect(p.paneBySerial(serial) != null);
+}
+
test "a pane's own look and exec go to the event reader holding it, which writes them back to act" {
const gpa = testing.allocator;
const p = try withFile(gpa, "Msg routed\n");
@@ -1951,7 +2033,7 @@ test "Joincol with no column to the right and Theme with no such theme say so" {
try testing.expectEqual(E.INVAL, themed.errno());
try testing.expectStringStartsWith(themed.reply.ename, "bad value in control message; like it: ");
try testing.expect(std.mem.indexOf(u8, themed.reply.ename, ": dr") != null);
- try testing.expect(std.mem.endsWith(u8, themed.reply.ename, "\"Theme drak\""));
+ try testing.expect(std.mem.endsWith(u8, themed.reply.ename, "\"Theme\""));
// A click on the word says it on the message row.
_ = wr(p, Node.of(serialOf(p), .exec), "Theme drak\n");
const pane = p.panes[p.active].?;
@@ -2089,8 +2171,8 @@ test "a setting this frontend cannot show says GUI-only, and DumpDir reads back
defer p.deinit();
const root_ctl = @intFromEnum(tree.TopFile.ctl);
if (!builtins.capabilities.lift) {
- try testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift on\"", wr(p, root_ctl, "Lift on\n").reply.ename);
- try testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth 120\"", wr(p, root_ctl, "GripWidth 120\n").reply.ename);
+ 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
// runs as a shell command.
if (comptime !pardes.font_picker) {
@@ -2100,7 +2182,7 @@ test "a setting this frontend cannot show says GUI-only, and DumpDir reads back
try testing.expectEqualStrings("Fonts is GUI-only, invalid here", ran.reply.ename);
}
}
- try testing.expectEqualStrings("unknown control message \"Liftt on\"", wr(p, root_ctl, "Liftt on\n").reply.ename);
+ try testing.expectEqualStrings("unknown control message \"Liftt\"", wr(p, root_ctl, "Liftt on\n").reply.ename);
var buf: [900]u8 = undefined;
if (pardes.dump.directory(&buf, "")) |dir| {
var want: [920]u8 = undefined;
@@ -2945,13 +3027,14 @@ test "every EINVAL a write gets says why, in its err record too; DEL is a contro
try testing.expect(th.logHas(p, "an empty name"));
}
-test "/index shows a name as the log does: a newline in it is \\n, controls spaces, bytes not UTF-8 \\xNN" {
+test "/index shows a name as the log does: a newline in it is \\n, a control byte, C1 or byte not UTF-8 \\xNN" {
const p = try th.withTerm(testing.allocator);
defer p.deinit();
const term = p.paneBySerial(serialOf(p)).?;
- p.setCwd(term, "/tmp/two\nlines\x7f\xff");
+ p.setCwd(term, "/tmp/two\nlines\x07\x7f\xc2\x85\xff");
const index = rd(p, @intFromEnum(tree.TopFile.index), 0, 4096).bytes;
- try testing.expect(std.mem.indexOf(u8, index, "/tmp/two\\nlines \\xff ") != null);
+ // Each byte as itself, so the name decodes to what it is.
+ try testing.expect(std.mem.indexOf(u8, index, "/tmp/two\\nlines\\x07\\x7f\\xc2\\x85\\xff ") != null);
try testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, "\n"));
try testing.expect(index.len > 0);
try testing.expectEqual(@as(u64, 0), call(p, .{ .tag = 1, .op = .getattr, .node = @intFromEnum(tree.TopFile.index) }).reply.attr.size);