2014-03-31 16:40:21 +00:00
|
|
|
#![feature(macro_rules)]
|
2014-04-08 13:37:17 +00:00
|
|
|
#![crate_id(name="echo", vers="1.0.0", author="Derek Chiang")]
|
2013-12-01 09:15:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Derek Chiang <derekchiang93@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
extern crate libc;
|
2013-12-01 09:15:07 +00:00
|
|
|
|
|
|
|
use std::os;
|
2014-02-05 10:40:22 +00:00
|
|
|
use std::io::{print, println};
|
2013-12-01 09:15:07 +00:00
|
|
|
use std::uint;
|
|
|
|
|
2014-02-23 22:17:48 +00:00
|
|
|
#[path = "../common/util.rs"]
|
2014-02-07 06:39:07 +00:00
|
|
|
mod util;
|
2013-12-01 09:15:07 +00:00
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
static NAME: &'static str = "echo";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
2014-02-05 10:40:22 +00:00
|
|
|
|
2013-12-01 09:15:07 +00:00
|
|
|
fn print_char(c: char) {
|
|
|
|
print!("{}", c);
|
|
|
|
}
|
|
|
|
|
2014-04-26 05:03:08 +00:00
|
|
|
fn to_char(bytes: &Vec<u8>, base: uint) -> char {
|
|
|
|
uint::parse_bytes(bytes.as_slice(), base).unwrap() as u8 as char
|
2013-12-01 09:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn isxdigit(c: u8) -> bool {
|
|
|
|
match c as char {
|
|
|
|
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' |
|
|
|
|
'8' | '9' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn isodigit(c: u8) -> bool {
|
|
|
|
match c as char {
|
|
|
|
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-02 01:00:44 +00:00
|
|
|
fn convert_str(string: &str, index: uint, base: uint) -> (char, int) {
|
|
|
|
let (max_digits, is_legal_digit) = match base {
|
|
|
|
8u => (3, isodigit),
|
|
|
|
16u => (2, isxdigit),
|
|
|
|
_ => fail!(),
|
|
|
|
};
|
|
|
|
|
2014-04-26 05:03:08 +00:00
|
|
|
let mut bytes = vec!();
|
2013-12-02 01:00:44 +00:00
|
|
|
for offset in range(0, max_digits) {
|
|
|
|
let c = string[index + offset as uint];
|
|
|
|
if is_legal_digit(c) {
|
|
|
|
bytes.push(c as u8);
|
|
|
|
} else {
|
|
|
|
if bytes.len() > 0 {
|
2014-04-26 05:03:08 +00:00
|
|
|
return (to_char(&bytes, base), offset);
|
2013-12-02 01:00:44 +00:00
|
|
|
} else {
|
|
|
|
return (' ', offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 04:41:53 +00:00
|
|
|
|
|
|
|
(to_char(&bytes, base), max_digits)
|
2013-12-02 01:00:44 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 12:01:30 +00:00
|
|
|
#[allow(dead_code)]
|
2014-06-08 07:56:37 +00:00
|
|
|
fn main() { os::set_exit_status(uumain(os::args())); }
|
2014-05-28 11:43:37 +00:00
|
|
|
|
2014-06-08 07:56:37 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> int {
|
2014-05-16 08:32:58 +00:00
|
|
|
let program = args.get(0).clone();
|
2014-05-30 08:35:54 +00:00
|
|
|
let opts = [
|
2014-02-07 06:39:07 +00:00
|
|
|
getopts::optflag("n", "", "do not output the trailing newline"),
|
|
|
|
getopts::optflag("e", "", "enable interpretation of backslash escapes"),
|
|
|
|
getopts::optflag("E", "", "disable interpretation of backslash escapes (default)"),
|
|
|
|
getopts::optflag("h", "help", "display this help and exit"),
|
|
|
|
getopts::optflag("V", "version", "output version information and exit"),
|
2013-12-01 09:15:07 +00:00
|
|
|
];
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), opts) {
|
2013-12-01 09:15:07 +00:00
|
|
|
Ok(m) => m,
|
2014-06-15 10:50:40 +00:00
|
|
|
Err(f) => crash!(1, "Invalid options\n{}", f)
|
2013-12-01 09:15:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("help") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("echo {:s} - display a line of text", VERSION);
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2013-12-01 09:15:07 +00:00
|
|
|
println!(" {0:s} [SHORT-OPTION]... [STRING]...", program);
|
|
|
|
println!(" {0:s} LONG-OPTION", program);
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("");
|
2014-05-17 10:32:14 +00:00
|
|
|
println(getopts::usage("Echo the STRING(s) to standard output.", opts).as_slice());
|
2013-12-03 12:56:15 +00:00
|
|
|
println("If -e is in effect, the following sequences are recognized:
|
|
|
|
|
|
|
|
\\\\ backslash
|
|
|
|
\\a alert (BEL)
|
|
|
|
\\b backspace
|
|
|
|
\\c produce no further output
|
|
|
|
\\e escape
|
|
|
|
\\f form feed
|
|
|
|
\\n new line
|
|
|
|
\\r carriage return
|
|
|
|
\\t horizontal tab
|
|
|
|
\\v vertical tab
|
|
|
|
\\0NNN byte with octal value NNN (1 to 3 digits)
|
|
|
|
\\xHH byte with hexadecimal value HH (1 to 2 digits)");
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2013-12-01 09:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if matches.opt_present("version") {
|
2014-06-08 07:56:37 +00:00
|
|
|
println!("echo version: {:s}", VERSION);
|
|
|
|
return 0;
|
2013-12-01 09:15:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-01 21:18:56 +00:00
|
|
|
if !matches.free.is_empty() {
|
2013-12-01 09:15:07 +00:00
|
|
|
let string = matches.free.connect(" ");
|
|
|
|
if matches.opt_present("e") {
|
|
|
|
let mut prev_was_slash = false;
|
2014-05-23 12:28:40 +00:00
|
|
|
let mut iter = string.as_slice().chars().enumerate();
|
2013-12-01 09:15:07 +00:00
|
|
|
loop {
|
|
|
|
match iter.next() {
|
|
|
|
Some((index, c)) => {
|
|
|
|
if !prev_was_slash {
|
|
|
|
if c != '\\' {
|
|
|
|
print_char(c);
|
|
|
|
} else {
|
|
|
|
prev_was_slash = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prev_was_slash = false;
|
|
|
|
match c {
|
|
|
|
'\\' => print_char('\\'),
|
|
|
|
'a' => print_char('\x07'),
|
|
|
|
'b' => print_char('\x08'),
|
|
|
|
'c' => break,
|
|
|
|
'e' => print_char('\x1B'),
|
|
|
|
'f' => print_char('\x0C'),
|
|
|
|
'n' => print_char('\n'),
|
|
|
|
'r' => print_char('\r'),
|
|
|
|
't' => print_char('\t'),
|
|
|
|
'v' => print_char('\x0B'),
|
|
|
|
'x' => {
|
2014-05-23 12:28:40 +00:00
|
|
|
let (c, num_char_used) = convert_str(string.as_slice(), index + 1, 16u);
|
2013-12-02 01:00:44 +00:00
|
|
|
if num_char_used == 0 {
|
2013-12-01 09:15:07 +00:00
|
|
|
print_char('\\');
|
|
|
|
print_char('x');
|
|
|
|
} else {
|
2013-12-02 01:00:44 +00:00
|
|
|
print_char(c);
|
|
|
|
for _ in range(0, num_char_used) {
|
|
|
|
iter.next(); // consume used characters
|
|
|
|
}
|
2013-12-01 09:15:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'0' => {
|
2014-05-23 12:28:40 +00:00
|
|
|
let (c, num_char_used) = convert_str(string.as_slice(), index + 1, 8u);
|
2013-12-02 01:00:44 +00:00
|
|
|
if num_char_used == 0 {
|
2013-12-01 09:15:07 +00:00
|
|
|
print_char('\\');
|
|
|
|
print_char('0');
|
|
|
|
} else {
|
2013-12-02 01:00:44 +00:00
|
|
|
print_char(c);
|
|
|
|
for _ in range(0, num_char_used) {
|
|
|
|
iter.next(); // consume used characters
|
|
|
|
}
|
2013-12-01 09:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
print_char('\\');
|
|
|
|
print_char(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-05-23 12:28:40 +00:00
|
|
|
print(string.as_slice());
|
2013-12-01 09:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !matches.opt_present("n") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("")
|
2013-12-01 09:15:07 +00:00
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
0
|
2013-12-01 09:15:07 +00:00
|
|
|
}
|