blob: b1d98433267c3c0231b360c7091ac73bb832a88f [file] [log] [blame]
Michal Vasko25895052015-09-21 11:41:12 +02001/**
2 * @file xpath.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions header
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko25895052015-09-21 11:41:12 +020013 */
14
15#ifndef _XPATH_H
16#define _XPATH_H
17
18#include <stdint.h>
19
Michal Vaskofcd974b2017-08-22 10:17:49 +020020#include "libyang.h"
Michal Vaskofd76bd12015-09-24 15:49:57 +020021#include "tree_schema.h"
22#include "tree_data.h"
Michal Vasko25895052015-09-21 11:41:12 +020023
24/*
25 * XPath evaluator fully compliant with http://www.w3.org/TR/1999/REC-xpath-19991116/
26 * except the following restrictions in the grammar.
27 *
28 * PARSED GRAMMAR
29 *
30 * Full axes are not supported, abbreviated forms must be used,
Michal Vasko93909772016-10-26 10:32:10 +020031 * variables are not supported, "id()" function is not supported,
32 * and processing instruction and comment nodes are not supported,
33 * which is also reflected in the grammar. Undefined rules and
34 * constants are tokens.
Michal Vasko25895052015-09-21 11:41:12 +020035 *
36 * Modified full grammar:
37 *
Michal Vasko764c8c42017-11-07 15:43:06 +010038 * [1] Expr ::= OrExpr // just an alias
39 *
40 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
41 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
42 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
43 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
44 * [6] NodeTest ::= NameTest | NodeType '(' ')'
45 * [7] Predicate ::= '[' Expr ']'
46 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
47 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
48 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko25895052015-09-21 11:41:12 +020049 * | PrimaryExpr Predicate* '/' RelativeLocationPath
50 * | PrimaryExpr Predicate* '//' RelativeLocationPath
Michal Vasko764c8c42017-11-07 15:43:06 +010051 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
52 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
53 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko25895052015-09-21 11:41:12 +020054 * | EqualityExpr '!=' RelationalExpr
Michal Vasko764c8c42017-11-07 15:43:06 +010055 * [14] RelationalExpr ::= AdditiveExpr
Michal Vasko25895052015-09-21 11:41:12 +020056 * | RelationalExpr '<' AdditiveExpr
57 * | RelationalExpr '>' AdditiveExpr
58 * | RelationalExpr '<=' AdditiveExpr
59 * | RelationalExpr '>=' AdditiveExpr
Michal Vasko764c8c42017-11-07 15:43:06 +010060 * [15] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko25895052015-09-21 11:41:12 +020061 * | AdditiveExpr '+' MultiplicativeExpr
62 * | AdditiveExpr '-' MultiplicativeExpr
Michal Vasko764c8c42017-11-07 15:43:06 +010063 * [16] MultiplicativeExpr ::= UnaryExpr
Michal Vasko25895052015-09-21 11:41:12 +020064 * | MultiplicativeExpr '*' UnaryExpr
65 * | MultiplicativeExpr 'div' UnaryExpr
66 * | MultiplicativeExpr 'mod' UnaryExpr
Michal Vasko764c8c42017-11-07 15:43:06 +010067 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
68 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko25895052015-09-21 11:41:12 +020069 */
70
71/* expression tokens allocation */
72#define LYXP_EXPR_SIZE_START 10
73#define LYXP_EXPR_SIZE_STEP 5
74
75/* XPath matches allocation */
76#define LYXP_SET_SIZE_START 2
77#define LYXP_SET_SIZE_STEP 2
78
79/* building string when casting */
80#define LYXP_STRING_CAST_SIZE_START 64
81#define LYXP_STRING_CAST_SIZE_STEP 16
82
83/**
84 * @brief Tokens that can be in an XPath expression.
85 */
86enum lyxp_token {
87 LYXP_TOKEN_NONE = 0,
88 LYXP_TOKEN_PAR1, /* '(' */
89 LYXP_TOKEN_PAR2, /* ')' */
90 LYXP_TOKEN_BRACK1, /* '[' */
91 LYXP_TOKEN_BRACK2, /* ']' */
92 LYXP_TOKEN_DOT, /* '.' */
93 LYXP_TOKEN_DDOT, /* '..' */
94 LYXP_TOKEN_AT, /* '@' */
95 LYXP_TOKEN_COMMA, /* ',' */
96 /* LYXP_TOKEN_DCOLON, * '::' * axes not supported */
97 LYXP_TOKEN_NAMETEST, /* NameTest */
98 LYXP_TOKEN_NODETYPE, /* NodeType */
99 LYXP_TOKEN_FUNCNAME, /* FunctionName */
100 LYXP_TOKEN_OPERATOR_LOG, /* Operator 'and', 'or' */
101 LYXP_TOKEN_OPERATOR_COMP, /* Operator '=', '!=', '<', '<=', '>', '>=' */
102 LYXP_TOKEN_OPERATOR_MATH, /* Operator '+', '-', '*', 'div', 'mod', '-' (unary) */
103 LYXP_TOKEN_OPERATOR_UNI, /* Operator '|' */
104 LYXP_TOKEN_OPERATOR_PATH, /* Operator '/', '//' */
105 /* LYXP_TOKEN_AXISNAME, * AxisName * axes not supported */
106 LYXP_TOKEN_LITERAL, /* Literal - with either single or double quote */
107 LYXP_TOKEN_NUMBER /* Number */
108};
109
110/**
Michal Vasko764c8c42017-11-07 15:43:06 +0100111 * @brief XPath (sub)expressions that can be repeated.
112 */
113enum lyxp_expr_type {
114 LYXP_EXPR_NONE = 0,
115 LYXP_EXPR_OR,
116 LYXP_EXPR_AND,
117 LYXP_EXPR_EQUALITY,
118 LYXP_EXPR_RELATIONAL,
119 LYXP_EXPR_ADDITIVE,
120 LYXP_EXPR_MULTIPLICATIVE,
Michal Vasko2c05f1b2017-11-16 13:36:09 +0100121 LYXP_EXPR_UNARY,
Michal Vasko764c8c42017-11-07 15:43:06 +0100122 LYXP_EXPR_UNION,
123};
124
125/**
Michal Vasko25895052015-09-21 11:41:12 +0200126 * @brief Structure holding a parsed XPath expression.
127 */
128struct lyxp_expr {
129 enum lyxp_token *tokens; /* array of tokens */
130 uint16_t *expr_pos; /* array of pointers to the expression in expr (idx of the beginning) */
Michal Vasko7b704872018-02-15 16:09:43 +0100131 uint16_t *tok_len; /* array of token lengths in expr */
Michal Vasko764c8c42017-11-07 15:43:06 +0100132 enum lyxp_expr_type **repeat; /* array of expression types that this token begins and is repeated ended with 0,
133 more in the comment after this declaration */
Michal Vasko25895052015-09-21 11:41:12 +0200134 uint16_t used; /* used array items */
135 uint16_t size; /* allocated array items */
136
Michal Vasko2b84ea72016-05-05 17:56:58 +0200137 char *expr; /* the original XPath expression */
Michal Vasko25895052015-09-21 11:41:12 +0200138};
139
140/*
141 * lyxp_expr repeat
142 *
143 * This value is NULL for all the tokens that do not begin an
144 * expression which can be repeated. Otherwise it is an array
Michal Vasko764c8c42017-11-07 15:43:06 +0100145 * of expression types that this token begins. These values
Michal Vasko25895052015-09-21 11:41:12 +0200146 * are used during evaluation to know whether we need to
Michal Vasko764c8c42017-11-07 15:43:06 +0100147 * duplicate the current context or not and to decide what
148 * the current expression is (for example, if we are only
149 * starting the parsing and the first token has no repeat,
150 * we do not parse it as an OrExpr but directly as PathExpr).
151 * Examples:
Michal Vasko25895052015-09-21 11:41:12 +0200152 *
153 * Expression: "/ *[key1 and key2 or key1 < key2]"
Michal Vasko764c8c42017-11-07 15:43:06 +0100154 * Tokens: '/', '*', '[', NameTest, 'and', NameTest, 'or', NameTest, '<', NameTest, ']'
155 * Repeat: NULL, NULL, NULL, [AndExpr, NULL, NULL, NULL, [RelationalExpr, NULL, NULL, NULL
156 * OrExpr, 0],
157 * 0],
Michal Vasko25895052015-09-21 11:41:12 +0200158 *
159 * Expression: "//node[key and node2]/key | /cont"
Michal Vasko764c8c42017-11-07 15:43:06 +0100160 * Tokens: '//', 'NameTest', '[', 'NameTest', 'and', 'NameTest', ']', '/', 'NameTest', '|', '/', 'NameTest'
161 * Repeat: [UnionExpr, NULL, NULL, [AndExpr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
162 * 0], 0],
Michal Vasko25895052015-09-21 11:41:12 +0200163 *
Michal Vasko56d082c2016-10-25 14:00:42 +0200164 * Operators between expressions which this concerns:
Michal Vasko25895052015-09-21 11:41:12 +0200165 * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|'
166 */
167
168/**
169 * @brief Supported types of (partial) XPath results.
170 */
171enum lyxp_set_type {
172 LYXP_SET_EMPTY = 0,
173 LYXP_SET_NODE_SET,
Michal Vasko5b3492c2016-07-20 09:37:40 +0200174 LYXP_SET_SNODE_SET,
Michal Vasko25895052015-09-21 11:41:12 +0200175 LYXP_SET_BOOLEAN,
176 LYXP_SET_NUMBER,
177 LYXP_SET_STRING
178};
179
Michal Vaskod0484562018-04-26 14:30:47 +0200180#ifdef LY_ENABLED_CACHE
181
182/**
183 * @brief Item stored in an XPath set hash table.
184 */
185struct lyxp_set_hash_node {
186 struct lyd_node *node;
187 enum lyxp_node_type type;
188};
189
190#endif
191
Michal Vasko25895052015-09-21 11:41:12 +0200192/**
Michal Vasko8146d4c2016-05-09 15:50:29 +0200193 * @brief XPath set - (partial) result.
194 */
195struct lyxp_set {
196 enum lyxp_set_type type;
197 union {
Michal Vasko11306452018-04-25 16:11:13 +0200198 struct lyxp_set_node {
Michal Vasko8146d4c2016-05-09 15:50:29 +0200199 struct lyd_node *node;
200 enum lyxp_node_type type;
201 uint32_t pos;
202 } *nodes;
Michal Vasko11306452018-04-25 16:11:13 +0200203 struct lyxp_set_snode {
Michal Vasko5b3492c2016-07-20 09:37:40 +0200204 struct lys_node *snode;
205 enum lyxp_node_type type;
206 /* 0 - snode was traversed, but not currently in the context,
207 * 1 - snode currently in context,
208 * 2 - snode in context and just added, so skip it for the current operation,
209 * >=3 - snode is not in context because we are in a predicate and this snode was used/will be used later */
210 uint32_t in_ctx;
211 } *snodes;
Michal Vasko11306452018-04-25 16:11:13 +0200212 struct lyxp_set_attr {
Michal Vasko8146d4c2016-05-09 15:50:29 +0200213 struct lyd_attr *attr;
214 enum lyxp_node_type type;
Michal Vasko5b3492c2016-07-20 09:37:40 +0200215 uint32_t pos; /* if node_type is LYXP_SET_NODE_ATTR, it is the parent node position */
Michal Vasko8146d4c2016-05-09 15:50:29 +0200216 } *attrs;
Michal Vasko40afe1a2016-08-22 14:20:43 +0200217 char *str;
Michal Vasko8146d4c2016-05-09 15:50:29 +0200218 long double num;
219 int bool;
220 } val;
221
Michal Vasko5b3492c2016-07-20 09:37:40 +0200222 /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SNODE_SET */
Michal Vasko8146d4c2016-05-09 15:50:29 +0200223 uint32_t used;
224 uint32_t size;
Michal Vaskod0484562018-04-26 14:30:47 +0200225#ifdef LY_ENABLED_CACHE
226 struct hash_table *ht;
227#endif
Michal Vasko5b3492c2016-07-20 09:37:40 +0200228 /* this is valid only for type LYXP_SET_NODE_SET */
Michal Vaskodb86ff42016-05-13 15:50:11 +0200229 uint32_t ctx_pos;
230 uint32_t ctx_size;
Michal Vasko8146d4c2016-05-09 15:50:29 +0200231};
232
Michal Vasko25895052015-09-21 11:41:12 +0200233/**
Michal Vasko6ed199c2016-02-04 12:07:19 +0100234 * @brief Evaluate the XPath expression \p expr on data. Be careful when using this function, the result can often
235 * be confusing without thorough understanding of XPath evaluation rules defined in RFC 6020.
Michal Vasko25895052015-09-21 11:41:12 +0200236 *
Michal Vasko30646e62015-10-09 14:02:09 +0200237 * @param[in] expr XPath expression to evaluate. Must be in JSON format (prefixes are model names).
Michal Vaskofdb73ae2016-08-24 16:02:12 +0200238 * @param[in] cur_node Current (context) data node. If the node has #LYD_VAL_INUSE flag, it is considered dummy (intended
239 * for but not restricted to evaluation with the LYXP_WHEN flag).
Michal Vaskob5157df2016-08-23 13:19:41 +0200240 * @param[in] cur_node_type Current (context) data node type. For every standard case use #LYXP_NODE_ELEM. But there are
241 * cases when the context node \p cur_node is actually supposed to be the XML root, there is no such data node. So, in
Michal Vaskob94a5e42016-09-08 14:01:56 +0200242 * this case just pass the first top-level node into \p cur_node and use an enum value for this kind of root
243 * (#LYXP_NODE_ROOT_CONFIG if \p cur_node has config true, otherwise #LYXP_NODE_ROOT). #LYXP_NODE_TEXT and #LYXP_NODE_ATTR can also be used,
Michal Vaskob5157df2016-08-23 13:19:41 +0200244 * but there are no use-cases in YANG.
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100245 * @param[in] local_mod Local module relative to the \p expr. Used only to determine the internal canonical value for identities.
Michal Vasko488590f2016-03-29 12:23:25 +0200246 * @param[out] set Result set. Must be valid and in the same libyang context as \p cur_node.
247 * To be safe, always either zero or cast the \p set to empty. After done using, either cast
248 * the \p set to empty (if allocated statically) or free it (if allocated dynamically) to
249 * prevent memory leaks.
Michal Vaskoa87d2582016-03-16 15:37:45 +0100250 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vasko5b3492c2016-07-20 09:37:40 +0200251 * LYXP_MUST - apply must data tree access restrictions.
252 * LYXP_WHEN - apply when data tree access restrictions and consider LYD_WHEN flags in data nodes.
Michal Vasko25895052015-09-21 11:41:12 +0200253 *
Michal Vaskoa87d2582016-03-16 15:37:45 +0100254 * @return EXIT_SUCCESS on success, EXIT_FAILURE on unresolved when dependency, -1 on error.
Michal Vasko25895052015-09-21 11:41:12 +0200255 */
Michal Vaskoa59495d2016-08-22 09:18:58 +0200256int lyxp_eval(const char *expr, const struct lyd_node *cur_node, enum lyxp_node_type cur_node_type,
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100257 const struct lys_module *local_mod, struct lyxp_set *set, int options);
Michal Vasko25895052015-09-21 11:41:12 +0200258
Michal Vasko5fb299e2015-10-06 15:44:55 +0200259/**
Michal Vasko5b3492c2016-07-20 09:37:40 +0200260 * @brief Get all the partial XPath nodes (atoms) that are required for \p expr to be evaluated.
261 *
Michal Vaskof96dfb62017-08-17 12:23:49 +0200262 * If any LYXP_SNODE* options is set, only fatal errors are printed, otherwise they are downgraded
263 * to warnings.
264 *
Michal Vasko5b3492c2016-07-20 09:37:40 +0200265 * @param[in] expr XPath expression to be evaluated. Must be in JSON format (prefixes are model names).
266 * @param[in] cur_snode Current (context) schema node.
Michal Vasko508a50d2016-09-07 14:50:33 +0200267 * @param[in] cur_snode_type Current (context) schema node type.
Michal Vasko5b3492c2016-07-20 09:37:40 +0200268 * @param[out] set Result set. Must be valid and in the same libyang context as \p cur_snode.
269 * To be safe, always either zero or cast the \p set to empty. After done using, either cast
270 * the \p set to empty (if allocated statically) or free it (if allocated dynamically) to
271 * prevent memory leaks.
272 * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used.
273 * LYXP_SNODE - no special data tree access modifiers.
274 * LYXP_SNODE_MUST - apply must data tree access restrictions.
275 * LYXP_SNODE_WHEN - apply when data tree access restrictions.
Michal Vaskob94a5e42016-09-08 14:01:56 +0200276 * LYXP_SNODE_OUTPUT - search RPC/action output instead input
Michal Vasko41cb7c32017-07-03 12:59:56 +0200277 * @param[out] ctx_snode Actual context node for the expression (it often changes for "when" expressions).
Michal Vasko5b3492c2016-07-20 09:37:40 +0200278 *
279 * @return EXIT_SUCCESS on success, -1 on error.
280 */
Michal Vasko508a50d2016-09-07 14:50:33 +0200281int lyxp_atomize(const char *expr, const struct lys_node *cur_snode, enum lyxp_node_type cur_snode_type,
Michal Vasko41cb7c32017-07-03 12:59:56 +0200282 struct lyxp_set *set, int options, const struct lys_node **ctx_snode);
Michal Vasko5b3492c2016-07-20 09:37:40 +0200283
284/* these are used only internally */
285#define LYXP_SNODE 0x04
286#define LYXP_SNODE_MUST 0x08
287#define LYXP_SNODE_WHEN 0x10
Michal Vaskodb1da032016-09-08 10:07:38 +0200288#define LYXP_SNODE_OUTPUT 0x20
Michal Vasko5b3492c2016-07-20 09:37:40 +0200289
290#define LYXP_SNODE_ALL 0x1C
291
292/**
Michal Vasko508a50d2016-09-07 14:50:33 +0200293 * @brief Works like lyxp_atomize(), but it is executed on all the when and must expressions
294 * which the node has.
Michal Vasko7e18c452015-10-07 09:34:36 +0200295 *
Michal Vasko508a50d2016-09-07 14:50:33 +0200296 * @param[in] node Node to examine.
297 * @param[in,out] set Resulting set of atoms merged from all the expressions.
298 * Will be cleared before use.
Michal Vasko0b963112017-08-11 12:45:36 +0200299 * @param[in] set_ext_dep_flags Set #LYS_XPATH_DEP for conditions that require foreign subtree and
300 * also for the node itself, if it has any such condition.
Michal Vasko7e18c452015-10-07 09:34:36 +0200301 *
Michal Vaskof96dfb62017-08-17 12:23:49 +0200302 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko7e18c452015-10-07 09:34:36 +0200303 */
Michal Vaskof96dfb62017-08-17 12:23:49 +0200304int lyxp_node_atomize(const struct lys_node *node, struct lyxp_set *set, int set_ext_dep_flags);
Michal Vasko7e18c452015-10-07 09:34:36 +0200305
306/**
Michal Vasko364918a2017-03-17 13:23:46 +0100307 * @brief Check syntax of all the XPath expressions of the node.
308 *
309 * @param[in] node Node to examine.
310 *
311 * @return EXIT_SUCCESS on success, -1 on error.
312 */
313int lyxp_node_check_syntax(const struct lys_node *node);
314
315/**
Michal Vaskocf024702015-10-08 15:01:42 +0200316 * @brief Cast XPath set to another type.
317 * Indirectly context position aware.
318 *
319 * @param[in] set Set to cast.
320 * @param[in] target Target type to cast \p set into.
Michal Vasko488590f2016-03-29 12:23:25 +0200321 * @param[in] cur_node Current (context) data node. Cannot be NULL.
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100322 * @param[in] local_mod Local expression module.
Michal Vaskoa87d2582016-03-16 15:37:45 +0100323 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vaskofdb73ae2016-08-24 16:02:12 +0200324 *
325 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +0200326 */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100327int lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target, const struct lyd_node *cur_node,
328 const struct lys_module *local_mod, int options);
Michal Vasko04854892015-10-06 15:45:43 +0200329
330/**
Michal Vasko5fb299e2015-10-06 15:44:55 +0200331 * @brief Free contents of an XPath \p set.
332 *
333 * @param[in] set Set to free.
Michal Vasko5fb299e2015-10-06 15:44:55 +0200334 */
Michal Vasko40afe1a2016-08-22 14:20:43 +0200335void lyxp_set_free(struct lyxp_set *set);
Michal Vasko5fb299e2015-10-06 15:44:55 +0200336
Michal Vasko56d082c2016-10-25 14:00:42 +0200337/**
338 * @brief Parse an XPath expression into a structure of tokens.
339 * Logs directly.
340 *
341 * http://www.w3.org/TR/1999/REC-xpath-19991116/ section 3.7
342 *
Michal Vasko53b7da02018-02-13 15:28:42 +0100343 * @param[in] ctx Context for errors.
Michal Vasko56d082c2016-10-25 14:00:42 +0200344 * @param[in] expr XPath expression to parse. It is duplicated.
345 *
346 * @return Filled expression structure or NULL on error.
347 */
Michal Vasko53b7da02018-02-13 15:28:42 +0100348struct lyxp_expr *lyxp_parse_expr(struct ly_ctx *ctx, const char *expr);
Michal Vasko56d082c2016-10-25 14:00:42 +0200349
350/**
Michal Vasko89afc112017-03-16 13:57:28 +0100351 * @brief Frees a parsed XPath expression. \p expr should not be used afterwards.
352 *
353 * @param[in] expr Expression to free.
354 */
355void lyxp_expr_free(struct lyxp_expr *expr);
Michal Vasko56d082c2016-10-25 14:00:42 +0200356
Michal Vasko25895052015-09-21 11:41:12 +0200357#endif /* _XPATH_H */