nanosm/src/WordExp.hpp

30 lines
899 B
C++

#pragma once
#include <string>
#include <string_view>
#include <vector>
namespace nanosm {
/// A kind-of reimplementation of wordexp(3) in C++, since it's rife with security issues,
/// annoying, and very poorly implemented (one implementation actually
/// ends up spawning a shell, that end up running Perl code. I'm not kidding.)
struct WordExp {
std::vector<std::string> words;
/// Expand a string (e.g "hello world \"testing 1234\"") into individual parts.
/// This function also respects quotation marks.
///
/// This function does NOT:
/// - Expand environment strings (e.g: $PWD or etc.)
/// - Expand shell ~~injection~~ strings (eg `uname -r`)
///
/// For the sample input, the returned object's `words` vector would look like:
/// [0]: hello
/// [1]: world
/// [2]: testing 1234
static WordExp Expand(std::string_view string);
};
} // namespace nanosm