blob: b2f2eb51854cf51e85d5c275671cf67bcd3a4321 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions header
5 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
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
15#ifndef _XPATH_H
16#define _XPATH_H
17
18#include <stdint.h>
19
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include "log.h"
21
22struct ly_ctx;
23struct lysc_node;
24
Radek Krejcib1646a92018-11-02 16:08:26 +010025/*
26 * XPath evaluator fully compliant with http://www.w3.org/TR/1999/REC-xpath-19991116/
27 * except the following restrictions in the grammar.
28 *
29 * PARSED GRAMMAR
30 *
31 * Full axes are not supported, abbreviated forms must be used,
32 * variables are not supported, "id()" function is not supported,
33 * and processing instruction and comment nodes are not supported,
34 * which is also reflected in the grammar. Undefined rules and
35 * constants are tokens.
36 *
37 * Modified full grammar:
38 *
39 * [1] Expr ::= OrExpr // just an alias
40 *
41 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
42 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
43 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
44 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
45 * [6] NodeTest ::= NameTest | NodeType '(' ')'
46 * [7] Predicate ::= '[' Expr ']'
47 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
48 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
49 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
50 * | PrimaryExpr Predicate* '/' RelativeLocationPath
51 * | PrimaryExpr Predicate* '//' RelativeLocationPath
52 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
53 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
54 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
55 * | EqualityExpr '!=' RelationalExpr
56 * [14] RelationalExpr ::= AdditiveExpr
57 * | RelationalExpr '<' AdditiveExpr
58 * | RelationalExpr '>' AdditiveExpr
59 * | RelationalExpr '<=' AdditiveExpr
60 * | RelationalExpr '>=' AdditiveExpr
61 * [15] AdditiveExpr ::= MultiplicativeExpr
62 * | AdditiveExpr '+' MultiplicativeExpr
63 * | AdditiveExpr '-' MultiplicativeExpr
64 * [16] MultiplicativeExpr ::= UnaryExpr
65 * | MultiplicativeExpr '*' UnaryExpr
66 * | MultiplicativeExpr 'div' UnaryExpr
67 * | MultiplicativeExpr 'mod' UnaryExpr
68 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
69 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
70 */
71
72/* expression tokens allocation */
73#define LYXP_EXPR_SIZE_START 10
74#define LYXP_EXPR_SIZE_STEP 5
75
76/* XPath matches allocation */
77#define LYXP_SET_SIZE_START 2
78#define LYXP_SET_SIZE_STEP 2
79
80/* building string when casting */
81#define LYXP_STRING_CAST_SIZE_START 64
82#define LYXP_STRING_CAST_SIZE_STEP 16
83
84/**
85 * @brief Tokens that can be in an XPath expression.
86 */
87enum lyxp_token {
88 LYXP_TOKEN_NONE = 0,
89 LYXP_TOKEN_PAR1, /* '(' */
90 LYXP_TOKEN_PAR2, /* ')' */
91 LYXP_TOKEN_BRACK1, /* '[' */
92 LYXP_TOKEN_BRACK2, /* ']' */
93 LYXP_TOKEN_DOT, /* '.' */
94 LYXP_TOKEN_DDOT, /* '..' */
95 LYXP_TOKEN_AT, /* '@' */
96 LYXP_TOKEN_COMMA, /* ',' */
97 /* LYXP_TOKEN_DCOLON, * '::' * axes not supported */
98 LYXP_TOKEN_NAMETEST, /* NameTest */
99 LYXP_TOKEN_NODETYPE, /* NodeType */
100 LYXP_TOKEN_FUNCNAME, /* FunctionName */
101 LYXP_TOKEN_OPERATOR_LOG, /* Operator 'and', 'or' */
102 LYXP_TOKEN_OPERATOR_COMP, /* Operator '=', '!=', '<', '<=', '>', '>=' */
103 LYXP_TOKEN_OPERATOR_MATH, /* Operator '+', '-', '*', 'div', 'mod', '-' (unary) */
104 LYXP_TOKEN_OPERATOR_UNI, /* Operator '|' */
105 LYXP_TOKEN_OPERATOR_PATH, /* Operator '/', '//' */
106 /* LYXP_TOKEN_AXISNAME, * AxisName * axes not supported */
107 LYXP_TOKEN_LITERAL, /* Literal - with either single or double quote */
108 LYXP_TOKEN_NUMBER /* Number */
109};
110
111/**
112 * @brief XPath (sub)expressions that can be repeated.
113 */
114enum lyxp_expr_type {
115 LYXP_EXPR_NONE = 0,
116 LYXP_EXPR_OR,
117 LYXP_EXPR_AND,
118 LYXP_EXPR_EQUALITY,
119 LYXP_EXPR_RELATIONAL,
120 LYXP_EXPR_ADDITIVE,
121 LYXP_EXPR_MULTIPLICATIVE,
122 LYXP_EXPR_UNARY,
123 LYXP_EXPR_UNION,
124};
125
126/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200127 * @brief Types of context nodes, #LYXP_NODE_ROOT_CONFIG used only in when or must conditions.
128 */
129enum lyxp_node_type {
Michal Vasko2caefc12019-11-14 16:07:56 +0100130 LYXP_NODE_NONE, /* invalid node type */
131
Michal Vasko03ff5a72019-09-11 13:49:33 +0200132 /* XML document roots */
133 LYXP_NODE_ROOT, /* access to all the data (node value first top-level node) */
134 LYXP_NODE_ROOT_CONFIG, /* <running> data context, no state data (node value first top-level node) */
135
136 /* XML elements */
137 LYXP_NODE_ELEM, /* XML element (most common) */
138 LYXP_NODE_TEXT, /* XML text element (extremely specific use, unlikely to be ever needed) */
139 LYXP_NODE_ATTR /* XML attribute (in YANG cannot happen, do not use for the context node) */
140};
141
142/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100143 * @brief Structure holding a parsed XPath expression.
144 */
145struct lyxp_expr {
146 enum lyxp_token *tokens; /* array of tokens */
147 uint16_t *tok_pos; /* array of the token offsets in expr */
148 uint16_t *tok_len; /* array of token lengths in expr */
149 enum lyxp_expr_type **repeat; /* array of expression types that this token begins and is repeated ended with 0,
150 more in the comment after this declaration */
151 uint16_t used; /* used array items */
152 uint16_t size; /* allocated array items */
153
154 const char *expr; /* the original XPath expression */
155};
156
157/*
158 * lyxp_expr repeat
159 *
160 * This value is NULL for all the tokens that do not begin an
161 * expression which can be repeated. Otherwise it is an array
162 * of expression types that this token begins. These values
163 * are used during evaluation to know whether we need to
164 * duplicate the current context or not and to decide what
165 * the current expression is (for example, if we are only
166 * starting the parsing and the first token has no repeat,
167 * we do not parse it as an OrExpr but directly as PathExpr).
168 * Examples:
169 *
170 * Expression: "/ *[key1 and key2 or key1 < key2]"
171 * Tokens: '/', '*', '[', NameTest, 'and', NameTest, 'or', NameTest, '<', NameTest, ']'
172 * Repeat: NULL, NULL, NULL, [AndExpr, NULL, NULL, NULL, [RelationalExpr, NULL, NULL, NULL
173 * OrExpr, 0],
174 * 0],
175 *
176 * Expression: "//node[key and node2]/key | /cont"
177 * Tokens: '//', 'NameTest', '[', 'NameTest', 'and', 'NameTest', ']', '/', 'NameTest', '|', '/', 'NameTest'
178 * Repeat: [UnionExpr, NULL, NULL, [AndExpr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
179 * 0], 0],
180 *
181 * Operators between expressions which this concerns:
182 * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|'
183 */
184
185/**
186 * @brief Supported types of (partial) XPath results.
187 */
188enum lyxp_set_type {
189 LYXP_SET_EMPTY = 0,
190 LYXP_SET_NODE_SET,
Michal Vasko03ff5a72019-09-11 13:49:33 +0200191 LYXP_SET_SCNODE_SET,
Radek Krejcib1646a92018-11-02 16:08:26 +0100192 LYXP_SET_BOOLEAN,
193 LYXP_SET_NUMBER,
194 LYXP_SET_STRING
195};
Radek Krejcib1646a92018-11-02 16:08:26 +0100196
197/**
198 * @brief Item stored in an XPath set hash table.
199 */
200struct lyxp_set_hash_node {
201 struct lyd_node *node;
202 enum lyxp_node_type type;
203} _PACKED;
204
Radek Krejcib1646a92018-11-02 16:08:26 +0100205/**
206 * @brief XPath set - (partial) result.
207 */
208struct lyxp_set {
209 enum lyxp_set_type type;
210 union {
211 struct lyxp_set_node {
212 struct lyd_node *node;
213 enum lyxp_node_type type;
214 uint32_t pos;
215 } *nodes;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200216 struct lyxp_set_scnode {
217 struct lysc_node *scnode;
Radek Krejcib1646a92018-11-02 16:08:26 +0100218 enum lyxp_node_type type;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100219 /* -2 - scnode not traversed, currently (the only node) in context;
220 * -1 - scnode not traversed except for the eval start, not currently in the context;
221 * 0 - scnode was traversed, but not currently in the context;
222 * 1 - scnode currently in context;
223 * 2 - scnode in context and just added, so skip it for the current operation;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200224 * >=3 - scnode is not in context because we are in a predicate and this scnode was used/will be used later */
Michal Vasko5c4e5892019-11-14 12:31:38 +0100225 int32_t in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200226 } *scnodes;
Radek Krejcib1646a92018-11-02 16:08:26 +0100227 struct lyxp_set_attr {
228 struct lyd_attr *attr;
229 enum lyxp_node_type type;
230 uint32_t pos; /* if node_type is LYXP_SET_NODE_ATTR, it is the parent node position */
231 } *attrs;
232 char *str;
233 long double num;
234 int bool;
235 } val;
236
Michal Vasko03ff5a72019-09-11 13:49:33 +0200237 /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SCNODE_SET */
Radek Krejcib1646a92018-11-02 16:08:26 +0100238 uint32_t used;
239 uint32_t size;
Radek Krejcib1646a92018-11-02 16:08:26 +0100240 struct hash_table *ht;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200241
242 /* XPath context information, this is valid only for type LYXP_SET_NODE_SET */
Radek Krejcib1646a92018-11-02 16:08:26 +0100243 uint32_t ctx_pos;
244 uint32_t ctx_size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200245
246 /* general context */
247 struct ly_ctx *ctx;
248 union {
249 const struct lyd_node *ctx_node;
250 const struct lysc_node *ctx_scnode;
251 };
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100252 enum lyxp_node_type root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200253 const struct lys_module *local_mod;
254 const struct lyd_node **trees;
255 LYD_FORMAT format;
Radek Krejcib1646a92018-11-02 16:08:26 +0100256};
257
258/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200259 * @brief Evaluate an XPath expression on data. Be careful when using this function, the result can often
260 * be confusing without thorough understanding of XPath evaluation rules defined in RFC 7950.
Radek Krejcib1646a92018-11-02 16:08:26 +0100261 *
Michal Vaskoecd62de2019-11-13 12:35:11 +0100262 * @param[in] exp Parsed XPath expression to be evaluated.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200263 * @param[in] format Format of the XPath expression (more specifcally, of any used prefixes).
264 * @param[in] local_mod Local module relative to the @p expr.
Michal Vaskod0aa1a82019-11-07 08:58:42 +0100265 * @param[in] ctx_node Current (context) data node, NULL for root node.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200266 * @param[in] ctx_node_type Current (context) data node type. For every standard case use #LYXP_NODE_ELEM. But there are
267 * cases when the context node @p ctx_node is actually supposed to be the XML root, there is no such data node. So, in
Michal Vaskod0aa1a82019-11-07 08:58:42 +0100268 * this case just pass NULL for @p ctx_node and use an enum value for this kind of root (#LYXP_NODE_ROOT_CONFIG if
269 * @p ctx_node has config true, otherwise #LYXP_NODE_ROOT). #LYXP_NODE_TEXT and #LYXP_NODE_ATTR can also be used,
Radek Krejcib1646a92018-11-02 16:08:26 +0100270 * but there are no use-cases in YANG.
Michal Vaskod0aa1a82019-11-07 08:58:42 +0100271 * @param[in] trees Data trees on which to perform the evaluation, they must include all the available tree (including
272 * the tree of @p ctx_node).
Michal Vasko03ff5a72019-09-11 13:49:33 +0200273 * @param[out] set Result set. Must be valid and in the same libyang context as @p ctx_node.
274 * To be safe, always either zero or cast the @p set to empty. After done using, either cast
275 * the @p set to empty (if allocated statically) or free it (if allocated dynamically) to
Radek Krejcib1646a92018-11-02 16:08:26 +0100276 * prevent memory leaks.
277 * @param[in] options Whether to apply some evaluation restrictions.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100278 * @return LY_ERR (LY_EINVAL, LY_EMEM, LY_EINT, LY_EVALID for invalid argument types/count,
279 * LY_EINCOMPLETE for unresolved when).
Radek Krejcib1646a92018-11-02 16:08:26 +0100280 */
Michal Vaskoecd62de2019-11-13 12:35:11 +0100281LY_ERR lyxp_eval(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Michal Vasko03ff5a72019-09-11 13:49:33 +0200282 enum lyxp_node_type ctx_node_type, const struct lyd_node **trees, struct lyxp_set *set, int options);
283
284#define LYXP_SCHEMA 0x01 /**< Apply data node access restrictions defined for 'when' and 'must' evaluation. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100285
286/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200287 * @brief Get all the partial XPath nodes (atoms) that are required for @p exp to be evaluated.
Radek Krejcib1646a92018-11-02 16:08:26 +0100288 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200289 * If any LYXP_SCNODE* options is set, only fatal errors are printed, otherwise they are downgraded
Radek Krejcib1646a92018-11-02 16:08:26 +0100290 * to warnings.
291 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200292 * @param[in] exp Parsed XPath expression to be evaluated.
293 * @param[in] format Format of the XPath expression (more specifcally, of any used prefixes).
294 * @param[in] local_mod Local module relative to the @p exp.
Michal Vaskod0aa1a82019-11-07 08:58:42 +0100295 * @param[in] ctx_scnode Current (context) schema node, NULL for root node.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200296 * @param[in] ctx_scnode_type Current (context) schema node type.
297 * @param[out] set Result set. Must be valid and in the same libyang context as @p ctx_scnode.
298 * To be safe, always either zero or cast the @p set to empty. After done using, either cast
299 * the @p set to empty (if allocated statically) or free it (if allocated dynamically) to
Radek Krejcib1646a92018-11-02 16:08:26 +0100300 * prevent memory leaks.
301 * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used.
Michal Vaskoecd62de2019-11-13 12:35:11 +0100302 * @return LY_ERR (same as lyxp_eval()).
Radek Krejcib1646a92018-11-02 16:08:26 +0100303 */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200304LY_ERR lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
305 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options);
Radek Krejcib1646a92018-11-02 16:08:26 +0100306
307/* these are used only internally */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200308#define LYXP_SCNODE 0x02 /**< No special data tree access modifiers. */
309#define LYXP_SCNODE_SCHEMA 0x04 /**< Apply schema node access restrictions defined for 'when' and 'must' evaluation. */
310#define LYXP_SCNODE_OUTPUT 0x08 /**< Search RPC/action output instead of input. */
Radek Krejcib1646a92018-11-02 16:08:26 +0100311
Michal Vasko03ff5a72019-09-11 13:49:33 +0200312#define LYXP_SCNODE_ALL 0x0E
Radek Krejcib1646a92018-11-02 16:08:26 +0100313
314/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100315 * @brief Cast XPath set to another type.
316 * Indirectly context position aware.
317 *
318 * @param[in] set Set to cast.
319 * @param[in] target Target type to cast \p set into.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200320 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +0100321 */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100322LY_ERR lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200323
Radek Krejcib1646a92018-11-02 16:08:26 +0100324/**
Michal Vaskoecd62de2019-11-13 12:35:11 +0100325 * @brief Insert schema node into set.
326 *
327 * @param[in] set Set to insert into.
328 * @param[in] node Node to insert.
329 * @param[in] node_type Node type of @p node.
330 * @return Index of the inserted node in set.
331 */
332int lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type);
333
334/**
335 * @brief Check for duplicates in a schema node set.
336 *
337 * @param[in] set Set to check.
338 * @param[in] node Node to look for in @p set.
339 * @param[in] node_type Type of @p node.
340 * @param[in] skip_idx Index from @p set to skip.
341 * @return Index of the found node, -1 if not found.
342 */
343int lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
344 int skip_idx);
345
346/**
347 * @brief Merge 2 schema node sets.
348 *
349 * @param[in] set1 Set to merge into.
350 * @param[in] set2 Set to merge. Its content is freed.
351 */
352void lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2);
353
354/**
Radek Krejcib1646a92018-11-02 16:08:26 +0100355 * @brief Parse an XPath expression into a structure of tokens.
356 * Logs directly.
357 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 * https://www.w3.org/TR/1999/REC-xpath-19991116/#exprlex
Radek Krejcib1646a92018-11-02 16:08:26 +0100359 *
360 * @param[in] ctx Context for errors.
361 * @param[in] expr XPath expression to parse. It is duplicated.
Radek Krejcib1646a92018-11-02 16:08:26 +0100362 * @return Filled expression structure or NULL on error.
363 */
364struct lyxp_expr *lyxp_expr_parse(struct ly_ctx *ctx, const char *expr);
365
366/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367 * @brief Frees a parsed XPath expression. @p expr should not be used afterwards.
Radek Krejcib1646a92018-11-02 16:08:26 +0100368 *
369 * @param[in] ctx libyang context of the expression.
370 * @param[in] expr Expression to free.
371 */
372void lyxp_expr_free(struct ly_ctx *ctx, struct lyxp_expr *expr);
373
374#endif /* _XPATH_H */