diff --git a/src/uu/stty/src/stty.rs b/src/uu/stty/src/stty.rs index c58408732..e6ed38d2a 100644 --- a/src/uu/stty/src/stty.rs +++ b/src/uu/stty/src/stty.rs @@ -3,7 +3,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore clocal erange tcgetattr tcsetattr tcsanow tiocgwinsz tiocswinsz cfgetospeed cfsetospeed ushort vmin vtime cflag lflag +// spell-checker:ignore clocal erange tcgetattr tcsetattr tcsanow tiocgwinsz tiocswinsz cfgetospeed cfsetospeed ushort vmin vtime cflag lflag ispeed ospeed mod flags; @@ -250,6 +250,23 @@ fn stty(opts: &Options) -> UResult<()> { } else { return Err(USimpleError::new(1, format!("missing argument to '{arg}'"))); } + // ispeed/ospeed baud rate setting + } else if *arg == "ispeed" || *arg == "ospeed" { + match args_iter.next() { + Some(speed) => { + if let Some(baud_flag) = string_to_baud(speed) { + valid_args.push(ArgOptions::Flags(baud_flag)); + } else { + return Err(USimpleError::new(1, format!("invalid {arg} '{speed}'"))); + } + } + None => { + return Err(USimpleError::new(1, format!("missing argument to '{arg}'"))); + } + } + // baud rate setting + } else if let Some(baud_flag) = string_to_baud(arg) { + valid_args.push(ArgOptions::Flags(baud_flag)); // non control char flag } else if let Some(flag) = string_to_flag(arg) { let remove_group = match flag { @@ -356,8 +373,7 @@ fn cc_to_index(option: &str) -> Option { None } -// return Some(flag) if the input is a valid flag, None if not -fn string_to_flag(option: &str) -> Option { +fn string_to_baud(arg: &str) -> Option { // BSDs use a u32 for the baud rate, so any decimal number applies. #[cfg(any( target_os = "freebsd", @@ -367,7 +383,7 @@ fn string_to_flag(option: &str) -> Option { target_os = "netbsd", target_os = "openbsd" ))] - if let Ok(n) = option.parse::() { + if let Ok(n) = arg.parse::() { return Some(AllFlags::Baud(n)); } @@ -380,11 +396,15 @@ fn string_to_flag(option: &str) -> Option { target_os = "openbsd" )))] for (text, baud_rate) in BAUD_RATES { - if *text == option { + if *text == arg { return Some(AllFlags::Baud(*baud_rate)); } } + None +} +// return Some(flag) if the input is a valid flag, None if not +fn string_to_flag(option: &str) -> Option { let remove = option.starts_with('-'); let name = option.trim_start_matches('-'); diff --git a/tests/by-util/test_stty.rs b/tests/by-util/test_stty.rs index 00a6a803a..997336281 100644 --- a/tests/by-util/test_stty.rs +++ b/tests/by-util/test_stty.rs @@ -2,7 +2,7 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore parenb parmrk ixany iuclc onlcr ofdel icanon noflsh econl igpar +// spell-checker:ignore parenb parmrk ixany iuclc onlcr ofdel icanon noflsh econl igpar ispeed ospeed use uutests::new_ucmd; use uutests::util::TestScenario; @@ -110,3 +110,60 @@ fn invalid_setting() { .fails() .stderr_contains("invalid argument 'igpar'"); } + +#[test] +fn invalid_baud_setting() { + #[cfg(not(any( + target_os = "freebsd", + target_os = "dragonfly", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" + )))] + new_ucmd!() + .args(&["100"]) + .fails() + .stderr_contains("invalid argument '100'"); + + new_ucmd!() + .args(&["-1"]) + .fails() + .stderr_contains("invalid argument '-1'"); + + new_ucmd!() + .args(&["ispeed"]) + .fails() + .stderr_contains("missing argument to 'ispeed'"); + + new_ucmd!() + .args(&["ospeed"]) + .fails() + .stderr_contains("missing argument to 'ospeed'"); + + #[cfg(not(any( + target_os = "freebsd", + target_os = "dragonfly", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" + )))] + new_ucmd!() + .args(&["ispeed", "995"]) + .fails() + .stderr_contains("invalid ispeed '995'"); + + #[cfg(not(any( + target_os = "freebsd", + target_os = "dragonfly", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd" + )))] + new_ucmd!() + .args(&["ospeed", "995"]) + .fails() + .stderr_contains("invalid ospeed '995'"); +}