coreutils/seq/seq.rs

219 lines
7.5 KiB
Rust
Raw Normal View History

2014-03-31 16:40:21 +00:00
#![crate_id(name="seq", vers="1.0.0", author="Daniel MacDougall")]
2014-02-05 19:46:28 +00:00
2014-03-31 16:40:21 +00:00
#![feature(macro_rules)]
2014-02-05 19:46:28 +00:00
// TODO: Make -w flag work with decimals
// TODO: Support -f flag
extern crate getopts;
extern crate libc;
2014-02-05 19:46:28 +00:00
use std::os;
2014-02-23 22:17:48 +00:00
#[path = "../common/util.rs"]
mod util;
static NAME: &'static str = "seq";
#[deriving(Clone)]
struct SeqOptions {
separator: String,
terminator: Option<String>,
widths: bool
2014-02-05 19:46:28 +00:00
}
fn parse_float(s: &str) -> Result<f64, String>{
2014-02-05 19:46:28 +00:00
match from_str(s) {
Some(n) => Ok(n),
None => Err(format!("seq: invalid floating point argument: {:s}", s))
}
}
2014-05-25 09:20:52 +00:00
fn escape_sequences(s: &str) -> String {
2014-02-05 19:46:28 +00:00
s.replace("\\n", "\n").
replace("\\t", "\t")
}
fn parse_options(args: Vec<String>, options: &mut SeqOptions) -> Result<Vec<String>, int> {
let mut seq_args = vec!();
let program = args.get(0).clone();
let mut iter = args.move_iter().skip(1);
loop {
match iter.next() {
Some(arg) => match arg.as_slice() {
"--help" | "-h" => {
print_help(&program);
return Err(0);
}
"--version" | "-V" => {
print_version();
return Err(0);
}
"-s" | "--separator" => match iter.next() {
Some(sep) => options.separator = sep,
None => {
show_error!("expected a separator after {}", arg);
return Err(1);
}
},
"-t" | "--terminator" => match iter.next() {
Some(term) => options.terminator = Some(term),
None => {
show_error!("expected a terminator after '{}'", arg);
return Err(1);
}
},
"-w" | "--widths" => options.widths = true,
_ => {
if arg.len() > 1 && arg.as_slice().char_at(0) == '-' {
let argptr: *String = &arg; // escape from the borrow checker
let mut chiter = unsafe { (*argptr).as_slice() }.chars().skip(1);
let mut ch = ' ';
while match chiter.next() { Some(m) => { ch = m; true } None => false } {
match ch {
'h' => {
print_help(&program);
return Err(0);
}
'V' => {
print_version();
return Err(0);
}
's' => match iter.next() {
Some(sep) => {
options.separator = sep;
let next = chiter.next();
if next.is_some() {
show_error!("unexpected character ('{}')", next.unwrap());
return Err(1);
}
}
None => {
show_error!("expected a separator after {}", arg);
return Err(1);
}
},
't' => match iter.next() {
Some(term) => {
options.terminator = Some(term);
let next = chiter.next();
if next.is_some() {
show_error!("unexpected character ('{}')", next.unwrap());
return Err(1);
}
}
None => {
show_error!("expected a terminator after {}", arg);
return Err(1);
}
},
'w' => options.widths = true,
_ => { seq_args.push(arg); break }
}
}
} else {
seq_args.push(arg);
}
}
},
None => break
}
}
Ok(seq_args)
}
2014-05-28 11:43:37 +00:00
fn print_help(program: &String) {
2014-05-30 08:35:54 +00:00
let opts = [
getopts::optopt("s", "separator", "Separator character (defaults to \\n)", ""),
getopts::optopt("t", "terminator", "Terminator character (defaults to separator)", ""),
getopts::optflag("w", "widths", "Equalize widths of all numbers by padding with zeros"),
getopts::optflag("h", "help", "print this help text and exit"),
getopts::optflag("V", "version", "print version and exit"),
2014-02-05 19:46:28 +00:00
];
println!("seq 1.0.0\n");
println!("Usage:\n {} [-w] [-s string] [-t string] [first [step]] last\n", *program);
println!("{:s}", getopts::usage("Print sequences of numbers", opts));
}
fn print_version() {
println!("seq 1.0.0\n");
}
#[allow(dead_code)]
fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone();
let mut options = SeqOptions {
separator: "\n".to_string(),
terminator: None,
widths: false
2014-02-05 19:46:28 +00:00
};
let free = match parse_options(args, &mut options) {
Ok(m) => m,
Err(f) => return f
};
if free.len() < 1 || free.len() > 3 {
crash!(1, "too {} operands.\nTry '{} --help' for more information.",
if free.len() < 1 { "few" } else { "many" }, program);
2014-02-05 19:46:28 +00:00
}
let first = if free.len() > 1 {
match parse_float(free.get(0).as_slice()) {
2014-02-05 19:46:28 +00:00
Ok(n) => n,
2014-06-09 03:26:51 +00:00
Err(s) => { show_error!("{:s}", s); return 1; }
2014-02-05 19:46:28 +00:00
}
} else {
1.0
};
let step = if free.len() > 2 {
match parse_float(free.get(1).as_slice()) {
2014-02-05 19:46:28 +00:00
Ok(n) => n,
2014-06-09 03:26:51 +00:00
Err(s) => { show_error!("{:s}", s); return 1; }
2014-02-05 19:46:28 +00:00
}
} else {
1.0
};
let last = match parse_float(free.get(free.len()-1).as_slice()) {
2014-02-05 19:46:28 +00:00
Ok(n) => n,
2014-06-09 03:26:51 +00:00
Err(s) => { show_error!("{:s}", s); return 1; }
2014-02-05 19:46:28 +00:00
};
let separator = escape_sequences(options.separator.as_slice());
let terminator = match options.terminator {
Some(term) => escape_sequences(term.as_slice()),
None => separator.clone()
};
print_seq(first, step, last, separator, terminator, options.widths);
0
2014-02-05 19:46:28 +00:00
}
#[inline(always)]
fn done_printing(next: f64, step: f64, last: f64) -> bool {
if step > 0f64 {
2014-02-05 19:46:28 +00:00
next > last
} else {
next < last
}
}
fn print_seq(first: f64, step: f64, last: f64, separator: String, terminator: String, pad: bool) {
2014-02-05 19:46:28 +00:00
let mut i = first;
let maxlen = first.max(last).to_str().len();
2014-02-05 19:46:28 +00:00
while !done_printing(i, step, last) {
let ilen = i.to_str().len();
if pad && ilen < maxlen {
for _ in range(0, maxlen - ilen) {
print!("0");
}
}
print!("{:f}", i);
i += step;
if !done_printing(i, step, last) {
print!("{:s}", separator);
}
}
if i != first {
print!("{:s}", terminator);
}
2014-02-05 19:46:28 +00:00
}