authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 15:41:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-12 15:41:38-07:00
log5258c3caad3a914fd4d8276dc118428181b364ee
treeae21958f74dc0fb35a8ff8ee733057de6accfc9e
parent0c725a354a801c84100011db5eb53ae6c58086c9

std: add type safety to cc_t


10 files changed, 267 insertions(+), 121 deletions(-)

lib/std/c.zig+161
......@@ -679,6 +679,167 @@ pub const MAP = switch (native_os) {
679679/// Used by libc to communicate failure. Not actually part of the underlying syscall.
680680pub const MAP_FAILED: *anyopaque = @ptrFromInt(std.math.maxInt(usize));
681681
682pub const cc_t = switch (native_os) {
683 .linux => std.os.linux.cc_t,
684 .macos, .ios, .tvos, .watchos => enum(u8) {
685 VEOF = 0,
686 VEOL = 1,
687 VEOL2 = 2,
688 VERASE = 3,
689 VWERASE = 4,
690 VKILL = 5,
691 VREPRINT = 6,
692 VINTR = 8,
693 VQUIT = 9,
694 VSUSP = 10,
695 VDSUSP = 11,
696 VSTART = 12,
697 VSTOP = 13,
698 VLNEXT = 14,
699 VDISCARD = 15,
700 VMIN = 16,
701 VTIME = 17,
702 VSTATUS = 18,
703 },
704 .freebsd, .kfreebsd => enum(u8) {
705 VEOF = 0,
706 VEOL = 1,
707 VEOL2 = 2,
708 VERASE = 3,
709 VWERASE = 4,
710 VKILL = 5,
711 VREPRINT = 6,
712 VERASE2 = 7,
713 VINTR = 8,
714 VQUIT = 9,
715 VSUSP = 10,
716 VDSUSP = 11,
717 VSTART = 12,
718 VSTOP = 13,
719 VLNEXT = 14,
720 VDISCARD = 15,
721 VMIN = 16,
722 VTIME = 17,
723 VSTATUS = 18,
724 },
725 .netbsd => enum(u8) {
726 VEOF = 0,
727 VEOL = 1,
728 VEOL2 = 2,
729 VERASE = 3,
730 VWERASE = 4,
731 VKILL = 5,
732 VREPRINT = 6,
733 VINTR = 8,
734 VQUIT = 9,
735 VSUSP = 10,
736 VDSUSP = 11,
737 VSTART = 12,
738 VSTOP = 13,
739 VLNEXT = 14,
740 VDISCARD = 15,
741 VMIN = 16,
742 VTIME = 17,
743 VSTATUS = 18,
744 },
745 .openbsd => enum(u8) {
746 VEOF = 0,
747 VEOL = 1,
748 VEOL2 = 2,
749 VERASE = 3,
750 VWERASE = 4,
751 VKILL = 5,
752 VREPRINT = 6,
753 VINTR = 8,
754 VQUIT = 9,
755 VSUSP = 10,
756 VDSUSP = 11,
757 VSTART = 12,
758 VSTOP = 13,
759 VLNEXT = 14,
760 VDISCARD = 15,
761 VMIN = 16,
762 VTIME = 17,
763 VSTATUS = 18,
764 },
765 .haiku => enum(u8) {
766 VINTR = 0,
767 VQUIT = 1,
768 VERASE = 2,
769 VKILL = 3,
770 VEOF = 4,
771 VEOL = 5,
772 VMIN = 4,
773 VTIME = 5,
774 VEOL2 = 6,
775 VSWTCH = 7,
776 VSTART = 8,
777 VSTOP = 9,
778 VSUSP = 10,
779 },
780 .solaris, .illumos => enum(u8) {
781 VINTR = 0,
782 VQUIT = 1,
783 VERASE = 2,
784 VKILL = 3,
785 VEOF = 4,
786 VEOL = 5,
787 VEOL2 = 6,
788 VMIN = 4,
789 VTIME = 5,
790 VSWTCH = 7,
791 VSTART = 8,
792 VSTOP = 9,
793 VSUSP = 10,
794 VDSUSP = 11,
795 VREPRINT = 12,
796 VDISCARD = 13,
797 VWERASE = 14,
798 VLNEXT = 15,
799 VSTATUS = 16,
800 VERASE2 = 17,
801 },
802 .emscripten => enum(u8) {
803 VINTR = 0,
804 VQUIT = 1,
805 VERASE = 2,
806 VKILL = 3,
807 VEOF = 4,
808 VTIME = 5,
809 VMIN = 6,
810 VSWTC = 7,
811 VSTART = 8,
812 VSTOP = 9,
813 VSUSP = 10,
814 VEOL = 11,
815 VREPRINT = 12,
816 VDISCARD = 13,
817 VWERASE = 14,
818 VLNEXT = 15,
819 VEOL2 = 16,
820 },
821 .wasi => enum(u8) {
822 VINTR = 0,
823 VQUIT = 1,
824 VERASE = 2,
825 VKILL = 3,
826 VEOF = 4,
827 VTIME = 5,
828 VMIN = 6,
829 VSWTC = 7,
830 VSTART = 8,
831 VSTOP = 9,
832 VSUSP = 10,
833 VEOL = 11,
834 VREPRINT = 12,
835 VDISCARD = 13,
836 VWERASE = 14,
837 VLNEXT = 15,
838 VEOL2 = 16,
839 },
840 else => @compileError("target libc does not have cc_t"),
841};
842
682843pub const whence_t = if (native_os == .wasi) std.os.wasi.whence_t else c_int;
683844
684845// Unix-like systems
lib/std/c/darwin.zig+1-24
......@@ -2692,31 +2692,8 @@ pub const SHUT = struct {
26922692 pub const RDWR = 2;
26932693};
26942694
2695// Term
2696pub const V = struct {
2697 pub const EOF = 0;
2698 pub const EOL = 1;
2699 pub const EOL2 = 2;
2700 pub const ERASE = 3;
2701 pub const WERASE = 4;
2702 pub const KILL = 5;
2703 pub const REPRINT = 6;
2704 pub const INTR = 8;
2705 pub const QUIT = 9;
2706 pub const SUSP = 10;
2707 pub const DSUSP = 11;
2708 pub const START = 12;
2709 pub const STOP = 13;
2710 pub const LNEXT = 14;
2711 pub const DISCARD = 15;
2712 pub const MIN = 16;
2713 pub const TIME = 17;
2714 pub const STATUS = 18;
2715};
2716
27172695pub const NCCS = 20; // 2 spares (7, 19)
27182696
2719pub const cc_t = u8;
27202697pub const speed_t = u64;
27212698pub const tcflag_t = u64;
27222699
......@@ -2859,7 +2836,7 @@ pub const termios = extern struct {
28592836 oflag: tcflag_t, // output flags
28602837 cflag: tcflag_t, // control flags
28612838 lflag: tcflag_t, // local flags
2862 cc: [NCCS]cc_t, // control chars
2839 cc: [NCCS]std.c.cc_t, // control chars
28632840 ispeed: speed_t align(8), // input speed
28642841 ospeed: speed_t, // output speed
28652842};
lib/std/c/emscripten.zig+16-2
......@@ -72,8 +72,6 @@ pub const sigset_t = emscripten.sigset_t;
7272pub const sockaddr = emscripten.sockaddr;
7373pub const socklen_t = emscripten.socklen_t;
7474pub const stack_t = emscripten.stack_t;
75pub const tcflag_t = emscripten.tcflag_t;
76pub const termios = emscripten.termios;
7775pub const time_t = emscripten.time_t;
7876pub const timespec = emscripten.timespec;
7977pub const timeval = emscripten.timeval;
......@@ -180,3 +178,19 @@ pub const dirent = struct {
180178 type: u8,
181179 name: [256]u8,
182180};
181
182pub const speed_t = u32;
183pub const tcflag_t = u32;
184
185pub const NCCS = 32;
186
187pub const termios = extern struct {
188 iflag: tcflag_t,
189 oflag: tcflag_t,
190 cflag: tcflag_t,
191 lflag: tcflag_t,
192 line: std.c.cc_t,
193 cc: [NCCS]std.c.cc_t,
194 ispeed: speed_t,
195 ospeed: speed_t,
196};
lib/std/c/haiku.zig+2-3
......@@ -950,7 +950,6 @@ pub const directory_which = enum(c_int) {
950950 _,
951951};
952952
953pub const cc_t = u8;
954953pub const speed_t = u8;
955954pub const tcflag_t = u32;
956955
......@@ -961,10 +960,10 @@ pub const termios = extern struct {
961960 c_oflag: tcflag_t,
962961 c_cflag: tcflag_t,
963962 c_lflag: tcflag_t,
964 c_line: cc_t,
963 c_line: std.c.cc_t,
965964 c_ispeed: speed_t,
966965 c_ospeed: speed_t,
967 cc_t: [NCCS]cc_t,
966 cc_t: [NCCS]std.c.cc_t,
968967};
969968
970969pub const MSG_NOSIGNAL = 0x0800;
lib/std/c/netbsd.zig+1-26
......@@ -806,30 +806,6 @@ pub const T = struct {
806806 pub const IOCXMTFRAME = 0x80087444;
807807};
808808
809// Term
810const V = struct {
811 pub const EOF = 0; // ICANON
812 pub const EOL = 1; // ICANON
813 pub const EOL2 = 2; // ICANON
814 pub const ERASE = 3; // ICANON
815 pub const WERASE = 4; // ICANON
816 pub const KILL = 5; // ICANON
817 pub const REPRINT = 6; // ICANON
818 // 7 spare 1
819 pub const INTR = 8; // ISIG
820 pub const QUIT = 9; // ISIG
821 pub const SUSP = 10; // ISIG
822 pub const DSUSP = 11; // ISIG
823 pub const START = 12; // IXON, IXOFF
824 pub const STOP = 13; // IXON, IXOFF
825 pub const LNEXT = 14; // IEXTEN
826 pub const DISCARD = 15; // IEXTEN
827 pub const MIN = 16; // !ICANON
828 pub const TIME = 17; // !ICANON
829 pub const STATUS = 18; // ICANON
830 // 19 spare 2
831};
832
833809// Input flags - software input processing
834810pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
835811pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINT
......@@ -876,7 +852,6 @@ pub const CHWFLOW: tcflag_t = (MDMBUF | CRTSCTS | CDTRCTS); // all types of hw f
876852
877853pub const tcflag_t = c_uint;
878854pub const speed_t = c_uint;
879pub const cc_t = u8;
880855
881856pub const NCCS = 20;
882857
......@@ -885,7 +860,7 @@ pub const termios = extern struct {
885860 oflag: tcflag_t, // output flags
886861 cflag: tcflag_t, // control flags
887862 lflag: tcflag_t, // local flags
888 cc: [NCCS]cc_t, // control chars
863 cc: [NCCS]std.c.cc_t, // control chars
889864 ispeed: c_int, // input speed
890865 ospeed: c_int, // output speed
891866};
lib/std/c/openbsd.zig+1-26
......@@ -768,33 +768,8 @@ pub const AUTH = struct {
768768 pub const ALLOW: c_int = (OKAY | ROOTOKAY | SECURE);
769769};
770770
771// Term
772pub const V = struct {
773 pub const EOF = 0; // ICANON
774 pub const EOL = 1; // ICANON
775 pub const EOL2 = 2; // ICANON
776 pub const ERASE = 3; // ICANON
777 pub const WERASE = 4; // ICANON
778 pub const KILL = 5; // ICANON
779 pub const REPRINT = 6; // ICANON
780 // 7 spare 1
781 pub const INTR = 8; // ISIG
782 pub const QUIT = 9; // ISIG
783 pub const SUSP = 10; // ISIG
784 pub const DSUSP = 11; // ISIG
785 pub const START = 12; // IXON, IXOFF
786 pub const STOP = 13; // IXON, IXOFF
787 pub const LNEXT = 14; // IEXTEN
788 pub const DISCARD = 15; // IEXTEN
789 pub const MIN = 16; // !ICANON
790 pub const TIME = 17; // !ICANON
791 pub const STATUS = 18; // ICANON
792 // 19 spare 2
793};
794
795771pub const tcflag_t = c_uint;
796772pub const speed_t = c_uint;
797pub const cc_t = u8;
798773
799774pub const NCCS = 20;
800775
......@@ -848,7 +823,7 @@ pub const termios = extern struct {
848823 oflag: tcflag_t, // output flags
849824 cflag: tcflag_t, // control flags
850825 lflag: tcflag_t, // local flags
851 cc: [NCCS]cc_t, // control chars
826 cc: [NCCS]std.c.cc_t, // control chars
852827 ispeed: c_int, // input speed
853828 ospeed: c_int, // output speed
854829};
lib/std/c/solaris.zig+1-2
......@@ -699,7 +699,6 @@ pub const SEEK = struct {
699699};
700700
701701pub const tcflag_t = c_uint;
702pub const cc_t = u8;
703702pub const speed_t = c_uint;
704703
705704pub const NCCS = 19;
......@@ -709,7 +708,7 @@ pub const termios = extern struct {
709708 c_oflag: tcflag_t,
710709 c_cflag: tcflag_t,
711710 c_lflag: tcflag_t,
712 c_cc: [NCCS]cc_t,
711 c_cc: [NCCS]std.c.cc_t,
713712};
714713
715714fn tioc(t: u16, num: u8) u16 {
lib/std/os.zig+1
......@@ -139,6 +139,7 @@ pub const W = system.W;
139139pub const addrinfo = system.addrinfo;
140140pub const blkcnt_t = system.blkcnt_t;
141141pub const blksize_t = system.blksize_t;
142pub const cc_t = system.cc_t;
142143pub const clock_t = system.clock_t;
143144pub const cpu_set_t = system.cpu_set_t;
144145pub const dev_t = system.dev_t;
lib/std/os/emscripten.zig-17
......@@ -1098,23 +1098,6 @@ pub const stack_t = extern struct {
10981098 size: usize,
10991099};
11001100
1101pub const cc_t = u8;
1102pub const speed_t = u32;
1103pub const tcflag_t = u32;
1104
1105pub const NCCS = 32;
1106
1107pub const termios = extern struct {
1108 iflag: tcflag_t,
1109 oflag: tcflag_t,
1110 cflag: tcflag_t,
1111 lflag: tcflag_t,
1112 line: cc_t,
1113 cc: [NCCS]cc_t,
1114 ispeed: speed_t,
1115 ospeed: speed_t,
1116};
1117
11181101pub const timespec = extern struct {
11191102 tv_sec: time_t,
11201103 tv_nsec: isize,
lib/std/os/linux.zig+83-21
......@@ -5004,9 +5004,7 @@ pub const rusage = extern struct {
50045004 pub const THREAD = 1;
50055005};
50065006
5007pub const cc_t = u8;
50085007pub const speed_t = u32;
5009pub const tcflag_t = u32;
50105008
50115009pub const NCCS = 32;
50125010
......@@ -5124,21 +5122,84 @@ pub const V = switch (native_arch) {
51245122 },
51255123};
51265124
5127pub const IGNBRK: tcflag_t = 1;
5128pub const BRKINT: tcflag_t = 2;
5129pub const IGNPAR: tcflag_t = 4;
5130pub const PARMRK: tcflag_t = 8;
5131pub const INPCK: tcflag_t = 16;
5132pub const ISTRIP: tcflag_t = 32;
5133pub const INLCR: tcflag_t = 64;
5134pub const IGNCR: tcflag_t = 128;
5135pub const ICRNL: tcflag_t = 256;
5136pub const IUCLC: tcflag_t = 512;
5137pub const IXON: tcflag_t = 1024;
5138pub const IXANY: tcflag_t = 2048;
5139pub const IXOFF: tcflag_t = 4096;
5140pub const IMAXBEL: tcflag_t = 8192;
5141pub const IUTF8: tcflag_t = 16384;
5125pub const tc_iflag_t = packed struct (u32) {
5126 IGNBRK: bool = false,
5127 BRKINT: bool = false,
5128 IGNPAR: bool = false,
5129 PARMRK: bool = false,
5130 INPCK: bool = false,
5131 ISTRIP: bool = false,
5132 INLCR: bool = false,
5133 IGNCR: bool = false,
5134 ICRNL: bool = false,
5135 IUCLC: bool = false,
5136 IXON: bool = false,
5137 IXANY: bool = false,
5138 IXOFF: bool = false,
5139 IMAXBEL: bool = false,
5140 IUTF8: bool = false,
5141 _: u17 = 0,
5142};
5143
5144pub const cc_t = switch (native_arch) {
5145 .mips, .mipsel, .mips64, .mips64el => enum(u8){
5146 VINTR = 0,
5147 VQUIT = 1,
5148 VERASE = 2,
5149 VKILL = 3,
5150 VMIN = 4,
5151 VTIME = 5,
5152 VEOL2 = 6,
5153 VSWTC = 7,
5154 VSTART = 8,
5155 VSTOP = 9,
5156 VSUSP = 10,
5157 VREPRINT = 12,
5158 VDISCARD = 13,
5159 VWERASE = 14,
5160 VLNEXT = 15,
5161 VEOF = 16,
5162 VEOL = 17,
5163 },
5164 .powerpc, .powerpc64, .powerpc64le => enum(u8) {
5165 VINTR = 0,
5166 VQUIT = 1,
5167 VERASE = 2,
5168 VKILL = 3,
5169 VEOF = 4,
5170 VMIN = 5,
5171 VEOL = 6,
5172 VTIME = 7,
5173 VEOL2 = 8,
5174 VSWTC = 9,
5175 VWERASE = 10,
5176 VREPRINT = 11,
5177 VSUSP = 12,
5178 VSTART = 13,
5179 VSTOP = 14,
5180 VLNEXT = 15,
5181 VDISCARD = 16,
5182 },
5183 else => enum(u8) {
5184 VINTR = 0,
5185 VQUIT = 1,
5186 VERASE = 2,
5187 VKILL = 3,
5188 VEOF = 4,
5189 VTIME = 5,
5190 VMIN = 6,
5191 VSWTC = 7,
5192 VSTART = 8,
5193 VSTOP = 9,
5194 VSUSP = 10,
5195 VEOL = 11,
5196 VREPRINT = 12,
5197 VDISCARD = 13,
5198 VWERASE = 14,
5199 VLNEXT = 15,
5200 VEOL2 = 16,
5201 },
5202};
51425203
51435204pub const OPOST: tcflag_t = 1;
51445205pub const OLCUC: tcflag_t = 2;
......@@ -5148,6 +5209,7 @@ pub const ONOCR: tcflag_t = 16;
51485209pub const ONLRET: tcflag_t = 32;
51495210pub const OFILL: tcflag_t = 64;
51505211pub const OFDEL: tcflag_t = 128;
5212
51515213pub const VTDLY: tcflag_t = 16384;
51525214pub const VT0: tcflag_t = 0;
51535215pub const VT1: tcflag_t = 16384;
......@@ -5182,10 +5244,10 @@ pub const TCSA = enum(c_uint) {
51825244};
51835245
51845246pub const termios = extern struct {
5185 iflag: tcflag_t,
5186 oflag: tcflag_t,
5187 cflag: tcflag_t,
5188 lflag: tcflag_t,
5247 iflag: tc_iflag_t,
5248 oflag: tc_oflag_t,
5249 cflag: tc_cflag_t,
5250 lflag: tc_lflag_t,
51895251 line: cc_t,
51905252 cc: [NCCS]cc_t,
51915253 ispeed: speed_t,