authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 16:43:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 16:43:51-07:00
log47643cc5cc4db4c0fb2aaa7f37cedb049fce7def
treeb1028914cc897c70278973d47239dfb41efb57c1
parent0c88f927f161224f5c5ce2b12a76133a421af081

std.os.termios: add type safety to iflag field

This creates `tc_iflag_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, 161 insertions(+), 72 deletions(-)

lib/std/c.zig+115-5
...@@ -793,7 +793,7 @@ pub const NCCS = switch (native_os) {...@@ -793,7 +793,7 @@ pub const NCCS = switch (native_os) {
793pub const termios = switch (native_os) {793pub const termios = switch (native_os) {
794 .linux => std.os.linux.termios,794 .linux => std.os.linux.termios,
795 .macos, .ios, .tvos, .watchos => extern struct {795 .macos, .ios, .tvos, .watchos => extern struct {
796 iflag: tcflag_t,796 iflag: tc_iflag_t,
797 oflag: tcflag_t,797 oflag: tcflag_t,
798 cflag: tcflag_t,798 cflag: tcflag_t,
799 lflag: tcflag_t,799 lflag: tcflag_t,
...@@ -802,7 +802,7 @@ pub const termios = switch (native_os) {...@@ -802,7 +802,7 @@ pub const termios = switch (native_os) {
802 ospeed: speed_t,802 ospeed: speed_t,
803 },803 },
804 .freebsd, .kfreebsd, .netbsd, .dragonfly, .openbsd => extern struct {804 .freebsd, .kfreebsd, .netbsd, .dragonfly, .openbsd => extern struct {
805 iflag: tcflag_t,805 iflag: tc_iflag_t,
806 oflag: tcflag_t,806 oflag: tcflag_t,
807 cflag: tcflag_t,807 cflag: tcflag_t,
808 lflag: tcflag_t,808 lflag: tcflag_t,
...@@ -811,7 +811,7 @@ pub const termios = switch (native_os) {...@@ -811,7 +811,7 @@ pub const termios = switch (native_os) {
811 ospeed: speed_t,811 ospeed: speed_t,
812 },812 },
813 .haiku => extern struct {813 .haiku => extern struct {
814 iflag: tcflag_t,814 iflag: tc_iflag_t,
815 oflag: tcflag_t,815 oflag: tcflag_t,
816 cflag: tcflag_t,816 cflag: tcflag_t,
817 lflag: tcflag_t,817 lflag: tcflag_t,
...@@ -821,14 +821,14 @@ pub const termios = switch (native_os) {...@@ -821,14 +821,14 @@ pub const termios = switch (native_os) {
821 cc: [NCCS]cc_t,821 cc: [NCCS]cc_t,
822 },822 },
823 .solaris, .illumos => extern struct {823 .solaris, .illumos => extern struct {
824 iflag: tcflag_t,824 iflag: tc_iflag_t,
825 oflag: tcflag_t,825 oflag: tcflag_t,
826 cflag: tcflag_t,826 cflag: tcflag_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: tcflag_t,831 iflag: tc_iflag_t,
832 oflag: tcflag_t,832 oflag: tcflag_t,
833 cflag: tcflag_t,833 cflag: tcflag_t,
834 lflag: tcflag_t,834 lflag: tcflag_t,
...@@ -840,6 +840,116 @@ pub const termios = switch (native_os) {...@@ -840,6 +840,116 @@ pub const termios = switch (native_os) {
840 else => @compileError("target libc does not have termios"),840 else => @compileError("target libc does not have termios"),
841};841};
842842
843pub const tc_iflag_t = switch (native_os) {
844 .linux => std.os.linux.tc_iflag_t,
845 .macos, .ios, .tvos, .watchos => packed struct(u32) {
846 IGNBRK: bool = false,
847 BRKINT: bool = false,
848 IGNPAR: bool = false,
849 PARMRK: bool = false,
850 INPCK: bool = false,
851 ISTRIP: bool = false,
852 INLCR: bool = false,
853 IGNCR: bool = false,
854 ICRNL: bool = false,
855 IXON: bool = false,
856 IXOFF: bool = false,
857 IXANY: bool = false,
858 _12: u1 = 0,
859 IMAXBEL: bool = false,
860 IUTF8: bool = false,
861 _: u17 = 0,
862 },
863 .netbsd, .freebsd, .kfreebsd, .dragonfly => packed struct(u32) {
864 IGNBRK: bool = false,
865 BRKINT: bool = false,
866 IGNPAR: bool = false,
867 PARMRK: bool = false,
868 INPCK: bool = false,
869 ISTRIP: bool = false,
870 INLCR: bool = false,
871 IGNCR: bool = false,
872 ICRNL: bool = false,
873 IXON: bool = false,
874 IXOFF: bool = false,
875 IXANY: bool = false,
876 _12: u1 = 0,
877 IMAXBEL: bool = false,
878 _: u18 = 0,
879 },
880 .openbsd => packed struct(u32) {
881 IGNBRK: bool = false,
882 BRKINT: bool = false,
883 IGNPAR: bool = false,
884 PARMRK: bool = false,
885 INPCK: bool = false,
886 ISTRIP: bool = false,
887 INLCR: bool = false,
888 IGNCR: bool = false,
889 ICRNL: bool = false,
890 IXON: bool = false,
891 IXOFF: bool = false,
892 IXANY: bool = false,
893 IUCLC: bool = false,
894 IMAXBEL: bool = false,
895 _: u18 = 0,
896 },
897 .haiku => packed struct(u32) {
898 IGNBRK: bool = false,
899 BRKINT: bool = false,
900 IGNPAR: bool = false,
901 PARMRK: bool = false,
902 INPCK: bool = false,
903 ISTRIP: bool = false,
904 INLCR: bool = false,
905 IGNCR: bool = false,
906 ICRNL: bool = false,
907 IUCLC: bool = false,
908 IXON: bool = false,
909 IXANY: bool = false,
910 IXOFF: bool = false,
911 _: u19 = 0,
912 },
913 .solaris, .illumos => packed struct(u32) {
914 IGNBRK: bool = false,
915 BRKINT: bool = false,
916 IGNPAR: bool = false,
917 PARMRK: bool = false,
918 INPCK: bool = false,
919 ISTRIP: bool = false,
920 INLCR: bool = false,
921 IGNCR: bool = false,
922 ICRNL: bool = false,
923 IUCLC: bool = false,
924 IXON: bool = false,
925 IXANY: bool = false,
926 _12: u1 = 0,
927 IMAXBEL: bool = false,
928 _14: u1 = 0,
929 DOSMODE: bool = false,
930 _: u16 = 0,
931 },
932 .emscripten, .wasi => packed struct(u32) {
933 IGNBRK: bool = false,
934 BRKINT: bool = false,
935 IGNPAR: bool = false,
936 PARMRK: bool = false,
937 INPCK: bool = false,
938 ISTRIP: bool = false,
939 INLCR: bool = false,
940 IGNCR: bool = false,
941 ICRNL: bool = false,
942 IUCLC: bool = false,
943 IXON: bool = false,
944 IXANY: bool = false,
945 IXOFF: bool = false,
946 IMAXBEL: bool = false,
947 IUTF8: bool = false,
948 _: u17 = 0,
949 },
950 else => @compileError("target libc does not have tc_iflag_t"),
951};
952
843pub const tcflag_t = switch (native_os) {953pub const tcflag_t = switch (native_os) {
844 .linux => std.os.linux.tcflag_t,954 .linux => std.os.linux.tcflag_t,
845 .macos, .ios, .tvos, .watchos => u64,955 .macos, .ios, .tvos, .watchos => u64,
lib/std/c/darwin.zig-15
...@@ -2692,21 +2692,6 @@ pub const SHUT = struct {...@@ -2692,21 +2692,6 @@ pub const SHUT = struct {
2692 pub const RDWR = 2;2692 pub const RDWR = 2;
2693};2693};
26942694
2695pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
2696pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINTR
2697pub const IGNPAR: tcflag_t = 0x00000004; // ignore (discard) parity errors
2698pub const PARMRK: tcflag_t = 0x00000008; // mark parity and framing errors
2699pub const INPCK: tcflag_t = 0x00000010; // enable checking of parity errors
2700pub const ISTRIP: tcflag_t = 0x00000020; // strip 8th bit off chars
2701pub const INLCR: tcflag_t = 0x00000040; // map NL into CR
2702pub const IGNCR: tcflag_t = 0x00000080; // ignore CR
2703pub const ICRNL: tcflag_t = 0x00000100; // map CR to NL (ala CRMOD)
2704pub const IXON: tcflag_t = 0x00000200; // enable output flow control
2705pub const IXOFF: tcflag_t = 0x00000400; // enable input flow control
2706pub const IXANY: tcflag_t = 0x00000800; // any char will restart after stop
2707pub const IMAXBEL: tcflag_t = 0x00002000; // ring bell on input queue full
2708pub const IUTF8: tcflag_t = 0x00004000; // maintain state for UTF-8 VERASE
2709
2710pub const OPOST: tcflag_t = 0x00000001; //enable following output processing2695pub const OPOST: tcflag_t = 0x00000001; //enable following output processing
2711pub const ONLCR: tcflag_t = 0x00000002; // map NL to CR-NL (ala CRMOD)2696pub const ONLCR: tcflag_t = 0x00000002; // map NL to CR-NL (ala CRMOD)
2712pub const OXTABS: tcflag_t = 0x00000004; // expand tabs to spaces2697pub const OXTABS: tcflag_t = 0x00000004; // expand tabs to spaces
lib/std/c/netbsd.zig-14
...@@ -806,20 +806,6 @@ pub const T = struct {...@@ -806,20 +806,6 @@ pub const T = struct {
806 pub const IOCXMTFRAME = 0x80087444;806 pub const IOCXMTFRAME = 0x80087444;
807};807};
808808
809// Input flags - software input processing
810pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
811pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINT
812pub const IGNPAR: tcflag_t = 0x00000004; // ignore (discard) parity errors
813pub const PARMRK: tcflag_t = 0x00000008; // mark parity and framing errors
814pub const INPCK: tcflag_t = 0x00000010; // enable checking of parity errors
815pub const ISTRIP: tcflag_t = 0x00000020; // strip 8th bit off chars
816pub const INLCR: tcflag_t = 0x00000040; // map NL into CR
817pub const IGNCR: tcflag_t = 0x00000080; // ignore CR
818pub const ICRNL: tcflag_t = 0x00000100; // map CR to NL (ala CRMOD)
819pub const IXON: tcflag_t = 0x00000200; // enable output flow control
820pub const IXOFF: tcflag_t = 0x00000400; // enable input flow control
821pub const IXANY: tcflag_t = 0x00000800; // any char will restart after stop
822pub const IMAXBEL: tcflag_t = 0x00002000; // ring bell on input queue full
823809
824// Output flags - software output processing810// Output flags - software output processing
825pub const OPOST: tcflag_t = 0x00000001; // enable following output processing811pub const OPOST: tcflag_t = 0x00000001; // enable following output processing
lib/std/c/openbsd.zig-15
...@@ -768,21 +768,6 @@ pub const AUTH = struct {...@@ -768,21 +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// Input flags - software input processing
772pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
773pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINT
774pub const IGNPAR: tcflag_t = 0x00000004; // ignore (discard) parity errors
775pub const PARMRK: tcflag_t = 0x00000008; // mark parity and framing errors
776pub const INPCK: tcflag_t = 0x00000010; // enable checking of parity errors
777pub const ISTRIP: tcflag_t = 0x00000020; // strip 8th bit off chars
778pub const INLCR: tcflag_t = 0x00000040; // map NL into CR
779pub const IGNCR: tcflag_t = 0x00000080; // ignore CR
780pub const ICRNL: tcflag_t = 0x00000100; // map CR to NL (ala CRMOD)
781pub const IXON: tcflag_t = 0x00000200; // enable output flow control
782pub const IXOFF: tcflag_t = 0x00000400; // enable input flow control
783pub const IXANY: tcflag_t = 0x00000800; // any char will restart after stop
784pub const IUCLC: tcflag_t = 0x00001000; // translate upper to lower case
785pub const IMAXBEL: tcflag_t = 0x00002000; // ring bell on input queue full
786771
787// Output flags - software output processing772// Output flags - software output processing
788pub const OPOST: tcflag_t = 0x00000001; // enable following output processing773pub const OPOST: tcflag_t = 0x00000001; // enable following output processing
lib/std/os.zig+5-3
...@@ -106,7 +106,6 @@ pub const MFD = system.MFD;...@@ -106,7 +106,6 @@ pub const MFD = system.MFD;
106pub const MMAP2_UNIT = system.MMAP2_UNIT;106pub const MMAP2_UNIT = system.MMAP2_UNIT;
107pub const MSG = system.MSG;107pub const MSG = system.MSG;
108pub const NAME_MAX = system.NAME_MAX;108pub const NAME_MAX = system.NAME_MAX;
109pub const NCCS = system.NCCS;
110pub const O = system.O;109pub const O = system.O;
111pub const PATH_MAX = system.PATH_MAX;110pub const PATH_MAX = system.PATH_MAX;
112pub const POLL = system.POLL;111pub const POLL = system.POLL;
...@@ -173,9 +172,7 @@ pub const siginfo_t = system.siginfo_t;...@@ -173,9 +172,7 @@ pub const siginfo_t = system.siginfo_t;
173pub const sigset_t = system.sigset_t;172pub const sigset_t = system.sigset_t;
174pub const sockaddr = system.sockaddr;173pub const sockaddr = system.sockaddr;
175pub const socklen_t = system.socklen_t;174pub const socklen_t = system.socklen_t;
176pub const speed_t = system.speed_t;
177pub const stack_t = system.stack_t;175pub const stack_t = system.stack_t;
178pub const tcflag_t = system.tcflag_t;
179pub const termios = system.termios;176pub const termios = system.termios;
180pub const time_t = system.time_t;177pub const time_t = system.time_t;
181pub const timespec = system.timespec;178pub const timespec = system.timespec;
...@@ -187,6 +184,11 @@ pub const uid_t = system.uid_t;...@@ -187,6 +184,11 @@ pub const uid_t = system.uid_t;
187pub const user_desc = system.user_desc;184pub const user_desc = system.user_desc;
188pub const utsname = system.utsname;185pub const utsname = system.utsname;
189186
187pub const NCCS = system.NCCS;
188pub const speed_t = system.speed_t;
189pub const tcflag_t = system.tcflag_t;
190pub const tc_iflag_t = system.tc_iflag_t;
191
190pub const F_OK = system.F_OK;192pub const F_OK = system.F_OK;
191pub const R_OK = system.R_OK;193pub const R_OK = system.R_OK;
192pub const W_OK = system.W_OK;194pub const W_OK = system.W_OK;
lib/std/os/linux.zig+41-20
...@@ -5005,7 +5005,6 @@ pub const rusage = extern struct {...@@ -5005,7 +5005,6 @@ pub const rusage = extern struct {
5005};5005};
50065006
5007pub const speed_t = u32;5007pub const speed_t = u32;
5008pub const tcflag_t = u32;
50095008
5010pub const NCCS = switch (native_arch) {5009pub const NCCS = switch (native_arch) {
5011 .powerpc, .powerpcle, .powerpc64, .powerpc64le => 19,5010 .powerpc, .powerpcle, .powerpc64, .powerpc64le => 19,
...@@ -5045,23 +5044,43 @@ pub const B3000000 = 0o0010015;...@@ -5045,23 +5044,43 @@ pub const B3000000 = 0o0010015;
5045pub const B3500000 = 0o0010016;5044pub const B3500000 = 0o0010016;
5046pub const B4000000 = 0o0010017;5045pub const B4000000 = 0o0010017;
50475046
5048pub const tc_iflag_t = packed struct(u32) {5047pub const tc_iflag_t = switch (native_arch) {
5049 IGNBRK: bool = false,5048 .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
5050 BRKINT: bool = false,5049 IGNBRK: bool = false,
5051 IGNPAR: bool = false,5050 BRKINT: bool = false,
5052 PARMRK: bool = false,5051 IGNPAR: bool = false,
5053 INPCK: bool = false,5052 PARMRK: bool = false,
5054 ISTRIP: bool = false,5053 INPCK: bool = false,
5055 INLCR: bool = false,5054 ISTRIP: bool = false,
5056 IGNCR: bool = false,5055 INLCR: bool = false,
5057 ICRNL: bool = false,5056 IGNCR: bool = false,
5058 IUCLC: bool = false,5057 ICRNL: bool = false,
5059 IXON: bool = false,5058 IXON: bool = false,
5060 IXANY: bool = false,5059 IXOFF: bool = false,
5061 IXOFF: bool = false,5060 IXANY: bool = false,
5062 IMAXBEL: bool = false,5061 IUCLC: bool = false,
5063 IUTF8: bool = false,5062 IMAXBEL: bool = false,
5064 _: u17 = 0,5063 IUTF8: bool = false,
5064 _: u17 = 0,
5065 },
5066 else => packed struct(u32) {
5067 IGNBRK: bool = false,
5068 BRKINT: bool = false,
5069 IGNPAR: bool = false,
5070 PARMRK: bool = false,
5071 INPCK: bool = false,
5072 ISTRIP: bool = false,
5073 INLCR: bool = false,
5074 IGNCR: bool = false,
5075 ICRNL: bool = false,
5076 IUCLC: bool = false,
5077 IXON: bool = false,
5078 IXANY: bool = false,
5079 IXOFF: bool = false,
5080 IMAXBEL: bool = false,
5081 IUTF8: bool = false,
5082 _: u17 = 0,
5083 },
5065};5084};
50665085
5067pub const cc_t = switch (native_arch) {5086pub const cc_t = switch (native_arch) {
...@@ -5124,6 +5143,8 @@ pub const cc_t = switch (native_arch) {...@@ -5124,6 +5143,8 @@ pub const cc_t = switch (native_arch) {
5124 },5143 },
5125};5144};
51265145
5146pub const tcflag_t = u32;
5147
5127pub const OPOST: tcflag_t = 1;5148pub const OPOST: tcflag_t = 1;
5128pub const OLCUC: tcflag_t = 2;5149pub const OLCUC: tcflag_t = 2;
5129pub const ONLCR: tcflag_t = 4;5150pub const ONLCR: tcflag_t = 4;
...@@ -5168,7 +5189,7 @@ pub const TCSA = enum(c_uint) {...@@ -5168,7 +5189,7 @@ pub const TCSA = enum(c_uint) {
51685189
5169pub const termios = switch (native_arch) {5190pub const termios = switch (native_arch) {
5170 .powerpc, .powerpcle, .powerpc64, .powerpc64le => extern struct {5191 .powerpc, .powerpcle, .powerpc64, .powerpc64le => extern struct {
5171 iflag: tcflag_t,5192 iflag: tc_iflag_t,
5172 oflag: tcflag_t,5193 oflag: tcflag_t,
5173 cflag: tcflag_t,5194 cflag: tcflag_t,
5174 lflag: tcflag_t,5195 lflag: tcflag_t,
...@@ -5178,7 +5199,7 @@ pub const termios = switch (native_arch) {...@@ -5178,7 +5199,7 @@ pub const termios = switch (native_arch) {
5178 ospeed: speed_t,5199 ospeed: speed_t,
5179 },5200 },
5180 else => extern struct {5201 else => extern struct {
5181 iflag: tcflag_t,5202 iflag: tc_iflag_t,
5182 oflag: tcflag_t,5203 oflag: tcflag_t,
5183 cflag: tcflag_t,5204 cflag: tcflag_t,
5184 lflag: tcflag_t,5205 lflag: tcflag_t,