88 lines
2.3 KiB
Rust
88 lines
2.3 KiB
Rust
/*
|
||
* 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;
|
||
extern crate sysexits;
|
||
|
||
use getopt::GetOpt;
|
||
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 optind = 1;
|
||
let mut reqs = Reqs {
|
||
ascii: false, blank: false, cntrl: false, digit: false, lower: false,
|
||
upper: false, inuse: false, extra: String::new()
|
||
};
|
||
|
||
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() == optind { return usage(&argv[0]); }
|
||
|
||
drop(argv);
|
||
|
||
if reqs.inuse {
|
||
for arg in args().skip(optind) {
|
||
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;
|
||
} else {
|
||
return ExitCode::FAILURE;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
ExitCode::SUCCESS
|
||
}
|