1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

stty: add grouped flags

This commit is contained in:
Terts Diepraam 2022-06-27 13:00:31 +02:00 committed by Sylvestre Ledru
parent a23702caeb
commit 4cda8f33e7
2 changed files with 218 additions and 399 deletions

View file

@ -3,330 +3,95 @@
// * For the full copyright and license information, please view the LICENSE file // * For the full copyright and license information, please view the LICENSE file
// * that was distributed with this source code. // * that was distributed with this source code.
// spell-checker:ignore parenb parodd cmspar hupcl cstopb cread clocal crtscts // spell-checker:ignore parenb parodd cmspar hupcl cstopb cread clocal crtscts CSIZE
// spell-checker:ignore ignbrk brkint ignpar parmrk inpck istrip inlcr igncr icrnl ixoff ixon iuclc ixany imaxbel iutf // 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 // 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 isig icanon iexten echoe crterase echok echonl noflsh xcase tostop echoprt prterase echoctl ctlecho echoke crtkill flusho extproc
use crate::Flag; use crate::Flag;
use nix::sys::termios::{ControlFlags, InputFlags, LocalFlags, OutputFlags}; use nix::sys::termios::{ControlFlags as C, InputFlags as I, LocalFlags as L, OutputFlags as O};
pub const CONTROL_FLAGS: [Flag<ControlFlags>; 8] = [ pub const CONTROL_FLAGS: [Flag<C>; 12] = [
Flag { Flag::new("parenb", C::PARENB),
name: "parenb", Flag::new("parodd", C::PARODD),
flag: ControlFlags::PARENB, Flag::new("cmspar", C::CMSPAR),
show: true, Flag::new("cs5", C::CS5).group(C::CSIZE),
sane: false, Flag::new("cs6", C::CS6).group(C::CSIZE),
}, Flag::new("cs7", C::CS7).group(C::CSIZE),
Flag { Flag::new("cs8", C::CS8).group(C::CSIZE).sane(),
name: "parodd", Flag::new("hupcl", C::HUPCL).sane(),
flag: ControlFlags::PARODD, Flag::new("cstopb", C::CSTOPB),
show: true, Flag::new("cread", C::CREAD).sane(),
sane: false, Flag::new("clocal", C::CLOCAL),
}, Flag::new("crtscts", C::CRTSCTS),
Flag {
name: "cmspar",
flag: ControlFlags::CMSPAR,
show: true,
sane: false,
},
Flag {
name: "hupcl",
flag: ControlFlags::HUPCL,
show: true,
sane: true,
},
Flag {
name: "cstopb",
flag: ControlFlags::CSTOPB,
show: true,
sane: false,
},
Flag {
name: "cread",
flag: ControlFlags::CREAD,
show: true,
sane: true,
},
Flag {
name: "clocal",
flag: ControlFlags::CLOCAL,
show: true,
sane: false,
},
Flag {
name: "crtscts",
flag: ControlFlags::CRTSCTS,
show: true,
sane: false,
},
]; ];
pub const INPUT_FLAGS: [Flag<InputFlags>; 15] = [ pub const INPUT_FLAGS: [Flag<I>; 15] = [
Flag { Flag::new("ignbrk", I::IGNBRK),
name: "ignbrk", Flag::new("brkint", I::BRKINT).sane(),
flag: InputFlags::IGNBRK, Flag::new("ignpar", I::IGNPAR),
show: true, Flag::new("parmrk", I::PARMRK),
sane: false, Flag::new("inpck", I::INPCK),
}, Flag::new("istrip", I::ISTRIP),
Flag { Flag::new("inlcr", I::INLCR),
name: "brkint", Flag::new("igncr", I::IGNCR),
flag: InputFlags::BRKINT, Flag::new("icrnl", I::ICRNL).sane(),
show: true, Flag::new("ixoff", I::IXOFF),
sane: true, Flag::new("tandem", I::IXOFF),
}, Flag::new("ixon", I::IXON),
Flag {
name: "ignpar",
flag: InputFlags::IGNPAR,
show: true,
sane: false,
},
Flag {
name: "parmrk",
flag: InputFlags::PARMRK,
show: true,
sane: false,
},
Flag {
name: "inpck",
flag: InputFlags::INPCK,
show: true,
sane: false,
},
Flag {
name: "istrip",
flag: InputFlags::ISTRIP,
show: true,
sane: false,
},
Flag {
name: "inlcr",
flag: InputFlags::INLCR,
show: true,
sane: false,
},
Flag {
name: "igncr",
flag: InputFlags::IGNCR,
show: true,
sane: false,
},
Flag {
name: "icrnl",
flag: InputFlags::ICRNL,
show: true,
sane: true,
},
Flag {
name: "ixoff",
flag: InputFlags::IXOFF,
show: true,
sane: false,
},
Flag {
name: "tandem",
flag: InputFlags::IXOFF,
show: false,
sane: false,
},
Flag {
name: "ixon",
flag: InputFlags::IXON,
show: true,
sane: false,
},
// not supported by nix // not supported by nix
// Flag { // Flag::new("iuclc", I::IUCLC),
// name: "iuclc", Flag::new("ixany", I::IXANY).sane(),
// flag: InputFlags::IUCLC, Flag::new("imaxbel", I::IMAXBEL).sane(),
// show: true, Flag::new("iutf8", I::IUTF8).sane(),
// default: false,
// },
Flag {
name: "ixany",
flag: InputFlags::IXANY,
show: true,
sane: false,
},
Flag {
name: "imaxbel",
flag: InputFlags::IMAXBEL,
show: true,
sane: true,
},
Flag {
name: "iutf8",
flag: InputFlags::IUTF8,
show: true,
sane: false,
},
]; ];
pub const OUTPUT_FLAGS: [Flag<OutputFlags>; 8] = [ pub const OUTPUT_FLAGS: [Flag<O>; 24] = [
Flag { Flag::new("opost", O::OPOST).sane(),
name: "opost", Flag::new("olcuc", O::OLCUC),
flag: OutputFlags::OPOST, Flag::new("ocrnl", O::OCRNL),
show: true, Flag::new("onlcr", O::ONLCR).sane(),
sane: true, Flag::new("onocr", O::ONOCR),
}, Flag::new("onlret", O::ONLRET),
Flag { Flag::new("ofill", O::OFILL),
name: "olcuc", Flag::new("ofdel", O::OFDEL),
flag: OutputFlags::OLCUC, Flag::new("nl0", O::NL0).group(O::NLDLY).sane(),
show: true, Flag::new("nl1", O::NL1).group(O::NLDLY),
sane: false, Flag::new("cr0", O::CR0).group(O::CRDLY).sane(),
}, Flag::new("cr1", O::CR1).group(O::CRDLY),
Flag { Flag::new("cr2", O::CR2).group(O::CRDLY),
name: "ocrnl", Flag::new("cr3", O::CR3).group(O::CRDLY),
flag: OutputFlags::OCRNL, Flag::new("tab0", O::TAB0).group(O::TABDLY).sane(),
show: true, Flag::new("tab1", O::TAB1).group(O::TABDLY),
sane: false, Flag::new("tab2", O::TAB2).group(O::TABDLY),
}, Flag::new("tab3", O::TAB3).group(O::TABDLY),
Flag { Flag::new("bs0", O::BS0).group(O::BSDLY).sane(),
name: "onlcr", Flag::new("bs1", O::BS1).group(O::BSDLY),
flag: OutputFlags::ONLCR, Flag::new("vt0", O::VT0).group(O::VTDLY).sane(),
show: true, Flag::new("vt1", O::VT1).group(O::VTDLY),
sane: true, Flag::new("ff0", O::FF0).group(O::FFDLY).sane(),
}, Flag::new("ff1", O::FF1).group(O::FFDLY),
Flag {
name: "onocr",
flag: OutputFlags::ONOCR,
show: true,
sane: false,
},
Flag {
name: "onlret",
flag: OutputFlags::ONLRET,
show: true,
sane: false,
},
Flag {
name: "ofill",
flag: OutputFlags::OFILL,
show: true,
sane: false,
},
Flag {
name: "ofdel",
flag: OutputFlags::OFDEL,
show: true,
sane: false,
},
]; ];
pub const LOCAL_FLAGS: [Flag<LocalFlags>; 18] = [ pub const LOCAL_FLAGS: [Flag<L>; 18] = [
Flag { Flag::new("isig", L::ISIG).sane(),
name: "isig", Flag::new("icanon", L::ICANON).sane(),
flag: LocalFlags::ISIG, Flag::new("iexten", L::IEXTEN).sane(),
show: true, Flag::new("echo", L::ECHO).sane(),
sane: true, Flag::new("echoe", L::ECHOE).sane(),
}, Flag::new("crterase", L::ECHOE).hidden().sane(),
Flag { Flag::new("echok", L::ECHOK).sane(),
name: "icanon", Flag::new("echonl", L::ECHONL),
flag: LocalFlags::ICANON, Flag::new("noflsh", L::NOFLSH),
show: true,
sane: true,
},
Flag {
name: "iexten",
flag: LocalFlags::IEXTEN,
show: true,
sane: true,
},
Flag {
name: "echo",
flag: LocalFlags::ECHO,
show: true,
sane: true,
},
Flag {
name: "echoe",
flag: LocalFlags::ECHOE,
show: true,
sane: true,
},
Flag {
name: "crterase",
flag: LocalFlags::ECHOE,
show: false,
sane: true,
},
Flag {
name: "echok",
flag: LocalFlags::ECHOK,
show: true,
sane: true,
},
Flag {
name: "echonl",
flag: LocalFlags::ECHONL,
show: true,
sane: false,
},
Flag {
name: "noflsh",
flag: LocalFlags::NOFLSH,
show: true,
sane: false,
},
// Not supported by nix // Not supported by nix
// Flag { // Flag::new("xcase", L::XCASE),
// name: "xcase", Flag::new("tostop", L::TOSTOP),
// flag: LocalFlags::XCASE, Flag::new("echoprt", L::ECHOPRT),
// show: true, Flag::new("prterase", L::ECHOPRT).hidden(),
// sane: false, Flag::new("echoctl", L::ECHOCTL).sane(),
// }, Flag::new("ctlecho", L::ECHOCTL).sane().hidden(),
Flag { Flag::new("echoke", L::ECHOKE).sane(),
name: "tostop", Flag::new("crtkill", L::ECHOKE).sane().hidden(),
flag: LocalFlags::TOSTOP, Flag::new("flusho", L::FLUSHO),
show: true, Flag::new("extproc", L::EXTPROC),
sane: false,
},
Flag {
name: "echoprt",
flag: LocalFlags::ECHOPRT,
show: true,
sane: false,
},
Flag {
name: "prterase",
flag: LocalFlags::ECHOPRT,
show: false,
sane: false,
},
Flag {
name: "echoctl",
flag: LocalFlags::ECHOCTL,
show: true,
sane: true,
},
Flag {
name: "ctlecho",
flag: LocalFlags::ECHOCTL,
show: false,
sane: true,
},
Flag {
name: "echoke",
flag: LocalFlags::ECHOKE,
show: true,
sane: true,
},
Flag {
name: "crtkill",
flag: LocalFlags::ECHOKE,
show: false,
sane: true,
},
Flag {
name: "flusho",
flag: LocalFlags::FLUSHO,
show: true,
sane: false,
},
Flag {
name: "extproc",
flag: LocalFlags::EXTPROC,
show: true,
sane: false,
},
]; ];

View file

@ -12,8 +12,9 @@ use nix::sys::termios::{
tcgetattr, tcsetattr, ControlFlags, InputFlags, LocalFlags, OutputFlags, Termios, tcgetattr, tcsetattr, ControlFlags, InputFlags, LocalFlags, OutputFlags, Termios,
}; };
use std::io::{self, stdout}; use std::io::{self, stdout};
use std::ops::ControlFlow;
use std::os::unix::io::{AsRawFd, RawFd}; use std::os::unix::io::{AsRawFd, RawFd};
use uucore::error::UResult; use uucore::error::{UResult, USimpleError};
use uucore::{format_usage, InvalidEncodingHandling}; use uucore::{format_usage, InvalidEncodingHandling};
use flags::{CONTROL_FLAGS, INPUT_FLAGS, LOCAL_FLAGS, OUTPUT_FLAGS}; use flags::{CONTROL_FLAGS, INPUT_FLAGS, LOCAL_FLAGS, OUTPUT_FLAGS};
@ -25,15 +26,53 @@ const USAGE: &str = "\
{} [-F DEVICE | --file=DEVICE] [-g|--save]"; {} [-F DEVICE | --file=DEVICE] [-g|--save]";
const SUMMARY: &str = "Print or change terminal characteristics."; const SUMMARY: &str = "Print or change terminal characteristics.";
#[derive(Clone, Copy, Debug)]
pub struct Flag<T> { pub struct Flag<T> {
name: &'static str, name: &'static str,
flag: T, flag: T,
show: bool, show: bool,
sane: bool, sane: bool,
group: Option<T>,
} }
trait TermiosFlag { impl<T> Flag<T>
fn is_in(&self, termios: &Termios) -> bool; where
T: Copy,
{
pub const fn new(name: &'static str, flag: T) -> Self {
Flag {
name,
flag,
show: true,
sane: false,
group: None,
}
}
pub const fn hidden(&self) -> Self {
Self {
show: false,
..*self
}
}
pub const fn sane(&self) -> Self {
Self {
sane: true,
..*self
}
}
pub const fn group(&self, group: T) -> Self {
Self {
group: Some(group),
..*self
}
}
}
trait TermiosFlag: Copy {
fn is_in(&self, termios: &Termios, group: Option<Self>) -> bool;
fn apply(&self, termios: &mut Termios, val: bool); fn apply(&self, termios: &mut Termios, val: bool);
} }
@ -83,7 +122,12 @@ fn stty(opts: &Options) -> UResult<()> {
let mut termios = tcgetattr(opts.file).expect("Could not get terminal attributes"); let mut termios = tcgetattr(opts.file).expect("Could not get terminal attributes");
if let Some(settings) = &opts.settings { if let Some(settings) = &opts.settings {
for setting in settings { for setting in settings {
apply_setting(&mut termios, setting); if let ControlFlow::Break(false) = apply_setting(&mut termios, setting) {
return Err(USimpleError::new(
1,
format!("invalid argument '{}'", setting),
));
}
} }
tcsetattr(opts.file, nix::sys::termios::SetArg::TCSANOW, &termios) tcsetattr(opts.file, nix::sys::termios::SetArg::TCSANOW, &termios)
@ -95,72 +139,91 @@ fn stty(opts: &Options) -> UResult<()> {
} }
fn print_settings(termios: &Termios, opts: &Options) { fn print_settings(termios: &Termios, opts: &Options) {
if print_flags(termios, opts, &CONTROL_FLAGS) { print_flags(termios, opts, &CONTROL_FLAGS);
println!(); print_flags(termios, opts, &INPUT_FLAGS);
} print_flags(termios, opts, &OUTPUT_FLAGS);
if print_flags(termios, opts, &INPUT_FLAGS) { print_flags(termios, opts, &LOCAL_FLAGS);
println!();
}
if print_flags(termios, opts, &OUTPUT_FLAGS) {
println!();
}
if print_flags(termios, opts, &LOCAL_FLAGS) {
println!();
}
} }
fn print_flags<T>(termios: &Termios, opts: &Options, flags: &[Flag<T>]) -> bool fn print_flags<T: TermiosFlag>(termios: &Termios, opts: &Options, flags: &[Flag<T>]) {
where
Flag<T>: TermiosFlag,
{
let mut printed = false; let mut printed = false;
for flag in flags { for &Flag {
if !flag.show { name,
flag,
show,
sane,
group,
} in flags
{
if !show {
continue; continue;
} }
let val = flag.is_in(termios); let val = flag.is_in(termios, group);
if opts.all || val != flag.sane { if group.is_some() {
if !val { if val && (!sane || opts.all) {
print!("-"); print!("{name} ");
printed = true;
}
} else {
if opts.all || val != sane {
if !val {
print!("-");
}
print!("{name} ");
printed = true;
} }
print!("{} ", flag.name);
printed = true;
} }
} }
printed if printed {
println!();
}
} }
fn apply_setting(termios: &mut Termios, s: &str) -> Option<()> { /// Apply a single setting
if let Some(()) = apply_flag(termios, &CONTROL_FLAGS, s) { ///
return Some(()); /// The value inside the `Break` variant of the `ControlFlow` indicates whether
} /// the setting has been applied.
if let Some(()) = apply_flag(termios, &INPUT_FLAGS, s) { fn apply_setting(termios: &mut Termios, s: &str) -> ControlFlow<bool> {
return Some(()); let (remove, name) = match s.strip_prefix('-') {
}
if let Some(()) = apply_flag(termios, &OUTPUT_FLAGS, s) {
return Some(());
}
if let Some(()) = apply_flag(termios, &LOCAL_FLAGS, s) {
return Some(());
}
None
}
fn apply_flag<T>(termios: &mut Termios, flags: &[Flag<T>], name: &str) -> Option<()>
where
T: Copy,
Flag<T>: TermiosFlag,
{
let (remove, name) = strip_hyphen(name);
find(flags, name)?.apply(termios, !remove);
Some(())
}
fn strip_hyphen(s: &str) -> (bool, &str) {
match s.strip_prefix('-') {
Some(s) => (true, s), Some(s) => (true, s),
None => (false, s), None => (false, s),
};
apply_flag(termios, &CONTROL_FLAGS, name, remove)?;
apply_flag(termios, &INPUT_FLAGS, name, remove)?;
apply_flag(termios, &OUTPUT_FLAGS, name, remove)?;
apply_flag(termios, &LOCAL_FLAGS, name, remove)?;
ControlFlow::Break(false)
}
/// Apply a flag to a slice of flags
///
/// The value inside the `Break` variant of the `ControlFlow` indicates whether
/// the setting has been applied.
fn apply_flag<T: TermiosFlag>(
termios: &mut Termios,
flags: &[Flag<T>],
input: &str,
remove: bool,
) -> ControlFlow<bool> {
for Flag {
name, flag, group, ..
} in flags
{
if input == *name {
// Flags with groups cannot be removed
// Since the name matches, we can short circuit and don't have to check the other flags.
if remove || group.is_some() {
return ControlFlow::Break(false);
}
// If there is a group, the bits for that group should be cleared before applying the flag
if let Some(group) = group {
group.apply(termios, false);
}
flag.apply(termios, !remove);
return ControlFlow::Break(true);
}
} }
ControlFlow::Continue(())
} }
pub fn uu_app<'a>() -> Command<'a> { pub fn uu_app<'a>() -> Command<'a> {
@ -186,55 +249,46 @@ pub fn uu_app<'a>() -> Command<'a> {
) )
} }
impl TermiosFlag for Flag<ControlFlags> { impl TermiosFlag for ControlFlags {
fn is_in(&self, termios: &Termios) -> bool { fn is_in(&self, termios: &Termios, group: Option<Self>) -> bool {
termios.control_flags.contains(self.flag) termios.control_flags.contains(*self)
&& group.map_or(true, |g| !termios.control_flags.intersects(g - *self))
} }
fn apply(&self, termios: &mut Termios, val: bool) { fn apply(&self, termios: &mut Termios, val: bool) {
termios.control_flags.set(self.flag, val) termios.control_flags.set(*self, val)
} }
} }
impl TermiosFlag for Flag<InputFlags> { impl TermiosFlag for InputFlags {
fn is_in(&self, termios: &Termios) -> bool { fn is_in(&self, termios: &Termios, group: Option<Self>) -> bool {
termios.input_flags.contains(self.flag) termios.input_flags.contains(*self)
&& group.map_or(true, |g| !termios.input_flags.intersects(g - *self))
} }
fn apply(&self, termios: &mut Termios, val: bool) { fn apply(&self, termios: &mut Termios, val: bool) {
termios.input_flags.set(self.flag, val) termios.input_flags.set(*self, val)
} }
} }
impl TermiosFlag for Flag<OutputFlags> { impl TermiosFlag for OutputFlags {
fn is_in(&self, termios: &Termios) -> bool { fn is_in(&self, termios: &Termios, group: Option<Self>) -> bool {
termios.output_flags.contains(self.flag) termios.output_flags.contains(*self)
&& group.map_or(true, |g| !termios.output_flags.intersects(g - *self))
} }
fn apply(&self, termios: &mut Termios, val: bool) { fn apply(&self, termios: &mut Termios, val: bool) {
termios.output_flags.set(self.flag, val) termios.output_flags.set(*self, val)
} }
} }
impl TermiosFlag for Flag<LocalFlags> { impl TermiosFlag for LocalFlags {
fn is_in(&self, termios: &Termios) -> bool { fn is_in(&self, termios: &Termios, group: Option<Self>) -> bool {
termios.local_flags.contains(self.flag) termios.local_flags.contains(*self)
&& group.map_or(true, |g| !termios.local_flags.intersects(g - *self))
} }
fn apply(&self, termios: &mut Termios, val: bool) { fn apply(&self, termios: &mut Termios, val: bool) {
termios.local_flags.set(self.flag, val) termios.local_flags.set(*self, val)
} }
} }
fn find<'a, T>(flags: &'a [Flag<T>], flag_name: &str) -> Option<&'a Flag<T>>
where
T: Copy,
{
flags.iter().find_map(|flag| {
if flag.name == flag_name {
Some(flag)
} else {
None
}
})
}