mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
stty: fix bsd/mac builds
This commit is contained in:
parent
420c69aa98
commit
85e6f8659f
3 changed files with 85 additions and 68 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2913,7 +2913,7 @@ dependencies = [
|
||||||
name = "uu_stty"
|
name = "uu_stty"
|
||||||
version = "0.0.14"
|
version = "0.0.14"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap 3.1.18",
|
"clap 3.2.15",
|
||||||
"nix",
|
"nix",
|
||||||
"uucore",
|
"uucore",
|
||||||
]
|
]
|
||||||
|
|
|
@ -9,13 +9,25 @@
|
||||||
// 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::{
|
|
||||||
BaudRate, ControlFlags as C, InputFlags as I, LocalFlags as L, OutputFlags as O,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const CONTROL_FLAGS: [Flag<C>; 12] = [
|
#[cfg(not(any(
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "dragonfly",
|
||||||
|
target_os = "ios",
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "netbsd",
|
||||||
|
target_os = "openbsd"
|
||||||
|
)))]
|
||||||
|
use nix::sys::termios::BaudRate;
|
||||||
|
use nix::sys::termios::{ControlFlags as C, InputFlags as I, LocalFlags as L, OutputFlags as O};
|
||||||
|
|
||||||
|
pub const CONTROL_FLAGS: &[Flag<C>] = &[
|
||||||
Flag::new("parenb", C::PARENB),
|
Flag::new("parenb", C::PARENB),
|
||||||
Flag::new("parodd", C::PARODD),
|
Flag::new("parodd", C::PARODD),
|
||||||
|
#[cfg(any(
|
||||||
|
target_os = "android",
|
||||||
|
all(target_os = "linux", not(target_arch = "mips"))
|
||||||
|
))]
|
||||||
Flag::new("cmspar", C::CMSPAR),
|
Flag::new("cmspar", C::CMSPAR),
|
||||||
Flag::new_grouped("cs5", C::CS5, C::CSIZE),
|
Flag::new_grouped("cs5", C::CS5, C::CSIZE),
|
||||||
Flag::new_grouped("cs6", C::CS6, C::CSIZE),
|
Flag::new_grouped("cs6", C::CS6, C::CSIZE),
|
||||||
|
@ -28,7 +40,7 @@ pub const CONTROL_FLAGS: [Flag<C>; 12] = [
|
||||||
Flag::new("crtscts", C::CRTSCTS),
|
Flag::new("crtscts", C::CRTSCTS),
|
||||||
];
|
];
|
||||||
|
|
||||||
pub const INPUT_FLAGS: [Flag<I>; 15] = [
|
pub const INPUT_FLAGS: &[Flag<I>] = &[
|
||||||
Flag::new("ignbrk", I::IGNBRK),
|
Flag::new("ignbrk", I::IGNBRK),
|
||||||
Flag::new("brkint", I::BRKINT).sane(),
|
Flag::new("brkint", I::BRKINT).sane(),
|
||||||
Flag::new("ignpar", I::IGNPAR),
|
Flag::new("ignpar", I::IGNPAR),
|
||||||
|
@ -48,8 +60,14 @@ pub const INPUT_FLAGS: [Flag<I>; 15] = [
|
||||||
Flag::new("iutf8", I::IUTF8),
|
Flag::new("iutf8", I::IUTF8),
|
||||||
];
|
];
|
||||||
|
|
||||||
pub const OUTPUT_FLAGS: [Flag<O>; 24] = [
|
pub const OUTPUT_FLAGS: &[Flag<O>] = &[
|
||||||
Flag::new("opost", O::OPOST).sane(),
|
Flag::new("opost", O::OPOST).sane(),
|
||||||
|
#[cfg(any(
|
||||||
|
target_os = "android",
|
||||||
|
target_os = "haiku",
|
||||||
|
target_os = "linux",
|
||||||
|
target_os = "openbsd"
|
||||||
|
))]
|
||||||
Flag::new("olcuc", O::OLCUC),
|
Flag::new("olcuc", O::OLCUC),
|
||||||
Flag::new("ocrnl", O::OCRNL),
|
Flag::new("ocrnl", O::OCRNL),
|
||||||
Flag::new("onlcr", O::ONLCR).sane(),
|
Flag::new("onlcr", O::ONLCR).sane(),
|
||||||
|
@ -75,7 +93,7 @@ pub const OUTPUT_FLAGS: [Flag<O>; 24] = [
|
||||||
Flag::new_grouped("ff1", O::FF1, O::FFDLY),
|
Flag::new_grouped("ff1", O::FF1, O::FFDLY),
|
||||||
];
|
];
|
||||||
|
|
||||||
pub const LOCAL_FLAGS: [Flag<L>; 18] = [
|
pub const LOCAL_FLAGS: &[Flag<L>] = &[
|
||||||
Flag::new("isig", L::ISIG).sane(),
|
Flag::new("isig", L::ISIG).sane(),
|
||||||
Flag::new("icanon", L::ICANON).sane(),
|
Flag::new("icanon", L::ICANON).sane(),
|
||||||
Flag::new("iexten", L::IEXTEN).sane(),
|
Flag::new("iexten", L::IEXTEN).sane(),
|
||||||
|
@ -98,6 +116,15 @@ pub const LOCAL_FLAGS: [Flag<L>; 18] = [
|
||||||
Flag::new("extproc", L::EXTPROC),
|
Flag::new("extproc", L::EXTPROC),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// BSD's use u32 as baud rate, to using the enum is unnecessary.
|
||||||
|
#[cfg(not(any(
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "dragonfly",
|
||||||
|
target_os = "ios",
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "netbsd",
|
||||||
|
target_os = "openbsd"
|
||||||
|
)))]
|
||||||
pub const BAUD_RATES: &[(&str, BaudRate)] = &[
|
pub const BAUD_RATES: &[(&str, BaudRate)] = &[
|
||||||
("0", BaudRate::B0),
|
("0", BaudRate::B0),
|
||||||
("50", BaudRate::B50),
|
("50", BaudRate::B50),
|
||||||
|
@ -111,62 +138,17 @@ pub const BAUD_RATES: &[(&str, BaudRate)] = &[
|
||||||
("1200", BaudRate::B1200),
|
("1200", BaudRate::B1200),
|
||||||
("1800", BaudRate::B1800),
|
("1800", BaudRate::B1800),
|
||||||
("2400", BaudRate::B2400),
|
("2400", BaudRate::B2400),
|
||||||
#[cfg(any(
|
|
||||||
target_os = "dragonfly",
|
|
||||||
target_os = "freebsd",
|
|
||||||
target_os = "macos",
|
|
||||||
target_os = "netbsd",
|
|
||||||
target_os = "openbsd"
|
|
||||||
))]
|
|
||||||
("4800", BaudRate::B4800),
|
|
||||||
("9600", BaudRate::B9600),
|
("9600", BaudRate::B9600),
|
||||||
#[cfg(any(
|
|
||||||
target_os = "dragonfly",
|
|
||||||
target_os = "freebsd",
|
|
||||||
target_os = "macos",
|
|
||||||
target_os = "netbsd",
|
|
||||||
target_os = "openbsd"
|
|
||||||
))]
|
|
||||||
("14400", BaudRate::B14400),
|
|
||||||
("19200", BaudRate::B19200),
|
("19200", BaudRate::B19200),
|
||||||
#[cfg(any(
|
|
||||||
target_os = "dragonfly",
|
|
||||||
target_os = "freebsd",
|
|
||||||
target_os = "macos",
|
|
||||||
target_os = "netbsd",
|
|
||||||
target_os = "openbsd"
|
|
||||||
))]
|
|
||||||
("28800", BaudRate::B28800),
|
|
||||||
("38400", BaudRate::B38400),
|
("38400", BaudRate::B38400),
|
||||||
("57600", BaudRate::B57600),
|
("57600", BaudRate::B57600),
|
||||||
#[cfg(any(
|
|
||||||
target_os = "dragonfly",
|
|
||||||
target_os = "freebsd",
|
|
||||||
target_os = "macos",
|
|
||||||
target_os = "netbsd",
|
|
||||||
target_os = "openbsd"
|
|
||||||
))]
|
|
||||||
("76800", BaudRate::B76800),
|
|
||||||
("115200", BaudRate::B115200),
|
("115200", BaudRate::B115200),
|
||||||
("230400", BaudRate::B230400),
|
("230400", BaudRate::B230400),
|
||||||
#[cfg(any(
|
|
||||||
target_os = "dragonfly",
|
|
||||||
target_os = "freebsd",
|
|
||||||
target_os = "macos",
|
|
||||||
target_os = "netbsd",
|
|
||||||
target_os = "openbsd"
|
|
||||||
))]
|
|
||||||
("460800", BaudRate::B460800),
|
|
||||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||||
("500000", BaudRate::B500000),
|
("500000", BaudRate::B500000),
|
||||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||||
("576000", BaudRate::B576000),
|
("576000", BaudRate::B576000),
|
||||||
#[cfg(any(
|
#[cfg(any(target_os = "android", target_os = "linux",))]
|
||||||
target_os = "android",
|
|
||||||
target_os = "freebsd",
|
|
||||||
target_os = "linux",
|
|
||||||
target_os = "netbsd"
|
|
||||||
))]
|
|
||||||
("921600", BaudRate::B921600),
|
("921600", BaudRate::B921600),
|
||||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||||
("1000000", BaudRate::B1000000),
|
("1000000", BaudRate::B1000000),
|
||||||
|
|
|
@ -19,7 +19,16 @@ use std::os::unix::io::{AsRawFd, RawFd};
|
||||||
use uucore::error::{UResult, USimpleError};
|
use uucore::error::{UResult, USimpleError};
|
||||||
use uucore::{format_usage, InvalidEncodingHandling};
|
use uucore::{format_usage, InvalidEncodingHandling};
|
||||||
|
|
||||||
use flags::{BAUD_RATES, CONTROL_FLAGS, INPUT_FLAGS, LOCAL_FLAGS, OUTPUT_FLAGS};
|
#[cfg(not(any(
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "dragonfly",
|
||||||
|
target_os = "ios",
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "netbsd",
|
||||||
|
target_os = "openbsd"
|
||||||
|
)))]
|
||||||
|
use flags::BAUD_RATES;
|
||||||
|
use flags::{CONTROL_FLAGS, INPUT_FLAGS, LOCAL_FLAGS, OUTPUT_FLAGS};
|
||||||
|
|
||||||
const NAME: &str = "stty";
|
const NAME: &str = "stty";
|
||||||
const USAGE: &str = "\
|
const USAGE: &str = "\
|
||||||
|
@ -177,6 +186,28 @@ fn stty(opts: &Options) -> UResult<()> {
|
||||||
|
|
||||||
fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
|
fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
|
||||||
let speed = cfgetospeed(termios);
|
let speed = cfgetospeed(termios);
|
||||||
|
|
||||||
|
// BSDs use a u32 for the baud rate, so we can simply print it.
|
||||||
|
#[cfg(any(
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "dragonfly",
|
||||||
|
target_os = "ios",
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "netbsd",
|
||||||
|
target_os = "openbsd"
|
||||||
|
))]
|
||||||
|
print!("speed {} baud; ", speed);
|
||||||
|
|
||||||
|
// Other platforms need to use the baud rate enum, so printing the right value
|
||||||
|
// becomes slightly more complicated.
|
||||||
|
#[cfg(not(any(
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "dragonfly",
|
||||||
|
target_os = "ios",
|
||||||
|
target_os = "macos",
|
||||||
|
target_os = "netbsd",
|
||||||
|
target_os = "openbsd"
|
||||||
|
)))]
|
||||||
for (text, baud_rate) in BAUD_RATES {
|
for (text, baud_rate) in BAUD_RATES {
|
||||||
if *baud_rate == speed {
|
if *baud_rate == speed {
|
||||||
print!("speed {} baud; ", text);
|
print!("speed {} baud; ", text);
|
||||||
|
@ -190,21 +221,25 @@ fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
|
||||||
print!("rows {}; columns {}; ", size.rows, size.columns);
|
print!("rows {}; columns {}; ", size.rows, size.columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For some reason the normal nix Termios struct does not expose the line,
|
#[cfg(any(target_os = "linux", target_os = "redox"))]
|
||||||
// so we get the underlying libc::termios struct to get that information.
|
{
|
||||||
let libc_termios: nix::libc::termios = termios.clone().into();
|
// For some reason the normal nix Termios struct does not expose the line,
|
||||||
let line = libc_termios.c_line;
|
// so we get the underlying libc::termios struct to get that information.
|
||||||
print!("line = {};", line);
|
let libc_termios: nix::libc::termios = termios.clone().into();
|
||||||
|
let line = libc_termios.c_line;
|
||||||
|
print!("line = {};", line);
|
||||||
|
}
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_settings(termios: &Termios, opts: &Options) -> nix::Result<()> {
|
fn print_settings(termios: &Termios, opts: &Options) -> nix::Result<()> {
|
||||||
print_terminal_size(termios, opts)?;
|
print_terminal_size(termios, opts)?;
|
||||||
print_flags(termios, opts, &CONTROL_FLAGS);
|
print_flags(termios, opts, CONTROL_FLAGS);
|
||||||
print_flags(termios, opts, &INPUT_FLAGS);
|
print_flags(termios, opts, INPUT_FLAGS);
|
||||||
print_flags(termios, opts, &OUTPUT_FLAGS);
|
print_flags(termios, opts, OUTPUT_FLAGS);
|
||||||
print_flags(termios, opts, &LOCAL_FLAGS);
|
print_flags(termios, opts, LOCAL_FLAGS);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,10 +284,10 @@ fn apply_setting(termios: &mut Termios, s: &str) -> ControlFlow<bool> {
|
||||||
Some(s) => (true, s),
|
Some(s) => (true, s),
|
||||||
None => (false, s),
|
None => (false, s),
|
||||||
};
|
};
|
||||||
apply_flag(termios, &CONTROL_FLAGS, name, remove)?;
|
apply_flag(termios, CONTROL_FLAGS, name, remove)?;
|
||||||
apply_flag(termios, &INPUT_FLAGS, name, remove)?;
|
apply_flag(termios, INPUT_FLAGS, name, remove)?;
|
||||||
apply_flag(termios, &OUTPUT_FLAGS, name, remove)?;
|
apply_flag(termios, OUTPUT_FLAGS, name, remove)?;
|
||||||
apply_flag(termios, &LOCAL_FLAGS, name, remove)?;
|
apply_flag(termios, LOCAL_FLAGS, name, remove)?;
|
||||||
ControlFlow::Break(false)
|
ControlFlow::Break(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue