From d558e37288bbb003d7b85c49ea5fb1e28027bdda Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Sat, 23 May 2015 03:21:53 -0400 Subject: [PATCH] Add macro to manually flush a writer. I built upon the pipe_* macros, adding pipe_flush!() to flush an optional writer (default to stdout if no writer is given). --- src/common/util.rs | 28 ++++++++++++++++++++++++++++ src/echo/echo.rs | 5 +++-- src/env/env.rs | 6 ++++++ src/seq/seq.rs | 1 + 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/common/util.rs b/src/common/util.rs index 4161cfbd7..690848ff8 100644 --- a/src/common/util.rs +++ b/src/common/util.rs @@ -162,6 +162,34 @@ macro_rules! pipe_writeln( ) ); +#[macro_export] +macro_rules! pipe_flush( + () => ( + match ::std::io::stdout().flush() { + Ok(_) => true, + Err(f) => { + if f.kind() == ::std::io::ErrorKind::BrokenPipe { + false + } else { + panic!("{}", f) + } + } + } + ); + ($fd:expr) => ( + match $fd.flush() { + Ok(_) => true, + Err(f) => { + if f.kind() == ::std::io::ErrorKind::BrokenPipe { + false + } else { + panic!("{}", f) + } + } + } + ) +); + #[macro_export] macro_rules! safe_write( ($fd:expr, $($args:tt)+) => ( diff --git a/src/echo/echo.rs b/src/echo/echo.rs index 1fe6b1608..11ba354ef 100644 --- a/src/echo/echo.rs +++ b/src/echo/echo.rs @@ -12,10 +12,11 @@ extern crate getopts; extern crate libc; -use std::io::{stdout, Write}; +use std::io::Write; use std::str::from_utf8; #[path = "../common/util.rs"] +#[macro_use] mod util; #[allow(dead_code)] @@ -244,7 +245,7 @@ pub fn uumain(args: Vec) -> i32 { } if options.newline { - let _ = stdout().flush(); + pipe_flush!(); } else { println!("") } diff --git a/src/env/env.rs b/src/env/env.rs index e930afe0c..a6ccbe718 100644 --- a/src/env/env.rs +++ b/src/env/env.rs @@ -14,8 +14,13 @@ #![allow(non_camel_case_types)] use std::env; +use std::io::Write; use std::process::Command; +#[path = "../common/util.rs"] +#[macro_use] +mod util; + struct options { ignore_env: bool, null: bool, @@ -194,6 +199,7 @@ pub fn uumain(args: Vec) -> i32 { } else { // no program provided print_env(opts.null); + pipe_flush!(); } 0 diff --git a/src/seq/seq.rs b/src/seq/seq.rs index 7c17d7b86..88fcb2e71 100644 --- a/src/seq/seq.rs +++ b/src/seq/seq.rs @@ -256,4 +256,5 @@ fn print_seq(first: f64, step: f64, last: f64, largest_dec: usize, separator: St if (first >= last && step < 0f64) || (first <= last && step > 0f64) { pipe_print!("{}", terminator); } + pipe_flush!(); }