Skip to content

Commit

Permalink
stty: fix bsd/mac builds
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Aug 16, 2022
1 parent 750e725 commit 39d1770
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 24 additions & 50 deletions src/uu/stty/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ use nix::sys::termios::{
BaudRate, ControlFlags as C, InputFlags as I, LocalFlags as L, OutputFlags as O,
};

pub const CONTROL_FLAGS: [Flag<C>; 12] = [
pub const CONTROL_FLAGS: &[Flag<C>] = &[
Flag::new("parenb", C::PARENB),
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_grouped("cs5", C::CS5, C::CSIZE),
Flag::new_grouped("cs6", C::CS6, C::CSIZE),
Expand All @@ -28,7 +32,7 @@ pub const CONTROL_FLAGS: [Flag<C>; 12] = [
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("brkint", I::BRKINT).sane(),
Flag::new("ignpar", I::IGNPAR),
Expand All @@ -48,8 +52,14 @@ pub const INPUT_FLAGS: [Flag<I>; 15] = [
Flag::new("iutf8", I::IUTF8),
];

pub const OUTPUT_FLAGS: [Flag<O>; 24] = [
pub const OUTPUT_FLAGS: &[Flag<O>] = &[
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("ocrnl", O::OCRNL),
Flag::new("onlcr", O::ONLCR).sane(),
Expand All @@ -75,7 +85,7 @@ pub const OUTPUT_FLAGS: [Flag<O>; 24] = [
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("icanon", L::ICANON).sane(),
Flag::new("iexten", L::IEXTEN).sane(),
Expand All @@ -98,6 +108,15 @@ pub const LOCAL_FLAGS: [Flag<L>; 18] = [
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)] = &[
("0", BaudRate::B0),
("50", BaudRate::B50),
Expand All @@ -111,62 +130,17 @@ pub const BAUD_RATES: &[(&str, BaudRate)] = &[
("1200", BaudRate::B1200),
("1800", BaudRate::B1800),
("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),
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
))]
("14400", BaudRate::B14400),
("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),
("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),
("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"))]
("500000", BaudRate::B500000),
#[cfg(any(target_os = "android", target_os = "linux"))]
("576000", BaudRate::B576000),
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd"
))]
#[cfg(any(target_os = "android", target_os = "linux",))]
("921600", BaudRate::B921600),
#[cfg(any(target_os = "android", target_os = "linux"))]
("1000000", BaudRate::B1000000),
Expand Down
52 changes: 39 additions & 13 deletions src/uu/stty/src/stty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ fn stty(opts: &Options) -> UResult<()> {

fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
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 {
if *baud_rate == speed {
print!("speed {} baud; ", text);
Expand All @@ -190,21 +212,25 @@ fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
print!("rows {}; columns {}; ", size.rows, size.columns);
}

// For some reason the normal nix Termios struct does not expose the line,
// so we get the underlying libc::termios struct to get that information.
let libc_termios: nix::libc::termios = termios.clone().into();
let line = libc_termios.c_line;
print!("line = {};", line);
#[cfg(any(target_os = "linux", target_os = "redox"))]
{
// For some reason the normal nix Termios struct does not expose the line,
// so we get the underlying libc::termios struct to get that information.
let libc_termios: nix::libc::termios = termios.clone().into();
let line = libc_termios.c_line;
print!("line = {};", line);
}

println!();
Ok(())
}

fn print_settings(termios: &Termios, opts: &Options) -> nix::Result<()> {
print_terminal_size(termios, opts)?;
print_flags(termios, opts, &CONTROL_FLAGS);
print_flags(termios, opts, &INPUT_FLAGS);
print_flags(termios, opts, &OUTPUT_FLAGS);
print_flags(termios, opts, &LOCAL_FLAGS);
print_flags(termios, opts, CONTROL_FLAGS);
print_flags(termios, opts, INPUT_FLAGS);
print_flags(termios, opts, OUTPUT_FLAGS);
print_flags(termios, opts, LOCAL_FLAGS);
Ok(())
}

Expand Down Expand Up @@ -249,10 +275,10 @@ fn apply_setting(termios: &mut Termios, s: &str) -> ControlFlow<bool> {
Some(s) => (true, 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)?;
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)
}

Expand Down

0 comments on commit 39d1770

Please sign in to comment.