diff options
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/Host.zig | 4 | ||||
| -rw-r--r-- | dev/Ptys.zig | 27 | ||||
| -rw-r--r-- | dev/gui/gui.zig | 2 | ||||
| -rw-r--r-- | dev/host_io.zig | 42 |
4 files changed, 34 insertions, 41 deletions
diff --git a/dev/Host.zig b/dev/Host.zig index cb7aefe1..52205518 100644 --- a/dev/Host.zig +++ b/dev/Host.zig @@ -30,9 +30,9 @@ pub const VTable = struct { /// Upgrade: this process exec'd as the pardes installed since, at /// the step's end (upgrade.zig); `pane` hears why if it cannot be. upgrade: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, - spawn: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cwd: []const u8) void = null, + spawn: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void = null, pty_write: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, bytes: []const u8) void = null, - pty_resize: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cols: u16, rows: u16) void = null, + pty_resize: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, size: pardes.PtySize) void = null, pty_signal: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, sig: pardes.PtySignal) void = null, close_pty: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, tty_taken: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) bool = null, diff --git a/dev/Ptys.zig b/dev/Ptys.zig index 583e3c38..5b8705ef 100644 --- a/dev/Ptys.zig +++ b/dev/Ptys.zig @@ -45,16 +45,15 @@ pub fn Calls(comptime Ctx: type) type { fn of(ctx: ?*anyopaque) *Ctx { return @ptrCast(@alignCast(ctx.?)); } - pub fn spawn(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { + pub fn spawn(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void { const s = of(ctx); - s.ptys.spawn(s.core, id, cwd); + s.ptys.spawn(s.core, id, cwd, size); } pub fn write(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { of(ctx).ptys.write(id, bytes); } - pub fn resize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { - const s = of(ctx); - s.ptys.resize(s.core, id, cols, rows); + pub fn resize(ctx: ?*anyopaque, id: pardes.Pane.Id, size: pardes.PtySize) void { + of(ctx).ptys.resize(id, size); } pub fn signal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { of(ctx).ptys.signal(id, sig); @@ -82,14 +81,14 @@ pub fn Calls(comptime Ctx: type) type { } /// Pane `id`'s shell, in `cwd` (empty: where its pane's directory says), -/// or the adopted one waiting for it. -pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8) void { +/// at `size`, or the adopted one waiting for it. +pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void { const pane = id.index(); - if (p.adoptions.take(id)) |a| return p.take(core, pane, a); + if (p.adoptions.take(id)) |a| return p.take(core, pane, a, size); // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = p.slots[pane] != null; - const child = host_io.forkShell(core, pane, p.prompt_rcs, core.shellBin(), cwd, core.view().screen_h, core.view().screen_w) catch |err| { + const child = host_io.forkShell(core, pane, p.prompt_rcs, core.shellBin(), cwd, size) catch |err| { if (running) return core.restartFailed(id, err); return core.shellFailed(id, err); }; @@ -107,7 +106,7 @@ pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8 /// An adopted shell, running already, as pane `pane`'s: told the size /// twice (a size it never had, then the pane's), which makes it redraw. -fn take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption) void { +fn take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption, size: pardes.PtySize) void { p.close(.at(pane)); p.gens[pane] +%= 1; p.slots[pane] = .{ .fd = a.fd, .pid = a.pid }; @@ -117,7 +116,7 @@ fn take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption) void { if (host_io.shellCwd(a.pid, &lbuf)) |wd| core.setCwd(.at(pane), wd); const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); - if (core.panes[pane]) |pn| p.resize(core, .at(pane), @max(pn.cols, 1), @max(pn.rows, 1)); + p.resize(.at(pane), size); p.read(core, pane); } @@ -144,9 +143,9 @@ pub fn write(p: *Ptys, id: pardes.Pane.Id, bytes: []const u8) void { if (p.slots[id.index()]) |pt| _ = host_io.writeFd(pt.fd, bytes); } -pub fn resize(p: *Ptys, core: *const pardes.Pardes, id: pardes.Pane.Id, cols: u16, rows: u16) void { +pub fn resize(p: *Ptys, id: pardes.Pane.Id, size: pardes.PtySize) void { const pt = p.slots[id.index()] orelse return; - const ws = host_io.ptyWinsize(core, cols, rows); + const ws = host_io.ptyWinsize(size); _ = posix.system.ioctl(pt.fd, TIOCSWINSZ, @intFromPtr(&ws)); } @@ -290,7 +289,7 @@ fn testing(queue: *Inbox) Ptys { } fn testShell(p: *Ptys, pane: u8) !libc.pid_t { - const child = try host_io.forkShell(null, pane, p.prompt_rcs, "/bin/sh", "", 24, 80); + const child = try host_io.forkShell(null, pane, p.prompt_rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); p.slots[pane] = .{ .fd = child.file.handle, .pid = child.pid }; p.slots[pane].?.reader = try p.queue.startReader(.{ .handle = child.file.handle, .flags = .{ .nonblocking = false } }, .at(pane), p.gens[pane]); return child.pid; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index bfe658f9..d9d38f06 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -7677,7 +7677,7 @@ test "a detached GUI authority follows terminal directory changes with no window defer ptys.deinit(); var no_peers: replica.Peers = .{}; var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .watches = undefined, .peers = &no_peers }; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); ptys.slots[0] = .{ .fd = child.file.handle, .pid = child.pid }; try std.testing.expect(host_io.writeFd(child.file.handle, "cd child\n")); const until = std.Io.Clock.awake.now(io).toMilliseconds() + 2_000; diff --git a/dev/host_io.zig b/dev/host_io.zig index 419c4570..f189c8ea 100644 --- a/dev/host_io.zig +++ b/dev/host_io.zig @@ -420,13 +420,8 @@ pub const exec_command_max = pardes.exec.command_max; /// A pane's pty size, in cells and in pixels: a program sizes its kitty /// graphics by the pixels (yazi's previews; pardes nested in a pane). -pub fn ptyWinsize(core: ?*const pardes.Pardes, cols: u16, rows: u16) posix.winsize { - var ws: posix.winsize = .{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; - if (core) |c| { - ws.xpixel = std.math.cast(u16, @as(u32, cols) * c.view().cell_pixels.w) orelse 0; - ws.ypixel = std.math.cast(u16, @as(u32, rows) * c.view().cell_pixels.h) orelse 0; - } - return ws; +pub fn ptyWinsize(size: pardes.PtySize) posix.winsize { + return .{ .row = size.rows, .col = size.cols, .xpixel = size.w_px, .ypixel = size.h_px }; } pub fn forkShell( @@ -435,8 +430,7 @@ pub fn forkShell( prompt_rcs: *const Shell.PromptFiles, bin: []const u8, cwd: []const u8, - rows: u16, - cols: u16, + size: pardes.PtySize, ) !Child { const native_cwd = filesystem.localPath(cwd) orelse cwd; if (std.mem.indexOfScalar(u8, native_cwd, 0) != null) return error.InvalidPath; @@ -494,7 +488,7 @@ pub fn forkShell( else .terminal; const envp = ChildEnv.build(&env_slots, paging); - const ws = ptyWinsize(core, cols, rows); + const ws = ptyWinsize(size); // The child says here why it could not become the shell (its chdir or // its exec failed, as for a script whose interpreter is not there): the // pipe closes on exec, so the parent reads nothing when the shell runs, @@ -1672,9 +1666,9 @@ test "the child environment replaces the launcher's terminal identity exactly on test "shell spawn rejects invalid directories before creating a child" { const rcs: Shell.PromptFiles = .{}; - try std.testing.expectError(error.InvalidPath, forkShell(null, 0, &rcs, "/bin/sh", "/tmp\x00/ignored", 24, 80)); + try std.testing.expectError(error.InvalidPath, forkShell(null, 0, &rcs, "/bin/sh", "/tmp\x00/ignored", .{ .cols = 80, .rows = 24 })); const too_long = @as([4096]u8, @splat('x')); - try std.testing.expectError(error.NameTooLong, forkShell(null, 0, &rcs, "/bin/sh", &too_long, 24, 80)); + try std.testing.expectError(error.NameTooLong, forkShell(null, 0, &rcs, "/bin/sh", &too_long, .{ .cols = 80, .rows = 24 })); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); @@ -1683,12 +1677,12 @@ test "shell spawn rejects invalid directories before creating a child" { const directory = directory_buf[0..try tmp.dir.realPath(std.testing.io, &directory_buf)]; var path_buf: [4096]u8 = undefined; const file = try std.fmt.bufPrint(&path_buf, "{s}/file", .{directory}); - try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", file, 24, 80)); + try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", file, .{ .cols = 80, .rows = 24 })); const explicit_file = try std.fmt.bufPrint(&path_buf, "/n/os{s}/file", .{directory}); - try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", explicit_file, 24, 80)); + try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", explicit_file, .{ .cols = 80, .rows = 24 })); const missing = try std.fmt.bufPrint(&path_buf, "{s}/missing/" ++ (std.mem.asBytes(&@as([50][6]u8, @splat("child/".*)))), .{directory}); try std.testing.expect(missing.len > 256); - try std.testing.expectError(error.FileNotFound, forkShell(null, 0, &rcs, "/bin/sh", missing, 24, 80)); + try std.testing.expectError(error.FileNotFound, forkShell(null, 0, &rcs, "/bin/sh", missing, .{ .cols = 80, .rows = 24 })); } test "a forked shell is told which terminal it is talking to" { @@ -1711,7 +1705,7 @@ test "a forked shell is told which terminal it is talking to" { _ = Shell.setenv("TERM", "dumb", 1); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1739,7 +1733,7 @@ test "shell spawn uses an explicit OS directory longer than 256 bytes" { var explicit_buf: [4096]u8 = undefined; const explicit = try std.fmt.bufPrint(&explicit_buf, "/n/os{s}", .{directory}); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", explicit, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", explicit, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1768,7 +1762,7 @@ test "a terminal is where its shell is: under a wrapper the foreground shell, ne // A wrapper exec'd over the shell (timeout standing in for 9ns) runs // another, two directories down, in the foreground. { - const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1787,7 +1781,7 @@ test "a terminal is where its shell is: under a wrapper the foreground shell, ne // A job of the shell's own, gone into a/b, leaves the pane where the // shell is. { - const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1812,7 +1806,7 @@ test "shell cwd rejects truncation and preserves an owned child path longer than const directory = path_buf[0..try tmp.dir.realPathFile(io, nested, &path_buf)]; try std.testing.expect(directory.len > 1024); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1839,7 +1833,7 @@ test "a shell whose directory was removed is not named `... (deleted)`" { var path_buf: [4096]u8 = undefined; const directory = path_buf[0..try tmp.dir.realPathFile(io, "gone", &path_buf)]; const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1949,7 +1943,7 @@ test "a command pane's child is its shell running the one line, and its exit sta _ = try panes.newCommand(core, id, "", "printf 'said '; exit 7"); while (core.nextEffect()) |_| {} const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(core, id, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer _ = libc.close(child.file.handle); var got: [256]u8 = undefined; var n: usize = 0; @@ -1980,7 +1974,7 @@ test "a command's exit is its process's, told by its watcher, whether or not its const id = core.freeSlot().?; _ = try panes.newCommand(core, id, "", c.line); while (core.nextEffect()) |_| {} - const child = try forkShell(core, id, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer _ = libc.close(child.file.handle); try std.testing.expect(watchExit(child.pid)); var got: ?libc.pid_t = null; @@ -2021,7 +2015,7 @@ test "a command's background job outlives it and its pty, and Kill stops its who const id = core.freeSlot().?; _ = try panes.newCommand(core, id, dir, c.line); while (core.nextEffect()) |_| {} - const child = try forkShell(core, id, &rcs, "/bin/sh", dir, 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", dir, .{ .cols = 80, .rows = 24 }); if (c.kill) { sleepMs(150); signalTty(child.pid, child.file.handle, .term, true); |
