Example #1
0
static void test_default()
{
	printf ("Test default " KDB_DEFAULT_STORAGE "\n");

	KeySet *modules = ksNew(0, KS_END);
	elektraModulesInit(modules, 0);


	Plugin *plugin = elektraPluginOpen(KDB_DEFAULT_STORAGE, modules, set_pluginconf(), 0);
	exit_if_fail (plugin, "KDB_DEFAULT_STORAGE: " KDB_DEFAULT_STORAGE " plugin could not be loaded");

	KeySet *test_config = set_pluginconf();
	KeySet *config = elektraPluginGetConfig (plugin);
	succeed_if (config != 0, "there should be a config");
	compare_keyset(config, test_config);
	ksDel (test_config);

	succeed_if (plugin->kdbGet != 0, "no get pointer");
	succeed_if (plugin->kdbSet != 0, "no set pointer");

	elektraPluginClose(plugin, 0);

	Backend *backend = elektraBackendOpenDefault(modules, KDB_DB_FILE, 0);

	Key *mp;
	succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found");
	succeed_if_same_string (keyName(mp), "");
	succeed_if_same_string (keyString(mp), "default");

	elektraBackendClose(backend, 0);
	elektraModulesClose (modules, 0);
	ksDel (modules);
}
Example #2
0
int elektraTracerOpen (Plugin * handle, Key * errorKey)
{
	ssize_t nr_keys = 0;
	KeySet * config = elektraPluginGetConfig (handle);

	if (ksLookupByName (config, "/module", 0))
	{
		if (ksLookupByName (config, "/logmodule", 0))
		{
			Key * k;
			printf ("tracer: openmodule(%p, %s = %s): ", (void *) handle, keyName (errorKey), keyString (errorKey));
			while ((k = ksNext (config)) != 0)
			{
				printf ("%s=%s ", keyName (k), keyString (k));
				++nr_keys;
			}
			printf ("%zd\n", nr_keys);
		}
	}
	else
	{
		Key * k;
		printf ("tracer: open(%p, %s = %s): ", (void *) handle, keyName (errorKey), keyString (errorKey));
		while ((k = ksNext (config)) != 0)
		{
			printf ("%s=%s ", keyName (k), keyString (k));
			++nr_keys;
		}
		printf ("%zd\n", nr_keys);
	}


	return 0;
}
int elektraCsvstorageSet(Plugin *handle, KeySet *returned, Key *parentKey)
{
    KeySet *config = elektraPluginGetConfig(handle);
    Key *delimKey = ksLookupByName(config, "/delimiter", 0);
    char outputDelim;
    if(delimKey)
    {
        const char *delimString = keyString(delimKey);
        outputDelim = delimString[0];
    }
    else
    {
        outputDelim = ';';
    }
    Key *useHeaderKey = ksLookupByName(config, "/header", 0);
    short useHeader = 0;
    if(!strcmp(keyString(useHeaderKey), "skip")) useHeader = -1;
    if(csvWrite(returned, parentKey, outputDelim, useHeader) == -1)
    {
        return -1;
    }
    else
    {
        return 1; /* success */
    }
}
Example #4
0
	inline static int open (ckdb::Plugin * handle, ckdb::Key * errorKey, Builder builder = defaultBuilder)
	{
		kdb::KeySet config (elektraPluginGetConfig (handle));
		int ret = openHelper (handle, config, errorKey, builder);
		config.release ();
		return ret;
	}
