Skip to content

Commit 3fc98a9

Browse files
stty: can now set control chars. need to improve checks on valid mappings
1 parent 9244eb5 commit 3fc98a9

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/uu/stty/src/stty.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use flags::{CONTROL_CHARS, CONTROL_FLAGS, INPUT_FLAGS, LOCAL_FLAGS, OUTPUT_FLAGS
3636

3737
const USAGE: &str = help_usage!("stty.md");
3838
const SUMMARY: &str = help_about!("stty.md");
39+
const ASCII_DEL: u8 = 127;
3940

4041
#[derive(Clone, Copy, Debug)]
4142
pub struct Flag<T> {
@@ -209,7 +210,7 @@ fn stty(opts: &Options) -> UResult<()> {
209210
let Some(new_cc) = settings_iter.next() else {
210211
return Err(USimpleError::new(
211212
1,
212-
format!("no mapping specified for '{setting}'"),
213+
format!("missing argument to '{setting}'"),
213214
));
214215
};
215216
if let ControlFlow::Break(false) =
@@ -490,9 +491,9 @@ fn apply_baud_rate_flag(termios: &mut Termios, input: &str) -> ControlFlow<bool>
490491
fn apply_char_mapping(
491492
termios: &mut Termios,
492493
control_char_index: SpecialCharacterIndices,
493-
new_cc: &str,
494+
new_val: &str,
494495
) -> ControlFlow<bool> {
495-
if let Some(val) = string_to_control_char(new_cc) {
496+
if let Some(val) = string_to_control_char(new_val) {
496497
termios.control_chars[control_char_index as usize] = val;
497498
return ControlFlow::Break(true);
498499
}
@@ -507,9 +508,8 @@ fn apply_char_mapping(
507508
// c. decimal, no prefix
508509
// 3. Disabling the control character: '^-' or 'undef'
509510
//
510-
// This function returns the ascii value of the given char, or None if the input cannot be parsed
511+
// This function returns the ascii value of valid control chars, or None if the input is invalid
511512
fn string_to_control_char(s: &str) -> Option<u8> {
512-
// try to parse int, then char
513513
if s == "undef" || s == "^-" {
514514
return Some(0);
515515
}
@@ -526,16 +526,15 @@ fn string_to_control_char(s: &str) -> Option<u8> {
526526
// try to parse ^<char> or just <char>
527527
let mut chars = s.chars();
528528
match (chars.next(), chars.next()) {
529-
(Some('^'), Some(c)) if c.is_ascii_alphabetic() => {
530-
// subract by '@' to turn the char into the ascii value of '^<char>'
529+
(Some('^'), Some(c)) => {
530+
// special case: ascii value of '^?' is greater than '?'
531531
if c == '?' {
532-
println!("{}", (c.to_ascii_uppercase() as u8).wrapping_sub(b'@'));
532+
return Some(ASCII_DEL);
533533
}
534+
// subract by '@' to turn the char into the ascii value of '^<char>'
534535
Some((c.to_ascii_uppercase() as u8).wrapping_sub(b'@'))
535536
}
536-
(Some(c), None) => {
537-
Some(c as u8)
538-
}
537+
(Some(c), _) => Some(c as u8),
539538
_ => None,
540539
}
541540
}

0 commit comments

Comments
 (0)