From aa58eff8d667833e149c7d182261d6afe058b531 Mon Sep 17 00:00:00 2001 From: Will Shuttleworth Date: Wed, 18 Jun 2025 11:44:41 -0400 Subject: [PATCH] stty: add rows/cols settings --- src/uu/stty/src/stty.rs | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/uu/stty/src/stty.rs b/src/uu/stty/src/stty.rs index 7866ecaad..9ac8a8c9e 100644 --- a/src/uu/stty/src/stty.rs +++ b/src/uu/stty/src/stty.rs @@ -108,9 +108,15 @@ enum ControlCharMappingError { MultipleChars, } +enum SpecialSettings { + Rows(u16), + Cols(u16), +} + enum ArgOptions<'a> { Flags(AllFlags<'a>), Mapping((SpecialCharacterIndices, u8)), + Special(SpecialSettings), } impl<'a> From> for ArgOptions<'a> { @@ -280,6 +286,32 @@ fn stty(opts: &Options) -> UResult<()> { return Err(USimpleError::new(1, format!("invalid argument '{arg}'"))); } valid_args.push(flag.into()); + } else if *arg == "rows" { + if let Some(rows) = args_iter.next() { + if let Some(n) = parse_rows_cols(rows) { + valid_args.push(ArgOptions::Special(SpecialSettings::Rows(n))); + } else { + return Err(USimpleError::new( + 1, + format!("invalid integer argument: '{rows}'"), + )); + } + } else { + return Err(USimpleError::new(1, format!("missing argument to '{arg}'"))); + } + } else if *arg == "columns" || *arg == "cols" { + if let Some(cols) = args_iter.next() { + if let Some(n) = parse_rows_cols(cols) { + valid_args.push(ArgOptions::Special(SpecialSettings::Cols(n))); + } else { + return Err(USimpleError::new( + 1, + format!("invalid integer argument: '{cols}'"), + )); + } + } else { + return Err(USimpleError::new(1, format!("missing argument to '{arg}'"))); + } // not a valid control char or flag } else { return Err(USimpleError::new(1, format!("invalid argument '{arg}'"))); @@ -294,6 +326,9 @@ fn stty(opts: &Options) -> UResult<()> { match arg { ArgOptions::Mapping(mapping) => apply_char_mapping(&mut termios, mapping), ArgOptions::Flags(flag) => apply_setting(&mut termios, flag), + ArgOptions::Special(setting) => { + apply_special_setting(setting, opts.file.as_raw_fd())?; + } } } tcsetattr( @@ -310,6 +345,15 @@ fn stty(opts: &Options) -> UResult<()> { Ok(()) } +// GNU uses an unsigned 32 bit integer for row/col sizes, but then wraps around 16 bits +// this function returns Some(n), where n is a u16 row/col size, or None if the string arg cannot be parsed as a u32 +fn parse_rows_cols(arg: &str) -> Option { + if let Ok(n) = arg.parse::() { + return Some((n % (u16::MAX as u32 + 1)) as u16); + } + None +} + fn check_flag_group(flag: &Flag, remove: bool) -> bool { remove && flag.group.is_some() } @@ -588,6 +632,17 @@ fn apply_char_mapping(termios: &mut Termios, mapping: &(SpecialCharacterIndices, termios.control_chars[mapping.0 as usize] = mapping.1; } +fn apply_special_setting(setting: &SpecialSettings, fd: i32) -> nix::Result<()> { + let mut size = TermSize::default(); + unsafe { tiocgwinsz(fd, &raw mut size)? }; + match setting { + SpecialSetting::Rows(n) => size.rows = *n, + SpecialSetting::Cols(n) => size.columns = *n, + } + unsafe { tiocswinsz(fd, &raw mut size)? }; + Ok(()) +} + // GNU stty defines some valid values for the control character mappings // 1. Standard character, can be a a single char (ie 'C') or hat notation (ie '^C') // 2. Integer