cyfs_util/util/
process_util.rs1use std::process::Command;
2
3pub struct ProcessUtil;
4
5impl ProcessUtil {
6 pub fn parse_cmd(cmd: &str) -> Vec<&str> {
10 let mut args = Vec::new();
11 let mut next_quot: Option<char> = None;
12 let mut prev_index = 0;
13 for (i, c) in cmd.chars().enumerate() {
14 if c == '"' || c == '\'' {
15 if next_quot.is_none() {
16 next_quot = Some(c);
17 } else if *next_quot.as_ref().unwrap() != c {
18 continue;
19 } else {
20 next_quot = None;
21
22 let arg = &cmd[prev_index..(i + 1)];
23 let arg = arg.trim_start_matches(c).trim_end_matches(c);
24 if !arg.is_empty() {
25 args.push(arg);
26 }
27
28 prev_index = i + 1;
29 }
30 } else if c.is_whitespace() && next_quot.is_none() {
31 let arg = &cmd[prev_index..(i + 1)];
32 let arg = arg.trim_start().trim_end();
33 if !arg.is_empty() {
34 args.push(arg);
35 }
36
37 prev_index = i + 1;
38 }
39 }
40
41 if prev_index < cmd.len() {
43 let arg = &cmd[prev_index..cmd.len()];
44 let arg = arg.trim_start().trim_end();
45 if !arg.is_empty() {
46 args.push(arg);
47 }
48 }
49 args
50 }
51
52 #[cfg(target_os = "windows")]
54 pub fn detach(cmd: &mut Command) {
55 use std::os::windows::process::CommandExt;
56
57 pub const DETACHED_PROCESS: u32 = 0x00000008;
58 pub const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
59 pub const CREATE_NO_WINDOW: u32 = 0x08000000;
60
61 let flags = DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW;
62 cmd.creation_flags(flags);
63 }
64
65 #[cfg(not(target_os = "windows"))]
66 pub fn detach(_cmd: &mut Command) {}
67}
68
69