forked from bonsai/harakit
stris(1)
This commit is contained in:
93
src/stris.rs
Normal file
93
src/stris.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||
* Copyright (c) 2023 Marceline Cramer <mars@tebibyte.media>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU Affero General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option) any
|
||||
* later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
use std::{
|
||||
env::args,
|
||||
process::ExitCode
|
||||
};
|
||||
|
||||
extern crate getopt;
|
||||
use getopt::{ Opt, Parser };
|
||||
|
||||
extern crate sysexits;
|
||||
use sysexits::EX_USAGE;
|
||||
|
||||
struct Reqs {
|
||||
ascii: bool, blank: bool, cntrl: bool, digit: bool, lower: bool,
|
||||
upper: bool, inuse: bool, extra: String
|
||||
}
|
||||
|
||||
fn usage(s: &str) -> ExitCode {
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if argv.len() == opts.index() {
|
||||
return usage(&argv[0]);
|
||||
}
|
||||
|
||||
drop(argv);
|
||||
|
||||
if reqs.inuse {
|
||||
'args: for arg in args().skip(opts.index()) {
|
||||
for c in arg.chars() {
|
||||
if (reqs.ascii && c.is_ascii())
|
||||
|| (reqs.blank && c.is_whitespace())
|
||||
|| (reqs.cntrl && c.is_control())
|
||||
|| (reqs.digit && c.is_numeric())
|
||||
|| (reqs.lower && c.is_lowercase())
|
||||
|| (reqs.upper && c.is_uppercase())
|
||||
|| reqs.extra.contains(c) {
|
||||
continue 'args;
|
||||
} else {
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Reference in New Issue
Block a user