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

Merge pull request #69 from Heather/master

Important changes! [ Mostly due language Syntax changes ]
This commit is contained in:
Arcterus 2014-02-05 20:56:18 -08:00
commit 2fa4f23d86
12 changed files with 154 additions and 115 deletions

View file

@ -3,7 +3,7 @@ RUSTC ?= rustc
RM := rm
# Flags
RUSTCFLAGS := --opt-level=3
RUSTCFLAGS := --opt-level=3 -A unused_must_use
RMFLAGS :=
# Possible programs

View file

@ -82,7 +82,10 @@ fn main() {
}
fn decode(input: &mut Reader, ignore_garbage: bool) {
let mut to_decode = str::from_utf8_owned(input.read_to_end()).unwrap();
let mut to_decode = str::from_utf8_owned(match input.read_to_end() {
Ok(m) => m,
Err(f) => fail!(f.to_str())
}).unwrap();
to_decode = str::replace(to_decode, "\n", "");
@ -120,7 +123,10 @@ fn encode(input: &mut Reader, line_wrap: uint) {
_ => Some(line_wrap)
}
};
let to_encode = input.read_to_end();
let to_encode = match input.read_to_end() {
Ok(m) => m,
Err(f) => fail!(f.to_str())
};
let mut encoded = to_encode.to_base64(b64_conf);
// To my knowledge, RFC 3548 does not specify which line endings to use. It

View file

