Commit 5258c3caad

Andrew Kelley <andrew@ziglang.org>
2024-02-12 23:41:38
std: add type safety to cc_t
1 parent 0c725a3
lib/std/c/darwin.zig
@@ -2692,31 +2692,8 @@ pub const SHUT = struct {
     pub const RDWR = 2;
 };
 
-// Term
-pub const V = struct {
-    pub const EOF = 0;
-    pub const EOL = 1;
-    pub const EOL2 = 2;
-    pub const ERASE = 3;
-    pub const WERASE = 4;
-    pub const KILL = 5;
-    pub const REPRINT = 6;
-    pub const INTR = 8;
-    pub const QUIT = 9;
-    pub const SUSP = 10;
-    pub const DSUSP = 11;
-    pub const START = 12;
-    pub const STOP = 13;
-    pub const LNEXT = 14;
-    pub const DISCARD = 15;
-    pub const MIN = 16;
-    pub const TIME = 17;
-    pub const STATUS = 18;
-};
-
 pub const NCCS = 20; // 2 spares (7, 19)
 
-pub const cc_t = u8;
 pub const speed_t = u64;
 pub const tcflag_t = u64;
 
@@ -2859,7 +2836,7 @@ pub const termios = extern struct {
     oflag: tcflag_t, // output flags
     cflag: tcflag_t, // control flags
     lflag: tcflag_t, // local flags
-    cc: [NCCS]cc_t, // control chars
+    cc: [NCCS]std.c.cc_t, // control chars
     ispeed: speed_t align(8), // input speed
     ospeed: speed_t, // output speed
 };
lib/std/c/emscripten.zig
@@ -72,8 +72,6 @@ pub const sigset_t = emscripten.sigset_t;
 pub const sockaddr = emscripten.sockaddr;
 pub const socklen_t = emscripten.socklen_t;
 pub const stack_t = emscripten.stack_t;
-pub const tcflag_t = emscripten.tcflag_t;
-pub const termios = emscripten.termios;
 pub const time_t = emscripten.time_t;
 pub const timespec = emscripten.timespec;
 pub const timeval = emscripten.timeval;
@@ -180,3 +178,19 @@ pub const dirent = struct {
     type: u8,
     name: [256]u8,
 };
+
+pub const speed_t = u32;
+pub const tcflag_t = u32;
+
+pub const NCCS = 32;
+
+pub const termios = extern struct {
+    iflag: tcflag_t,
+    oflag: tcflag_t,
+    cflag: tcflag_t,
+    lflag: tcflag_t,
+    line: std.c.cc_t,
+    cc: [NCCS]std.c.cc_t,
+    ispeed: speed_t,
+    ospeed: speed_t,
+};
lib/std/c/haiku.zig
@@ -950,7 +950,6 @@ pub const directory_which = enum(c_int) {
     _,
 };
 
-pub const cc_t = u8;
 pub const speed_t = u8;
 pub const tcflag_t = u32;
 
@@ -961,10 +960,10 @@ pub const termios = extern struct {
     c_oflag: tcflag_t,
     c_cflag: tcflag_t,
     c_lflag: tcflag_t,
-    c_line: cc_t,
+    c_line: std.c.cc_t,
     c_ispeed: speed_t,
     c_ospeed: speed_t,
-    cc_t: [NCCS]cc_t,
+    cc_t: [NCCS]std.c.cc_t,
 };
 
 pub const MSG_NOSIGNAL = 0x0800;
lib/std/c/netbsd.zig
@@ -806,30 +806,6 @@ pub const T = struct {
     pub const IOCXMTFRAME = 0x80087444;
 };
 
-// Term
-const V = struct {
-    pub const EOF = 0; // ICANON
-    pub const EOL = 1; // ICANON
-    pub const EOL2 = 2; // ICANON
-    pub const ERASE = 3; // ICANON
-    pub const WERASE = 4; // ICANON
-    pub const KILL = 5; // ICANON
-    pub const REPRINT = 6; // ICANON
-    //  7    spare 1
-    pub const INTR = 8; // ISIG
-    pub const QUIT = 9; // ISIG
-    pub const SUSP = 10; // ISIG
-    pub const DSUSP = 11; // ISIG
-    pub const START = 12; // IXON, IXOFF
-    pub const STOP = 13; // IXON, IXOFF
-    pub const LNEXT = 14; // IEXTEN
-    pub const DISCARD = 15; // IEXTEN
-    pub const MIN = 16; // !ICANON
-    pub const TIME = 17; // !ICANON
-    pub const STATUS = 18; // ICANON
-    //  19      spare 2
-};
-
 // Input flags - software input processing
 pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
 pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINT
