Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef _XPATH_H |
| 16 | #define _XPATH_H |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 20 | #include "libyang.h" |
Michal Vasko | fd76bd1 | 2015-09-24 15:49:57 +0200 | [diff] [blame] | 21 | #include "tree_schema.h" |
| 22 | #include "tree_data.h" |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 23 | |
| 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 Vasko | 9390977 | 2016-10-26 10:32:10 +0200 | [diff] [blame] | 31 | * 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 Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 35 | * |
| 36 | * Modified full grammar: |
| 37 | * |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 38 | * [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 Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 49 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 50 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 51 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 52 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 53 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 54 | * | EqualityExpr '!=' RelationalExpr |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 55 | * [14] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 56 | * | RelationalExpr '<' AdditiveExpr |
| 57 | * | RelationalExpr '>' AdditiveExpr |
| 58 | * | RelationalExpr '<=' AdditiveExpr |
| 59 | * | RelationalExpr '>=' AdditiveExpr |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 60 | * [15] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 61 | * | AdditiveExpr '+' MultiplicativeExpr |
| 62 | * | AdditiveExpr '-' MultiplicativeExpr |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 63 | * [16] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 64 | * | MultiplicativeExpr '*' UnaryExpr |
| 65 | * | MultiplicativeExpr 'div' UnaryExpr |
| 66 | * | MultiplicativeExpr 'mod' UnaryExpr |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 67 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 68 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 69 | */ |
| 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 | */ |
| 86 | enum 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 Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 111 | * @brief XPath (sub)expressions that can be repeated. |
| 112 | */ |
| 113 | enum 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 Vasko | 2c05f1b | 2017-11-16 13:36:09 +0100 | [diff] [blame] | 121 | LYXP_EXPR_UNARY, |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 122 | LYXP_EXPR_UNION, |
| 123 | }; |
| 124 | |
| 125 | /** |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 126 | * @brief Structure holding a parsed XPath expression. |
| 127 | */ |
| 128 | struct 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 Vasko | 7b70487 | 2018-02-15 16:09:43 +0100 | [diff] [blame] | 131 | uint16_t *tok_len; /* array of token lengths in expr */ |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 132 | 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 Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 134 | uint16_t used; /* used array items */ |
| 135 | uint16_t size; /* allocated array items */ |
| 136 | |
Michal Vasko | 2b84ea7 | 2016-05-05 17:56:58 +0200 | [diff] [blame] | 137 | char *expr; /* the original XPath expression */ |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 138 | }; |
| 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 Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 145 | * of expression types that this token begins. These values |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 146 | * are used during evaluation to know whether we need to |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 147 | * 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 Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 152 | * |
| 153 | * Expression: "/ *[key1 and key2 or key1 < key2]" |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 154 | * 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 Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 158 | * |
| 159 | * Expression: "//node[key and node2]/key | /cont" |
Michal Vasko | 764c8c4 | 2017-11-07 15:43:06 +0100 | [diff] [blame] | 160 | * 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 Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 163 | * |
Michal Vasko | 56d082c | 2016-10-25 14:00:42 +0200 | [diff] [blame] | 164 | * Operators between expressions which this concerns: |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 165 | * 'or', 'and', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', 'div', 'mod', '|' |
| 166 | */ |
| 167 | |
| 168 | /** |
| 169 | * @brief Supported types of (partial) XPath results. |
| 170 | */ |
| 171 | enum lyxp_set_type { |
| 172 | LYXP_SET_EMPTY = 0, |
| 173 | LYXP_SET_NODE_SET, |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 174 | LYXP_SET_SNODE_SET, |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 175 | LYXP_SET_BOOLEAN, |
| 176 | LYXP_SET_NUMBER, |
| 177 | LYXP_SET_STRING |
| 178 | }; |
| 179 | |
Michal Vasko | d048456 | 2018-04-26 14:30:47 +0200 | [diff] [blame^] | 180 | #ifdef LY_ENABLED_CACHE |
| 181 | |
| 182 | /** |
| 183 | * @brief Item stored in an XPath set hash table. |
| 184 | */ |
| 185 | struct lyxp_set_hash_node { |
| 186 | struct lyd_node *node; |
| 187 | enum lyxp_node_type type; |
| 188 | }; |
| 189 | |
| 190 | #endif |
| 191 | |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 192 | /** |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 193 | * @brief XPath set - (partial) result. |
| 194 | */ |
| 195 | struct lyxp_set { |
| 196 | enum lyxp_set_type type; |
| 197 | union { |
Michal Vasko | 1130645 | 2018-04-25 16:11:13 +0200 | [diff] [blame] | 198 | struct lyxp_set_node { |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 199 | struct lyd_node *node; |
| 200 | enum lyxp_node_type type; |
| 201 | uint32_t pos; |
| 202 | } *nodes; |
Michal Vasko | 1130645 | 2018-04-25 16:11:13 +0200 | [diff] [blame] | 203 | struct lyxp_set_snode { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 204 | struct lys_node *snode; |
| 205 | enum lyxp_node_type type; |
| 206 | /* 0 - snode was traversed, but not currently in the context, |
| 207 | * 1 - snode currently in context, |
| 208 | * 2 - snode in context and just added, so skip it for the current operation, |
| 209 | * >=3 - snode is not in context because we are in a predicate and this snode was used/will be used later */ |
| 210 | uint32_t in_ctx; |
| 211 | } *snodes; |
Michal Vasko | 1130645 | 2018-04-25 16:11:13 +0200 | [diff] [blame] | 212 | struct lyxp_set_attr { |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 213 | struct lyd_attr *attr; |
| 214 | enum lyxp_node_type type; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 215 | uint32_t pos; /* if node_type is LYXP_SET_NODE_ATTR, it is the parent node position */ |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 216 | } *attrs; |
Michal Vasko | 40afe1a | 2016-08-22 14:20:43 +0200 | [diff] [blame] | 217 | char *str; |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 218 | long double num; |
| 219 | int bool; |
| 220 | } val; |
| 221 | |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 222 | /* this is valid only for type LYXP_SET_NODE_SET and LYXP_SET_SNODE_SET */ |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 223 | uint32_t used; |
| 224 | uint32_t size; |
Michal Vasko | d048456 | 2018-04-26 14:30:47 +0200 | [diff] [blame^] | 225 | #ifdef LY_ENABLED_CACHE |
| 226 | struct hash_table *ht; |
| 227 | #endif |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 228 | /* this is valid only for type LYXP_SET_NODE_SET */ |
Michal Vasko | db86ff4 | 2016-05-13 15:50:11 +0200 | [diff] [blame] | 229 | uint32_t ctx_pos; |
| 230 | uint32_t ctx_size; |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 231 | }; |
| 232 | |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 233 | /** |
Michal Vasko | 6ed199c | 2016-02-04 12:07:19 +0100 | [diff] [blame] | 234 | * @brief Evaluate the XPath expression \p expr on data. Be careful when using this function, the result can often |
| 235 | * be confusing without thorough understanding of XPath evaluation rules defined in RFC 6020. |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 236 | * |
Michal Vasko | 30646e6 | 2015-10-09 14:02:09 +0200 | [diff] [blame] | 237 | * @param[in] expr XPath expression to evaluate. Must be in JSON format (prefixes are model names). |
Michal Vasko | fdb73ae | 2016-08-24 16:02:12 +0200 | [diff] [blame] | 238 | * @param[in] cur_node Current (context) data node. If the node has #LYD_VAL_INUSE flag, it is considered dummy (intended |
| 239 | * for but not restricted to evaluation with the LYXP_WHEN flag). |
Michal Vasko | b5157df | 2016-08-23 13:19:41 +0200 | [diff] [blame] | 240 | * @param[in] cur_node_type Current (context) data node type. For every standard case use #LYXP_NODE_ELEM. But there are |
| 241 | * 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 Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 242 | * this case just pass the first top-level node into \p cur_node and use an enum value for this kind of root |
| 243 | * (#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 Vasko | b5157df | 2016-08-23 13:19:41 +0200 | [diff] [blame] | 244 | * but there are no use-cases in YANG. |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 245 | * @param[in] local_mod Local module relative to the \p expr. Used only to determine the internal canonical value for identities. |
Michal Vasko | 488590f | 2016-03-29 12:23:25 +0200 | [diff] [blame] | 246 | * @param[out] set Result set. Must be valid and in the same libyang context as \p cur_node. |
| 247 | * To be safe, always either zero or cast the \p set to empty. After done using, either cast |
| 248 | * the \p set to empty (if allocated statically) or free it (if allocated dynamically) to |
| 249 | * prevent memory leaks. |
Michal Vasko | a87d258 | 2016-03-16 15:37:45 +0100 | [diff] [blame] | 250 | * @param[in] options Whether to apply some evaluation restrictions. |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 251 | * LYXP_MUST - apply must data tree access restrictions. |
| 252 | * LYXP_WHEN - apply when data tree access restrictions and consider LYD_WHEN flags in data nodes. |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 253 | * |
Michal Vasko | a87d258 | 2016-03-16 15:37:45 +0100 | [diff] [blame] | 254 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on unresolved when dependency, -1 on error. |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 255 | */ |
Michal Vasko | a59495d | 2016-08-22 09:18:58 +0200 | [diff] [blame] | 256 | int lyxp_eval(const char *expr, const struct lyd_node *cur_node, enum lyxp_node_type cur_node_type, |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 257 | const struct lys_module *local_mod, struct lyxp_set *set, int options); |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 258 | |
Michal Vasko | 5fb299e | 2015-10-06 15:44:55 +0200 | [diff] [blame] | 259 | /** |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 260 | * @brief Get all the partial XPath nodes (atoms) that are required for \p expr to be evaluated. |
| 261 | * |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 262 | * If any LYXP_SNODE* options is set, only fatal errors are printed, otherwise they are downgraded |
| 263 | * to warnings. |
| 264 | * |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 265 | * @param[in] expr XPath expression to be evaluated. Must be in JSON format (prefixes are model names). |
| 266 | * @param[in] cur_snode Current (context) schema node. |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 267 | * @param[in] cur_snode_type Current (context) schema node type. |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 268 | * @param[out] set Result set. Must be valid and in the same libyang context as \p cur_snode. |
| 269 | * To be safe, always either zero or cast the \p set to empty. After done using, either cast |
| 270 | * the \p set to empty (if allocated statically) or free it (if allocated dynamically) to |
| 271 | * prevent memory leaks. |
| 272 | * @param[in] options Whether to apply some evaluation restrictions, one flag must always be used. |
| 273 | * LYXP_SNODE - no special data tree access modifiers. |
| 274 | * LYXP_SNODE_MUST - apply must data tree access restrictions. |
| 275 | * LYXP_SNODE_WHEN - apply when data tree access restrictions. |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 276 | * LYXP_SNODE_OUTPUT - search RPC/action output instead input |
Michal Vasko | 41cb7c3 | 2017-07-03 12:59:56 +0200 | [diff] [blame] | 277 | * @param[out] ctx_snode Actual context node for the expression (it often changes for "when" expressions). |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 278 | * |
| 279 | * @return EXIT_SUCCESS on success, -1 on error. |
| 280 | */ |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 281 | int lyxp_atomize(const char *expr, const struct lys_node *cur_snode, enum lyxp_node_type cur_snode_type, |
Michal Vasko | 41cb7c3 | 2017-07-03 12:59:56 +0200 | [diff] [blame] | 282 | struct lyxp_set *set, int options, const struct lys_node **ctx_snode); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 283 | |
| 284 | /* these are used only internally */ |
| 285 | #define LYXP_SNODE 0x04 |
| 286 | #define LYXP_SNODE_MUST 0x08 |
| 287 | #define LYXP_SNODE_WHEN 0x10 |
Michal Vasko | db1da03 | 2016-09-08 10:07:38 +0200 | [diff] [blame] | 288 | #define LYXP_SNODE_OUTPUT 0x20 |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 289 | |
| 290 | #define LYXP_SNODE_ALL 0x1C |
| 291 | |
| 292 | /** |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 293 | * @brief Works like lyxp_atomize(), but it is executed on all the when and must expressions |
| 294 | * which the node has. |
Michal Vasko | 7e18c45 | 2015-10-07 09:34:36 +0200 | [diff] [blame] | 295 | * |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 296 | * @param[in] node Node to examine. |
| 297 | * @param[in,out] set Resulting set of atoms merged from all the expressions. |
| 298 | * Will be cleared before use. |
Michal Vasko | 0b96311 | 2017-08-11 12:45:36 +0200 | [diff] [blame] | 299 | * @param[in] set_ext_dep_flags Set #LYS_XPATH_DEP for conditions that require foreign subtree and |
| 300 | * also for the node itself, if it has any such condition. |
Michal Vasko | 7e18c45 | 2015-10-07 09:34:36 +0200 | [diff] [blame] | 301 | * |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 302 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 7e18c45 | 2015-10-07 09:34:36 +0200 | [diff] [blame] | 303 | */ |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 304 | int lyxp_node_atomize(const struct lys_node *node, struct lyxp_set *set, int set_ext_dep_flags); |
Michal Vasko | 7e18c45 | 2015-10-07 09:34:36 +0200 | [diff] [blame] | 305 | |
| 306 | /** |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 307 | * @brief Check syntax of all the XPath expressions of the node. |
| 308 | * |
| 309 | * @param[in] node Node to examine. |
| 310 | * |
| 311 | * @return EXIT_SUCCESS on success, -1 on error. |
| 312 | */ |
| 313 | int lyxp_node_check_syntax(const struct lys_node *node); |
| 314 | |
| 315 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 316 | * @brief Cast XPath set to another type. |
| 317 | * Indirectly context position aware. |
| 318 | * |
| 319 | * @param[in] set Set to cast. |
| 320 | * @param[in] target Target type to cast \p set into. |
Michal Vasko | 488590f | 2016-03-29 12:23:25 +0200 | [diff] [blame] | 321 | * @param[in] cur_node Current (context) data node. Cannot be NULL. |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 322 | * @param[in] local_mod Local expression module. |
Michal Vasko | a87d258 | 2016-03-16 15:37:45 +0100 | [diff] [blame] | 323 | * @param[in] options Whether to apply some evaluation restrictions. |
Michal Vasko | fdb73ae | 2016-08-24 16:02:12 +0200 | [diff] [blame] | 324 | * |
| 325 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 326 | */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 327 | int lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target, const struct lyd_node *cur_node, |
| 328 | const struct lys_module *local_mod, int options); |
Michal Vasko | 0485489 | 2015-10-06 15:45:43 +0200 | [diff] [blame] | 329 | |
| 330 | /** |
Michal Vasko | 5fb299e | 2015-10-06 15:44:55 +0200 | [diff] [blame] | 331 | * @brief Free contents of an XPath \p set. |
| 332 | * |
| 333 | * @param[in] set Set to free. |
Michal Vasko | 5fb299e | 2015-10-06 15:44:55 +0200 | [diff] [blame] | 334 | */ |
Michal Vasko | 40afe1a | 2016-08-22 14:20:43 +0200 | [diff] [blame] | 335 | void lyxp_set_free(struct lyxp_set *set); |
Michal Vasko | 5fb299e | 2015-10-06 15:44:55 +0200 | [diff] [blame] | 336 | |
Michal Vasko | 56d082c | 2016-10-25 14:00:42 +0200 | [diff] [blame] | 337 | /** |
| 338 | * @brief Parse an XPath expression into a structure of tokens. |
| 339 | * Logs directly. |
| 340 | * |
| 341 | * http://www.w3.org/TR/1999/REC-xpath-19991116/ section 3.7 |
| 342 | * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 343 | * @param[in] ctx Context for errors. |
Michal Vasko | 56d082c | 2016-10-25 14:00:42 +0200 | [diff] [blame] | 344 | * @param[in] expr XPath expression to parse. It is duplicated. |
| 345 | * |
| 346 | * @return Filled expression structure or NULL on error. |
| 347 | */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 348 | struct lyxp_expr *lyxp_parse_expr(struct ly_ctx *ctx, const char *expr); |
Michal Vasko | 56d082c | 2016-10-25 14:00:42 +0200 | [diff] [blame] | 349 | |
| 350 | /** |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 351 | * @brief Frees a parsed XPath expression. \p expr should not be used afterwards. |
| 352 | * |
| 353 | * @param[in] expr Expression to free. |
| 354 | */ |
| 355 | void lyxp_expr_free(struct lyxp_expr *expr); |
Michal Vasko | 56d082c | 2016-10-25 14:00:42 +0200 | [diff] [blame] | 356 | |
Michal Vasko | 2589505 | 2015-09-21 11:41:12 +0200 | [diff] [blame] | 357 | #endif /* _XPATH_H */ |