blob: 6797c151301a7fe98bd933f091640a97d7d54e67 [file] [log] [blame]
Radek Krejci0935f412019-08-20 16:15:18 +02001/**
2 * @file plugins_exts.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoddd76592022-01-17 13:34:48 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief helper functions for extension plugins
Radek Krejci0935f412019-08-20 16:15:18 +02006 *
Michal Vaskoddd76592022-01-17 13:34:48 +01007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejci0935f412019-08-20 16:15:18 +02008 *
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 Krejci0935f412019-08-20 16:15:18 +020015
16#include "plugins_exts.h"
17
Michal Vasko193dacd2022-10-13 08:43:05 +020018#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
Michal Vasko193dacd2022-10-13 08:43:05 +020020#include <stdlib.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021
Michal Vasko193dacd2022-10-13 08:43:05 +020022#include "dict.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010023#include "ly_common.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020024#include "parser_internal.h"
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010025#include "printer_internal.h"
Radek Krejci5f9a3672021-03-05 21:35:22 +010026#include "schema_compile.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020027#include "schema_compile_amend.h"
28#include "schema_compile_node.h"
29#include "schema_features.h"
30#include "tree_schema_internal.h"
31
32LIBYANG_API_DEF const struct lysp_module *
33lyplg_ext_parse_get_cur_pmod(const struct lysp_ctx *pctx)
34{
35 return PARSER_CUR_PMOD(pctx);
36}
37
38LIBYANG_API_DEF LY_ERR
39lyplg_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
76cleanup:
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 Vasko193dacd2022-10-13 08:43:05 +020087 * @return LY_ERR value.
88 */
89static LY_ERR
90lys_compile_ext_instance_stmt(struct lysc_ctx *ctx, const void *parsed, struct lysc_ext_instance *ext,
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020091 struct lysc_ext_substmt *substmt)
Michal Vasko193dacd2022-10-13 08:43:05 +020092{
93 LY_ERR rc = LY_SUCCESS;
94 ly_bool length_restr = 0;
95 LY_DATA_TYPE basetype;
96
Michal Vaskod595eff2023-02-20 11:29:28 +010097 assert(parsed);
98
Michal Vasko193dacd2022-10-13 08:43:05 +020099 /* compilation wthout any storage */
100 if (substmt->stmt == LY_STMT_IF_FEATURE) {
101 ly_bool enabled;
102
103 /* evaluate */
104 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, parsed, &enabled), cleanup);
105 if (!enabled) {
106 /* it is disabled, remove the whole extension instance */
107 rc = LY_ENOT;
108 }
109 }
110
Michal Vaskod595eff2023-02-20 11:29:28 +0100111 if (!substmt->storage) {
112 /* nothing to store */
Michal Vasko193dacd2022-10-13 08:43:05 +0200113 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 Vaskofbd037c2022-11-08 10:34:20 +0100135 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
Michal Vasko193dacd2022-10-13 08:43:05 +0200136 pnodes = (struct lysp_node *)parsed;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200137
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 Vasko193dacd2022-10-13 08:43:05 +0200149 }
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 */
166 LY_CHECK_GOTO(rc = lydict_insert(ctx->ctx, parsed, 0, substmt->storage), cleanup);
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 */
178 LY_CHECK_GOTO(rc = lys_compile_type_enums(ctx, parsed, basetype, NULL, substmt->storage), cleanup);
179 break;
180
181 case LY_STMT_CONFIG: {
Michal Vasko118eb522023-01-12 11:04:51 +0100182 uint16_t flags;
Michal Vasko193dacd2022-10-13 08:43:05 +0200183
184 if (!(ctx->compile_opts & LYS_COMPILE_NO_CONFIG)) {
Michal Vasko118eb522023-01-12 11:04:51 +0100185 memcpy(&flags, &parsed, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200186 if (flags & LYS_CONFIG_MASK) {
187 /* explicitly set */
Michal Vasko118eb522023-01-12 11:04:51 +0100188 flags |= LYS_SET_CONFIG;
Michal Vasko193dacd2022-10-13 08:43:05 +0200189 } else if (ext->parent_stmt & LY_STMT_DATA_NODE_MASK) {
190 /* inherit */
Michal Vasko118eb522023-01-12 11:04:51 +0100191 flags = ((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_MASK;
Michal Vasko193dacd2022-10-13 08:43:05 +0200192 } else {
193 /* default config */
Michal Vasko118eb522023-01-12 11:04:51 +0100194 flags = LYS_CONFIG_W;
Michal Vasko193dacd2022-10-13 08:43:05 +0200195 }
Michal Vasko118eb522023-01-12 11:04:51 +0100196 memcpy(substmt->storage, &flags, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200197 } /* else leave zero */
198 break;
199 }
200 case LY_STMT_MUST: {
201 const struct lysp_restr *restrs = parsed;
Michal Vasko193dacd2022-10-13 08:43:05 +0200202
203 /* sized array */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200204 COMPILE_ARRAY_GOTO(ctx, restrs, *(struct lysc_must **)substmt->storage, lys_compile_must, rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200205 break;
206 }
207 case LY_STMT_WHEN: {
208 const uint16_t flags;
209 const struct lysp_when *when = parsed;
210
211 /* read compiled status */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100212 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
Michal Vasko193dacd2022-10-13 08:43:05 +0200213
214 /* compile */
215 LY_CHECK_GOTO(rc = lys_compile_when(ctx, when, flags, NULL, NULL, NULL, substmt->storage), cleanup);
216 break;
217 }
218 case LY_STMT_FRACTION_DIGITS:
219 case LY_STMT_REQUIRE_INSTANCE:
220 /* just make a copy */
Michal Vasko118eb522023-01-12 11:04:51 +0100221 memcpy(substmt->storage, &parsed, 1);
Michal Vasko193dacd2022-10-13 08:43:05 +0200222 break;
223
224 case LY_STMT_MANDATORY:
225 case LY_STMT_ORDERED_BY:
226 case LY_STMT_STATUS:
227 /* just make a copy */
Michal Vasko118eb522023-01-12 11:04:51 +0100228 memcpy(substmt->storage, &parsed, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200229 break;
230
231 case LY_STMT_MAX_ELEMENTS:
232 case LY_STMT_MIN_ELEMENTS:
233 /* just make a copy */
Michal Vasko118eb522023-01-12 11:04:51 +0100234 memcpy(substmt->storage, &parsed, 4);
Michal Vasko193dacd2022-10-13 08:43:05 +0200235 break;
236
237 case LY_STMT_POSITION:
238 case LY_STMT_VALUE:
239 /* just make a copy */
Michal Vasko118eb522023-01-12 11:04:51 +0100240 memcpy(substmt->storage, &parsed, 8);
Michal Vasko193dacd2022-10-13 08:43:05 +0200241 break;
242
243 case LY_STMT_IDENTITY:
244 /* compile */
245 LY_CHECK_GOTO(rc = lys_identity_precompile(ctx, NULL, NULL, parsed, substmt->storage), cleanup);
246 break;
247
248 case LY_STMT_LENGTH:
249 length_restr = 1;
250 /* fallthrough */
251 case LY_STMT_RANGE:
252 /* compile, use uint64 default range */
253 LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, parsed, LY_TYPE_UINT64, length_restr, 0, NULL, substmt->storage),
254 cleanup);
255 break;
256
257 case LY_STMT_PATTERN:
258 /* compile */
259 LY_CHECK_GOTO(rc = lys_compile_type_patterns(ctx, parsed, NULL, substmt->storage), cleanup);
260 break;
261
262 case LY_STMT_TYPE: {
263 const uint16_t flags;
264 const char *units;
265 const struct lysp_type *ptype = parsed;
266
267 /* read compiled info */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100268 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
269 lyplg_ext_get_storage(ext, LY_STMT_UNITS, sizeof units, (const void **)&units);
Michal Vasko193dacd2022-10-13 08:43:05 +0200270
271 /* compile */
272 LY_CHECK_GOTO(rc = lys_compile_type(ctx, NULL, flags, ext->def->name, ptype, substmt->storage, &units, NULL), cleanup);
Michal Vaskod595eff2023-02-20 11:29:28 +0100273 LY_ATOMIC_INC_BARRIER((*(struct lysc_type **)substmt->storage)->refcount);
Michal Vasko193dacd2022-10-13 08:43:05 +0200274 break;
275 }
276 case LY_STMT_EXTENSION_INSTANCE: {
277 struct lysp_ext_instance *extps = (struct lysp_ext_instance *)parsed;
Michal Vasko193dacd2022-10-13 08:43:05 +0200278
279 /* compile sized array */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200280 COMPILE_EXTS_GOTO(ctx, extps, *(struct lysc_ext_instance **)substmt->storage, ext, rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200281 break;
282 }
283 case LY_STMT_AUGMENT:
284 case LY_STMT_GROUPING:
285 case LY_STMT_BASE:
286 case LY_STMT_BELONGS_TO:
287 case LY_STMT_DEFAULT:
288 case LY_STMT_DEVIATE:
289 case LY_STMT_DEVIATION:
290 case LY_STMT_EXTENSION:
291 case LY_STMT_FEATURE:
292 case LY_STMT_IF_FEATURE:
293 case LY_STMT_IMPORT:
294 case LY_STMT_INCLUDE:
295 case LY_STMT_MODULE:
296 case LY_STMT_PATH:
297 case LY_STMT_PREFIX:
298 case LY_STMT_REFINE:
299 case LY_STMT_REVISION:
300 case LY_STMT_REVISION_DATE:
301 case LY_STMT_SUBMODULE:
302 case LY_STMT_TYPEDEF:
303 case LY_STMT_UNIQUE:
304 case LY_STMT_YANG_VERSION:
305 case LY_STMT_YIN_ELEMENT:
306 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" compilation is not supported.", lyplg_ext_stmt2str(substmt->stmt));
307 rc = LY_EVALID;
308 goto cleanup;
309
310 default:
311 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension "
312 "(found in \"%s%s%s\") substatement.", lyplg_ext_stmt2str(substmt->stmt), ext->def->name,
313 ext->argument ? " " : "", ext->argument ? ext->argument : "");
314 rc = LY_EVALID;
315 goto cleanup;
316 }
317
318cleanup:
319 return rc;
320}
321
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200322LIBYANG_API_DEF LY_ERR
323lyplg_ext_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *extp,
324 struct lysc_ext_instance *ext)
Michal Vasko193dacd2022-10-13 08:43:05 +0200325{
326 LY_ERR rc = LY_SUCCESS;
327 LY_ARRAY_COUNT_TYPE u, v;
328 enum ly_stmt stmtp;
329 const void *storagep;
330 struct ly_set storagep_compiled = {0};
331
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200332 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, extp, ext, LY_EINVAL);
333
Michal Vasko193dacd2022-10-13 08:43:05 +0200334 /* note into the compile context that we are processing extension now */
335 ctx->ext = ext;
336
337 LY_ARRAY_FOR(extp->substmts, u) {
338 stmtp = extp->substmts[u].stmt;
339 storagep = *(void **)extp->substmts[u].storage;
340
Michal Vaskod595eff2023-02-20 11:29:28 +0100341 if (!storagep || ly_set_contains(&storagep_compiled, storagep, NULL)) {
342 /* nothing parsed or already compiled (for example, if it is a linked list of parsed nodes) */
Michal Vasko193dacd2022-10-13 08:43:05 +0200343 continue;
344 }
345
346 LY_ARRAY_FOR(ext->substmts, v) {
347 if (stmtp != ext->substmts[v].stmt) {
348 continue;
349 }
350
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200351 if ((rc = lys_compile_ext_instance_stmt(ctx, storagep, ext, &ext->substmts[v]))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200352 goto cleanup;
353 }
354
355 /* parsed substatement compiled */
356 break;
357 }
358
359 /* compiled */
360 ly_set_add(&storagep_compiled, storagep, 1, NULL);
361 }
362
363cleanup:
364 ctx->ext = NULL;
365 ly_set_erase(&storagep_compiled, NULL);
366 return rc;
367}
368
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100369LIBYANG_API_DEF struct ly_ctx *
Michal Vasko193dacd2022-10-13 08:43:05 +0200370lyplg_ext_compile_get_ctx(const struct lysc_ctx *ctx)
Radek Krejci5f9a3672021-03-05 21:35:22 +0100371{
372 return ctx->ctx;
373}
374
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100375LIBYANG_API_DEF uint32_t *
Michal Vasko193dacd2022-10-13 08:43:05 +0200376lyplg_ext_compile_get_options(const struct lysc_ctx *ctx)
Radek Krejci5f9a3672021-03-05 21:35:22 +0100377{
Michal Vasko7c565922021-06-10 14:58:27 +0200378 return &((struct lysc_ctx *)ctx)->compile_opts;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100379}
380
Michal Vasko6a914fb2022-01-17 10:36:14 +0100381LIBYANG_API_DEF const struct lys_module *
Michal Vasko193dacd2022-10-13 08:43:05 +0200382lyplg_ext_compile_get_cur_mod(const struct lysc_ctx *ctx)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100383{
384 return ctx->cur_mod;
385}
386
Michal Vasko6a914fb2022-01-17 10:36:14 +0100387LIBYANG_API_DEF struct lysp_module *
Michal Vasko193dacd2022-10-13 08:43:05 +0200388lyplg_ext_compile_get_pmod(const struct lysc_ctx *ctx)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100389{
390 return ctx->pmod;
391}
Michal Vaskoddd76592022-01-17 13:34:48 +0100392
Michal Vasko193dacd2022-10-13 08:43:05 +0200393LIBYANG_API_DEF struct ly_out **
394lyplg_ext_print_get_out(const struct lyspr_ctx *ctx)
395{
396 return &((struct lyspr_ctx *)ctx)->out;
397}
398
399LIBYANG_API_DEF uint32_t *
400lyplg_ext_print_get_options(const struct lyspr_ctx *ctx)
401{
402 return &((struct lyspr_ctx *)ctx)->options;
403}
404
405LIBYANG_API_DEF uint16_t *
406lyplg_ext_print_get_level(const struct lyspr_ctx *ctx)
407{
408 return &((struct lyspr_ctx *)ctx)->level;
409}
410
aPiecek03cb4872022-10-24 10:31:51 +0200411LIBYANG_API_DECL LY_ERR
412lyplg_ext_sprinter_ctree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_ext_instance *ext,
413 lyplg_ext_sprinter_ctree_override_clb clb)
414{
415 LY_ERR rc = LY_SUCCESS;
416 uint32_t i;
417 struct lysc_node *schema;
418
419 LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL);
420
421 LY_ARRAY_FOR(ext->substmts, i) {
422 switch (ext->substmts[i].stmt) {
423 case LY_STMT_NOTIFICATION:
424 case LY_STMT_INPUT:
425 case LY_STMT_OUTPUT:
426 case LY_STMT_ACTION:
427 case LY_STMT_RPC:
428 case LY_STMT_ANYDATA:
429 case LY_STMT_ANYXML:
430 case LY_STMT_CASE:
431 case LY_STMT_CHOICE:
432 case LY_STMT_CONTAINER:
433 case LY_STMT_LEAF:
434 case LY_STMT_LEAF_LIST:
435 case LY_STMT_LIST:
436 schema = *((struct lysc_node **)ext->substmts[i].storage);
437 if (schema) {
438 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, schema, clb);
439 return rc;
440 }
441 default:
442 break;
443 }
444 }
445
446 return rc;
447}
448
449LIBYANG_API_DECL LY_ERR
450lyplg_ext_sprinter_ptree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_ext_instance *ext,
451 lyplg_ext_sprinter_ptree_override_clb clb)
452{
453 LY_ERR rc = LY_SUCCESS;
454 uint32_t i;
455 struct lysp_node *schema;
456
457 LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL);
458
459 LY_ARRAY_FOR(ext->substmts, i) {
460 switch (ext->substmts[i].stmt) {
461 case LY_STMT_NOTIFICATION:
462 case LY_STMT_INPUT:
463 case LY_STMT_OUTPUT:
464 case LY_STMT_ACTION:
465 case LY_STMT_RPC:
466 case LY_STMT_ANYDATA:
467 case LY_STMT_ANYXML:
468 case LY_STMT_CASE:
469 case LY_STMT_CHOICE:
470 case LY_STMT_CONTAINER:
471 case LY_STMT_LEAF:
472 case LY_STMT_LEAF_LIST:
473 case LY_STMT_LIST:
474 schema = *((struct lysp_node **)ext->substmts[i].storage);
475 if (schema) {
476 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, schema, clb);
477 return rc;
478 }
479 default:
480 break;
481 }
482 }
483
484 return rc;
485}
486
487LIBYANG_API_DECL LY_ERR
488lyplg_ext_sprinter_ctree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_node *nodes,
489 lyplg_ext_sprinter_ctree_override_clb clb)
490{
491 struct lyspr_tree_schema *new;
492
493 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
494
495 if (!nodes) {
496 return LY_SUCCESS;
497 }
498
499 LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM);
500 new->compiled = 1;
501 new->ctree = nodes;
502 new->cn_overr = clb;
503
504 return LY_SUCCESS;
505}
506
507LIBYANG_API_DECL LY_ERR
508lyplg_ext_sprinter_ptree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_node *nodes,
509 lyplg_ext_sprinter_ptree_override_clb clb)
510{
511 struct lyspr_tree_schema *new;
512
513 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
514
515 if (!nodes) {
516 return LY_SUCCESS;
517 }
518
519 LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM);
520 new->compiled = 0;
521 new->ptree = nodes;
522 new->pn_overr = clb;
523
524 return LY_SUCCESS;
525}
526
527LIBYANG_API_DECL LY_ERR
528lyplg_ext_sprinter_tree_set_priv(const struct lyspr_tree_ctx *ctx, void *plugin_priv, void (*free_clb)(void *plugin_priv))
529{
530 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
531
532 ((struct lyspr_tree_ctx *)ctx)->plugin_priv = plugin_priv;
533 ((struct lyspr_tree_ctx *)ctx)->free_plugin_priv = free_clb;
534
535 return LY_SUCCESS;
536}
537
Michal Vasko193dacd2022-10-13 08:43:05 +0200538LIBYANG_API_DEF const char *
539lyplg_ext_stmt2str(enum ly_stmt stmt)
540{
541 if (stmt == LY_STMT_EXTENSION_INSTANCE) {
542 return "extension instance";
543 } else {
Michal Vaskob872d0f2022-12-08 09:36:54 +0100544 return lys_stmt_str(stmt);
Michal Vasko193dacd2022-10-13 08:43:05 +0200545 }
546}
547
548LIBYANG_API_DEF enum ly_stmt
549lyplg_ext_nodetype2stmt(uint16_t nodetype)
550{
551 switch (nodetype) {
552 case LYS_CONTAINER:
553 return LY_STMT_CONTAINER;
554 case LYS_CHOICE:
555 return LY_STMT_CHOICE;
556 case LYS_LEAF:
557 return LY_STMT_LEAF;
558 case LYS_LEAFLIST:
559 return LY_STMT_LEAF_LIST;
560 case LYS_LIST:
561 return LY_STMT_LIST;
562 case LYS_ANYXML:
563 return LY_STMT_ANYXML;
564 case LYS_ANYDATA:
565 return LY_STMT_ANYDATA;
566 case LYS_CASE:
567 return LY_STMT_CASE;
568 case LYS_RPC:
569 return LY_STMT_RPC;
570 case LYS_ACTION:
571 return LY_STMT_ACTION;
572 case LYS_NOTIF:
573 return LY_STMT_NOTIFICATION;
574 case LYS_USES:
575 return LY_STMT_USES;
576 case LYS_INPUT:
577 return LY_STMT_INPUT;
578 case LYS_OUTPUT:
579 return LY_STMT_OUTPUT;
580 default:
581 return LY_STMT_NONE;
582 }
583}
584
585LY_ERR
586lyplg_ext_get_storage_p(const struct lysc_ext_instance *ext, int stmt, const void ***storage_p)
587{
588 LY_ARRAY_COUNT_TYPE u;
589 enum ly_stmt match = 0;
590
591 *storage_p = NULL;
592
593 if (!(stmt & LY_STMT_NODE_MASK)) {
594 /* matching a non-node statement */
595 match = stmt;
596 }
597
598 LY_ARRAY_FOR(ext->substmts, u) {
599 if ((match && (ext->substmts[u].stmt == match)) || (!match && (ext->substmts[u].stmt & stmt))) {
600 *storage_p = ext->substmts[u].storage;
601 return LY_SUCCESS;
602 }
603 }
604
605 return LY_ENOT;
606}
607
608LIBYANG_API_DEF LY_ERR
Michal Vaskofbd037c2022-11-08 10:34:20 +0100609lyplg_ext_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, const void **storage)
Michal Vasko193dacd2022-10-13 08:43:05 +0200610{
Michal Vaskob9878502022-11-11 10:00:05 +0100611 LY_ERR rc = LY_SUCCESS;
Michal Vasko193dacd2022-10-13 08:43:05 +0200612 const void **s;
613
Michal Vasko250ffba2022-11-08 11:31:05 +0100614 /* get pointer to the storage, is set even on error */
Michal Vaskob9878502022-11-11 10:00:05 +0100615 rc = lyplg_ext_get_storage_p(ext, stmt, &s);
Michal Vasko193dacd2022-10-13 08:43:05 +0200616
Michal Vasko250ffba2022-11-08 11:31:05 +0100617 /* assign */
Michal Vaskob9878502022-11-11 10:00:05 +0100618 if (s) {
Michal Vaskob9878502022-11-11 10:00:05 +0100619 memcpy(storage, s, storage_size);
Michal Vaskob9878502022-11-11 10:00:05 +0100620 } else {
621 memset(storage, 0, storage_size);
622 }
623
624 return rc;
Michal Vasko193dacd2022-10-13 08:43:05 +0200625}
626
627LIBYANG_API_DEF LY_ERR
Michal Vaskofbd037c2022-11-08 10:34:20 +0100628lyplg_ext_parsed_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, const void **storage)
Michal Vasko193dacd2022-10-13 08:43:05 +0200629{
630 LY_ARRAY_COUNT_TYPE u;
631 const struct lysp_ext_instance *extp = NULL;
632 enum ly_stmt match = 0;
Michal Vaskofbd037c2022-11-08 10:34:20 +0100633 const void **s = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200634
Michal Vasko193dacd2022-10-13 08:43:05 +0200635 /* find the parsed ext instance */
636 LY_ARRAY_FOR(ext->module->parsed->exts, u) {
637 extp = &ext->module->parsed->exts[u];
638
639 if (ext->def == extp->def->compiled) {
640 break;
641 }
642 extp = NULL;
643 }
644 assert(extp);
645
646 if (!(stmt & LY_STMT_NODE_MASK)) {
647 /* matching a non-node statement */
648 match = stmt;
649 }
650
651 /* get the substatement */
652 LY_ARRAY_FOR(extp->substmts, u) {
653 if ((match && (extp->substmts[u].stmt == match)) || (!match && (extp->substmts[u].stmt & stmt))) {
Michal Vaskofbd037c2022-11-08 10:34:20 +0100654 s = extp->substmts[u].storage;
655 break;
Michal Vasko193dacd2022-10-13 08:43:05 +0200656 }
657 }
658
Michal Vasko250ffba2022-11-08 11:31:05 +0100659 /* assign */
Michal Vaskob9878502022-11-11 10:00:05 +0100660 if (s) {
Michal Vaskob9878502022-11-11 10:00:05 +0100661 memcpy(storage, s, storage_size);
Michal Vaskob9878502022-11-11 10:00:05 +0100662 } else {
663 memset(storage, 0, storage_size);
664 }
665
666 return LY_SUCCESS;
Michal Vasko193dacd2022-10-13 08:43:05 +0200667}
668
Michal Vaskoddd76592022-01-17 13:34:48 +0100669LIBYANG_API_DEF LY_ERR
670lyplg_ext_get_data(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, void **ext_data, ly_bool *ext_data_free)
671{
Michal Vasko40a7a442022-12-09 13:01:38 +0100672 LY_ERR rc;
673
Michal Vaskoddd76592022-01-17 13:34:48 +0100674 if (!ctx->ext_clb) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200675 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Failed to get extension data, no callback set.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100676 return LY_EINVAL;
677 }
678
Michal Vasko40a7a442022-12-09 13:01:38 +0100679 if ((rc = ctx->ext_clb(ext, ctx->ext_clb_data, ext_data, ext_data_free))) {
680 lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Callback for getting ext data failed.");
681 }
682 return rc;
Michal Vaskoddd76592022-01-17 13:34:48 +0100683}