Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * @file xpath.c |
3 | | * @author Michal Vasko <mvasko@cesnet.cz> |
4 | | * @brief YANG XPath evaluation functions |
5 | | * |
6 | | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
7 | | * |
8 | | * This source code is licensed under BSD 3-Clause License (the "License"). |
9 | | * You may not use this file except in compliance with the License. |
10 | | * You may obtain a copy of the License at |
11 | | * |
12 | | * https://opensource.org/licenses/BSD-3-Clause |
13 | | */ |
14 | | #define _GNU_SOURCE /* asprintf, strdup */ |
15 | | #define _DEFAULT_SOURCE /* fmodl */ |
16 | | |
17 | | #include "xpath.h" |
18 | | |
19 | | #include <assert.h> |
20 | | #include <ctype.h> |
21 | | #include <errno.h> |
22 | | #include <math.h> |
23 | | #include <stdint.h> |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | |
28 | | #include "compat.h" |
29 | | #include "context.h" |
30 | | #include "dict.h" |
31 | | #include "hash_table.h" |
32 | | #include "ly_common.h" |
33 | | #include "out.h" |
34 | | #include "parser_data.h" |
35 | | #include "path.h" |
36 | | #include "plugins_exts/metadata.h" |
37 | | #include "plugins_types.h" |
38 | | #include "printer_data.h" |
39 | | #include "schema_compile_node.h" |
40 | | #include "tree.h" |
41 | | #include "tree_data.h" |
42 | | #include "tree_data_internal.h" |
43 | | #include "tree_edit.h" |
44 | | #include "tree_schema_internal.h" |
45 | | #include "xml.h" |
46 | | |
47 | | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth); |
48 | | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, |
49 | | struct lyxp_set *set, uint32_t options); |
50 | | static LY_ERR moveto_resolve_module(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
51 | | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
52 | | static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, |
53 | | const struct lyd_node *node, enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set); |
54 | | static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
55 | | enum lyxp_axis axis, uint32_t options); |
56 | | static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
57 | | enum lyxp_axis axis, uint32_t options); |
58 | | static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result); |
59 | | |
60 | | /* Functions are divided into the following basic classes: |
61 | | * |
62 | | * (re)parse functions: |
63 | | * Parse functions parse the expression into |
64 | | * tokens (syntactic analysis). |
65 | | * Reparse functions perform semantic analysis |
66 | | * (do not save the result, just a check) of |
67 | | * the expression and fill repeat indices. |
68 | | * |
69 | | * warn functions: |
70 | | * Warn functions check specific reasonable conditions for schema XPath |
71 | | * and print a warning if they are not satisfied. |
72 | | * |
73 | | * moveto functions: |
74 | | * They and only they actually change the context (set). |
75 | | * |
76 | | * eval functions: |
77 | | * They execute a parsed XPath expression on some data subtree. |
78 | | */ |
79 | | |
80 | | /** |
81 | | * @brief Print the type of an XPath \p set. |
82 | | * |
83 | | * @param[in] set Set to use. |
84 | | * @return Set type string. |
85 | | */ |
86 | | static const char * |
87 | | print_set_type(struct lyxp_set *set) |
88 | 0 | { |
89 | 0 | switch (set->type) { |
90 | 0 | case LYXP_SET_NODE_SET: |
91 | 0 | return "node set"; |
92 | 0 | case LYXP_SET_SCNODE_SET: |
93 | 0 | return "schema node set"; |
94 | 0 | case LYXP_SET_BOOLEAN: |
95 | 0 | return "boolean"; |
96 | 0 | case LYXP_SET_NUMBER: |
97 | 0 | return "number"; |
98 | 0 | case LYXP_SET_STRING: |
99 | 0 | return "string"; |
100 | 0 | } |
101 | | |
102 | 0 | return NULL; |
103 | 0 | } |
104 | | |
105 | | const char * |
106 | | lyxp_token2str(enum lyxp_token tok) |
107 | 12.2k | { |
108 | 12.2k | switch (tok) { |
109 | 370 | case LYXP_TOKEN_PAR1: |
110 | 370 | return "("; |
111 | 350 | case LYXP_TOKEN_PAR2: |
112 | 350 | return ")"; |
113 | 266 | case LYXP_TOKEN_BRACK1: |
114 | 266 | return "["; |
115 | 258 | case LYXP_TOKEN_BRACK2: |
116 | 258 | return "]"; |
117 | 2.41k | case LYXP_TOKEN_DOT: |
118 | 2.41k | return "."; |
119 | 513 | case LYXP_TOKEN_DDOT: |
120 | 513 | return ".."; |
121 | 13 | case LYXP_TOKEN_AT: |
122 | 13 | return "@"; |
123 | 16 | case LYXP_TOKEN_COMMA: |
124 | 16 | return ","; |
125 | 0 | case LYXP_TOKEN_DCOLON: |
126 | 0 | return "::"; |
127 | 1.02k | case LYXP_TOKEN_NAMETEST: |
128 | 1.02k | return "NameTest"; |
129 | 0 | case LYXP_TOKEN_NODETYPE: |
130 | 0 | return "NodeType"; |
131 | 7 | case LYXP_TOKEN_VARREF: |
132 | 7 | return "VariableReference"; |
133 | 417 | case LYXP_TOKEN_FUNCNAME: |
134 | 417 | return "FunctionName"; |
135 | 0 | case LYXP_TOKEN_OPER_LOG: |
136 | 0 | return "Operator(Logic)"; |
137 | 2.51k | case LYXP_TOKEN_OPER_EQUAL: |
138 | 2.51k | return "Operator(Equal)"; |
139 | 1 | case LYXP_TOKEN_OPER_NEQUAL: |
140 | 1 | return "Operator(Non-equal)"; |
141 | 27 | case LYXP_TOKEN_OPER_COMP: |
142 | 27 | return "Operator(Comparison)"; |
143 | 31 | case LYXP_TOKEN_OPER_MATH: |
144 | 31 | return "Operator(Math)"; |
145 | 5 | case LYXP_TOKEN_OPER_UNI: |
146 | 5 | return "Operator(Union)"; |
147 | 1.12k | case LYXP_TOKEN_OPER_PATH: |
148 | 1.12k | return "Operator(Path)"; |
149 | 67 | case LYXP_TOKEN_OPER_RPATH: |
150 | 67 | return "Operator(Recursive Path)"; |
151 | 15 | case LYXP_TOKEN_AXISNAME: |
152 | 15 | return "AxisName"; |
153 | 2.76k | case LYXP_TOKEN_LITERAL: |
154 | 2.76k | return "Literal"; |
155 | 33 | case LYXP_TOKEN_NUMBER: |
156 | 33 | return "Number"; |
157 | 0 | default: |
158 | 0 | LOGINT(NULL); |
159 | 0 | return ""; |
160 | 12.2k | } |
161 | 12.2k | } |
162 | | |
163 | | /** |
164 | | * @brief Transform string into an axis. |
165 | | * |
166 | | * @param[in] str String to transform. |
167 | | * @param[in] str_len Length of @p str. |
168 | | * @return Transformed axis. |
169 | | */ |
170 | | static enum lyxp_axis |
171 | | str2axis(const char *str, uint32_t str_len) |
172 | 0 | { |
173 | 0 | switch (str_len) { |
174 | 0 | case 4: |
175 | 0 | assert(!strncmp("self", str, str_len)); |
176 | 0 | return LYXP_AXIS_SELF; |
177 | 0 | case 5: |
178 | 0 | assert(!strncmp("child", str, str_len)); |
179 | 0 | return LYXP_AXIS_CHILD; |
180 | 0 | case 6: |
181 | 0 | assert(!strncmp("parent", str, str_len)); |
182 | 0 | return LYXP_AXIS_PARENT; |
183 | 0 | case 8: |
184 | 0 | assert(!strncmp("ancestor", str, str_len)); |
185 | 0 | return LYXP_AXIS_ANCESTOR; |
186 | 0 | case 9: |
187 | 0 | if (str[0] == 'a') { |
188 | 0 | assert(!strncmp("attribute", str, str_len)); |
189 | 0 | return LYXP_AXIS_ATTRIBUTE; |
190 | 0 | } else if (str[0] == 'f') { |
191 | 0 | assert(!strncmp("following", str, str_len)); |
192 | 0 | return LYXP_AXIS_FOLLOWING; |
193 | 0 | } else { |
194 | 0 | assert(!strncmp("preceding", str, str_len)); |
195 | 0 | return LYXP_AXIS_PRECEDING; |
196 | 0 | } |
197 | 0 | break; |
198 | 0 | case 10: |
199 | 0 | assert(!strncmp("descendant", str, str_len)); |
200 | 0 | return LYXP_AXIS_DESCENDANT; |
201 | 0 | case 16: |
202 | 0 | assert(!strncmp("ancestor-or-self", str, str_len)); |
203 | 0 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
204 | 0 | case 17: |
205 | 0 | if (str[0] == 'f') { |
206 | 0 | assert(!strncmp("following-sibling", str, str_len)); |
207 | 0 | return LYXP_AXIS_FOLLOWING_SIBLING; |
208 | 0 | } else { |
209 | 0 | assert(!strncmp("preceding-sibling", str, str_len)); |
210 | 0 | return LYXP_AXIS_PRECEDING_SIBLING; |
211 | 0 | } |
212 | 0 | break; |
213 | 0 | case 18: |
214 | 0 | assert(!strncmp("descendant-or-self", str, str_len)); |
215 | 0 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
216 | 0 | } |
217 | | |
218 | 0 | LOGINT(NULL); |
219 | 0 | return 0; |
220 | 0 | } |
221 | | |
222 | | /** |
223 | | * @brief Append a string to a dynamic string variable. |
224 | | * |
225 | | * @param[in,out] str String to use. |
226 | | * @param[in,out] size String size. |
227 | | * @param[in,out] used String used size excluding terminating zero. |
228 | | * @param[in] format Message format. |
229 | | * @param[in] ... Message format arguments. |
230 | | */ |
231 | | static void |
232 | | print_expr_str(char **str, size_t *size, size_t *used, const char *format, ...) |
233 | 0 | { |
234 | 0 | int p; |
235 | 0 | va_list ap; |
236 | |
|
237 | 0 | va_start(ap, format); |
238 | | |
239 | | /* try to append the string */ |
240 | 0 | p = vsnprintf(*str ? *str + *used : NULL, *size - *used, format, ap); |
241 | |
|
242 | 0 | if ((unsigned)p >= *size - *used) { |
243 | | /* realloc */ |
244 | 0 | *str = ly_realloc(*str, *size + p + 1); |
245 | 0 | *size += p + 1; |
246 | | |
247 | | /* restart ap */ |
248 | 0 | va_end(ap); |
249 | 0 | va_start(ap, format); |
250 | | |
251 | | /* print */ |
252 | 0 | p = vsnprintf(*str + *used, *size - *used, format, ap); |
253 | 0 | } |
254 | |
|
255 | 0 | *used += p; |
256 | 0 | va_end(ap); |
257 | 0 | } |
258 | | |
259 | | /** |
260 | | * @brief Print the whole expression @p exp to debug output. |
261 | | * |
262 | | * @param[in] exp Expression to use. |
263 | | */ |
264 | | static void |
265 | | print_expr_struct_debug(const struct lyxp_expr *exp) |
266 | 77.2k | { |
267 | 77.2k | char *buf = NULL; |
268 | 77.2k | uint32_t i, j; |
269 | 77.2k | size_t size = 0, used = 0; |
270 | | |
271 | 77.2k | if (!exp || (ly_ll < LY_LLDBG)) { |
272 | 77.2k | return; |
273 | 77.2k | } |
274 | | |
275 | 0 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
276 | 0 | for (i = 0; i < exp->used; ++i) { |
277 | 0 | print_expr_str(&buf, &size, &used, "\ttoken %s, in expression \"%.*s\"", |
278 | 0 | lyxp_token2str(exp->tokens[i]), exp->tok_len[i], &exp->expr[exp->tok_pos[i]]); |
279 | |
|
280 | 0 | if (exp->repeat && exp->repeat[i]) { |
281 | 0 | print_expr_str(&buf, &size, &used, " (repeat %d", exp->repeat[i][0]); |
282 | 0 | for (j = 1; exp->repeat[i][j]; ++j) { |
283 | 0 | print_expr_str(&buf, &size, &used, ", %d", exp->repeat[i][j]); |
284 | 0 | } |
285 | 0 | print_expr_str(&buf, &size, &used, ")"); |
286 | 0 | } |
287 | 0 | LOGDBG(LY_LDGXPATH, buf); |
288 | 0 | used = 0; |
289 | 0 | } |
290 | |
|
291 | 0 | free(buf); |
292 | 0 | } |
293 | | |
294 | | #ifndef NDEBUG |
295 | | |
296 | | /** |
297 | | * @brief Print XPath set content to debug output. |
298 | | * |
299 | | * @param[in] set Set to print. |
300 | | */ |
301 | | static void |
302 | | print_set_debug(struct lyxp_set *set) |
303 | 472 | { |
304 | 472 | uint32_t i; |
305 | 472 | char *str; |
306 | 472 | struct lyxp_set_node *item; |
307 | 472 | struct lyxp_set_scnode *sitem; |
308 | | |
309 | 472 | if (ly_ll < LY_LLDBG) { |
310 | 472 | return; |
311 | 472 | } |
312 | | |
313 | 0 | switch (set->type) { |
314 | 0 | case LYXP_SET_NODE_SET: |
315 | 0 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
316 | 0 | for (i = 0; i < set->used; ++i) { |
317 | 0 | item = &set->val.nodes[i]; |
318 | |
|
319 | 0 | switch (item->type) { |
320 | 0 | case LYXP_NODE_NONE: |
321 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
322 | 0 | break; |
323 | 0 | case LYXP_NODE_ROOT: |
324 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
325 | 0 | break; |
326 | 0 | case LYXP_NODE_ROOT_CONFIG: |
327 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
328 | 0 | break; |
329 | 0 | case LYXP_NODE_ELEM: |
330 | 0 | if (item->node->schema && (item->node->schema->nodetype == LYS_LIST) && |
331 | 0 | (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) { |
332 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
333 | 0 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
334 | 0 | } else if (lyd_get_value(item->node)) { |
335 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
336 | 0 | LYD_NAME(item->node), lyd_get_value(item->node)); |
337 | 0 | } else { |
338 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, LYD_NAME(item->node)); |
339 | 0 | } |
340 | 0 | break; |
341 | 0 | case LYXP_NODE_TEXT: |
342 | 0 | if (item->node->schema && (item->node->schema->nodetype & LYS_ANYDATA)) { |
343 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
344 | 0 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
345 | 0 | } else { |
346 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node)); |
347 | 0 | } |
348 | 0 | break; |
349 | 0 | case LYXP_NODE_META: |
350 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
351 | 0 | set->val.meta[i].meta->value); |
352 | 0 | break; |
353 | 0 | } |
354 | 0 | } |
355 | 0 | break; |
356 | | |
357 | 0 | case LYXP_SET_SCNODE_SET: |
358 | 0 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
359 | 0 | for (i = 0; i < set->used; ++i) { |
360 | 0 | sitem = &set->val.scnodes[i]; |
361 | |
|
362 | 0 | switch (sitem->type) { |
363 | 0 | case LYXP_NODE_ROOT: |
364 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
365 | 0 | break; |
366 | 0 | case LYXP_NODE_ROOT_CONFIG: |
367 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
368 | 0 | break; |
369 | 0 | case LYXP_NODE_ELEM: |
370 | 0 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
371 | 0 | break; |
372 | 0 | default: |
373 | 0 | LOGINT(NULL); |
374 | 0 | break; |
375 | 0 | } |
376 | 0 | } |
377 | 0 | break; |
378 | | |
379 | 0 | case LYXP_SET_BOOLEAN: |
380 | 0 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
381 | 0 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
382 | 0 | break; |
383 | | |
384 | 0 | case LYXP_SET_STRING: |
385 | 0 | LOGDBG(LY_LDGXPATH, "set STRING"); |
386 | 0 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
387 | 0 | break; |
388 | | |
389 | 0 | case LYXP_SET_NUMBER: |
390 | 0 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
391 | |
|
392 | 0 | if (isnan(set->val.num)) { |
393 | 0 | str = strdup("NaN"); |
394 | 0 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
395 | 0 | str = strdup("0"); |
396 | 0 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
397 | 0 | str = strdup("Infinity"); |
398 | 0 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
399 | 0 | str = strdup("-Infinity"); |
400 | 0 | } else if ((long long)set->val.num == set->val.num) { |
401 | 0 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
402 | 0 | str = NULL; |
403 | 0 | } |
404 | 0 | } else { |
405 | 0 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
406 | 0 | str = NULL; |
407 | 0 | } |
408 | 0 | } |
409 | 0 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
410 | |
|
411 | 0 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
412 | 0 | free(str); |
413 | 0 | } |
414 | 0 | } |
415 | | |
416 | | #endif |
417 | | |
418 | | /** |
419 | | * @brief Realloc the string \p str. |
420 | | * |
421 | | * @param[in] ctx libyang context for logging. |
422 | | * @param[in] needed How much free space is required. |
423 | | * @param[in,out] str Pointer to the string to use. |
424 | | * @param[in,out] used Used bytes in \p str. |
425 | | * @param[in,out] size Allocated bytes in \p str. |
426 | | * @return LY_ERR |
427 | | */ |
428 | | static LY_ERR |
429 | | cast_string_realloc(const struct ly_ctx *ctx, uint64_t needed, char **str, uint32_t *used, uint32_t *size) |
430 | 2.34k | { |
431 | 2.34k | if (*size - (unsigned)*used < needed) { |
432 | 342k | do { |
433 | 342k | if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
434 | 0 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX); |
435 | 0 | return LY_EINVAL; |
436 | 0 | } |
437 | 342k | *size += LYXP_STRING_CAST_SIZE_STEP; |
438 | 342k | } while (*size - (unsigned)*used < needed); |
439 | 401 | *str = ly_realloc(*str, *size * sizeof(char)); |
440 | 401 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
441 | 401 | } |
442 | | |
443 | 2.34k | return LY_SUCCESS; |
444 | 2.34k | } |
445 | | |
446 | | /** |
447 | | * @brief Cast nodes recursively to one string @p str. |
448 | | * |
449 | | * @param[in] node Node to cast, NULL if root. |
450 | | * @param[in] set XPath set. |
451 | | * @param[in] indent Current indent. |
452 | | * @param[in,out] str Resulting string. |
453 | | * @param[in,out] used Used bytes in @p str. |
454 | | * @param[in,out] size Allocated bytes in @p str. |
455 | | * @return LY_ERR value. |
456 | | */ |
457 | | static LY_ERR |
458 | | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used, |
459 | | uint32_t *size) |
460 | 2.34k | { |
461 | 2.34k | char *buf, *line, *ptr = NULL; |
462 | 2.34k | const char *value_str; |
463 | 2.34k | uint16_t nodetype; |
464 | 2.34k | const struct lyd_node *child; |
465 | 2.34k | enum lyxp_node_type child_type; |
466 | 2.34k | struct lyd_node *tree; |
467 | 2.34k | struct lyd_node_any *any; |
468 | 2.34k | LY_ERR rc; |
469 | | |
470 | 2.34k | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && node->schema && (node->schema->flags & LYS_CONFIG_R)) { |
471 | 0 | return LY_SUCCESS; |
472 | 0 | } |
473 | | |
474 | 2.34k | if (!node) { |
475 | | /* fake container */ |
476 | 0 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
477 | 0 | strcpy(*str + (*used - 1), "\n"); |
478 | 0 | ++(*used); |
479 | |
|
480 | 0 | ++indent; |
481 | | |
482 | | /* print all the top-level nodes */ |
483 | 0 | child = NULL; |
484 | 0 | child_type = 0; |
485 | 0 | while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) { |
486 | 0 | LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size)); |
487 | 0 | } |
488 | | |
489 | | /* end fake container */ |
490 | 0 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
491 | 0 | strcpy(*str + (*used - 1), "\n"); |
492 | 0 | ++(*used); |
493 | |
|
494 | 0 | --indent; |
495 | 2.34k | } else { |
496 | 2.34k | if (node->schema) { |
497 | 2.34k | nodetype = node->schema->nodetype; |
498 | 2.34k | } else if (lyd_child(node)) { |
499 | 0 | nodetype = LYS_CONTAINER; |
500 | 0 | } else { |
501 | 0 | nodetype = LYS_LEAF; |
502 | 0 | } |
503 | | |
504 | 2.34k | switch (nodetype) { |
505 | 0 | case LYS_CONTAINER: |
506 | 0 | case LYS_LIST: |
507 | 0 | case LYS_RPC: |
508 | 0 | case LYS_NOTIF: |
509 | 0 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
510 | 0 | strcpy(*str + (*used - 1), "\n"); |
511 | 0 | ++(*used); |
512 | |
|
513 | 0 | for (child = lyd_child(node); child; child = child->next) { |
514 | 0 | LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size)); |
515 | 0 | } |
516 | | |
517 | 0 | break; |
518 | | |
519 | 0 | case LYS_LEAF: |
520 | 2.34k | case LYS_LEAFLIST: |
521 | 2.34k | value_str = lyd_get_value(node); |
522 | | |
523 | | /* print indent */ |
524 | 2.34k | LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size)); |
525 | 2.34k | memset(*str + (*used - 1), ' ', indent * 2); |
526 | 2.34k | *used += indent * 2; |
527 | | |
528 | | /* print value */ |
529 | 2.34k | if (*used == 1) { |
530 | 2.34k | sprintf(*str + (*used - 1), "%s", value_str); |
531 | 2.34k | *used += strlen(value_str); |
532 | 2.34k | } else { |
533 | 0 | sprintf(*str + (*used - 1), "%s\n", value_str); |
534 | 0 | *used += strlen(value_str) + 1; |
535 | 0 | } |
536 | | |
537 | 2.34k | break; |
538 | | |
539 | 0 | case LYS_ANYXML: |
540 | 0 | case LYS_ANYDATA: |
541 | 0 | any = (struct lyd_node_any *)node; |
542 | 0 | if (!(void *)any->value.tree) { |
543 | | /* no content */ |
544 | 0 | buf = strdup(""); |
545 | 0 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
546 | 0 | } else { |
547 | 0 | struct ly_out *out; |
548 | |
|
549 | 0 | if (any->value_type == LYD_ANYDATA_LYB) { |
550 | | /* try to parse it into a data tree */ |
551 | 0 | if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB, |
552 | 0 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
553 | | /* successfully parsed */ |
554 | 0 | free(any->value.mem); |
555 | 0 | any->value.tree = tree; |
556 | 0 | any->value_type = LYD_ANYDATA_DATATREE; |
557 | 0 | } |
558 | | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
559 | 0 | } |
560 | |
|
561 | 0 | switch (any->value_type) { |
562 | 0 | case LYD_ANYDATA_STRING: |
563 | 0 | case LYD_ANYDATA_XML: |
564 | 0 | case LYD_ANYDATA_JSON: |
565 | 0 | buf = strdup(any->value.json); |
566 | 0 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
567 | 0 | break; |
568 | 0 | case LYD_ANYDATA_DATATREE: |
569 | 0 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
570 | 0 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
571 | 0 | ly_out_free(out, NULL, 0); |
572 | 0 | LY_CHECK_RET(rc); |
573 | 0 | break; |
574 | 0 | case LYD_ANYDATA_LYB: |
575 | 0 | LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string."); |
576 | 0 | return LY_EINVAL; |
577 | 0 | } |
578 | 0 | } |
579 | | |
580 | 0 | line = strtok_r(buf, "\n", &ptr); |
581 | 0 | do { |
582 | 0 | rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size); |
583 | 0 | if (rc != LY_SUCCESS) { |
584 | 0 | free(buf); |
585 | 0 | return rc; |
586 | 0 | } |
587 | 0 | memset(*str + (*used - 1), ' ', indent * 2); |
588 | 0 | *used += indent * 2; |
589 | |
|
590 | 0 | strcpy(*str + (*used - 1), line); |
591 | 0 | *used += strlen(line); |
592 | |
|
593 | 0 | strcpy(*str + (*used - 1), "\n"); |
594 | 0 | *used += 1; |
595 | 0 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
596 | | |
597 | 0 | free(buf); |
598 | 0 | break; |
599 | | |
600 | 0 | default: |
601 | 0 | LOGINT_RET(set->ctx); |
602 | 2.34k | } |
603 | 2.34k | } |
604 | | |
605 | 2.34k | return LY_SUCCESS; |
606 | 2.34k | } |
607 | | |
608 | | /** |
609 | | * @brief Cast an element into a string. |
610 | | * |
611 | | * @param[in] node Node to cast, NULL if root. |
612 | | * @param[in] set XPath set. |
613 | | * @param[out] str Element cast to dynamically-allocated string. |
614 | | * @return LY_ERR |
615 | | */ |
616 | | static LY_ERR |
617 | | cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str) |
618 | 2.34k | { |
619 | 2.34k | uint32_t used, size; |
620 | 2.34k | LY_ERR rc; |
621 | | |
622 | 2.34k | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
623 | 2.34k | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
624 | 2.34k | (*str)[0] = '\0'; |
625 | 2.34k | used = 1; |
626 | 2.34k | size = LYXP_STRING_CAST_SIZE_START; |
627 | | |
628 | 2.34k | rc = cast_string_recursive(node, set, 0, str, &used, &size); |
629 | 2.34k | if (rc != LY_SUCCESS) { |
630 | 0 | free(*str); |
631 | 0 | return rc; |
632 | 0 | } |
633 | | |
634 | 2.34k | if (size > used) { |
635 | 2.34k | *str = ly_realloc(*str, used * sizeof(char)); |
636 | 2.34k | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
637 | 2.34k | } |
638 | 2.34k | return LY_SUCCESS; |
639 | 2.34k | } |
640 | | |
641 | | /** |
642 | | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
643 | | * Context position aware. |
644 | | * |
645 | | * @param[in] set Set to cast. |
646 | | * @param[out] str Cast dynamically-allocated string. |
647 | | * @return LY_ERR |
648 | | */ |
649 | | static LY_ERR |
650 | | cast_node_set_to_string(struct lyxp_set *set, char **str) |
651 | 2.50k | { |
652 | 2.50k | if (!set->used) { |
653 | 156 | *str = strdup(""); |
654 | 156 | if (!*str) { |
655 | 0 | LOGMEM_RET(set->ctx); |
656 | 0 | } |
657 | 156 | return LY_SUCCESS; |
658 | 156 | } |
659 | | |
660 | 2.34k | switch (set->val.nodes[0].type) { |
661 | 0 | case LYXP_NODE_NONE: |
662 | | /* invalid */ |
663 | 0 | LOGINT_RET(set->ctx); |
664 | 0 | case LYXP_NODE_ROOT: |
665 | 0 | case LYXP_NODE_ROOT_CONFIG: |
666 | 2.34k | case LYXP_NODE_ELEM: |
667 | 2.34k | case LYXP_NODE_TEXT: |
668 | 2.34k | return cast_string_elem(set->val.nodes[0].node, set, str); |
669 | 0 | case LYXP_NODE_META: |
670 | 0 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
671 | 0 | if (!*str) { |
672 | 0 | LOGMEM_RET(set->ctx); |
673 | 0 | } |
674 | 0 | return LY_SUCCESS; |
675 | 2.34k | } |
676 | | |
677 | 2.34k | LOGINT_RET(set->ctx); |
678 | 2.34k | } |
679 | | |
680 | | /** |
681 | | * @brief Cast a string into an XPath number. |
682 | | * |
683 | | * @param[in] str String to use. |
684 | | * @return Cast number. |
685 | | */ |
686 | | static long double |
687 | | cast_string_to_number(const char *str) |
688 | 0 | { |
689 | 0 | long double num; |
690 | 0 | char *ptr; |
691 | |
|
692 | 0 | errno = 0; |
693 | 0 | num = strtold(str, &ptr); |
694 | 0 | if (errno || *ptr || (ptr == str)) { |
695 | 0 | num = NAN; |
696 | 0 | } |
697 | 0 | return num; |
698 | 0 | } |
699 | | |
700 | | /** |
701 | | * @brief Callback for checking value equality. |
702 | | * |
703 | | * Implementation of ::lyht_value_equal_cb. |
704 | | * |
705 | | * @param[in] val1_p First value. |
706 | | * @param[in] val2_p Second value. |
707 | | * @param[in] mod Whether hash table is being modified. |
708 | | * @param[in] cb_data Callback data. |
709 | | * @return Boolean value whether values are equal or not. |
710 | | */ |
711 | | static ly_bool |
712 | | set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
713 | 6.63k | { |
714 | 6.63k | struct lyxp_set_hash_node *val1, *val2; |
715 | | |
716 | 6.63k | val1 = (struct lyxp_set_hash_node *)val1_p; |
717 | 6.63k | val2 = (struct lyxp_set_hash_node *)val2_p; |
718 | | |
719 | 6.63k | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
720 | 6.63k | return 1; |
721 | 6.63k | } |
722 | | |
723 | 0 | return 0; |
724 | 6.63k | } |
725 | | |
726 | | /** |
727 | | * @brief Insert node and its hash into set. |
728 | | * |
729 | | * @param[in] set et to insert to. |
730 | | * @param[in] node Node with hash. |
731 | | * @param[in] type Node type. |
732 | | */ |
733 | | static void |
734 | | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
735 | 10.5k | { |
736 | 10.5k | LY_ERR r; |
737 | 10.5k | uint32_t i, hash; |
738 | 10.5k | struct lyxp_set_hash_node hnode; |
739 | | |
740 | 10.5k | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
741 | | /* create hash table and add all the nodes */ |
742 | 80 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
743 | 320 | for (i = 0; i < set->used; ++i) { |
744 | 320 | hnode.node = set->val.nodes[i].node; |
745 | 320 | hnode.type = set->val.nodes[i].type; |
746 | | |
747 | 320 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
748 | 320 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
749 | 320 | hash = lyht_hash_multi(hash, NULL, 0); |
750 | | |
751 | 320 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
752 | 320 | assert(!r); |
753 | 320 | (void)r; |
754 | | |
755 | 320 | if ((hnode.node == node) && (hnode.type == type)) { |
756 | | /* it was just added, do not add it twice */ |
757 | 80 | return; |
758 | 80 | } |
759 | 320 | } |
760 | 80 | } |
761 | | |
762 | 10.4k | if (set->ht) { |
763 | | /* add the new node into hash table */ |
764 | 1.90k | hnode.node = node; |
765 | 1.90k | hnode.type = type; |
766 | | |
767 | 1.90k | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
768 | 1.90k | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
769 | 1.90k | hash = lyht_hash_multi(hash, NULL, 0); |
770 | | |
771 | 1.90k | r = lyht_insert(set->ht, &hnode, hash, NULL); |
772 | 1.90k | assert(!r); |
773 | 1.90k | (void)r; |
774 | 1.90k | } |
775 | 10.4k | } |
776 | | |
777 | | /** |
778 | | * @brief Remove node and its hash from set. |
779 | | * |
780 | | * @param[in] set Set to remove from. |
781 | | * @param[in] node Node to remove. |
782 | | * @param[in] type Node type. |
783 | | */ |
784 | | static void |
785 | | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
786 | 2.28k | { |
787 | 2.28k | LY_ERR r; |
788 | 2.28k | struct lyxp_set_hash_node hnode; |
789 | 2.28k | uint32_t hash; |
790 | | |
791 | 2.28k | if (set->ht) { |
792 | 2.19k | hnode.node = node; |
793 | 2.19k | hnode.type = type; |
794 | | |
795 | 2.19k | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
796 | 2.19k | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
797 | 2.19k | hash = lyht_hash_multi(hash, NULL, 0); |
798 | | |
799 | 2.19k | r = lyht_remove(set->ht, &hnode, hash); |
800 | 2.19k | assert(!r); |
801 | 2.19k | (void)r; |
802 | | |
803 | 2.19k | if (!set->ht->used) { |
804 | 50 | lyht_free(set->ht, NULL); |
805 | 50 | set->ht = NULL; |
806 | 50 | } |
807 | 2.19k | } |
808 | 2.28k | } |
809 | | |
810 | | /** |
811 | | * @brief Check whether node is in set based on its hash. |
812 | | * |
813 | | * @param[in] set Set to search in. |
814 | | * @param[in] node Node to search for. |
815 | | * @param[in] type Node type. |
816 | | * @param[in] skip_idx Index in @p set to skip. |
817 | | * @return LY_ERR |
818 | | */ |
819 | | static LY_ERR |
820 | | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
821 | 0 | { |
822 | 0 | struct lyxp_set_hash_node hnode, *match_p; |
823 | 0 | uint32_t hash; |
824 | |
|
825 | 0 | hnode.node = node; |
826 | 0 | hnode.type = type; |
827 | |
|
828 | 0 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
829 | 0 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
830 | 0 | hash = lyht_hash_multi(hash, NULL, 0); |
831 | |
|
832 | 0 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
833 | 0 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
834 | | /* we found it on the index that should be skipped, find another */ |
835 | 0 | hnode = *match_p; |
836 | 0 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
837 | | /* none other found */ |
838 | 0 | return LY_SUCCESS; |
839 | 0 | } |
840 | 0 | } |
841 | | |
842 | 0 | return LY_EEXIST; |
843 | 0 | } |
844 | | |
845 | | /* not found */ |
846 | 0 | return LY_SUCCESS; |
847 | 0 | } |
848 | | |
849 | | void |
850 | | lyxp_set_free_content(struct lyxp_set *set) |
851 | 26.7k | { |
852 | 26.7k | if (!set) { |
853 | 0 | return; |
854 | 0 | } |
855 | | |
856 | 26.7k | if (set->type == LYXP_SET_NODE_SET) { |
857 | 18.1k | free(set->val.nodes); |
858 | 18.1k | lyht_free(set->ht, NULL); |
859 | 18.1k | } else if (set->type == LYXP_SET_SCNODE_SET) { |
860 | 334 | free(set->val.scnodes); |
861 | 334 | lyht_free(set->ht, NULL); |
862 | 8.19k | } else { |
863 | 8.19k | if (set->type == LYXP_SET_STRING) { |
864 | 4.99k | free(set->val.str); |
865 | 4.99k | } |
866 | 8.19k | set->type = LYXP_SET_NODE_SET; |
867 | 8.19k | } |
868 | | |
869 | 26.7k | set->val.nodes = NULL; |
870 | 26.7k | set->used = 0; |
871 | 26.7k | set->size = 0; |
872 | 26.7k | set->ht = NULL; |
873 | 26.7k | set->ctx_pos = 0; |
874 | 26.7k | set->ctx_size = 0; |
875 | 26.7k | } |
876 | | |
877 | | /** |
878 | | * @brief Free dynamically-allocated set. |
879 | | * |
880 | | * @param[in] set Set to free. |
881 | | */ |
882 | | static void |
883 | | lyxp_set_free(struct lyxp_set *set) |
884 | 0 | { |
885 | 0 | if (!set) { |
886 | 0 | return; |
887 | 0 | } |
888 | | |
889 | 0 | lyxp_set_free_content(set); |
890 | 0 | free(set); |
891 | 0 | } |
892 | | |
893 | | /** |
894 | | * @brief Initialize set context. |
895 | | * |
896 | | * @param[in] new Set to initialize. |
897 | | * @param[in] set Arbitrary initialized set. |
898 | | */ |
899 | | static void |
900 | | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
901 | 17.4k | { |
902 | 17.4k | memset(new, 0, sizeof *new); |
903 | 17.4k | if (!set) { |
904 | 0 | return; |
905 | 0 | } |
906 | | |
907 | 17.4k | new->non_child_axis = set->non_child_axis; |
908 | 17.4k | new->not_found = set->not_found; |
909 | 17.4k | new->ctx = set->ctx; |
910 | 17.4k | new->cur_node = set->cur_node; |
911 | 17.4k | new->root_type = set->root_type; |
912 | 17.4k | new->context_op = set->context_op; |
913 | 17.4k | new->tree = set->tree; |
914 | 17.4k | new->cur_mod = set->cur_mod; |
915 | 17.4k | new->format = set->format; |
916 | 17.4k | new->prefix_data = set->prefix_data; |
917 | 17.4k | new->vars = set->vars; |
918 | 17.4k | } |
919 | | |
920 | | /** |
921 | | * @brief Create a deep copy of a set. |
922 | | * |
923 | | * @param[in] set Set to copy. |
924 | | * @return Copy of @p set. |
925 | | */ |
926 | | static struct lyxp_set * |
927 | | set_copy(struct lyxp_set *set) |
928 | 0 | { |
929 | 0 | struct lyxp_set *ret; |
930 | 0 | uint32_t i; |
931 | |
|
932 | 0 | if (!set) { |
933 | 0 | return NULL; |
934 | 0 | } |
935 | | |
936 | 0 | ret = malloc(sizeof *ret); |
937 | 0 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
938 | 0 | set_init(ret, set); |
939 | |
|
940 | 0 | if (set->type == LYXP_SET_SCNODE_SET) { |
941 | 0 | ret->type = set->type; |
942 | |
|
943 | 0 | for (i = 0; i < set->used; ++i) { |
944 | 0 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
945 | 0 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
946 | 0 | uint32_t idx; |
947 | |
|
948 | 0 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, |
949 | 0 | set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL); |
950 | | /* coverity seems to think scnodes can be NULL */ |
951 | 0 | if (!ret->val.scnodes) { |
952 | 0 | lyxp_set_free(ret); |
953 | 0 | return NULL; |
954 | 0 | } |
955 | 0 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
956 | 0 | } |
957 | 0 | } |
958 | 0 | } else if (set->type == LYXP_SET_NODE_SET) { |
959 | 0 | ret->type = set->type; |
960 | 0 | if (set->used) { |
961 | 0 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
962 | 0 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
963 | 0 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
964 | 0 | } else { |
965 | 0 | ret->val.nodes = NULL; |
966 | 0 | } |
967 | | |
968 | 0 | ret->used = ret->size = set->used; |
969 | 0 | ret->ctx_pos = set->ctx_pos; |
970 | 0 | ret->ctx_size = set->ctx_size; |
971 | 0 | if (set->ht) { |
972 | 0 | ret->ht = lyht_dup(set->ht); |
973 | 0 | } |
974 | 0 | } else { |
975 | 0 | memcpy(ret, set, sizeof *ret); |
976 | 0 | if (set->type == LYXP_SET_STRING) { |
977 | 0 | ret->val.str = strdup(set->val.str); |
978 | 0 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
979 | 0 | } |
980 | 0 | } |
981 | | |
982 | 0 | return ret; |
983 | 0 | } |
984 | | |
985 | | /** |
986 | | * @brief Fill XPath set with a string. Any current data are disposed of. |
987 | | * |
988 | | * @param[in] set Set to fill. |
989 | | * @param[in] string String to fill into \p set. |
990 | | * @param[in] str_len Length of \p string. 0 is a valid value! |
991 | | */ |
992 | | static void |
993 | | set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len) |
994 | 2.48k | { |
995 | 2.48k | lyxp_set_free_content(set); |
996 | | |
997 | 2.48k | set->type = LYXP_SET_STRING; |
998 | 2.48k | if ((str_len == 0) && (string[0] != '\0')) { |
999 | 0 | string = ""; |
1000 | 0 | } |
1001 | 2.48k | set->val.str = strndup(string, str_len); |
1002 | 2.48k | } |
1003 | | |
1004 | | /** |
1005 | | * @brief Fill XPath set with a number. Any current data are disposed of. |
1006 | | * |
1007 | | * @param[in] set Set to fill. |
1008 | | * @param[in] number Number to fill into \p set. |
1009 | | */ |
1010 | | static void |
1011 | | set_fill_number(struct lyxp_set *set, long double number) |
1012 | 0 | { |
1013 | 0 | lyxp_set_free_content(set); |
1014 | |
|
1015 | 0 | set->type = LYXP_SET_NUMBER; |
1016 | 0 | set->val.num = number; |
1017 | 0 | } |
1018 | | |
1019 | | /** |
1020 | | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
1021 | | * |
1022 | | * @param[in] set Set to fill. |
1023 | | * @param[in] boolean Boolean to fill into \p set. |
1024 | | */ |
1025 | | static void |
1026 | | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
1027 | 2.34k | { |
1028 | 2.34k | lyxp_set_free_content(set); |
1029 | | |
1030 | 2.34k | set->type = LYXP_SET_BOOLEAN; |
1031 | 2.34k | set->val.bln = boolean; |
1032 | 2.34k | } |
1033 | | |
1034 | | /** |
1035 | | * @brief Fill XPath set with the value from another set (deep assign). |
1036 | | * Any current data are disposed of. |
1037 | | * |
1038 | | * @param[in] trg Set to fill. |
1039 | | * @param[in] src Source set to copy into \p trg. |
1040 | | */ |
1041 | | static void |
1042 | | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
1043 | 4.76k | { |
1044 | 4.76k | if (!trg || !src) { |
1045 | 0 | return; |
1046 | 0 | } |
1047 | | |
1048 | 4.76k | lyxp_set_free_content(trg); |
1049 | 4.76k | set_init(trg, src); |
1050 | | |
1051 | 4.76k | if (src->type == LYXP_SET_SCNODE_SET) { |
1052 | 0 | trg->type = LYXP_SET_SCNODE_SET; |
1053 | 0 | trg->used = src->used; |
1054 | 0 | trg->size = src->used; |
1055 | |
|
1056 | 0 | if (trg->size) { |
1057 | 0 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
1058 | 0 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
1059 | 0 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
1060 | 0 | } else { |
1061 | 0 | trg->val.scnodes = NULL; |
1062 | 0 | } |
1063 | 4.76k | } else if (src->type == LYXP_SET_BOOLEAN) { |
1064 | 0 | set_fill_boolean(trg, src->val.bln); |
1065 | 4.76k | } else if (src->type == LYXP_SET_NUMBER) { |
1066 | 0 | set_fill_number(trg, src->val.num); |
1067 | 4.76k | } else if (src->type == LYXP_SET_STRING) { |
1068 | 0 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
1069 | 4.76k | } else { |
1070 | 4.76k | if (trg->type == LYXP_SET_NODE_SET) { |
1071 | 4.76k | free(trg->val.nodes); |
1072 | 4.76k | } else if (trg->type == LYXP_SET_STRING) { |
1073 | 0 | free(trg->val.str); |
1074 | 0 | } |
1075 | | |
1076 | 4.76k | assert(src->type == LYXP_SET_NODE_SET); |
1077 | | |
1078 | 4.76k | trg->type = LYXP_SET_NODE_SET; |
1079 | 4.76k | trg->used = src->used; |
1080 | 4.76k | trg->size = src->used; |
1081 | 4.76k | trg->ctx_pos = src->ctx_pos; |
1082 | 4.76k | trg->ctx_size = src->ctx_size; |
1083 | | |
1084 | 4.76k | if (trg->size) { |
1085 | 4.69k | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
1086 | 4.69k | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
1087 | 4.69k | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
1088 | 4.69k | } else { |
1089 | 74 | trg->val.nodes = NULL; |
1090 | 74 | } |
1091 | 4.76k | if (src->ht) { |
1092 | 0 | trg->ht = lyht_dup(src->ht); |
1093 | 4.76k | } else { |
1094 | 4.76k | trg->ht = NULL; |
1095 | 4.76k | } |
1096 | 4.76k | } |
1097 | 4.76k | } |
1098 | | |
1099 | | /** |
1100 | | * @brief Clear context of all schema nodes. |
1101 | | * |
1102 | | * @param[in] set Set to clear. |
1103 | | * @param[in] new_ctx New context state for all the nodes currently in the context. |
1104 | | */ |
1105 | | static void |
1106 | | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
1107 | 334 | { |
1108 | 334 | uint32_t i; |
1109 | | |
1110 | 668 | for (i = 0; i < set->used; ++i) { |
1111 | 334 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
1112 | 0 | set->val.scnodes[i].in_ctx = new_ctx; |
1113 | 334 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
1114 | 334 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
1115 | 334 | } |
1116 | 334 | } |
1117 | 334 | } |
1118 | | |
1119 | | /** |
1120 | | * @brief Remove a node from a set. Removing last node changes |
1121 | | * set into LYXP_SET_EMPTY. Context position aware. |
1122 | | * |
1123 | | * @param[in] set Set to use. |
1124 | | * @param[in] idx Index from @p set of the node to be removed. |
1125 | | */ |
1126 | | static void |
1127 | | set_remove_node(struct lyxp_set *set, uint32_t idx) |
1128 | 0 | { |
1129 | 0 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
1130 | 0 | assert(idx < set->used); |
1131 | | |
1132 | 0 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
1133 | |
|
1134 | 0 | --set->used; |
1135 | 0 | if (idx < set->used) { |
1136 | 0 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
1137 | 0 | } else if (!set->used) { |
1138 | 0 | lyxp_set_free_content(set); |
1139 | 0 | } |
1140 | 0 | } |
1141 | | |
1142 | | /** |
1143 | | * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE. |
1144 | | * |
1145 | | * @param[in] set Set to use. |
1146 | | * @param[in] idx Index from @p set of the node to be removed. |
1147 | | */ |
1148 | | static void |
1149 | | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
1150 | 2.28k | { |
1151 | 2.28k | assert(set && (set->type == LYXP_SET_NODE_SET)); |
1152 | 2.28k | assert(idx < set->used); |
1153 | | |
1154 | 2.28k | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
1155 | 2.28k | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
1156 | 2.28k | } |
1157 | 2.28k | set->val.nodes[idx].type = LYXP_NODE_NONE; |
1158 | 2.28k | } |
1159 | | |
1160 | | /** |
1161 | | * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes |
1162 | | * set into LYXP_SET_EMPTY. Context position aware. |
1163 | | * |
1164 | | * @param[in] set Set to consolidate. |
1165 | | */ |
1166 | | static void |
1167 | | set_remove_nodes_none(struct lyxp_set *set) |
1168 | 142 | { |
1169 | 142 | uint32_t i, orig_used, end = 0; |
1170 | 142 | int64_t start; |
1171 | | |
1172 | 142 | assert(set); |
1173 | | |
1174 | 142 | orig_used = set->used; |
1175 | 142 | set->used = 0; |
1176 | 333 | for (i = 0; i < orig_used; ) { |
1177 | 191 | start = -1; |
1178 | 2.34k | do { |
1179 | 2.34k | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
1180 | 60 | start = i; |
1181 | 2.28k | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
1182 | 50 | end = i; |
1183 | 50 | ++i; |
1184 | 50 | break; |
1185 | 50 | } |
1186 | | |
1187 | 2.29k | ++i; |
1188 | 2.29k | if (i == orig_used) { |
1189 | 141 | end = i; |
1190 | 141 | } |
1191 | 2.29k | } while (i < orig_used); |
1192 | | |
1193 | 191 | if (start > -1) { |
1194 | | /* move the whole chunk of valid nodes together */ |
1195 | 60 | if (set->used != (unsigned)start) { |
1196 | 0 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
1197 | 0 | } |
1198 | 60 | set->used += end - start; |
1199 | 60 | } |
1200 | 191 | } |
1201 | 142 | } |
1202 | | |
1203 | | /** |
1204 | | * @brief Check for duplicates in a node set. |
1205 | | * |
1206 | | * @param[in] set Set to check. |
1207 | | * @param[in] node Node to look for in @p set. |
1208 | | * @param[in] node_type Type of @p node. |
1209 | | * @param[in] skip_idx Index from @p set to skip. |
1210 | | * @return LY_ERR |
1211 | | */ |
1212 | | static LY_ERR |
1213 | | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
1214 | 327 | { |
1215 | 327 | uint32_t i; |
1216 | | |
1217 | 327 | if (set->ht && node) { |
1218 | 0 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
1219 | 0 | } |
1220 | | |
1221 | 327 | for (i = 0; i < set->used; ++i) { |
1222 | 0 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
1223 | 0 | continue; |
1224 | 0 | } |
1225 | | |
1226 | 0 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
1227 | 0 | return LY_EEXIST; |
1228 | 0 | } |
1229 | 0 | } |
1230 | | |
1231 | 327 | return LY_SUCCESS; |
1232 | 327 | } |
1233 | | |
1234 | | ly_bool |
1235 | | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
1236 | | uint32_t *index_p) |
1237 | 847 | { |
1238 | 847 | uint32_t i; |
1239 | | |
1240 | 1.87k | for (i = 0; i < set->used; ++i) { |
1241 | 1.02k | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
1242 | 0 | continue; |
1243 | 0 | } |
1244 | | |
1245 | 1.02k | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
1246 | 0 | if (index_p) { |
1247 | 0 | *index_p = i; |
1248 | 0 | } |
1249 | 0 | return 1; |
1250 | 0 | } |
1251 | 1.02k | } |
1252 | | |
1253 | 847 | return 0; |
1254 | 847 | } |
1255 | | |
1256 | | void |
1257 | | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
1258 | 0 | { |
1259 | 0 | uint32_t orig_used, i, j; |
1260 | |
|
1261 | 0 | assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET)); |
1262 | | |
1263 | 0 | if (!set2->used) { |
1264 | 0 | return; |
1265 | 0 | } |
1266 | | |
1267 | 0 | if (!set1->used) { |
1268 | | /* release hidden allocated data (lyxp_set.size) */ |
1269 | 0 | lyxp_set_free_content(set1); |
1270 | | /* direct copying of the entire structure */ |
1271 | 0 | memcpy(set1, set2, sizeof *set1); |
1272 | 0 | return; |
1273 | 0 | } |
1274 | | |
1275 | 0 | if (set1->used + set2->used > set1->size) { |
1276 | 0 | set1->size = set1->used + set2->used; |
1277 | 0 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
1278 | 0 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
1279 | 0 | } |
1280 | | |
1281 | 0 | orig_used = set1->used; |
1282 | |
|
1283 | 0 | for (i = 0; i < set2->used; ++i) { |
1284 | 0 | for (j = 0; j < orig_used; ++j) { |
1285 | | /* detect duplicities */ |
1286 | 0 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
1287 | 0 | break; |
1288 | 0 | } |
1289 | 0 | } |
1290 | |
|
1291 | 0 | if (j < orig_used) { |
1292 | | /* node is there, but update its status if needed */ |
1293 | 0 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
1294 | 0 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
1295 | 0 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
1296 | 0 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
1297 | 0 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
1298 | 0 | } |
1299 | 0 | } else { |
1300 | 0 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
1301 | 0 | ++set1->used; |
1302 | 0 | } |
1303 | 0 | } |
1304 | |
|
1305 | 0 | lyxp_set_free_content(set2); |
1306 | 0 | set2->type = LYXP_SET_SCNODE_SET; |
1307 | 0 | } |
1308 | | |
1309 | | /** |
1310 | | * @brief Insert a node into a set. Context position aware. |
1311 | | * |
1312 | | * @param[in] set Set to use. |
1313 | | * @param[in] node Node to insert to @p set. |
1314 | | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
1315 | | * @param[in] node_type Node type of @p node. |
1316 | | * @param[in] idx Index in @p set to insert into. |
1317 | | */ |
1318 | | static void |
1319 | | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
1320 | 10.5k | { |
1321 | 10.5k | assert(set && (set->type == LYXP_SET_NODE_SET)); |
1322 | | |
1323 | 10.5k | if (!set->size) { |
1324 | | /* first item */ |
1325 | 8.32k | if (idx) { |
1326 | | /* no real harm done, but it is a bug */ |
1327 | 0 | LOGINT(set->ctx); |
1328 | 0 | idx = 0; |
1329 | 0 | } |
1330 | 8.32k | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
1331 | 8.32k | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
1332 | 8.32k | set->type = LYXP_SET_NODE_SET; |
1333 | 8.32k | set->used = 0; |
1334 | 8.32k | set->size = LYXP_SET_SIZE_START; |
1335 | 8.32k | set->ctx_pos = 1; |
1336 | 8.32k | set->ctx_size = 1; |
1337 | 8.32k | set->ht = NULL; |
1338 | 8.32k | } else { |
1339 | | /* not an empty set */ |
1340 | 2.20k | if (set->used == set->size) { |
1341 | | |
1342 | | /* set is full */ |
1343 | 212 | set->val.nodes = ly_realloc(set->val.nodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.nodes); |
1344 | 212 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
1345 | 212 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
1346 | 212 | } |
1347 | | |
1348 | 2.20k | if (idx > set->used) { |
1349 | 0 | LOGINT(set->ctx); |
1350 | 0 | idx = set->used; |
1351 | 0 | } |
1352 | | |
1353 | | /* make space for the new node */ |
1354 | 2.20k | if (idx < set->used) { |
1355 | 0 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
1356 | 0 | } |
1357 | 2.20k | } |
1358 | | |
1359 | | /* finally assign the value */ |
1360 | 10.5k | set->val.nodes[idx].node = (struct lyd_node *)node; |
1361 | 10.5k | set->val.nodes[idx].type = node_type; |
1362 | 10.5k | set->val.nodes[idx].pos = pos; |
1363 | 10.5k | ++set->used; |
1364 | | |
1365 | | /* add into hash table */ |
1366 | 10.5k | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
1367 | 10.5k | } |
1368 | | |
1369 | | LY_ERR |
1370 | | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
1371 | | enum lyxp_axis axis, uint32_t *index_p) |
1372 | 847 | { |
1373 | 847 | uint32_t index; |
1374 | | |
1375 | 847 | assert(set->type == LYXP_SET_SCNODE_SET); |
1376 | | |
1377 | 847 | if (!set->size) { |
1378 | | /* first item */ |
1379 | 334 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
1380 | 334 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
1381 | 334 | set->type = LYXP_SET_SCNODE_SET; |
1382 | 334 | set->used = 0; |
1383 | 334 | set->size = LYXP_SET_SIZE_START; |
1384 | 334 | set->ctx_pos = 1; |
1385 | 334 | set->ctx_size = 1; |
1386 | 334 | set->ht = NULL; |
1387 | 334 | } |
1388 | | |
1389 | 847 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
1390 | | /* BUG if axes differ, this new one is thrown away */ |
1391 | 0 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
1392 | 847 | } else { |
1393 | 847 | if (set->used == set->size) { |
1394 | 0 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.scnodes); |
1395 | 0 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
1396 | 0 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
1397 | 0 | } |
1398 | | |
1399 | 847 | index = set->used; |
1400 | 847 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
1401 | 847 | set->val.scnodes[index].type = node_type; |
1402 | 847 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
1403 | 847 | set->val.scnodes[index].axis = axis; |
1404 | 847 | ++set->used; |
1405 | 847 | } |
1406 | | |
1407 | 847 | if (index_p) { |
1408 | 342 | *index_p = index; |
1409 | 342 | } |
1410 | | |
1411 | 847 | return LY_SUCCESS; |
1412 | 847 | } |
1413 | | |
1414 | | /** |
1415 | | * @brief Set all nodes with ctx 1 to a new unique context value. |
1416 | | * |
1417 | | * @param[in] set Set to modify. |
1418 | | * @return New context value. |
1419 | | */ |
1420 | | static int32_t |
1421 | | set_scnode_new_in_ctx(struct lyxp_set *set) |
1422 | 0 | { |
1423 | 0 | uint32_t i; |
1424 | 0 | int32_t ret_ctx; |
1425 | |
|
1426 | 0 | assert(set->type == LYXP_SET_SCNODE_SET); |
1427 | | |
1428 | 0 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
1429 | 0 | retry: |
1430 | 0 | for (i = 0; i < set->used; ++i) { |
1431 | 0 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
1432 | 0 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
1433 | 0 | goto retry; |
1434 | 0 | } |
1435 | 0 | } |
1436 | 0 | for (i = 0; i < set->used; ++i) { |
1437 | 0 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
1438 | 0 | set->val.scnodes[i].in_ctx = ret_ctx; |
1439 | 0 | } |
1440 | 0 | } |
1441 | |
|
1442 | 0 | return ret_ctx; |
1443 | 0 | } |
1444 | | |
1445 | | /** |
1446 | | * @brief Get unique @p node position in the data. |
1447 | | * |
1448 | | * @param[in] node Node to find. |
1449 | | * @param[in] node_type Node type of @p node. |
1450 | | * @param[in] root Root node. |
1451 | | * @param[in] root_type Type of the XPath @p root node. |
1452 | | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
1453 | | * be used to increase efficiency and start the DFS from this node. |
1454 | | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
1455 | | * @return Node position. |
1456 | | */ |
1457 | | static uint32_t |
1458 | | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
1459 | | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
1460 | 2.32k | { |
1461 | 2.32k | const struct lyd_node *elem = NULL, *top_sibling; |
1462 | 2.32k | uint32_t pos = 1; |
1463 | 2.32k | ly_bool found = 0; |
1464 | | |
1465 | 2.32k | assert(prev && prev_pos && !root->prev->next); |
1466 | | |
1467 | 2.32k | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
1468 | 0 | return 0; |
1469 | 0 | } |
1470 | | |
1471 | 2.32k | if (*prev) { |
1472 | | /* start from the previous element instead from the root */ |
1473 | 2.20k | pos = *prev_pos; |
1474 | 4.40k | for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {} |
1475 | 2.20k | goto dfs_search; |
1476 | 2.20k | } |
1477 | | |
1478 | 236 | LY_LIST_FOR(root, top_sibling) { |
1479 | 2.69k | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
1480 | 4.89k | dfs_search: |
1481 | 4.89k | LYD_TREE_DFS_continue = 0; |
1482 | | |
1483 | 4.89k | if (*prev && !elem) { |
1484 | | /* resume previous DFS */ |
1485 | 2.20k | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
1486 | 2.20k | LYD_TREE_DFS_continue = 0; |
1487 | 2.20k | } |
1488 | | |
1489 | 4.89k | if ((root_type == LYXP_NODE_ROOT_CONFIG) && elem->schema && (elem->schema->flags & LYS_CONFIG_R)) { |
1490 | | /* skip */ |
1491 | 0 | LYD_TREE_DFS_continue = 1; |
1492 | 4.89k | } else { |
1493 | 4.89k | if (elem == node) { |
1494 | 2.32k | found = 1; |
1495 | 2.32k | break; |
1496 | 2.32k | } |
1497 | 2.57k | ++pos; |
1498 | 2.57k | } |
1499 | | |
1500 | 2.57k | LYD_TREE_DFS_END(top_sibling, elem); |
1501 | 2.44k | } |
1502 | | |
1503 | | /* node found */ |
1504 | 2.44k | if (found) { |
1505 | 2.32k | break; |
1506 | 2.32k | } |
1507 | 2.44k | } |
1508 | | |
1509 | 2.32k | if (!found) { |
1510 | 0 | if (!(*prev)) { |
1511 | | /* we went from root and failed to find it, cannot be */ |
1512 | 0 | LOGINT(LYD_CTX(node)); |
1513 | 0 | return 0; |
1514 | 0 | } else { |
1515 | | /* start the search again from the beginning */ |
1516 | 0 | *prev = root; |
1517 | |
|
1518 | 0 | top_sibling = root; |
1519 | 0 | pos = 1; |
1520 | 0 | goto dfs_search; |
1521 | 0 | } |
1522 | 0 | } |
1523 | | |
1524 | | /* remember the last found node for next time */ |
1525 | 2.32k | *prev = node; |
1526 | 2.32k | *prev_pos = pos; |
1527 | | |
1528 | 2.32k | return pos; |
1529 | 2.32k | } |
1530 | | |
1531 | | /** |
1532 | | * @brief Assign (fill) missing node positions. |
1533 | | * |
1534 | | * @param[in] set Set to fill positions in. |
1535 | | * @param[in] root Context root node. |
1536 | | * @param[in] root_type Context root type. |
1537 | | * @return LY_ERR |
1538 | | */ |
1539 | | static LY_ERR |
1540 | | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
1541 | 236 | { |
1542 | 236 | const struct lyd_node *prev = NULL, *tmp_node; |
1543 | 236 | uint32_t i, tmp_pos = 0; |
1544 | | |
1545 | 4.88k | for (i = 0; i < set->used; ++i) { |
1546 | 4.64k | if (!set->val.nodes[i].pos) { |
1547 | 2.32k | tmp_node = NULL; |
1548 | 2.32k | switch (set->val.nodes[i].type) { |
1549 | 0 | case LYXP_NODE_META: |
1550 | 0 | tmp_node = set->val.meta[i].meta->parent; |
1551 | 0 | if (!tmp_node) { |
1552 | 0 | LOGINT_RET(root->schema->module->ctx); |
1553 | 0 | } |
1554 | | /* fall through */ |
1555 | 2.32k | case LYXP_NODE_ELEM: |
1556 | 2.32k | case LYXP_NODE_TEXT: |
1557 | 2.32k | if (!tmp_node) { |
1558 | 2.32k | tmp_node = set->val.nodes[i].node; |
1559 | 2.32k | } |
1560 | 2.32k | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
1561 | 2.32k | break; |
1562 | 0 | default: |
1563 | | /* all roots have position 0 */ |
1564 | 0 | break; |
1565 | 2.32k | } |
1566 | 2.32k | } |
1567 | 4.64k | } |
1568 | | |
1569 | 236 | return LY_SUCCESS; |
1570 | 236 | } |
1571 | | |
1572 | | /** |
1573 | | * @brief Get unique @p meta position in the parent metadata. |
1574 | | * |
1575 | | * @param[in] meta Metadata to use. |
1576 | | * @return Metadata position. |
1577 | | */ |
1578 | | static uint32_t |
1579 | | get_meta_pos(struct lyd_meta *meta) |
1580 | 0 | { |
1581 | 0 | uint32_t pos = 0; |
1582 | 0 | struct lyd_meta *meta2; |
1583 | |
|
1584 | 0 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
1585 | 0 | ++pos; |
1586 | 0 | } |
1587 | |
|
1588 | 0 | assert(meta2); |
1589 | 0 | return pos; |
1590 | 0 | } |
1591 | | |
1592 | | /** |
1593 | | * @brief Compare 2 nodes in respect to XPath document order. |
1594 | | * |
1595 | | * @param[in] item1 1st node. |
1596 | | * @param[in] item2 2nd node. |
1597 | | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
1598 | | */ |
1599 | | static int |
1600 | | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
1601 | 4.40k | { |
1602 | 4.40k | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
1603 | | |
1604 | 4.40k | if (item1->pos < item2->pos) { |
1605 | 2.26k | return -1; |
1606 | 2.26k | } |
1607 | | |
1608 | 2.14k | if (item1->pos > item2->pos) { |
1609 | 2.14k | return 1; |
1610 | 2.14k | } |
1611 | | |
1612 | | /* node positions are equal, the fun case */ |
1613 | | |
1614 | | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
1615 | | /* special case since text nodes are actually saved as their parents */ |
1616 | 0 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
1617 | 0 | if (item1->type == LYXP_NODE_ELEM) { |
1618 | 0 | assert(item2->type == LYXP_NODE_TEXT); |
1619 | 0 | return -1; |
1620 | 0 | } else { |
1621 | 0 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
1622 | 0 | return 1; |
1623 | 0 | } |
1624 | 0 | } |
1625 | | |
1626 | | /* we need meta positions now */ |
1627 | 0 | if (item1->type == LYXP_NODE_META) { |
1628 | 0 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
1629 | 0 | } |
1630 | 0 | if (item2->type == LYXP_NODE_META) { |
1631 | 0 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
1632 | 0 | } |
1633 | | |
1634 | | /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */ |
1635 | | /* check for duplicates */ |
1636 | 0 | if (item1->node == item2->node) { |
1637 | 0 | assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2))); |
1638 | 0 | return 0; |
1639 | 0 | } |
1640 | | |
1641 | | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
1642 | | /* elem is always first, 2nd node is after it */ |
1643 | 0 | if (item1->type == LYXP_NODE_ELEM) { |
1644 | 0 | assert(item2->type != LYXP_NODE_ELEM); |
1645 | 0 | return -1; |
1646 | 0 | } |
1647 | | |
1648 | | /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */ |
1649 | | /* 2nd is before 1st */ |
1650 | 0 | if (((item1->type == LYXP_NODE_TEXT) && |
1651 | 0 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
1652 | 0 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
1653 | 0 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
1654 | 0 | (meta_pos1 > meta_pos2))) { |
1655 | 0 | return 1; |
1656 | 0 | } |
1657 | | |
1658 | | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
1659 | | /* 2nd is after 1st */ |
1660 | 0 | return -1; |
1661 | 0 | } |
1662 | | |
1663 | | /** |
1664 | | * @brief Set cast for comparisons. |
1665 | | * |
1666 | | * @param[in,out] trg Target set to cast source into. |
1667 | | * @param[in] src Source set. |
1668 | | * @param[in] type Target set type. |
1669 | | * @param[in] src_idx Source set node index. |
1670 | | * @return LY_SUCCESS on success. |
1671 | | * @return LY_ERR value on error. |
1672 | | */ |
1673 | | static LY_ERR |
1674 | | set_comp_cast(struct lyxp_set *trg, const struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
1675 | 2.34k | { |
1676 | 2.34k | assert(src->type == LYXP_SET_NODE_SET); |
1677 | | |
1678 | 2.34k | set_init(trg, src); |
1679 | | |
1680 | | /* insert node into target set */ |
1681 | 2.34k | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
1682 | | |
1683 | | /* cast target set appropriately */ |
1684 | 2.34k | return lyxp_set_cast(trg, type); |
1685 | 2.34k | } |
1686 | | |
1687 | | /** |
1688 | | * @brief Set content canonization for comparisons. |
1689 | | * |
1690 | | * @param[in,out] set Set to canonize. |
1691 | | * @param[in] xp_node Source XPath node/meta to use for canonization. |
1692 | | * @return LY_SUCCESS on success. |
1693 | | * @return LY_ERR value on error. |
1694 | | */ |
1695 | | static LY_ERR |
1696 | | set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node) |
1697 | 2.34k | { |
1698 | 2.34k | const struct lysc_type *type = NULL; |
1699 | 2.34k | struct lyd_value val; |
1700 | 2.34k | struct ly_err_item *err = NULL; |
1701 | 2.34k | LY_ERR r; |
1702 | | |
1703 | | /* is there anything to canonize even? */ |
1704 | 2.34k | if (set->type == LYXP_SET_STRING) { |
1705 | | /* do we have a type to use for canonization? */ |
1706 | 2.34k | if ((xp_node->type == LYXP_NODE_ELEM) && xp_node->node->schema && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
1707 | 2.34k | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
1708 | 2.34k | } else if (xp_node->type == LYXP_NODE_META) { |
1709 | 0 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
1710 | 0 | } |
1711 | 2.34k | } |
1712 | 2.34k | if (!type) { |
1713 | | /* no canonization needed/possible */ |
1714 | 0 | return LY_SUCCESS; |
1715 | 0 | } |
1716 | | |
1717 | | /* check for built-in types without required canonization */ |
1718 | 2.34k | if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) { |
1719 | | /* string */ |
1720 | 2.34k | return LY_SUCCESS; |
1721 | 2.34k | } |
1722 | 0 | if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) { |
1723 | | /* boolean */ |
1724 | 0 | return LY_SUCCESS; |
1725 | 0 | } |
1726 | 0 | if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) { |
1727 | | /* enumeration */ |
1728 | 0 | return LY_SUCCESS; |
1729 | 0 | } |
1730 | | |
1731 | | /* print canonized string, ignore errors, the value may not satisfy schema constraints */ |
1732 | 0 | r = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data, |
1733 | 0 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
1734 | 0 | ly_err_free(err); |
1735 | 0 | if (r && (r != LY_EINCOMPLETE)) { |
1736 | | /* invalid value, function store automaticaly dealloc value when fail */ |
1737 | 0 | return LY_SUCCESS; |
1738 | 0 | } |
1739 | | |
1740 | | /* use the canonized string value */ |
1741 | 0 | free(set->val.str); |
1742 | 0 | set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val)); |
1743 | 0 | type->plugin->free(set->ctx, &val); |
1744 | 0 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
1745 | |
|
1746 | 0 | return LY_SUCCESS; |
1747 | 0 | } |
1748 | | |
1749 | | /** |
1750 | | * @brief Bubble sort @p set into XPath document order. |
1751 | | * Context position aware. |
1752 | | * |
1753 | | * @param[in] set Set to sort. |
1754 | | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
1755 | | */ |
1756 | | static int |
1757 | | set_sort(struct lyxp_set *set) |
1758 | 5.91k | { |
1759 | 5.91k | uint32_t i, j; |
1760 | 5.91k | int ret = 0, cmp; |
1761 | 5.91k | ly_bool inverted, change; |
1762 | 5.91k | const struct lyd_node *root; |
1763 | 5.91k | struct lyxp_set_node item; |
1764 | 5.91k | struct lyxp_set_hash_node hnode; |
1765 | 5.91k | uint64_t hash; |
1766 | | |
1767 | 5.91k | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
1768 | 5.68k | return 0; |
1769 | 5.68k | } |
1770 | | |
1771 | | /* find first top-level node to be used as anchor for positions */ |
1772 | 236 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
1773 | 236 | for ( ; root->prev->next; root = root->prev) {} |
1774 | | |
1775 | | /* fill positions */ |
1776 | 236 | if (set_assign_pos(set, root, set->root_type)) { |
1777 | 0 | return -1; |
1778 | 0 | } |
1779 | | |
1780 | 236 | #ifndef NDEBUG |
1781 | 236 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
1782 | 236 | print_set_debug(set); |
1783 | 236 | #endif |
1784 | | |
1785 | 236 | for (i = 0; i < set->used; ++i) { |
1786 | 236 | inverted = 0; |
1787 | 236 | change = 0; |
1788 | | |
1789 | 4.64k | for (j = 1; j < set->used - i; ++j) { |
1790 | | /* compare node positions */ |
1791 | 4.40k | if (inverted) { |
1792 | 2.14k | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
1793 | 2.26k | } else { |
1794 | 2.26k | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
1795 | 2.26k | } |
1796 | | |
1797 | | /* swap if needed */ |
1798 | 4.40k | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
1799 | 0 | change = 1; |
1800 | |
|
1801 | 0 | item = set->val.nodes[j - 1]; |
1802 | 0 | set->val.nodes[j - 1] = set->val.nodes[j]; |
1803 | 0 | set->val.nodes[j] = item; |
1804 | 4.40k | } else { |
1805 | | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
1806 | 4.40k | inverted = !inverted; |
1807 | 4.40k | } |
1808 | 4.40k | } |
1809 | | |
1810 | 236 | ++ret; |
1811 | | |
1812 | 236 | if (!change) { |
1813 | 236 | break; |
1814 | 236 | } |
1815 | 236 | } |
1816 | | |
1817 | 236 | #ifndef NDEBUG |
1818 | 236 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
1819 | 236 | print_set_debug(set); |
1820 | 236 | #endif |
1821 | | |
1822 | | /* check node hashes */ |
1823 | 236 | if (set->used >= LYD_HT_MIN_ITEMS) { |
1824 | 160 | assert(set->ht); |
1825 | 4.60k | for (i = 0; i < set->used; ++i) { |
1826 | 4.44k | hnode.node = set->val.nodes[i].node; |
1827 | 4.44k | hnode.type = set->val.nodes[i].type; |
1828 | | |
1829 | 4.44k | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
1830 | 4.44k | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
1831 | 4.44k | hash = lyht_hash_multi(hash, NULL, 0); |
1832 | | |
1833 | 4.44k | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
1834 | 4.44k | } |
1835 | 160 | } |
1836 | | |
1837 | 236 | return ret - 1; |
1838 | 236 | } |
1839 | | |
1840 | | /** |
1841 | | * @brief Merge 2 sorted sets into one. |
1842 | | * |
1843 | | * @param[in,out] trg Set to merge into. Duplicates are removed. |
1844 | | * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success. |
1845 | | * @return LY_ERR |
1846 | | */ |
1847 | | static LY_ERR |
1848 | | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
1849 | 0 | { |
1850 | 0 | uint32_t i, j, k, count, dup_count; |
1851 | 0 | int cmp; |
1852 | 0 | const struct lyd_node *root; |
1853 | |
|
1854 | 0 | if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) { |
1855 | 0 | return LY_EINVAL; |
1856 | 0 | } |
1857 | | |
1858 | 0 | if (!src->used) { |
1859 | 0 | return LY_SUCCESS; |
1860 | 0 | } else if (!trg->used) { |
1861 | 0 | set_fill_set(trg, src); |
1862 | 0 | lyxp_set_free_content(src); |
1863 | 0 | return LY_SUCCESS; |
1864 | 0 | } |
1865 | | |
1866 | | /* find first top-level node to be used as anchor for positions */ |
1867 | 0 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
1868 | 0 | for ( ; root->prev->next; root = root->prev) {} |
1869 | | |
1870 | | /* fill positions */ |
1871 | 0 | if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) { |
1872 | 0 | return LY_EINT; |
1873 | 0 | } |
1874 | | |
1875 | 0 | #ifndef NDEBUG |
1876 | 0 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
1877 | 0 | print_set_debug(trg); |
1878 | 0 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
1879 | 0 | print_set_debug(src); |
1880 | 0 | #endif |
1881 | | |
1882 | | /* make memory for the merge (duplicates are not detected yet, so space |
1883 | | * will likely be wasted on them, too bad) */ |
1884 | 0 | if (trg->size - trg->used < src->used) { |
1885 | 0 | trg->size = trg->used + src->used; |
1886 | |
|
1887 | 0 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
1888 | 0 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
1889 | 0 | } |
1890 | | |
1891 | 0 | i = 0; |
1892 | 0 | j = 0; |
1893 | 0 | count = 0; |
1894 | 0 | dup_count = 0; |
1895 | 0 | do { |
1896 | 0 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
1897 | 0 | if (!cmp) { |
1898 | 0 | if (!count) { |
1899 | | /* duplicate, just skip it */ |
1900 | 0 | ++i; |
1901 | 0 | ++j; |
1902 | 0 | } else { |
1903 | | /* we are copying something already, so let's copy the duplicate too, |
1904 | | * we are hoping that afterwards there are some more nodes to |
1905 | | * copy and this way we can copy them all together */ |
1906 | 0 | ++count; |
1907 | 0 | ++dup_count; |
1908 | 0 | ++i; |
1909 | 0 | ++j; |
1910 | 0 | } |
1911 | 0 | } else if (cmp < 0) { |
1912 | | /* inserting src node into trg, just remember it for now */ |
1913 | 0 | ++count; |
1914 | 0 | ++i; |
1915 | | |
1916 | | /* insert the hash now */ |
1917 | 0 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
1918 | 0 | } else if (count) { |
1919 | 0 | copy_nodes: |
1920 | | /* time to actually copy the nodes, we have found the largest block of nodes */ |
1921 | 0 | memmove(&trg->val.nodes[j + (count - dup_count)], |
1922 | 0 | &trg->val.nodes[j], |
1923 | 0 | (trg->used - j) * sizeof *trg->val.nodes); |
1924 | 0 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
1925 | |
|
1926 | 0 | trg->used += count - dup_count; |
1927 | | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
1928 | 0 | j += count - dup_count; |
1929 | |
|
1930 | 0 | count = 0; |
1931 | 0 | dup_count = 0; |
1932 | 0 | } else { |
1933 | 0 | ++j; |
1934 | 0 | } |
1935 | 0 | } while ((i < src->used) && (j < trg->used)); |
1936 | |
|
1937 | 0 | if ((i < src->used) || count) { |
1938 | | /* insert all the hashes first */ |
1939 | 0 | for (k = i; k < src->used; ++k) { |
1940 | 0 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
1941 | 0 | } |
1942 | | |
1943 | | /* loop ended, but we need to copy something at trg end */ |
1944 | 0 | count += src->used - i; |
1945 | 0 | i = src->used; |
1946 | 0 | goto copy_nodes; |
1947 | 0 | } |
1948 | | |
1949 | | /* we are inserting hashes before the actual node insert, which causes |
1950 | | * situations when there were initially not enough items for a hash table, |
1951 | | * but even after some were inserted, hash table was not created (during |
1952 | | * insertion the number of items is not updated yet) */ |
1953 | 0 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
1954 | 0 | set_insert_node_hash(trg, NULL, 0); |
1955 | 0 | } |
1956 | |
|
1957 | 0 | #ifndef NDEBUG |
1958 | 0 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
1959 | 0 | print_set_debug(trg); |
1960 | 0 | #endif |
1961 | |
|
1962 | 0 | lyxp_set_free_content(src); |
1963 | 0 | return LY_SUCCESS; |
1964 | 0 | } |
1965 | | |
1966 | | LY_ERR |
1967 | | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok) |
1968 | 1.81M | { |
1969 | 1.81M | if (exp->used == tok_idx) { |
1970 | 456k | if (ctx) { |
1971 | 155 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
1972 | 155 | } |
1973 | 456k | return LY_EINCOMPLETE; |
1974 | 456k | } |
1975 | | |
1976 | 1.36M | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
1977 | 296k | if (ctx) { |
1978 | 457 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
1979 | 457 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
1980 | 457 | } |
1981 | 296k | return LY_ENOT; |
1982 | 296k | } |
1983 | | |
1984 | 1.06M | return LY_SUCCESS; |
1985 | 1.36M | } |
1986 | | |
1987 | | LY_ERR |
1988 | | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok) |
1989 | 1.32M | { |
1990 | 1.32M | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
1991 | | |
1992 | | /* skip the token */ |
1993 | 585k | ++(*tok_idx); |
1994 | | |
1995 | 585k | return LY_SUCCESS; |
1996 | 1.32M | } |
1997 | | |
1998 | | /* just like lyxp_check_token() but tests for 2 tokens */ |
1999 | | static LY_ERR |
2000 | | exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok1, |
2001 | | enum lyxp_token want_tok2) |
2002 | 9.81k | { |
2003 | 9.81k | if (exp->used == tok_idx) { |
2004 | 1.23k | if (ctx) { |
2005 | 2 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
2006 | 2 | } |
2007 | 1.23k | return LY_EINCOMPLETE; |
2008 | 1.23k | } |
2009 | | |
2010 | 8.57k | if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) { |
2011 | 5.96k | if (ctx) { |
2012 | 10 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
2013 | 10 | &exp->expr[exp->tok_pos[tok_idx]]); |
2014 | 10 | } |
2015 | 5.96k | return LY_ENOT; |
2016 | 5.96k | } |
2017 | | |
2018 | 2.60k | return LY_SUCCESS; |
2019 | 8.57k | } |
2020 | | |
2021 | | LY_ERR |
2022 | | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok1, |
2023 | | enum lyxp_token want_tok2) |
2024 | 507 | { |
2025 | 507 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
2026 | | |
2027 | | /* skip the token */ |
2028 | 495 | ++(*tok_idx); |
2029 | | |
2030 | 495 | return LY_SUCCESS; |
2031 | 507 | } |
2032 | | |
2033 | | /** |
2034 | | * @brief Stack operation push on the repeat array. |
2035 | | * |
2036 | | * @param[in] exp Expression to use. |
2037 | | * @param[in] tok_idx Position in the expresion @p exp. |
2038 | | * @param[in] repeat_expr_type Repeated expression type, this value is pushed. |
2039 | | */ |
2040 | | static void |
2041 | | exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type) |
2042 | 372 | { |
2043 | 372 | uint32_t i; |
2044 | | |
2045 | 372 | if (exp->repeat[tok_idx]) { |
2046 | 0 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
2047 | 0 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
2048 | 0 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
2049 | 0 | exp->repeat[tok_idx][i] = repeat_expr_type; |
2050 | 0 | exp->repeat[tok_idx][i + 1] = 0; |
2051 | 372 | } else { |
2052 | 372 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
2053 | 372 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
2054 | 372 | exp->repeat[tok_idx][0] = repeat_expr_type; |
2055 | 372 | } |
2056 | 372 | } |
2057 | | |
2058 | | /** |
2059 | | * @brief Reparse Predicate. Logs directly on error. |
2060 | | * |
2061 | | * [7] Predicate ::= '[' Expr ']' |
2062 | | * |
2063 | | * @param[in] ctx Context for logging. |
2064 | | * @param[in] exp Parsed XPath expression. |
2065 | | * @param[in] tok_idx Position in the expression @p exp. |
2066 | | * @param[in] depth Current number of nested expressions. |
2067 | | * @return LY_ERR |
2068 | | */ |
2069 | | static LY_ERR |
2070 | | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2071 | 372 | { |
2072 | 372 | LY_ERR rc; |
2073 | | |
2074 | 372 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
2075 | 372 | LY_CHECK_RET(rc); |
2076 | 372 | ++(*tok_idx); |
2077 | | |
2078 | 372 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
2079 | 372 | LY_CHECK_RET(rc); |
2080 | | |
2081 | 372 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
2082 | 372 | LY_CHECK_RET(rc); |
2083 | 372 | ++(*tok_idx); |
2084 | | |
2085 | 372 | return LY_SUCCESS; |
2086 | 372 | } |
2087 | | |
2088 | | /** |
2089 | | * @brief Reparse RelativeLocationPath. Logs directly on error. |
2090 | | * |
2091 | | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
2092 | | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
2093 | | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
2094 | | * |
2095 | | * @param[in] ctx Context for logging. |
2096 | | * @param[in] exp Parsed XPath expression. |
2097 | | * @param[in] tok_idx Position in the expression \p exp. |
2098 | | * @param[in] depth Current number of nested expressions. |
2099 | | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
2100 | | */ |
2101 | | static LY_ERR |
2102 | | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2103 | 744 | { |
2104 | 744 | LY_ERR rc; |
2105 | | |
2106 | 744 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
2107 | 744 | LY_CHECK_RET(rc); |
2108 | | |
2109 | 744 | goto step; |
2110 | 513 | do { |
2111 | | /* '/' or '//' */ |
2112 | 513 | ++(*tok_idx); |
2113 | | |
2114 | 513 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
2115 | 513 | LY_CHECK_RET(rc); |
2116 | 1.25k | step: |
2117 | | /* Step */ |
2118 | 1.25k | switch (exp->tokens[*tok_idx]) { |
2119 | 201 | case LYXP_TOKEN_DOT: |
2120 | 201 | ++(*tok_idx); |
2121 | 201 | break; |
2122 | | |
2123 | 342 | case LYXP_TOKEN_DDOT: |
2124 | 342 | ++(*tok_idx); |
2125 | 342 | break; |
2126 | | |
2127 | 0 | case LYXP_TOKEN_AXISNAME: |
2128 | 0 | ++(*tok_idx); |
2129 | |
|
2130 | 0 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
2131 | 0 | LY_CHECK_RET(rc); |
2132 | | |
2133 | | /* fall through */ |
2134 | 0 | case LYXP_TOKEN_AT: |
2135 | 0 | ++(*tok_idx); |
2136 | |
|
2137 | 0 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
2138 | 0 | LY_CHECK_RET(rc); |
2139 | 0 | if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) { |
2140 | 0 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
2141 | 0 | return LY_EVALID; |
2142 | 0 | } |
2143 | 0 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
2144 | 0 | goto reparse_nodetype; |
2145 | 0 | } |
2146 | | /* fall through */ |
2147 | 714 | case LYXP_TOKEN_NAMETEST: |
2148 | 714 | ++(*tok_idx); |
2149 | 714 | goto reparse_predicate; |
2150 | | |
2151 | 0 | case LYXP_TOKEN_NODETYPE: |
2152 | 0 | reparse_nodetype: |
2153 | 0 | ++(*tok_idx); |
2154 | | |
2155 | | /* '(' */ |
2156 | 0 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
2157 | 0 | LY_CHECK_RET(rc); |
2158 | 0 | ++(*tok_idx); |
2159 | | |
2160 | | /* ')' */ |
2161 | 0 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
2162 | 0 | LY_CHECK_RET(rc); |
2163 | 0 | ++(*tok_idx); |
2164 | |
|
2165 | 714 | reparse_predicate: |
2166 | | /* Predicate* */ |
2167 | 1.08k | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
2168 | 372 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
2169 | 372 | LY_CHECK_RET(rc); |
2170 | 372 | } |
2171 | 714 | break; |
2172 | 714 | default: |
2173 | 0 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
2174 | 0 | return LY_EVALID; |
2175 | 1.25k | } |
2176 | 1.25k | } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
2177 | | |
2178 | 744 | return LY_SUCCESS; |
2179 | 0 | } |
2180 | | |
2181 | | /** |
2182 | | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
2183 | | * |
2184 | | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
2185 | | * |
2186 | | * @param[in] ctx Context for logging. |
2187 | | * @param[in] exp Parsed XPath expression. |
2188 | | * @param[in] tok_idx Position in the expression \p exp. |
2189 | | * @param[in] depth Current number of nested expressions. |
2190 | | * @return LY_ERR |
2191 | | */ |
2192 | | static LY_ERR |
2193 | | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2194 | 30 | { |
2195 | 30 | LY_ERR rc; |
2196 | | |
2197 | 30 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
2198 | | |
2199 | | /* '/' RelativeLocationPath? */ |
2200 | 30 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
2201 | | /* '/' */ |
2202 | 30 | ++(*tok_idx); |
2203 | | |
2204 | 30 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
2205 | 0 | return LY_SUCCESS; |
2206 | 0 | } |
2207 | 30 | switch (exp->tokens[*tok_idx]) { |
2208 | 0 | case LYXP_TOKEN_DOT: |
2209 | 0 | case LYXP_TOKEN_DDOT: |
2210 | 0 | case LYXP_TOKEN_AXISNAME: |
2211 | 0 | case LYXP_TOKEN_AT: |
2212 | 30 | case LYXP_TOKEN_NAMETEST: |
2213 | 30 | case LYXP_TOKEN_NODETYPE: |
2214 | 30 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
2215 | 30 | LY_CHECK_RET(rc); |
2216 | | /* fall through */ |
2217 | 30 | default: |
2218 | 30 | break; |
2219 | 30 | } |
2220 | | |
2221 | 30 | } else { |
2222 | | /* '//' RelativeLocationPath */ |
2223 | 0 | ++(*tok_idx); |
2224 | |
|
2225 | 0 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
2226 | 0 | LY_CHECK_RET(rc); |
2227 | 0 | } |
2228 | | |
2229 | 30 | return LY_SUCCESS; |
2230 | 30 | } |
2231 | | |
2232 | | /** |
2233 | | * @brief Reparse FunctionCall. Logs directly on error. |
2234 | | * |
2235 | | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
2236 | | * |
2237 | | * @param[in] ctx Context for logging. |
2238 | | * @param[in] exp Parsed XPath expression. |
2239 | | * @param[in] tok_idx Position in the expression @p exp. |
2240 | | * @param[in] depth Current number of nested expressions. |
2241 | | * @return LY_ERR |
2242 | | */ |
2243 | | static LY_ERR |
2244 | | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2245 | 171 | { |
2246 | 171 | int8_t min_arg_count = -1; |
2247 | 171 | uint32_t arg_count, max_arg_count = 0, func_tok_idx; |
2248 | 171 | LY_ERR rc; |
2249 | | |
2250 | 171 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
2251 | 171 | LY_CHECK_RET(rc); |
2252 | 171 | func_tok_idx = *tok_idx; |
2253 | 171 | switch (exp->tok_len[*tok_idx]) { |
2254 | 0 | case 3: |
2255 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
2256 | 0 | min_arg_count = 1; |
2257 | 0 | max_arg_count = 1; |
2258 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
2259 | 0 | min_arg_count = 1; |
2260 | 0 | max_arg_count = 1; |
2261 | 0 | } |
2262 | 0 | break; |
2263 | 0 | case 4: |
2264 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
2265 | 0 | min_arg_count = 1; |
2266 | 0 | max_arg_count = 1; |
2267 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
2268 | 0 | min_arg_count = 0; |
2269 | 0 | max_arg_count = 0; |
2270 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
2271 | 0 | min_arg_count = 0; |
2272 | 0 | max_arg_count = 1; |
2273 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
2274 | 0 | min_arg_count = 0; |
2275 | 0 | max_arg_count = 0; |
2276 | 0 | } |
2277 | 0 | break; |
2278 | 0 | case 5: |
2279 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
2280 | 0 | min_arg_count = 1; |
2281 | 0 | max_arg_count = 1; |
2282 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
2283 | 0 | min_arg_count = 0; |
2284 | 0 | max_arg_count = 0; |
2285 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
2286 | 0 | min_arg_count = 1; |
2287 | 0 | max_arg_count = 1; |
2288 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
2289 | 0 | min_arg_count = 1; |
2290 | 0 | max_arg_count = 1; |
2291 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
2292 | 0 | min_arg_count = 1; |
2293 | 0 | max_arg_count = 1; |
2294 | 0 | } |
2295 | 0 | break; |
2296 | 0 | case 6: |
2297 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
2298 | 0 | min_arg_count = 2; |
2299 | 0 | max_arg_count = UINT32_MAX; |
2300 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
2301 | 0 | min_arg_count = 0; |
2302 | 0 | max_arg_count = 1; |
2303 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
2304 | 0 | min_arg_count = 0; |
2305 | 0 | max_arg_count = 1; |
2306 | 0 | } |
2307 | 0 | break; |
2308 | 171 | case 7: |
2309 | 171 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
2310 | 0 | min_arg_count = 1; |
2311 | 0 | max_arg_count = 1; |
2312 | 171 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
2313 | 0 | min_arg_count = 1; |
2314 | 0 | max_arg_count = 1; |
2315 | 171 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
2316 | 171 | min_arg_count = 0; |
2317 | 171 | max_arg_count = 0; |
2318 | 171 | } |
2319 | 171 | break; |
2320 | 0 | case 8: |
2321 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
2322 | 0 | min_arg_count = 2; |
2323 | 0 | max_arg_count = 2; |
2324 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
2325 | 0 | min_arg_count = 0; |
2326 | 0 | max_arg_count = 0; |
2327 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
2328 | 0 | min_arg_count = 2; |
2329 | 0 | max_arg_count = 2; |
2330 | 0 | } |
2331 | 0 | break; |
2332 | 0 | case 9: |
2333 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
2334 | 0 | min_arg_count = 2; |
2335 | 0 | max_arg_count = 3; |
2336 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
2337 | 0 | min_arg_count = 3; |
2338 | 0 | max_arg_count = 3; |
2339 | 0 | } |
2340 | 0 | break; |
2341 | 0 | case 10: |
2342 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
2343 | 0 | min_arg_count = 0; |
2344 | 0 | max_arg_count = 1; |
2345 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
2346 | 0 | min_arg_count = 1; |
2347 | 0 | max_arg_count = 1; |
2348 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
2349 | 0 | min_arg_count = 2; |
2350 | 0 | max_arg_count = 2; |
2351 | 0 | } |
2352 | 0 | break; |
2353 | 0 | case 11: |
2354 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
2355 | 0 | min_arg_count = 2; |
2356 | 0 | max_arg_count = 2; |
2357 | 0 | } |
2358 | 0 | break; |
2359 | 0 | case 12: |
2360 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
2361 | 0 | min_arg_count = 2; |
2362 | 0 | max_arg_count = 2; |
2363 | 0 | } |
2364 | 0 | break; |
2365 | 0 | case 13: |
2366 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
2367 | 0 | min_arg_count = 0; |
2368 | 0 | max_arg_count = 1; |
2369 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
2370 | 0 | min_arg_count = 0; |
2371 | 0 | max_arg_count = 1; |
2372 | 0 | } |
2373 | 0 | break; |
2374 | 0 | case 15: |
2375 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
2376 | 0 | min_arg_count = 0; |
2377 | 0 | max_arg_count = 1; |
2378 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
2379 | 0 | min_arg_count = 2; |
2380 | 0 | max_arg_count = 2; |
2381 | 0 | } |
2382 | 0 | break; |
2383 | 0 | case 16: |
2384 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
2385 | 0 | min_arg_count = 2; |
2386 | 0 | max_arg_count = 2; |
2387 | 0 | } |
2388 | 0 | break; |
2389 | 0 | case 20: |
2390 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
2391 | 0 | min_arg_count = 2; |
2392 | 0 | max_arg_count = 2; |
2393 | 0 | } |
2394 | 0 | break; |
2395 | 171 | } |
2396 | 171 | if (min_arg_count == -1) { |
2397 | 0 | LOGVAL(ctx, LY_VCODE_XP_INFUNC, (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
2398 | 0 | return LY_EINVAL; |
2399 | 0 | } |
2400 | 171 | ++(*tok_idx); |
2401 | | |
2402 | | /* '(' */ |
2403 | 171 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
2404 | 171 | LY_CHECK_RET(rc); |
2405 | 171 | ++(*tok_idx); |
2406 | | |
2407 | | /* ( Expr ( ',' Expr )* )? */ |
2408 | 171 | arg_count = 0; |
2409 | 171 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
2410 | 171 | LY_CHECK_RET(rc); |
2411 | 171 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
2412 | 0 | ++arg_count; |
2413 | 0 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
2414 | 0 | LY_CHECK_RET(rc); |
2415 | 0 | } |
2416 | 171 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
2417 | 0 | ++(*tok_idx); |
2418 | |
|
2419 | 0 | ++arg_count; |
2420 | 0 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
2421 | 0 | LY_CHECK_RET(rc); |
2422 | 0 | } |
2423 | | |
2424 | | /* ')' */ |
2425 | 171 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
2426 | 171 | LY_CHECK_RET(rc); |
2427 | 171 | ++(*tok_idx); |
2428 | | |
2429 | 171 | if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) { |
2430 | 0 | LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, (int)exp->tok_len[func_tok_idx], |
2431 | 0 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
2432 | 0 | return LY_EVALID; |
2433 | 0 | } |
2434 | | |
2435 | 171 | return LY_SUCCESS; |
2436 | 171 | } |
2437 | | |
2438 | | /** |
2439 | | * @brief Reparse PathExpr. Logs directly on error. |
2440 | | * |
2441 | | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
2442 | | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
2443 | | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
2444 | | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
2445 | | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
2446 | | * |
2447 | | * @param[in] ctx Context for logging. |
2448 | | * @param[in] exp Parsed XPath expression. |
2449 | | * @param[in] tok_idx Position in the expression @p exp. |
2450 | | * @param[in] depth Current number of nested expressions. |
2451 | | * @return LY_ERR |
2452 | | */ |
2453 | | static LY_ERR |
2454 | | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2455 | 945 | { |
2456 | 945 | LY_ERR rc; |
2457 | | |
2458 | 945 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
2459 | 0 | return LY_EVALID; |
2460 | 0 | } |
2461 | | |
2462 | 945 | switch (exp->tokens[*tok_idx]) { |
2463 | 0 | case LYXP_TOKEN_PAR1: |
2464 | | /* '(' Expr ')' Predicate* */ |
2465 | 0 | ++(*tok_idx); |
2466 | |
|
2467 | 0 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
2468 | 0 | LY_CHECK_RET(rc); |
2469 | |
|
2470 | 0 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
2471 | 0 | LY_CHECK_RET(rc); |
2472 | 0 | ++(*tok_idx); |
2473 | 0 | goto predicate; |
2474 | 201 | case LYXP_TOKEN_DOT: |
2475 | 372 | case LYXP_TOKEN_DDOT: |
2476 | 372 | case LYXP_TOKEN_AXISNAME: |
2477 | 372 | case LYXP_TOKEN_AT: |
2478 | 543 | case LYXP_TOKEN_NAMETEST: |
2479 | 543 | case LYXP_TOKEN_NODETYPE: |
2480 | | /* RelativeLocationPath */ |
2481 | 543 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
2482 | 543 | LY_CHECK_RET(rc); |
2483 | 543 | break; |
2484 | 0 | case LYXP_TOKEN_VARREF: |
2485 | | /* VariableReference */ |
2486 | 0 | ++(*tok_idx); |
2487 | 0 | goto predicate; |
2488 | 171 | case LYXP_TOKEN_FUNCNAME: |
2489 | | /* FunctionCall */ |
2490 | 171 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
2491 | 171 | LY_CHECK_RET(rc); |
2492 | 171 | goto predicate; |
2493 | 30 | case LYXP_TOKEN_OPER_PATH: |
2494 | 30 | case LYXP_TOKEN_OPER_RPATH: |
2495 | | /* AbsoluteLocationPath */ |
2496 | 30 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
2497 | 30 | LY_CHECK_RET(rc); |
2498 | 30 | break; |
2499 | 201 | case LYXP_TOKEN_LITERAL: |
2500 | | /* Literal */ |
2501 | 201 | ++(*tok_idx); |
2502 | 201 | goto predicate; |
2503 | 0 | case LYXP_TOKEN_NUMBER: |
2504 | | /* Number */ |
2505 | 0 | ++(*tok_idx); |
2506 | 0 | goto predicate; |
2507 | 0 | default: |
2508 | 0 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
2509 | 0 | return LY_EVALID; |
2510 | 945 | } |
2511 | | |
2512 | 573 | return LY_SUCCESS; |
2513 | | |
2514 | 372 | predicate: |
2515 | | /* Predicate* */ |
2516 | 372 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
2517 | 0 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
2518 | 0 | LY_CHECK_RET(rc); |
2519 | 0 | } |
2520 | | |
2521 | | /* ('/' or '//') RelativeLocationPath */ |
2522 | 372 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
2523 | | |
2524 | | /* '/' or '//' */ |
2525 | 171 | ++(*tok_idx); |
2526 | | |
2527 | 171 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
2528 | 171 | LY_CHECK_RET(rc); |
2529 | 171 | } |
2530 | | |
2531 | 372 | return LY_SUCCESS; |
2532 | 372 | } |
2533 | | |
2534 | | /** |
2535 | | * @brief Reparse UnaryExpr. Logs directly on error. |
2536 | | * |
2537 | | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
2538 | | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
2539 | | * |
2540 | | * @param[in] ctx Context for logging. |
2541 | | * @param[in] exp Parsed XPath expression. |
2542 | | * @param[in] tok_idx Position in the expression @p exp. |
2543 | | * @param[in] depth Current number of nested expressions. |
2544 | | * @return LY_ERR |
2545 | | */ |
2546 | | static LY_ERR |
2547 | | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2548 | 945 | { |
2549 | 945 | uint32_t prev_exp; |
2550 | 945 | LY_ERR rc; |
2551 | | |
2552 | | /* ('-')* */ |
2553 | 945 | prev_exp = *tok_idx; |
2554 | 945 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
2555 | 945 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
2556 | 0 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
2557 | 0 | ++(*tok_idx); |
2558 | 0 | } |
2559 | | |
2560 | | /* PathExpr */ |
2561 | 945 | prev_exp = *tok_idx; |
2562 | 945 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
2563 | 945 | LY_CHECK_RET(rc); |
2564 | | |
2565 | | /* ('|' PathExpr)* */ |
2566 | 945 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
2567 | 0 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
2568 | 0 | ++(*tok_idx); |
2569 | |
|
2570 | 0 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
2571 | 0 | LY_CHECK_RET(rc); |
2572 | 0 | } |
2573 | | |
2574 | 945 | return LY_SUCCESS; |
2575 | 945 | } |
2576 | | |
2577 | | /** |
2578 | | * @brief Reparse AdditiveExpr. Logs directly on error. |
2579 | | * |
2580 | | * [15] AdditiveExpr ::= MultiplicativeExpr |
2581 | | * | AdditiveExpr '+' MultiplicativeExpr |
2582 | | * | AdditiveExpr '-' MultiplicativeExpr |
2583 | | * [16] MultiplicativeExpr ::= UnaryExpr |
2584 | | * | MultiplicativeExpr '*' UnaryExpr |
2585 | | * | MultiplicativeExpr 'div' UnaryExpr |
2586 | | * | MultiplicativeExpr 'mod' UnaryExpr |
2587 | | * |
2588 | | * @param[in] ctx Context for logging. |
2589 | | * @param[in] exp Parsed XPath expression. |
2590 | | * @param[in] tok_idx Position in the expression @p exp. |
2591 | | * @param[in] depth Current number of nested expressions. |
2592 | | * @return LY_ERR |
2593 | | */ |
2594 | | static LY_ERR |
2595 | | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2596 | 945 | { |
2597 | 945 | uint32_t prev_add_exp, prev_mul_exp; |
2598 | 945 | LY_ERR rc; |
2599 | | |
2600 | 945 | prev_add_exp = *tok_idx; |
2601 | 945 | goto reparse_multiplicative_expr; |
2602 | | |
2603 | | /* ('+' / '-' MultiplicativeExpr)* */ |
2604 | 945 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
2605 | 945 | ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) { |
2606 | 0 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
2607 | 0 | ++(*tok_idx); |
2608 | |
|
2609 | 945 | reparse_multiplicative_expr: |
2610 | | /* UnaryExpr */ |
2611 | 945 | prev_mul_exp = *tok_idx; |
2612 | 945 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
2613 | 945 | LY_CHECK_RET(rc); |
2614 | | |
2615 | | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
2616 | 945 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
2617 | 945 | ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) { |
2618 | 0 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
2619 | 0 | ++(*tok_idx); |
2620 | |
|
2621 | 0 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
2622 | 0 | LY_CHECK_RET(rc); |
2623 | 0 | } |
2624 | 945 | } |
2625 | | |
2626 | 945 | return LY_SUCCESS; |
2627 | 0 | } |
2628 | | |
2629 | | /** |
2630 | | * @brief Reparse EqualityExpr. Logs directly on error. |
2631 | | * |
2632 | | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
2633 | | * | EqualityExpr '!=' RelationalExpr |
2634 | | * [14] RelationalExpr ::= AdditiveExpr |
2635 | | * | RelationalExpr '<' AdditiveExpr |
2636 | | * | RelationalExpr '>' AdditiveExpr |
2637 | | * | RelationalExpr '<=' AdditiveExpr |
2638 | | * | RelationalExpr '>=' AdditiveExpr |
2639 | | * |
2640 | | * @param[in] ctx Context for logging. |
2641 | | * @param[in] exp Parsed XPath expression. |
2642 | | * @param[in] tok_idx Position in the expression @p exp. |
2643 | | * @param[in] depth Current number of nested expressions. |
2644 | | * @return LY_ERR |
2645 | | */ |
2646 | | static LY_ERR |
2647 | | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2648 | 573 | { |
2649 | 573 | uint32_t prev_eq_exp, prev_rel_exp; |
2650 | 573 | LY_ERR rc; |
2651 | | |
2652 | 573 | prev_eq_exp = *tok_idx; |
2653 | 573 | goto reparse_additive_expr; |
2654 | | |
2655 | | /* ('=' / '!=' RelationalExpr)* */ |
2656 | 945 | while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) { |
2657 | 372 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
2658 | 372 | ++(*tok_idx); |
2659 | | |
2660 | 945 | reparse_additive_expr: |
2661 | | /* AdditiveExpr */ |
2662 | 945 | prev_rel_exp = *tok_idx; |
2663 | 945 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
2664 | 945 | LY_CHECK_RET(rc); |
2665 | | |
2666 | | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
2667 | 945 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
2668 | 0 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
2669 | 0 | ++(*tok_idx); |
2670 | |
|
2671 | 0 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
2672 | 0 | LY_CHECK_RET(rc); |
2673 | 0 | } |
2674 | 945 | } |
2675 | | |
2676 | 573 | return LY_SUCCESS; |
2677 | 0 | } |
2678 | | |
2679 | | /** |
2680 | | * @brief Reparse OrExpr. Logs directly on error. |
2681 | | * |
2682 | | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
2683 | | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
2684 | | * |
2685 | | * @param[in] ctx Context for logging. |
2686 | | * @param[in] exp Parsed XPath expression. |
2687 | | * @param[in] tok_idx Position in the expression @p exp. |
2688 | | * @param[in] depth Current number of nested expressions. |
2689 | | * @return LY_ERR |
2690 | | */ |
2691 | | static LY_ERR |
2692 | | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
2693 | 573 | { |
2694 | 573 | uint32_t prev_or_exp, prev_and_exp; |
2695 | 573 | LY_ERR rc; |
2696 | | |
2697 | 573 | ++depth; |
2698 | 573 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
2699 | | |
2700 | 573 | prev_or_exp = *tok_idx; |
2701 | 573 | goto reparse_equality_expr; |
2702 | | |
2703 | | /* ('or' AndExpr)* */ |
2704 | 573 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) { |
2705 | 0 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
2706 | 0 | ++(*tok_idx); |
2707 | |
|
2708 | 573 | reparse_equality_expr: |
2709 | | /* EqualityExpr */ |
2710 | 573 | prev_and_exp = *tok_idx; |
2711 | 573 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
2712 | 573 | LY_CHECK_RET(rc); |
2713 | | |
2714 | | /* ('and' EqualityExpr)* */ |
2715 | 573 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) { |
2716 | 0 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
2717 | 0 | ++(*tok_idx); |
2718 | |
|
2719 | 0 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
2720 | 0 | LY_CHECK_RET(rc); |
2721 | 0 | } |
2722 | 573 | } |
2723 | | |
2724 | 573 | return LY_SUCCESS; |
2725 | 0 | } |
2726 | | |
2727 | | /** |
2728 | | * @brief Parse NCName. |
2729 | | * |
2730 | | * @param[in] ncname Name to parse. |
2731 | | * @return Length of @p ncname valid bytes. |
2732 | | */ |
2733 | | static ssize_t |
2734 | | parse_ncname(const char *ncname) |
2735 | 408k | { |
2736 | 408k | uint32_t uc; |
2737 | 408k | size_t size; |
2738 | 408k | ssize_t len = 0; |
2739 | | |
2740 | 408k | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
2741 | 408k | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
2742 | 177 | return len; |
2743 | 177 | } |
2744 | | |
2745 | 10.4M | do { |
2746 | 10.4M | len += size; |
2747 | 10.4M | if (!*ncname) { |
2748 | 76.1k | break; |
2749 | 76.1k | } |
2750 | 10.3M | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
2751 | 10.3M | } while (is_xmlqnamechar(uc) && (uc != ':')); |
2752 | | |
2753 | 408k | return len; |
2754 | 408k | } |
2755 | | |
2756 | | /** |
2757 | | * @brief Add @p token into the expression @p exp. |
2758 | | * |
2759 | | * @param[in] ctx Context for logging. |
2760 | | * @param[in] exp Expression to use. |
2761 | | * @param[in] token Token to add. |
2762 | | * @param[in] tok_pos Token position in the XPath expression. |
2763 | | * @param[in] tok_len Token length in the XPath expression. |
2764 | | * @return LY_ERR |
2765 | | */ |
2766 | | static LY_ERR |
2767 | | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint32_t tok_pos, uint32_t tok_len) |
2768 | 1.41M | { |
2769 | 1.41M | uint32_t prev; |
2770 | | |
2771 | 1.41M | if (exp->used == exp->size) { |
2772 | 205k | prev = exp->size; |
2773 | 205k | exp->size += LYXP_EXPR_SIZE_STEP; |
2774 | 205k | if (prev > exp->size) { |
2775 | 0 | LOGINT(ctx); |
2776 | 0 | return LY_EINT; |
2777 | 0 | } |
2778 | | |
2779 | 205k | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
2780 | 205k | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
2781 | 205k | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
2782 | 205k | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
2783 | 205k | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
2784 | 205k | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
2785 | 205k | } |
2786 | | |
2787 | 1.41M | exp->tokens[exp->used] = token; |
2788 | 1.41M | exp->tok_pos[exp->used] = tok_pos; |
2789 | 1.41M | exp->tok_len[exp->used] = tok_len; |
2790 | 1.41M | ++exp->used; |
2791 | 1.41M | return LY_SUCCESS; |
2792 | 1.41M | } |
2793 | | |
2794 | | void |
2795 | | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
2796 | 1.47M | { |
2797 | 1.47M | uint32_t i; |
2798 | | |
2799 | 1.47M | if (!expr) { |
2800 | 1.32M | return; |
2801 | 1.32M | } |
2802 | | |
2803 | 154k | lydict_remove(ctx, expr->expr); |
2804 | 154k | free(expr->tokens); |
2805 | 154k | free(expr->tok_pos); |
2806 | 154k | free(expr->tok_len); |
2807 | 154k | if (expr->repeat) { |
2808 | 1.89M | for (i = 0; i < expr->used; ++i) { |
2809 | 1.74M | free(expr->repeat[i]); |
2810 | 1.74M | } |
2811 | 152k | } |
2812 | 154k | free(expr->repeat); |
2813 | 154k | free(expr); |
2814 | 154k | } |
2815 | | |
2816 | | /** |
2817 | | * @brief Parse Axis name. |
2818 | | * |
2819 | | * @param[in] str String to parse. |
2820 | | * @param[in] str_len Length of @p str. |
2821 | | * @return LY_SUCCESS if an axis. |
2822 | | * @return LY_ENOT otherwise. |
2823 | | */ |
2824 | | static LY_ERR |
2825 | | expr_parse_axis(const char *str, size_t str_len) |
2826 | 3.48k | { |
2827 | 3.48k | switch (str_len) { |
2828 | 332 | case 4: |
2829 | 332 | if (!strncmp("self", str, str_len)) { |
2830 | 309 | return LY_SUCCESS; |
2831 | 309 | } |
2832 | 23 | break; |
2833 | 321 | case 5: |
2834 | 321 | if (!strncmp("child", str, str_len)) { |
2835 | 298 | return LY_SUCCESS; |
2836 | 298 | } |
2837 | 23 | break; |
2838 | 287 | case 6: |
2839 | 287 | if (!strncmp("parent", str, str_len)) { |
2840 | 251 | return LY_SUCCESS; |
2841 | 251 | } |
2842 | 36 | break; |
2843 | 252 | case 8: |
2844 | 252 | if (!strncmp("ancestor", str, str_len)) { |
2845 | 206 | return LY_SUCCESS; |
2846 | 206 | } |
2847 | 46 | break; |
2848 | 793 | case 9: |
2849 | 793 | if (!strncmp("attribute", str, str_len)) { |
2850 | 205 | return LY_SUCCESS; |
2851 | 588 | } else if (!strncmp("following", str, str_len)) { |
2852 | 198 | return LY_SUCCESS; |
2853 | 390 | } else if (!strncmp("namespace", str, str_len)) { |
2854 | 1 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
2855 | 1 | return LY_ENOT; |
2856 | 389 | } else if (!strncmp("preceding", str, str_len)) { |
2857 | 214 | return LY_SUCCESS; |
2858 | 214 | } |
2859 | 175 | break; |
2860 | 233 | case 10: |
2861 | 233 | if (!strncmp("descendant", str, str_len)) { |
2862 | 184 | return LY_SUCCESS; |
2863 | 184 | } |
2864 | 49 | break; |
2865 | 307 | case 16: |
2866 | 307 | if (!strncmp("ancestor-or-self", str, str_len)) { |
2867 | 227 | return LY_SUCCESS; |
2868 | 227 | } |
2869 | 80 | break; |
2870 | 611 | case 17: |
2871 | 611 | if (!strncmp("following-sibling", str, str_len)) { |
2872 | 236 | return LY_SUCCESS; |
2873 | 375 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
2874 | 244 | return LY_SUCCESS; |
2875 | 244 | } |
2876 | 131 | break; |
2877 | 329 | case 18: |
2878 | 329 | if (!strncmp("descendant-or-self", str, str_len)) { |
2879 | 233 | return LY_SUCCESS; |
2880 | 233 | } |
2881 | 96 | break; |
2882 | 3.48k | } |
2883 | | |
2884 | 682 | return LY_ENOT; |
2885 | 3.48k | } |
2886 | | |
2887 | | LY_ERR |
2888 | | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p) |
2889 | 78.6k | { |
2890 | 78.6k | LY_ERR ret = LY_SUCCESS; |
2891 | 78.6k | struct lyxp_expr *expr; |
2892 | 78.6k | size_t parsed = 0, tok_len; |
2893 | 78.6k | enum lyxp_token tok_type; |
2894 | 78.6k | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
2895 | 78.6k | uint32_t tok_idx = 0; |
2896 | 78.6k | ssize_t ncname_len; |
2897 | | |
2898 | 78.6k | assert(expr_p); |
2899 | | |
2900 | 78.6k | if (!expr_str[0]) { |
2901 | 0 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
2902 | 0 | return LY_EVALID; |
2903 | 0 | } |
2904 | | |
2905 | 78.6k | if (!expr_len) { |
2906 | 201 | expr_len = strlen(expr_str); |
2907 | 201 | } |
2908 | 78.6k | if (expr_len > UINT32_MAX) { |
2909 | 0 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX); |
2910 | 0 | return LY_EVALID; |
2911 | 0 | } |
2912 | | |
2913 | | /* init lyxp_expr structure */ |
2914 | 78.6k | expr = calloc(1, sizeof *expr); |
2915 | 78.6k | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
2916 | 78.6k | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
2917 | 78.6k | expr->used = 0; |
2918 | 78.6k | expr->size = LYXP_EXPR_SIZE_START; |
2919 | 78.6k | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
2920 | 78.6k | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
2921 | | |
2922 | 78.6k | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
2923 | 78.6k | LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error); |
2924 | | |
2925 | 78.6k | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
2926 | 78.6k | LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error); |
2927 | | |
2928 | | /* make expr 0-terminated */ |
2929 | 78.6k | expr_str = expr->expr; |
2930 | | |
2931 | 78.6k | while (is_xmlws(expr_str[parsed])) { |
2932 | 0 | ++parsed; |
2933 | 0 | } |
2934 | | |
2935 | 1.40M | do { |
2936 | 1.40M | if (expr_str[parsed] == '(') { |
2937 | | |
2938 | | /* '(' */ |
2939 | 123k | tok_len = 1; |
2940 | 123k | tok_type = LYXP_TOKEN_PAR1; |
2941 | | |
2942 | 123k | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
2943 | 123k | (((expr->tok_len[expr->used - 1] == 4) && |
2944 | 14.1k | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
2945 | 1.23k | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
2946 | 14.1k | ((expr->tok_len[expr->used - 1] == 7) && |
2947 | 13.6k | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
2948 | | /* it is NodeType after all */ |
2949 | 799 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
2950 | | |
2951 | 799 | prev_ntype_check = 0; |
2952 | 799 | prev_func_check = 0; |
2953 | 122k | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
2954 | | /* it is FunctionName after all */ |
2955 | 12.8k | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
2956 | | |
2957 | 12.8k | prev_ntype_check = 0; |
2958 | 12.8k | prev_func_check = 0; |
2959 | 12.8k | } |
2960 | | |
2961 | 1.28M | } else if (expr_str[parsed] == ')') { |
2962 | | |
2963 | | /* ')' */ |
2964 | 11.3k | tok_len = 1; |
2965 | 11.3k | tok_type = LYXP_TOKEN_PAR2; |
2966 | | |
2967 | 1.27M | } else if (expr_str[parsed] == '[') { |
2968 | | |
2969 | | /* '[' */ |
2970 | 146k | tok_len = 1; |
2971 | 146k | tok_type = LYXP_TOKEN_BRACK1; |
2972 | | |
2973 | 1.12M | } else if (expr_str[parsed] == ']') { |
2974 | | |
2975 | | /* ']' */ |
2976 | 56.0k | tok_len = 1; |
2977 | 56.0k | tok_type = LYXP_TOKEN_BRACK2; |
2978 | | |
2979 | 1.06M | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
2980 | | |
2981 | | /* '..' */ |
2982 | 77.6k | tok_len = 2; |
2983 | 77.6k | tok_type = LYXP_TOKEN_DDOT; |
2984 | | |
2985 | 991k | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
2986 | | |
2987 | | /* '.' */ |
2988 | 88.1k | tok_len = 1; |
2989 | 88.1k | tok_type = LYXP_TOKEN_DOT; |
2990 | | |
2991 | 903k | } else if (expr_str[parsed] == '@') { |
2992 | | |
2993 | | /* '@' */ |
2994 | 2.01k | tok_len = 1; |
2995 | 2.01k | tok_type = LYXP_TOKEN_AT; |
2996 | | |
2997 | 901k | } else if (expr_str[parsed] == ',') { |
2998 | | |
2999 | | /* ',' */ |
3000 | 8.82k | tok_len = 1; |
3001 | 8.82k | tok_type = LYXP_TOKEN_COMMA; |
3002 | | |
3003 | 892k | } else if (expr_str[parsed] == '\'') { |
3004 | | |
3005 | | /* Literal with ' */ |
3006 | 48.4k | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
3007 | 34.4k | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
3008 | 34.4k | LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
3009 | 34.4k | error); |
3010 | 34.3k | ++tok_len; |
3011 | 34.3k | tok_type = LYXP_TOKEN_LITERAL; |
3012 | | |
3013 | 858k | } else if (expr_str[parsed] == '\"') { |
3014 | | |
3015 | | /* Literal with " */ |
3016 | 2.32k | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
3017 | 225 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
3018 | 225 | LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
3019 | 225 | error); |
3020 | 207 | ++tok_len; |
3021 | 207 | tok_type = LYXP_TOKEN_LITERAL; |
3022 | | |
3023 | 858k | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
3024 | | |
3025 | | /* Number */ |
3026 | 112k | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
3027 | 6.59k | if (expr_str[parsed + tok_len] == '.') { |
3028 | 834 | ++tok_len; |
3029 | 5.13k | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
3030 | 834 | } |
3031 | 6.59k | tok_type = LYXP_TOKEN_NUMBER; |
3032 | | |
3033 | 851k | } else if (expr_str[parsed] == '$') { |
3034 | | |
3035 | | /* VariableReference */ |
3036 | 412 | parsed++; |
3037 | 412 | ncname_len = parse_ncname(&expr_str[parsed]); |
3038 | 412 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
3039 | 412 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
3040 | 376 | tok_len = ncname_len; |
3041 | 376 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
3042 | 376 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
3043 | 376 | error); |
3044 | 373 | tok_type = LYXP_TOKEN_VARREF; |
3045 | | |
3046 | 851k | } else if (expr_str[parsed] == '/') { |
3047 | | |
3048 | | /* Operator '/', '//' */ |
3049 | 282k | if (!strncmp(&expr_str[parsed], "//", 2)) { |
3050 | 887 | tok_len = 2; |
3051 | 887 | tok_type = LYXP_TOKEN_OPER_RPATH; |
3052 | 281k | } else { |
3053 | 281k | tok_len = 1; |
3054 | 281k | tok_type = LYXP_TOKEN_OPER_PATH; |
3055 | 281k | } |
3056 | | |
3057 | 568k | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
3058 | | |
3059 | | /* Operator '!=' */ |
3060 | 448 | tok_len = 2; |
3061 | 448 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
3062 | | |
3063 | 567k | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
3064 | | |
3065 | | /* Operator '<=', '>=' */ |
3066 | 1.57k | tok_len = 2; |
3067 | 1.57k | tok_type = LYXP_TOKEN_OPER_COMP; |
3068 | | |
3069 | 566k | } else if (expr_str[parsed] == '|') { |
3070 | | |
3071 | | /* Operator '|' */ |
3072 | 791 | tok_len = 1; |
3073 | 791 | tok_type = LYXP_TOKEN_OPER_UNI; |
3074 | | |
3075 | 565k | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
3076 | | |
3077 | | /* Operator '+', '-' */ |
3078 | 21.9k | tok_len = 1; |
3079 | 21.9k | tok_type = LYXP_TOKEN_OPER_MATH; |
3080 | | |
3081 | 543k | } else if (expr_str[parsed] == '=') { |
3082 | | |
3083 | | /* Operator '=' */ |
3084 | 130k | tok_len = 1; |
3085 | 130k | tok_type = LYXP_TOKEN_OPER_EQUAL; |
3086 | | |
3087 | 412k | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
3088 | | |
3089 | | /* Operator '<', '>' */ |
3090 | 84.3k | tok_len = 1; |
3091 | 84.3k | tok_type = LYXP_TOKEN_OPER_COMP; |
3092 | | |
3093 | 328k | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
3094 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
3095 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
3096 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
3097 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
3098 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
3099 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
3100 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
3101 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
3102 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
3103 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
3104 | 328k | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
3105 | | |
3106 | | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
3107 | 7.30k | if (expr_str[parsed] == '*') { |
3108 | 6.08k | tok_len = 1; |
3109 | 6.08k | tok_type = LYXP_TOKEN_OPER_MATH; |
3110 | | |
3111 | 6.08k | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
3112 | 269 | tok_len = 2; |
3113 | 269 | tok_type = LYXP_TOKEN_OPER_LOG; |
3114 | | |
3115 | 957 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
3116 | 213 | tok_len = 3; |
3117 | 213 | tok_type = LYXP_TOKEN_OPER_LOG; |
3118 | | |
3119 | 744 | } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) { |
3120 | 397 | tok_len = 3; |
3121 | 397 | tok_type = LYXP_TOKEN_OPER_MATH; |
3122 | | |
3123 | 397 | } else if (prev_ntype_check || prev_func_check) { |
3124 | 235 | LOGVAL(ctx, LYVE_XPATH, |
3125 | 235 | "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
3126 | 235 | expr_str[parsed], expr_str[parsed], (int)expr->tok_len[expr->used - 1], |
3127 | 235 | &expr->expr[expr->tok_pos[expr->used - 1]]); |
3128 | 235 | ret = LY_EVALID; |
3129 | 235 | goto error; |
3130 | 235 | } else { |
3131 | 112 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); |
3132 | 112 | ret = LY_EVALID; |
3133 | 112 | goto error; |
3134 | 112 | } |
3135 | 321k | } else { |
3136 | | |
3137 | | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
3138 | 321k | if (expr_str[parsed] == '*') { |
3139 | 6.86k | ncname_len = 1; |
3140 | 314k | } else { |
3141 | 314k | ncname_len = parse_ncname(&expr_str[parsed]); |
3142 | 314k | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
3143 | 314k | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
3144 | 314k | } |
3145 | 320k | tok_len = ncname_len; |
3146 | | |
3147 | 320k | has_axis = 0; |
3148 | 320k | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
3149 | | /* axis */ |
3150 | 3.48k | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
3151 | 3.48k | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); ret = LY_EVALID, |
3152 | 3.48k | error); |
3153 | 2.80k | tok_type = LYXP_TOKEN_AXISNAME; |
3154 | | |
3155 | 2.80k | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
3156 | 2.80k | parsed += tok_len; |
3157 | | |
3158 | | /* '::' */ |
3159 | 2.80k | tok_len = 2; |
3160 | 2.80k | tok_type = LYXP_TOKEN_DCOLON; |
3161 | | |
3162 | 2.80k | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
3163 | 2.80k | parsed += tok_len; |
3164 | | |
3165 | 2.80k | if (expr_str[parsed] == '*') { |
3166 | 363 | ncname_len = 1; |
3167 | 2.44k | } else { |
3168 | 2.44k | ncname_len = parse_ncname(&expr_str[parsed]); |
3169 | 2.44k | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
3170 | 2.44k | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
3171 | 2.39k | } |
3172 | 2.75k | tok_len = ncname_len; |
3173 | | |
3174 | 2.75k | has_axis = 1; |
3175 | 2.75k | } |
3176 | | |
3177 | 320k | if (expr_str[parsed + tok_len] == ':') { |
3178 | 91.9k | ++tok_len; |
3179 | 91.9k | if (expr_str[parsed + tok_len] == '*') { |
3180 | 224 | ++tok_len; |
3181 | 91.7k | } else { |
3182 | 91.7k | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
3183 | 91.7k | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
3184 | 91.7k | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
3185 | 91.6k | tok_len += ncname_len; |
3186 | 91.6k | } |
3187 | | /* remove old flags to prevent ambiguities */ |
3188 | 91.8k | prev_ntype_check = 0; |
3189 | 91.8k | prev_func_check = 0; |
3190 | 91.8k | tok_type = LYXP_TOKEN_NAMETEST; |
3191 | 228k | } else { |
3192 | | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
3193 | 228k | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
3194 | 228k | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
3195 | 228k | tok_type = LYXP_TOKEN_NAMETEST; |
3196 | 228k | } |
3197 | 320k | } |
3198 | | |
3199 | | /* store the token, move on to the next one */ |
3200 | 1.40M | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
3201 | 1.40M | parsed += tok_len; |
3202 | 1.42M | while (is_xmlws(expr_str[parsed])) { |
3203 | 22.3k | ++parsed; |
3204 | 22.3k | } |
3205 | | |
3206 | 1.40M | } while (expr_str[parsed]); |
3207 | | |
3208 | 77.2k | if (reparse) { |
3209 | | /* prealloc repeat */ |
3210 | 201 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
3211 | 201 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
3212 | | |
3213 | | /* fill repeat */ |
3214 | 201 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error); |
3215 | 201 | if (expr->used > tok_idx) { |
3216 | 0 | LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
3217 | 0 | &expr->expr[expr->tok_pos[tok_idx]]); |
3218 | 0 | ret = LY_EVALID; |
3219 | 0 | goto error; |
3220 | 0 | } |
3221 | 201 | } |
3222 | | |
3223 | 77.2k | print_expr_struct_debug(expr); |
3224 | 77.2k | *expr_p = expr; |
3225 | 77.2k | return LY_SUCCESS; |
3226 | | |
3227 | 1.44k | error: |
3228 | 1.44k | lyxp_expr_free(ctx, expr); |
3229 | 1.44k | return ret; |
3230 | 77.2k | } |
3231 | | |
3232 | | LY_ERR |
3233 | | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t start_idx, uint32_t end_idx, |
3234 | | struct lyxp_expr **dup_p) |
3235 | 75.7k | { |
3236 | 75.7k | LY_ERR ret = LY_SUCCESS; |
3237 | 75.7k | struct lyxp_expr *dup = NULL; |
3238 | 75.7k | uint32_t used = 0, i, j, expr_len; |
3239 | 75.7k | const char *expr_start; |
3240 | | |
3241 | 75.7k | assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx))); |
3242 | | |
3243 | 75.7k | if (!exp) { |
3244 | 0 | goto cleanup; |
3245 | 0 | } |
3246 | | |
3247 | 75.7k | if (!start_idx && !end_idx) { |
3248 | 75.4k | end_idx = exp->used - 1; |
3249 | 75.4k | } |
3250 | | |
3251 | 75.7k | expr_start = exp->expr + exp->tok_pos[start_idx]; |
3252 | 75.7k | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
3253 | | |
3254 | 75.7k | dup = calloc(1, sizeof *dup); |
3255 | 75.7k | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
3256 | | |
3257 | 75.7k | if (exp->used) { |
3258 | 75.7k | used = (end_idx - start_idx) + 1; |
3259 | | |
3260 | 75.7k | dup->tokens = malloc(used * sizeof *dup->tokens); |
3261 | 75.7k | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
3262 | 75.7k | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
3263 | | |
3264 | 75.7k | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
3265 | 75.7k | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
3266 | 75.7k | memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos); |
3267 | | |
3268 | 75.7k | if (start_idx) { |
3269 | | /* fix the indices in the expression */ |
3270 | 1.69k | for (i = 0; i < used; ++i) { |
3271 | 1.36k | dup->tok_pos[i] -= expr_start - exp->expr; |
3272 | 1.36k | } |
3273 | 334 | } |
3274 | | |
3275 | 75.7k | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
3276 | 75.7k | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
3277 | 75.7k | memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len); |
3278 | | |
3279 | 75.7k | if (exp->repeat) { |
3280 | 75.7k | dup->repeat = malloc(used * sizeof *dup->repeat); |
3281 | 75.7k | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
3282 | 539k | for (i = start_idx; i <= end_idx; ++i) { |
3283 | 463k | if (!exp->repeat[i]) { |
3284 | 453k | dup->repeat[i - start_idx] = NULL; |
3285 | 453k | } else { |
3286 | 18.8k | for (j = 0; exp->repeat[i][j]; ++j) {} |
3287 | | /* the ending 0 as well */ |
3288 | 9.42k | ++j; |
3289 | | |
3290 | 9.42k | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
3291 | 9.42k | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
3292 | 9.42k | memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat); |
3293 | 9.42k | } |
3294 | 463k | } |
3295 | 75.7k | } |
3296 | 75.7k | } |
3297 | | |
3298 | 75.7k | dup->used = used; |
3299 | 75.7k | dup->size = used; |
3300 | | |
3301 | | /* copy only subexpression */ |
3302 | 75.7k | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup); |
3303 | | |
3304 | 75.7k | cleanup: |
3305 | 75.7k | if (ret) { |
3306 | 0 | lyxp_expr_free(ctx, dup); |
3307 | 75.7k | } else { |
3308 | 75.7k | *dup_p = dup; |
3309 | 75.7k | } |
3310 | 75.7k | return ret; |
3311 | 75.7k | } |
3312 | | |
3313 | | /** |
3314 | | * @brief Get the last-added schema node that is currently in the context. |
3315 | | * |
3316 | | * @param[in] set Set to search in. |
3317 | | * @return Last-added schema context node, NULL if no node is in context. |
3318 | | */ |
3319 | | static struct lysc_node * |
3320 | | warn_get_scnode_in_ctx(struct lyxp_set *set) |
3321 | 0 | { |
3322 | 0 | uint32_t i; |
3323 | |
|
3324 | 0 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
3325 | 0 | return NULL; |
3326 | 0 | } |
3327 | | |
3328 | 0 | i = set->used; |
3329 | 0 | do { |
3330 | 0 | --i; |
3331 | 0 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
3332 | | /* if there are more, simply return the first found (last added) */ |
3333 | 0 | return set->val.scnodes[i].scnode; |
3334 | 0 | } |
3335 | 0 | } while (i); |
3336 | | |
3337 | 0 | return NULL; |
3338 | 0 | } |
3339 | | |
3340 | | /** |
3341 | | * @brief Test whether a type is numeric - integer type or decimal64. |
3342 | | * |
3343 | | * @param[in] type Type to test. |
3344 | | * @return Boolean value whether @p type is numeric type or not. |
3345 | | */ |
3346 | | static ly_bool |
3347 | | warn_is_numeric_type(struct lysc_type *type) |
3348 | 0 | { |
3349 | 0 | struct lysc_type_union *uni; |
3350 | 0 | ly_bool ret; |
3351 | 0 | LY_ARRAY_COUNT_TYPE u; |
3352 | |
|
3353 | 0 | switch (type->basetype) { |
3354 | 0 | case LY_TYPE_DEC64: |
3355 | 0 | case LY_TYPE_INT8: |
3356 | 0 | case LY_TYPE_UINT8: |
3357 | 0 | case LY_TYPE_INT16: |
3358 | 0 | case LY_TYPE_UINT16: |
3359 | 0 | case LY_TYPE_INT32: |
3360 | 0 | case LY_TYPE_UINT32: |
3361 | 0 | case LY_TYPE_INT64: |
3362 | 0 | case LY_TYPE_UINT64: |
3363 | 0 | return 1; |
3364 | 0 | case LY_TYPE_UNION: |
3365 | 0 | uni = (struct lysc_type_union *)type; |
3366 | 0 | LY_ARRAY_FOR(uni->types, u) { |
3367 | 0 | ret = warn_is_numeric_type(uni->types[u]); |
3368 | 0 | if (ret) { |
3369 | | /* found a suitable type */ |
3370 | 0 | return ret; |
3371 | 0 | } |
3372 | 0 | } |
3373 | | /* did not find any suitable type */ |
3374 | 0 | return 0; |
3375 | 0 | case LY_TYPE_LEAFREF: |
3376 | 0 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
3377 | 0 | default: |
3378 | 0 | return 0; |
3379 | 0 | } |
3380 | 0 | } |
3381 | | |
3382 | | /** |
3383 | | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
3384 | | * |
3385 | | * @param[in] type Type to test. |
3386 | | * @return Boolean value whether @p type's basetype is string type or not. |
3387 | | */ |
3388 | | static ly_bool |
3389 | | warn_is_string_type(struct lysc_type *type) |
3390 | 0 | { |
3391 | 0 | struct lysc_type_union *uni; |
3392 | 0 | ly_bool ret; |
3393 | 0 | LY_ARRAY_COUNT_TYPE u; |
3394 | |
|
3395 | 0 | switch (type->basetype) { |
3396 | 0 | case LY_TYPE_BITS: |
3397 | 0 | case LY_TYPE_ENUM: |
3398 | 0 | case LY_TYPE_IDENT: |
3399 | 0 | case LY_TYPE_INST: |
3400 | 0 | case LY_TYPE_STRING: |
3401 | 0 | return 1; |
3402 | 0 | case LY_TYPE_UNION: |
3403 | 0 | uni = (struct lysc_type_union *)type; |
3404 | 0 | LY_ARRAY_FOR(uni->types, u) { |
3405 | 0 | ret = warn_is_string_type(uni->types[u]); |
3406 | 0 | if (ret) { |
3407 | | /* found a suitable type */ |
3408 | 0 | return ret; |
3409 | 0 | } |
3410 | 0 | } |
3411 | | /* did not find any suitable type */ |
3412 | 0 | return 0; |
3413 | 0 | case LY_TYPE_LEAFREF: |
3414 | 0 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
3415 | 0 | default: |
3416 | 0 | return 0; |
3417 | 0 | } |
3418 | 0 | } |
3419 | | |
3420 | | /** |
3421 | | * @brief Test whether a type is one specific type. |
3422 | | * |
3423 | | * @param[in] type Type to test. |
3424 | | * @param[in] base Expected type. |
3425 | | * @return Boolean value whether the given @p type is of the specific basetype @p base. |
3426 | | */ |
3427 | | static ly_bool |
3428 | | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
3429 | 0 | { |
3430 | 0 | struct lysc_type_union *uni; |
3431 | 0 | ly_bool ret; |
3432 | 0 | LY_ARRAY_COUNT_TYPE u; |
3433 | |
|
3434 | 0 | if (type->basetype == base) { |
3435 | 0 | return 1; |
3436 | 0 | } else if (type->basetype == LY_TYPE_UNION) { |
3437 | 0 | uni = (struct lysc_type_union *)type; |
3438 | 0 | LY_ARRAY_FOR(uni->types, u) { |
3439 | 0 | ret = warn_is_specific_type(uni->types[u], base); |
3440 | 0 | if (ret) { |
3441 | | /* found a suitable type */ |
3442 | 0 | return ret; |
3443 | 0 | } |
3444 | 0 | } |
3445 | | /* did not find any suitable type */ |
3446 | 0 | return 0; |
3447 | 0 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
3448 | 0 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
3449 | 0 | } |
3450 | | |
3451 | 0 | return 0; |
3452 | 0 | } |
3453 | | |
3454 | | /** |
3455 | | * @brief Get next type of a (union) type. |
3456 | | * |
3457 | | * @param[in] type Base type. |
3458 | | * @param[in] prev_type Previously returned type. |
3459 | | * @param[in,out] found Whether @p prev_type has already been found or not. |
3460 | | * @return Next type or NULL. |
3461 | | */ |
3462 | | static struct lysc_type * |
3463 | | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type, ly_bool *found) |
3464 | 0 | { |
3465 | 0 | struct lysc_type *next_type; |
3466 | 0 | struct lysc_type_union *uni; |
3467 | 0 | LY_ARRAY_COUNT_TYPE u; |
3468 | |
|
3469 | 0 | if (type->basetype == LY_TYPE_LEAFREF) { |
3470 | 0 | type = ((struct lysc_type_leafref *)type)->realtype; |
3471 | 0 | } |
3472 | |
|
3473 | 0 | if (type->basetype == LY_TYPE_UNION) { |
3474 | 0 | uni = (struct lysc_type_union *)type; |
3475 | 0 | LY_ARRAY_FOR(uni->types, u) { |
3476 | 0 | next_type = warn_is_equal_type_next_type(uni->types[u], prev_type, found); |
3477 | 0 | if (next_type) { |
3478 | 0 | return next_type; |
3479 | 0 | } |
3480 | 0 | } |
3481 | 0 | } else { |
3482 | 0 | if (*found) { |
3483 | 0 | return type; |
3484 | 0 | } else if (prev_type == type) { |
3485 | 0 | *found = 1; |
3486 | 0 | } |
3487 | 0 | } |
3488 | | |
3489 | 0 | return NULL; |
3490 | 0 | } |
3491 | | |
3492 | | /** |
3493 | | * @brief Test whether 2 types have a common type. |
3494 | | * |
3495 | | * @param[in] type1 First type. |
3496 | | * @param[in] type2 Second type. |
3497 | | * @return 1 if they do, 0 otherwise. |
3498 | | */ |
3499 | | static int |
3500 | | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
3501 | 0 | { |
3502 | 0 | struct lysc_type *t1, *t2; |
3503 | 0 | ly_bool found1 = 1, found2 = 1; |
3504 | |
|
3505 | 0 | t1 = NULL; |
3506 | 0 | while ((t1 = warn_is_equal_type_next_type(type1, t1, &found1))) { |
3507 | 0 | found1 = 0; |
3508 | |
|
3509 | 0 | t2 = NULL; |
3510 | 0 | while ((t2 = warn_is_equal_type_next_type(type2, t2, &found2))) { |
3511 | 0 | found2 = 0; |
3512 | |
|
3513 | 0 | if (t2->basetype == t1->basetype) { |
3514 | | /* match found */ |
3515 | 0 | return 1; |
3516 | 0 | } |
3517 | 0 | } |
3518 | 0 | } |
3519 | | |
3520 | 0 | return 0; |
3521 | 0 | } |
3522 | | |
3523 | | /** |
3524 | | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
3525 | | * |
3526 | | * @param[in] ctx Context for logging. |
3527 | | * @param[in] tok_pos Index of the subexpression in the whole expression. |
3528 | | * @param[in] subexpr Subexpression start. |
3529 | | * @param[in] subexpr_len Length of @p subexpr to print. |
3530 | | * @param[in] cur_scnode Expression context node. |
3531 | | */ |
3532 | | static void |
3533 | | warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len, |
3534 | | const struct lysc_node *cur_scnode) |
3535 | 0 | { |
3536 | 0 | char *path; |
3537 | |
|
3538 | 0 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
3539 | 0 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".", |
3540 | 0 | tok_pos, subexpr_len, subexpr, path); |
3541 | 0 | free(path); |
3542 | 0 | } |
3543 | | |
3544 | | /** |
3545 | | * @brief Check both operands of comparison operators. |
3546 | | * |
3547 | | * @param[in] ctx Context for errors. |
3548 | | * @param[in] set1 First operand set. |
3549 | | * @param[in] set2 Second operand set. |
3550 | | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
3551 | | * @param[in] expr Start of the expression to print with the warning. |
3552 | | * @param[in] tok_pos Token position. |
3553 | | */ |
3554 | | static void |
3555 | | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
3556 | | uint32_t tok_pos) |
3557 | 0 | { |
3558 | 0 | struct lysc_node_leaf *node1, *node2; |
3559 | 0 | ly_bool leaves = 1, warning = 0; |
3560 | |
|
3561 | 0 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
3562 | 0 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
3563 | |
|
3564 | 0 | if (!node1 && !node2) { |
3565 | | /* no node-sets involved, nothing to do */ |
3566 | 0 | return; |
3567 | 0 | } |
3568 | | |
3569 | 0 | if (node1) { |
3570 | 0 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
3571 | 0 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
3572 | 0 | warning = 1; |
3573 | 0 | leaves = 0; |
3574 | 0 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
3575 | 0 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
3576 | 0 | warning = 1; |
3577 | 0 | } |
3578 | 0 | } |
3579 | |
|
3580 | 0 | if (node2) { |
3581 | 0 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
3582 | 0 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
3583 | 0 | warning = 1; |
3584 | 0 | leaves = 0; |
3585 | 0 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
3586 | 0 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
3587 | 0 | warning = 1; |
3588 | 0 | } |
3589 | 0 | } |
3590 | |
|
3591 | 0 | if (node1 && node2 && leaves && !numbers_only) { |
3592 | 0 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
3593 | 0 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
3594 | 0 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
3595 | 0 | !warn_is_equal_type(node1->type, node2->type))) { |
3596 | 0 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
3597 | 0 | warning = 1; |
3598 | 0 | } |
3599 | 0 | } |
3600 | |
|
3601 | 0 | if (warning) { |
3602 | 0 | warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode); |
3603 | 0 | } |
3604 | 0 | } |
3605 | | |
3606 | | /** |
3607 | | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
3608 | | * |
3609 | | * @param[in] exp Parsed XPath expression. |
3610 | | * @param[in] set Set with the leaf/leaf-list. |
3611 | | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
3612 | | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
3613 | | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
3614 | | */ |
3615 | | static void |
3616 | | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp, |
3617 | | uint32_t last_equal_exp) |
3618 | 0 | { |
3619 | 0 | struct lysc_node *scnode; |
3620 | 0 | struct lysc_type *type; |
3621 | 0 | char *value; |
3622 | 0 | struct lyd_value storage; |
3623 | 0 | LY_ERR rc; |
3624 | 0 | struct ly_err_item *err = NULL; |
3625 | |
|
3626 | 0 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
3627 | 0 | ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
3628 | | /* check that the node can have the specified value */ |
3629 | 0 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
3630 | 0 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
3631 | 0 | } else { |
3632 | 0 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
3633 | 0 | } |
3634 | 0 | if (!value) { |
3635 | 0 | LOGMEM(set->ctx); |
3636 | 0 | return; |
3637 | 0 | } |
3638 | | |
3639 | 0 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
3640 | 0 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
3641 | 0 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
3642 | 0 | warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp], |
3643 | 0 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
3644 | 0 | set->cur_scnode); |
3645 | 0 | } |
3646 | |
|
3647 | 0 | type = ((struct lysc_node_leaf *)scnode)->type; |
3648 | 0 | if (type->basetype != LY_TYPE_IDENT) { |
3649 | 0 | rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data, |
3650 | 0 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
3651 | 0 | if (rc == LY_EINCOMPLETE) { |
3652 | 0 | rc = LY_SUCCESS; |
3653 | 0 | } |
3654 | |
|
3655 | 0 | if (err) { |
3656 | 0 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
3657 | 0 | ly_err_free(err); |
3658 | 0 | } else if (rc != LY_SUCCESS) { |
3659 | 0 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
3660 | 0 | } |
3661 | 0 | if (rc != LY_SUCCESS) { |
3662 | 0 | warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp], |
3663 | 0 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
3664 | 0 | set->cur_scnode); |
3665 | 0 | } else { |
3666 | 0 | type->plugin->free(set->ctx, &storage); |
3667 | 0 | } |
3668 | 0 | } |
3669 | 0 | free(value); |
3670 | 0 | } |
3671 | 0 | } |
3672 | | |
3673 | | /* |
3674 | | * XPath functions |
3675 | | */ |
3676 | | |
3677 | | /** |
3678 | | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
3679 | | * depending on whether the first node bit value from the second argument is set. |
3680 | | * |
3681 | | * @param[in] args Array of arguments. |
3682 | | * @param[in] arg_count Count of elements in @p args. |
3683 | | * @param[in,out] set Context and result set at the same time. |
3684 | | * @param[in] options XPath options. |
3685 | | * @return LY_ERR |
3686 | | */ |
3687 | | static LY_ERR |
3688 | | xpath_bit_is_set(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
3689 | 0 | { |
3690 | 0 | struct lyd_node_term *leaf; |
3691 | 0 | struct lysc_node_leaf *sleaf; |
3692 | 0 | struct lyd_value_bits *bits; |
3693 | 0 | struct lyd_value *val; |
3694 | 0 | LY_ERR rc = LY_SUCCESS; |
3695 | 0 | LY_ARRAY_COUNT_TYPE u; |
3696 | |
|
3697 | 0 | if (options & LYXP_SCNODE_ALL) { |
3698 | 0 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
3699 | 0 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
3700 | 0 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
3701 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
3702 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
3703 | 0 | sleaf->name); |
3704 | 0 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
3705 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
3706 | 0 | } |
3707 | 0 | } |
3708 | |
|
3709 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
3710 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
3711 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
3712 | 0 | sleaf->name); |
3713 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
3714 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
3715 | 0 | } |
3716 | 0 | } |
3717 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
3718 | 0 | return rc; |
3719 | 0 | } |
3720 | | |
3721 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
3722 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)"); |
3723 | 0 | return LY_EVALID; |
3724 | 0 | } |
3725 | 0 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
3726 | 0 | LY_CHECK_RET(rc); |
3727 | |
|
3728 | 0 | set_fill_boolean(set, 0); |
3729 | 0 | if (args[0]->used && (args[0]->val.nodes[0].node->schema->nodetype & LYD_NODE_TERM)) { |
3730 | 0 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
3731 | 0 | val = &leaf->value; |
3732 | 0 | if (val->realtype->basetype == LY_TYPE_UNION) { |
3733 | 0 | val = &val->subvalue->value; |
3734 | 0 | } |
3735 | 0 | if (val->realtype->basetype == LY_TYPE_BITS) { |
3736 | 0 | LYD_VALUE_GET(val, bits); |
3737 | 0 | LY_ARRAY_FOR(bits->items, u) { |
3738 | 0 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
3739 | 0 | set_fill_boolean(set, 1); |
3740 | 0 | break; |
3741 | 0 | } |
3742 | 0 | } |
3743 | 0 | } |
3744 | 0 | } |
3745 | |
|
3746 | 0 | return LY_SUCCESS; |
3747 | 0 | } |
3748 | | |
3749 | | /** |
3750 | | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
3751 | | * with the argument converted to boolean. |
3752 | | * |
3753 | | * @param[in] args Array of arguments. |
3754 | | * @param[in] arg_count Count of elements in @p args. |
3755 | | * @param[in,out] set Context and result set at the same time. |
3756 | | * @param[in] options XPath options. |
3757 | | * @return LY_ERR |
3758 | | */ |
3759 | | static LY_ERR |
3760 | | xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
3761 | 0 | { |
3762 | 0 | LY_ERR rc; |
3763 | |
|
3764 | 0 | if (options & LYXP_SCNODE_ALL) { |
3765 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
3766 | 0 | return LY_SUCCESS; |
3767 | 0 | } |
3768 | | |
3769 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
3770 | 0 | LY_CHECK_RET(rc); |
3771 | 0 | set_fill_set(set, args[0]); |
3772 | |
|
3773 | 0 | return LY_SUCCESS; |
3774 | 0 | } |
3775 | | |
3776 | | /** |
3777 | | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
3778 | | * with the first argument rounded up to the nearest integer. |
3779 | | * |
3780 | | * @param[in] args Array of arguments. |
3781 | | * @param[in] arg_count Count of elements in @p args. |
3782 | | * @param[in,out] set Context and result set at the same time. |
3783 | | * @param[in] options XPath options. |
3784 | | * @return LY_ERR |
3785 | | */ |
3786 | | static LY_ERR |
3787 | | xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
3788 | 0 | { |
3789 | 0 | struct lysc_node_leaf *sleaf; |
3790 | 0 | LY_ERR rc = LY_SUCCESS; |
3791 | |
|
3792 | 0 | if (options & LYXP_SCNODE_ALL) { |
3793 | 0 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
3794 | 0 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
3795 | 0 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
3796 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
3797 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
3798 | 0 | sleaf->name); |
3799 | 0 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
3800 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
3801 | 0 | } |
3802 | 0 | } |
3803 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
3804 | 0 | return rc; |
3805 | 0 | } |
3806 | | |
3807 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
3808 | 0 | LY_CHECK_RET(rc); |
3809 | 0 | if ((long long)args[0]->val.num != args[0]->val.num) { |
3810 | 0 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
3811 | 0 | } else { |
3812 | 0 | set_fill_number(set, args[0]->val.num); |
3813 | 0 | } |
3814 | |
|
3815 | 0 | return LY_SUCCESS; |
3816 | 0 | } |
3817 | | |
3818 | | /** |
3819 | | * @brief Execute the XPath concat(string, string, string*) function. |
3820 | | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
3821 | | * |
3822 | | * @param[in] args Array of arguments. |
3823 | | * @param[in] arg_count Count of elements in @p args. |
3824 | | * @param[in,out] set Context and result set at the same time. |
3825 | | * @param[in] options XPath options. |
3826 | | * @return LY_ERR |
3827 | | */ |
3828 | | static LY_ERR |
3829 | | xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
3830 | 0 | { |
3831 | 0 | uint32_t i; |
3832 | 0 | char *str = NULL; |
3833 | 0 | size_t used = 1; |
3834 | 0 | LY_ERR rc = LY_SUCCESS; |
3835 | 0 | struct lysc_node_leaf *sleaf; |
3836 | |
|
3837 | 0 | if (options & LYXP_SCNODE_ALL) { |
3838 | 0 | for (i = 0; i < arg_count; ++i) { |
3839 | 0 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
3840 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
3841 | 0 | LOGWRN(set->ctx, "Argument #%" PRIu32 " of %s is a %s node \"%s\".", |
3842 | 0 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
3843 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
3844 | 0 | LOGWRN(set->ctx, "Argument #%" PRIu32 " of %s is node \"%s\", not of string-type.", i + 1, __func__, |
3845 | 0 | sleaf->name); |
3846 | 0 | } |
3847 | 0 | } |
3848 | 0 | } |
3849 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
3850 | 0 | return rc; |
3851 | 0 | } |
3852 | | |
3853 | 0 | for (i = 0; i < arg_count; ++i) { |
3854 | 0 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
3855 | 0 | if (rc != LY_SUCCESS) { |
3856 | 0 | free(str); |
3857 | 0 | return rc; |
3858 | 0 | } |
3859 | | |
3860 | 0 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
3861 | 0 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
3862 | 0 | strcpy(str + used - 1, args[i]->val.str); |
3863 | 0 | used += strlen(args[i]->val.str); |
3864 | 0 | } |
3865 | | |
3866 | | /* free, kind of */ |
3867 | 0 | lyxp_set_free_content(set); |
3868 | 0 | set->type = LYXP_SET_STRING; |
3869 | 0 | set->val.str = str; |
3870 | |
|
3871 | 0 | return LY_SUCCESS; |
3872 | 0 | } |
3873 | | |
3874 | | /** |
3875 | | * @brief Execute the XPath contains(string, string) function. |
3876 | | * Returns LYXP_SET_BOOLEAN whether the second argument can |
3877 | | * be found in the first or not. |
3878 | | * |
3879 | | * @param[in] args Array of arguments. |
3880 | | * @param[in] arg_count Count of elements in @p args. |
3881 | | * @param[in,out] set Context and result set at the same time. |
3882 | | * @param[in] options XPath options. |
3883 | | * @return LY_ERR |
3884 | | */ |
3885 | | static LY_ERR |
3886 | | xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
3887 | 0 | { |
3888 | 0 | struct lysc_node_leaf *sleaf; |
3889 | 0 | LY_ERR rc = LY_SUCCESS; |
3890 | |
|
3891 | 0 | if (options & LYXP_SCNODE_ALL) { |
3892 | 0 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
3893 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
3894 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
3895 | 0 | sleaf->name); |
3896 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
3897 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
3898 | 0 | } |
3899 | 0 | } |
3900 | |
|
3901 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
3902 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
3903 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
3904 | 0 | sleaf->name); |
3905 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
3906 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
3907 | 0 | } |
3908 | 0 | } |
3909 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
3910 | 0 | return rc; |
3911 | 0 | } |
3912 | | |
3913 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
3914 | 0 | LY_CHECK_RET(rc); |
3915 | 0 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
3916 | 0 | LY_CHECK_RET(rc); |
3917 | |
|
3918 | 0 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
3919 | 0 | set_fill_boolean(set, 1); |
3920 | 0 | } else { |
3921 | 0 | set_fill_boolean(set, 0); |
3922 | 0 | } |
3923 | |
|
3924 | 0 | return LY_SUCCESS; |
3925 | 0 | } |
3926 | | |
3927 | | /** |
3928 | | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
3929 | | * with the size of the node-set from the argument. |
3930 | | * |
3931 | | * @param[in] args Array of arguments. |
3932 | | * @param[in] arg_count Count of elements in @p args. |
3933 | | * @param[in,out] set Context and result set at the same time. |
3934 | | * @param[in] options XPath options. |
3935 | | * @return LY_ERR |
3936 | | */ |
3937 | | static LY_ERR |
3938 | | xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
3939 | 0 | { |
3940 | 0 | LY_ERR rc = LY_SUCCESS; |
3941 | |
|
3942 | 0 | if (options & LYXP_SCNODE_ALL) { |
3943 | 0 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
3944 | 0 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
3945 | 0 | } |
3946 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
3947 | 0 | return rc; |
3948 | 0 | } |
3949 | | |
3950 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
3951 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
3952 | 0 | return LY_EVALID; |
3953 | 0 | } |
3954 | | |
3955 | 0 | set_fill_number(set, args[0]->used); |
3956 | 0 | return LY_SUCCESS; |
3957 | 0 | } |
3958 | | |
3959 | | /** |
3960 | | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
3961 | | * with the context with the intial node. |
3962 | | * |
3963 | | * @param[in] args Array of arguments. |
3964 | | * @param[in] arg_count Count of elements in @p args. |
3965 | | * @param[in,out] set Context and result set at the same time. |
3966 | | * @param[in] options XPath options. |
3967 | | * @return LY_ERR |
3968 | | */ |
3969 | | static LY_ERR |
3970 | | xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
3971 | 327 | { |
3972 | 327 | if (arg_count || args) { |
3973 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()")); |
3974 | 0 | return LY_EVALID; |
3975 | 0 | } |
3976 | | |
3977 | 327 | if (options & LYXP_SCNODE_ALL) { |
3978 | 171 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
3979 | | |
3980 | 171 | if (set->cur_scnode) { |
3981 | 171 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL)); |
3982 | 171 | } else { |
3983 | | /* root node */ |
3984 | 0 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL)); |
3985 | 0 | } |
3986 | 171 | } else { |
3987 | 156 | lyxp_set_free_content(set); |
3988 | | |
3989 | 156 | if (set->cur_node) { |
3990 | | /* position is filled later */ |
3991 | 156 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
3992 | 156 | } else { |
3993 | | /* root node */ |
3994 | 0 | set_insert_node(set, NULL, 0, set->root_type, 0); |
3995 | 0 | } |
3996 | 156 | } |
3997 | | |
3998 | 327 | return LY_SUCCESS; |
3999 | 327 | } |
4000 | | |
4001 | | /** |
4002 | | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
4003 | | * leafref or instance-identifier target node(s). |
4004 | | * |
4005 | | * @param[in] args Array of arguments. |
4006 | | * @param[in] arg_count Count of elements in @p args. |
4007 | | * @param[in,out] set Context and result set at the same time. |
4008 | | * @param[in] options XPath options. |
4009 | | * @return LY_ERR |
4010 | | */ |
4011 | | static LY_ERR |
4012 | | xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4013 | 0 | { |
4014 | 0 | struct lyd_node_term *leaf; |
4015 | 0 | struct lysc_node_leaf *sleaf = NULL; |
4016 | 0 | struct lysc_type_leafref *lref; |
4017 | 0 | const struct lysc_node *target; |
4018 | 0 | struct ly_path *p; |
4019 | 0 | struct lyd_node *node; |
4020 | 0 | char *errmsg = NULL; |
4021 | 0 | uint8_t oper; |
4022 | 0 | LY_ERR r; |
4023 | 0 | LY_ERR ret = LY_SUCCESS; |
4024 | 0 | struct ly_set *targets = NULL; |
4025 | 0 | uint32_t i; |
4026 | |
|
4027 | 0 | if (options & LYXP_SCNODE_ALL) { |
4028 | 0 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
4029 | 0 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
4030 | 0 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
4031 | 0 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
4032 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
4033 | 0 | sleaf->name); |
4034 | 0 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
4035 | 0 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
4036 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
4037 | 0 | __func__, sleaf->name); |
4038 | 0 | } |
4039 | 0 | } |
4040 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4041 | 0 | if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
4042 | 0 | lref = (struct lysc_type_leafref *)sleaf->type; |
4043 | 0 | oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
4044 | | |
4045 | | /* it was already evaluated on schema, it must succeed */ |
4046 | 0 | r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY, |
4047 | 0 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
4048 | 0 | if (!r) { |
4049 | | /* get the target node */ |
4050 | 0 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
4051 | 0 | ly_path_free(p); |
4052 | |
|
4053 | 0 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL)); |
4054 | 0 | } /* else the target was found before but is disabled so it was removed */ |
4055 | 0 | } |
4056 | | |
4057 | 0 | ret = LY_SUCCESS; |
4058 | 0 | goto cleanup; |
4059 | 0 | } |
4060 | | |
4061 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
4062 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
4063 | 0 | ret = LY_EVALID; |
4064 | 0 | goto cleanup; |
4065 | 0 | } |
4066 | | |
4067 | 0 | lyxp_set_free_content(set); |
4068 | 0 | if (args[0]->used) { |
4069 | 0 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
4070 | 0 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
4071 | 0 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
4072 | 0 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
4073 | | /* find leafref target */ |
4074 | 0 | r = lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree, |
4075 | 0 | &targets, &errmsg); |
4076 | 0 | if (r) { |
4077 | 0 | LOGERR(set->ctx, LY_EINVAL, "%s", errmsg); |
4078 | 0 | free(errmsg); |
4079 | 0 | ret = LY_EINVAL; |
4080 | 0 | goto cleanup; |
4081 | 0 | } |
4082 | | |
4083 | | /* insert nodes into set */ |
4084 | 0 | for (i = 0; i < targets->count; ++i) { |
4085 | 0 | set_insert_node(set, targets->dnodes[i], 0, LYXP_NODE_ELEM, 0); |
4086 | 0 | } |
4087 | 0 | } else { |
4088 | 0 | assert(sleaf->type->basetype == LY_TYPE_INST); |
4089 | 0 | if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) { |
4090 | 0 | LOGERR(set->ctx, LY_EINVAL, "Invalid instance-identifier \"%s\" value - required instance not found.", |
4091 | 0 | lyd_get_value(&leaf->node)); |
4092 | 0 | ret = LY_EINVAL; |
4093 | 0 | goto cleanup; |
4094 | 0 | } |
4095 | | |
4096 | | /* insert it */ |
4097 | 0 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
4098 | 0 | } |
4099 | 0 | } |
4100 | 0 | } |
4101 | | |
4102 | 0 | cleanup: |
4103 | 0 | ly_set_free(targets, NULL); |
4104 | 0 | return ret; |
4105 | 0 | } |
4106 | | |
4107 | | /** |
4108 | | * @brief Get the module of an identity used in derived-from(-or-self)() functions. |
4109 | | * |
4110 | | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
4111 | | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
4112 | | * @param[in] set Set with general XPath context. |
4113 | | * @param[out] mod Module of the identity. |
4114 | | * @return LY_ERR |
4115 | | */ |
4116 | | static LY_ERR |
4117 | | xpath_derived_ident_module(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
4118 | | const struct lys_module **mod) |
4119 | 0 | { |
4120 | 0 | LY_CHECK_RET(moveto_resolve_module(qname, qname_len, set, set->cur_node ? set->cur_node->schema : NULL, mod)); |
4121 | 0 | if (!*mod) { |
4122 | | /* unprefixed JSON identity */ |
4123 | 0 | assert(set->format == LY_VALUE_JSON); |
4124 | 0 | *mod = set->cur_mod; |
4125 | 0 | } |
4126 | | |
4127 | 0 | return LY_SUCCESS; |
4128 | 0 | } |
4129 | | |
4130 | | static LY_ERR |
4131 | | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func) |
4132 | 0 | { |
4133 | 0 | uint32_t i, id_len; |
4134 | 0 | LY_ARRAY_COUNT_TYPE u; |
4135 | 0 | struct lyd_node_term *leaf; |
4136 | 0 | struct lysc_node_leaf *sleaf; |
4137 | 0 | struct lyd_meta *meta; |
4138 | 0 | struct lyd_value *val; |
4139 | 0 | const struct lys_module *mod; |
4140 | 0 | const char *id_name; |
4141 | 0 | struct lysc_ident *id; |
4142 | 0 | LY_ERR rc = LY_SUCCESS; |
4143 | 0 | ly_bool found; |
4144 | |
|
4145 | 0 | if (options & LYXP_SCNODE_ALL) { |
4146 | 0 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
4147 | 0 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
4148 | 0 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
4149 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
4150 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
4151 | 0 | sleaf->name); |
4152 | 0 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
4153 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
4154 | 0 | } |
4155 | 0 | } |
4156 | |
|
4157 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
4158 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
4159 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
4160 | 0 | sleaf->name); |
4161 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
4162 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
4163 | 0 | } |
4164 | 0 | } |
4165 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4166 | 0 | return rc; |
4167 | 0 | } |
4168 | | |
4169 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
4170 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)"); |
4171 | 0 | return LY_EVALID; |
4172 | 0 | } |
4173 | 0 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
4174 | 0 | LY_CHECK_RET(rc); |
4175 | | |
4176 | | /* parse the identity */ |
4177 | 0 | id_name = args[1]->val.str; |
4178 | 0 | id_len = strlen(id_name); |
4179 | 0 | rc = xpath_derived_ident_module(&id_name, &id_len, set, &mod); |
4180 | 0 | LY_CHECK_RET(rc); |
4181 | | |
4182 | | /* find the identity */ |
4183 | 0 | found = 0; |
4184 | 0 | LY_ARRAY_FOR(mod->identities, u) { |
4185 | 0 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
4186 | | /* we have match */ |
4187 | 0 | found = 1; |
4188 | 0 | break; |
4189 | 0 | } |
4190 | 0 | } |
4191 | 0 | if (!found) { |
4192 | 0 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
4193 | 0 | return LY_EVALID; |
4194 | 0 | } |
4195 | 0 | id = &mod->identities[u]; |
4196 | |
|
4197 | 0 | set_fill_boolean(set, 0); |
4198 | 0 | found = 0; |
4199 | 0 | for (i = 0; i < args[0]->used; ++i) { |
4200 | 0 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
4201 | 0 | continue; |
4202 | 0 | } |
4203 | | |
4204 | 0 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
4205 | 0 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
4206 | 0 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
4207 | 0 | val = &leaf->value; |
4208 | 0 | if (!sleaf || !(sleaf->nodetype & LYD_NODE_TERM)) { |
4209 | | /* uninteresting */ |
4210 | 0 | continue; |
4211 | 0 | } |
4212 | 0 | } else { |
4213 | 0 | meta = args[0]->val.meta[i].meta; |
4214 | 0 | val = &meta->value; |
4215 | 0 | } |
4216 | | |
4217 | 0 | if (val->realtype->basetype == LY_TYPE_UNION) { |
4218 | 0 | val = &val->subvalue->value; |
4219 | 0 | } |
4220 | 0 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
4221 | | /* uninteresting */ |
4222 | 0 | continue; |
4223 | 0 | } |
4224 | | |
4225 | | /* check the identity itself */ |
4226 | 0 | if (self_match && (id == val->ident)) { |
4227 | 0 | set_fill_boolean(set, 1); |
4228 | 0 | found = 1; |
4229 | 0 | } |
4230 | 0 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
4231 | 0 | set_fill_boolean(set, 1); |
4232 | 0 | found = 1; |
4233 | 0 | } |
4234 | |
|
4235 | 0 | if (found) { |
4236 | 0 | break; |
4237 | 0 | } |
4238 | 0 | } |
4239 | |
|
4240 | 0 | return LY_SUCCESS; |
4241 | 0 | } |
4242 | | |
4243 | | /** |
4244 | | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
4245 | | * on whether the first argument nodes contain a node of an identity derived from the second |
4246 | | * argument identity. |
4247 | | * |
4248 | | * @param[in] args Array of arguments. |
4249 | | * @param[in] arg_count Count of elements in @p args. |
4250 | | * @param[in,out] set Context and result set at the same time. |
4251 | | * @param[in] options XPath options. |
4252 | | * @return LY_ERR |
4253 | | */ |
4254 | | static LY_ERR |
4255 | | xpath_derived_from(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4256 | 0 | { |
4257 | 0 | return xpath_derived_(args, set, options, 0, __func__); |
4258 | 0 | } |
4259 | | |
4260 | | /** |
4261 | | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
4262 | | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
4263 | | * the second argument identity. |
4264 | | * |
4265 | | * @param[in] args Array of arguments. |
4266 | | * @param[in] arg_count Count of elements in @p args. |
4267 | | * @param[in,out] set Context and result set at the same time. |
4268 | | * @param[in] options XPath options. |
4269 | | * @return LY_ERR |
4270 | | */ |
4271 | | static LY_ERR |
4272 | | xpath_derived_from_or_self(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4273 | 0 | { |
4274 | 0 | return xpath_derived_(args, set, options, 1, __func__); |
4275 | 0 | } |
4276 | | |
4277 | | /** |
4278 | | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
4279 | | * with the integer value of the first node's enum value, otherwise NaN. |
4280 | | * |
4281 | | * @param[in] args Array of arguments. |
4282 | | * @param[in] arg_count Count of elements in @p args. |
4283 | | * @param[in,out] set Context and result set at the same time. |
4284 | | * @param[in] options XPath options. |
4285 | | * @return LY_ERR |
4286 | | */ |
4287 | | static LY_ERR |
4288 | | xpath_enum_value(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4289 | 0 | { |
4290 | 0 | struct lyd_node_term *leaf; |
4291 | 0 | struct lysc_node_leaf *sleaf; |
4292 | 0 | LY_ERR rc = LY_SUCCESS; |
4293 | |
|
4294 | 0 | if (options & LYXP_SCNODE_ALL) { |
4295 | 0 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
4296 | 0 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
4297 | 0 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
4298 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
4299 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
4300 | 0 | sleaf->name); |
4301 | 0 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
4302 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
4303 | 0 | } |
4304 | 0 | } |
4305 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4306 | 0 | return rc; |
4307 | 0 | } |
4308 | | |
4309 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
4310 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
4311 | 0 | return LY_EVALID; |
4312 | 0 | } |
4313 | | |
4314 | 0 | set_fill_number(set, NAN); |
4315 | 0 | if (args[0]->used) { |
4316 | 0 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
4317 | 0 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
4318 | 0 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
4319 | 0 | set_fill_number(set, leaf->value.enum_item->value); |
4320 | 0 | } |
4321 | 0 | } |
4322 | |
|
4323 | 0 | return LY_SUCCESS; |
4324 | 0 | } |
4325 | | |
4326 | | /** |
4327 | | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
4328 | | * with false value. |
4329 | | * |
4330 | | * @param[in] args Array of arguments. |
4331 | | * @param[in] arg_count Count of elements in @p args. |
4332 | | * @param[in,out] set Context and result set at the same time. |
4333 | | * @param[in] options XPath options. |
4334 | | * @return LY_ERR |
4335 | | */ |
4336 | | static LY_ERR |
4337 | | xpath_false(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4338 | 0 | { |
4339 | 0 | if (options & LYXP_SCNODE_ALL) { |
4340 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
4341 | 0 | return LY_SUCCESS; |
4342 | 0 | } |
4343 | | |
4344 | 0 | set_fill_boolean(set, 0); |
4345 | 0 | return LY_SUCCESS; |
4346 | 0 | } |
4347 | | |
4348 | | /** |
4349 | | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
4350 | | * with the first argument floored (truncated). |
4351 | | * |
4352 | | * @param[in] args Array of arguments. |
4353 | | * @param[in] arg_count Count of elements in @p args. |
4354 | | * @param[in,out] set Context and result set at the same time. |
4355 | | * @param[in] options XPath options. |
4356 | | * @return LY_ERR |
4357 | | */ |
4358 | | static LY_ERR |
4359 | | xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4360 | 0 | { |
4361 | 0 | struct lysc_node_leaf *sleaf; |
4362 | 0 | LY_ERR rc = LY_SUCCESS; |
4363 | |
|
4364 | 0 | if (options & LYXP_SCNODE_ALL) { |
4365 | 0 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
4366 | 0 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
4367 | 0 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
4368 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
4369 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
4370 | 0 | sleaf->name); |
4371 | 0 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
4372 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
4373 | 0 | } |
4374 | 0 | } |
4375 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4376 | 0 | return rc; |
4377 | 0 | } |
4378 | | |
4379 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
4380 | 0 | LY_CHECK_RET(rc); |
4381 | 0 | if (isfinite(args[0]->val.num)) { |
4382 | 0 | set_fill_number(set, (long long)args[0]->val.num); |
4383 | 0 | } |
4384 | |
|
4385 | 0 | return LY_SUCCESS; |
4386 | 0 | } |
4387 | | |
4388 | | /** |
4389 | | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
4390 | | * whether the language of the text matches the one from the argument. |
4391 | | * |
4392 | | * @param[in] args Array of arguments. |
4393 | | * @param[in] arg_count Count of elements in @p args. |
4394 | | * @param[in,out] set Context and result set at the same time. |
4395 | | * @param[in] options XPath options. |
4396 | | * @return LY_ERR |
4397 | | */ |
4398 | | static LY_ERR |
4399 | | xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4400 | 0 | { |
4401 | 0 | const struct lyd_node *node; |
4402 | 0 | struct lysc_node_leaf *sleaf; |
4403 | 0 | struct lyd_meta *meta = NULL; |
4404 | 0 | const char *val; |
4405 | 0 | LY_ERR rc = LY_SUCCESS; |
4406 | |
|
4407 | 0 | if (options & LYXP_SCNODE_ALL) { |
4408 | 0 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
4409 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
4410 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
4411 | 0 | sleaf->name); |
4412 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
4413 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
4414 | 0 | } |
4415 | 0 | } |
4416 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4417 | 0 | return rc; |
4418 | 0 | } |
4419 | | |
4420 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
4421 | 0 | LY_CHECK_RET(rc); |
4422 | |
|
4423 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
4424 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
4425 | 0 | return LY_EVALID; |
4426 | 0 | } else if (!set->used) { |
4427 | 0 | set_fill_boolean(set, 0); |
4428 | 0 | return LY_SUCCESS; |
4429 | 0 | } |
4430 | | |
4431 | 0 | switch (set->val.nodes[0].type) { |
4432 | 0 | case LYXP_NODE_ELEM: |
4433 | 0 | case LYXP_NODE_TEXT: |
4434 | 0 | node = set->val.nodes[0].node; |
4435 | 0 | break; |
4436 | 0 | case LYXP_NODE_META: |
4437 | 0 | node = set->val.meta[0].meta->parent; |
4438 | 0 | break; |
4439 | 0 | default: |
4440 | | /* nothing to do with roots */ |
4441 | 0 | set_fill_boolean(set, 0); |
4442 | 0 | return LY_SUCCESS; |
4443 | 0 | } |
4444 | | |
4445 | | /* find lang metadata */ |
4446 | 0 | for ( ; node; node = lyd_parent(node)) { |
4447 | 0 | for (meta = node->meta; meta; meta = meta->next) { |
4448 | | /* annotations */ |
4449 | 0 | if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) { |
4450 | 0 | break; |
4451 | 0 | } |
4452 | 0 | } |
4453 | |
|
4454 | 0 | if (meta) { |
4455 | 0 | break; |
4456 | 0 | } |
4457 | 0 | } |
4458 | | |
4459 | | /* compare languages */ |
4460 | 0 | if (!meta) { |
4461 | 0 | set_fill_boolean(set, 0); |
4462 | 0 | } else { |
4463 | 0 | uint64_t i; |
4464 | |
|
4465 | 0 | val = lyd_get_meta_value(meta); |
4466 | 0 | for (i = 0; args[0]->val.str[i]; ++i) { |
4467 | 0 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
4468 | 0 | set_fill_boolean(set, 0); |
4469 | 0 | break; |
4470 | 0 | } |
4471 | 0 | } |
4472 | 0 | if (!args[0]->val.str[i]) { |
4473 | 0 | if (!val[i] || (val[i] == '-')) { |
4474 | 0 | set_fill_boolean(set, 1); |
4475 | 0 | } else { |
4476 | 0 | set_fill_boolean(set, 0); |
4477 | 0 | } |
4478 | 0 | } |
4479 | 0 | } |
4480 | |
|
4481 | 0 | return LY_SUCCESS; |
4482 | 0 | } |
4483 | | |
4484 | | /** |
4485 | | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
4486 | | * with the context size. |
4487 | | * |
4488 | | * @param[in] args Array of arguments. |
4489 | | * @param[in] arg_count Count of elements in @p args. |
4490 | | * @param[in,out] set Context and result set at the same time. |
4491 | | * @param[in] options XPath options. |
4492 | | * @return LY_ERR |
4493 | | */ |
4494 | | static LY_ERR |
4495 | | xpath_last(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4496 | 0 | { |
4497 | 0 | if (options & LYXP_SCNODE_ALL) { |
4498 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
4499 | 0 | return LY_SUCCESS; |
4500 | 0 | } |
4501 | | |
4502 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
4503 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
4504 | 0 | return LY_EVALID; |
4505 | 0 | } else if (!set->used) { |
4506 | 0 | set_fill_number(set, 0); |
4507 | 0 | return LY_SUCCESS; |
4508 | 0 | } |
4509 | | |
4510 | 0 | set_fill_number(set, set->ctx_size); |
4511 | 0 | return LY_SUCCESS; |
4512 | 0 | } |
4513 | | |
4514 | | /** |
4515 | | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
4516 | | * with the node name without namespace from the argument or the context. |
4517 | | * |
4518 | | * @param[in] args Array of arguments. |
4519 | | * @param[in] arg_count Count of elements in @p args. |
4520 | | * @param[in,out] set Context and result set at the same time. |
4521 | | * @param[in] options XPath options. |
4522 | | * @return LY_ERR |
4523 | | */ |
4524 | | static LY_ERR |
4525 | | xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
4526 | 0 | { |
4527 | 0 | struct lyxp_set_node *item; |
4528 | | |
4529 | | /* suppress unused variable warning */ |
4530 | 0 | (void)options; |
4531 | |
|
4532 | 0 | if (options & LYXP_SCNODE_ALL) { |
4533 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
4534 | 0 | return LY_SUCCESS; |
4535 | 0 | } |
4536 | | |
4537 | 0 | if (arg_count) { |
4538 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
4539 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
4540 | 0 | "local-name(node-set?)"); |
4541 | 0 | return LY_EVALID; |
4542 | 0 | } else if (!args[0]->used) { |
4543 | 0 | set_fill_string(set, "", 0); |
4544 | 0 | return LY_SUCCESS; |
4545 | 0 | } |
4546 | | |
4547 | | /* we need the set sorted, it affects the result */ |
4548 | 0 | assert(!set_sort(args[0])); |
4549 | | |
4550 | 0 | item = &args[0]->val.nodes[0]; |
4551 | 0 | } else { |
4552 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
4553 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
4554 | 0 | return LY_EVALID; |
4555 | 0 | } else if (!set->used) { |
4556 | 0 | set_fill_string(set, "", 0); |
4557 | 0 | return LY_SUCCESS; |
4558 | 0 | } |
4559 | | |
4560 | | /* we need the set sorted, it affects the result */ |
4561 | 0 | assert(!set_sort(set)); |
4562 | | |
4563 | 0 | item = &set->val.nodes[0]; |
4564 | 0 | } |
4565 | | |
4566 | 0 | switch (item->type) { |
4567 | 0 | case LYXP_NODE_NONE: |
4568 | 0 | LOGINT_RET(set->ctx); |
4569 | 0 | case LYXP_NODE_ROOT: |
4570 | 0 | case LYXP_NODE_ROOT_CONFIG: |
4571 | 0 | case LYXP_NODE_TEXT: |
4572 | 0 | set_fill_string(set, "", 0); |
4573 | 0 | break; |
4574 | 0 | case LYXP_NODE_ELEM: |
4575 | 0 | set_fill_string(set, LYD_NAME(item->node), strlen(LYD_NAME(item->node))); |
4576 | 0 | break; |
4577 | 0 | case LYXP_NODE_META: |
4578 | 0 | set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name)); |
4579 | 0 | break; |
4580 | 0 | } |
4581 | | |
4582 | 0 | return LY_SUCCESS; |
4583 | 0 | } |
4584 | | |
4585 | | /** |
4586 | | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
4587 | | * with the node name fully qualified (with namespace) from the argument or the context. |
4588 | | * |
4589 | | * @param[in] args Array of arguments. |
4590 | | * @param[in] arg_count Count of elements in @p args. |
4591 | | * @param[in,out] set Context and result set at the same time. |
4592 | | * @param[in] options XPath options. |
4593 | | * @return LY_ERR |
4594 | | */ |
4595 | | static LY_ERR |
4596 | | xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
4597 | 0 | { |
4598 | 0 | struct lyxp_set_node *item; |
4599 | 0 | const struct lys_module *mod = NULL; |
4600 | 0 | char *str; |
4601 | 0 | const char *name = NULL; |
4602 | |
|
4603 | 0 | if (options & LYXP_SCNODE_ALL) { |
4604 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
4605 | 0 | return LY_SUCCESS; |
4606 | 0 | } |
4607 | | |
4608 | 0 | if (arg_count) { |
4609 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
4610 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
4611 | 0 | return LY_EVALID; |
4612 | 0 | } else if (!args[0]->used) { |
4613 | 0 | set_fill_string(set, "", 0); |
4614 | 0 | return LY_SUCCESS; |
4615 | 0 | } |
4616 | | |
4617 | | /* we need the set sorted, it affects the result */ |
4618 | 0 | assert(!set_sort(args[0])); |
4619 | | |
4620 | 0 | item = &args[0]->val.nodes[0]; |
4621 | 0 | } else { |
4622 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
4623 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
4624 | 0 | return LY_EVALID; |
4625 | 0 | } else if (!set->used) { |
4626 | 0 | set_fill_string(set, "", 0); |
4627 | 0 | return LY_SUCCESS; |
4628 | 0 | } |
4629 | | |
4630 | | /* we need the set sorted, it affects the result */ |
4631 | 0 | assert(!set_sort(set)); |
4632 | | |
4633 | 0 | item = &set->val.nodes[0]; |
4634 | 0 | } |
4635 | | |
4636 | 0 | switch (item->type) { |
4637 | 0 | case LYXP_NODE_NONE: |
4638 | 0 | LOGINT_RET(set->ctx); |
4639 | 0 | case LYXP_NODE_ROOT: |
4640 | 0 | case LYXP_NODE_ROOT_CONFIG: |
4641 | 0 | case LYXP_NODE_TEXT: |
4642 | | /* keep NULL */ |
4643 | 0 | break; |
4644 | 0 | case LYXP_NODE_ELEM: |
4645 | 0 | mod = lyd_node_module(item->node); |
4646 | 0 | name = LYD_NAME(item->node); |
4647 | 0 | break; |
4648 | 0 | case LYXP_NODE_META: |
4649 | 0 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
4650 | 0 | name = ((struct lyd_meta *)item->node)->name; |
4651 | 0 | break; |
4652 | 0 | } |
4653 | | |
4654 | 0 | if (mod && name) { |
4655 | 0 | int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name); |
4656 | |
|
4657 | 0 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
4658 | 0 | set_fill_string(set, str, strlen(str)); |
4659 | 0 | free(str); |
4660 | 0 | } else { |
4661 | 0 | set_fill_string(set, "", 0); |
4662 | 0 | } |
4663 | | |
4664 | 0 | return LY_SUCCESS; |
4665 | 0 | } |
4666 | | |
4667 | | /** |
4668 | | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
4669 | | * with the namespace of the node from the argument or the context. |
4670 | | * |
4671 | | * @param[in] args Array of arguments. |
4672 | | * @param[in] arg_count Count of elements in @p args. |
4673 | | * @param[in,out] set Context and result set at the same time. |
4674 | | * @param[in] options XPath options. |
4675 | | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
4676 | | */ |
4677 | | static LY_ERR |
4678 | | xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
4679 | 0 | { |
4680 | 0 | struct lyxp_set_node *item; |
4681 | 0 | const struct lys_module *mod; |
4682 | | |
4683 | | /* suppress unused variable warning */ |
4684 | 0 | (void)options; |
4685 | |
|
4686 | 0 | if (options & LYXP_SCNODE_ALL) { |
4687 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4688 | 0 | return LY_SUCCESS; |
4689 | 0 | } |
4690 | | |
4691 | 0 | if (arg_count) { |
4692 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
4693 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
4694 | 0 | "namespace-uri(node-set?)"); |
4695 | 0 | return LY_EVALID; |
4696 | 0 | } else if (!args[0]->used) { |
4697 | 0 | set_fill_string(set, "", 0); |
4698 | 0 | return LY_SUCCESS; |
4699 | 0 | } |
4700 | | |
4701 | | /* we need the set sorted, it affects the result */ |
4702 | 0 | assert(!set_sort(args[0])); |
4703 | | |
4704 | 0 | item = &args[0]->val.nodes[0]; |
4705 | 0 | } else { |
4706 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
4707 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
4708 | 0 | return LY_EVALID; |
4709 | 0 | } else if (!set->used) { |
4710 | 0 | set_fill_string(set, "", 0); |
4711 | 0 | return LY_SUCCESS; |
4712 | 0 | } |
4713 | | |
4714 | | /* we need the set sorted, it affects the result */ |
4715 | 0 | assert(!set_sort(set)); |
4716 | | |
4717 | 0 | item = &set->val.nodes[0]; |
4718 | 0 | } |
4719 | | |
4720 | 0 | switch (item->type) { |
4721 | 0 | case LYXP_NODE_NONE: |
4722 | 0 | LOGINT_RET(set->ctx); |
4723 | 0 | case LYXP_NODE_ROOT: |
4724 | 0 | case LYXP_NODE_ROOT_CONFIG: |
4725 | 0 | case LYXP_NODE_TEXT: |
4726 | 0 | set_fill_string(set, "", 0); |
4727 | 0 | break; |
4728 | 0 | case LYXP_NODE_ELEM: |
4729 | 0 | case LYXP_NODE_META: |
4730 | 0 | if (item->type == LYXP_NODE_ELEM) { |
4731 | 0 | mod = lyd_node_module(item->node); |
4732 | 0 | } else { /* LYXP_NODE_META */ |
4733 | | /* annotations */ |
4734 | 0 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
4735 | 0 | } |
4736 | |
|
4737 | 0 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
4738 | 0 | break; |
4739 | 0 | } |
4740 | | |
4741 | 0 | return LY_SUCCESS; |
4742 | 0 | } |
4743 | | |
4744 | | /** |
4745 | | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
4746 | | * with normalized value (no leading, trailing, double white spaces) of the node |
4747 | | * from the argument or the context. |
4748 | | * |
4749 | | * @param[in] args Array of arguments. |
4750 | | * @param[in] arg_count Count of elements in @p args. |
4751 | | * @param[in,out] set Context and result set at the same time. |
4752 | | * @param[in] options XPath options. |
4753 | | * @return LY_ERR |
4754 | | */ |
4755 | | static LY_ERR |
4756 | | xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
4757 | 0 | { |
4758 | 0 | uint32_t i, new_used; |
4759 | 0 | char *new; |
4760 | 0 | ly_bool have_spaces = 0, space_before = 0; |
4761 | 0 | struct lysc_node_leaf *sleaf; |
4762 | 0 | LY_ERR rc = LY_SUCCESS; |
4763 | |
|
4764 | 0 | if (options & LYXP_SCNODE_ALL) { |
4765 | 0 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
4766 | 0 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
4767 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
4768 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
4769 | 0 | sleaf->name); |
4770 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
4771 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
4772 | 0 | } |
4773 | 0 | } |
4774 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4775 | 0 | return rc; |
4776 | 0 | } |
4777 | | |
4778 | 0 | if (arg_count) { |
4779 | 0 | set_fill_set(set, args[0]); |
4780 | 0 | } |
4781 | 0 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
4782 | 0 | LY_CHECK_RET(rc); |
4783 | | |
4784 | | /* is there any normalization necessary? */ |
4785 | 0 | for (i = 0; set->val.str[i]; ++i) { |
4786 | 0 | if (is_xmlws(set->val.str[i])) { |
4787 | 0 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
4788 | 0 | have_spaces = 1; |
4789 | 0 | break; |
4790 | 0 | } |
4791 | 0 | space_before = 1; |
4792 | 0 | } else { |
4793 | 0 | space_before = 0; |
4794 | 0 | } |
4795 | 0 | } |
4796 | | |
4797 | | /* yep, there is */ |
4798 | 0 | if (have_spaces) { |
4799 | | /* it's enough, at least one character will go, makes space for ending '\0' */ |
4800 | 0 | new = malloc(strlen(set->val.str) * sizeof(char)); |
4801 | 0 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
4802 | 0 | new_used = 0; |
4803 | |
|
4804 | 0 | space_before = 0; |
4805 | 0 | for (i = 0; set->val.str[i]; ++i) { |
4806 | 0 | if (is_xmlws(set->val.str[i])) { |
4807 | 0 | if ((i == 0) || space_before) { |
4808 | 0 | space_before = 1; |
4809 | 0 | continue; |
4810 | 0 | } else { |
4811 | 0 | space_before = 1; |
4812 | 0 | } |
4813 | 0 | } else { |
4814 | 0 | space_before = 0; |
4815 | 0 | } |
4816 | | |
4817 | 0 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
4818 | 0 | ++new_used; |
4819 | 0 | } |
4820 | | |
4821 | | /* at worst there is one trailing space now */ |
4822 | 0 | if (new_used && is_xmlws(new[new_used - 1])) { |
4823 | 0 | --new_used; |
4824 | 0 | } |
4825 | |
|
4826 | 0 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
4827 | 0 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
4828 | 0 | new[new_used] = '\0'; |
4829 | |
|
4830 | 0 | free(set->val.str); |
4831 | 0 | set->val.str = new; |
4832 | 0 | } |
4833 | | |
4834 | 0 | return LY_SUCCESS; |
4835 | 0 | } |
4836 | | |
4837 | | /** |
4838 | | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
4839 | | * with the argument converted to boolean and logically inverted. |
4840 | | * |
4841 | | * @param[in] args Array of arguments. |
4842 | | * @param[in] arg_count Count of elements in @p args. |
4843 | | * @param[in,out] set Context and result set at the same time. |
4844 | | * @param[in] options XPath options. |
4845 | | * @return LY_ERR |
4846 | | */ |
4847 | | static LY_ERR |
4848 | | xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4849 | 0 | { |
4850 | 0 | if (options & LYXP_SCNODE_ALL) { |
4851 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
4852 | 0 | return LY_SUCCESS; |
4853 | 0 | } |
4854 | | |
4855 | 0 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
4856 | 0 | if (args[0]->val.bln) { |
4857 | 0 | set_fill_boolean(set, 0); |
4858 | 0 | } else { |
4859 | 0 | set_fill_boolean(set, 1); |
4860 | 0 | } |
4861 | |
|
4862 | 0 | return LY_SUCCESS; |
4863 | 0 | } |
4864 | | |
4865 | | /** |
4866 | | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
4867 | | * with the number representation of either the argument or the context. |
4868 | | * |
4869 | | * @param[in] args Array of arguments. |
4870 | | * @param[in] arg_count Count of elements in @p args. |
4871 | | * @param[in,out] set Context and result set at the same time. |
4872 | | * @param[in] options XPath options. |
4873 | | * @return LY_ERR |
4874 | | */ |
4875 | | static LY_ERR |
4876 | | xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
4877 | 0 | { |
4878 | 0 | LY_ERR rc; |
4879 | |
|
4880 | 0 | if (options & LYXP_SCNODE_ALL) { |
4881 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4882 | 0 | return LY_SUCCESS; |
4883 | 0 | } |
4884 | | |
4885 | 0 | if (arg_count) { |
4886 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
4887 | 0 | LY_CHECK_RET(rc); |
4888 | 0 | set_fill_set(set, args[0]); |
4889 | 0 | } else { |
4890 | 0 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
4891 | 0 | LY_CHECK_RET(rc); |
4892 | 0 | } |
4893 | | |
4894 | 0 | return LY_SUCCESS; |
4895 | 0 | } |
4896 | | |
4897 | | /** |
4898 | | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
4899 | | * with the context position. |
4900 | | * |
4901 | | * @param[in] args Array of arguments. |
4902 | | * @param[in] arg_count Count of elements in @p args. |
4903 | | * @param[in,out] set Context and result set at the same time. |
4904 | | * @param[in] options XPath options. |
4905 | | * @return LY_ERR |
4906 | | */ |
4907 | | static LY_ERR |
4908 | | xpath_position(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4909 | 0 | { |
4910 | 0 | if (options & LYXP_SCNODE_ALL) { |
4911 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
4912 | 0 | return LY_SUCCESS; |
4913 | 0 | } |
4914 | | |
4915 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
4916 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
4917 | 0 | return LY_EVALID; |
4918 | 0 | } else if (!set->used) { |
4919 | 0 | set_fill_number(set, 0); |
4920 | 0 | return LY_SUCCESS; |
4921 | 0 | } |
4922 | | |
4923 | 0 | set_fill_number(set, set->ctx_pos); |
4924 | | |
4925 | | /* UNUSED in 'Release' build type */ |
4926 | 0 | (void)options; |
4927 | 0 | return LY_SUCCESS; |
4928 | 0 | } |
4929 | | |
4930 | | /** |
4931 | | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
4932 | | * depending on whether the second argument regex matches the first argument string. For details refer to |
4933 | | * YANG 1.1 RFC section 10.2.1. |
4934 | | * |
4935 | | * @param[in] args Array of arguments. |
4936 | | * @param[in] arg_count Count of elements in @p args. |
4937 | | * @param[in,out] set Context and result set at the same time. |
4938 | | * @param[in] options XPath options. |
4939 | | * @return LY_ERR |
4940 | | */ |
4941 | | static LY_ERR |
4942 | | xpath_re_match(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
4943 | 0 | { |
4944 | 0 | struct lysc_pattern **patterns = NULL, **pattern; |
4945 | 0 | struct lysc_node_leaf *sleaf; |
4946 | 0 | LY_ERR rc = LY_SUCCESS; |
4947 | 0 | struct ly_err_item *err; |
4948 | |
|
4949 | 0 | if (options & LYXP_SCNODE_ALL) { |
4950 | 0 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
4951 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
4952 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
4953 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
4954 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
4955 | 0 | } |
4956 | 0 | } |
4957 | |
|
4958 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
4959 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
4960 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
4961 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
4962 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
4963 | 0 | } |
4964 | 0 | } |
4965 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
4966 | 0 | return rc; |
4967 | 0 | } |
4968 | | |
4969 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
4970 | 0 | LY_CHECK_RET(rc); |
4971 | 0 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
4972 | 0 | LY_CHECK_RET(rc); |
4973 | |
|
4974 | 0 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
4975 | 0 | *pattern = calloc(1, sizeof **pattern); |
4976 | 0 | if (set->cur_node) { |
4977 | 0 | LOG_LOCSET(NULL, set->cur_node); |
4978 | 0 | } |
4979 | 0 | rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code); |
4980 | 0 | if (set->cur_node) { |
4981 | 0 | LOG_LOCBACK(0, 1); |
4982 | 0 | } |
4983 | 0 | if (rc != LY_SUCCESS) { |
4984 | 0 | free(*pattern); |
4985 | 0 | LY_ARRAY_FREE(patterns); |
4986 | 0 | return rc; |
4987 | 0 | } |
4988 | | |
4989 | 0 | rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
4990 | 0 | pcre2_code_free((*pattern)->code); |
4991 | 0 | free(*pattern); |
4992 | 0 | LY_ARRAY_FREE(patterns); |
4993 | 0 | if (rc && (rc != LY_EVALID)) { |
4994 | 0 | ly_err_print(set->ctx, err); |
4995 | 0 | ly_err_free(err); |
4996 | 0 | return rc; |
4997 | 0 | } |
4998 | | |
4999 | 0 | if (rc == LY_EVALID) { |
5000 | 0 | ly_err_free(err); |
5001 | 0 | set_fill_boolean(set, 0); |
5002 | 0 | } else { |
5003 | 0 | set_fill_boolean(set, 1); |
5004 | 0 | } |
5005 | |
|
5006 | 0 | return LY_SUCCESS; |
5007 | 0 | } |
5008 | | |
5009 | | /** |
5010 | | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
5011 | | * with the rounded first argument. For details refer to |
5012 | | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
5013 | | * |
5014 | | * @param[in] args Array of arguments. |
5015 | | * @param[in] arg_count Count of elements in @p args. |
5016 | | * @param[in,out] set Context and result set at the same time. |
5017 | | * @param[in] options XPath options. |
5018 | | * @return LY_ERR |
5019 | | */ |
5020 | | static LY_ERR |
5021 | | xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
5022 | 0 | { |
5023 | 0 | struct lysc_node_leaf *sleaf; |
5024 | 0 | LY_ERR rc = LY_SUCCESS; |
5025 | |
|
5026 | 0 | if (options & LYXP_SCNODE_ALL) { |
5027 | 0 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
5028 | 0 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
5029 | 0 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
5030 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5031 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
5032 | 0 | sleaf->name); |
5033 | 0 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
5034 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
5035 | 0 | } |
5036 | 0 | } |
5037 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5038 | 0 | return rc; |
5039 | 0 | } |
5040 | | |
5041 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
5042 | 0 | LY_CHECK_RET(rc); |
5043 | | |
5044 | | /* cover only the cases where floor can't be used */ |
5045 | 0 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
5046 | 0 | set_fill_number(set, -0.0f); |
5047 | 0 | } else { |
5048 | 0 | args[0]->val.num += 0.5; |
5049 | 0 | rc = xpath_floor(args, 1, args[0], options); |
5050 | 0 | LY_CHECK_RET(rc); |
5051 | 0 | set_fill_number(set, args[0]->val.num); |
5052 | 0 | } |
5053 | | |
5054 | 0 | return LY_SUCCESS; |
5055 | 0 | } |
5056 | | |
5057 | | /** |
5058 | | * @brief Execute the XPath starts-with(string, string) function. |
5059 | | * Returns LYXP_SET_BOOLEAN whether the second argument is |
5060 | | * the prefix of the first or not. |
5061 | | * |
5062 | | * @param[in] args Array of arguments. |
5063 | | * @param[in] arg_count Count of elements in @p args. |
5064 | | * @param[in,out] set Context and result set at the same time. |
5065 | | * @param[in] options XPath options. |
5066 | | * @return LY_ERR |
5067 | | */ |
5068 | | static LY_ERR |
5069 | | xpath_starts_with(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
5070 | 0 | { |
5071 | 0 | struct lysc_node_leaf *sleaf; |
5072 | 0 | LY_ERR rc = LY_SUCCESS; |
5073 | |
|
5074 | 0 | if (options & LYXP_SCNODE_ALL) { |
5075 | 0 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
5076 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5077 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5078 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5079 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5080 | 0 | } |
5081 | 0 | } |
5082 | |
|
5083 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
5084 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5085 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5086 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5087 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5088 | 0 | } |
5089 | 0 | } |
5090 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5091 | 0 | return rc; |
5092 | 0 | } |
5093 | | |
5094 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
5095 | 0 | LY_CHECK_RET(rc); |
5096 | 0 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
5097 | 0 | LY_CHECK_RET(rc); |
5098 | |
|
5099 | 0 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
5100 | 0 | set_fill_boolean(set, 0); |
5101 | 0 | } else { |
5102 | 0 | set_fill_boolean(set, 1); |
5103 | 0 | } |
5104 | |
|
5105 | 0 | return LY_SUCCESS; |
5106 | 0 | } |
5107 | | |
5108 | | /** |
5109 | | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
5110 | | * with the string representation of either the argument or the context. |
5111 | | * |
5112 | | * @param[in] args Array of arguments. |
5113 | | * @param[in] arg_count Count of elements in @p args. |
5114 | | * @param[in,out] set Context and result set at the same time. |
5115 | | * @param[in] options XPath options. |
5116 | | * @return LY_ERR |
5117 | | */ |
5118 | | static LY_ERR |
5119 | | xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
5120 | 0 | { |
5121 | 0 | LY_ERR rc; |
5122 | |
|
5123 | 0 | if (options & LYXP_SCNODE_ALL) { |
5124 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5125 | 0 | return LY_SUCCESS; |
5126 | 0 | } |
5127 | | |
5128 | 0 | if (arg_count) { |
5129 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
5130 | 0 | LY_CHECK_RET(rc); |
5131 | 0 | set_fill_set(set, args[0]); |
5132 | 0 | } else { |
5133 | 0 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
5134 | 0 | LY_CHECK_RET(rc); |
5135 | 0 | } |
5136 | | |
5137 | 0 | return LY_SUCCESS; |
5138 | 0 | } |
5139 | | |
5140 | | /** |
5141 | | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
5142 | | * with the length of the string in either the argument or the context. |
5143 | | * |
5144 | | * @param[in] args Array of arguments. |
5145 | | * @param[in] arg_count Count of elements in @p args. |
5146 | | * @param[in,out] set Context and result set at the same time. |
5147 | | * @param[in] options XPath options. |
5148 | | * @return LY_ERR |
5149 | | */ |
5150 | | static LY_ERR |
5151 | | xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
5152 | 0 | { |
5153 | 0 | struct lysc_node_leaf *sleaf; |
5154 | 0 | LY_ERR rc = LY_SUCCESS; |
5155 | |
|
5156 | 0 | if (options & LYXP_SCNODE_ALL) { |
5157 | 0 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
5158 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5159 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5160 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5161 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5162 | 0 | } |
5163 | 0 | } |
5164 | 0 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
5165 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5166 | 0 | LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5167 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5168 | 0 | LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5169 | 0 | } |
5170 | 0 | } |
5171 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5172 | 0 | return rc; |
5173 | 0 | } |
5174 | | |
5175 | 0 | if (arg_count) { |
5176 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
5177 | 0 | LY_CHECK_RET(rc); |
5178 | 0 | set_fill_number(set, strlen(args[0]->val.str)); |
5179 | 0 | } else { |
5180 | 0 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
5181 | 0 | LY_CHECK_RET(rc); |
5182 | 0 | set_fill_number(set, strlen(set->val.str)); |
5183 | 0 | } |
5184 | | |
5185 | 0 | return LY_SUCCESS; |
5186 | 0 | } |
5187 | | |
5188 | | /** |
5189 | | * @brief Execute the XPath substring(string, number, number?) function. |
5190 | | * Returns LYXP_SET_STRING substring of the first argument starting |
5191 | | * on the second argument index ending on the third argument index, |
5192 | | * indexed from 1. For exact definition refer to |
5193 | | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
5194 | | * |
5195 | | * @param[in] args Array of arguments. |
5196 | | * @param[in] arg_count Count of elements in @p args. |
5197 | | * @param[in,out] set Context and result set at the same time. |
5198 | | * @param[in] options XPath options. |
5199 | | * @return LY_ERR |
5200 | | */ |
5201 | | static LY_ERR |
5202 | | xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
5203 | 0 | { |
5204 | 0 | int64_t start; |
5205 | 0 | int32_t len; |
5206 | 0 | uint32_t str_start, str_len, pos; |
5207 | 0 | struct lysc_node_leaf *sleaf; |
5208 | 0 | LY_ERR rc = LY_SUCCESS; |
5209 | |
|
5210 | 0 | if (options & LYXP_SCNODE_ALL) { |
5211 | 0 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
5212 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5213 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5214 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5215 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5216 | 0 | } |
5217 | 0 | } |
5218 | |
|
5219 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
5220 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5221 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5222 | 0 | } else if (!warn_is_numeric_type(sleaf->type)) { |
5223 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
5224 | 0 | } |
5225 | 0 | } |
5226 | |
|
5227 | 0 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
5228 | 0 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
5229 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5230 | 0 | LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5231 | 0 | } else if (!warn_is_numeric_type(sleaf->type)) { |
5232 | 0 | LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
5233 | 0 | } |
5234 | 0 | } |
5235 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5236 | 0 | return rc; |
5237 | 0 | } |
5238 | | |
5239 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
5240 | 0 | LY_CHECK_RET(rc); |
5241 | | |
5242 | | /* start */ |
5243 | 0 | if (xpath_round(&args[1], 1, args[1], options)) { |
5244 | 0 | return -1; |
5245 | 0 | } |
5246 | 0 | if (isfinite(args[1]->val.num)) { |
5247 | 0 | start = args[1]->val.num - 1; |
5248 | 0 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
5249 | 0 | start = INT32_MIN; |
5250 | 0 | } else { |
5251 | 0 | start = INT32_MAX; |
5252 | 0 | } |
5253 | | |
5254 | | /* len */ |
5255 | 0 | if (arg_count == 3) { |
5256 | 0 | rc = xpath_round(&args[2], 1, args[2], options); |
5257 | 0 | LY_CHECK_RET(rc); |
5258 | 0 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
5259 | 0 | len = 0; |
5260 | 0 | } else if (isfinite(args[2]->val.num)) { |
5261 | 0 | len = args[2]->val.num; |
5262 | 0 | } else { |
5263 | 0 | len = INT32_MAX; |
5264 | 0 | } |
5265 | 0 | } else { |
5266 | 0 | len = INT32_MAX; |
5267 | 0 | } |
5268 | | |
5269 | | /* find matching character positions */ |
5270 | 0 | str_start = 0; |
5271 | 0 | str_len = 0; |
5272 | 0 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
5273 | 0 | if (pos < start) { |
5274 | 0 | ++str_start; |
5275 | 0 | } else if (pos < start + len) { |
5276 | 0 | ++str_len; |
5277 | 0 | } else { |
5278 | 0 | break; |
5279 | 0 | } |
5280 | 0 | } |
5281 | |
|
5282 | 0 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
5283 | 0 | return LY_SUCCESS; |
5284 | 0 | } |
5285 | | |
5286 | | /** |
5287 | | * @brief Execute the XPath substring-after(string, string) function. |
5288 | | * Returns LYXP_SET_STRING with the string succeeding the occurance |
5289 | | * of the second argument in the first or an empty string. |
5290 | | * |
5291 | | * @param[in] args Array of arguments. |
5292 | | * @param[in] arg_count Count of elements in @p args. |
5293 | | * @param[in,out] set Context and result set at the same time. |
5294 | | * @param[in] options XPath options. |
5295 | | * @return LY_ERR |
5296 | | */ |
5297 | | static LY_ERR |
5298 | | xpath_substring_after(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
5299 | 0 | { |
5300 | 0 | char *ptr; |
5301 | 0 | struct lysc_node_leaf *sleaf; |
5302 | 0 | LY_ERR rc = LY_SUCCESS; |
5303 | |
|
5304 | 0 | if (options & LYXP_SCNODE_ALL) { |
5305 | 0 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
5306 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5307 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5308 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5309 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5310 | 0 | } |
5311 | 0 | } |
5312 | |
|
5313 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
5314 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5315 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5316 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5317 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5318 | 0 | } |
5319 | 0 | } |
5320 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5321 | 0 | return rc; |
5322 | 0 | } |
5323 | | |
5324 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
5325 | 0 | LY_CHECK_RET(rc); |
5326 | 0 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
5327 | 0 | LY_CHECK_RET(rc); |
5328 | |
|
5329 | 0 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
5330 | 0 | if (ptr) { |
5331 | 0 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
5332 | 0 | } else { |
5333 | 0 | set_fill_string(set, "", 0); |
5334 | 0 | } |
5335 | |
|
5336 | 0 | return LY_SUCCESS; |
5337 | 0 | } |
5338 | | |
5339 | | /** |
5340 | | * @brief Execute the XPath substring-before(string, string) function. |
5341 | | * Returns LYXP_SET_STRING with the string preceding the occurance |
5342 | | * of the second argument in the first or an empty string. |
5343 | | * |
5344 | | * @param[in] args Array of arguments. |
5345 | | * @param[in] arg_count Count of elements in @p args. |
5346 | | * @param[in,out] set Context and result set at the same time. |
5347 | | * @param[in] options XPath options. |
5348 | | * @return LY_ERR |
5349 | | */ |
5350 | | static LY_ERR |
5351 | | xpath_substring_before(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
5352 | 0 | { |
5353 | 0 | char *ptr; |
5354 | 0 | struct lysc_node_leaf *sleaf; |
5355 | 0 | LY_ERR rc = LY_SUCCESS; |
5356 | |
|
5357 | 0 | if (options & LYXP_SCNODE_ALL) { |
5358 | 0 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
5359 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5360 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5361 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5362 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5363 | 0 | } |
5364 | 0 | } |
5365 | |
|
5366 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
5367 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5368 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5369 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5370 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5371 | 0 | } |
5372 | 0 | } |
5373 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5374 | 0 | return rc; |
5375 | 0 | } |
5376 | | |
5377 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
5378 | 0 | LY_CHECK_RET(rc); |
5379 | 0 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
5380 | 0 | LY_CHECK_RET(rc); |
5381 | |
|
5382 | 0 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
5383 | 0 | if (ptr) { |
5384 | 0 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
5385 | 0 | } else { |
5386 | 0 | set_fill_string(set, "", 0); |
5387 | 0 | } |
5388 | |
|
5389 | 0 | return LY_SUCCESS; |
5390 | 0 | } |
5391 | | |
5392 | | /** |
5393 | | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
5394 | | * with the sum of all the nodes in the context. |
5395 | | * |
5396 | | * @param[in] args Array of arguments. |
5397 | | * @param[in] arg_count Count of elements in @p args. |
5398 | | * @param[in,out] set Context and result set at the same time. |
5399 | | * @param[in] options XPath options. |
5400 | | * @return LY_ERR |
5401 | | */ |
5402 | | static LY_ERR |
5403 | | xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
5404 | 0 | { |
5405 | 0 | long double num; |
5406 | 0 | char *str; |
5407 | 0 | uint32_t i; |
5408 | 0 | struct lyxp_set set_item; |
5409 | 0 | struct lysc_node_leaf *sleaf; |
5410 | 0 | LY_ERR rc = LY_SUCCESS; |
5411 | |
|
5412 | 0 | if (options & LYXP_SCNODE_ALL) { |
5413 | 0 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
5414 | 0 | for (i = 0; i < args[0]->used; ++i) { |
5415 | 0 | if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
5416 | 0 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
5417 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5418 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
5419 | 0 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5420 | 0 | } else if (!warn_is_numeric_type(sleaf->type)) { |
5421 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
5422 | 0 | } |
5423 | 0 | } |
5424 | 0 | } |
5425 | 0 | } |
5426 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5427 | 0 | return rc; |
5428 | 0 | } |
5429 | | |
5430 | 0 | set_fill_number(set, 0); |
5431 | |
|
5432 | 0 | if (args[0]->type != LYXP_SET_NODE_SET) { |
5433 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
5434 | 0 | return LY_EVALID; |
5435 | 0 | } else if (!args[0]->used) { |
5436 | 0 | return LY_SUCCESS; |
5437 | 0 | } |
5438 | | |
5439 | 0 | set_init(&set_item, set); |
5440 | |
|
5441 | 0 | set_item.type = LYXP_SET_NODE_SET; |
5442 | 0 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
5443 | 0 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
5444 | |
|
5445 | 0 | set_item.used = 1; |
5446 | 0 | set_item.size = 1; |
5447 | |
|
5448 | 0 | for (i = 0; i < args[0]->used; ++i) { |
5449 | 0 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
5450 | |
|
5451 | 0 | rc = cast_node_set_to_string(&set_item, &str); |
5452 | 0 | LY_CHECK_RET(rc); |
5453 | 0 | num = cast_string_to_number(str); |
5454 | 0 | free(str); |
5455 | 0 | set->val.num += num; |
5456 | 0 | } |
5457 | | |
5458 | 0 | free(set_item.val.nodes); |
5459 | |
|
5460 | 0 | return LY_SUCCESS; |
5461 | 0 | } |
5462 | | |
5463 | | /** |
5464 | | * @brief Execute the XPath translate(string, string, string) function. |
5465 | | * Returns LYXP_SET_STRING with the first argument with the characters |
5466 | | * from the second argument replaced by those on the corresponding |
5467 | | * positions in the third argument. |
5468 | | * |
5469 | | * @param[in] args Array of arguments. |
5470 | | * @param[in] arg_count Count of elements in @p args. |
5471 | | * @param[in,out] set Context and result set at the same time. |
5472 | | * @param[in] options XPath options. |
5473 | | * @return LY_ERR |
5474 | | */ |
5475 | | static LY_ERR |
5476 | | xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
5477 | 0 | { |
5478 | 0 | uint32_t i, j, new_used; |
5479 | 0 | char *new; |
5480 | 0 | ly_bool have_removed; |
5481 | 0 | struct lysc_node_leaf *sleaf; |
5482 | 0 | LY_ERR rc = LY_SUCCESS; |
5483 | |
|
5484 | 0 | if (options & LYXP_SCNODE_ALL) { |
5485 | 0 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
5486 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5487 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5488 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5489 | 0 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5490 | 0 | } |
5491 | 0 | } |
5492 | |
|
5493 | 0 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
5494 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5495 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5496 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5497 | 0 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5498 | 0 | } |
5499 | 0 | } |
5500 | |
|
5501 | 0 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
5502 | 0 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5503 | 0 | LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
5504 | 0 | } else if (!warn_is_string_type(sleaf->type)) { |
5505 | 0 | LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
5506 | 0 | } |
5507 | 0 | } |
5508 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5509 | 0 | return rc; |
5510 | 0 | } |
5511 | | |
5512 | 0 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
5513 | 0 | LY_CHECK_RET(rc); |
5514 | 0 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
5515 | 0 | LY_CHECK_RET(rc); |
5516 | 0 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
5517 | 0 | LY_CHECK_RET(rc); |
5518 | |
|
5519 | 0 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
5520 | 0 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
5521 | 0 | new_used = 0; |
5522 | |
|
5523 | 0 | have_removed = 0; |
5524 | 0 | for (i = 0; args[0]->val.str[i]; ++i) { |
5525 | 0 | ly_bool found = 0; |
5526 | |
|
5527 | 0 | for (j = 0; args[1]->val.str[j]; ++j) { |
5528 | 0 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
5529 | | /* removing this char */ |
5530 | 0 | if (j >= strlen(args[2]->val.str)) { |
5531 | 0 | have_removed = 1; |
5532 | 0 | found = 1; |
5533 | 0 | break; |
5534 | 0 | } |
5535 | | /* replacing this char */ |
5536 | 0 | new[new_used] = args[2]->val.str[j]; |
5537 | 0 | ++new_used; |
5538 | 0 | found = 1; |
5539 | 0 | break; |
5540 | 0 | } |
5541 | 0 | } |
5542 | | |
5543 | | /* copying this char */ |
5544 | 0 | if (!found) { |
5545 | 0 | new[new_used] = args[0]->val.str[i]; |
5546 | 0 | ++new_used; |
5547 | 0 | } |
5548 | 0 | } |
5549 | |
|
5550 | 0 | if (have_removed) { |
5551 | 0 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
5552 | 0 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
5553 | 0 | } |
5554 | 0 | new[new_used] = '\0'; |
5555 | |
|
5556 | 0 | lyxp_set_free_content(set); |
5557 | 0 | set->type = LYXP_SET_STRING; |
5558 | 0 | set->val.str = new; |
5559 | |
|
5560 | 0 | return LY_SUCCESS; |
5561 | 0 | } |
5562 | | |
5563 | | /** |
5564 | | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
5565 | | * with true value. |
5566 | | * |
5567 | | * @param[in] args Array of arguments. |
5568 | | * @param[in] arg_count Count of elements in @p args. |
5569 | | * @param[in,out] set Context and result set at the same time. |
5570 | | * @param[in] options XPath options. |
5571 | | * @return LY_ERR |
5572 | | */ |
5573 | | static LY_ERR |
5574 | | xpath_true(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
5575 | 0 | { |
5576 | 0 | if (options & LYXP_SCNODE_ALL) { |
5577 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
5578 | 0 | return LY_SUCCESS; |
5579 | 0 | } |
5580 | | |
5581 | 0 | set_fill_boolean(set, 1); |
5582 | 0 | return LY_SUCCESS; |
5583 | 0 | } |
5584 | | |
5585 | | /** |
5586 | | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
5587 | | * with only nodes from the context. |
5588 | | * |
5589 | | * @param[in,out] set Context and result set at the same time. |
5590 | | * @param[in] axis Axis to search on. |
5591 | | * @param[in] options XPath options. |
5592 | | * @return LY_ERR |
5593 | | */ |
5594 | | static LY_ERR |
5595 | | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
5596 | 2.84k | { |
5597 | 2.84k | if (options & LYXP_SCNODE_ALL) { |
5598 | 171 | return moveto_scnode(set, NULL, NULL, axis, options); |
5599 | 171 | } |
5600 | | |
5601 | 2.67k | if (set->type != LYXP_SET_NODE_SET) { |
5602 | 0 | lyxp_set_free_content(set); |
5603 | 0 | return LY_SUCCESS; |
5604 | 0 | } |
5605 | | |
5606 | | /* just like moving to a node with no restrictions */ |
5607 | 2.67k | return moveto_node(set, NULL, NULL, axis, options); |
5608 | 2.67k | } |
5609 | | |
5610 | | /** |
5611 | | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
5612 | | * with the text content of the nodes in the context. |
5613 | | * |
5614 | | * @param[in,out] set Context and result set at the same time. |
5615 | | * @param[in] axis Axis to search on. |
5616 | | * @param[in] options XPath options. |
5617 | | * @return LY_ERR |
5618 | | */ |
5619 | | static LY_ERR |
5620 | | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
5621 | 0 | { |
5622 | 0 | uint32_t i; |
5623 | |
|
5624 | 0 | if (options & LYXP_SCNODE_ALL) { |
5625 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
5626 | 0 | return LY_SUCCESS; |
5627 | 0 | } |
5628 | | |
5629 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
5630 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
5631 | 0 | return LY_EVALID; |
5632 | 0 | } |
5633 | | |
5634 | 0 | if (axis != LYXP_AXIS_CHILD) { |
5635 | | /* even following and preceding axescan return text nodes, but whatever */ |
5636 | 0 | lyxp_set_free_content(set); |
5637 | 0 | return LY_SUCCESS; |
5638 | 0 | } |
5639 | | |
5640 | 0 | for (i = 0; i < set->used; ++i) { |
5641 | 0 | switch (set->val.nodes[i].type) { |
5642 | 0 | case LYXP_NODE_NONE: |
5643 | 0 | LOGINT_RET(set->ctx); |
5644 | 0 | case LYXP_NODE_ELEM: |
5645 | 0 | if (!set->val.nodes[i].node->schema || (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
5646 | 0 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
5647 | 0 | break; |
5648 | 0 | } |
5649 | | /* fall through */ |
5650 | 0 | case LYXP_NODE_ROOT: |
5651 | 0 | case LYXP_NODE_ROOT_CONFIG: |
5652 | 0 | case LYXP_NODE_TEXT: |
5653 | 0 | case LYXP_NODE_META: |
5654 | 0 | set_remove_node_none(set, i); |
5655 | 0 | break; |
5656 | 0 | } |
5657 | 0 | } |
5658 | 0 | set_remove_nodes_none(set); |
5659 | |
|
5660 | 0 | return LY_SUCCESS; |
5661 | 0 | } |
5662 | | |
5663 | | /** |
5664 | | * @brief Skip prefix and return corresponding module. Logs directly. |
5665 | | * |
5666 | | * XPath @p set is expected to be a (sc)node set! |
5667 | | * |
5668 | | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
5669 | | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
5670 | | * @param[in] set Set with general XPath context. |
5671 | | * @param[in] ctx_scnode Current context schema node (parent). |
5672 | | * @param[out] moveto_mod Expected module of a matching node. |
5673 | | * @return LY_ERR |
5674 | | */ |
5675 | | static LY_ERR |
5676 | | moveto_resolve_module(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
5677 | | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
5678 | 870 | { |
5679 | 870 | const struct lys_module *mod = NULL; |
5680 | 870 | const char *ptr; |
5681 | 870 | size_t pref_len; |
5682 | | |
5683 | 870 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
5684 | | |
5685 | 870 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
5686 | | /* specific module */ |
5687 | 0 | pref_len = ptr - *qname; |
5688 | 0 | mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data); |
5689 | | |
5690 | | /* check for errors and non-implemented modules, as they are not valid */ |
5691 | 0 | if (!mod || !mod->implemented) { |
5692 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, (int)pref_len, *qname); |
5693 | 0 | return LY_EVALID; |
5694 | 0 | } |
5695 | | |
5696 | 0 | *qname += pref_len + 1; |
5697 | 0 | *qname_len -= pref_len + 1; |
5698 | 870 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
5699 | | /* all modules - special case */ |
5700 | 0 | mod = NULL; |
5701 | 870 | } else { |
5702 | 870 | switch (set->format) { |
5703 | 0 | case LY_VALUE_SCHEMA: |
5704 | 870 | case LY_VALUE_SCHEMA_RESOLVED: |
5705 | | /* current module */ |
5706 | 870 | mod = set->cur_mod; |
5707 | 870 | break; |
5708 | 0 | case LY_VALUE_CANON: |
5709 | 0 | case LY_VALUE_JSON: |
5710 | 0 | case LY_VALUE_LYB: |
5711 | 0 | case LY_VALUE_STR_NS: |
5712 | | /* inherit parent (context node) module */ |
5713 | 0 | if (ctx_scnode) { |
5714 | 0 | mod = ctx_scnode->module; |
5715 | 0 | } else { |
5716 | | /* JSON XPath is our own format (except for identityref), which supports node names matching all the modules */ |
5717 | 0 | mod = NULL; |
5718 | 0 | } |
5719 | 0 | break; |
5720 | 0 | case LY_VALUE_XML: |
5721 | | /* all nodes need to be prefixed */ |
5722 | 0 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", (int)*qname_len, *qname); |
5723 | 0 | return LY_EVALID; |
5724 | 870 | } |
5725 | 870 | } |
5726 | | |
5727 | 870 | *moveto_mod = mod; |
5728 | 870 | return LY_SUCCESS; |
5729 | 870 | } |
5730 | | |
5731 | | /** |
5732 | | * @brief Move context @p set to the root. Handles absolute path. |
5733 | | * Result is LYXP_SET_NODE_SET. |
5734 | | * |
5735 | | * @param[in,out] set Set to use. |
5736 | | * @param[in] options Xpath options. |
5737 | | * @return LY_ERR value. |
5738 | | */ |
5739 | | static LY_ERR |
5740 | | moveto_root(struct lyxp_set *set, uint32_t options) |
5741 | 30 | { |
5742 | 30 | assert(!(options & LYXP_SKIP_EXPR)); |
5743 | | |
5744 | 30 | if (options & LYXP_SCNODE_ALL) { |
5745 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
5746 | 0 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL)); |
5747 | 30 | } else { |
5748 | 30 | lyxp_set_free_content(set); |
5749 | 30 | set_insert_node(set, NULL, 0, set->root_type, 0); |
5750 | 30 | set->non_child_axis = 0; |
5751 | 30 | } |
5752 | | |
5753 | 30 | return LY_SUCCESS; |
5754 | 30 | } |
5755 | | |
5756 | | /** |
5757 | | * @brief Check @p node as a part of NameTest processing. |
5758 | | * |
5759 | | * @param[in] node Node to check. |
5760 | | * @param[in] node_type Node type of @p node. |
5761 | | * @param[in] set Set to read general context from. |
5762 | | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
5763 | | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
5764 | | * @param[in] options XPath options. |
5765 | | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
5766 | | * LY_EINVAL if neither node nor any children match) |
5767 | | */ |
5768 | | static LY_ERR |
5769 | | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
5770 | | const char *node_name, const struct lys_module *moveto_mod, uint32_t options) |
5771 | 5.37k | { |
5772 | 5.37k | const struct lysc_node *schema; |
5773 | | |
5774 | 5.37k | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
5775 | 327 | assert(node_type == set->root_type); |
5776 | | |
5777 | 327 | if (node_name || moveto_mod) { |
5778 | | /* root will not match a specific node */ |
5779 | 0 | return LY_ENOT; |
5780 | 0 | } |
5781 | 327 | return LY_SUCCESS; |
5782 | 5.04k | } else if (node_type != LYXP_NODE_ELEM) { |
5783 | | /* other types will not match */ |
5784 | 0 | return LY_ENOT; |
5785 | 0 | } |
5786 | | |
5787 | | /* get schema node even of an opaque node */ |
5788 | 5.04k | schema = lyd_node_schema(node); |
5789 | 5.04k | if (!schema) { |
5790 | | /* unknown opaque node never matches */ |
5791 | 0 | return LY_ENOT; |
5792 | 0 | } |
5793 | | |
5794 | | /* module check */ |
5795 | 5.04k | if (moveto_mod) { |
5796 | 2.69k | if ((set->ctx == LYD_CTX(node)) && (schema->module != moveto_mod)) { |
5797 | 0 | return LY_ENOT; |
5798 | 2.69k | } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->module->name, moveto_mod->name)) { |
5799 | 0 | return LY_ENOT; |
5800 | 0 | } |
5801 | 2.69k | } |
5802 | | |
5803 | | /* context check */ |
5804 | 5.04k | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (schema->flags & LYS_CONFIG_R)) { |
5805 | 0 | return LY_EINVAL; |
5806 | 5.04k | } else if (set->context_op && (schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (schema != set->context_op)) { |
5807 | 0 | return LY_EINVAL; |
5808 | 0 | } |
5809 | | |
5810 | | /* name check */ |
5811 | 5.04k | if (node_name) { |
5812 | 2.69k | if ((set->ctx == LYD_CTX(node)) && (schema->name != node_name)) { |
5813 | 353 | return LY_ENOT; |
5814 | 2.34k | } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->name, node_name)) { |
5815 | 0 | return LY_ENOT; |
5816 | 0 | } |
5817 | 2.69k | } |
5818 | | |
5819 | | /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */ |
5820 | 4.69k | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(schema) && !(node->flags & LYD_WHEN_TRUE) && |
5821 | 4.69k | (node != set->cur_node)) { |
5822 | 0 | return LY_EINCOMPLETE; |
5823 | 0 | } |
5824 | | |
5825 | | /* match */ |
5826 | 4.69k | return LY_SUCCESS; |
5827 | 4.69k | } |
5828 | | |
5829 | | /** |
5830 | | * @brief Get the next node in a forward DFS. |
5831 | | * |
5832 | | * @param[in] iter Last returned node. |
5833 | | * @param[in] stop Node to stop the search on and not return. |
5834 | | * @return Next node, NULL if there are no more. |
5835 | | */ |
5836 | | static const struct lyd_node * |
5837 | | moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop) |
5838 | 0 | { |
5839 | 0 | const struct lyd_node *next = NULL; |
5840 | | |
5841 | | /* 1) child */ |
5842 | 0 | next = lyd_child(iter); |
5843 | 0 | if (!next) { |
5844 | 0 | if (iter == stop) { |
5845 | | /* reached stop, no more descendants */ |
5846 | 0 | return NULL; |
5847 | 0 | } |
5848 | | /* 2) child next sibling */ |
5849 | 0 | next = iter->next; |
5850 | 0 | } |
5851 | 0 | while (!next) { |
5852 | 0 | iter = lyd_parent(iter); |
5853 | 0 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
5854 | 0 | return NULL; |
5855 | 0 | } |
5856 | 0 | next = iter->next; |
5857 | 0 | } |
5858 | | |
5859 | 0 | return next; |
5860 | 0 | } |
5861 | | |
5862 | | /** |
5863 | | * @brief Get the next node in a backward DFS. |
5864 | | * |
5865 | | * @param[in] iter Last returned node. |
5866 | | * @param[in] stop Node to stop the search on and not return. |
5867 | | * @return Next node, NULL if there are no more. |
5868 | | */ |
5869 | | static const struct lyd_node * |
5870 | | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
5871 | 0 | { |
5872 | 0 | const struct lyd_node *next = NULL; |
5873 | | |
5874 | | /* 1) previous sibling innermost last child */ |
5875 | 0 | next = iter->prev->next ? iter->prev : NULL; |
5876 | 0 | while (next && lyd_child(next)) { |
5877 | 0 | next = lyd_child(next); |
5878 | 0 | next = next->prev; |
5879 | 0 | } |
5880 | |
|
5881 | 0 | if (!next) { |
5882 | | /* 2) parent */ |
5883 | 0 | iter = lyd_parent(iter); |
5884 | 0 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
5885 | 0 | return NULL; |
5886 | 0 | } |
5887 | 0 | next = iter; |
5888 | 0 | } |
5889 | | |
5890 | 0 | return next; |
5891 | 0 | } |
5892 | | |
5893 | | /** |
5894 | | * @brief Get the first node on an axis for a context node. |
5895 | | * |
5896 | | * @param[in,out] iter NULL, updated to the next node. |
5897 | | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
5898 | | * @param[in] node Context node. |
5899 | | * @param[in] node_type Type of @p node. |
5900 | | * @param[in] axis Axis to use. |
5901 | | * @param[in] set XPath set with the general context. |
5902 | | * @return LY_SUCCESS on success. |
5903 | | * @return LY_ENOTFOUND if no next node found. |
5904 | | */ |
5905 | | static LY_ERR |
5906 | | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
5907 | | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
5908 | 2.85k | { |
5909 | 2.85k | const struct lyd_node *next = NULL; |
5910 | 2.85k | enum lyxp_node_type next_type = 0; |
5911 | | |
5912 | 2.85k | assert(!*iter); |
5913 | 2.85k | assert(!*iter_type); |
5914 | | |
5915 | 2.85k | switch (axis) { |
5916 | 0 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
5917 | 0 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
5918 | 2.34k | case LYXP_AXIS_SELF: |
5919 | | /* return the context node */ |
5920 | 2.34k | next = node; |
5921 | 2.34k | next_type = node_type; |
5922 | 2.34k | break; |
5923 | | |
5924 | 0 | case LYXP_AXIS_ANCESTOR: |
5925 | 327 | case LYXP_AXIS_PARENT: |
5926 | 327 | if (node_type == LYXP_NODE_ELEM) { |
5927 | 327 | next = lyd_parent(node); |
5928 | 327 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
5929 | 327 | } else if (node_type == LYXP_NODE_TEXT) { |
5930 | 0 | next = node; |
5931 | 0 | next_type = LYXP_NODE_ELEM; |
5932 | 0 | } else if (node_type == LYXP_NODE_META) { |
5933 | 0 | next = ((struct lyd_meta *)node)->parent; |
5934 | 0 | next_type = LYXP_NODE_ELEM; |
5935 | 0 | } /* else root does not have a parent */ |
5936 | 327 | break; |
5937 | | |
5938 | 178 | case LYXP_AXIS_CHILD: |
5939 | 178 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
5940 | 45 | assert(!node); |
5941 | | |
5942 | | /* search in all the trees */ |
5943 | 45 | next = set->tree; |
5944 | 45 | next_type = next ? LYXP_NODE_ELEM : 0; |
5945 | 133 | } else { |
5946 | | /* search in children */ |
5947 | 133 | next = lyd_child(node); |
5948 | 133 | next_type = next ? LYXP_NODE_ELEM : 0; |
5949 | 133 | } |
5950 | 178 | break; |
5951 | | |
5952 | 178 | case LYXP_AXIS_DESCENDANT: |
5953 | 0 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
5954 | | /* top-level nodes */ |
5955 | 0 | next = set->tree; |
5956 | 0 | next_type = LYXP_NODE_ELEM; |
5957 | 0 | } else if (node_type == LYXP_NODE_ELEM) { |
5958 | | /* start from the context node */ |
5959 | 0 | next = moveto_axis_node_next_dfs_forward(node, node); |
5960 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
5961 | 0 | } /* else no children */ |
5962 | 0 | break; |
5963 | | |
5964 | 0 | case LYXP_AXIS_FOLLOWING: |
5965 | 0 | case LYXP_AXIS_FOLLOWING_SIBLING: |
5966 | 0 | if (node_type == LYXP_NODE_ELEM) { |
5967 | | /* first next sibling */ |
5968 | 0 | next = node->next; |
5969 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
5970 | 0 | } /* else no sibling */ |
5971 | 0 | break; |
5972 | | |
5973 | 0 | case LYXP_AXIS_PRECEDING: |
5974 | 0 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
5975 | | /* skip ancestors */ |
5976 | 0 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
5977 | 0 | assert(next); |
5978 | 0 | next_type = LYXP_NODE_ELEM; |
5979 | 0 | } /* else no sibling */ |
5980 | 0 | break; |
5981 | | |
5982 | 0 | case LYXP_AXIS_PRECEDING_SIBLING: |
5983 | 0 | if (node_type == LYXP_NODE_ELEM) { |
5984 | | /* first previous sibling */ |
5985 | 0 | next = node->prev->next ? node->prev : NULL; |
5986 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
5987 | 0 | } /* else no sibling */ |
5988 | 0 | break; |
5989 | | |
5990 | 0 | case LYXP_AXIS_ATTRIBUTE: |
5991 | | /* handled specially */ |
5992 | 0 | assert(0); |
5993 | 0 | LOGINT(set->ctx); |
5994 | 0 | break; |
5995 | 2.85k | } |
5996 | | |
5997 | 2.85k | *iter = next; |
5998 | 2.85k | *iter_type = next_type; |
5999 | 2.85k | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
6000 | 2.85k | } |
6001 | | |
6002 | | /** |
6003 | | * @brief Iterate over all nodes on an axis for a context node. |
6004 | | * |
6005 | | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
6006 | | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
6007 | | * @param[in] node Context node. |
6008 | | * @param[in] node_type Type of @p node. |
6009 | | * @param[in] axis Axis to use. |
6010 | | * @param[in] set XPath set with the general context. |
6011 | | * @return LY_SUCCESS on success. |
6012 | | * @return LY_ENOTFOUND if no next node found. |
6013 | | */ |
6014 | | static LY_ERR |
6015 | | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
6016 | | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
6017 | 8.22k | { |
6018 | 8.22k | const struct lyd_node *next = NULL; |
6019 | 8.22k | enum lyxp_node_type next_type = 0; |
6020 | | |
6021 | 8.22k | if (!*iter_type) { |
6022 | | /* first returned node */ |
6023 | 2.85k | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
6024 | 2.85k | } |
6025 | | |
6026 | 5.37k | switch (axis) { |
6027 | 0 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
6028 | 0 | if ((*iter == node) && (*iter_type == node_type)) { |
6029 | | /* fake first ancestor, we returned self before */ |
6030 | 0 | *iter = NULL; |
6031 | 0 | *iter_type = 0; |
6032 | 0 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
6033 | 0 | } /* else continue ancestor */ |
6034 | | |
6035 | | /* fallthrough */ |
6036 | 0 | case LYXP_AXIS_ANCESTOR: |
6037 | 0 | if (*iter_type == LYXP_NODE_ELEM) { |
6038 | | /* iter parent */ |
6039 | 0 | next = lyd_parent(*iter); |
6040 | 0 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
6041 | 0 | } /* else root, no ancestors */ |
6042 | 0 | break; |
6043 | | |
6044 | 2.69k | case LYXP_AXIS_CHILD: |
6045 | 2.69k | assert(*iter_type == LYXP_NODE_ELEM); |
6046 | | |
6047 | | /* next sibling (child) */ |
6048 | 2.69k | next = (*iter)->next; |
6049 | 2.69k | next_type = next ? LYXP_NODE_ELEM : 0; |
6050 | 2.69k | break; |
6051 | | |
6052 | 0 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
6053 | 0 | if ((*iter == node) && (*iter_type == node_type)) { |
6054 | | /* fake first descendant, we returned self before */ |
6055 | 0 | *iter = NULL; |
6056 | 0 | *iter_type = 0; |
6057 | 0 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
6058 | 0 | } /* else continue descendant */ |
6059 | | |
6060 | | /* fallthrough */ |
6061 | 0 | case LYXP_AXIS_DESCENDANT: |
6062 | 0 | assert(*iter_type == LYXP_NODE_ELEM); |
6063 | 0 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
6064 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6065 | 0 | break; |
6066 | | |
6067 | 0 | case LYXP_AXIS_FOLLOWING: |
6068 | 0 | assert(*iter_type == LYXP_NODE_ELEM); |
6069 | 0 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
6070 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6071 | 0 | break; |
6072 | | |
6073 | 0 | case LYXP_AXIS_FOLLOWING_SIBLING: |
6074 | 0 | assert(*iter_type == LYXP_NODE_ELEM); |
6075 | | |
6076 | | /* next sibling */ |
6077 | 0 | next = (*iter)->next; |
6078 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6079 | 0 | break; |
6080 | | |
6081 | 327 | case LYXP_AXIS_PARENT: |
6082 | 2.67k | case LYXP_AXIS_SELF: |
6083 | | /* parent/self was returned before */ |
6084 | 2.67k | break; |
6085 | | |
6086 | 0 | case LYXP_AXIS_PRECEDING: |
6087 | 0 | assert(*iter_type == LYXP_NODE_ELEM); |
6088 | 0 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
6089 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6090 | 0 | break; |
6091 | | |
6092 | 0 | case LYXP_AXIS_PRECEDING_SIBLING: |
6093 | 0 | assert(*iter_type == LYXP_NODE_ELEM); |
6094 | | |
6095 | | /* previous sibling */ |
6096 | 0 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
6097 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6098 | 0 | break; |
6099 | | |
6100 | 0 | case LYXP_AXIS_ATTRIBUTE: |
6101 | | /* handled specially */ |
6102 | 0 | assert(0); |
6103 | 0 | LOGINT(set->ctx); |
6104 | 0 | break; |
6105 | 5.37k | } |
6106 | | |
6107 | 5.37k | *iter = next; |
6108 | 5.37k | *iter_type = next_type; |
6109 | 5.37k | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
6110 | 5.37k | } |
6111 | | |
6112 | | /** |
6113 | | * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware. |
6114 | | * |
6115 | | * @param[in,out] set Set to use. |
6116 | | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
6117 | | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
6118 | | * @param[in] axis Axis to search on. |
6119 | | * @param[in] options XPath options. |
6120 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
6121 | | */ |
6122 | | static LY_ERR |
6123 | | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
6124 | | uint32_t options) |
6125 | 2.91k | { |
6126 | 2.91k | LY_ERR r, rc = LY_SUCCESS; |
6127 | 2.91k | const struct lyd_node *iter; |
6128 | 2.91k | enum lyxp_node_type iter_type; |
6129 | 2.91k | struct lyxp_set result; |
6130 | 2.91k | uint32_t i; |
6131 | | |
6132 | 2.91k | if (options & LYXP_SKIP_EXPR) { |
6133 | 30 | return LY_SUCCESS; |
6134 | 30 | } |
6135 | | |
6136 | 2.88k | if (set->type != LYXP_SET_NODE_SET) { |
6137 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
6138 | 0 | return LY_EVALID; |
6139 | 0 | } |
6140 | | |
6141 | | /* init result set */ |
6142 | 2.88k | set_init(&result, set); |
6143 | | |
6144 | 5.74k | for (i = 0; i < set->used; ++i) { |
6145 | | /* iterate over all the nodes on the axis of the node */ |
6146 | 2.85k | iter = NULL; |
6147 | 2.85k | iter_type = 0; |
6148 | 8.22k | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
6149 | 5.37k | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
6150 | 5.37k | if (r == LY_EINCOMPLETE) { |
6151 | 0 | rc = r; |
6152 | 0 | goto cleanup; |
6153 | 5.37k | } else if (r) { |
6154 | 353 | continue; |
6155 | 353 | } |
6156 | | |
6157 | | /* check for duplicates if they are possible */ |
6158 | 5.01k | switch (axis) { |
6159 | 0 | case LYXP_AXIS_ANCESTOR: |
6160 | 0 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
6161 | 0 | case LYXP_AXIS_DESCENDANT: |
6162 | 0 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
6163 | 0 | case LYXP_AXIS_FOLLOWING: |
6164 | 0 | case LYXP_AXIS_FOLLOWING_SIBLING: |
6165 | 327 | case LYXP_AXIS_PARENT: |
6166 | 327 | case LYXP_AXIS_PRECEDING: |
6167 | 327 | case LYXP_AXIS_PRECEDING_SIBLING: |
6168 | 327 | result.non_child_axis = 1; |
6169 | 327 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
6170 | 0 | continue; |
6171 | 0 | } |
6172 | 327 | break; |
6173 | 2.34k | case LYXP_AXIS_CHILD: |
6174 | 4.69k | case LYXP_AXIS_SELF: |
6175 | 4.69k | break; |
6176 | 0 | case LYXP_AXIS_ATTRIBUTE: |
6177 | | /* handled specially */ |
6178 | 0 | assert(0); |
6179 | 0 | LOGINT(set->ctx); |
6180 | 0 | break; |
6181 | 5.01k | } |
6182 | | |
6183 | | /* matching node */ |
6184 | 5.01k | set_insert_node(&result, iter, 0, iter_type, result.used); |
6185 | 5.01k | } |
6186 | 2.85k | } |
6187 | | |
6188 | | /* move result to the set */ |
6189 | 2.88k | lyxp_set_free_content(set); |
6190 | 2.88k | *set = result; |
6191 | 2.88k | result.type = LYXP_SET_NUMBER; |
6192 | | |
6193 | | /* sort the final set if the document order could have been broken */ |
6194 | 2.88k | if (set->non_child_axis) { |
6195 | 2.84k | set_sort(set); |
6196 | 2.84k | } else { |
6197 | 40 | assert(!set_sort(set)); |
6198 | 40 | } |
6199 | | |
6200 | 2.88k | cleanup: |
6201 | 2.88k | lyxp_set_free_content(&result); |
6202 | 2.88k | return rc; |
6203 | 2.88k | } |
6204 | | |
6205 | | /** |
6206 | | * @brief Move context @p set to child nodes using hashes. Result is LYXP_SET_NODE_SET. Context position aware. |
6207 | | * |
6208 | | * @param[in,out] set Set to use. |
6209 | | * @param[in] scnode Matching node schema. |
6210 | | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
6211 | | * @param[in] options XPath options. |
6212 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
6213 | | */ |
6214 | | static LY_ERR |
6215 | | moveto_node_hash_child(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
6216 | | uint32_t options) |
6217 | 312 | { |
6218 | 312 | LY_ERR ret = LY_SUCCESS, r; |
6219 | 312 | uint32_t i; |
6220 | 312 | const struct lyd_node *siblings; |
6221 | 312 | struct lyxp_set result; |
6222 | 312 | struct lyd_node *sub, *inst = NULL; |
6223 | | |
6224 | 312 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
6225 | | |
6226 | | /* init result set */ |
6227 | 312 | set_init(&result, set); |
6228 | | |
6229 | 312 | if (options & LYXP_SKIP_EXPR) { |
6230 | 0 | goto cleanup; |
6231 | 0 | } |
6232 | | |
6233 | 312 | if (set->type != LYXP_SET_NODE_SET) { |
6234 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
6235 | 0 | ret = LY_EVALID; |
6236 | 0 | goto cleanup; |
6237 | 0 | } |
6238 | | |
6239 | | /* context check for all the nodes since we have the schema node */ |
6240 | 312 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
6241 | 0 | lyxp_set_free_content(set); |
6242 | 0 | goto cleanup; |
6243 | 312 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
6244 | 312 | (scnode != set->context_op)) { |
6245 | 0 | lyxp_set_free_content(set); |
6246 | 0 | goto cleanup; |
6247 | 0 | } |
6248 | | |
6249 | | /* create specific data instance if needed */ |
6250 | 312 | if (scnode->nodetype == LYS_LIST) { |
6251 | 156 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, 1, &inst), cleanup); |
6252 | 156 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
6253 | 0 | LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup); |
6254 | 0 | } |
6255 | | |
6256 | 624 | for (i = 0; i < set->used; ++i) { |
6257 | 312 | siblings = NULL; |
6258 | | |
6259 | 312 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
6260 | 312 | assert(!set->val.nodes[i].node); |
6261 | | |
6262 | | /* search in all the trees */ |
6263 | 312 | siblings = set->tree; |
6264 | 312 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
6265 | | /* search in children */ |
6266 | 0 | siblings = lyd_child(set->val.nodes[i].node); |
6267 | 0 | } |
6268 | | |
6269 | | /* find the node using hashes */ |
6270 | 312 | if (inst) { |
6271 | 156 | r = lyd_find_sibling_first(siblings, inst, &sub); |
6272 | 156 | } else { |
6273 | 156 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
6274 | 156 | } |
6275 | 312 | if (r == LY_ENOTFOUND) { |
6276 | | /* may still be an opaque node */ |
6277 | 179 | r = lyd_find_sibling_opaq_next(siblings, scnode->name, &sub); |
6278 | 179 | } |
6279 | 312 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
6280 | | |
6281 | | /* when check */ |
6282 | 312 | if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) { |
6283 | 0 | ret = LY_EINCOMPLETE; |
6284 | 0 | goto cleanup; |
6285 | 0 | } |
6286 | | |
6287 | 312 | if (sub) { |
6288 | | /* pos filled later */ |
6289 | 133 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
6290 | 133 | } |
6291 | 312 | } |
6292 | | |
6293 | | /* move result to the set */ |
6294 | 312 | lyxp_set_free_content(set); |
6295 | 312 | *set = result; |
6296 | 312 | result.type = LYXP_SET_NUMBER; |
6297 | 312 | assert(!set_sort(set)); |
6298 | | |
6299 | 312 | cleanup: |
6300 | 312 | lyxp_set_free_content(&result); |
6301 | 312 | lyd_free_tree(inst); |
6302 | 312 | return ret; |
6303 | 312 | } |
6304 | | |
6305 | | /** |
6306 | | * @brief Check @p node as a part of schema NameTest processing. |
6307 | | * |
6308 | | * @param[in] node Schema node to check. |
6309 | | * @param[in] ctx_scnode Context node. |
6310 | | * @param[in] set Set to read general context from. |
6311 | | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
6312 | | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
6313 | | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
6314 | | */ |
6315 | | static LY_ERR |
6316 | | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
6317 | | const char *node_name, const struct lys_module *moveto_mod) |
6318 | 6.32k | { |
6319 | 6.32k | if (!moveto_mod && node_name) { |
6320 | 0 | switch (set->format) { |
6321 | 0 | case LY_VALUE_SCHEMA: |
6322 | 0 | case LY_VALUE_SCHEMA_RESOLVED: |
6323 | | /* use current module */ |
6324 | 0 | moveto_mod = set->cur_mod; |
6325 | 0 | break; |
6326 | 0 | case LY_VALUE_JSON: |
6327 | 0 | case LY_VALUE_LYB: |
6328 | 0 | case LY_VALUE_STR_NS: |
6329 | | /* inherit module of the context node, if any */ |
6330 | 0 | if (ctx_scnode) { |
6331 | 0 | moveto_mod = ctx_scnode->module; |
6332 | 0 | } |
6333 | 0 | break; |
6334 | 0 | case LY_VALUE_CANON: |
6335 | 0 | case LY_VALUE_XML: |
6336 | | /* not defined */ |
6337 | 0 | LOGINT(set->ctx); |
6338 | 0 | return LY_EINVAL; |
6339 | 0 | } |
6340 | 0 | } |
6341 | | |
6342 | 6.32k | if (!node) { |
6343 | | /* root will not match a specific node */ |
6344 | 171 | if (node_name || moveto_mod) { |
6345 | 0 | return LY_ENOT; |
6346 | 0 | } |
6347 | 171 | return LY_SUCCESS; |
6348 | 171 | } |
6349 | | |
6350 | | /* module check */ |
6351 | 6.15k | if (moveto_mod && (node->module != moveto_mod)) { |
6352 | 855 | return LY_ENOT; |
6353 | 855 | } |
6354 | | |
6355 | | /* context check */ |
6356 | 5.30k | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
6357 | 0 | return LY_EINVAL; |
6358 | 5.30k | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
6359 | 0 | return LY_EINVAL; |
6360 | 0 | } |
6361 | | |
6362 | | /* name check */ |
6363 | 5.30k | if (node_name && (node->name != node_name)) { |
6364 | 5.13k | return LY_ENOT; |
6365 | 5.13k | } |
6366 | | |
6367 | | /* match */ |
6368 | 171 | return LY_SUCCESS; |
6369 | 5.30k | } |
6370 | | |
6371 | | /** |
6372 | | * @brief Get the next node in a forward schema node DFS. |
6373 | | * |
6374 | | * @param[in] iter Last returned node. |
6375 | | * @param[in] stop Node to stop the search on and not return. |
6376 | | * @param[in] getnext_opts Options for ::lys_getnext(). |
6377 | | * @return Next node, NULL if there are no more. |
6378 | | */ |
6379 | | static const struct lysc_node * |
6380 | | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
6381 | 0 | { |
6382 | 0 | const struct lysc_node *next = NULL; |
6383 | |
|
6384 | 0 | next = lysc_node_child(iter); |
6385 | 0 | if (!next) { |
6386 | | /* no children, try siblings */ |
6387 | 0 | if ((iter == stop) || !lysc_data_parent(iter)) { |
6388 | | /* we are done, no next element to process */ |
6389 | 0 | return NULL; |
6390 | 0 | } |
6391 | | |
6392 | 0 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
6393 | 0 | } |
6394 | 0 | while (!next && iter) { |
6395 | | /* parent is already processed, go to its sibling */ |
6396 | 0 | iter = iter->parent; |
6397 | 0 | if ((iter == stop) || !lysc_data_parent(iter)) { |
6398 | | /* we are done, no next element to process */ |
6399 | 0 | return NULL; |
6400 | 0 | } |
6401 | 0 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
6402 | 0 | } |
6403 | | |
6404 | 0 | return next; |
6405 | 0 | } |
6406 | | |
6407 | | /** |
6408 | | * @brief Consider schema node based on its in_ctx enum value. |
6409 | | * |
6410 | | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
6411 | | * @param[in] axis Axis to use. |
6412 | | * @return LY_SUCCESS on success. |
6413 | | * @return LY_ENOT if the node should not be returned. |
6414 | | */ |
6415 | | static LY_ERR |
6416 | | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
6417 | 855 | { |
6418 | 855 | switch (axis) { |
6419 | 0 | case LYXP_AXIS_SELF: |
6420 | 0 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
6421 | | /* additionally put the start node into context */ |
6422 | 0 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
6423 | 0 | return LY_SUCCESS; |
6424 | 0 | } |
6425 | 0 | break; |
6426 | 342 | case LYXP_AXIS_PARENT: |
6427 | 342 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
6428 | 342 | case LYXP_AXIS_ANCESTOR: |
6429 | 342 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
6430 | 342 | case LYXP_AXIS_DESCENDANT: |
6431 | 342 | case LYXP_AXIS_FOLLOWING: |
6432 | 342 | case LYXP_AXIS_FOLLOWING_SIBLING: |
6433 | 342 | case LYXP_AXIS_PRECEDING: |
6434 | 342 | case LYXP_AXIS_PRECEDING_SIBLING: |
6435 | 855 | case LYXP_AXIS_CHILD: |
6436 | 855 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
6437 | | /* remember that context node was used */ |
6438 | 0 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
6439 | 0 | return LY_SUCCESS; |
6440 | 855 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
6441 | | /* traversed */ |
6442 | 342 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
6443 | 342 | return LY_SUCCESS; |
6444 | 342 | } |
6445 | 513 | break; |
6446 | 513 | case LYXP_AXIS_ATTRIBUTE: |
6447 | | /* unreachable */ |
6448 | 0 | assert(0); |
6449 | 0 | LOGINT(NULL); |
6450 | 0 | break; |
6451 | 855 | } |
6452 | | |
6453 | 513 | return LY_ENOT; |
6454 | 855 | } |
6455 | | |
6456 | | /** |
6457 | | * @brief Get previous sibling for a schema node. |
6458 | | * |
6459 | | * @param[in] scnode Schema node. |
6460 | | * @param[in] getnext_opts Options for ::lys_getnext(). |
6461 | | * @return Previous sibling, NULL if none. |
6462 | | */ |
6463 | | static const struct lysc_node * |
6464 | | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
6465 | 0 | { |
6466 | 0 | const struct lysc_node *next = NULL, *prev = NULL; |
6467 | |
|
6468 | 0 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
6469 | 0 | if (next == scnode) { |
6470 | 0 | break; |
6471 | 0 | } |
6472 | | |
6473 | 0 | prev = next; |
6474 | 0 | } |
6475 | |
|
6476 | 0 | return prev; |
6477 | 0 | } |
6478 | | |
6479 | | /** |
6480 | | * @brief Get the first schema node on an axis for a context node. |
6481 | | * |
6482 | | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
6483 | | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
6484 | | * @param[in,out] iter_mod Internal module iterator, do not change. |
6485 | | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
6486 | | * @param[in] scnode Context node. |
6487 | | * @param[in] node_type Type of @p scnode. |
6488 | | * @param[in] in_ctx In_ctx enum of @p scnode. |
6489 | | * @param[in] axis Axis to use. |
6490 | | * @param[in] set XPath set with the general context. |
6491 | | * @param[in] getnext_opts Options for ::lys_getnext(). |
6492 | | * @return LY_SUCCESS on success. |
6493 | | * @return LY_ENOTFOUND if no next node found. |
6494 | | */ |
6495 | | static LY_ERR |
6496 | | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
6497 | | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
6498 | | struct lyxp_set *set, uint32_t getnext_opts) |
6499 | 342 | { |
6500 | 342 | const struct lysc_node *next = NULL; |
6501 | 342 | enum lyxp_node_type next_type = 0; |
6502 | | |
6503 | 342 | assert(!*iter); |
6504 | 342 | assert(!*iter_type); |
6505 | | |
6506 | 342 | *iter_mod = NULL; |
6507 | 342 | *iter_mod_idx = 0; |
6508 | | |
6509 | 342 | switch (axis) { |
6510 | 0 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
6511 | 0 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
6512 | 0 | case LYXP_AXIS_SELF: |
6513 | 0 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
6514 | | /* just return the node */ |
6515 | 0 | next = scnode; |
6516 | 0 | next_type = node_type; |
6517 | 0 | } |
6518 | 0 | break; |
6519 | | |
6520 | 0 | case LYXP_AXIS_ANCESTOR: |
6521 | 171 | case LYXP_AXIS_PARENT: |
6522 | 171 | if (node_type == LYXP_NODE_ELEM) { |
6523 | 171 | next = lysc_data_parent(scnode); |
6524 | 171 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
6525 | 171 | } /* else no parent */ |
6526 | 171 | break; |
6527 | | |
6528 | 0 | case LYXP_AXIS_DESCENDANT: |
6529 | 171 | case LYXP_AXIS_CHILD: |
6530 | 171 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
6531 | | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
6532 | | * it can be in a top-level augment */ |
6533 | 855 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
6534 | | /* module may not be implemented or not compiled yet */ |
6535 | 855 | if (!(*iter_mod)->compiled) { |
6536 | 513 | continue; |
6537 | 513 | } |
6538 | | |
6539 | | /* get next node */ |
6540 | 342 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
6541 | 171 | next_type = LYXP_NODE_ELEM; |
6542 | 171 | break; |
6543 | 171 | } |
6544 | 342 | } |
6545 | 171 | } else if (node_type == LYXP_NODE_ELEM) { |
6546 | | /* get next node */ |
6547 | 0 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
6548 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6549 | 0 | } |
6550 | 171 | break; |
6551 | | |
6552 | 0 | case LYXP_AXIS_FOLLOWING: |
6553 | 0 | case LYXP_AXIS_FOLLOWING_SIBLING: |
6554 | 0 | if (node_type == LYXP_NODE_ELEM) { |
6555 | | /* first next sibling */ |
6556 | 0 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
6557 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6558 | 0 | } /* else no sibling */ |
6559 | 0 | break; |
6560 | | |
6561 | 0 | case LYXP_AXIS_PRECEDING: |
6562 | 0 | case LYXP_AXIS_PRECEDING_SIBLING: |
6563 | 0 | if (node_type == LYXP_NODE_ELEM) { |
6564 | | /* first parent sibling */ |
6565 | 0 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
6566 | 0 | if (next == scnode) { |
6567 | | /* no preceding sibling */ |
6568 | 0 | next = NULL; |
6569 | 0 | } |
6570 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6571 | 0 | } /* else no sibling */ |
6572 | 0 | break; |
6573 | | |
6574 | 0 | case LYXP_AXIS_ATTRIBUTE: |
6575 | | /* unreachable */ |
6576 | 0 | assert(0); |
6577 | 0 | LOGINT(set->ctx); |
6578 | 0 | break; |
6579 | 342 | } |
6580 | | |
6581 | 342 | *iter = next; |
6582 | 342 | *iter_type = next_type; |
6583 | 342 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
6584 | 342 | } |
6585 | | |
6586 | | /** |
6587 | | * @brief Iterate over all schema nodes on an axis for a context node. |
6588 | | * |
6589 | | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
6590 | | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
6591 | | * @param[in,out] iter_mod Internal module iterator, do not change. |
6592 | | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
6593 | | * @param[in] scnode Context node. |
6594 | | * @param[in] node_type Type of @p scnode. |
6595 | | * @param[in] axis Axis to use. |
6596 | | * @param[in] set XPath set with the general context. |
6597 | | * @param[in] getnext_opts Options for ::lys_getnext(). |
6598 | | * @return LY_SUCCESS on success. |
6599 | | * @return LY_ENOTFOUND if no next node found. |
6600 | | */ |
6601 | | static LY_ERR |
6602 | | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
6603 | | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
6604 | | struct lyxp_set *set, uint32_t getnext_opts) |
6605 | 6.66k | { |
6606 | 6.66k | const struct lysc_node *next = NULL, *dfs_stop; |
6607 | 6.66k | enum lyxp_node_type next_type = 0; |
6608 | | |
6609 | 6.66k | if (!*iter_type) { |
6610 | | /* first returned node */ |
6611 | 342 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
6612 | 342 | getnext_opts); |
6613 | 342 | } |
6614 | | |
6615 | 6.32k | switch (axis) { |
6616 | 171 | case LYXP_AXIS_PARENT: |
6617 | 171 | case LYXP_AXIS_SELF: |
6618 | | /* parent/self was returned before */ |
6619 | 171 | break; |
6620 | | |
6621 | 0 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
6622 | 0 | if ((*iter == scnode) && (*iter_type == node_type)) { |
6623 | | /* fake first ancestor, we returned self before */ |
6624 | 0 | *iter = NULL; |
6625 | 0 | *iter_type = 0; |
6626 | 0 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
6627 | 0 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
6628 | 0 | } /* else continue ancestor */ |
6629 | | |
6630 | | /* fallthrough */ |
6631 | 0 | case LYXP_AXIS_ANCESTOR: |
6632 | 0 | if (*iter_type == LYXP_NODE_ELEM) { |
6633 | 0 | next = lysc_data_parent(*iter); |
6634 | 0 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
6635 | 0 | } /* else no ancestor */ |
6636 | 0 | break; |
6637 | | |
6638 | 0 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
6639 | 0 | if ((*iter == scnode) && (*iter_type == node_type)) { |
6640 | | /* fake first descendant, we returned self before */ |
6641 | 0 | *iter = NULL; |
6642 | 0 | *iter_type = 0; |
6643 | 0 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
6644 | 0 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
6645 | 0 | } /* else DFS until context node */ |
6646 | 0 | dfs_stop = scnode; |
6647 | | |
6648 | | /* fallthrough */ |
6649 | 0 | case LYXP_AXIS_DESCENDANT: |
6650 | 0 | if (axis == LYXP_AXIS_DESCENDANT) { |
6651 | | /* DFS until the context node */ |
6652 | 0 | dfs_stop = scnode; |
6653 | 0 | } |
6654 | | |
6655 | | /* fallthrough */ |
6656 | 0 | case LYXP_AXIS_PRECEDING: |
6657 | 0 | if (axis == LYXP_AXIS_PRECEDING) { |
6658 | | /* DFS until the previous sibling */ |
6659 | 0 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
6660 | 0 | assert(dfs_stop); |
6661 | | |
6662 | 0 | if (*iter == dfs_stop) { |
6663 | | /* we are done */ |
6664 | 0 | break; |
6665 | 0 | } |
6666 | 0 | } |
6667 | | |
6668 | | /* fallthrough */ |
6669 | 0 | case LYXP_AXIS_FOLLOWING: |
6670 | 0 | if (axis == LYXP_AXIS_FOLLOWING) { |
6671 | | /* DFS through the whole module */ |
6672 | 0 | dfs_stop = NULL; |
6673 | 0 | } |
6674 | | |
6675 | | /* nested nodes */ |
6676 | 0 | assert(*iter); |
6677 | 0 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
6678 | 0 | if (next) { |
6679 | 0 | next_type = LYXP_NODE_ELEM; |
6680 | 0 | break; |
6681 | 0 | } /* else get next top-level node just like a child */ |
6682 | | |
6683 | | /* fallthrough */ |
6684 | 6.15k | case LYXP_AXIS_CHILD: |
6685 | 6.15k | case LYXP_AXIS_FOLLOWING_SIBLING: |
6686 | 6.15k | if (!*iter_mod) { |
6687 | | /* nodes from a single module */ |
6688 | 0 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
6689 | 0 | next_type = LYXP_NODE_ELEM; |
6690 | 0 | break; |
6691 | 0 | } |
6692 | | |
6693 | 0 | assert(scnode); |
6694 | 0 | if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) { |
6695 | | /* iterating over top-level nodes, find next */ |
6696 | 0 | while (lysc_data_parent(*iter)) { |
6697 | 0 | *iter = lysc_data_parent(*iter); |
6698 | 0 | } |
6699 | 0 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
6700 | 0 | next_type = LYXP_NODE_ELEM; |
6701 | 0 | break; |
6702 | 0 | } |
6703 | 0 | } |
6704 | 0 | } |
6705 | | |
6706 | 7.01k | while (*iter_mod) { |
6707 | | /* module top-level nodes */ |
6708 | 6.84k | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
6709 | 5.98k | next_type = LYXP_NODE_ELEM; |
6710 | 5.98k | break; |
6711 | 5.98k | } |
6712 | | |
6713 | | /* get next module */ |
6714 | 1.02k | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
6715 | | /* module may not be implemented or not compiled yet */ |
6716 | 855 | if ((*iter_mod)->compiled) { |
6717 | 684 | break; |
6718 | 684 | } |
6719 | 855 | } |
6720 | | |
6721 | | /* new module, start over */ |
6722 | 855 | *iter = NULL; |
6723 | 855 | } |
6724 | 6.15k | break; |
6725 | | |
6726 | 0 | case LYXP_AXIS_PRECEDING_SIBLING: |
6727 | 0 | assert(*iter); |
6728 | | |
6729 | | /* next parent sibling until scnode */ |
6730 | 0 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
6731 | 0 | if (next == scnode) { |
6732 | | /* no previous sibling */ |
6733 | 0 | next = NULL; |
6734 | 0 | } |
6735 | 0 | next_type = next ? LYXP_NODE_ELEM : 0; |
6736 | 0 | break; |
6737 | | |
6738 | 0 | case LYXP_AXIS_ATTRIBUTE: |
6739 | | /* unreachable */ |
6740 | 0 | assert(0); |
6741 | 0 | LOGINT(set->ctx); |
6742 | 0 | break; |
6743 | 6.32k | } |
6744 | | |
6745 | 6.32k | *iter = next; |
6746 | 6.32k | *iter_type = next_type; |
6747 | 6.32k | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
6748 | 6.32k | } |
6749 | | |
6750 | | /** |
6751 | | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
6752 | | * |
6753 | | * @param[in,out] set Set to use. |
6754 | | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
6755 | | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
6756 | | * @param[in] axis Axis to search on. |
6757 | | * @param[in] options XPath options. |
6758 | | * @return LY_ERR |
6759 | | */ |
6760 | | static LY_ERR |
6761 | | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
6762 | | uint32_t options) |
6763 | 342 | { |
6764 | 342 | ly_bool temp_ctx = 0; |
6765 | 342 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
6766 | 342 | const struct lys_module *mod = NULL; |
6767 | 342 | const struct lysc_node *iter; |
6768 | 342 | enum lyxp_node_type iter_type; |
6769 | | |
6770 | 342 | if (options & LYXP_SKIP_EXPR) { |
6771 | 0 | return LY_SUCCESS; |
6772 | 0 | } |
6773 | | |
6774 | 342 | if (set->type != LYXP_SET_SCNODE_SET) { |
6775 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
6776 | 0 | return LY_EVALID; |
6777 | 0 | } |
6778 | | |
6779 | | /* getnext opts */ |
6780 | 342 | getnext_opts = 0; |
6781 | 342 | if (options & LYXP_SCNODE_OUTPUT) { |
6782 | 0 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
6783 | 0 | } |
6784 | 342 | if (options & LYXP_SCNODE_SCHEMAMOUNT) { |
6785 | 0 | getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT; |
6786 | 0 | } |
6787 | | |
6788 | 342 | orig_used = set->used; |
6789 | 1.19k | for (i = 0; i < orig_used; ++i) { |
6790 | | /* update in_ctx first */ |
6791 | 855 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
6792 | | /* not usable, skip */ |
6793 | 513 | continue; |
6794 | 513 | } |
6795 | | |
6796 | 342 | iter = NULL; |
6797 | 342 | iter_type = 0; |
6798 | 6.66k | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
6799 | 6.66k | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
6800 | 6.32k | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
6801 | 5.98k | continue; |
6802 | 5.98k | } |
6803 | | |
6804 | | /* insert */ |
6805 | 342 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx)); |
6806 | | |
6807 | | /* we need to prevent these nodes from being considered in this moveto */ |
6808 | 342 | if ((idx < orig_used) && (idx > i)) { |
6809 | 0 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
6810 | 0 | temp_ctx = 1; |
6811 | 0 | } |
6812 | 342 | } |
6813 | | |
6814 | | /* only consider extension nodes after no local ones were found */ |
6815 | 342 | if ((orig_used == set->used) && moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) && |
6816 | 342 | (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode, |
6817 | 0 | moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) { |
6818 | | /* there is a matching node from an extension, use it */ |
6819 | 0 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx)); |
6820 | 0 | if ((idx < orig_used) && (idx > i)) { |
6821 | 0 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
6822 | 0 | temp_ctx = 1; |
6823 | 0 | } |
6824 | 0 | } |
6825 | 342 | } |
6826 | | |
6827 | | /* correct temporary in_ctx values */ |
6828 | 342 | if (temp_ctx) { |
6829 | 0 | for (i = 0; i < orig_used; ++i) { |
6830 | 0 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
6831 | 0 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
6832 | 0 | } |
6833 | 0 | } |
6834 | 0 | } |
6835 | | |
6836 | 342 | return LY_SUCCESS; |
6837 | 342 | } |
6838 | | |
6839 | | /** |
6840 | | * @brief Move context @p set to a child node and all its descendants. Result is LYXP_SET_NODE_SET. |
6841 | | * Context position aware. |
6842 | | * |
6843 | | * @param[in] set Set to use. |
6844 | | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
6845 | | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
6846 | | * @param[in] options XPath options. |
6847 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
6848 | | */ |
6849 | | static LY_ERR |
6850 | | moveto_node_alldesc_child(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
6851 | 0 | { |
6852 | 0 | uint32_t i; |
6853 | 0 | const struct lyd_node *next, *elem, *start; |
6854 | 0 | struct lyxp_set ret_set; |
6855 | 0 | LY_ERR rc; |
6856 | |
|
6857 | 0 | if (options & LYXP_SKIP_EXPR) { |
6858 | 0 | return LY_SUCCESS; |
6859 | 0 | } |
6860 | | |
6861 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
6862 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
6863 | 0 | return LY_EVALID; |
6864 | 0 | } |
6865 | | |
6866 | | /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */ |
6867 | 0 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
6868 | 0 | LY_CHECK_RET(rc); |
6869 | | |
6870 | | /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */ |
6871 | 0 | set_init(&ret_set, set); |
6872 | 0 | for (i = 0; i < set->used; ++i) { |
6873 | | |
6874 | | /* TREE DFS */ |
6875 | 0 | start = set->val.nodes[i].node; |
6876 | 0 | for (elem = next = start; elem; elem = next) { |
6877 | 0 | rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options); |
6878 | 0 | if (!rc) { |
6879 | | /* add matching node into result set */ |
6880 | 0 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
6881 | 0 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
6882 | | /* the node is a duplicate, we'll process it later in the set */ |
6883 | 0 | goto skip_children; |
6884 | 0 | } |
6885 | 0 | } else if (rc == LY_EINCOMPLETE) { |
6886 | 0 | return rc; |
6887 | 0 | } else if (rc == LY_EINVAL) { |
6888 | 0 | goto skip_children; |
6889 | 0 | } |
6890 | | |
6891 | | /* TREE DFS NEXT ELEM */ |
6892 | | /* select element for the next run - children first */ |
6893 | 0 | next = lyd_child(elem); |
6894 | 0 | if (!next) { |
6895 | 0 | skip_children: |
6896 | | /* no children, so try siblings, but only if it's not the start, |
6897 | | * that is considered to be the root and it's siblings are not traversed */ |
6898 | 0 | if (elem != start) { |
6899 | 0 | next = elem->next; |
6900 | 0 | } else { |
6901 | 0 | break; |
6902 | 0 | } |
6903 | 0 | } |
6904 | 0 | while (!next) { |
6905 | | /* no siblings, go back through the parents */ |
6906 | 0 | if (lyd_parent(elem) == start) { |
6907 | | /* we are done, no next element to process */ |
6908 | 0 | break; |
6909 | 0 | } |
6910 | | /* parent is already processed, go to its sibling */ |
6911 | 0 | elem = lyd_parent(elem); |
6912 | 0 | next = elem->next; |
6913 | 0 | } |
6914 | 0 | } |
6915 | 0 | } |
6916 | | |
6917 | | /* make the temporary set the current one */ |
6918 | 0 | ret_set.ctx_pos = set->ctx_pos; |
6919 | 0 | ret_set.ctx_size = set->ctx_size; |
6920 | 0 | lyxp_set_free_content(set); |
6921 | 0 | memcpy(set, &ret_set, sizeof *set); |
6922 | 0 | assert(!set_sort(set)); |
6923 | | |
6924 | 0 | return LY_SUCCESS; |
6925 | 0 | } |
6926 | | |
6927 | | /** |
6928 | | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
6929 | | * Result is LYXP_SET_NODE_SET. |
6930 | | * |
6931 | | * @param[in] set Set to use. |
6932 | | * @param[in] start Start node whose subtree to add. |
6933 | | * @param[in] start_idx Index of @p start in @p set. |
6934 | | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
6935 | | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
6936 | | * @param[in] options XPath options. |
6937 | | * @return LY_ERR value. |
6938 | | */ |
6939 | | static LY_ERR |
6940 | | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
6941 | | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
6942 | 0 | { |
6943 | 0 | const struct lysc_node *next, *elem; |
6944 | 0 | uint32_t idx; |
6945 | 0 | LY_ERR rc; |
6946 | | |
6947 | | /* TREE DFS */ |
6948 | 0 | for (elem = next = start; elem; elem = next) { |
6949 | 0 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
6950 | | /* schema-only nodes, skip root */ |
6951 | 0 | goto next_iter; |
6952 | 0 | } |
6953 | | |
6954 | 0 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
6955 | 0 | if (!rc) { |
6956 | 0 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
6957 | 0 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
6958 | 0 | if (idx > start_idx) { |
6959 | | /* we will process it later in the set */ |
6960 | 0 | goto skip_children; |
6961 | 0 | } |
6962 | 0 | } else { |
6963 | 0 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL)); |
6964 | 0 | } |
6965 | 0 | } else if (rc == LY_EINVAL) { |
6966 | 0 | goto skip_children; |
6967 | 0 | } |
6968 | | |
6969 | 0 | next_iter: |
6970 | | /* TREE DFS NEXT ELEM */ |
6971 | | /* select element for the next run - children first */ |
6972 | 0 | next = lysc_node_child(elem); |
6973 | 0 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
6974 | 0 | next = next->next; |
6975 | 0 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
6976 | 0 | next = next->next; |
6977 | 0 | } |
6978 | 0 | if (!next) { |
6979 | 0 | skip_children: |
6980 | | /* no children, so try siblings, but only if it's not the start, |
6981 | | * that is considered to be the root and it's siblings are not traversed */ |
6982 | 0 | if (elem != start) { |
6983 | 0 | next = elem->next; |
6984 | 0 | } else { |
6985 | 0 | break; |
6986 | 0 | } |
6987 | 0 | } |
6988 | 0 | while (!next) { |
6989 | | /* no siblings, go back through the parents */ |
6990 | 0 | if (elem->parent == start) { |
6991 | | /* we are done, no next element to process */ |
6992 | 0 | break; |
6993 | 0 | } |
6994 | | /* parent is already processed, go to its sibling */ |
6995 | 0 | elem = elem->parent; |
6996 | 0 | next = elem->next; |
6997 | 0 | } |
6998 | 0 | } |
6999 | | |
7000 | 0 | return LY_SUCCESS; |
7001 | 0 | } |
7002 | | |
7003 | | /** |
7004 | | * @brief Move context @p set to a child schema node and all its descendants. Result is LYXP_SET_NODE_SET. |
7005 | | * |
7006 | | * @param[in] set Set to use. |
7007 | | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
7008 | | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
7009 | | * @param[in] options XPath options. |
7010 | | * @return LY_ERR value. |
7011 | | */ |
7012 | | static LY_ERR |
7013 | | moveto_scnode_alldesc_child(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
7014 | 0 | { |
7015 | 0 | uint32_t i, orig_used, mod_idx; |
7016 | 0 | const struct lys_module *mod; |
7017 | 0 | const struct lysc_node *root; |
7018 | |
|
7019 | 0 | if (options & LYXP_SKIP_EXPR) { |
7020 | 0 | return LY_SUCCESS; |
7021 | 0 | } |
7022 | | |
7023 | 0 | if (set->type != LYXP_SET_SCNODE_SET) { |
7024 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
7025 | 0 | return LY_EVALID; |
7026 | 0 | } |
7027 | | |
7028 | 0 | orig_used = set->used; |
7029 | 0 | for (i = 0; i < orig_used; ++i) { |
7030 | 0 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
7031 | 0 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
7032 | 0 | continue; |
7033 | 0 | } |
7034 | | |
7035 | | /* remember context node */ |
7036 | 0 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
7037 | 0 | } else { |
7038 | 0 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
7039 | 0 | } |
7040 | | |
7041 | 0 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
7042 | | /* traverse all top-level nodes in all the modules */ |
7043 | 0 | mod_idx = 0; |
7044 | 0 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
7045 | | /* module may not be implemented or not compiled yet */ |
7046 | 0 | if (!mod->compiled) { |
7047 | 0 | continue; |
7048 | 0 | } |
7049 | | |
7050 | 0 | root = NULL; |
7051 | | /* no getnext opts needed */ |
7052 | 0 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
7053 | 0 | LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options)); |
7054 | 0 | } |
7055 | 0 | } |
7056 | |
|
7057 | 0 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
7058 | | /* add all the descendants recursively */ |
7059 | 0 | LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options)); |
7060 | 0 | } |
7061 | 0 | } |
7062 | | |
7063 | 0 | return LY_SUCCESS; |
7064 | 0 | } |
7065 | | |
7066 | | /** |
7067 | | * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET. |
7068 | | * Indirectly context position aware. |
7069 | | * |
7070 | | * @param[in,out] set Set to use. |
7071 | | * @param[in] mod Matching metadata module, NULL for any. |
7072 | | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
7073 | | * @param[in] options XPath options. |
7074 | | * @return LY_ERR |
7075 | | */ |
7076 | | static LY_ERR |
7077 | | moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options) |
7078 | 0 | { |
7079 | 0 | struct lyd_meta *sub; |
7080 | |
|
7081 | 0 | if (options & LYXP_SKIP_EXPR) { |
7082 | 0 | return LY_SUCCESS; |
7083 | 0 | } |
7084 | | |
7085 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
7086 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
7087 | 0 | return LY_EVALID; |
7088 | 0 | } |
7089 | | |
7090 | 0 | for (uint32_t i = 0; i < set->used; ) { |
7091 | 0 | ly_bool replaced = 0; |
7092 | | |
7093 | | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
7094 | | * our attributes are always qualified */ |
7095 | 0 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
7096 | 0 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
7097 | | |
7098 | | /* check "namespace" */ |
7099 | 0 | if (mod && (sub->annotation->module != mod)) { |
7100 | 0 | continue; |
7101 | 0 | } |
7102 | | |
7103 | 0 | if (!ncname || (sub->name == ncname)) { |
7104 | | /* match */ |
7105 | 0 | if (!replaced) { |
7106 | 0 | set->val.meta[i].meta = sub; |
7107 | 0 | set->val.meta[i].type = LYXP_NODE_META; |
7108 | | /* pos does not change */ |
7109 | 0 | replaced = 1; |
7110 | 0 | } else { |
7111 | 0 | set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_META, i + 1); |
7112 | 0 | } |
7113 | 0 | ++i; |
7114 | 0 | } |
7115 | 0 | } |
7116 | 0 | } |
7117 | |
|
7118 | 0 | if (!replaced) { |
7119 | | /* no match */ |
7120 | 0 | set_remove_node(set, i); |
7121 | 0 | } |
7122 | 0 | } |
7123 | |
|
7124 | 0 | return LY_SUCCESS; |
7125 | 0 | } |
7126 | | |
7127 | | /** |
7128 | | * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards. |
7129 | | * Result is LYXP_SET_NODE_SET. Context position aware. |
7130 | | * |
7131 | | * @param[in,out] set1 Set to use for the result. |
7132 | | * @param[in] set2 Set that is copied to @p set1. |
7133 | | * @return LY_ERR |
7134 | | */ |
7135 | | static LY_ERR |
7136 | | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
7137 | 0 | { |
7138 | 0 | LY_ERR rc; |
7139 | |
|
7140 | 0 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
7141 | 0 | LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
7142 | 0 | return LY_EVALID; |
7143 | 0 | } |
7144 | | |
7145 | | /* set2 is empty or both set1 and set2 */ |
7146 | 0 | if (!set2->used) { |
7147 | 0 | return LY_SUCCESS; |
7148 | 0 | } |
7149 | | |
7150 | 0 | if (!set1->used) { |
7151 | | /* release hidden allocated data (lyxp_set.size) */ |
7152 | 0 | lyxp_set_free_content(set1); |
7153 | | /* direct copying of the entire structure */ |
7154 | 0 | memcpy(set1, set2, sizeof *set1); |
7155 | | /* dynamic memory belongs to set1 now, do not free */ |
7156 | 0 | memset(set2, 0, sizeof *set2); |
7157 | 0 | return LY_SUCCESS; |
7158 | 0 | } |
7159 | | |
7160 | | /* we assume sets are sorted */ |
7161 | 0 | assert(!set_sort(set1) && !set_sort(set2)); |
7162 | | |
7163 | | /* sort, remove duplicates */ |
7164 | 0 | rc = set_sorted_merge(set1, set2); |
7165 | 0 | LY_CHECK_RET(rc); |
7166 | | |
7167 | | /* final set must be sorted */ |
7168 | 0 | assert(!set_sort(set1)); |
7169 | | |
7170 | 0 | return LY_SUCCESS; |
7171 | 0 | } |
7172 | | |
7173 | | /** |
7174 | | * @brief Move context @p set to an attribute in any of the descendants. Result is LYXP_SET_NODE_SET. |
7175 | | * Context position aware. |
7176 | | * |
7177 | | * @param[in,out] set Set to use. |
7178 | | * @param[in] mod Matching metadata module, NULL for any. |
7179 | | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
7180 | | * @param[in] options XPath options. |
7181 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
7182 | | */ |
7183 | | static int |
7184 | | moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options) |
7185 | 0 | { |
7186 | 0 | struct lyd_meta *sub; |
7187 | 0 | struct lyxp_set *set_all_desc = NULL; |
7188 | 0 | LY_ERR rc; |
7189 | |
|
7190 | 0 | if (options & LYXP_SKIP_EXPR) { |
7191 | 0 | return LY_SUCCESS; |
7192 | 0 | } |
7193 | | |
7194 | 0 | if (set->type != LYXP_SET_NODE_SET) { |
7195 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
7196 | 0 | return LY_EVALID; |
7197 | 0 | } |
7198 | | |
7199 | | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
7200 | | * but it likely won't be used much, so it's a waste of time */ |
7201 | | /* copy the context */ |
7202 | 0 | set_all_desc = set_copy(set); |
7203 | | /* get all descendant nodes (the original context nodes are removed) */ |
7204 | 0 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
7205 | 0 | if (rc != LY_SUCCESS) { |
7206 | 0 | lyxp_set_free(set_all_desc); |
7207 | 0 | return rc; |
7208 | 0 | } |
7209 | | /* prepend the original context nodes */ |
7210 | 0 | rc = moveto_union(set, set_all_desc); |
7211 | 0 | if (rc != LY_SUCCESS) { |
7212 | 0 | lyxp_set_free(set_all_desc); |
7213 | 0 | return rc; |
7214 | 0 | } |
7215 | 0 | lyxp_set_free(set_all_desc); |
7216 | |
|
7217 | 0 | for (uint32_t i = 0; i < set->used; ) { |
7218 | 0 | ly_bool replaced = 0; |
7219 | | |
7220 | | /* only attributes of an elem can be in the result, skip all the rest, |
7221 | | * we have all attributes qualified in lyd tree */ |
7222 | 0 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
7223 | 0 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
7224 | | /* check "namespace" */ |
7225 | 0 | if (mod && (sub->annotation->module != mod)) { |
7226 | 0 | continue; |
7227 | 0 | } |
7228 | | |
7229 | 0 | if (!ncname || (sub->name == ncname)) { |
7230 | | /* match */ |
7231 | 0 | if (!replaced) { |
7232 | 0 | set->val.meta[i].meta = sub; |
7233 | 0 | set->val.meta[i].type = LYXP_NODE_META; |
7234 | | /* pos does not change */ |
7235 | 0 | replaced = 1; |
7236 | 0 | } else { |
7237 | 0 | set_insert_node(set, (struct lyd_node *)sub, set->val.meta[i].pos, LYXP_NODE_META, i + 1); |
7238 | 0 | } |
7239 | 0 | ++i; |
7240 | 0 | } |
7241 | 0 | } |
7242 | 0 | } |
7243 | |
|
7244 | 0 | if (!replaced) { |
7245 | | /* no match */ |
7246 | 0 | set_remove_node(set, i); |
7247 | 0 | } |
7248 | 0 | } |
7249 | |
|
7250 | 0 | return LY_SUCCESS; |
7251 | 0 | } |
7252 | | |
7253 | | /** |
7254 | | * @brief Move context @p set1 single item to the result of a comparison. |
7255 | | * |
7256 | | * @param[in] set1 First set with the item to compare. |
7257 | | * @param[in] idx1 Index of the item in @p set1. |
7258 | | * @param[in] set2 Second set. |
7259 | | * @param[in] op Comparison operator to process. |
7260 | | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
7261 | | * @param[out] result Result of the comparison. |
7262 | | * @return LY_ERR value. |
7263 | | */ |
7264 | | static LY_ERR |
7265 | | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
7266 | | ly_bool switch_operands, ly_bool *result) |
7267 | 2.34k | { |
7268 | 2.34k | struct lyxp_set tmp1 = {0}; |
7269 | 2.34k | LY_ERR rc = LY_SUCCESS; |
7270 | | |
7271 | 2.34k | assert(set1->type == LYXP_SET_NODE_SET); |
7272 | | |
7273 | | /* cast set1 */ |
7274 | 2.34k | switch (set2->type) { |
7275 | 0 | case LYXP_SET_NUMBER: |
7276 | 0 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
7277 | 0 | break; |
7278 | 0 | case LYXP_SET_BOOLEAN: |
7279 | 0 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
7280 | 0 | break; |
7281 | 2.34k | default: |
7282 | 2.34k | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
7283 | 2.34k | break; |
7284 | 2.34k | } |
7285 | 2.34k | LY_CHECK_GOTO(rc, cleanup); |
7286 | | |
7287 | | /* canonize set2 */ |
7288 | 2.34k | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
7289 | | |
7290 | | /* compare recursively and store the result */ |
7291 | 2.34k | if (switch_operands) { |
7292 | 0 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup); |
7293 | 2.34k | } else { |
7294 | 2.34k | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup); |
7295 | 2.34k | } |
7296 | | |
7297 | 2.34k | cleanup: |
7298 | 2.34k | lyxp_set_free_content(&tmp1); |
7299 | 2.34k | return rc; |
7300 | 2.34k | } |
7301 | | |
7302 | | /** |
7303 | | * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
7304 | | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
7305 | | * |
7306 | | * @param[in] set1 Set acting as the first operand for @p op. |
7307 | | * @param[in] set2 Set acting as the second operand for @p op. |
7308 | | * @param[in] op Comparison operator to process. |
7309 | | * @param[out] result Result of the comparison. |
7310 | | * @return LY_ERR |
7311 | | */ |
7312 | | static LY_ERR |
7313 | | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result) |
7314 | 4.69k | { |
7315 | | /* |
7316 | | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
7317 | | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
7318 | | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
7319 | | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
7320 | | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
7321 | | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
7322 | | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
7323 | | * |
7324 | | * '=' or '!=' |
7325 | | * BOOLEAN + BOOLEAN |
7326 | | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
7327 | | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
7328 | | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
7329 | | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
7330 | | * NUMBER + NUMBER |
7331 | | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
7332 | | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
7333 | | * STRING + STRING |
7334 | | * |
7335 | | * '<=', '<', '>=', '>' |
7336 | | * NUMBER + NUMBER |
7337 | | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
7338 | | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
7339 | | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
7340 | | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
7341 | | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
7342 | | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
7343 | | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
7344 | | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
7345 | | */ |
7346 | 4.69k | uint32_t i; |
7347 | 4.69k | LY_ERR rc; |
7348 | | |
7349 | | /* iterative evaluation with node-sets */ |
7350 | 4.69k | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
7351 | 2.34k | if (set1->type == LYXP_SET_NODE_SET) { |
7352 | 4.63k | for (i = 0; i < set1->used; ++i) { |
7353 | | /* evaluate for the single item */ |
7354 | 2.34k | LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result)); |
7355 | | |
7356 | | /* lazy evaluation until true */ |
7357 | 2.34k | if (*result) { |
7358 | 60 | return LY_SUCCESS; |
7359 | 60 | } |
7360 | 2.34k | } |
7361 | 2.34k | } else { |
7362 | 0 | for (i = 0; i < set2->used; ++i) { |
7363 | | /* evaluate for the single item */ |
7364 | 0 | LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result)); |
7365 | | |
7366 | | /* lazy evaluation until true */ |
7367 | 0 | if (*result) { |
7368 | 0 | return LY_SUCCESS; |
7369 | 0 | } |
7370 | 0 | } |
7371 | 0 | } |
7372 | | |
7373 | | /* false for all the nodes */ |
7374 | 2.28k | *result = 0; |
7375 | 2.28k | return LY_SUCCESS; |
7376 | 2.34k | } |
7377 | | |
7378 | | /* first convert properly */ |
7379 | 2.34k | if ((op[0] == '=') || (op[0] == '!')) { |
7380 | 2.34k | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
7381 | 0 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
7382 | 0 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
7383 | 2.34k | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
7384 | 0 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
7385 | 0 | LY_CHECK_RET(rc); |
7386 | 0 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
7387 | 0 | LY_CHECK_RET(rc); |
7388 | 0 | } /* else we have 2 strings */ |
7389 | 2.34k | } else { |
7390 | 0 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
7391 | 0 | LY_CHECK_RET(rc); |
7392 | 0 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
7393 | 0 | LY_CHECK_RET(rc); |
7394 | 0 | } |
7395 | | |
7396 | 2.34k | assert(set1->type == set2->type); |
7397 | | |
7398 | | /* compute result */ |
7399 | 2.34k | if (op[0] == '=') { |
7400 | 2.34k | if (set1->type == LYXP_SET_BOOLEAN) { |
7401 | 0 | *result = (set1->val.bln == set2->val.bln); |
7402 | 2.34k | } else if (set1->type == LYXP_SET_NUMBER) { |
7403 | 0 | *result = (set1->val.num == set2->val.num); |
7404 | 2.34k | } else { |
7405 | 2.34k | assert(set1->type == LYXP_SET_STRING); |
7406 | 2.34k | *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
7407 | 2.34k | } |
7408 | 2.34k | } else if (op[0] == '!') { |
7409 | 0 | if (set1->type == LYXP_SET_BOOLEAN) { |
7410 | 0 | *result = (set1->val.bln != set2->val.bln); |
7411 | 0 | } else if (set1->type == LYXP_SET_NUMBER) { |
7412 | 0 | *result = (set1->val.num != set2->val.num); |
7413 | 0 | } else { |
7414 | 0 | assert(set1->type == LYXP_SET_STRING); |
7415 | 0 | *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
7416 | 0 | } |
7417 | 0 | } else { |
7418 | 0 | assert(set1->type == LYXP_SET_NUMBER); |
7419 | 0 | if (op[0] == '<') { |
7420 | 0 | if (op[1] == '=') { |
7421 | 0 | *result = (set1->val.num <= set2->val.num); |
7422 | 0 | } else { |
7423 | 0 | *result = (set1->val.num < set2->val.num); |
7424 | 0 | } |
7425 | 0 | } else { |
7426 | 0 | if (op[1] == '=') { |
7427 | 0 | *result = (set1->val.num >= set2->val.num); |
7428 | 0 | } else { |
7429 | 0 | *result = (set1->val.num > set2->val.num); |
7430 | 0 | } |
7431 | 0 | } |
7432 | 0 | } |
7433 | | |
7434 | 2.34k | return LY_SUCCESS; |
7435 | 2.34k | } |
7436 | | |
7437 | | /** |
7438 | | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
7439 | | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
7440 | | * |
7441 | | * @param[in,out] set1 Set to use for the result. |
7442 | | * @param[in] set2 Set acting as the second operand for @p op. |
7443 | | * @param[in] op Operator to process. |
7444 | | * @return LY_ERR |
7445 | | */ |
7446 | | static LY_ERR |
7447 | | moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op) |
7448 | 0 | { |
7449 | 0 | LY_ERR rc; |
7450 | | |
7451 | | /* unary '-' */ |
7452 | 0 | if (!set2 && (op[0] == '-')) { |
7453 | 0 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
7454 | 0 | LY_CHECK_RET(rc); |
7455 | 0 | set1->val.num *= -1; |
7456 | 0 | lyxp_set_free(set2); |
7457 | 0 | return LY_SUCCESS; |
7458 | 0 | } |
7459 | | |
7460 | 0 | assert(set1 && set2); |
7461 | | |
7462 | 0 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
7463 | 0 | LY_CHECK_RET(rc); |
7464 | 0 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
7465 | 0 | LY_CHECK_RET(rc); |
7466 | |
|
7467 | 0 | switch (op[0]) { |
7468 | | /* '+' */ |
7469 | 0 | case '+': |
7470 | 0 | set1->val.num += set2->val.num; |
7471 | 0 | break; |
7472 | | |
7473 | | /* '-' */ |
7474 | 0 | case '-': |
7475 | 0 | set1->val.num -= set2->val.num; |
7476 | 0 | break; |
7477 | | |
7478 | | /* '*' */ |
7479 | 0 | case '*': |
7480 | 0 | set1->val.num *= set2->val.num; |
7481 | 0 | break; |
7482 | | |
7483 | | /* 'div' */ |
7484 | 0 | case 'd': |
7485 | 0 | set1->val.num /= set2->val.num; |
7486 | 0 | break; |
7487 | | |
7488 | | /* 'mod' */ |
7489 | 0 | case 'm': |
7490 | 0 | set1->val.num = fmodl(set1->val.num, set2->val.num); |
7491 | 0 | break; |
7492 | | |
7493 | 0 | default: |
7494 | 0 | LOGINT_RET(set1->ctx); |
7495 | 0 | } |
7496 | | |
7497 | 0 | return LY_SUCCESS; |
7498 | 0 | } |
7499 | | |
7500 | | /** |
7501 | | * @brief Evaluate Predicate. Logs directly on error. |
7502 | | * |
7503 | | * [9] Predicate ::= '[' Expr ']' |
7504 | | * |
7505 | | * @param[in] exp Parsed XPath expression. |
7506 | | * @param[in] tok_idx Position in the expression @p exp. |
7507 | | * @param[in,out] set Context and result set. |
7508 | | * @param[in] options XPath options. |
7509 | | * @param[in] axis Axis to search on. |
7510 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
7511 | | */ |
7512 | | static LY_ERR |
7513 | | eval_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis) |
7514 | 216 | { |
7515 | 216 | LY_ERR rc; |
7516 | 216 | uint32_t i, orig_exp, orig_pos, orig_size; |
7517 | 216 | int32_t pred_in_ctx; |
7518 | 216 | ly_bool reverse_axis = 0; |
7519 | 216 | struct lyxp_set set2 = {0}; |
7520 | | |
7521 | | /* '[' */ |
7522 | 216 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
7523 | 216 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
7524 | 216 | ++(*tok_idx); |
7525 | | |
7526 | 216 | if (options & LYXP_SKIP_EXPR) { |
7527 | 74 | only_parse: |
7528 | 74 | rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR); |
7529 | 74 | LY_CHECK_RET(rc); |
7530 | 216 | } else if (set->type == LYXP_SET_NODE_SET) { |
7531 | | /* we (possibly) need the set sorted, it can affect the result (if the predicate result is a number) */ |
7532 | 216 | assert(!set_sort(set)); |
7533 | | |
7534 | | /* empty set, nothing to evaluate */ |
7535 | 216 | if (!set->used) { |
7536 | 74 | goto only_parse; |
7537 | 74 | } |
7538 | | |
7539 | | /* decide forward or reverse axis */ |
7540 | 142 | switch (axis) { |
7541 | 0 | case LYXP_AXIS_ANCESTOR: |
7542 | 0 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
7543 | 0 | case LYXP_AXIS_PRECEDING: |
7544 | 0 | case LYXP_AXIS_PRECEDING_SIBLING: |
7545 | 0 | reverse_axis = 1; |
7546 | 0 | break; |
7547 | 0 | case LYXP_AXIS_DESCENDANT: |
7548 | 0 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
7549 | 0 | case LYXP_AXIS_FOLLOWING: |
7550 | 0 | case LYXP_AXIS_FOLLOWING_SIBLING: |
7551 | 0 | case LYXP_AXIS_PARENT: |
7552 | 142 | case LYXP_AXIS_CHILD: |
7553 | 142 | case LYXP_AXIS_SELF: |
7554 | 142 | case LYXP_AXIS_ATTRIBUTE: |
7555 | 142 | reverse_axis = 0; |
7556 | 142 | break; |
7557 | 142 | } |
7558 | | |
7559 | 142 | orig_exp = *tok_idx; |
7560 | 142 | orig_pos = reverse_axis ? set->used + 1 : 0; |
7561 | 142 | orig_size = set->used; |
7562 | 2.48k | for (i = 0; i < set->used; ++i) { |
7563 | 2.34k | set_init(&set2, set); |
7564 | 2.34k | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
7565 | | |
7566 | | /* remember the node context position for position() and context size for last() */ |
7567 | 2.34k | orig_pos += reverse_axis ? -1 : 1; |
7568 | | |
7569 | 2.34k | set2.ctx_pos = orig_pos; |
7570 | 2.34k | set2.ctx_size = orig_size; |
7571 | 2.34k | *tok_idx = orig_exp; |
7572 | | |
7573 | 2.34k | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
7574 | 2.34k | if (!rc && set2.not_found) { |
7575 | 0 | set->not_found = 1; |
7576 | 0 | break; |
7577 | 0 | } |
7578 | 2.34k | if (rc) { |
7579 | 0 | lyxp_set_free_content(&set2); |
7580 | 0 | return rc; |
7581 | 0 | } |
7582 | | |
7583 | | /* number is a proximity position */ |
7584 | 2.34k | if (set2.type == LYXP_SET_NUMBER) { |
7585 | 0 | if ((long long)set2.val.num == orig_pos) { |
7586 | 0 | set2.val.num = 1; |
7587 | 0 | } else { |
7588 | 0 | set2.val.num = 0; |
7589 | 0 | } |
7590 | 0 | } |
7591 | 2.34k | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
7592 | | |
7593 | | /* predicate satisfied or not? */ |
7594 | 2.34k | if (!set2.val.bln) { |
7595 | 2.28k | set_remove_node_none(set, i); |
7596 | 2.28k | } |
7597 | 2.34k | } |
7598 | 142 | set_remove_nodes_none(set); |
7599 | | |
7600 | 142 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
7601 | 0 | for (i = 0; i < set->used; ++i) { |
7602 | 0 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
7603 | | /* there is a currently-valid node */ |
7604 | 0 | break; |
7605 | 0 | } |
7606 | 0 | } |
7607 | | /* empty set, nothing to evaluate */ |
7608 | 0 | if (i == set->used) { |
7609 | 0 | goto only_parse; |
7610 | 0 | } |
7611 | | |
7612 | 0 | orig_exp = *tok_idx; |
7613 | | |
7614 | | /* set special in_ctx to all the valid snodes */ |
7615 | 0 | pred_in_ctx = set_scnode_new_in_ctx(set); |
7616 | | |
7617 | | /* use the valid snodes one-by-one */ |
7618 | 0 | for (i = 0; i < set->used; ++i) { |
7619 | 0 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
7620 | 0 | continue; |
7621 | 0 | } |
7622 | 0 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
7623 | |
|
7624 | 0 | *tok_idx = orig_exp; |
7625 | |
|
7626 | 0 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
7627 | 0 | if (!rc && set->not_found) { |
7628 | 0 | break; |
7629 | 0 | } |
7630 | 0 | LY_CHECK_RET(rc); |
7631 | |
|
7632 | 0 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
7633 | 0 | } |
7634 | | |
7635 | | /* restore the state as it was before the predicate */ |
7636 | 0 | for (i = 0; i < set->used; ++i) { |
7637 | 0 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
7638 | 0 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
7639 | 0 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
7640 | 0 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
7641 | 0 | } |
7642 | 0 | } |
7643 | |
|
7644 | 0 | } else { |
7645 | 0 | set2.type = LYXP_SET_NODE_SET; |
7646 | 0 | set_fill_set(&set2, set); |
7647 | |
|
7648 | 0 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
7649 | 0 | if (rc) { |
7650 | 0 | lyxp_set_free_content(&set2); |
7651 | 0 | return rc; |
7652 | 0 | } |
7653 | | |
7654 | 0 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
7655 | 0 | if (!set2.val.bln) { |
7656 | 0 | lyxp_set_free_content(set); |
7657 | 0 | } |
7658 | 0 | lyxp_set_free_content(&set2); |
7659 | 0 | } |
7660 | | |
7661 | | /* ']' */ |
7662 | 216 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
7663 | 216 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
7664 | 216 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
7665 | 216 | ++(*tok_idx); |
7666 | | |
7667 | 216 | return LY_SUCCESS; |
7668 | 216 | } |
7669 | | |
7670 | | /** |
7671 | | * @brief Evaluate Literal. Logs directly on error. |
7672 | | * |
7673 | | * @param[in] exp Parsed XPath expression. |
7674 | | * @param[in] tok_idx Position in the expression @p exp. |
7675 | | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
7676 | | */ |
7677 | | static void |
7678 | | eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set) |
7679 | 2.71k | { |
7680 | 2.71k | if (set) { |
7681 | 2.48k | if (exp->tok_len[*tok_idx] == 2) { |
7682 | 2.48k | set_fill_string(set, "", 0); |
7683 | 2.48k | } else { |
7684 | 0 | set_fill_string(set, &exp->expr[exp->tok_pos[*tok_idx] + 1], exp->tok_len[*tok_idx] - 2); |
7685 | 0 | } |
7686 | 2.48k | } |
7687 | 2.71k | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
7688 | 2.71k | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
7689 | 2.71k | ++(*tok_idx); |
7690 | 2.71k | } |
7691 | | |
7692 | | /** |
7693 | | * @brief Check that a nametest in a predicate matches a key node. |
7694 | | * |
7695 | | * @param[in] nametest Nametest to check. |
7696 | | * @param[in] len Length of @p nametest. |
7697 | | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
7698 | | * @param[in] set Context set. |
7699 | | * @param[in] key Expected key node. |
7700 | | * @return LY_SUCCESS on success, |
7701 | | * @return LY_ENOT if a predicate could not be compiled. |
7702 | | * @return LY_ERR on any error. |
7703 | | */ |
7704 | | static LY_ERR |
7705 | | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
7706 | | const struct lyxp_set *set, const struct lysc_node *key) |
7707 | 171 | { |
7708 | 171 | const struct lys_module *mod; |
7709 | | |
7710 | | /* prefix (module) */ |
7711 | 171 | LY_CHECK_RET(moveto_resolve_module(&nametest, &len, set, ctx_scnode, &mod)); |
7712 | 171 | if (mod && (mod != key->module)) { |
7713 | 0 | return LY_ENOT; |
7714 | 0 | } |
7715 | | |
7716 | | /* node name */ |
7717 | 171 | if (ly_strncmp(key->name, nametest, len)) { |
7718 | 0 | return LY_ENOT; |
7719 | 0 | } |
7720 | | |
7721 | 171 | return LY_SUCCESS; |
7722 | 171 | } |
7723 | | |
7724 | | /** |
7725 | | * @brief Append a simple predicate for the node. |
7726 | | * |
7727 | | * @param[in] exp Full parsed XPath expression. |
7728 | | * @param[in] tok_idx Predicate start index in @p exp. |
7729 | | * @param[in] end_tok_idx Predicate end index in @p exp. |
7730 | | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
7731 | | * @param[in] set Context set. |
7732 | | * @param[in] pred_node Node with the value referenced in the predicate. |
7733 | | * @param[in,out] pred Predicate to append to. |
7734 | | * @param[in,out] pred_len Length of @p pred, is updated. |
7735 | | * @return LY_SUCCESS on success, |
7736 | | * @return LY_ENOT if a predicate could not be compiled. |
7737 | | * @return LY_ERR on any error. |
7738 | | */ |
7739 | | static LY_ERR |
7740 | | eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint32_t tok_idx, uint32_t end_tok_idx, |
7741 | | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
7742 | | uint32_t *pred_len) |
7743 | 334 | { |
7744 | 334 | LY_ERR rc = LY_SUCCESS; |
7745 | 334 | uint32_t i; |
7746 | 334 | const struct lyd_node *siblings; |
7747 | 334 | struct lyd_node *ctx_node; |
7748 | 334 | const struct lysc_node *sparent, *cur_scnode; |
7749 | 334 | struct lyxp_expr *val_exp = NULL; |
7750 | 334 | struct lyxp_set set2 = {0}; |
7751 | 334 | char quot; |
7752 | | |
7753 | | /* duplicate the value expression */ |
7754 | 334 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
7755 | | |
7756 | | /* get its atoms */ |
7757 | 334 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
7758 | 334 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
7759 | 334 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
7760 | | |
7761 | | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
7762 | 1.18k | for (i = 0; i < set2.used; ++i) { |
7763 | 847 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
7764 | | /* skip root and context node */ |
7765 | 505 | continue; |
7766 | 505 | } |
7767 | | |
7768 | | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
7769 | | * context node instance */ |
7770 | 342 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
7771 | | /* 1.1) context node child was accessed on the child axis, certain dependency */ |
7772 | 0 | rc = LY_ENOT; |
7773 | 0 | goto cleanup; |
7774 | 0 | } |
7775 | 342 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
7776 | 0 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
7777 | 0 | if (sparent) { |
7778 | | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
7779 | 0 | rc = LY_ENOT; |
7780 | 0 | goto cleanup; |
7781 | 0 | } |
7782 | 0 | } |
7783 | | |
7784 | | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
7785 | | * but the current node can be safely ignored, it is always the same data instance */ |
7786 | 342 | if ((set2.val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (cur_scnode != set2.val.scnodes[i].scnode)) { |
7787 | 0 | rc = LY_ENOT; |
7788 | 0 | goto cleanup; |
7789 | 0 | } |
7790 | 342 | } |
7791 | | |
7792 | | /* get any data instance of the context node, we checked it makes no difference */ |
7793 | 334 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
7794 | 334 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
7795 | | |
7796 | | /* evaluate the value subexpression with the root context node */ |
7797 | 298 | lyxp_set_free_content(&set2); |
7798 | 298 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
7799 | 298 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
7800 | | |
7801 | | /* cast it into a string */ |
7802 | 298 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
7803 | | |
7804 | | /* append the JSON predicate */ |
7805 | 298 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
7806 | 298 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
7807 | 298 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
7808 | 298 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
7809 | | |
7810 | 334 | cleanup: |
7811 | 334 | lyxp_expr_free(set->ctx, val_exp); |
7812 | 334 | lyxp_set_free_content(&set2); |
7813 | 334 | return rc; |
7814 | 298 | } |
7815 | | |
7816 | | /** |
7817 | | * @brief Try to compile list or leaf-list predicate in the known format to be used for hash-based instance search. |
7818 | | * |
7819 | | * @param[in] exp Full parsed XPath expression. |
7820 | | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
7821 | | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
7822 | | * @param[in] set Context set. |
7823 | | * @param[out] predicates Parsed predicates. |
7824 | | * @return LY_SUCCESS on success, |
7825 | | * @return LY_ENOT if a predicate could not be compiled. |
7826 | | * @return LY_ERR on any error. |
7827 | | */ |
7828 | | static LY_ERR |
7829 | | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode, |
7830 | | const struct lyxp_set *set, struct ly_path_predicate **predicates) |
7831 | 334 | { |
7832 | 334 | LY_ERR rc = LY_SUCCESS; |
7833 | 334 | uint32_t e_idx, val_start_idx, pred_idx = 0, *prev_lo, temp_lo = 0, pred_len = 0, nested_pred; |
7834 | 334 | const struct lysc_node *key; |
7835 | 334 | char *pred = NULL; |
7836 | 334 | struct lyxp_expr *exp2 = NULL; |
7837 | | |
7838 | 334 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
7839 | | |
7840 | | /* turn logging off */ |
7841 | 334 | prev_lo = ly_temp_log_options(&temp_lo); |
7842 | | |
7843 | 334 | if (ctx_scnode->nodetype == LYS_LIST) { |
7844 | | /* check for predicates "[key1=...][key2=...]..." */ |
7845 | | |
7846 | | /* get key count */ |
7847 | 171 | if (ctx_scnode->flags & LYS_KEYLESS) { |
7848 | 0 | rc = LY_ENOT; |
7849 | 0 | goto cleanup; |
7850 | 0 | } |
7851 | | |
7852 | | /* learn where the predicates end */ |
7853 | 171 | e_idx = *tok_idx; |
7854 | 327 | for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) { |
7855 | | /* '[' */ |
7856 | 171 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
7857 | 0 | rc = LY_ENOT; |
7858 | 0 | goto cleanup; |
7859 | 0 | } |
7860 | 171 | ++e_idx; |
7861 | | |
7862 | 171 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
7863 | | /* not a key */ |
7864 | 0 | rc = LY_ENOT; |
7865 | 0 | goto cleanup; |
7866 | 0 | } |
7867 | | |
7868 | | /* check key */ |
7869 | 171 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
7870 | 171 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
7871 | | |
7872 | 171 | ++e_idx; |
7873 | | |
7874 | 171 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
7875 | | /* not '=' */ |
7876 | 0 | rc = LY_ENOT; |
7877 | 0 | goto cleanup; |
7878 | 0 | } |
7879 | 171 | ++e_idx; |
7880 | | |
7881 | | /* value start */ |
7882 | 171 | val_start_idx = e_idx; |
7883 | | |
7884 | | /* ']' */ |
7885 | 171 | nested_pred = 1; |
7886 | 1.19k | do { |
7887 | 1.19k | ++e_idx; |
7888 | | |
7889 | 1.19k | if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
7890 | | /* higher priority than '=' */ |
7891 | 0 | rc = LY_ENOT; |
7892 | 0 | goto cleanup; |
7893 | 1.19k | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
7894 | | /* nested predicate */ |
7895 | 0 | ++nested_pred; |
7896 | 1.19k | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
7897 | | /* predicate end */ |
7898 | 171 | --nested_pred; |
7899 | 171 | } |
7900 | 1.19k | } while (nested_pred); |
7901 | | |
7902 | | /* try to evaluate the value */ |
7903 | 171 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
7904 | 171 | set, key, &pred, &pred_len), cleanup); |
7905 | | |
7906 | 156 | ++e_idx; |
7907 | 156 | } |
7908 | 171 | } else { |
7909 | | /* check for predicate "[.=...]" */ |
7910 | | |
7911 | | /* learn just where this single predicate ends */ |
7912 | 163 | e_idx = *tok_idx; |
7913 | | |
7914 | | /* '[' */ |
7915 | 163 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
7916 | 0 | rc = LY_ENOT; |
7917 | 0 | goto cleanup; |
7918 | 0 | } |
7919 | 163 | ++e_idx; |
7920 | | |
7921 | 163 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
7922 | | /* not the node value */ |
7923 | 0 | rc = LY_ENOT; |
7924 | 0 | goto cleanup; |
7925 | 0 | } |
7926 | 163 | ++e_idx; |
7927 | | |
7928 | 163 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
7929 | | /* not '=' */ |
7930 | 0 | rc = LY_ENOT; |
7931 | 0 | goto cleanup; |
7932 | 0 | } |
7933 | 163 | ++e_idx; |
7934 | | |
7935 | | /* value start */ |
7936 | 163 | val_start_idx = e_idx; |
7937 | | |
7938 | | /* ']' */ |
7939 | 163 | nested_pred = 1; |
7940 | 163 | do { |
7941 | 163 | ++e_idx; |
7942 | | |
7943 | 163 | if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
7944 | | /* higher priority than '=' */ |
7945 | 0 | rc = LY_ENOT; |
7946 | 0 | goto cleanup; |
7947 | 163 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
7948 | | /* nested predicate */ |
7949 | 0 | ++nested_pred; |
7950 | 163 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
7951 | | /* predicate end */ |
7952 | 163 | --nested_pred; |
7953 | 163 | } |
7954 | 163 | } while (nested_pred); |
7955 | | |
7956 | | /* try to evaluate the value */ |
7957 | 163 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
7958 | 163 | ctx_scnode, &pred, &pred_len), cleanup); |
7959 | | |
7960 | 142 | ++e_idx; |
7961 | 142 | } |
7962 | | |
7963 | | /* parse the predicate(s) */ |
7964 | 298 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
7965 | 298 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
7966 | | |
7967 | | /* compile */ |
7968 | 298 | rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2, |
7969 | 298 | &pred_idx, LY_VALUE_JSON, NULL, predicates); |
7970 | 298 | LY_CHECK_GOTO(rc, cleanup); |
7971 | | |
7972 | | /* success, the predicate must include all the needed information for hash-based search */ |
7973 | 156 | *tok_idx = e_idx; |
7974 | | |
7975 | 334 | cleanup: |
7976 | 334 | ly_temp_log_options(prev_lo); |
7977 | 334 | lyxp_expr_free(set->ctx, exp2); |
7978 | 334 | free(pred); |
7979 | 334 | return rc; |
7980 | 156 | } |
7981 | | |
7982 | | /** |
7983 | | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
7984 | | * data node(s) could be found using a single hash-based search. |
7985 | | * |
7986 | | * @param[in] ctx libyang context. |
7987 | | * @param[in] node Next context node to check. |
7988 | | * @param[in] name Expected node name. |
7989 | | * @param[in] name_len Length of @p name. |
7990 | | * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix. |
7991 | | * @param[in] root_type XPath root type. |
7992 | | * @param[in] format Prefix format. |
7993 | | * @param[in,out] found Previously found node, is updated. |
7994 | | * @return LY_SUCCESS on success, |
7995 | | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
7996 | | */ |
7997 | | static LY_ERR |
7998 | | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
7999 | | uint32_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format, |
8000 | | const struct lysc_node **found) |
8001 | 490 | { |
8002 | 490 | const struct lysc_node *scnode, *scnode2; |
8003 | 490 | const struct lys_module *mod; |
8004 | 490 | uint32_t idx = 0; |
8005 | | |
8006 | 490 | assert((format == LY_VALUE_JSON) || moveto_mod); |
8007 | | |
8008 | 490 | continue_search: |
8009 | 490 | scnode = NULL; |
8010 | 490 | if (!node) { |
8011 | 357 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
8012 | | /* search all modules for a single match */ |
8013 | 0 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
8014 | 0 | if (!mod->implemented) { |
8015 | 0 | continue; |
8016 | 0 | } |
8017 | | |
8018 | 0 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
8019 | 0 | if (scnode) { |
8020 | | /* we have found a match */ |
8021 | 0 | break; |
8022 | 0 | } |
8023 | 0 | } |
8024 | |
|
8025 | 0 | if (!scnode) { |
8026 | | /* all modules searched */ |
8027 | 0 | idx = 0; |
8028 | 0 | } |
8029 | 357 | } else { |
8030 | | /* search in top-level */ |
8031 | 357 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
8032 | 357 | } |
8033 | 357 | } else if (node->schema && (!*found || (lysc_data_parent(*found) != node->schema))) { |
8034 | 133 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
8035 | | /* we must adjust the module to inherit the one from the context node */ |
8036 | 0 | moveto_mod = node->schema->module; |
8037 | 0 | } |
8038 | | |
8039 | | /* search in children, do not repeat the same search */ |
8040 | 133 | if (node->schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
8041 | | /* make sure the node is unique, whether in input or output */ |
8042 | 0 | scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0); |
8043 | 0 | scnode2 = lys_find_child(node->schema, moveto_mod, name, name_len, 0, LYS_GETNEXT_OUTPUT); |
8044 | 0 | if (scnode && scnode2) { |
8045 | | /* conflict, do not use hashes */ |
8046 | 0 | scnode = NULL; |
8047 | 0 | } else if (scnode2) { |
8048 | 0 | scnode = scnode2; |
8049 | 0 | } |
8050 | 133 | } else { |
8051 | 133 | scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0); |
8052 | 133 | } |
8053 | 133 | } /* else skip redundant search */ |
8054 | | |
8055 | | /* additional context check */ |
8056 | 490 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
8057 | 0 | scnode = NULL; |
8058 | 0 | } |
8059 | | |
8060 | 490 | if (scnode) { |
8061 | 490 | if (*found) { |
8062 | | /* we found a schema node with the same name but at different level, give up, too complicated |
8063 | | * (more hash-based searches would be required, not supported) */ |
8064 | 0 | return LY_ENOT; |
8065 | 490 | } else { |
8066 | | /* remember the found schema node and continue to make sure it can be used */ |
8067 | 490 | *found = scnode; |
8068 | 490 | } |
8069 | 490 | } |
8070 | | |
8071 | 490 | if (idx) { |
8072 | | /* continue searching all the following modules */ |
8073 | 0 | goto continue_search; |
8074 | 0 | } |
8075 | | |
8076 | 490 | return LY_SUCCESS; |
8077 | 490 | } |
8078 | | |
8079 | | /** |
8080 | | * @brief Generate message when no matching schema nodes were found for a path segment. |
8081 | | * |
8082 | | * @param[in] set XPath set. |
8083 | | * @param[in] scparent Previous schema parent in the context, if only one. |
8084 | | * @param[in] ncname XPath NCName being evaluated. |
8085 | | * @param[in] ncname_len Length of @p ncname. |
8086 | | * @param[in] expr Whole XPath expression. |
8087 | | * @param[in] options XPath options. |
8088 | | */ |
8089 | | static void |
8090 | | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
8091 | | uint32_t ncname_len, const char *expr, uint32_t options) |
8092 | 0 | { |
8093 | 0 | const char *format; |
8094 | 0 | char *path = NULL, *ppath = NULL; |
8095 | |
|
8096 | 0 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
8097 | 0 | if (scparent) { |
8098 | | /* generate path for the parent */ |
8099 | 0 | if (scparent->type == LYXP_NODE_ELEM) { |
8100 | 0 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
8101 | 0 | } else if (scparent->type == LYXP_NODE_ROOT) { |
8102 | 0 | ppath = strdup("<root>"); |
8103 | 0 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
8104 | 0 | ppath = strdup("<config-root>"); |
8105 | 0 | } |
8106 | 0 | } |
8107 | 0 | if (ppath) { |
8108 | 0 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
8109 | 0 | if (options & LYXP_SCNODE_ERROR) { |
8110 | 0 | LOGERR(set->ctx, LY_ENOTFOUND, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
8111 | 0 | } else { |
8112 | 0 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
8113 | 0 | } |
8114 | 0 | } else { |
8115 | 0 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
8116 | 0 | if (options & LYXP_SCNODE_ERROR) { |
8117 | 0 | LOGERR(set->ctx, LY_ENOTFOUND, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
8118 | 0 | } else { |
8119 | 0 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
8120 | 0 | } |
8121 | 0 | } |
8122 | 0 | free(path); |
8123 | 0 | free(ppath); |
8124 | 0 | } |
8125 | | |
8126 | | /** |
8127 | | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
8128 | | * |
8129 | | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
8130 | | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
8131 | | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
8132 | | * |
8133 | | * @param[in] exp Parsed XPath expression. |
8134 | | * @param[in] tok_idx Position in the expression @p exp. |
8135 | | * @param[in] axis What axis to search on. |
8136 | | * @param[in] all_desc Whether to search all the descendants or children only. |
8137 | | * @param[in,out] set Context and result set. |
8138 | | * @param[in] options XPath options. |
8139 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node) |
8140 | | */ |
8141 | | static LY_ERR |
8142 | | eval_name_test_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
8143 | | struct lyxp_set *set, uint32_t options) |
8144 | 729 | { |
8145 | 729 | LY_ERR rc = LY_SUCCESS, r; |
8146 | 729 | const char *ncname, *ncname_dict = NULL; |
8147 | 729 | uint32_t i, ncname_len; |
8148 | 729 | const struct lys_module *moveto_mod = NULL, *moveto_m; |
8149 | 729 | const struct lysc_node *scnode = NULL; |
8150 | 729 | struct ly_path_predicate *predicates = NULL; |
8151 | 729 | int scnode_skip_pred = 0; |
8152 | | |
8153 | 729 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8154 | 729 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8155 | 729 | ++(*tok_idx); |
8156 | | |
8157 | 729 | if (options & LYXP_SKIP_EXPR) { |
8158 | 30 | goto moveto; |
8159 | 30 | } |
8160 | | |
8161 | 699 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
8162 | 699 | ncname_len = exp->tok_len[*tok_idx - 1]; |
8163 | | |
8164 | 699 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
8165 | | /* all nodes will match */ |
8166 | 0 | goto moveto; |
8167 | 0 | } |
8168 | | |
8169 | | /* parse (and skip) module name */ |
8170 | 699 | rc = moveto_resolve_module(&ncname, &ncname_len, set, NULL, &moveto_mod); |
8171 | 699 | LY_CHECK_GOTO(rc, cleanup); |
8172 | | |
8173 | 699 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
8174 | | /* all nodes from the module will match */ |
8175 | 0 | goto moveto; |
8176 | 0 | } |
8177 | | |
8178 | 699 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
8179 | 699 | (set->type == LYXP_SET_NODE_SET)) { |
8180 | | /* find the matching schema node in some parent in the context */ |
8181 | 1.01k | for (i = 0; i < set->used; ++i) { |
8182 | 490 | if (moveto_mod && (set->val.nodes[i].type == LYXP_NODE_ELEM) && |
8183 | 490 | (moveto_mod->ctx != LYD_CTX(set->val.nodes[i].node))) { |
8184 | | /* extension data, use the correct module */ |
8185 | 0 | moveto_m = ly_ctx_get_module_implemented(LYD_CTX(set->val.nodes[i].node), moveto_mod->name); |
8186 | 490 | } else { |
8187 | 490 | moveto_m = moveto_mod; |
8188 | 490 | } |
8189 | 490 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
8190 | 490 | moveto_m, set->root_type, set->format, &scnode)) { |
8191 | | /* check failed */ |
8192 | 0 | scnode = NULL; |
8193 | 0 | break; |
8194 | 0 | } |
8195 | 490 | } |
8196 | | |
8197 | 528 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
8198 | | /* try to create the predicates */ |
8199 | 334 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) { |
8200 | | /* hashes cannot be used */ |
8201 | 178 | scnode = NULL; |
8202 | 178 | } |
8203 | 334 | } |
8204 | 528 | } |
8205 | | |
8206 | 699 | if (!scnode) { |
8207 | | /* we are not able to match based on a schema node and not all the modules match ("*"), |
8208 | | * use dictionary for efficient comparison */ |
8209 | 387 | LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup); |
8210 | 387 | } |
8211 | | |
8212 | 729 | moveto: |
8213 | | /* move to the attribute(s), data node(s), or schema node(s) */ |
8214 | 729 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
8215 | 0 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
8216 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
8217 | 0 | } else { |
8218 | 0 | if (all_desc) { |
8219 | 0 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
8220 | 0 | } else { |
8221 | 0 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
8222 | 0 | } |
8223 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8224 | 0 | } |
8225 | 729 | } else { |
8226 | 729 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
8227 | 171 | const struct lyxp_set_scnode *scparent = NULL; |
8228 | 171 | ly_bool found = 0; |
8229 | | |
8230 | | /* remember parent if there is only one, to print in the warning */ |
8231 | 684 | for (i = 0; i < set->used; ++i) { |
8232 | 513 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
8233 | 171 | if (!scparent) { |
8234 | | /* remember the context node */ |
8235 | 171 | scparent = &set->val.scnodes[i]; |
8236 | 171 | } else { |
8237 | | /* several context nodes, no reasonable error possible */ |
8238 | 0 | scparent = NULL; |
8239 | 0 | break; |
8240 | 0 | } |
8241 | 171 | } |
8242 | 513 | } |
8243 | | |
8244 | 171 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
8245 | | /* efficient evaluation that does not add all the descendants into the set */ |
8246 | 0 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
8247 | 171 | } else { |
8248 | 171 | if (all_desc) { |
8249 | | /* "//" == "/descendant-or-self::node()/" */ |
8250 | 0 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
8251 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8252 | 0 | } |
8253 | 171 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
8254 | 171 | } |
8255 | 171 | LY_CHECK_GOTO(rc, cleanup); |
8256 | | |
8257 | 171 | if (set->used) { |
8258 | 171 | i = set->used; |
8259 | 171 | assert(i); |
8260 | 171 | do { |
8261 | 171 | --i; |
8262 | 171 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
8263 | 171 | found = 1; |
8264 | 171 | break; |
8265 | 171 | } |
8266 | 171 | } while (i); |
8267 | 171 | } |
8268 | 171 | if (!found) { |
8269 | | /* generate message */ |
8270 | 0 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
8271 | |
|
8272 | 0 | if (options & LYXP_SCNODE_ERROR) { |
8273 | | /* error */ |
8274 | 0 | set->not_found = 1; |
8275 | 0 | } |
8276 | | |
8277 | | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
8278 | 0 | rc = LY_ENOT; |
8279 | 0 | scnode_skip_pred = 1; |
8280 | 0 | } |
8281 | 558 | } else { |
8282 | 558 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
8283 | | /* efficient evaluation */ |
8284 | 0 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
8285 | 558 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
8286 | | /* we can find the child nodes using hashes */ |
8287 | 312 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
8288 | 312 | } else { |
8289 | 246 | if (all_desc) { |
8290 | | /* "//" == "/descendant-or-self::node()/" */ |
8291 | 0 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
8292 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8293 | 0 | } |
8294 | 246 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
8295 | 246 | } |
8296 | 558 | LY_CHECK_GOTO(rc, cleanup); |
8297 | 558 | } |
8298 | 729 | } |
8299 | | |
8300 | 729 | if (scnode_skip_pred) { |
8301 | | /* skip predicates */ |
8302 | 0 | options |= LYXP_SKIP_EXPR; |
8303 | 0 | } |
8304 | | |
8305 | | /* Predicate* */ |
8306 | 945 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
8307 | 216 | r = eval_predicate(exp, tok_idx, set, options, axis); |
8308 | 216 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
8309 | 216 | } |
8310 | | |
8311 | 729 | cleanup: |
8312 | 729 | if (scnode_skip_pred) { |
8313 | | /* restore options */ |
8314 | 0 | options &= ~LYXP_SKIP_EXPR; |
8315 | 0 | } |
8316 | 729 | lydict_remove(set->ctx, ncname_dict); |
8317 | 729 | if (predicates) { |
8318 | 156 | ly_path_predicates_free(scnode->module->ctx, predicates); |
8319 | 156 | } |
8320 | 729 | return rc; |
8321 | 729 | } |
8322 | | |
8323 | | /** |
8324 | | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
8325 | | * |
8326 | | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
8327 | | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
8328 | | * [8] NodeType ::= 'text' | 'node' |
8329 | | * |
8330 | | * @param[in] exp Parsed XPath expression. |
8331 | | * @param[in] tok_idx Position in the expression @p exp. |
8332 | | * @param[in] axis Axis to search on. |
8333 | | * @param[in] all_desc Whether to search all the descendants or axis only. |
8334 | | * @param[in,out] set Context and result set. |
8335 | | * @param[in] options XPath options. |
8336 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
8337 | | */ |
8338 | | static LY_ERR |
8339 | | eval_node_type_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
8340 | | struct lyxp_set *set, uint32_t options) |
8341 | 0 | { |
8342 | 0 | LY_ERR rc; |
8343 | |
|
8344 | 0 | (void)all_desc; |
8345 | |
|
8346 | 0 | if (!(options & LYXP_SKIP_EXPR)) { |
8347 | 0 | assert(exp->tok_len[*tok_idx] == 4); |
8348 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
8349 | 0 | rc = xpath_pi_node(set, axis, options); |
8350 | 0 | } else { |
8351 | 0 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
8352 | 0 | rc = xpath_pi_text(set, axis, options); |
8353 | 0 | } |
8354 | 0 | LY_CHECK_RET(rc); |
8355 | 0 | } |
8356 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8357 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8358 | 0 | ++(*tok_idx); |
8359 | | |
8360 | | /* '(' */ |
8361 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
8362 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8363 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8364 | 0 | ++(*tok_idx); |
8365 | | |
8366 | | /* ')' */ |
8367 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
8368 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8369 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8370 | 0 | ++(*tok_idx); |
8371 | | |
8372 | | /* Predicate* */ |
8373 | 0 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
8374 | 0 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
8375 | 0 | LY_CHECK_RET(rc); |
8376 | 0 | } |
8377 | | |
8378 | 0 | return LY_SUCCESS; |
8379 | 0 | } |
8380 | | |
8381 | | /** |
8382 | | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
8383 | | * |
8384 | | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
8385 | | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
8386 | | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
8387 | | * |
8388 | | * @param[in] exp Parsed XPath expression. |
8389 | | * @param[in] tok_idx Position in the expression @p exp. |
8390 | | * @param[in] all_desc Whether to search all the descendants or children only. |
8391 | | * @param[in,out] set Context and result set. |
8392 | | * @param[in] options XPath options. |
8393 | | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
8394 | | */ |
8395 | | static LY_ERR |
8396 | | eval_relative_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
8397 | | uint32_t options) |
8398 | 2.96k | { |
8399 | 2.96k | LY_ERR rc = LY_SUCCESS; |
8400 | 2.96k | enum lyxp_axis axis; |
8401 | 2.96k | int scnode_skip_path = 0; |
8402 | | |
8403 | 2.96k | goto step; |
8404 | 684 | do { |
8405 | | /* evaluate '/' or '//' */ |
8406 | 684 | if (exp->tok_len[*tok_idx] == 1) { |
8407 | 684 | all_desc = 0; |
8408 | 684 | } else { |
8409 | 0 | assert(exp->tok_len[*tok_idx] == 2); |
8410 | 0 | all_desc = 1; |
8411 | 0 | } |
8412 | 684 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8413 | 684 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8414 | 684 | ++(*tok_idx); |
8415 | | |
8416 | 3.64k | step: |
8417 | | /* AxisSpecifier */ |
8418 | 3.64k | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
8419 | 0 | axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]); |
8420 | |
|
8421 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8422 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8423 | 0 | ++(*tok_idx); |
8424 | |
|
8425 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
8426 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8427 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8428 | 0 | ++(*tok_idx); |
8429 | 3.64k | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
8430 | 0 | axis = LYXP_AXIS_ATTRIBUTE; |
8431 | |
|
8432 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8433 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8434 | 0 | ++(*tok_idx); |
8435 | 3.64k | } else { |
8436 | | /* default */ |
8437 | 3.64k | axis = LYXP_AXIS_CHILD; |
8438 | 3.64k | } |
8439 | | |
8440 | | /* NodeTest Predicate* */ |
8441 | 3.64k | switch (exp->tokens[*tok_idx]) { |
8442 | 2.40k | case LYXP_TOKEN_DOT: |
8443 | | /* evaluate '.' */ |
8444 | 2.40k | if (!(options & LYXP_SKIP_EXPR)) { |
8445 | 2.34k | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
8446 | 2.34k | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
8447 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
8448 | 0 | rc = LY_EVALID; |
8449 | 0 | goto cleanup; |
8450 | 0 | } |
8451 | | |
8452 | 2.34k | if (all_desc) { |
8453 | 0 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
8454 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8455 | 0 | } |
8456 | 2.34k | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
8457 | 2.34k | LY_CHECK_GOTO(rc, cleanup); |
8458 | 2.34k | } |
8459 | 2.40k | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
8460 | 2.40k | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8461 | 2.40k | ++(*tok_idx); |
8462 | 2.40k | break; |
8463 | | |
8464 | 513 | case LYXP_TOKEN_DDOT: |
8465 | | /* evaluate '..' */ |
8466 | 513 | if (!(options & LYXP_SKIP_EXPR)) { |
8467 | 498 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
8468 | 498 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
8469 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
8470 | 0 | rc = LY_EVALID; |
8471 | 0 | goto cleanup; |
8472 | 0 | } |
8473 | | |
8474 | 498 | if (all_desc) { |
8475 | 0 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
8476 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8477 | 0 | } |
8478 | 498 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
8479 | 498 | LY_CHECK_GOTO(rc, cleanup); |
8480 | 498 | } |
8481 | 513 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8482 | 513 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8483 | 513 | ++(*tok_idx); |
8484 | 513 | break; |
8485 | | |
8486 | 729 | case LYXP_TOKEN_NAMETEST: |
8487 | | /* evaluate NameTest Predicate* */ |
8488 | 729 | rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options); |
8489 | 729 | if (rc == LY_ENOT) { |
8490 | 0 | assert(options & LYXP_SCNODE_ALL); |
8491 | 0 | rc = LY_SUCCESS; |
8492 | | |
8493 | | /* skip the rest of this path */ |
8494 | 0 | scnode_skip_path = 1; |
8495 | 0 | options |= LYXP_SKIP_EXPR; |
8496 | 0 | } |
8497 | 729 | LY_CHECK_GOTO(rc, cleanup); |
8498 | 729 | break; |
8499 | | |
8500 | 0 | case LYXP_TOKEN_NODETYPE: |
8501 | | /* evaluate NodeType Predicate* */ |
8502 | 0 | rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options); |
8503 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8504 | 0 | break; |
8505 | | |
8506 | 0 | default: |
8507 | 0 | LOGINT(set->ctx); |
8508 | 0 | rc = LY_EINT; |
8509 | 0 | goto cleanup; |
8510 | 3.64k | } |
8511 | 3.64k | } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
8512 | | |
8513 | 2.96k | cleanup: |
8514 | 2.96k | if (scnode_skip_path) { |
8515 | 0 | options &= ~LYXP_SKIP_EXPR; |
8516 | 0 | } |
8517 | 2.96k | return rc; |
8518 | 0 | } |
8519 | | |
8520 | | /** |
8521 | | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
8522 | | * |
8523 | | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
8524 | | * |
8525 | | * @param[in] exp Parsed XPath expression. |
8526 | | * @param[in] tok_idx Position in the expression @p exp. |
8527 | | * @param[in,out] set Context and result set. |
8528 | | * @param[in] options XPath options. |
8529 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
8530 | | */ |
8531 | | static LY_ERR |
8532 | | eval_absolute_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
8533 | 30 | { |
8534 | 30 | ly_bool all_desc; |
8535 | | |
8536 | 30 | if (!(options & LYXP_SKIP_EXPR)) { |
8537 | | /* no matter what tokens follow, we need to be at the root */ |
8538 | 30 | LY_CHECK_RET(moveto_root(set, options)); |
8539 | 30 | } |
8540 | | |
8541 | | /* '/' RelativeLocationPath? */ |
8542 | 30 | if (exp->tok_len[*tok_idx] == 1) { |
8543 | | /* evaluate '/' - deferred */ |
8544 | 30 | all_desc = 0; |
8545 | 30 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8546 | 30 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8547 | 30 | ++(*tok_idx); |
8548 | | |
8549 | 30 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
8550 | 0 | return LY_SUCCESS; |
8551 | 0 | } |
8552 | 30 | switch (exp->tokens[*tok_idx]) { |
8553 | 0 | case LYXP_TOKEN_DOT: |
8554 | 0 | case LYXP_TOKEN_DDOT: |
8555 | 0 | case LYXP_TOKEN_AXISNAME: |
8556 | 0 | case LYXP_TOKEN_AT: |
8557 | 30 | case LYXP_TOKEN_NAMETEST: |
8558 | 30 | case LYXP_TOKEN_NODETYPE: |
8559 | 30 | LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options)); |
8560 | 30 | break; |
8561 | 0 | default: |
8562 | 0 | break; |
8563 | 30 | } |
8564 | | |
8565 | 30 | } else { |
8566 | | /* '//' RelativeLocationPath */ |
8567 | | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
8568 | 0 | all_desc = 1; |
8569 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8570 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8571 | 0 | ++(*tok_idx); |
8572 | |
|
8573 | 0 | LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options)); |
8574 | 0 | } |
8575 | | |
8576 | 30 | return LY_SUCCESS; |
8577 | 30 | } |
8578 | | |
8579 | | /** |
8580 | | * @brief Evaluate FunctionCall. Logs directly on error. |
8581 | | * |
8582 | | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
8583 | | * |
8584 | | * @param[in] exp Parsed XPath expression. |
8585 | | * @param[in] tok_idx Position in the expression @p exp. |
8586 | | * @param[in,out] set Context and result set. |
8587 | | * @param[in] options XPath options. |
8588 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
8589 | | */ |
8590 | | static LY_ERR |
8591 | | eval_function_call(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
8592 | 342 | { |
8593 | 342 | LY_ERR rc; |
8594 | | |
8595 | 342 | LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL; |
8596 | 342 | uint32_t arg_count = 0, i; |
8597 | 342 | struct lyxp_set **args = NULL, **args_aux; |
8598 | | |
8599 | 342 | if (!(options & LYXP_SKIP_EXPR)) { |
8600 | | /* FunctionName */ |
8601 | 327 | switch (exp->tok_len[*tok_idx]) { |
8602 | 0 | case 3: |
8603 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
8604 | 0 | xpath_func = &xpath_not; |
8605 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
8606 | 0 | xpath_func = &xpath_sum; |
8607 | 0 | } |
8608 | 0 | break; |
8609 | 0 | case 4: |
8610 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
8611 | 0 | xpath_func = &xpath_lang; |
8612 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
8613 | 0 | xpath_func = &xpath_last; |
8614 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
8615 | 0 | xpath_func = &xpath_name; |
8616 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
8617 | 0 | xpath_func = &xpath_true; |
8618 | 0 | } |
8619 | 0 | break; |
8620 | 0 | case 5: |
8621 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
8622 | 0 | xpath_func = &xpath_count; |
8623 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
8624 | 0 | xpath_func = &xpath_false; |
8625 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
8626 | 0 | xpath_func = &xpath_floor; |
8627 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
8628 | 0 | xpath_func = &xpath_round; |
8629 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
8630 | 0 | xpath_func = &xpath_deref; |
8631 | 0 | } |
8632 | 0 | break; |
8633 | 0 | case 6: |
8634 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
8635 | 0 | xpath_func = &xpath_concat; |
8636 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
8637 | 0 | xpath_func = &xpath_number; |
8638 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
8639 | 0 | xpath_func = &xpath_string; |
8640 | 0 | } |
8641 | 0 | break; |
8642 | 327 | case 7: |
8643 | 327 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
8644 | 0 | xpath_func = &xpath_boolean; |
8645 | 327 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
8646 | 0 | xpath_func = &xpath_ceiling; |
8647 | 327 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
8648 | 327 | xpath_func = &xpath_current; |
8649 | 327 | } |
8650 | 327 | break; |
8651 | 0 | case 8: |
8652 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
8653 | 0 | xpath_func = &xpath_contains; |
8654 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
8655 | 0 | xpath_func = &xpath_position; |
8656 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
8657 | 0 | xpath_func = &xpath_re_match; |
8658 | 0 | } |
8659 | 0 | break; |
8660 | 0 | case 9: |
8661 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
8662 | 0 | xpath_func = &xpath_substring; |
8663 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
8664 | 0 | xpath_func = &xpath_translate; |
8665 | 0 | } |
8666 | 0 | break; |
8667 | 0 | case 10: |
8668 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
8669 | 0 | xpath_func = &xpath_local_name; |
8670 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
8671 | 0 | xpath_func = &xpath_enum_value; |
8672 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
8673 | 0 | xpath_func = &xpath_bit_is_set; |
8674 | 0 | } |
8675 | 0 | break; |
8676 | 0 | case 11: |
8677 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
8678 | 0 | xpath_func = &xpath_starts_with; |
8679 | 0 | } |
8680 | 0 | break; |
8681 | 0 | case 12: |
8682 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
8683 | 0 | xpath_func = &xpath_derived_from; |
8684 | 0 | } |
8685 | 0 | break; |
8686 | 0 | case 13: |
8687 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
8688 | 0 | xpath_func = &xpath_namespace_uri; |
8689 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
8690 | 0 | xpath_func = &xpath_string_length; |
8691 | 0 | } |
8692 | 0 | break; |
8693 | 0 | case 15: |
8694 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
8695 | 0 | xpath_func = &xpath_normalize_space; |
8696 | 0 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
8697 | 0 | xpath_func = &xpath_substring_after; |
8698 | 0 | } |
8699 | 0 | break; |
8700 | 0 | case 16: |
8701 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
8702 | 0 | xpath_func = &xpath_substring_before; |
8703 | 0 | } |
8704 | 0 | break; |
8705 | 0 | case 20: |
8706 | 0 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
8707 | 0 | xpath_func = &xpath_derived_from_or_self; |
8708 | 0 | } |
8709 | 0 | break; |
8710 | 327 | } |
8711 | | |
8712 | 327 | if (!xpath_func) { |
8713 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
8714 | 0 | return LY_EVALID; |
8715 | 0 | } |
8716 | 327 | } |
8717 | | |
8718 | 342 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8719 | 342 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8720 | 342 | ++(*tok_idx); |
8721 | | |
8722 | | /* '(' */ |
8723 | 342 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
8724 | 342 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8725 | 342 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8726 | 342 | ++(*tok_idx); |
8727 | | |
8728 | | /* ( Expr ( ',' Expr )* )? */ |
8729 | 342 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
8730 | 0 | if (!(options & LYXP_SKIP_EXPR)) { |
8731 | 0 | args = malloc(sizeof *args); |
8732 | 0 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
8733 | 0 | arg_count = 1; |
8734 | 0 | args[0] = set_copy(set); |
8735 | 0 | if (!args[0]) { |
8736 | 0 | rc = LY_EMEM; |
8737 | 0 | goto cleanup; |
8738 | 0 | } |
8739 | | |
8740 | 0 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
8741 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8742 | 0 | set->not_found = args[0]->not_found; |
8743 | 0 | } else { |
8744 | 0 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
8745 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8746 | 0 | } |
8747 | 0 | } |
8748 | 342 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
8749 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8750 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8751 | 0 | ++(*tok_idx); |
8752 | |
|
8753 | 0 | if (!(options & LYXP_SKIP_EXPR)) { |
8754 | 0 | ++arg_count; |
8755 | 0 | args_aux = realloc(args, arg_count * sizeof *args); |
8756 | 0 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
8757 | 0 | args = args_aux; |
8758 | 0 | args[arg_count - 1] = set_copy(set); |
8759 | 0 | if (!args[arg_count - 1]) { |
8760 | 0 | rc = LY_EMEM; |
8761 | 0 | goto cleanup; |
8762 | 0 | } |
8763 | | |
8764 | 0 | rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options); |
8765 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8766 | 0 | if (args[arg_count - 1]->not_found) { |
8767 | 0 | set->not_found = 1; |
8768 | 0 | } |
8769 | 0 | } else { |
8770 | 0 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
8771 | 0 | LY_CHECK_GOTO(rc, cleanup); |
8772 | 0 | } |
8773 | 0 | } |
8774 | | |
8775 | | /* ')' */ |
8776 | 342 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
8777 | 342 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8778 | 342 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8779 | 342 | ++(*tok_idx); |
8780 | | |
8781 | 342 | if (!(options & LYXP_SKIP_EXPR)) { |
8782 | | /* evaluate function */ |
8783 | 327 | rc = xpath_func(args, arg_count, set, options); |
8784 | | |
8785 | 327 | if (options & LYXP_SCNODE_ALL) { |
8786 | | /* merge all nodes from arg evaluations */ |
8787 | 171 | for (i = 0; i < arg_count; ++i) { |
8788 | 0 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
8789 | 0 | lyxp_set_scnode_merge(set, args[i]); |
8790 | 0 | } |
8791 | 171 | } |
8792 | 327 | } else { |
8793 | 15 | rc = LY_SUCCESS; |
8794 | 15 | } |
8795 | | |
8796 | 342 | cleanup: |
8797 | 342 | for (i = 0; i < arg_count; ++i) { |
8798 | 0 | lyxp_set_free(args[i]); |
8799 | 0 | } |
8800 | 342 | free(args); |
8801 | 342 | return rc; |
8802 | 342 | } |
8803 | | |
8804 | | /** |
8805 | | * @brief Evaluate Number. Logs directly on error. |
8806 | | * |
8807 | | * @param[in] ctx Context for errors. |
8808 | | * @param[in] exp Parsed XPath expression. |
8809 | | * @param[in] tok_idx Position in the expression @p exp. |
8810 | | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
8811 | | * @return LY_ERR |
8812 | | */ |
8813 | | static LY_ERR |
8814 | | eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set) |
8815 | 0 | { |
8816 | 0 | long double num; |
8817 | 0 | char *endptr; |
8818 | |
|
8819 | 0 | if (set) { |
8820 | 0 | errno = 0; |
8821 | 0 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
8822 | 0 | if (errno) { |
8823 | 0 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
8824 | 0 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
8825 | 0 | (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno)); |
8826 | 0 | return LY_EVALID; |
8827 | 0 | } else if ((uint32_t)(endptr - &exp->expr[exp->tok_pos[*tok_idx]]) != exp->tok_len[*tok_idx]) { |
8828 | 0 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
8829 | 0 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
8830 | 0 | (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
8831 | 0 | return LY_EVALID; |
8832 | 0 | } |
8833 | | |
8834 | 0 | set_fill_number(set, num); |
8835 | 0 | } |
8836 | | |
8837 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
8838 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8839 | 0 | ++(*tok_idx); |
8840 | 0 | return LY_SUCCESS; |
8841 | 0 | } |
8842 | | |
8843 | | LY_ERR |
8844 | | lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len, |
8845 | | struct lyxp_var **var) |
8846 | 0 | { |
8847 | 0 | LY_ARRAY_COUNT_TYPE u; |
8848 | |
|
8849 | 0 | assert(name); |
8850 | | |
8851 | 0 | if (!name_len) { |
8852 | 0 | name_len = strlen(name); |
8853 | 0 | } |
8854 | |
|
8855 | 0 | LY_ARRAY_FOR(vars, u) { |
8856 | 0 | if (!strncmp(vars[u].name, name, name_len)) { |
8857 | 0 | if (var) { |
8858 | 0 | *var = (struct lyxp_var *)&vars[u]; |
8859 | 0 | } |
8860 | 0 | return LY_SUCCESS; |
8861 | 0 | } |
8862 | 0 | } |
8863 | | |
8864 | 0 | if (ctx) { |
8865 | 0 | LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name); |
8866 | 0 | } |
8867 | 0 | return LY_ENOTFOUND; |
8868 | 0 | } |
8869 | | |
8870 | | /** |
8871 | | * @brief Evaluate VariableReference. |
8872 | | * |
8873 | | * @param[in] exp Parsed XPath expression. |
8874 | | * @param[in] tok_idx Position in the expression @p exp. |
8875 | | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
8876 | | * @param[in,out] set Context and result set. |
8877 | | * @param[in] options XPath options. |
8878 | | * @return LY_ERR value. |
8879 | | */ |
8880 | | static LY_ERR |
8881 | | eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
8882 | 0 | { |
8883 | 0 | LY_ERR ret; |
8884 | 0 | const char *name; |
8885 | 0 | struct lyxp_var *var; |
8886 | 0 | struct lyxp_expr *tokens = NULL; |
8887 | 0 | uint32_t token_index, name_len; |
8888 | | |
8889 | | /* find out the name and value of the variable */ |
8890 | 0 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
8891 | 0 | name_len = exp->tok_len[*tok_idx]; |
8892 | 0 | ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var); |
8893 | 0 | LY_CHECK_RET(ret); |
8894 | | |
8895 | | /* parse value */ |
8896 | 0 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
8897 | 0 | LY_CHECK_GOTO(ret, cleanup); |
8898 | | |
8899 | | /* evaluate value */ |
8900 | 0 | token_index = 0; |
8901 | 0 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
8902 | 0 | LY_CHECK_GOTO(ret, cleanup); |
8903 | |
|
8904 | 0 | cleanup: |
8905 | 0 | lyxp_expr_free(set->ctx, tokens); |
8906 | |
|
8907 | 0 | return ret; |
8908 | 0 | } |
8909 | | |
8910 | | /** |
8911 | | * @brief Evaluate PathExpr. Logs directly on error. |
8912 | | * |
8913 | | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
8914 | | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
8915 | | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
8916 | | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
8917 | | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
8918 | | * |
8919 | | * @param[in] exp Parsed XPath expression. |
8920 | | * @param[in] tok_idx Position in the expression @p exp. |
8921 | | * @param[in,out] set Context and result set. |
8922 | | * @param[in] options XPath options. |
8923 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
8924 | | */ |
8925 | | static LY_ERR |
8926 | | eval_path_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
8927 | 5.67k | { |
8928 | 5.67k | ly_bool all_desc; |
8929 | 5.67k | LY_ERR rc; |
8930 | | |
8931 | 5.67k | switch (exp->tokens[*tok_idx]) { |
8932 | 0 | case LYXP_TOKEN_PAR1: |
8933 | | /* '(' Expr ')' */ |
8934 | | |
8935 | | /* '(' */ |
8936 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8937 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8938 | 0 | ++(*tok_idx); |
8939 | | |
8940 | | /* Expr */ |
8941 | 0 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
8942 | 0 | LY_CHECK_RET(rc); |
8943 | | |
8944 | | /* ')' */ |
8945 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
8946 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
8947 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
8948 | 0 | ++(*tok_idx); |
8949 | |
|
8950 | 0 | goto predicate; |
8951 | | |
8952 | 2.40k | case LYXP_TOKEN_DOT: |
8953 | 2.57k | case LYXP_TOKEN_DDOT: |
8954 | 2.57k | case LYXP_TOKEN_AXISNAME: |
8955 | 2.57k | case LYXP_TOKEN_AT: |
8956 | 2.59k | case LYXP_TOKEN_NAMETEST: |
8957 | 2.59k | case LYXP_TOKEN_NODETYPE: |
8958 | | /* RelativeLocationPath */ |
8959 | 2.59k | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
8960 | 2.59k | LY_CHECK_RET(rc); |
8961 | 2.59k | break; |
8962 | | |
8963 | 0 | case LYXP_TOKEN_VARREF: |
8964 | | /* VariableReference */ |
8965 | 0 | rc = eval_variable_reference(exp, tok_idx, set, options); |
8966 | 0 | LY_CHECK_RET(rc); |
8967 | 0 | ++(*tok_idx); |
8968 | |
|
8969 | 0 | goto predicate; |
8970 | | |
8971 | 342 | case LYXP_TOKEN_FUNCNAME: |
8972 | | /* FunctionCall */ |
8973 | 342 | rc = eval_function_call(exp, tok_idx, set, options); |
8974 | 342 | LY_CHECK_RET(rc); |
8975 | | |
8976 | 342 | goto predicate; |
8977 | | |
8978 | 30 | case LYXP_TOKEN_OPER_PATH: |
8979 | 30 | case LYXP_TOKEN_OPER_RPATH: |
8980 | | /* AbsoluteLocationPath */ |
8981 | 30 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
8982 | 30 | LY_CHECK_RET(rc); |
8983 | 30 | break; |
8984 | | |
8985 | 2.71k | case LYXP_TOKEN_LITERAL: |
8986 | | /* Literal */ |
8987 | 2.71k | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
8988 | 222 | if (!(options & LYXP_SKIP_EXPR)) { |
8989 | 163 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
8990 | 163 | } |
8991 | 222 | eval_literal(exp, tok_idx, NULL); |
8992 | 2.48k | } else { |
8993 | 2.48k | eval_literal(exp, tok_idx, set); |
8994 | 2.48k | } |
8995 | | |
8996 | 2.71k | goto predicate; |
8997 | | |
8998 | 0 | case LYXP_TOKEN_NUMBER: |
8999 | | /* Number */ |
9000 | 0 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
9001 | 0 | if (!(options & LYXP_SKIP_EXPR)) { |
9002 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
9003 | 0 | } |
9004 | 0 | rc = eval_number(NULL, exp, tok_idx, NULL); |
9005 | 0 | } else { |
9006 | 0 | rc = eval_number(set->ctx, exp, tok_idx, set); |
9007 | 0 | } |
9008 | 0 | LY_CHECK_RET(rc); |
9009 | |
|
9010 | 0 | goto predicate; |
9011 | | |
9012 | 0 | default: |
9013 | 0 | LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
9014 | 0 | return LY_EVALID; |
9015 | 5.67k | } |
9016 | | |
9017 | 2.62k | return LY_SUCCESS; |
9018 | | |
9019 | 3.05k | predicate: |
9020 | | /* Predicate* */ |
9021 | 3.05k | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
9022 | 0 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
9023 | 0 | LY_CHECK_RET(rc); |
9024 | 0 | } |
9025 | | |
9026 | | /* ('/' or '//') RelativeLocationPath */ |
9027 | 3.05k | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
9028 | | |
9029 | | /* evaluate '/' or '//' */ |
9030 | 342 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
9031 | 342 | all_desc = 0; |
9032 | 342 | } else { |
9033 | 0 | all_desc = 1; |
9034 | 0 | } |
9035 | | |
9036 | 342 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
9037 | 342 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9038 | 342 | ++(*tok_idx); |
9039 | | |
9040 | 342 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
9041 | 342 | LY_CHECK_RET(rc); |
9042 | 342 | } |
9043 | | |
9044 | 3.05k | return LY_SUCCESS; |
9045 | 3.05k | } |
9046 | | |
9047 | | /** |
9048 | | * @brief Evaluate UnionExpr. Logs directly on error. |
9049 | | * |
9050 | | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
9051 | | * |
9052 | | * @param[in] exp Parsed XPath expression. |
9053 | | * @param[in] tok_idx Position in the expression @p exp. |
9054 | | * @param[in] repeat How many times this expression is repeated. |
9055 | | * @param[in,out] set Context and result set. |
9056 | | * @param[in] options XPath options. |
9057 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9058 | | */ |
9059 | | static LY_ERR |
9060 | | eval_union_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
9061 | 0 | { |
9062 | 0 | LY_ERR rc = LY_SUCCESS; |
9063 | 0 | uint32_t i; |
9064 | 0 | struct lyxp_set orig_set, set2; |
9065 | 0 | ly_bool found = 0; |
9066 | |
|
9067 | 0 | assert(repeat); |
9068 | | |
9069 | 0 | set_init(&orig_set, set); |
9070 | 0 | set_init(&set2, set); |
9071 | |
|
9072 | 0 | set_fill_set(&orig_set, set); |
9073 | |
|
9074 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
9075 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9076 | 0 | if (set->not_found) { |
9077 | 0 | set->not_found = 0; |
9078 | 0 | } else { |
9079 | 0 | found = 1; |
9080 | 0 | } |
9081 | | |
9082 | | /* ('|' PathExpr)* */ |
9083 | 0 | for (i = 0; i < repeat; ++i) { |
9084 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
9085 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
9086 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9087 | 0 | ++(*tok_idx); |
9088 | |
|
9089 | 0 | if (options & LYXP_SKIP_EXPR) { |
9090 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
9091 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9092 | 0 | continue; |
9093 | 0 | } |
9094 | | |
9095 | 0 | set_fill_set(&set2, &orig_set); |
9096 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
9097 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9098 | 0 | if (!set2.not_found) { |
9099 | 0 | found = 1; |
9100 | 0 | } |
9101 | | |
9102 | | /* eval */ |
9103 | 0 | if (options & LYXP_SCNODE_ALL) { |
9104 | 0 | lyxp_set_scnode_merge(set, &set2); |
9105 | 0 | } else { |
9106 | 0 | rc = moveto_union(set, &set2); |
9107 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9108 | 0 | } |
9109 | 0 | } |
9110 | | |
9111 | 0 | cleanup: |
9112 | 0 | lyxp_set_free_content(&orig_set); |
9113 | 0 | lyxp_set_free_content(&set2); |
9114 | 0 | if (!found) { |
9115 | 0 | set->not_found = 1; |
9116 | 0 | } |
9117 | 0 | return rc; |
9118 | 0 | } |
9119 | | |
9120 | | /** |
9121 | | * @brief Evaluate UnaryExpr. Logs directly on error. |
9122 | | * |
9123 | | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
9124 | | * |
9125 | | * @param[in] exp Parsed XPath expression. |
9126 | | * @param[in] tok_idx Position in the expression @p exp. |
9127 | | * @param[in] repeat How many times this expression is repeated. |
9128 | | * @param[in,out] set Context and result set. |
9129 | | * @param[in] options XPath options. |
9130 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9131 | | */ |
9132 | | static LY_ERR |
9133 | | eval_unary_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
9134 | 0 | { |
9135 | 0 | LY_ERR rc; |
9136 | 0 | uint32_t this_op, i; |
9137 | |
|
9138 | 0 | assert(repeat); |
9139 | | |
9140 | | /* ('-')+ */ |
9141 | 0 | this_op = *tok_idx; |
9142 | 0 | for (i = 0; i < repeat; ++i) { |
9143 | 0 | assert(!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && (exp->expr[exp->tok_pos[*tok_idx]] == '-')); |
9144 | | |
9145 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
9146 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9147 | 0 | ++(*tok_idx); |
9148 | 0 | } |
9149 | | |
9150 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
9151 | 0 | LY_CHECK_RET(rc); |
9152 | |
|
9153 | 0 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
9154 | 0 | if (options & LYXP_SCNODE_ALL) { |
9155 | 0 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
9156 | 0 | } else { |
9157 | 0 | rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]); |
9158 | 0 | LY_CHECK_RET(rc); |
9159 | 0 | } |
9160 | 0 | } |
9161 | | |
9162 | 0 | return LY_SUCCESS; |
9163 | 0 | } |
9164 | | |
9165 | | /** |
9166 | | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
9167 | | * |
9168 | | * [18] MultiplicativeExpr ::= UnaryExpr |
9169 | | * | MultiplicativeExpr '*' UnaryExpr |
9170 | | * | MultiplicativeExpr 'div' UnaryExpr |
9171 | | * | MultiplicativeExpr 'mod' UnaryExpr |
9172 | | * |
9173 | | * @param[in] exp Parsed XPath expression. |
9174 | | * @param[in] tok_idx Position in the expression @p exp. |
9175 | | * @param[in] repeat How many times this expression is repeated. |
9176 | | * @param[in,out] set Context and result set. |
9177 | | * @param[in] options XPath options. |
9178 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9179 | | */ |
9180 | | static LY_ERR |
9181 | | eval_multiplicative_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, |
9182 | | uint32_t options) |
9183 | 0 | { |
9184 | 0 | LY_ERR rc = LY_SUCCESS; |
9185 | 0 | uint32_t i, this_op; |
9186 | 0 | struct lyxp_set orig_set, set2; |
9187 | |
|
9188 | 0 | assert(repeat); |
9189 | | |
9190 | 0 | set_init(&orig_set, set); |
9191 | 0 | set_init(&set2, set); |
9192 | |
|
9193 | 0 | set_fill_set(&orig_set, set); |
9194 | |
|
9195 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
9196 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9197 | | |
9198 | | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
9199 | 0 | for (i = 0; i < repeat; ++i) { |
9200 | 0 | this_op = *tok_idx; |
9201 | |
|
9202 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
9203 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
9204 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9205 | 0 | ++(*tok_idx); |
9206 | |
|
9207 | 0 | if (options & LYXP_SKIP_EXPR) { |
9208 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
9209 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9210 | 0 | continue; |
9211 | 0 | } |
9212 | | |
9213 | 0 | set_fill_set(&set2, &orig_set); |
9214 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
9215 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9216 | 0 | if (set2.not_found) { |
9217 | 0 | set->not_found = 1; |
9218 | 0 | } |
9219 | | |
9220 | | /* eval */ |
9221 | 0 | if (options & LYXP_SCNODE_ALL) { |
9222 | 0 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
9223 | 0 | lyxp_set_scnode_merge(set, &set2); |
9224 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
9225 | 0 | } else { |
9226 | 0 | rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
9227 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9228 | 0 | } |
9229 | 0 | } |
9230 | | |
9231 | 0 | cleanup: |
9232 | 0 | lyxp_set_free_content(&orig_set); |
9233 | 0 | lyxp_set_free_content(&set2); |
9234 | 0 | return rc; |
9235 | 0 | } |
9236 | | |
9237 | | /** |
9238 | | * @brief Evaluate AdditiveExpr. Logs directly on error. |
9239 | | * |
9240 | | * [17] AdditiveExpr ::= MultiplicativeExpr |
9241 | | * | AdditiveExpr '+' MultiplicativeExpr |
9242 | | * | AdditiveExpr '-' MultiplicativeExpr |
9243 | | * |
9244 | | * @param[in] exp Parsed XPath expression. |
9245 | | * @param[in] tok_idx Position in the expression @p exp. |
9246 | | * @param[in] repeat How many times this expression is repeated. |
9247 | | * @param[in,out] set Context and result set. |
9248 | | * @param[in] options XPath options. |
9249 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9250 | | */ |
9251 | | static LY_ERR |
9252 | | eval_additive_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
9253 | 0 | { |
9254 | 0 | LY_ERR rc = LY_SUCCESS; |
9255 | 0 | uint32_t i, this_op; |
9256 | 0 | struct lyxp_set orig_set, set2; |
9257 | |
|
9258 | 0 | assert(repeat); |
9259 | | |
9260 | 0 | set_init(&orig_set, set); |
9261 | 0 | set_init(&set2, set); |
9262 | |
|
9263 | 0 | set_fill_set(&orig_set, set); |
9264 | |
|
9265 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
9266 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9267 | | |
9268 | | /* ('+' / '-' MultiplicativeExpr)* */ |
9269 | 0 | for (i = 0; i < repeat; ++i) { |
9270 | 0 | this_op = *tok_idx; |
9271 | |
|
9272 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
9273 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
9274 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9275 | 0 | ++(*tok_idx); |
9276 | |
|
9277 | 0 | if (options & LYXP_SKIP_EXPR) { |
9278 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
9279 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9280 | 0 | continue; |
9281 | 0 | } |
9282 | | |
9283 | 0 | set_fill_set(&set2, &orig_set); |
9284 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
9285 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9286 | 0 | if (set2.not_found) { |
9287 | 0 | set->not_found = 1; |
9288 | 0 | } |
9289 | | |
9290 | | /* eval */ |
9291 | 0 | if (options & LYXP_SCNODE_ALL) { |
9292 | 0 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
9293 | 0 | lyxp_set_scnode_merge(set, &set2); |
9294 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
9295 | 0 | } else { |
9296 | 0 | rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
9297 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9298 | 0 | } |
9299 | 0 | } |
9300 | | |
9301 | 0 | cleanup: |
9302 | 0 | lyxp_set_free_content(&orig_set); |
9303 | 0 | lyxp_set_free_content(&set2); |
9304 | 0 | return rc; |
9305 | 0 | } |
9306 | | |
9307 | | /** |
9308 | | * @brief Evaluate RelationalExpr. Logs directly on error. |
9309 | | * |
9310 | | * [16] RelationalExpr ::= AdditiveExpr |
9311 | | * | RelationalExpr '<' AdditiveExpr |
9312 | | * | RelationalExpr '>' AdditiveExpr |
9313 | | * | RelationalExpr '<=' AdditiveExpr |
9314 | | * | RelationalExpr '>=' AdditiveExpr |
9315 | | * |
9316 | | * @param[in] exp Parsed XPath expression. |
9317 | | * @param[in] tok_idx Position in the expression @p exp. |
9318 | | * @param[in] repeat How many times this expression is repeated. |
9319 | | * @param[in,out] set Context and result set. |
9320 | | * @param[in] options XPath options. |
9321 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9322 | | */ |
9323 | | static LY_ERR |
9324 | | eval_relational_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
9325 | 0 | { |
9326 | 0 | LY_ERR rc = LY_SUCCESS; |
9327 | 0 | uint32_t i, this_op; |
9328 | 0 | struct lyxp_set orig_set, set2; |
9329 | |
|
9330 | 0 | assert(repeat); |
9331 | | |
9332 | 0 | set_init(&orig_set, set); |
9333 | 0 | set_init(&set2, set); |
9334 | |
|
9335 | 0 | set_fill_set(&orig_set, set); |
9336 | |
|
9337 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
9338 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9339 | | |
9340 | | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
9341 | 0 | for (i = 0; i < repeat; ++i) { |
9342 | 0 | this_op = *tok_idx; |
9343 | |
|
9344 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
9345 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
9346 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9347 | 0 | ++(*tok_idx); |
9348 | |
|
9349 | 0 | if (options & LYXP_SKIP_EXPR) { |
9350 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
9351 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9352 | 0 | continue; |
9353 | 0 | } |
9354 | | |
9355 | 0 | set_fill_set(&set2, &orig_set); |
9356 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
9357 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9358 | 0 | if (set2.not_found) { |
9359 | 0 | set->not_found = 1; |
9360 | 0 | } |
9361 | | |
9362 | | /* eval */ |
9363 | 0 | if (options & LYXP_SCNODE_ALL) { |
9364 | 0 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
9365 | 0 | lyxp_set_scnode_merge(set, &set2); |
9366 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
9367 | 0 | } else { |
9368 | 0 | ly_bool result; |
9369 | |
|
9370 | 0 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result); |
9371 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9372 | 0 | set_fill_boolean(set, result); |
9373 | 0 | } |
9374 | 0 | } |
9375 | | |
9376 | 0 | cleanup: |
9377 | 0 | lyxp_set_free_content(&orig_set); |
9378 | 0 | lyxp_set_free_content(&set2); |
9379 | 0 | return rc; |
9380 | 0 | } |
9381 | | |
9382 | | /** |
9383 | | * @brief Evaluate EqualityExpr. Logs directly on error. |
9384 | | * |
9385 | | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
9386 | | * | EqualityExpr '!=' RelationalExpr |
9387 | | * |
9388 | | * @param[in] exp Parsed XPath expression. |
9389 | | * @param[in] tok_idx Position in the expression @p exp. |
9390 | | * @param[in] repeat How many times this expression is repeated. |
9391 | | * @param[in,out] set Context and result set. |
9392 | | * @param[in] options XPath options. |
9393 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9394 | | */ |
9395 | | static LY_ERR |
9396 | | eval_equality_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
9397 | 2.42k | { |
9398 | 2.42k | LY_ERR rc = LY_SUCCESS; |
9399 | 2.42k | uint32_t i, this_op; |
9400 | 2.42k | struct lyxp_set orig_set, set2; |
9401 | | |
9402 | 2.42k | assert(repeat); |
9403 | | |
9404 | 2.42k | set_init(&orig_set, set); |
9405 | 2.42k | set_init(&set2, set); |
9406 | | |
9407 | 2.42k | set_fill_set(&orig_set, set); |
9408 | | |
9409 | 2.42k | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
9410 | 2.42k | LY_CHECK_GOTO(rc, cleanup); |
9411 | | |
9412 | | /* ('=' / '!=' RelationalExpr)* */ |
9413 | 4.84k | for (i = 0; i < repeat; ++i) { |
9414 | 2.42k | this_op = *tok_idx; |
9415 | | |
9416 | 2.42k | assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL)); |
9417 | 2.42k | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
9418 | 2.42k | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9419 | 2.42k | ++(*tok_idx); |
9420 | | |
9421 | 2.42k | if (options & LYXP_SKIP_EXPR) { |
9422 | 74 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
9423 | 74 | LY_CHECK_GOTO(rc, cleanup); |
9424 | 74 | continue; |
9425 | 74 | } |
9426 | | |
9427 | 2.34k | set_fill_set(&set2, &orig_set); |
9428 | 2.34k | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
9429 | 2.34k | LY_CHECK_GOTO(rc, cleanup); |
9430 | 2.34k | if (set2.not_found) { |
9431 | 0 | set->not_found = 1; |
9432 | 0 | } |
9433 | | |
9434 | | /* eval */ |
9435 | 2.34k | if (options & LYXP_SCNODE_ALL) { |
9436 | 0 | warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]); |
9437 | 0 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
9438 | 0 | warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1); |
9439 | 0 | lyxp_set_scnode_merge(set, &set2); |
9440 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
9441 | 2.34k | } else { |
9442 | 2.34k | ly_bool result; |
9443 | | |
9444 | 2.34k | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result); |
9445 | 2.34k | LY_CHECK_GOTO(rc, cleanup); |
9446 | 2.34k | set_fill_boolean(set, result); |
9447 | 2.34k | } |
9448 | 2.34k | } |
9449 | | |
9450 | 2.42k | cleanup: |
9451 | 2.42k | lyxp_set_free_content(&orig_set); |
9452 | 2.42k | lyxp_set_free_content(&set2); |
9453 | 2.42k | return rc; |
9454 | 2.42k | } |
9455 | | |
9456 | | /** |
9457 | | * @brief Evaluate AndExpr. Logs directly on error. |
9458 | | * |
9459 | | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
9460 | | * |
9461 | | * @param[in] exp Parsed XPath expression. |
9462 | | * @param[in] tok_idx Position in the expression @p exp. |
9463 | | * @param[in] repeat How many times this expression is repeated. |
9464 | | * @param[in,out] set Context and result set. |
9465 | | * @param[in] options XPath options. |
9466 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9467 | | */ |
9468 | | static LY_ERR |
9469 | | eval_and_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
9470 | 0 | { |
9471 | 0 | LY_ERR rc = LY_SUCCESS; |
9472 | 0 | struct lyxp_set orig_set, set2; |
9473 | 0 | uint32_t i; |
9474 | |
|
9475 | 0 | assert(repeat); |
9476 | | |
9477 | 0 | set_init(&orig_set, set); |
9478 | 0 | set_init(&set2, set); |
9479 | |
|
9480 | 0 | set_fill_set(&orig_set, set); |
9481 | |
|
9482 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
9483 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9484 | |
|
9485 | 0 | if (!(options & LYXP_SKIP_EXPR)) { |
9486 | 0 | if (options & LYXP_SCNODE_ALL) { |
9487 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
9488 | 0 | } else { |
9489 | | /* cast to boolean, we know that will be the final result */ |
9490 | 0 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
9491 | 0 | } |
9492 | 0 | } |
9493 | | |
9494 | | /* ('and' EqualityExpr)* */ |
9495 | 0 | for (i = 0; i < repeat; ++i) { |
9496 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
9497 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"), |
9498 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9499 | 0 | ++(*tok_idx); |
9500 | | |
9501 | | /* lazy evaluation */ |
9502 | 0 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
9503 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR); |
9504 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9505 | 0 | continue; |
9506 | 0 | } |
9507 | | |
9508 | 0 | set_fill_set(&set2, &orig_set); |
9509 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
9510 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9511 | 0 | if (set2.not_found) { |
9512 | 0 | set->not_found = 1; |
9513 | 0 | } |
9514 | | |
9515 | | /* eval - just get boolean value actually */ |
9516 | 0 | if (set->type == LYXP_SET_SCNODE_SET) { |
9517 | 0 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
9518 | 0 | lyxp_set_scnode_merge(set, &set2); |
9519 | 0 | } else { |
9520 | 0 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
9521 | 0 | set_fill_set(set, &set2); |
9522 | 0 | } |
9523 | 0 | } |
9524 | | |
9525 | 0 | cleanup: |
9526 | 0 | lyxp_set_free_content(&orig_set); |
9527 | 0 | lyxp_set_free_content(&set2); |
9528 | 0 | return rc; |
9529 | 0 | } |
9530 | | |
9531 | | /** |
9532 | | * @brief Evaluate OrExpr. Logs directly on error. |
9533 | | * |
9534 | | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
9535 | | * |
9536 | | * @param[in] exp Parsed XPath expression. |
9537 | | * @param[in] tok_idx Position in the expression @p exp. |
9538 | | * @param[in] repeat How many times this expression is repeated. |
9539 | | * @param[in,out] set Context and result set. |
9540 | | * @param[in] options XPath options. |
9541 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9542 | | */ |
9543 | | static LY_ERR |
9544 | | eval_or_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
9545 | 0 | { |
9546 | 0 | LY_ERR rc = LY_SUCCESS; |
9547 | 0 | struct lyxp_set orig_set, set2; |
9548 | 0 | uint32_t i; |
9549 | |
|
9550 | 0 | assert(repeat); |
9551 | | |
9552 | 0 | set_init(&orig_set, set); |
9553 | 0 | set_init(&set2, set); |
9554 | |
|
9555 | 0 | set_fill_set(&orig_set, set); |
9556 | |
|
9557 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
9558 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9559 | |
|
9560 | 0 | if (!(options & LYXP_SKIP_EXPR)) { |
9561 | 0 | if (options & LYXP_SCNODE_ALL) { |
9562 | 0 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
9563 | 0 | } else { |
9564 | | /* cast to boolean, we know that will be the final result */ |
9565 | 0 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
9566 | 0 | } |
9567 | 0 | } |
9568 | | |
9569 | | /* ('or' AndExpr)* */ |
9570 | 0 | for (i = 0; i < repeat; ++i) { |
9571 | 0 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
9572 | 0 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"), |
9573 | 0 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
9574 | 0 | ++(*tok_idx); |
9575 | | |
9576 | | /* lazy evaluation */ |
9577 | 0 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
9578 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR); |
9579 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9580 | 0 | continue; |
9581 | 0 | } |
9582 | | |
9583 | 0 | set_fill_set(&set2, &orig_set); |
9584 | | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
9585 | | * but it does not matter */ |
9586 | 0 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
9587 | 0 | LY_CHECK_GOTO(rc, cleanup); |
9588 | 0 | if (set2.not_found) { |
9589 | 0 | set->not_found = 1; |
9590 | 0 | } |
9591 | | |
9592 | | /* eval - just get boolean value actually */ |
9593 | 0 | if (set->type == LYXP_SET_SCNODE_SET) { |
9594 | 0 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
9595 | 0 | lyxp_set_scnode_merge(set, &set2); |
9596 | 0 | } else { |
9597 | 0 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
9598 | 0 | set_fill_set(set, &set2); |
9599 | 0 | } |
9600 | 0 | } |
9601 | | |
9602 | 0 | cleanup: |
9603 | 0 | lyxp_set_free_content(&orig_set); |
9604 | 0 | lyxp_set_free_content(&set2); |
9605 | 0 | return rc; |
9606 | 0 | } |
9607 | | |
9608 | | /** |
9609 | | * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly. |
9610 | | * |
9611 | | * @param[in] exp Parsed XPath expression. |
9612 | | * @param[in] tok_idx Position in the expression @p exp. |
9613 | | * @param[in] etype Expression type to evaluate. |
9614 | | * @param[in,out] set Context and result set. |
9615 | | * @param[in] options XPath options. |
9616 | | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
9617 | | */ |
9618 | | static LY_ERR |
9619 | | eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
9620 | | uint32_t options) |
9621 | 8.09k | { |
9622 | 8.09k | uint32_t i, count; |
9623 | 8.09k | enum lyxp_expr_type next_etype; |
9624 | 8.09k | LY_ERR rc; |
9625 | | |
9626 | | /* process operator repeats */ |
9627 | 8.09k | if (!exp->repeat[*tok_idx]) { |
9628 | 3.25k | next_etype = LYXP_EXPR_NONE; |
9629 | 4.84k | } else { |
9630 | | /* find etype repeat */ |
9631 | 7.26k | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
9632 | | |
9633 | | /* select one-priority lower because etype expression called us */ |
9634 | 4.84k | if (i) { |
9635 | 2.42k | next_etype = exp->repeat[*tok_idx][i - 1]; |
9636 | | /* count repeats for that expression */ |
9637 | 4.84k | for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {} |
9638 | 2.42k | } else { |
9639 | 2.42k | next_etype = LYXP_EXPR_NONE; |
9640 | 2.42k | } |
9641 | 4.84k | } |
9642 | | |
9643 | | /* decide what expression are we parsing based on the repeat */ |
9644 | 8.09k | switch (next_etype) { |
9645 | 0 | case LYXP_EXPR_OR: |
9646 | 0 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
9647 | 0 | break; |
9648 | 0 | case LYXP_EXPR_AND: |
9649 | 0 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
9650 | 0 | break; |
9651 | 2.42k | case LYXP_EXPR_EQUALITY: |
9652 | 2.42k | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
9653 | 2.42k | break; |
9654 | 0 | case LYXP_EXPR_RELATIONAL: |
9655 | 0 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
9656 | 0 | break; |
9657 | 0 | case LYXP_EXPR_ADDITIVE: |
9658 | 0 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
9659 | 0 | break; |
9660 | 0 | case LYXP_EXPR_MULTIPLICATIVE: |
9661 | 0 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
9662 | 0 | break; |
9663 | 0 | case LYXP_EXPR_UNARY: |
9664 | 0 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
9665 | 0 | break; |
9666 | 0 | case LYXP_EXPR_UNION: |
9667 | 0 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
9668 | 0 | break; |
9669 | 5.67k | case LYXP_EXPR_NONE: |
9670 | 5.67k | rc = eval_path_expr(exp, tok_idx, set, options); |
9671 | 5.67k | break; |
9672 | 0 | default: |
9673 | 0 | LOGINT_RET(set->ctx); |
9674 | 8.09k | } |
9675 | | |
9676 | 8.09k | return rc; |
9677 | 8.09k | } |
9678 | | |
9679 | | /** |
9680 | | * @brief Get root type. |
9681 | | * |
9682 | | * @param[in] ctx_node Context node. |
9683 | | * @param[in] ctx_scnode Schema context node. |
9684 | | * @param[in] options XPath options. |
9685 | | * @return Root type. |
9686 | | */ |
9687 | | static enum lyxp_node_type |
9688 | | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, uint32_t options) |
9689 | 833 | { |
9690 | 833 | const struct lysc_node *op; |
9691 | | |
9692 | | /* explicit */ |
9693 | 833 | if (options & LYXP_ACCESS_TREE_ALL) { |
9694 | 0 | return LYXP_NODE_ROOT; |
9695 | 833 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
9696 | 0 | return LYXP_NODE_ROOT_CONFIG; |
9697 | 0 | } |
9698 | | |
9699 | 833 | if (options & LYXP_SCNODE_ALL) { |
9700 | | /* schema */ |
9701 | 801 | for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {} |
9702 | | |
9703 | 334 | if (op || !(options & LYXP_SCNODE_SCHEMA)) { |
9704 | | /* general root that can access everything */ |
9705 | 334 | return LYXP_NODE_ROOT; |
9706 | 334 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
9707 | | /* root context node can access only config data (because we said so, it is unspecified) */ |
9708 | 0 | return LYXP_NODE_ROOT_CONFIG; |
9709 | 0 | } |
9710 | 0 | return LYXP_NODE_ROOT; |
9711 | 334 | } |
9712 | | |
9713 | | /* data */ |
9714 | 499 | op = ctx_node ? ctx_node->schema : NULL; |
9715 | 1.13k | for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {} |
9716 | | |
9717 | 499 | if (op || !(options & LYXP_SCHEMA)) { |
9718 | | /* general root that can access everything */ |
9719 | 499 | return LYXP_NODE_ROOT; |
9720 | 499 | } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
9721 | | /* root context node can access only config data (because we said so, it is unspecified) */ |
9722 | 0 | return LYXP_NODE_ROOT_CONFIG; |
9723 | 0 | } |
9724 | 0 | return LYXP_NODE_ROOT; |
9725 | 499 | } |
9726 | | |
9727 | | LY_ERR |
9728 | | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
9729 | | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
9730 | | const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options) |
9731 | 499 | { |
9732 | 499 | uint32_t tok_idx = 0; |
9733 | 499 | LY_ERR rc; |
9734 | | |
9735 | 998 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
9736 | 499 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
9737 | 0 | LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used."); |
9738 | 0 | return LY_EINVAL; |
9739 | 0 | } |
9740 | | |
9741 | 499 | if (tree) { |
9742 | | /* adjust the pointer to be the first top-level sibling */ |
9743 | 499 | while (tree->parent) { |
9744 | 0 | tree = lyd_parent(tree); |
9745 | 0 | } |
9746 | 499 | tree = lyd_first_sibling(tree); |
9747 | | |
9748 | 499 | if (lysc_data_parent(tree->schema)) { |
9749 | | /* unable to evaluate absolute paths */ |
9750 | 0 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
9751 | 0 | LYD_NAME(tree)); |
9752 | 0 | return LY_EINVAL; |
9753 | 0 | } |
9754 | 499 | } |
9755 | | |
9756 | | /* prepare set for evaluation */ |
9757 | 499 | memset(set, 0, sizeof *set); |
9758 | 499 | set->type = LYXP_SET_NODE_SET; |
9759 | 499 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
9760 | 499 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
9761 | | |
9762 | 499 | set->ctx = (struct ly_ctx *)ctx; |
9763 | 499 | set->cur_node = cur_node; |
9764 | 499 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
9765 | 998 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
9766 | 499 | set->context_op = set->context_op->parent) {} |
9767 | 499 | set->tree = tree; |
9768 | 499 | set->cur_mod = cur_mod; |
9769 | 499 | set->format = format; |
9770 | 499 | set->prefix_data = prefix_data; |
9771 | 499 | set->vars = vars; |
9772 | | |
9773 | 499 | if (set->cur_node) { |
9774 | 499 | LOG_LOCSET(NULL, set->cur_node); |
9775 | 499 | } |
9776 | | |
9777 | | /* evaluate */ |
9778 | 499 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
9779 | 499 | if (!rc && set->not_found) { |
9780 | 0 | rc = LY_ENOTFOUND; |
9781 | 0 | } |
9782 | 499 | if (rc) { |
9783 | 0 | lyxp_set_free_content(set); |
9784 | 0 | } |
9785 | | |
9786 | 499 | if (set->cur_node) { |
9787 | 499 | LOG_LOCBACK(0, 1); |
9788 | 499 | } |
9789 | 499 | return rc; |
9790 | 499 | } |
9791 | | |
9792 | | #if 0 |
9793 | | |
9794 | | /* full xml printing of set elements, not used currently */ |
9795 | | |
9796 | | void |
9797 | | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
9798 | | { |
9799 | | uint32_t i; |
9800 | | char *str_num; |
9801 | | struct lyout out; |
9802 | | |
9803 | | memset(&out, 0, sizeof out); |
9804 | | |
9805 | | out.type = LYOUT_STREAM; |
9806 | | out.method.f = f; |
9807 | | |
9808 | | switch (set->type) { |
9809 | | case LYXP_SET_EMPTY: |
9810 | | ly_print_(&out, "Empty XPath set\n\n"); |
9811 | | break; |
9812 | | case LYXP_SET_BOOLEAN: |
9813 | | ly_print_(&out, "Boolean XPath set:\n"); |
9814 | | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
9815 | | break; |
9816 | | case LYXP_SET_STRING: |
9817 | | ly_print_(&out, "String XPath set:\n"); |
9818 | | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
9819 | | break; |
9820 | | case LYXP_SET_NUMBER: |
9821 | | ly_print_(&out, "Number XPath set:\n"); |
9822 | | |
9823 | | if (isnan(set->value.num)) { |
9824 | | str_num = strdup("NaN"); |
9825 | | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
9826 | | str_num = strdup("0"); |
9827 | | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
9828 | | str_num = strdup("Infinity"); |
9829 | | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
9830 | | str_num = strdup("-Infinity"); |
9831 | | } else if ((long long)set->value.num == set->value.num) { |
9832 | | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
9833 | | str_num = NULL; |
9834 | | } |
9835 | | } else { |
9836 | | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
9837 | | str_num = NULL; |
9838 | | } |
9839 | | } |
9840 | | if (!str_num) { |
9841 | | LOGMEM; |
9842 | | return; |
9843 | | } |
9844 | | ly_print_(&out, "%s\n\n", str_num); |
9845 | | free(str_num); |
9846 | | break; |
9847 | | case LYXP_SET_NODE_SET: |
9848 | | ly_print_(&out, "Node XPath set:\n"); |
9849 | | |
9850 | | for (i = 0; i < set->used; ++i) { |
9851 | | ly_print_(&out, "%d. ", i + 1); |
9852 | | switch (set->node_type[i]) { |
9853 | | case LYXP_NODE_ROOT_ALL: |
9854 | | ly_print_(&out, "ROOT all\n\n"); |
9855 | | break; |
9856 | | case LYXP_NODE_ROOT_CONFIG: |
9857 | | ly_print_(&out, "ROOT config\n\n"); |
9858 | | break; |
9859 | | case LYXP_NODE_ROOT_STATE: |
9860 | | ly_print_(&out, "ROOT state\n\n"); |
9861 | | break; |
9862 | | case LYXP_NODE_ROOT_NOTIF: |
9863 | | ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
9864 | | break; |
9865 | | case LYXP_NODE_ROOT_RPC: |
9866 | | ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
9867 | | break; |
9868 | | case LYXP_NODE_ROOT_OUTPUT: |
9869 | | ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
9870 | | break; |
9871 | | case LYXP_NODE_ELEM: |
9872 | | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
9873 | | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
9874 | | ly_print_(&out, "\n"); |
9875 | | break; |
9876 | | case LYXP_NODE_TEXT: |
9877 | | ly_print_(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
9878 | | break; |
9879 | | case LYXP_NODE_ATTR: |
9880 | | ly_print_(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
9881 | | break; |
9882 | | } |
9883 | | } |
9884 | | break; |
9885 | | } |
9886 | | } |
9887 | | |
9888 | | #endif |
9889 | | |
9890 | | LY_ERR |
9891 | | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
9892 | 4.99k | { |
9893 | 4.99k | long double num; |
9894 | 4.99k | char *str; |
9895 | 4.99k | LY_ERR rc; |
9896 | | |
9897 | 4.99k | if (!set || (set->type == target)) { |
9898 | 2.48k | return LY_SUCCESS; |
9899 | 2.48k | } |
9900 | | |
9901 | | /* it's not possible to convert anything into a node set */ |
9902 | 2.50k | assert(target != LYXP_SET_NODE_SET); |
9903 | | |
9904 | 2.50k | if (set->type == LYXP_SET_SCNODE_SET) { |
9905 | 0 | lyxp_set_free_content(set); |
9906 | 0 | return LY_EINVAL; |
9907 | 0 | } |
9908 | | |
9909 | | /* to STRING */ |
9910 | 2.50k | if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) { |
9911 | 2.50k | switch (set->type) { |
9912 | 0 | case LYXP_SET_NUMBER: |
9913 | 0 | if (isnan(set->val.num)) { |
9914 | 0 | set->val.str = strdup("NaN"); |
9915 | 0 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
9916 | 0 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
9917 | 0 | set->val.str = strdup("0"); |
9918 | 0 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
9919 | 0 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
9920 | 0 | set->val.str = strdup("Infinity"); |
9921 | 0 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
9922 | 0 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
9923 | 0 | set->val.str = strdup("-Infinity"); |
9924 | 0 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
9925 | 0 | } else if ((long long)set->val.num == set->val.num) { |
9926 | 0 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
9927 | 0 | LOGMEM_RET(set->ctx); |
9928 | 0 | } |
9929 | 0 | set->val.str = str; |
9930 | 0 | } else { |
9931 | 0 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
9932 | 0 | LOGMEM_RET(set->ctx); |
9933 | 0 | } |
9934 | 0 | set->val.str = str; |
9935 | 0 | } |
9936 | 0 | break; |
9937 | 0 | case LYXP_SET_BOOLEAN: |
9938 | 0 | if (set->val.bln) { |
9939 | 0 | set->val.str = strdup("true"); |
9940 | 0 | } else { |
9941 | 0 | set->val.str = strdup("false"); |
9942 | 0 | } |
9943 | 0 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
9944 | 0 | break; |
9945 | 2.50k | case LYXP_SET_NODE_SET: |
9946 | | /* we need the set sorted, it affects the result */ |
9947 | 2.50k | assert(!set_sort(set)); |
9948 | | |
9949 | 2.50k | rc = cast_node_set_to_string(set, &str); |
9950 | 2.50k | LY_CHECK_RET(rc); |
9951 | 2.50k | lyxp_set_free_content(set); |
9952 | 2.50k | set->val.str = str; |
9953 | 2.50k | break; |
9954 | 0 | default: |
9955 | 0 | LOGINT_RET(set->ctx); |
9956 | 2.50k | } |
9957 | 2.50k | set->type = LYXP_SET_STRING; |
9958 | 2.50k | } |
9959 | | |
9960 | | /* to NUMBER */ |
9961 | 2.50k | if (target == LYXP_SET_NUMBER) { |
9962 | 0 | switch (set->type) { |
9963 | 0 | case LYXP_SET_STRING: |
9964 | 0 | num = cast_string_to_number(set->val.str); |
9965 | 0 | lyxp_set_free_content(set); |
9966 | 0 | set->val.num = num; |
9967 | 0 | break; |
9968 | 0 | case LYXP_SET_BOOLEAN: |
9969 | 0 | if (set->val.bln) { |
9970 | 0 | set->val.num = 1; |
9971 | 0 | } else { |
9972 | 0 | set->val.num = 0; |
9973 | 0 | } |
9974 | 0 | break; |
9975 | 0 | default: |
9976 | 0 | LOGINT_RET(set->ctx); |
9977 | 0 | } |
9978 | 0 | set->type = LYXP_SET_NUMBER; |
9979 | 0 | } |
9980 | | |
9981 | | /* to BOOLEAN */ |
9982 | 2.50k | if (target == LYXP_SET_BOOLEAN) { |
9983 | 0 | switch (set->type) { |
9984 | 0 | case LYXP_SET_NUMBER: |
9985 | 0 | if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) { |
9986 | 0 | set->val.bln = 0; |
9987 | 0 | } else { |
9988 | 0 | set->val.bln = 1; |
9989 | 0 | } |
9990 | 0 | break; |
9991 | 0 | case LYXP_SET_STRING: |
9992 | 0 | if (set->val.str[0]) { |
9993 | 0 | lyxp_set_free_content(set); |
9994 | 0 | set->val.bln = 1; |
9995 | 0 | } else { |
9996 | 0 | lyxp_set_free_content(set); |
9997 | 0 | set->val.bln = 0; |
9998 | 0 | } |
9999 | 0 | break; |
10000 | 0 | case LYXP_SET_NODE_SET: |
10001 | 0 | if (set->used) { |
10002 | 0 | lyxp_set_free_content(set); |
10003 | 0 | set->val.bln = 1; |
10004 | 0 | } else { |
10005 | 0 | lyxp_set_free_content(set); |
10006 | 0 | set->val.bln = 0; |
10007 | 0 | } |
10008 | 0 | break; |
10009 | 0 | default: |
10010 | 0 | LOGINT_RET(set->ctx); |
10011 | 0 | } |
10012 | 0 | set->type = LYXP_SET_BOOLEAN; |
10013 | 0 | } |
10014 | | |
10015 | 2.50k | return LY_SUCCESS; |
10016 | 2.50k | } |
10017 | | |
10018 | | LY_ERR |
10019 | | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
10020 | | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
10021 | | const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options) |
10022 | 334 | { |
10023 | 334 | LY_ERR rc; |
10024 | 334 | uint32_t tok_idx = 0; |
10025 | | |
10026 | 668 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
10027 | 334 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
10028 | 0 | LOGARG(NULL, "Current module must be set if schema format is used."); |
10029 | 0 | return LY_EINVAL; |
10030 | 0 | } |
10031 | | |
10032 | | /* prepare set for evaluation */ |
10033 | 334 | memset(set, 0, sizeof *set); |
10034 | 334 | set->type = LYXP_SET_SCNODE_SET; |
10035 | 334 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
10036 | 334 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, LYXP_AXIS_SELF, NULL)); |
10037 | 334 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
10038 | | |
10039 | 334 | set->ctx = (struct ly_ctx *)ctx; |
10040 | 334 | set->cur_scnode = cur_scnode; |
10041 | 334 | for (set->context_op = cur_scnode; |
10042 | 668 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
10043 | 334 | set->context_op = set->context_op->parent) {} |
10044 | 334 | set->cur_mod = cur_mod; |
10045 | 334 | set->format = format; |
10046 | 334 | set->prefix_data = prefix_data; |
10047 | | |
10048 | 334 | if (set->cur_scnode) { |
10049 | 334 | LOG_LOCSET(set->cur_scnode, NULL); |
10050 | 334 | } |
10051 | | |
10052 | | /* evaluate */ |
10053 | 334 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
10054 | 334 | if (!rc && set->not_found) { |
10055 | 0 | rc = LY_ENOTFOUND; |
10056 | 0 | } |
10057 | | |
10058 | 334 | if (set->cur_scnode) { |
10059 | 334 | LOG_LOCBACK(1, 0); |
10060 | 334 | } |
10061 | 334 | return rc; |
10062 | 334 | } |
10063 | | |
10064 | | LIBYANG_API_DEF const char * |
10065 | | lyxp_get_expr(const struct lyxp_expr *path) |
10066 | 3.21k | { |
10067 | 3.21k | if (!path) { |
10068 | 0 | return NULL; |
10069 | 0 | } |
10070 | | |
10071 | 3.21k | return path->expr; |
10072 | 3.21k | } |