2016-03-11 03:48:44 +02:00
|
|
|
#![allow(dead_code, unused_variables)]
|
2016-02-27 18:07:26 +02:00
|
|
|
|
|
|
|
extern crate docopt;
|
2016-06-20 22:53:48 +02:00
|
|
|
extern crate grep;
|
2016-03-11 03:48:44 +02:00
|
|
|
extern crate memchr;
|
|
|
|
extern crate memmap;
|
2016-02-27 18:07:26 +02:00
|
|
|
extern crate regex;
|
2016-03-11 03:48:44 +02:00
|
|
|
extern crate regex_syntax as syntax;
|
2016-02-27 18:07:26 +02:00
|
|
|
extern crate rustc_serialize;
|
|
|
|
|
|
|
|
const USAGE: &'static str = "
|
|
|
|
Usage: rep [options] <pattern> [<file> ...]
|
2016-03-31 04:24:59 +02:00
|
|
|
|
|
|
|
Options:
|
|
|
|
-c, --count Suppress normal output and show count of matches.
|
2016-02-27 18:07:26 +02:00
|
|
|
";
|
|
|
|
|
|
|
|
use std::error::Error;
|
2016-06-20 22:53:48 +02:00
|
|
|
use std::io::{self, Write};
|
2016-02-27 18:07:26 +02:00
|
|
|
use std::process;
|
|
|
|
use std::result;
|
|
|
|
|
|
|
|
use docopt::Docopt;
|
|
|
|
|
2016-06-20 22:53:48 +02:00
|
|
|
use grep::{Grep, GrepBuilder};
|
2016-03-11 03:48:44 +02:00
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, Box<Error + Send + Sync>>;
|
2016-02-27 18:07:26 +02:00
|
|
|
|
|
|
|
#[derive(RustcDecodable)]
|
|
|
|
struct Args {
|
|
|
|
arg_pattern: String,
|
|
|
|
arg_file: Vec<String>,
|
2016-03-31 04:24:59 +02:00
|
|
|
flag_count: bool,
|
2016-02-27 18:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = Docopt::new(USAGE).and_then(|d| d.decode())
|
|
|
|
.unwrap_or_else(|e| e.exit());
|
|
|
|
match run(&args) {
|
|
|
|
Ok(count) if count == 0 => process::exit(1),
|
|
|
|
Ok(_) => process::exit(0),
|
|
|
|
Err(err) => {
|
|
|
|
let _ = writeln!(&mut io::stderr(), "{}", err);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(args: &Args) -> Result<u64> {
|
2016-03-11 03:48:44 +02:00
|
|
|
if args.arg_file.is_empty() {
|
2016-06-20 22:53:48 +02:00
|
|
|
unimplemented!()
|
2016-03-11 03:48:44 +02:00
|
|
|
} else {
|
2016-03-30 03:21:34 +02:00
|
|
|
let searcher =
|
2016-06-20 22:53:48 +02:00
|
|
|
try!(GrepBuilder::new(&args.arg_pattern).create());
|
|
|
|
run_mmap(args, &searcher)
|
2016-03-11 03:48:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:53:48 +02:00
|
|
|
fn run_mmap(args: &Args, searcher: &Grep) -> Result<u64> {
|
|
|
|
for m in searcher.iter(text) {
|
|
|
|
if !args.flag_count {
|
|
|
|
try!(wtr.write(&text[m.start()..m.end()]));
|
|
|
|
try!(wtr.write(b"\n"));
|
|
|
|
}
|
2016-03-30 03:21:34 +02:00
|
|
|
count += 1;
|
|
|
|
}
|
2016-04-04 03:22:09 +02:00
|
|
|
Ok(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
fn run_mmap_count_only(args: &Args, searcher: &LineSearcher) -> Result<u64> {
|
|
|
|
use memmap::{Mmap, Protection};
|
|
|
|
|
|
|
|
assert!(args.arg_file.len() == 1);
|
|
|
|
let mut wtr = io::BufWriter::new(io::stdout());
|
|
|
|
let mmap = try!(Mmap::open_path(&args.arg_file[0], Protection::Read));
|
|
|
|
let text = unsafe { mmap.as_slice() };
|
|
|
|
let count = searcher.search(text).last().map_or(0, |m| m.count + 1);
|
|
|
|
try!(writeln!(wtr, "{}", count));
|
2016-03-30 03:21:34 +02:00
|
|
|
Ok(count)
|
|
|
|
}
|