1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37: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; 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,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) { 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.
if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) { match result(|| reader.read(buf)) {
writer.write_str(format!("{0:6u}\t", counter)); Ok(Some(n)) if n != 0 => {
counter += 1; for byte in buf.slice_to(n).iter() {
at_line_start = false; if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) {
} write!(&mut writer as &mut Writer, "{0:6u}\t", counter);
if is_numbering && *byte == LF { counter += 1;
at_line_start = true; at_line_start = false;
} }
if show_tabs && *byte == TAB { if is_numbering && *byte == LF {
writer.write(bytes!("^I")); at_line_start = true;
} else if show_ends && *byte == LF { }
writer.write(bytes!("$\n")); if show_tabs && *byte == TAB {
} else if show_nonprint && (*byte < 32 || *byte >= 127) && !is_newline_char(*byte) { writer.write(bytes!("^I"));
let mut byte = *byte; } else if show_ends && *byte == LF {
if byte >= 128 { writer.write(bytes!("$\n"));
writer.write(bytes!("M-")); } else if show_nonprint && (*byte < 32 || *byte >= 127) && !is_newline_char(*byte) {
byte = byte - 128; 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]);
} }
} _ => break
if reader.eof() {
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);
} }
} }

View file

@ -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;

View file

@ -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");

View file

@ -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;