From de2807214031f7fae097daad9bc1d3c0de4aba09 Mon Sep 17 00:00:00 2001 From: kwantam Date: Mon, 27 Apr 2015 13:47:54 -0400 Subject: [PATCH 1/3] update `yes` This commit makes `yes` build on recent nightly. --- src/yes/yes.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/yes/yes.rs b/src/yes/yes.rs index 3bfe4830d..72c637330 100644 --- a/src/yes/yes.rs +++ b/src/yes/yes.rs @@ -1,5 +1,5 @@ #![crate_name = "yes"] -#![feature(collections, old_io, rustc_private)] +#![feature(rustc_private)] /* * This file is part of the uutils coreutils package. @@ -15,8 +15,7 @@ extern crate getopts; extern crate libc; -use std::old_io::print; -use std::borrow::IntoCow; +use std::io::Write; #[path = "../common/util.rs"] #[macro_use] @@ -30,7 +29,7 @@ pub fn uumain(args: Vec) -> i32 { getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("V", "version", "output version information and exit"), ]; - let matches = match getopts::getopts(args.tail(), &opts) { + let matches = match getopts::getopts(&args[1..], &opts) { Ok(m) => m, Err(f) => { crash!(1, "invalid options\n{}", f) @@ -42,7 +41,7 @@ pub fn uumain(args: Vec) -> i32 { println!("Usage:"); println!(" {0} [STRING]... [OPTION]...", program); println!(""); - print(&getopts::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", &opts)[..]); + print!("{}", getopts::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", &opts)); return 0; } if matches.opt_present("version") { @@ -50,9 +49,9 @@ pub fn uumain(args: Vec) -> i32 { return 0; } let string = if matches.free.is_empty() { - "y".into_cow() + "y".to_string() } else { - matches.free.connect(" ").into_cow() + matches.free.connect(" ") }; exec(&string[..]); From 05520e69cb4667d694bd971c1b57c88e6540bd8e Mon Sep 17 00:00:00 2001 From: kwantam Date: Mon, 27 Apr 2015 14:06:13 -0400 Subject: [PATCH 2/3] clean up #![features] For the most part, this commit removes the `collections` feature. In some places I've removed other features where the effort to do so was pretty low. --- src/base64/base64.rs | 4 ++-- src/basename/basename.rs | 4 ++-- src/cat/cat.rs | 6 ++---- src/chroot/chroot.rs | 6 +++--- src/echo/echo.rs | 4 ++-- src/fmt/fmt.rs | 6 ++---- src/fmt/parasplit.rs | 2 +- src/sync/sync.rs | 4 ++-- 8 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/base64/base64.rs b/src/base64/base64.rs index f75d70bda..09987361a 100644 --- a/src/base64/base64.rs +++ b/src/base64/base64.rs @@ -1,5 +1,5 @@ #![crate_name = "base64"] -#![feature(box_syntax, collections, rustc_private)] +#![feature(box_syntax, rustc_private)] /* * This file is part of the uutils coreutils package. @@ -48,7 +48,7 @@ pub fn uumain(args: Vec) -> i32 { optflag("h", "help", "display this help text and exit"), optflag("V", "version", "output version information and exit") ]; - let matches = match getopts(args.tail(), &opts) { + let matches = match getopts(&args[1..], &opts) { Ok(m) => m, Err(e) => { crash!(1, "error: {}", e); diff --git a/src/basename/basename.rs b/src/basename/basename.rs index b5f6be03c..1179c709c 100644 --- a/src/basename/basename.rs +++ b/src/basename/basename.rs @@ -1,5 +1,5 @@ #![crate_name = "basename"] -#![feature(collections, rustc_private)] +#![feature(rustc_private)] /* * This file is part of the uutils coreutils package. @@ -34,7 +34,7 @@ pub fn uumain(args: Vec) -> i32 { getopts::optflag("V", "version", "output version information and exit"), ]; - let matches = match getopts::getopts(args.tail(), &opts) { + let matches = match getopts::getopts(&args[1..], &opts) { Ok(m) => m, Err(f) => crash!(1, "Invalid options\n{}", f) }; diff --git a/src/cat/cat.rs b/src/cat/cat.rs index 27d22252f..dfea5d71c 100644 --- a/src/cat/cat.rs +++ b/src/cat/cat.rs @@ -1,7 +1,5 @@ #![crate_name = "cat"] -#![feature(collections, rustc_private)] - -#![feature(box_syntax, unsafe_destructor)] +#![feature(rustc_private, box_syntax, unsafe_destructor)] /* * This file is part of the uutils coreutils package. @@ -42,7 +40,7 @@ pub fn uumain(args: Vec) -> i32 { getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("V", "version", "output version information and exit"), ]; - let matches = match getopts::getopts(args.tail(), &opts) { + let matches = match getopts::getopts(&args[1..], &opts) { Ok(m) => m, Err(f) => panic!("Invalid options\n{}", f) }; diff --git a/src/chroot/chroot.rs b/src/chroot/chroot.rs index 87c413ac7..e2bd6a926 100644 --- a/src/chroot/chroot.rs +++ b/src/chroot/chroot.rs @@ -1,5 +1,5 @@ #![crate_name = "chroot"] -#![feature(collections, rustc_private, path_ext)] +#![feature(rustc_private, path_ext)] /* * This file is part of the uutils coreutils package. @@ -57,7 +57,7 @@ pub fn uumain(args: Vec) -> i32 { optflag("V", "version", "Show program's version") ]; - let opts = match getopts(args.tail(), &options) { + let opts = match getopts(&args[1..], &options) { Ok(m) => m, Err(f) => { show_error!("{}", f); @@ -195,7 +195,7 @@ fn set_groups_from_str(groups: &str) { fn set_user(user: &str) { if !user.is_empty() { - let user_id = get_pw_from_args(&vec!(String::from_str(user))).unwrap().pw_uid; + let user_id = get_pw_from_args(&vec!(user.to_string())).unwrap().pw_uid; let err = unsafe { setuid(user_id as libc::uid_t) }; if err != 0 { crash!(1, "cannot set user to {}: {}", user, Error::last_os_error()) diff --git a/src/echo/echo.rs b/src/echo/echo.rs index ea9c53720..350b01faa 100644 --- a/src/echo/echo.rs +++ b/src/echo/echo.rs @@ -1,5 +1,5 @@ #![crate_name = "echo"] -#![feature(rustc_private, str_char)] +#![feature(rustc_private)] /* * This file is part of the uutils coreutils package. @@ -94,7 +94,7 @@ fn parse_options(args: Vec, options: &mut EchoOptions) -> Option options.escape = true, "-E" => options.escape = false, _ => { - if arg.len() > 1 && arg.char_at(0) == '-' { + if arg.len() > 1 && arg.chars().next().unwrap_or('_') == '-' { let mut newopts = options.clone(); for ch in arg.chars().skip(1) { match ch { diff --git a/src/fmt/fmt.rs b/src/fmt/fmt.rs index a20047278..7edae633f 100644 --- a/src/fmt/fmt.rs +++ b/src/fmt/fmt.rs @@ -1,4 +1,5 @@ #![crate_name = "fmt"] +#![feature(box_syntax,rustc_private,str_char,unicode,core)] /* * This file is part of `fmt` from the uutils coreutils package. @@ -9,9 +10,6 @@ * file that was distributed with this source code. */ -#![feature(box_syntax,core,rustc_private,collections,str_char,unicode)] - -extern crate core; extern crate getopts; extern crate rustc_unicode; extern crate unicode_width; @@ -87,7 +85,7 @@ pub fn uumain(args: Vec) -> i32 { getopts::optflag("h", "help", "Display this help message and exit.") ]; - let matches = match getopts::getopts(args.tail(), &opts[..]) { + let matches = match getopts::getopts(&args[1..], &opts[..]) { Ok(m) => m, Err(f) => crash!(1, "{}\nTry `{} --help' for more information.", f, args[0]) }; diff --git a/src/fmt/parasplit.rs b/src/fmt/parasplit.rs index 22c688585..155edf6c2 100644 --- a/src/fmt/parasplit.rs +++ b/src/fmt/parasplit.rs @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -use core::iter::Peekable; +use std::iter::Peekable; use std::io::{BufRead, Lines}; use std::slice::Iter; use std::str::CharRange; diff --git a/src/sync/sync.rs b/src/sync/sync.rs index 8b76b30ec..0c4ca4280 100644 --- a/src/sync/sync.rs +++ b/src/sync/sync.rs @@ -1,5 +1,5 @@ #![crate_name = "sync"] -#![feature(collections, rustc_private)] +#![feature(rustc_private)] /* * This file is part of the uutils coreutils package. @@ -148,7 +148,7 @@ pub fn uumain(args: Vec) -> i32 { optflag("V", "version", "output version information and exit") ]; - let matches = match getopts(args.tail(), &options) { + let matches = match getopts(&args[1..], &options) { Ok(m) => { m } _ => { help(program, &options); return 1 } }; From 8cd81ec4188c1039b31b0bd4ee7b7591f2f4a849 Mon Sep 17 00:00:00 2001 From: kwantam Date: Mon, 27 Apr 2015 15:25:01 -0400 Subject: [PATCH 3/3] update `uniq` --- src/uniq/uniq.rs | 83 ++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/src/uniq/uniq.rs b/src/uniq/uniq.rs index f400f5867..376915d85 100644 --- a/src/uniq/uniq.rs +++ b/src/uniq/uniq.rs @@ -1,5 +1,5 @@ #![crate_name = "uniq"] -#![feature(collections, core, old_io, old_path, rustc_private, std_misc)] +#![feature(rustc_private)] /* * This file is part of the uutils coreutils package. @@ -13,10 +13,11 @@ extern crate getopts; -use std::ascii::OwnedAsciiExt; use std::cmp::min; use std::str::FromStr; -use std::old_io as io; +use std::fs::File; +use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; +use std::path::Path; #[path = "../common/util.rs"] #[macro_use] @@ -37,10 +38,10 @@ struct Uniq { } impl Uniq { - pub fn print_uniq(&self, reader: &mut io::BufferedReader, writer: &mut io::BufferedWriter) { + pub fn print_uniq(&self, reader: &mut BufReader, writer: &mut BufWriter) { let mut lines: Vec = vec!(); let mut first_line_printed = false; - let delimiters = self.delimiters.as_slice(); + let delimiters = &self.delimiters[..]; for io_line in reader.lines() { let line = crash_if_err!(1, io_line); @@ -68,18 +69,18 @@ impl Uniq { Some(i) => min(slice_start + i, len), None => len }; - let sliced = line.as_slice()[slice_start..slice_stop].to_string(); - if self.ignore_case { - sliced.into_ascii_uppercase() - } else { - sliced - } + + line[slice_start..slice_stop].chars() + .map(|c| match c { + 'a' ... 'z' if self.ignore_case => ((c as u8) - 32) as char, + _ => c, + }).collect() } else { line.clone() } } - fn print_lines(&self, writer: &mut io::BufferedWriter, lines: &Vec, print_delimiter: bool) -> bool { + fn print_lines(&self, writer: &mut BufWriter, lines: &Vec, print_delimiter: bool) -> bool { let mut first_line_printed = false; let mut count = if self.all_repeated { 1 } else { lines.len() }; if lines.len() == 1 && !self.repeats_only @@ -89,7 +90,7 @@ impl Uniq { count += 1; } if self.all_repeated { - for line in lines.tail().iter() { + for line in lines[1..].iter() { self.print_line(writer, line, count, print_delimiter && !first_line_printed); first_line_printed = true; count += 1; @@ -98,16 +99,17 @@ impl Uniq { first_line_printed } - fn print_line(&self, writer: &mut io::BufferedWriter, line: &String, count: usize, print_delimiter: bool) { - let output_line = if self.show_counts { - format!("{:7} {}", count, line) - } else { - line.clone() - }; + fn print_line(&self, writer: &mut BufWriter, line: &String, count: usize, print_delimiter: bool) { if print_delimiter { - crash_if_err!(1, writer.write_line("")); + crash_if_err!(1, writer.write_all(&['\n' as u8])); } - crash_if_err!(1, writer.write_str(output_line.as_slice())); + + crash_if_err!(1, if self.show_counts { + writer.write_all(format!("{:7} {}", count, line).as_bytes()) + } else { + writer.write_all(line.as_bytes()) + }); + crash_if_err!(1, writer.write_all("\n".as_bytes())); } } @@ -120,9 +122,6 @@ fn opt_parsed(opt_name: &str, matches: &getopts::Matches) -> Option< } pub fn uumain(args: Vec) -> i32 { - let program_path = Path::new(args[0].clone()); - let program = program_path.filename_str().unwrap_or(NAME); - let opts = [ getopts::optflag("c", "count", "prefix lines by the number of occurrences"), getopts::optflag("d", "repeated", "only print duplicate lines"), @@ -139,7 +138,7 @@ pub fn uumain(args: Vec) -> i32 { getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("V", "version", "output version information and exit") ]; - let matches = match getopts::getopts(args.tail(), &opts) { + let matches = match getopts::getopts(&args[1..], &opts) { Ok(m) => m, Err(f) => crash!(1, "{}", f) }; @@ -148,13 +147,13 @@ pub fn uumain(args: Vec) -> i32 { println!("{} {}", NAME, VERSION); println!(""); println!("Usage:"); - println!(" {0} [OPTION]... [FILE]...", program); + println!(" {0} [OPTION]... [FILE]...", args[0]); println!(""); print!("{}", getopts::usage("Filter adjacent matching lines from INPUT (or standard input),\n\ writing to OUTPUT (or standard output).", &opts)); println!(""); println!("Note: '{0}' does not detect repeated lines unless they are adjacent.\n\ - You may want to sort the input first, or use 'sort -u' without '{0}'.\n", program); + You may want to sort the input first, or use 'sort -u' without '{0}'.\n", args[0]); } else if matches.opt_present("version") { println!("{} {}", NAME, VERSION); } else { @@ -171,7 +170,7 @@ pub fn uumain(args: Vec) -> i32 { uniques_only: matches.opt_present("unique"), all_repeated: matches.opt_present("all-repeated"), delimiters: match matches.opt_default("all-repeated", "none") { - Some(ref opt_arg) if opt_arg.as_slice() != "none" => { + Some(ref opt_arg) if opt_arg != "none" => { let rep_args = ["prepend".to_string(), "separate".to_string()]; if !rep_args.contains(opt_arg) { crash!(1, "Incorrect argument for all-repeated: {}", opt_arg.clone()); @@ -191,26 +190,26 @@ pub fn uumain(args: Vec) -> i32 { 0 } -fn open_input_file(in_file_name: String) -> io::BufferedReader> { - let in_file = if in_file_name.as_slice() == "-" { - Box::new(io::stdio::stdin_raw()) as Box +fn open_input_file(in_file_name: String) -> BufReader> { + let in_file = if in_file_name == "-" { + Box::new(stdin()) as Box } else { - let path = Path::new(in_file_name); - let in_file = io::File::open(&path); + let path = Path::new(&in_file_name[..]); + let in_file = File::open(&path); let r = crash_if_err!(1, in_file); - Box::new(r) as Box + Box::new(r) as Box }; - io::BufferedReader::new(in_file) + BufReader::new(in_file) } -fn open_output_file(out_file_name: String) -> io::BufferedWriter> { - let out_file = if out_file_name.as_slice() == "-" { - Box::new(io::stdio::stdout_raw()) as Box +fn open_output_file(out_file_name: String) -> BufWriter> { + let out_file = if out_file_name == "-" { + Box::new(stdout()) as Box } else { - let path = Path::new(out_file_name); - let in_file = io::File::create(&path); + let path = Path::new(&out_file_name[..]); + let in_file = File::create(&path); let w = crash_if_err!(1, in_file); - Box::new(w) as Box + Box::new(w) as Box }; - io::BufferedWriter::new(out_file) + BufWriter::new(out_file) }