Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file plugins_exts.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief helper functions for extension plugins |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 7 | * Copyright (c) 2019 - 2022 CESNET, z.s.p.o. |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 15 | |
| 16 | #include "plugins_exts.h" |
| 17 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 22 | #include "dict.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 23 | #include "ly_common.h" |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 24 | #include "parser_internal.h" |
Radek Krejci | f8d7f9a | 2021-03-10 14:32:36 +0100 | [diff] [blame] | 25 | #include "printer_internal.h" |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 26 | #include "schema_compile.h" |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 27 | #include "schema_compile_amend.h" |
| 28 | #include "schema_compile_node.h" |
| 29 | #include "schema_features.h" |
| 30 | #include "tree_schema_internal.h" |
| 31 | |
| 32 | LIBYANG_API_DEF const struct lysp_module * |
| 33 | lyplg_ext_parse_get_cur_pmod(const struct lysp_ctx *pctx) |
| 34 | { |
| 35 | return PARSER_CUR_PMOD(pctx); |
| 36 | } |
| 37 | |
| 38 | LIBYANG_API_DEF LY_ERR |
| 39 | lyplg_ext_parse_extension_instance(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) |
| 40 | { |
| 41 | LY_ERR rc = LY_SUCCESS; |
| 42 | LY_ARRAY_COUNT_TYPE u; |
| 43 | struct lysp_stmt *stmt; |
| 44 | |
| 45 | /* check for invalid substatements */ |
| 46 | LY_LIST_FOR(ext->child, stmt) { |
| 47 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 48 | continue; |
| 49 | } |
| 50 | LY_ARRAY_FOR(ext->substmts, u) { |
| 51 | if (ext->substmts[u].stmt == stmt->kw) { |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | if (u == LY_ARRAY_COUNT(ext->substmts)) { |
| 56 | LOGVAL(PARSER_CTX(pctx), LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 57 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 58 | rc = LY_EVALID; |
| 59 | goto cleanup; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* parse all the known statements */ |
| 64 | LY_ARRAY_FOR(ext->substmts, u) { |
| 65 | LY_LIST_FOR(ext->child, stmt) { |
| 66 | if (ext->substmts[u].stmt != stmt->kw) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if ((rc = lys_parse_ext_instance_stmt(pctx, &ext->substmts[u], stmt))) { |
| 71 | goto cleanup; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | cleanup: |
| 77 | return rc; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @brief Compile an instance extension statement. |
| 82 | * |
| 83 | * @param[in] ctx Compile context. |
| 84 | * @param[in] parsed Parsed ext instance substatement structure. |
| 85 | * @param[in] ext Compiled ext instance. |
| 86 | * @param[in] substmt Compled ext instance substatement info. |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 87 | * @return LY_ERR value. |
| 88 | */ |
| 89 | static LY_ERR |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 90 | lys_compile_ext_instance_stmt(struct lysc_ctx *ctx, uint64_t parsed, struct lysc_ext_instance *ext, |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 91 | struct lysc_ext_substmt *substmt) |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 92 | { |
| 93 | LY_ERR rc = LY_SUCCESS; |
| 94 | ly_bool length_restr = 0; |
| 95 | LY_DATA_TYPE basetype; |
| 96 | |
Michal Vasko | d595eff | 2023-02-20 11:29:28 +0100 | [diff] [blame] | 97 | assert(parsed); |
| 98 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 99 | /* compilation wthout any storage */ |
| 100 | if (substmt->stmt == LY_STMT_IF_FEATURE) { |
| 101 | ly_bool enabled; |
| 102 | |
| 103 | /* evaluate */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 104 | LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, (struct lysp_qname *)parsed, &enabled), cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 105 | if (!enabled) { |
| 106 | /* it is disabled, remove the whole extension instance */ |
| 107 | rc = LY_ENOT; |
| 108 | } |
| 109 | } |
| 110 | |
Michal Vasko | d595eff | 2023-02-20 11:29:28 +0100 | [diff] [blame] | 111 | if (!substmt->storage) { |
| 112 | /* nothing to store */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 113 | goto cleanup; |
| 114 | } |
| 115 | |
| 116 | switch (substmt->stmt) { |
| 117 | case LY_STMT_NOTIFICATION: |
| 118 | case LY_STMT_INPUT: |
| 119 | case LY_STMT_OUTPUT: |
| 120 | case LY_STMT_ACTION: |
| 121 | case LY_STMT_RPC: |
| 122 | case LY_STMT_ANYDATA: |
| 123 | case LY_STMT_ANYXML: |
| 124 | case LY_STMT_CASE: |
| 125 | case LY_STMT_CHOICE: |
| 126 | case LY_STMT_CONTAINER: |
| 127 | case LY_STMT_LEAF: |
| 128 | case LY_STMT_LEAF_LIST: |
| 129 | case LY_STMT_LIST: |
| 130 | case LY_STMT_USES: { |
| 131 | const uint16_t flags; |
| 132 | struct lysp_node *pnodes, *pnode; |
| 133 | struct lysc_node *node; |
| 134 | |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 135 | lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 136 | pnodes = (struct lysp_node *)parsed; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 137 | |
| 138 | /* compile nodes */ |
| 139 | LY_LIST_FOR(pnodes, pnode) { |
| 140 | if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 141 | /* manual compile */ |
| 142 | node = calloc(1, sizeof(struct lysc_node_action_inout)); |
| 143 | LY_CHECK_ERR_GOTO(!node, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup); |
| 144 | LY_CHECK_GOTO(rc = lys_compile_node_action_inout(ctx, pnode, node), cleanup); |
| 145 | LY_CHECK_GOTO(rc = lys_compile_node_connect(ctx, NULL, node), cleanup); |
| 146 | } else { |
| 147 | /* ctx->ext substatement storage is used as the document root */ |
| 148 | LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, NULL, flags, NULL), cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | break; |
| 152 | } |
| 153 | case LY_STMT_ARGUMENT: |
| 154 | case LY_STMT_CONTACT: |
| 155 | case LY_STMT_DESCRIPTION: |
| 156 | case LY_STMT_ERROR_APP_TAG: |
| 157 | case LY_STMT_ERROR_MESSAGE: |
| 158 | case LY_STMT_KEY: |
| 159 | case LY_STMT_MODIFIER: |
| 160 | case LY_STMT_NAMESPACE: |
| 161 | case LY_STMT_ORGANIZATION: |
| 162 | case LY_STMT_PRESENCE: |
| 163 | case LY_STMT_REFERENCE: |
| 164 | case LY_STMT_UNITS: |
| 165 | /* just make a copy */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 166 | LY_CHECK_GOTO(rc = lydict_insert(ctx->ctx, (const char *)parsed, 0, (const char **)substmt->storage), cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case LY_STMT_BIT: |
| 170 | basetype = LY_TYPE_BITS; |
| 171 | /* fallthrough */ |
| 172 | case LY_STMT_ENUM: |
| 173 | if (substmt->stmt == LY_STMT_ENUM) { |
| 174 | basetype = LY_TYPE_ENUM; |
| 175 | } |
| 176 | |
| 177 | /* compile */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 178 | rc = lys_compile_type_enums(ctx, (struct lysp_type_enum *)parsed, basetype, NULL, |
| 179 | (struct lysc_type_bitenum_item **)substmt->storage); |
| 180 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 181 | break; |
| 182 | |
| 183 | case LY_STMT_CONFIG: { |
Michal Vasko | 118eb52 | 2023-01-12 11:04:51 +0100 | [diff] [blame] | 184 | uint16_t flags; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 185 | |
| 186 | if (!(ctx->compile_opts & LYS_COMPILE_NO_CONFIG)) { |
Michal Vasko | 118eb52 | 2023-01-12 11:04:51 +0100 | [diff] [blame] | 187 | memcpy(&flags, &parsed, 2); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 188 | if (flags & LYS_CONFIG_MASK) { |
| 189 | /* explicitly set */ |
Michal Vasko | 118eb52 | 2023-01-12 11:04:51 +0100 | [diff] [blame] | 190 | flags |= LYS_SET_CONFIG; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 191 | } else if (ext->parent_stmt & LY_STMT_DATA_NODE_MASK) { |
| 192 | /* inherit */ |
Michal Vasko | 118eb52 | 2023-01-12 11:04:51 +0100 | [diff] [blame] | 193 | flags = ((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_MASK; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 194 | } else { |
| 195 | /* default config */ |
Michal Vasko | 118eb52 | 2023-01-12 11:04:51 +0100 | [diff] [blame] | 196 | flags = LYS_CONFIG_W; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 197 | } |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 198 | memcpy((void *)substmt->storage, &flags, 2); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 199 | } /* else leave zero */ |
| 200 | break; |
| 201 | } |
| 202 | case LY_STMT_MUST: { |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 203 | const struct lysp_restr *restrs = (struct lysp_restr *)parsed; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 204 | |
| 205 | /* sized array */ |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 206 | COMPILE_ARRAY_GOTO(ctx, restrs, *(struct lysc_must **)substmt->storage, lys_compile_must, rc, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 207 | break; |
| 208 | } |
| 209 | case LY_STMT_WHEN: { |
| 210 | const uint16_t flags; |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 211 | const struct lysp_when *when = (struct lysp_when *)parsed; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 212 | |
| 213 | /* read compiled status */ |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 214 | lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 215 | |
| 216 | /* compile */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 217 | LY_CHECK_GOTO(rc = lys_compile_when(ctx, when, flags, NULL, NULL, NULL, (struct lysc_when **)substmt->storage), cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | case LY_STMT_FRACTION_DIGITS: |
| 221 | case LY_STMT_REQUIRE_INSTANCE: |
| 222 | /* just make a copy */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 223 | memcpy((void *)substmt->storage, &parsed, 1); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 224 | break; |
| 225 | |
| 226 | case LY_STMT_MANDATORY: |
| 227 | case LY_STMT_ORDERED_BY: |
| 228 | case LY_STMT_STATUS: |
| 229 | /* just make a copy */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 230 | memcpy((void *)substmt->storage, &parsed, 2); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 231 | break; |
| 232 | |
| 233 | case LY_STMT_MAX_ELEMENTS: |
| 234 | case LY_STMT_MIN_ELEMENTS: |
| 235 | /* just make a copy */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 236 | memcpy((void *)substmt->storage, &parsed, 4); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 237 | break; |
| 238 | |
| 239 | case LY_STMT_POSITION: |
| 240 | case LY_STMT_VALUE: |
| 241 | /* just make a copy */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 242 | memcpy((void *)substmt->storage, &parsed, 8); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 243 | break; |
| 244 | |
| 245 | case LY_STMT_IDENTITY: |
| 246 | /* compile */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 247 | rc = lys_identity_precompile(ctx, NULL, NULL, (struct lysp_ident *)parsed, (struct lysc_ident **)substmt->storage); |
| 248 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 249 | break; |
| 250 | |
| 251 | case LY_STMT_LENGTH: |
| 252 | length_restr = 1; |
| 253 | /* fallthrough */ |
| 254 | case LY_STMT_RANGE: |
| 255 | /* compile, use uint64 default range */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 256 | rc = lys_compile_type_range(ctx, (struct lysp_restr *)parsed, LY_TYPE_UINT64, length_restr, 0, NULL, |
| 257 | (struct lysc_range **)substmt->storage); |
| 258 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 259 | break; |
| 260 | |
| 261 | case LY_STMT_PATTERN: |
| 262 | /* compile */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 263 | rc = lys_compile_type_patterns(ctx, (struct lysp_restr *)parsed, NULL, (struct lysc_pattern ***)substmt->storage); |
| 264 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 265 | break; |
| 266 | |
| 267 | case LY_STMT_TYPE: { |
| 268 | const uint16_t flags; |
| 269 | const char *units; |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 270 | const struct lysp_type *ptype = (struct lysp_type *)parsed; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 271 | |
| 272 | /* read compiled info */ |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 273 | lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags); |
| 274 | lyplg_ext_get_storage(ext, LY_STMT_UNITS, sizeof units, (const void **)&units); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 275 | |
| 276 | /* compile */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 277 | rc = lys_compile_type(ctx, NULL, flags, ext->def->name, ptype, (struct lysc_type **)substmt->storage, &units, NULL); |
| 278 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d595eff | 2023-02-20 11:29:28 +0100 | [diff] [blame] | 279 | LY_ATOMIC_INC_BARRIER((*(struct lysc_type **)substmt->storage)->refcount); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | case LY_STMT_EXTENSION_INSTANCE: { |
| 283 | struct lysp_ext_instance *extps = (struct lysp_ext_instance *)parsed; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 284 | |
| 285 | /* compile sized array */ |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 286 | COMPILE_EXTS_GOTO(ctx, extps, *(struct lysc_ext_instance **)substmt->storage, ext, rc, cleanup); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 287 | break; |
| 288 | } |
| 289 | case LY_STMT_AUGMENT: |
| 290 | case LY_STMT_GROUPING: |
| 291 | case LY_STMT_BASE: |
| 292 | case LY_STMT_BELONGS_TO: |
| 293 | case LY_STMT_DEFAULT: |
| 294 | case LY_STMT_DEVIATE: |
| 295 | case LY_STMT_DEVIATION: |
| 296 | case LY_STMT_EXTENSION: |
| 297 | case LY_STMT_FEATURE: |
| 298 | case LY_STMT_IF_FEATURE: |
| 299 | case LY_STMT_IMPORT: |
| 300 | case LY_STMT_INCLUDE: |
| 301 | case LY_STMT_MODULE: |
| 302 | case LY_STMT_PATH: |
| 303 | case LY_STMT_PREFIX: |
| 304 | case LY_STMT_REFINE: |
| 305 | case LY_STMT_REVISION: |
| 306 | case LY_STMT_REVISION_DATE: |
| 307 | case LY_STMT_SUBMODULE: |
| 308 | case LY_STMT_TYPEDEF: |
| 309 | case LY_STMT_UNIQUE: |
| 310 | case LY_STMT_YANG_VERSION: |
| 311 | case LY_STMT_YIN_ELEMENT: |
| 312 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" compilation is not supported.", lyplg_ext_stmt2str(substmt->stmt)); |
| 313 | rc = LY_EVALID; |
| 314 | goto cleanup; |
| 315 | |
| 316 | default: |
| 317 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension " |
| 318 | "(found in \"%s%s%s\") substatement.", lyplg_ext_stmt2str(substmt->stmt), ext->def->name, |
| 319 | ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 320 | rc = LY_EVALID; |
| 321 | goto cleanup; |
| 322 | } |
| 323 | |
| 324 | cleanup: |
| 325 | return rc; |
| 326 | } |
| 327 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 328 | LIBYANG_API_DEF LY_ERR |
| 329 | lyplg_ext_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *extp, |
| 330 | struct lysc_ext_instance *ext) |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 331 | { |
| 332 | LY_ERR rc = LY_SUCCESS; |
| 333 | LY_ARRAY_COUNT_TYPE u, v; |
| 334 | enum ly_stmt stmtp; |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 335 | uint64_t storagep; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 336 | struct ly_set storagep_compiled = {0}; |
| 337 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 338 | LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, extp, ext, LY_EINVAL); |
| 339 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 340 | /* note into the compile context that we are processing extension now */ |
| 341 | ctx->ext = ext; |
| 342 | |
| 343 | LY_ARRAY_FOR(extp->substmts, u) { |
| 344 | stmtp = extp->substmts[u].stmt; |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 345 | storagep = *(uint64_t *)extp->substmts[u].storage; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 346 | |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 347 | if (!storagep || ly_set_contains(&storagep_compiled, (void *)storagep, NULL)) { |
Michal Vasko | d595eff | 2023-02-20 11:29:28 +0100 | [diff] [blame] | 348 | /* nothing parsed or already compiled (for example, if it is a linked list of parsed nodes) */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 349 | continue; |
| 350 | } |
| 351 | |
| 352 | LY_ARRAY_FOR(ext->substmts, v) { |
| 353 | if (stmtp != ext->substmts[v].stmt) { |
| 354 | continue; |
| 355 | } |
| 356 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 357 | if ((rc = lys_compile_ext_instance_stmt(ctx, storagep, ext, &ext->substmts[v]))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 358 | goto cleanup; |
| 359 | } |
| 360 | |
| 361 | /* parsed substatement compiled */ |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | /* compiled */ |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 366 | ly_set_add(&storagep_compiled, (void *)storagep, 1, NULL); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | cleanup: |
| 370 | ctx->ext = NULL; |
| 371 | ly_set_erase(&storagep_compiled, NULL); |
| 372 | return rc; |
| 373 | } |
| 374 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 375 | LIBYANG_API_DEF struct ly_ctx * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 376 | lyplg_ext_compile_get_ctx(const struct lysc_ctx *ctx) |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 377 | { |
| 378 | return ctx->ctx; |
| 379 | } |
| 380 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 381 | LIBYANG_API_DEF uint32_t * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 382 | lyplg_ext_compile_get_options(const struct lysc_ctx *ctx) |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 383 | { |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 384 | return &((struct lysc_ctx *)ctx)->compile_opts; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 387 | LIBYANG_API_DEF const struct lys_module * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 388 | lyplg_ext_compile_get_cur_mod(const struct lysc_ctx *ctx) |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 389 | { |
| 390 | return ctx->cur_mod; |
| 391 | } |
| 392 | |
Michal Vasko | 6a914fb | 2022-01-17 10:36:14 +0100 | [diff] [blame] | 393 | LIBYANG_API_DEF struct lysp_module * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 394 | lyplg_ext_compile_get_pmod(const struct lysc_ctx *ctx) |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 395 | { |
| 396 | return ctx->pmod; |
| 397 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 398 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 399 | LIBYANG_API_DEF struct ly_out ** |
| 400 | lyplg_ext_print_get_out(const struct lyspr_ctx *ctx) |
| 401 | { |
| 402 | return &((struct lyspr_ctx *)ctx)->out; |
| 403 | } |
| 404 | |
| 405 | LIBYANG_API_DEF uint32_t * |
| 406 | lyplg_ext_print_get_options(const struct lyspr_ctx *ctx) |
| 407 | { |
| 408 | return &((struct lyspr_ctx *)ctx)->options; |
| 409 | } |
| 410 | |
| 411 | LIBYANG_API_DEF uint16_t * |
| 412 | lyplg_ext_print_get_level(const struct lyspr_ctx *ctx) |
| 413 | { |
| 414 | return &((struct lyspr_ctx *)ctx)->level; |
| 415 | } |
| 416 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 417 | LIBYANG_API_DECL LY_ERR |
| 418 | lyplg_ext_sprinter_ctree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_ext_instance *ext, |
| 419 | lyplg_ext_sprinter_ctree_override_clb clb) |
| 420 | { |
| 421 | LY_ERR rc = LY_SUCCESS; |
| 422 | uint32_t i; |
| 423 | struct lysc_node *schema; |
| 424 | |
| 425 | LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL); |
| 426 | |
| 427 | LY_ARRAY_FOR(ext->substmts, i) { |
| 428 | switch (ext->substmts[i].stmt) { |
| 429 | case LY_STMT_NOTIFICATION: |
| 430 | case LY_STMT_INPUT: |
| 431 | case LY_STMT_OUTPUT: |
| 432 | case LY_STMT_ACTION: |
| 433 | case LY_STMT_RPC: |
| 434 | case LY_STMT_ANYDATA: |
| 435 | case LY_STMT_ANYXML: |
| 436 | case LY_STMT_CASE: |
| 437 | case LY_STMT_CHOICE: |
| 438 | case LY_STMT_CONTAINER: |
| 439 | case LY_STMT_LEAF: |
| 440 | case LY_STMT_LEAF_LIST: |
| 441 | case LY_STMT_LIST: |
| 442 | schema = *((struct lysc_node **)ext->substmts[i].storage); |
| 443 | if (schema) { |
| 444 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, schema, clb); |
| 445 | return rc; |
| 446 | } |
| 447 | default: |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return rc; |
| 453 | } |
| 454 | |
| 455 | LIBYANG_API_DECL LY_ERR |
| 456 | lyplg_ext_sprinter_ptree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_ext_instance *ext, |
| 457 | lyplg_ext_sprinter_ptree_override_clb clb) |
| 458 | { |
| 459 | LY_ERR rc = LY_SUCCESS; |
| 460 | uint32_t i; |
| 461 | struct lysp_node *schema; |
| 462 | |
| 463 | LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL); |
| 464 | |
| 465 | LY_ARRAY_FOR(ext->substmts, i) { |
| 466 | switch (ext->substmts[i].stmt) { |
| 467 | case LY_STMT_NOTIFICATION: |
| 468 | case LY_STMT_INPUT: |
| 469 | case LY_STMT_OUTPUT: |
| 470 | case LY_STMT_ACTION: |
| 471 | case LY_STMT_RPC: |
| 472 | case LY_STMT_ANYDATA: |
| 473 | case LY_STMT_ANYXML: |
| 474 | case LY_STMT_CASE: |
| 475 | case LY_STMT_CHOICE: |
| 476 | case LY_STMT_CONTAINER: |
| 477 | case LY_STMT_LEAF: |
| 478 | case LY_STMT_LEAF_LIST: |
| 479 | case LY_STMT_LIST: |
| 480 | schema = *((struct lysp_node **)ext->substmts[i].storage); |
| 481 | if (schema) { |
| 482 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, schema, clb); |
| 483 | return rc; |
| 484 | } |
| 485 | default: |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | return rc; |
| 491 | } |
| 492 | |
| 493 | LIBYANG_API_DECL LY_ERR |
| 494 | lyplg_ext_sprinter_ctree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_node *nodes, |
| 495 | lyplg_ext_sprinter_ctree_override_clb clb) |
| 496 | { |
| 497 | struct lyspr_tree_schema *new; |
| 498 | |
| 499 | LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL); |
| 500 | |
| 501 | if (!nodes) { |
| 502 | return LY_SUCCESS; |
| 503 | } |
| 504 | |
| 505 | LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM); |
| 506 | new->compiled = 1; |
| 507 | new->ctree = nodes; |
| 508 | new->cn_overr = clb; |
| 509 | |
| 510 | return LY_SUCCESS; |
| 511 | } |
| 512 | |
| 513 | LIBYANG_API_DECL LY_ERR |
| 514 | lyplg_ext_sprinter_ptree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_node *nodes, |
| 515 | lyplg_ext_sprinter_ptree_override_clb clb) |
| 516 | { |
| 517 | struct lyspr_tree_schema *new; |
| 518 | |
| 519 | LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL); |
| 520 | |
| 521 | if (!nodes) { |
| 522 | return LY_SUCCESS; |
| 523 | } |
| 524 | |
| 525 | LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM); |
| 526 | new->compiled = 0; |
| 527 | new->ptree = nodes; |
| 528 | new->pn_overr = clb; |
| 529 | |
| 530 | return LY_SUCCESS; |
| 531 | } |
| 532 | |
| 533 | LIBYANG_API_DECL LY_ERR |
| 534 | lyplg_ext_sprinter_tree_set_priv(const struct lyspr_tree_ctx *ctx, void *plugin_priv, void (*free_clb)(void *plugin_priv)) |
| 535 | { |
| 536 | LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL); |
| 537 | |
| 538 | ((struct lyspr_tree_ctx *)ctx)->plugin_priv = plugin_priv; |
| 539 | ((struct lyspr_tree_ctx *)ctx)->free_plugin_priv = free_clb; |
| 540 | |
| 541 | return LY_SUCCESS; |
| 542 | } |
| 543 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 544 | LIBYANG_API_DEF const char * |
| 545 | lyplg_ext_stmt2str(enum ly_stmt stmt) |
| 546 | { |
| 547 | if (stmt == LY_STMT_EXTENSION_INSTANCE) { |
| 548 | return "extension instance"; |
| 549 | } else { |
Michal Vasko | b872d0f | 2022-12-08 09:36:54 +0100 | [diff] [blame] | 550 | return lys_stmt_str(stmt); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | LIBYANG_API_DEF enum ly_stmt |
| 555 | lyplg_ext_nodetype2stmt(uint16_t nodetype) |
| 556 | { |
| 557 | switch (nodetype) { |
| 558 | case LYS_CONTAINER: |
| 559 | return LY_STMT_CONTAINER; |
| 560 | case LYS_CHOICE: |
| 561 | return LY_STMT_CHOICE; |
| 562 | case LYS_LEAF: |
| 563 | return LY_STMT_LEAF; |
| 564 | case LYS_LEAFLIST: |
| 565 | return LY_STMT_LEAF_LIST; |
| 566 | case LYS_LIST: |
| 567 | return LY_STMT_LIST; |
| 568 | case LYS_ANYXML: |
| 569 | return LY_STMT_ANYXML; |
| 570 | case LYS_ANYDATA: |
| 571 | return LY_STMT_ANYDATA; |
| 572 | case LYS_CASE: |
| 573 | return LY_STMT_CASE; |
| 574 | case LYS_RPC: |
| 575 | return LY_STMT_RPC; |
| 576 | case LYS_ACTION: |
| 577 | return LY_STMT_ACTION; |
| 578 | case LYS_NOTIF: |
| 579 | return LY_STMT_NOTIFICATION; |
| 580 | case LYS_USES: |
| 581 | return LY_STMT_USES; |
| 582 | case LYS_INPUT: |
| 583 | return LY_STMT_INPUT; |
| 584 | case LYS_OUTPUT: |
| 585 | return LY_STMT_OUTPUT; |
| 586 | default: |
| 587 | return LY_STMT_NONE; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | LY_ERR |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 592 | lyplg_ext_get_storage_p(const struct lysc_ext_instance *ext, int stmt, uint64_t *storage_p) |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 593 | { |
| 594 | LY_ARRAY_COUNT_TYPE u; |
| 595 | enum ly_stmt match = 0; |
| 596 | |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 597 | *storage_p = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 598 | |
| 599 | if (!(stmt & LY_STMT_NODE_MASK)) { |
| 600 | /* matching a non-node statement */ |
| 601 | match = stmt; |
| 602 | } |
| 603 | |
| 604 | LY_ARRAY_FOR(ext->substmts, u) { |
| 605 | if ((match && (ext->substmts[u].stmt == match)) || (!match && (ext->substmts[u].stmt & stmt))) { |
| 606 | *storage_p = ext->substmts[u].storage; |
| 607 | return LY_SUCCESS; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return LY_ENOT; |
| 612 | } |
| 613 | |
| 614 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 615 | lyplg_ext_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, const void **storage) |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 616 | { |
Michal Vasko | b987850 | 2022-11-11 10:00:05 +0100 | [diff] [blame] | 617 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 618 | uint64_t s; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 619 | |
Michal Vasko | 250ffba | 2022-11-08 11:31:05 +0100 | [diff] [blame] | 620 | /* get pointer to the storage, is set even on error */ |
Michal Vasko | b987850 | 2022-11-11 10:00:05 +0100 | [diff] [blame] | 621 | rc = lyplg_ext_get_storage_p(ext, stmt, &s); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 622 | |
Michal Vasko | 250ffba | 2022-11-08 11:31:05 +0100 | [diff] [blame] | 623 | /* assign */ |
Michal Vasko | b987850 | 2022-11-11 10:00:05 +0100 | [diff] [blame] | 624 | if (s) { |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 625 | memcpy(storage, (void *)s, storage_size); |
Michal Vasko | b987850 | 2022-11-11 10:00:05 +0100 | [diff] [blame] | 626 | } else { |
| 627 | memset(storage, 0, storage_size); |
| 628 | } |
| 629 | |
| 630 | return rc; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 634 | lyplg_ext_parsed_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, const void **storage) |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 635 | { |
| 636 | LY_ARRAY_COUNT_TYPE u; |
| 637 | const struct lysp_ext_instance *extp = NULL; |
| 638 | enum ly_stmt match = 0; |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 639 | uint64_t s = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 640 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 641 | /* find the parsed ext instance */ |
| 642 | LY_ARRAY_FOR(ext->module->parsed->exts, u) { |
| 643 | extp = &ext->module->parsed->exts[u]; |
| 644 | |
| 645 | if (ext->def == extp->def->compiled) { |
| 646 | break; |
| 647 | } |
| 648 | extp = NULL; |
| 649 | } |
| 650 | assert(extp); |
| 651 | |
| 652 | if (!(stmt & LY_STMT_NODE_MASK)) { |
| 653 | /* matching a non-node statement */ |
| 654 | match = stmt; |
| 655 | } |
| 656 | |
| 657 | /* get the substatement */ |
| 658 | LY_ARRAY_FOR(extp->substmts, u) { |
| 659 | if ((match && (extp->substmts[u].stmt == match)) || (!match && (extp->substmts[u].stmt & stmt))) { |
Michal Vasko | fbd037c | 2022-11-08 10:34:20 +0100 | [diff] [blame] | 660 | s = extp->substmts[u].storage; |
| 661 | break; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Michal Vasko | 250ffba | 2022-11-08 11:31:05 +0100 | [diff] [blame] | 665 | /* assign */ |
Michal Vasko | b987850 | 2022-11-11 10:00:05 +0100 | [diff] [blame] | 666 | if (s) { |
Michal Vasko | 708a8bf | 2024-06-13 09:44:51 +0200 | [diff] [blame] | 667 | memcpy(storage, (void *)s, storage_size); |
Michal Vasko | b987850 | 2022-11-11 10:00:05 +0100 | [diff] [blame] | 668 | } else { |
| 669 | memset(storage, 0, storage_size); |
| 670 | } |
| 671 | |
| 672 | return LY_SUCCESS; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 675 | LIBYANG_API_DEF LY_ERR |
| 676 | lyplg_ext_get_data(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, void **ext_data, ly_bool *ext_data_free) |
| 677 | { |
Michal Vasko | 40a7a44 | 2022-12-09 13:01:38 +0100 | [diff] [blame] | 678 | LY_ERR rc; |
| 679 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 680 | if (!ctx->ext_clb) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 681 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Failed to get extension data, no callback set."); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 682 | return LY_EINVAL; |
| 683 | } |
| 684 | |
Michal Vasko | 40a7a44 | 2022-12-09 13:01:38 +0100 | [diff] [blame] | 685 | if ((rc = ctx->ext_clb(ext, ctx->ext_clb_data, ext_data, ext_data_free))) { |
| 686 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Callback for getting ext data failed."); |
| 687 | } |
| 688 | return rc; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 689 | } |