diff --git a/Cargo.lock b/Cargo.lock index 5bd8496c114..83f806239e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2868,7 +2868,7 @@ dependencies = [ name = "uu_stty" version = "0.0.14" dependencies = [ - "clap 3.1.18", + "clap 3.2.15", "nix", "uucore", ] diff --git a/src/uu/stty/src/flags.rs b/src/uu/stty/src/flags.rs index e564011b5bc..4ae27350efc 100644 --- a/src/uu/stty/src/flags.rs +++ b/src/uu/stty/src/flags.rs @@ -13,9 +13,13 @@ use nix::sys::termios::{ BaudRate, ControlFlags as C, InputFlags as I, LocalFlags as L, OutputFlags as O, }; -pub const CONTROL_FLAGS: [Flag; 12] = [ +pub const CONTROL_FLAGS: &[Flag] = &[ Flag::new("parenb", C::PARENB), Flag::new("parodd", C::PARODD), + #[cfg(any( + target_os = "android", + all(target_os = "linux", not(target_arch = "mips")) + ))] Flag::new("cmspar", C::CMSPAR), Flag::new_grouped("cs5", C::CS5, C::CSIZE), Flag::new_grouped("cs6", C::CS6, C::CSIZE), @@ -28,7 +32,7 @@ pub const CONTROL_FLAGS: [Flag; 12] = [ Flag::new("crtscts", C::CRTSCTS), ]; -pub const INPUT_FLAGS: [Flag; 15] = [ +pub const INPUT_FLAGS: &[Flag] = &[ Flag::new("ignbrk", I::IGNBRK), Flag::new("brkint", I::BRKINT).sane(), Flag::new("ignpar", I::IGNPAR), @@ -48,8 +52,14 @@ pub const INPUT_FLAGS: [Flag; 15] = [ Flag::new("iutf8", I::IUTF8), ]; -pub const OUTPUT_FLAGS: [Flag; 24] = [ +pub const OUTPUT_FLAGS: &[Flag] = &[ Flag::new("opost", O::OPOST).sane(), + #[cfg(any( + target_os = "android", + target_os = "haiku", + target_os = "linux", + target_os = "openbsd" + ))] Flag::new("olcuc", O::OLCUC), Flag::new("ocrnl", O::OCRNL), Flag::new("onlcr", O::ONLCR).sane(), @@ -75,7 +85,7 @@ pub const OUTPUT_FLAGS: [Flag; 24] = [ Flag::new_grouped("ff1", O::FF1, O::FFDLY), ]; -pub const LOCAL_FLAGS: [Flag; 18] = [ +pub const LOCAL_FLAGS: &[Flag] = &[ Flag::new("isig", L::ISIG).sane(), Flag::new("icanon", L::ICANON).sane(), Flag::new("iexten", L::IEXTEN).sane(), @@ -98,6 +108,15 @@ pub const LOCAL_FLAGS: [Flag; 18] = [ Flag::new("extproc", L::EXTPROC), ]; +// BSD's use u32 as baud rate, to using the enum is unnecessary. +#[cfg(not(any( + target_os = "freebsd", + target_os = "dragonfly", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" +)))] pub const BAUD_RATES: &[(&str, BaudRate)] = &[ ("0", BaudRate::B0), ("50", BaudRate::B50), @@ -111,62 +130,17 @@ pub const BAUD_RATES: &[(&str, BaudRate)] = &[ ("1200", BaudRate::B1200), ("1800", BaudRate::B1800), ("2400", BaudRate::B2400), - #[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" - ))] - ("4800", BaudRate::B4800), ("9600", BaudRate::B9600), - #[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" - ))] - ("14400", BaudRate::B14400), ("19200", BaudRate::B19200), - #[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" - ))] - ("28800", BaudRate::B28800), ("38400", BaudRate::B38400), ("57600", BaudRate::B57600), - #[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" - ))] - ("76800", BaudRate::B76800), ("115200", BaudRate::B115200), ("230400", BaudRate::B230400), - #[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" - ))] - ("460800", BaudRate::B460800), #[cfg(any(target_os = "android", target_os = "linux"))] ("500000", BaudRate::B500000), #[cfg(any(target_os = "android", target_os = "linux"))] ("576000", BaudRate::B576000), - #[cfg(any( - target_os = "android", - target_os = "freebsd", - target_os = "linux", - target_os = "netbsd" - ))] + #[cfg(any(target_os = "android", target_os = "linux",))] ("921600", BaudRate::B921600), #[cfg(any(target_os = "android", target_os = "linux"))] ("1000000", BaudRate::B1000000), diff --git a/src/uu/stty/src/stty.rs b/src/uu/stty/src/stty.rs index c53c01557c4..0e25852b29b 100644 --- a/src/uu/stty/src/stty.rs +++ b/src/uu/stty/src/stty.rs @@ -177,6 +177,28 @@ fn stty(opts: &Options) -> UResult<()> { fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> { let speed = cfgetospeed(termios); + + // BSDs use a u32 for the baud rate, so we can simply print it. + #[cfg(any( + target_os = "freebsd", + target_os = "dragonfly", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" + ))] + print!("speed {} baud; ", speed); + + // Other platforms need to use the baud rate enum, so printing the right value + // becomes slightly more complicated. + #[cfg(not(any( + target_os = "freebsd", + target_os = "dragonfly", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" + )))] for (text, baud_rate) in BAUD_RATES { if *baud_rate == speed { print!("speed {} baud; ", text); @@ -190,21 +212,25 @@ fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> { print!("rows {}; columns {}; ", size.rows, size.columns); } - // For some reason the normal nix Termios struct does not expose the line, - // so we get the underlying libc::termios struct to get that information. - let libc_termios: nix::libc::termios = termios.clone().into(); - let line = libc_termios.c_line; - print!("line = {};", line); + #[cfg(any(target_os = "linux", target_os = "redox"))] + { + // For some reason the normal nix Termios struct does not expose the line, + // so we get the underlying libc::termios struct to get that information. + let libc_termios: nix::libc::termios = termios.clone().into(); + let line = libc_termios.c_line; + print!("line = {};", line); + } + println!(); Ok(()) } fn print_settings(termios: &Termios, opts: &Options) -> nix::Result<()> { print_terminal_size(termios, opts)?; - print_flags(termios, opts, &CONTROL_FLAGS); - print_flags(termios, opts, &INPUT_FLAGS); - print_flags(termios, opts, &OUTPUT_FLAGS); - print_flags(termios, opts, &LOCAL_FLAGS); + print_flags(termios, opts, CONTROL_FLAGS); + print_flags(termios, opts, INPUT_FLAGS); + print_flags(termios, opts, OUTPUT_FLAGS); + print_flags(termios, opts, LOCAL_FLAGS); Ok(()) } @@ -249,10 +275,10 @@ fn apply_setting(termios: &mut Termios, s: &str) -> ControlFlow { Some(s) => (true, s), None => (false, s), }; - apply_flag(termios, &CONTROL_FLAGS, name, remove)?; - apply_flag(termios, &INPUT_FLAGS, name, remove)?; - apply_flag(termios, &OUTPUT_FLAGS, name, remove)?; - apply_flag(termios, &LOCAL_FLAGS, name, remove)?; + apply_flag(termios, CONTROL_FLAGS, name, remove)?; + apply_flag(termios, INPUT_FLAGS, name, remove)?; + apply_flag(termios, OUTPUT_FLAGS, name, remove)?; + apply_flag(termios, LOCAL_FLAGS, name, remove)?; ControlFlow::Break(false) }