2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "cat"]
|
2015-01-10 00:16:05 +00:00
|
|
|
#![feature(box_syntax, unsafe_destructor)]
|
2013-08-04 23:54:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* last synced with: cat (GNU coreutils) 8.13 */
|
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate getopts;
|
2013-08-04 23:54:10 +00:00
|
|
|
|
2014-03-19 16:35:33 +00:00
|
|
|
use std::io::{print, File};
|
2014-05-27 21:48:37 +00:00
|
|
|
use std::io::stdio::{stdout_raw, stdin_raw, stderr};
|
|
|
|
use std::io::{IoResult};
|
|
|
|
use std::ptr::{copy_nonoverlapping_memory};
|
2013-08-04 23:54:10 +00:00
|
|
|
|
2015-01-10 13:07:39 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> isize {
|
2014-07-20 01:13:55 +00:00
|
|
|
let program = args[0].as_slice();
|
2014-05-27 21:48:37 +00:00
|
|
|
let opts = [
|
2014-02-07 06:39:07 +00:00
|
|
|
getopts::optflag("A", "show-all", "equivalent to -vET"),
|
2014-05-27 21:48:37 +00:00
|
|
|
getopts::optflag("b", "number-nonblank",
|
|
|
|
"number nonempty output lines, overrides -n"),
|
2014-02-07 06:39:07 +00:00
|
|
|
getopts::optflag("e", "", "equivalent to -vE"),
|
|
|
|
getopts::optflag("E", "show-ends", "display $ at end of each line"),
|
|
|
|
getopts::optflag("n", "number", "number all output lines"),
|
|
|
|
getopts::optflag("s", "squeeze-blank", "suppress repeated empty output lines"),
|
|
|
|
getopts::optflag("t", "", "equivalent to -vT"),
|
|
|
|
getopts::optflag("T", "show-tabs", "display TAB characters as ^I"),
|
2014-05-27 21:48:37 +00:00
|
|
|
getopts::optflag("v", "show-nonprinting",
|
|
|
|
"use ^ and M- notation, except for LF (\\n) and TAB (\\t)"),
|
2014-02-07 06:39:07 +00:00
|
|
|
getopts::optflag("h", "help", "display this help and exit"),
|
|
|
|
getopts::optflag("V", "version", "output version information and exit"),
|
2013-08-04 23:54:10 +00:00
|
|
|
];
|
2014-11-19 20:55:25 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), &opts) {
|
2013-08-04 23:54:10 +00:00
|
|
|
Ok(m) => m,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(f) => panic!("Invalid options\n{}", f)
|
2013-08-04 23:54:10 +00:00
|
|
|
};
|
2013-10-22 12:22:10 +00:00
|
|
|
if matches.opt_present("help") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("cat 1.0.0");
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2014-11-21 09:09:43 +00:00
|
|
|
println!(" {0} [OPTION]... [FILE]...", program);
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("");
|
2014-05-27 21:48:37 +00:00
|
|
|
print(getopts::usage("Concatenate FILE(s), or standard input, to \
|
2014-11-19 20:55:25 +00:00
|
|
|
standard output.", &opts).as_slice());
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("");
|
|
|
|
println!("With no FILE, or when FILE is -, read standard input.");
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
2013-10-22 12:22:10 +00:00
|
|
|
if matches.opt_present("version") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("cat 1.0.0");
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 20:50:37 +00:00
|
|
|
let mut number_mode = NumberingMode::NumberNone;
|
2014-05-27 21:48:37 +00:00
|
|
|
if matches.opt_present("n") {
|
2014-11-19 20:50:37 +00:00
|
|
|
number_mode = NumberingMode::NumberAll;
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
2014-05-27 21:48:37 +00:00
|
|
|
if matches.opt_present("b") {
|
2014-11-19 20:50:37 +00:00
|
|
|
number_mode = NumberingMode::NumberNonEmpty;
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
2014-11-19 20:55:25 +00:00
|
|
|
let show_nonprint = matches.opts_present(&["A".to_string(), "e".to_string(),
|
2014-05-27 21:48:37 +00:00
|
|
|
"t".to_string(), "v".to_string()]);
|
2014-11-19 20:55:25 +00:00
|
|
|
let show_ends = matches.opts_present(&["E".to_string(), "A".to_string(),
|
2014-05-27 21:48:37 +00:00
|
|
|
"e".to_string()]);
|
2014-11-19 20:55:25 +00:00
|
|
|
let show_tabs = matches.opts_present(&["A".to_string(), "T".to_string(),
|
2014-05-27 21:48:37 +00:00
|
|
|
"t".to_string()]);
|
|
|
|
let squeeze_blank = matches.opt_present("s");
|
2013-08-04 23:54:10 +00:00
|
|
|
let mut files = matches.free;
|
|
|
|
if files.is_empty() {
|
2014-05-27 21:48:37 +00:00
|
|
|
files.push("-".to_string());
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exec(files, number_mode, show_nonprint, show_ends, show_tabs, squeeze_blank);
|
2014-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
0
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 12:56:41 +00:00
|
|
|
#[derive(Eq, PartialEq)]
|
2014-05-27 21:48:37 +00:00
|
|
|
enum NumberingMode {
|
2013-08-04 23:54:10 +00:00
|
|
|
NumberNone,
|
|
|
|
NumberNonEmpty,
|
|
|
|
NumberAll,
|
|
|
|
}
|
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
fn write_lines(files: Vec<String>, number: NumberingMode, squeeze_blank: bool,
|
|
|
|
show_ends: bool) {
|
2013-08-04 23:54:10 +00:00
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
let mut line_counter: uint = 1;
|
2013-08-04 23:54:10 +00:00
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
for path in files.iter() {
|
|
|
|
let (mut reader, interactive) = match open(path.as_slice()) {
|
|
|
|
Some(f) => f,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
2015-01-08 12:58:23 +00:00
|
|
|
let mut in_buf = [0; 1024 * 31];
|
|
|
|
let mut out_buf = [0; 1024 * 64];
|
2014-05-27 21:48:37 +00:00
|
|
|
let mut writer = UnsafeWriter::new(out_buf.as_mut_slice(), stdout_raw());
|
|
|
|
let mut at_line_start = true;
|
|
|
|
loop {
|
2014-11-19 20:55:25 +00:00
|
|
|
let n = match reader.read(&mut in_buf) {
|
2014-05-27 21:48:37 +00:00
|
|
|
Ok(n) if n != 0 => n,
|
|
|
|
_ => break,
|
|
|
|
};
|
|
|
|
let in_buf = in_buf.slice_to(n);
|
|
|
|
let mut buf_pos = range(0, n);
|
|
|
|
loop {
|
|
|
|
writer.possibly_flush();
|
|
|
|
let pos = match buf_pos.next() {
|
|
|
|
Some(p) => p,
|
|
|
|
None => break,
|
|
|
|
};
|
|
|
|
if in_buf[pos] == '\n' as u8 {
|
|
|
|
if !at_line_start || !squeeze_blank {
|
2014-11-19 20:50:37 +00:00
|
|
|
if at_line_start && number == NumberingMode::NumberAll {
|
2014-11-21 09:09:43 +00:00
|
|
|
(write!(&mut writer, "{0:6}\t", line_counter)).unwrap();
|
2014-05-27 21:48:37 +00:00
|
|
|
line_counter += 1;
|
|
|
|
}
|
|
|
|
if show_ends {
|
|
|
|
writer.write_u8('$' as u8).unwrap();
|
|
|
|
}
|
|
|
|
writer.write_u8('\n' as u8).unwrap();
|
|
|
|
if interactive {
|
|
|
|
writer.flush().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
at_line_start = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-19 20:50:37 +00:00
|
|
|
if at_line_start && number != NumberingMode::NumberNone {
|
2014-11-21 09:09:43 +00:00
|
|
|
(write!(&mut writer, "{0:6}\t", line_counter)).unwrap();
|
2014-05-27 21:48:37 +00:00
|
|
|
line_counter += 1;
|
|
|
|
}
|
2014-08-17 08:27:22 +00:00
|
|
|
match in_buf.slice_from(pos).iter().position(|c| *c == '\n' as u8) {
|
2014-05-27 21:48:37 +00:00
|
|
|
Some(p) => {
|
|
|
|
writer.write(in_buf.slice(pos, pos + p)).unwrap();
|
|
|
|
if show_ends {
|
|
|
|
writer.write_u8('$' as u8).unwrap();
|
|
|
|
}
|
|
|
|
writer.write_u8('\n' as u8).unwrap();
|
|
|
|
if interactive {
|
|
|
|
writer.flush().unwrap();
|
|
|
|
}
|
|
|
|
buf_pos = range(pos + p + 1, n);
|
|
|
|
at_line_start = true;
|
|
|
|
},
|
|
|
|
None => {
|
2014-08-17 08:27:22 +00:00
|
|
|
writer.write(in_buf.slice_from(pos)).unwrap();
|
2014-05-27 21:48:37 +00:00
|
|
|
at_line_start = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
fn write_bytes(files: Vec<String>, number: NumberingMode, squeeze_blank: bool,
|
|
|
|
show_ends: bool, show_nonprint: bool, show_tabs: bool) {
|
2013-08-04 23:54:10 +00:00
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
let mut line_counter: uint = 1;
|
2013-10-22 13:31:51 +00:00
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
for path in files.iter() {
|
|
|
|
let (mut reader, interactive) = match open(path.as_slice()) {
|
|
|
|
Some(f) => f,
|
|
|
|
None => continue,
|
|
|
|
};
|
2013-08-04 23:54:10 +00:00
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
// Flush all 1024 iterations.
|
2014-06-25 11:12:43 +00:00
|
|
|
let mut flush_counter = range(0u, 1024);
|
2014-05-27 21:48:37 +00:00
|
|
|
|
2015-01-08 12:58:23 +00:00
|
|
|
let mut in_buf = [0; 1024 * 32];
|
|
|
|
let mut out_buf = [0; 1024 * 64];
|
2014-05-27 21:48:37 +00:00
|
|
|
let mut writer = UnsafeWriter::new(out_buf.as_mut_slice(), stdout_raw());
|
|
|
|
let mut at_line_start = true;
|
|
|
|
loop {
|
2014-11-19 20:55:25 +00:00
|
|
|
let n = match reader.read(&mut in_buf) {
|
2014-05-27 21:48:37 +00:00
|
|
|
Ok(n) if n != 0 => n,
|
|
|
|
_ => break,
|
|
|
|
};
|
|
|
|
for &byte in in_buf.slice_to(n).iter() {
|
|
|
|
if flush_counter.next().is_none() {
|
|
|
|
writer.possibly_flush();
|
2014-06-25 11:12:43 +00:00
|
|
|
flush_counter = range(0u, 1024);
|
2014-05-27 21:48:37 +00:00
|
|
|
}
|
|
|
|
if byte == '\n' as u8 {
|
|
|
|
if !at_line_start || !squeeze_blank {
|
2014-11-19 20:50:37 +00:00
|
|
|
if at_line_start && number == NumberingMode::NumberAll {
|
2014-11-21 09:09:43 +00:00
|
|
|
(write!(&mut writer, "{0:6}\t", line_counter)).unwrap();
|
2014-05-27 21:48:37 +00:00
|
|
|
line_counter += 1;
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
2014-05-27 21:48:37 +00:00
|
|
|
if show_ends {
|
|
|
|
writer.write_u8('$' as u8).unwrap();
|
|
|
|
}
|
|
|
|
writer.write_u8('\n' as u8).unwrap();
|
|
|
|
if interactive {
|
|
|
|
writer.flush().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
at_line_start = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-19 20:50:37 +00:00
|
|
|
if at_line_start && number != NumberingMode::NumberNone {
|
2014-11-21 09:09:43 +00:00
|
|
|
(write!(&mut writer, "{0:6}\t", line_counter)).unwrap();
|
2014-05-27 21:48:37 +00:00
|
|
|
line_counter += 1;
|
|
|
|
at_line_start = false;
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
2014-05-27 21:48:37 +00:00
|
|
|
// This code is slow because of the many branches. cat in glibc avoids
|
|
|
|
// this by having the whole loop inside show_nonprint.
|
|
|
|
if byte == '\t' as u8 {
|
|
|
|
if show_tabs {
|
|
|
|
writer.write_str("^I")
|
|
|
|
} else {
|
|
|
|
writer.write_u8(byte)
|
|
|
|
}
|
|
|
|
} else if show_nonprint {
|
|
|
|
let byte = match byte {
|
2014-10-02 14:42:30 +00:00
|
|
|
128 ... 255 => {
|
2014-05-27 21:48:37 +00:00
|
|
|
writer.write_str("M-").unwrap();
|
|
|
|
byte - 128
|
|
|
|
},
|
|
|
|
_ => byte,
|
|
|
|
};
|
|
|
|
match byte {
|
2014-11-19 20:55:25 +00:00
|
|
|
0 ... 31 => writer.write(&['^' as u8, byte + 64]),
|
|
|
|
127 => writer.write(&['^' as u8, byte - 64]),
|
2014-10-02 14:42:30 +00:00
|
|
|
_ => writer.write_u8(byte),
|
2014-05-27 21:48:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writer.write_u8(byte)
|
|
|
|
}.unwrap();
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 21:48:37 +00:00
|
|
|
}
|
2013-08-04 23:54:10 +00:00
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
fn write_fast(files: Vec<String>) {
|
2014-03-19 16:42:34 +00:00
|
|
|
let mut writer = stdout_raw();
|
2015-01-08 12:58:23 +00:00
|
|
|
let mut in_buf = [0; 1024 * 64];
|
2014-05-27 21:48:37 +00:00
|
|
|
|
2013-08-04 23:54:10 +00:00
|
|
|
for path in files.iter() {
|
2014-05-27 21:48:37 +00:00
|
|
|
let (mut reader, _) = match open(path.as_slice()) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => continue,
|
2013-08-04 23:54:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
2014-11-19 20:55:25 +00:00
|
|
|
match reader.read(&mut in_buf) {
|
2014-02-04 07:14:26 +00:00
|
|
|
Ok(n) if n != 0 => {
|
2014-05-27 21:48:37 +00:00
|
|
|
// This interface is completely broken.
|
|
|
|
writer.write(in_buf.slice_to(n)).unwrap();
|
|
|
|
},
|
|
|
|
_ => break
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
fn exec(files: Vec<String>, number: NumberingMode, show_nonprint: bool,
|
|
|
|
show_ends: bool, show_tabs: bool, squeeze_blank: bool) {
|
|
|
|
|
|
|
|
if show_nonprint || show_tabs {
|
|
|
|
write_bytes(files, number, squeeze_blank, show_ends, show_nonprint, show_tabs);
|
2014-11-19 20:50:37 +00:00
|
|
|
} else if number != NumberingMode::NumberNone || squeeze_blank || show_ends {
|
2014-05-27 21:48:37 +00:00
|
|
|
write_lines(files, number, squeeze_blank, show_ends);
|
|
|
|
} else {
|
|
|
|
write_fast(files);
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
2014-05-27 21:48:37 +00:00
|
|
|
}
|
2013-08-04 23:54:10 +00:00
|
|
|
|
2014-05-27 21:48:37 +00:00
|
|
|
fn open(path: &str) -> Option<(Box<Reader>, bool)> {
|
|
|
|
if path == "-" {
|
|
|
|
let stdin = stdin_raw();
|
|
|
|
let interactive = stdin.isatty();
|
|
|
|
return Some((box stdin as Box<Reader>, interactive));
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
2014-05-27 21:48:37 +00:00
|
|
|
|
|
|
|
match File::open(&std::path::Path::new(path)) {
|
2014-06-12 04:41:53 +00:00
|
|
|
Ok(f) => Some((box f as Box<Reader>, false)),
|
2014-05-27 21:48:37 +00:00
|
|
|
Err(e) => {
|
2014-11-22 06:27:54 +00:00
|
|
|
(writeln!(&mut stderr(), "cat: {0}: {1}", path, e.to_string())).unwrap();
|
2014-06-12 04:41:53 +00:00
|
|
|
None
|
2014-05-27 21:48:37 +00:00
|
|
|
},
|
2014-06-12 04:41:53 +00:00
|
|
|
}
|
2013-08-04 23:54:10 +00:00
|
|
|
}
|
2014-05-27 21:48:37 +00:00
|
|
|
|
|
|
|
struct UnsafeWriter<'a, W> {
|
|
|
|
inner: W,
|
|
|
|
buf: &'a mut [u8],
|
|
|
|
pos: uint,
|
|
|
|
threshold: uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, W: Writer> UnsafeWriter<'a, W> {
|
|
|
|
fn new(buf: &'a mut [u8], inner: W) -> UnsafeWriter<'a, W> {
|
|
|
|
let threshold = buf.len()/2;
|
|
|
|
UnsafeWriter {
|
|
|
|
inner: inner,
|
|
|
|
buf: buf,
|
|
|
|
pos: 0,
|
|
|
|
threshold: threshold,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush_buf(&mut self) -> IoResult<()> {
|
|
|
|
if self.pos != 0 {
|
|
|
|
let ret = self.inner.write(self.buf.slice_to(self.pos));
|
|
|
|
self.pos = 0;
|
|
|
|
ret
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn possibly_flush(&mut self) {
|
|
|
|
if self.pos > self.threshold {
|
|
|
|
self.inner.write(self.buf.slice_to(self.pos)).unwrap();
|
|
|
|
self.pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
fn fail() -> ! {
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!("assertion failed");
|
2014-05-27 21:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, W: Writer> Writer for UnsafeWriter<'a, W> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-09-17 15:11:39 +00:00
|
|
|
let dst = self.buf.slice_from_mut(self.pos);
|
2014-05-27 21:48:37 +00:00
|
|
|
if buf.len() > dst.len() {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
copy_nonoverlapping_memory(dst.as_mut_ptr(), buf.as_ptr(), buf.len())
|
|
|
|
}
|
|
|
|
self.pos += buf.len();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> IoResult<()> {
|
|
|
|
self.flush_buf().and_then(|()| self.inner.flush())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
|
|
|
impl<'a, W: Writer> Drop for UnsafeWriter<'a, W> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = self.flush_buf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-19 16:48:47 +00:00
|
|
|
/* vim: set ai ts=4 sw=4 sts=4 et : */
|