blob: 4d56f7adcd2d395fcdfb3b7180b4c7019c820e02 [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
180/**
Michal Vasko8146d4c2016-05-09 15:50:29 +0200181 * @brief XPath set - (partial) result.
182 */
183struct lyxp_set {
184 enum lyxp_set_type type;
185 union {
186 struct lyxp_set_nodes {
187 struct lyd_node *node;
188 enum lyxp_node_type type;
189 uint32_t pos;
190 } *nodes;
Michal Vasko5b3492c2016-07-20 09:37:40 +0200191 struct lyxp_set_snodes {
192 struct lys_node *snode;
193 enum lyxp_node_type type;
194 /* 0 - snode was traversed, but not currently in the context,
195 * 1 - snode currently in context,
196 * 2 - snode in context and just added, so skip it for the current operation,
197 * >=3 - snode is not in context because we are in a predicate and this snode was used/will be used later */
198 uint32_t in_ctx;
199 } *snodes;
Michal Vasko8146d4c2016-05-09 15:50:29 +0200200 struct lyxp_set_attrs {
201 struct lyd_attr *attr;
202 enum lyxp_node_type type;
Michal Vasko5b3492c2016-07-20 09:37:40 +0200203 uint32_t pos; /* if node_type is LYXP_SET_NODE_ATTR, it is the parent node position */
Michal Vasko8146d4c2016-05-09 15:50:29 +0200204 } *attrs;
Michal Vasko40afe1a2016-08-22 14:20:43 +0200205 char *str;
Michal Vasko8146d4c2016-05-09 15:50:29 +0200206 long double num;
207 int bool;
208 } val;
209
Michal Vasko5b3492c2016-07-20 09:37:40 +0200210 /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SNODE_SET */
Michal Vasko8146d4c2016-05-09 15:50:29 +0200211 uint32_t used;
212 uint32_t size;
Michal Vasko5b3492c2016-07-20 09:37:40 +0200213 /* this is valid only for type LYXP_SET_NODE_SET */
Michal Vaskodb86ff42016-05-13 15:50:11 +0200214 uint32_t ctx_pos;
215 uint32_t ctx_size;
Michal Vasko8146d4c2016-05-09 15:50:29 +0200216};
217
Michal Vasko25895052015-09-21 11:41:12 +0200218/**
Michal Vasko6ed199c2016-02-04 12:07:19 +0100219 * @brief Evaluate the XPath expression \p expr on data. Be careful when using this function, the result can often
220 * be confusing without thorough understanding of XPath evaluation rules defined in RFC 6020.
Michal Vasko25895052015-09-21 11:41:12 +0200221 *
Michal Vasko30646e62015-10-09 14:02:09 +0200222 * @param[in] expr XPath expression to evaluate. Must be in JSON format (prefixes are model names).
Michal Vaskofdb73ae2016-08-24 16:02:12 +0200223 * @param[in] cur_node Current (context) data node. If the node has #LYD_VAL_INUSE flag, it is considered dummy (intended
224 * for but not restricted to evaluation with the LYXP_WHEN flag).
Michal Vaskob5157df2016-08-23 13:19:41 +0200225 * @param[in] cur_node_type Current (context) data node type. For every standard case use #LYXP_NODE_ELEM. But there are
226 * 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 +0200227 * this case just pass the first top-level node into \p cur_node and use an enum value for this kind of root
228 * (#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 +0200229 * but there are no use-cases in YANG.
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100230 * @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 +0200231 * @param[out] set Result set. Must be valid and in the same libyang context as \p cur_node.
232 * To be safe, always either zero or cast the \p set to empty. After done using, either cast
233 * the \p set to empty (if allocated statically) or free it (if allocated dynamically) to
234 * prevent memory leaks.
Michal Vaskoa87d2582016-03-16 15:37:45 +0100235 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vasko5b3492c2016-07-20 09:37:40 +0200236 * LYXP_MUST - apply must data tree access restrictions.
237 * LYXP_WHEN - apply when data tree access restrictions and consider LYD_WHEN flags in data nodes.
Michal Vasko25895052015-09-21 11:41:12 +0200238 *
Michal Vaskoa87d2582016-03-16 15:37:45 +0100239 * @return EXIT_SUCCESS on success, EXIT_FAILURE on unresolved when dependency, -1 on error.
Michal Vasko25895052015-09-21 11:41:12 +0200240 */
Michal Vaskoa59495d2016-08-22 09:18:58 +0200241int 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 +0100242 const struct lys_module *local_mod, struct lyxp_set *set, int options);
Michal Vasko25895052015-09-21 11:41:12 +0200243
Michal Vasko5fb299e2015-10-06 15:44:55 +0200244/**
Michal Vasko5b3492c2016-07-20 09:37:40 +0200245 * @brief Get all the partial XPath nodes (atoms) that are required for \p expr to be evaluated.
246 *
Michal Vaskof96dfb62017-08-17 12:23:49 +0200247 * If any LYXP_SNODE* options is set, only fatal errors are printed, otherwise they are downgraded
248 * to warnings.
249 *
Michal Vasko5b3492c2016-07-20 09:37:40 +0200250 * @param[in] expr XPath expression to be evaluated. Must be in JSON format (prefixes are model names).
251 * @param[in] cur_snode Current (context) schema node.
Michal Vasko508a50d2016-09-07 14:50:33 +0200252 * @param[in] cur_snode_type Current (context) schema node type.
Michal Vasko5b3492c2016-07-20 09:37:40 +0200253 * @param[out] set Result set. Must be valid and in the same libyang context as \p cur_snode.
254 * To be safe, always either zero or cast the \p set to empty. After done using, either cast
255 * the \p set to empty (if allocated statically) or free it (if allocated dynamically) to
256 * prevent memory leaks.
257 * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used.
258 * LYXP_SNODE - no special data tree access modifiers.
259 * LYXP_SNODE_MUST - apply must data tree access restrictions.
260 * LYXP_SNODE_WHEN - apply when data tree access restrictions.
Michal Vaskob94a5e42016-09-08 14:01:56 +0200261 * LYXP_SNODE_OUTPUT - search RPC/action output instead input
Michal Vasko41cb7c32017-07-03 12:59:56 +0200262 * @param[out] ctx_snode Actual context node for the expression (it often changes for "when" expressions).
Michal Vasko5b3492c2016-07-20 09:37:40 +0200263 *
264 * @return EXIT_SUCCESS on success, -1 on error.
265 */
Michal Vasko508a50d2016-09-07 14:50:33 +0200266int 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 +0200267 struct lyxp_set *set, int options, const struct lys_node **ctx_snode);
Michal Vasko5b3492c2016-07-20 09:37:40 +0200268
269/* these are used only internally */
270#define LYXP_SNODE 0x04
271#define LYXP_SNODE_MUST 0x08
272#define LYXP_SNODE_WHEN 0x10
Michal Vaskodb1da032016-09-08 10:07:38 +0200273#define LYXP_SNODE_OUTPUT 0x20
Michal Vasko5b3492c2016-07-20 09:37:40 +0200274
275#define LYXP_SNODE_ALL 0x1C
276
277/**
Michal Vasko508a50d2016-09-07 14:50:33 +0200278 * @brief Works like lyxp_atomize(), but it is executed on all the when and must expressions
279 * which the node has.
Michal Vasko7e18c452015-10-07 09:34:36 +0200280 *
Michal Vasko508a50d2016-09-07 14:50:33 +0200281 * @param[in] node Node to examine.
282 * @param[in,out] set Resulting set of atoms merged from all the expressions.
283 * Will be cleared before use.
Michal Vasko0b963112017-08-11 12:45:36 +0200284 * @param[in] set_ext_dep_flags Set #LYS_XPATH_DEP for conditions that require foreign subtree and
285 * also for the node itself, if it has any such condition.
Michal Vasko7e18c452015-10-07 09:34:36 +0200286 *
Michal Vaskof96dfb62017-08-17 12:23:49 +0200287 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vasko7e18c452015-10-07 09:34:36 +0200288 */
Michal Vaskof96dfb62017-08-17 12:23:49 +0200289int lyxp_node_atomize(const struct lys_node *node, struct lyxp_set *set, int set_ext_dep_flags);
Michal Vasko7e18c452015-10-07 09:34:36 +0200290
291/**
Michal Vasko364918a2017-03-17 13:23:46 +0100292 * @brief Check syntax of all the XPath expressions of the node.
293 *
294 * @param[in] node Node to examine.
295 *
296 * @return EXIT_SUCCESS on success, -1 on error.
297 */
298int lyxp_node_check_syntax(const struct lys_node *node);
299
300/**
Michal Vaskocf024702015-10-08 15:01:42 +0200301 * @brief Cast XPath set to another type.
302 * Indirectly context position aware.
303 *
304 * @param[in] set Set to cast.
305 * @param[in] target Target type to cast \p set into.
Michal Vasko488590f2016-03-29 12:23:25 +0200306 * @param[in] cur_node Current (context) data node. Cannot be NULL.
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100307 * @param[in] local_mod Local expression module.
Michal Vaskoa87d2582016-03-16 15:37:45 +0100308 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vaskofdb73ae2016-08-24 16:02:12 +0200309 *
310 * @return EXIT_SUCCESS on success, -1 on error.
Michal Vaskocf024702015-10-08 15:01:42 +0200311 */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100312int lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target, const struct lyd_node *cur_node,
313 const struct lys_module *local_mod, int options);
Michal Vasko04854892015-10-06 15:45:43 +0200314
315/**
Michal Vasko5fb299e2015-10-06 15:44:55 +0200316 * @brief Free contents of an XPath \p set.
317 *
318 * @param[in] set Set to free.
Michal Vasko5fb299e2015-10-06 15:44:55 +0200319 */
Michal Vasko40afe1a2016-08-22 14:20:43 +0200320void lyxp_set_free(struct lyxp_set *set);
Michal Vasko5fb299e2015-10-06 15:44:55 +0200321
Michal Vasko56d082c2016-10-25 14:00:42 +0200322/**
323 * @brief Parse an XPath expression into a structure of tokens.
324 * Logs directly.
325 *
326 * http://www.w3.org/TR/1999/REC-xpath-19991116/ section 3.7
327 *
Michal Vasko53b7da02018-02-13 15:28:42 +0100328 * @param[in] ctx Context for errors.
Michal Vasko56d082c2016-10-25 14:00:42 +0200329 * @param[in] expr XPath expression to parse. It is duplicated.
330 *
331 * @return Filled expression structure or NULL on error.
332 */
Michal Vasko53b7da02018-02-13 15:28:42 +0100333struct lyxp_expr *lyxp_parse_expr(struct ly_ctx *ctx, const char *expr);
Michal Vasko56d082c2016-10-25 14:00:42 +0200334
335/**
Michal Vasko89afc112017-03-16 13:57:28 +0100336 * @brief Frees a parsed XPath expression. \p expr should not be used afterwards.
337 *
338 * @param[in] expr Expression to free.
339 */
340void lyxp_expr_free(struct lyxp_expr *expr);
Michal Vasko56d082c2016-10-25 14:00:42 +0200341
Michal Vasko25895052015-09-21 11:41:12 +0200342#endif /* _XPATH_H */