diff options
| -rw-r--r-- | src/ninep/tree.zig | 13 | ||||
| -rw-r--r-- | test/behaviour/rules.zig | 17 |
2 files changed, 28 insertions, 2 deletions
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index 707ce40a..3b8a8581 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -1258,8 +1258,17 @@ pub fn holdsPage(p: *Pardes, req: Req) bool { fn writeLines(p: *Pardes, req: Req, target: Target) Reply { // A command line holds no control character but a tab: the whole write - // is refused at once, not held to fail unseen at the close. - if (resultsFile(target) or ctlFile(target)) for (req.data) |c| if ((c < ' ' and c != '\t' and c != '\n' and c != '\r') or c == 0x7f) return failText(req.tag, E.INVAL, ctl.e_control); + // is refused at once, not held to fail unseen at the close, and the + // line it is in goes with it, as one refused for its length does: the + // start a write before held, and the rest, through its newline, that + // the next write brings. + if (resultsFile(target) or ctlFile(target)) for (req.data) |c| if ((c < ' ' and c != '\t' and c != '\n' and c != '\r') or c == 0x7f) { + if (openOf(p, req)) |o| if (o.what == .lines or o.what == .ctl) { + o.pending.clearRetainingCapacity(); + o.discarding = !std.mem.endsWith(u8, req.data, "\n"); + }; + return failText(req.tag, E.INVAL, ctl.e_control); + }; // A NUL no name may hold fails its write at once, not the close. if (target == .pane and target.pane.file == .name and std.mem.indexOfScalar(u8, req.data, 0) != null) return failText(req.tag, E.INVAL, pane.e_name_char ++ ": a NUL"); diff --git a/test/behaviour/rules.zig b/test/behaviour/rules.zig index 8f5597ae..80f94406 100644 --- a/test/behaviour/rules.zig +++ b/test/behaviour/rules.zig @@ -18,6 +18,23 @@ test "rules: a control character in a write to any ctl refuses the whole write, try std.testing.expect(std.mem.indexOfScalar(u8, s.said, '\t') == null); } +test "rules: a write refused for a control character takes its line with it: the start a write before held, and the rest the next write brings" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const ctl = try s.hold("/ctl", 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ctl, "bogus")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(ctl, "wo\x01rd")); + // The refused line's rest goes through its newline unread; what follows + // is a line of its own, which runs at the close. + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ctl, "tail\nnext")); + s.clunk(ctl); + const log = try s.read("/log"); + try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, log, "err ")); + try std.testing.expect(std.mem.indexOf(u8, log, "bogus") == null); + try std.testing.expect(std.mem.indexOf(u8, log, "tail") == null); + try std.testing.expect(std.mem.indexOf(u8, log, "\"next\"") != null); +} + test "rules: body reads at any offset and writes append; a stat says its length, and its version moves with each edit" { var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); defer s.deinit(); |
