Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 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 "common.h" |
| 16 | |
| 17 | #include <ctype.h> |
| 18 | #include <dirent.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <linux/limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "libyang.h" |
| 29 | #include "context.h" |
| 30 | #include "tree_schema_internal.h" |
| 31 | #include "xpath.h" |
| 32 | |
| 33 | #define FREE_ARRAY(CTX, ARRAY, FUNC) {uint64_t c__; LY_ARRAY_FOR(ARRAY, c__){FUNC(CTX, &(ARRAY)[c__]);}LY_ARRAY_FREE(ARRAY);} |
| 34 | #define FREE_MEMBER(CTX, MEMBER, FUNC) if (MEMBER) {FUNC(CTX, MEMBER);free(MEMBER);} |
| 35 | #define FREE_STRING(CTX, STRING) if (STRING) {lydict_remove(CTX, STRING);} |
| 36 | #define FREE_STRINGS(CTX, ARRAY) {uint64_t c__; LY_ARRAY_FOR(ARRAY, c__){FREE_STRING(CTX, ARRAY[c__]);}LY_ARRAY_FREE(ARRAY);} |
| 37 | |
| 38 | static void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp); |
| 39 | static void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node); |
| 40 | |
| 41 | static void |
| 42 | lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt) |
| 43 | { |
| 44 | struct lysp_stmt *child, *next; |
| 45 | |
| 46 | FREE_STRING(ctx, stmt->stmt); |
| 47 | FREE_STRING(ctx, stmt->arg); |
| 48 | |
| 49 | LY_LIST_FOR_SAFE(stmt->child, next, child) { |
| 50 | lysp_stmt_free(ctx, child); |
| 51 | } |
| 52 | |
| 53 | free(stmt); |
| 54 | } |
| 55 | |
| 56 | static void |
| 57 | lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext) |
| 58 | { |
| 59 | struct lysp_stmt *stmt, *next; |
| 60 | |
| 61 | FREE_STRING(ctx, ext->name); |
| 62 | FREE_STRING(ctx, ext->argument); |
| 63 | |
| 64 | LY_LIST_FOR_SAFE(ext->child, next, stmt) { |
| 65 | lysp_stmt_free(ctx, stmt); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import) |
| 71 | { |
| 72 | /* imported module is freed directly from the context's list */ |
| 73 | FREE_STRING(ctx, import->name); |
| 74 | FREE_STRING(ctx, import->prefix); |
| 75 | FREE_STRING(ctx, import->dsc); |
| 76 | FREE_STRING(ctx, import->ref); |
| 77 | FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free); |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include) |
| 82 | { |
| 83 | if (include->submodule) { |
| 84 | lysp_module_free(include->submodule); |
| 85 | } |
| 86 | FREE_STRING(ctx, include->name); |
| 87 | FREE_STRING(ctx, include->dsc); |
| 88 | FREE_STRING(ctx, include->ref); |
| 89 | FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev) |
| 94 | { |
| 95 | FREE_STRING(ctx, rev->dsc); |
| 96 | FREE_STRING(ctx, rev->ref); |
| 97 | FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free); |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext) |
| 102 | { |
| 103 | FREE_STRING(ctx, ext->name); |
| 104 | FREE_STRING(ctx, ext->argument); |
| 105 | FREE_STRING(ctx, ext->dsc); |
| 106 | FREE_STRING(ctx, ext->ref); |
| 107 | FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free); |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat) |
| 112 | { |
| 113 | FREE_STRING(ctx, feat->name); |
| 114 | FREE_STRINGS(ctx, feat->iffeatures); |
| 115 | FREE_STRING(ctx, feat->dsc); |
| 116 | FREE_STRING(ctx, feat->ref); |
| 117 | FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free); |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident) |
| 122 | { |
| 123 | FREE_STRING(ctx, ident->name); |
| 124 | FREE_STRINGS(ctx, ident->iffeatures); |
| 125 | FREE_STRINGS(ctx, ident->bases); |
| 126 | FREE_STRING(ctx, ident->dsc); |
| 127 | FREE_STRING(ctx, ident->ref); |
| 128 | FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free); |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr) |
| 133 | { |
| 134 | FREE_STRING(ctx, restr->arg); |
| 135 | FREE_STRING(ctx, restr->emsg); |
| 136 | FREE_STRING(ctx, restr->eapptag); |
| 137 | FREE_STRING(ctx, restr->dsc); |
| 138 | FREE_STRING(ctx, restr->ref); |
| 139 | FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free); |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item) |
| 144 | { |
| 145 | FREE_STRING(ctx, item->name); |
| 146 | FREE_STRING(ctx, item->dsc); |
| 147 | FREE_STRING(ctx, item->ref); |
| 148 | FREE_STRINGS(ctx, item->iffeatures); |
| 149 | FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free); |
| 150 | } |
| 151 | |
| 152 | static void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type); |
| 153 | static void |
| 154 | lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type) |
| 155 | { |
| 156 | FREE_STRING(ctx, type->name); |
| 157 | FREE_MEMBER(ctx, type->range, lysp_restr_free); |
| 158 | FREE_MEMBER(ctx, type->length, lysp_restr_free); |
| 159 | FREE_ARRAY(ctx, type->patterns, lysp_restr_free); |
| 160 | FREE_ARRAY(ctx, type->enums, lysp_type_enum_free); |
| 161 | FREE_ARRAY(ctx, type->bits, lysp_type_enum_free); |
| 162 | FREE_STRING(ctx, type->path); |
| 163 | FREE_STRINGS(ctx, type->bases); |
| 164 | FREE_ARRAY(ctx, type->types, lysp_type_free); |
| 165 | FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free); |
| 166 | if (type->compiled) { |
| 167 | lysc_type_free(ctx, type->compiled); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf) |
| 173 | { |
| 174 | FREE_STRING(ctx, tpdf->name); |
| 175 | FREE_STRING(ctx, tpdf->units); |
| 176 | FREE_STRING(ctx, tpdf->dflt); |
| 177 | FREE_STRING(ctx, tpdf->dsc); |
| 178 | FREE_STRING(ctx, tpdf->ref); |
| 179 | FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free); |
| 180 | |
| 181 | lysp_type_free(ctx, &tpdf->type); |
| 182 | |
| 183 | } |
| 184 | |
| 185 | static void |
| 186 | lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout) |
| 187 | { |
| 188 | struct lysp_node *node, *next; |
| 189 | |
| 190 | FREE_ARRAY(ctx, inout->musts, lysp_restr_free); |
| 191 | FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free); |
| 192 | FREE_ARRAY(ctx, inout->groupings, lysp_grp_free); |
| 193 | LY_LIST_FOR_SAFE(inout->data, next, node) { |
| 194 | lysp_node_free(ctx, node); |
| 195 | } |
| 196 | FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free); |
| 197 | |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action) |
| 202 | { |
| 203 | FREE_STRING(ctx, action->name); |
| 204 | FREE_STRING(ctx, action->dsc); |
| 205 | FREE_STRING(ctx, action->ref); |
| 206 | FREE_STRINGS(ctx, action->iffeatures); |
| 207 | FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free); |
| 208 | FREE_ARRAY(ctx, action->groupings, lysp_grp_free); |
| 209 | FREE_MEMBER(ctx, action->input, lysp_action_inout_free); |
| 210 | FREE_MEMBER(ctx, action->output, lysp_action_inout_free); |
| 211 | FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free); |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif) |
| 216 | { |
| 217 | struct lysp_node *node, *next; |
| 218 | |
| 219 | FREE_STRING(ctx, notif->name); |
| 220 | FREE_STRING(ctx, notif->dsc); |
| 221 | FREE_STRING(ctx, notif->ref); |
| 222 | FREE_STRINGS(ctx, notif->iffeatures); |
| 223 | FREE_ARRAY(ctx, notif->musts, lysp_restr_free); |
| 224 | FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free); |
| 225 | FREE_ARRAY(ctx, notif->groupings, lysp_grp_free); |
| 226 | LY_LIST_FOR_SAFE(notif->data, next, node) { |
| 227 | lysp_node_free(ctx, node); |
| 228 | } |
| 229 | FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free); |
| 230 | } |
| 231 | |
| 232 | static void |
| 233 | lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp) |
| 234 | { |
| 235 | struct lysp_node *node, *next; |
| 236 | |
| 237 | FREE_STRING(ctx, grp->name); |
| 238 | FREE_STRING(ctx, grp->dsc); |
| 239 | FREE_STRING(ctx, grp->ref); |
| 240 | FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free); |
| 241 | FREE_ARRAY(ctx, grp->groupings, lysp_grp_free); |
| 242 | LY_LIST_FOR_SAFE(grp->data, next, node) { |
| 243 | lysp_node_free(ctx, node); |
| 244 | } |
| 245 | FREE_ARRAY(ctx, grp->actions, lysp_action_free); |
| 246 | FREE_ARRAY(ctx, grp->notifs, lysp_notif_free); |
| 247 | FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free); |
| 248 | } |
| 249 | |
| 250 | static void |
| 251 | lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when) |
| 252 | { |
| 253 | FREE_STRING(ctx, when->cond); |
| 254 | FREE_STRING(ctx, when->dsc); |
| 255 | FREE_STRING(ctx, when->ref); |
| 256 | FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free); |
| 257 | } |
| 258 | |
| 259 | static void |
| 260 | lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment) |
| 261 | { |
| 262 | struct lysp_node *node, *next; |
| 263 | |
| 264 | FREE_STRING(ctx, augment->nodeid); |
| 265 | FREE_STRING(ctx, augment->dsc); |
| 266 | FREE_STRING(ctx, augment->ref); |
| 267 | FREE_MEMBER(ctx, augment->when, lysp_when_free); |
| 268 | FREE_STRINGS(ctx, augment->iffeatures); |
| 269 | LY_LIST_FOR_SAFE(augment->child, next, node) { |
| 270 | lysp_node_free(ctx, node); |
| 271 | } |
| 272 | FREE_ARRAY(ctx, augment->actions, lysp_action_free); |
| 273 | FREE_ARRAY(ctx, augment->notifs, lysp_notif_free); |
| 274 | FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free); |
| 275 | } |
| 276 | |
| 277 | static void |
| 278 | lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d) |
| 279 | { |
| 280 | struct lysp_deviate_add *add = (struct lysp_deviate_add*)d; |
| 281 | struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl*)d; |
| 282 | |
| 283 | FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free); |
| 284 | switch(d->mod) { |
| 285 | case LYS_DEV_NOT_SUPPORTED: |
| 286 | /* nothing to do */ |
| 287 | break; |
| 288 | case LYS_DEV_ADD: |
| 289 | case LYS_DEV_DELETE: /* compatible for dynamically allocated data */ |
| 290 | FREE_STRING(ctx, add->units); |
| 291 | FREE_ARRAY(ctx, add->musts, lysp_restr_free); |
| 292 | FREE_STRINGS(ctx, add->uniques); |
| 293 | FREE_STRINGS(ctx, add->dflts); |
| 294 | break; |
| 295 | case LYS_DEV_REPLACE: |
| 296 | FREE_MEMBER(ctx, rpl->type, lysp_type_free); |
| 297 | FREE_STRING(ctx, rpl->units); |
| 298 | FREE_STRING(ctx, rpl->dflt); |
| 299 | break; |
| 300 | default: |
| 301 | LOGINT(ctx); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void |
| 307 | lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev) |
| 308 | { |
| 309 | struct lysp_deviate *next, *iter; |
| 310 | |
| 311 | FREE_STRING(ctx, dev->nodeid); |
| 312 | FREE_STRING(ctx, dev->dsc); |
| 313 | FREE_STRING(ctx, dev->ref); |
| 314 | LY_LIST_FOR_SAFE(dev->deviates, next, iter) { |
| 315 | lysp_deviate_free(ctx, iter); |
| 316 | free(iter); |
| 317 | } |
| 318 | FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free); |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref) |
| 323 | { |
| 324 | FREE_STRING(ctx, ref->nodeid); |
| 325 | FREE_STRING(ctx, ref->dsc); |
| 326 | FREE_STRING(ctx, ref->ref); |
| 327 | FREE_STRINGS(ctx, ref->iffeatures); |
| 328 | FREE_ARRAY(ctx, ref->musts, lysp_restr_free); |
| 329 | FREE_STRING(ctx, ref->presence); |
| 330 | FREE_STRINGS(ctx, ref->dflts); |
| 331 | FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free); |
| 332 | } |
| 333 | |
| 334 | static void |
| 335 | lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node) |
| 336 | { |
| 337 | struct lysp_node *child, *next; |
| 338 | |
| 339 | FREE_STRING(ctx, node->name); |
| 340 | FREE_STRING(ctx, node->dsc); |
| 341 | FREE_STRING(ctx, node->ref); |
| 342 | FREE_MEMBER(ctx, node->when, lysp_when_free); |
| 343 | FREE_STRINGS(ctx, node->iffeatures); |
| 344 | FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free); |
| 345 | |
| 346 | switch(node->nodetype) { |
| 347 | case LYS_CONTAINER: |
| 348 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->musts, lysp_restr_free); |
| 349 | FREE_STRING(ctx, ((struct lysp_node_container*)node)->presence); |
| 350 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->typedefs, lysp_tpdf_free); |
| 351 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->groupings, lysp_grp_free); |
| 352 | LY_LIST_FOR_SAFE(((struct lysp_node_container*)node)->child, next, child) { |
| 353 | lysp_node_free(ctx, child); |
| 354 | } |
| 355 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->actions, lysp_action_free); |
| 356 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->notifs, lysp_notif_free); |
| 357 | break; |
| 358 | case LYS_LEAF: |
| 359 | FREE_ARRAY(ctx, ((struct lysp_node_leaf*)node)->musts, lysp_restr_free); |
| 360 | lysp_type_free(ctx, &((struct lysp_node_leaf*)node)->type); |
| 361 | FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->units); |
| 362 | FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->dflt); |
| 363 | break; |
| 364 | case LYS_LEAFLIST: |
| 365 | FREE_ARRAY(ctx, ((struct lysp_node_leaflist*)node)->musts, lysp_restr_free); |
| 366 | lysp_type_free(ctx, &((struct lysp_node_leaflist*)node)->type); |
| 367 | FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->units); |
| 368 | FREE_STRINGS(ctx, ((struct lysp_node_leaflist*)node)->dflts); |
| 369 | break; |
| 370 | case LYS_LIST: |
| 371 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->musts, lysp_restr_free); |
| 372 | FREE_STRING(ctx, ((struct lysp_node_list*)node)->key); |
| 373 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->typedefs, lysp_tpdf_free); |
| 374 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->groupings, lysp_grp_free); |
| 375 | LY_LIST_FOR_SAFE(((struct lysp_node_list*)node)->child, next, child) { |
| 376 | lysp_node_free(ctx, child); |
| 377 | } |
| 378 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->actions, lysp_action_free); |
| 379 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->notifs, lysp_notif_free); |
| 380 | FREE_STRINGS(ctx, ((struct lysp_node_list*)node)->uniques); |
| 381 | break; |
| 382 | case LYS_CHOICE: |
| 383 | LY_LIST_FOR_SAFE(((struct lysp_node_choice*)node)->child, next, child) { |
| 384 | lysp_node_free(ctx, child); |
| 385 | } |
| 386 | FREE_STRING(ctx, ((struct lysp_node_choice*)node)->dflt); |
| 387 | break; |
| 388 | case LYS_CASE: |
| 389 | LY_LIST_FOR_SAFE(((struct lysp_node_case*)node)->child, next, child) { |
| 390 | lysp_node_free(ctx, child); |
| 391 | } |
| 392 | break; |
| 393 | case LYS_ANYDATA: |
| 394 | case LYS_ANYXML: |
| 395 | FREE_ARRAY(ctx, ((struct lysp_node_anydata*)node)->musts, lysp_restr_free); |
| 396 | break; |
| 397 | case LYS_USES: |
| 398 | FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->refines, lysp_refine_free); |
| 399 | FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->augments, lysp_augment_free); |
| 400 | break; |
| 401 | default: |
| 402 | LOGINT(ctx); |
| 403 | } |
| 404 | |
| 405 | free(node); |
| 406 | } |
| 407 | |
| 408 | API void |
| 409 | lysp_module_free(struct lysp_module *module) |
| 410 | { |
| 411 | struct ly_ctx *ctx; |
| 412 | struct lysp_node *node, *next; |
| 413 | |
| 414 | LY_CHECK_ARG_RET(NULL, module,); |
| 415 | ctx = module->ctx; |
| 416 | |
| 417 | FREE_STRING(ctx, module->name); |
| 418 | FREE_STRING(ctx, module->filepath); |
| 419 | FREE_STRING(ctx, module->ns); /* or belongs-to */ |
| 420 | FREE_STRING(ctx, module->prefix); |
| 421 | |
| 422 | FREE_ARRAY(ctx, module->imports, lysp_import_free); |
| 423 | FREE_ARRAY(ctx, module->includes, lysp_include_free); |
| 424 | |
| 425 | FREE_STRING(ctx, module->org); |
| 426 | FREE_STRING(ctx, module->contact); |
| 427 | FREE_STRING(ctx, module->dsc); |
| 428 | FREE_STRING(ctx, module->ref); |
| 429 | |
| 430 | FREE_ARRAY(ctx, module->revs, lysp_revision_free); |
| 431 | FREE_ARRAY(ctx, module->extensions, lysp_ext_free); |
| 432 | FREE_ARRAY(ctx, module->features, lysp_feature_free); |
| 433 | FREE_ARRAY(ctx, module->identities, lysp_ident_free); |
| 434 | FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free); |
| 435 | FREE_ARRAY(ctx, module->groupings, lysp_grp_free); |
| 436 | LY_LIST_FOR_SAFE(module->data, next, node) { |
| 437 | lysp_node_free(ctx, node); |
| 438 | } |
| 439 | FREE_ARRAY(ctx, module->augments, lysp_augment_free); |
| 440 | FREE_ARRAY(ctx, module->rpcs, lysp_action_free); |
| 441 | FREE_ARRAY(ctx, module->notifs, lysp_notif_free); |
| 442 | FREE_ARRAY(ctx, module->deviations, lysp_deviation_free); |
| 443 | FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free); |
| 444 | |
| 445 | free(module); |
| 446 | } |
| 447 | |
| 448 | static void |
| 449 | lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
| 450 | { |
| 451 | FREE_STRING(ctx, ext->argument); |
| 452 | FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free); |
| 453 | } |
| 454 | |
| 455 | static void |
| 456 | lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff) |
| 457 | { |
| 458 | LY_ARRAY_FREE(iff->features); |
| 459 | free(iff->expr); |
| 460 | } |
| 461 | |
| 462 | static void |
| 463 | lysc_import_free(struct ly_ctx *ctx, struct lysc_import *import) |
| 464 | { |
| 465 | /* imported module is freed directly from the context's list */ |
| 466 | FREE_STRING(ctx, import->prefix); |
| 467 | FREE_ARRAY(ctx, import->exts, lysc_ext_instance_free); |
| 468 | } |
| 469 | |
| 470 | static void |
| 471 | lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident) |
| 472 | { |
| 473 | FREE_STRING(ctx, ident->name); |
| 474 | FREE_ARRAY(ctx, ident->iffeatures, lysc_iffeature_free); |
| 475 | LY_ARRAY_FREE(ident->derived); |
| 476 | FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free); |
| 477 | } |
| 478 | |
| 479 | static void |
| 480 | lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat) |
| 481 | { |
| 482 | FREE_STRING(ctx, feat->name); |
| 483 | FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free); |
| 484 | LY_ARRAY_FREE(feat->depfeatures); |
| 485 | FREE_ARRAY(ctx, feat->exts, lysc_ext_instance_free); |
| 486 | } |
| 487 | |
| 488 | static void |
| 489 | lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range) |
| 490 | { |
| 491 | LY_ARRAY_FREE(range->parts); |
| 492 | FREE_STRING(ctx, range->eapptag); |
| 493 | FREE_STRING(ctx, range->emsg); |
| 494 | FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free); |
| 495 | } |
| 496 | |
| 497 | static void |
| 498 | lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern) |
| 499 | { |
| 500 | if (--(*pattern)->refcount) { |
| 501 | return; |
| 502 | } |
| 503 | pcre_free((*pattern)->expr); |
| 504 | pcre_free_study((*pattern)->expr_extra); |
| 505 | FREE_STRING(ctx, (*pattern)->eapptag); |
| 506 | FREE_STRING(ctx, (*pattern)->emsg); |
| 507 | FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free); |
| 508 | free(*pattern); |
| 509 | } |
| 510 | |
| 511 | static void |
| 512 | lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_enum_item *item) |
| 513 | { |
| 514 | FREE_STRING(ctx, item->name); |
| 515 | FREE_ARRAY(ctx, item->iffeatures, lysc_iffeature_free); |
| 516 | FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free); |
| 517 | } |
| 518 | |
| 519 | static void |
| 520 | lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type) |
| 521 | { |
| 522 | if (--type->refcount) { |
| 523 | return; |
| 524 | } |
| 525 | switch(type->basetype) { |
| 526 | case LY_TYPE_BINARY: |
| 527 | FREE_MEMBER(ctx, ((struct lysc_type_bin*)type)->length, lysc_range_free); |
| 528 | break; |
| 529 | case LY_TYPE_BITS: |
| 530 | FREE_ARRAY(ctx, (struct lysc_type_enum_item*)((struct lysc_type_bits*)type)->bits, lysc_enum_item_free); |
| 531 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame^] | 532 | case LY_TYPE_DEC64: |
| 533 | FREE_MEMBER(ctx, ((struct lysc_type_dec*)type)->range, lysc_range_free); |
| 534 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 535 | case LY_TYPE_STRING: |
| 536 | FREE_MEMBER(ctx, ((struct lysc_type_str*)type)->length, lysc_range_free); |
| 537 | FREE_ARRAY(ctx, ((struct lysc_type_str*)type)->patterns, lysc_pattern_free); |
| 538 | break; |
| 539 | case LY_TYPE_ENUM: |
| 540 | FREE_ARRAY(ctx, ((struct lysc_type_enum*)type)->enums, lysc_enum_item_free); |
| 541 | break; |
| 542 | case LY_TYPE_INT8: |
| 543 | case LY_TYPE_UINT8: |
| 544 | case LY_TYPE_INT16: |
| 545 | case LY_TYPE_UINT16: |
| 546 | case LY_TYPE_INT32: |
| 547 | case LY_TYPE_UINT32: |
| 548 | case LY_TYPE_INT64: |
| 549 | case LY_TYPE_UINT64: |
| 550 | FREE_MEMBER(ctx, ((struct lysc_type_num*)type)->range, lysc_range_free); |
| 551 | break; |
| 552 | case LY_TYPE_BOOL: |
| 553 | case LY_TYPE_EMPTY: |
| 554 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 555 | /* nothing to do */ |
| 556 | break; |
| 557 | } |
| 558 | FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free); |
| 559 | |
| 560 | free(type); |
| 561 | } |
| 562 | |
| 563 | void |
| 564 | lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node) |
| 565 | { |
| 566 | struct lysc_node *child, *child_next; |
| 567 | |
| 568 | LY_LIST_FOR_SAFE(node->child, child_next, child) { |
| 569 | lysc_node_free(ctx, child); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | static void |
| 574 | lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node) |
| 575 | { |
| 576 | if (node->type) { |
| 577 | lysc_type_free(ctx, node->type); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void |
| 582 | lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node) |
| 583 | { |
| 584 | /* common part */ |
| 585 | FREE_STRING(ctx, node->name); |
| 586 | |
| 587 | /* nodetype-specific part */ |
| 588 | switch(node->nodetype) { |
| 589 | case LYS_CONTAINER: |
| 590 | lysc_node_container_free(ctx, (struct lysc_node_container*)node); |
| 591 | break; |
| 592 | case LYS_LEAF: |
| 593 | lysc_node_leaf_free(ctx, (struct lysc_node_leaf*)node); |
| 594 | break; |
| 595 | default: |
| 596 | LOGINT(ctx); |
| 597 | } |
| 598 | |
| 599 | free(node); |
| 600 | } |
| 601 | |
| 602 | static void |
| 603 | lysc_module_free_(struct lysc_module *module) |
| 604 | { |
| 605 | struct ly_ctx *ctx; |
| 606 | struct lysc_node *node, *node_next; |
| 607 | |
| 608 | LY_CHECK_ARG_RET(NULL, module,); |
| 609 | ctx = module->ctx; |
| 610 | |
| 611 | FREE_STRING(ctx, module->name); |
| 612 | FREE_STRING(ctx, module->ns); |
| 613 | FREE_STRING(ctx, module->prefix); |
| 614 | FREE_STRING(ctx, module->revision); |
| 615 | |
| 616 | FREE_ARRAY(ctx, module->imports, lysc_import_free); |
| 617 | FREE_ARRAY(ctx, module->features, lysc_feature_free); |
| 618 | FREE_ARRAY(ctx, module->identities, lysc_ident_free); |
| 619 | |
| 620 | LY_LIST_FOR_SAFE(module->data, node_next, node) { |
| 621 | lysc_node_free(ctx, node); |
| 622 | } |
| 623 | |
| 624 | FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free); |
| 625 | |
| 626 | free(module); |
| 627 | } |
| 628 | |
| 629 | void |
| 630 | lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv)) |
| 631 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 632 | if (module) { |
| 633 | lysc_module_free_(module); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | void |
| 638 | lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv)) |
| 639 | { |
| 640 | if (!module) { |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | lysc_module_free(module->compiled, private_destructor); |
| 645 | lysp_module_free(module->parsed); |
| 646 | free(module); |
| 647 | } |