14 #include <mach-o/dyld.h>
18 using std::vector, std::string, std::map;
19 namespace fs = std::filesystem;
23 std::string executable_path() {
24 std::error_code error;
26 constexpr std::uint32_t size = 1024;
27 std::vector<char> buffer(size);
28 if (_NSGetExecutablePath(buffer.data(), &size) != 0) {
30 if (_NSGetExecutablePath(buffer.data(), &size) != 0)
return "mach3";
32 const auto path = fs::canonical(buffer.data(), error);
34 const auto path = fs::read_symlink(
"/proc/self/exe", error);
36 return error ?
"mach3" : path.string();
40 std::string shell_quote(
const std::string& value) {
41 std::string result =
"'";
42 for (
const char character : value) {
43 if (character ==
'\'') result +=
"'\\''";
44 else result += character;
53 std::string_view operation =
"creating parent directories";
56 fs::create_directories(path.parent_path());
58 out.exceptions(std::ios::failbit | std::ios::badbit);
59 operation =
"opening";
61 operation =
"writing";
63 operation =
"closing";
66 }
catch (
const std::exception& err) {
67 const int error = errno;
68 std::cerr <<
"Cannot write completion script '" << path.string() <<
"' while " << operation
69 <<
": " << err.what();
70 if (error != 0) std::cerr <<
": " << std::strerror(error);
77 const char* shell = std::getenv(
"SHELL");
78 if (!shell)
return "";
81 const auto name = fs::path(shell).filename().string();
89 const char* home = std::getenv(
"HOME");
90 if (!home || home[0] ==
'\0') {
91 std::cerr <<
"Cannot determine HOME directory\n";
97 std::cerr <<
"Could not detect your shell (check $SHELL is exported). Supported: bash, zsh\n";
103 const std::string binary_path = shell_quote(executable_path());
107 auto embed_path = [&](std::string_view tmpl) -> std::string {
109 const std::string placeholder =
"mach3 --complete";
110 const std::string replacement = binary_path +
" --complete";
111 for (std::size_t pos = s.find(placeholder); pos != std::string::npos;
112 pos = s.find(placeholder, pos + replacement.size()))
113 s.replace(pos, placeholder.size(), replacement);
117 fs::path home_path(home);
119 if (shell ==
"bash") {
120 fs::path path = home_path /
".local/share/bash-completion/completions/mach3";
122 std::cout <<
"Installed bash completions → " << path <<
"\n";
126 if (shell ==
"zsh") {
127 fs::path path = home_path /
".local/share/zsh/site-functions/_mach3";
129 std::cout <<
"Installed zsh completions → " << path <<
"\n";
136 std::cout <<
name <<
"\n";
140 std::vector<std::string> args(argv, argv + argc);
142 MaCh3ArgumentParser::parse_args(args);
147 if (!parser || parser->
name().empty()) {
148 throw std::invalid_argument(
"Module/plugin must provide a non-null parser with a non-empty command name");
150 if (m_subparser_map.count(parser->
name()) != 0) {
151 throw std::invalid_argument(
"Duplicate command name: " + parser->
name());
158 [&module](
const auto& entry) { return entry.second == &module; })) {
159 throw std::invalid_argument(
"Core module is already registered");
163 this->add_subparser(*parser);
173 const char* env = std::getenv(
"MACH3_PLUGINS");
174 if (!env || env[0] ==
'\0')
return;
176 std::stringstream ss(env);
179 while (std::getline(ss, path,
':')) {
181 std::cerr <<
"Ignoring empty plugin path in MACH3_PLUGINS\n";
184 std::vector<std::string> files;
189 catch (
const fs::filesystem_error& err) {
190 std::cerr <<
"Cannot discover plugins in '" << path <<
"': " << err.what() <<
"\n";
193 for (
const auto& sofile : files) {
196 std::cerr <<
"Skipping duplicate plugin library: " << sofile <<
"\n";
200 std::unique_ptr<DynamicPlugin> dlplugin = std::make_unique<DynamicPlugin>(sofile);
203 this->add_subparser(*parser);
206 catch (
const std::exception& e) {
207 std::cerr <<
"Failed to load plugin from " << sofile <<
": " << e.what() <<
"\n";
211 std::cerr <<
"Failed to load plugin from " << sofile <<
": unknown error\n";
226 if (this->is_subcommand_used(*parser))
return module->Run();
229 if (this->is_subcommand_used(*parser))
return dplugin->Run();
243 std::vector<std::string> result;
247 if (!fs::exists(p)) {
248 std::cerr <<
"Plugin path does not exist: " << path <<
"\n";
252 if (fs::is_regular_file(p)) {
254 result.push_back(fs::canonical(p).
string());
256 else if (fs::is_directory(p)) {
258 for (
auto& entry : fs::directory_iterator(p)) {
259 const auto extension = entry.path().extension();
260 bool is_good_plugin = extension ==
".so";
262 is_good_plugin = is_good_plugin || extension ==
".dylib";
265 is_good_plugin = is_good_plugin && entry.is_regular_file();
267 catch (
const fs::filesystem_error& err) {
269 std::cerr <<
"Cannot inspect plugin '" << entry.path().string() <<
"': " << err.what() <<
"\n";
272 if (is_good_plugin) {
274 result.push_back(fs::canonical(entry.path()).string());
276 catch (
const fs::filesystem_error& err) {
277 std::cerr <<
"Cannot canonicalize plugin '" << entry.path().string() <<
"': " << err.what() <<
"\n";
284 std::cerr <<
"Invalid plugin path (not file or directory): " << path <<
"\n";
288 std::sort(result.begin(), result.end());
Core program class for the MaCh3 command-line interface.
Interface for MaCh3 plugins and modules.
virtual MaCh3ArgumentParser * get_parser()=0
Get the argument parser for this plugin.
Extended ArgumentParser with MaCh3-specific functionality.
const std::string name() const
Get the name of this parser/subcommand.
std::vector< std::string > get_completions(const std::string &prefix, const std::vector< std::string > &context) const
Complete subcommands or long options at any depth without parsing incomplete arguments.
void insert_default_subcommands(std::vector< std::string > &args, std::size_t pos=0) const
Insert default subcommands into args where no explicit subcommand was given.
void add_core_module(IModule &module)
Add a core module to the program.
bool write_file(const fs::path &path, std::string_view content) const
Write content to a file, creating parent directories if needed.
static constexpr std::string_view ZSH_COMPLETION
void completions(const std::string &prefix, const std::vector< std::string > &context) const
Generate subcommand or long-option suggestions at the active parser depth.
static constexpr std::string_view BASH_COMPLETION
int install_completions() const
Install shell completion scripts for bash and zsh.
int Run()
Run the selected subcommand.
std::map< const MaCh3ArgumentParser *, IModule * > m_module_map
Map of parsers to core modules (non-owning)
std::set< std::string > m_attempted_plugin_paths
std::map< const MaCh3ArgumentParser *, std::unique_ptr< DynamicPlugin > > m_dynamic_plugin_map
Map of parsers to dynamic plugins (owning)
std::string detect_shell() const
Detect the user's shell from the SHELL environment variable.
void load_dynamic_plugins()
Load dynamic plugins from the MACH3_PLUGINS environment variable.
std::vector< std::string > expand_plugin_path(const std::string &path) const
Expand a plugin path (file or directory) into a list of canonical shared-library paths.
void validate_parser(const MaCh3ArgumentParser *parser) const
void parse_args(int argc, const char *const argv[])
Parse args after inserting any default subcommands.
Main namespace for MaCh3 software.