Skip to content

Commit

Permalink
stty: Support 'drain' option
Browse files Browse the repository at this point in the history
This also changes the default value to match GNU.

Part of uutils#3859.
  • Loading branch information
dezgeg committed Jul 19, 2023
1 parent b08ee68 commit 6690184
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/uu/stty/src/stty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// * For the full copyright and license information, please view the LICENSE file
// * that was distributed with this source code.

// spell-checker:ignore clocal erange tcgetattr tcsetattr tcsanow tiocgwinsz tiocswinsz cfgetospeed cfsetospeed ushort vmin vtime
// spell-checker:ignore clocal erange tcgetattr tcsetattr tcsadrain tcsanow tiocgwinsz tiocswinsz cfgetospeed cfsetospeed ushort vmin vtime

mod flags;

Expand Down Expand Up @@ -126,6 +126,7 @@ impl<'a> Options<'a> {

struct Context {
termios: Termios,
set_arg: nix::sys::termios::SetArg,
fd: RawFd,
}

Expand Down Expand Up @@ -191,6 +192,7 @@ fn stty(opts: &Options) -> UResult<()> {
if let Some(settings) = &opts.settings {
let mut ctx = Context {
termios,
set_arg: nix::sys::termios::SetArg::TCSADRAIN,
fd: opts.file,
};

Expand All @@ -203,7 +205,7 @@ fn stty(opts: &Options) -> UResult<()> {
}
}

tcsetattr(opts.file, nix::sys::termios::SetArg::TCSANOW, &ctx.termios)
tcsetattr(opts.file, ctx.set_arg, &ctx.termios)
.expect("Could not write terminal attributes");
} else {
print_settings(&termios, opts).expect("TODO: make proper error here from nix error");
Expand Down Expand Up @@ -385,6 +387,7 @@ fn apply_setting(ctx: &mut Context, s: &str) -> ControlFlow<bool> {
Some(s) => (true, s),
None => (false, s),
};
apply_drain_flag(ctx, name, remove)?;
apply_flag(ctx, CONTROL_FLAGS, name, remove)?;
apply_flag(ctx, INPUT_FLAGS, name, remove)?;
apply_flag(ctx, OUTPUT_FLAGS, name, remove)?;
Expand Down Expand Up @@ -423,6 +426,18 @@ fn apply_flag<T: TermiosFlag>(
ControlFlow::Continue(())
}

fn apply_drain_flag(ctx: &mut Context, input: &str, remove: bool) -> ControlFlow<bool> {
if input == "drain" {
if remove {
ctx.set_arg = nix::sys::termios::SetArg::TCSANOW;
} else {
ctx.set_arg = nix::sys::termios::SetArg::TCSADRAIN;
}
return ControlFlow::Break(true);
}
ControlFlow::Continue(())
}

fn apply_baud_rate_flag(ctx: &mut Context, input: &str) -> ControlFlow<bool> {
// BSDs use a u32 for the baud rate, so any decimal number applies.
#[cfg(any(
Expand Down

0 comments on commit 6690184

Please sign in to comment.