2013-12-27 19:56:15 +00:00
|
|
|
#[crate_id(name="tee", vers="1.0.0", author="Aleksander Bielawski")];
|
|
|
|
#[license="MIT"];
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern mod extra;
|
2014-02-07 06:39:07 +00:00
|
|
|
extern mod getopts;
|
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};
|
2013-12-23 17:12:26 +00:00
|
|
|
use std::os::{args, set_exit_status};
|
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";
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
match options(args()).and_then(exec) {
|
2013-12-27 20:04:11 +00:00
|
|
|
Ok(_) => set_exit_status(0),
|
|
|
|
Err(_) => set_exit_status(1)
|
2013-12-23 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
program: ~str,
|
|
|
|
append: bool,
|
|
|
|
ignore_interrupts: bool,
|
|
|
|
print_and_exit: Option<~str>,
|
|
|
|
files: ~[Path]
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:04:11 +00:00
|
|
|
fn options(args: &[~str]) -> Result<Options, ()> {
|
2013-12-23 17:12:26 +00:00
|
|
|
let opts = ~[
|
|
|
|
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"),
|
|
|
|
];
|
|
|
|
|
2013-12-23 17:12:26 +00:00
|
|
|
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()))
|
|
|
|
})
|
2013-12-27 20:04:11 +00:00
|
|
|
}).map_err(|message| warn(message))
|
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 {
|
2013-12-27 20:04:11 +00:00
|
|
|
Some(text) => Ok(println(text)),
|
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-02-07 06:39:07 +00:00
|
|
|
let writers = options.files.map(|path| open(path, options.append));
|
|
|
|
let output = &mut MultiWriter::new(writers);
|
|
|
|
let input = &mut NamedReader { inner: ~stdin() as ~Reader };
|
|
|
|
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
|
|
|
|
2013-12-28 01:23:13 +00:00
|
|
|
fn open(path: &Path, append: bool) -> ~Writer {
|
2014-02-07 06:39:07 +00:00
|
|
|
let inner = if *path == Path::new("-") {
|
2013-12-28 01:23:13 +00:00
|
|
|
~stdout() as ~Writer
|
|
|
|
} else {
|
|
|
|
let mode = if append { Append } else { Truncate };
|
2013-12-27 20:04:11 +00:00
|
|
|
match File::open_mode(path, mode, Write) {
|
2014-02-07 06:39:07 +00:00
|
|
|
Ok(file) => ~file as ~Writer,
|
|
|
|
Err(_) => ~NullWriter as ~Writer
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
2014-02-07 06:39:07 +00:00
|
|
|
};
|
2013-12-27 20:04:11 +00:00
|
|
|
~NamedWriter { inner: inner, path: ~path.clone() } as ~Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NamedWriter {
|
2014-02-07 06:39:07 +00:00
|
|
|
inner: ~Writer,
|
|
|
|
path: ~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-02-15 04:50:03 +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() {
|
|
|
|
self.inner = ~NullWriter as ~Writer;
|
|
|
|
}
|
|
|
|
val
|
|
|
|
})
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
fn flush(&mut self) -> IoResult<()> {
|
2014-02-15 04:50:03 +00:00
|
|
|
with_path(self.path.clone(), || {
|
2014-02-07 06:39:07 +00:00
|
|
|
let val = self.inner.flush();
|
|
|
|
if val.is_err() {
|
|
|
|
self.inner = ~NullWriter as ~Writer;
|
|
|
|
}
|
|
|
|
val
|
|
|
|
})
|
2013-12-28 01:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:04:11 +00:00
|
|
|
struct NamedReader {
|
2014-02-07 06:39:07 +00:00
|
|
|
inner: ~Reader
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for NamedReader {
|
2014-02-07 06:39:07 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
|
|
|
with_path(&Path::new("stdin"), || {
|
|
|
|
self.inner.read(buf)
|
|
|
|
})
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
fn with_path<T>(path: &Path, cb: || -> IoResult<T>) -> IoResult<T> {
|
|
|
|
match cb() {
|
|
|
|
Err(f) => { warn(format!("{}: {}", path.display(), f.to_str())); Err(f) }
|
|
|
|
okay => okay
|
|
|
|
}
|
2013-12-27 20:04:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-28 00:49:44 +00:00
|
|
|
fn warn(message: &str) {
|
|
|
|
error!("{}: {}", args()[0], message);
|
|
|
|
}
|