diff --git a/src/Surface.zig b/src/Surface.zig index 8c6064d8c..c8055cfee 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -36,6 +36,7 @@ const App = @import("App.zig"); const internal_os = @import("os/main.zig"); const inspectorpkg = @import("inspector/main.zig"); const SurfaceMouse = @import("surface_mouse.zig"); +const ProcessInfo = @import("pty.zig").ProcessInfo; const log = std.log.scoped(.surface); @@ -6342,29 +6343,11 @@ fn testMouseSelectionIsNull( ); } -pub const ProcessInfo = enum { - /// The PID of the process that currently controls the PTY. - foreground_pid, - /// Gets the name of the slave PTY. Returned name points to an internal - /// buffer so it should not be modified or freed. - tty_name, - - pub fn Type(comptime info: ProcessInfo) type { - return switch (info) { - .foreground_pid => u64, - .tty_name => [:0]const u8, - }; - } -}; - /// Get information about the process(es) running within the surface. Returns /// `null` if there was an error getting the information or the information is /// not available on a particular platform. pub fn getProcessInfo(self: *Surface, comptime info: ProcessInfo) ?ProcessInfo.Type(info) { - return switch (info) { - .foreground_pid => self.io.getProcessInfo(.foreground_pid), - .tty_name => self.io.getProcessInfo(.tty_name), - }; + return self.io.getProcessInfo(info); } test "Surface: selection logic" { diff --git a/src/build/SharedDeps.zig b/src/build/SharedDeps.zig index 463308ffe..1b24c7429 100644 --- a/src/build/SharedDeps.zig +++ b/src/build/SharedDeps.zig @@ -137,34 +137,27 @@ pub fn add( // C imports needed to manage/create PTYs switch (target.result.os.tag) { - .freebsd => { + .freebsd, + .linux, + .macos, + => { const c = b.addTranslateC(.{ - .root_source_file = b.path("src/pty/freebsd.c"), + .root_source_file = b.path("src/pty.c"), .target = target, .optimize = optimize, }); - step.root_module.addImport("pty-c", c.createModule()); - }, - .linux => { - const c = b.addTranslateC(.{ - .root_source_file = b.path("src/pty/linux.c"), - .target = target, - .optimize = optimize, - }); - step.root_module.addImport("pty-c", c.createModule()); - }, - .macos => { - const c = b.addTranslateC(.{ - .root_source_file = b.path("src/pty/macos.c"), - .target = target, - .optimize = optimize, - }); - const libc = try std.zig.LibCInstallation.findNative(.{ - .allocator = b.allocator, - .target = &target.result, - .verbose = false, - }); - c.addSystemIncludePath(.{ .cwd_relative = libc.sys_include_dir.? }); + c.defineCMacro("ZIG_TARGET", @tagName(target.result.os.tag)); + switch (target.result.os.tag) { + .macos => { + const libc = try std.zig.LibCInstallation.findNative(.{ + .allocator = b.allocator, + .target = &target.result, + .verbose = false, + }); + c.addSystemIncludePath(.{ .cwd_relative = libc.sys_include_dir.? }); + }, + else => {}, + } step.root_module.addImport("pty-c", c.createModule()); }, else => {}, diff --git a/src/pty.c b/src/pty.c new file mode 100644 index 000000000..f4f25ad5e --- /dev/null +++ b/src/pty.c @@ -0,0 +1,34 @@ +#if ZIG_TARGET == freebsd + #include // ioctl and constants + #include // openpty + #include // ptsname_r + #include // tcgetpgrp +#endif + +#if ZIG_TARGET == linux + #define _GNU_SOURCE // ptsname_r + #include // openpty + #include // ptsname_r + #include // ioctl and constants + #include // tcgetpgrp, setsid +#endif + +#if ZIG_TARGET == macos + #include // ioctl and constants + #include // ioctl and constants for TIOCPTYGNAME + #include + #include // tcgetpgrp + #include // openpty + + #ifndef tiocsctty + #define tiocsctty 536900705 + #endif + + #ifndef tiocswinsz + #define tiocswinsz 2148037735 + #endif + + #ifndef tiocgwinsz + #define tiocgwinsz 1074295912 + #endif +#endif diff --git a/src/pty.zig b/src/pty.zig index 21f2ba3cc..40277a8ba 100644 --- a/src/pty.zig +++ b/src/pty.zig @@ -36,6 +36,21 @@ pub const Mode = packed struct { echo: bool = true, }; +pub const ProcessInfo = enum { + /// The PID of the process that controls the PTY. + foreground_pid, + /// Gets the name of the slave PTY. Returned name points to an internal buffer + /// so it should not be modified or freed. + tty_name, + + pub fn Type(comptime info: ProcessInfo) type { + return switch (info) { + .foreground_pid => u64, + .tty_name => [:0]const u8, + }; + } +}; + // A pty implementation that does nothing. // // TODO: This should be removed. This is only temporary until we have @@ -80,21 +95,6 @@ const NullPty = struct { _ = self; } - pub const ProcessInfo = enum { - /// The PID of the process that controls the PTY. - foreground_pid, - /// Gets the name of the slave PTY. Returned name points to an internal buffer - /// so it should not be modified or freed. - tty_name, - - pub fn Type(comptime info: ProcessInfo) type { - return switch (info) { - .foreground_pid => u64, - .tty_name => [:0]const u8, - }; - } - }; - /// Get information about the process(es) attached to the PTY. Returns /// `null` if there was an error getting the information or the information /// is not available on a particular platform. @@ -264,21 +264,6 @@ const PosixPty = struct { posix.close(self.master); } - pub const ProcessInfo = enum { - /// The PID of the process that currently controls the PTY. - foreground_pid, - /// Gets the name of the slave PTY. Returned name points to an internal buffer - /// so it should not be modified or freed. - tty_name, - - pub fn Type(comptime info: ProcessInfo) type { - return switch (info) { - .foreground_pid => u64, - .tty_name => [:0]const u8, - }; - } - }; - /// Get information about the process(es) attached to the PTY. Returns /// `null` if there was an error getting the information or the information /// is not available on a particular platform. @@ -484,21 +469,6 @@ const WindowsPty = struct { self.size = size; } - pub const ProcessInfo = enum { - /// The PID of the process that currently controls the PTY. - foreground_pid, - /// Gets the name of the slave PTY. Returned name points to an internal - /// buffer so it should not be modified or freed. - tty_name, - - pub fn Type(comptime info: ProcessInfo) type { - return switch (info) { - .foreground_pid => u64, - .tty_name => []const u8, - }; - } - }; - /// Get information about the process(es) attached to the PTY. Returns /// `null` if there was an error getting the information or the information /// is not available on a particular platform. diff --git a/src/pty/freebsd.c b/src/pty/freebsd.c deleted file mode 100644 index fbca41852..000000000 --- a/src/pty/freebsd.c +++ /dev/null @@ -1,4 +0,0 @@ -#include // ioctl and constants -#include // openpty -#include // ptsname_r -#include // tcgetpgrp diff --git a/src/pty/linux.c b/src/pty/linux.c deleted file mode 100644 index 2625c62dc..000000000 --- a/src/pty/linux.c +++ /dev/null @@ -1,5 +0,0 @@ -#define _GNU_SOURCE // ptsname_r -#include // openpty -#include // ptsname_r -#include // ioctl and constants -#include // tcgetpgrp, setsid diff --git a/src/pty/macos.c b/src/pty/macos.c deleted file mode 100644 index b0125df69..000000000 --- a/src/pty/macos.c +++ /dev/null @@ -1,17 +0,0 @@ -#include // ioctl and constants -#include // ioctl and constants for TIOCPTYGNAME -#include -#include // tcgetpgrp -#include // openpty - -#ifndef TIOCSCTTY -#define TIOCSCTTY 536900705 -#endif - -#ifndef TIOCSWINSZ -#define TIOCSWINSZ 2148037735 -#endif - -#ifndef TIOCGWINSZ -#define TIOCGWINSZ 1074295912 -#endif diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 15a892620..0f35b5787 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -27,6 +27,7 @@ const Pty = ptypkg.Pty; const EnvMap = std.process.EnvMap; const PasswdEntry = internal_os.passwd.Entry; const windows = internal_os.windows; +const ProcessInfo = @import("../pty.zig").ProcessInfo; const log = std.log.scoped(.io_exec); @@ -1227,30 +1228,12 @@ const Subprocess = struct { try command.signal(c.SIGHUP, true); } - pub const ProcessInfo = enum { - /// The PID of the process that currently controls the PTY. - foreground_pid, - /// Gets the name of the slave PTY. Returned name points to an internal - /// buffer so it should not be modified or freed. - tty_name, - - pub fn Type(comptime info: Subprocess.ProcessInfo) type { - return switch (info) { - .foreground_pid => u64, - .tty_name => [:0]const u8, - }; - } - }; - /// Get information about the process(es) running within the subprocess. /// Returns `null` if there was an error getting the information or the /// information is not available on a particular platform. - pub fn getProcessInfo(self: *Subprocess, comptime info: Subprocess.ProcessInfo) ?Subprocess.ProcessInfo.Type(info) { + pub fn getProcessInfo(self: *Subprocess, comptime info: ProcessInfo) ?ProcessInfo.Type(info) { const pty = &(self.pty orelse return null); - return switch (info) { - .foreground_pid => pty.getProcessInfo(.foreground_pid), - .tty_name => pty.getProcessInfo(.tty_name), - }; + return pty.getProcessInfo(info); } }; @@ -1606,29 +1589,11 @@ fn execCommand( }; } -pub const ProcessInfo = enum { - /// The PID of the process that currently controls the PTY. - foreground_pid, - /// Gets the name of the slave PTY. Returned name points to an internal - /// buffer so it should not be modified or freed. - tty_name, - - pub fn Type(comptime info: ProcessInfo) type { - return switch (info) { - .foreground_pid => u64, - .tty_name => [:0]const u8, - }; - } -}; - /// Get information about the process(es) running within the backend. Returns /// `null` if there was an error getting the information or the information is /// not available on a particular platform. pub fn getProcessInfo(self: *Exec, comptime info: ProcessInfo) ?ProcessInfo.Type(info) { - return switch (info) { - .foreground_pid => self.subprocess.getProcessInfo(.foreground_pid), - .tty_name => self.subprocess.getProcessInfo(.tty_name), - }; + return self.subprocess.getProcessInfo(info); } test "execCommand darwin: shell command" { diff --git a/src/termio/Termio.zig b/src/termio/Termio.zig index aab275605..f05379077 100644 --- a/src/termio/Termio.zig +++ b/src/termio/Termio.zig @@ -19,6 +19,7 @@ const apprt = @import("../apprt.zig"); const internal_os = @import("../os/main.zig"); const windows = internal_os.windows; const configpkg = @import("../config.zig"); +const ProcessInfo = @import("../pty.zig").ProcessInfo; const log = std.log.scoped(.io_exec); @@ -765,27 +766,9 @@ pub const ThreadData = struct { } }; -pub const ProcessInfo = enum { - /// The PID of the process that currently controls the PTY. - foreground_pid, - /// Gets the name of the slave PTY. Returned name points to an internal - /// buffer so it should not be modified or freed. - tty_name, - - pub fn Type(comptime info: ProcessInfo) type { - return switch (info) { - .foreground_pid => u64, - .tty_name => [:0]const u8, - }; - } -}; - /// Get information about the process(es) attached to the backend. Returns /// `null` if there was an error getting the information or the information is /// not available on a particular platform. pub fn getProcessInfo(self: *Termio, comptime info: ProcessInfo) ?ProcessInfo.Type(info) { - return switch (info) { - .foreground_pid => self.backend.getProcessInfo(.foreground_pid), - .tty_name => self.backend.getProcessInfo(.tty_name), - }; + return self.backend.getProcessInfo(info); } diff --git a/src/termio/backend.zig b/src/termio/backend.zig index ef8ac22e6..c29009acb 100644 --- a/src/termio/backend.zig +++ b/src/termio/backend.zig @@ -4,6 +4,7 @@ const posix = std.posix; const renderer = @import("../renderer.zig"); const terminal = @import("../terminal/main.zig"); const termio = @import("../termio.zig"); +const ProcessInfo = @import("../pty.zig").ProcessInfo; // The preallocation size for the write request pool. This should be big // enough to satisfy most write requests. It must be a power of 2. @@ -101,30 +102,12 @@ pub const Backend = union(Kind) { } } - pub const ProcessInfo = enum { - /// The PID of the process that currently controls the PTY. - foreground_pid, - /// Gets the name of the slave PTY. Returned name points to an internal - /// buffer so it should not be modified or freed. - tty_name, - - pub fn Type(comptime info: ProcessInfo) type { - return switch (info) { - .foreground_pid => u64, - .tty_name => [:0]const u8, - }; - } - }; - /// Get information about the process(es) attached to the backend. Returns /// `null` if there was an error getting the information or the information /// is not available on a particular platform. pub fn getProcessInfo(self: *Backend, comptime info: ProcessInfo) ?ProcessInfo.Type(info) { return switch (self.*) { - .exec => |*exec| switch (info) { - .foreground_pid => exec.getProcessInfo(.foreground_pid), - .tty_name => exec.getProcessInfo(.tty_name), - }, + .exec => |*exec| exec.getProcessInfo(info), }; } };