diff --git a/Makefile b/Makefile index e7eaa95ae..8f036ad53 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ RUSTC ?= rustc RM := rm # Flags -RUSTCFLAGS := --opt-level=3 +RUSTCFLAGS := --opt-level=3 -A unused_must_use RMFLAGS := # Possible programs diff --git a/base64/base64.rs b/base64/base64.rs index bac21d1de..801154d65 100644 --- a/base64/base64.rs +++ b/base64/base64.rs @@ -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 diff --git a/basename/basename.rs b/basename/basename.rs index 7f6540ec3..e0693a97f 100644 --- a/basename/basename.rs +++ b/basename/basename.rs @@ -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,28 +20,31 @@ 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"), ]; 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; - } + Ok(m) => m, + 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; } diff --git a/cat/cat.rs b/cat/cat.rs index fea871e4d..41b9e670c 100644 --- a/cat/cat.rs +++ b/cat/cat.rs @@ -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 } diff --git a/dirname/dirname.rs b/dirname/dirname.rs index 3dd37c4b8..f2ba61139 100644 --- a/dirname/dirname.rs +++ b/dirname/dirname.rs @@ -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 { diff --git a/echo/echo.rs b/echo/echo.rs index a1fbfafc7..ddd7f7a92 100644 --- a/echo/echo.rs +++ b/echo/echo.rs @@ -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") { diff --git a/env/env.rs b/env/env.rs index 34ab95bf8..e4cd4b7be 100644 --- a/env/env.rs +++ b/env/env.rs @@ -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) } } diff --git a/mkdir/mkdir.rs b/mkdir/mkdir.rs index 4320ef155..54f170e56 100644 --- a/mkdir/mkdir.rs +++ b/mkdir/mkdir.rs @@ -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, diff --git a/rm/rm.rs b/rm/rm.rs index e6e55897f..06367e314 100644 --- a/rm/rm.rs +++ b/rm/rm.rs @@ -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(|_| { - writeln!(&mut stderr() as &mut Writer, - "Could not remove directory '{}'", name); - os::set_exit_status(1); - }).inside(|| { - fs::rmdir(path); - if verbose { - println!("Removed '{}'", name); + match fs::rmdir(path) { + Ok(_) => if verbose { println!("Removed '{}'", name); }, + Err(f) => { + writeln!(&mut stderr() as &mut Writer, + "{}", f.to_str()); + os::set_exit_status(1); } - }); + } } } @@ -183,16 +190,14 @@ fn remove_file(path: &Path, name: &str, interactive: InteractiveMode, verbose: b true }; if response { - io_error::cond.trap(|_| { - writeln!(&mut stderr() as &mut Writer, - "Could not remove file '{}'", name); - os::set_exit_status(1); - }).inside(|| { - fs::unlink(path); - if verbose { - println!("Removed '{}'", name); + match fs::unlink(path) { + Ok(_) => if verbose { println!("Removed '{}'", name); }, + Err(f) => { + writeln!(&mut stderr() as &mut Writer, + "{}", f.to_str()); + os::set_exit_status(1); } - }); + } } } @@ -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 } } diff --git a/rmdir/rmdir.rs b/rmdir/rmdir.rs index e2c122287..f82b3b691 100644 --- a/rmdir/rmdir.rs +++ b/rmdir/rmdir.rs @@ -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,23 +77,34 @@ 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); - if verbose { - println!("Removed directory '{}'", *dir); - } - if parents { - let dirname = path.dirname_str().unwrap(); - if dirname != "." { - remove_dir(&Path::new(dirname), &dirname.to_owned(), ignore, parents, verbose); + return; + } + }; + if walk_dir.next() == None { + match fs::rmdir(path) { + Ok(_) => { + if verbose { + println!("Removed directory '{}'", *dir); + } + if parents { + let dirname = path.dirname_str().unwrap(); + if dirname != "." { + 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); diff --git a/sleep/sleep.rs b/sleep/sleep.rs index 3665f10d6..b1cab4e23 100644 --- a/sleep/sleep.rs +++ b/sleep/sleep.rs @@ -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 } diff --git a/wc/wc.rs b/wc/wc.rs index c16177e71..de0f96f84 100644 --- a/wc/wc.rs +++ b/wc/wc.rs @@ -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 {