authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 21:21:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 21:21:45-07:00
loga280ff2767cbb8da1a10d70eaddb0d6c0730443f
treee1355608beacddcecd71dbc8e732b45df7c18492
parente97fa8b03803dfb5d2046d9b2ebecbee85a0e1f9

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.

4 files changed, 155 insertions(+), 88 deletions(-)

lib/std/c.zig+105-17
...@@ -796,7 +796,7 @@ pub const termios = switch (native_os) {...@@ -796,7 +796,7 @@ pub const termios = switch (native_os) {
796 iflag: tc_iflag_t,796 iflag: tc_iflag_t,
797 oflag: tc_oflag_t,797 oflag: tc_oflag_t,
798 cflag: tc_cflag_t,798 cflag: tc_cflag_t,
799 lflag: tcflag_t,799 lflag: tc_lflag_t,
800 cc: [NCCS]cc_t,800 cc: [NCCS]cc_t,
801 ispeed: speed_t align(8),801 ispeed: speed_t align(8),
802 ospeed: speed_t,802 ospeed: speed_t,
...@@ -805,7 +805,7 @@ pub const termios = switch (native_os) {...@@ -805,7 +805,7 @@ pub const termios = switch (native_os) {
805 iflag: tc_iflag_t,805 iflag: tc_iflag_t,
806 oflag: tc_oflag_t,806 oflag: tc_oflag_t,
807 cflag: tc_cflag_t,807 cflag: tc_cflag_t,
808 lflag: tcflag_t,808 lflag: tc_lflag_t,
809 cc: [NCCS]cc_t,809 cc: [NCCS]cc_t,
810 ispeed: speed_t,810 ispeed: speed_t,
811 ospeed: speed_t,811 ospeed: speed_t,
...@@ -814,7 +814,7 @@ pub const termios = switch (native_os) {...@@ -814,7 +814,7 @@ pub const termios = switch (native_os) {
814 iflag: tc_iflag_t,814 iflag: tc_iflag_t,
815 oflag: tc_oflag_t,815 oflag: tc_oflag_t,
816 cflag: tc_cflag_t,816 cflag: tc_cflag_t,
817 lflag: tcflag_t,817 lflag: tc_lflag_t,
818 line: cc_t,818 line: cc_t,
819 ispeed: speed_t,819 ispeed: speed_t,
820 ospeed: speed_t,820 ospeed: speed_t,
...@@ -824,14 +824,14 @@ pub const termios = switch (native_os) {...@@ -824,14 +824,14 @@ pub const termios = switch (native_os) {
824 iflag: tc_iflag_t,824 iflag: tc_iflag_t,
825 oflag: tc_oflag_t,825 oflag: tc_oflag_t,
826 cflag: tc_cflag_t,826 cflag: tc_cflag_t,
827 lflag: tcflag_t,827 lflag: tc_lflag_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: tc_cflag_t,833 cflag: tc_cflag_t,
834 lflag: tcflag_t,834 lflag: tc_lflag_t,
835 line: std.c.cc_t,835 line: std.c.cc_t,
836 cc: [NCCS]cc_t,836 cc: [NCCS]cc_t,
837 ispeed: speed_t,837 ispeed: speed_t,
...@@ -1183,18 +1183,106 @@ pub const tc_cflag_t = switch (native_os) {...@@ -1183,18 +1183,106 @@ pub const tc_cflag_t = switch (native_os) {
1183 else => @compileError("target libc does not have tc_cflag_t"),1183 else => @compileError("target libc does not have tc_cflag_t"),
1184};1184};
11851185
1186pub const tcflag_t = switch (native_os) {1186pub const tc_lflag_t = switch (native_os) {
1187 .linux => std.os.linux.tcflag_t,1187 .linux => std.os.linux.tc_lflag_t,
1188 .macos, .ios, .tvos, .watchos => u64,1188 .macos, .ios, .tvos, .watchos, .netbsd, .freebsd, .kfreebsd, .dragonfly => packed struct(u32) {
1189 .freebsd, .kfreebsd => c_uint,1189 ECHOKE: bool = false,
1190 .netbsd => c_uint,1190 ECHOE: bool = false,
1191 .dragonfly => c_uint,1191 ECHOK: bool = false,
1192 .openbsd => c_uint,1192 ECHO: bool = false,
1193 .haiku => u32,1193 ECHONL: bool = false,
1194 .solaris, .illumos => c_uint,1194 ECHOPRT: bool = false,
1195 .emscripten => u32,1195 ECHOCTL: bool = false,
1196 .wasi => c_uint,1196 ISIG: bool = false,
1197 else => @compileError("target libc does not have tcflag_t"),1197 ICANON: bool = false,
1198 ALTWERASE: bool = false,
1199 IEXTEN: bool = false,
1200 EXTPROC: bool = false,
1201 _12: u10 = 0,
1202 TOSTOP: bool = false,
1203 FLUSHO: bool = false,
1204 _24: u1 = 0,
1205 NOKERNINFO: bool = false,
1206 _26: u3 = 0,
1207 PENDIN: bool = false,
1208 _30: u1 = 0,
1209 NOFLSH: bool = false,
1210 },
1211 .openbsd => packed struct(u32) {
1212 ECHOKE: bool = false,
1213 ECHOE: bool = false,
1214 ECHOK: bool = false,
1215 ECHO: bool = false,
1216 ECHONL: bool = false,
1217 ECHOPRT: bool = false,
1218 ECHOCTL: bool = false,
1219 ISIG: bool = false,
1220 ICANON: bool = false,
1221 ALTWERASE: bool = false,
1222 IEXTEN: bool = false,
1223 EXTPROC: bool = false,
1224 _12: u10 = 0,
1225 TOSTOP: bool = false,
1226 FLUSHO: bool = false,
1227 XCASE: bool = false,
1228 NOKERNINFO: bool = false,
1229 _26: u3 = 0,
1230 PENDIN: bool = false,
1231 _30: u1 = 0,
1232 NOFLSH: bool = false,
1233 },
1234 .haiku => packed struct(u32) {
1235 ISIG: bool = false,
1236 ICANON: bool = false,
1237 XCASE: bool = false,
1238 ECHO: bool = false,
1239 ECHOE: bool = false,
1240 ECHOK: bool = false,
1241 ECHONL: bool = false,
1242 NOFLSH: bool = false,
1243 TOSTOP: bool = false,
1244 IEXTEN: bool = false,
1245 ECHOCTL: bool = false,
1246 ECHOPRT: bool = false,
1247 ECHOKE: bool = false,
1248 FLUSHO: bool = false,
1249 PENDIN: bool = false,
1250 _: u17 = 0,
1251 },
1252 .solaris, .illumos => packed struct(u32) {
1253 ISIG: bool = false,
1254 ICANON: bool = false,
1255 XCASE: bool = false,
1256 ECHO: bool = false,
1257 ECHOE: bool = false,
1258 ECHOK: bool = false,
1259 ECHONL: bool = false,
1260 NOFLSH: bool = false,
1261 TOSTOP: bool = false,
1262 ECHOCTL: bool = false,
1263 ECHOPRT: bool = false,
1264 ECHOKE: bool = false,
1265 DEFECHO: bool = false,
1266 FLUSHO: bool = false,
1267 PENDIN: bool = false,
1268 IEXTEN: bool = false,
1269 _: u16 = 0,
1270 },
1271 .wasi, .emscripten => packed struct(u32) {
1272 ISIG: bool = false,
1273 ICANON: bool = false,
1274 _2: u1 = 0,
1275 ECHO: bool = false,
1276 ECHOE: bool = false,
1277 ECHOK: bool = false,
1278 ECHONL: bool = false,
1279 NOFLSH: bool = false,
1280 TOSTOP: bool = false,
1281 _9: u6 = 0,
1282 IEXTEN: bool = false,
1283 _: u16 = 0,
1284 },
1285 else => @compileError("target libc does not have tc_lflag_t"),
1198};1286};
11991287
1200pub const speed_t = switch (native_os) {1288pub const speed_t = switch (native_os) {
lib/std/c/darwin.zig-56
...@@ -2692,28 +2692,6 @@ pub const SHUT = struct {...@@ -2692,28 +2692,6 @@ pub const SHUT = struct {
2692 pub const RDWR = 2;2692 pub const RDWR = 2;
2693};2693};
26942694
2695pub const ECHOKE: tcflag_t = 0x00000001; // visual erase for line kill
2696pub const ECHOE: tcflag_t = 0x00000002; // visually erase chars
2697pub const ECHOK: tcflag_t = 0x00000004; // echo NL after line kill
2698pub const ECHO: tcflag_t = 0x00000008; // enable echoing
2699pub const ECHONL: tcflag_t = 0x00000010; // echo NL even if ECHO is off
2700pub const ECHOPRT: tcflag_t = 0x00000020; // visual erase mode for hardcopy
2701pub const ECHOCTL: tcflag_t = 0x00000040; // echo control chars as ^(Char)
2702pub const ISIG: tcflag_t = 0x00000080; // enable signals INTR, QUIT, [D]SUSP
2703pub const ICANON: tcflag_t = 0x00000100; // canonicalize input lines
2704pub const ALTWERASE: tcflag_t = 0x00000200; // use alternate WERASE algorithm
2705pub const IEXTEN: tcflag_t = 0x00000400; // enable DISCARD and LNEXT
2706pub const EXTPROC: tcflag_t = 0x00000800; // external processing
2707pub const TOSTOP: tcflag_t = 0x00400000; // stop background jobs from output
2708pub const FLUSHO: tcflag_t = 0x00800000; // output being flushed (state)
2709pub const NOKERNINFO: tcflag_t = 0x02000000; // no kernel output from VSTATUS
2710pub const PENDIN: tcflag_t = 0x20000000; // XXX retype pending input (state)
2711pub const NOFLSH: tcflag_t = 0x80000000; // don't flush after interrupt
2712
2713pub const TCSANOW: tcflag_t = 0; // make change immediate
2714pub const TCSADRAIN: tcflag_t = 1; // drain output, then change
2715pub const TCSAFLUSH: tcflag_t = 2; // drain output, flush input
2716pub const TCSASOFT: tcflag_t = 0x10; // flag - don't alter h.w. state
2717pub const TCSA = enum(c_uint) {2695pub const TCSA = enum(c_uint) {
2718 NOW,2696 NOW,
2719 DRAIN,2697 DRAIN,
...@@ -2721,40 +2699,6 @@ pub const TCSA = enum(c_uint) {...@@ -2721,40 +2699,6 @@ pub const TCSA = enum(c_uint) {
2721 _,2699 _,
2722};2700};
27232701
2724pub const B0: tcflag_t = 0;
2725pub const B50: tcflag_t = 50;
2726pub const B75: tcflag_t = 75;
2727pub const B110: tcflag_t = 110;
2728pub const B134: tcflag_t = 134;
2729pub const B150: tcflag_t = 150;
2730pub const B200: tcflag_t = 200;
2731pub const B300: tcflag_t = 300;
2732pub const B600: tcflag_t = 600;
2733pub const B1200: tcflag_t = 1200;
2734pub const B1800: tcflag_t = 1800;
2735pub const B2400: tcflag_t = 2400;
2736pub const B4800: tcflag_t = 4800;
2737pub const B9600: tcflag_t = 9600;
2738pub const B19200: tcflag_t = 19200;
2739pub const B38400: tcflag_t = 38400;
2740pub const B7200: tcflag_t = 7200;
2741pub const B14400: tcflag_t = 14400;
2742pub const B28800: tcflag_t = 28800;
2743pub const B57600: tcflag_t = 57600;
2744pub const B76800: tcflag_t = 76800;
2745pub const B115200: tcflag_t = 115200;
2746pub const B230400: tcflag_t = 230400;
2747pub const EXTA: tcflag_t = 19200;
2748pub const EXTB: tcflag_t = 38400;
2749
2750pub const TCIFLUSH: tcflag_t = 1;
2751pub const TCOFLUSH: tcflag_t = 2;
2752pub const TCIOFLUSH: tcflag_t = 3;
2753pub const TCOOFF: tcflag_t = 1;
2754pub const TCOON: tcflag_t = 2;
2755pub const TCIOFF: tcflag_t = 3;
2756pub const TCION: tcflag_t = 4;
2757
2758pub const winsize = extern struct {2702pub const winsize = extern struct {
2759 ws_row: u16,2703 ws_row: u16,
2760 ws_col: u16,2704 ws_col: u16,
lib/std/os.zig+1-1
...@@ -187,10 +187,10 @@ pub const utsname = system.utsname;...@@ -187,10 +187,10 @@ pub const utsname = system.utsname;
187pub const CSIZE = system.CSIZE;187pub const CSIZE = system.CSIZE;
188pub const NCCS = system.NCCS;188pub const NCCS = system.NCCS;
189pub const speed_t = system.speed_t;189pub const speed_t = system.speed_t;
190pub const tcflag_t = system.tcflag_t;
191pub const tc_iflag_t = system.tc_iflag_t;190pub const tc_iflag_t = system.tc_iflag_t;
192pub const tc_oflag_t = system.tc_oflag_t;191pub const tc_oflag_t = system.tc_oflag_t;
193pub const tc_cflag_t = system.tc_cflag_t;192pub const tc_cflag_t = system.tc_cflag_t;
193pub const tc_lflag_t = system.tc_lflag_t;
194194
195pub const F_OK = system.F_OK;195pub const F_OK = system.F_OK;
196pub const R_OK = system.R_OK;196pub const R_OK = system.R_OK;
lib/std/os/linux.zig+49-14
...@@ -5147,6 +5147,53 @@ pub const tc_cflag_t = switch (native_arch) {...@@ -5147,6 +5147,53 @@ pub const tc_cflag_t = switch (native_arch) {
5147 },5147 },
5148};5148};
51495149
5150pub const tc_lflag_t = switch (native_arch) {
5151 .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
5152 _0: u1 = 0,
5153 ECHOE: bool = false,
5154 ECHOK: bool = false,
5155 ECHO: bool = false,
5156 ECHONL: bool = false,
5157 _5: u2 = 0,
5158 ISIG: bool = false,
5159 ICANON: bool = false,
5160 _9: u1 = 0,
5161 IEXTEN: bool = false,
5162 _11: u11 = 0,
5163 TOSTOP: bool = false,
5164 _23: u8 = 0,
5165 NOFLSH: bool = false,
5166 },
5167 .mips, .mipsel, .mips64, .mips64el => packed struct(u32) {
5168 ISIG: bool = false,
5169 ICANON: bool = false,
5170 _2: u1 = 0,
5171 ECHO: bool = false,
5172 ECHOE: bool = false,
5173 ECHOK: bool = false,
5174 ECHONL: bool = false,
5175 NOFLSH: bool = false,
5176 IEXTEN: bool = false,
5177 _9: u6 = 0,
5178 TOSTOP: bool = false,
5179 _: u16 = 0,
5180 },
5181 else => packed struct(u32) {
5182 ISIG: bool = false,
5183 ICANON: bool = false,
5184 _2: u1 = 0,
5185 ECHO: bool = false,
5186 ECHOE: bool = false,
5187 ECHOK: bool = false,
5188 ECHONL: bool = false,
5189 NOFLSH: bool = false,
5190 TOSTOP: bool = false,
5191 _9: u6 = 0,
5192 IEXTEN: bool = false,
5193 _: u16 = 0,
5194 },
5195};
5196
5150pub const cc_t = switch (native_arch) {5197pub const cc_t = switch (native_arch) {
5151 .mips, .mipsel, .mips64, .mips64el => enum(u8) {5198 .mips, .mipsel, .mips64, .mips64el => enum(u8) {
5152 VINTR = 0,5199 VINTR = 0,
...@@ -5207,18 +5254,6 @@ pub const cc_t = switch (native_arch) {...@@ -5207,18 +5254,6 @@ pub const cc_t = switch (native_arch) {
5207 },5254 },
5208};5255};
52095256
5210pub const tcflag_t = u32;
5211
5212pub const ISIG: tcflag_t = 1;
5213pub const ICANON: tcflag_t = 2;
5214pub const ECHO: tcflag_t = 8;
5215pub const ECHOE: tcflag_t = 16;
5216pub const ECHOK: tcflag_t = 32;
5217pub const ECHONL: tcflag_t = 64;
5218pub const NOFLSH: tcflag_t = 128;
5219pub const TOSTOP: tcflag_t = 256;
5220pub const IEXTEN: tcflag_t = 32768;
5221
5222pub const TCSA = enum(c_uint) {5257pub const TCSA = enum(c_uint) {
5223 NOW,5258 NOW,
5224 DRAIN,5259 DRAIN,
...@@ -5231,7 +5266,7 @@ pub const termios = switch (native_arch) {...@@ -5231,7 +5266,7 @@ pub const termios = switch (native_arch) {
5231 iflag: tc_iflag_t,5266 iflag: tc_iflag_t,
5232 oflag: tc_oflag_t,5267 oflag: tc_oflag_t,
5233 cflag: tc_cflag_t,5268 cflag: tc_cflag_t,
5234 lflag: tcflag_t,5269 lflag: tc_lflag_t,
5235 cc: [NCCS]cc_t,5270 cc: [NCCS]cc_t,
5236 line: cc_t,5271 line: cc_t,
5237 ispeed: speed_t,5272 ispeed: speed_t,
...@@ -5241,7 +5276,7 @@ pub const termios = switch (native_arch) {...@@ -5241,7 +5276,7 @@ pub const termios = switch (native_arch) {
5241 iflag: tc_iflag_t,5276 iflag: tc_iflag_t,
5242 oflag: tc_oflag_t,5277 oflag: tc_oflag_t,
5243 cflag: tc_cflag_t,5278 cflag: tc_cflag_t,
5244 lflag: tcflag_t,5279 lflag: tc_lflag_t,
5245 line: cc_t,5280 line: cc_t,
5246 cc: [NCCS]cc_t,5281 cc: [NCCS]cc_t,
5247 ispeed: speed_t,5282 ispeed: speed_t,