BastionGuard/src/wizard/shell_quote.hpp

22 lines
650 B
C++

#pragma once
#include <string>
namespace BastionGuard::wizard {
// Quote a single argument for POSIX-compatible shells. In particular, a
// single quote inside the argument must not terminate the quoted string.
// Prefer bg_process::run(argv) where a shell is not actually required.
inline std::string shell_quote(const std::string& argument) {
std::string quoted;
quoted.reserve(argument.size() + 2);
quoted.push_back('\'');
for (const char ch : argument) {
if (ch == '\'') quoted += "'\"'\"'";
else quoted.push_back(ch);
}
quoted.push_back('\'');
return quoted;
}
} // namespace BastionGuard::wizard