diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-26 18:20:50 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-26 18:20:50 -0500 |
commit | 1f4be08130e8e762382773b534784358ffb44003 (patch) | |
tree | 85736e25c2b16bbfe2e007261a9d0f7884f33f60 | |
parent | 4f25f859124b426bf25e171d20ede082b797b23f (diff) |
update wordexp-rs code
-rw-r--r-- | lib/wordexp-rs/src/lib.rs | 40 | ||||
-rw-r--r-- | lib/wordexp-rs/src/ll.rs | 15 | ||||
-rw-r--r-- | src/joshuto/command.rs | 14 |
3 files changed, 60 insertions, 9 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); } diff --git a/src/joshuto/command.rs b/src/joshuto/command.rs index 255d9c0..b811370 100644 --- a/src/joshuto/command.rs +++ b/src/joshuto/command.rs @@ -96,10 +96,16 @@ pub fn from_args(command: &str, args: Option<&Vec<String>>) -> Option<Box<dyn Jo match command { "cd" => { if let Some(args) = args { - let exp_strs = wordexp::wordexp(args[0].as_str(), 0); - for exp_str in exp_strs { - let path = path::PathBuf::from(exp_str); - return Some(Box::new(self::ChangeDirectory::new(path))); + match wordexp::wordexp(args[0].as_str(), 0) { + Ok(exp_strs) => { + for exp_str in exp_strs { + let path = path::PathBuf::from(exp_str); + return Some(Box::new(self::ChangeDirectory::new(path))); + } + }, + Err(e) => { + eprintln!("Error: Failed to parse {:?}", args); + } } } return None; |