summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/termios/types.rs
blob: d978bfcca4734b2d3aaa2adfbf55681476b6446f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
use crate::backend::c;
use crate::{backend, io};
use bitflags::bitflags;

/// `struct termios` for use with [`tcgetattr`] and [`tcsetattr`].
///
/// [`tcgetattr`]: crate::termios::tcgetattr
/// [`tcsetattr`]: crate::termios::tcsetattr
#[repr(C)]
#[derive(Clone)]
pub struct Termios {
    /// How is input interpreted?
    #[doc(alias = "c_iflag")]
    pub input_modes: InputModes,

    /// How is output translated?
    #[doc(alias = "c_oflag")]
    pub output_modes: OutputModes,

    /// Low-level configuration flags.
    #[doc(alias = "c_cflag")]
    pub control_modes: ControlModes,

    /// High-level configuration flags.
    #[doc(alias = "c_lflag")]
    pub local_modes: LocalModes,

    /// Line discipline.
    #[doc(alias = "c_line")]
    #[cfg(not(all(linux_raw, any(target_arch = "powerpc", target_arch = "powerpc64"))))]
    #[cfg(any(
        linux_like,
        target_env = "newlib",
        target_os = "haiku",
        target_os = "fuchsia",
        target_os = "redox"
    ))]
    pub line_discipline: c::cc_t,

    /// How are various special control codes handled?
    #[doc(alias = "c_cc")]
    pub special_codes: SpecialCodes,

    /// Line discipline.
    // On PowerPC, this field comes after `c_cc`.
    #[doc(alias = "c_line")]
    #[cfg(all(linux_raw, any(target_arch = "powerpc", target_arch = "powerpc64")))]
    pub line_discipline: c::cc_t,

    /// See the `input_speed` and `set_input_seed` functions.
    ///
    /// On Linux and BSDs, this is the arbitrary integer speed value. On all
    /// other platforms, this is the encoded speed value.
    pub(crate) input_speed: c::speed_t,

    /// See the `output_speed` and `set_output_seed` functions.
    ///
    /// On Linux and BSDs, this is the integer speed value. On all other
    /// platforms, this is the encoded speed value.
    pub(crate) output_speed: c::speed_t,
}

impl Termios {
    /// `cfmakeraw(self)`—Set a `Termios` value to the settings for "raw" mode.
    ///
    /// In raw mode, input is available a byte at a time, echoing is disabled,
    /// and special terminal input and output codes are disabled.
    #[doc(alias = "cfmakeraw")]
    #[inline]
    pub fn make_raw(&mut self) {
        backend::termios::syscalls::cfmakeraw(self)
    }

    /// Return the input communication speed.
    ///
    /// Unlike the `c_ispeed` field in GLIBC and others, this returns the
    /// integer value of the speed, rather than the `B*` encoded constant
    /// value.
    #[doc(alias = "c_ispeed")]
    #[doc(alias = "cfgetispeed")]
    #[doc(alias = "cfgetspeed")]
    #[inline]
    pub fn input_speed(&self) -> u32 {
        // On Linux and BSDs, `input_speed` is the arbitrary integer speed.
        #[cfg(any(linux_kernel, bsd))]
        {
            debug_assert!(u32::try_from(self.input_speed).is_ok());
            self.input_speed as u32
        }

        // On other platforms, it's the encoded speed.
        #[cfg(not(any(linux_kernel, bsd)))]
        {
            speed::decode(self.input_speed).unwrap()
        }
    }

    /// Return the output communication speed.
    ///
    /// Unlike the `c_ospeed` field in GLIBC and others, this returns the
    /// arbitrary integer value of the speed, rather than the `B*` encoded
    /// constant value.
    #[inline]
    pub fn output_speed(&self) -> u32 {
        // On Linux and BSDs, `input_speed` is the arbitrary integer speed.
        #[cfg(any(linux_kernel, bsd))]
        {
            debug_assert!(u32::try_from(self.output_speed).is_ok());
            self.output_speed as u32
        }

        // On other platforms, it's the encoded speed.
        #[cfg(not(any(linux_kernel, bsd)))]
        {
            speed::decode(self.output_speed).unwrap()
        }
    }

    /// Set the input and output communication speeds.
    ///
    /// Unlike the `c_ispeed` and `c_ospeed` fields in GLIBC and others, this
    /// takes the arbitrary integer value of the speed, rather than the `B*`
    /// encoded constant value. Not all implementations support all integer
    /// values; use the constants in the [`speed`] module for likely-supported
    /// speeds.
    #[doc(alias = "cfsetspeed")]
    #[doc(alias = "CBAUD")]
    #[doc(alias = "CBAUDEX")]
    #[doc(alias = "CIBAUD")]
    #[doc(alias = "CIBAUDEX")]
    #[inline]
    pub fn set_speed(&mut self, new_speed: u32) -> io::Result<()> {
        backend::termios::syscalls::set_speed(self, new_speed)
    }

