mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 05:57:46 +00:00
echo: handle -- correctly
This commit is contained in:
parent
ca06ef6155
commit
76cf0e47ed
2 changed files with 67 additions and 46 deletions
59
echo/echo.rs
59
echo/echo.rs
|
@ -20,9 +20,15 @@ use std::uint;
|
||||||
#[path = "../common/util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
static NAME: &'static str = "echo";
|
static NAME: &'static str = "echo";
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
|
|
||||||
|
struct EchoOptions {
|
||||||
|
newline: bool,
|
||||||
|
escape: bool
|
||||||
|
}
|
||||||
|
|
||||||
fn print_char(c: char) {
|
fn print_char(c: char) {
|
||||||
print!("{}", c);
|
print!("{}", c);
|
||||||
}
|
}
|
||||||
|
@ -70,11 +76,12 @@ fn convert_str(string: &str, index: uint, base: uint) -> (char, int) {
|
||||||
(to_char(&bytes, base), max_digits)
|
(to_char(&bytes, base), max_digits)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
fn parse_options(args: Vec<String>, options: &mut EchoOptions) -> Option<Vec<String>> {
|
||||||
fn main() { os::set_exit_status(uumain(os::args())); }
|
let mut echo_args = vec!();
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> int {
|
|
||||||
let program = args.get(0).clone();
|
let program = args.get(0).clone();
|
||||||
|
for arg in args.move_iter().skip(1) {
|
||||||
|
match arg.as_slice() {
|
||||||
|
"--help" | "-h" => {
|
||||||
let opts = [
|
let opts = [
|
||||||
getopts::optflag("n", "", "do not output the trailing newline"),
|
getopts::optflag("n", "", "do not output the trailing newline"),
|
||||||
getopts::optflag("e", "", "enable interpretation of backslash escapes"),
|
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("h", "help", "display this help and exit"),
|
||||||
getopts::optflag("V", "version", "output version information 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!("echo {:s} - display a line of text", VERSION);
|
||||||
println!("");
|
println!("");
|
||||||
println!("Usage:");
|
println!("Usage:");
|
||||||
|
@ -110,17 +110,38 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
\\v vertical tab
|
\\v vertical tab
|
||||||
\\0NNN byte with octal value NNN (1 to 3 digits)
|
\\0NNN byte with octal value NNN (1 to 3 digits)
|
||||||
\\xHH byte with hexadecimal value HH (1 to 2 digits)");
|
\\xHH byte with hexadecimal value HH (1 to 2 digits)");
|
||||||
return 0;
|
return None;
|
||||||
}
|
}
|
||||||
|
"--version" | "-V" => {
|
||||||
if matches.opt_present("version") {
|
|
||||||
println!("echo version: {:s}", VERSION);
|
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() {
|
#[allow(dead_code)]
|
||||||
let string = matches.free.connect(" ");
|
fn main() { os::set_exit_status(uumain(os::args())); }
|
||||||
if matches.opt_present("e") {
|
|
||||||
|
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 prev_was_slash = false;
|
||||||
let mut iter = string.as_slice().chars().enumerate();
|
let mut iter = string.as_slice().chars().enumerate();
|
||||||
loop {
|
loop {
|
||||||
|
@ -184,7 +205,7 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !matches.opt_present("n") {
|
if !options.newline {
|
||||||
println!("")
|
println!("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue