From 045955d5db1fe146daf6768be28f85575e23c13b Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Wed, 10 Jul 2024 22:39:45 +0200 Subject: [PATCH] wip: macos --- examples/vt.zig | 2 +- src/widgets/terminal/Command.zig | 43 ++++++++++++++++++++----------- src/widgets/terminal/Pty.zig | 2 +- src/widgets/terminal/Terminal.zig | 3 +++ 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/examples/vt.zig b/examples/vt.zig index 4b1a570..fb9d618 100644 --- a/examples/vt.zig +++ b/examples/vt.zig @@ -49,6 +49,7 @@ pub fn main() !void { }; const shell = env.get("SHELL") orelse "bash"; const argv = [_][]const u8{shell}; + std.debug.print("executing: {s}\n", .{shell}); var vt = try vaxis.widgets.Terminal.init( alloc, &argv, @@ -62,7 +63,6 @@ pub fn main() !void { var redraw: bool = false; while (true) { std.debug.print("inside while loop before resize\n", .{}); - std.time.sleep(8 * std.time.ns_per_ms); // try vt events first while (vt.tryEvent()) |event| { std.debug.print("inside stryEventloop \n", .{}); diff --git a/src/widgets/terminal/Command.zig b/src/widgets/terminal/Command.zig index 3681028..0638fb6 100644 --- a/src/widgets/terminal/Command.zig +++ b/src/widgets/terminal/Command.zig @@ -20,12 +20,13 @@ pty: Pty, const TIOCSCTTY = if (builtin.os.tag == .macos) 536900705 else c.TIOCSCTTY; const TIOCSWINSZ = if (builtin.os.tag == .macos) 2148037735 else c.TIOCSWINSZ; const TIOCGWINSZ = if (builtin.os.tag == .macos) 1074295912 else c.TIOCGWINSZ; -extern "c" fn setsid() std.c.pid_t; +// extern "c" fn setsid() std.c.pid_t; const c = struct { usingnamespace switch (builtin.os.tag) { .macos => @cImport({ @cInclude("sys/ioctl.h"); // ioctl and constants - @cInclude("util.h"); // openpty() + @cInclude("util.h"); + @cInclude("unistd.h"); // openpty() }), else => @cImport({ @cInclude("sys/ioctl.h"); // ioctl and constants @@ -47,15 +48,27 @@ pub fn spawn(self: *Command, allocator: std.mem.Allocator) !void { const pid = try std.posix.fork(); if (pid == 0) { - // we are the child - _ = std.os.linux.setsid(); - _ = setsid(); - // if (setsid() < 0) return error.ProcessGroupFailed; - - // set the controlling terminal + std.posix.close(self.pty.pty); + std.debug.print("inside child\n", .{}); + if (c.setsid() != 0) return error.SetSid; + std.debug.print("setting up io for tty\n", .{}); var u: c_uint = std.posix.STDIN_FILENO; + if (c.ioctl(self.pty.tty, TIOCSCTTY, @intFromPtr(&u)) != 0) return error.IoctlError; + // switch (builtin.os.tag) { + // .linux => {}, + // .ios, .macos => { + // // Mac doesn't support dup3 so we use dup2. We purposely clear + // // CLO_ON_EXEC for this fd. + // const flags = try posix.fcntl(self.pty.tty, posix.F.GETFD, 0); + // if (flags & posix.FD_CLOEXEC != 0) { + // _ = try posix.fcntl(self.pty.tty, posix.F.SETFD, flags & ~@as(u32, posix.FD_CLOEXEC)); + // } + // }, + // else => @compileError("unsupported platform"), + // } + // set up io try posix.dup2(self.pty.tty, std.posix.STDIN_FILENO); try posix.dup2(self.pty.tty, std.posix.STDOUT_FILENO); @@ -69,17 +82,17 @@ pub fn spawn(self: *Command, allocator: std.mem.Allocator) !void { } // exec - const err = std.posix.execvpeZ(argv_buf.ptr[0].?, argv_buf.ptr, envp); - _ = err catch {}; - } else { - std.debug.print("we are not child: {d}\n", .{pid}); - // posix.close(self.pty.tty); - _ = posix.waitpid(pid, 0); + _ = std.posix.execvpeZ(argv_buf.ptr[0].?, argv_buf.ptr, envp) catch null; + + //return error.ExecFailed; + // const err = std.posix.execvpeZ("/bin/zsh", "/bin/zsh", envp); + // _ = err catch {}; } // we are the parent self.pid = @intCast(pid); - + _ = posix.waitpid(pid, 0); + std.debug.print("waitpid in parent\n", .{}); if (!Terminal.global_sigchild_installed) { Terminal.global_sigchild_installed = true; var act = posix.Sigaction{ diff --git a/src/widgets/terminal/Pty.zig b/src/widgets/terminal/Pty.zig index c3a046a..892bebb 100644 --- a/src/widgets/terminal/Pty.zig +++ b/src/widgets/terminal/Pty.zig @@ -33,7 +33,7 @@ tty: posix.fd_t, pub fn init() !Pty { switch (builtin.os.tag) { .linux => return openPtyLinux(), - .macos => return openPtyMacos2(), + .macos => return openPtyMacos(), else => @compileError("unsupported os"), } } diff --git a/src/widgets/terminal/Terminal.zig b/src/widgets/terminal/Terminal.zig index 9208eb7..b2983c0 100644 --- a/src/widgets/terminal/Terminal.zig +++ b/src/widgets/terminal/Terminal.zig @@ -94,6 +94,9 @@ pub fn init( unicode: *const vaxis.Unicode, opts: Options, ) !Terminal { + if (opts.initial_working_directory) |pwd| { + if (!std.fs.path.isAbsolute(pwd)) return error.InvalidWorkingDirectory; + } const pty = try Pty.init(); try pty.setSize(opts.winsize); std.debug.print("set size done\n", .{});