Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/uu/stty/src/stty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -250,6 +250,23 @@
} 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 {

Check warning on line 259 in src/uu/stty/src/stty.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L258-L259

Added lines #L258 - L259 were not covered by tests
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));

Check warning on line 269 in src/uu/stty/src/stty.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L269

Added line #L269 was not covered by tests
// non control char flag
} else if let Some(flag) = string_to_flag(arg) {
let remove_group = match flag {
Expand Down Expand Up @@ -356,8 +373,7 @@
None
}

// return Some(flag) if the input is a valid flag, None if not
fn string_to_flag(option: &str) -> Option<AllFlags> {
fn string_to_baud(arg: &str) -> Option<AllFlags> {
// BSDs use a u32 for the baud rate, so any decimal number applies.
#[cfg(any(
target_os = "freebsd",
Expand All @@ -367,7 +383,7 @@
target_os = "netbsd",
target_os = "openbsd"
))]
if let Ok(n) = option.parse::<u32>() {
if let Ok(n) = arg.parse::<u32>() {
return Some(AllFlags::Baud(n));
}

Expand All @@ -380,11 +396,15 @@
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<AllFlags> {
let remove = option.starts_with('-');
let name = option.trim_start_matches('-');

Expand Down
59 changes: 58 additions & 1 deletion tests/by-util/test_stty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'");
}
Loading