2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "tee"]
|
2015-01-10 19:31:55 +00:00
|
|
|
#![allow(unstable)]
|
|
|
|
|
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;
|
2015-01-08 20:59:07 +00:00
|
|
|
#[macro_use] extern crate log;
|
2013-12-23 17:12:26 +00:00
|
|
|
|
2014-01-13 09:05:02 +00:00
|
|
|
use std::io::{println, stdin, stdout, Append, File, Truncate, Write};
|
2014-02-07 06:39:07 +00:00
|
|
|
use std::io::{IoResult};
|
2013-12-27 20:04:11 +00:00
|
|
|
use std::io::util::{copy, NullWriter, MultiWriter};
|
2014-06-08 07:56:37 +00:00
|
|
|
use std::os;
|
2014-02-07 06:39:07 +00:00
|
|
|
use getopts::{getopts, optflag, usage};
|
2013-12-23 17:12:26 +00:00
|
|
|
|
|
|
|
static NAME: &'static str = "tee";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
|
|
|
|
2015-01-10 13:07:39 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> isize {
|
2014-05-28 11:43:37 +00:00
|
|
|
match options(args.as_slice()).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>,
|
2014-05-09 00:12:57 +00:00
|
|
|
files: Box<Vec<Path>>
|
2013-12-23 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 09:20:52 +00:00
|
|
|
fn options(args: &[String]) -> Result<Options, ()> {
|
2014-05-30 08:35:54 +00:00
|
|
|
let opts = [
|
2013-12-23 17:12:26 +00:00
|
|
|
optflag("a", "append", "append to the given FILEs, do not overwrite"),
|
|
|
|
optflag("i", "ignore-interrupts", "ignore interrupt signals"),
|
2014-01-07 00:35:24 +00:00
|
|
|
optflag("h", "help", "display this help and exit"),
|
|
|
|
optflag("V", "version", "output version information and exit"),
|
|
|
|
];
|
|
|
|
|
2014-05-28 06:33:39 +00:00
|
|
|
let args: Vec<String> = args.iter().map(|x| x.to_string()).collect();
|
2014-05-17 10:32:14 +00:00
|
|
|
|
2014-11-19 20:55:25 +00:00
|
|
|
getopts(args.tail(), &opts).map_err(|e| format!("{}", e)).and_then(|m| {
|
2013-12-23 17:12:26 +00:00
|
|
|
let version = format!("{} {}", NAME, VERSION);
|
2014-07-20 01:13:55 +00:00
|
|
|
let program = args[0].as_slice();
|
2013-12-23 17:12:26 +00:00
|
|
|
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{}",
|
2014-11-19 20:55:25 +00:00
|
|
|
version, program, arguments, usage(brief, &opts),
|
2013-12-23 17:12:26 +00:00
|
|
|
comment);
|
2014-09-23 22:01:40 +00:00
|
|
|
let mut names = m.free.clone().into_iter().collect::<Vec<String>>();
|
|
|
|
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 {
|
2014-05-28 06:33:39 +00:00
|
|
|
program: program.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-01-10 17:15:04 +00:00
|
|
|
files: Box::new(names.iter().map(|name| Path::new(name.clone())).collect())
|
2013-12-23 17:12:26 +00:00
|
|
|
})
|
2014-05-17 10:32:14 +00:00
|
|
|
}).map_err(|message| warn(message.as_slice()))
|
2013-12-23 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 20:04:11 +00:00
|
|
|
fn exec(options: Options) -> Result<(), ()> {
|
2013-12-23 17:12:26 +00:00
|
|
|
match options.print_and_exit {
|
2014-05-23 12:28:40 +00:00
|
|
|
Some(text) => Ok(println(text.as_slice())),
|
2013-12-23 17:12:26 +00:00
|
|
|
None => tee(options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:04:11 +00:00
|
|
|
fn tee(options: Options) -> Result<(), ()> {
|
2014-03-22 08:18:52 +00:00
|
|
|
let writers = options.files.iter().map(|path| open(path, options.append)).collect();
|
2014-02-07 06:39:07 +00:00
|
|
|
let output = &mut MultiWriter::new(writers);
|
2015-01-10 17:15:04 +00:00
|
|
|
let input = &mut NamedReader { inner: Box::new(stdin()) as Box<Reader> };
|
2014-02-07 06:39:07 +00:00
|
|
|
if copy(input, output).is_err() || output.flush().is_err() {
|
|
|
|
Err(())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2013-12-23 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-28 00:49:44 +00:00
|
|
|
|
2014-09-10 06:26:42 +00:00
|
|
|
fn open(path: &Path, append: bool) -> Box<Writer+'static> {
|
2014-02-07 06:39:07 +00:00
|
|
|
let inner = if *path == Path::new("-") {
|
2015-01-10 17:15:04 +00:00
|
|
|
Box::new(stdout()) as Box<Writer>
|
2013-12-28 01:23:13 +00:00
|
|
|
} else {
|
|
|
|
let mode = if append { Append } else { Truncate };
|
2013-12-27 20:04:11 +00:00
|
|
|
match File::open_mode(path, mode, Write) {
|
2015-01-10 17:15:04 +00:00
|
|
|
Ok(file) => Box::new(file) as Box<Writer>,
|
|
|
|
Err(_) => Box::new(NullWriter) as Box<Writer>
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
2014-02-07 06:39:07 +00:00
|
|
|
};
|
2015-01-10 17:15:04 +00:00
|
|
|
Box::new(NamedWriter { inner: inner, path: Box::new(path.clone()) }) as Box<Writer>
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct NamedWriter {
|
2014-09-10 06:26:42 +00:00
|
|
|
inner: Box<Writer+'static>,
|
2014-05-09 00:12:57 +00:00
|
|
|
path: Box<Path>
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for NamedWriter {
|
2014-02-07 06:39:07 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-07-20 01:13:55 +00:00
|
|
|
with_path(&*self.path.clone(), || {
|
2014-02-07 06:39:07 +00:00
|
|
|
let val = self.inner.write(buf);
|
|
|
|
if val.is_err() {
|
2015-01-10 17:15:04 +00:00
|
|
|
self.inner = Box::new(NullWriter) as Box<Writer>;
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
|
|
|
val
|
|
|
|
})
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
fn flush(&mut self) -> IoResult<()> {
|
2014-07-20 01:13:55 +00:00
|
|
|
with_path(&*self.path.clone(), || {
|
2014-02-07 06:39:07 +00:00
|
|
|
let val = self.inner.flush();
|
|
|
|
if val.is_err() {
|
2015-01-10 17:15:04 +00:00
|
|
|
self.inner = Box::new(NullWriter) as Box<Writer>;
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
|
|
|
val
|
|
|
|
})
|
2013-12-28 01:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:04:11 +00:00
|
|
|
struct NamedReader {
|
2014-09-10 06:26:42 +00:00
|
|
|
inner: Box<Reader+'static>
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for NamedReader {
|
2015-01-10 17:15:04 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
|
2014-02-07 06:39:07 +00:00
|
|
|
with_path(&Path::new("stdin"), || {
|
|
|
|
self.inner.read(buf)
|
|
|
|
})
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 17:15:04 +00:00
|
|
|
fn with_path<F, T>(path: &Path, mut cb: F) -> IoResult<T> where F: FnMut() -> IoResult<T> {
|
2014-02-07 06:39:07 +00:00
|
|
|
match cb() {
|
2014-07-09 08:29:50 +00:00
|
|
|
Err(f) => { warn(format!("{}: {}", path.display(), f.to_string()).as_slice()); Err(f) }
|
2014-02-07 06:39:07 +00:00
|
|
|
okay => okay
|
|
|
|
}
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-28 00:49:44 +00:00
|
|
|
fn warn(message: &str) {
|
2014-10-21 11:36:10 +00:00
|
|
|
error!("{}: {}", os::args()[0], message);
|
2013-12-28 00:49:44 +00:00
|
|
|
}
|