hooks: create plugin config from contract
@@ -45,11 +45,56 @@ static int initHooksGopts (KDB * kdb, Plugin * plugin, Key * errorKey)
return 0;
}
-static Plugin * loadPlugin (const char * pluginName, KeySet * modules, Key * errorKey)
+static KeySet * getPluginConfigFromContract(const char * pluginName, const KeySet * contract)
+{
+ KeySet * tmpContract = ksDup (contract);
+ KeySet * config = ksNew (0, KS_END);
+
+ Key * mountContractRoot = keyNew ("system:/elektra/contract/mountglobal", KEY_END);
+ Key * pluginConfigRoot = keyNew ("user:/", KEY_END);
+
+ for (elektraCursor it = ksFindHierarchy (tmpContract, mountContractRoot, NULL); it < ksGetSize (tmpContract); it++)
+ {
+ Key * cur = ksAtCursor (tmpContract, it);
+ if (keyIsDirectlyBelow (mountContractRoot, cur) == 1)
+ {
+ const char * pluginNameOfConfig = keyBaseName (cur);
+ if(strcmp (pluginName, pluginNameOfConfig) != 0)
+ {
+ break;
+ }
+
+ KeySet * pluginConfig = ksCut (tmpContract, cur);
+
+ // increment ref count, because cur is part of pluginConfig and
+ // we hold a reference to cur that is still needed (via pluginName)
+ keyIncRef (cur);
+ ksRename (pluginConfig, cur, pluginConfigRoot);
+ ksAppend (config, pluginConfig);
+
+ // we need to delete cur separately, because it was ksCut() from contract
+ // we also need to decrement the ref count, because it was incremented above
+ keyDecRef (cur);
+ keyDel (cur);
+
+ --it;
+ }
+ }
+
+ keyDel (mountContractRoot);
+ keyDel (pluginConfigRoot);
+ ksDel (tmpContract);
+
+ return config;
+}
+
+static Plugin * loadPlugin (const char * pluginName, KeySet * global, KeySet * modules, const KeySet * contract, Key * errorKey)
{
Key * openKey = keyDup (errorKey, KEY_CP_ALL);
- Plugin * plugin = elektraPluginOpen (pluginName, modules, ksNew (0, KS_END), openKey);
+ KeySet * config = getPluginConfigFromContract(pluginName, contract);
+
+ Plugin * plugin = elektraPluginOpen (pluginName, modules, config, openKey);
if (!plugin)
{
@@ -59,6 +104,8 @@ static Plugin * loadPlugin (const char * pluginName, KeySet * modules, Key * err
return NULL;
}
+ plugin->global = global;
+
return plugin;
}
@@ -75,7 +122,7 @@ int initHooks (KDB * kdb, const KeySet * config, KeySet * modules, const KeySet
{
freeHooks (kdb, errorKey);
- if (isGoptsEnabledByContract (contract) && initHooksGopts (kdb, loadPlugin ("gopts", modules, errorKey), errorKey) != 0)
+ if (isGoptsEnabledByContract (contract) && initHooksGopts (kdb, loadPlugin ("gopts", kdb->global, modules, contract, errorKey), errorKey) != 0)
{
goto error;
}
