Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include "null.h"
10 :
11 : #ifndef HAVE_KDBCONFIG
12 : #include "kdbconfig.h"
13 : #endif
14 :
15 : #include <kdbhelper.h>
16 :
17 : #include <stdlib.h>
18 : #include <string.h>
19 :
20 166 : int elektraNullGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
21 : {
22 166 : if (!strcmp (keyName (parentKey), "system/elektra/modules/null"))
23 : {
24 101 : KeySet * moduleConfig =
25 101 : ksNew (30, keyNew ("system/elektra/modules/null", KEY_VALUE, "null plugin waits for your orders", KEY_END),
26 : keyNew ("system/elektra/modules/null/exports", KEY_END),
27 : keyNew ("system/elektra/modules/null/exports/get", KEY_FUNC, elektraNullGet, KEY_END),
28 : keyNew ("system/elektra/modules/null/exports/set", KEY_FUNC, elektraNullSet, KEY_END),
29 : #include "readme_null.c"
30 : keyNew ("system/elektra/modules/null/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
31 101 : ksAppend (returned, moduleConfig);
32 101 : ksDel (moduleConfig);
33 :
34 101 : return 1;
35 : }
36 : /* get all keys */
37 :
38 : Key * k;
39 65 : ksRewind (returned);
40 350 : while ((k = ksNext (returned)) != 0)
41 : {
42 220 : if (!strcmp (keyString (k), "@NULL"))
43 : {
44 4 : keySetBinary (k, 0, 0);
45 : }
46 216 : else if (!strcmp (keyString (k), "@EMPTY"))
47 : {
48 4 : keySetString (k, "");
49 : }
50 212 : else if (!strncmp (keyString (k), "@@", 2))
51 : {
52 : /* Drop the first of the @ */
53 0 : keySetString (k, keyString (k) + 1);
54 : }
55 : }
56 :
57 : return 1; /* success */
58 : }
59 :
60 17 : int elektraNullSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey ELEKTRA_UNUSED)
61 : {
62 : /* set all keys */
63 :
64 : Key * k;
65 17 : ksRewind (returned);
66 72 : while ((k = ksNext (returned)) != 0)
67 : {
68 38 : if (keyValue (k) == 0)
69 : {
70 1 : keySetString (k, "@NULL");
71 : }
72 37 : else if (!strcmp (keyValue (k), ""))
73 : {
74 4 : keySetString (k, "@EMPTY");
75 : }
76 33 : else if (!strncmp (keyValue (k), "@", 1))
77 : {
78 0 : char * n = elektraMalloc (keyGetValueSize (k) + 1);
79 0 : strcpy (n, "@");
80 0 : strcat (n, keyValue (k));
81 0 : keySetString (k, n);
82 0 : elektraFree (n);
83 : }
84 : }
85 :
86 17 : return 1; /* success */
87 : }
88 :
89 653 : Plugin * ELEKTRA_PLUGIN_EXPORT
90 : {
91 : // clang-format off
92 653 : return elektraPluginExport("null",
93 : ELEKTRA_PLUGIN_GET, &elektraNullGet,
94 : ELEKTRA_PLUGIN_SET, &elektraNullSet,
95 : ELEKTRA_PLUGIN_END);
96 : }
97 :
|