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) {...@@ -679,6 +679,167 @@ pub const MAP = switch (native_os) {
679/// Used by libc to communicate failure. Not actually part of the underlying syscall.679/// Used by libc to communicate failure. Not actually part of the underlying syscall.
680pub const MAP_FAILED: *anyopaque = @ptrFromInt(std.math.maxInt(usize));680pub 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
682pub const whence_t = if (native_os == .wasi) std.os.wasi.whence_t else c_int;843pub const whence_t = if (native_os == .wasi) std.os.wasi.whence_t else c_int;
683844
684// Unix-like systems845// Unix-like systems
lib/std/c/darwin.zig+1-24
...@@ -2692,31 +2692,8 @@ pub const SHUT = struct {...@@ -2692,31 +2692,8 @@ pub const SHUT = struct {
2692 pub const RDWR = 2;2692 pub const RDWR = 2;
2693};2693};
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
2717pub const NCCS = 20; // 2 spares (7, 19)2695pub const NCCS = 20; // 2 spares (7, 19)
27182696
2719pub const cc_t = u8;
2720pub const speed_t = u64;2697pub const speed_t = u64;
2721pub const tcflag_t = u64;2698pub const tcflag_t = u64;
27222699
...@@ -2859,7 +2836,7 @@ pub const termios = extern struct {...@@ -2859,7 +2836,7 @@ pub const termios = extern struct {
2859 oflag: tcflag_t, // output flags2836 oflag: tcflag_t, // output flags
2860 cflag: tcflag_t, // control flags2837 cflag: tcflag_t, // control flags
2861 lflag: tcflag_t, // local flags2838 lflag: tcflag_t, // local flags
2862 cc: [NCCS]cc_t, // control chars2839 cc: [NCCS]std.c.cc_t, // control chars
2863 ispeed: speed_t align(8), // input speed2840 ispeed: speed_t align(8), // input speed
2864 ospeed: speed_t, // output speed2841 ospeed: speed_t, // output speed
2865};2842};
lib/std/c/emscripten.zig+16-2
...@@ -72,8 +72,6 @@ pub const sigset_t = emscripten.sigset_t;...@@ -72,8 +72,6 @@ pub const sigset_t = emscripten.sigset_t;
72pub const sockaddr = emscripten.sockaddr;72pub const sockaddr = emscripten.sockaddr;
73pub const socklen_t = emscripten.socklen_t;73pub const socklen_t = emscripten.socklen_t;
74pub const stack_t = emscripten.stack_t;74pub const stack_t = emscripten.stack_t;
75pub const tcflag_t = emscripten.tcflag_t;
76pub const termios = emscripten.termios;
77pub const time_t = emscripten.time_t;75pub const time_t = emscripten.time_t;
78pub const timespec = emscripten.timespec;76pub const timespec = emscripten.timespec;
79pub const timeval = emscripten.timeval;77pub const timeval = emscripten.timeval;
...@@ -180,3 +178,19 @@ pub const dirent = struct {...@@ -180,3 +178,19 @@ pub const dirent = struct {
180 type: u8,178 type: u8,
181 name: [256]u8,179 name: [256]u8,
182};180};
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) {...@@ -950,7 +950,6 @@ pub const directory_which = enum(c_int) {
950 _,950 _,
951};951};
952952
953pub const cc_t = u8;
954pub const speed_t = u8;953pub const speed_t = u8;
955pub const tcflag_t = u32;954pub const tcflag_t = u32;
956955
...@@ -961,10 +960,10 @@ pub const termios = extern struct {...@@ -961,10 +960,10 @@ pub const termios = extern struct {
961 c_oflag: tcflag_t,960 c_oflag: tcflag_t,
962 c_cflag: tcflag_t,961 c_cflag: tcflag_t,
963 c_lflag: tcflag_t,962 c_lflag: tcflag_t,
964 c_line: cc_t,963 c_line: std.c.cc_t,
965 c_ispeed: speed_t,964 c_ispeed: speed_t,
966 c_ospeed: speed_t,965 c_ospeed: speed_t,
967 cc_t: [NCCS]cc_t,966 cc_t: [NCCS]std.c.cc_t,
968};967};
969968
970pub const MSG_NOSIGNAL = 0x0800;969pub const MSG_NOSIGNAL = 0x0800;
lib/std/c/netbsd.zig+1-26
...@@ -806,30 +806,6 @@ pub const T = struct {...@@ -806,30 +806,6 @@ pub const T = struct {
806 pub const IOCXMTFRAME = 0x80087444;806 pub const IOCXMTFRAME = 0x80087444;
807};807};
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
833// Input flags - software input processing809// Input flags - software input processing
834pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition810pub const IGNBRK: tcflag_t = 0x00000001; // ignore BREAK condition
835pub const BRKINT: tcflag_t = 0x00000002; // map BREAK to SIGINT811pub 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...@@ -876,7 +852,6 @@ pub const CHWFLOW: tcflag_t = (MDMBUF | CRTSCTS | CDTRCTS); // all types of hw f
876852
877pub const tcflag_t = c_uint;853pub const tcflag_t = c_uint;
878pub const speed_t = c_uint;854pub const speed_t = c_uint;
879pub const cc_t = u8;
880855
881pub const NCCS = 20;856pub const NCCS = 20;
882857
...@@ -885,7 +860,7 @@ pub const termios = extern struct {...@@ -885,7 +860,7 @@ pub const termios = extern struct {
885 oflag: tcflag_t, // output flags860 oflag: tcflag_t, // output flags
886 cflag: tcflag_t, // control flags861 cflag: tcflag_t, // control flags
887 lflag: tcflag_t, // local flags862 lflag: tcflag_t, // local flags
888 cc: [NCCS]cc_t, // control chars863 cc: [NCCS]std.c.cc_t, // control chars
889 ispeed: c_int, // input speed864 ispeed: c_int, // input speed
890 ospeed: c_int, // output speed865 ospeed: c_int, // output speed
891};866};
lib/std/c/openbsd.zig+1-26
...@@ -768,33 +768,8 @@ pub const AUTH = struct {...@@ -768,33 +768,8 @@ 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// 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
795pub const tcflag_t = c_uint;771pub const tcflag_t = c_uint;
796pub const speed_t = c_uint;772pub const speed_t = c_uint;
797pub const cc_t = u8;
798773
799pub const NCCS = 20;774pub const NCCS = 20;
800775
...@@ -848,7 +823,7 @@ pub const termios = extern struct {...@@ -848,7 +823,7 @@ pub const termios = extern struct {
848 oflag: tcflag_t, // output flags823 oflag: tcflag_t, // output flags
849 cflag: tcflag_t, // control flags824 cflag: tcflag_t, // control flags
850 lflag: tcflag_t, // local flags825 lflag: tcflag_t, // local flags
851 cc: [NCCS]cc_t, // control chars826 cc: [NCCS]std.c.cc_t, // control chars
852 ispeed: c_int, // input speed827 ispeed: c_int, // input speed
853 ospeed: c_int, // output speed828 ospeed: c_int, // output speed
854};829};
lib/std/c/solaris.zig+1-2
...@@ -699,7 +699,6 @@ pub const SEEK = struct {...@@ -699,7 +699,6 @@ pub const SEEK = struct {
699};699};
700700
701pub const tcflag_t = c_uint;701pub const tcflag_t = c_uint;
702pub const cc_t = u8;
703pub const speed_t = c_uint;702pub const speed_t = c_uint;
704703
705pub const NCCS = 19;704pub const NCCS = 19;
...@@ -709,7 +708,7 @@ pub const termios = extern struct {...@@ -709,7 +708,7 @@ pub const termios = extern struct {
709 c_oflag: tcflag_t,708 c_oflag: tcflag_t,
710 c_cflag: tcflag_t,709 c_cflag: tcflag_t,
711 c_lflag: tcflag_t,710 c_lflag: tcflag_t,
712 c_cc: [NCCS]cc_t,711 c_cc: [NCCS]std.c.cc_t,
713};712};
714713
715fn tioc(t: u16, num: u8) u16 {714fn tioc(t: u16, num: u8) u16 {
lib/std/os.zig+1
...@@ -139,6 +139,7 @@ pub const W = system.W;...@@ -139,6 +139,7 @@ pub const W = system.W;
139pub const addrinfo = system.addrinfo;139pub const addrinfo = system.addrinfo;
140pub const blkcnt_t = system.blkcnt_t;140pub const blkcnt_t = system.blkcnt_t;
141pub const blksize_t = system.blksize_t;141pub const blksize_t = system.blksize_t;
142pub const cc_t = system.cc_t;
142pub const clock_t = system.clock_t;143pub const clock_t = system.clock_t;
143pub const cpu_set_t = system.cpu_set_t;144pub const cpu_set_t = system.cpu_set_t;
144pub const dev_t = system.dev_t;145pub const dev_t = system.dev_t;
lib/std/os/emscripten.zig-17
...@@ -1098,23 +1098,6 @@ pub const stack_t = extern struct {...@@ -1098,23 +1098,6 @@ pub const stack_t = extern struct {
1098 size: usize,1098 size: usize,
1099};1099};
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
1118pub const timespec = extern struct {1101pub const timespec = extern struct {
1119 tv_sec: time_t,1102 tv_sec: time_t,
1120 tv_nsec: isize,1103 tv_nsec: isize,
lib/std/os/linux.zig+83-21
...@@ -5004,9 +5004,7 @@ pub const rusage = extern struct {...@@ -5004,9 +5004,7 @@ pub const rusage = extern struct {
5004 pub const THREAD = 1;5004 pub const THREAD = 1;
5005};5005};
50065006
5007pub const cc_t = u8;
5008pub const speed_t = u32;5007pub const speed_t = u32;
5009pub const tcflag_t = u32;
50105008
5011pub const NCCS = 32;5009pub const NCCS = 32;
50125010
...@@ -5124,21 +5122,84 @@ pub const V = switch (native_arch) {...@@ -5124,21 +5122,84 @@ pub const V = switch (native_arch) {
5124 },5122 },
5125};5123};
51265124
5127pub const IGNBRK: tcflag_t = 1;5125pub const tc_iflag_t = packed struct (u32) {
5128pub const BRKINT: tcflag_t = 2;5126 IGNBRK: bool = false,
5129pub const IGNPAR: tcflag_t = 4;5127 BRKINT: bool = false,
5130pub const PARMRK: tcflag_t = 8;5128 IGNPAR: bool = false,
5131pub const INPCK: tcflag_t = 16;5129 PARMRK: bool = false,
5132pub const ISTRIP: tcflag_t = 32;5130 INPCK: bool = false,
5133pub const INLCR: tcflag_t = 64;5131 ISTRIP: bool = false,
5134pub const IGNCR: tcflag_t = 128;5132 INLCR: bool = false,
5135pub const ICRNL: tcflag_t = 256;5133 IGNCR: bool = false,
5136pub const IUCLC: tcflag_t = 512;5134 ICRNL: bool = false,
5137pub const IXON: tcflag_t = 1024;5135 IUCLC: bool = false,
5138pub const IXANY: tcflag_t = 2048;5136 IXON: bool = false,
5139pub const IXOFF: tcflag_t = 4096;5137 IXANY: bool = false,
5140pub const IMAXBEL: tcflag_t = 8192;5138 IXOFF: bool = false,
5141pub const IUTF8: tcflag_t = 16384;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
5143pub const OPOST: tcflag_t = 1;5204pub const OPOST: tcflag_t = 1;
5144pub const OLCUC: tcflag_t = 2;5205pub const OLCUC: tcflag_t = 2;
...@@ -5148,6 +5209,7 @@ pub const ONOCR: tcflag_t = 16;...@@ -5148,6 +5209,7 @@ pub const ONOCR: tcflag_t = 16;
5148pub const ONLRET: tcflag_t = 32;5209pub const ONLRET: tcflag_t = 32;
5149pub const OFILL: tcflag_t = 64;5210pub const OFILL: tcflag_t = 64;
5150pub const OFDEL: tcflag_t = 128;5211pub const OFDEL: tcflag_t = 128;
5212
5151pub const VTDLY: tcflag_t = 16384;5213pub const VTDLY: tcflag_t = 16384;
5152pub const VT0: tcflag_t = 0;5214pub const VT0: tcflag_t = 0;
5153pub const VT1: tcflag_t = 16384;5215pub const VT1: tcflag_t = 16384;
...@@ -5182,10 +5244,10 @@ pub const TCSA = enum(c_uint) {...@@ -5182,10 +5244,10 @@ pub const TCSA = enum(c_uint) {
5182};5244};
51835245
5184pub const termios = extern struct {5246pub const termios = extern struct {
5185 iflag: tcflag_t,5247 iflag: tc_iflag_t,
5186 oflag: tcflag_t,5248 oflag: tc_oflag_t,
5187 cflag: tcflag_t,5249 cflag: tc_cflag_t,
5188 lflag: tcflag_t,5250 lflag: tc_lflag_t,
5189 line: cc_t,5251 line: cc_t,
5190 cc: [NCCS]cc_t,5252 cc: [NCCS]cc_t,
5191 ispeed: speed_t,5253 ispeed: speed_t,