libcmd: add raw arg access to legacy commands

we'll need this to modify argv for socket-activated daemons. this is our
replacement for the old savedArgv mechanism that was unscoped and fucky.

Change-Id: Ie048eb8ea99f1c9cd627a051292c836c83197068
This commit is contained in:
eldritch horrors
2025-12-07 18:49:34 +00:00
parent aba740a276
commit 6410748c9f
2 changed files with 23 additions and 4 deletions
+17 -3
View File
@@ -5,18 +5,32 @@
#include <functional>
#include <list>
#include <map>
#include <span>
#include <string>
namespace nix {
typedef std::function<int(AsyncIoRoot &, std::string, std::list<std::string>)> MainFunction;
struct LegacyCommandRegistry
{
using LegacyCommandMap = std::map<std::string, MainFunction>;
typedef std::function<int(AsyncIoRoot &, std::string, std::list<std::string>)> MainFunction;
typedef std::function<
int(AsyncIoRoot &, std::string, std::list<std::string>, std::span<char *>)>
RawMainFunction;
using LegacyCommandMap = std::map<std::string, RawMainFunction>;
static LegacyCommandMap * commands;
static void add(const std::string & name, MainFunction fun)
{
addWithRaw(
name,
[fun](AsyncIoRoot & aio, std::string name, std::list<std::string> args, std::span<char *>) {
return fun(aio, name, args);
}
);
}
static void addWithRaw(const std::string & name, RawMainFunction fun)
{
if (!commands) commands = new LegacyCommandMap;
(*commands)[name] = fun;
+6 -1
View File
@@ -491,7 +491,12 @@ int mainWrapped(AsyncIoRoot & aio, int argc, char ** argv)
registerLegacyCommands();
auto legacy = (*LegacyCommandRegistry::commands)[programName];
if (legacy) {
return legacy(aio, std::string(baseNameOf(argv[0])), Strings(argv + 1, argv + argc));
return legacy(
aio,
std::string(baseNameOf(argv[0])),
Strings(argv + 1, argv + argc),
{argv + 1, argv + argc}
);
}
}