    /// Set the input communication speed.
    ///
    /// Unlike the `c_ispeed` field in GLIBC and others, this takes the
    /// arbitrary integer value of the speed, rather than the `B*` encoded
    /// constant value. Not all implementations support all integer values; use
    /// the constants in the [`speed`] module for known-supported speeds.
    ///
    /// On some platforms, changing the input speed changes the output speed
    /// to the same speed.
    #[doc(alias = "c_ispeed")]
    #[doc(alias = "cfsetispeed")]
    #[doc(alias = "CIBAUD")]
    #[doc(alias = "CIBAUDEX")]
    #[inline]
    pub fn set_input_speed(&mut self, new_speed: u32) -> io::Result<()> {
        backend::termios::syscalls::set_input_speed(self, new_speed)
    }

    /// Set the output communication speed.
    ///
    /// Unlike the `c_ospeed` field in GLIBC and others, this takes the
    /// arbitrary integer value of the speed, rather than the `B*` encoded
    /// constant value. Not all implementations support all integer values; use
    /// the constants in the [`speed`] module for known-supported speeds.
    ///
    /// On some platforms, changing the output speed changes the input speed
    /// to the same speed.
    #[doc(alias = "c_ospeed")]
    #[doc(alias = "cfsetospeed")]
    #[doc(alias = "CBAUD")]
    #[doc(alias = "CBAUDEX")]
    #[inline]
    pub fn set_output_speed(&mut self, new_speed: u32) -> io::Result<()> {
        backend::termios::syscalls::set_output_speed(self, new_speed)
    }
}

impl core::fmt::Debug for Termios {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut d = f.debug_struct("Termios");
        d.field("input_modes", &self.input_modes);
        d.field("output_modes", &self.output_modes);
        d.field("control_modes", &self.control_modes);
        d.field("local_modes", &self.local_modes);
        #[cfg(any(
            linux_like,
            target_env = "newlib",
            target_os = "haiku",
            target_os = "fuchsia",
            target_os = "redox"
        ))]
        {
            d.field("line_discipline", &self.line_discipline);
        }
        d.field("special_codes", &self.special_codes);
        d.field("input_speed", &self.input_speed());
        d.field("output_speed", &self.output_speed());
        d.finish()
    }
}

bitflags! {
    /// Flags controlling terminal input.
    #[repr(transparent)]
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct InputModes: c::tcflag_t {
        /// `IGNBRK`
        const IGNBRK = c::IGNBRK;

        /// `BRKINT`
        const BRKINT = c::BRKINT;

        /// `IGNPAR`
        const IGNPAR = c::IGNPAR;

        /// `PARMRK`
        const PARMRK = c::PARMRK;

        /// `INPCK`
        const INPCK = c::INPCK;

        /// `ISTRIP`
        const ISTRIP = c::ISTRIP;

        /// `INLCR`
        const INLCR = c::INLCR;

        /// `IGNCR`
        const IGNCR = c::IGNCR;

        /// `ICRNL`
        const ICRNL = c::ICRNL;

        /// `IUCLC`
        #[cfg(any(linux_kernel, solarish, target_os = "haiku"))]
        const IUCLC = c::IUCLC;

        /// `IXON`
        const IXON = c::IXON;

        /// `IXANY`
        #[cfg(not(target_os = "redox"))]
        const IXANY = c::IXANY;

        /// `IXOFF`
        const IXOFF = c::IXOFF;

        /// `IMAXBEL`
        #[cfg(not(any(target_os = "haiku", target_os = "redox")))]
        const IMAXBEL = c::IMAXBEL;

        /// `IUTF8`
        #[cfg(not(any(
            solarish,
            target_os = "aix",
            target_os = "dragonfly",
            target_os = "emscripten",
            target_os = "freebsd",
            target_os = "haiku",
            target_os = "netbsd",
            target_os = "openbsd",
            target_os = "redox",
        )))]
        const IUTF8 = c::IUTF8;
    }
}

