| 28 | |
| 29 | impl ProcessArguments { |
| 30 | pub fn new(command: &str) -> Self { |
| 31 | let args: Vec<_> = command.split(' ').collect(); |
| 32 | let mut args_copy: Vec<String> = args |
| 33 | .iter() |
| 34 | .filter(|&arg| !arg.is_empty()) |
| 35 | .map(|&arg| { |
| 36 | let mut string = String::new(); |
| 37 | string.push_str(arg); |
| 38 | string.push('\0'); |
| 39 | string |
| 40 | }) |
| 41 | .collect(); |
| 42 | |
| 43 | // redirect input |
| 44 | let mut input = String::new(); |
| 45 | if let Some((idx, _)) = args_copy |
| 46 | .iter() |
| 47 | .enumerate() |
| 48 | .find(|(_, arg)| arg.as_str() == "<\0") |
| 49 | { |
| 50 | input = args_copy[idx + 1].clone(); |
| 51 | args_copy.drain(idx..=idx + 1); |
| 52 | } |
| 53 | |
| 54 | // redirect output |
| 55 | let mut output = String::new(); |
| 56 | if let Some((idx, _)) = args_copy |
| 57 | .iter() |
| 58 | .enumerate() |
| 59 | .find(|(_, arg)| arg.as_str() == ">\0") |
| 60 | { |
| 61 | output = args_copy[idx + 1].clone(); |
| 62 | args_copy.drain(idx..=idx + 1); |
| 63 | } |
| 64 | |
| 65 | let mut args_addr: Vec<*const u8> = args_copy.iter().map(|arg| arg.as_ptr()).collect(); |
| 66 | args_addr.push(core::ptr::null::<u8>()); |
| 67 | |
| 68 | Self { |
| 69 | input, |
| 70 | output, |
| 71 | args_copy, |
| 72 | args_addr, |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #[no_mangle] |