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
/
spec.c
584 lines (493 loc) · 15.9 KB
/
spec.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include "spec.h"
#include "arrayspec.h"
#include "kdberrors.h"
#include "matching.h"
#include <ctype.h>
#include <kdbhelper.h>
#include <stdio.h>
#include <stdlib.h>
/**
* Replace the {@link searchFor} with {@link c} in the {@link str}.
* The {@link newStr} contains the string with the replaced character.
*
* @param str the old string
* @param[out] newStr the newly created string with the already replaced character
* It has to provide enough allocated memory to store the
* same size as in {@link str}.
* @param searchFor the character to replace
* @param c the character to use instead
*/
static void replaceCharacter (const char * str, char * newStr, const char searchFor, const char c)
{
for (size_t i = 0; i < elektraStrLen (str); i++)
{
if (str[i] == searchFor)
{
newStr[i] = c;
}
else
{
newStr[i] = str[i];
}
}
}
/**
* Appends a key to the KeySet with the default value specified in the meta key (meta:/default) in the
* {@link specKey}.
*
* The default key is added to the `default:/` namespace.
*
* @param ks the KeySet to append the new default key to
* @param specKey specification key with meta data of the new default key
* @param isArraySpec boolean value indicating whether this is an array specification key
*/
static void addDefaultKey (KeySet * ks, Key * specKey, bool isArraySpec)
{
const Key * defaultMetaKey = keyGetMeta (specKey, "default");
const char * defaultValue = keyString (defaultMetaKey);
if (elektraStrCmp (defaultValue, "(null)") == 0)
{
return;
}
const char * specKeyName = strchr (keyName (specKey), '/');
char * formattedKeyName = elektraFormat (isArraySpec && specKeyName[elektraStrLen (specKeyName) - 2] == '#' ?
(specKeyName[0] == '/' ? "default:%s0" : "default:/%s0") :
(specKeyName[0] == '/' ? "default:%s" : "default:/%s"),
specKeyName);
if (containsArraySpecElementWithNoDigitOrUnderlineAfterwards (formattedKeyName))
{
elektraFree (formattedKeyName);
return;
}
Key * newDefaultKey = keyNew (formattedKeyName, KEY_VALUE, defaultValue, KEY_END);
keyCopyAllMeta (newDefaultKey, specKey);
ksAppendKey (ks, newDefaultKey);
elektraFree (formattedKeyName);
keyDel (newDefaultKey);
}
/**
* Check if the specification key has a meta key `require` with the value `true`.
*
* @param specKey the specification key to check for the require meta key
* @retval true - if the specification key contains a meta key require
* @retval false - if the specification key does not contain a meta key require
*/
static bool isRequired (const Key * specKey)
{
const Key * key = keyGetMeta (specKey, "require");
if (key == 0)
{
return false;
}
const char * keyValue = keyString (key);
return elektraStrCmp (keyValue, "true") == 0;
}
/**
* Check if the specification key has a meta key `default`.
*
* @param specKey the specification key to check for the default meta key
* @retval true - if the specification key contains a meta key default
* @retval false - if the specification key does not contain the meta key default
*/
static bool hasDefault (const Key * specKey)
{
return keyGetMeta (specKey, "default") != 0;
}
/**
* Extracts all the specification keys from the KeySet. After applying this method the {@link ks}
* contains no specification namespace.
*
* It returns the extracted specification keys.
*
* @param ks the KeySet to extract specification keys from
*
* @retval the extracted specification keys
* @retval NULL - if the {@link ks} is NULL
*/
static KeySet * extractSpecKeys (KeySet * ks)
{
if (ks == NULL)
{
return NULL;
}
Key * specKey = keyNew ("spec:/", KEY_END);
KeySet * ksRet = ksCut (ks, specKey);
keyDel (specKey);
return ksRet;
}
/**
* Checks if this specification key has a wildcard character in the key name.
*
* @param specKey the specification to check for if it has a wildcard character
* @retval true - if the specification key has a wildcard character in the key name
* @retval false - if the specification does not contain a wildcard character
*/
static bool isWildcardSpecification (const Key * specKey)
{
const char * keyWithoutNamespace = strchr (keyName (specKey), '/');
for (size_t i = 0; i < elektraStrLen (keyWithoutNamespace); i++)
{
if (keyWithoutNamespace[i] == '_')
{
return true;
}
}
return false;
}
/**
* Checks if the specification has a collision. A collision is when two specification keys exist, one as wildcard specification,
* the other as array specification and it is not clear in this case what the correct specification is.
*
* Example:
* spec:/server/_/name => meta:/description = value1
* spec:/server/#/name => meta:/description = value2
*
* @param specKeys specification keys to check for collision
* @return pointer to the key which caused collision
* @retval 0 - if no collision was found
*/
static Key * specCollision (KeySet * specKeys)
{
for (elektraCursor it = 0; it < ksGetSize (specKeys); it++)
{
Key * current = ksAtCursor (specKeys, it);
if (isWildcardSpecification (current))
{
const char * wildcardSpec = strchr (keyName (current), '/');
char * arraySpecKey = elektraMalloc (elektraStrLen (wildcardSpec));
replaceCharacter (wildcardSpec, arraySpecKey, '_', '#');
char * formattedLookupKeyName = elektraFormat ("spec:/%s", arraySpecKey);
Key * foundKey = ksLookupByName (specKeys, formattedLookupKeyName, 0);
elektraFree (arraySpecKey);
elektraFree (formattedLookupKeyName);
if (foundKey != 0)
{
return foundKey;
}
}
else if (isArraySpecification (current))
{
const char * arraySpec = strchr (keyName (current), '/');
char * wildcardSpecKey = elektraMalloc (elektraStrLen (arraySpec));
replaceCharacter (arraySpec, wildcardSpecKey, '#', '_');
char * formattedLookupKeyName = elektraFormat ("spec:/%s", wildcardSpecKey);
Key * foundKey = ksLookupByName (specKeys, formattedLookupKeyName, 0);
elektraFree (wildcardSpecKey);
elektraFree (formattedLookupKeyName);
if (foundKey != 0)
{
return foundKey;
}
}
}
return 0;
}
/**
* It iterates over the {@p name} and re-allocates a new string where it omits all the
* digits after an array character.
*
* NOTE: Returned string needs to be freed manually.
*
* @param name the original array key name
* @param lastOccurenceOfCharacter used to omit all digits after this character '#'
* @return a newly allocated string without digits after the array character, in case there is an error, NULL
*/
static char * getKeyNameWithoutDigitsAfterArrayCharacter (const char * name, const char * lastOccurenceOfCharacter)
{
if (name == NULL || lastOccurenceOfCharacter == NULL)
{
return NULL;
}
size_t sizeOfName = elektraStrLen (name);
char * newName = elektraCalloc (sizeOfName);
int c = 0;
int offBy = 1;
for (size_t i = 0; i < sizeOfName; i = i + offBy, c++)
{
offBy = 1;
if (lastOccurenceOfCharacter == &name[i])
{
for (size_t x = i + 1; x < sizeOfName; x++)
{
if (isdigit (name[x]))
{
offBy++;
}
else
{
break;
}
}
}
newName[c] = name[i];
}
return newName;
}
/**
* Copy the meta data for a given key {@link specKey} by searching through the {@link ks} KeySet.
*
* In case no key was found for {@link specKey} and it has meta key default (meta:/default) it will
* be created.
*
* In case the key is missing and has no meta key default the method returns -1 and it fails by
* adding an error (or warning in the case of `kdbGet`).
*
* @param parentKey the parent key (primarily used to handle in case of error, warning)
* @param specKey the specification key containing the meta data to be copied
* @param specKeys the specification keys in this {@link ks}
* @param ks the KeySet to search for the key
* @param isKdbGet
* @retval 0 - if the meta data was copied successfully
* @retval -1 - if the metadata could not be copied (error is also added there)
* @retval -1 - if the key was not found but has meta:/require and no meta:/default in {@link specKey}
* @retval -1 - if the array specification is not valid
*/
static int copyMetaData (Key * parentKey, Key * specKey, KeySet * specKeys, KeySet * ks, bool isKdbGet)
{
int found = -1;
bool isArraySpec = isArraySpecification (specKey);
if (isArraySpec && !isValidArraySize (ks, specKeys, parentKey))
{
return -1;
}
char * specKeyWithoutNamespace = strchr (keyName (specKey), '/');
// in case array spec does not look like #_10, #__100
// this will instantiate array keys and add to default:/ if they contain a default value
if (isArraySpec && !containsUnderlineInArraySpec (specKey) && hasDefault (specKey))
{
int num = getNumberOfArrayCharactersInSpecName (specKey);
if (num == 0)
{
return 0;
}
int * arrayPositions = elektraMalloc (num * sizeof (int));
if (arrayPositions == NULL)
{
return 0;
}
setArrayPositions (strchr (keyName (specKey), '/'), arrayPositions, num);
if (specKeyWithoutNamespace == NULL)
{
elektraFree (arrayPositions);
return 0;
}
for (int i = 0; i < num; i++)
{
char * untilArrayElementAtPositionI = elektraCalloc (arrayPositions[i] + 1);
if (untilArrayElementAtPositionI == NULL)
{
elektraFree (arrayPositions);
return 0;
}
memcpy (untilArrayElementAtPositionI, &specKeyWithoutNamespace[0], arrayPositions[i]);
Key * arraySizeKeyToInstantiate = getMatchingKeyFromKeySet (specKeys, untilArrayElementAtPositionI);
if (arraySizeKeyToInstantiate == NULL)
{
arraySizeKeyToInstantiate = getMatchingKeyFromKeySet (ks, untilArrayElementAtPositionI);
}
const char * arraySizeToInstantiate = keyString (keyGetMeta (arraySizeKeyToInstantiate, "array"));
if (arraySizeKeyToInstantiate == NULL)
{
// no array size, does any instantiated array element at this already exist
if (copyAllMetaDataForMatchingArrayKeyName (ks, parentKey, specKey, isKdbGet) == -1)
{
addDefaultKey (ks, specKey, true);
}
elektraFree (untilArrayElementAtPositionI);
continue;
}
int actualArraySize = getActualArraySize (ks, specKey, arrayPositions[i]);
char * end;
char * rest;
char * afterPossibleArrayElement = elektraStrDup (arraySizeToInstantiate);
int size = strchr (arraySizeToInstantiate, '#') == NULL ?
strtol (arraySizeToInstantiate, &end, 10) :
strtol (strtok_r (afterPossibleArrayElement, "#", &rest), &end, 10) + 1;
elektraFree (afterPossibleArrayElement);
elektraFree (untilArrayElementAtPositionI);
if (actualArraySize == size)
{
continue;
}
instantiateArraySpecificationAndCopyMeta (specKey, ks, size, arrayPositions[i]);
}
elektraFree (arrayPositions);
return 0;
}
for (elektraCursor it = 0; it < ksGetSize (ks); it++)
{
Key * current = ksAtCursor (ks, it);
char * withoutNamespace = strchr (keyName (current), '/');
if (elektraStrCmp (specKeyWithoutNamespace, "/") == 0 || elektraStrCmp (withoutNamespace, "/") == 0)
{
continue;
}
if (specMatches (specKey, current))
{
found = 0;
if (keyCopyAllMeta (current, specKey) < 0)
{
if (isKdbGet)
{
ELEKTRA_ADD_PLUGIN_MISBEHAVIOR_WARNINGF (parentKey, "Could not copy metadata from spec key %s",
keyName (specKey));
}
else
{
ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (parentKey, "Could not copy metadata from spec key %s",
keyName (specKey));
}
return -1;
}
}
}
// key was not found
if (found == -1)
{
if (hasDefault (specKey))
{
addDefaultKey (ks, specKey, false);
}
return 0;
}
return 0;
}
int elektraSpecCopy (ELEKTRA_UNUSED Plugin * handle, KeySet * returned, Key * parentKey, bool isKdbGet)
{
KeySet * specKeys = extractSpecKeys (returned);
Key * collisionKey = specCollision (specKeys);
if (collisionKey != 0)
{
if (isKdbGet)
{
ELEKTRA_ADD_PLUGIN_MISBEHAVIOR_WARNINGF (parentKey,
"Specification %s has a collision. It seems that there exists an array "
"and wildcard specification for the same key.",
keyName (collisionKey));
}
else
{
ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (parentKey,
"Specification %s has a collision. It seems that there exists an array and "
"wildcard specification for the same key.",
keyName (collisionKey));
}
ksDel (specKeys);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
for (elektraCursor it = 0; it < ksGetSize (specKeys); it++)
{
Key * current = ksAtCursor (specKeys, it);
// if required and no default => cascade lookup if exists in other namespaces
if ((isRequired (current) && !hasDefault (current)) || (isRequired (current) && isWildcardSpecification (current)))
{
Key * cascadingKey = keyNew (strchr (keyName (current), '/'), KEY_END);
if (ksLookup (returned, cascadingKey, 0) == 0)
{
if (isKdbGet)
{
ELEKTRA_ADD_PLUGIN_MISBEHAVIOR_WARNINGF (parentKey, "Key for specification %s does not exist",
keyName (current));
}
else
{
ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (parentKey, "Key for specification %s does not exist",
keyName (current));
}
keyDel (cascadingKey);
ksDel (specKeys);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
keyDel (cascadingKey);
}
if (copyMetaData (parentKey, current, specKeys, returned, isKdbGet) != 0)
{
ksDel (specKeys);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
ksAppend (returned, specKeys);
ksDel (specKeys);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int elektraSpecRemove (ELEKTRA_UNUSED Plugin * handle, KeySet * returned, ELEKTRA_UNUSED Key * parentKey)
{
Key * specName = keyNew ("spec:/", KEY_END);
for (elektraCursor i = 0; i < ksGetSize (returned); i++)
{
Key * current = ksAtCursor (returned, i);
if (keyGetNamespace (current) == KEY_NS_SPEC)
{
continue;
}
// Find out if there is a spec:/ key for the current key
const char * name = keyName (current);
keySetName (specName, "spec:/");
// check if specName is array name
char * lastOccurenceOfArrayCharacter = strrchr (name, '#');
if (lastOccurenceOfArrayCharacter != NULL)
{
Key * key = keyNew ("spec:/", NULL);
keyAddName (key, strchr (name, '/'));
int count = getNumberOfArrayCharactersInSpecName (key);
if (count > 1)
{
char * newName =
getKeyNameWithoutDigitsAfterArrayCharacter (strchr (name, '/'), lastOccurenceOfArrayCharacter);
keyAddName (specName, newName);
elektraFree (newName);
}
else
{
keyAddName (specName, strchr (name, '/'));
}
keyDel (key);
}
else
{
keyAddName (specName, strchr (name, '/'));
}
Key * specKey = ksLookup (returned, specName, 0);
if (specKey != NULL)
{
KeySet * specMeta = keyMeta (specKey);
KeySet * meta = keyMeta (current);
for (elektraCursor it = 0; it < ksGetSize (specMeta); it++)
{
Key * m = ksAtCursor (specMeta, it);
if (ksLookup (meta, m, 0) == m)
{
keySetMeta (current, keyName (m), NULL);
}
}
}
keyDel (specKey);
}
keyDel (specName);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int elektraSpecGet (ELEKTRA_UNUSED Plugin * handle, KeySet * returned, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/spec"))
{
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/spec", KEY_VALUE, "spec plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/spec/exports", KEY_END),
keyNew ("system:/elektra/modules/spec/exports/get", KEY_FUNC, elektraSpecGet, KEY_END),
keyNew ("system:/elektra/modules/spec/exports/hook/spec/copy", KEY_FUNC, elektraSpecCopy, KEY_END),
keyNew ("system:/elektra/modules/spec/exports/hook/spec/remove", KEY_FUNC, elektraSpecRemove, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/spec/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("spec",
ELEKTRA_PLUGIN_GET, &elektraSpecGet,
ELEKTRA_PLUGIN_END);
}