Commit 47643cc5cc

Andrew Kelley <andrew@ziglang.org>
2024-02-13 00:43:51
std.os.termios: add type safety to iflag field
This creates `tc_iflag_t` even though such a type is not defined by libc. I also collected the missing flag bits from all the operating systems.
1 parent 0c88f92
lib/std/c/darwin.zig
@@ -2692,21 +2692,6 @@ pub const SHUT = struct {
     pub const RDWR = 2;
 };
 
-pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
-pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINTR
-pub const IGNPAR: tcflag_t = 0x00000004; // ignore (discard) parity errors
-pub const PARMRK: tcflag_t = 0x00000008; // mark parity and framing errors
-pub const INPCK: tcflag_t = 0x00000010; // enable checking of parity errors
-pub const ISTRIP: tcflag_t = 0x00000020; // strip 8th bit off chars
-pub const INLCR: tcflag_t = 0x00000040; // map NL into CR
-pub const IGNCR: tcflag_t = 0x00000080; // ignore CR
-pub const ICRNL: tcflag_t = 0x00000100; // map CR to NL (ala CRMOD)
-pub const IXON: tcflag_t = 0x00000200; // enable output flow control
-pub const IXOFF: tcflag_t = 0x00000400; // enable input flow control
-pub const IXANY: tcflag_t = 0x00000800; // any char will restart after stop
-pub const IMAXBEL: tcflag_t = 0x00002000; // ring bell on input queue full
-pub const IUTF8: tcflag_t = 0x00004000; // maintain state for UTF-8 VERASE
-
 pub const OPOST: tcflag_t = 0x00000001; //enable following output processing
 pub const ONLCR: tcflag_t = 0x00000002; // map NL to CR-NL (ala CRMOD)
 pub const OXTABS: tcflag_t = 0x00000004; // expand tabs to spaces
lib/std/c/netbsd.zig
@@ -806,20 +806,6 @@ pub const T = struct {
     pub const IOCXMTFRAME = 0x80087444;
 };
 
-// Input flags - software input processing
-pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
-pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINT
-pub const IGNPAR: tcflag_t = 0x00000004; // ignore (discard) parity errors
-pub const PARMRK: tcflag_t = 0x00000008; // mark parity and framing errors
-pub const INPCK: tcflag_t = 0x00000010; // enable checking of parity errors
-pub const ISTRIP: tcflag_t = 0x00000020; // strip 8th bit off chars
-pub const INLCR: tcflag_t = 0x00000040; // map NL into CR
-pub const IGNCR: tcflag_t = 0x00000080; // ignore CR
-pub const ICRNL: tcflag_t = 0x00000100; // map CR to NL (ala CRMOD)
-pub const IXON: tcflag_t = 0x00000200; // enable output flow control
-pub const IXOFF: tcflag_t = 0x00000400; // enable input flow control
-pub const IXANY: tcflag_t = 0x00000800; // any char will restart after stop
-pub const IMAXBEL: tcflag_t = 0x00002000; // ring bell on input queue full
 
 // Output flags - software output processing
 pub const OPOST: tcflag_t = 0x00000001; // enable following output processing
lib/std/c/openbsd.zig
@@ -768,21 +768,6 @@ pub const AUTH = struct {
     pub const ALLOW: c_int = (OKAY | ROOTOKAY | SECURE);
 };
 
-// Input flags - software input processing
-pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
-pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINT
-pub const IGNPAR: tcflag_t = 0x00000004; // ignore (discard) parity errors
-pub const PARMRK: tcflag_t = 0x00000008; // mark parity and framing errors
-pub const INPCK: tcflag_t = 0x00000010; // enable checking of parity errors
-pub const ISTRIP: tcflag_t = 0x00000020; // strip 8th bit off chars
-pub const INLCR: tcflag_t = 0x00000040; // map NL into CR
-pub const IGNCR: tcflag_t = 0x00000080; // ignore CR
-pub const ICRNL: tcflag_t = 0x00000100; // map CR to NL (ala CRMOD)
-pub const IXON: tcflag_t = 0x00000200; // enable output flow control
-pub const IXOFF: tcflag_t = 0x00000400; // enable input flow control
-pub const IXANY: tcflag_t = 0x00000800; // any char will restart after stop
-pub const IUCLC: tcflag_t = 0x00001000; // translate upper to lower case
-pub const IMAXBEL: tcflag_t = 0x00002000; // ring bell on input queue full
 
 // Output flags - software output processing
 pub const OPOST: tcflag_t = 0x00000001; // enable following output processing
lib/std/os/linux.zig
@@ -5005,7 +5005,6 @@ pub const rusage = extern struct {
 };
 
 pub const speed_t = u32;
