diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 15:19:25 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 17:02:40 -0300 |
| commit | 08c9415dde1a1d5da2c20124c4fcd4c92783b290 (patch) | |
| tree | 62c344cba45a989076bfa99eb4d2d2cf8dadc599 /test/kitty_client.zig | |
| parent | effc6d5c1da5c8e6ec9de3f201a0aa1211d3ffb2 (diff) | |
| download | pardes-08c9415dde1a1d5da2c20124c4fcd4c92783b290.tar.gz pardes-08c9415dde1a1d5da2c20124c4fcd4c92783b290.zip | |
kitty_client: the kernel's termios ioctls on Linux: std.posix.tcgetattr calls glibc's in a program linking libc, which writes its 60-byte termios into Zig's 44-byte one, 16 bytes past (main.zig exports kernel ones; this program has none)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'test/kitty_client.zig')
| -rw-r--r-- | test/kitty_client.zig | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/test/kitty_client.zig b/test/kitty_client.zig index 0d84f374..03ad629e 100644 --- a/test/kitty_client.zig +++ b/test/kitty_client.zig @@ -17,6 +17,23 @@ pub const side = 64; pub const colour = [3]u8{ 13, 77, 231 }; const sync = "\x1b_Gi=999,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\"; +// The kernel's ioctls on Linux: glibc's tcgetattr writes its 60-byte +// termios into Zig's 44-byte kernel one, and std.posix's calls glibc's in a +// program linking libc (src/main.zig exports kernel ones; this has none). +const linux = @import("builtin").os.tag == .linux; + +fn getTermios() !std.posix.termios { + if (!linux) return std.posix.tcgetattr(0) catch error.NotATerminal; + var t: std.os.linux.termios = undefined; + if (std.os.linux.errno(std.os.linux.tcgetattr(0, &t)) != .SUCCESS) return error.NotATerminal; + return t; +} + +fn setTermios(t: std.posix.termios) void { + if (!linux) return std.posix.tcsetattr(0, .NOW, t) catch {}; + _ = std.os.linux.tcsetattr(0, .NOW, &t); +} + pub fn main(init: std.process.Init) !void { var arena_state: std.heap.ArenaAllocator = .init(std.heap.page_allocator); defer arena_state.deinit(); @@ -26,16 +43,14 @@ pub fn main(init: std.process.Init) !void { const case = args[1]; const out = try arena.dupeSentinel(u8, args[2], 0); - // std.posix, not std.c: glibc's tcgetattr writes its 60-byte termios - // into Zig's 44-byte kernel one. - const saved = std.posix.tcgetattr(0) catch return error.NotATerminal; + const saved = try getTermios(); var raw = saved; raw.lflag.ICANON = false; raw.lflag.ECHO = false; raw.cc[@backingInt(std.posix.V.MIN)] = 0; raw.cc[@backingInt(std.posix.V.TIME)] = 1; - std.posix.tcsetattr(0, .NOW, raw) catch {}; - defer std.posix.tcsetattr(0, .NOW, saved) catch {}; + setTermios(raw); + defer setTermios(saved); var cmds: std.ArrayList(u8) = .empty; try play(arena, case, &cmds); |
