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