Example #5
0
int elektraSpecloadOpen (Plugin * handle, Key * errorKey)
{
	Specload * specload = elektraMalloc (sizeof (Specload));

	KeySet * conf = elektraPluginGetConfig (handle);
	if (ksLookupByName (conf, "system/module", 0) != NULL || ksLookupByName (conf, "system/sendspec", 0) != NULL)
	{
		elektraFree (specload);
		return ELEKTRA_PLUGIN_STATUS_SUCCESS;
	}

	if (!getAppAndArgs (conf, &specload->app, &specload->argv, errorKey))
	{
		elektraFree (specload);
		return ELEKTRA_PLUGIN_STATUS_ERROR;
	}

	specload->quickDumpConfig = ksNew (0, KS_END);
	specload->quickDump = elektraInvokeOpen ("quickdump", specload->quickDumpConfig, errorKey);

	if (!specload->quickDump)
	{
		elektraFree (specload);
		return ELEKTRA_PLUGIN_STATUS_ERROR;
	}

	elektraPluginSetData (handle, specload);

	return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Example #6
0
static void test_init (void)
{
	Plugin * plugin = NULL;
	Key * parentKey = keyNew ("system", KEY_END);
	KeySet * modules = ksNew (0, KS_END);
	KeySet * configKs = newPluginConfiguration ();
	elektraModulesInit (modules, 0);

	plugin = elektraPluginOpen (PLUGIN_NAME, modules, configKs, 0);
	succeed_if (plugin != 0, "failed to open the plugin");
	if (plugin)
	{
		succeed_if (!strcmp (plugin->name, PLUGIN_NAME), "got wrong name");

		KeySet * config = elektraPluginGetConfig (plugin);
		succeed_if (config != 0, "there should be a config");

		succeed_if (plugin->kdbOpen != 0, "no open pointer");
		succeed_if (plugin->kdbClose != 0, "no close pointer");
		succeed_if (plugin->kdbGet != 0, "no get pointer");
		succeed_if (plugin->kdbSet != 0, "no set pointer");

		elektraPluginClose (plugin, 0);
	}

	elektraModulesClose (modules, 0);
	ksDel (modules);
	keyDel (parentKey);
}
Example #7
0
/**
 * @brief Builds together a format string by the plugin's configuration
 *
 * @param handle to plugin
 * @param first format string for key
 * @param second format string for value
 *
 * @return newly allocated format (to be freed with elektraFree);
 */
static char * getFormat (Plugin * handle, const char * first, const char * second)
{
	char * format;
	Key * key = ksLookupByName (elektraPluginGetConfig (handle), "/format", 0);
	if (!key)
	{
		format = elektraStrDup ("%s = %s\n");
	}
	else
	{
		const size_t maxFactor = 2; // at maximum every char is a %, %% -> %%%%
		const size_t newLineAtEnd = 2;
		const size_t userFormatSize = keyGetValueSize (key);
		format = elektraMalloc (userFormatSize * maxFactor + newLineAtEnd);

		const char * userFormat = keyString (key);
		int gotPercent = 0;
		size_t j = 0;
		for (size_t i = 0; i < userFormatSize; ++i, ++j)
		{
			const char c = userFormat[i];
			if (gotPercent)
			{
				if (c == '%')
				{
					// escaped %% -> %%%%
					format[j++] = '%';
					format[j++] = '%';
					format[j] = '%';
				}
				else
				{
					// single % -> %s
					format[j++] = 's';
					format[j] = c;
				}
				gotPercent = 0;
			}
			else if (c == '%')
			{
				format[j] = c;
				gotPercent = 1;
			}
			else
			{
				format[j] = c;
			}
		}
		--j; // discard null byte that is already there
		ELEKTRA_ASSERT (format[j] == '\0', "should be null byte at end of string but was %c", format[j]);
		format[j++] = '\n';
		format[j] = '\0';
	}

	char * ret = elektraFormat (format, first, second);
	elektraFree (format);
	return ret;
}
Example #8
0
void test_resolve ()
{
	int pathLen = tempHomeLen + 1 + strlen (KDB_DB_USER) + 12 + 1;
	char * path = elektraMalloc (pathLen);
	exit_if_fail (path != 0, "elektraMalloc failed");
	snprintf (path, pathLen, "%s/%s/elektra.ecf", tempHome, KDB_DB_USER);

	printf ("Resolve Filename\n");

	KeySet * modules = ksNew (0, KS_END);
	elektraModulesInit (modules, 0);

	Key * parentKey = keyNew ("system", KEY_END);
	Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0);
	exit_if_fail (plugin, "could not load resolver plugin");

	KeySet * test_config = set_pluginconf ();
	KeySet * config = elektraPluginGetConfig (plugin);
	succeed_if (config != 0, "there should be a config");
	compare_keyset (config, test_config);
	ksDel (test_config);

	succeed_if (plugin->kdbOpen != 0, "no open pointer");
	succeed_if (plugin->kdbClose != 0, "no open pointer");
	succeed_if (plugin->kdbGet != 0, "no open pointer");
	succeed_if (plugin->kdbSet != 0, "no open pointer");
	succeed_if (plugin->kdbError != 0, "no open pointer");

	succeed_if (!strncmp (plugin->name, "resolver", strlen ("resolver")), "got wrong name");

	resolverHandles * h = elektraPluginGetData (plugin);
	exit_if_fail (h != 0, "no plugin handle");
	succeed_if (!strcmp (h->system.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp (h->system.filename, KDB_DB_SYSTEM "/elektra.ecf"), "resulting filename not correct");
	succeed_if_same_string (h->user.path, "elektra.ecf");
	succeed_if_same_string (h->user.filename, path);
	plugin->kdbClose (plugin, parentKey);

	// reinit with system path only
	plugin->kdbOpen (plugin, parentKey);
	h = elektraPluginGetData (plugin);
	exit_if_fail (h != 0, "no plugin handle");
	succeed_if (!strcmp (h->system.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp (h->system.filename, KDB_DB_SYSTEM "/elektra.ecf"), "resulting filename not correct");
	succeed_if (h->user.filename == NULL, "user was initialized, but is not needed");
	plugin->kdbClose (plugin, parentKey);

	keyDel (parentKey);
	elektraPluginClose (plugin, 0);
	elektraModulesClose (modules, 0);
	ksDel (modules);
	elektraFree (path);
}
Example #9
0
static inline const char * getTo (Plugin * handle)
{
	const char * to;
	Key * k;

	k = ksLookupByName (elektraPluginGetConfig (handle), "/to", 0);
	if (!k)
		to = "UTF-8";
	else
		to = keyString (k);

	return to;
}
Example #10
0
static inline const char * getFrom (Plugin * handle)
{
	const char * from;
	Key * k;

	k = ksLookupByName (elektraPluginGetConfig (handle), "/from", 0);
	if (!k)
		from = nl_langinfo (CODESET);
	else
		from = keyString (k);

	return from;
}
Example #11
0
static void test_backref()
{
	printf ("Test back references\n");

	KeySet *modules = ksNew(0, KS_END);
	elektraModulesInit(modules, 0);

	Backend *backend = elektraBackendOpen(set_backref(), modules, 0);
	succeed_if (backend != 0, "there should be a backend");
	succeed_if (backend->getplugins[0] == 0, "there should be no plugin");
	exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin");
	succeed_if (backend->getplugins[2] == 0, "there should be no plugin");

	succeed_if (backend->setplugins[0] == 0, "there should be no plugin");
	exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin");
	succeed_if (backend->setplugins[2] == 0, "there should be no plugin");

	Key *mp;
	succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found");
	succeed_if_same_string (keyName(mp), "user/tests/backend/backref");
	succeed_if_same_string (keyString(mp), "backref");

	Plugin *plugin1 = backend->getplugins[1];
	Plugin *plugin2 = backend->setplugins[1];
	Plugin *plugin3 = backend->errorplugins[1];

	succeed_if (plugin1 != 0, "there should be a plugin");
	succeed_if (plugin2 != 0, "there should be a plugin");
	succeed_if (plugin3 != 0, "there should be a plugin");

	succeed_if (plugin1 == plugin2, "it should be the same plugin");
	succeed_if (plugin2 == plugin3, "it should be the same plugin");
	succeed_if (plugin1 == plugin3, "it should be the same plugin");

	succeed_if (plugin1->refcounter == 3, "ref counter should be 3");

	KeySet *test_config = set_pluginconf();
	KeySet *config = elektraPluginGetConfig (plugin1);
	succeed_if (config != 0, "there should be a config");
	compare_keyset(config, test_config);
	ksDel (test_config);

	succeed_if (plugin1->kdbGet != 0, "no get pointer");
	succeed_if (plugin1->kdbSet != 0, "no set pointer");
	succeed_if (plugin2->kdbGet != 0, "no get pointer");
	succeed_if (plugin2->kdbSet != 0, "no set pointer");

	elektraBackendClose (backend, 0);
	elektraModulesClose (modules, 0);
	ksDel (modules);
}
Example #12
0
int elektraTracerClose (Plugin * handle, Key * errorKey)
{
	KeySet * config = elektraPluginGetConfig (handle);

	if (ksLookupByName (config, "/module", 0))
	{
		if (ksLookupByName (config, "/logmodule", 0))
		{
			printf ("tracer: closemodule(%p, %s = %s)\n", (void *) handle, keyName (errorKey), keyString (errorKey));
		}
	}
	else
	{
		printf ("tracer: close(%p, %s = %s)\n", (void *) handle, keyName (errorKey), keyString (errorKey));
	}

	return 0;
}
Example #13
0
int elektraWresolverOpen (Plugin * handle, Key * errorKey)
{
	KeySet * resolverConfig = elektraPluginGetConfig (handle);
	const char * path = keyString (ksLookupByName (resolverConfig, "/path", 0));

	if (!path)
	{
		ELEKTRA_SET_ERROR (34, errorKey, "Could not find file configuration");
		return -1;
	}

	resolverHandles * p = elektraMalloc (sizeof (resolverHandles));

	// switch is only present to forget no namespace and to get
	// a warning whenever a new namespace is present.
	// (also used below in close)
	// In fact its linear code executed:
	switch (KEY_NS_SPEC)
	{
	case KEY_NS_SPEC:
		resolverInit (&p->spec, path);
		elektraResolveSpec (&p->spec, errorKey);
	case KEY_NS_DIR:
		resolverInit (&p->dir, path);
		elektraResolveDir (&p->dir, errorKey);
	case KEY_NS_USER:
		resolverInit (&p->user, path);
		elektraResolveUser (&p->user, errorKey);
	case KEY_NS_SYSTEM:
		resolverInit (&p->system, path);
		elektraResolveSystem (&p->system, errorKey);
	case KEY_NS_PROC:
	case KEY_NS_EMPTY:
	case KEY_NS_NONE:
	case KEY_NS_META:
	case KEY_NS_CASCADING:
		break;
	}

	elektraPluginSetData (handle, p);

	return 0; /* success */
}
Example #14
0
void test_tempname()
{
	printf ("Resolve Tempname\n");

	KeySet *modules = ksNew(0);
	elektraModulesInit (modules, 0);

	Plugin *plugin = elektraPluginOpen("resolver", modules, set_pluginconf(), 0);
	exit_if_fail (plugin, "could not load resolver plugin");

	KeySet *test_config = set_pluginconf();
	KeySet *config = elektraPluginGetConfig (plugin);
	succeed_if (config != 0, "there should be a config");
	compare_keyset(config, test_config);
	ksDel (test_config);

	succeed_if (plugin->kdbOpen != 0, "no open pointer");
	succeed_if (plugin->kdbClose != 0, "no open pointer");
	succeed_if (plugin->kdbGet != 0, "no open pointer");
	succeed_if (plugin->kdbSet != 0, "no open pointer");
	succeed_if (plugin->kdbError!= 0, "no open pointer");

	succeed_if (!strcmp(plugin->name, "resolver"), "got wrong name");

	resolverHandles *h = elektraPluginGetData(plugin);
	succeed_if (h != 0, "no plugin handle");

	Key *parentKey= keyNew("system", KEY_END);
	plugin->kdbGet(plugin, 0, parentKey);
	succeed_if (!strncmp(h->system.tempfile, KDB_DB_SYSTEM "/elektra.ecf", sizeof(KDB_DB_SYSTEM)),
			"resulting filename not correct");

	keySetName(parentKey, "user");
	plugin->kdbGet(plugin, 0, parentKey);
	succeed_if (!strncmp(h->user.tempfile, KDB_DB_HOME "/" KDB_DB_USER "/elektra.ecf.tmp", sizeof(KDB_DB_HOME "/" KDB_DB_USER)),
			"resulting filename not correct");

	keyDel (parentKey);
	elektraPluginClose(plugin, 0);
	elektraModulesClose(modules, 0);
	ksDel (modules);
}
Example #15
0
static void test_simple ()
{
	printf ("Test plugin\n");

	KeySet * modules = ksNew (0, KS_END);
	elektraModulesInit (modules, 0);

	Plugin * plugin = elektraPluginOpen (KDB_DEFAULT_STORAGE, modules, set_pluginconf (), 0);
	exit_if_fail (plugin, "KDB_DEFAULT_STORAGE: " KDB_DEFAULT_STORAGE " plugin could not be loaded");

	KeySet * test_config = set_pluginconf ();
	KeySet * config = elektraPluginGetConfig (plugin);
	succeed_if (config != 0, "there should be a config");
	compare_keyset (config, test_config);
	ksDel (test_config);

	succeed_if (plugin->kdbGet != 0, "no get pointer");
	succeed_if (plugin->kdbSet != 0, "no set pointer");

	elektraPluginClose (plugin, 0);
	elektraModulesClose (modules, 0);
	ksDel (modules);
}
void test_name ()
{
	printf ("Resolve Name\n");

	KeySet * modules = ksNew (0, KS_END);
	elektraModulesInit (modules, 0);

	Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0);
	exit_if_fail (plugin, "could not load resolver plugin");

	KeySet * test_config = set_pluginconf ();
	KeySet * config = elektraPluginGetConfig (plugin);
	succeed_if (config != 0, "there should be a config");
	compare_keyset (config, test_config);
	ksDel (test_config);

	succeed_if (plugin->kdbOpen != 0, "no open pointer");
	succeed_if (plugin->kdbClose != 0, "no open pointer");
	succeed_if (plugin->kdbGet != 0, "no open pointer");
	succeed_if (plugin->kdbSet != 0, "no open pointer");
	succeed_if (plugin->kdbError != 0, "no open pointer");

	succeed_if (!strncmp (plugin->name, "resolver", strlen ("resolver")), "got wrong name");

	resolverHandles * h = elektraPluginGetData (plugin);
	succeed_if (h != 0, "no plugin handle");

	Key * parentKey = keyNew ("system", KEY_END);
	plugin->kdbGet (plugin, 0, parentKey);
	succeed_if_same_string (keyString (parentKey), KDB_DB_SYSTEM "/elektra.ecf");

	keyDel (parentKey);
	elektraPluginClose (plugin, 0);
	elektraModulesClose (modules, 0);
	ksDel (modules);
}
Example #17
0
	return t;
}

int elektraTimeofdayOpen(Plugin *handle, Key *parentKey ELEKTRA_UNUSED)
{
	TimeofdayInfo *ti = calloc(1, sizeof (TimeofdayInfo));
	// char t[24];

	elektraPluginSetData(handle, ti);

	// init time
	gettimeofday(&ti->start, 0);
	ti->last = ti->start;

	KeySet *config  = elektraPluginGetConfig(handle);
	if (ksLookupByName(config, "/module", 0))
	{
		if (ksLookupByName(config, "/logmodule", 0))
		{
			char t[24];
			fprintf(stderr, "open (module)\t%s\n", elektraTimeofdayHelper (t, ti));
		}
	} else {
		char t[24];
		fprintf(stderr, "open\t%s\n", elektraTimeofdayHelper (t, ti));
	}

	return 0; /* success */
}
Example #18
0
 * @brief Source for dini plugin
 *
 * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
 *
 */

#include "dini.h"

#include <kdbhelper.h>


int elektraDiniOpen (Plugin * handle, Key * errorKey ELEKTRA_UNUSED)
{
	Dini * dini = elektraMalloc (sizeof (Dini));

	dini->dumpConfig = ksDup (elektraPluginGetConfig (handle));
	dini->iniConfig = ksDup (elektraPluginGetConfig (handle));
	dini->dump = elektraInvokeOpen ("dump", dini->dumpConfig, 0);
	dini->ini = elektraInvokeOpen ("ini", dini->iniConfig, 0);
	dini->bin = elektraInvokeOpen ("binary", dini->iniConfig, 0);

	dini->dumpErrors = keyNew ("", KEY_END);

	elektraPluginSetData (handle, dini);

	return dini->ini ? ELEKTRA_PLUGIN_STATUS_SUCCESS : ELEKTRA_PLUGIN_STATUS_ERROR;
}

