summaryrefslogtreecommitdiff
path: root/src/ninep/pty.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/pty.zig')
-rw-r--r--src/ninep/pty.zig97
1 files changed, 41 insertions, 56 deletions
diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig
index b913e83b..be5bb83f 100644
--- a/src/ninep/pty.zig
+++ b/src/ninep/pty.zig
@@ -36,26 +36,6 @@ const winsize_max = 4096;
const e_winsize_range = std.fmt.comptimePrint("invalid winsize: out of range, 1x{d} to {d}x{d}", .{ pty_rows_min, winsize_max, winsize_max });
const e_winsize_rows = std.fmt.comptimePrint("invalid winsize: at least {d} rows", .{pty_rows_min});
-/// `winsize C R` whose R is a size but fewer rows than a pty takes.
-fn tooFewRows(line: []const u8) bool {
- var words = std.mem.tokenizeAny(u8, line, " \t");
- if (!std.mem.eql(u8, words.next() orelse return false, "winsize")) return false;
- _ = dimension(words.next() orelse return false) orelse return false;
- const rows = dimension(words.next() orelse return false) orelse return false;
- return words.next() == null and rows < pty_rows_min;
-}
-
-/// A winsize whose numbers are numbers but not sizes: said as such.
-fn outOfRange(line: []const u8) bool {
- var words = std.mem.tokenizeAny(u8, line, " \t");
- if (!std.mem.eql(u8, words.next() orelse return false, "winsize")) return false;
- var n: u8 = 0;
- while (words.next()) |w| : (n += 1) {
- _ = std.fmt.parseInt(u32, w, 10) catch return false;
- }
- return n == 2;
-}
-
fn signalNamed(word: []const u8) ?pardes.PtySignal {
if (std.mem.eql(u8, word, "INT")) return .int;
if (std.mem.eql(u8, word, "TERM")) return .term;
@@ -71,61 +51,68 @@ pub fn writeCtl(p: *Pardes, req: Req, id: usize) Reply {
while (it.next()) |raw| {
const line = std.mem.trim(u8, raw, " \t\r");
if (line.len == 0) continue;
- // A command pane's child is its command, done or running: exec
- // would run its line again with no header, footer or records.
- if (!apply and std.mem.eql(u8, line, "exec")) if (p.panes[id]) |pane| if (pane.command != null)
- return tree.failText(req.tag, E.INVAL, "invalid exec: a command pane does not restart");
- // A shell restarted where the directory is gone would not start:
- // refused before anything runs, as Term there is.
- if (!apply and std.mem.eql(u8, line, "exec")) if (p.panes[id]) |pane| if (pardes.exec.missingDir(p, pane.cwdSlice())) {
+ if (!apply and std.mem.eql(u8, line, "exec")) if (p.panes[id]) |pane| {
+ // A command pane's child is its command, done or running: exec
+ // would run its line again with no header, footer or records.
+ if (pane.command != null) return tree.failText(req.tag, E.INVAL, "invalid exec: a command pane does not restart");
+ // A shell restarted where the directory is gone would not start:
+ // refused before anything runs, as Term there is.
const dir = pane.cwdSlice();
- return tree.failText(req.tag, E.NOENT, std.fmt.bufPrint(&p.fs.ename, "exec: {s}: no such directory", .{dir[0..@min(dir.len, 256)]}) catch "exec: no such directory");
- };
- // Nor is a shell that is not there, or a script whose
- // interpreter is not: refused, and the running shell kept.
- if (comptime pardes.hosted) if (!apply and std.mem.eql(u8, line, "exec")) if (p.panes[id]) |pane| {
- const bin = pane.shell orelse p.shellBin();
- var why: [320]u8 = undefined;
- if (pardes.tape.value(.shell, @import("../host_io.zig").Shell.refusal, .{ bin[0..@min(bin.len, 200)], &why })) |refused|
- return tree.failText(req.tag, E.NOENT, std.fmt.bufPrint(&p.fs.ename, "exec: {s}", .{refused}) catch "exec: no such shell");
+ if (pardes.exec.missingDir(p, dir))
+ return tree.failText(req.tag, E.NOENT, std.fmt.bufPrint(&p.fs.ename, "exec: {s}: no such directory", .{dir[0..@min(dir.len, 256)]}) catch "exec: no such directory");
+ // Nor is a shell that is not there, or a script whose
+ // interpreter is not: refused, and the running shell kept.
+ if (comptime pardes.hosted) {
+ const bin = pane.shell orelse p.shellBin();
+ var why: [320]u8 = undefined;
+ if (pardes.tape.value(.shell, @import("../host_io.zig").Shell.refusal, .{ bin[0..@min(bin.len, 200)], &why })) |refused|
+ return tree.failText(req.tag, E.NOENT, std.fmt.bufPrint(&p.fs.ename, "exec: {s}", .{refused}) catch "exec: no such shell");
+ }
};
- if (!verb(p, id, line, apply)) return tree.failText(req.tag, E.INVAL, if (tooFewRows(line)) e_winsize_rows else if (outOfRange(line)) e_winsize_range else e_bad_pty_ctl);
+ if (verb(p, id, line, apply)) |why| return tree.failText(req.tag, E.INVAL, why);
}
}
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
}
-fn verb(p: *Pardes, id: usize, line: []const u8, apply: bool) bool {
- const pane = p.panes[id] orelse return false;
+/// The refusal of one pty/ctl line, or null when it is taken (and, with
+/// `apply`, done).
+fn verb(p: *Pardes, id: usize, line: []const u8, apply: bool) ?[]const u8 {
+ const pane = p.panes[id] orelse return e_bad_pty_ctl;
var words = std.mem.tokenizeAny(u8, line, " \t");
- const v = std.meta.stringToEnum(Verb, words.next() orelse return false) orelse return false;
+ const v = std.meta.stringToEnum(Verb, words.next() orelse return e_bad_pty_ctl) orelse return e_bad_pty_ctl;
switch (v) {
.winsize => {
- const cols = dimension(words.next() orelse return false) orelse return false;
+ const c = words.next() orelse return e_bad_pty_ctl;
+ const r = words.next() orelse return e_bad_pty_ctl;
+ if (words.next() != null) return e_bad_pty_ctl;
+ // Numbers that are no sizes are said as such, not as a bad line.
+ _ = std.fmt.parseInt(u32, c, 10) catch return e_bad_pty_ctl;
+ _ = std.fmt.parseInt(u32, r, 10) catch return e_bad_pty_ctl;
+ const cols = dimension(c) orelse return e_winsize_range;
+ const rows = dimension(r) orelse return e_winsize_range;
// Two rows at least, as a pane is laid out: a one-row pty
// loses its prompt's input mark and reads busy for ever. Fewer
// is refused, never quietly raised.
- const rows = dimension(words.next() orelse return false) orelse return false;
- if (rows < pty_rows_min) return false;
- if (words.next() != null) return false;
- if (!apply) return true;
+ if (rows < pty_rows_min) return e_winsize_rows;
+ if (!apply) return null;
p.emit(.{ .resize_pty = .{ .pane = @intCast(id), .cols = cols, .rows = rows } });
pane.fs.winsize = .{ cols, rows };
},
.sig => {
- const which = signalNamed(words.next() orelse return false) orelse return false;
- if (words.next() != null) return false;
- if (!apply) return true;
+ const which = signalNamed(words.next() orelse return e_bad_pty_ctl) orelse return e_bad_pty_ctl;
+ if (words.next() != null) return e_bad_pty_ctl;
+ if (!apply) return null;
p.emit(.{ .signal_pty = .{ .pane = @intCast(id), .serial = pane.serial, .sig = which } });
},
.exec => {
- if (words.next() != null) return false;
- if (pane.cwdSlice().len > pardes.effect_path_cap) return false;
- if (!apply) return true;
+ if (words.next() != null) return e_bad_pty_ctl;
+ if (pane.cwdSlice().len > pardes.effect_path_cap) return e_bad_pty_ctl;
+ if (!apply) return null;
p.emitSpawn(id, pane.serial, pane.cwdSlice());
},
}
- return true;
+ return null;
}
/// ctl reads back its state in the words it takes, as a Plan 9 ctl does.
@@ -223,16 +210,14 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
// One line a run, its answer read before the next: a second line (bash
// writes printf 'a\nb\n' a line at a time) would run and lose the
// first's answer.
- const one = std.mem.trimEnd(u8, req.data, "\r\n");
- if (std.mem.indexOfScalar(u8, one, '\n') != null or (slot.phase == .done and slot.read == 0))
+ const line = std.mem.trimEnd(u8, req.data, "\r\n");
+ if (std.mem.indexOfScalar(u8, line, '\n') != null or (slot.phase == .done and slot.read == 0))
return tree.failText(req.tag, E.INVAL, e_one_line);
// A new line on this open: a new answer.
p.gpa.free(slot.output);
p.gpa.free(slot.line);
slot.* = .{};
- const line = std.mem.trimEnd(u8, req.data, "\r\n");
if (std.mem.trim(u8, line, " \t").len == 0) return tree.failText(req.tag, E.INVAL, e_bad_line);
- if (line.len == 0) return tree.failText(req.tag, E.INVAL, e_bad_line);
for (line) |c| if (c < ' ' and c != '\t') return tree.failText(req.tag, E.INVAL, e_bad_line);
if (comptime !pardes.panes.terminal.enabled) return tree.failText(req.tag, E.INVAL, e_bad_line);
const pf = &pane.fs;