Commit a280ff2767

Andrew Kelley <andrew@ziglang.org>
2024-02-13 05:21:45
std.os.termios: add type safety to lflag field
This creates `tc_cflag_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 e97fa8b
Changed files (4)
lib/std/c/darwin.zig
@@ -2692,28 +2692,6 @@ pub const SHUT = struct {
     pub const RDWR = 2;
 };
 
-pub const ECHOKE: tcflag_t = 0x00000001; // visual erase for line kill
-pub const ECHOE: tcflag_t = 0x00000002; // visually erase chars
-pub const ECHOK: tcflag_t = 0x00000004; // echo NL after line kill
-pub const ECHO: tcflag_t = 0x00000008; // enable echoing
-pub const ECHONL: tcflag_t = 0x00000010; // echo NL even if ECHO is off
-pub const ECHOPRT: tcflag_t = 0x00000020; // visual erase mode for hardcopy
-pub const ECHOCTL: tcflag_t = 0x00000040; // echo control chars as ^(Char)
-pub const ISIG: tcflag_t = 0x00000080; // enable signals INTR, QUIT, [D]SUSP
-pub const ICANON: tcflag_t = 0x00000100; // canonicalize input lines
-pub const ALTWERASE: tcflag_t = 0x00000200; // use alternate WERASE algorithm
-pub const IEXTEN: tcflag_t = 0x00000400; // enable DISCARD and LNEXT
-pub const EXTPROC: tcflag_t = 0x00000800; // external processing
-pub const TOSTOP: tcflag_t = 0x00400000; // stop background jobs from output
-pub const FLUSHO: tcflag_t = 0x00800000; // output being flushed (state)
-pub const NOKERNINFO: tcflag_t = 0x02000000; // no kernel output from VSTATUS
-pub const PENDIN: tcflag_t = 0x20000000; // XXX retype pending input (state)
-pub const NOFLSH: tcflag_t = 0x80000000; // don't flush after interrupt
-
-pub const TCSANOW: tcflag_t = 0; // make change immediate
-pub const TCSADRAIN: tcflag_t = 1; // drain output, then change
-pub const TCSAFLUSH: tcflag_t = 2; // drain output, flush input
-pub const TCSASOFT: tcflag_t = 0x10; // flag - don't alter h.w. state
 pub const TCSA = enum(c_uint) {
     NOW,
     DRAIN,
@@ -2721,40 +2699,6 @@ pub const TCSA = enum(c_uint) {
     _,
 };
 
-pub const B0: tcflag_t = 0;
-pub const B50: tcflag_t = 50;
-pub const B75: tcflag_t = 75;
-pub const B110: tcflag_t = 110;
-pub const B134: tcflag_t = 134;
-pub const B150: tcflag_t = 150;
-pub const B200: tcflag_t = 200;
-pub const B300: tcflag_t = 300;
-pub const B600: tcflag_t = 600;
-pub const B1200: tcflag_t = 1200;
-pub const B1800: tcflag_t = 1800;
-pub const B2400: tcflag_t = 2400;
-pub const B4800: tcflag_t = 4800;
-pub const B9600: tcflag_t = 9600;
-pub const B19200: tcflag_t = 19200;
-pub const B38400: tcflag_t = 38400;
-pub const B7200: tcflag_t = 7200;
-pub const B14400: tcflag_t = 14400;
-pub const B28800: tcflag_t = 28800;
-pub const B57600: tcflag_t = 57600;
-pub const B76800: tcflag_t = 76800;
-pub const B115200: tcflag_t = 115200;
-pub const B230400: tcflag_t = 230400;
-pub const EXTA: tcflag_t = 19200;
-pub const EXTB: tcflag_t = 38400;
-
-pub const TCIFLUSH: tcflag_t = 1;
-pub const TCOFLUSH: tcflag_t = 2;
-pub const TCIOFLUSH: tcflag_t = 3;
-pub const TCOOFF: tcflag_t = 1;
-pub const TCOON: tcflag_t = 2;
-pub const TCIOFF: tcflag_t = 3;
-pub const TCION: tcflag_t = 4;
-
 pub const winsize = extern struct {
     ws_row: u16,
     ws_col: u16,
lib/std/os/linux.zig
@@ -5147,6 +5147,53 @@ pub const tc_cflag_t = switch (native_arch) {
     },
 };
 
+pub const tc_lflag_t = switch (native_arch) {
+    .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
+        _0: u1 = 0,
+        ECHOE: bool = false,
+        ECHOK: bool = false,
+        ECHO: bool = false,
+        ECHONL: bool = false,
+        _5: u2 = 0,
+        ISIG: bool = false,
+        ICANON: bool = false,
+        _9: u1 = 0,
+        IEXTEN: bool = false,
+        _11: u11 = 0,
+        TOSTOP: bool = false,
+        _23: u8 = 0,
+        NOFLSH: bool = false,
+    },
+    .mips, .mipsel, .mips64, .mips64el => packed struct(u32) {
+        ISIG: bool = false,
+        ICANON: bool = false,
+        _2: u1 = 0,
+        ECHO: bool = false,
+        ECHOE: bool = false,
+        ECHOK: bool = false,
+        ECHONL: bool = false,
+        NOFLSH: bool = false,
+        IEXTEN: bool = false,
+        _9: u6 = 0,
+        TOSTOP: bool = false,
+        _: u16 = 0,
+    },
+    else => packed struct(u32) {
+        ISIG: bool = false,
+        ICANON: bool = false,
+        _2: u1 = 0,
+        ECHO: bool = false,
+        ECHOE: bool = false,
+        ECHOK: bool = false,
+        ECHONL: bool = false,
+        NOFLSH: bool = false,
+        TOSTOP: bool = false,
+        _9: u6 = 0,
+        IEXTEN: bool = false,
+        _: u16 = 0,
+    },
+};
+
 pub const cc_t = switch (native_arch) {
     .mips, .mipsel, .mips64, .mips64el => enum(u8) {
         VINTR = 0,
@@ -5207,18 +5254,6 @@ pub const cc_t = switch (native_arch) {
     },
 };
 
-pub const tcflag_t = u32;
-
-pub const ISIG: tcflag_t = 1;
-pub const ICANON: tcflag_t = 2;
-pub const ECHO: tcflag_t = 8;
-pub const ECHOE: tcflag_t = 16;
-pub const ECHOK: tcflag_t = 32;
-pub const ECHONL: tcflag_t = 64;
-pub const NOFLSH: tcflag_t = 128;
-pub const TOSTOP: tcflag_t = 256;
-pub const IEXTEN: tcflag_t = 32768;
-
 pub const TCSA = enum(c_uint) {
     NOW,
     DRAIN,
@@ -5231,7 +5266,7 @@ pub const termios = switch (native_arch) {
         iflag: tc_iflag_t,
         oflag: tc_oflag_t,
         cflag: tc_cflag_t,
-        lflag: tcflag_t,
+        lflag: tc_lflag_t,
         cc: [NCCS]cc_t,
         line: cc_t,
         ispeed: speed_t,
@@ -5241,7 +5276,7 @@ pub const termios = switch (native_arch) {
         iflag: tc_iflag_t,
         oflag: tc_oflag_t,
         cflag: tc_cflag_t,
-        lflag: tcflag_t,
+        lflag: tc_lflag_t,
         line: cc_t,
         cc: [NCCS]cc_t,
         ispeed: speed_t,
lib/std/c.zig
@@ -796,7 +796,7 @@ pub const termios = switch (native_os) {
         iflag: tc_iflag_t,
         oflag: tc_oflag_t,
         cflag: tc_cflag_t,
-        lflag: tcflag_t,
+        lflag: tc_lflag_t,
         cc: [NCCS]cc_t,
         ispeed: speed_t align(8),
         ospeed: speed_t,
@@ -805,7 +805,7 @@ pub const termios = switch (native_os) {
         iflag: tc_iflag_t,
         oflag: tc_oflag_t,
         cflag: tc_cflag_t,
-        lflag: tcflag_t,
+        lflag: tc_lflag_t,
         cc: [NCCS]cc_t,
         ispeed: speed_t,
         ospeed: speed_t,
@@ -814,7 +814,7 @@ pub const termios = switch (native_os) {
         iflag: tc_iflag_t,
         oflag: tc_oflag_t,
         cflag: tc_cflag_t,
-        lflag: tcflag_t,
+        lflag: tc_lflag_t,
         line: cc_t,
         ispeed: speed_t,
         ospeed: speed_t,
@@ -824,14 +824,14 @@ pub const termios = switch (native_os) {
         iflag: tc_iflag_t,
         oflag: tc_oflag_t,
         cflag: tc_cflag_t,
-        lflag: tcflag_t,
+        lflag: tc_lflag_t,
         cc: [NCCS]cc_t,
     },
     .emscripten, .wasi => extern struct {
         iflag: tc_iflag_t,
         oflag: tc_oflag_t,
         cflag: tc_cflag_t,
-        lflag: tcflag_t,
+        lflag: tc_lflag_t,
         line: std.c.cc_t,
         cc: [NCCS]cc_t,
         ispeed: speed_t,
@@ -1183,18 +1183,106 @@ pub const tc_cflag_t = switch (native_os) {
     else => @compileError("target libc does not have tc_cflag_t"),
 };
 
-pub const tcflag_t = switch (native_os) {
-    .linux => std.os.linux.tcflag_t,
-    .macos, .ios, .tvos, .watchos => u64,
-    .freebsd, .kfreebsd => c_uint,
-    .netbsd => c_uint,
-    .dragonfly => c_uint,
-    .openbsd => c_uint,
-    .haiku => u32,
-    .solaris, .illumos => c_uint,
-    .emscripten => u32,
-    .wasi => c_uint,
-    else => @compileError("target libc does not have tcflag_t"),
+pub const tc_lflag_t = switch (native_os) {
+    .linux => std.os.linux.tc_lflag_t,
+    .macos, .ios, .tvos, .watchos, .netbsd, .freebsd, .kfreebsd, .dragonfly => packed struct(u32) {
+        ECHOKE: bool = false,
+        ECHOE: bool = false,
+        ECHOK: bool = false,
+        ECHO: bool = false,
+        ECHONL: bool = false,
+        ECHOPRT: bool = false,
+        ECHOCTL: bool = false,
+        ISIG: bool = false,
+        ICANON: bool = false,
+        ALTWERASE: bool = false,
+        IEXTEN: bool = false,
+        EXTPROC: bool = false,
+        _12: u10 = 0,
+        TOSTOP: bool = false,
+        FLUSHO: bool = false,
+        _24: u1 = 0,
+        NOKERNINFO: bool = false,
+        _26: u3 = 0,
+        PENDIN: bool = false,
+        _30: u1 = 0,
+        NOFLSH: bool = false,
+    },
+    .openbsd => packed struct(u32) {
+        ECHOKE: bool = false,
+        ECHOE: bool = false,
+        ECHOK: bool = false,
+        ECHO: bool = false,
+        ECHONL: bool = false,
+        ECHOPRT: bool = false,
+        ECHOCTL: bool = false,
+        ISIG: bool = false,
+        ICANON: bool = false,
+        ALTWERASE: bool = false,
+        IEXTEN: bool = false,
+        EXTPROC: bool = false,
+        _12: u10 = 0,
+        TOSTOP: bool = false,
+        FLUSHO: bool = false,
+        XCASE: bool = false,
+        NOKERNINFO: bool = false,
+        _26: u3 = 0,
+        PENDIN: bool = false,
+        _30: u1 = 0,
+        NOFLSH: bool = false,
+    },
+    .haiku => packed struct(u32) {
+        ISIG: bool = false,
+        ICANON: bool = false,
+        XCASE: bool = false,
+        ECHO: bool = false,
+        ECHOE: bool = false,
+        ECHOK: bool = false,
+        ECHONL: bool = false,
+        NOFLSH: bool = false,
+        TOSTOP: bool = false,
+        IEXTEN: bool = false,
+        ECHOCTL: bool = false,
+        ECHOPRT: bool = false,
+        ECHOKE: bool = false,
+        FLUSHO: bool = false,
+        PENDIN: bool = false,
+        _: u17 = 0,
+    },
+    .solaris, .illumos => packed struct(u32) {
+        ISIG: bool = false,
+        ICANON: bool = false,
+        XCASE: bool = false,
+        ECHO: bool = false,
+        ECHOE: bool = false,
+        ECHOK: bool = false,
+        ECHONL: bool = false,
+        NOFLSH: bool = false,
+        TOSTOP: bool = false,
+        ECHOCTL: bool = false,
+        ECHOPRT: bool = false,
+        ECHOKE: bool = false,
+        DEFECHO: bool = false,
+        FLUSHO: bool = false,
+        PENDIN: bool = false,
+        IEXTEN: bool = false,
+        _: u16 = 0,
+    },
+    .wasi, .emscripten => packed struct(u32) {
+        ISIG: bool = false,
+        ICANON: bool = false,
+        _2: u1 = 0,
+        ECHO: bool = false,
+        ECHOE: bool = false,
+        ECHOK: bool = false,
+        ECHONL: bool = false,
+        NOFLSH: bool = false,
+        TOSTOP: bool = false,
+        _9: u6 = 0,
+        IEXTEN: bool = false,
+        _: u16 = 0,
+    },
+    else => @compileError("target libc does not have tc_lflag_t"),
 };
 
 pub const speed_t = switch (native_os) {
lib/std/os.zig
@@ -187,10 +187,10 @@ pub const utsname = system.utsname;
 pub const CSIZE = system.CSIZE;
 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 tc_oflag_t = system.tc_oflag_t;
 pub const tc_cflag_t = system.tc_cflag_t;
+pub const tc_lflag_t = system.tc_lflag_t;
 
 pub const F_OK = system.F_OK;
 pub const R_OK = system.R_OK;