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; |
| 405 | lydict_insert(ctx, ext->argument, 0, &aug_pdata->aug->nodeid); |
| 406 | aug_pdata->aug->child = aug_pdata->child; |
| 407 | /* avoid double free */ |
| 408 | aug_pdata->child = NULL; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 409 | |
| 410 | return LY_SUCCESS; |
| 411 | |
| 412 | emem: |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 413 | 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] | 414 | return LY_EMEM; |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 415 | } |
| 416 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 417 | static LY_ERR |
| 418 | structure_sprinter_pnode(const struct lysp_node *UNUSED(node), const void *UNUSED(plugin_priv), |
| 419 | ly_bool *UNUSED(skip), const char **flags, const char **UNUSED(add_opts)) |
| 420 | { |
| 421 | *flags = ""; |
| 422 | return LY_SUCCESS; |
| 423 | } |
| 424 | |
| 425 | static LY_ERR |
| 426 | structure_sprinter_cnode(const struct lysc_node *UNUSED(node), const void *UNUSED(plugin_priv), |
| 427 | ly_bool *UNUSED(skip), const char **flags, const char **UNUSED(add_opts)) |
| 428 | { |
| 429 | *flags = ""; |
| 430 | return LY_SUCCESS; |
| 431 | } |
| 432 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 433 | /** |
| 434 | * @brief Structure schema compiled tree printer. |
| 435 | * |
| 436 | * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. |
| 437 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 438 | static LY_ERR |
| 439 | structure_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 440 | const char **UNUSED(flags), const char **UNUSED(add_opts)) |
| 441 | { |
| 442 | LY_ERR rc; |
| 443 | |
| 444 | rc = lyplg_ext_sprinter_ctree_add_ext_nodes(ctx, ext, structure_sprinter_cnode); |
| 445 | return rc; |
| 446 | } |
| 447 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 448 | /** |
| 449 | * @brief Structure schema parsed tree printer. |
| 450 | * |
| 451 | * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. |
| 452 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 453 | static LY_ERR |
| 454 | structure_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 455 | const char **UNUSED(flags), const char **UNUSED(add_opts)) |
| 456 | { |
| 457 | LY_ERR rc; |
| 458 | |
| 459 | rc = lyplg_ext_sprinter_ptree_add_ext_nodes(ctx, ext, structure_sprinter_pnode); |
| 460 | return rc; |
| 461 | } |
| 462 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 463 | /** |
| 464 | * @brief Augment structure schema parsed tree printer. |
| 465 | * |
| 466 | * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. |
| 467 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 468 | static LY_ERR |
| 469 | structure_aug_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 470 | const char **UNUSED(flags), const char **UNUSED(add_opts)) |
| 471 | { |
| 472 | LY_ERR rc = LY_SUCCESS; |
| 473 | struct lysp_node_augment **aug; |
| 474 | |
| 475 | assert(ctx); |
| 476 | |
| 477 | aug = ext->substmts[12].storage; |
| 478 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, (*aug)->child, structure_sprinter_pnode); |
| 479 | |
| 480 | return rc; |
| 481 | } |
| 482 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 483 | /** |
| 484 | * @brief Augment structure schema compiled tree printer. |
| 485 | * |
| 486 | * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. |
| 487 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 488 | static LY_ERR |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 489 | structure_aug_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, const char **flags, |
| 490 | const char **add_opts) |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 491 | { |
| 492 | LY_ERR rc = LY_SUCCESS; |
| 493 | |
| 494 | LY_ARRAY_COUNT_TYPE i; |
| 495 | struct lysp_ext_instance *parsed_ext; |
| 496 | |
| 497 | assert(ctx); |
| 498 | |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 499 | /* find the parsed ext structure */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 500 | parsed_ext = ext->module->parsed->exts; |
| 501 | LY_ARRAY_FOR(parsed_ext, i) { |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 502 | if (!strcmp(parsed_ext[i].name, "sx:augment-structure") && !strcmp(parsed_ext[i].argument, ext->argument)) { |
| 503 | break; |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 504 | } |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 505 | } |
Michal Vasko | 220f055 | 2022-11-08 09:04:06 +0100 | [diff] [blame] | 506 | assert(i < LY_ARRAY_COUNT(parsed_ext)); |
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 | /* for augments print the parsed tree */ |
| 509 | rc = structure_aug_sprinter_ptree(parsed_ext, ctx, flags, add_opts); |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 510 | return rc; |
| 511 | } |
| 512 | |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 513 | /** |
| 514 | * @brief Plugin descriptions for the structure extension |
| 515 | * |
| 516 | * Note that external plugins are supposed to use: |
| 517 | * |
| 518 | * LYPLG_EXTENSIONS = { |
| 519 | */ |
| 520 | const struct lyplg_ext_record plugins_structure[] = { |
| 521 | { |
| 522 | .module = "ietf-yang-structure-ext", |
| 523 | .revision = "2020-06-17", |
| 524 | .name = "structure", |
| 525 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 526 | .plugin.id = "ly2 structure v1", |
| 527 | .plugin.parse = structure_parse, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 528 | .plugin.compile = structure_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 529 | .plugin.printer_info = structure_printer_info, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 530 | .plugin.printer_ctree = structure_sprinter_ctree, |
| 531 | .plugin.printer_ptree = structure_sprinter_ptree, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 532 | .plugin.node = NULL, |
| 533 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 534 | .plugin.validate = NULL, |
| 535 | .plugin.pfree = structure_pfree, |
| 536 | .plugin.cfree = structure_cfree |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 537 | }, |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 538 | { |
| 539 | .module = "ietf-yang-structure-ext", |
| 540 | .revision = "2020-06-17", |
| 541 | .name = "augment-structure", |
| 542 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 543 | .plugin.id = "ly2 structure v1", |
| 544 | .plugin.parse = structure_aug_parse, |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 545 | .plugin.compile = NULL, |
| 546 | .plugin.printer_info = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 547 | .plugin.printer_ctree = structure_aug_sprinter_ctree, |
| 548 | .plugin.printer_ptree = structure_aug_sprinter_ptree, |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 549 | .plugin.node = NULL, |
| 550 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 551 | .plugin.validate = NULL, |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 552 | .plugin.pfree = structure_pfree, |
| 553 | .plugin.cfree = NULL |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 554 | }, |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 555 | {0} /* terminating zeroed record */ |
| 556 | }; |