bitflags! {
    /// Flags controlling terminal output.
    #[repr(transparent)]
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct OutputModes: c::tcflag_t {
        /// `OPOST`
        const OPOST = c::OPOST;

        /// `OLCUC`
        #[cfg(not(any(
            apple,
            target_os = "aix",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "redox",
        )))]
        const OLCUC = c::OLCUC;

        /// `ONLCR`
        const ONLCR = c::ONLCR;

        /// `OCRNL`
        const OCRNL = c::OCRNL;

        /// `ONOCR`
        const ONOCR = c::ONOCR;

        /// `ONLRET`
        const ONLRET = c::ONLRET;

        /// `OFILL`
        #[cfg(not(bsd))]
        const OFILL = c::OFILL;

        /// `OFDEL`
        #[cfg(not(bsd))]
        const OFDEL = c::OFDEL;

        /// `NLDLY`
        #[cfg(not(any(bsd, solarish, target_os = "redox")))]
        const NLDLY = c::NLDLY;

        /// `NL0`
        #[cfg(not(any(bsd, solarish, target_os = "fuchsia", target_os = "redox")))]
        const NL0 = c::NL0;

        /// `NL1`
        #[cfg(not(any(bsd, solarish, target_os = "fuchsia", target_os = "redox")))]
        const NL1 = c::NL1;

        /// `CRDLY`
        #[cfg(not(any(bsd, solarish, target_os = "redox")))]
        const CRDLY = c::CRDLY;

        /// `CR0`
        #[cfg(not(any(bsd, solarish, target_os = "fuchsia", target_os = "redox")))]
        const CR0 = c::CR0;

        /// `CR1`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const CR1 = c::CR1;

        /// `CR2`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const CR2 = c::CR2;

        /// `CR3`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const CR3 = c::CR3;

        /// `TABDLY`
        #[cfg(not(any(
            netbsdlike,
            solarish,
            target_os = "dragonfly",
            target_os = "redox",
        )))]
        const TABDLY = c::TABDLY;

        /// `TAB0`
        #[cfg(not(any(
            netbsdlike,
            solarish,
            target_os = "dragonfly",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const TAB0 = c::TAB0;

        /// `TAB1`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const TAB1 = c::TAB1;

        /// `TAB2`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const TAB2 = c::TAB2;

        /// `TAB3`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const TAB3 = c::TAB3;

        /// `XTABS`
        #[cfg(not(any(
            bsd,
            solarish,
            target_os = "aix",
            target_os = "haiku",
            target_os = "redox",
        )))]
        const XTABS = c::XTABS;

        /// `BSDLY`
        #[cfg(not(any(bsd, solarish, target_os = "redox")))]
        const BSDLY = c::BSDLY;

        /// `BS0`
        #[cfg(not(any(bsd, solarish, target_os = "fuchsia", target_os = "redox")))]
        const BS0 = c::BS0;

        /// `BS1`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const BS1 = c::BS1;

        /// `FFDLY`
        #[cfg(not(any(bsd, solarish, target_os = "redox")))]
        const FFDLY = c::FFDLY;

        /// `FF0`
        #[cfg(not(any(bsd, solarish, target_os = "fuchsia", target_os = "redox")))]
        const FF0 = c::FF0;

        /// `FF1`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const FF1 = c::FF1;

        /// `VTDLY`
        #[cfg(not(any(bsd, solarish, target_os = "redox")))]
        const VTDLY = c::VTDLY;

        /// `VT0`
        #[cfg(not(any(bsd, solarish, target_os = "fuchsia", target_os = "redox")))]
        const VT0 = c::VT0;

        /// `VT1`
        #[cfg(not(any(
            target_env = "musl",
            bsd,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia",
            target_os = "redox",
        )))]
        const VT1 = c::VT1;
    }
}

bitflags! {
    /// Flags controlling special terminal modes.
    ///
    /// `CBAUD`, `CBAUDEX`, `CIBAUD`, and `CIBAUDEX` are not defined here,
    /// because they're handled automatically by [`Termios::set_speed`] and
    /// related functions.
    #[repr(transparent)]
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct ControlModes: c::tcflag_t {
        /// `CSIZE`
        const CSIZE = c::CSIZE;

        /// `CS5`
        const CS5 = c::CS5;

        /// `CS6`
        const CS6 = c::CS6;

        /// `CS7`
        const CS7 = c::CS7;

        /// `CS8`
        const CS8 = c::CS8;

        /// `CSTOPB`
        const CSTOPB = c::CSTOPB;

        /// `CREAD`
        const CREAD = c::CREAD;

        /// `PARENB`
        const PARENB = c::PARENB;

        /// `PARODD`
        const PARODD = c::PARODD;

        /// `HUPCL`
        const HUPCL = c::HUPCL;

        /// `CLOCAL`
        const CLOCAL = c::CLOCAL;

        /// `CRTSCTS`
        #[cfg(not(any(target_os = "aix", target_os = "redox")))]
        const CRTSCTS = c::CRTSCTS;

        /// `CMSPAR`
        #[cfg(not(any(
            bsd,
            solarish,
            target_os = "aix",
            target_os = "emscripten",
            target_os = "haiku",
            target_os = "redox",
        )))]
        const CMSPAR = c::CMSPAR;
    }
}