int elektraDiniClose (Plugin * handle, Key * errorKey ELEKTRA_UNUSED)
{
	Dini * dini = elektraPluginGetData (handle);
Example #19
0
static const char *getLensPath(Plugin *handle)
{
	KeySet *config = elektraPluginGetConfig (handle);
	Key* lensPathKey = ksLookupByName (config, "/lens", 0);
	return keyString (lensPathKey);
}
Example #20
0
static void test_simple()
{
	printf ("Test simple building of backend\n");

	KeySet *modules = ksNew(0, KS_END);
	elektraModulesInit(modules, 0);

	Key *errorKey = 0;
	Backend *backend = elektraBackendOpen(set_simple(), modules, errorKey);
	succeed_if (backend->errorplugins[0] == 0, "there should be no plugin");
	succeed_if (backend->errorplugins[2] == 0, "there should be no plugin");
	succeed_if (backend->errorplugins[3] == 0, "there should be no plugin");
	succeed_if (backend->errorplugins[4] == 0, "there should be no plugin");
	succeed_if (backend->errorplugins[5] == 0, "there should be no plugin");
	succeed_if (backend->errorplugins[6] == 0, "there should be no plugin");
	succeed_if (backend->errorplugins[7] == 0, "there should be no plugin");
	succeed_if (backend->errorplugins[8] == 0, "there should be no plugin");
	succeed_if (backend->errorplugins[9] == 0, "there should be no plugin");
	exit_if_fail (backend->errorplugins[1] != 0, "there should be a plugin");

	succeed_if (backend->getplugins[0] == 0, "there should be no plugin");
	succeed_if (backend->getplugins[2] == 0, "there should be no plugin");
	succeed_if (backend->getplugins[3] == 0, "there should be no plugin");
	succeed_if (backend->getplugins[4] == 0, "there should be no plugin");
	succeed_if (backend->getplugins[5] == 0, "there should be no plugin");
	succeed_if (backend->getplugins[6] == 0, "there should be no plugin");
	succeed_if (backend->getplugins[7] == 0, "there should be no plugin");
	succeed_if (backend->getplugins[8] == 0, "there should be no plugin");
	succeed_if (backend->getplugins[9] == 0, "there should be no plugin");
	exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin");

	succeed_if (backend->setplugins[0] == 0, "there should be no plugin");
	succeed_if (backend->setplugins[2] == 0, "there should be no plugin");
	succeed_if (backend->setplugins[3] == 0, "there should be no plugin");
	succeed_if (backend->setplugins[4] == 0, "there should be no plugin");
	succeed_if (backend->setplugins[5] == 0, "there should be no plugin");
	succeed_if (backend->setplugins[6] == 0, "there should be no plugin");
	succeed_if (backend->setplugins[7] == 0, "there should be no plugin");
	succeed_if (backend->setplugins[8] == 0, "there should be no plugin");
	succeed_if (backend->setplugins[9] == 0, "there should be no plugin");
	exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin");

	Key *mp;
	succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found");
	succeed_if_same_string (keyName(mp), "user/tests/backend/simple");
	succeed_if_same_string (keyString(mp), "simple");

	Plugin *plugin = backend->getplugins[1];

	KeySet *test_config = set_pluginconf();
	KeySet *config = elektraPluginGetConfig (plugin);
	succeed_if (config != 0, "there should be a config");
	compare_keyset(config, test_config);
	ksDel (test_config);

	succeed_if (plugin->kdbGet != 0, "no get pointer");
	succeed_if (plugin->kdbSet != 0, "no set pointer");

	elektraBackendClose (backend, errorKey);
	elektraModulesClose (modules, 0);
	ksDel (modules);
}
Example #21
0
 * @brief
 *
 * @copyright BSD License (see doc/LICENSE.md or http://www.libelektra.org)
 */

#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif

#include "log.h"

int elektraSyslogOpen (Plugin * handle, Key * parentKey ELEKTRA_UNUSED)
{
    /* plugin initialization logic */

    if (!ksLookupByName (elektraPluginGetConfig (handle), "/dontopensyslog", 0))
    {
        openlog ("elektra", LOG_PID, LOG_USER);
    }

    return 0; /* success */
}

int elektraSyslogClose (Plugin * handle, Key * parentKey ELEKTRA_UNUSED)
{
    /* free all plugin resources and shut it down */

    if (!ksLookupByName (elektraPluginGetConfig (handle), "/dontopensyslog", 0))
    {
        closelog ();
    }
int elektraCsvstorageGet(Plugin *handle, KeySet *returned, Key *parentKey)
{
    if (!strcmp(keyName(parentKey), "system/elektra/modules/csvstorage"))
    {
        KeySet *contract = ksNew (30,
                                  keyNew ("system/elektra/modules/csvstorage",
                                          KEY_VALUE, "csvstorage plugin waits for your orders", KEY_END),
                                  keyNew ("system/elektra/modules/csvstorage/exports", KEY_END),
                                  keyNew ("system/elektra/modules/csvstorage/exports/get",
                                          KEY_FUNC, elektraCsvstorageGet, KEY_END),
                                  keyNew ("system/elektra/modules/csvstorage/exports/set",
                                          KEY_FUNC, elektraCsvstorageSet, KEY_END),
#include ELEKTRA_README(csvstorage)
                                  keyNew ("system/elektra/modules/csvstorage/infos/version",
                                          KEY_VALUE, PLUGINVERSION, KEY_END),
                                  KS_END);
        ksAppend (returned, contract);
        ksDel (contract);

        return 1; /* success */
    }

    KeySet *config = elektraPluginGetConfig(handle);
    Key *delimKey = ksLookupByName(config, "/delimiter", 0);
    char delim = ';';
    if(delimKey)
    {
        const char *delimString = keyString(delimKey);
        delim = delimString[0];
    }

    Key *readHeaderKey = ksLookupByName(config, "/header", 0);
    short useHeader = 0;
    if(readHeaderKey)
    {
        const char *printHeaderString = keyString(readHeaderKey);
        if(!strcmp(printHeaderString, "colname"))
        {
            useHeader = 1;
        }
        else if(!(strcmp(printHeaderString, "skip")))
        {
            useHeader = -1;
        }
        else if(!(strcmp(printHeaderString, "record")))
        {
            useHeader = 0;
        }
        else
        {
            useHeader = 0;
        }
    }
    unsigned long fixColumnCount = 0;
    Key *fixColumnCountKey = ksLookupByName(config, "/columns", 0);
    if(fixColumnCountKey)
    {
        if(keyString(fixColumnCountKey))
        {
            fixColumnCount = atol(keyString(fixColumnCountKey));
        }
    }
    Key *setNamesKey = ksLookupByName(config, "/columns/names", 0);
    char *colNames = NULL;
    if(setNamesKey)
    {
        if(fixColumnCountKey)
        {
            KeySet *namesKS = ksCut(config, setNamesKey);
            unsigned long nrNames = (unsigned long)ksGetSize(namesKS)-1;
            if(nrNames == fixColumnCount)
            {
                colNames = (char *)elektraMalloc(nrNames*sizeof(char *));
                Key *cur;
                char **ptr = (char **)colNames;
                while((cur = ksNext(namesKS)) != NULL)
                {
                    if(!strcmp(keyName(cur), keyName(setNamesKey))) continue;
                    if(!strcmp(keyString(cur), ""))
                        *ptr = NULL;
                    else
                        *ptr = (char *)keyString(cur);
                    ++ptr;
                }
            }
            ksAppend(config, namesKS);
            ksDel(namesKS);
        }
    }
    int nr_keys;
    nr_keys = csvRead(returned, parentKey, delim, useHeader, fixColumnCount, (const char **)colNames);
    if(colNames)
        elektraFree(colNames);
    if (nr_keys == -1) return -1;
    return 1;
}
Example #23
0
void test_resolve()
{
	char *path;
	int pathLen;

	printf ("Resolve Filename\n");

	KeySet *modules = ksNew(0);
	elektraModulesInit (modules, 0);

	Plugin *plugin = elektraPluginOpen("resolver", modules, set_pluginconf(), 0);
	exit_if_fail (plugin, "could not load resolver plugin");

	KeySet *test_config = set_pluginconf();
	KeySet *config = elektraPluginGetConfig (plugin);
	succeed_if (config != 0, "there should be a config");
	compare_keyset(config, test_config);
	ksDel (test_config);

	succeed_if (plugin->kdbOpen != 0, "no open pointer");
	succeed_if (plugin->kdbClose != 0, "no open pointer");
	succeed_if (plugin->kdbGet != 0, "no open pointer");
	succeed_if (plugin->kdbSet != 0, "no open pointer");
	succeed_if (plugin->kdbError!= 0, "no open pointer");

	succeed_if (!strcmp(plugin->name, "resolver"), "got wrong name");

	resolverHandles *h = elektraPluginGetData(plugin);
	succeed_if (h != 0, "no plugin handle");
	resolverClose(&h->system);
	resolverClose(&h->user);

	Key *forKey = keyNew("system", KEY_END);
	succeed_if (resolveFilename(forKey, &h->system, forKey) != -1,
			"could not resolve filename");

	succeed_if (!strcmp(h->system.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp(h->system.filename, KDB_DB_SYSTEM "/elektra.ecf"), "resulting filename not correct");
	resolverClose(&h->system);


	keySetName(forKey, "user");
	succeed_if (resolveFilename(forKey, &h->user, forKey) != -1,
			"could not resolve filename");

	pathLen = tempHomeLen + 1 + strlen (KDB_DB_USER) + 12 + 1;
	path = malloc (pathLen);
	succeed_if (path != 0, "malloc failed");
	snprintf (path, pathLen, "%s/%s/elektra.ecf", tempHome, KDB_DB_USER);

	succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp(h->user.filename, path), "filename not set correctly");
	resolverClose(&h->user);

#ifdef HAVE_SETENV
	unsetenv("USER");
	unsetenv("HOME");
	keySetName(forKey, "system");
	succeed_if (resolveFilename(forKey, &h->system, forKey) != -1,
			"could not resolve filename");

	succeed_if (!strcmp(h->system.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp(h->system.filename, KDB_DB_SYSTEM "/elektra.ecf"), "resulting filename not correct");
	resolverClose(&h->system);


	keySetName(forKey, "user");
	succeed_if (resolveFilename(forKey, &h->user, forKey) != -1,
			"could not resolve filename");

	succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp(h->user.filename, KDB_DB_HOME "/" KDB_DB_USER "/elektra.ecf"), "filename not set correctly");
	resolverClose(&h->user);


	setenv("USER","other",1);
	succeed_if (resolveFilename(forKey, &h->user, forKey) != -1,
			"could not resolve filename");

	succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp(h->user.filename, KDB_DB_HOME "/other/" KDB_DB_USER "/elektra.ecf"), "filename not set correctly");
	resolverClose(&h->user);

	setenv("HOME","/nfshome//max//",1);
	succeed_if (resolveFilename(forKey, &h->user, forKey) != -1,
			"could not resolve filename");

	succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp(h->user.filename, "/nfshome/max/" KDB_DB_USER "/elektra.ecf"), "filename not set correctly");
	resolverClose(&h->user);
	unsetenv("HOME");
	unsetenv("USER");
#endif

	keySetName(forKey, "user");
	succeed_if (resolveFilename(forKey, &h->user, forKey) != -1,
			"could not resolve filename");

	succeed_if (!strcmp(h->user.path, "elektra.ecf"), "path not set correctly");
	succeed_if (!strcmp(h->user.filename, KDB_DB_HOME "/" KDB_DB_USER "/elektra.ecf"), "filename not set correctly");
	resolverClose(&h->user);

	keyDel (forKey);
	elektraPluginClose(plugin, 0);
	elektraModulesClose(modules, 0);
	ksDel (modules);
	free (path);
}