|
| 1 | +use std::ffi::{CStr, CString, OsStr, OsString}; |
| 2 | +use std::os::unix::ffi::OsStrExt; |
| 3 | + |
| 4 | +use crate::cshadow as c; |
| 5 | + |
| 6 | +/// Parses a string as a list of arguments following the shell's parsing rules. This |
| 7 | +/// uses `g_shell_parse_argv()` for parsing. |
| 8 | +#[allow(dead_code)] |
| 9 | +fn parse_string_as_args(args_str: &OsStr) -> Result<Vec<OsString>, String> { |
| 10 | + if args_str.len() == 0 { |
| 11 | + return Ok(Vec::new()); |
| 12 | + } |
| 13 | + |
| 14 | + let args_str = CString::new(args_str.as_bytes()).unwrap(); |
| 15 | + |
| 16 | + // parse the argument string |
| 17 | + let mut argc: libc::c_int = 0; |
| 18 | + let mut argv: *mut *mut libc::c_char = std::ptr::null_mut(); |
| 19 | + let mut error: *mut libc::c_char = std::ptr::null_mut(); |
| 20 | + let rv = unsafe { c::process_parseArgStr(args_str.as_ptr(), &mut argc, &mut argv, &mut error) }; |
| 21 | + |
| 22 | + // if there was an error, return a copy of the error string |
| 23 | + if !rv { |
| 24 | + let error_message = match error.is_null() { |
| 25 | + false => unsafe { CStr::from_ptr(error) }.to_str().unwrap(), |
| 26 | + true => "Unknown parsing error", |
| 27 | + } |
| 28 | + .to_string(); |
| 29 | + |
| 30 | + unsafe { c::process_parseArgStrFree(argv, error) }; |
| 31 | + return Err(error_message); |
| 32 | + } |
| 33 | + |
| 34 | + assert!(!argv.is_null()); |
| 35 | + |
| 36 | + // copy the arg strings |
| 37 | + let args: Vec<_> = (0..argc) |
| 38 | + .map(|x| unsafe { |
| 39 | + let arg_ptr = *argv.add(x as usize); |
| 40 | + assert!(!arg_ptr.is_null()); |
| 41 | + OsStr::from_bytes(CStr::from_ptr(arg_ptr).to_bytes()).to_os_string() |
| 42 | + }) |
| 43 | + .collect(); |
| 44 | + |
| 45 | + unsafe { c::process_parseArgStrFree(argv, error) }; |
| 46 | + Ok(args) |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod tests { |
| 51 | + use super::*; |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn test_parse_args() { |
| 55 | + let arg_str = r#"the quick brown fox "jumped over" the "\"lazy\" dog""#; |
| 56 | + let expected_args = &[ |
| 57 | + "the", |
| 58 | + "quick", |
| 59 | + "brown", |
| 60 | + "fox", |
| 61 | + "jumped over", |
| 62 | + "the", |
| 63 | + "\"lazy\" dog", |
| 64 | + ]; |
| 65 | + |
| 66 | + let arg_str: OsString = arg_str.into(); |
| 67 | + let args = parse_string_as_args(&arg_str).unwrap(); |
| 68 | + |
| 69 | + assert_eq!(args, expected_args); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_parse_args_empty() { |
| 74 | + let arg_str = ""; |
| 75 | + let expected_args: &[&str] = &[]; |
| 76 | + |
| 77 | + let arg_str: OsString = arg_str.into(); |
| 78 | + let args = parse_string_as_args(&arg_str).unwrap(); |
| 79 | + |
| 80 | + assert_eq!(args, expected_args); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_parse_args_error() { |
| 85 | + let arg_str = r#"hello "world"#; |
| 86 | + |
| 87 | + let arg_str: OsString = arg_str.into(); |
| 88 | + let err_str = parse_string_as_args(&arg_str).unwrap_err(); |
| 89 | + |
| 90 | + assert!(err_str.len() != 0); |
| 91 | + } |
| 92 | +} |
0 commit comments