1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

Update main utils for language changes.

This commit is contained in:
Huon Wilson 2013-11-11 20:39:23 +11:00
parent a501bade88
commit c4fab4cf67
4 changed files with 64 additions and 57 deletions

View file

@ -15,7 +15,7 @@
extern mod extra;
use std::os;
use std::io::{stdin, stderr, stdout, Writer, Reader};
use std::rt::io::{stdin, stderr, stdout, File, result};
use extra::getopts::groups;
fn main() {
@ -37,8 +37,8 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
stderr().write_line("Invalid options");
stderr().write_line(f.to_err_msg());
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return
}
@ -47,7 +47,7 @@ fn main() {
println("cat 1.0.0");
println("");
println("Usage:");
println(format!(" {0:s} [OPTION]... [FILE]...", program));
println!(" {0:s} [OPTION]... [FILE]...", program);
println("");
print(groups::usage("Concatenate FILE(s), or standard input, to standard output.", opts));
println("");
@ -100,54 +100,57 @@ 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) {
let writer = stdout();
let mut writer = stdout();
if NumberNone != number || show_nonprint || show_ends || show_tabs || squeeze_blank {
let mut counter: uint = 1;
let is_numbering = number == NumberAll || number == NumberNonEmpty;
for path in files.iter() {
let reader = match open(path.to_owned()) {
let mut reader = match open(path.to_owned()) {
Some(f) => f,
None => { continue }
};
let mut at_line_start = true;
let mut buf = [0, .. 2];
loop {
let buf = reader.read_bytes(2);
for byte in buf.iter() {
if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) {
writer.write_str(format!("{0:6u}\t", counter));
counter += 1;
at_line_start = false;
}
if is_numbering && *byte == LF {
at_line_start = true;
}
if show_tabs && *byte == TAB {
writer.write(bytes!("^I"));
} else if show_ends && *byte == LF {
writer.write(bytes!("$\n"));
} else if show_nonprint && (*byte < 32 || *byte >= 127) && !is_newline_char(*byte) {
let mut byte = *byte;
if byte >= 128 {
writer.write(bytes!("M-"));
byte = byte - 128;
// reading from a TTY seems to raise a condition on
// 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))) {
write!(&mut writer as &mut Writer, "{0:6u}\t", counter);
counter += 1;
at_line_start = false;
}
if is_numbering && *byte == LF {
at_line_start = true;
}
if show_tabs && *byte == TAB {
writer.write(bytes!("^I"));
} else if show_ends && *byte == LF {
writer.write(bytes!("$\n"));
} else if show_nonprint && (*byte < 32 || *byte >= 127) && !is_newline_char(*byte) {
let mut byte = *byte;
if byte >= 128 {
writer.write(bytes!("M-"));
byte = byte - 128;
}
if byte < 32 {
writer.write(['^' as u8, byte + 64]);
} else if byte == 127 {
writer.write(['^' as u8, byte - 64]);
} else {
writer.write([byte]);
}
} else {
writer.write([*byte]);
}
}
if byte < 32 {
writer.write(['^' as u8, byte + 64]);
} else if byte == 127 {
writer.write(['^' as u8, byte - 64]);
} else {
writer.write([byte]);
}
} else {
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;
}
let mut buf = [0, .. 100000];
// passthru mode
for path in files.iter() {
let reader = match open(path.to_owned()) {
let mut reader = match open(path.to_owned()) {
Some(f) => f,
None => { continue }
};
loop {
writer.write(reader.read_bytes(100000));
if reader.eof() {
break;
// reading from a TTY seems to raise a condition on EOF,
// rather than return Some(0) like a file.
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 {
return Some(stdin());
return Some(~stdin() as ~Reader);
}
match std::io::file_reader(&std::path::Path::new(path.as_slice())) {
Ok(fd) => return Some(fd),
match result(|| File::open(&std::path::Path::new(path.as_slice()))) {
Ok(fd) => return Some(~fd as ~Reader),
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);
}
}

View file

@ -14,7 +14,7 @@
extern mod extra;
use std::os;
use std::io::stderr;
use std::rt::io::stderr;
use extra::getopts::groups;
fn main() {
@ -28,8 +28,8 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
stderr().write_line("Invalid options");
stderr().write_line(f.to_err_msg());
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return
}
@ -38,7 +38,7 @@ fn main() {
println("printenv 1.0.0");
println("");
println("Usage:");
println(format!(" {0:s} [VARIABLE]... [OPTION]...", program));
println!(" {0:s} [VARIABLE]... [OPTION]...", program);
println("");
print(groups::usage("Prints the given environment VARIABLE(s), otherwise prints them all.", opts));
return;
@ -59,7 +59,7 @@ pub fn exec(args: ~[~str], separator: &str) {
if args.is_empty() {
let vars = os::env();
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);
}
return;

View file

@ -48,7 +48,7 @@ fn main() {
];
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => fail2!(f.to_err_msg()),
Err(f) => fail!(f.to_err_msg()),
};
if matches.opt_present("help") {
println("whoami 1.0.0");

View file

@ -14,7 +14,7 @@
extern mod extra;
use std::os;
use std::io::stderr;
use std::rt::io::stderr;
use extra::getopts::groups;
fn main() {
@ -27,8 +27,8 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
stderr().write_line("Invalid options");
stderr().write_line(f.to_err_msg());
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return
}
@ -37,7 +37,7 @@ fn main() {
println("yes 1.0.0");
println("");
println("Usage:");
println(format!(" {0:s} [STRING]... [OPTION]...", program));
println!(" {0:s} [STRING]... [OPTION]...", program);
println("");
print(groups::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", opts));
return;