1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Starting a program: the setup every program Mortise starts shares, and the
//! split of a command line into words.
use std::ffi::OsStr;
use std::process::{Command, Stdio};
/// A command with no standard input and, on Windows, no console window.
pub(crate) fn command(program: impl AsRef<OsStr>) -> Command {
let mut command = Command::new(program);
command.stdin(Stdio::null());
// The app has no console on Windows, so each run of a program would open a window.
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
command.creation_flags(CREATE_NO_WINDOW);
}
command
}
/// Splits a command into words at spaces and tabs. A part in double quotes is one
/// word. A backslash is an ordinary letter, so a Windows path needs no escape.
pub(crate) fn split_words(command: &str) -> Vec<String> {
let mut words: Vec<String> = Vec::new();
let mut word = String::new();
let mut quoted = false;
let mut started = false;
for letter in command.chars() {
match letter {
'"' => {
quoted = !quoted;
started = true;
}
' ' | '\t' if !quoted => {
if started {
words.push(std::mem::take(&mut word));
started = false;
}
}
_ => {
word.push(letter);
started = true;
}
}
}
if started {
words.push(word);
}
words
}