forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyajl_parse.c
456 lines (378 loc) · 11.3 KB
/
yajl_parse.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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "yajl.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <kdbmacros.h>
#include <yajl/yajl_parse.h>
typedef struct
{
KeySet * ks;
elektraCursor cursor;
} Context;
static void elektraYajlSetArrayLength (KeySet * ks, Key * current)
{
// Update array length in array key
Key * arrayKey = keyNew (keyName (current), KEY_END);
keySetBaseName (arrayKey, 0);
Key * foundKey = ksLookup (ks, arrayKey, 0);
keySetMeta (foundKey, "array", keyBaseName (current));
keyDel (arrayKey);
}
static void elektraYajlAppendKey (Context * context, Key * key)
{
ksAppendKey (context->ks, key);
context->cursor = ksSearch (context->ks, key);
}
static Key * elektraYajlCurrentKey (Context * context)
{
return ksAtCursor (context->ks, context->cursor);
}
/**
@retval 0 if the current @p context does not hold an array entry
@retval 1 if the array entry will be used because its the first
@retval 2 if a new array entry was created
@retval -1 error in snprintf
*/
static int elektraYajlIncrementArrayEntry (Context * context)
{
Key * current = elektraYajlCurrentKey (context);
const char * baseName = keyBaseName (current);
const char * meta = keyString (keyGetMeta (current, "array"));
if (!strcmp (meta, "empty"))
{
current = keyNew (keyName (current), KEY_END);
keyAddName (current, "#0");
elektraYajlAppendKey (context, current);
elektraYajlSetArrayLength (context->ks, current);
return 1;
}
else if (baseName && *baseName == '#')
{
// we are in an array
current = keyNew (keyName (current), KEY_END);
elektraArrayIncName (current);
elektraYajlAppendKey (context, current);
elektraYajlSetArrayLength (context->ks, current);
return 2;
}
else
{
// previous entry indicates this is not an array
return 0;
}
}
static int elektraYajlParseNull (void * ctx)
{
Context * context = (Context *) ctx;
elektraYajlIncrementArrayEntry (context);
Key * current = elektraYajlCurrentKey (context);
keySetBinary (current, NULL, 0);
ELEKTRA_LOG_DEBUG ("parse null");
return 1;
}
static int elektraYajlParseBoolean (void * ctx, int boolean)
{
Context * context = (Context *) ctx;
elektraYajlIncrementArrayEntry (context);
Key * current = elektraYajlCurrentKey (context);
if (boolean == 1)
{
keySetString (current, "1");
}
else
{
keySetString (current, "0");
}
keySetMeta (current, "type", "boolean");
ELEKTRA_LOG_DEBUG ("%d", boolean);
return 1;
}
static int elektraYajlParseNumber (void * ctx, const char * stringVal, yajl_size_type stringLen)
{
Context * context = (Context *) ctx;
elektraYajlIncrementArrayEntry (context);
Key * current = elektraYajlCurrentKey (context);
unsigned char delim = stringVal[stringLen];
char * stringValue = (char *) stringVal;
stringValue[stringLen] = '\0';
ELEKTRA_LOG_DEBUG ("%s %zu", stringVal, stringLen);
keySetString (current, stringVal);
keySetMeta (current, "type", "double");
// restore old character in buffer
stringValue[stringLen] = delim;
return 1;
}
static int elektraYajlParseString (void * ctx, const unsigned char * stringVal, yajl_size_type stringLen)
{
Context * context = (Context *) ctx;
elektraYajlIncrementArrayEntry (context);
Key * current = elektraYajlCurrentKey (context);
unsigned char delim = stringVal[stringLen];
char * stringValue = (char *) stringVal;
stringValue[stringLen] = '\0';
ELEKTRA_LOG_DEBUG ("%s %zu", stringVal, stringLen);
keySetString (current, stringValue);
// restore old character in buffer
stringValue[stringLen] = delim;
return 1;
}
static int elektraYajlParseMapKey (void * ctx, const unsigned char * stringVal, yajl_size_type stringLen)
{
Context * context = (Context *) ctx;
elektraYajlIncrementArrayEntry (context);
Key * currentKey = keyNew (keyName (elektraYajlCurrentKey (context)), KEY_END);
keySetString (currentKey, 0);
unsigned char delim = stringVal[stringLen];
char * stringValue = (char *) stringVal;
stringValue[stringLen] = '\0';
ELEKTRA_LOG_DEBUG ("stringValue: %s currentKey: %s", stringValue, keyName (currentKey));
if (currentKey && !strcmp (keyBaseName (currentKey), "___empty_map"))
{
// remove old key
keyDel (ksLookup (context->ks, currentKey, KDB_O_POP));
context->cursor = 0; // current key was popped from KeySet
// now we know the name of the object
keySetBaseName (currentKey, stringValue);
}
else
{
// we entered a new pair (inside the previous object)
keySetBaseName (currentKey, stringValue);
}
elektraYajlAppendKey (context, currentKey);
// restore old character in buffer
stringValue[stringLen] = delim;
return 1;
}
static int elektraYajlParseStartMap (void * ctx)
{
Context * context = (Context *) ctx;
elektraYajlIncrementArrayEntry (context);
Key * currentKey = elektraYajlCurrentKey (context);
Key * newKey = keyNew (keyName (currentKey), KEY_END);
// add a pseudo element for empty map
keyAddBaseName (newKey, "___empty_map");
elektraYajlAppendKey (context, newKey);
ELEKTRA_LOG_DEBUG ("with new key %s", keyName (newKey));
return 1;
}
static int elektraYajlParseEnd (void * ctx)
{
Context * context = (Context *) ctx;
Key * currentKey = elektraYajlCurrentKey (context);
const char * meta = keyString (keyGetMeta (currentKey, "array"));
// If array is still empty by the time we reach the end, replace with ""
if (!strcmp (meta, "empty"))
{
keySetMeta (currentKey, "array", "");
return 1;
}
Key * lookupKey = keyNew (keyName (currentKey), KEY_END);
keySetBaseName (lookupKey, 0); // remove current baseName
// lets move iterator to the correct place
context->cursor = ksSearch (context->ks, lookupKey);
#ifdef HAVE_LOGGER
if (context->cursor >= 0)
{
ELEKTRA_LOG_DEBUG ("Iterator position: %zd", context->cursor);
}
else
{
ELEKTRA_LOG_DEBUG ("Did not find key %s", keyName (lookupKey));
}
#endif
keyDel (lookupKey);
return 1;
}
static int elektraYajlParseStartArray (void * ctx)
{
Context * context = (Context *) ctx;
elektraYajlIncrementArrayEntry (context);
Key * currentKey = elektraYajlCurrentKey (context);
Key * newKey = keyNew (keyName (currentKey), KEY_END);
keySetMeta (newKey, "array", "empty");
elektraYajlAppendKey (context, newKey);
ELEKTRA_LOG_DEBUG ("with new key %s", keyName (newKey));
return 1;
}
/**
* @brief Remove all non-leaf keys except for arrays
*
* @param returned to remove the keys from
*/
static void elektraYajlParseSuppressNonLeafKeys (KeySet * returned)
{
elektraCursor it = 0;
Key * cur = ksAtCursor (returned, it);
while (cur != NULL)
{
Key * next = ksAtCursor (returned, ++it);
if (next == NULL) break;
Key * peekDup = keyDup (next, KEY_CP_ALL);
keySetBaseName (peekDup, 0);
if (!strcmp (keyName (peekDup), keyName (cur)))
{
const char * baseName = keyBaseName (next);
// TODO: Add test for empty array check
if (strcmp (baseName, "#0"))
{
ELEKTRA_LOG_DEBUG ("Removing non-leaf key %s", keyName (cur));
keyDel (ksLookup (returned, cur, KDB_O_POP));
it--;
}
else
{
// Set array key to NULL to avoid empty ___dirdata entries
keySetBinary (cur, NULL, 0);
}
}
keyDel (peekDup);
cur = ksAtCursor (returned, it);
}
}
/**
* @brief Remove ___empty_map if thats the only thing which would be
* returned.
*
* @param returned to remove the key from
*/
static void elektraYajlParseSuppressEmptyMap (KeySet * returned, Key * parentKey)
{
if (ksGetSize (returned) == 2)
{
Key * lookupKey = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (lookupKey, "___empty_map");
Key * toRemove = ksLookup (returned, lookupKey, KDB_O_POP);
#ifdef HAVE_LOGGER
if (toRemove)
{
ELEKTRA_LOG_DEBUG ("remove %s", keyName (toRemove));
}
else
{
Key * cur;
for (elektraCursor it = 0; it < ksGetSize (returned); ++it)
{
cur = ksAtCursor (returned, it);
ELEKTRA_LOG_DEBUG ("key %s has value %s", keyName (cur), keyString (cur));
}
ELEKTRA_LOG_DEBUG ("did not find %s", keyName (lookupKey));
}
#endif
if (toRemove)
{
keyDel (toRemove);
}
keyDel (lookupKey);
}
}
static inline KeySet * elektraGetModuleConfig (void)
{
return ksNew (30, keyNew ("system:/elektra/modules/yajl", KEY_VALUE, "yajl plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/yajl/exports", KEY_END),
keyNew ("system:/elektra/modules/yajl/exports/get", KEY_FUNC, elektraYajlGet, KEY_END),
keyNew ("system:/elektra/modules/yajl/exports/set", KEY_FUNC, elektraYajlSet, KEY_END),
#include "readme_yajl.c"
keyNew ("system:/elektra/modules/yajl/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END),
keyNew ("system:/elektra/modules/yajl/config", KEY_END),
keyNew ("system:/elektra/modules/yajl/config/", KEY_VALUE, "system", KEY_END),
keyNew ("system:/elektra/modules/yajl/config/below", KEY_VALUE, "user", KEY_END),
keyNew ("system:/elektra/modules/yajl/config/needs/boolean/restoreas", KEY_VALUE, "none", KEY_END), KS_END);
}
int elektraYajlGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!strcmp (keyName (parentKey), "system:/elektra/modules/yajl"))
{
KeySet * moduleConfig = elektraGetModuleConfig ();
ksAppend (returned, moduleConfig);
ksDel (moduleConfig);
return 1;
}
yajl_callbacks callbacks = { elektraYajlParseNull,
elektraYajlParseBoolean,
NULL,
NULL,
elektraYajlParseNumber,
elektraYajlParseString,
elektraYajlParseStartMap,
elektraYajlParseMapKey,
elektraYajlParseEnd,
elektraYajlParseStartArray,
elektraYajlParseEnd };
ksAppendKey (returned, keyNew (keyName ((parentKey)), KEY_END));
Context context = { .ks = returned, .cursor = 0 };
#if YAJL_MAJOR == 1
yajl_parser_config cfg = { 1, 1 };
yajl_handle hand = yajl_alloc (&callbacks, &cfg, NULL, &context);
#else
yajl_handle hand = yajl_alloc (&callbacks, NULL, &context);
yajl_config (hand, yajl_allow_comments, 1);
#endif
int errnosave = errno;
unsigned char fileData[65536];
int done = 0;
FILE * fileHandle = fopen (keyString (parentKey), "r");
if (!fileHandle)
{
yajl_free (hand);
ELEKTRA_SET_ERROR_GET (parentKey);
errno = errnosave;
return -1;
}
while (!done)
{
yajl_size_type rd = fread ((void *) fileData, 1, sizeof (fileData) - 1, fileHandle);
if (rd == 0)
{
if (!feof (fileHandle))
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Error while reading file: %s", keyString (parentKey));
fclose (fileHandle);
yajl_free (hand);
return -1;
}
done = 1;
}
fileData[rd] = 0;
yajl_status stat;
if (done)
{
#if YAJL_MAJOR == 1
stat = yajl_parse_complete (hand);
#else
stat = yajl_complete_parse (hand);
#endif
}
else
{
stat = yajl_parse (hand, fileData, rd);
}
int test_status = (stat != yajl_status_ok);
#if YAJL_MAJOR == 1
test_status = test_status && (stat != yajl_status_insufficient_data);
#endif
if (test_status)
{
unsigned char * str = yajl_get_error (hand, 1, fileData, rd);
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Yajl parse error happened. Reason: %s", (char *) str);
yajl_free_error (hand, str);
yajl_free (hand);
fclose (fileHandle);
return -1;
}
}
yajl_free (hand);
fclose (fileHandle);
elektraYajlParseSuppressNonLeafKeys (returned);
elektraYajlParseSuppressEmptyMap (returned, parentKey);
return 1; /* success */
}