@@ -876,7 +852,6 @@ pub const CHWFLOW: tcflag_t = (MDMBUF | CRTSCTS | CDTRCTS); // all types of hw f
 
 pub const tcflag_t = c_uint;
 pub const speed_t = c_uint;
-pub const cc_t = u8;
 
 pub const NCCS = 20;
 
@@ -885,7 +860,7 @@ pub const termios = extern struct {
     oflag: tcflag_t, // output flags
     cflag: tcflag_t, // control flags
     lflag: tcflag_t, // local flags
-    cc: [NCCS]cc_t, // control chars
+    cc: [NCCS]std.c.cc_t, // control chars
     ispeed: c_int, // input speed
     ospeed: c_int, // output speed
 };
lib/std/c/openbsd.zig
@@ -768,33 +768,8 @@ pub const AUTH = struct {
     pub const ALLOW: c_int = (OKAY | ROOTOKAY | SECURE);
 };
 
-// Term
-pub const V = struct {
-    pub const EOF = 0; // ICANON
-    pub const EOL = 1; // ICANON
-    pub const EOL2 = 2; // ICANON
-    pub const ERASE = 3; // ICANON
-    pub const WERASE = 4; // ICANON
-    pub const KILL = 5; // ICANON
-    pub const REPRINT = 6; // ICANON
-    //  7    spare 1
-    pub const INTR = 8; // ISIG
-    pub const QUIT = 9; // ISIG
-    pub const SUSP = 10; // ISIG
-    pub const DSUSP = 11; // ISIG
-    pub const START = 12; // IXON, IXOFF
-    pub const STOP = 13; // IXON, IXOFF
-    pub const LNEXT = 14; // IEXTEN
-    pub const DISCARD = 15; // IEXTEN
-    pub const MIN = 16; // !ICANON
-    pub const TIME = 17; // !ICANON
-    pub const STATUS = 18; // ICANON
-    //  19      spare 2
-};
-
 pub const tcflag_t = c_uint;
 pub const speed_t = c_uint;
-pub const cc_t = u8;
 
 pub const NCCS = 20;
 
@@ -848,7 +823,7 @@ pub const termios = extern struct {
     oflag: tcflag_t, // output flags
     cflag: tcflag_t, // control flags
     lflag: tcflag_t, // local flags
-    cc: [NCCS]cc_t, // control chars
+    cc: [NCCS]std.c.cc_t, // control chars
     ispeed: c_int, // input speed
     ospeed: c_int, // output speed
 };
lib/std/c/solaris.zig
@@ -699,7 +699,6 @@ pub const SEEK = struct {
 };
 
 pub const tcflag_t = c_uint;
-pub const cc_t = u8;
 pub const speed_t = c_uint;
 
 pub const NCCS = 19;
