Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file structure.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 4 | * @brief libyang extension plugin - structure (RFC 8791) |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2022 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 15 | #include <assert.h> |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 20 | #include "compat.h" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 21 | #include "libyang.h" |
| 22 | #include "plugins_exts.h" |
| 23 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 24 | struct lysp_ext_instance_structure { |
| 25 | struct lysp_restr *musts; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 26 | uint16_t flags; |
| 27 | const char *dsc; |
| 28 | const char *ref; |
| 29 | struct lysp_tpdf *typedefs; |
| 30 | struct lysp_node_grp *groupings; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 31 | struct lysp_node *child; |
| 32 | }; |
| 33 | |
| 34 | struct lysc_ext_instance_structure { |
| 35 | struct lysc_must *musts; |
| 36 | uint16_t flags; |
| 37 | const char *dsc; |
| 38 | const char *ref; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 39 | struct lysc_node *child; |
| 40 | }; |
| 41 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 42 | struct lysp_ext_instance_augment_structure { |
| 43 | uint16_t flags; |
| 44 | const char *dsc; |
| 45 | const char *ref; |
| 46 | struct lysp_node *child; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 47 | struct lysp_node_augment *aug; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 50 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 51 | * @brief Parse structure extension instances. |
| 52 | * |
| 53 | * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. |
| 54 | */ |
| 55 | static LY_ERR |
| 56 | structure_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) |
| 57 | { |
| 58 | LY_ERR rc; |
| 59 | LY_ARRAY_COUNT_TYPE u; |
| 60 | struct lysp_module *pmod; |
| 61 | struct lysp_ext_instance_structure *struct_pdata; |
| 62 | |
| 63 | /* structure can appear only at the top level of a YANG module or submodule */ |
| 64 | if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { |
| 65 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, |
| 66 | "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name, |
| 67 | lyplg_ext_stmt2str(ext->parent_stmt)); |
| 68 | return LY_EVALID; |
| 69 | } |
| 70 | |
| 71 | pmod = ext->parent; |
| 72 | |
| 73 | /* check for duplication */ |
| 74 | LY_ARRAY_FOR(pmod->exts, u) { |
| 75 | if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) { |
| 76 | /* duplication of the same structure extension in a single module */ |
| 77 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name); |
| 78 | return LY_EVALID; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* allocate the storage */ |
| 83 | struct_pdata = calloc(1, sizeof *struct_pdata); |
| 84 | if (!struct_pdata) { |
| 85 | goto emem; |
| 86 | } |
| 87 | ext->parsed = struct_pdata; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 88 | LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 14, rc, emem); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 89 | |
| 90 | /* parse substatements */ |
| 91 | LY_ARRAY_INCREMENT(ext->substmts); |
| 92 | ext->substmts[0].stmt = LY_STMT_MUST; |
| 93 | ext->substmts[0].storage = &struct_pdata->musts; |
| 94 | |
| 95 | LY_ARRAY_INCREMENT(ext->substmts); |
| 96 | ext->substmts[1].stmt = LY_STMT_STATUS; |
| 97 | ext->substmts[1].storage = &struct_pdata->flags; |
| 98 | |
| 99 | LY_ARRAY_INCREMENT(ext->substmts); |
| 100 | ext->substmts[2].stmt = LY_STMT_DESCRIPTION; |
| 101 | ext->substmts[2].storage = &struct_pdata->dsc; |
| 102 | |
| 103 | LY_ARRAY_INCREMENT(ext->substmts); |
| 104 | ext->substmts[3].stmt = LY_STMT_REFERENCE; |
| 105 | ext->substmts[3].storage = &struct_pdata->ref; |
| 106 | |
| 107 | LY_ARRAY_INCREMENT(ext->substmts); |
| 108 | ext->substmts[4].stmt = LY_STMT_TYPEDEF; |
| 109 | ext->substmts[4].storage = &struct_pdata->typedefs; |
| 110 | |
| 111 | LY_ARRAY_INCREMENT(ext->substmts); |
| 112 | ext->substmts[5].stmt = LY_STMT_GROUPING; |
| 113 | ext->substmts[5].storage = &struct_pdata->groupings; |
| 114 | |
| 115 | /* data-def-stmt */ |
| 116 | LY_ARRAY_INCREMENT(ext->substmts); |
| 117 | ext->substmts[6].stmt = LY_STMT_CONTAINER; |
| 118 | ext->substmts[6].storage = &struct_pdata->child; |
| 119 | |
| 120 | LY_ARRAY_INCREMENT(ext->substmts); |
| 121 | ext->substmts[7].stmt = LY_STMT_LEAF; |
| 122 | ext->substmts[7].storage = &struct_pdata->child; |
| 123 | |
| 124 | LY_ARRAY_INCREMENT(ext->substmts); |
| 125 | ext->substmts[8].stmt = LY_STMT_LEAF_LIST; |
| 126 | ext->substmts[8].storage = &struct_pdata->child; |
| 127 | |
| 128 | LY_ARRAY_INCREMENT(ext->substmts); |
| 129 | ext->substmts[9].stmt = LY_STMT_LIST; |
| 130 | ext->substmts[9].storage = &struct_pdata->child; |
| 131 | |
| 132 | LY_ARRAY_INCREMENT(ext->substmts); |
| 133 | ext->substmts[10].stmt = LY_STMT_CHOICE; |
| 134 | ext->substmts[10].storage = &struct_pdata->child; |
| 135 | |
| 136 | LY_ARRAY_INCREMENT(ext->substmts); |
| 137 | ext->substmts[11].stmt = LY_STMT_ANYDATA; |
| 138 | ext->substmts[11].storage = &struct_pdata->child; |
| 139 | |
| 140 | LY_ARRAY_INCREMENT(ext->substmts); |
| 141 | ext->substmts[12].stmt = LY_STMT_ANYXML; |
| 142 | ext->substmts[12].storage = &struct_pdata->child; |
| 143 | |
| 144 | LY_ARRAY_INCREMENT(ext->substmts); |
| 145 | ext->substmts[13].stmt = LY_STMT_USES; |
| 146 | ext->substmts[13].storage = &struct_pdata->child; |
| 147 | |
| 148 | rc = lyplg_ext_parse_extension_instance(pctx, ext); |
| 149 | return rc; |
| 150 | |
| 151 | emem: |
| 152 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); |
| 153 | return LY_EMEM; |
| 154 | } |
| 155 | |
| 156 | /** |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 157 | * @brief Compile structure extension instances. |
| 158 | * |
| 159 | * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. |
| 160 | */ |
| 161 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 162 | structure_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext) |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 163 | { |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 164 | LY_ERR rc; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 165 | struct lysc_module *mod_c; |
| 166 | const struct lysc_node *child; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 167 | struct lysc_ext_instance_structure *struct_cdata; |
| 168 | uint32_t prev_options = *lyplg_ext_compile_get_options(cctx); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 169 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 170 | mod_c = ext->parent; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 171 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 172 | /* check identifier namespace with the compiled nodes */ |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 173 | LY_LIST_FOR(mod_c->data, child) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 174 | if (!strcmp(child->name, ext->argument)) { |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 175 | /* identifier collision */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 176 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, "Extension %s collides with a %s with the same identifier.", |
| 177 | extp->name, lys_nodetype2str(child->nodetype)); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 178 | return LY_EVALID; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* allocate the storage */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 183 | struct_cdata = calloc(1, sizeof *struct_cdata); |
| 184 | if (!struct_cdata) { |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 185 | goto emem; |
| 186 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 187 | ext->compiled = struct_cdata; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 188 | |
| 189 | /* compile substatements */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 190 | LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 14, rc, emem); |
| 191 | LY_ARRAY_INCREMENT(ext->substmts); |
| 192 | ext->substmts[0].stmt = LY_STMT_MUST; |
| 193 | ext->substmts[0].storage = &struct_cdata->musts; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 194 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 195 | LY_ARRAY_INCREMENT(ext->substmts); |
| 196 | ext->substmts[1].stmt = LY_STMT_STATUS; |
| 197 | ext->substmts[1].storage = &struct_cdata->flags; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 198 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 199 | LY_ARRAY_INCREMENT(ext->substmts); |
| 200 | ext->substmts[2].stmt = LY_STMT_DESCRIPTION; |
| 201 | ext->substmts[2].storage = &struct_cdata->dsc; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 202 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 203 | LY_ARRAY_INCREMENT(ext->substmts); |
| 204 | ext->substmts[3].stmt = LY_STMT_REFERENCE; |
| 205 | ext->substmts[3].storage = &struct_cdata->ref; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 206 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 207 | LY_ARRAY_INCREMENT(ext->substmts); |
| 208 | ext->substmts[4].stmt = LY_STMT_TYPEDEF; |
| 209 | ext->substmts[4].storage = NULL; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 210 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 211 | LY_ARRAY_INCREMENT(ext->substmts); |
| 212 | ext->substmts[5].stmt = LY_STMT_GROUPING; |
| 213 | ext->substmts[5].storage = NULL; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 214 | |
| 215 | /* data-def-stmt */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 216 | LY_ARRAY_INCREMENT(ext->substmts); |
| 217 | ext->substmts[6].stmt = LY_STMT_CONTAINER; |
| 218 | ext->substmts[6].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 219 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 220 | LY_ARRAY_INCREMENT(ext->substmts); |
| 221 | ext->substmts[7].stmt = LY_STMT_LEAF; |
| 222 | ext->substmts[7].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 223 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 224 | LY_ARRAY_INCREMENT(ext->substmts); |
| 225 | ext->substmts[8].stmt = LY_STMT_LEAF_LIST; |
| 226 | ext->substmts[8].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 227 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 228 | LY_ARRAY_INCREMENT(ext->substmts); |
| 229 | ext->substmts[9].stmt = LY_STMT_LIST; |
| 230 | ext->substmts[9].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 231 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 232 | LY_ARRAY_INCREMENT(ext->substmts); |
| 233 | ext->substmts[10].stmt = LY_STMT_CHOICE; |
| 234 | ext->substmts[10].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 235 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 236 | LY_ARRAY_INCREMENT(ext->substmts); |
| 237 | ext->substmts[11].stmt = LY_STMT_ANYDATA; |
| 238 | ext->substmts[11].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 239 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 240 | LY_ARRAY_INCREMENT(ext->substmts); |
| 241 | ext->substmts[12].stmt = LY_STMT_ANYXML; |
| 242 | ext->substmts[12].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 243 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 244 | LY_ARRAY_INCREMENT(ext->substmts); |
| 245 | ext->substmts[13].stmt = LY_STMT_USES; |
| 246 | ext->substmts[13].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 247 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 248 | *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED; |
| 249 | rc = lyplg_ext_compile_extension_instance(cctx, extp, ext); |
| 250 | *lyplg_ext_compile_get_options(cctx) = prev_options; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 251 | if (rc) { |
| 252 | return rc; |
| 253 | } |
| 254 | |
| 255 | return LY_SUCCESS; |
| 256 | |
| 257 | emem: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 258 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 259 | return LY_EMEM; |
| 260 | } |
| 261 | |
| 262 | /** |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 263 | * @brief Structure schema info printer. |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 264 | * |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 265 | * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 266 | */ |
| 267 | static LY_ERR |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 268 | structure_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag) |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 269 | { |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 270 | lyplg_ext_print_info_extension_instance(ctx, ext, flag); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 271 | return LY_SUCCESS; |
| 272 | } |
| 273 | |
| 274 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 275 | * @brief Free parsed structure extension instance data. |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 276 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 277 | * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree. |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 278 | */ |
| 279 | static void |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 280 | structure_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext) |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 281 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 282 | lyplg_ext_pfree_instance_substatements(ctx, ext->substmts); |
| 283 | free(ext->parsed); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @brief Free compiled structure extension instance data. |
| 288 | * |
| 289 | * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree. |
| 290 | */ |
| 291 | static void |
| 292 | structure_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
| 293 | { |
| 294 | lyplg_ext_cfree_instance_substatements(ctx, ext->substmts); |
| 295 | free(ext->compiled); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * @brief Parse augment-structure extension instances. |
| 300 | * |
| 301 | * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. |
| 302 | */ |
| 303 | static LY_ERR |
| 304 | structure_aug_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) |
| 305 | { |
| 306 | LY_ERR rc; |
| 307 | struct lysp_stmt *stmt; |
| 308 | struct lysp_ext_instance_augment_structure *aug_pdata; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 309 | const struct ly_ctx *ctx = lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 310 | |
| 311 | /* augment-structure can appear only at the top level of a YANG module or submodule */ |
| 312 | if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { |
| 313 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, |
| 314 | "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name, |
| 315 | lyplg_ext_stmt2str(ext->parent_stmt)); |
| 316 | return LY_EVALID; |
| 317 | } |
| 318 | |
| 319 | /* augment-structure must define some data-def-stmt */ |
| 320 | LY_LIST_FOR(ext->child, stmt) { |
| 321 | if (stmt->kw & LY_STMT_DATA_NODE_MASK) { |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | if (!stmt) { |
| 326 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s does not define any data-def-stmt statements.", |
| 327 | ext->name); |
| 328 | return LY_EVALID; |
| 329 | } |
| 330 | |
| 331 | /* allocate the storage */ |
| 332 | aug_pdata = calloc(1, sizeof *aug_pdata); |
| 333 | if (!aug_pdata) { |
| 334 | goto emem; |
| 335 | } |
| 336 | ext->parsed = aug_pdata; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 337 | LY_ARRAY_CREATE_GOTO(ctx, ext->substmts, 13, rc, emem); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 338 | |
| 339 | /* parse substatements */ |
| 340 | LY_ARRAY_INCREMENT(ext->substmts); |
| 341 | ext->substmts[0].stmt = LY_STMT_STATUS; |
| 342 | ext->substmts[0].storage = &aug_pdata->flags; |
| 343 | |
| 344 | LY_ARRAY_INCREMENT(ext->substmts); |
| 345 | ext->substmts[1].stmt = LY_STMT_DESCRIPTION; |
| 346 | ext->substmts[1].storage = &aug_pdata->dsc; |
| 347 | |
| 348 | LY_ARRAY_INCREMENT(ext->substmts); |
| 349 | ext->substmts[2].stmt = LY_STMT_REFERENCE; |
| 350 | ext->substmts[2].storage = &aug_pdata->ref; |
| 351 | |
| 352 | /* data-def-stmt */ |
| 353 | LY_ARRAY_INCREMENT(ext->substmts); |
| 354 | ext->substmts[3].stmt = LY_STMT_CONTAINER; |
| 355 | ext->substmts[3].storage = &aug_pdata->child; |
| 356 | |
| 357 | LY_ARRAY_INCREMENT(ext->substmts); |
| 358 | ext->substmts[4].stmt = LY_STMT_LEAF; |
| 359 | ext->substmts[4].storage = &aug_pdata->child; |
| 360 | |
| 361 | LY_ARRAY_INCREMENT(ext->substmts); |
| 362 | ext->substmts[5].stmt = LY_STMT_LEAF_LIST; |
| 363 | ext->substmts[5].storage = &aug_pdata->child; |
| 364 | |
| 365 | LY_ARRAY_INCREMENT(ext->substmts); |
| 366 | ext->substmts[6].stmt = LY_STMT_LIST; |
| 367 | ext->substmts[6].storage = &aug_pdata->child; |
| 368 | |
| 369 | LY_ARRAY_INCREMENT(ext->substmts); |
| 370 | ext->substmts[7].stmt = LY_STMT_CHOICE; |
| 371 | ext->substmts[7].storage = &aug_pdata->child; |
| 372 | |
| 373 | LY_ARRAY_INCREMENT(ext->substmts); |
| 374 | ext->substmts[8].stmt = LY_STMT_ANYDATA; |
| 375 | ext->substmts[8].storage = &aug_pdata->child; |
| 376 | |
| 377 | LY_ARRAY_INCREMENT(ext->substmts); |
| 378 | ext->substmts[9].stmt = LY_STMT_ANYXML; |
| 379 | ext->substmts[9].storage = &aug_pdata->child; |
| 380 | |
| 381 | LY_ARRAY_INCREMENT(ext->substmts); |
| 382 | ext->substmts[10].stmt = LY_STMT_USES; |
| 383 | ext->substmts[10].storage = &aug_pdata->child; |
| 384 | |
| 385 | /* case */ |
| 386 | LY_ARRAY_INCREMENT(ext->substmts); |
| 387 | ext->substmts[11].stmt = LY_STMT_CASE; |
| 388 | ext->substmts[11].storage = &aug_pdata->child; |
| 389 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 390 | if ((rc = lyplg_ext_parse_extension_instance(pctx, ext))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 391 | return rc; |
| 392 | } |
| 393 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 394 | /* add fake parsed augment node */ |
| 395 | LY_ARRAY_INCREMENT(ext->substmts); |
| 396 | ext->substmts[12].stmt = LY_STMT_AUGMENT; |
| 397 | ext->substmts[12].storage = &aug_pdata->aug; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 398 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 399 | aug_pdata->aug = calloc(1, sizeof *aug_pdata->aug); |
| 400 | if (!aug_pdata->aug) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 401 | goto emem; |
| 402 | } |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 403 | aug_pdata->aug->nodetype = LYS_AUGMENT; |
| 404 | aug_pdata->aug->flags = aug_pdata->flags; |
Michal Vasko | 5b3edcb | 2022-11-08 10:50:15 +0100 | [diff] [blame] | 405 | if (lydict_insert(ctx, ext->argument, 0, &aug_pdata->aug->nodeid)) { |
| 406 | goto emem; |
| 407 | } |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 408 | aug_pdata->aug->child = aug_pdata->child; |
| 409 | /* avoid double free */ |
| 410 | aug_pdata->child = NULL; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 411 | |
| 412 | return LY_SUCCESS; |
| 413 | |
| 414 | emem: |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 415 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 416 | return LY_EMEM; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 417 | } |
| 418 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 419 | static LY_ERR |
| 420 | structure_sprinter_pnode(const struct lysp_node *UNUSED(node), const void *UNUSED(plugin_priv), |
| 421 | ly_bool *UNUSED(skip), const char **flags, const char **UNUSED(add_opts)) |
| 422 | { |
| 423 | *flags = ""; |
| 424 | return LY_SUCCESS; |
| 425 | } |
| 426 | |
| 427 | static LY_ERR |
| 428 | structure_sprinter_cnode(const struct lysc_node *UNUSED(node), const void *UNUSED(plugin_priv), |
| 429 | ly_bool *UNUSED(skip), const char **flags, const char **UNUSED(add_opts)) |
| 430 | { |
| 431 | *flags = ""; |
| 432 | return LY_SUCCESS; |
| 433 | } |
| 434 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 435 | /** |
| 436 | * @brief Structure schema compiled tree printer. |
| 437 | * |
| 438 | * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. |
| 439 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 440 | static LY_ERR |
| 441 | structure_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 442 | const char **UNUSED(flags), const char **UNUSED(add_opts)) |
| 443 | { |
| 444 | LY_ERR rc; |
| 445 | |
| 446 | rc = lyplg_ext_sprinter_ctree_add_ext_nodes(ctx, ext, structure_sprinter_cnode); |
| 447 | return rc; |
| 448 | } |
| 449 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 450 | /** |
| 451 | * @brief Structure schema parsed tree printer. |
| 452 | * |
| 453 | * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. |
| 454 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 455 | static LY_ERR |
| 456 | structure_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 457 | const char **UNUSED(flags), const char **UNUSED(add_opts)) |
| 458 | { |
| 459 | LY_ERR rc; |
| 460 | |
| 461 | rc = lyplg_ext_sprinter_ptree_add_ext_nodes(ctx, ext, structure_sprinter_pnode); |
| 462 | return rc; |
| 463 | } |
| 464 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 465 | /** |
| 466 | * @brief Augment structure schema parsed tree printer. |
| 467 | * |
| 468 | * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. |
| 469 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 470 | static LY_ERR |
| 471 | structure_aug_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 472 | const char **UNUSED(flags), const char **UNUSED(add_opts)) |
| 473 | { |
| 474 | LY_ERR rc = LY_SUCCESS; |
| 475 | struct lysp_node_augment **aug; |
| 476 | |
| 477 | assert(ctx); |
| 478 | |
| 479 | aug = ext->substmts[12].storage; |
| 480 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, (*aug)->child, structure_sprinter_pnode); |
| 481 | |
| 482 | return rc; |
| 483 | } |
| 484 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 485 | /** |
| 486 | * @brief Augment structure schema compiled tree printer. |
| 487 | * |
| 488 | * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. |
| 489 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 490 | static LY_ERR |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 491 | structure_aug_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, const char **flags, |
| 492 | const char **add_opts) |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 493 | { |
| 494 | LY_ERR rc = LY_SUCCESS; |
| 495 | |
| 496 | LY_ARRAY_COUNT_TYPE i; |
| 497 | struct lysp_ext_instance *parsed_ext; |
| 498 | |
| 499 | assert(ctx); |
| 500 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 501 | /* find the parsed ext structure */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 502 | parsed_ext = ext->module->parsed->exts; |
| 503 | LY_ARRAY_FOR(parsed_ext, i) { |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 504 | if (!strcmp(parsed_ext[i].name, "sx:augment-structure") && !strcmp(parsed_ext[i].argument, ext->argument)) { |
| 505 | break; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 506 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 507 | } |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 508 | assert(i < LY_ARRAY_COUNT(parsed_ext)); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 509 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 510 | /* for augments print the parsed tree */ |
| 511 | rc = structure_aug_sprinter_ptree(parsed_ext, ctx, flags, add_opts); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 512 | return rc; |
| 513 | } |
| 514 | |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 515 | /** |
| 516 | * @brief Plugin descriptions for the structure extension |
| 517 | * |
| 518 | * Note that external plugins are supposed to use: |
| 519 | * |
| 520 | * LYPLG_EXTENSIONS = { |
| 521 | */ |
| 522 | const struct lyplg_ext_record plugins_structure[] = { |
| 523 | { |
| 524 | .module = "ietf-yang-structure-ext", |
| 525 | .revision = "2020-06-17", |
| 526 | .name = "structure", |
| 527 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 528 | .plugin.id = "ly2 structure v1", |
| 529 | .plugin.parse = structure_parse, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 530 | .plugin.compile = structure_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 531 | .plugin.printer_info = structure_printer_info, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 532 | .plugin.printer_ctree = structure_sprinter_ctree, |
| 533 | .plugin.printer_ptree = structure_sprinter_ptree, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 534 | .plugin.node = NULL, |
| 535 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 536 | .plugin.validate = NULL, |
| 537 | .plugin.pfree = structure_pfree, |
| 538 | .plugin.cfree = structure_cfree |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 539 | }, |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 540 | { |
| 541 | .module = "ietf-yang-structure-ext", |
| 542 | .revision = "2020-06-17", |
| 543 | .name = "augment-structure", |
| 544 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 545 | .plugin.id = "ly2 structure v1", |
| 546 | .plugin.parse = structure_aug_parse, |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 547 | .plugin.compile = NULL, |
| 548 | .plugin.printer_info = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 549 | .plugin.printer_ctree = structure_aug_sprinter_ctree, |
| 550 | .plugin.printer_ptree = structure_aug_sprinter_ptree, |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 551 | .plugin.node = NULL, |
| 552 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 553 | .plugin.validate = NULL, |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 554 | .plugin.pfree = structure_pfree, |
| 555 | .plugin.cfree = NULL |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 556 | }, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 557 | {0} /* terminating zeroed record */ |
| 558 | }; |