-pub const tcflag_t = u32;
 
 pub const NCCS = switch (native_arch) {
     .powerpc, .powerpcle, .powerpc64, .powerpc64le => 19,
@@ -5045,23 +5044,43 @@ pub const B3000000 = 0o0010015;
 pub const B3500000 = 0o0010016;
 pub const B4000000 = 0o0010017;
 
-pub const tc_iflag_t = packed struct(u32) {
-    IGNBRK: bool = false,
-    BRKINT: bool = false,
-    IGNPAR: bool = false,
-    PARMRK: bool = false,
-    INPCK: bool = false,
-    ISTRIP: bool = false,
-    INLCR: bool = false,
-    IGNCR: bool = false,
-    ICRNL: bool = false,
-    IUCLC: bool = false,
-    IXON: bool = false,
-    IXANY: bool = false,
-    IXOFF: bool = false,
-    IMAXBEL: bool = false,
-    IUTF8: bool = false,
-    _: u17 = 0,
+pub const tc_iflag_t = switch (native_arch) {
+    .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
+        IGNBRK: bool = false,
+        BRKINT: bool = false,
+        IGNPAR: bool = false,
+        PARMRK: bool = false,
+        INPCK: bool = false,
+        ISTRIP: bool = false,
+        INLCR: bool = false,
+        IGNCR: bool = false,
+        ICRNL: bool = false,
+        IXON: bool = false,
+        IXOFF: bool = false,
+        IXANY: bool = false,
+        IUCLC: bool = false,
+        IMAXBEL: bool = false,
+        IUTF8: bool = false,
+        _: u17 = 0,
+    },
+    else => packed struct(u32) {
+        IGNBRK: bool = false,
+        BRKINT: bool = false,
+        IGNPAR: bool = false,
+        PARMRK: bool = false,
+        INPCK: bool = false,
+        ISTRIP: bool = false,
+        INLCR: bool = false,
+        IGNCR: bool = false,
+        ICRNL: bool = false,
+        IUCLC: bool = false,
+        IXON: bool = false,
+        IXANY: bool = false,
+        IXOFF: bool = false,
+        IMAXBEL: bool = false,
+        IUTF8: bool = false,
+        _: u17 = 0,
+    },
 };
 
 pub const cc_t = switch (native_arch) {
@@ -5124,6 +5143,8 @@ pub const cc_t = switch (native_arch) {
     },
 };
 
+pub const tcflag_t = u32;
+
 pub const OPOST: tcflag_t = 1;
 pub const OLCUC: tcflag_t = 2;
 pub const ONLCR: tcflag_t = 4;
@@ -5168,7 +5189,7 @@ pub const TCSA = enum(c_uint) {
 
 pub const termios = switch (native_arch) {
     .powerpc, .powerpcle, .powerpc64, .powerpc64le => extern struct {
-        iflag: tcflag_t,
+        iflag: tc_iflag_t,
         oflag: tcflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
@@ -5178,7 +5199,7 @@ pub const termios = switch (native_arch) {
         ospeed: speed_t,
     },
     else => extern struct {
-        iflag: tcflag_t,
+        iflag: tc_iflag_t,
         oflag: tcflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
lib/std/c.zig
@@ -793,7 +793,7 @@ pub const NCCS = switch (native_os) {
 pub const termios = switch (native_os) {
     .linux => std.os.linux.termios,
     .macos, .ios, .tvos, .watchos => extern struct {
-        iflag: tcflag_t,
+        iflag: tc_iflag_t,
         oflag: tcflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
@@ -802,7 +802,7 @@ pub const termios = switch (native_os) {
         ospeed: speed_t,
     },
     .freebsd, .kfreebsd, .netbsd, .dragonfly, .openbsd => extern struct {
-        iflag: tcflag_t,
+        iflag: tc_iflag_t,
         oflag: tcflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
@@ -811,7 +811,7 @@ pub const termios = switch (native_os) {
         ospeed: speed_t,
     },
     .haiku => extern struct {
-        iflag: tcflag_t,
+        iflag: tc_iflag_t,
         oflag: tcflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
@@ -821,14 +821,14 @@ pub const termios = switch (native_os) {
         cc: [NCCS]cc_t,
     },
     .solaris, .illumos => extern struct {
-        iflag: tcflag_t,
+        iflag: tc_iflag_t,
         oflag: tcflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
         cc: [NCCS]cc_t,
     },
     .emscripten, .wasi => extern struct {
-        iflag: tcflag_t,
+        iflag: tc_iflag_t,
         oflag: tcflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
@@ -840,6 +840,116 @@ pub const termios = switch (native_os) {
     else => @compileError("target libc does not have termios"),
 };
 
+pub const tc_iflag_t = switch (native_os) {
+    .linux => std.os.linux.tc_iflag_t,
+    .macos, .ios, .tvos, .watchos => packed struct(u32) {
+        IGNBRK: bool = false,
+        BRKINT: bool = false,
+        IGNPAR: bool = false,
+        PARMRK: bool = false,
+        INPCK: bool = false,
+        ISTRIP: bool = false,
+        INLCR: bool = false,
+        IGNCR: bool = false,
+        ICRNL: bool = false,
+        IXON: bool = false,
+        IXOFF: bool = false,
+        IXANY: bool = false,
+        _12: u1 = 0,
+        IMAXBEL: bool = false,
+        IUTF8: bool = false,
+        _: u17 = 0,
+    },
+    .netbsd, .freebsd, .kfreebsd, .dragonfly => packed struct(u32) {
+        IGNBRK: bool = false,
+        BRKINT: bool = false,
+        IGNPAR: bool = false,
+        PARMRK: bool = false,
+        INPCK: bool = false,
+        ISTRIP: bool = false,
+        INLCR: bool = false,
+        IGNCR: bool = false,
+        ICRNL: bool = false,
+        IXON: bool = false,
+        IXOFF: bool = false,
+        IXANY: bool = false,
+        _12: u1 = 0,
+        IMAXBEL: bool = false,
+        _: u18 = 0,
+    },
+    .openbsd => packed struct(u32) {
+        IGNBRK: bool = false,
+        BRKINT: bool = false,
+        IGNPAR: bool = false,
+        PARMRK: bool = false,
+        INPCK: bool = false,
+        ISTRIP: bool = false,
+        INLCR: bool = false,
+        IGNCR: bool = false,
+        ICRNL: bool = false,
+        IXON: bool = false,
+        IXOFF: bool = false,
+        IXANY: bool = false,
+        IUCLC: bool = false,
+        IMAXBEL: bool = false,
+        _: u18 = 0,
+    },
+    .haiku => packed struct(u32) {
+        IGNBRK: bool = false,
+        BRKINT: bool = false,
+        IGNPAR: bool = false,
+        PARMRK: bool = false,
+        INPCK: bool = false,
+        ISTRIP: bool = false,
+        INLCR: bool = false,
+        IGNCR: bool = false,
+        ICRNL: bool = false,
+        IUCLC: bool = false,
+        IXON: bool = false,
+        IXANY: bool = false,
+        IXOFF: bool = false,
+        _: u19 = 0,
+    },
+    .solaris, .illumos => packed struct(u32) {
+        IGNBRK: bool = false,
+        BRKINT: bool = false,
+        IGNPAR: bool = false,
+        PARMRK: bool = false,
+        INPCK: bool = false,
+        ISTRIP: bool = false,
+        INLCR: bool = false,
+        IGNCR: bool = false,
+        ICRNL: bool = false,
+        IUCLC: bool = false,
+        IXON: bool = false,
+        IXANY: bool = false,
+        _12: u1 = 0,
+        IMAXBEL: bool = false,
+        _14: u1 = 0,
+        DOSMODE: bool = false,
+        _: u16 = 0,
+    },
+    .emscripten, .wasi => packed struct(u32) {
+        IGNBRK: bool = false,
+        BRKINT: bool = false,
+        IGNPAR: bool = false,
+        PARMRK: bool = false,
+        INPCK: bool = false,
+        ISTRIP: bool = false,
+        INLCR: bool = false,
+        IGNCR: bool = false,
+        ICRNL: bool = false,
+        IUCLC: bool = false,
+        IXON: bool = false,
+        IXANY: bool = false,
+        IXOFF: bool = false,
+        IMAXBEL: bool = false,
+        IUTF8: bool = false,
+        _: u17 = 0,
+    },
+    else => @compileError("target libc does not have tc_iflag_t"),
+};
+
 pub const tcflag_t = switch (native_os) {
     .linux => std.os.linux.tcflag_t,
     .macos, .ios, .tvos, .watchos => u64,
lib/std/os.zig
@@ -106,7 +106,6 @@ pub const MFD = system.MFD;
 pub const MMAP2_UNIT = system.MMAP2_UNIT;
 pub const MSG = system.MSG;
 pub const NAME_MAX = system.NAME_MAX;
-pub const NCCS = system.NCCS;
 pub const O = system.O;
 pub const PATH_MAX = system.PATH_MAX;
 pub const POLL = system.POLL;
@@ -173,9 +172,7 @@ pub const siginfo_t = system.siginfo_t;
 pub const sigset_t = system.sigset_t;
 pub const sockaddr = system.sockaddr;
 pub const socklen_t = system.socklen_t;
-pub const speed_t = system.speed_t;
 pub const stack_t = system.stack_t;
-pub const tcflag_t = system.tcflag_t;
 pub const termios = system.termios;
 pub const time_t = system.time_t;
 pub const timespec = system.timespec;
@@ -187,6 +184,11 @@ pub const uid_t = system.uid_t;
 pub const user_desc = system.user_desc;
 pub const utsname = system.utsname;
 
+pub const NCCS = system.NCCS;
+pub const speed_t = system.speed_t;
+pub const tcflag_t = system.tcflag_t;
+pub const tc_iflag_t = system.tc_iflag_t;
+
 pub const F_OK = system.F_OK;
 pub const R_OK = system.R_OK;
 pub const W_OK = system.W_OK;