blob: 86a4823fac795812ab7bb193eef824cce693ca46 [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,
121 LYXP_EXPR_UNION,
122};
123
124/**
Michal Vasko25895052015-09-21 11:41:12 +0200125 * @brief Structure holding a parsed XPath expression.
126 */
127struct lyxp_expr {
128 enum lyxp_token *tokens; /* array of tokens */
129 uint16_t *expr_pos; /* array of pointers to the expression in expr (idx of the beginning) */
130 uint8_t *tok_len; /* array of token lengths in expr */
Michal Vasko764c8c42017-11-07 15:43:06 +0100131 enum lyxp_expr_type **repeat; /* array of expression types that this token begins and is repeated ended with 0,
132 more in the comment after this declaration */
Michal Vasko25895052015-09-21 11:41:12 +0200133 uint16_t used; /* used array items */
134 uint16_t size; /* allocated array items */
135
Michal Vasko2b84ea72016-05-05 17:56:58 +0200136 char *expr; /* the original XPath expression */
Michal Vasko25895052015-09-21 11:41:12 +0200137};
138
139/*
140 * lyxp_expr repeat
141 *
142 * This value is NULL for all the tokens that do not begin an
143 * expression which can be repeated. Otherwise it is an array
Michal Vasko764c8c42017-11-07 15:43:06 +0100144 * of expression types that this token begins. These values
Michal Vasko25895052015-09-21 11:41:12 +0200145 * are used during evaluation to know whether we need to
Michal Vasko764c8c42017-11-07 15:43:06 +0100146 * duplicate the current context or not and to decide what
147 * the current expression is (for example, if we are only
148 * starting the parsing and the first token has no repeat,
149 * we do not parse it as an OrExpr but directly as PathExpr).
150 * Examples:
Michal Vasko25895052015-09-21 11:41:12 +0200151 *
152 * Expression: "/ *[key1 and key2 or key1 < key2]"
Michal Vasko764c8c42017-11-07 15:43:06 +0100153 * Tokens: '/', '*', '[', NameTest, 'and', NameTest, 'or', NameTest, '<', NameTest, ']'
154 * Repeat: NULL, NULL, NULL, [AndExpr, NULL, NULL, NULL, [RelationalExpr, NULL, NULL, NULL
155 * OrExpr, 0],
156 * 0],
Michal Vasko25895052015-09-21 11:41:12 +0200157 *
158 * Expression: "//node[key and node2]/key | /cont"
Michal Vasko764c8c42017-11-07 15:43:06 +0100159 * Tokens: '//', 'NameTest', '[', 'NameTest', 'and', 'NameTest', ']', '/', 'NameTest', '|', '/', 'NameTest'
160 * Repeat: [UnionExpr, NULL, NULL, [AndExpr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
161 * 0], 0],
Michal Vasko25895052015-09-21 11:41:12 +0200162 *
Michal Vasko56d082c2016-10-25 14:00:42 +0200163 * Operators between expressions which this concerns:
Michal Vasko25895052015-09-21 11:41:12 +0200164 * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|'
165 */
166
167/**
168 * @brief Supported types of (partial) XPath results.
169 */
170enum lyxp_set_type {
171 LYXP_SET_EMPTY = 0,
172 LYXP_SET_NODE_SET,
Michal Vasko5b3492c2016-07-20 09:37:40 +0200173 LYXP_SET_SNODE_SET,
Michal Vasko25895052015-09-21 11:41:12 +0200174 LYXP_SET_BOOLEAN,
175 LYXP_SET_NUMBER,
176 LYXP_SET_STRING
177};
178
179/**
Michal Vasko8146d4c2016-05-09 15:50:29 +0200180 * @brief XPath set - (partial) result.
181 */
182struct lyxp_set {
183 enum lyxp_set_type type;
184 union {
185 struct lyxp_set_nodes {
186 struct lyd_node *node;
187 enum lyxp_node_type type;
188 uint32_t pos;
189 } *nodes;
Michal Vasko5b3492c2016-07-20 09:37:40 +0200190 struct lyxp_set_snodes {
191 struct lys_node *snode;
192 enum lyxp_node_type type;
193 /* 0 - snode was traversed, but not currently in the context,
194 * 1 - snode currently in context,
195 * 2 - snode in context and just added, so skip it for the current operation,
196 * >=3 - snode is not in context because we are in a predicate and this snode was used/will be used later */
197 uint32_t in_ctx;
198 } *snodes;
Michal Vasko8146d4c2016-05-09 15:50:29 +0200199 struct lyxp_set_attrs {
200 struct lyd_attr *attr;
201 enum lyxp_node_type type;
Michal Vasko5b3492c2016-07-20 09:37:40 +0200202 uint32_t pos; /* if node_type is LYXP_SET_NODE_ATTR, it is the parent node position */
Michal Vasko8146d4c2016-05-09 15:50:29 +0200203 } *attrs;
Michal Vasko40afe1a2016-08-22 14:20:43 +0200204 char *str;
Michal Vasko8146d4c2016-05-09 15:50:29 +0200205 long double num;
206 int bool;
207 } val;
208
Michal Vasko5b3492c2016-07-20 09:37:40 +0200209 /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SNODE_SET */
Michal Vasko8146d4c2016-05-09 15:50:29 +0200210 uint32_t used;
211 uint32_t size;
Michal Vasko5b3492c2016-07-20 09:37:40 +0200212 /* this is valid only for type LYXP_SET_NODE_SET */
Michal Vaskodb86ff42016-05-13 15:50:11 +0200213 uint32_t ctx_pos;
214 uint32_t ctx_size;
Michal Vasko8146d4c2016-05-09 15:50:29 +0200215};
216
Michal Vasko25895052015-09-21 11:41:12 +0200217/**
Michal Vasko6ed199c2016-02-04 12:07:19 +0100218 * @brief Evaluate the XPath expression \p expr on data. Be careful when using this function, the result can often
219 * be confusing without thorough understanding of XPath evaluation rules defined in RFC 6020.
Michal Vasko25895052015-09-21 11:41:12 +0200220 *
Michal Vasko30646e62015-10-09 14:02:09 +0200221 * @param[in] expr XPath expression to evaluate. Must be in JSON format (prefixes are model names).
Michal Vaskofdb73ae2016-08-24 16:02:12 +0200222 * @param[in] cur_node Current (context) data node. If the node has #LYD_VAL_INUSE flag, it is considered dummy (intended
223 * for but not restricted to evaluation with the LYXP_WHEN flag).
Michal Vaskob5157df2016-08-23 13:19:41 +0200224 * @param[in] cur_node_type Current (context) data node type. For every standard case use #LYXP_NODE_ELEM. But there are
225 * 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 +0200226 * this case just pass the first top-level node into \p cur_node and use an enum value for this kind of root
227 * (#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 +0200228 * but there are no use-cases in YANG.
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100229 * @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 +0200230 * @param[out] set Result set. Must be valid and in the same libyang context as \p cur_node.
231 * To be safe, always either zero or cast the \p set to empty. After done using, either cast
232 * the \p set to empty (if allocated statically) or free it (if allocated dynamically) to
233 * prevent memory leaks.
Michal Vaskoa87d2582016-03-16 15:37:45 +0100234 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vasko5b3492c2016-07-20 09:37:40 +0200235 * LYXP_MUST - apply must data tree access restrictions.
236 * LYXP_WHEN - apply when data tree access restrictions and consider LYD_WHEN flags in data nodes.
Michal Vasko25895052015-09-21 11:41:12 +0200237 *
Michal Vaskoa87d2582016-03-16 15:37:45 +0100238 * @return EXIT_SUCCESS on success, EXIT_FAILURE on unresolved when dependency, -1 on error.
Michal Vasko25895052015-09-21 11:41:12 +0200239 */
Michal Vaskoa59495d2016-08-22 09:18:58 +0200240int 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 +0100241 const struct lys_module *local_mod, struct lyxp_set *set, int options);
Michal Vasko25895052015-09-21 11:41:12 +0200242
Michal Vasko5fb299e2015-10-06 15:44:55 +0200243/**
Michal Vasko5b3492c2016-07-20 09:37:40 +0200244 * @brief Get all the partial XPath nodes (atoms) that are required for \p expr to be evaluated.
245 *
Michal Vaskof96dfb62017-08-17 12:23:49 +0200246 * If any LYXP_SNODE* options is set, only fatal errors are printed, otherwise they are downgraded
247 * to warnings.
248 *
Michal Vasko5b3492c2016-07-20 09:37:40 +0200249 * @param[in] expr XPath expression to be evaluated. Must be in JSON format (prefixes are model names).
250 * @param[in] cur_snode Current (context) schema node.
Michal Vasko508a50d2016-09-07 14:50:33 +0200251 * @param[in] cur_snode_type Current (context) schema node type.
Michal Vasko5b3492c2016-07-20 09:37:40 +0200252 * @param[out] set Result set. Must be valid and in the same libyang context as \p cur_snode.
253 * To be safe, always either zero or cast the \p set to empty. After done using, either cast
254 * the \p set to empty (if allocated statically) or free it (if allocated dynamically) to
255 * prevent memory leaks.
256 * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used.
257 * LYXP_SNODE - no special data tree access modifiers.
258 * LYXP_SNODE_MUST - apply must data tree access restrictions.
259 * LYXP_SNODE_WHEN - apply when data tree access restrictions.
Michal Vaskob94a5e42016-09-08 14:01:56 +0200260 * LYXP_SNODE_OUTPUT - search RPC/action output instead input
Michal Vasko41cb7c32017-07-03 12:59:56 +0200261 * @param[out] ctx_snode Actual context node for the expression (it often changes for "when" expressions).
Michal Vasko5b3492c2016-07-20 09:37:40 +0200262 *
263 * @return EXIT_SUCCESS on success, -1 on error.
264 */
Michal Vasko508a50d2016-09-07 14:50:33 +0200265int 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 +0200266 struct lyxp_set *set, int options, const struct lys_node **ctx_snode);
Michal Vasko5b3492c2016-07-20 09:37:40 +0200267
268/* these are used only internally */
269#define LYXP_SNODE 0x04
270#define LYXP_SNODE_MUST 0x08
271#define LYXP_SNODE_WHEN 0x10
Michal Vaskodb1da032016-09-08 10:07:38 +0200272#define LYXP_SNODE_OUTPUT 0x20
Michal Vasko5b3492c2016-07-20 09:37:40 +0200273
274#define LYXP_SNODE_ALL 0x1C
275
276/**
Michal Vasko508a50d2016-09-07 14:50:33 +0200277 * @brief Works like lyxp_atomize(), but it is executed on all the when and must expressions
278 * which the node has.
Michal Vasko7e18c452015-10-07 09:34:36 +0200279 *
Michal Vasko508a50d2016-09-07 14:50:33 +0200280 * @param[in] node Node to examine.
281 * @param[in,out] set Resulting set of atoms merged from all the expressions.
282 * Will be cleared before use.
Michal Vasko0b963112017-08-11 12:45:36 +0200283 * @param[in] set_ext_dep_flags Set #LYS_XPATH_DEP for conditions that require foreign subtree and
284 * also for the node itself, if it has any such condition.
Michal Vasko7e18c452015-10-07 09:34:36 +0200285 *
Michal Vaskof96dfb62017-08-17 12:23:49 +0200286 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko7e18c452015-10-07 09:34:36 +0200287 */
Michal Vaskof96dfb62017-08-17 12:23:49 +0200288int lyxp_node_atomize(const struct lys_node *node, struct lyxp_set *set, int set_ext_dep_flags);
Michal Vasko7e18c452015-10-07 09:34:36 +0200289
290/**
Michal Vasko364918a2017-03-17 13:23:46 +0100291 * @brief Check syntax of all the XPath expressions of the node.
292 *
293 * @param[in] node Node to examine.
294 *
295 * @return EXIT_SUCCESS on success, -1 on error.
296 */
297int lyxp_node_check_syntax(const struct lys_node *node);
298
299/**
Michal Vaskocf024702015-10-08 15:01:42 +0200300 * @brief Cast XPath set to another type.
301 * Indirectly context position aware.
302 *
303 * @param[in] set Set to cast.
304 * @param[in] target Target type to cast \p set into.
Michal Vasko488590f2016-03-29 12:23:25 +0200305 * @param[in] cur_node Current (context) data node. Cannot be NULL.
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100306 * @param[in] local_mod Local expression module.
Michal Vaskoa87d2582016-03-16 15:37:45 +0100307 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vaskofdb73ae2016-08-24 16:02:12 +0200308 *
309 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +0200310 */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100311int lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target, const struct lyd_node *cur_node,
312 const struct lys_module *local_mod, int options);
Michal Vasko04854892015-10-06 15:45:43 +0200313
314/**
Michal Vasko5fb299e2015-10-06 15:44:55 +0200315 * @brief Free contents of an XPath \p set.
316 *
317 * @param[in] set Set to free.
Michal Vasko5fb299e2015-10-06 15:44:55 +0200318 */
Michal Vasko40afe1a2016-08-22 14:20:43 +0200319void lyxp_set_free(struct lyxp_set *set);
Michal Vasko5fb299e2015-10-06 15:44:55 +0200320
Michal Vasko56d082c2016-10-25 14:00:42 +0200321/**
322 * @brief Parse an XPath expression into a structure of tokens.
323 * Logs directly.
324 *
325 * http://www.w3.org/TR/1999/REC-xpath-19991116/ section 3.7
326 *
327 * @param[in] expr XPath expression to parse. It is duplicated.
328 *
329 * @return Filled expression structure or NULL on error.
330 */
331struct lyxp_expr *lyxp_parse_expr(const char *expr);
332
333/**
Michal Vasko89afc112017-03-16 13:57:28 +0100334 * @brief Frees a parsed XPath expression. \p expr should not be used afterwards.
335 *
336 * @param[in] expr Expression to free.
337 */
338void lyxp_expr_free(struct lyxp_expr *expr);
Michal Vasko56d082c2016-10-25 14:00:42 +0200339
Michal Vasko25895052015-09-21 11:41:12 +0200340#endif /* _XPATH_H */