bitflags! {
    /// Flags controlling "local" terminal modes.
    #[repr(transparent)]
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct LocalModes: c::tcflag_t {
        /// `XCASE`
        #[cfg(any(linux_kernel, target_arch = "s390x", target_os = "haiku"))]
        const XCASE = c::XCASE;

        /// `ECHOCTL`
        #[cfg(not(target_os = "redox"))]
        const ECHOCTL = c::ECHOCTL;

        /// `ECHOPRT`
        #[cfg(not(target_os = "redox"))]
        const ECHOPRT = c::ECHOPRT;

        /// `ECHOKE`
        #[cfg(not(target_os = "redox"))]
        const ECHOKE = c::ECHOKE;

        /// `FLUSHO`
        #[cfg(not(target_os = "redox"))]
        const FLUSHO = c::FLUSHO;

        /// `PENDIN`
        #[cfg(not(target_os = "redox"))]
        const PENDIN = c::PENDIN;

        /// `EXTPROC`
        #[cfg(not(any(target_os = "aix", target_os = "haiku", target_os = "redox")))]
        const EXTPROC = c::EXTPROC;

        /// `ISIG`
        const ISIG = c::ISIG;

        /// `ICANON`—A flag for the `c_lflag` field of [`Termios`] indicating
        /// canonical mode.
        const ICANON = c::ICANON;

        /// `ECHO`
        const ECHO = c::ECHO;

        /// `ECHOE`
        const ECHOE = c::ECHOE;

        /// `ECHOK`
        const ECHOK = c::ECHOK;

        /// `ECHONL`
        const ECHONL = c::ECHONL;

        /// `NOFLSH`
        const NOFLSH = c::NOFLSH;

        /// `TOSTOP`
        const TOSTOP = c::TOSTOP;

        /// `IEXTEN`
        const IEXTEN = c::IEXTEN;
    }
}

/// Speeds for use with [`Termios::set_input_speed`] and
/// [`Termios::set_output_speed`].
///
/// Unlike in some platforms' libc APIs, these always have the same numerical
/// value as their names; for example, `B50` has the value `50`, and so on.
/// Consequently, it's not necessary to use them. They are provided here
/// because they help identify speeds which are likely to be supported, on
/// platforms which don't support arbitrary speeds.
pub mod speed {
    #[cfg(not(bsd))]
    use crate::backend::c;

    /// `B0`
    pub const B0: u32 = 0;

    /// `B50`
    pub const B50: u32 = 50;

    /// `B75`
    pub const B75: u32 = 75;

    /// `B110`
    pub const B110: u32 = 110;

    /// `B134`
    pub const B134: u32 = 134;

    /// `B150`
    pub const B150: u32 = 150;

    /// `B200`
    pub const B200: u32 = 200;

    /// `B300`
    pub const B300: u32 = 300;

    /// `B600`
    pub const B600: u32 = 600;

    /// `B1200`
    pub const B1200: u32 = 1200;

    /// `B1800`
    pub const B1800: u32 = 1800;

    /// `B2400`
    pub const B2400: u32 = 2400;

    /// `B4800`
    pub const B4800: u32 = 4800;

    /// `B9600`
    pub const B9600: u32 = 9600;

    /// `B19200`
    #[doc(alias = "EXTA")]
    pub const B19200: u32 = 19200;

    /// `B38400`
    #[doc(alias = "EXTB")]
    pub const B38400: u32 = 38400;

    /// `B57600`
    #[cfg(not(target_os = "aix"))]
    pub const B57600: u32 = 57600;

    /// `B115200`
    #[cfg(not(target_os = "aix"))]
    pub const B115200: u32 = 115_200;

    /// `B230400`
    #[cfg(not(target_os = "aix"))]
    pub const B230400: u32 = 230_400;

