forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
106 lines (90 loc) · 2.7 KB
/
error.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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "error.h"
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include <stdlib.h>
// #include <stdio.h>
int elektraErrorOpen (Plugin * handle ELEKTRA_UNUSED, Key * parentKey)
{
KeySet * conf = elektraPluginGetConfig (handle);
if (ksLookupByName (conf, "/module", 0))
{
// suppress warnings + errors if it is just a module
return 0;
}
Key * warning = ksLookupByName (conf, "/on_open/warnings", 0);
if (warning)
{
elektraTriggerWarnings (keyString (warning), parentKey, "from error plugin in kdbOpen");
}
Key * error = ksLookupByName (conf, "/on_open/error", 0);
if (error)
{
if (parentKey)
{
elektraTriggerError (keyString (error), parentKey, "from error plugin in kdbOpen");
}
return -1;
}
return 0;
}
int elektraErrorGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey ELEKTRA_UNUSED)
{
if (!strcmp (keyName (parentKey), "system:/elektra/modules/error"))
{
KeySet * n;
ksAppend (returned,
n = ksNew (30, keyNew ("system:/elektra/modules/error", KEY_VALUE, "error plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/error/exports", KEY_END),
keyNew ("system:/elektra/modules/error/exports/open", KEY_FUNC, elektraErrorOpen, KEY_END),
keyNew ("system:/elektra/modules/error/exports/get", KEY_FUNC, elektraErrorGet, KEY_END),
keyNew ("system:/elektra/modules/error/exports/set", KEY_FUNC, elektraErrorSet, KEY_END),
#include "readme_error.c"
keyNew ("system:/elektra/modules/error/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END));
ksDel (n);
ksAppend (returned, n = elektraErrorSpecification ());
ksDel (n);
}
return 1;
}
int elektraErrorSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
for (elektraCursor it = 0; it < ksGetSize (returned); ++it)
{
Key * cur = ksAtCursor (returned, it);
const Key * meta = 0;
meta = keyGetMeta (cur, "trigger/warnings");
if (meta)
{
elektraTriggerWarnings (keyString (meta), parentKey, "from error plugin in kdbSet");
}
meta = keyGetMeta (cur, "trigger/error");
if (meta)
{
elektraTriggerError (keyString (meta), parentKey, "from error plugin in kdbSet");
return -1; /* error */
}
meta = keyGetMeta (cur, "trigger/error/nofail");
if (meta)
{
elektraTriggerError (keyString (meta), parentKey, "from error plugin in kdbSet");
}
}
return 0;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("error",
ELEKTRA_PLUGIN_OPEN, &elektraErrorOpen,
ELEKTRA_PLUGIN_GET, &elektraErrorGet,
ELEKTRA_PLUGIN_SET, &elektraErrorSet,
ELEKTRA_PLUGIN_END);
}