2024-04-02 02:37:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
|
|
|
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser 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 Lesser General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
|
2024-04-12 02:33:42 +00:00
|
|
|
use std::ffi::{ c_int, c_char, CString, CStr };
|
2024-04-02 02:37:35 +00:00
|
|
|
|
2024-06-04 20:28:19 +00:00
|
|
|
/* binding to getopt(3p) */
|
|
|
|
extern "C" {
|
|
|
|
static mut optarg: *mut c_char;
|
|
|
|
static mut _opterr: c_int;
|
|
|
|
static mut optind: c_int;
|
|
|
|
static mut optopt: c_int;
|
|
|
|
|
|
|
|
fn getopt(
|
|
|
|
___argc: c_int,
|
|
|
|
___argv: *const *mut c_char,
|
|
|
|
__shortopts: *const c_char,
|
|
|
|
) -> c_int;
|
|
|
|
}
|
|
|
|
|
2024-04-02 02:37:35 +00:00
|
|
|
pub struct Opt {
|
|
|
|
pub arg: Option<String>,
|
2024-06-05 17:54:55 +00:00
|
|
|
ind: *mut i32,
|
2024-04-13 23:11:04 +00:00
|
|
|
pub opt: String,
|
2024-04-02 02:37:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 17:54:55 +00:00
|
|
|
impl Opt {
|
|
|
|
pub fn index(&self) -> usize { unsafe { *self.ind as usize } }
|
|
|
|
|
|
|
|
pub fn set_index(&self, ind: i32) { unsafe { *self.ind = ind; } }
|
|
|
|
}
|
|
|
|
|
2024-04-02 02:37:35 +00:00
|
|
|
pub enum OptError {
|
2024-04-13 23:11:04 +00:00
|
|
|
MissingArg(String),
|
|
|
|
UnknownOpt(String),
|
2024-04-02 02:37:35 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 05:51:52 +00:00
|
|
|
/* function signature */
|
2024-04-02 02:37:35 +00:00
|
|
|
pub trait GetOpt {
|
2024-04-12 02:21:01 +00:00
|
|
|
fn getopt(&self, optstring: &str) -> Option<Result<Opt, OptError>>;
|
2024-04-02 02:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GetOpt for Vec<String> {
|
2024-04-12 02:33:42 +00:00
|
|
|
fn getopt(&self, optstring: &str) -> Option<Result<Opt, OptError>> {
|
|
|
|
let c_strings: Vec<_> = self
|
2024-04-12 02:21:01 +00:00
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.map(CString::new)
|
|
|
|
.map(Result::unwrap)
|
2024-04-12 02:33:42 +00:00
|
|
|
.collect();
|
2024-04-02 02:37:35 +00:00
|
|
|
|
2024-04-12 05:51:52 +00:00
|
|
|
/* these operations must be separated out into separate operations so
|
|
|
|
* the CStrings can live long enough */
|
2024-04-12 02:33:42 +00:00
|
|
|
let argv: Vec<_> = c_strings.iter().map(|x| x.as_ptr()).collect();
|
|
|
|
let argv_ptr = argv.as_ptr() as *const *mut c_char;
|
2024-04-12 05:51:52 +00:00
|
|
|
let opts = CString::new(optstring).unwrap().into_raw();
|
2024-04-12 02:33:42 +00:00
|
|
|
let len = self.len() as c_int;
|
|
|
|
|
2024-06-04 20:28:19 +00:00
|
|
|
unsafe {
|
2024-04-14 05:42:11 +00:00
|
|
|
match getopt(len, argv_ptr, opts) {
|
2024-04-02 02:37:35 +00:00
|
|
|
/* From getopt(3p):
|
|
|
|
*
|
2024-06-05 01:15:22 +00:00
|
|
|
* The getopt() f unction shall return the next option character
|
2024-04-02 02:37:35 +00:00
|
|
|
* specified on the command line.
|
|
|
|
*
|
|
|
|
* A <colon> (':') shall be returned if getopt() detects a
|
|
|
|
* missing argument and the first character of optstring was a
|
|
|
|
* <colon> (':').
|
|
|
|
*
|
|
|
|
* A <question-mark> ('?') shall be returned if getopt()
|
|
|
|
* encounters an option character not in optstring or detects a
|
|
|
|
* missing argument and the first character of optstring was not
|
|
|
|
* a <colon> (':').
|
|
|
|
*
|
|
|
|
* Otherwise, getopt() shall return -1 when all command line
|
|
|
|
* options are parsed. */
|
2024-06-05 17:54:55 +00:00
|
|
|
58 => { /* numerical ASCII value for ':' */
|
|
|
|
Some(Err(OptError::MissingArg(optopt.to_string())))
|
|
|
|
},
|
|
|
|
63 => { /* numerical ASCII value for '?' */
|
|
|
|
Some(Err(OptError::UnknownOpt(optopt.to_string())))
|
|
|
|
},
|
2024-04-02 02:37:35 +00:00
|
|
|
/* From getopt(3p):
|
|
|
|
*
|
|
|
|
* If, when getopt() is called:
|
|
|
|
*
|
|
|
|
* argv[optind] is a null pointer
|
|
|
|
* *argv[optind] is not the character -
|
|
|
|
* argv[optind] points to the string "-"
|
|
|
|
*
|
|
|
|
* getopt() shall return -1 without changing optind. If:
|
|
|
|
*
|
|
|
|
* argv[optind] points to the string "--"
|
|
|
|
*
|
|
|
|
* getopt() shall return -1 after incrementing optind. */
|
2024-04-12 02:33:42 +00:00
|
|
|
-1 => return None,
|
2024-04-13 23:11:04 +00:00
|
|
|
opt => {
|
|
|
|
let arg = CStr::from_ptr(optarg)
|
|
|
|
.to_string_lossy()
|
|
|
|
.into_owned();
|
2024-04-02 02:37:35 +00:00
|
|
|
|
2024-04-13 23:11:04 +00:00
|
|
|
Some(Ok(Opt {
|
2024-04-14 05:42:11 +00:00
|
|
|
arg: Some(arg), /* opt argument */
|
2024-06-05 01:15:22 +00:00
|
|
|
/* From getopt(3p):
|
|
|
|
*
|
|
|
|
* The variable optind is the index of the next element
|
|
|
|
* of the argv[] vector to be processed. It shall be
|
|
|
|
* initialized to 1 by the system, and getopt() shall
|
|
|
|
* update it when it finishes with each element of
|
|
|
|
* argv[]. If the application sets optind to zero
|
|
|
|
* before calling getopt(), the behavior is unspecified.
|
|
|
|
* When an element of argv[] contains multiple option
|
|
|
|
* characters, it is unspecified how getopt()
|
|
|
|
* determines which options have already been processed.
|
|
|
|
*
|
2024-06-05 17:54:55 +00:00
|
|
|
* This API is can be utilized with the index() and
|
|
|
|
* set_index() methods implemented for Opt. */
|
2024-06-05 01:15:22 +00:00
|
|
|
ind: std::ptr::addr_of_mut!(optind),
|
2024-04-14 05:42:11 +00:00
|
|
|
opt: opt.to_string(), /* option itself */
|
2024-04-13 23:11:04 +00:00
|
|
|
}))
|
|
|
|
},
|
|
|
|
}
|
2024-04-12 02:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-02 02:37:35 +00:00
|
|
|
}
|