2015-12-08 02:42:08 +00:00
|
|
|
#![crate_name = "uu_tee"]
|
2015-01-10 19:31:55 +00:00
|
|
|
|
2013-12-23 17:12:26 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Aleksander Bielawski <pabzdzdzwiagief@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate getopts;
|
2013-12-23 17:12:26 +00:00
|
|
|
|
2015-11-24 01:00:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate uucore;
|
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
use std::fs::OpenOptions;
|
|
|
|
use std::io::{copy, Error, ErrorKind, Read, Result, sink, stdin, stdout, Write};
|
|
|
|
use std::path::{Path, PathBuf};
|
2015-05-21 18:42:42 +00:00
|
|
|
|
2013-12-23 17:12:26 +00:00
|
|
|
static NAME: &'static str = "tee";
|
2015-11-25 09:52:10 +00:00
|
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2013-12-23 17:12:26 +00:00
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2015-05-05 04:04:27 +00:00
|
|
|
match options(&args).and_then(exec) {
|
2014-06-08 07:56:37 +00:00
|
|
|
Ok(_) => 0,
|
|
|
|
Err(_) => 1
|
2013-12-23 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:13:55 +00:00
|
|
|
#[allow(dead_code)]
|
2013-12-23 17:12:26 +00:00
|
|
|
struct Options {
|
2014-05-25 09:20:52 +00:00
|
|
|
program: String,
|
2013-12-23 17:12:26 +00:00
|
|
|
append: bool,
|
|
|
|
ignore_interrupts: bool,
|
2014-05-25 09:20:52 +00:00
|
|
|
print_and_exit: Option<String>,
|
2015-05-05 04:04:27 +00:00
|
|
|
files: Vec<String>
|
2013-12-23 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
fn options(args: &Vec<String>) -> Result<Options> {
|
2015-05-21 18:42:42 +00:00
|
|
|
let mut opts = getopts::Options::new();
|
|
|
|
|
|
|
|
opts.optflag("a", "append", "append to the given FILEs, do not overwrite");
|
|
|
|
opts.optflag("i", "ignore-interrupts", "ignore interrupt signals");
|
|
|
|
opts.optflag("h", "help", "display this help and exit");
|
|
|
|
opts.optflag("V", "version", "output version information and exit");
|
|
|
|
|
|
|
|
opts.parse(&args[1..]).map_err(|e| Error::new(ErrorKind::Other, format!("{}", e))).and_then(|m| {
|
2013-12-23 17:12:26 +00:00
|
|
|
let version = format!("{} {}", NAME, VERSION);
|
|
|
|
let arguments = "[OPTION]... [FILE]...";
|
2014-05-23 12:28:40 +00:00
|
|
|
let brief = "Copy standard input to each FILE, and also to standard output.";
|
2013-12-23 17:12:26 +00:00
|
|
|
let comment = "If a FILE is -, copy again to standard output.";
|
|
|
|
let help = format!("{}\n\nUsage:\n {} {}\n\n{}\n{}",
|
2015-05-21 18:42:42 +00:00
|
|
|
version, NAME, arguments, opts.usage(brief),
|
2013-12-23 17:12:26 +00:00
|
|
|
comment);
|
2015-05-05 04:04:27 +00:00
|
|
|
let mut names: Vec<String> = m.free.clone().into_iter().collect();
|
2014-09-23 22:01:40 +00:00
|
|
|
names.push("-".to_string());
|
2013-12-23 17:12:26 +00:00
|
|
|
let to_print = if m.opt_present("help") { Some(help) }
|
|
|
|
else if m.opt_present("version") { Some(version) }
|
|
|
|
else { None };
|
|
|
|
Ok(Options {
|
2015-05-21 18:42:42 +00:00
|
|
|
program: NAME.to_string(),
|
2013-12-23 17:12:26 +00:00
|
|
|
append: m.opt_present("append"),
|
|
|
|
ignore_interrupts: m.opt_present("ignore-interrupts"),
|
|
|
|
print_and_exit: to_print,
|
2015-05-05 04:04:27 +00:00
|
|
|
files: names
|
2013-12-23 17:12:26 +00:00
|
|
|
})
|
2015-05-05 04:04:27 +00:00
|
|
|
}).map_err(|message| warn(format!("{}", message).as_ref()))
|
2013-12-23 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
fn exec(options: Options) -> Result<()> {
|
2013-12-23 17:12:26 +00:00
|
|
|
match options.print_and_exit {
|
2015-05-05 04:04:27 +00:00
|
|
|
Some(text) => Ok(println!("{}", text)),
|
2013-12-23 17:12:26 +00:00
|
|
|
None => tee(options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
fn tee(options: Options) -> Result<()> {
|
|
|
|
let writers: Vec<Box<Write>> = options.files.clone().into_iter().map(|file| open(file, options.append)).collect();
|
|
|
|
let output = &mut MultiWriter { writers: writers };
|
|
|
|
let input = &mut NamedReader { inner: Box::new(stdin()) as Box<Read> };
|
2014-02-07 06:39:07 +00:00
|
|
|
if copy(input, output).is_err() || output.flush().is_err() {
|
2015-05-05 04:04:27 +00:00
|
|
|
Err(Error::new(ErrorKind::Other, ""))
|
2014-02-07 06:39:07 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
2013-12-23 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-28 00:49:44 +00:00
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
fn open(name: String, append: bool) -> Box<Write> {
|
|
|
|
let is_stdout = name == "-";
|
|
|
|
let path = PathBuf::from(name);
|
|
|
|
let inner: Box<Write> = if is_stdout {
|
|
|
|
Box::new(stdout())
|
2013-12-28 01:23:13 +00:00
|
|
|
} else {
|
2015-05-05 04:04:27 +00:00
|
|
|
let mut options = OpenOptions::new();
|
|
|
|
let mode = if append { options.append(true) } else { options.truncate(true) };
|
|
|
|
match mode.write(true).create(true).open(path.as_path()) {
|
|
|
|
Ok(file) => Box::new(file),
|
|
|
|
Err(_) => Box::new(sink())
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
2014-02-07 06:39:07 +00:00
|
|
|
};
|
2015-05-05 04:04:27 +00:00
|
|
|
Box::new(NamedWriter { inner: inner, path: path }) as Box<Write>
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MultiWriter {
|
|
|
|
writers: Vec<Box<Write>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Write for MultiWriter {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
for writer in self.writers.iter_mut() {
|
|
|
|
try!(writer.write_all(buf));
|
|
|
|
}
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<()> {
|
|
|
|
for writer in self.writers.iter_mut() {
|
|
|
|
try!(writer.flush());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct NamedWriter {
|
2015-05-05 04:04:27 +00:00
|
|
|
inner: Box<Write>,
|
|
|
|
path: PathBuf
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
impl Write for NamedWriter {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
match self.inner.write(buf) {
|
|
|
|
Err(f) => {
|
|
|
|
self.inner = Box::new(sink()) as Box<Write>;
|
|
|
|
warn(format!("{}: {}", self.path.display(), f.to_string()).as_ref());
|
|
|
|
Err(f)
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
2015-05-05 04:04:27 +00:00
|
|
|
okay => okay
|
|
|
|
}
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
fn flush(&mut self) -> Result<()> {
|
|
|
|
match self.inner.flush() {
|
|
|
|
Err(f) => {
|
|
|
|
self.inner = Box::new(sink()) as Box<Write>;
|
|
|
|
warn(format!("{}: {}", self.path.display(), f.to_string()).as_ref());
|
|
|
|
Err(f)
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
2015-05-05 04:04:27 +00:00
|
|
|
okay => okay
|
|
|
|
}
|
2013-12-28 01:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:04:11 +00:00
|
|
|
struct NamedReader {
|
2015-05-05 04:04:27 +00:00
|
|
|
inner: Box<Read>
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
impl Read for NamedReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
match self.inner.read(buf) {
|
|
|
|
Err(f) => {
|
|
|
|
warn(format!("{}: {}", Path::new("stdin").display(), f.to_string()).as_ref());
|
|
|
|
Err(f)
|
|
|
|
}
|
|
|
|
okay => okay
|
|
|
|
}
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 04:04:27 +00:00
|
|
|
fn warn(message: &str) -> Error {
|
2015-05-21 18:42:42 +00:00
|
|
|
eprintln!("{}: {}", NAME, message);
|
|
|
|
Error::new(ErrorKind::Other, format!("{}: {}", NAME, message))
|
2013-12-28 00:49:44 +00:00
|
|
|
}
|