stris(1): modernize code

This commit is contained in:
dtb 2024-08-28 18:50:21 -06:00
parent 1796e772fb
commit 72c3ac5df0
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -23,9 +23,9 @@ use std::{
};
extern crate getopt;
use getopt::{ Opt, Parser };
extern crate sysexits;
use getopt::GetOpt;
use sysexits::EX_USAGE;
struct Reqs {
@ -34,45 +34,39 @@ struct Reqs {
}
fn usage(s: &str) -> ExitCode {
eprintln!("Usage: {} (-7bcdlu) (-i [inclusions]) [strings...]", s);
eprintln!("Usage: {} [-7bcdlu] [-i inclusions] [strings...]", s);
ExitCode::from(EX_USAGE as u8)
}
fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>();
let mut opts = Parser::new(&argv, "7bcdi:lu");
let mut optind = 1;
let mut reqs = Reqs {
ascii: false, blank: false, cntrl: false, digit: false, lower: false,
upper: false, inuse: false, extra: String::new()
};
loop {
match opts.next() {
None => break,
Some(opt) => {
match opt {
Ok(Opt('7', None)) => reqs.ascii = true,
Ok(Opt('b', None)) => reqs.blank = true,
Ok(Opt('c', None)) => reqs.cntrl = true,
Ok(Opt('d', None)) => reqs.digit = true,
Ok(Opt('i', Some(arg))) => reqs.extra = arg,
Ok(Opt('l', None)) => reqs.lower = true,
Ok(Opt('u', None)) => reqs.upper = true,
_ => { return usage(&argv[0]); }
}
reqs.inuse = true;
}
while let Some(opt) = argv.getopt("7bcdi:lu") {
match opt.opt() {
Ok("7") => reqs.ascii = true,
Ok("b") => reqs.blank = true,
Ok("c") => reqs.cntrl = true,
Ok("d") => reqs.digit = true,
Ok("i") => reqs.extra = opt.arg().unwrap(),
Ok("l") => reqs.lower = true,
Ok("u") => reqs.upper = true,
_ => { return usage(&argv[0]); }
}
optind = opt.ind();
reqs.inuse = true;
}
if argv.len() == opts.index() {
return usage(&argv[0]);
}
if argv.len() == optind { return usage(&argv[0]); }
drop(argv);
if reqs.inuse {
for arg in args().skip(opts.index()) {
for arg in args().skip(optind) {
for c in arg.chars() {
if (reqs.ascii && c.is_ascii())
|| (reqs.blank && c.is_whitespace())