diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/wordexp-rs/src/lib.rs | 40 | ||||
-rw-r--r-- | lib/wordexp-rs/src/ll.rs | 15 |
2 files changed, 50 insertions, 5 deletions
diff --git a/lib/wordexp-rs/src/lib.rs b/lib/wordexp-rs/src/lib.rs index fde8c00..9ae7538 100644 --- a/lib/wordexp-rs/src/lib.rs +++ b/lib/wordexp-rs/src/lib.rs @@ -5,6 +5,13 @@ use std::ffi::CStr; mod ll; +pub const WRDE_DOOFFS: i32 = (1 << 0); +pub const WRDE_APPEND: i32 = (1 << 1); +pub const WRDE_NOCMD: i32 = (1 << 2); +pub const WRDE_REUSE: i32 = (1 << 3); +pub const WRDE_SHOWERR: i32 = (1 << 4); +pub const WRDE_UNDEF: i32 = (1 << 5); + trait ToCStr { fn to_c_str(&self) -> CString; } @@ -23,7 +30,6 @@ pub struct Wordexp<'a> { } impl<'a> Wordexp<'a> { - pub fn new(wordexp_ref: ll::wordexp_t) -> Self { let we_wordc: usize = wordexp_ref.we_wordc as usize; @@ -69,7 +75,28 @@ impl<'a> std::iter::Iterator for Wordexp<'a> { } } -pub fn wordexp<'a>(s: &str, flags: i32) -> Wordexp +#[derive(Clone, Debug)] +pub struct WordexpError { + pub error_type: i32, +} + +impl WordexpError { + pub fn new(error_type: i32) -> Self + { + WordexpError { error_type } + } +} + +impl std::fmt::Display for WordexpError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result + { + write!(f, "{}", self.error_type) + } +} + +impl std::error::Error for WordexpError {} + +pub fn wordexp<'a>(s: &str, flags: i32) -> Result<Wordexp, WordexpError> { let mut wordexp = ll::wordexp_t { we_wordc: 0, @@ -77,9 +104,12 @@ pub fn wordexp<'a>(s: &str, flags: i32) -> Wordexp we_offs: 0, }; + let result: i32; unsafe { - ll::wordexp(s.to_c_str().as_ptr(), &mut wordexp, flags); + result = ll::wordexp(s.to_c_str().as_ptr(), &mut wordexp, flags); + match result { + 0 => Ok(Wordexp::new(wordexp)), + _ => Err(WordexpError::new(result)), + } } - - Wordexp::new(wordexp) } diff --git a/lib/wordexp-rs/src/ll.rs b/lib/wordexp-rs/src/ll.rs index 0967448..f047443 100644 --- a/lib/wordexp-rs/src/ll.rs +++ b/lib/wordexp-rs/src/ll.rs @@ -16,6 +16,21 @@ impl std::ops::Drop for wordexp_t { } extern "C" { +/* + pub static WRDE_APPEND: i32; + pub static WRDE_DOOFFS: i32; + pub static WRDE_NOCMD: i32; + pub static WRDE_REUSE: i32; + pub static WRDE_SHOWERR: i32; + pub static WRDE_UNDEF: i32; + + pub static WRDE_BADCHAR: i32; + pub static WRDE_BADVAL: i32; + pub static WRDE_CMDSUB: i32; + pub static WRDE_NOSPACE: i32; + pub static WRDE_SYNTAX: i32; +*/ + pub fn wordexp(_: *const libc::c_char, _: &mut wordexp_t, _: i32) -> i32; pub fn wordfree(_: &mut wordexp_t); } |