forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.c
198 lines (177 loc) · 5.09 KB
/
validation.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "validation.h"
static int validateKey (Key *, Key *);
int elektraValidationGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey ELEKTRA_UNUSED)
{
KeySet * n;
ksAppend (returned,
n = ksNew (30,
keyNew ("system:/elektra/modules/validation", KEY_VALUE, "validation plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/validation/exports", KEY_END),
keyNew ("system:/elektra/modules/validation/exports/get", KEY_FUNC, elektraValidationGet, KEY_END),
keyNew ("system:/elektra/modules/validation/exports/set", KEY_FUNC, elektraValidationSet, KEY_END),
keyNew ("system:/elektra/modules/validation/exports/ksLookupRE", KEY_FUNC, ksLookupRE, KEY_END),
keyNew ("system:/elektra/modules/validation/exports/validateKey", KEY_FUNC, validateKey, KEY_END),
#include "readme_validation.c"
keyNew ("system:/elektra/modules/validation/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END));
ksDel (n);
return 1;
}
static int validateKey (Key * key, Key * parentKey)
{
const Key * regexMeta = keyGetMeta (key, "check/validation");
const Key * icaseMeta = keyGetMeta (key, "check/validation/ignorecase");
const Key * matchMeta = keyGetMeta (key, "check/validation/match");
const Key * invertMeta = keyGetMeta (key, "check/validation/invert");
const Key * typeMeta = keyGetMeta (key, "check/validation/type");
int lineValidation = 0;
int wordValidation = 0;
int icaseValidation = 0;
int invertValidation = 0;
if (icaseMeta) icaseValidation = 1;
if (invertMeta) invertValidation = 1;
if (matchMeta)
{
char * matchCopy = elektraStrDup (keyString (matchMeta));
char * ptr = matchCopy;
while (*ptr)
{
*ptr = toupper (*ptr);
++ptr;
}
if (!strcmp (matchCopy, "LINE")) lineValidation = 1;
if (!strcmp (matchCopy, "WORD")) wordValidation = 1;
if (!strcmp (matchCopy, "ANY"))
{
lineValidation = 0;
wordValidation = 0;
}
elektraFree (matchCopy);
}
int cflags = REG_NOSUB | REG_EXTENDED;
if (icaseValidation) cflags |= REG_ICASE;
if (lineValidation) cflags |= REG_NEWLINE;
if (typeMeta)
{
char * typeCopy = elektraStrDup (keyString (typeMeta));
char * ptr = typeCopy;
while (*ptr)
{
*ptr = toupper (*ptr);
++ptr;
}
if (!strcmp (typeCopy, "ERE"))
cflags |= REG_EXTENDED;
else if (!strcmp (typeCopy, "BRE"))
cflags &= REG_EXTENDED;
elektraFree (typeCopy);
}
char * regexString = NULL;
int freeString = 0;
if (lineValidation || wordValidation)
{
regexString = elektraMalloc (keyGetValueSize (regexMeta) + 2);
freeString = 1;
sprintf (regexString, "^%s$", keyString (regexMeta));
}
else
{
regexString = (char *) keyString (regexMeta);
}
regex_t regex;
regmatch_t offsets;
int ret = regcomp (®ex, regexString, cflags);
if (ret != 0)
{
char buffer[1000];
regerror (ret, ®ex, buffer, 999);
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Could not compile regex '%s' of the key '%s'. Reason: %s",
keyString (regexMeta), keyName (key), buffer);
regfree (®ex);
if (freeString) elektraFree (regexString);
return 0;
}
int match = 0;
if (!wordValidation)
{
ret = regexec (®ex, keyString (key), 1, &offsets, 0);
if (ret == 0) match = 1;
}
else
{
char * savePtr;
char * token;
char * string = (char *) keyString (key);
while ((token = strtok_r (string, " \t\n", &savePtr)) != NULL)
{
ret = regexec (®ex, token, 1, &offsets, 0);
if (ret == 0)
{
match = 1;
break;
}
string = NULL;
}
}
if (invertValidation) match = !match;
if (!match)
{
const Key * msg = keyGetMeta (key, "check/validation/message");
if (msg)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey,
"The key '%s' with value '%s' does not confirm to '%s'. Reason: %s",
keyName (key), keyString (key), regexString, keyString (msg));
regfree (®ex);
if (freeString) elektraFree (regexString);
return 0;
}
else
{
char buffer[1000];
regerror (ret, ®ex, buffer, 999);
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey,
"The key '%s' with value '%s' does not confirm to '%s'. Reason: %s",
keyName (key), keyString (key), regexString, buffer);
regfree (®ex);
if (freeString) elektraFree (regexString);
return 0;
}
}
regfree (®ex);
if (freeString) elektraFree (regexString);
return 1;
}
int elektraValidationSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
Key * cur = 0;
while ((cur = ksNext (returned)) != 0)
{
const Key * regexMeta = keyGetMeta (cur, "check/validation");
if (!regexMeta) continue;
int rc = validateKey (cur, parentKey);
if (!rc) return -1;
}
return 1; /* success */
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("validation",
ELEKTRA_PLUGIN_GET, &elektraValidationGet,
ELEKTRA_PLUGIN_SET, &elektraValidationSet,
ELEKTRA_PLUGIN_END);
}