    /// `B460800`
    #[cfg(not(any(
        apple,
        target_os = "aix",
        target_os = "dragonfly",
        target_os = "haiku",
        target_os = "openbsd"
    )))]
    pub const B460800: u32 = 460_800;

    /// `B500000`
    #[cfg(not(any(bsd, solarish, target_os = "aix", target_os = "haiku")))]
    pub const B500000: u32 = 500_000;

    /// `B576000`
    #[cfg(not(any(bsd, solarish, target_os = "aix", target_os = "haiku")))]
    pub const B576000: u32 = 576_000;

    /// `B921600`
    #[cfg(not(any(
        apple,
        target_os = "aix",
        target_os = "dragonfly",
        target_os = "haiku",
        target_os = "openbsd"
    )))]
    pub const B921600: u32 = 921_600;

    /// `B1000000`
    #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
    pub const B1000000: u32 = 1_000_000;

    /// `B1152000`
    #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
    pub const B1152000: u32 = 1_152_000;

    /// `B1500000`
    #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
    pub const B1500000: u32 = 1_500_000;

    /// `B2000000`
    #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
    pub const B2000000: u32 = 2_000_000;

    /// `B2500000`
    #[cfg(not(any(
        target_arch = "sparc",
        target_arch = "sparc64",
        bsd,
        target_os = "aix",
        target_os = "haiku",
        target_os = "solaris",
    )))]
    pub const B2500000: u32 = 2_500_000;

    /// `B3000000`
    #[cfg(not(any(
        target_arch = "sparc",
        target_arch = "sparc64",
        bsd,
        target_os = "aix",
        target_os = "haiku",
        target_os = "solaris",
    )))]
    pub const B3000000: u32 = 3_000_000;

    /// `B3500000`
    #[cfg(not(any(
        target_arch = "sparc",
        target_arch = "sparc64",
        bsd,
        target_os = "aix",
        target_os = "haiku",
        target_os = "solaris",
    )))]
    pub const B3500000: u32 = 3_500_000;

    /// `B4000000`
    #[cfg(not(any(
        target_arch = "sparc",
        target_arch = "sparc64",
        bsd,
        target_os = "aix",
        target_os = "haiku",
        target_os = "solaris",
    )))]
    pub const B4000000: u32 = 4_000_000;

    /// Translate from a `c::speed_t` code to an arbitrary integer speed value
    /// `u32`.
    #[cfg(not(any(linux_kernel, bsd)))]
    pub(crate) fn decode(encoded_speed: c::speed_t) -> Option<u32> {
        match encoded_speed {
            c::B0 => Some(0),
            c::B50 => Some(50),
            c::B75 => Some(75),
            c::B110 => Some(110),
            c::B134 => Some(134),
            c::B150 => Some(150),
            c::B200 => Some(200),
            c::B300 => Some(300),
            c::B600 => Some(600),
            c::B1200 => Some(1200),
            c::B1800 => Some(1800),
            c::B2400 => Some(2400),
            c::B4800 => Some(4800),
            c::B9600 => Some(9600),
            c::B19200 => Some(19200),
            c::B38400 => Some(38400),
            #[cfg(not(target_os = "aix"))]
            c::B57600 => Some(57600),
            #[cfg(not(target_os = "aix"))]
            c::B115200 => Some(115_200),
            #[cfg(not(target_os = "aix"))]
            c::B230400 => Some(230_400),
            #[cfg(not(any(
                apple,
                target_os = "aix",
                target_os = "dragonfly",
                target_os = "haiku",
                target_os = "openbsd"
            )))]
            c::B460800 => Some(460_800),
            #[cfg(not(any(bsd, solarish, target_os = "aix", target_os = "haiku")))]
            c::B500000 => Some(500_000),
            #[cfg(not(any(bsd, solarish, target_os = "aix", target_os = "haiku")))]
            c::B576000 => Some(576_000),
            #[cfg(not(any(
                apple,
                target_os = "aix",
                target_os = "dragonfly",
                target_os = "haiku",
                target_os = "openbsd"
            )))]
            c::B921600 => Some(921_600),
            #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
            c::B1000000 => Some(1_000_000),
            #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
            c::B1152000 => Some(1_152_000),
            #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
            c::B1500000 => Some(1_500_000),
            #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
            c::B2000000 => Some(2_000_000),
            #[cfg(not(any(
                target_arch = "sparc",
                target_arch = "sparc64",
                bsd,
                target_os = "aix",
                target_os = "haiku",
                target_os = "solaris",
            )))]
            c::B2500000 => Some(2_500_000),
            #[cfg(not(any(
                target_arch = "sparc",
                target_arch = "sparc64",
                bsd,
                target_os = "aix",
                target_os = "haiku",
                target_os = "solaris",
            )))]
            c::B3000000 => Some(3_000_000),
            #[cfg(not(any(
                target_arch = "sparc",
                target_arch = "sparc64",
                bsd,
                target_os = "aix",
                target_os = "haiku",
                target_os = "solaris",
            )))]
            c::B3500000 => Some(3_500_000),
            #[cfg(not(any(
                target_arch = "sparc",
                target_arch = "sparc64",
                bsd,
                target_os = "aix",
                target_os = "haiku",
                target_os = "solaris",
            )))]
            c::B4000000 => Some(4_000_000),
            _ => None,
        }
    }

    /// Translate from an arbitrary `u32` arbitrary integer speed value to a
    /// `c::speed_t` code.
    #[cfg(not(bsd))]
    pub(crate) fn encode(speed: u32) -> Option<c::speed_t> {
        match speed {
            0 => Some(c::B0),
            50 => Some(c::B50),
            75 => Some(c::B75),
            110 => Some(c::B110),
            134 => Some(c::B134),
            150 => Some(c::B150),
            200 => Some(c::B200),
            300 => Some(c::B300),
            600 => Some(c::B600),
            1200 => Some(c::B1200),
            1800 => Some(c::B1800),
            2400 => Some(c::B2400),
            4800 => Some(c::B4800),
            9600 => Some(c::B9600),
            19200 => Some(c::B19200),
            38400 => Some(c::B38400),
            #[cfg(not(target_os = "aix"))]
            57600 => Some(c::B57600),
            #[cfg(not(target_os = "aix"))]
            115_200 => Some(c::B115200),
            #[cfg(not(target_os = "aix"))]
            230_400 => Some(c::B230400),
            #[cfg(not(any(
                apple,
                target_os = "aix",
                target_os = "dragonfly",
                target_os = "haiku",
                target_os = "openbsd"
            )))]
            460_800 => Some(c::B460800),
            #[cfg(not(any(bsd, solarish, target_os = "aix", target_os = "haiku")))]
            500_000 => Some(c::B500000),
            #[cfg(not(any(bsd, solarish, target_os = "aix", target_os = "haiku")))]
            576_000 => Some(c::B576000),
            #[cfg(not(any(
                apple,
                target_os = "aix",
                target_os = "dragonfly",
                target_os = "haiku",
                target_os = "openbsd"
            )))]
            921_600 => Some(c::B921600),
            #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
            1_000_000 => Some(c::B1000000),
            #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
            1_152_000 => Some(c::B1152000),
            #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
            1_500_000 => Some(c::B1500000),
            #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
            2_000_000 => Some(c::B2000000),
            #[cfg(not(any(
                target_arch = "sparc",
                target_arch = "sparc64",
                bsd,
                target_os = "aix",
                target_os = "haiku",
                target_os = "solaris",
            )))]
            2_500_000 => Some(c::B2500000),
            #[cfg(not(any(
                target_arch = "sparc",
                target_arch = "sparc64",
                bsd,
                target_os = "aix",
                target_os = "haiku",
                target_os = "solaris",
            )))]
            3_000_000 => Some(c::B3000000),
            #[cfg(not(any(
                target_arch = "sparc",
                target_arch = "sparc64",
                bsd,
                target_os = "aix",
                target_os = "haiku",
                target_os = "solaris",
            )))]
            3_500_000 => Some(c::B3500000),
            #[cfg(not(any(
                target_arch = "sparc",
                target_arch = "sparc64",
                bsd,
                target_os = "aix",
                target_os = "haiku",
                target_os = "solaris",
            )))]
            4_000_000 => Some(c::B4000000),
            _ => None,
        }
    }
}

