blob: bdc65ed24e496fe6d8ae7551e00e476c5d78c169 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
Radek Krejcie7b95092019-05-15 11:03:07 +02002 * @file tree_schema_free.c
Radek Krejci19a96102018-11-15 13:38:09 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Freeing functions for schema tree structures.
Radek Krejci19a96102018-11-15 13:38:09 +01005 *
Radek Krejcie7b95092019-05-15 11:03:07 +02006 * Copyright (c) 2019 CESNET, z.s.p.o.
Radek Krejci19a96102018-11-15 13:38:09 +01007 *
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
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <stdlib.h>
16
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "common.h"
18#include "config.h"
19#include "plugins_exts.h"
20#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010024#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010026#include "xpath.h"
27
Radek Krejci2d7a47b2019-05-16 13:34:10 +020028void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
29void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Michal Vasko5fe75f12020-03-02 13:52:37 +010030void lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext);
Radek Krejci19a96102018-11-15 13:38:09 +010031
32static void
33lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
34{
35 struct lysp_stmt *child, *next;
36
37 FREE_STRING(ctx, stmt->stmt);
38 FREE_STRING(ctx, stmt->arg);
39
40 LY_LIST_FOR_SAFE(stmt->child, next, child) {
41 lysp_stmt_free(ctx, child);
42 }
43
44 free(stmt);
45}
46
Radek Krejci2d7a47b2019-05-16 13:34:10 +020047void
Radek Krejci19a96102018-11-15 13:38:09 +010048lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
49{
50 struct lysp_stmt *stmt, *next;
51
52 FREE_STRING(ctx, ext->name);
53 FREE_STRING(ctx, ext->argument);
54
55 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
56 lysp_stmt_free(ctx, stmt);
57 }
58}
59
David Sedlák298ff6d2019-07-26 14:29:03 +020060void
Radek Krejci19a96102018-11-15 13:38:09 +010061lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
62{
63 /* imported module is freed directly from the context's list */
64 FREE_STRING(ctx, import->name);
65 FREE_STRING(ctx, import->prefix);
66 FREE_STRING(ctx, import->dsc);
67 FREE_STRING(ctx, import->ref);
68 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
69}
70
David Sedlák0c2bab92019-07-22 15:33:19 +020071void
Radek Krejci19a96102018-11-15 13:38:09 +010072lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
73{
74 if (include->submodule) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +010075 lysp_submodule_free(ctx, include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010076 }
77 FREE_STRING(ctx, include->name);
78 FREE_STRING(ctx, include->dsc);
79 FREE_STRING(ctx, include->ref);
80 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
81}
82
David Sedlákaa854b02019-07-22 14:17:10 +020083void
Radek Krejci19a96102018-11-15 13:38:09 +010084lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
85{
86 FREE_STRING(ctx, rev->dsc);
87 FREE_STRING(ctx, rev->ref);
88 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
89}
90
David Sedlák986cb412019-07-04 13:10:11 +020091void
Radek Krejci19a96102018-11-15 13:38:09 +010092lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
93{
94 FREE_STRING(ctx, ext->name);
95 FREE_STRING(ctx, ext->argument);
96 FREE_STRING(ctx, ext->dsc);
97 FREE_STRING(ctx, ext->ref);
98 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +010099 if (ext->compiled) {
100 lysc_extension_free(ctx, &ext->compiled);
101 }
Radek Krejci19a96102018-11-15 13:38:09 +0100102}
103
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200104void
Radek Krejci19a96102018-11-15 13:38:09 +0100105lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
106{
107 FREE_STRING(ctx, feat->name);
108 FREE_STRINGS(ctx, feat->iffeatures);
109 FREE_STRING(ctx, feat->dsc);
110 FREE_STRING(ctx, feat->ref);
111 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
112}
113
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200114void
Radek Krejci19a96102018-11-15 13:38:09 +0100115lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
116{
117 FREE_STRING(ctx, ident->name);
118 FREE_STRINGS(ctx, ident->iffeatures);
119 FREE_STRINGS(ctx, ident->bases);
120 FREE_STRING(ctx, ident->dsc);
121 FREE_STRING(ctx, ident->ref);
122 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
123}
124
125static void
126lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
127{
128 FREE_STRING(ctx, restr->arg);
129 FREE_STRING(ctx, restr->emsg);
130 FREE_STRING(ctx, restr->eapptag);
131 FREE_STRING(ctx, restr->dsc);
132 FREE_STRING(ctx, restr->ref);
133 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
134}
135
136static void
137lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
138{
139 FREE_STRING(ctx, item->name);
140 FREE_STRING(ctx, item->dsc);
141 FREE_STRING(ctx, item->ref);
142 FREE_STRINGS(ctx, item->iffeatures);
143 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
144}
145
Radek Krejcicdfecd92018-11-26 11:27:32 +0100146void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
David Sedlák32488102019-07-15 17:44:10 +0200147void
Radek Krejci19a96102018-11-15 13:38:09 +0100148lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
149{
150 FREE_STRING(ctx, type->name);
151 FREE_MEMBER(ctx, type->range, lysp_restr_free);
152 FREE_MEMBER(ctx, type->length, lysp_restr_free);
153 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
154 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
155 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
156 FREE_STRING(ctx, type->path);
157 FREE_STRINGS(ctx, type->bases);
158 FREE_ARRAY(ctx, type->types, lysp_type_free);
159 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
160 if (type->compiled) {
161 lysc_type_free(ctx, type->compiled);
162 }
163}
164
David Sedlák04e17b22019-07-19 15:29:48 +0200165void
Radek Krejci19a96102018-11-15 13:38:09 +0100166lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
167{
168 FREE_STRING(ctx, tpdf->name);
169 FREE_STRING(ctx, tpdf->units);
170 FREE_STRING(ctx, tpdf->dflt);
171 FREE_STRING(ctx, tpdf->dsc);
172 FREE_STRING(ctx, tpdf->ref);
173 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
174
175 lysp_type_free(ctx, &tpdf->type);
176
177}
178
Radek Krejcif538ce52019-03-05 10:46:14 +0100179void
Radek Krejci19a96102018-11-15 13:38:09 +0100180lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout)
181{
182 struct lysp_node *node, *next;
183
184 FREE_ARRAY(ctx, inout->musts, lysp_restr_free);
185 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
186 FREE_ARRAY(ctx, inout->groupings, lysp_grp_free);
187 LY_LIST_FOR_SAFE(inout->data, next, node) {
188 lysp_node_free(ctx, node);
189 }
190 FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free);
191
192}
193
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200194void
Radek Krejci19a96102018-11-15 13:38:09 +0100195lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action)
196{
197 FREE_STRING(ctx, action->name);
198 FREE_STRING(ctx, action->dsc);
199 FREE_STRING(ctx, action->ref);
200 FREE_STRINGS(ctx, action->iffeatures);
201 FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free);
202 FREE_ARRAY(ctx, action->groupings, lysp_grp_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100203 lysp_action_inout_free(ctx, &action->input);
204 lysp_action_inout_free(ctx, &action->output);
Radek Krejci19a96102018-11-15 13:38:09 +0100205 FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free);
206}
207
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200208void
Radek Krejci19a96102018-11-15 13:38:09 +0100209lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif)
210{
211 struct lysp_node *node, *next;
212
213 FREE_STRING(ctx, notif->name);
214 FREE_STRING(ctx, notif->dsc);
215 FREE_STRING(ctx, notif->ref);
216 FREE_STRINGS(ctx, notif->iffeatures);
217 FREE_ARRAY(ctx, notif->musts, lysp_restr_free);
218 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
219 FREE_ARRAY(ctx, notif->groupings, lysp_grp_free);
220 LY_LIST_FOR_SAFE(notif->data, next, node) {
221 lysp_node_free(ctx, node);
222 }
223 FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free);
224}
225
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200226void
Radek Krejci19a96102018-11-15 13:38:09 +0100227lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp)
228{
229 struct lysp_node *node, *next;
230
231 FREE_STRING(ctx, grp->name);
232 FREE_STRING(ctx, grp->dsc);
233 FREE_STRING(ctx, grp->ref);
234 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
235 FREE_ARRAY(ctx, grp->groupings, lysp_grp_free);
236 LY_LIST_FOR_SAFE(grp->data, next, node) {
237 lysp_node_free(ctx, node);
238 }
239 FREE_ARRAY(ctx, grp->actions, lysp_action_free);
240 FREE_ARRAY(ctx, grp->notifs, lysp_notif_free);
241 FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free);
242}
243
Radek Krejcif09e4e82019-06-14 15:08:11 +0200244void
Radek Krejci19a96102018-11-15 13:38:09 +0100245lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
246{
247 FREE_STRING(ctx, when->cond);
248 FREE_STRING(ctx, when->dsc);
249 FREE_STRING(ctx, when->ref);
250 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
251}
252
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200253void
Radek Krejci19a96102018-11-15 13:38:09 +0100254lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment)
255{
256 struct lysp_node *node, *next;
257
258 FREE_STRING(ctx, augment->nodeid);
259 FREE_STRING(ctx, augment->dsc);
260 FREE_STRING(ctx, augment->ref);
261 FREE_MEMBER(ctx, augment->when, lysp_when_free);
262 FREE_STRINGS(ctx, augment->iffeatures);
263 LY_LIST_FOR_SAFE(augment->child, next, node) {
264 lysp_node_free(ctx, node);
265 }
266 FREE_ARRAY(ctx, augment->actions, lysp_action_free);
267 FREE_ARRAY(ctx, augment->notifs, lysp_notif_free);
268 FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free);
269}
270
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200271void
Radek Krejci19a96102018-11-15 13:38:09 +0100272lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
273{
274 struct lysp_deviate_add *add = (struct lysp_deviate_add*)d;
275 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl*)d;
276
277 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
278 switch(d->mod) {
279 case LYS_DEV_NOT_SUPPORTED:
280 /* nothing to do */
281 break;
282 case LYS_DEV_ADD:
283 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
284 FREE_STRING(ctx, add->units);
285 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
286 FREE_STRINGS(ctx, add->uniques);
287 FREE_STRINGS(ctx, add->dflts);
288 break;
289 case LYS_DEV_REPLACE:
290 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
291 FREE_STRING(ctx, rpl->units);
292 FREE_STRING(ctx, rpl->dflt);
293 break;
294 default:
295 LOGINT(ctx);
296 break;
297 }
298}
299
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200300void
Radek Krejci19a96102018-11-15 13:38:09 +0100301lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
302{
303 struct lysp_deviate *next, *iter;
304
305 FREE_STRING(ctx, dev->nodeid);
306 FREE_STRING(ctx, dev->dsc);
307 FREE_STRING(ctx, dev->ref);
308 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
309 lysp_deviate_free(ctx, iter);
310 free(iter);
311 }
312 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
313}
314
David Sedlákd2d676a2019-07-22 11:28:19 +0200315void
Radek Krejci19a96102018-11-15 13:38:09 +0100316lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
317{
318 FREE_STRING(ctx, ref->nodeid);
319 FREE_STRING(ctx, ref->dsc);
320 FREE_STRING(ctx, ref->ref);
321 FREE_STRINGS(ctx, ref->iffeatures);
322 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
323 FREE_STRING(ctx, ref->presence);
324 FREE_STRINGS(ctx, ref->dflts);
325 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
326}
327
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200328void
Radek Krejci19a96102018-11-15 13:38:09 +0100329lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
330{
331 struct lysp_node *child, *next;
332
333 FREE_STRING(ctx, node->name);
334 FREE_STRING(ctx, node->dsc);
335 FREE_STRING(ctx, node->ref);
336 FREE_MEMBER(ctx, node->when, lysp_when_free);
337 FREE_STRINGS(ctx, node->iffeatures);
338 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
339
340 switch(node->nodetype) {
341 case LYS_CONTAINER:
342 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->musts, lysp_restr_free);
343 FREE_STRING(ctx, ((struct lysp_node_container*)node)->presence);
344 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->typedefs, lysp_tpdf_free);
345 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->groupings, lysp_grp_free);
346 LY_LIST_FOR_SAFE(((struct lysp_node_container*)node)->child, next, child) {
347 lysp_node_free(ctx, child);
348 }
349 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->actions, lysp_action_free);
350 FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->notifs, lysp_notif_free);
351 break;
352 case LYS_LEAF:
353 FREE_ARRAY(ctx, ((struct lysp_node_leaf*)node)->musts, lysp_restr_free);
354 lysp_type_free(ctx, &((struct lysp_node_leaf*)node)->type);
355 FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->units);
356 FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->dflt);
357 break;
358 case LYS_LEAFLIST:
359 FREE_ARRAY(ctx, ((struct lysp_node_leaflist*)node)->musts, lysp_restr_free);
360 lysp_type_free(ctx, &((struct lysp_node_leaflist*)node)->type);
361 FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->units);
362 FREE_STRINGS(ctx, ((struct lysp_node_leaflist*)node)->dflts);
363 break;
364 case LYS_LIST:
365 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->musts, lysp_restr_free);
366 FREE_STRING(ctx, ((struct lysp_node_list*)node)->key);
367 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->typedefs, lysp_tpdf_free);
368 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->groupings, lysp_grp_free);
369 LY_LIST_FOR_SAFE(((struct lysp_node_list*)node)->child, next, child) {
370 lysp_node_free(ctx, child);
371 }
372 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->actions, lysp_action_free);
373 FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->notifs, lysp_notif_free);
374 FREE_STRINGS(ctx, ((struct lysp_node_list*)node)->uniques);
375 break;
376 case LYS_CHOICE:
377 LY_LIST_FOR_SAFE(((struct lysp_node_choice*)node)->child, next, child) {
378 lysp_node_free(ctx, child);
379 }
380 FREE_STRING(ctx, ((struct lysp_node_choice*)node)->dflt);
381 break;
382 case LYS_CASE:
383 LY_LIST_FOR_SAFE(((struct lysp_node_case*)node)->child, next, child) {
384 lysp_node_free(ctx, child);
385 }
386 break;
387 case LYS_ANYDATA:
388 case LYS_ANYXML:
389 FREE_ARRAY(ctx, ((struct lysp_node_anydata*)node)->musts, lysp_restr_free);
390 break;
391 case LYS_USES:
392 FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->refines, lysp_refine_free);
393 FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->augments, lysp_augment_free);
394 break;
395 default:
396 LOGINT(ctx);
397 }
398
399 free(node);
400}
401
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100402void
403lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod)
404{
405 struct lysp_node *node, *next;
406
Radek Krejci40544fa2019-01-11 09:38:37 +0100407 if (!submod) {
408 return;
409 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100410
411 FREE_ARRAY(ctx, submod->imports, lysp_import_free);
412 FREE_ARRAY(ctx, submod->includes, lysp_include_free);
413
414 FREE_ARRAY(ctx, submod->revs, lysp_revision_free);
415 FREE_ARRAY(ctx, submod->extensions, lysp_ext_free);
416 FREE_ARRAY(ctx, submod->features, lysp_feature_free);
417 FREE_ARRAY(ctx, submod->identities, lysp_ident_free);
418 FREE_ARRAY(ctx, submod->typedefs, lysp_tpdf_free);
419 FREE_ARRAY(ctx, submod->groupings, lysp_grp_free);
420 LY_LIST_FOR_SAFE(submod->data, next, node) {
421 lysp_node_free(ctx, node);
422 }
423 FREE_ARRAY(ctx, submod->augments, lysp_augment_free);
424 FREE_ARRAY(ctx, submod->rpcs, lysp_action_free);
425 FREE_ARRAY(ctx, submod->notifs, lysp_notif_free);
426 FREE_ARRAY(ctx, submod->deviations, lysp_deviation_free);
427 FREE_ARRAY(ctx, submod->exts, lysp_ext_instance_free);
428
429 FREE_STRING(ctx, submod->belongsto);
430 FREE_STRING(ctx, submod->name);
431 FREE_STRING(ctx, submod->filepath);
432 FREE_STRING(ctx, submod->prefix);
433 FREE_STRING(ctx, submod->org);
434 FREE_STRING(ctx, submod->contact);
435 FREE_STRING(ctx, submod->dsc);
436 FREE_STRING(ctx, submod->ref);
437
438 free(submod);
439}
440
Radek Krejci19a96102018-11-15 13:38:09 +0100441API void
442lysp_module_free(struct lysp_module *module)
443{
444 struct ly_ctx *ctx;
445 struct lysp_node *node, *next;
446
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100447 if (!module) {
448 return;
449 }
450 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100451
452 FREE_ARRAY(ctx, module->imports, lysp_import_free);
453 FREE_ARRAY(ctx, module->includes, lysp_include_free);
454
Radek Krejci19a96102018-11-15 13:38:09 +0100455 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
456 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
457 FREE_ARRAY(ctx, module->features, lysp_feature_free);
458 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
459 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
460 FREE_ARRAY(ctx, module->groupings, lysp_grp_free);
461 LY_LIST_FOR_SAFE(module->data, next, node) {
462 lysp_node_free(ctx, node);
463 }
464 FREE_ARRAY(ctx, module->augments, lysp_augment_free);
465 FREE_ARRAY(ctx, module->rpcs, lysp_action_free);
466 FREE_ARRAY(ctx, module->notifs, lysp_notif_free);
467 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
468 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
469
470 free(module);
471}
472
Radek Krejci0af46292019-01-11 16:02:31 +0100473void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100474lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
475{
476 if (--(*ext)->refcount) {
477 return;
478 }
479 FREE_STRING(ctx, (*ext)->name);
480 FREE_STRING(ctx, (*ext)->argument);
481 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
482 free(*ext);
483}
484
485void
Radek Krejci19a96102018-11-15 13:38:09 +0100486lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
487{
fredganebc50572019-10-31 15:39:23 +0800488 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200489 ext->def->plugin->free(ctx, ext);
490 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100491 if (ext->def) {
492 lysc_extension_free(ctx, &ext->def);
493 }
Radek Krejci0935f412019-08-20 16:15:18 +0200494 FREE_STRING(ctx, ext->argument);
495 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
496}
497
498void
Radek Krejci19a96102018-11-15 13:38:09 +0100499lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
500{
501 LY_ARRAY_FREE(iff->features);
502 free(iff->expr);
503}
504
505static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100506lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100507{
Radek Krejci00b874b2019-02-12 10:54:50 +0100508 if (--(*w)->refcount) {
509 return;
510 }
511 lyxp_expr_free(ctx, (*w)->cond);
512 FREE_STRING(ctx, (*w)->dsc);
513 FREE_STRING(ctx, (*w)->ref);
514 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
515 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100516}
517
Radek Krejciccd20f12019-02-15 14:12:27 +0100518void
Radek Krejci58d171e2018-11-23 13:50:55 +0100519lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
520{
521 lyxp_expr_free(ctx, must->cond);
522 FREE_STRING(ctx, must->emsg);
523 FREE_STRING(ctx, must->eapptag);
Radek Krejcic8b31002019-01-08 10:24:45 +0100524 FREE_STRING(ctx, must->dsc);
525 FREE_STRING(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100526 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
527}
528
529static void
Radek Krejci19a96102018-11-15 13:38:09 +0100530lysc_import_free(struct ly_ctx *ctx, struct lysc_import *import)
531{
532 /* imported module is freed directly from the context's list */
533 FREE_STRING(ctx, import->prefix);
534 FREE_ARRAY(ctx, import->exts, lysc_ext_instance_free);
535}
536
537static void
538lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
539{
540 FREE_STRING(ctx, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100541 FREE_STRING(ctx, ident->dsc);
542 FREE_STRING(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100543 FREE_ARRAY(ctx, ident->iffeatures, lysc_iffeature_free);
544 LY_ARRAY_FREE(ident->derived);
545 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
546}
547
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200548void
Radek Krejci19a96102018-11-15 13:38:09 +0100549lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat)
550{
551 FREE_STRING(ctx, feat->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100552 FREE_STRING(ctx, feat->dsc);
553 FREE_STRING(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100554 FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free);
555 LY_ARRAY_FREE(feat->depfeatures);
556 FREE_ARRAY(ctx, feat->exts, lysc_ext_instance_free);
557}
558
559static void
560lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
561{
562 LY_ARRAY_FREE(range->parts);
563 FREE_STRING(ctx, range->eapptag);
564 FREE_STRING(ctx, range->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100565 FREE_STRING(ctx, range->dsc);
566 FREE_STRING(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100567 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
568}
569
570static void
571lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
572{
573 if (--(*pattern)->refcount) {
574 return;
575 }
Radek Krejci54579462019-04-30 12:47:06 +0200576 pcre2_code_free((*pattern)->code);
577 FREE_STRING(ctx, (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +0100578 FREE_STRING(ctx, (*pattern)->eapptag);
579 FREE_STRING(ctx, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100580 FREE_STRING(ctx, (*pattern)->dsc);
581 FREE_STRING(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100582 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
583 free(*pattern);
584}
585
586static void
Radek Krejci693262f2019-04-29 15:23:20 +0200587lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100588{
589 FREE_STRING(ctx, item->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100590 FREE_STRING(ctx, item->dsc);
591 FREE_STRING(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100592 FREE_ARRAY(ctx, item->iffeatures, lysc_iffeature_free);
593 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
594}
595
Radek Krejcia3045382018-11-22 14:30:31 +0100596static void
597lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
598{
599 lysc_type_free(ctx, *type);
600}
Radek Krejcicdfecd92018-11-26 11:27:32 +0100601void
Radek Krejci19a96102018-11-15 13:38:09 +0100602lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
603{
604 if (--type->refcount) {
605 return;
606 }
607 switch(type->basetype) {
608 case LY_TYPE_BINARY:
609 FREE_MEMBER(ctx, ((struct lysc_type_bin*)type)->length, lysc_range_free);
610 break;
611 case LY_TYPE_BITS:
Radek Krejci693262f2019-04-29 15:23:20 +0200612 FREE_ARRAY(ctx, (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)type)->bits, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100613 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100614 case LY_TYPE_DEC64:
615 FREE_MEMBER(ctx, ((struct lysc_type_dec*)type)->range, lysc_range_free);
616 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100617 case LY_TYPE_STRING:
618 FREE_MEMBER(ctx, ((struct lysc_type_str*)type)->length, lysc_range_free);
619 FREE_ARRAY(ctx, ((struct lysc_type_str*)type)->patterns, lysc_pattern_free);
620 break;
621 case LY_TYPE_ENUM:
622 FREE_ARRAY(ctx, ((struct lysc_type_enum*)type)->enums, lysc_enum_item_free);
623 break;
624 case LY_TYPE_INT8:
625 case LY_TYPE_UINT8:
626 case LY_TYPE_INT16:
627 case LY_TYPE_UINT16:
628 case LY_TYPE_INT32:
629 case LY_TYPE_UINT32:
630 case LY_TYPE_INT64:
631 case LY_TYPE_UINT64:
632 FREE_MEMBER(ctx, ((struct lysc_type_num*)type)->range, lysc_range_free);
633 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100634 case LY_TYPE_IDENT:
635 LY_ARRAY_FREE(((struct lysc_type_identityref*)type)->bases);
636 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100637 case LY_TYPE_UNION:
638 FREE_ARRAY(ctx, ((struct lysc_type_union*)type)->types, lysc_type2_free);
639 break;
640 case LY_TYPE_LEAFREF:
641 FREE_STRING(ctx, ((struct lysc_type_leafref*)type)->path);
642 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100643 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100644 case LY_TYPE_BOOL:
645 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100646 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100647 /* nothing to do */
648 break;
649 }
650 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejcia1911222019-07-22 17:24:50 +0200651 if (type->dflt) {
652 type->plugin->free(ctx, type->dflt);
653 lysc_type_free(ctx, type->dflt->realtype);
654 free(type->dflt);
655 }
Radek Krejci19a96102018-11-15 13:38:09 +0100656 free(type);
657}
658
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100659void
Radek Krejcif538ce52019-03-05 10:46:14 +0100660lysc_action_inout_free(struct ly_ctx *ctx, struct lysc_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100661{
662 struct lysc_node *child, *child_next;
663
Radek Krejcif538ce52019-03-05 10:46:14 +0100664 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
665 LY_LIST_FOR_SAFE(inout->data, child_next, child) {
666 lysc_node_free(ctx, child);
667 }
668}
669
670void
671lysc_action_free(struct ly_ctx *ctx, struct lysc_action *action)
672{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100673 FREE_STRING(ctx, action->name);
674 FREE_STRING(ctx, action->dsc);
675 FREE_STRING(ctx, action->ref);
676 FREE_ARRAY(ctx, action->iffeatures, lysc_iffeature_free);
677 FREE_ARRAY(ctx, action->exts, lysc_ext_instance_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200678 FREE_ARRAY(ctx, action->input_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100679 lysc_action_inout_free(ctx, &action->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200680 FREE_ARRAY(ctx, action->output_exts, lysc_ext_instance_free);
Radek Krejcif538ce52019-03-05 10:46:14 +0100681 lysc_action_inout_free(ctx, &action->output);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100682}
683
Radek Krejcifc11bd72019-04-11 16:00:05 +0200684void
685lysc_notif_free(struct ly_ctx *ctx, struct lysc_notif *notif)
686{
687 struct lysc_node *child, *child_next;
688
689 FREE_STRING(ctx, notif->name);
690 FREE_STRING(ctx, notif->dsc);
691 FREE_STRING(ctx, notif->ref);
692 FREE_ARRAY(ctx, notif->iffeatures, lysc_iffeature_free);
693 FREE_ARRAY(ctx, notif->exts, lysc_ext_instance_free);
694 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
695 LY_LIST_FOR_SAFE(notif->data, child_next, child) {
696 lysc_node_free(ctx, child);
697 }
698}
699
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200700void
Radek Krejci19a96102018-11-15 13:38:09 +0100701lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
702{
703 struct lysc_node *child, *child_next;
704
705 LY_LIST_FOR_SAFE(node->child, child_next, child) {
706 lysc_node_free(ctx, child);
707 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100708 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100709 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200710 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100711}
712
713static void
714lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
715{
Radek Krejci58d171e2018-11-23 13:50:55 +0100716 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100717 if (node->type) {
718 lysc_type_free(ctx, node->type);
719 }
Radek Krejci58d171e2018-11-23 13:50:55 +0100720 FREE_STRING(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200721 if (node->dflt) {
722 node->dflt->realtype->plugin->free(ctx, node->dflt);
723 lysc_type_free(ctx, node->dflt->realtype);
724 free(node->dflt);
725 }
Radek Krejci19a96102018-11-15 13:38:09 +0100726}
727
Radek Krejci42452ac2018-11-28 17:09:52 +0100728static void
729lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
730{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200731 LY_ARRAY_SIZE_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100732
Radek Krejci42452ac2018-11-28 17:09:52 +0100733 FREE_ARRAY(ctx, node->musts, lysc_must_free);
734 if (node->type) {
735 lysc_type_free(ctx, node->type);
736 }
737 FREE_STRING(ctx, node->units);
738 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200739 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
740 lysc_type_free(ctx, node->dflts[u]->realtype);
741 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100742 }
743 LY_ARRAY_FREE(node->dflts);
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200744 LY_ARRAY_FREE(node->dflts_mods);
Radek Krejci42452ac2018-11-28 17:09:52 +0100745}
746
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100747static void
748lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
749{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200750 LY_ARRAY_SIZE_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100751 struct lysc_node *child, *child_next;
752
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100753 LY_LIST_FOR_SAFE(node->child, child_next, child) {
754 lysc_node_free(ctx, child);
755 }
756 FREE_ARRAY(ctx, node->musts, lysc_must_free);
757
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100758 LY_ARRAY_FOR(node->uniques, u) {
759 LY_ARRAY_FREE(node->uniques[u]);
760 }
761 LY_ARRAY_FREE(node->uniques);
762
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100763 FREE_ARRAY(ctx, node->actions, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200764 FREE_ARRAY(ctx, node->notifs, lysc_notif_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100765}
766
Radek Krejci056d0a82018-12-06 16:57:25 +0100767static void
768lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
769{
770 struct lysc_node *child, *child_next;
771
Radek Krejci056d0a82018-12-06 16:57:25 +0100772 if (node->cases) {
773 LY_LIST_FOR_SAFE(node->cases->child, child_next, child) {
774 lysc_node_free(ctx, child);
775 }
776 LY_LIST_FOR_SAFE((struct lysc_node*)node->cases, child_next, child) {
777 lysc_node_free(ctx, child);
778 }
779 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100780}
Radek Krejci056d0a82018-12-06 16:57:25 +0100781
Radek Krejci9800fb82018-12-13 14:26:23 +0100782static void
783lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
784{
785 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100786}
787
Radek Krejci19a96102018-11-15 13:38:09 +0100788void
789lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node)
790{
791 /* common part */
792 FREE_STRING(ctx, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +0100793 FREE_STRING(ctx, node->dsc);
794 FREE_STRING(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100795
796 /* nodetype-specific part */
797 switch(node->nodetype) {
798 case LYS_CONTAINER:
799 lysc_node_container_free(ctx, (struct lysc_node_container*)node);
800 break;
801 case LYS_LEAF:
802 lysc_node_leaf_free(ctx, (struct lysc_node_leaf*)node);
803 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100804 case LYS_LEAFLIST:
805 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist*)node);
806 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100807 case LYS_LIST:
808 lysc_node_list_free(ctx, (struct lysc_node_list*)node);
809 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100810 case LYS_CHOICE:
811 lysc_node_choice_free(ctx, (struct lysc_node_choice*)node);
812 break;
813 case LYS_CASE:
814 /* nothing specific */
815 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100816 case LYS_ANYDATA:
817 case LYS_ANYXML:
818 lysc_node_anydata_free(ctx, (struct lysc_node_anydata*)node);
819 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100820 default:
821 LOGINT(ctx);
822 }
823
Radek Krejci00b874b2019-02-12 10:54:50 +0100824 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100825 FREE_ARRAY(ctx, node->iffeatures, lysc_iffeature_free);
826 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100827 free(node);
828}
829
830static void
831lysc_module_free_(struct lysc_module *module)
832{
833 struct ly_ctx *ctx;
834 struct lysc_node *node, *node_next;
835
836 LY_CHECK_ARG_RET(NULL, module,);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100837 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100838
Radek Krejci19a96102018-11-15 13:38:09 +0100839 FREE_ARRAY(ctx, module->imports, lysc_import_free);
840 FREE_ARRAY(ctx, module->features, lysc_feature_free);
841 FREE_ARRAY(ctx, module->identities, lysc_ident_free);
842
843 LY_LIST_FOR_SAFE(module->data, node_next, node) {
844 lysc_node_free(ctx, node);
845 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100846 FREE_ARRAY(ctx, module->rpcs, lysc_action_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200847 FREE_ARRAY(ctx, module->notifs, lysc_notif_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100848 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
Michal Vasko57c10cd2020-05-27 15:57:11 +0200849 LY_ARRAY_FREE(module->deviated_by);
Radek Krejci19a96102018-11-15 13:38:09 +0100850
851 free(module);
852}
853
854void
855lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
856{
Radek Krejci9b042892019-02-13 14:28:44 +0100857 /* TODO use the destructor, this just suppress warning about unused parameter */
858 (void) private_destructor;
859
Radek Krejci19a96102018-11-15 13:38:09 +0100860 if (module) {
861 lysc_module_free_(module);
862 }
863}
864
865void
866lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv))
867{
868 if (!module) {
869 return;
870 }
871
872 lysc_module_free(module->compiled, private_destructor);
Radek Krejci0af46292019-01-11 16:02:31 +0100873 FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100874 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100875
876 FREE_STRING(module->ctx, module->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100877 FREE_STRING(module->ctx, module->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100878 FREE_STRING(module->ctx, module->ns);
879 FREE_STRING(module->ctx, module->prefix);
880 FREE_STRING(module->ctx, module->filepath);
881 FREE_STRING(module->ctx, module->org);
882 FREE_STRING(module->ctx, module->contact);
883 FREE_STRING(module->ctx, module->dsc);
884 FREE_STRING(module->ctx, module->ref);
885
Radek Krejci19a96102018-11-15 13:38:09 +0100886 free(module);
887}
Radek Krejci38d85362019-09-05 16:26:38 +0200888API void
889lysc_extension_instance_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
890{
891 for (unsigned int u = 0; substmts[u].stmt; ++u) {
892 if (!substmts[u].storage) {
893 continue;
894 }
895
896 switch(substmts[u].stmt) {
897 case LY_STMT_TYPE:
898 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
899 /* single item */
900 struct lysc_type *type = *((struct lysc_type**)substmts[u].storage);
901 if (!type) {
902 break;
903 }
904 lysc_type_free(ctx, type);
905 } else {
906 /* multiple items */
907 struct lysc_type **types = *((struct lysc_type***)substmts[u].storage);
908 if (!types) {
909 break;
910 }
911 FREE_ARRAY(ctx, types, lysc_type2_free);
912 }
913 break;
914 case LY_STMT_UNITS:
915 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
916 /* single item */
917 const char *str = *((const char**)substmts[u].storage);
918 if (!str) {
919 break;
920 }
921 FREE_STRING(ctx, str);
922 } else {
923 /* multiple items */
924 const char **strs = *((const char***)substmts[u].storage);
925 if (!strs) {
926 break;
927 }
928 FREE_STRINGS(ctx, strs);
929 }
930 break;
931 case LY_STMT_STATUS:
Radek Krejciad5963b2019-09-06 16:03:05 +0200932 case LY_STMT_CONFIG:
Radek Krejci38d85362019-09-05 16:26:38 +0200933 /* nothing to do */
934 break;
935 case LY_STMT_IF_FEATURE: {
936 struct lysc_iffeature *iff = *((struct lysc_iffeature**)substmts[u].storage);
937 if (!iff) {
938 break;
939 }
940 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
941 /* single item */
942 lysc_iffeature_free(ctx, iff);
943 free(iff);
944 } else {
945 /* multiple items */
946 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
947 }
948 break;
949 }
950
951 /* TODO other statements */
952 default:
953 LOGINT(ctx);
954 }
955 }
956}
David Sedlákebd3acf2019-07-26 15:04:32 +0200957
958void
Michal Vaskob36053d2020-03-26 15:49:30 +0100959yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200960{
961 if (ctx) {
962 free(ctx);
963 }
964}
965
966void
Michal Vaskob36053d2020-03-26 15:49:30 +0100967yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +0200968{
969 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100970 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +0200971 free(ctx);
972 }
973}