mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
commit
56d6bb906c
6 changed files with 84 additions and 121 deletions
3
Makefile
3
Makefile
|
@ -22,7 +22,7 @@ endef
|
||||||
|
|
||||||
# Test exe built rules
|
# Test exe built rules
|
||||||
define TEST_BUILD
|
define TEST_BUILD
|
||||||
test_$(1): tmp/$(1)_test
|
test_$(1): tmp/$(1)_test build/$(1)
|
||||||
$(call command,tmp/$(1)_test)
|
$(call command,tmp/$(1)_test)
|
||||||
|
|
||||||
tmp/$(1)_test: $(1)/test.rs
|
tmp/$(1)_test: $(1)/test.rs
|
||||||
|
@ -49,4 +49,3 @@ $(foreach exe,$(EXES),$(eval $(call EXE_BUILD,$(exe))))
|
||||||
$(foreach test,$(TESTS),$(eval $(call TEST_BUILD,$(test))))
|
$(foreach test,$(TESTS),$(eval $(call TEST_BUILD,$(test))))
|
||||||
|
|
||||||
.PHONY: all test clean
|
.PHONY: all test clean
|
||||||
|
|
||||||
|
|
49
cat/cat.rs
49
cat/cat.rs
|
@ -15,7 +15,7 @@
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
|
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::{stdin, stderr, stdout, Writer, Reader};
|
use std::rt::io::{stdin, stderr, stdout, File, result};
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -37,8 +37,8 @@ fn main() {
|
||||||
let matches = match groups::getopts(args.tail(), opts) {
|
let matches = match groups::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
stderr().write_line("Invalid options");
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
stderr().write_line(f.to_err_msg());
|
"Invalid options\n{}", f.to_err_msg());
|
||||||
os::set_exit_status(1);
|
os::set_exit_status(1);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ fn main() {
|
||||||
println("cat 1.0.0");
|
println("cat 1.0.0");
|
||||||
println("");
|
println("");
|
||||||
println("Usage:");
|
println("Usage:");
|
||||||
println(format!(" {0:s} [OPTION]... [FILE]...", program));
|
println!(" {0:s} [OPTION]... [FILE]...", program);
|
||||||
println("");
|
println("");
|
||||||
print(groups::usage("Concatenate FILE(s), or standard input, to standard output.", opts));
|
print(groups::usage("Concatenate FILE(s), or standard input, to standard output.", opts));
|
||||||
println("");
|
println("");
|
||||||
|
@ -100,24 +100,28 @@ fn is_newline_char(byte: u8) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_ends: bool, show_tabs: bool, squeeze_blank: bool) {
|
pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_ends: bool, show_tabs: bool, squeeze_blank: bool) {
|
||||||
let writer = stdout();
|
let mut writer = stdout();
|
||||||
|
|
||||||
if NumberNone != number || show_nonprint || show_ends || show_tabs || squeeze_blank {
|
if NumberNone != number || show_nonprint || show_ends || show_tabs || squeeze_blank {
|
||||||
let mut counter: uint = 1;
|
let mut counter: uint = 1;
|
||||||
let is_numbering = number == NumberAll || number == NumberNonEmpty;
|
let is_numbering = number == NumberAll || number == NumberNonEmpty;
|
||||||
|
|
||||||
for path in files.iter() {
|
for path in files.iter() {
|
||||||
let reader = match open(path.to_owned()) {
|
let mut reader = match open(path.to_owned()) {
|
||||||
Some(f) => f,
|
Some(f) => f,
|
||||||
None => { continue }
|
None => { continue }
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut at_line_start = true;
|
let mut at_line_start = true;
|
||||||
|
let mut buf = [0, .. 2];
|
||||||
loop {
|
loop {
|
||||||
let buf = reader.read_bytes(2);
|
// reading from a TTY seems to raise a condition on
|
||||||
for byte in buf.iter() {
|
// EOF, rather than return Some(0) like a file.
|
||||||
|
match result(|| reader.read(buf)) {
|
||||||
|
Ok(Some(n)) if n != 0 => {
|
||||||
|
for byte in buf.slice_to(n).iter() {
|
||||||
if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) {
|
if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) {
|
||||||
writer.write_str(format!("{0:6u}\t", counter));
|
write!(&mut writer as &mut Writer, "{0:6u}\t", counter);
|
||||||
counter += 1;
|
counter += 1;
|
||||||
at_line_start = false;
|
at_line_start = false;
|
||||||
}
|
}
|
||||||
|
@ -145,9 +149,8 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
|
||||||
writer.write([*byte]);
|
writer.write([*byte]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if reader.eof() {
|
_ => break
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,31 +158,35 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut buf = [0, .. 100000];
|
||||||
// passthru mode
|
// passthru mode
|
||||||
for path in files.iter() {
|
for path in files.iter() {
|
||||||
let reader = match open(path.to_owned()) {
|
let mut reader = match open(path.to_owned()) {
|
||||||
Some(f) => f,
|
Some(f) => f,
|
||||||
None => { continue }
|
None => { continue }
|
||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
writer.write(reader.read_bytes(100000));
|
// reading from a TTY seems to raise a condition on EOF,
|
||||||
if reader.eof() {
|
// rather than return Some(0) like a file.
|
||||||
break;
|
match result(|| reader.read(buf)) {
|
||||||
|
Ok(Some(n)) if n != 0 => writer.write(buf.slice_to(n)),
|
||||||
|
_ => break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(path: ~str) -> Option<@Reader> {
|
fn open(path: ~str) -> Option<~Reader> {
|
||||||
if "-" == path {
|
if "-" == path {
|
||||||
return Some(stdin());
|
return Some(~stdin() as ~Reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
match std::io::file_reader(&std::path::Path::new(path.as_slice())) {
|
match result(|| File::open(&std::path::Path::new(path.as_slice()))) {
|
||||||
Ok(fd) => return Some(fd),
|
Ok(fd) => return Some(~fd as ~Reader),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
stderr().write_line(format!("cat: {0:s}: {1:s}", path, e));
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
|
"cat: {0:s}: {1:s}", path, e.to_str());
|
||||||
os::set_exit_status(1);
|
os::set_exit_status(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
81
cat/test.rs
81
cat/test.rs
|
@ -1,7 +1,4 @@
|
||||||
use std::rt::io::process::{Process, ProcessConfig, CreatePipe, Ignored};
|
use std::{run, str};
|
||||||
use std::rt::io::{Reader, Writer};
|
|
||||||
use std::rt::io::pipe::PipeStream;
|
|
||||||
use std::str;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
test_output_multi_files_print_all_chars();
|
test_output_multi_files_print_all_chars();
|
||||||
|
@ -9,71 +6,31 @@ fn main() {
|
||||||
test_stdin_number_non_blank();
|
test_stdin_number_non_blank();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_all(input: &mut Reader) -> ~str {
|
|
||||||
let mut ret = ~"";
|
|
||||||
let mut buf = [0, ..1024];
|
|
||||||
loop {
|
|
||||||
match input.read(buf) {
|
|
||||||
None => { break }
|
|
||||||
Some(n) => { ret = ret + str::from_utf8(buf.slice_to(n)); }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_output_multi_files_print_all_chars() {
|
fn test_output_multi_files_print_all_chars() {
|
||||||
let output = PipeStream::new().unwrap();
|
let prog = run::process_output("build/cat",
|
||||||
let io = ~[Ignored,
|
[~"cat/fixtures/alpha.txt", ~"cat/fixtures/256.txt",
|
||||||
CreatePipe(output, false, true)];
|
~"-A", ~"-n"]);
|
||||||
let args = ProcessConfig {
|
let out = str::from_utf8_owned(prog.output);
|
||||||
program: "build/cat",
|
assert_eq!(out,
|
||||||
args: [~"cat/fixtures/alpha.txt", ~"cat/fixtures/256.txt", ~"-A", ~"-n"],
|
~" 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^VM-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:M-;M-<M-=M->M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?");
|
||||||
env: None,
|
|
||||||
cwd: None,
|
|
||||||
io: io,
|
|
||||||
};
|
|
||||||
let mut p = Process::new(args).expect("proc fail");
|
|
||||||
let out = read_all(p.io[1].get_mut_ref() as &mut Reader);
|
|
||||||
assert_eq!(p.wait(), 0);
|
|
||||||
assert_eq!(out, ~" 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^VM-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:M-;M-<M-=M->M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_stdin_squeeze() {
|
fn test_stdin_squeeze() {
|
||||||
let input = PipeStream::new().unwrap();
|
let mut prog = run::Process::new("build/cat", [~"-A"], run::ProcessOptions::new());
|
||||||
let output = PipeStream::new().unwrap();
|
|
||||||
let io = ~[CreatePipe(input, true, false),
|
prog.input().write(bytes!("\x00\x01\x02"));
|
||||||
CreatePipe(output, false, true)];
|
prog.close_input();
|
||||||
let args = ProcessConfig {
|
|
||||||
program: "build/cat",
|
let out = str::from_utf8_owned(prog.finish_with_output().output);
|
||||||
args: [~"-A"],
|
|
||||||
env: None,
|
|
||||||
cwd: None,
|
|
||||||
io: io,
|
|
||||||
};
|
|
||||||
let mut p = Process::new(args).expect("proc fail");
|
|
||||||
p.io[0].get_mut_ref().write("\x00\x01\x02".as_bytes());
|
|
||||||
p.io[0] = None; // close stdin;
|
|
||||||
let out = read_all(p.io[1].get_mut_ref() as &mut Reader);
|
|
||||||
assert_eq!(p.wait(), 0);
|
|
||||||
assert_eq!(out, ~"^@^A^B");
|
assert_eq!(out, ~"^@^A^B");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_stdin_number_non_blank() {
|
fn test_stdin_number_non_blank() {
|
||||||
let input = PipeStream::new().unwrap();
|
let mut prog = run::Process::new("build/cat", [~"-b", ~"-"], run::ProcessOptions::new());
|
||||||
let output = PipeStream::new().unwrap();
|
|
||||||
let io = ~[CreatePipe(input, true, false),
|
prog.input().write(bytes!("\na\nb\n\n\nc"));
|
||||||
CreatePipe(output, false, true)];
|
prog.close_input();
|
||||||
let args = ProcessConfig {
|
|
||||||
program: "build/cat",
|
let out = str::from_utf8_owned(prog.finish_with_output().output);
|
||||||
args: [~"-b", ~"-"],
|
|
||||||
env: None,
|
|
||||||
cwd: None,
|
|
||||||
io: io,
|
|
||||||
};
|
|
||||||
let mut p = Process::new(args).expect("proc fail");
|
|
||||||
p.io[0].get_mut_ref().write("\na\nb\n\n\nc".as_bytes());
|
|
||||||
p.io[0] = None; // close stdin;
|
|
||||||
let out = read_all(p.io[1].get_mut_ref() as &mut Reader);
|
|
||||||
assert_eq!(p.wait(), 0);
|
|
||||||
assert_eq!(out, ~"\n 1\ta\n 2\tb\n\n\n 3\tc");
|
assert_eq!(out, ~"\n 1\ta\n 2\tb\n\n\n 3\tc");
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
|
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::stderr;
|
use std::rt::io::stderr;
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -28,8 +28,8 @@ fn main() {
|
||||||
let matches = match groups::getopts(args.tail(), opts) {
|
let matches = match groups::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
stderr().write_line("Invalid options");
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
stderr().write_line(f.to_err_msg());
|
"Invalid options\n{}", f.to_err_msg());
|
||||||
os::set_exit_status(1);
|
os::set_exit_status(1);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ fn main() {
|
||||||
println("printenv 1.0.0");
|
println("printenv 1.0.0");
|
||||||
println("");
|
println("");
|
||||||
println("Usage:");
|
println("Usage:");
|
||||||
println(format!(" {0:s} [VARIABLE]... [OPTION]...", program));
|
println!(" {0:s} [VARIABLE]... [OPTION]...", program);
|
||||||
println("");
|
println("");
|
||||||
print(groups::usage("Prints the given environment VARIABLE(s), otherwise prints them all.", opts));
|
print(groups::usage("Prints the given environment VARIABLE(s), otherwise prints them all.", opts));
|
||||||
return;
|
return;
|
||||||
|
@ -59,7 +59,7 @@ pub fn exec(args: ~[~str], separator: &str) {
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
let vars = os::env();
|
let vars = os::env();
|
||||||
for (env_var, value) in vars.move_iter() {
|
for (env_var, value) in vars.move_iter() {
|
||||||
print(format!("{0:s}={1:s}", env_var, value));
|
print!("{0:s}={1:s}", env_var, value);
|
||||||
print(separator);
|
print(separator);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -48,7 +48,7 @@ fn main() {
|
||||||
];
|
];
|
||||||
let matches = match groups::getopts(args.tail(), opts) {
|
let matches = match groups::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => fail2!(f.to_err_msg()),
|
Err(f) => fail!(f.to_err_msg()),
|
||||||
};
|
};
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
println("whoami 1.0.0");
|
println("whoami 1.0.0");
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
|
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::stderr;
|
use std::rt::io::stderr;
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -27,8 +27,8 @@ fn main() {
|
||||||
let matches = match groups::getopts(args.tail(), opts) {
|
let matches = match groups::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
stderr().write_line("Invalid options");
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
stderr().write_line(f.to_err_msg());
|
"Invalid options\n{}", f.to_err_msg());
|
||||||
os::set_exit_status(1);
|
os::set_exit_status(1);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ fn main() {
|
||||||
println("yes 1.0.0");
|
println("yes 1.0.0");
|
||||||
println("");
|
println("");
|
||||||
println("Usage:");
|
println("Usage:");
|
||||||
println(format!(" {0:s} [STRING]... [OPTION]...", program));
|
println!(" {0:s} [STRING]... [OPTION]...", program);
|
||||||
println("");
|
println("");
|
||||||
print(groups::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", opts));
|
print(groups::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", opts));
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue