blob: 00970fa405ebf6ae42ad5d1ecf6d9683648c30ed [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
Radek Krejci5f9a3672021-03-05 21:35:22 +010022#include "common.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020023#include "dict.h"
24#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
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
Michal Vaskofbd037c2022-11-08 10:34:20 +0100133 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
Michal Vasko193dacd2022-10-13 08:43:05 +0200134 pnodes = (struct lysp_node *)parsed;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200135
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 Vasko193dacd2022-10-13 08:43:05 +0200147 }
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: {
Michal Vasko118eb522023-01-12 11:04:51 +0100180 uint16_t flags;
Michal Vasko193dacd2022-10-13 08:43:05 +0200181
182 if (!(ctx->compile_opts & LYS_COMPILE_NO_CONFIG)) {
Michal Vasko118eb522023-01-12 11:04:51 +0100183 memcpy(&flags, &parsed, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200184 if (flags & LYS_CONFIG_MASK) {
185 /* explicitly set */
Michal Vasko118eb522023-01-12 11:04:51 +0100186 flags |= LYS_SET_CONFIG;
Michal Vasko193dacd2022-10-13 08:43:05 +0200187 } else if (ext->parent_stmt & LY_STMT_DATA_NODE_MASK) {
188 /* inherit */
Michal Vasko118eb522023-01-12 11:04:51 +0100189 flags = ((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_MASK;
Michal Vasko193dacd2022-10-13 08:43:05 +0200190 } else {
191 /* default config */
Michal Vasko118eb522023-01-12 11:04:51 +0100192 flags = LYS_CONFIG_W;
Michal Vasko193dacd2022-10-13 08:43:05 +0200193 }
Michal Vasko118eb522023-01-12 11:04:51 +0100194 memcpy(substmt->storage, &flags, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200195 } /* else leave zero */
196 break;
197 }
198 case LY_STMT_MUST: {
199 const struct lysp_restr *restrs = parsed;
Michal Vasko193dacd2022-10-13 08:43:05 +0200200
201 /* sized array */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200202 COMPILE_ARRAY_GOTO(ctx, restrs, *(struct lysc_must **)substmt->storage, lys_compile_must, rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200203 break;
204 }
205 case LY_STMT_WHEN: {
206 const uint16_t flags;
207 const struct lysp_when *when = parsed;
208
209 /* read compiled status */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100210 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
Michal Vasko193dacd2022-10-13 08:43:05 +0200211
212 /* compile */
213 LY_CHECK_GOTO(rc = lys_compile_when(ctx, when, flags, NULL, NULL, NULL, substmt->storage), cleanup);
214 break;
215 }
216 case LY_STMT_FRACTION_DIGITS:
217 case LY_STMT_REQUIRE_INSTANCE:
218 /* just make a copy */
Michal Vasko118eb522023-01-12 11:04:51 +0100219 memcpy(substmt->storage, &parsed, 1);
Michal Vasko193dacd2022-10-13 08:43:05 +0200220 break;
221
222 case LY_STMT_MANDATORY:
223 case LY_STMT_ORDERED_BY:
224 case LY_STMT_STATUS:
225 /* just make a copy */
Michal Vasko118eb522023-01-12 11:04:51 +0100226 memcpy(substmt->storage, &parsed, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200227 break;
228
229 case LY_STMT_MAX_ELEMENTS:
230 case LY_STMT_MIN_ELEMENTS:
231 /* just make a copy */
Michal Vasko118eb522023-01-12 11:04:51 +0100232 memcpy(substmt->storage, &parsed, 4);
Michal Vasko193dacd2022-10-13 08:43:05 +0200233 break;
234
235 case LY_STMT_POSITION:
236 case LY_STMT_VALUE:
237 /* just make a copy */
Michal Vasko118eb522023-01-12 11:04:51 +0100238 memcpy(substmt->storage, &parsed, 8);
Michal Vasko193dacd2022-10-13 08:43:05 +0200239 break;
240
241 case LY_STMT_IDENTITY:
242 /* compile */
243 LY_CHECK_GOTO(rc = lys_identity_precompile(ctx, NULL, NULL, parsed, substmt->storage), cleanup);
244 break;
245
246 case LY_STMT_LENGTH:
247 length_restr = 1;
248 /* fallthrough */
249 case LY_STMT_RANGE:
250 /* compile, use uint64 default range */
251 LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, parsed, LY_TYPE_UINT64, length_restr, 0, NULL, substmt->storage),
252 cleanup);
253 break;
254
255 case LY_STMT_PATTERN:
256 /* compile */
257 LY_CHECK_GOTO(rc = lys_compile_type_patterns(ctx, parsed, NULL, substmt->storage), cleanup);
258 break;
259
260 case LY_STMT_TYPE: {
261 const uint16_t flags;
262 const char *units;
263 const struct lysp_type *ptype = parsed;
264
265 /* read compiled info */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100266 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
267 lyplg_ext_get_storage(ext, LY_STMT_UNITS, sizeof units, (const void **)&units);
Michal Vasko193dacd2022-10-13 08:43:05 +0200268
269 /* compile */
270 LY_CHECK_GOTO(rc = lys_compile_type(ctx, NULL, flags, ext->def->name, ptype, substmt->storage, &units, NULL), cleanup);
271 break;
272 }
273 case LY_STMT_EXTENSION_INSTANCE: {
274 struct lysp_ext_instance *extps = (struct lysp_ext_instance *)parsed;
Michal Vasko193dacd2022-10-13 08:43:05 +0200275
276 /* compile sized array */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200277 COMPILE_EXTS_GOTO(ctx, extps, *(struct lysc_ext_instance **)substmt->storage, ext, rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200278 break;
279 }
280 case LY_STMT_AUGMENT:
281 case LY_STMT_GROUPING:
282 case LY_STMT_BASE:
283 case LY_STMT_BELONGS_TO:
284 case LY_STMT_DEFAULT:
285 case LY_STMT_DEVIATE:
286 case LY_STMT_DEVIATION:
287 case LY_STMT_EXTENSION:
288 case LY_STMT_FEATURE:
289 case LY_STMT_IF_FEATURE:
290 case LY_STMT_IMPORT:
291 case LY_STMT_INCLUDE:
292 case LY_STMT_MODULE:
293 case LY_STMT_PATH:
294 case LY_STMT_PREFIX:
295 case LY_STMT_REFINE:
296 case LY_STMT_REVISION:
297 case LY_STMT_REVISION_DATE:
298 case LY_STMT_SUBMODULE:
299 case LY_STMT_TYPEDEF:
300 case LY_STMT_UNIQUE:
301 case LY_STMT_YANG_VERSION:
302 case LY_STMT_YIN_ELEMENT:
303 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" compilation is not supported.", lyplg_ext_stmt2str(substmt->stmt));
304 rc = LY_EVALID;
305 goto cleanup;
306
307 default:
308 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension "
309 "(found in \"%s%s%s\") substatement.", lyplg_ext_stmt2str(substmt->stmt), ext->def->name,
310 ext->argument ? " " : "", ext->argument ? ext->argument : "");
311 rc = LY_EVALID;
312 goto cleanup;
313 }
314
315cleanup:
316 return rc;
317}
318
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200319LIBYANG_API_DEF LY_ERR
320lyplg_ext_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *extp,
321 struct lysc_ext_instance *ext)
Michal Vasko193dacd2022-10-13 08:43:05 +0200322{
323 LY_ERR rc = LY_SUCCESS;
324 LY_ARRAY_COUNT_TYPE u, v;
325 enum ly_stmt stmtp;
326 const void *storagep;
327 struct ly_set storagep_compiled = {0};
328
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200329 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, extp, ext, LY_EINVAL);
330
Michal Vasko193dacd2022-10-13 08:43:05 +0200331 /* note into the compile context that we are processing extension now */
332 ctx->ext = ext;
333
334 LY_ARRAY_FOR(extp->substmts, u) {
335 stmtp = extp->substmts[u].stmt;
336 storagep = *(void **)extp->substmts[u].storage;
337
338 if (ly_set_contains(&storagep_compiled, storagep, NULL)) {
339 /* this parsed statement has already been compiled (for example, if it is a linked list of parsed nodes) */
340 continue;
341 }
342
343 LY_ARRAY_FOR(ext->substmts, v) {
344 if (stmtp != ext->substmts[v].stmt) {
345 continue;
346 }
347
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200348 if ((rc = lys_compile_ext_instance_stmt(ctx, storagep, ext, &ext->substmts[v]))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200349 goto cleanup;
350 }
351
352 /* parsed substatement compiled */
353 break;
354 }
355
356 /* compiled */
357 ly_set_add(&storagep_compiled, storagep, 1, NULL);
358 }
359
360cleanup:
361 ctx->ext = NULL;
362 ly_set_erase(&storagep_compiled, NULL);
363 return rc;
364}
365
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100366LIBYANG_API_DEF struct ly_ctx *
Michal Vasko193dacd2022-10-13 08:43:05 +0200367lyplg_ext_compile_get_ctx(const struct lysc_ctx *ctx)
Radek Krejci5f9a3672021-03-05 21:35:22 +0100368{
369 return ctx->ctx;
370}
371
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100372LIBYANG_API_DEF uint32_t *
Michal Vasko193dacd2022-10-13 08:43:05 +0200373lyplg_ext_compile_get_options(const struct lysc_ctx *ctx)
Radek Krejci5f9a3672021-03-05 21:35:22 +0100374{
Michal Vasko7c565922021-06-10 14:58:27 +0200375 return &((struct lysc_ctx *)ctx)->compile_opts;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100376}
377
Michal Vasko6a914fb2022-01-17 10:36:14 +0100378LIBYANG_API_DEF const struct lys_module *
Michal Vasko193dacd2022-10-13 08:43:05 +0200379lyplg_ext_compile_get_cur_mod(const struct lysc_ctx *ctx)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100380{
381 return ctx->cur_mod;
382}
383
Michal Vasko6a914fb2022-01-17 10:36:14 +0100384LIBYANG_API_DEF struct lysp_module *
Michal Vasko193dacd2022-10-13 08:43:05 +0200385lyplg_ext_compile_get_pmod(const struct lysc_ctx *ctx)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100386{
387 return ctx->pmod;
388}
Michal Vaskoddd76592022-01-17 13:34:48 +0100389
Michal Vasko193dacd2022-10-13 08:43:05 +0200390LIBYANG_API_DEF struct ly_out **
391lyplg_ext_print_get_out(const struct lyspr_ctx *ctx)
392{
393 return &((struct lyspr_ctx *)ctx)->out;
394}
395
396LIBYANG_API_DEF uint32_t *
397lyplg_ext_print_get_options(const struct lyspr_ctx *ctx)
398{
399 return &((struct lyspr_ctx *)ctx)->options;
400}
401
402LIBYANG_API_DEF uint16_t *
403lyplg_ext_print_get_level(const struct lyspr_ctx *ctx)
404{
405 return &((struct lyspr_ctx *)ctx)->level;
406}
407
aPiecek03cb4872022-10-24 10:31:51 +0200408LIBYANG_API_DECL LY_ERR
409lyplg_ext_sprinter_ctree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_ext_instance *ext,
410 lyplg_ext_sprinter_ctree_override_clb clb)
411{
412 LY_ERR rc = LY_SUCCESS;
413 uint32_t i;
414 struct lysc_node *schema;
415
416 LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL);
417
418 LY_ARRAY_FOR(ext->substmts, i) {
419 switch (ext->substmts[i].stmt) {
420 case LY_STMT_NOTIFICATION:
421 case LY_STMT_INPUT:
422 case LY_STMT_OUTPUT:
423 case LY_STMT_ACTION:
424 case LY_STMT_RPC:
425 case LY_STMT_ANYDATA:
426 case LY_STMT_ANYXML:
427 case LY_STMT_CASE:
428 case LY_STMT_CHOICE:
429 case LY_STMT_CONTAINER:
430 case LY_STMT_LEAF:
431 case LY_STMT_LEAF_LIST:
432 case LY_STMT_LIST:
433 schema = *((struct lysc_node **)ext->substmts[i].storage);
434 if (schema) {
435 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, schema, clb);
436 return rc;
437 }
438 default:
439 break;
440 }
441 }
442
443 return rc;
444}
445
446LIBYANG_API_DECL LY_ERR
447lyplg_ext_sprinter_ptree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_ext_instance *ext,
448 lyplg_ext_sprinter_ptree_override_clb clb)
449{
450 LY_ERR rc = LY_SUCCESS;
451 uint32_t i;
452 struct lysp_node *schema;
453
454 LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL);
455
456 LY_ARRAY_FOR(ext->substmts, i) {
457 switch (ext->substmts[i].stmt) {
458 case LY_STMT_NOTIFICATION:
459 case LY_STMT_INPUT:
460 case LY_STMT_OUTPUT:
461 case LY_STMT_ACTION:
462 case LY_STMT_RPC:
463 case LY_STMT_ANYDATA:
464 case LY_STMT_ANYXML:
465 case LY_STMT_CASE:
466 case LY_STMT_CHOICE:
467 case LY_STMT_CONTAINER:
468 case LY_STMT_LEAF:
469 case LY_STMT_LEAF_LIST:
470 case LY_STMT_LIST:
471 schema = *((struct lysp_node **)ext->substmts[i].storage);
472 if (schema) {
473 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, schema, clb);
474 return rc;
475 }
476 default:
477 break;
478 }
479 }
480
481 return rc;
482}
483
484LIBYANG_API_DECL LY_ERR
485lyplg_ext_sprinter_ctree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_node *nodes,
486 lyplg_ext_sprinter_ctree_override_clb clb)
487{
488 struct lyspr_tree_schema *new;
489
490 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
491
492 if (!nodes) {
493 return LY_SUCCESS;
494 }
495
496 LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM);
497 new->compiled = 1;
498 new->ctree = nodes;
499 new->cn_overr = clb;
500
501 return LY_SUCCESS;
502}
503
504LIBYANG_API_DECL LY_ERR
505lyplg_ext_sprinter_ptree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_node *nodes,
506 lyplg_ext_sprinter_ptree_override_clb clb)
507{
508 struct lyspr_tree_schema *new;
509
510 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
511
512 if (!nodes) {
513 return LY_SUCCESS;
514 }
515
516 LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM);
517 new->compiled = 0;
518 new->ptree = nodes;
519 new->pn_overr = clb;
520
521 return LY_SUCCESS;
522}
523
524LIBYANG_API_DECL LY_ERR
525lyplg_ext_sprinter_tree_set_priv(const struct lyspr_tree_ctx *ctx, void *plugin_priv, void (*free_clb)(void *plugin_priv))
526{
527 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
528
529 ((struct lyspr_tree_ctx *)ctx)->plugin_priv = plugin_priv;
530 ((struct lyspr_tree_ctx *)ctx)->free_plugin_priv = free_clb;
531
532 return LY_SUCCESS;
533}
534
Michal Vasko193dacd2022-10-13 08:43:05 +0200535LIBYANG_API_DEF const char *
536lyplg_ext_stmt2str(enum ly_stmt stmt)
537{
538 if (stmt == LY_STMT_EXTENSION_INSTANCE) {
539 return "extension instance";
540 } else {
Michal Vaskob872d0f2022-12-08 09:36:54 +0100541 return lys_stmt_str(stmt);
Michal Vasko193dacd2022-10-13 08:43:05 +0200542 }
543}
544
545LIBYANG_API_DEF enum ly_stmt
546lyplg_ext_nodetype2stmt(uint16_t nodetype)
547{
548 switch (nodetype) {
549 case LYS_CONTAINER:
550 return LY_STMT_CONTAINER;
551 case LYS_CHOICE:
552 return LY_STMT_CHOICE;
553 case LYS_LEAF:
554 return LY_STMT_LEAF;
555 case LYS_LEAFLIST:
556 return LY_STMT_LEAF_LIST;
557 case LYS_LIST:
558 return LY_STMT_LIST;
559 case LYS_ANYXML:
560 return LY_STMT_ANYXML;
561 case LYS_ANYDATA:
562 return LY_STMT_ANYDATA;
563 case LYS_CASE:
564 return LY_STMT_CASE;
565 case LYS_RPC:
566 return LY_STMT_RPC;
567 case LYS_ACTION:
568 return LY_STMT_ACTION;
569 case LYS_NOTIF:
570 return LY_STMT_NOTIFICATION;
571 case LYS_USES:
572 return LY_STMT_USES;
573 case LYS_INPUT:
574 return LY_STMT_INPUT;
575 case LYS_OUTPUT:
576 return LY_STMT_OUTPUT;
577 default:
578 return LY_STMT_NONE;
579 }
580}
581
582LY_ERR
583lyplg_ext_get_storage_p(const struct lysc_ext_instance *ext, int stmt, const void ***storage_p)
584{
585 LY_ARRAY_COUNT_TYPE u;
586 enum ly_stmt match = 0;
587
588 *storage_p = NULL;
589
590 if (!(stmt & LY_STMT_NODE_MASK)) {
591 /* matching a non-node statement */
592 match = stmt;
593 }
594
595 LY_ARRAY_FOR(ext->substmts, u) {
596 if ((match && (ext->substmts[u].stmt == match)) || (!match && (ext->substmts[u].stmt & stmt))) {
597 *storage_p = ext->substmts[u].storage;
598 return LY_SUCCESS;
599 }
600 }
601
602 return LY_ENOT;
603}
604
605LIBYANG_API_DEF LY_ERR
Michal Vaskofbd037c2022-11-08 10:34:20 +0100606lyplg_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 +0200607{
Michal Vaskob9878502022-11-11 10:00:05 +0100608 LY_ERR rc = LY_SUCCESS;
Michal Vasko193dacd2022-10-13 08:43:05 +0200609 const void **s;
610
Michal Vasko250ffba2022-11-08 11:31:05 +0100611 /* get pointer to the storage, is set even on error */
Michal Vaskob9878502022-11-11 10:00:05 +0100612 rc = lyplg_ext_get_storage_p(ext, stmt, &s);
Michal Vasko193dacd2022-10-13 08:43:05 +0200613
Michal Vasko250ffba2022-11-08 11:31:05 +0100614 /* assign */
Michal Vaskob9878502022-11-11 10:00:05 +0100615 if (s) {
Michal Vaskob9878502022-11-11 10:00:05 +0100616 memcpy(storage, s, storage_size);
Michal Vaskob9878502022-11-11 10:00:05 +0100617 } else {
618 memset(storage, 0, storage_size);
619 }
620
621 return rc;
Michal Vasko193dacd2022-10-13 08:43:05 +0200622}
623
624LIBYANG_API_DEF LY_ERR
Michal Vaskofbd037c2022-11-08 10:34:20 +0100625lyplg_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 +0200626{
627 LY_ARRAY_COUNT_TYPE u;
628 const struct lysp_ext_instance *extp = NULL;
629 enum ly_stmt match = 0;
Michal Vaskofbd037c2022-11-08 10:34:20 +0100630 const void **s = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200631
Michal Vasko193dacd2022-10-13 08:43:05 +0200632 /* find the parsed ext instance */
633 LY_ARRAY_FOR(ext->module->parsed->exts, u) {
634 extp = &ext->module->parsed->exts[u];
635
636 if (ext->def == extp->def->compiled) {
637 break;
638 }
639 extp = NULL;
640 }
641 assert(extp);
642
643 if (!(stmt & LY_STMT_NODE_MASK)) {
644 /* matching a non-node statement */
645 match = stmt;
646 }
647
648 /* get the substatement */
649 LY_ARRAY_FOR(extp->substmts, u) {
650 if ((match && (extp->substmts[u].stmt == match)) || (!match && (extp->substmts[u].stmt & stmt))) {
Michal Vaskofbd037c2022-11-08 10:34:20 +0100651 s = extp->substmts[u].storage;
652 break;
Michal Vasko193dacd2022-10-13 08:43:05 +0200653 }
654 }
655
Michal Vasko250ffba2022-11-08 11:31:05 +0100656 /* assign */
Michal Vaskob9878502022-11-11 10:00:05 +0100657 if (s) {
Michal Vaskob9878502022-11-11 10:00:05 +0100658 memcpy(storage, s, storage_size);
Michal Vaskob9878502022-11-11 10:00:05 +0100659 } else {
660 memset(storage, 0, storage_size);
661 }
662
663 return LY_SUCCESS;
Michal Vasko193dacd2022-10-13 08:43:05 +0200664}
665
Michal Vaskoddd76592022-01-17 13:34:48 +0100666LIBYANG_API_DEF LY_ERR
667lyplg_ext_get_data(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, void **ext_data, ly_bool *ext_data_free)
668{
Michal Vasko40a7a442022-12-09 13:01:38 +0100669 LY_ERR rc;
670
Michal Vaskoddd76592022-01-17 13:34:48 +0100671 if (!ctx->ext_clb) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200672 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 +0100673 return LY_EINVAL;
674 }
675
Michal Vasko40a7a442022-12-09 13:01:38 +0100676 if ((rc = ctx->ext_clb(ext, ctx->ext_clb_data, ext_data, ext_data_free))) {
677 lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Callback for getting ext data failed.");
678 }
679 return rc;
Michal Vaskoddd76592022-01-17 13:34:48 +0100680}