Skip to main content

rustix/termios/
types.rs

1use crate::backend::c;
2use crate::backend::termios::types;
3#[cfg(target_os = "nto")]
4use crate::ffi;
5#[cfg(target_os = "nto")]
6use crate::types::Padding;
7use crate::{backend, io};
8use bitflags::bitflags;
9
10/// `struct termios` for use with [`tcgetattr`] and [`tcsetattr`].
11///
12/// [`tcgetattr`]: crate::termios::tcgetattr
13/// [`tcsetattr`]: crate::termios::tcsetattr
14#[repr(C)]
15#[derive(Clone)]
16pub struct Termios {
17    /// How is input interpreted?
18    #[doc(alias = "c_iflag")]
19    pub input_modes: InputModes,
20
21    /// How is output translated?
22    #[doc(alias = "c_oflag")]
23    pub output_modes: OutputModes,
24
25    /// Low-level configuration flags.
26    #[doc(alias = "c_cflag")]
27    pub control_modes: ControlModes,
28
29    /// High-level configuration flags.
30    #[doc(alias = "c_lflag")]
31    pub local_modes: LocalModes,
32
33    /// Line discipline.
34    #[doc(alias = "c_line")]
35    #[cfg(not(all(linux_raw, any(target_arch = "powerpc", target_arch = "powerpc64"))))]
36    #[cfg(any(
37        linux_like,
38        target_env = "newlib",
39        target_os = "fuchsia",
40        target_os = "haiku",
41        target_os = "redox"
42    ))]
43    pub line_discipline: u8,
44
45    /// How are various special control codes handled?
46    #[doc(alias = "c_cc")]
47    #[cfg(not(target_os = "haiku"))]
48    pub special_codes: SpecialCodes,
49
50    #[cfg(target_os = "nto")]
51    pub(crate) __reserved: Padding<[ffi::c_uint; 3]>,
52
53    /// Line discipline.
54    // On PowerPC, this field comes after `c_cc`.
55    #[doc(alias = "c_line")]
56    #[cfg(all(linux_raw, any(target_arch = "powerpc", target_arch = "powerpc64")))]
57    pub line_discipline: c::cc_t,
58
59    /// See the `input_speed` and `set_input_seed` functions.
60    ///
61    /// On Linux and BSDs, this is the arbitrary integer speed value. On all
62    /// other platforms, this is the encoded speed value.
63    #[cfg(not(any(solarish, all(libc, target_env = "newlib"), target_os = "aix")))]
64    pub(crate) input_speed: c::speed_t,
65
66    /// See the `output_speed` and `set_output_seed` functions.
67    ///
68    /// On Linux and BSDs, this is the integer speed value. On all other
69    /// platforms, this is the encoded speed value.
70    #[cfg(not(any(solarish, all(libc, target_env = "newlib"), target_os = "aix")))]
71    pub(crate) output_speed: c::speed_t,
72
73    /// How are various special control codes handled?
74    #[doc(alias = "c_cc")]
75    #[cfg(target_os = "haiku")]
76    pub special_codes: SpecialCodes,
77}
78
79impl Termios {
80    /// `cfmakeraw(self)`—Set a `Termios` value to the settings for “raw” mode.
81    ///
82    /// In raw mode, input is available a byte at a time, echoing is disabled,
83    /// and special terminal input and output codes are disabled.
84    #[cfg(not(target_os = "nto"))]
85    #[doc(alias = "cfmakeraw")]
86    #[inline]
87    pub fn make_raw(&mut self) {
88        backend::termios::syscalls::cfmakeraw(self)
89    }
90
91    /// Return the input communication speed.
92    ///
93    /// Unlike the `c_ispeed` field in glibc and others, this returns the
94    /// integer value of the speed, rather than the `B*` encoded constant
95    /// value.
96    #[doc(alias = "c_ispeed")]
97    #[doc(alias = "cfgetispeed")]
98    #[doc(alias = "cfgetspeed")]
99    #[inline]
100    pub fn input_speed(&self) -> u32 {
101        // On Linux and BSDs, `input_speed` is the arbitrary integer speed.
102        #[cfg(any(linux_kernel, bsd))]
103        {
104            debug_assert!(u32::try_from(self.input_speed).is_ok());
105            self.input_speed as u32
106        }
107
108        // On illumos, `input_speed` is not present.
109        #[cfg(any(solarish, all(libc, target_env = "newlib"), target_os = "aix"))]
110        unsafe {
111            speed::decode(c::cfgetispeed(crate::utils::as_ptr(self).cast())).unwrap()
112        }
113
114        // On other platforms, it's the encoded speed.
115        #[cfg(not(any(
116            linux_kernel,
117            bsd,
118            solarish,
119            all(libc, target_env = "newlib"),
120            target_os = "aix"
121        )))]
122        {
123            speed::decode(self.input_speed).unwrap()
124        }
125    }
126
127    /// Return the output communication speed.
128    ///
129    /// Unlike the `c_ospeed` field in glibc and others, this returns the
130    /// arbitrary integer value of the speed, rather than the `B*` encoded
131    /// constant value.
132    #[inline]
133    pub fn output_speed(&self) -> u32 {
134        // On Linux and BSDs, `output_speed` is the arbitrary integer speed.
135        #[cfg(any(linux_kernel, bsd))]
136        {
137            debug_assert!(u32::try_from(self.output_speed).is_ok());
138            self.output_speed as u32
139        }
140
141        // On illumos, `output_speed` is not present.
142        #[cfg(any(solarish, all(libc, target_env = "newlib"), target_os = "aix"))]
143        unsafe {
144            speed::decode(c::cfgetospeed(crate::utils::as_ptr(self).cast())).unwrap()
145        }
146
147        // On other platforms, it's the encoded speed.
148        #[cfg(not(any(
149            linux_kernel,
150            bsd,
151            solarish,
152            all(libc, target_env = "newlib"),
153            target_os = "aix"
154        )))]
155        {
156            speed::decode(self.output_speed).unwrap()
157        }
158    }
159
160    /// Set the input and output communication speeds.
161    ///
162    /// Unlike the `c_ispeed` and `c_ospeed` fields in glibc and others, this
163    /// takes the arbitrary integer value of the speed, rather than the `B*`
164    /// encoded constant value. Not all implementations support all integer
165    /// values; use the constants in the [`speed`] module for likely-supported
166    /// speeds.
167    #[cfg(not(target_os = "nto"))]
168    #[doc(alias = "cfsetspeed")]
169    #[doc(alias = "CBAUD")]
170    #[doc(alias = "CBAUDEX")]
171    #[doc(alias = "CIBAUD")]
172    #[doc(alias = "CIBAUDEX")]
173    #[inline]
174    pub fn set_speed(&mut self, new_speed: u32) -> io::Result<()> {
175        backend::termios::syscalls::set_speed(self, new_speed)
176    }
177
178    /// Set the input communication speed.
179    ///
180    /// Unlike the `c_ispeed` field in glibc and others, this takes the
181    /// arbitrary integer value of the speed, rather than the `B*` encoded
182    /// constant value. Not all implementations support all integer values; use
183    /// the constants in the [`speed`] module for known-supported speeds.
184    ///
185    /// On some platforms, changing the input speed changes the output speed to
186    /// the same speed.
187    #[doc(alias = "c_ispeed")]
188    #[doc(alias = "cfsetispeed")]
189    #[doc(alias = "CIBAUD")]
190    #[doc(alias = "CIBAUDEX")]
191    #[inline]
192    pub fn set_input_speed(&mut self, new_speed: u32) -> io::Result<()> {
193        backend::termios::syscalls::set_input_speed(self, new_speed)
194    }
195
196    /// Set the output communication speed.
197    ///
198    /// Unlike the `c_ospeed` field in glibc and others, this takes the
199    /// arbitrary integer value of the speed, rather than the `B*` encoded
200    /// constant value. Not all implementations support all integer values; use
201    /// the constants in the [`speed`] module for known-supported speeds.
202    ///
203    /// On some platforms, changing the output speed changes the input speed to
204    /// the same speed.
205    #[doc(alias = "c_ospeed")]
206    #[doc(alias = "cfsetospeed")]
207    #[doc(alias = "CBAUD")]
208    #[doc(alias = "CBAUDEX")]
209    #[inline]
210    pub fn set_output_speed(&mut self, new_speed: u32) -> io::Result<()> {
211        backend::termios::syscalls::set_output_speed(self, new_speed)
212    }
213}
214
215impl core::fmt::Debug for Termios {
216    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
217        let mut d = f.debug_struct("Termios");
218        d.field("input_modes", &self.input_modes);
219        d.field("output_modes", &self.output_modes);
220
221        // This includes any bits set in the `CBAUD` and `CIBAUD` ranges, which
222        // is a little ugly, because we also decode those bits for the speeds
223        // below. However, it seems better to print them here than to hide
224        // them, because hiding them would make the `Termios` debug output
225        // appear to disagree with the `ControlModes` debug output for the same
226        // value, which could be confusing.
227        d.field("control_modes", &self.control_modes);
228
229        d.field("local_modes", &self.local_modes);
230        #[cfg(any(
231            linux_like,
232            target_env = "newlib",
233            target_os = "fuchsia",
234            target_os = "haiku",
235            target_os = "redox"
236        ))]
237        {
238            d.field("line_discipline", &SpecialCode(self.line_discipline));
239        }
240        d.field("special_codes", &self.special_codes);
241        d.field("input_speed", &self.input_speed());
242        d.field("output_speed", &self.output_speed());
243        d.finish()
244    }
245}
246
247bitflags! {
248    /// Flags controlling terminal input.
249    #[repr(transparent)]
250    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
251    pub struct InputModes: types::tcflag_t {
252        /// `IGNBRK`
253        const IGNBRK = c::IGNBRK;
254
255        /// `BRKINT`
256        const BRKINT = c::BRKINT;
257
258        /// `IGNPAR`
259        const IGNPAR = c::IGNPAR;
260
261        /// `PARMRK`
262        const PARMRK = c::PARMRK;
263
264        /// `INPCK`
265        const INPCK = c::INPCK;
266
267        /// `ISTRIP`
268        const ISTRIP = c::ISTRIP;
269
270        /// `INLCR`
271        const INLCR = c::INLCR;
272
273        /// `IGNCR`
274        const IGNCR = c::IGNCR;
275
276        /// `ICRNL`
277        const ICRNL = c::ICRNL;
278
279        /// `IUCLC`
280        #[cfg(any(linux_raw_dep, solarish, target_os = "aix", target_os = "haiku", target_os = "nto"))]
281        const IUCLC = c::IUCLC;
282
283        /// `IXON`
284        const IXON = c::IXON;
285
286        /// `IXANY`
287        #[cfg(not(target_os = "redox"))]
288        const IXANY = c::IXANY;
289
290        /// `IXOFF`
291        const IXOFF = c::IXOFF;
292
293        /// `IMAXBEL`
294        #[cfg(not(any(target_os = "haiku", target_os = "redox")))]
295        const IMAXBEL = c::IMAXBEL;
296
297        /// `IUTF8`
298        #[cfg(not(any(
299            freebsdlike,
300            netbsdlike,
301            solarish,
302            target_os = "aix",
303            target_os = "emscripten",
304            target_os = "haiku",
305            target_os = "hurd",
306            target_os = "redox",
307        )))]
308        const IUTF8 = c::IUTF8;
309
310        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
311        const _ = !0;
312    }
313}
314
315bitflags! {
316    /// Flags controlling terminal output.
317    #[repr(transparent)]
318    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
319    pub struct OutputModes: types::tcflag_t {
320        /// `OPOST`
321        const OPOST = c::OPOST;
322
323        /// `OLCUC`
324        #[cfg(not(any(
325            apple,
326            freebsdlike,
327            target_os = "aix",
328            target_os = "netbsd",
329            target_os = "redox",
330        )))]
331        const OLCUC = c::OLCUC;
332
333        /// `ONLCR`
334        const ONLCR = c::ONLCR;
335
336        /// `OCRNL`
337        const OCRNL = c::OCRNL;
338
339        /// `ONOCR`
340        const ONOCR = c::ONOCR;
341
342        /// `ONLRET`
343        const ONLRET = c::ONLRET;
344
345        /// `OFILL`
346        #[cfg(not(any(
347            freebsdlike,
348            netbsdlike,
349        )))]
350        const OFILL = c::OFILL;
351
352        /// `OFDEL`
353        #[cfg(not(any(
354            freebsdlike,
355            netbsdlike,
356        )))]
357        const OFDEL = c::OFDEL;
358
359        /// `NLDLY`
360        #[cfg(not(any(
361            freebsdlike,
362            netbsdlike,
363            solarish,
364            target_os = "redox",
365        )))]
366        const NLDLY = c::NLDLY;
367
368        /// `NL0`
369        #[cfg(not(any(
370            freebsdlike,
371            netbsdlike,
372            solarish,
373            target_os = "fuchsia",
374            target_os = "redox",
375        )))]
376        const NL0 = c::NL0;
377
378        /// `NL1`
379        #[cfg(not(any(
380            freebsdlike,
381            netbsdlike,
382            solarish,
383            target_os = "fuchsia",
384            target_os = "redox",
385        )))]
386        const NL1 = c::NL1;
387
388        /// `CRDLY`
389        #[cfg(not(any(
390            freebsdlike,
391            netbsdlike,
392            solarish,
393            target_os = "redox",
394        )))]
395        const CRDLY = c::CRDLY;
396
397        /// `CR0`
398        #[cfg(not(any(
399            freebsdlike,
400            netbsdlike,
401            solarish,
402            target_os = "fuchsia",
403            target_os = "redox",
404        )))]
405        const CR0 = c::CR0;
406
407        /// `CR1`
408        #[cfg(not(any(
409            target_env = "musl",
410            freebsdlike,
411            netbsdlike,
412            solarish,
413            target_os = "emscripten",
414            target_os = "fuchsia",
415            target_os = "redox",
416        )))]
417        const CR1 = c::CR1;
418
419        /// `CR2`
420        #[cfg(not(any(
421            target_env = "musl",
422            freebsdlike,
423            netbsdlike,
424            solarish,
425            target_os = "emscripten",
426            target_os = "fuchsia",
427            target_os = "redox",
428        )))]
429        const CR2 = c::CR2;
430
431        /// `CR3`
432        #[cfg(not(any(
433            target_env = "musl",
434            freebsdlike,
435            netbsdlike,
436            solarish,
437            target_os = "emscripten",
438            target_os = "fuchsia",
439            target_os = "redox",
440        )))]
441        const CR3 = c::CR3;
442
443        /// `TABDLY`
444        #[cfg(not(any(
445            netbsdlike,
446            solarish,
447            target_os = "dragonfly",
448            target_os = "redox",
449        )))]
450        const TABDLY = c::TABDLY;
451
452        /// `TAB0`
453        #[cfg(not(any(
454            netbsdlike,
455            solarish,
456            target_os = "dragonfly",
457            target_os = "fuchsia",
458            target_os = "redox",
459        )))]
460        const TAB0 = c::TAB0;
461
462        /// `TAB1`
463        #[cfg(not(any(
464            target_env = "musl",
465            freebsdlike,
466            netbsdlike,
467            solarish,
468            target_os = "emscripten",
469            target_os = "fuchsia",
470            target_os = "redox",
471        )))]
472        const TAB1 = c::TAB1;
473
474        /// `TAB2`
475        #[cfg(not(any(
476            target_env = "musl",
477            freebsdlike,
478            netbsdlike,
479            solarish,
480            target_os = "emscripten",
481            target_os = "fuchsia",
482            target_os = "redox",
483        )))]
484        const TAB2 = c::TAB2;
485
486        /// `TAB3`
487        #[cfg(not(any(
488            target_env = "musl",
489            freebsdlike,
490            netbsdlike,
491            solarish,
492            target_os = "emscripten",
493            target_os = "fuchsia",
494            target_os = "redox",
495        )))]
496        const TAB3 = c::TAB3;
497
498        /// `XTABS`
499        #[cfg(not(any(
500            bsd,
501            solarish,
502            target_os = "haiku",
503            target_os = "redox",
504        )))]
505        const XTABS = c::XTABS;
506
507        /// `BSDLY`
508        #[cfg(not(any(
509            freebsdlike,
510            netbsdlike,
511            solarish,
512            target_os = "redox",
513        )))]
514        const BSDLY = c::BSDLY;
515
516        /// `BS0`
517        #[cfg(not(any(
518            freebsdlike,
519            netbsdlike,
520            solarish,
521            target_os = "fuchsia",
522            target_os = "redox",
523        )))]
524        const BS0 = c::BS0;
525
526        /// `BS1`
527        #[cfg(not(any(
528            target_env = "musl",
529            freebsdlike,
530            netbsdlike,
531            solarish,
532            target_os = "emscripten",
533            target_os = "fuchsia",
534            target_os = "redox",
535        )))]
536        const BS1 = c::BS1;
537
538        /// `FFDLY`
539        #[cfg(not(any(
540            freebsdlike,
541            netbsdlike,
542            solarish,
543            target_os = "redox",
544        )))]
545        const FFDLY = c::FFDLY;
546
547        /// `FF0`
548        #[cfg(not(any(
549            freebsdlike,
550            netbsdlike,
551            solarish,
552            target_os = "fuchsia",
553            target_os = "redox",
554        )))]
555        const FF0 = c::FF0;
556
557        /// `FF1`
558        #[cfg(not(any(
559            target_env = "musl",
560            freebsdlike,
561            netbsdlike,
562            solarish,
563            target_os = "emscripten",
564            target_os = "fuchsia",
565            target_os = "redox",
566        )))]
567        const FF1 = c::FF1;
568
569        /// `VTDLY`
570        #[cfg(not(any(
571            freebsdlike,
572            netbsdlike,
573            solarish,
574            target_os = "redox",
575        )))]
576        const VTDLY = c::VTDLY;
577
578        /// `VT0`
579        #[cfg(not(any(
580            freebsdlike,
581            netbsdlike,
582            solarish,
583            target_os = "fuchsia",
584            target_os = "redox",
585        )))]
586        const VT0 = c::VT0;
587
588        /// `VT1`
589        #[cfg(not(any(
590            target_env = "musl",
591            freebsdlike,
592            netbsdlike,
593            solarish,
594            target_os = "emscripten",
595            target_os = "fuchsia",
596            target_os = "redox",
597        )))]
598        const VT1 = c::VT1;
599
600        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
601        const _ = !0;
602    }
603}
604
605bitflags! {
606    /// Flags controlling special terminal modes.
607    ///
608    /// `CBAUD`, `CBAUDEX`, `CIBAUD`, `CIBAUDEX`, and various `B*` speed
609    /// constants are often included in the control modes, however rustix
610    /// handles them separately, in [`Termios::set_speed`] and related
611    /// functions. If you see extra bits in the `Debug` output, they're
612    /// probably these flags.
613    #[repr(transparent)]
614    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
615    pub struct ControlModes: types::tcflag_t {
616        /// `CSIZE`
617        const CSIZE = c::CSIZE;
618
619        /// `CS5`
620        const CS5 = c::CS5;
621
622        /// `CS6`
623        const CS6 = c::CS6;
624
625        /// `CS7`
626        const CS7 = c::CS7;
627
628        /// `CS8`
629        const CS8 = c::CS8;
630
631        /// `CSTOPB`
632        const CSTOPB = c::CSTOPB;
633
634        /// `CREAD`
635        const CREAD = c::CREAD;
636
637        /// `PARENB`
638        const PARENB = c::PARENB;
639
640        /// `PARODD`
641        const PARODD = c::PARODD;
642
643        /// `HUPCL`
644        const HUPCL = c::HUPCL;
645
646        /// `CLOCAL`
647        const CLOCAL = c::CLOCAL;
648
649        /// `CRTSCTS`
650        #[cfg(not(any(target_os = "aix", target_os = "nto", target_os = "redox")))]
651        const CRTSCTS = c::CRTSCTS;
652
653        /// `CMSPAR`
654        #[cfg(not(any(
655            bsd,
656            solarish,
657            target_os = "aix",
658            target_os = "emscripten",
659            target_os = "haiku",
660            target_os = "hurd",
661            target_os = "nto",
662            target_os = "redox",
663        )))]
664        const CMSPAR = c::CMSPAR;
665
666        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
667        const _ = !0;
668    }
669}
670
671bitflags! {
672    /// Flags controlling “local” terminal modes.
673    #[repr(transparent)]
674    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
675    pub struct LocalModes: types::tcflag_t {
676        /// `XCASE`
677        #[cfg(any(linux_raw_dep, target_arch = "s390x", target_os = "haiku"))]
678        const XCASE = c::XCASE;
679
680        /// `ECHOCTL`
681        #[cfg(not(target_os = "redox"))]
682        const ECHOCTL = c::ECHOCTL;
683
684        /// `ECHOPRT`
685        #[cfg(not(any(target_os = "cygwin", target_os = "nto", target_os = "redox")))]
686        const ECHOPRT = c::ECHOPRT;
687
688        /// `ECHOKE`
689        #[cfg(not(target_os = "redox"))]
690        const ECHOKE = c::ECHOKE;
691
692        /// `FLUSHO`
693        #[cfg(not(any(target_os = "nto", target_os = "redox")))]
694        const FLUSHO = c::FLUSHO;
695
696        /// `PENDIN`
697        #[cfg(not(any(target_os = "cygwin", target_os = "nto", target_os = "redox")))]
698        const PENDIN = c::PENDIN;
699
700        /// `EXTPROC`
701        #[cfg(not(any(
702            target_os = "aix",
703            target_os = "cygwin",
704            target_os = "haiku",
705            target_os = "nto",
706            target_os = "redox",
707        )))]
708        const EXTPROC = c::EXTPROC;
709
710        /// `ISIG`
711        const ISIG = c::ISIG;
712
713        /// `ICANON`—A flag for the `c_lflag` field of [`Termios`] indicating
714        /// canonical mode.
715        const ICANON = c::ICANON;
716
717        /// `ECHO`
718        const ECHO = c::ECHO;
719
720        /// `ECHOE`
721        const ECHOE = c::ECHOE;
722
723        /// `ECHOK`
724        const ECHOK = c::ECHOK;
725
726        /// `ECHONL`
727        const ECHONL = c::ECHONL;
728
729        /// `NOFLSH`
730        const NOFLSH = c::NOFLSH;
731
732        /// `TOSTOP`
733        const TOSTOP = c::TOSTOP;
734
735        /// `IEXTEN`
736        const IEXTEN = c::IEXTEN;
737
738        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
739        const _ = !0;
740    }
741}
742
743/// Speeds for use with [`Termios::set_speed`], [`Termios::set_input_speed`],
744/// and [`Termios::set_output_speed`].
745///
746/// Unlike in some platforms' libc APIs, these always have the same numerical
747/// value as their names; for example, `B50` has the value `50`, and so on.
748/// Consequently, it's not necessary to use them. They are provided here
749/// because they help identify speeds which are likely to be supported, on
750/// platforms and devices which don't support arbitrary speeds.
751pub mod speed {
752    #[cfg(not(bsd))]
753    use crate::backend::c;
754
755    /// `B0`
756    pub const B0: u32 = 0;
757
758    /// `B50`
759    pub const B50: u32 = 50;
760
761    /// `B75`
762    pub const B75: u32 = 75;
763
764    /// `B110`
765    pub const B110: u32 = 110;
766
767    /// `B134`
768    pub const B134: u32 = 134;
769
770    /// `B150`
771    pub const B150: u32 = 150;
772
773    /// `B200`
774    pub const B200: u32 = 200;
775
776    /// `B300`
777    pub const B300: u32 = 300;
778
779    /// `B600`
780    pub const B600: u32 = 600;
781
782    /// `B1200`
783    pub const B1200: u32 = 1200;
784
785    /// `B1800`
786    pub const B1800: u32 = 1800;
787
788    /// `B2400`
789    pub const B2400: u32 = 2400;
790
791    /// `B4800`
792    pub const B4800: u32 = 4800;
793
794    /// `B9600`
795    pub const B9600: u32 = 9600;
796
797    /// `B19200`
798    #[doc(alias = "EXTA")]
799    pub const B19200: u32 = 19200;
800
801    /// `B38400`
802    #[doc(alias = "EXTB")]
803    pub const B38400: u32 = 38400;
804
805    /// `B57600`
806    #[cfg(not(target_os = "aix"))]
807    pub const B57600: u32 = 57600;
808
809    /// `B115200`
810    #[cfg(not(target_os = "aix"))]
811    pub const B115200: u32 = 115_200;
812
813    /// `B230400`
814    #[cfg(not(target_os = "aix"))]
815    pub const B230400: u32 = 230_400;
816
817    /// `B460800`
818    #[cfg(not(any(apple, target_os = "aix", target_os = "haiku", target_os = "openbsd")))]
819    pub const B460800: u32 = 460_800;
820
821    /// `B500000`
822    #[cfg(not(any(bsd, solarish, target_os = "aix", target_os = "haiku")))]
823    pub const B500000: u32 = 500_000;
824
825    /// `B576000`
826    #[cfg(not(any(bsd, solarish, target_os = "aix", target_os = "haiku")))]
827    pub const B576000: u32 = 576_000;
828
829    /// `B921600`
830    #[cfg(not(any(apple, target_os = "aix", target_os = "haiku", target_os = "openbsd")))]
831    pub const B921600: u32 = 921_600;
832
833    /// `B1000000`
834    #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
835    pub const B1000000: u32 = 1_000_000;
836
837    /// `B1152000`
838    #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
839    pub const B1152000: u32 = 1_152_000;
840
841    /// `B1500000`
842    #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
843    pub const B1500000: u32 = 1_500_000;
844
845    /// `B2000000`
846    #[cfg(not(any(bsd, target_os = "aix", target_os = "haiku", target_os = "solaris")))]
847    pub const B2000000: u32 = 2_000_000;
848
849    /// `B2500000`
850    #[cfg(not(any(
851        target_arch = "sparc",
852        target_arch = "sparc64",
853        bsd,
854        target_os = "aix",
855        target_os = "haiku",
856        target_os = "solaris",
857    )))]
858    pub const B2500000: u32 = 2_500_000;
859
860    /// `B3000000`
861    #[cfg(not(any(
862        target_arch = "sparc",
863        target_arch = "sparc64",
864        bsd,
865        target_os = "aix",
866        target_os = "haiku",
867        target_os = "solaris",
868    )))]
869    pub const B3000000: u32 = 3_000_000;
870
871    /// `B3500000`
872    #[cfg(not(any(
873        target_arch = "sparc",
874        target_arch = "sparc64",
875        bsd,
876        target_os = "aix",
877        target_os = "haiku",
878        target_os = "solaris",
879    )))]
880    pub const B3500000: u32 = 3_500_000;
881
882    /// `B4000000`
883    #[cfg(not(any(
884        target_arch = "sparc",
885        target_arch = "sparc64",
886        bsd,
887        target_os = "aix",
888        target_os = "haiku",
889        target_os = "solaris",
890    )))]
891    pub const B4000000: u32 = 4_000_000;
892
893    /// Translate from a `c::speed_t` code to an arbitrary integer speed value
894    /// `u32`.
895    ///
896    /// On BSD platforms, integer speed values are already the same as their
897    /// encoded values.
898    ///
899    /// On Linux on PowerPC, `TCGETS`/`TCSETS` support the `c_ispeed` and
900    /// `c_ospeed` fields.
901    ///
902    /// On Linux on architectures other than PowerPC, `TCGETS`/`TCSETS` don't
903    /// support the `c_ispeed` and `c_ospeed` fields, so we have to fall back
904    /// to `TCGETS2`/`TCSETS2` to support them.
905    #[cfg(not(any(
906        bsd,
907        all(linux_kernel, any(target_arch = "powerpc", target_arch = "powerpc64"))
908    )))]
909    pub(crate) const fn decode(encoded_speed: c::speed_t) -> Option<u32> {
910        match encoded_speed {
911            c::B0 => Some(0),
912            c::B50 => Some(50),
913            c::B75 => Some(75),
914            c::B110 => Some(110),
915            c::B134 => Some(134),
916            c::B150 => Some(150),
917            c::B200 => Some(200),
918            c::B300 => Some(300),
919            c::B600 => Some(600),
920            c::B1200 => Some(1200),
921            c::B1800 => Some(1800),
922            c::B2400 => Some(2400),
923            c::B4800 => Some(4800),
924            c::B9600 => Some(9600),
925            c::B19200 => Some(19200),
926            c::B38400 => Some(38400),
927            #[cfg(not(target_os = "aix"))]
928            c::B57600 => Some(57600),
929            #[cfg(not(target_os = "aix"))]
930            c::B115200 => Some(115_200),
931            #[cfg(not(any(target_os = "aix", target_os = "nto")))]
932            c::B230400 => Some(230_400),
933            #[cfg(not(any(
934                apple,
935                target_os = "aix",
936                target_os = "haiku",
937                target_os = "nto",
938                target_os = "openbsd"
939            )))]
940            c::B460800 => Some(460_800),
941            #[cfg(not(any(
942                bsd,
943                solarish,
944                target_os = "aix",
945                target_os = "haiku",
946                target_os = "nto"
947            )))]
948            c::B500000 => Some(500_000),
949            #[cfg(not(any(
950                bsd,
951                solarish,
952                target_os = "aix",
953                target_os = "haiku",
954                target_os = "nto"
955            )))]
956            c::B576000 => Some(576_000),
957            #[cfg(not(any(
958                apple,
959                target_os = "aix",
960                target_os = "haiku",
961                target_os = "nto",
962                target_os = "openbsd"
963            )))]
964            c::B921600 => Some(921_600),
965            #[cfg(not(any(
966                bsd,
967                target_os = "aix",
968                target_os = "haiku",
969                target_os = "nto",
970                target_os = "solaris"
971            )))]
972            c::B1000000 => Some(1_000_000),
973            #[cfg(not(any(
974                bsd,
975                target_os = "aix",
976                target_os = "haiku",
977                target_os = "nto",
978                target_os = "solaris"
979            )))]
980            c::B1152000 => Some(1_152_000),
981            #[cfg(not(any(
982                bsd,
983                target_os = "aix",
984                target_os = "haiku",
985                target_os = "nto",
986                target_os = "solaris"
987            )))]
988            c::B1500000 => Some(1_500_000),
989            #[cfg(not(any(
990                bsd,
991                target_os = "aix",
992                target_os = "haiku",
993                target_os = "nto",
994                target_os = "solaris"
995            )))]
996            c::B2000000 => Some(2_000_000),
997            #[cfg(not(any(
998                target_arch = "sparc",
999                target_arch = "sparc64",
1000                bsd,
1001                target_os = "aix",
1002                target_os = "haiku",
1003                target_os = "nto",
1004                target_os = "solaris",
1005            )))]
1006            c::B2500000 => Some(2_500_000),
1007            #[cfg(not(any(
1008                target_arch = "sparc",
1009                target_arch = "sparc64",
1010                bsd,
1011                target_os = "aix",
1012                target_os = "haiku",
1013                target_os = "nto",
1014                target_os = "solaris",
1015            )))]
1016            c::B3000000 => Some(3_000_000),
1017            #[cfg(not(any(
1018                target_arch = "sparc",
1019                target_arch = "sparc64",
1020                bsd,
1021                target_os = "aix",
1022                target_os = "cygwin",
1023                target_os = "haiku",
1024                target_os = "nto",
1025                target_os = "solaris",
1026            )))]
1027            c::B3500000 => Some(3_500_000),
1028            #[cfg(not(any(
1029                target_arch = "sparc",
1030                target_arch = "sparc64",
1031                bsd,
1032                target_os = "aix",
1033                target_os = "cygwin",
1034                target_os = "haiku",
1035                target_os = "nto",
1036                target_os = "solaris",
1037            )))]
1038            c::B4000000 => Some(4_000_000),
1039            _ => None,
1040        }
1041    }
1042
1043    /// Translate from an arbitrary `u32` arbitrary integer speed value to a
1044    /// `c::speed_t` code.
1045    #[cfg(not(bsd))]
1046    pub(crate) const fn encode(speed: u32) -> Option<c::speed_t> {
1047        match speed {
1048            0 => Some(c::B0),
1049            50 => Some(c::B50),
1050            75 => Some(c::B75),
1051            110 => Some(c::B110),
1052            134 => Some(c::B134),
1053            150 => Some(c::B150),
1054            200 => Some(c::B200),
1055            300 => Some(c::B300),
1056            600 => Some(c::B600),
1057            1200 => Some(c::B1200),
1058            1800 => Some(c::B1800),
1059            2400 => Some(c::B2400),
1060            4800 => Some(c::B4800),
1061            9600 => Some(c::B9600),
1062            19200 => Some(c::B19200),
1063            38400 => Some(c::B38400),
1064            #[cfg(not(target_os = "aix"))]
1065            57600 => Some(c::B57600),
1066            #[cfg(not(target_os = "aix"))]
1067            115_200 => Some(c::B115200),
1068            #[cfg(not(any(target_os = "aix", target_os = "nto")))]
1069            230_400 => Some(c::B230400),
1070            #[cfg(not(any(
1071                apple,
1072                target_os = "aix",
1073                target_os = "haiku",
1074                target_os = "nto",
1075                target_os = "openbsd",
1076            )))]
1077            460_800 => Some(c::B460800),
1078            #[cfg(not(any(
1079                bsd,
1080                solarish,
1081                target_os = "aix",
1082                target_os = "haiku",
1083                target_os = "nto"
1084            )))]
1085            500_000 => Some(c::B500000),
1086            #[cfg(not(any(
1087                bsd,
1088                solarish,
1089                target_os = "aix",
1090                target_os = "haiku",
1091                target_os = "nto"
1092            )))]
1093            576_000 => Some(c::B576000),
1094            #[cfg(not(any(
1095                apple,
1096                target_os = "aix",
1097                target_os = "haiku",
1098                target_os = "nto",
1099                target_os = "openbsd"
1100            )))]
1101            921_600 => Some(c::B921600),
1102            #[cfg(not(any(
1103                bsd,
1104                target_os = "aix",
1105                target_os = "haiku",
1106                target_os = "nto",
1107                target_os = "solaris"
1108            )))]
1109            1_000_000 => Some(c::B1000000),
1110            #[cfg(not(any(
1111                bsd,
1112                target_os = "aix",
1113                target_os = "haiku",
1114                target_os = "nto",
1115                target_os = "solaris"
1116            )))]
1117            1_152_000 => Some(c::B1152000),
1118            #[cfg(not(any(
1119                bsd,
1120                target_os = "aix",
1121                target_os = "haiku",
1122                target_os = "nto",
1123                target_os = "solaris"
1124            )))]
1125            1_500_000 => Some(c::B1500000),
1126            #[cfg(not(any(
1127                bsd,
1128                target_os = "aix",
1129                target_os = "haiku",
1130                target_os = "nto",
1131                target_os = "solaris"
1132            )))]
1133            2_000_000 => Some(c::B2000000),
1134            #[cfg(not(any(
1135                target_arch = "sparc",
1136                target_arch = "sparc64",
1137                bsd,
1138                target_os = "aix",
1139                target_os = "haiku",
1140                target_os = "nto",
1141                target_os = "solaris",
1142            )))]
1143            2_500_000 => Some(c::B2500000),
1144            #[cfg(not(any(
1145                target_arch = "sparc",
1146                target_arch = "sparc64",
1147                bsd,
1148                target_os = "aix",
1149                target_os = "haiku",
1150                target_os = "nto",
1151                target_os = "solaris",
1152            )))]
1153            3_000_000 => Some(c::B3000000),
1154            #[cfg(not(any(
1155                target_arch = "sparc",
1156                target_arch = "sparc64",
1157                bsd,
1158                target_os = "aix",
1159                target_os = "cygwin",
1160                target_os = "haiku",
1161                target_os = "nto",
1162                target_os = "solaris",
1163            )))]
1164            3_500_000 => Some(c::B3500000),
1165            #[cfg(not(any(
1166                target_arch = "sparc",
1167                target_arch = "sparc64",
1168                bsd,
1169                target_os = "aix",
1170                target_os = "cygwin",
1171                target_os = "haiku",
1172                target_os = "nto",
1173                target_os = "solaris",
1174            )))]
1175            4_000_000 => Some(c::B4000000),
1176            _ => None,
1177        }
1178    }
1179}
1180
1181/// An array indexed by [`SpecialCodeIndex`] indicating the current values of
1182/// various special control codes.
1183#[repr(transparent)]
1184#[derive(Clone)]
1185pub struct SpecialCodes(pub(crate) [c::cc_t; c::NCCS as usize]);
1186
1187impl core::ops::Index<SpecialCodeIndex> for SpecialCodes {
1188    type Output = u8;
1189
1190    fn index(&self, index: SpecialCodeIndex) -> &Self::Output {
1191        &self.0[index.0]
1192    }
1193}
1194
1195impl core::ops::IndexMut<SpecialCodeIndex> for SpecialCodes {
1196    fn index_mut(&mut self, index: SpecialCodeIndex) -> &mut Self::Output {
1197        &mut self.0[index.0]
1198    }
1199}
1200
1201impl core::fmt::Debug for SpecialCodes {
1202    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1203        write!(f, "SpecialCodes {{")?;
1204        let mut first = true;
1205        for i in 0..self.0.len() {
1206            if first {
1207                write!(f, " ")?;
1208            } else {
1209                write!(f, ", ")?;
1210            }
1211            first = false;
1212            let index = SpecialCodeIndex(i);
1213            write!(f, "{:?}: {:?}", index, SpecialCode(self[index]))?;
1214        }
1215        if !first {
1216            write!(f, " ")?;
1217        }
1218        write!(f, "}}")
1219    }
1220}
1221
1222/// A newtype for pretty printing.
1223struct SpecialCode(u8);
1224
1225impl core::fmt::Debug for SpecialCode {
1226    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1227        if self.0 == 0 {
1228            write!(f, "<undef>")
1229        } else if self.0 < 0x20 {
1230            write!(f, "^{}", (self.0 + 0x40) as char)
1231        } else if self.0 == 0x7f {
1232            write!(f, "^?")
1233        } else if self.0 >= 0x80 {
1234            write!(f, "M-")?;
1235            SpecialCode(self.0 - 0x80).fmt(f)
1236        } else {
1237            write!(f, "{}", (self.0 as char))
1238        }
1239    }
1240}
1241
1242/// Indices for use with [`Termios::special_codes`].
1243#[derive(Copy, Clone, Eq, PartialEq, Hash)]
1244pub struct SpecialCodeIndex(usize);
1245
1246#[rustfmt::skip]
1247impl SpecialCodeIndex {
1248    /// `VINTR`
1249    pub const VINTR: Self = Self(c::VINTR as usize);
1250
1251    /// `VQUIT`
1252    pub const VQUIT: Self = Self(c::VQUIT as usize);
1253
1254    /// `VERASE`
1255    pub const VERASE: Self = Self(c::VERASE as usize);
1256
1257    /// `VKILL`
1258    pub const VKILL: Self = Self(c::VKILL as usize);
1259
1260    /// `VEOF`
1261    pub const VEOF: Self = Self(c::VEOF as usize);
1262
1263    /// `VTIME`
1264    pub const VTIME: Self = Self(c::VTIME as usize);
1265
1266    /// `VMIN`
1267    pub const VMIN: Self = Self(c::VMIN as usize);
1268
1269    /// `VSWTC`
1270    #[cfg(not(any(
1271        bsd,
1272        solarish,
1273        target_os = "aix",
1274        target_os = "haiku",
1275        target_os = "hurd",
1276        target_os = "nto",
1277    )))]
1278    pub const VSWTC: Self = Self(c::VSWTC as usize);
1279
1280    /// `VSTART`
1281    pub const VSTART: Self = Self(c::VSTART as usize);
1282
1283    /// `VSTOP`
1284    pub const VSTOP: Self = Self(c::VSTOP as usize);
1285
1286    /// `VSUSP`
1287    pub const VSUSP: Self = Self(c::VSUSP as usize);
1288
1289    /// `VEOL`
1290    pub const VEOL: Self = Self(c::VEOL as usize);
1291
1292    /// `VREPRINT`
1293    #[cfg(not(target_os = "haiku"))]
1294    pub const VREPRINT: Self = Self(c::VREPRINT as usize);
1295
1296    /// `VDISCARD`
1297    #[cfg(not(any(target_os = "aix", target_os = "haiku")))]
1298    pub const VDISCARD: Self = Self(c::VDISCARD as usize);
1299
1300    /// `VWERASE`
1301    #[cfg(not(any(target_os = "aix", target_os = "haiku")))]
1302    pub const VWERASE: Self = Self(c::VWERASE as usize);
1303
1304    /// `VLNEXT`
1305    #[cfg(not(target_os = "haiku"))]
1306    pub const VLNEXT: Self = Self(c::VLNEXT as usize);
1307
1308    /// `VEOL2`
1309    pub const VEOL2: Self = Self(c::VEOL2 as usize);
1310
1311    /// `VSWTCH`
1312    #[cfg(any(solarish, target_os = "haiku", target_os = "nto"))]
1313    pub const VSWTCH: Self = Self(c::VSWTCH as usize);
1314
1315    /// `VDSUSP`
1316    #[cfg(any(
1317        bsd,
1318        solarish,
1319        target_os = "aix",
1320        target_os = "hurd",
1321        target_os = "nto"
1322    ))]
1323    pub const VDSUSP: Self = Self(c::VDSUSP as usize);
1324
1325    /// `VSTATUS`
1326    #[cfg(any(bsd, target_os = "hurd", target_os = "illumos"))]
1327    pub const VSTATUS: Self = Self(c::VSTATUS as usize);
1328
1329    /// `VERASE2`
1330    #[cfg(any(freebsdlike, target_os = "illumos"))]
1331    pub const VERASE2: Self = Self(c::VERASE2 as usize);
1332}
1333
1334impl core::fmt::Debug for SpecialCodeIndex {
1335    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1336        match *self {
1337            Self::VINTR => write!(f, "VINTR"),
1338            Self::VQUIT => write!(f, "VQUIT"),
1339            Self::VERASE => write!(f, "VERASE"),
1340            Self::VKILL => write!(f, "VKILL"),
1341            #[cfg(not(any(
1342                solarish,
1343                all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")),
1344                target_os = "aix",
1345                target_os = "haiku",
1346            )))]
1347            Self::VEOF => write!(f, "VEOF"),
1348            #[cfg(not(any(
1349                solarish,
1350                all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")),
1351                target_os = "aix",
1352                target_os = "haiku",
1353            )))]
1354            Self::VTIME => write!(f, "VTIME"),
1355            #[cfg(not(any(
1356                solarish,
1357                all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")),
1358                target_os = "aix",
1359                target_os = "haiku",
1360            )))]
1361            Self::VMIN => write!(f, "VMIN"),
1362
1363            // On Solarish platforms, Linux on SPARC, AIX, and Haiku, `VMIN`
1364            // and `VTIME` have the same value as `VEOF` and `VEOL`.
1365            #[cfg(any(
1366                solarish,
1367                all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")),
1368                target_os = "aix",
1369                target_os = "haiku",
1370            ))]
1371            Self::VMIN => write!(f, "VMIN/VEOF"),
1372            #[cfg(any(
1373                solarish,
1374                all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")),
1375                target_os = "aix",
1376                target_os = "haiku",
1377            ))]
1378            Self::VTIME => write!(f, "VTIME/VEOL"),
1379
1380            #[cfg(not(any(
1381                bsd,
1382                solarish,
1383                target_os = "aix",
1384                target_os = "haiku",
1385                target_os = "hurd",
1386                target_os = "nto",
1387            )))]
1388            Self::VSWTC => write!(f, "VSWTC"),
1389            Self::VSTART => write!(f, "VSTART"),
1390            Self::VSTOP => write!(f, "VSTOP"),
1391            Self::VSUSP => write!(f, "VSUSP"),
1392            #[cfg(not(any(
1393                solarish,
1394                all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64")),
1395                target_os = "aix",
1396                target_os = "haiku",
1397            )))]
1398            Self::VEOL => write!(f, "VEOL"),
1399            #[cfg(not(target_os = "haiku"))]
1400            Self::VREPRINT => write!(f, "VREPRINT"),
1401            #[cfg(not(any(target_os = "aix", target_os = "haiku")))]
1402            Self::VDISCARD => write!(f, "VDISCARD"),
1403            #[cfg(not(any(target_os = "aix", target_os = "haiku")))]
1404            Self::VWERASE => write!(f, "VWERASE"),
1405            #[cfg(not(target_os = "haiku"))]
1406            Self::VLNEXT => write!(f, "VLNEXT"),
1407            Self::VEOL2 => write!(f, "VEOL2"),
1408            #[cfg(any(solarish, target_os = "haiku", target_os = "nto"))]
1409            Self::VSWTCH => write!(f, "VSWTCH"),
1410            #[cfg(any(
1411                bsd,
1412                solarish,
1413                target_os = "aix",
1414                target_os = "hurd",
1415                target_os = "nto"
1416            ))]
1417            Self::VDSUSP => write!(f, "VDSUSP"),
1418            #[cfg(any(bsd, target_os = "hurd", target_os = "illumos"))]
1419            Self::VSTATUS => write!(f, "VSTATUS"),
1420            #[cfg(any(freebsdlike, target_os = "illumos"))]
1421            Self::VERASE2 => write!(f, "VERASE2"),
1422
1423            _ => write!(f, "unknown"),
1424        }
1425    }
1426}
1427
1428/// `TCSA*` values for use with [`tcsetattr`].
1429///
1430/// [`tcsetattr`]: crate::termios::tcsetattr
1431#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
1432#[repr(u32)]
1433pub enum OptionalActions {
1434    /// `TCSANOW`—Make the change immediately.
1435    #[doc(alias = "TCSANOW")]
1436    Now = c::TCSANOW as u32,
1437
1438    /// `TCSADRAIN`—Make the change after all output has been transmitted.
1439    #[doc(alias = "TCSADRAIN")]
1440    Drain = c::TCSADRAIN as u32,
1441
1442    /// `TCSAFLUSH`—Discard any pending input and then make the change
1443    /// after all output has been transmitted.
1444    #[doc(alias = "TCSAFLUSH")]
1445    Flush = c::TCSAFLUSH as u32,
1446}
1447
1448/// `TC*` values for use with [`tcflush`].
1449///
1450/// [`tcflush`]: crate::termios::tcflush
1451#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
1452#[repr(u32)]
1453pub enum QueueSelector {
1454    /// `TCIFLUSH`—Flush data received but not read.
1455    #[doc(alias = "TCIFLUSH")]
1456    IFlush = c::TCIFLUSH as u32,
1457
1458    /// `TCOFLUSH`—Flush data written but not transmitted.
1459    #[doc(alias = "TCOFLUSH")]
1460    OFlush = c::TCOFLUSH as u32,
1461
1462    /// `TCIOFLUSH`—`IFlush` and `OFlush` combined.
1463    #[doc(alias = "TCIOFLUSH")]
1464    IOFlush = c::TCIOFLUSH as u32,
1465}
1466
1467/// `TC*` values for use with [`tcflow`].
1468///
1469/// [`tcflow`]: crate::termios::tcflow
1470#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
1471#[repr(u32)]
1472pub enum Action {
1473    /// `TCOOFF`—Suspend output.
1474    #[doc(alias = "TCOOFF")]
1475    OOff = c::TCOOFF as u32,
1476
1477    /// `TCOON`—Restart suspended output.
1478    #[doc(alias = "TCOON")]
1479    OOn = c::TCOON as u32,
1480
1481    /// `TCIOFF`—Transmits a STOP byte.
1482    #[doc(alias = "TCIOFF")]
1483    IOff = c::TCIOFF as u32,
1484
1485    /// `TCION`—Transmits a START byte.
1486    #[doc(alias = "TCION")]
1487    IOn = c::TCION as u32,
1488}
1489
1490/// `struct winsize` for use with [`tcgetwinsize`].
1491///
1492/// [`tcgetwinsize`]: crate::termios::tcgetwinsize
1493#[doc(alias = "winsize")]
1494#[repr(C)]
1495#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
1496#[allow(missing_docs)]
1497pub struct Winsize {
1498    /// The number of rows the terminal has.
1499    pub ws_row: u16,
1500    /// The number of columns the terminal has.
1501    pub ws_col: u16,
1502
1503    pub ws_xpixel: u16,
1504    pub ws_ypixel: u16,
1505}
1506
1507#[cfg(test)]
1508mod tests {
1509    use super::*;
1510
1511    #[test]
1512    fn termios_layouts() {
1513        check_renamed_type!(InputModes, tcflag_t);
1514        check_renamed_type!(OutputModes, tcflag_t);
1515        check_renamed_type!(ControlModes, tcflag_t);
1516        check_renamed_type!(LocalModes, tcflag_t);
1517        static_assertions::assert_eq_size!(u8, libc::cc_t);
1518        static_assertions::assert_eq_size!(types::tcflag_t, libc::tcflag_t);
1519
1520        check_renamed_struct!(Winsize, winsize, ws_row, ws_col, ws_xpixel, ws_ypixel);
1521
1522        // On platforms with a termios/termios2 split, check `termios`.
1523        #[cfg(linux_raw)]
1524        {
1525            check_renamed_type!(Termios, termios2);
1526            check_renamed_struct_renamed_field!(Termios, termios2, input_modes, c_iflag);
1527            check_renamed_struct_renamed_field!(Termios, termios2, output_modes, c_oflag);
1528            check_renamed_struct_renamed_field!(Termios, termios2, control_modes, c_cflag);
1529            check_renamed_struct_renamed_field!(Termios, termios2, local_modes, c_lflag);
1530            check_renamed_struct_renamed_field!(Termios, termios2, line_discipline, c_line);
1531            check_renamed_struct_renamed_field!(Termios, termios2, special_codes, c_cc);
1532            check_renamed_struct_renamed_field!(Termios, termios2, input_speed, c_ispeed);
1533            check_renamed_struct_renamed_field!(Termios, termios2, output_speed, c_ospeed);
1534
1535            // We assume that `termios` has the same layout as `termios2` minus the
1536            // `c_ispeed` and `c_ospeed` fields.
1537            check_renamed_struct_renamed_field!(Termios, termios, input_modes, c_iflag);
1538            check_renamed_struct_renamed_field!(Termios, termios, output_modes, c_oflag);
1539            check_renamed_struct_renamed_field!(Termios, termios, control_modes, c_cflag);
1540            check_renamed_struct_renamed_field!(Termios, termios, local_modes, c_lflag);
1541            check_renamed_struct_renamed_field!(Termios, termios, special_codes, c_cc);
1542
1543            // On everything except PowerPC, `termios` matches `termios2` except
1544            // for the addition of `c_ispeed` and `c_ospeed`.
1545            #[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
1546            static_assertions::const_assert_eq!(
1547                memoffset::offset_of!(Termios, input_speed),
1548                core::mem::size_of::<c::termios>()
1549            );
1550
1551            // On PowerPC, `termios2` is `termios`.
1552            #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
1553            static_assertions::assert_eq_size!(c::termios2, c::termios);
1554        }
1555
1556        #[cfg(not(linux_raw))]
1557        {
1558            // On MIPS, SPARC, and Android, the libc lacks the ospeed and ispeed
1559            // fields.
1560            #[cfg(all(
1561                not(all(
1562                    target_env = "gnu",
1563                    any(
1564                        target_arch = "mips",
1565                        target_arch = "mips32r6",
1566                        target_arch = "mips64",
1567                        target_arch = "mips64r6",
1568                        target_arch = "sparc",
1569                        target_arch = "sparc64"
1570                    )
1571                )),
1572                not(all(libc, target_os = "android"))
1573            ))]
1574            check_renamed_type!(Termios, termios);
1575            #[cfg(not(all(
1576                not(all(
1577                    target_env = "gnu",
1578                    any(
1579                        target_arch = "mips",
1580                        target_arch = "mips32r6",
1581                        target_arch = "mips64",
1582                        target_arch = "mips64r6",
1583                        target_arch = "sparc",
1584                        target_arch = "sparc64"
1585                    )
1586                )),
1587                not(all(libc, target_os = "android"))
1588            )))]
1589            static_assertions::const_assert!(
1590                core::mem::size_of::<Termios>() >= core::mem::size_of::<c::termios>()
1591            );
1592
1593            check_renamed_struct_renamed_field!(Termios, termios, input_modes, c_iflag);
1594            check_renamed_struct_renamed_field!(Termios, termios, output_modes, c_oflag);
1595            check_renamed_struct_renamed_field!(Termios, termios, control_modes, c_cflag);
1596            check_renamed_struct_renamed_field!(Termios, termios, local_modes, c_lflag);
1597            #[cfg(any(
1598                linux_like,
1599                target_env = "newlib",
1600                target_os = "fuchsia",
1601                target_os = "haiku",
1602                target_os = "redox"
1603            ))]
1604            check_renamed_struct_renamed_field!(Termios, termios, line_discipline, c_line);
1605            check_renamed_struct_renamed_field!(Termios, termios, special_codes, c_cc);
1606            #[cfg(not(any(
1607                linux_kernel,
1608                solarish,
1609                target_os = "emscripten",
1610                target_os = "fuchsia"
1611            )))]
1612            {
1613                check_renamed_struct_renamed_field!(Termios, termios, input_speed, c_ispeed);
1614                check_renamed_struct_renamed_field!(Termios, termios, output_speed, c_ospeed);
1615            }
1616            #[cfg(any(target_env = "musl", target_os = "fuchsia"))]
1617            {
1618                check_renamed_struct_renamed_field!(Termios, termios, input_speed, __c_ispeed);
1619                check_renamed_struct_renamed_field!(Termios, termios, output_speed, __c_ospeed);
1620            }
1621        }
1622
1623        check_renamed_type!(OptionalActions, c_int);
1624        check_renamed_type!(QueueSelector, c_int);
1625        check_renamed_type!(Action, c_int);
1626    }
1627
1628    #[test]
1629    #[cfg(not(any(
1630        solarish,
1631        target_os = "cygwin",
1632        target_os = "emscripten",
1633        target_os = "haiku",
1634        target_os = "redox",
1635    )))]
1636    fn termios_legacy() {
1637        // Check that our doc aliases above are correct.
1638        static_assertions::const_assert_eq!(c::EXTA, c::B19200);
1639        static_assertions::const_assert_eq!(c::EXTB, c::B38400);
1640    }
1641
1642    #[cfg(bsd)]
1643    #[test]
1644    fn termios_bsd() {
1645        // On BSD platforms we can assume that the `B*` constants have their
1646        // arbitrary integer speed value. Confirm this.
1647        static_assertions::const_assert_eq!(c::B0, 0);
1648        static_assertions::const_assert_eq!(c::B50, 50);
1649        static_assertions::const_assert_eq!(c::B19200, 19200);
1650        static_assertions::const_assert_eq!(c::B38400, 38400);
1651    }
1652
1653    #[test]
1654    #[cfg(not(bsd))]
1655    fn termios_speed_encoding() {
1656        assert_eq!(speed::encode(0), Some(c::B0));
1657        assert_eq!(speed::encode(50), Some(c::B50));
1658        assert_eq!(speed::encode(19200), Some(c::B19200));
1659        assert_eq!(speed::encode(38400), Some(c::B38400));
1660        assert_eq!(speed::encode(1), None);
1661        assert_eq!(speed::encode(!0), None);
1662
1663        #[cfg(not(linux_kernel))]
1664        {
1665            assert_eq!(speed::decode(c::B0), Some(0));
1666            assert_eq!(speed::decode(c::B50), Some(50));
1667            assert_eq!(speed::decode(c::B19200), Some(19200));
1668            assert_eq!(speed::decode(c::B38400), Some(38400));
1669        }
1670    }
1671
1672    #[cfg(linux_kernel)]
1673    #[test]
1674    fn termios_ioctl_contiguity() {
1675        // When using `termios2`, we assume that we can add the optional actions
1676        // value to the ioctl request code. Test this assumption.
1677
1678        static_assertions::const_assert_eq!(c::TCSETS2, c::TCSETS2 + 0);
1679        static_assertions::const_assert_eq!(c::TCSETSW2, c::TCSETS2 + 1);
1680        static_assertions::const_assert_eq!(c::TCSETSF2, c::TCSETS2 + 2);
1681
1682        static_assertions::const_assert_eq!(c::TCSANOW - c::TCSANOW, 0);
1683        static_assertions::const_assert_eq!(c::TCSADRAIN - c::TCSANOW, 1);
1684        static_assertions::const_assert_eq!(c::TCSAFLUSH - c::TCSANOW, 2);
1685
1686        // MIPS is different here.
1687        #[cfg(any(
1688            target_arch = "mips",
1689            target_arch = "mips32r6",
1690            target_arch = "mips64",
1691            target_arch = "mips64r6"
1692        ))]
1693        {
1694            assert_eq!(i128::from(c::TCSANOW) - i128::from(c::TCSETS), 0);
1695            assert_eq!(i128::from(c::TCSADRAIN) - i128::from(c::TCSETS), 1);
1696            assert_eq!(i128::from(c::TCSAFLUSH) - i128::from(c::TCSETS), 2);
1697        }
1698        #[cfg(not(any(
1699            target_arch = "mips",
1700            target_arch = "mips32r6",
1701            target_arch = "mips64",
1702            target_arch = "mips64r6"
1703        )))]
1704        {
1705            static_assertions::const_assert_eq!(c::TCSANOW, 0);
1706            static_assertions::const_assert_eq!(c::TCSADRAIN, 1);
1707            static_assertions::const_assert_eq!(c::TCSAFLUSH, 2);
1708        }
1709    }
1710
1711    #[cfg(linux_kernel)]
1712    #[test]
1713    fn termios_cibaud() {
1714        // Test an assumption.
1715        static_assertions::const_assert_eq!(c::CIBAUD, c::CBAUD << c::IBSHIFT);
1716    }
1717}