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) {
793793pub const termios = switch (native_os) {
794794 .linux => std.os.linux.termios,
795795 .macos, .ios, .tvos, .watchos => extern struct {
796 iflag: tcflag_t,
796 iflag: tc_iflag_t,
797797 oflag: tcflag_t,
798798 cflag: tcflag_t,
799799 lflag: tcflag_t,
......@@ -802,7 +802,7 @@ pub const termios = switch (native_os) {
802802 ospeed: speed_t,
803803 },
804804 .freebsd, .kfreebsd, .netbsd, .dragonfly, .openbsd => extern struct {
805 iflag: tcflag_t,
805 iflag: tc_iflag_t,
806806 oflag: tcflag_t,
807807 cflag: tcflag_t,
808808 lflag: tcflag_t,
......@@ -811,7 +811,7 @@ pub const termios = switch (native_os) {
811811 ospeed: speed_t,
812812 },
813813 .haiku => extern struct {
814 iflag: tcflag_t,
814 iflag: tc_iflag_t,
815815 oflag: tcflag_t,
816816 cflag: tcflag_t,
817817 lflag: tcflag_t,
......@@ -821,14 +821,14 @@ pub const termios = switch (native_os) {
821821 cc: [NCCS]cc_t,
822822 },
823823 .solaris, .illumos => extern struct {
824 iflag: tcflag_t,
824 iflag: tc_iflag_t,
825825 oflag: tcflag_t,
826826 cflag: tcflag_t,
827827 lflag: tcflag_t,
828828 cc: [NCCS]cc_t,
829829 },
830830 .emscripten, .wasi => extern struct {
831 iflag: tcflag_t,
831 iflag: tc_iflag_t,
832832 oflag: tcflag_t,
833833 cflag: tcflag_t,
834834 lflag: tcflag_t,
......@@ -840,6 +840,116 @@ pub const termios = switch (native_os) {
840840 else => @compileError("target libc does not have termios"),
841841};
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
843953pub const tcflag_t = switch (native_os) {
844954 .linux => std.os.linux.tcflag_t,
845955 .macos, .ios, .tvos, .watchos => u64,
lib/std/c/darwin.zig-15
......@@ -2692,21 +2692,6 @@ pub const SHUT = struct {
26922692 pub const RDWR = 2;
26932693};
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
27102695pub const OPOST: tcflag_t = 0x00000001; //enable following output processing
27112696pub const ONLCR: tcflag_t = 0x00000002; // map NL to CR-NL (ala CRMOD)
27122697pub const OXTABS: tcflag_t = 0x00000004; // expand tabs to spaces
lib/std/c/netbsd.zig-14
......@@ -806,20 +806,6 @@ pub const T = struct {
806806 pub const IOCXMTFRAME = 0x80087444;
807807};
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
824810// Output flags - software output processing
825811pub const OPOST: tcflag_t = 0x00000001; // enable following output processing
lib/std/c/openbsd.zig-15
......@@ -768,21 +768,6 @@ pub const AUTH = struct {
768768 pub const ALLOW: c_int = (OKAY | ROOTOKAY | SECURE);
769769};
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
787772// Output flags - software output processing
788773pub const OPOST: tcflag_t = 0x00000001; // enable following output processing
lib/std/os.zig+5-3
......@@ -106,7 +106,6 @@ pub const MFD = system.MFD;
106106pub const MMAP2_UNIT = system.MMAP2_UNIT;
107107pub const MSG = system.MSG;
108108pub const NAME_MAX = system.NAME_MAX;
109pub const NCCS = system.NCCS;
110109pub const O = system.O;
111110pub const PATH_MAX = system.PATH_MAX;
112111pub const POLL = system.POLL;
......@@ -173,9 +172,7 @@ pub const siginfo_t = system.siginfo_t;
173172pub const sigset_t = system.sigset_t;
174173pub const sockaddr = system.sockaddr;
175174pub const socklen_t = system.socklen_t;
176pub const speed_t = system.speed_t;
177175pub const stack_t = system.stack_t;
178pub const tcflag_t = system.tcflag_t;
179176pub const termios = system.termios;
180177pub const time_t = system.time_t;
181178pub const timespec = system.timespec;
......@@ -187,6 +184,11 @@ pub const uid_t = system.uid_t;
187184pub const user_desc = system.user_desc;
188185pub 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
190192pub const F_OK = system.F_OK;
191193pub const R_OK = system.R_OK;
192194pub const W_OK = system.W_OK;
lib/std/os/linux.zig+41-20
......@@ -5005,7 +5005,6 @@ pub const rusage = extern struct {
50055005};
50065006
50075007pub const speed_t = u32;
5008pub const tcflag_t = u32;
50095008
50105009pub const NCCS = switch (native_arch) {
50115010 .powerpc, .powerpcle, .powerpc64, .powerpc64le => 19,
......@@ -5045,23 +5044,43 @@ pub const B3000000 = 0o0010015;
50455044pub const B3500000 = 0o0010016;
50465045pub const B4000000 = 0o0010017;
50475046
5048pub const tc_iflag_t = packed struct(u32) {
5049 IGNBRK: bool = false,
5050 BRKINT: bool = false,
5051 IGNPAR: bool = false,
5052 PARMRK: bool = false,
5053 INPCK: bool = false,
5054 ISTRIP: bool = false,
5055 INLCR: bool = false,
5056 IGNCR: bool = false,
5057 ICRNL: bool = false,
5058 IUCLC: bool = false,
5059 IXON: bool = false,
5060 IXANY: bool = false,
5061 IXOFF: bool = false,
5062 IMAXBEL: bool = false,
5063 IUTF8: bool = false,
5064 _: u17 = 0,
5047pub const tc_iflag_t = switch (native_arch) {
5048 .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
5049 IGNBRK: bool = false,
5050 BRKINT: bool = false,
5051 IGNPAR: bool = false,
5052 PARMRK: bool = false,
5053 INPCK: bool = false,
5054 ISTRIP: bool = false,
5055 INLCR: bool = false,
5056 IGNCR: bool = false,
5057 ICRNL: bool = false,
5058 IXON: bool = false,
5059 IXOFF: bool = false,
5060 IXANY: bool = false,
5061 IUCLC: bool = false,
5062 IMAXBEL: bool = false,
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 },
50655084};
50665085
50675086pub const cc_t = switch (native_arch) {
......@@ -5124,6 +5143,8 @@ pub const cc_t = switch (native_arch) {
51245143 },
51255144};
51265145
5146pub const tcflag_t = u32;
5147
51275148pub const OPOST: tcflag_t = 1;
51285149pub const OLCUC: tcflag_t = 2;
51295150pub const ONLCR: tcflag_t = 4;
......@@ -5168,7 +5189,7 @@ pub const TCSA = enum(c_uint) {
51685189
51695190pub const termios = switch (native_arch) {
51705191 .powerpc, .powerpcle, .powerpc64, .powerpc64le => extern struct {
5171 iflag: tcflag_t,
5192 iflag: tc_iflag_t,
51725193 oflag: tcflag_t,
51735194 cflag: tcflag_t,
51745195 lflag: tcflag_t,
......@@ -5178,7 +5199,7 @@ pub const termios = switch (native_arch) {
51785199 ospeed: speed_t,
51795200 },
51805201 else => extern struct {
5181 iflag: tcflag_t,
5202 iflag: tc_iflag_t,
51825203 oflag: tcflag_t,
51835204 cflag: tcflag_t,
51845205 lflag: tcflag_t,