Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.h |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions header |
| 5 | * |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 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 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 15 | #ifndef LY_XPATH_H |
| 16 | #define LY_XPATH_H |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 17 | |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 18 | #include <stddef.h> |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 20 | |
Michal Vasko | c5a2283 | 2020-08-20 13:21:33 +0200 | [diff] [blame] | 21 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include "log.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 23 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include "tree_schema.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | |
| 26 | struct ly_ctx; |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 27 | struct lyd_node; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | /* |
| 30 | * XPath evaluator fully compliant with http://www.w3.org/TR/1999/REC-xpath-19991116/ |
| 31 | * except the following restrictions in the grammar. |
| 32 | * |
| 33 | * PARSED GRAMMAR |
| 34 | * |
| 35 | * Full axes are not supported, abbreviated forms must be used, |
| 36 | * variables are not supported, "id()" function is not supported, |
| 37 | * and processing instruction and comment nodes are not supported, |
| 38 | * which is also reflected in the grammar. Undefined rules and |
| 39 | * constants are tokens. |
| 40 | * |
| 41 | * Modified full grammar: |
| 42 | * |
| 43 | * [1] Expr ::= OrExpr // just an alias |
| 44 | * |
| 45 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 46 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 47 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 48 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 49 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 50 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 51 | * [8] NodeType ::= 'text' | 'node' |
| 52 | * [9] Predicate ::= '[' Expr ']' |
| 53 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 54 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 55 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 56 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 57 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 58 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 59 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 60 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 61 | * | EqualityExpr '!=' RelationalExpr |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 62 | * [16] RelationalExpr ::= AdditiveExpr |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 63 | * | RelationalExpr '<' AdditiveExpr |
| 64 | * | RelationalExpr '>' AdditiveExpr |
| 65 | * | RelationalExpr '<=' AdditiveExpr |
| 66 | * | RelationalExpr '>=' AdditiveExpr |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 67 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 68 | * | AdditiveExpr '+' MultiplicativeExpr |
| 69 | * | AdditiveExpr '-' MultiplicativeExpr |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 70 | * [18] MultiplicativeExpr ::= UnaryExpr |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 71 | * | MultiplicativeExpr '*' UnaryExpr |
| 72 | * | MultiplicativeExpr 'div' UnaryExpr |
| 73 | * | MultiplicativeExpr 'mod' UnaryExpr |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 74 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 75 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 76 | */ |
| 77 | |
| 78 | /* expression tokens allocation */ |
| 79 | #define LYXP_EXPR_SIZE_START 10 |
| 80 | #define LYXP_EXPR_SIZE_STEP 5 |
| 81 | |
| 82 | /* XPath matches allocation */ |
| 83 | #define LYXP_SET_SIZE_START 2 |
| 84 | #define LYXP_SET_SIZE_STEP 2 |
| 85 | |
| 86 | /* building string when casting */ |
| 87 | #define LYXP_STRING_CAST_SIZE_START 64 |
| 88 | #define LYXP_STRING_CAST_SIZE_STEP 16 |
| 89 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 90 | /* Maximum number of nested expressions. */ |
| 91 | #define LYXP_MAX_BLOCK_DEPTH 100 |
| 92 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 93 | /** |
| 94 | * @brief Tokens that can be in an XPath expression. |
| 95 | */ |
| 96 | enum lyxp_token { |
| 97 | LYXP_TOKEN_NONE = 0, |
| 98 | LYXP_TOKEN_PAR1, /* '(' */ |
| 99 | LYXP_TOKEN_PAR2, /* ')' */ |
| 100 | LYXP_TOKEN_BRACK1, /* '[' */ |
| 101 | LYXP_TOKEN_BRACK2, /* ']' */ |
| 102 | LYXP_TOKEN_DOT, /* '.' */ |
| 103 | LYXP_TOKEN_DDOT, /* '..' */ |
| 104 | LYXP_TOKEN_AT, /* '@' */ |
| 105 | LYXP_TOKEN_COMMA, /* ',' */ |
| 106 | /* LYXP_TOKEN_DCOLON, * '::' * axes not supported */ |
| 107 | LYXP_TOKEN_NAMETEST, /* NameTest */ |
| 108 | LYXP_TOKEN_NODETYPE, /* NodeType */ |
| 109 | LYXP_TOKEN_FUNCNAME, /* FunctionName */ |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | LYXP_TOKEN_OPER_LOG, /* Operator 'and', 'or' */ |
| 111 | LYXP_TOKEN_OPER_EQUAL, /* Operator '=' */ |
| 112 | LYXP_TOKEN_OPER_NEQUAL, /* Operator '!=' */ |
| 113 | LYXP_TOKEN_OPER_COMP, /* Operator '<', '<=', '>', '>=' */ |
| 114 | LYXP_TOKEN_OPER_MATH, /* Operator '+', '-', '*', 'div', 'mod', '-' (unary) */ |
| 115 | LYXP_TOKEN_OPER_UNI, /* Operator '|' */ |
| 116 | LYXP_TOKEN_OPER_PATH, /* Operator '/' */ |
| 117 | LYXP_TOKEN_OPER_RPATH, /* Operator '//' (recursive path) */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 118 | /* LYXP_TOKEN_AXISNAME, * AxisName * axes not supported */ |
| 119 | LYXP_TOKEN_LITERAL, /* Literal - with either single or double quote */ |
| 120 | LYXP_TOKEN_NUMBER /* Number */ |
| 121 | }; |
| 122 | |
| 123 | /** |
| 124 | * @brief XPath (sub)expressions that can be repeated. |
| 125 | */ |
| 126 | enum lyxp_expr_type { |
| 127 | LYXP_EXPR_NONE = 0, |
| 128 | LYXP_EXPR_OR, |
| 129 | LYXP_EXPR_AND, |
| 130 | LYXP_EXPR_EQUALITY, |
| 131 | LYXP_EXPR_RELATIONAL, |
| 132 | LYXP_EXPR_ADDITIVE, |
| 133 | LYXP_EXPR_MULTIPLICATIVE, |
| 134 | LYXP_EXPR_UNARY, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 135 | LYXP_EXPR_UNION |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 139 | * @brief Types of context nodes, #LYXP_NODE_ROOT_CONFIG used only in when or must conditions. |
| 140 | */ |
| 141 | enum lyxp_node_type { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 142 | LYXP_NODE_NONE, /* invalid node type */ |
| 143 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 144 | /* XML document roots */ |
| 145 | LYXP_NODE_ROOT, /* access to all the data (node value first top-level node) */ |
| 146 | LYXP_NODE_ROOT_CONFIG, /* <running> data context, no state data (node value first top-level node) */ |
| 147 | |
| 148 | /* XML elements */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 149 | LYXP_NODE_ELEM, /* YANG data element (most common) */ |
| 150 | LYXP_NODE_TEXT, /* YANG data text element (extremely specific use, unlikely to be ever needed) */ |
| 151 | LYXP_NODE_META /* YANG metadata (do not use for the context node) */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /** |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 155 | * @brief Structure holding a parsed XPath expression. |
| 156 | */ |
| 157 | struct lyxp_expr { |
| 158 | enum lyxp_token *tokens; /* array of tokens */ |
| 159 | uint16_t *tok_pos; /* array of the token offsets in expr */ |
| 160 | uint16_t *tok_len; /* array of token lengths in expr */ |
| 161 | enum lyxp_expr_type **repeat; /* array of expression types that this token begins and is repeated ended with 0, |
| 162 | more in the comment after this declaration */ |
| 163 | uint16_t used; /* used array items */ |
| 164 | uint16_t size; /* allocated array items */ |
| 165 | |
| 166 | const char *expr; /* the original XPath expression */ |
| 167 | }; |
| 168 | |
| 169 | /* |
| 170 | * lyxp_expr repeat |
| 171 | * |
| 172 | * This value is NULL for all the tokens that do not begin an |
| 173 | * expression which can be repeated. Otherwise it is an array |
| 174 | * of expression types that this token begins. These values |
| 175 | * are used during evaluation to know whether we need to |
| 176 | * duplicate the current context or not and to decide what |
| 177 | * the current expression is (for example, if we are only |
| 178 | * starting the parsing and the first token has no repeat, |
| 179 | * we do not parse it as an OrExpr but directly as PathExpr). |
| 180 | * Examples: |
| 181 | * |
| 182 | * Expression: "/ *[key1 and key2 or key1 < key2]" |
| 183 | * Tokens: '/', '*', '[', NameTest, 'and', NameTest, 'or', NameTest, '<', NameTest, ']' |
| 184 | * Repeat: NULL, NULL, NULL, [AndExpr, NULL, NULL, NULL, [RelationalExpr, NULL, NULL, NULL |
| 185 | * OrExpr, 0], |
| 186 | * 0], |
| 187 | * |
| 188 | * Expression: "//node[key and node2]/key | /cont" |
| 189 | * Tokens: '//', 'NameTest', '[', 'NameTest', 'and', 'NameTest', ']', '/', 'NameTest', '|', '/', 'NameTest' |
| 190 | * Repeat: [UnionExpr, NULL, NULL, [AndExpr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
| 191 | * 0], 0], |
| 192 | * |
| 193 | * Operators between expressions which this concerns: |
| 194 | * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|' |
| 195 | */ |
| 196 | |
| 197 | /** |
| 198 | * @brief Supported types of (partial) XPath results. |
| 199 | */ |
| 200 | enum lyxp_set_type { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 201 | LYXP_SET_NODE_SET = 0, |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 202 | LYXP_SET_SCNODE_SET, |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 203 | LYXP_SET_BOOLEAN, |
| 204 | LYXP_SET_NUMBER, |
| 205 | LYXP_SET_STRING |
| 206 | }; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 207 | |
| 208 | /** |
| 209 | * @brief Item stored in an XPath set hash table. |
| 210 | */ |
| 211 | struct lyxp_set_hash_node { |
| 212 | struct lyd_node *node; |
| 213 | enum lyxp_node_type type; |
| 214 | } _PACKED; |
| 215 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 216 | /** |
| 217 | * @brief XPath set - (partial) result. |
| 218 | */ |
| 219 | struct lyxp_set { |
| 220 | enum lyxp_set_type type; |
| 221 | union { |
| 222 | struct lyxp_set_node { |
| 223 | struct lyd_node *node; |
| 224 | enum lyxp_node_type type; |
| 225 | uint32_t pos; |
| 226 | } *nodes; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 227 | struct lyxp_set_scnode { |
| 228 | struct lysc_node *scnode; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 229 | enum lyxp_node_type type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 230 | |
Michal Vasko | d97959c | 2020-12-10 12:18:28 +0100 | [diff] [blame] | 231 | /* _START and _ATOM values should have grouped values */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 232 | #define LYXP_SET_SCNODE_START -2 /**< scnode not traversed, currently (the only node) in context */ |
| 233 | #define LYXP_SET_SCNODE_START_USED -1 /**< scnode not traversed except for the eval start, not currently in the context */ |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 234 | #define LYXP_SET_SCNODE_ATOM_NODE 0 /**< scnode was traversed, but not currently in the context */ |
| 235 | #define LYXP_SET_SCNODE_ATOM_VAL 1 /**< scnode was traversed and its value used, but not currently in the context */ |
| 236 | #define LYXP_SET_SCNODE_ATOM_CTX 2 /**< scnode currently in context */ |
| 237 | #define LYXP_SET_SCNODE_ATOM_NEW_CTX 3 /**< scnode in context and just added, so skip it for the current operation */ |
| 238 | #define LYXP_SET_SCNODE_ATOM_PRED_CTX 4 /**< includes any higher value - scnode is not in context because we are in |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 239 | a predicate and this scnode was used/will be used later */ |
| 240 | int32_t in_ctx; /**< values defined as LYXP_SET_SCNODE_* */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 241 | } *scnodes; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 242 | struct lyxp_set_meta { |
| 243 | struct lyd_meta *meta; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 244 | enum lyxp_node_type type; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 245 | uint32_t pos; /* if node_type is LYXP_SET_NODE_META, it is the parent node position */ |
| 246 | } *meta; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 247 | char *str; |
| 248 | long double num; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 249 | ly_bool bln; /* boolean */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 250 | } val; |
| 251 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 252 | /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SCNODE_SET */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 253 | uint32_t used; |
| 254 | uint32_t size; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 255 | struct hash_table *ht; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 256 | |
| 257 | /* XPath context information, this is valid only for type LYXP_SET_NODE_SET */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 258 | uint32_t ctx_pos; |
| 259 | uint32_t ctx_size; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 260 | |
| 261 | /* general context */ |
| 262 | struct ly_ctx *ctx; |
| 263 | union { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 264 | const struct lyd_node *cur_node; |
| 265 | const struct lysc_node *cur_scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 266 | }; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 267 | enum lyxp_node_type root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 268 | const struct lysc_node *context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 269 | const struct lyd_node *tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 270 | const struct lys_module *cur_mod; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 271 | LY_VALUE_FORMAT format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 272 | void *prefix_data; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | /** |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 276 | * @brief Print an XPath token \p tok type. |
| 277 | * |
| 278 | * @param[in] tok Token to print. |
| 279 | * @return Token type string. |
| 280 | */ |
| 281 | const char *lyxp_print_token(enum lyxp_token tok); |
| 282 | |
| 283 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 284 | * @brief Evaluate an XPath expression on data. Be careful when using this function, the result can often |
| 285 | * be confusing without thorough understanding of XPath evaluation rules defined in RFC 7950. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 286 | * |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 287 | * @param[in] ctx libyang context to use. |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 288 | * @param[in] exp Parsed XPath expression to be evaluated. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 289 | * @param[in] cur_mod Current module for the expression (where it was "instantiated"). |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 290 | * @param[in] format Format of the XPath expression (more specifcally, of any used prefixes). |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 291 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
| 292 | * @param[in] ctx_node Current (context) data node, NULL in case of the root node. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 293 | * @param[in] tree Data tree on which to perform the evaluation, it must include all the available data (including |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 294 | * the tree of @p ctx_node). Can be any node of the tree, it is adjusted. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 295 | * @param[out] set Result set. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 296 | * @param[in] options Whether to apply some evaluation restrictions. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 297 | * @return LY_EVALID for invalid argument types/count, |
| 298 | * @return LY_EINCOMPLETE for unresolved when, |
| 299 | * @return LY_EINVAL, LY_EMEM, LY_EINT for other errors. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 300 | */ |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 301 | LY_ERR lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 302 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree, |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 303 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 304 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 305 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 306 | * @brief Get all the partial XPath nodes (atoms) that are required for @p exp to be evaluated. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 307 | * |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 308 | * @param[in] ctx libyang context to use. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 309 | * @param[in] exp Parsed XPath expression to be evaluated. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 310 | * @param[in] cur_mod Current module for the expression (where it was "instantiated"). |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 311 | * @param[in] format Format of the XPath expression (more specifcally, of any used prefixes). |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 312 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
| 313 | * @param[in] ctx_scnode Current (context) schema node, NULL in case of the root node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 314 | * @param[out] set Result set. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 315 | * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 316 | * @return LY_ERR (same as ::lyxp_eval()). |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 317 | */ |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 318 | LY_ERR lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 319 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set, |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 320 | uint32_t options); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 321 | |
Radek Krejci | bed1394 | 2020-10-19 16:06:28 +0200 | [diff] [blame] | 322 | /* used only internally, maps with @ref findxpathoptions */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 323 | #define LYXP_IGNORE_WHEN 0x01 /**< Ignore unevaluated when in data nodes and do not return ::LY_EINCOMPLETE. */ |
| 324 | #define LYXP_SCHEMA 0x02 /**< Apply data node access restrictions defined for 'when' and 'must' evaluation. */ |
| 325 | #define LYXP_SCNODE 0x04 /**< No special tree access modifiers. */ |
| 326 | #define LYXP_SCNODE_SCHEMA LYS_FIND_XP_SCHEMA /**< Apply node access restrictions defined for 'when' and 'must' evaluation. */ |
| 327 | #define LYXP_SCNODE_OUTPUT LYS_FIND_XP_OUTPUT /**< Search RPC/action output nodes instead of input ones. */ |
| 328 | #define LYXP_SCNODE_ALL 0x1C /**< mask for all the LYXP_* values */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 329 | #define LYXP_SKIP_EXPR 0x20 /**< The rest of the expression will not be evaluated (lazy evaluation) */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 330 | |
| 331 | /** |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 332 | * @brief Cast XPath set to another type. |
| 333 | * Indirectly context position aware. |
| 334 | * |
| 335 | * @param[in] set Set to cast. |
| 336 | * @param[in] target Target type to cast \p set into. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 337 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 338 | */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 339 | LY_ERR lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 340 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 341 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 342 | * @brief Free dynamic content of a set. |
| 343 | * |
| 344 | * @param[in] set Set to modify. |
| 345 | */ |
| 346 | void lyxp_set_free_content(struct lyxp_set *set); |
| 347 | |
| 348 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 349 | * @brief Insert schema node into set. |
| 350 | * |
| 351 | * @param[in] set Set to insert into. |
| 352 | * @param[in] node Node to insert. |
| 353 | * @param[in] node_type Node type of @p node. |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 354 | * @param[out] index_p Optional pointer to store index if the inserted @p node. |
| 355 | * @return LY_SUCCESS on success. |
| 356 | * @return LY_EMEM on memory allocation failure. |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 357 | */ |
Michal Vasko | ee38a5d | 2020-11-09 21:02:18 +0100 | [diff] [blame] | 358 | LY_ERR lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 359 | uint32_t *index_p); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * @brief Check for duplicates in a schema node set. |
| 363 | * |
| 364 | * @param[in] set Set to check. |
| 365 | * @param[in] node Node to look for in @p set. |
| 366 | * @param[in] node_type Type of @p node. |
| 367 | * @param[in] skip_idx Index from @p set to skip. |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 368 | * @param[out] index_p Optional pointer to store index if the node is found. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 369 | * @return Boolean value whether the @p node found or not. |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 370 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 371 | ly_bool lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 372 | int skip_idx, uint32_t *index_p); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 373 | |
| 374 | /** |
| 375 | * @brief Merge 2 schema node sets. |
| 376 | * |
| 377 | * @param[in] set1 Set to merge into. |
| 378 | * @param[in] set2 Set to merge. Its content is freed. |
| 379 | */ |
| 380 | void lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2); |
| 381 | |
| 382 | /** |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 383 | * @brief Parse an XPath expression into a structure of tokens. |
| 384 | * Logs directly. |
| 385 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 386 | * https://www.w3.org/TR/1999/REC-xpath-19991116/#exprlex |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 387 | * |
| 388 | * @param[in] ctx Context for errors. |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 389 | * @param[in] expr_str XPath expression to parse. It is duplicated. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 390 | * @param[in] expr_len Length of @p expr, can be 0 if @p expr is 0-terminated. |
| 391 | * @param[in] reparse Whether to re-parse the expression to finalize full XPath parsing and fill |
| 392 | * information about expressions and their operators (fill repeat). |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 393 | * @param[out] expr_p Pointer to return the filled expression structure. |
| 394 | * @return LY_SUCCESS in case of success. |
| 395 | * @return LY_EMEM in case of memory allocation failure. |
| 396 | * @return LY_EVALID in case of invalid XPath expression in @p expr_str. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 397 | */ |
Michal Vasko | ee38a5d | 2020-11-09 21:02:18 +0100 | [diff] [blame] | 398 | LY_ERR lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, |
| 399 | struct lyxp_expr **expr_p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * @brief Duplicate parsed XPath expression. |
| 403 | * |
| 404 | * @param[in] ctx Context with a dictionary. |
| 405 | * @param[in] exp Parsed expression. |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 406 | * @param[out] dup Duplicated structure. |
| 407 | * @return LY_ERR value. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 408 | */ |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 409 | LY_ERR lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 410 | |
| 411 | /** |
| 412 | * @brief Look at the next token and check its kind. |
| 413 | * |
| 414 | * @param[in] ctx Context for logging, not logged if NULL. |
| 415 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 416 | * @param[in] tok_idx Token index in the expression \p exp. |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 417 | * @param[in] want_tok Expected token. |
| 418 | * @return LY_EINCOMPLETE on EOF, |
| 419 | * @return LY_ENOT on non-matching token, |
| 420 | * @return LY_SUCCESS on success. |
| 421 | */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 422 | LY_ERR lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok); |
| 423 | |
| 424 | /** |
| 425 | * @brief Look at the next token and skip it if it matches the expected one. |
| 426 | * |
| 427 | * @param[in] ctx Context for logging, not logged if NULL. |
| 428 | * @param[in] exp Expression to use. |
| 429 | * @param[in,out] tok_idx Token index in the expression \p exp, is updated. |
| 430 | * @param[in] want_tok Expected token. |
| 431 | * @return LY_EINCOMPLETE on EOF, |
| 432 | * @return LY_ENOT on non-matching token, |
| 433 | * @return LY_SUCCESS on success. |
| 434 | */ |
| 435 | LY_ERR lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 436 | |
| 437 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 438 | * @brief Frees a parsed XPath expression. @p expr should not be used afterwards. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 439 | * |
| 440 | * @param[in] ctx libyang context of the expression. |
| 441 | * @param[in] expr Expression to free. |
| 442 | */ |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 443 | void lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 444 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 445 | #endif /* LY_XPATH_H */ |