diff options
| -rw-r--r-- | src/gui/gui.zig | 18 | ||||
| -rw-r--r-- | src/pardes.zig | 30 | ||||
| -rw-r--r-- | src/replica.zig | 27 | ||||
| -rw-r--r-- | src/tape.zig | 6 | ||||
| -rw-r--r-- | src/tty/tty.zig | 13 |
5 files changed, 70 insertions, 24 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index ead89a8a..be287647 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -4563,21 +4563,14 @@ const Shell = struct { var buf: [96]u8 = undefined; return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in"); } - var newest: u32 = 0; - for (core.panes) |slot| if (slot) |pn| { - newest = @max(newest, pn.serial); - }; if (s.adoptions_len < s.adoptions.len) { s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null }; s.adoptions_len += 1; - _ = core.executeBuiltinLine(core.active, "Tty"); - for (core.panes, 0..) |slot, id| if (slot) |pn| if (pn.serial > newest and pn.isTerminal()) { - // Its shell is up already: nothing to greet with `ls`. - pn.greet = false; - s.adoptions[s.adoptions_len - 1].pane = @intCast(id); + if (core.adoptShell()) |id| { + s.adoptions[s.adoptions_len - 1].pane = id; s.adopted += 1; return; - }; + } s.adoptions_len -= 1; } _ = libc.close(fd); @@ -4662,10 +4655,7 @@ const Shell = struct { if (s.gens[e.pane] == e.gen) eof: { if (e.failure) |err| { s.closePty(e.pane); - if (s.core.panes[e.pane]) |pane| { - pane.body.mode = .normal; - s.core.reportError(e.pane, "terminal reader", err); - } + s.core.readerFailed(e.pane, err); break :eof; } const pt = if (s.ptys[e.pane]) |*pt| pt else { diff --git a/src/pardes.zig b/src/pardes.zig index a4fcb2d3..81ea0cdd 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -5557,6 +5557,36 @@ pub const Pardes = struct { return if (selected.len == 0) config.defaultShell() else selected; } + /// A shell a dead session left, taken in (Adopt): a terminal pane made + /// for it as the Tty word makes one, its greeting off (the shell is up + /// already), whose spawn the host answers with that shell's pty. Its id, + /// or null with no room. An input, as every host write into the core + /// is: a replica of the session makes the same pane. + pub fn adoptShell(p: *Pardes) ?u8 { + const taped = tape.enter(p, .adopt_shell); + defer tape.leave(p, taped); + var newest: u32 = 0; + for (p.panes) |slot| if (slot) |pn| { + newest = @max(newest, pn.serial); + }; + _ = p.executeBuiltinLine(p.active, "Tty"); + for (p.panes, 0..) |slot, id| if (slot) |pn| if (pn.serial > newest and pn.isTerminal()) { + pn.greet = false; + return @intCast(id); + }; + return null; + } + + /// A terminal's reader failed (no end of file: the host closed its pty): + /// its pane takes keys again, and why is said. + pub fn readerFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .reader_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); + const pane = p.panes[id] orelse return; + pane.body.mode = .normal; + p.reportError(id, "terminal reader", err); + } + pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_marks: bool) void { const taped = tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } }); defer tape.leave(p, taped); diff --git a/src/replica.zig b/src/replica.zig index 2af32cdb..6a01ec26 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -3205,6 +3205,33 @@ test "a replica joined from the session's dump replays the units after it" { try std.testing.expectEqual(digestOf(a), r.digest()); } +test "a host's Adopt and a terminal reader's failure are inputs: a replica makes the same pane" { + const gpa = std.testing.allocator; + // Its terminals as screen state, as a session's for its clients are. + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), true); + defer a.deinit(); + const was = Stepping.begin(&a.t, null); + const bytes = try pardes.dump.dumpBytes(a.core); + tape.resync(&a.t); + was.end(); + defer a.core.gpa.free(bytes); + const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + // What tty.zig's and gui.zig's adoptShell and a failed reader do between + // steps: they wrote the core straight, and every client joined again. + const host = Stepping.begin(&a.t, null); + const id = a.core.adoptShell().?; + try std.testing.expect(a.core.panes[id].?.isTerminal() and !a.core.panes[id].?.greet); + a.core.readerFailed(id, error.InputOutput); + a.core.pump(a.host) catch {}; + host.end(); + a.seq += 1; + r.confirm(try a.unit(0, a.seq)); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + test "a join's dump, its blobs put back where they go, is the session's dump" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); diff --git a/src/tape.zig b/src/tape.zig index 12e3bdec..e446c06a 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -46,6 +46,10 @@ pub const Input = union(enum) { take_restore, set_cwd: struct { id: usize, cwd: []const u8 }, acknowledge_shell: struct { id: usize, executable: []const u8, prompt_marks: bool }, + /// A dead session's shell taken in (Adopt), and a terminal's reader + /// that failed: what a host writes into the core, made inputs. + adopt_shell, + reader_failed: struct { id: u8, err: anyerror }, shell_failed: struct { id: u8, err: anyerror }, restart_failed: struct { id: u8, err: anyerror }, save_failed: struct { id: u8, path: []const u8, err: anyerror }, @@ -590,6 +594,8 @@ pub const Tape = struct { .take_restore => _ = p.takeRestore(), .set_cwd => |a| p.setCwd(a.id, a.cwd), .acknowledge_shell => |a| p.acknowledgeShell(a.id, a.executable, a.prompt_marks), + .adopt_shell => _ = p.adoptShell(), + .reader_failed => |a| p.readerFailed(a.id, a.err), .shell_failed => |a| p.shellFailed(a.id, a.err), .restart_failed => |a| p.restartFailed(a.id, a.err), .save_failed => |a| p.saveFailed(a.id, a.path, a.err), diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 03b7a7e2..430c020d 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -2397,23 +2397,16 @@ const Shell = struct { var buf: [96]u8 = undefined; return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in"); } - var newest: u32 = 0; - for (core.panes) |slot| if (slot) |pn| { - newest = @max(newest, pn.serial); - }; if (s.adoptions_len < s.adoptions.len) { // A terminal pane, as the Tty word makes one; its spawn takes // this pty instead of forking a shell (`spawn`). s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null }; s.adoptions_len += 1; - _ = core.executeBuiltinLine(core.active, "Tty"); - for (core.panes, 0..) |slot, id| if (slot) |pn| if (pn.serial > newest and pn.isTerminal()) { - // Its shell is up already: nothing to greet with `ls`. - pn.greet = false; - s.adoptions[s.adoptions_len - 1].pane = @intCast(id); + if (core.adoptShell()) |id| { + s.adoptions[s.adoptions_len - 1].pane = id; s.adopted += 1; return; - }; + } s.adoptions_len -= 1; } // No room for it: its master goes, and the shell with this session. |