/// An array indexed by [`SpecialCodeIndex`] indicating the current values
/// of various special control codes.
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct SpecialCodes(pub(crate) [c::cc_t; c::NCCS as usize]);

impl core::ops::Index<SpecialCodeIndex> for SpecialCodes {
    type Output = c::cc_t;

    fn index(&self, index: SpecialCodeIndex) -> &Self::Output {
        &self.0[index.0]
    }
}

impl core::ops::IndexMut<SpecialCodeIndex> for SpecialCodes {
    fn index_mut(&mut self, index: SpecialCodeIndex) -> &mut Self::Output {
        &mut self.0[index.0]
    }
}

/// Indices for use with [`Termios::special_codes`].
pub struct SpecialCodeIndex(usize);

#[rustfmt::skip]
impl SpecialCodeIndex {
    /// `VINTR`
    pub const VINTR: Self = Self(c::VINTR as usize);

    /// `VQUIT`
    pub const VQUIT: Self = Self(c::VQUIT as usize);

    /// `VERASE`
    pub const VERASE: Self = Self(c::VERASE as usize);

    /// `VKILL`
    pub const VKILL: Self = Self(c::VKILL as usize);

    /// `VEOF`
    pub const VEOF: Self = Self(c::VEOF as usize);

    /// `VTIME`
    pub const VTIME: Self = Self(c::VTIME as usize);

    /// `VMIN`
    pub const VMIN: Self = Self(c::VMIN as usize);

    /// `VSWTC`
    #[cfg(not(any(
        apple,
        solarish,
        target_os = "aix",
        target_os = "dragonfly",
        target_os = "freebsd",
        target_os = "haiku",
        target_os = "netbsd",
        target_os = "openbsd",
    )))]
    pub const VSWTC: Self = Self(c::VSWTC as usize);

    /// `VSTART`
    pub const VSTART: Self = Self(c::VSTART as usize);

    /// `VSTOP`
    pub const VSTOP: Self = Self(c::VSTOP as usize);

    /// `VSUSP`
    pub const VSUSP: Self = Self(c::VSUSP as usize);

    /// `VEOL`
    pub const VEOL: Self = Self(c::VEOL as usize);

    /// `VREPRINT`
    #[cfg(not(target_os = "haiku"))]
    pub const VREPRINT: Self = Self(c::VREPRINT as usize);

    /// `VDISCARD`
    #[cfg(not(any(target_os = "aix", target_os = "haiku")))]
    pub const VDISCARD: Self = Self(c::VDISCARD as usize);

    /// `VWERASE`
    #[cfg(not(any(target_os = "aix", target_os = "haiku")))]
    pub const VWERASE: Self = Self(c::VWERASE as usize);

    /// `VLNEXT`
    #[cfg(not(target_os = "haiku"))]
    pub const VLNEXT: Self = Self(c::VLNEXT as usize);

    /// `VEOL2`
    pub const VEOL2: Self = Self(c::VEOL2 as usize);
}

