Commit 20abc0caee

Andrew Kelley <andrew@ziglang.org>
2024-02-13 01:28:09
std.os.termios: add type safety to oflag field
This creates `tc_oflag_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 47643cc
lib/std/c/darwin.zig
@@ -2692,42 +2692,6 @@ pub const SHUT = struct {
     pub const RDWR = 2;
 };
 
-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
-pub const ONOEOT: tcflag_t = 0x00000008; // discard EOT's (^D) on output)
-
-pub const OCRNL: tcflag_t = 0x00000010; // map CR to NL on output
-pub const ONOCR: tcflag_t = 0x00000020; // no CR output at column 0
-pub const ONLRET: tcflag_t = 0x00000040; // NL performs CR function
-pub const OFILL: tcflag_t = 0x00000080; // use fill characters for delay
-pub const NLDLY: tcflag_t = 0x00000300; // \n delay
-pub const TABDLY: tcflag_t = 0x00000c04; // horizontal tab delay
-pub const CRDLY: tcflag_t = 0x00003000; // \r delay
-pub const FFDLY: tcflag_t = 0x00004000; // form feed delay
-pub const BSDLY: tcflag_t = 0x00008000; // \b delay
-pub const VTDLY: tcflag_t = 0x00010000; // vertical tab delay
-pub const OFDEL: tcflag_t = 0x00020000; // fill is DEL, else NUL
-
-pub const NL0: tcflag_t = 0x00000000;
-pub const NL1: tcflag_t = 0x00000100;
-pub const NL2: tcflag_t = 0x00000200;
-pub const NL3: tcflag_t = 0x00000300;
-pub const TAB0: tcflag_t = 0x00000000;
-pub const TAB1: tcflag_t = 0x00000400;
-pub const TAB2: tcflag_t = 0x00000800;
-pub const TAB3: tcflag_t = 0x00000004;
-pub const CR0: tcflag_t = 0x00000000;
-pub const CR1: tcflag_t = 0x00001000;
-pub const CR2: tcflag_t = 0x00002000;
-pub const CR3: tcflag_t = 0x00003000;
-pub const FF0: tcflag_t = 0x00000000;
-pub const FF1: tcflag_t = 0x00004000;
-pub const BS0: tcflag_t = 0x00000000;
-pub const BS1: tcflag_t = 0x00008000;
-pub const VT0: tcflag_t = 0x00000000;
-pub const VT1: tcflag_t = 0x00010000;
-
 pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
 pub const CSIZE: tcflag_t = 0x00000300; // character size mask
 pub const CS5: tcflag_t = 0x00000000; //    5 bits (pseudo)
lib/std/c/netbsd.zig
@@ -807,15 +807,6 @@ pub const T = struct {
 };
 
 
-// Output flags - software output processing
-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
-pub const ONOEOT: tcflag_t = 0x00000008; // discard EOT's (^D) on output
-pub const OCRNL: tcflag_t = 0x00000010; // map CR to NL
-pub const ONOCR: tcflag_t = 0x00000040; // discard CR's when on column 0
-pub const ONLRET: tcflag_t = 0x00000080; // move to column 0 on CR
-
 // Control flags - hardware control of terminal
 pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
 pub const CSIZE: tcflag_t = 0x00000300; // character size mask
lib/std/c/openbsd.zig
@@ -769,15 +769,6 @@ pub const AUTH = struct {
 };
 
 
-// Output flags - software output processing
-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
-pub const ONOEOT: tcflag_t = 0x00000008; // discard EOT's (^D) on output
-pub const OCRNL: tcflag_t = 0x00000010; // map CR to NL
-pub const OLCUC: tcflag_t = 0x00000020; // translate lower case to upper case
-pub const ONOCR: tcflag_t = 0x00000040; // No CR output at column 0
-pub const ONLRET: tcflag_t = 0x00000080; // NL performs the CR function
 
 // Control flags - hardware control of terminal
 pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
lib/std/os/linux.zig
@@ -5083,6 +5083,43 @@ pub const tc_iflag_t = switch (native_arch) {
     },
 };
 
+pub const tc_oflag_t = switch (native_arch) {
+    .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
+        OPOST: bool = false,
+        ONLCR: bool = false,
+        OLCUC: bool = false,
+        OCRNL: bool = false,
+        ONOCR: bool = false,
+        ONLRET: bool = false,
+        OFILL: bool = false,
+        OFDEL: bool = false,
+        NLDLY: u2 = 0,
+        TABDLY: u2 = 0,
+        CRDLY: u2 = 0,
+        FFDLY: u1 = 0,
+        BSDLY: u1 = 0,
+        VTDLY: u1 = 0,
+        _: u15 = 0,
+    },
+    else => packed struct(u32) {
+        OPOST: bool = false,
+        OLCUC: bool = false,
+        ONLCR: bool = false,
+        OCRNL: bool = false,
+        ONOCR: bool = false,
+        ONLRET: bool = false,
+        OFILL: bool = false,
+        OFDEL: bool = false,
+        NLDLY: u1 = 0,
+        CRDLY: u2 = 0,
+        TABDLY: u2 = 0,
+        BSDLY: u1 = 0,
+        VTDLY: u1 = 0,
+        FFDLY: u1 = 0,
+        _: u16 = 0,
+    },
+};
+
 pub const cc_t = switch (native_arch) {
     .mips, .mipsel, .mips64, .mips64el => enum(u8) {
         VINTR = 0,
@@ -5145,19 +5182,6 @@ 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;
-pub const OCRNL: tcflag_t = 8;
-pub const ONOCR: tcflag_t = 16;
-pub const ONLRET: tcflag_t = 32;
-pub const OFILL: tcflag_t = 64;
-pub const OFDEL: tcflag_t = 128;
-
-pub const VTDLY: tcflag_t = 16384;
-pub const VT0: tcflag_t = 0;
-pub const VT1: tcflag_t = 16384;
-
 pub const CSIZE: tcflag_t = 48;
 pub const CS5: tcflag_t = 0;
 pub const CS6: tcflag_t = 16;
@@ -5190,7 +5214,7 @@ pub const TCSA = enum(c_uint) {
 pub const termios = switch (native_arch) {
     .powerpc, .powerpcle, .powerpc64, .powerpc64le => extern struct {
         iflag: tc_iflag_t,
-        oflag: tcflag_t,
+        oflag: tc_oflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
         cc: [NCCS]cc_t,
@@ -5200,7 +5224,7 @@ pub const termios = switch (native_arch) {
     },
     else => extern struct {
         iflag: tc_iflag_t,
-        oflag: tcflag_t,
+        oflag: tc_oflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
         line: cc_t,
lib/std/c.zig
@@ -794,7 +794,7 @@ pub const termios = switch (native_os) {
     .linux => std.os.linux.termios,
     .macos, .ios, .tvos, .watchos => extern struct {
         iflag: tc_iflag_t,
-        oflag: tcflag_t,
+        oflag: tc_oflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
         cc: [NCCS]cc_t,
@@ -803,7 +803,7 @@ pub const termios = switch (native_os) {
     },
     .freebsd, .kfreebsd, .netbsd, .dragonfly, .openbsd => extern struct {
         iflag: tc_iflag_t,
-        oflag: tcflag_t,
+        oflag: tc_oflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
         cc: [NCCS]cc_t,
@@ -812,7 +812,7 @@ pub const termios = switch (native_os) {
     },
     .haiku => extern struct {
         iflag: tc_iflag_t,
-        oflag: tcflag_t,
+        oflag: tc_oflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
         line: cc_t,
@@ -822,14 +822,14 @@ pub const termios = switch (native_os) {
     },
     .solaris, .illumos => extern struct {
         iflag: tc_iflag_t,
-        oflag: tcflag_t,
+        oflag: tc_oflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
         cc: [NCCS]cc_t,
     },
     .emscripten, .wasi => extern struct {
         iflag: tc_iflag_t,
-        oflag: tcflag_t,
+        oflag: tc_oflag_t,
         cflag: tcflag_t,
         lflag: tcflag_t,
         line: std.c.cc_t,
@@ -950,6 +950,97 @@ pub const tc_iflag_t = switch (native_os) {
     else => @compileError("target libc does not have tc_iflag_t"),
 };
 
+pub const tc_oflag_t = switch (native_os) {
+    .linux => std.os.linux.tc_oflag_t,
+    .macos, .ios, .tvos, .watchos => packed struct(u32) {
+        OPOST: bool = false,
+        ONLCR: bool = false,
+        OXTABS: bool = false,
+        ONOEOT: bool = false,
+        OCRNL: bool = false,
+        ONOCR: bool = false,
+        ONLRET: bool = false,
+        OFILL: bool = false,
+        NLDLY: u2 = 0,
+        TABDLY: u2 = 0,
+        CRDLY: u2 = 0,
+        FFDLY: u1 = 0,
+        BSDLY: u1 = 0,
+        VTDLY: u1 = 0,
+        OFDEL: bool = false,
+        _: u14 = 0,
+    },
+    .netbsd => packed struct(u32) {
+        OPOST: bool = false,
+        ONLCR: bool = false,
+        OXTABS: bool = false,
+        ONOEOT: bool = false,
+        OCRNL: bool = false,
+        _5: u1 = 0,
+        ONOCR: bool = false,
+        ONLRET: bool = false,
+        _: u24 = 0,
+    },
+    .openbsd => packed struct(u32) {
+        OPOST: bool = false,
+        ONLCR: bool = false,
+        OXTABS: bool = false,
+        ONOEOT: bool = false,
+        OCRNL: bool = false,
+        OLCUC: bool = false,
+        ONOCR: bool = false,
+        ONLRET: bool = false,
+        _: u24 = 0,
+    },
+    .freebsd, .kfreebsd, .dragonfly => packed struct(u32) {
+        OPOST: bool = false,
+        ONLCR: bool = false,
+        _2: u1 = 0,
+        ONOEOT: bool = false,
+        OCRNL: bool = false,
+        ONOCR: bool = false,
+        ONLRET: bool = false,
+        _: u25 = 0,
+    },
+    .solaris, .illumos => packed struct(u32) {
+        OPOST: bool = false,
+        OLCUC: bool = false,
+        ONLCR: bool = false,
+        OCRNL: bool = false,
+        ONOCR: bool = false,
+        ONLRET: bool = false,
+        OFILL: bool = false,
+        OFDEL: bool = false,
+        NLDLY: u1 = 0,
+        CRDLY: u2 = 0,
+        TABDLY: u2 = 0,
+        BSDLY: u1 = 0,
+        VTDLY: u1 = 0,
+        FFDLY: u1 = 0,
+        PAGEOUT: bool = false,
+        WRAP: bool = false,
+        _: u14 = 0,
+    },
+    .haiku, .wasi, .emscripten => packed struct(u32) {
+        OPOST: bool = false,
+        OLCUC: bool = false,
+        ONLCR: bool = false,
+        OCRNL: bool = false,
+        ONOCR: bool = false,
+        ONLRET: bool = false,
+        OFILL: bool = false,
+        OFDEL: bool = false,
+        NLDLY: u1 = 0,
+        CRDLY: u2 = 0,
+        TABDLY: u2 = 0,
+        BSDLY: u1 = 0,
+        VTDLY: u1 = 0,
+        FFDLY: u1 = 0,
+        _: u16 = 0,
+    },
+    else => @compileError("target libc does not have tc_oflag_t"),
+};
+
 pub const tcflag_t = switch (native_os) {
     .linux => std.os.linux.tcflag_t,
     .macos, .ios, .tvos, .watchos => u64,
lib/std/os.zig
@@ -188,6 +188,7 @@ 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 F_OK = system.F_OK;
 pub const R_OK = system.R_OK;