@@ -709,7 +708,7 @@ pub const termios = extern struct {
     c_oflag: tcflag_t,
     c_cflag: tcflag_t,
     c_lflag: tcflag_t,
-    c_cc: [NCCS]cc_t,
+    c_cc: [NCCS]std.c.cc_t,
 };
 
 fn tioc(t: u16, num: u8) u16 {
lib/std/os/emscripten.zig
@@ -1098,23 +1098,6 @@ pub const stack_t = extern struct {
     size: usize,
 };
 
-pub const cc_t = u8;
-pub const speed_t = u32;
-pub const tcflag_t = u32;
-
-pub const NCCS = 32;
-
-pub const termios = extern struct {
-    iflag: tcflag_t,
-    oflag: tcflag_t,
-    cflag: tcflag_t,
-    lflag: tcflag_t,
-    line: cc_t,
-    cc: [NCCS]cc_t,
-    ispeed: speed_t,
-    ospeed: speed_t,
-};
-
 pub const timespec = extern struct {
     tv_sec: time_t,
     tv_nsec: isize,
lib/std/os/linux.zig
@@ -5004,9 +5004,7 @@ pub const rusage = extern struct {
     pub const THREAD = 1;
 };
 
-pub const cc_t = u8;
 pub const speed_t = u32;
-pub const tcflag_t = u32;
 
 pub const NCCS = 32;
 
@@ -5124,21 +5122,84 @@ pub const V = switch (native_arch) {
     },
 };
 
-pub const IGNBRK: tcflag_t = 1;
-pub const BRKINT: tcflag_t = 2;
-pub const IGNPAR: tcflag_t = 4;
-pub const PARMRK: tcflag_t = 8;
-pub const INPCK: tcflag_t = 16;
-pub const ISTRIP: tcflag_t = 32;
-pub const INLCR: tcflag_t = 64;
-pub const IGNCR: tcflag_t = 128;
-pub const ICRNL: tcflag_t = 256;
-pub const IUCLC: tcflag_t = 512;
-pub const IXON: tcflag_t = 1024;
-pub const IXANY: tcflag_t = 2048;
-pub const IXOFF: tcflag_t = 4096;
-pub const IMAXBEL: tcflag_t = 8192;
-pub const IUTF8: tcflag_t = 16384;
+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 cc_t = switch (native_arch) {
+    .mips, .mipsel, .mips64, .mips64el => enum(u8){
+        VINTR    =  0,
+        VQUIT    =  1,
+        VERASE   =  2,
+        VKILL    =  3,
+        VMIN     =  4,
+        VTIME    =  5,
+        VEOL2    =  6,
+        VSWTC    =  7,
+        VSTART   =  8,
+        VSTOP    =  9,
+        VSUSP    = 10,
+        VREPRINT = 12,
+        VDISCARD = 13,
+        VWERASE  = 14,
+        VLNEXT   = 15,
+        VEOF     = 16,
+        VEOL     = 17,
+    },
+    .powerpc, .powerpc64, .powerpc64le => enum(u8) {
+        VINTR    =  0,
+        VQUIT    =  1,
+        VERASE   =  2,
+        VKILL    =  3,
+        VEOF     =  4,
+        VMIN     =  5,
+        VEOL     =  6,
+        VTIME    =  7,
+        VEOL2    =  8,
+        VSWTC    =  9,
+        VWERASE  = 10,
+        VREPRINT = 11,
+        VSUSP    = 12,
+        VSTART   = 13,
+        VSTOP    = 14,
+        VLNEXT   = 15,
+        VDISCARD = 16,
+    },
+    else => enum(u8) {
+        VINTR = 0,
+        VQUIT = 1,
+        VERASE = 2,
+        VKILL = 3,
+        VEOF = 4,
+        VTIME = 5,
+        VMIN = 6,
+        VSWTC = 7,
+        VSTART = 8,
+        VSTOP = 9,
+        VSUSP = 10,
+        VEOL = 11,
+        VREPRINT = 12,
+        VDISCARD = 13,
+        VWERASE = 14,
+        VLNEXT = 15,
+        VEOL2 = 16,
+    },
+};
 
 pub const OPOST: tcflag_t = 1;
 pub const OLCUC: tcflag_t = 2;
@@ -5148,6 +5209,7 @@ 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;
@@ -5182,10 +5244,10 @@ pub const TCSA = enum(c_uint) {
 };
 
 pub const termios = extern struct {
-    iflag: tcflag_t,
-    oflag: tcflag_t,
-    cflag: tcflag_t,
-    lflag: tcflag_t,
+    iflag: tc_iflag_t,
+    oflag: tc_oflag_t,
+    cflag: tc_cflag_t,
+    lflag: tc_lflag_t,
     line: cc_t,
     cc: [NCCS]cc_t,
     ispeed: speed_t,
lib/std/c.zig
@@ -679,6 +679,167 @@ pub const MAP = switch (native_os) {
 /// Used by libc to communicate failure. Not actually part of the underlying syscall.
 pub const MAP_FAILED: *anyopaque = @ptrFromInt(std.math.maxInt(usize));
 
+pub const cc_t = switch (native_os) {
+    .linux => std.os.linux.cc_t,
+    .macos, .ios, .tvos, .watchos => enum(u8) {
+        VEOF = 0,
+        VEOL = 1,
+        VEOL2 = 2,
+        VERASE = 3,
+        VWERASE = 4,
+        VKILL = 5,
+        VREPRINT = 6,
+        VINTR = 8,
+        VQUIT = 9,
+        VSUSP = 10,
+        VDSUSP = 11,
+        VSTART = 12,
+        VSTOP = 13,
+        VLNEXT = 14,
+        VDISCARD = 15,
+        VMIN = 16,
+        VTIME = 17,
+        VSTATUS = 18,
+    },
+    .freebsd, .kfreebsd => enum(u8) {
+        VEOF = 0,
+        VEOL = 1,
+        VEOL2 = 2,
+        VERASE = 3,
+        VWERASE = 4,
+        VKILL = 5,
+        VREPRINT = 6,
+        VERASE2 = 7,
+        VINTR = 8,
+        VQUIT = 9,
+        VSUSP = 10,
+        VDSUSP = 11,
+        VSTART = 12,
+        VSTOP = 13,
+        VLNEXT = 14,
+        VDISCARD = 15,
+        VMIN = 16,
+        VTIME = 17,
+        VSTATUS = 18,
+    },
+    .netbsd => enum(u8) {
+        VEOF = 0,
+        VEOL = 1,
+        VEOL2 = 2,
+        VERASE = 3,
+        VWERASE = 4,
+        VKILL = 5,
+        VREPRINT = 6,
+        VINTR = 8,
+        VQUIT = 9,
+        VSUSP = 10,
+        VDSUSP = 11,
+        VSTART = 12,
+        VSTOP = 13,
+        VLNEXT = 14,
+        VDISCARD = 15,
+        VMIN = 16,
+        VTIME = 17,
+        VSTATUS = 18,
+    },
+    .openbsd => enum(u8) {
+        VEOF = 0,
+        VEOL = 1,
+        VEOL2 = 2,
+        VERASE = 3,
+        VWERASE = 4,
+        VKILL = 5,
+        VREPRINT = 6,
+        VINTR = 8,
+        VQUIT = 9,
+        VSUSP = 10,
+        VDSUSP = 11,
+        VSTART = 12,
+        VSTOP = 13,
+        VLNEXT = 14,
+        VDISCARD = 15,
+        VMIN = 16,
+        VTIME = 17,
+        VSTATUS = 18,
+    },
+    .haiku => enum(u8) {
+        VINTR = 0,
+        VQUIT = 1,
+        VERASE = 2,
+        VKILL = 3,
+        VEOF = 4,
+        VEOL = 5,
+        VMIN = 4,
+        VTIME = 5,
+        VEOL2 = 6,
+        VSWTCH = 7,
+        VSTART = 8,
+        VSTOP = 9,
+        VSUSP = 10,
+    },
+    .solaris, .illumos => enum(u8) {
+        VINTR = 0,
+        VQUIT = 1,
+        VERASE = 2,
+        VKILL = 3,
+        VEOF = 4,
+        VEOL = 5,
+        VEOL2 = 6,
+        VMIN = 4,
+        VTIME = 5,
+        VSWTCH = 7,
+        VSTART = 8,
+        VSTOP = 9,
+        VSUSP = 10,
+        VDSUSP = 11,
+        VREPRINT = 12,
+        VDISCARD = 13,
+        VWERASE = 14,
+        VLNEXT = 15,
+        VSTATUS = 16,
+        VERASE2 = 17,
+    },
+    .emscripten => enum(u8) {
+        VINTR = 0,
+        VQUIT = 1,
+        VERASE = 2,
+        VKILL = 3,
+        VEOF = 4,
+        VTIME = 5,
+        VMIN = 6,
+        VSWTC = 7,
+        VSTART = 8,
+        VSTOP = 9,
+        VSUSP = 10,
+        VEOL = 11,
+        VREPRINT = 12,
+        VDISCARD = 13,
+        VWERASE = 14,
+        VLNEXT = 15,
+        VEOL2 = 16,
+    },
+    .wasi => enum(u8) {
+        VINTR = 0,
+        VQUIT = 1,
+        VERASE = 2,
+        VKILL = 3,
+        VEOF = 4,
+        VTIME = 5,
+        VMIN = 6,
+        VSWTC = 7,
+        VSTART = 8,
+        VSTOP = 9,
+        VSUSP = 10,
+        VEOL = 11,
+        VREPRINT = 12,
+        VDISCARD = 13,
+        VWERASE = 14,
+        VLNEXT = 15,
+        VEOL2 = 16,
+    },
+    else => @compileError("target libc does not have cc_t"),
+};
+
 pub const whence_t = if (native_os == .wasi) std.os.wasi.whence_t else c_int;
 
 // Unix-like systems
lib/std/os.zig
@@ -139,6 +139,7 @@ pub const W = system.W;
 pub const addrinfo = system.addrinfo;
 pub const blkcnt_t = system.blkcnt_t;
 pub const blksize_t = system.blksize_t;
+pub const cc_t = system.cc_t;
 pub const clock_t = system.clock_t;
 pub const cpu_set_t = system.cpu_set_t;
 pub const dev_t = system.dev_t;