core: address getProcessInfo feedback

* consolidate *.c files into a single file
* consolidate ProcessInfo enums into a single enum
This commit is contained in:
Jeffrey C. Ollie
2026-03-19 11:41:14 -05:00
parent 264a1a7cdd
commit 2ea6029c7a
10 changed files with 76 additions and 191 deletions

View File

@@ -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" {

View File

@@ -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 => {},

34
src/pty.c Normal file
View File

@@ -0,0 +1,34 @@
#if ZIG_TARGET == freebsd
#include <termios.h> // ioctl and constants
#include <libutil.h> // openpty
#include <stdlib.h> // ptsname_r
#include <unistd.h> // tcgetpgrp
#endif
#if ZIG_TARGET == linux
#define _GNU_SOURCE // ptsname_r
#include <pty.h> // openpty
#include <stdlib.h> // ptsname_r
#include <sys/ioctl.h> // ioctl and constants
#include <unistd.h> // tcgetpgrp, setsid
#endif
#if ZIG_TARGET == macos
#include <sys/ioctl.h> // ioctl and constants
#include <sys/ttycom.h> // ioctl and constants for TIOCPTYGNAME
#include <sys/types.h>
#include <unistd.h> // tcgetpgrp
#include <util.h> // openpty
#ifndef tiocsctty
#define tiocsctty 536900705
#endif
#ifndef tiocswinsz
#define tiocswinsz 2148037735
#endif
#ifndef tiocgwinsz
#define tiocgwinsz 1074295912
#endif
#endif

View File

@@ -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.

View File

@@ -1,4 +0,0 @@
#include <termios.h> // ioctl and constants
#include <libutil.h> // openpty
#include <stdlib.h> // ptsname_r
#include <unistd.h> // tcgetpgrp

View File

@@ -1,5 +0,0 @@
#define _GNU_SOURCE // ptsname_r
#include <pty.h> // openpty
#include <stdlib.h> // ptsname_r
#include <sys/ioctl.h> // ioctl and constants
#include <unistd.h> // tcgetpgrp, setsid

View File

@@ -1,17 +0,0 @@
#include <sys/ioctl.h> // ioctl and constants
#include <sys/ttycom.h> // ioctl and constants for TIOCPTYGNAME
#include <sys/types.h>
#include <unistd.h> // tcgetpgrp
#include <util.h> // openpty
#ifndef TIOCSCTTY
#define TIOCSCTTY 536900705
#endif
#ifndef TIOCSWINSZ
#define TIOCSWINSZ 2148037735
#endif
#ifndef TIOCGWINSZ
#define TIOCGWINSZ 1074295912
#endif

View File

@@ -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" {

View File

@@ -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);
}

View File

@@ -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),
};
}
};