This repository was archived by the owner on Feb 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathlength.c
More file actions
125 lines (108 loc) · 3.48 KB
/
Copy pathlength.c
File metadata and controls
125 lines (108 loc) · 3.48 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @file
*
* @brief Source for length plugin
*
* @copyright BSD License (see doc/LICENSE.md or https://www.libelektra.org)
*
*/
#include "length.h"
#include <kdbease.h>
#include <kdberrors.h>
#include <stdlib.h>
static bool validateKey (Key * key, Key * parentKey, bool addWarningOnly)
{
const Key * meta = keyGetMeta (key, "check/length/max");
if (meta == NULL)
{
return true;
}
kdb_unsigned_long_long_t max;
if (!elektraKeyToUnsignedLongLong (meta, &max))
{
if (addWarningOnly)
{
ELEKTRA_ADD_VALIDATION_SEMANTIC_WARNINGF (
parentKey, "Couldn't read check/length/max value '%s' on key '%s'. It should be a non-negative integer.",
keyString (meta), keyName (key));
}
else
{
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (
parentKey, "Couldn't read check/length/max value '%s' on key '%s'. It should be a non-negative integer.",
keyString (meta), keyName (key));
}
return false;
}
// subtract nul-terminator, if string value
size_t length = keyGetValueSize (key) - (keyIsString (key) ? 1 : 0);
if (length > max)
{
if (addWarningOnly)
{
ELEKTRA_ADD_VALIDATION_SEMANTIC_WARNINGF (
parentKey,
"Length check of key '%s' with value '%s' failed. Maximum length is " ELEKTRA_UNSIGNED_LONG_LONG_F
" but the given string has length %zd",
keyName (key), keyString (key), max, length);
}
else
{
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (
parentKey,
"Length check of key '%s' with value '%s' failed. Maximum length is " ELEKTRA_UNSIGNED_LONG_LONG_F
" but the given string has length %zd",
keyName (key), keyString (key), max, length);
}
return false;
}
return true;
}
int elektraLengthGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/length"))
{
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/length", KEY_VALUE, "length plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/length/exports", KEY_END),
keyNew ("system:/elektra/modules/length/exports/get", KEY_FUNC, elektraLengthGet, KEY_END),
keyNew ("system:/elektra/modules/length/exports/set", KEY_FUNC, elektraLengthSet, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/length/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Key * cur;
for (elektraCursor it = 0; it < ksGetSize (returned); ++it)
{
cur = ksAtCursor (returned, it);
const Key * meta = keyGetMeta (cur, "check/length/max");
if (!meta) continue;
validateKey (cur, parentKey, true);
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int elektraLengthSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
// set all keys
// this function is optional
Key * cur;
for (elektraCursor it = 0; it < ksGetSize (returned); ++it)
{
cur = ksAtCursor (returned, it);
const Key * meta = keyGetMeta (cur, "check/length/max");
if (!meta) continue;
int rc = validateKey (cur, parentKey, false);
if (!rc) return ELEKTRA_PLUGIN_STATUS_ERROR;
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("length",
ELEKTRA_PLUGIN_GET, &elektraLengthGet,
ELEKTRA_PLUGIN_SET, &elektraLengthSet,
ELEKTRA_PLUGIN_END);
}