-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshell.cppm
More file actions
60 lines (49 loc) · 1.75 KB
/
shell.cppm
File metadata and controls
60 lines (49 loc) · 1.75 KB
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
51
52
53
54
55
56
57
58
59
60
// mcpp.platform.shell — platform-aware shell quoting and redirect helpers.
//
// Provides shell-safe argument quoting for command construction:
// POSIX: single-quote wrapping ('arg')
// Windows: double-quote wrapping ("arg")
//
// NOTE on Windows: do NOT use quote() for the FIRST token in a
// popen/system command string — cmd.exe strips a leading " pair.
// Use the raw path string as the first token; quote() is safe for
// arguments only.
module;
export module mcpp.platform.shell;
import std;
export namespace mcpp::platform::shell {
// Platform-aware shell argument quoting.
std::string quote(std::string_view s);
// Silent redirect — stdout + stderr → /dev/null (or NUL on Windows).
// stdin is NOT touched here; that's the responsibility of
// mcpp::platform::process::seal_stdin, which is auto-applied by capture /
// run_silent / run_streaming on all platforms.
#if defined(_WIN32)
constexpr std::string_view silent_redirect = ">nul 2>&1";
#else
constexpr std::string_view silent_redirect = ">/dev/null 2>&1";
#endif
} // namespace mcpp::platform::shell
// ─── Implementation ──────────────────────────────────────────────────────
namespace mcpp::platform::shell {
std::string quote(std::string_view s) {
std::string out;
out.reserve(s.size() + 2);
#if defined(_WIN32)
out.push_back('"');
for (char c : s) {
if (c == '"') out += "\\\"";
else out.push_back(c);
}
out.push_back('"');
#else
out.push_back('\'');
for (char c : s) {
if (c == '\'') out += "'\\''";
else out.push_back(c);
}
out.push_back('\'');
#endif
return out;
}
} // namespace mcpp::platform::shell