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 | |
| 15 | #include <stdint.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 19 | #include "compat.h" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 20 | #include "libyang.h" |
| 21 | #include "plugins_exts.h" |
| 22 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 23 | struct lysp_ext_instance_structure { |
| 24 | struct lysp_restr *musts; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 25 | uint16_t flags; |
| 26 | const char *dsc; |
| 27 | const char *ref; |
| 28 | struct lysp_tpdf *typedefs; |
| 29 | struct lysp_node_grp *groupings; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 30 | struct lysp_node *child; |
| 31 | }; |
| 32 | |
| 33 | struct lysc_ext_instance_structure { |
| 34 | struct lysc_must *musts; |
| 35 | uint16_t flags; |
| 36 | const char *dsc; |
| 37 | const char *ref; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 38 | struct lysc_node *child; |
| 39 | }; |
| 40 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 41 | struct lysp_ext_instance_augment_structure { |
| 42 | uint16_t flags; |
| 43 | const char *dsc; |
| 44 | const char *ref; |
| 45 | struct lysp_node *child; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 46 | struct lysp_node_augment *aug; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 49 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 50 | * @brief Parse structure extension instances. |
| 51 | * |
| 52 | * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. |
| 53 | */ |
| 54 | static LY_ERR |
| 55 | structure_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) |
| 56 | { |
| 57 | LY_ERR rc; |
| 58 | LY_ARRAY_COUNT_TYPE u; |
| 59 | struct lysp_module *pmod; |
| 60 | struct lysp_ext_instance_structure *struct_pdata; |
| 61 | |
| 62 | /* structure can appear only at the top level of a YANG module or submodule */ |
| 63 | if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { |
| 64 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, |
| 65 | "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name, |
| 66 | lyplg_ext_stmt2str(ext->parent_stmt)); |
| 67 | return LY_EVALID; |
| 68 | } |
| 69 | |
| 70 | pmod = ext->parent; |
| 71 | |
| 72 | /* check for duplication */ |
| 73 | LY_ARRAY_FOR(pmod->exts, u) { |
| 74 | if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) { |
| 75 | /* duplication of the same structure extension in a single module */ |
| 76 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name); |
| 77 | return LY_EVALID; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* allocate the storage */ |
| 82 | struct_pdata = calloc(1, sizeof *struct_pdata); |
| 83 | if (!struct_pdata) { |
| 84 | goto emem; |
| 85 | } |
| 86 | ext->parsed = struct_pdata; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 87 | 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] | 88 | |
| 89 | /* parse substatements */ |
| 90 | LY_ARRAY_INCREMENT(ext->substmts); |
| 91 | ext->substmts[0].stmt = LY_STMT_MUST; |
| 92 | ext->substmts[0].storage = &struct_pdata->musts; |
| 93 | |
| 94 | LY_ARRAY_INCREMENT(ext->substmts); |
| 95 | ext->substmts[1].stmt = LY_STMT_STATUS; |
| 96 | ext->substmts[1].storage = &struct_pdata->flags; |
| 97 | |
| 98 | LY_ARRAY_INCREMENT(ext->substmts); |
| 99 | ext->substmts[2].stmt = LY_STMT_DESCRIPTION; |
| 100 | ext->substmts[2].storage = &struct_pdata->dsc; |
| 101 | |
| 102 | LY_ARRAY_INCREMENT(ext->substmts); |
| 103 | ext->substmts[3].stmt = LY_STMT_REFERENCE; |
| 104 | ext->substmts[3].storage = &struct_pdata->ref; |
| 105 | |
| 106 | LY_ARRAY_INCREMENT(ext->substmts); |
| 107 | ext->substmts[4].stmt = LY_STMT_TYPEDEF; |
| 108 | ext->substmts[4].storage = &struct_pdata->typedefs; |
| 109 | |
| 110 | LY_ARRAY_INCREMENT(ext->substmts); |
| 111 | ext->substmts[5].stmt = LY_STMT_GROUPING; |
| 112 | ext->substmts[5].storage = &struct_pdata->groupings; |
| 113 | |
| 114 | /* data-def-stmt */ |
| 115 | LY_ARRAY_INCREMENT(ext->substmts); |
| 116 | ext->substmts[6].stmt = LY_STMT_CONTAINER; |
| 117 | ext->substmts[6].storage = &struct_pdata->child; |
| 118 | |
| 119 | LY_ARRAY_INCREMENT(ext->substmts); |
| 120 | ext->substmts[7].stmt = LY_STMT_LEAF; |
| 121 | ext->substmts[7].storage = &struct_pdata->child; |
| 122 | |
| 123 | LY_ARRAY_INCREMENT(ext->substmts); |
| 124 | ext->substmts[8].stmt = LY_STMT_LEAF_LIST; |
| 125 | ext->substmts[8].storage = &struct_pdata->child; |
| 126 | |
| 127 | LY_ARRAY_INCREMENT(ext->substmts); |
| 128 | ext->substmts[9].stmt = LY_STMT_LIST; |
| 129 | ext->substmts[9].storage = &struct_pdata->child; |
| 130 | |
| 131 | LY_ARRAY_INCREMENT(ext->substmts); |
| 132 | ext->substmts[10].stmt = LY_STMT_CHOICE; |
| 133 | ext->substmts[10].storage = &struct_pdata->child; |
| 134 | |
| 135 | LY_ARRAY_INCREMENT(ext->substmts); |
| 136 | ext->substmts[11].stmt = LY_STMT_ANYDATA; |
| 137 | ext->substmts[11].storage = &struct_pdata->child; |
| 138 | |
| 139 | LY_ARRAY_INCREMENT(ext->substmts); |
| 140 | ext->substmts[12].stmt = LY_STMT_ANYXML; |
| 141 | ext->substmts[12].storage = &struct_pdata->child; |
| 142 | |
| 143 | LY_ARRAY_INCREMENT(ext->substmts); |
| 144 | ext->substmts[13].stmt = LY_STMT_USES; |
| 145 | ext->substmts[13].storage = &struct_pdata->child; |
| 146 | |
| 147 | rc = lyplg_ext_parse_extension_instance(pctx, ext); |
| 148 | return rc; |
| 149 | |
| 150 | emem: |
| 151 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); |
| 152 | return LY_EMEM; |
| 153 | } |
| 154 | |
| 155 | /** |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 156 | * @brief Compile structure extension instances. |
| 157 | * |
| 158 | * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. |
| 159 | */ |
| 160 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 161 | 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] | 162 | { |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 163 | LY_ERR rc; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 164 | struct lysc_module *mod_c; |
| 165 | const struct lysc_node *child; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 166 | struct lysc_ext_instance_structure *struct_cdata; |
| 167 | uint32_t prev_options = *lyplg_ext_compile_get_options(cctx); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 168 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 169 | mod_c = ext->parent; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 170 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 171 | /* check identifier namespace with the compiled nodes */ |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 172 | LY_LIST_FOR(mod_c->data, child) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 173 | if (!strcmp(child->name, ext->argument)) { |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 174 | /* identifier collision */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 175 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, "Extension %s collides with a %s with the same identifier.", |
| 176 | extp->name, lys_nodetype2str(child->nodetype)); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 177 | return LY_EVALID; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* allocate the storage */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 182 | struct_cdata = calloc(1, sizeof *struct_cdata); |
| 183 | if (!struct_cdata) { |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 184 | goto emem; |
| 185 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 186 | ext->compiled = struct_cdata; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 187 | |
| 188 | /* compile substatements */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 189 | LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 14, rc, emem); |
| 190 | LY_ARRAY_INCREMENT(ext->substmts); |
| 191 | ext->substmts[0].stmt = LY_STMT_MUST; |
| 192 | ext->substmts[0].storage = &struct_cdata->musts; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 193 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 194 | LY_ARRAY_INCREMENT(ext->substmts); |
| 195 | ext->substmts[1].stmt = LY_STMT_STATUS; |
| 196 | ext->substmts[1].storage = &struct_cdata->flags; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 197 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 198 | LY_ARRAY_INCREMENT(ext->substmts); |
| 199 | ext->substmts[2].stmt = LY_STMT_DESCRIPTION; |
| 200 | ext->substmts[2].storage = &struct_cdata->dsc; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 201 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 202 | LY_ARRAY_INCREMENT(ext->substmts); |
| 203 | ext->substmts[3].stmt = LY_STMT_REFERENCE; |
| 204 | ext->substmts[3].storage = &struct_cdata->ref; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 205 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 206 | LY_ARRAY_INCREMENT(ext->substmts); |
| 207 | ext->substmts[4].stmt = LY_STMT_TYPEDEF; |
| 208 | ext->substmts[4].storage = NULL; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 209 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 210 | LY_ARRAY_INCREMENT(ext->substmts); |
| 211 | ext->substmts[5].stmt = LY_STMT_GROUPING; |
| 212 | ext->substmts[5].storage = NULL; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 213 | |
| 214 | /* data-def-stmt */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 215 | LY_ARRAY_INCREMENT(ext->substmts); |
| 216 | ext->substmts[6].stmt = LY_STMT_CONTAINER; |
| 217 | ext->substmts[6].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 218 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 219 | LY_ARRAY_INCREMENT(ext->substmts); |
| 220 | ext->substmts[7].stmt = LY_STMT_LEAF; |
| 221 | ext->substmts[7].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 222 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 223 | LY_ARRAY_INCREMENT(ext->substmts); |
| 224 | ext->substmts[8].stmt = LY_STMT_LEAF_LIST; |
| 225 | ext->substmts[8].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 226 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 227 | LY_ARRAY_INCREMENT(ext->substmts); |
| 228 | ext->substmts[9].stmt = LY_STMT_LIST; |
| 229 | ext->substmts[9].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 230 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 231 | LY_ARRAY_INCREMENT(ext->substmts); |
| 232 | ext->substmts[10].stmt = LY_STMT_CHOICE; |
| 233 | ext->substmts[10].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 234 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 235 | LY_ARRAY_INCREMENT(ext->substmts); |
| 236 | ext->substmts[11].stmt = LY_STMT_ANYDATA; |
| 237 | ext->substmts[11].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 238 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 239 | LY_ARRAY_INCREMENT(ext->substmts); |
| 240 | ext->substmts[12].stmt = LY_STMT_ANYXML; |
| 241 | ext->substmts[12].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 242 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 243 | LY_ARRAY_INCREMENT(ext->substmts); |
| 244 | ext->substmts[13].stmt = LY_STMT_USES; |
| 245 | ext->substmts[13].storage = &struct_cdata->child; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 246 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 247 | *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED; |
| 248 | rc = lyplg_ext_compile_extension_instance(cctx, extp, ext); |
| 249 | *lyplg_ext_compile_get_options(cctx) = prev_options; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 250 | if (rc) { |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | return LY_SUCCESS; |
| 255 | |
| 256 | emem: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 257 | 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] | 258 | return LY_EMEM; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @brief INFO printer |
| 263 | * |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 264 | * 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] | 265 | */ |
| 266 | static LY_ERR |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 267 | 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] | 268 | { |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 269 | lyplg_ext_print_info_extension_instance(ctx, ext, flag); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 270 | return LY_SUCCESS; |
| 271 | } |
| 272 | |
| 273 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 274 | * @brief Free parsed structure extension instance data. |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 275 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 276 | * 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] | 277 | */ |
| 278 | static void |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 279 | structure_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext) |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 280 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 281 | lyplg_ext_pfree_instance_substatements(ctx, ext->substmts); |
| 282 | free(ext->parsed); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @brief Free compiled structure extension instance data. |
| 287 | * |
| 288 | * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree. |
| 289 | */ |
| 290 | static void |
| 291 | structure_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
| 292 | { |
| 293 | lyplg_ext_cfree_instance_substatements(ctx, ext->substmts); |
| 294 | free(ext->compiled); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * @brief Parse augment-structure extension instances. |
| 299 | * |
| 300 | * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. |
| 301 | */ |
| 302 | static LY_ERR |
| 303 | structure_aug_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) |
| 304 | { |
| 305 | LY_ERR rc; |
| 306 | struct lysp_stmt *stmt; |
| 307 | struct lysp_ext_instance_augment_structure *aug_pdata; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 308 | 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] | 309 | |
| 310 | /* augment-structure can appear only at the top level of a YANG module or submodule */ |
| 311 | if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { |
| 312 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, |
| 313 | "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name, |
| 314 | lyplg_ext_stmt2str(ext->parent_stmt)); |
| 315 | return LY_EVALID; |
| 316 | } |
| 317 | |
| 318 | /* augment-structure must define some data-def-stmt */ |
| 319 | LY_LIST_FOR(ext->child, stmt) { |
| 320 | if (stmt->kw & LY_STMT_DATA_NODE_MASK) { |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | if (!stmt) { |
| 325 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s does not define any data-def-stmt statements.", |
| 326 | ext->name); |
| 327 | return LY_EVALID; |
| 328 | } |
| 329 | |
| 330 | /* allocate the storage */ |
| 331 | aug_pdata = calloc(1, sizeof *aug_pdata); |
| 332 | if (!aug_pdata) { |
| 333 | goto emem; |
| 334 | } |
| 335 | ext->parsed = aug_pdata; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 336 | LY_ARRAY_CREATE_GOTO(ctx, ext->substmts, 13, rc, emem); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 337 | |
| 338 | /* parse substatements */ |
| 339 | LY_ARRAY_INCREMENT(ext->substmts); |
| 340 | ext->substmts[0].stmt = LY_STMT_STATUS; |
| 341 | ext->substmts[0].storage = &aug_pdata->flags; |
| 342 | |
| 343 | LY_ARRAY_INCREMENT(ext->substmts); |
| 344 | ext->substmts[1].stmt = LY_STMT_DESCRIPTION; |
| 345 | ext->substmts[1].storage = &aug_pdata->dsc; |
| 346 | |
| 347 | LY_ARRAY_INCREMENT(ext->substmts); |
| 348 | ext->substmts[2].stmt = LY_STMT_REFERENCE; |
| 349 | ext->substmts[2].storage = &aug_pdata->ref; |
| 350 | |
| 351 | /* data-def-stmt */ |
| 352 | LY_ARRAY_INCREMENT(ext->substmts); |
| 353 | ext->substmts[3].stmt = LY_STMT_CONTAINER; |
| 354 | ext->substmts[3].storage = &aug_pdata->child; |
| 355 | |
| 356 | LY_ARRAY_INCREMENT(ext->substmts); |
| 357 | ext->substmts[4].stmt = LY_STMT_LEAF; |
| 358 | ext->substmts[4].storage = &aug_pdata->child; |
| 359 | |
| 360 | LY_ARRAY_INCREMENT(ext->substmts); |
| 361 | ext->substmts[5].stmt = LY_STMT_LEAF_LIST; |
| 362 | ext->substmts[5].storage = &aug_pdata->child; |
| 363 | |
| 364 | LY_ARRAY_INCREMENT(ext->substmts); |
| 365 | ext->substmts[6].stmt = LY_STMT_LIST; |
| 366 | ext->substmts[6].storage = &aug_pdata->child; |
| 367 | |
| 368 | LY_ARRAY_INCREMENT(ext->substmts); |
| 369 | ext->substmts[7].stmt = LY_STMT_CHOICE; |
| 370 | ext->substmts[7].storage = &aug_pdata->child; |
| 371 | |
| 372 | LY_ARRAY_INCREMENT(ext->substmts); |
| 373 | ext->substmts[8].stmt = LY_STMT_ANYDATA; |
| 374 | ext->substmts[8].storage = &aug_pdata->child; |
| 375 | |
| 376 | LY_ARRAY_INCREMENT(ext->substmts); |
| 377 | ext->substmts[9].stmt = LY_STMT_ANYXML; |
| 378 | ext->substmts[9].storage = &aug_pdata->child; |
| 379 | |
| 380 | LY_ARRAY_INCREMENT(ext->substmts); |
| 381 | ext->substmts[10].stmt = LY_STMT_USES; |
| 382 | ext->substmts[10].storage = &aug_pdata->child; |
| 383 | |
| 384 | /* case */ |
| 385 | LY_ARRAY_INCREMENT(ext->substmts); |
| 386 | ext->substmts[11].stmt = LY_STMT_CASE; |
| 387 | ext->substmts[11].storage = &aug_pdata->child; |
| 388 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 389 | if ((rc = lyplg_ext_parse_extension_instance(pctx, ext))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 390 | return rc; |
| 391 | } |
| 392 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 393 | /* add fake parsed augment node */ |
| 394 | LY_ARRAY_INCREMENT(ext->substmts); |
| 395 | ext->substmts[12].stmt = LY_STMT_AUGMENT; |
| 396 | ext->substmts[12].storage = &aug_pdata->aug; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 397 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 398 | aug_pdata->aug = calloc(1, sizeof *aug_pdata->aug); |
| 399 | if (!aug_pdata->aug) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 400 | goto emem; |
| 401 | } |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 402 | aug_pdata->aug->nodetype = LYS_AUGMENT; |
| 403 | aug_pdata->aug->flags = aug_pdata->flags; |
| 404 | lydict_insert(ctx, ext->argument, 0, &aug_pdata->aug->nodeid); |
| 405 | aug_pdata->aug->child = aug_pdata->child; |
| 406 | /* avoid double free */ |
| 407 | aug_pdata->child = NULL; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 408 | |
| 409 | return LY_SUCCESS; |
| 410 | |
| 411 | emem: |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 412 | 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] | 413 | return LY_EMEM; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
| 417 | * @brief Plugin descriptions for the structure extension |
| 418 | * |
| 419 | * Note that external plugins are supposed to use: |
| 420 | * |
| 421 | * LYPLG_EXTENSIONS = { |
| 422 | */ |
| 423 | const struct lyplg_ext_record plugins_structure[] = { |
| 424 | { |
| 425 | .module = "ietf-yang-structure-ext", |
| 426 | .revision = "2020-06-17", |
| 427 | .name = "structure", |
| 428 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 429 | .plugin.id = "ly2 structure v1", |
| 430 | .plugin.parse = structure_parse, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 431 | .plugin.compile = structure_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 432 | .plugin.printer_info = structure_printer_info, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 433 | .plugin.node = NULL, |
| 434 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 435 | .plugin.validate = NULL, |
| 436 | .plugin.pfree = structure_pfree, |
| 437 | .plugin.cfree = structure_cfree |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 438 | }, |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 439 | { |
| 440 | .module = "ietf-yang-structure-ext", |
| 441 | .revision = "2020-06-17", |
| 442 | .name = "augment-structure", |
| 443 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 444 | .plugin.id = "ly2 structure v1", |
| 445 | .plugin.parse = structure_aug_parse, |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 446 | .plugin.compile = NULL, |
| 447 | .plugin.printer_info = NULL, |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 448 | .plugin.node = NULL, |
| 449 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 450 | .plugin.validate = NULL, |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 451 | .plugin.pfree = structure_pfree, |
| 452 | .plugin.cfree = NULL |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 453 | }, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 454 | {0} /* terminating zeroed record */ |
| 455 | }; |