/// `TCSA*` values for use with [`tcsetattr`].
///
/// [`tcsetattr`]: crate::termios::tcsetattr
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u32)]
pub enum OptionalActions {
    /// `TCSANOW`—Make the change immediately.
    #[doc(alias = "TCSANOW")]
    Now = c::TCSANOW as u32,

    /// `TCSADRAIN`—Make the change after all output has been transmitted.
    #[doc(alias = "TCSADRAIN")]
    Drain = c::TCSADRAIN as u32,

    /// `TCSAFLUSH`—Discard any pending input and then make the change
    /// after all output has been transmitted.
    #[doc(alias = "TCSAFLUSH")]
    Flush = c::TCSAFLUSH as u32,
}

/// `TC*` values for use with [`tcflush`].
///
/// [`tcflush`]: crate::termios::tcflush
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u32)]
pub enum QueueSelector {
    /// `TCIFLUSH`—Flush data received but not read.
    #[doc(alias = "TCIFLUSH")]
    IFlush = c::TCIFLUSH as u32,

    /// `TCOFLUSH`—Flush data written but not transmitted.
    #[doc(alias = "TCOFLUSH")]
    OFlush = c::TCOFLUSH as u32,

    /// `TCIOFLUSH`—`IFlush` and `OFlush` combined.
    #[doc(alias = "TCIOFLUSH")]
    IOFlush = c::TCIOFLUSH as u32,
}

/// `TC*` values for use with [`tcflow`].
///
/// [`tcflow`]: crate::termios::tcflow
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u32)]
pub enum Action {
    /// `TCOOFF`—Suspend output.
    #[doc(alias = "TCOOFF")]
    OOff = c::TCOOFF as u32,

    /// `TCOON`—Restart suspended output.
    #[doc(alias = "TCOON")]
    OOn = c::TCOON as u32,

    /// `TCIOFF`—Transmits a STOP byte.
    #[doc(alias = "TCIOFF")]
    IOff = c::TCIOFF as u32,

    /// `TCION`—Transmits a START byte.
    #[doc(alias = "TCION")]
    IOn = c::TCION as u32,
}

/// `struct winsize` for use with [`tcgetwinsize`].
///
/// [`tcgetwinsize`]: crate::termios::tcgetwinsize
#[doc(alias = "winsize")]
pub type Winsize = c::winsize;

#[test]
fn termios_layouts() {
    check_renamed_type!(InputModes, tcflag_t);
    check_renamed_type!(OutputModes, tcflag_t);
    check_renamed_type!(ControlModes, tcflag_t);
    check_renamed_type!(LocalModes, tcflag_t);

    // On platforms with a termios/termios2 split, check `termios`.
    #[cfg(linux_raw)]
    {
        check_renamed_type!(Termios, termios2);
        check_renamed_struct_renamed_field!(Termios, termios2, input_modes, c_iflag);
        check_renamed_struct_renamed_field!(Termios, termios2, output_modes, c_oflag);
        check_renamed_struct_renamed_field!(Termios, termios2, control_modes, c_cflag);
        check_renamed_struct_renamed_field!(Termios, termios2, local_modes, c_lflag);
        check_renamed_struct_renamed_field!(Termios, termios2, line_discipline, c_line);
        check_renamed_struct_renamed_field!(Termios, termios2, special_codes, c_cc);
        check_renamed_struct_renamed_field!(Termios, termios2, input_speed, c_ispeed);
        check_renamed_struct_renamed_field!(Termios, termios2, output_speed, c_ospeed);

        // We assume that `termios` has the same layout as `termios2` minus the
        // `c_ispeed` and `c_ospeed` fields.
        check_renamed_struct_renamed_field!(Termios, termios, input_modes, c_iflag);
        check_renamed_struct_renamed_field!(Termios, termios, output_modes, c_oflag);
        check_renamed_struct_renamed_field!(Termios, termios, control_modes, c_cflag);
        check_renamed_struct_renamed_field!(Termios, termios, local_modes, c_lflag);
        check_renamed_struct_renamed_field!(Termios, termios, special_codes, c_cc);

        // On everything except PowerPC, `termios` matches `termios2` except for
        // the addition of `c_ispeed` and `c_ospeed`.
        #[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
        assert_eq!(
            memoffset::offset_of!(Termios, input_speed),
            core::mem::size_of::<c::termios>()
        );

        // On PowerPC, `termios2` is `termios`.
        #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
        assert_eq!(
            core::mem::size_of::<c::termios2>(),
            core::mem::size_of::<c::termios>()
        );
    }

    #[cfg(not(linux_raw))]
    {
        #[cfg(not(all(
            target_env = "gnu",
            any(
                target_arch = "sparc",
                target_arch = "sparc64",
                target_arch = "mips",
                target_arch = "mips64"
            )
        )))]
        check_renamed_type!(Termios, termios);
        check_renamed_struct_renamed_field!(Termios, termios, input_modes, c_iflag);
        check_renamed_struct_renamed_field!(Termios, termios, output_modes, c_oflag);
        check_renamed_struct_renamed_field!(Termios, termios, control_modes, c_cflag);
        check_renamed_struct_renamed_field!(Termios, termios, local_modes, c_lflag);
        #[cfg(any(
            linux_like,
            target_env = "newlib",
            target_os = "haiku",
            target_os = "fuchsia",
            target_os = "redox"
        ))]
        check_renamed_struct_renamed_field!(Termios, termios, line_discipline, c_line);
        check_renamed_struct_renamed_field!(Termios, termios, special_codes, c_cc);
        #[cfg(not(any(
            linux_kernel,
            solarish,
            target_os = "emscripten",
            target_os = "fuchsia"
        )))]
        {
            check_renamed_struct_renamed_field!(Termios, termios, input_speed, c_ispeed);
            check_renamed_struct_renamed_field!(Termios, termios, output_speed, c_ospeed);
        }
        #[cfg(any(target_env = "musl", target_os = "fuchsia"))]
        {
            check_renamed_struct_renamed_field!(Termios, termios, input_speed, __c_ispeed);
            check_renamed_struct_renamed_field!(Termios, termios, output_speed, __c_ospeed);
        }
    }

    check_renamed_type!(OptionalActions, c_int);
    check_renamed_type!(QueueSelector, c_int);
    check_renamed_type!(Action, c_int);
}

