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