This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
counter.c
113 lines (93 loc) · 3.2 KB
/
counter.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
103
104
105
106
107
108
109
110
111
112
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include <stdio.h>
#include <string.h>
#include "counter.h"
typedef int Counter;
#define COUNTER_FMT "%d"
int elektraCounterOpen (Plugin * handle, Key * errorKey ELEKTRA_UNUSED)
{
static Counter elektraCountOpen;
elektraCountOpen += 1;
KeySet * config = elektraPluginGetConfig (handle);
if (ksLookupByName (config, "/module", 0))
{
if (ksLookupByName (config, "/logmodule", 0))
{
printf ("%p elektraCounterOpen (module) called " COUNTER_FMT " times\n", (void *) handle, elektraCountOpen);
}
}
else
{
printf ("%p elektraCounterOpen called " COUNTER_FMT " times\n", (void *) handle, elektraCountOpen);
}
return 1; /* success */
}
int elektraCounterClose (Plugin * handle, Key * errorKey ELEKTRA_UNUSED)
{
static Counter elektraCountClose;
elektraCountClose += 1;
KeySet * config = elektraPluginGetConfig (handle);
if (ksLookupByName (config, "/module", 0))
{
if (ksLookupByName (config, "/logmodule", 0))
{
printf ("%p elektraCounterClose (module) called " COUNTER_FMT " times\n", (void *) handle, elektraCountClose);
}
}
else
{
printf ("%p elektraCounterClose called " COUNTER_FMT " times\n", (void *) handle, elektraCountClose);
}
return 1; /* success */
}
int elektraCounterGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
if (!strcmp (keyName (parentKey), "system:/elektra/modules/counter"))
{
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/counter", KEY_VALUE, "counter plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/counter/exports", KEY_END),
keyNew ("system:/elektra/modules/counter/exports/open", KEY_FUNC, elektraCounterOpen, KEY_END),
keyNew ("system:/elektra/modules/counter/exports/close", KEY_FUNC, elektraCounterClose, KEY_END),
keyNew ("system:/elektra/modules/counter/exports/get", KEY_FUNC, elektraCounterGet, KEY_END),
keyNew ("system:/elektra/modules/counter/exports/set", KEY_FUNC, elektraCounterSet, KEY_END),
keyNew ("system:/elektra/modules/counter/exports/error", KEY_FUNC, elektraCounterError, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/counter/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return 1; /* success */
}
/* get all keys */
return 1; /* success */
}
int elektraCounterSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
/* set all keys */
return 1; /* success */
}
int elektraCounterError (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("counter",
ELEKTRA_PLUGIN_OPEN, &elektraCounterOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraCounterClose,
ELEKTRA_PLUGIN_GET, &elektraCounterGet,
ELEKTRA_PLUGIN_SET, &elektraCounterSet,
ELEKTRA_PLUGIN_ERROR, &elektraCounterError,
ELEKTRA_PLUGIN_END);
}