1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 04:27:45 +00:00

echo: handle -- correctly

This commit is contained in:
Arcterus 2014-06-19 13:05:43 -07:00
parent ca06ef6155
commit 76cf0e47ed
2 changed files with 67 additions and 46 deletions

View file

@ -20,9 +20,15 @@ use std::uint;
#[path = "../common/util.rs"]
mod util;
#[allow(dead_code)]
static NAME: &'static str = "echo";
static VERSION: &'static str = "1.0.0";
struct EchoOptions {
newline: bool,
escape: bool
}
fn print_char(c: char) {
print!("{}", c);
}
@ -70,11 +76,12 @@ fn convert_str(string: &str, index: uint, base: uint) -> (char, int) {
(to_char(&bytes, base), max_digits)
}
#[allow(dead_code)]
fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) -> int {
fn parse_options(args: Vec<String>, options: &mut EchoOptions) -> Option<Vec<String>> {
let mut echo_args = vec!();
let program = args.get(0).clone();
for arg in args.move_iter().skip(1) {
match arg.as_slice() {
"--help" | "-h" => {
let opts = [
getopts::optflag("n", "", "do not output the trailing newline"),
getopts::optflag("e", "", "enable interpretation of backslash escapes"),
@ -82,13 +89,6 @@ pub fn uumain(args: Vec<String>) -> int {
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) {
Ok(m) => m,
Err(f) => crash!(1, "Invalid options\n{}", f)
};
if matches.opt_present("help") {
println!("echo {:s} - display a line of text", VERSION);
println!("");
println!("Usage:");
@ -110,17 +110,38 @@ pub fn uumain(args: Vec<String>) -> int {
\\v vertical tab
\\0NNN byte with octal value NNN (1 to 3 digits)
\\xHH byte with hexadecimal value HH (1 to 2 digits)");
return 0;
return None;
}
if matches.opt_present("version") {
"--version" | "-V" => {
println!("echo version: {:s}", VERSION);
return 0;
return None;
}
"-n" => options.newline = true,
"-e" => options.escape = true,
"-E" => options.escape = false,
_ => echo_args.push(arg)
}
}
Some(echo_args)
}
if !matches.free.is_empty() {
let string = matches.free.connect(" ");
if matches.opt_present("e") {
#[allow(dead_code)]
fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) -> int {
let mut options = EchoOptions {
newline: false,
escape: false
};
let free = match parse_options(args, &mut options) {
Some(vec) => vec,
None => return 0
};
if !free.is_empty() {
let string = free.connect(" ");
if options.escape {
let mut prev_was_slash = false;
let mut iter = string.as_slice().chars().enumerate();
loop {
@ -184,7 +205,7 @@ pub fn uumain(args: Vec<String>) -> int {
}
}
if !matches.opt_present("n") {
if !options.newline {
println!("")
}