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.

Code comments from sylvestre.

Co-authored-by: Sylvestre Ledru <sledru@mozilla.com>
  • Loading branch information
dezgeg and sylvestre committed Jul 13, 2023
1 parent 0ee9298 commit 2787fe4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
44 changes: 43 additions & 1 deletion src/uu/stty/src/flags.rs
Expand Up @@ -7,6 +7,8 @@
// 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
// spell-checker:ignore sigquit sigtstp

use crate::Flag;

Expand All @@ -19,7 +21,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 +318,40 @@ pub const BAUD_RATES: &[(&str, BaudRate)] = &[
))]
("4000000", BaudRate::B4000000),
];
/// Control characters for the stty command.
///
/// This constant provides a mapping between the names of control characters
/// and their corresponding values in the `S` enum.
pub const CONTROL_CHARS: &[(&str, S)] = &[
// Sends an interrupt signal (SIGINT).
("intr", S::VINTR),
// Sends a quit signal (SIGQUIT).
("quit", S::VQUIT),
// Deletes the last typed character.
("erase", S::VERASE),
// Deletes the current line.
("kill", S::VKILL),
// Signals the end of input.
("eof", S::VEOF),
// Signals the end of line.
("eol", S::VEOL),
// Alternate end-of-line character.
("eol2", S::VEOL2),
// Switch character (only on Linux).
#[cfg(target_os = "linux")]
("swtch", S::VSWTC),
// Starts output after it has been stopped.
("start", S::VSTART),
// Stops output.
("stop", S::VSTOP),
// Sends a suspend signal (SIGTSTP).
("susp", S::VSUSP),
// Reprints the current line.
("rprnt", S::VREPRINT),
// Deletes the last word typed.
("werase", S::VWERASE),
// Enters literal mode (next character is taken literally).
("lnext", S::VLNEXT),
// Discards the current line.
("discard", S::VDISCARD),
];
53 changes: 50 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,52 @@ fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
Ok(())
}

fn control_char_to_string(cc: nix::libc::cc_t) -> nix::Result<String> {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L248 was not covered by tests
if cc == 0 {
return Ok("<undef>".to_string());

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L250

Added line #L250 was not covered by tests
}

let (meta_prefix, code) = if cc >= 0x80 {
("M-", cc - 0x80)

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#L254

Added line #L254 was not covered by tests
} else {
("", cc)

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L256

Added line #L256 was not covered by tests
};

// Determine the '^'-prefix if applicable and character based on the code
let (ctrl_prefix, character) = match code {
// Control characters in ASCII range
0..=0x1f => Ok(("^", (b'@' + code) as char)),
// Printable ASCII characters
0x20..=0x7e => Ok(("", code as char)),
// DEL character
0x7f => Ok(("^", '?')),

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L266

Added line #L266 was not covered by tests
// Out of range (above 8 bits)
_ => Err(nix::errno::Errno::ERANGE),
}?;

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#L268-L269

Added lines #L268 - L269 were not covered by tests

Ok(format!("{meta_prefix}{ctrl_prefix}{character}"))
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L271 - L272 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L274

Added line #L274 was not covered by tests
if !opts.all {
// TODO: this branch should print values that differ from defaults
return Ok(());

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L277

Added line #L277 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L286

Added line #L286 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 292 in src/uu/stty/src/stty.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/stty/src/stty.rs#L291-L292

Added lines #L291 - L292 were not covered by tests

fn print_in_save_format(termios: &Termios) {
print!(
"{:x}:{:x}:{:x}:{:x}",
Expand All @@ -264,6 +310,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 2787fe4

Please sign in to comment.