Skip to content

Commit 8a6e390

Browse files
stty: setting control char code review fixes
1 parent edcad82 commit 8a6e390

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/uu/stty/src/stty.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
mod flags;
99

1010
use clap::{Arg, ArgAction, ArgMatches, Command};
11-
use nix::libc::{O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ, c_ushort};
11+
use nix::libc::{c_ushort, O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ};
1212
use nix::sys::termios::{
13-
ControlFlags, InputFlags, LocalFlags, OutputFlags, SpecialCharacterIndices, Termios,
14-
cfgetospeed, cfsetospeed, tcgetattr, tcsetattr,
13+
cfgetospeed, cfsetospeed, tcgetattr, tcsetattr, ControlFlags, InputFlags, LocalFlags,
14+
OutputFlags, SpecialCharacterIndices, Termios,
1515
};
1616
use nix::{ioctl_read_bad, ioctl_write_ptr_bad};
1717
use std::fs::File;
18-
use std::io::{self, Stdout, stdout};
18+
use std::io::{self, stdout, Stdout};
1919
use std::ops::ControlFlow;
2020
use std::os::fd::{AsFd, BorrowedFd};
2121
use std::os::unix::fs::OpenOptionsExt;
@@ -211,32 +211,24 @@ fn stty(opts: &Options) -> UResult<()> {
211211
if let Some(settings) = &opts.settings {
212212
let mut settings_iter = settings.iter();
213213
while let Some(setting) = settings_iter.next() {
214-
if let Some(char_index) = is_control_char(setting) {
214+
if let Some(char_index) = cc_to_index(setting) {
215215
let Some(new_cc) = settings_iter.next() else {
216216
return Err(USimpleError::new(
217217
1,
218218
format!("missing argument to '{setting}'"),
219219
));
220220
};
221-
match apply_char_mapping(&mut termios, char_index, new_cc) {
222-
Ok(_) => {}
223-
Err(e) => match e {
224-
ControlCharMappingError::IntOutOfRange => {
225-
return Err(USimpleError::new(
226-
1,
227-
format!(
228-
"invalid integer argument: '{new_cc}': Numerical result out of range"
229-
),
230-
));
231-
}
221+
apply_char_mapping(&mut termios, char_index, new_cc).map_err(|e| {
222+
let message = match e {
223+
ControlCharMappingError::IntOutOfRange => format!(
224+
"invalid integer argument: '{new_cc}': Numerical result out of range"
225+
),
232226
ControlCharMappingError::MultipleChars => {
233-
return Err(USimpleError::new(
234-
1,
235-
format!("invalid integer argument: '{new_cc}'"),
236-
));
227+
format!("invalid integer argument: '{new_cc}'")
237228
}
238-
},
239-
}
229+
};
230+
USimpleError::new(1, message)
231+
})?;
240232
} else if let ControlFlow::Break(false) = apply_setting(&mut termios, setting) {
241233
return Err(USimpleError::new(
242234
1,
@@ -307,7 +299,7 @@ fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
307299
Ok(())
308300
}
309301

310-
fn is_control_char(option: &str) -> Option<SpecialCharacterIndices> {
302+
fn cc_to_index(option: &str) -> Option<SpecialCharacterIndices> {
311303
for cc in CONTROL_CHARS {
312304
if option == cc.0 {
313305
return Some(cc.1);

0 commit comments

Comments
 (0)