From f1e8a30872390c8b8f8c154b04c4a9e70168062a Mon Sep 17 00:00:00 2001 From: pabzdzdzwiagief Date: Mon, 23 Dec 2013 18:12:26 +0100 Subject: [PATCH 1/5] Implement tee --- Makefile | 1 + README.md | 1 - tee/tee.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tee/tee.rs diff --git a/Makefile b/Makefile index 9d8d17163..cb175821c 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ EXES := \ printenv \ pwd \ rmdir \ + tee \ true \ wc \ whoami \ diff --git a/README.md b/README.md index 759b5d1cb..434bd7b5b 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,6 @@ To do - tac-pipe - tac - tail -- tee - test - timeout - touch diff --git a/tee/tee.rs b/tee/tee.rs new file mode 100644 index 000000000..f729f66d3 --- /dev/null +++ b/tee/tee.rs @@ -0,0 +1,108 @@ +#[link(name="tee", vers="1.0.0", author="Aleksander Bielawski")]; + +/* + * This file is part of the uutils coreutils package. + * + * (c) Aleksander Bielawski + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern mod extra; + +use std::io::{stdin, stdout, Append, File, Truncate, Write}; +use std::io::{io_error, result, EndOfFile}; +use std::io::signal::{Interrupt, Listener}; +use std::io::util::{copy, MultiWriter}; +use std::os::{args, set_exit_status}; +use extra::getopts::groups::{getopts, optflag, usage}; + +static NAME: &'static str = "tee"; +static VERSION: &'static str = "1.0.0"; + +fn main() { + match options(args()).and_then(exec) { + Err(message) => { + error!("{}: {}", args()[0], message); + set_exit_status(1) + }, + Ok(status) => set_exit_status(status) + } +} + +struct Options { + program: ~str, + append: bool, + ignore_interrupts: bool, + print_and_exit: Option<~str>, + files: ~[Path] +} + +fn options(args: &[~str]) -> Result { + let opts = ~[ + optflag("a", "append", "append to the given FILEs, do not overwrite"), + optflag("i", "ignore-interrupts", "ignore interrupt signals"), + optflag("", "help", "display this help and exit"), + optflag("", "version", "output version information and exit")]; + getopts(args.tail(), opts).map_err(|e| e.to_err_msg()).and_then(|m| { + let version = format!("{} {}", NAME, VERSION); + let program = args[0].clone(); + let arguments = "[OPTION]... [FILE]..."; + let brief = "Copy standard input to each FILE, and also to standard " + + "output."; + let comment = "If a FILE is -, copy again to standard output."; + let help = format!("{}\n\nUsage:\n {} {}\n\n{}\n{}", + version, program, arguments, usage(brief, opts), + comment); + let names = m.free + ~[~"-"]; + let to_print = if m.opt_present("help") { Some(help) } + else if m.opt_present("version") { Some(version) } + else { None }; + Ok(Options { + program: program, + append: m.opt_present("append"), + ignore_interrupts: m.opt_present("ignore-interrupts"), + print_and_exit: to_print, + files: names.map(|name| Path::new(name.clone())) + }) + }) +} + +fn open(path: &Path, append: bool) -> ~Writer { + if *path == Path::new("-") { + ~stdout() as ~Writer + } else { + let mode = if append { Append } else { Truncate }; + ~File::open_mode(path, mode, Write) as ~Writer + } +} + +fn exec(options: Options) -> Result { + match options.print_and_exit { + Some(text) => { + println(text); + Ok(0) + }, + None => tee(options) + } +} + +fn tee(options: Options) -> Result { + let mut handler = Listener::new(); + if options.ignore_interrupts { + handler.register(Interrupt); + } + result(|| io_error::cond.trap(|e| { + if e.kind != EndOfFile { + io_error::cond.raise(e); + } + }).inside(|| { + let writers = options.files.map(|path| open(path, options.append)); + let output = &mut MultiWriter::new(writers); + let input = &mut stdin(); + copy(input, output); + output.flush(); + 0 + })).map_err(|err| err.desc.to_owned()) +} From 2d5180be07c7d0ca5094dcb7f3ace27b763702b5 Mon Sep 17 00:00:00 2001 From: pabzdzdzwiagief Date: Fri, 27 Dec 2013 20:56:15 +0100 Subject: [PATCH 2/5] Update crate attributes for tee --- tee/tee.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tee/tee.rs b/tee/tee.rs index f729f66d3..82fdf2919 100644 --- a/tee/tee.rs +++ b/tee/tee.rs @@ -1,4 +1,5 @@ -#[link(name="tee", vers="1.0.0", author="Aleksander Bielawski")]; +#[crate_id(name="tee", vers="1.0.0", author="Aleksander Bielawski")]; +#[license="MIT"]; /* * This file is part of the uutils coreutils package. From af4092124b0557e856279d607576a8d3946f5ca1 Mon Sep 17 00:00:00 2001 From: pabzdzdzwiagief Date: Sat, 28 Dec 2013 01:49:44 +0100 Subject: [PATCH 3/5] Add standard warning function in tee --- tee/tee.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tee/tee.rs b/tee/tee.rs index 82fdf2919..2ce671f96 100644 --- a/tee/tee.rs +++ b/tee/tee.rs @@ -25,7 +25,7 @@ static VERSION: &'static str = "1.0.0"; fn main() { match options(args()).and_then(exec) { Err(message) => { - error!("{}: {}", args()[0], message); + warn(message); set_exit_status(1) }, Ok(status) => set_exit_status(status) @@ -107,3 +107,7 @@ fn tee(options: Options) -> Result { 0 })).map_err(|err| err.desc.to_owned()) } + +fn warn(message: &str) { + error!("{}: {}", args()[0], message); +} From 7e260558b81ffcc36e7b958e4a5c79ebc4bec24a Mon Sep 17 00:00:00 2001 From: pabzdzdzwiagief Date: Sat, 28 Dec 2013 02:23:13 +0100 Subject: [PATCH 4/5] Rearrange functions in tee --- tee/tee.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tee/tee.rs b/tee/tee.rs index 2ce671f96..7322fe7da 100644 --- a/tee/tee.rs +++ b/tee/tee.rs @@ -70,15 +70,6 @@ fn options(args: &[~str]) -> Result { }) } -fn open(path: &Path, append: bool) -> ~Writer { - if *path == Path::new("-") { - ~stdout() as ~Writer - } else { - let mode = if append { Append } else { Truncate }; - ~File::open_mode(path, mode, Write) as ~Writer - } -} - fn exec(options: Options) -> Result { match options.print_and_exit { Some(text) => { @@ -108,6 +99,15 @@ fn tee(options: Options) -> Result { })).map_err(|err| err.desc.to_owned()) } +fn open(path: &Path, append: bool) -> ~Writer { + if *path == Path::new("-") { + ~stdout() as ~Writer + } else { + let mode = if append { Append } else { Truncate }; + ~File::open_mode(path, mode, Write) as ~Writer + } +} + fn warn(message: &str) { error!("{}: {}", args()[0], message); } From d545e5b31fb76e71d42dc81f737ad9909b1f830f Mon Sep 17 00:00:00 2001 From: pabzdzdzwiagief Date: Fri, 27 Dec 2013 21:04:11 +0100 Subject: [PATCH 5/5] Make tee keep going on errors --- tee/tee.rs | 92 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 25 deletions(-) diff --git a/tee/tee.rs b/tee/tee.rs index 7322fe7da..543c5aabc 100644 --- a/tee/tee.rs +++ b/tee/tee.rs @@ -13,9 +13,9 @@ extern mod extra; use std::io::{stdin, stdout, Append, File, Truncate, Write}; -use std::io::{io_error, result, EndOfFile}; +use std::io::{io_error, EndOfFile}; use std::io::signal::{Interrupt, Listener}; -use std::io::util::{copy, MultiWriter}; +use std::io::util::{copy, NullWriter, MultiWriter}; use std::os::{args, set_exit_status}; use extra::getopts::groups::{getopts, optflag, usage}; @@ -24,11 +24,8 @@ static VERSION: &'static str = "1.0.0"; fn main() { match options(args()).and_then(exec) { - Err(message) => { - warn(message); - set_exit_status(1) - }, - Ok(status) => set_exit_status(status) + Ok(_) => set_exit_status(0), + Err(_) => set_exit_status(1) } } @@ -40,7 +37,7 @@ struct Options { files: ~[Path] } -fn options(args: &[~str]) -> Result { +fn options(args: &[~str]) -> Result { let opts = ~[ optflag("a", "append", "append to the given FILEs, do not overwrite"), optflag("i", "ignore-interrupts", "ignore interrupt signals"), @@ -67,45 +64,90 @@ fn options(args: &[~str]) -> Result { print_and_exit: to_print, files: names.map(|name| Path::new(name.clone())) }) - }) + }).map_err(|message| warn(message)) } -fn exec(options: Options) -> Result { +fn exec(options: Options) -> Result<(), ()> { match options.print_and_exit { - Some(text) => { - println(text); - Ok(0) - }, + Some(text) => Ok(println(text)), None => tee(options) } } -fn tee(options: Options) -> Result { +fn tee(options: Options) -> Result<(), ()> { let mut handler = Listener::new(); if options.ignore_interrupts { handler.register(Interrupt); } - result(|| io_error::cond.trap(|e| { - if e.kind != EndOfFile { - io_error::cond.raise(e); - } - }).inside(|| { + let mut ok = true; + io_error::cond.trap(|_| ok = false).inside(|| { let writers = options.files.map(|path| open(path, options.append)); let output = &mut MultiWriter::new(writers); - let input = &mut stdin(); + let input = &mut NamedReader { inner: ~stdin() as ~Reader }; copy(input, output); output.flush(); - 0 - })).map_err(|err| err.desc.to_owned()) + }); + if ok { Ok(()) } else { Err(()) } } fn open(path: &Path, append: bool) -> ~Writer { - if *path == Path::new("-") { + let inner = with_path(path, || if *path == Path::new("-") { ~stdout() as ~Writer } else { let mode = if append { Append } else { Truncate }; - ~File::open_mode(path, mode, Write) as ~Writer + match File::open_mode(path, mode, Write) { + Some(file) => ~file as ~Writer, + None => ~NullWriter as ~Writer + } + }); + ~NamedWriter { inner: inner, path: ~path.clone() } as ~Writer +} + +struct NamedWriter { + priv inner: ~Writer, + priv path: ~Path +} + +impl Writer for NamedWriter { + fn write(&mut self, buf: &[u8]) { + with_path(self.path, || io_error::cond.trap(|e| { + self.inner = ~NullWriter as ~Writer; + io_error::cond.raise(e); + }).inside(|| self.inner.write(buf))) } + + fn flush(&mut self) { + with_path(self.path, || io_error::cond.trap(|e| { + self.inner = ~NullWriter as ~Writer; + io_error::cond.raise(e); + }).inside(|| self.inner.flush())) + } +} + +struct NamedReader { + priv inner: ~Reader +} + +impl Reader for NamedReader { + fn read(&mut self, buf: &mut [u8]) -> Option { + with_path(&Path::new("stdin"), || io_error::cond.trap(|e| { + if e.kind != EndOfFile { + io_error::cond.raise(e) + } + }).inside(|| self.inner.read(buf))) + + } + + fn eof(&mut self) -> bool { + self.inner.eof() + } +} + +fn with_path(path: &Path, cb: || -> T) -> T { + io_error::cond.trap(|e| { + warn(format!("{}: {}", path.display(), e.desc)); + io_error::cond.raise(e); + }).inside(cb) } fn warn(message: &str) {