authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 18:24:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 18:24:07-07:00
loge97fa8b03803dfb5d2046d9b2ebecbee85a0e1f9
treef7bf8e4ae4b742fa7eaae4eb4150e2b35a883257
parent20abc0caeeb8a9cc1cc1fe8c8ae318ec2906cc24

std.os.termios: add type safety to cflag 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.

6 files changed, 178 insertions(+), 81 deletions(-)

lib/std/c.zig+147-5
...@@ -795,7 +795,7 @@ pub const termios = switch (native_os) {...@@ -795,7 +795,7 @@ pub const termios = switch (native_os) {
795 .macos, .ios, .tvos, .watchos => extern struct {795 .macos, .ios, .tvos, .watchos => extern struct {
796 iflag: tc_iflag_t,796 iflag: tc_iflag_t,
797 oflag: tc_oflag_t,797 oflag: tc_oflag_t,
798 cflag: tcflag_t,798 cflag: tc_cflag_t,
799 lflag: tcflag_t,799 lflag: tcflag_t,
800 cc: [NCCS]cc_t,800 cc: [NCCS]cc_t,
801 ispeed: speed_t align(8),801 ispeed: speed_t align(8),
...@@ -804,7 +804,7 @@ pub const termios = switch (native_os) {...@@ -804,7 +804,7 @@ pub const termios = switch (native_os) {
804 .freebsd, .kfreebsd, .netbsd, .dragonfly, .openbsd => extern struct {804 .freebsd, .kfreebsd, .netbsd, .dragonfly, .openbsd => extern struct {
805 iflag: tc_iflag_t,805 iflag: tc_iflag_t,
806 oflag: tc_oflag_t,806 oflag: tc_oflag_t,
807 cflag: tcflag_t,807 cflag: tc_cflag_t,
808 lflag: tcflag_t,808 lflag: tcflag_t,
809 cc: [NCCS]cc_t,809 cc: [NCCS]cc_t,
810 ispeed: speed_t,810 ispeed: speed_t,
...@@ -813,7 +813,7 @@ pub const termios = switch (native_os) {...@@ -813,7 +813,7 @@ pub const termios = switch (native_os) {
813 .haiku => extern struct {813 .haiku => extern struct {
814 iflag: tc_iflag_t,814 iflag: tc_iflag_t,
815 oflag: tc_oflag_t,815 oflag: tc_oflag_t,
816 cflag: tcflag_t,816 cflag: tc_cflag_t,
817 lflag: tcflag_t,817 lflag: tcflag_t,
818 line: cc_t,818 line: cc_t,
819 ispeed: speed_t,819 ispeed: speed_t,
...@@ -823,14 +823,14 @@ pub const termios = switch (native_os) {...@@ -823,14 +823,14 @@ pub const termios = switch (native_os) {
823 .solaris, .illumos => extern struct {823 .solaris, .illumos => extern struct {
824 iflag: tc_iflag_t,824 iflag: tc_iflag_t,
825 oflag: tc_oflag_t,825 oflag: tc_oflag_t,
826 cflag: tcflag_t,826 cflag: tc_cflag_t,
827 lflag: tcflag_t,827 lflag: tcflag_t,
828 cc: [NCCS]cc_t,828 cc: [NCCS]cc_t,
829 },829 },
830 .emscripten, .wasi => extern struct {830 .emscripten, .wasi => extern struct {
831 iflag: tc_iflag_t,831 iflag: tc_iflag_t,
832 oflag: tc_oflag_t,832 oflag: tc_oflag_t,
833 cflag: tcflag_t,833 cflag: tc_cflag_t,
834 lflag: tcflag_t,834 lflag: tcflag_t,
835 line: std.c.cc_t,835 line: std.c.cc_t,
836 cc: [NCCS]cc_t,836 cc: [NCCS]cc_t,
...@@ -1041,6 +1041,148 @@ pub const tc_oflag_t = switch (native_os) {...@@ -1041,6 +1041,148 @@ pub const tc_oflag_t = switch (native_os) {
1041 else => @compileError("target libc does not have tc_oflag_t"),1041 else => @compileError("target libc does not have tc_oflag_t"),
1042};1042};
10431043
1044pub const CSIZE = switch (native_os) {
1045 .linux => std.os.linux.CSIZE,
1046 .haiku => enum(u1) { CS7, CS8 },
1047 else => enum(u2) { CS5, CS6, CS7, CS8 },
1048};
1049
1050pub const tc_cflag_t = switch (native_os) {
1051 .linux => std.os.linux.tc_cflag_t,
1052 .macos, .ios, .tvos, .watchos => packed struct(u32) {
1053 CIGNORE: bool = false,
1054 _1: u5 = 0,
1055 CSTOPB: bool = false,
1056 _7: u1 = 0,
1057 CSIZE: CSIZE = .CS5,
1058 _10: u1 = 0,
1059 CREAD: bool = false,
1060 PARENB: bool = false,
1061 PARODD: bool = false,
1062 HUPCL: bool = false,
1063 CLOCAL: bool = false,
1064 CCTS_OFLOW: bool = false,
1065 CRTS_IFLOW: bool = false,
1066 CDTR_IFLOW: bool = false,
1067 CDSR_OFLOW: bool = false,
1068 CCAR_OFLOW: bool = false,
1069 _: u11 = 0,
1070 },
1071 .freebsd, .kfreebsd => packed struct(u32) {
1072 CIGNORE: bool = false,
1073 _1: u7 = 0,
1074 CSIZE: CSIZE = .CS5,
1075 CSTOPB: bool = false,
1076 CREAD: bool = false,
1077 PARENB: bool = false,
1078 PARODD: bool = false,
1079 HUPCL: bool = false,
1080 CLOCAL: bool = false,
1081 CCTS_OFLOW: bool = false,
1082 CRTS_IFLOW: bool = false,
1083 CDTR_IFLOW: bool = false,
1084 CDSR_OFLOW: bool = false,
1085 CCAR_OFLOW: bool = false,
1086 CNO_RTSDTR: bool = false,
1087 _: u10 = 0,
1088 },
1089 .netbsd => packed struct(u32) {
1090 CIGNORE: bool = false,
1091 _1: u7 = 0,
1092 CSIZE: CSIZE = .CS5,
1093 CSTOPB: bool = false,
1094 CREAD: bool = false,
1095 PARENB: bool = false,
1096 PARODD: bool = false,
1097 HUPCL: bool = false,
1098 CLOCAL: bool = false,
1099 CRTSCTS: bool = false,
1100 CDTRCTS: bool = false,
1101 _18: u2 = 0,
1102 MDMBUF: bool = false,
1103 _: u11 = 0,
1104 },
1105 .dragonfly => packed struct(u32) {
1106 CIGNORE: bool = false,
1107 _1: u7 = 0,
1108 CSIZE: CSIZE = .CS5,
1109 CSTOPB: bool = false,
1110 CREAD: bool = false,
1111 PARENB: bool = false,
1112 PARODD: bool = false,
1113 HUPCL: bool = false,
1114 CLOCAL: bool = false,
1115 CCTS_OFLOW: bool = false,
1116 CRTS_IFLOW: bool = false,
1117 CDTR_IFLOW: bool = false,
1118 CDSR_OFLOW: bool = false,
1119 CCAR_OFLOW: bool = false,
1120 _: u11 = 0,
1121 },
1122 .openbsd => packed struct(u32) {
1123 CIGNORE: bool = false,
1124 _1: u7 = 0,
1125 CSIZE: CSIZE = .CS5,
1126 CSTOPB: bool = false,
1127 CREAD: bool = false,
1128 PARENB: bool = false,
1129 PARODD: bool = false,
1130 HUPCL: bool = false,
1131 CLOCAL: bool = false,
1132 CRTSCTS: bool = false,
1133 _17: u3 = 0,
1134 MDMBUF: bool = false,
1135 _: u11 = 0,
1136 },
1137 .haiku => packed struct(u32) {
1138 _0: u5 = 0,
1139 CSIZE: CSIZE = .CS7,
1140 CSTOPB: bool = false,
1141 CREAD: bool = false,
1142 PARENB: bool = false,
1143 PARODD: bool = false,
1144 HUPCL: bool = false,
1145 CLOCAL: bool = false,
1146 XLOBLK: bool = false,
1147 CTSFLOW: bool = false,
1148 RTSFLOW: bool = false,
1149 _: u17 = 0,
1150 },
1151 .solaris, .illumos => packed struct(u32) {
1152 _0: u4 = 0,
1153 CSIZE: CSIZE = .CS5,
1154 CSTOPB: bool = false,
1155 CREAD: bool = false,
1156 PARENB: bool = false,
1157 PARODD: bool = false,
1158 HUPCL: bool = false,
1159 CLOCAL: bool = false,
1160 RCV1EN: bool = false,
1161 XMT1EN: bool = false,
1162 LOBLK: bool = false,
1163 XCLUDE: bool = false,
1164 _16: u4 = 0,
1165 PAREXT: bool = false,
1166 CBAUDEXT: bool = false,
1167 CIBAUDEXT: bool = false,
1168 _23: u7 = 0,
1169 CRTSXOFF: bool = false,
1170 CRTSCTS: bool = false,
1171 },
1172 .wasi, .emscripten => packed struct(u32) {
1173 _0: u4 = 0,
1174 CSIZE: CSIZE = .CS5,
1175 CSTOPB: bool = false,
1176 CREAD: bool = false,
1177 PARENB: bool = false,
1178 PARODD: bool = false,
1179 HUPCL: bool = false,
1180 CLOCAL: bool = false,
1181 _: u20 = 0,
1182 },
1183 else => @compileError("target libc does not have tc_cflag_t"),
1184};
1185
1044pub const tcflag_t = switch (native_os) {1186pub const tcflag_t = switch (native_os) {
1045 .linux => std.os.linux.tcflag_t,1187 .linux => std.os.linux.tcflag_t,
1046 .macos, .ios, .tvos, .watchos => u64,1188 .macos, .ios, .tvos, .watchos => u64,
lib/std/c/darwin.zig-20
...@@ -2692,26 +2692,6 @@ pub const SHUT = struct {...@@ -2692,26 +2692,6 @@ pub const SHUT = struct {
2692 pub const RDWR = 2;2692 pub const RDWR = 2;
2693};2693};
26942694
2695pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
2696pub const CSIZE: tcflag_t = 0x00000300; // character size mask
2697pub const CS5: tcflag_t = 0x00000000; // 5 bits (pseudo)
2698pub const CS6: tcflag_t = 0x00000100; // 6 bits
2699pub const CS7: tcflag_t = 0x00000200; // 7 bits
2700pub const CS8: tcflag_t = 0x00000300; // 8 bits
2701pub const CSTOPB: tcflag_t = 0x0000040; // send 2 stop bits
2702pub const CREAD: tcflag_t = 0x00000800; // enable receiver
2703pub const PARENB: tcflag_t = 0x00001000; // parity enable
2704pub const PARODD: tcflag_t = 0x00002000; // odd parity, else even
2705pub const HUPCL: tcflag_t = 0x00004000; // hang up on last close
2706pub const CLOCAL: tcflag_t = 0x00008000; // ignore modem status lines
2707pub const CCTS_OFLOW: tcflag_t = 0x00010000; // CTS flow control of output
2708pub const CRTSCTS: tcflag_t = (CCTS_OFLOW | CRTS_IFLOW);
2709pub const CRTS_IFLOW: tcflag_t = 0x00020000; // RTS flow control of input
2710pub const CDTR_IFLOW: tcflag_t = 0x00040000; // DTR flow control of input
2711pub const CDSR_OFLOW: tcflag_t = 0x00080000; // DSR flow control of output
2712pub const CCAR_OFLOW: tcflag_t = 0x00100000; // DCD flow control of output
2713pub const MDMBUF: tcflag_t = 0x00100000; // old name for CCAR_OFLOW
2714
2715pub const ECHOKE: tcflag_t = 0x00000001; // visual erase for line kill2695pub const ECHOKE: tcflag_t = 0x00000001; // visual erase for line kill
2716pub const ECHOE: tcflag_t = 0x00000002; // visually erase chars2696pub const ECHOE: tcflag_t = 0x00000002; // visually erase chars
2717pub const ECHOK: tcflag_t = 0x00000004; // echo NL after line kill2697pub const ECHOK: tcflag_t = 0x00000004; // echo NL after line kill
lib/std/c/netbsd.zig-21
...@@ -806,27 +806,6 @@ pub const T = struct {...@@ -806,27 +806,6 @@ pub const T = struct {
806 pub const IOCXMTFRAME = 0x80087444;806 pub const IOCXMTFRAME = 0x80087444;
807};807};
808808
809
810// Control flags - hardware control of terminal
811pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
812pub const CSIZE: tcflag_t = 0x00000300; // character size mask
813pub const CS5: tcflag_t = 0x00000000; // 5 bits (pseudo)
814pub const CS6: tcflag_t = 0x00000100; // 6 bits
815pub const CS7: tcflag_t = 0x00000200; // 7 bits
816pub const CS8: tcflag_t = 0x00000300; // 8 bits
817pub const CSTOPB: tcflag_t = 0x00000400; // send 2 stop bits
818pub const CREAD: tcflag_t = 0x00000800; // enable receiver
819pub const PARENB: tcflag_t = 0x00001000; // parity enable
820pub const PARODD: tcflag_t = 0x00002000; // odd parity, else even
821pub const HUPCL: tcflag_t = 0x00004000; // hang up on last close
822pub const CLOCAL: tcflag_t = 0x00008000; // ignore modem status lines
823pub const CRTSCTS: tcflag_t = 0x00010000; // RTS/CTS full-duplex flow control
824pub const CRTS_IFLOW: tcflag_t = CRTSCTS; // XXX compat
825pub const CCTS_OFLOW: tcflag_t = CRTSCTS; // XXX compat
826pub const CDTRCTS: tcflag_t = 0x00020000; // DTR/CTS full-duplex flow control
827pub const MDMBUF: tcflag_t = 0x00100000; // DTR/DCD hardware flow control
828pub const CHWFLOW: tcflag_t = (MDMBUF | CRTSCTS | CDTRCTS); // all types of hw flow control
829
830// Commands passed to tcsetattr() for setting the termios structure.809// Commands passed to tcsetattr() for setting the termios structure.
831pub const TCSA = struct {810pub const TCSA = struct {
832 pub const NOW = 0; // make change immediate811 pub const NOW = 0; // make change immediate
lib/std/c/openbsd.zig-21
...@@ -768,27 +768,6 @@ pub const AUTH = struct {...@@ -768,27 +768,6 @@ pub const AUTH = struct {
768 pub const ALLOW: c_int = (OKAY | ROOTOKAY | SECURE);768 pub const ALLOW: c_int = (OKAY | ROOTOKAY | SECURE);
769};769};
770770
771
772
773// Control flags - hardware control of terminal
774pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
775pub const CSIZE: tcflag_t = 0x00000300; // character size mask
776pub const CS5: tcflag_t = 0x00000000; // 5 bits (pseudo)
777pub const CS6: tcflag_t = 0x00000100; // 6 bits
778pub const CS7: tcflag_t = 0x00000200; // 7 bits
779pub const CS8: tcflag_t = 0x00000300; // 8 bits
780pub const CSTOPB: tcflag_t = 0x00000400; // send 2 stop bits
781pub const CREAD: tcflag_t = 0x00000800; // enable receiver
782pub const PARENB: tcflag_t = 0x00001000; // parity enable
783pub const PARODD: tcflag_t = 0x00002000; // odd parity, else even
784pub const HUPCL: tcflag_t = 0x00004000; // hang up on last close
785pub const CLOCAL: tcflag_t = 0x00008000; // ignore modem status lines
786pub const CRTSCTS: tcflag_t = 0x00010000; // RTS/CTS full-duplex flow control
787pub const CRTS_IFLOW: tcflag_t = CRTSCTS; // XXX compat
788pub const CCTS_OFLOW: tcflag_t = CRTSCTS; // XXX compat
789pub const MDMBUF: tcflag_t = 0x00100000; // DTR/DCD hardware flow control
790pub const CHWFLOW: tcflag_t = (MDMBUF | CRTSCTS); // all types of hw flow control
791
792// Commands passed to tcsetattr() for setting the termios structure.771// Commands passed to tcsetattr() for setting the termios structure.
793pub const TCSA = struct {772pub const TCSA = struct {
794 pub const NOW = 0; // make change immediate773 pub const NOW = 0; // make change immediate
lib/std/os.zig+2
...@@ -184,11 +184,13 @@ pub const uid_t = system.uid_t;...@@ -184,11 +184,13 @@ pub const uid_t = system.uid_t;
184pub const user_desc = system.user_desc;184pub const user_desc = system.user_desc;
185pub const utsname = system.utsname;185pub const utsname = system.utsname;
186186
187pub const CSIZE = system.CSIZE;
187pub const NCCS = system.NCCS;188pub const NCCS = system.NCCS;
188pub const speed_t = system.speed_t;189pub const speed_t = system.speed_t;
189pub const tcflag_t = system.tcflag_t;190pub const tcflag_t = system.tcflag_t;
190pub const tc_iflag_t = system.tc_iflag_t;191pub const tc_iflag_t = system.tc_iflag_t;
191pub const tc_oflag_t = system.tc_oflag_t;192pub const tc_oflag_t = system.tc_oflag_t;
193pub const tc_cflag_t = system.tc_cflag_t;
192194
193pub const F_OK = system.F_OK;195pub const F_OK = system.F_OK;
194pub const R_OK = system.R_OK;196pub const R_OK = system.R_OK;
lib/std/os/linux.zig+29-14
...@@ -5120,6 +5120,33 @@ pub const tc_oflag_t = switch (native_arch) {...@@ -5120,6 +5120,33 @@ pub const tc_oflag_t = switch (native_arch) {
5120 },5120 },
5121};5121};
51225122
5123pub const CSIZE = enum(u2) { CS5, CS6, CS7, CS8 };
5124
5125pub const tc_cflag_t = switch (native_arch) {
5126 .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
5127 _0: u8 = 0,
5128 CSIZE: CSIZE = .CS5,
5129 CSTOPB: bool = false,
5130 CREAD: bool = false,
5131 PARENB: bool = false,
5132 PARODD: bool = false,
5133 HUPCL: bool = false,
5134 CLOCAL: bool = false,
5135 _: u16 = 0,
5136 },
5137 else => packed struct(u32) {
5138 _0: u4 = 0,
5139 CSIZE: CSIZE = .CS5,
5140 CSTOPB: bool = false,
5141 CREAD: bool = false,
5142 PARENB: bool = false,
5143 PARODD: bool = false,
5144 HUPCL: bool = false,
5145 CLOCAL: bool = false,
5146 _: u20 = 0,
5147 },
5148};
5149
5123pub const cc_t = switch (native_arch) {5150pub const cc_t = switch (native_arch) {
5124 .mips, .mipsel, .mips64, .mips64el => enum(u8) {5151 .mips, .mipsel, .mips64, .mips64el => enum(u8) {
5125 VINTR = 0,5152 VINTR = 0,
...@@ -5182,18 +5209,6 @@ pub const cc_t = switch (native_arch) {...@@ -5182,18 +5209,6 @@ pub const cc_t = switch (native_arch) {
51825209
5183pub const tcflag_t = u32;5210pub const tcflag_t = u32;
51845211
5185pub const CSIZE: tcflag_t = 48;
5186pub const CS5: tcflag_t = 0;
5187pub const CS6: tcflag_t = 16;
5188pub const CS7: tcflag_t = 32;
5189pub const CS8: tcflag_t = 48;
5190pub const CSTOPB: tcflag_t = 64;
5191pub const CREAD: tcflag_t = 128;
5192pub const PARENB: tcflag_t = 256;
5193pub const PARODD: tcflag_t = 512;
5194pub const HUPCL: tcflag_t = 1024;
5195pub const CLOCAL: tcflag_t = 2048;
5196
5197pub const ISIG: tcflag_t = 1;5212pub const ISIG: tcflag_t = 1;
5198pub const ICANON: tcflag_t = 2;5213pub const ICANON: tcflag_t = 2;
5199pub const ECHO: tcflag_t = 8;5214pub const ECHO: tcflag_t = 8;
...@@ -5215,7 +5230,7 @@ pub const termios = switch (native_arch) {...@@ -5215,7 +5230,7 @@ pub const termios = switch (native_arch) {
5215 .powerpc, .powerpcle, .powerpc64, .powerpc64le => extern struct {5230 .powerpc, .powerpcle, .powerpc64, .powerpc64le => extern struct {
5216 iflag: tc_iflag_t,5231 iflag: tc_iflag_t,
5217 oflag: tc_oflag_t,5232 oflag: tc_oflag_t,
5218 cflag: tcflag_t,5233 cflag: tc_cflag_t,
5219 lflag: tcflag_t,5234 lflag: tcflag_t,
5220 cc: [NCCS]cc_t,5235 cc: [NCCS]cc_t,
5221 line: cc_t,5236 line: cc_t,
...@@ -5225,7 +5240,7 @@ pub const termios = switch (native_arch) {...@@ -5225,7 +5240,7 @@ pub const termios = switch (native_arch) {
5225 else => extern struct {5240 else => extern struct {
5226 iflag: tc_iflag_t,5241 iflag: tc_iflag_t,
5227 oflag: tc_oflag_t,5242 oflag: tc_oflag_t,
5228 cflag: tcflag_t,5243 cflag: tc_cflag_t,
5229 lflag: tcflag_t,5244 lflag: tcflag_t,
5230 line: cc_t,5245 line: cc_t,
5231 cc: [NCCS]cc_t,5246 cc: [NCCS]cc_t,