#[test]
#[cfg(not(any(solarish, target_os = "emscripten")))]
fn termios_legacy() {
    // Check that our doc aliases above are correct.
    assert_eq!(c::EXTA, c::B19200);
    assert_eq!(c::EXTB, c::B38400);
}

#[cfg(bsd)]
#[test]
fn termios_bsd() {
    // On BSD platforms we can assume that the `B*` constants have their
    // arbitrary integer speed value. Confirm this.
    assert_eq!(c::B0, 0);
    assert_eq!(c::B50, 50);
    assert_eq!(c::B19200, 19200);
    assert_eq!(c::B38400, 38400);
}

#[test]
#[cfg(not(bsd))]
fn termios_speed_encoding() {
    assert_eq!(speed::encode(0), Some(c::B0));
    assert_eq!(speed::encode(50), Some(c::B50));
    assert_eq!(speed::encode(19200), Some(c::B19200));
    assert_eq!(speed::encode(38400), Some(c::B38400));
    assert_eq!(speed::encode(1), None);
    assert_eq!(speed::encode(!0), None);

    #[cfg(not(linux_kernel))]
    {
        assert_eq!(speed::decode(c::B0), Some(0));
        assert_eq!(speed::decode(c::B50), Some(50));
        assert_eq!(speed::decode(c::B19200), Some(19200));
        assert_eq!(speed::decode(c::B38400), Some(38400));
    }
}

#[cfg(linux_kernel)]
#[test]
fn termios_ioctl_contiguity() {
    // When using `termios2`, we assume that we can add the optional actions
    // value to the ioctl request code. Test this assumption.

    assert_eq!(c::TCSETS2, c::TCSETS2 + 0);
    assert_eq!(c::TCSETSW2, c::TCSETS2 + 1);
    assert_eq!(c::TCSETSF2, c::TCSETS2 + 2);

    assert_eq!(c::TCSANOW - c::TCSANOW, 0);
    assert_eq!(c::TCSADRAIN - c::TCSANOW, 1);
    assert_eq!(c::TCSAFLUSH - c::TCSANOW, 2);

    // MIPS is different here.
    #[cfg(any(target_arch = "mips", target_arch = "mips64"))]
    {
        assert_eq!(i128::from(c::TCSANOW) - i128::from(c::TCSETS), 0);
        assert_eq!(i128::from(c::TCSADRAIN) - i128::from(c::TCSETS), 1);
        assert_eq!(i128::from(c::TCSAFLUSH) - i128::from(c::TCSETS), 2);
    }
    #[cfg(not(any(target_arch = "mips", target_arch = "mips64")))]
    {
        assert_eq!(c::TCSANOW, 0);
        assert_eq!(c::TCSADRAIN, 1);
        assert_eq!(c::TCSAFLUSH, 2);
    }
}

#[cfg(linux_kernel)]
#[test]
fn termios_cibaud() {
    // Test an assumption.
    assert_eq!(c::CIBAUD, c::CBAUD << c::IBSHIFT);
}