authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 17:28:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 17:28:09-07:00
log20abc0caeeb8a9cc1cc1fe8c8ae318ec2906cc24
tree6bfbd64047a372b333416bc0d9e56253cf351290
parent47643cc5cc4db4c0fb2aaa7f37cedb049fce7def

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.

6 files changed, 136 insertions(+), 74 deletions(-)

lib/std/c.zig+96-5
......@@ -794,7 +794,7 @@ pub const termios = switch (native_os) {
794794 .linux => std.os.linux.termios,
795795 .macos, .ios, .tvos, .watchos => extern struct {
796796 iflag: tc_iflag_t,
797 oflag: tcflag_t,
797 oflag: tc_oflag_t,
798798 cflag: tcflag_t,
799799 lflag: tcflag_t,
800800 cc: [NCCS]cc_t,
......@@ -803,7 +803,7 @@ pub const termios = switch (native_os) {
803803 },
804804 .freebsd, .kfreebsd, .netbsd, .dragonfly, .openbsd => extern struct {
805805 iflag: tc_iflag_t,
806 oflag: tcflag_t,
806 oflag: tc_oflag_t,
807807 cflag: tcflag_t,
808808 lflag: tcflag_t,
809809 cc: [NCCS]cc_t,
......@@ -812,7 +812,7 @@ pub const termios = switch (native_os) {
812812 },
813813 .haiku => extern struct {
814814 iflag: tc_iflag_t,
815 oflag: tcflag_t,
815 oflag: tc_oflag_t,
816816 cflag: tcflag_t,
817817 lflag: tcflag_t,
818818 line: cc_t,
......@@ -822,14 +822,14 @@ pub const termios = switch (native_os) {
822822 },
823823 .solaris, .illumos => extern struct {
824824 iflag: tc_iflag_t,
825 oflag: tcflag_t,
825 oflag: tc_oflag_t,
826826 cflag: tcflag_t,
827827 lflag: tcflag_t,
828828 cc: [NCCS]cc_t,
829829 },
830830 .emscripten, .wasi => extern struct {
831831 iflag: tc_iflag_t,
832 oflag: tcflag_t,
832 oflag: tc_oflag_t,
833833 cflag: tcflag_t,
834834 lflag: tcflag_t,
835835 line: std.c.cc_t,
......@@ -950,6 +950,97 @@ pub const tc_iflag_t = switch (native_os) {
950950 else => @compileError("target libc does not have tc_iflag_t"),
951951};
952952
953pub const tc_oflag_t = switch (native_os) {
954 .linux => std.os.linux.tc_oflag_t,
955 .macos, .ios, .tvos, .watchos => packed struct(u32) {
956 OPOST: bool = false,
957 ONLCR: bool = false,
958 OXTABS: bool = false,
959 ONOEOT: bool = false,
960 OCRNL: bool = false,
961 ONOCR: bool = false,
962 ONLRET: bool = false,
963 OFILL: bool = false,
964 NLDLY: u2 = 0,
965 TABDLY: u2 = 0,
966 CRDLY: u2 = 0,
967 FFDLY: u1 = 0,
968 BSDLY: u1 = 0,
969 VTDLY: u1 = 0,
970 OFDEL: bool = false,
971 _: u14 = 0,
972 },
973 .netbsd => packed struct(u32) {
974 OPOST: bool = false,
975 ONLCR: bool = false,
976 OXTABS: bool = false,
977 ONOEOT: bool = false,
978 OCRNL: bool = false,
979 _5: u1 = 0,
980 ONOCR: bool = false,
981 ONLRET: bool = false,
982 _: u24 = 0,
983 },
984 .openbsd => packed struct(u32) {
985 OPOST: bool = false,
986 ONLCR: bool = false,
987 OXTABS: bool = false,
988 ONOEOT: bool = false,
989 OCRNL: bool = false,
990 OLCUC: bool = false,
991 ONOCR: bool = false,
992 ONLRET: bool = false,
993 _: u24 = 0,
994 },
995 .freebsd, .kfreebsd, .dragonfly => packed struct(u32) {
996 OPOST: bool = false,
997 ONLCR: bool = false,
998 _2: u1 = 0,
999 ONOEOT: bool = false,
1000 OCRNL: bool = false,
1001 ONOCR: bool = false,
1002 ONLRET: bool = false,
1003 _: u25 = 0,
1004 },
1005 .solaris, .illumos => packed struct(u32) {
1006 OPOST: bool = false,
1007 OLCUC: bool = false,
1008 ONLCR: bool = false,
1009 OCRNL: bool = false,
1010 ONOCR: bool = false,
1011 ONLRET: bool = false,
1012 OFILL: bool = false,
1013 OFDEL: bool = false,
1014 NLDLY: u1 = 0,
1015 CRDLY: u2 = 0,
1016 TABDLY: u2 = 0,
1017 BSDLY: u1 = 0,
1018 VTDLY: u1 = 0,
1019 FFDLY: u1 = 0,
1020 PAGEOUT: bool = false,
1021 WRAP: bool = false,
1022 _: u14 = 0,
1023 },
1024 .haiku, .wasi, .emscripten => packed struct(u32) {
1025 OPOST: bool = false,
1026 OLCUC: bool = false,
1027 ONLCR: bool = false,
1028 OCRNL: bool = false,
1029 ONOCR: bool = false,
1030 ONLRET: bool = false,
1031 OFILL: bool = false,
1032 OFDEL: bool = false,
1033 NLDLY: u1 = 0,
1034 CRDLY: u2 = 0,
1035 TABDLY: u2 = 0,
1036 BSDLY: u1 = 0,
1037 VTDLY: u1 = 0,
1038 FFDLY: u1 = 0,
1039 _: u16 = 0,
1040 },
1041 else => @compileError("target libc does not have tc_oflag_t"),
1042};
1043
9531044pub const tcflag_t = switch (native_os) {
9541045 .linux => std.os.linux.tcflag_t,
9551046 .macos, .ios, .tvos, .watchos => u64,
lib/std/c/darwin.zig-36
......@@ -2692,42 +2692,6 @@ pub const SHUT = struct {
26922692 pub const RDWR = 2;
26932693};
26942694
2695pub const OPOST: tcflag_t = 0x00000001; //enable following output processing
2696pub const ONLCR: tcflag_t = 0x00000002; // map NL to CR-NL (ala CRMOD)
2697pub const OXTABS: tcflag_t = 0x00000004; // expand tabs to spaces
2698pub const ONOEOT: tcflag_t = 0x00000008; // discard EOT's (^D) on output)
2699
2700pub const OCRNL: tcflag_t = 0x00000010; // map CR to NL on output
2701pub const ONOCR: tcflag_t = 0x00000020; // no CR output at column 0
2702pub const ONLRET: tcflag_t = 0x00000040; // NL performs CR function
2703pub const OFILL: tcflag_t = 0x00000080; // use fill characters for delay
2704pub const NLDLY: tcflag_t = 0x00000300; // \n delay
2705pub const TABDLY: tcflag_t = 0x00000c04; // horizontal tab delay
2706pub const CRDLY: tcflag_t = 0x00003000; // \r delay
2707pub const FFDLY: tcflag_t = 0x00004000; // form feed delay
2708pub const BSDLY: tcflag_t = 0x00008000; // \b delay
2709pub const VTDLY: tcflag_t = 0x00010000; // vertical tab delay
2710pub const OFDEL: tcflag_t = 0x00020000; // fill is DEL, else NUL
2711
2712pub const NL0: tcflag_t = 0x00000000;
2713pub const NL1: tcflag_t = 0x00000100;
2714pub const NL2: tcflag_t = 0x00000200;
2715pub const NL3: tcflag_t = 0x00000300;
2716pub const TAB0: tcflag_t = 0x00000000;
2717pub const TAB1: tcflag_t = 0x00000400;
2718pub const TAB2: tcflag_t = 0x00000800;
2719pub const TAB3: tcflag_t = 0x00000004;
2720pub const CR0: tcflag_t = 0x00000000;
2721pub const CR1: tcflag_t = 0x00001000;
2722pub const CR2: tcflag_t = 0x00002000;
2723pub const CR3: tcflag_t = 0x00003000;
2724pub const FF0: tcflag_t = 0x00000000;
2725pub const FF1: tcflag_t = 0x00004000;
2726pub const BS0: tcflag_t = 0x00000000;
2727pub const BS1: tcflag_t = 0x00008000;
2728pub const VT0: tcflag_t = 0x00000000;
2729pub const VT1: tcflag_t = 0x00010000;
2730
27312695pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
27322696pub const CSIZE: tcflag_t = 0x00000300; // character size mask
27332697pub const CS5: tcflag_t = 0x00000000; // 5 bits (pseudo)
lib/std/c/netbsd.zig-9
......@@ -807,15 +807,6 @@ pub const T = struct {
807807};
808808
809809
810// Output flags - software output processing
811pub const OPOST: tcflag_t = 0x00000001; // enable following output processing
812pub const ONLCR: tcflag_t = 0x00000002; // map NL to CR-NL (ala CRMOD)
813pub const OXTABS: tcflag_t = 0x00000004; // expand tabs to spaces
814pub const ONOEOT: tcflag_t = 0x00000008; // discard EOT's (^D) on output
815pub const OCRNL: tcflag_t = 0x00000010; // map CR to NL
816pub const ONOCR: tcflag_t = 0x00000040; // discard CR's when on column 0
817pub const ONLRET: tcflag_t = 0x00000080; // move to column 0 on CR
818
819810// Control flags - hardware control of terminal
820811pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
821812pub const CSIZE: tcflag_t = 0x00000300; // character size mask
lib/std/c/openbsd.zig-9
......@@ -769,15 +769,6 @@ pub const AUTH = struct {
769769};
770770
771771
772// Output flags - software output processing
773pub const OPOST: tcflag_t = 0x00000001; // enable following output processing
774pub const ONLCR: tcflag_t = 0x00000002; // map NL to CR-NL (ala CRMOD)
775pub const OXTABS: tcflag_t = 0x00000004; // expand tabs to spaces
776pub const ONOEOT: tcflag_t = 0x00000008; // discard EOT's (^D) on output
777pub const OCRNL: tcflag_t = 0x00000010; // map CR to NL
778pub const OLCUC: tcflag_t = 0x00000020; // translate lower case to upper case
779pub const ONOCR: tcflag_t = 0x00000040; // No CR output at column 0
780pub const ONLRET: tcflag_t = 0x00000080; // NL performs the CR function
781772
782773// Control flags - hardware control of terminal
783774pub const CIGNORE: tcflag_t = 0x00000001; // ignore control flags
lib/std/os.zig+1
......@@ -188,6 +188,7 @@ pub const NCCS = system.NCCS;
188188pub const speed_t = system.speed_t;
189189pub const tcflag_t = system.tcflag_t;
190190pub const tc_iflag_t = system.tc_iflag_t;
191pub const tc_oflag_t = system.tc_oflag_t;
191192
192193pub const F_OK = system.F_OK;
193194pub const R_OK = system.R_OK;
lib/std/os/linux.zig+39-15
......@@ -5083,6 +5083,43 @@ pub const tc_iflag_t = switch (native_arch) {
50835083 },
50845084};
50855085
5086pub const tc_oflag_t = switch (native_arch) {
5087 .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
5088 OPOST: bool = false,
5089 ONLCR: bool = false,
5090 OLCUC: bool = false,
5091 OCRNL: bool = false,
5092 ONOCR: bool = false,
5093 ONLRET: bool = false,
5094 OFILL: bool = false,
5095 OFDEL: bool = false,
5096 NLDLY: u2 = 0,
5097 TABDLY: u2 = 0,
5098 CRDLY: u2 = 0,
5099 FFDLY: u1 = 0,
5100 BSDLY: u1 = 0,
5101 VTDLY: u1 = 0,
5102 _: u15 = 0,
5103 },
5104 else => packed struct(u32) {
5105 OPOST: bool = false,
5106 OLCUC: bool = false,
5107 ONLCR: bool = false,
5108 OCRNL: bool = false,
5109 ONOCR: bool = false,
5110 ONLRET: bool = false,
5111 OFILL: bool = false,
5112 OFDEL: bool = false,
5113 NLDLY: u1 = 0,
5114 CRDLY: u2 = 0,
5115 TABDLY: u2 = 0,
5116 BSDLY: u1 = 0,
5117 VTDLY: u1 = 0,
5118 FFDLY: u1 = 0,
5119 _: u16 = 0,
5120 },
5121};
5122
50865123pub const cc_t = switch (native_arch) {
50875124 .mips, .mipsel, .mips64, .mips64el => enum(u8) {
50885125 VINTR = 0,
......@@ -5145,19 +5182,6 @@ pub const cc_t = switch (native_arch) {
51455182
51465183pub const tcflag_t = u32;
51475184
5148pub const OPOST: tcflag_t = 1;
5149pub const OLCUC: tcflag_t = 2;
5150pub const ONLCR: tcflag_t = 4;
5151pub const OCRNL: tcflag_t = 8;
5152pub const ONOCR: tcflag_t = 16;
5153pub const ONLRET: tcflag_t = 32;
5154pub const OFILL: tcflag_t = 64;
5155pub const OFDEL: tcflag_t = 128;
5156
5157pub const VTDLY: tcflag_t = 16384;
5158pub const VT0: tcflag_t = 0;
5159pub const VT1: tcflag_t = 16384;
5160
51615185pub const CSIZE: tcflag_t = 48;
51625186pub const CS5: tcflag_t = 0;
51635187pub const CS6: tcflag_t = 16;
......@@ -5190,7 +5214,7 @@ pub const TCSA = enum(c_uint) {
51905214pub const termios = switch (native_arch) {
51915215 .powerpc, .powerpcle, .powerpc64, .powerpc64le => extern struct {
51925216 iflag: tc_iflag_t,
5193 oflag: tcflag_t,
5217 oflag: tc_oflag_t,
51945218 cflag: tcflag_t,
51955219 lflag: tcflag_t,
51965220 cc: [NCCS]cc_t,
......@@ -5200,7 +5224,7 @@ pub const termios = switch (native_arch) {
52005224 },
52015225 else => extern struct {
52025226 iflag: tc_iflag_t,
5203 oflag: tcflag_t,
5227 oflag: tc_oflag_t,
52045228 cflag: tcflag_t,
52055229 lflag: tcflag_t,
52065230 line: cc_t,