From 19a9786bbfab5bd5d2d5fc7e08bb2f02120f98c3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 28 Jan 2025 18:41:25 +0100 Subject: [PATCH] impr: Make ImHex cli work more like other tools --- lib/libimhex/source/subcommands/subcommands.cpp | 12 ++++++------ main/gui/source/messaging/linux.cpp | 6 +++--- .../source/content/command_line_interface.cpp | 3 +++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/libimhex/source/subcommands/subcommands.cpp b/lib/libimhex/source/subcommands/subcommands.cpp index 6c9e88db7..03f6bd9f0 100644 --- a/lib/libimhex/source/subcommands/subcommands.cpp +++ b/lib/libimhex/source/subcommands/subcommands.cpp @@ -53,7 +53,7 @@ namespace hex::subcommands { while (argsIter != args.end()) { const std::string &arg = *argsIter; - if (arg == "--othercmd") { + if (!currentSubCommandArgs.empty() && arg.starts_with("--")) { // Save command to run if (currentSubCommand) { subCommands.emplace_back(*currentSubCommand, currentSubCommandArgs); @@ -61,10 +61,10 @@ namespace hex::subcommands { currentSubCommand = std::nullopt; currentSubCommandArgs = { }; - } else if (currentSubCommand) { // Add current argument to the current command currentSubCommandArgs.push_back(arg); + argsIter += 1; } else { // Get next subcommand from current argument currentSubCommand = findSubCommand(arg); @@ -72,9 +72,9 @@ namespace hex::subcommands { log::error("No subcommand named '{}' found", arg); exit(EXIT_FAILURE); } - } - argsIter += 1; + argsIter += 1; + } } // Save last command to run @@ -83,8 +83,8 @@ namespace hex::subcommands { } // Run the subcommands - for (auto &[subcommand, args] : subCommands) { - subcommand.callback(args); + for (auto &[subcommand, subCommandArgs] : subCommands) { + subcommand.callback(subCommandArgs); } // Exit the process if it's not the main instance (the commands have been forwarded to another instance) diff --git a/main/gui/source/messaging/linux.cpp b/main/gui/source/messaging/linux.cpp index e0c0dc8ca..1a35325e1 100644 --- a/main/gui/source/messaging/linux.cpp +++ b/main/gui/source/messaging/linux.cpp @@ -38,7 +38,7 @@ namespace hex::messaging { if (mkfifo(CommunicationPipePath, 0600) < 0) return; static int fifo = 0; - fifo = open(CommunicationPipePath, O_RDWR); + fifo = open(CommunicationPipePath, O_RDWR | O_NONBLOCK); static auto listenerThread = std::jthread([](const std::stop_token &stopToken){ std::vector buffer(0xFFFF); @@ -46,9 +46,9 @@ namespace hex::messaging { int result = ::read(fifo, buffer.data(), buffer.size()); if (result > 0) { EventNativeMessageReceived::post(std::vector{ buffer.begin(), buffer.begin() + result }); + } else { + std::this_thread::sleep_for(std::chrono::seconds(1)); } - - std::this_thread::sleep_for(std::chrono::seconds(1)); } }); diff --git a/plugins/builtin/source/content/command_line_interface.cpp b/plugins/builtin/source/content/command_line_interface.cpp index b815c008b..0302a865b 100644 --- a/plugins/builtin/source/content/command_line_interface.cpp +++ b/plugins/builtin/source/content/command_line_interface.cpp @@ -388,6 +388,9 @@ namespace hex::plugin::builtin { void registerCommandForwarders() { hex::subcommands::registerSubCommand("open", [](const std::vector &args){ for (auto &arg : args) { + if (arg.starts_with("--")) + break; + RequestOpenFile::post(arg); } });