Skip to content

Commit

Permalink
stty: Implement printing assignment of control chars
Browse files Browse the repository at this point in the history
Part of #3861
  • Loading branch information
dezgeg committed Jul 9, 2023
1 parent d21f1b3 commit ab2ec87
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/uu/stty/src/flags.rs
Expand Up @@ -7,6 +7,7 @@
// spell-checker:ignore ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixoff ixon iuclc ixany imaxbel iutf
// spell-checker:ignore opost olcuc ocrnl onlcr onocr onlret ofill ofdel nldly crdly tabdly bsdly vtdly ffdly
// spell-checker:ignore isig icanon iexten echoe crterase echok echonl noflsh xcase tostop echoprt prterase echoctl ctlecho echoke crtkill flusho extproc
// spell-checker:ignore lnext rprnt susp swtch vdiscard veof veol verase vintr vkill vlnext vquit vreprint vstart vstop vsusp vswtc vwerase werase

use crate::Flag;

Expand All @@ -19,7 +20,10 @@ use crate::Flag;
target_os = "openbsd"
)))]
use nix::sys::termios::BaudRate;
use nix::sys::termios::{ControlFlags as C, InputFlags as I, LocalFlags as L, OutputFlags as O};
use nix::sys::termios::{
ControlFlags as C, InputFlags as I, LocalFlags as L, OutputFlags as O,
SpecialCharacterIndices as S,
};

pub const CONTROL_FLAGS: &[Flag<C>] = &[
Flag::new("parenb", C::PARENB),
Expand Down Expand Up @@ -313,3 +317,22 @@ pub const BAUD_RATES: &[(&str, BaudRate)] = &[
))]
("4000000", BaudRate::B4000000),
];

pub const CONTROL_CHARS: &[(&str, S)] = &[
("intr", S::VINTR),
("quit", S::VQUIT),
("erase", S::VERASE),
("kill", S::VKILL),
("eof", S::VEOF),
("eol", S::VEOL),
("eol2", S::VEOL2),
#[cfg(target_os = "linux")]
("swtch", S::VSWTC),
("start", S::VSTART),
("stop", S::VSTOP),
("susp", S::VSUSP),
("rprnt", S::VREPRINT),
("werase", S::VWERASE),
("lnext", S::VLNEXT),
("discard", S::VDISCARD),
];
43 changes: 40 additions & 3 deletions src/uu/stty/src/stty.rs
Expand Up @@ -3,15 +3,15 @@
// * For the full copyright and license information, please view the LICENSE file
// * that was distributed with this source code.

// spell-checker:ignore clocal tcgetattr tcsetattr tcsanow tiocgwinsz tiocswinsz cfgetospeed cfsetospeed ushort
// spell-checker:ignore clocal erange tcgetattr tcsetattr tcsanow tiocgwinsz tiocswinsz cfgetospeed cfsetospeed ushort vmin vtime

mod flags;

use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
use nix::libc::{c_ushort, O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ};
use nix::sys::termios::{
cfgetospeed, cfsetospeed, tcgetattr, tcsetattr, ControlFlags, InputFlags, LocalFlags,
OutputFlags, Termios,
OutputFlags, SpecialCharacterIndices, Termios,
};
use nix::{ioctl_read_bad, ioctl_write_ptr_bad};
use std::io::{self, stdout};
Expand All @@ -30,7 +30,7 @@ use uucore::{format_usage, help_about, help_usage};
target_os = "openbsd"
)))]
use flags::BAUD_RATES;
use flags::{CONTROL_FLAGS, INPUT_FLAGS, LOCAL_FLAGS, OUTPUT_FLAGS};
use flags::{CONTROL_CHARS, CONTROL_FLAGS, INPUT_FLAGS, LOCAL_FLAGS, OUTPUT_FLAGS};

const USAGE: &str = help_usage!("stty.md");
const SUMMARY: &str = help_about!("stty.md");
Expand Down Expand Up @@ -245,6 +245,42 @@ fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
Ok(())
}

fn control_char_to_string(mut cc: nix::libc::cc_t) -> nix::Result<String> {
let mut prefix = "";

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L248-L249

Added lines #L248 - L249 were not covered by tests
if cc == 0 {
return Ok("<undef>".to_string());

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L251

Added line #L251 was not covered by tests
} else if cc >= 0x80 {
cc &= !0x80;
prefix = "M-";

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L253-L254

Added lines #L253 - L254 were not covered by tests
}

match cc {
0..=0x1f => Ok(format!("{}^{}", prefix, (b'@' as u8 + cc) as char)),

Check failure on line 258 in src/uu/stty/src/stty.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (macos-12, unix)

ERROR: `cargo clippy`: casting to the same type is unnecessary (`u8` -> `u8`) (file:'src/uu/stty/src/stty.rs', line:258)
0x20..=0x7e => Ok(format!("{}{}", prefix, cc as char)),
0x7f => Ok(format!("{}^?", prefix)),
_ => Err(nix::errno::Errno::ERANGE),

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L260-L261

Added lines #L260 - L261 were not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L263

Added line #L263 was not covered by tests

fn print_control_chars(termios: &Termios, opts: &Options) -> nix::Result<()> {

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L265

Added line #L265 was not covered by tests
if !opts.all {
return Ok(());

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L267

Added line #L267 was not covered by tests
}

for (text, cc_index) in CONTROL_CHARS {
print!(

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L271

Added line #L271 was not covered by tests
"{text} = {}; ",
control_char_to_string(termios.control_chars[*cc_index as usize])?
);
}
println!(

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L276

Added line #L276 was not covered by tests
"min = {}; time = {};",
termios.control_chars[SpecialCharacterIndices::VMIN as usize],
termios.control_chars[SpecialCharacterIndices::VTIME as usize]
);
Ok(())
}

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L281-L282

Added lines #L281 - L282 were not covered by tests

fn print_in_save_format(termios: &Termios) {
print!(
"{:x}:{:x}:{:x}:{:x}",
Expand All @@ -264,6 +300,7 @@ fn print_settings(termios: &Termios, opts: &Options) -> nix::Result<()> {
print_in_save_format(termios);
} else {
print_terminal_size(termios, opts)?;
print_control_chars(termios, opts)?;
print_flags(termios, opts, CONTROL_FLAGS);
print_flags(termios, opts, INPUT_FLAGS);
print_flags(termios, opts, OUTPUT_FLAGS);
Expand Down

0 comments on commit ab2ec87

Please sign in to comment.