forked from bonsai/harakit
getopt.rs(3): safe api around optind reading and setting
This commit is contained in:
parent
05cde15105
commit
e862b7fec6
@ -34,10 +34,16 @@ extern "C" {
|
||||
|
||||
pub struct Opt {
|
||||
pub arg: Option<String>,
|
||||
pub ind: *mut i32,
|
||||
ind: *mut i32,
|
||||
pub opt: String,
|
||||
}
|
||||
|
||||
impl Opt {
|
||||
pub fn index(&self) -> usize { unsafe { *self.ind as usize } }
|
||||
|
||||
pub fn set_index(&self, ind: i32) { unsafe { *self.ind = ind; } }
|
||||
}
|
||||
|
||||
pub enum OptError {
|
||||
MissingArg(String),
|
||||
UnknownOpt(String),
|
||||
@ -82,12 +88,12 @@ impl GetOpt for Vec<String> {
|
||||
*
|
||||
* Otherwise, getopt() shall return -1 when all command line
|
||||
* options are parsed. */
|
||||
58 => { /* numerical ASCII value for ':' */
|
||||
Some(Err(OptError::MissingArg(optopt.to_string())))
|
||||
},
|
||||
63 => { /* numerical ASCII value for '?' */
|
||||
Some(Err(OptError::UnknownOpt(optopt.to_string())))
|
||||
},
|
||||
58 => { /* numerical ASCII value for ':' */
|
||||
Some(Err(OptError::MissingArg(optopt.to_string())))
|
||||
},
|
||||
63 => { /* numerical ASCII value for '?' */
|
||||
Some(Err(OptError::UnknownOpt(optopt.to_string())))
|
||||
},
|
||||
/* From getopt(3p):
|
||||
*
|
||||
* If, when getopt() is called:
|
||||
@ -121,9 +127,8 @@ impl GetOpt for Vec<String> {
|
||||
* characters, it is unspecified how getopt()
|
||||
* determines which options have already been processed.
|
||||
*
|
||||
* This API is unsafe and can be utilized by
|
||||
* dereferencing the ind field before reading to and
|
||||
* writing from the optind memory. */
|
||||
* This API is can be utilized with the index() and
|
||||
* set_index() methods implemented for Opt. */
|
||||
ind: std::ptr::addr_of_mut!(optind),
|
||||
opt: opt.to_string(), /* option itself */
|
||||
}))
|
||||
|
Loading…
Reference in New Issue
Block a user