@ -1,3 +1,4 @@
#[feature(macro_rules)];
#[crate_id(name="basename", vers="1.0.0", author="Jimmy Lu")];
/*
@ -11,7 +12,7 @@
extern mod extra;
use std::io::{print, println, stderr};
use std::io::{print, println};
use std::os;
use std::str;
use std::str::StrSlice;
@ -19,15 +20,23 @@ use extra::getopts::groups;
static VERSION: &'static str = "1.0.0";
#[macro_export]
macro_rules! crash(
($exitcode:expr, $($args:expr),+) => (
{ ::std::os::set_exit_status($exitcode);
let _unused = write!(&mut ::std::io::stderr(), $($args),+);
return;
}
)
)
fn main() {
let args = os::args();
let program = strip_dir(&args[ 0 ].clone());
//
// Argument parsing
//
let opts = ~[
groups::optflag("h", "help", "display this help and exit"),
groups::optflag("V", "version", "output version information and exit"),
@ -35,12 +44,7 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return;
}
Err(f) => crash!(1, "Invalid options\n{}", f.to_err_msg())
};
if matches.opt_present("help") {
@ -95,7 +99,6 @@ fn strip_dir(fullname :&~str) -> ~str {
if c == '/' || c == '\\' {
return name;
}
name = str::from_char(c) + name;
}

View file

@ -15,7 +15,7 @@
extern mod extra;
use std::os;
use std::io::{print, stdin, stderr, stdout, File, result};
use std::io::{print, stdin, stdout, File};
use extra::getopts::groups;
fn main() {
@ -36,12 +36,7 @@ fn main() {
];
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return
}
Err(f) => fail!("Invalid options\n{}", f.to_err_msg())
};
if matches.opt_present("help") {
println!("cat 1.0.0");
@ -118,11 +113,13 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
loop {
// 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 => {
match reader.read(buf) {
Ok(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);
match write!(&mut writer as &mut Writer, "{0:6u}\t", counter) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
counter += 1;
at_line_start = false;
}
@ -130,32 +127,45 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
at_line_start = true;
}
if show_tabs && *byte == TAB {
writer.write(bytes!("^I"));
match writer.write(bytes!("^I")) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} else if show_ends && *byte == LF {
writer.write(bytes!("$\n"));
match writer.write(bytes!("$\n")) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} else if show_nonprint && (*byte < 32 || *byte >= 127) && !is_newline_char(*byte) {
let mut byte = *byte;
if byte >= 128 {
writer.write(bytes!("M-"));
match writer.write(bytes!("M-")) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
byte = byte - 128;
}
if byte < 32 {
writer.write(['^' as u8, byte + 64]);
match writer.write(['^' as u8, byte + 64]) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} else if byte == 127 {
writer.write(['^' as u8, byte - 64]);
match writer.write(['^' as u8, byte - 64]) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} else {
writer.write([byte]);
match writer.write([byte]) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
}
} else {
writer.write([*byte]);
}
match writer.write([*byte]) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
}
}
},
_ => break
}
}
}
return;
}
@ -170,9 +180,12 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
loop {
// 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
match reader.read(buf) {
Ok(n) if n != 0 => {
match writer.write(buf.slice_to(n)) {
Ok(_) => (), Err(err) => fail!("{}", err)
}
}, _ => break
}
}
}
@ -183,14 +196,8 @@ fn open(path: ~str) -> Option<~Reader> {
return Some(~stdin() as ~Reader);
}
match result(|| File::open(&std::path::Path::new(path.as_slice()))) {
match File::open(&std::path::Path::new(path.as_slice())) {
Ok(fd) => return Some(~fd as ~Reader),
Err(e) => {
writeln!(&mut stderr() as &mut Writer,
"cat: {0:s}: {1:s}", path, e.to_str());
os::set_exit_status(1);
Err(e) => fail!("cat: {0:s}: {1:s}", path, e.to_str())
}
}
None
}

View file

@ -12,7 +12,7 @@
extern mod extra;
use std::os;
use std::io::{print, stderr};
use std::io::print;
use extra::getopts::groups;
static VERSION: &'static str = "1.0.0";
@ -28,12 +28,7 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return
}
Err(f) => fail!("Invalid options\n{}", f.to_err_msg())
};
if matches.opt_present("help") {
@ -60,7 +55,10 @@ directory).", opts));
if !matches.free.is_empty() {
for path in matches.free.iter() {
let p = std::path::Path::new(path.clone());
print(std::str::from_utf8(p.dirname()));
let d = std::str::from_utf8(p.dirname());
if d.is_some() {
print(d.unwrap());
}
print(separator);
}
} else {

View file

@ -1,3 +1,4 @@
#[feature(macro_rules)];
#[crate_id(name="echo", vers="1.0.0", author="Derek Chiang")];
/*
@ -12,12 +13,22 @@
extern mod extra;
use std::os;
use std::io::{print, println, stderr};
use std::io::{print, println};
use std::uint;
use extra::getopts::groups;
static VERSION: &'static str = "1.0.0";
#[macro_export]
macro_rules! crash(
($exitcode:expr, $($args:expr),+) => (
{ ::std::os::set_exit_status($exitcode);
let _unused = write!(&mut ::std::io::stderr(), $($args),+);
return;
}
)
)
fn print_char(c: char) {
print!("{}", c);
}
@ -77,12 +88,7 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return
}
Err(f) => crash!(1, "Invalid options\n{}", f.to_err_msg())
};
if matches.opt_present("help") {

4
env/env.rs vendored
View file

@ -191,12 +191,12 @@ fn main() {
match opts.program {
[ref prog, ..args] => {
match std::run::process_status(prog.as_slice(), args.as_slice()) {
Some(exit) =>
Ok(exit) =>
std::os::set_exit_status(match exit {
std::io::process::ExitStatus(s) => s,
_ => 1
}),
None => std::os::set_exit_status(1)
Err(_) => std::os::set_exit_status(1)
}
}

View file

@ -12,7 +12,7 @@
extern mod extra;
use std::os;
use std::io::{fs, result, stderr};
use std::io::{fs, stderr};
use std::num::strconv;
use extra::getopts::groups;
@ -149,7 +149,7 @@ fn exec(dirs: ~[~str], mk_parents: bool, mode: u32, verbose: bool) {
* Wrapper to catch errors, return false if failed
*/
fn mkdir(path: &Path, mode: u32) -> bool {
match result(|| fs::mkdir(path, mode)) {
match fs::mkdir(path, mode) {
Ok(_) => true,
Err(e) => {
writeln!(&mut stderr() as &mut Writer,

View file

@ -12,7 +12,7 @@
extern mod extra;
use std::os;
use std::io::{print,stdin,stderr,stdio,fs,BufferedReader,io_error};
use std::io::{print,stdin,stderr,stdio,fs,BufferedReader};
use extra::getopts::groups;
#[deriving(Eq)]
@ -127,7 +127,16 @@ fn remove(files: &[~str], force: bool, interactive: InteractiveMode, one_fs: boo
if file.exists() {
if file.is_dir() {
if recursive && (*filename != ~"/" || !preserve_root) {
remove(fs::walk_dir(&file).map(|x| x.as_str().unwrap().to_owned()).to_owned_vec(), force, interactive, one_fs, preserve_root, recursive, dir, verbose);
let walk_dir = match fs::walk_dir(&file) {
Ok(m) => m,
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"{}", f.to_str());
os::set_exit_status(1);
return;
}
};
remove(walk_dir.map(|x| x.as_str().unwrap().to_owned()).to_owned_vec(), force, interactive, one_fs, preserve_root, recursive, dir, verbose);
remove_dir(&file, *filename, interactive, verbose);
} else if dir && (*filename != ~"/" || !preserve_root) {
remove_dir(&file, *filename, interactive, verbose);
@ -162,16 +171,14 @@ fn remove_dir(path: &Path, name: &str, interactive: InteractiveMode, verbose: bo
true
};
if response {
io_error::cond.trap(|_| {
match fs::rmdir(path) {
Ok(_) => if verbose { println!("Removed '{}'", name); },
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"Could not remove directory '{}'", name);
"{}", f.to_str());
os::set_exit_status(1);
}).inside(|| {
fs::rmdir(path);
if verbose {
println!("Removed '{}'", name);
}
});
}
}
}
@ -183,16 +190,14 @@ fn remove_file(path: &Path, name: &str, interactive: InteractiveMode, verbose: b
true
};
if response {
io_error::cond.trap(|_| {
match fs::unlink(path) {
Ok(_) => if verbose { println!("Removed '{}'", name); },
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"Could not remove file '{}'", name);
"{}", f.to_str());
os::set_exit_status(1);
}).inside(|| {
fs::unlink(path);
if verbose {
println!("Removed '{}'", name);
}
});
}
}
}
@ -212,7 +217,7 @@ fn prompt(msg: &str) -> bool {
fn read_prompt() -> bool {
stdio::flush();
match BufferedReader::new(stdin()).read_line() {
Some(line) => {
Ok(line) => {
match line.char_at(0) {
'y' | 'Y' => true,
'n' | 'N' => false,
@ -222,7 +227,7 @@ fn read_prompt() -> bool {
}
}
}
None => true
Err(_) => true
}
}

View file

@ -12,7 +12,7 @@
extern mod extra;
use std::os;
use std::io::{print, stderr, io_error, fs};
use std::io::{print, stderr, fs};
use extra::getopts::groups;
fn main() {
@ -77,13 +77,18 @@ fn remove(dirs: &[~str], ignore: bool, parents: bool, verbose: bool) {
}
fn remove_dir(path: &Path, dir: &~str, ignore: bool, parents: bool, verbose: bool) {
if fs::walk_dir(path).next() == None {
io_error::cond.trap(|_| {
let mut walk_dir = match fs::walk_dir(path) {
Ok(m) => m,
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"Failed to remove directory '{}'", *dir);
"{}", f.to_str());
os::set_exit_status(1);
}).inside(|| {
fs::rmdir(path);
return;
}
};
if walk_dir.next() == None {
match fs::rmdir(path) {
Ok(_) => {
if verbose {
println!("Removed directory '{}'", *dir);
}
@ -93,7 +98,13 @@ fn remove_dir(path: &Path, dir: &~str, ignore: bool, parents: bool, verbose: boo
remove_dir(&Path::new(dirname), &dirname.to_owned(), ignore, parents, verbose);
}
}
});
}
Err(f) => {
writeln!(&mut stderr() as &mut Writer,
"{}", f.to_str());
os::set_exit_status(1);
}
}
} else if !ignore {
writeln!(&mut stderr() as &mut Writer,
"Failed to remove directory '{}' (non-empty)", *dir);

View file

@ -28,7 +28,7 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
writeln!(&mut stderr() as &mut Writer, "{}", f.to_err_msg())
writeln!(&mut stderr() as &mut Writer, "{}", f.to_err_msg());
os::set_exit_status(1);
return
}

View file

@ -13,7 +13,7 @@ extern mod extra;
use std::os;
use std::io::{print, stdin, stderr, File, result, BufferedReader};
use std::str::from_utf8_opt;
use std::str::from_utf8;
use extra::getopts::{groups, Matches};
struct Result {
@ -58,7 +58,7 @@ fn main() {
return;
}
if (matches.opt_present("version")) {
if matches.opt_present("version") {
println!("wc 1.0.0");
return;
}
@ -111,14 +111,14 @@ pub fn wc(files: ~[~str], matches: &Matches) {
match result(| | reader.read_until(LF)) {
Ok(Some(raw_line)) => {
// GNU 'wc' only counts lines that end in LF as lines
if (raw_line.iter().last().unwrap() == &LF) {
if raw_line.iter().last().unwrap() == &LF {
line_count += 1;
}
byte_count += raw_line.iter().len();
// try and convert the bytes to UTF-8 first
match from_utf8_opt(raw_line) {
match from_utf8(raw_line) {
Some(line) => {
word_count += line.words().len();
current_char_count = line.chars().len();
@ -138,7 +138,7 @@ pub fn wc(files: ~[~str], matches: &Matches) {
}
}
if (current_char_count > longest_line_length) {
if current_char_count > longest_line_length {
// we subtract one here because `line.iter().len()` includes the LF
// matches GNU 'wc' behaviour
longest_line_length = current_char_count - 1;
@ -163,7 +163,7 @@ pub fn wc(files: ~[~str], matches: &Matches) {
total_char_count += char_count;
total_byte_count += byte_count;
if (longest_line_length > total_longest_line_length) {
if longest_line_length > total_longest_line_length {
total_longest_line_length = longest_line_length;
}
@ -175,38 +175,41 @@ pub fn wc(files: ~[~str], matches: &Matches) {
print_stats(&result.filename, result.lines, result.words, result.chars, result.bytes, result.max_line_length, matches, max_str_len);
}
if (files.len() > 1) {
if files.len() > 1 {
print_stats(&~"total", total_line_count, total_word_count, total_char_count, total_byte_count, total_longest_line_length, matches, max_str_len);
}
}
fn print_stats(filename: &~str, line_count: uint, word_count: uint, char_count: uint,
byte_count: uint, longest_line_length: uint, matches: &Matches, max_str_len: uint) {
if (matches.opt_present("lines")) {
if matches.opt_present("lines") {
print!("{:1$}", line_count, max_str_len);
}
if (matches.opt_present("words")) {
if matches.opt_present("words") {
print!("{:1$}", word_count, max_str_len);
}
if (matches.opt_present("bytes")) {
if matches.opt_present("bytes") {
print!("{:1$}", byte_count, max_str_len + 1);
}
if (matches.opt_present("chars")) {
if matches.opt_present("chars") {
print!("{:1$}", char_count, max_str_len);
}
if (matches.opt_present("max-line-length")) {
if matches.opt_present("max-line-length") {
print!("{:1$}", longest_line_length, max_str_len);
}
// defaults
if (!matches.opt_present("bytes") && !matches.opt_present("chars") && !matches.opt_present("lines")
&& !matches.opt_present("words") && !matches.opt_present("max-line-length")) {
if !matches.opt_present("bytes")
&& !matches.opt_present("chars")
&& !matches.opt_present("lines")
&& !matches.opt_present("words")
&& !matches.opt_present("max-line-length") {
print!("{:1$}", line_count, max_str_len);
print!("{:1$}", word_count, max_str_len + 1);
print!("{:1$}", byte_count, max_str_len + 1);
}
if (*filename != ~"-") {
if *filename != ~"-" {
println!(" {}", *filename);
}
else {