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 uutils#3861.
  • Loading branch information
dezgeg committed Jul 9, 2023
1 parent 5cbfd2f commit 11e1db8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/uu/stty/src/flags.rs
Original file line number Diff line number Diff line change
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),
];
48 changes: 45 additions & 3 deletions src/uu/stty/src/stty.rs
Original file line number Diff line number Diff line change
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,47 @@ fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
Ok(())
}

fn control_char_to_string(cc: nix::libc::cc_t) -> nix::Result<String> {
if cc == 0 {
return Ok("<undef>".to_string());
}

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

let (ctrl_prefix, character) = match code {
0..=0x1f => Ok(("^", (b'@' + code) as char)),
0x20..=0x7e => Ok(("", code as char)),
0x7f => Ok(("^", '?')),
_ => Err(nix::errno::Errno::ERANGE),
}?;

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

fn print_control_chars(termios: &Termios, opts: &Options) -> nix::Result<()> {
if !opts.all {
// TODO: this branch should print values that differ from defaults
return Ok(());
}

for (text, cc_index) in CONTROL_CHARS {
print!(
"{text} = {}; ",
control_char_to_string(termios.control_chars[*cc_index as usize])?
);
}
println!(
"min = {}; time = {};",
termios.control_chars[SpecialCharacterIndices::VMIN as usize],
termios.control_chars[SpecialCharacterIndices::VTIME as usize]
);
Ok(())
}

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

Please # to comment.