forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noresolver.c
103 lines (87 loc) · 3.04 KB
/
noresolver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include "noresolver.h"
#include <kdblogger.h>
#include <string.h>
/**
* @retval 1 on success (Relative path)
* @retval 0 on success (Absolute path)
* @retval never -1 (success guaranteed)
*/
int elektraNoresolverCheckFile (const char * filename)
{
if (filename[0] == '/') return 0;
return 1;
}
static KeySet * elektraNoresolverModules (void)
{
return ksNew (
50,
keyNew ("system:/elektra/modules/" ELEKTRA_PLUGIN_NAME "", KEY_VALUE,
"" ELEKTRA_PLUGIN_NAME " plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/" ELEKTRA_PLUGIN_NAME "/exports", KEY_END),
keyNew ("system:/elektra/modules/" ELEKTRA_PLUGIN_NAME "/exports/get", KEY_FUNC, elektraNoresolverGet, KEY_END),
keyNew ("system:/elektra/modules/" ELEKTRA_PLUGIN_NAME "/exports/set", KEY_FUNC, elektraNoresolverSet, KEY_END),
keyNew ("system:/elektra/modules/" ELEKTRA_PLUGIN_NAME "/exports/error", KEY_FUNC, elektraNoresolverError, KEY_END),
keyNew ("system:/elektra/modules/" ELEKTRA_PLUGIN_NAME "/exports/checkfile", KEY_FUNC, elektraNoresolverCheckFile, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/" ELEKTRA_PLUGIN_NAME "/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
}
int elektraNoresolverGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
{
Key * root = keyNew ("system:/elektra/modules/" ELEKTRA_PLUGIN_NAME, KEY_END);
if (keyCmp (root, parentKey) == 0 || keyIsBelow (root, parentKey) == 1)
{
keyDel (root);
KeySet * info = elektraNoresolverModules ();
ksAppend (returned, info);
ksDel (info);
return 1;
}
keyDel (root);
KeySet * config = elektraPluginGetConfig (handle);
Key * pathKey = ksLookupByName (config, "/path", KDB_O_NONE);
if (pathKey) keySetString (parentKey, keyString (pathKey));
if (!strcmp (keyString (ksLookupByName (config, "/assume/unchanged", 0)), "1"))
{
// always return 0, except the first time
uintptr_t nr = (uintptr_t) elektraPluginGetData (handle);
if (nr == 1)
{
ELEKTRA_LOG ("assume config is unchanged");
return 0;
}
elektraPluginSetData (handle, (void *) 1);
}
ELEKTRA_LOG ("assume config is changed");
return 1; /* success */
}
int elektraNoresolverSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey)
{
KeySet * config = elektraPluginGetConfig (handle);
Key * pathKey = ksLookupByName (config, "/path", KDB_O_NONE);
if (pathKey) keySetString (parentKey, keyString (pathKey));
return 1; /* success */
}
int elektraNoresolverError (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
/* set all keys */
return 1; /* success */
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("noresolver",
ELEKTRA_PLUGIN_GET, &elektraNoresolverGet,
ELEKTRA_PLUGIN_SET, &elektraNoresolverSet,
ELEKTRA_PLUGIN_ERROR, &elektraNoresolverError,
ELEKTRA_PLUGIN_END);
}