Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [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 | */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 14 | #define _DEFAULT_SOURCE |
| 15 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 16 | #include <ctype.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <linux/limits.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 25 | |
| 26 | #include "libyang.h" |
| 27 | #include "common.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 28 | #include "context.h" |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 29 | #include "tree_schema_internal.h" |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 30 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 31 | #define FREE_ARRAY(CTX, ARRAY, FUNC) {uint64_t c__; LY_ARRAY_FOR(ARRAY, c__){FUNC(CTX, &ARRAY[c__], dict);}LY_ARRAY_FREE(ARRAY);} |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 32 | #define FREE_MEMBER(CTX, MEMBER, FUNC) if (MEMBER) {FUNC(CTX, MEMBER, dict);free(MEMBER);} |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 33 | #define FREE_STRING(CTX, STRING, DICT) if (DICT && STRING) {lydict_remove(CTX, STRING);} |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 34 | #define FREE_STRINGS(CTX, ARRAY, DICT) {uint64_t c__; LY_ARRAY_FOR(ARRAY, c__){FREE_STRING(CTX, ARRAY[c__], DICT);}LY_ARRAY_FREE(ARRAY);} |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 35 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 36 | static void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp, int dict); |
| 37 | static void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node, int dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 38 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 39 | #define LYSC_CTX_BUFSIZE 4086 |
| 40 | struct lysc_ctx { |
| 41 | struct lysc_module *mod; |
| 42 | uint16_t path_len; |
| 43 | char path[LYSC_CTX_BUFSIZE]; |
| 44 | }; |
| 45 | |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 46 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 47 | lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 48 | { |
| 49 | struct lysp_stmt *child, *next; |
| 50 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 51 | FREE_STRING(ctx, stmt->stmt, dict); |
| 52 | FREE_STRING(ctx, stmt->arg, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 53 | |
| 54 | LY_LIST_FOR_SAFE(stmt->child, next, child) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 55 | lysp_stmt_free(ctx, child, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | free(stmt); |
| 59 | } |
| 60 | |
| 61 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 62 | lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 63 | { |
| 64 | struct lysp_stmt *stmt, *next; |
| 65 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 66 | FREE_STRING(ctx, ext->name, dict); |
| 67 | FREE_STRING(ctx, ext->argument, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 68 | |
| 69 | LY_LIST_FOR_SAFE(ext->child, next, stmt) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 70 | lysp_stmt_free(ctx, stmt, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 75 | lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 76 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 77 | FREE_STRING(ctx, import->name, dict); |
| 78 | FREE_STRING(ctx, import->prefix, dict); |
| 79 | FREE_STRING(ctx, import->dsc, dict); |
| 80 | FREE_STRING(ctx, import->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 81 | FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 85 | lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 86 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 87 | FREE_STRING(ctx, include->name, dict); |
| 88 | FREE_STRING(ctx, include->dsc, dict); |
| 89 | FREE_STRING(ctx, include->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 90 | FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 94 | lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 95 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 96 | FREE_STRING(ctx, rev->dsc, dict); |
| 97 | FREE_STRING(ctx, rev->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 98 | FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 102 | lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 103 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 104 | FREE_STRING(ctx, ext->name, dict); |
| 105 | FREE_STRING(ctx, ext->argument, dict); |
| 106 | FREE_STRING(ctx, ext->dsc, dict); |
| 107 | FREE_STRING(ctx, ext->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 108 | FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 112 | lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 113 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 114 | FREE_STRING(ctx, feat->name, dict); |
| 115 | FREE_STRINGS(ctx, feat->iffeatures, 1); |
| 116 | FREE_STRING(ctx, feat->dsc, dict); |
| 117 | FREE_STRING(ctx, feat->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 118 | FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 122 | lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 123 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 124 | FREE_STRING(ctx, ident->name, dict); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 125 | FREE_STRINGS(ctx, ident->iffeatures, 1); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 126 | FREE_STRINGS(ctx, ident->bases, dict); |
| 127 | FREE_STRING(ctx, ident->dsc, dict); |
| 128 | FREE_STRING(ctx, ident->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 129 | FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 133 | lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 134 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 135 | FREE_STRING(ctx, restr->arg, dict); |
| 136 | FREE_STRING(ctx, restr->emsg, dict); |
| 137 | FREE_STRING(ctx, restr->eapptag, dict); |
| 138 | FREE_STRING(ctx, restr->dsc, dict); |
| 139 | FREE_STRING(ctx, restr->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 140 | FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 144 | lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 145 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 146 | FREE_STRING(ctx, item->name, dict); |
| 147 | FREE_STRING(ctx, item->dsc, dict); |
| 148 | FREE_STRING(ctx, item->ref, dict); |
| 149 | FREE_STRINGS(ctx, item->iffeatures, 1); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 150 | FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 154 | lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 155 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 156 | FREE_STRING(ctx, type->name, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 157 | FREE_MEMBER(ctx, type->range, lysp_restr_free); |
| 158 | FREE_MEMBER(ctx, type->length, lysp_restr_free); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 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); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 162 | FREE_STRING(ctx, type->path, dict); |
| 163 | FREE_STRINGS(ctx, type->bases, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 164 | FREE_ARRAY(ctx, type->types, lysp_type_free); |
| 165 | FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 169 | lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 170 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 171 | FREE_STRING(ctx, tpdf->name, dict); |
| 172 | FREE_STRING(ctx, tpdf->units, dict); |
| 173 | FREE_STRING(ctx, tpdf->dflt, dict); |
| 174 | FREE_STRING(ctx, tpdf->dsc, dict); |
| 175 | FREE_STRING(ctx, tpdf->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 176 | FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 177 | lysp_type_free(ctx, &tpdf->type, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 181 | lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 182 | { |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 183 | struct lysp_node *node, *next; |
| 184 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 185 | FREE_ARRAY(ctx, inout->musts, lysp_restr_free); |
| 186 | FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free); |
| 187 | FREE_ARRAY(ctx, inout->groupings, lysp_grp_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 188 | LY_LIST_FOR_SAFE(inout->data, next, node) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 189 | lysp_node_free(ctx, node, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 190 | } |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 191 | FREE_ARRAY(ctx, inout->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 192 | |
| 193 | } |
| 194 | |
| 195 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 196 | lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 197 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 198 | FREE_STRING(ctx, action->name, dict); |
| 199 | FREE_STRING(ctx, action->dsc, dict); |
| 200 | FREE_STRING(ctx, action->ref, dict); |
| 201 | FREE_STRINGS(ctx, action->iffeatures, 1); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 202 | FREE_ARRAY(ctx, action->typedefs, lysp_tpdf_free); |
| 203 | FREE_ARRAY(ctx, action->groupings, lysp_grp_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 204 | FREE_MEMBER(ctx, action->input, lysp_action_inout_free); |
| 205 | FREE_MEMBER(ctx, action->output, lysp_action_inout_free); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 206 | FREE_ARRAY(ctx, action->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 210 | lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 211 | { |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 212 | struct lysp_node *node, *next; |
| 213 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 214 | FREE_STRING(ctx, notif->name, dict); |
| 215 | FREE_STRING(ctx, notif->dsc, dict); |
| 216 | FREE_STRING(ctx, notif->ref, dict); |
| 217 | FREE_STRINGS(ctx, notif->iffeatures, 1); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 218 | FREE_ARRAY(ctx, notif->musts, lysp_restr_free); |
| 219 | FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free); |
| 220 | FREE_ARRAY(ctx, notif->groupings, lysp_grp_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 221 | LY_LIST_FOR_SAFE(notif->data, next, node) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 222 | lysp_node_free(ctx, node, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 223 | } |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 224 | FREE_ARRAY(ctx, notif->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 228 | lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 229 | { |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 230 | struct lysp_node *node, *next; |
| 231 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 232 | FREE_STRING(ctx, grp->name, dict); |
| 233 | FREE_STRING(ctx, grp->dsc, dict); |
| 234 | FREE_STRING(ctx, grp->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 235 | FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free); |
| 236 | FREE_ARRAY(ctx, grp->groupings, lysp_grp_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 237 | LY_LIST_FOR_SAFE(grp->data, next, node) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 238 | lysp_node_free(ctx, node, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 239 | } |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 240 | FREE_ARRAY(ctx, grp->actions, lysp_action_free); |
| 241 | FREE_ARRAY(ctx, grp->notifs, lysp_notif_free); |
| 242 | FREE_ARRAY(ctx, grp->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 246 | lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 247 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 248 | FREE_STRING(ctx, when->cond, dict); |
| 249 | FREE_STRING(ctx, when->dsc, dict); |
| 250 | FREE_STRING(ctx, when->ref, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 251 | FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 255 | lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 256 | { |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 257 | struct lysp_node *node, *next; |
| 258 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 259 | FREE_STRING(ctx, augment->nodeid, dict); |
| 260 | FREE_STRING(ctx, augment->dsc, dict); |
| 261 | FREE_STRING(ctx, augment->ref, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 262 | FREE_MEMBER(ctx, augment->when, lysp_when_free); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 263 | FREE_STRINGS(ctx, augment->iffeatures, 1); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 264 | LY_LIST_FOR_SAFE(augment->child, next, node) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 265 | lysp_node_free(ctx, node, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 266 | } |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 267 | FREE_ARRAY(ctx, augment->actions, lysp_action_free); |
| 268 | FREE_ARRAY(ctx, augment->notifs, lysp_notif_free); |
| 269 | FREE_ARRAY(ctx, augment->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 273 | lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 274 | { |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 275 | struct lysp_deviate_add *add = (struct lysp_deviate_add*)d; |
| 276 | struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl*)d; |
| 277 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 278 | FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 279 | switch(d->mod) { |
| 280 | case LYS_DEV_NOT_SUPPORTED: |
| 281 | /* nothing to do */ |
| 282 | break; |
| 283 | case LYS_DEV_ADD: |
| 284 | case LYS_DEV_DELETE: /* compatible for dynamically allocated data */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 285 | FREE_STRING(ctx, add->units, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 286 | FREE_ARRAY(ctx, add->musts, lysp_restr_free); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 287 | FREE_STRINGS(ctx, add->uniques, dict); |
| 288 | FREE_STRINGS(ctx, add->dflts, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 289 | break; |
| 290 | case LYS_DEV_REPLACE: |
| 291 | FREE_MEMBER(ctx, rpl->type, lysp_type_free); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 292 | FREE_STRING(ctx, rpl->units, dict); |
| 293 | FREE_STRING(ctx, rpl->dflt, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 294 | break; |
| 295 | default: |
| 296 | LOGINT(ctx); |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 302 | lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 303 | { |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 304 | struct lysp_deviate *next, *iter; |
| 305 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 306 | FREE_STRING(ctx, dev->nodeid, dict); |
| 307 | FREE_STRING(ctx, dev->dsc, dict); |
| 308 | FREE_STRING(ctx, dev->ref, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 309 | LY_LIST_FOR_SAFE(dev->deviates, next, iter) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 310 | lysp_deviate_free(ctx, iter, dict); |
Michal Vasko | 8447f6a | 2018-10-15 10:56:16 +0200 | [diff] [blame] | 311 | free(iter); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 312 | } |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 313 | FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 317 | lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 318 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 319 | FREE_STRING(ctx, ref->nodeid, dict); |
| 320 | FREE_STRING(ctx, ref->dsc, dict); |
| 321 | FREE_STRING(ctx, ref->ref, dict); |
| 322 | FREE_STRINGS(ctx, ref->iffeatures, 1); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 323 | FREE_ARRAY(ctx, ref->musts, lysp_restr_free); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 324 | FREE_STRING(ctx, ref->presence, dict); |
| 325 | FREE_STRINGS(ctx, ref->dflts, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 326 | FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 330 | lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node, int dict) |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 331 | { |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 332 | struct lysp_node *child, *next; |
| 333 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 334 | FREE_STRING(ctx, node->name, dict); |
| 335 | FREE_STRING(ctx, node->dsc, dict); |
| 336 | FREE_STRING(ctx, node->ref, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 337 | FREE_MEMBER(ctx, node->when, lysp_when_free); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 338 | FREE_STRINGS(ctx, node->iffeatures, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 339 | FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 340 | |
| 341 | switch(node->nodetype) { |
| 342 | case LYS_CONTAINER: |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 343 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->musts, lysp_restr_free); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 344 | FREE_STRING(ctx, ((struct lysp_node_container*)node)->presence, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 345 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->typedefs, lysp_tpdf_free); |
| 346 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->groupings, lysp_grp_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 347 | LY_LIST_FOR_SAFE(((struct lysp_node_container*)node)->child, next, child) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 348 | lysp_node_free(ctx, child, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 349 | } |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 350 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->actions, lysp_action_free); |
| 351 | FREE_ARRAY(ctx, ((struct lysp_node_container*)node)->notifs, lysp_notif_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 352 | break; |
| 353 | case LYS_LEAF: |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 354 | FREE_ARRAY(ctx, ((struct lysp_node_leaf*)node)->musts, lysp_restr_free); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 355 | lysp_type_free(ctx, &((struct lysp_node_leaf*)node)->type, dict); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 356 | FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->units, dict); |
| 357 | FREE_STRING(ctx, ((struct lysp_node_leaf*)node)->dflt, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 358 | break; |
| 359 | case LYS_LEAFLIST: |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 360 | FREE_ARRAY(ctx, ((struct lysp_node_leaflist*)node)->musts, lysp_restr_free); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 361 | lysp_type_free(ctx, &((struct lysp_node_leaflist*)node)->type, dict); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 362 | FREE_STRING(ctx, ((struct lysp_node_leaflist*)node)->units, dict); |
| 363 | FREE_STRINGS(ctx, ((struct lysp_node_leaflist*)node)->dflts, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 364 | break; |
| 365 | case LYS_LIST: |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 366 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->musts, lysp_restr_free); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 367 | FREE_STRING(ctx, ((struct lysp_node_list*)node)->key, dict); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 368 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->typedefs, lysp_tpdf_free); |
| 369 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->groupings, lysp_grp_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 370 | LY_LIST_FOR_SAFE(((struct lysp_node_list*)node)->child, next, child) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 371 | lysp_node_free(ctx, child, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 372 | } |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 373 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->actions, lysp_action_free); |
| 374 | FREE_ARRAY(ctx, ((struct lysp_node_list*)node)->notifs, lysp_notif_free); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 375 | FREE_STRINGS(ctx, ((struct lysp_node_list*)node)->uniques, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 376 | break; |
| 377 | case LYS_CHOICE: |
| 378 | LY_LIST_FOR_SAFE(((struct lysp_node_choice*)node)->child, next, child) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 379 | lysp_node_free(ctx, child, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 380 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 381 | FREE_STRING(ctx, ((struct lysp_node_choice*)node)->dflt, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 382 | break; |
| 383 | case LYS_CASE: |
| 384 | LY_LIST_FOR_SAFE(((struct lysp_node_case*)node)->child, next, child) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 385 | lysp_node_free(ctx, child, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 386 | } |
| 387 | break; |
| 388 | case LYS_ANYDATA: |
| 389 | case LYS_ANYXML: |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 390 | FREE_ARRAY(ctx, ((struct lysp_node_anydata*)node)->musts, lysp_restr_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 391 | break; |
| 392 | case LYS_USES: |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 393 | FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->refines, lysp_refine_free); |
| 394 | FREE_ARRAY(ctx, ((struct lysp_node_uses*)node)->augments, lysp_augment_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 395 | break; |
| 396 | default: |
| 397 | LOGINT(ctx); |
| 398 | } |
| 399 | |
| 400 | free(node); |
| 401 | } |
| 402 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 403 | static void |
| 404 | lysp_module_free_(struct lysp_module *module, int dict) |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 405 | { |
| 406 | struct ly_ctx *ctx; |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 407 | struct lysp_node *node, *next; |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 408 | |
| 409 | LY_CHECK_ARG_RET(NULL, module,); |
| 410 | ctx = module->ctx; |
| 411 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 412 | FREE_STRING(ctx, module->name, dict); |
| 413 | FREE_STRING(ctx, module->filepath, dict); |
| 414 | FREE_STRING(ctx, module->ns, dict); /* or belongs-to */ |
| 415 | FREE_STRING(ctx, module->prefix, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 416 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 417 | FREE_ARRAY(ctx, module->imports, lysp_import_free); |
| 418 | FREE_ARRAY(ctx, module->includes, lysp_include_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 419 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 420 | FREE_STRING(ctx, module->org, dict); |
| 421 | FREE_STRING(ctx, module->contact, dict); |
| 422 | FREE_STRING(ctx, module->dsc, dict); |
| 423 | FREE_STRING(ctx, module->ref, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 424 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 425 | FREE_ARRAY(ctx, module->revs, lysp_revision_free); |
| 426 | FREE_ARRAY(ctx, module->extensions, lysp_ext_free); |
| 427 | FREE_ARRAY(ctx, module->features, lysp_feature_free); |
| 428 | FREE_ARRAY(ctx, module->identities, lysp_ident_free); |
| 429 | FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free); |
| 430 | FREE_ARRAY(ctx, module->groupings, lysp_grp_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 431 | LY_LIST_FOR_SAFE(module->data, next, node) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 432 | lysp_node_free(ctx, node, dict); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 433 | } |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 434 | FREE_ARRAY(ctx, module->augments, lysp_augment_free); |
| 435 | FREE_ARRAY(ctx, module->rpcs, lysp_action_free); |
| 436 | FREE_ARRAY(ctx, module->notifs, lysp_notif_free); |
| 437 | FREE_ARRAY(ctx, module->deviations, lysp_deviation_free); |
| 438 | FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free); |
Radek Krejci | 6f7feb6 | 2018-10-12 15:23:02 +0200 | [diff] [blame] | 439 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 440 | free(module); |
| 441 | } |
| 442 | |
| 443 | API void |
| 444 | lysp_module_free(struct lysp_module *module) |
| 445 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 446 | if (module) { |
| 447 | lysp_module_free_(module, 1); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static void |
| 452 | lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff, int UNUSED(dict)) |
| 453 | { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 454 | LY_ARRAY_FREE(iff->features); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 455 | free(iff->expr); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static void |
| 459 | lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat, int dict) |
| 460 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 461 | FREE_STRING(ctx, feat->name, dict); |
| 462 | FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 463 | LY_ARRAY_FREE(feat->depfeatures); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static void |
| 467 | lysc_module_free_(struct lysc_module *module, int dict) |
| 468 | { |
| 469 | struct ly_ctx *ctx; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 470 | |
| 471 | LY_CHECK_ARG_RET(NULL, module,); |
| 472 | ctx = module->ctx; |
| 473 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 474 | FREE_STRING(ctx, module->name, dict); |
| 475 | FREE_STRING(ctx, module->ns, dict); |
| 476 | FREE_STRING(ctx, module->prefix, dict); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 477 | |
| 478 | |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 479 | FREE_ARRAY(ctx, module->features, lysc_feature_free); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 480 | |
Radek Krejci | 3f5e3db | 2018-10-11 15:57:47 +0200 | [diff] [blame] | 481 | |
| 482 | free(module); |
| 483 | } |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 484 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 485 | API void |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 486 | lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv)) |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 487 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 488 | if (module) { |
| 489 | lysc_module_free_(module, 1); |
| 490 | } |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 491 | } |
| 492 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 493 | void |
| 494 | lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv)) |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 495 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 496 | if (!module) { |
| 497 | return; |
| 498 | } |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 499 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 500 | lysc_module_free(module->compiled, private_destructor); |
| 501 | lysp_module_free(module->parsed); |
| 502 | free(module); |
| 503 | } |
| 504 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 505 | struct iff_stack { |
| 506 | int size; |
| 507 | int index; /* first empty item */ |
| 508 | uint8_t *stack; |
| 509 | }; |
| 510 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 511 | static LY_ERR |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 512 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 513 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 514 | if (stack->index == stack->size) { |
| 515 | stack->size += 4; |
| 516 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 517 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 518 | } |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 519 | stack->stack[stack->index++] = value; |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 520 | return LY_SUCCESS; |
| 521 | } |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 522 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 523 | static uint8_t |
| 524 | iff_stack_pop(struct iff_stack *stack) |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 525 | { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 526 | stack->index--; |
| 527 | return stack->stack[stack->index]; |
| 528 | } |
| 529 | |
| 530 | static void |
| 531 | iff_stack_clean(struct iff_stack *stack) |
| 532 | { |
| 533 | stack->size = 0; |
| 534 | free(stack->stack); |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 539 | { |
| 540 | uint8_t *item; |
| 541 | uint8_t mask = 3; |
| 542 | |
| 543 | assert(pos >= 0); |
| 544 | assert(op <= 3); /* max 2 bits */ |
| 545 | |
| 546 | item = &list[pos / 4]; |
| 547 | mask = mask << 2 * (pos % 4); |
| 548 | *item = (*item) & ~mask; |
| 549 | *item = (*item) | (op << 2 * (pos % 4)); |
| 550 | } |
| 551 | |
| 552 | static uint8_t |
| 553 | iff_getop(uint8_t *list, int pos) |
| 554 | { |
| 555 | uint8_t *item; |
| 556 | uint8_t mask = 3, result; |
| 557 | |
| 558 | assert(pos >= 0); |
| 559 | |
| 560 | item = &list[pos / 4]; |
| 561 | result = (*item) & (mask << 2 * (pos % 4)); |
| 562 | return result >> 2 * (pos % 4); |
| 563 | } |
| 564 | |
| 565 | #define LYS_IFF_LP 0x04 /* ( */ |
| 566 | #define LYS_IFF_RP 0x08 /* ) */ |
| 567 | |
| 568 | API int |
| 569 | lysc_feature_value(const struct lysc_feature *feature) |
| 570 | { |
| 571 | LY_CHECK_ARG_RET(NULL, feature, -1); |
| 572 | return feature->flags & LYS_FENABLED ? 1 : 0; |
| 573 | } |
| 574 | |
| 575 | static struct lysc_feature * |
| 576 | lysc_feature_find(struct lysc_module *mod, const char *name, size_t len) |
| 577 | { |
| 578 | size_t i; |
| 579 | struct lysc_feature *f; |
| 580 | |
| 581 | for (i = 0; i < len; ++i) { |
| 582 | if (name[i] == ':') { |
| 583 | /* we have a prefixed feature */ |
| 584 | mod = lysc_module_find_prefix(mod, name, i); |
| 585 | LY_CHECK_RET(!mod, NULL); |
| 586 | |
| 587 | name = &name[i + 1]; |
| 588 | len = len - i - 1; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /* we have the correct module, get the feature */ |
| 593 | LY_ARRAY_FOR(mod->features, i) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 594 | f = &mod->features[i]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 595 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 596 | return f; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | static int |
| 604 | lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f) |
| 605 | { |
| 606 | uint8_t op; |
| 607 | int a, b; |
| 608 | |
| 609 | op = iff_getop(iff->expr, *index_e); |
| 610 | (*index_e)++; |
| 611 | |
| 612 | switch (op) { |
| 613 | case LYS_IFF_F: |
| 614 | /* resolve feature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 615 | return lysc_feature_value(iff->features[(*index_f)++]); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 616 | case LYS_IFF_NOT: |
| 617 | /* invert result */ |
| 618 | return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1; |
| 619 | case LYS_IFF_AND: |
| 620 | case LYS_IFF_OR: |
| 621 | a = lysc_iffeature_value_(iff, index_e, index_f); |
| 622 | b = lysc_iffeature_value_(iff, index_e, index_f); |
| 623 | if (op == LYS_IFF_AND) { |
| 624 | return a && b; |
| 625 | } else { /* LYS_IFF_OR */ |
| 626 | return a || b; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | API int |
| 634 | lysc_iffeature_value(const struct lysc_iffeature *iff) |
| 635 | { |
| 636 | int index_e = 0, index_f = 0; |
| 637 | |
| 638 | LY_CHECK_ARG_RET(NULL, iff, -1); |
| 639 | |
| 640 | if (iff->expr) { |
| 641 | return lysc_iffeature_value_(iff, &index_e, &index_f); |
| 642 | } |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * op: 1 - enable, 0 - disable |
| 648 | */ |
| 649 | /** |
| 650 | * @brief Enable/Disable the specified feature in the module. |
| 651 | * |
| 652 | * If the feature is already set to the desired value, LY_SUCCESS is returned. |
| 653 | * By changing the feature, also all the feature which depends on it via their |
| 654 | * if-feature statements are again evaluated (disabled if a if-feature statemen |
| 655 | * evaluates to false). |
| 656 | * |
| 657 | * @param[in] mod Compiled module where to set (search for) the feature. |
| 658 | * @param[in] name Name of the feature to set. Asterisk ('*') can be used to |
| 659 | * set all the features in the module. |
| 660 | * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable). |
| 661 | * @return LY_ERR value. |
| 662 | */ |
| 663 | static LY_ERR |
| 664 | lys_feature_change(const struct lysc_module *mod, const char *name, int value) |
| 665 | { |
| 666 | int all = 0; |
| 667 | unsigned int u; |
| 668 | struct lysc_feature *f, **df; |
| 669 | struct lysc_iffeature *iff; |
| 670 | struct ly_set *changed; |
| 671 | |
| 672 | if (!mod->features) { |
| 673 | LOGERR(mod->ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name); |
| 674 | return LY_EINVAL; |
| 675 | } |
| 676 | |
| 677 | if (!strcmp(name, "*")) { |
| 678 | /* enable all */ |
| 679 | all = 1; |
| 680 | } |
| 681 | changed = ly_set_new(); |
| 682 | |
| 683 | for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 684 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 685 | if (all || !strcmp(f->name, name)) { |
| 686 | if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) { |
| 687 | if (all) { |
| 688 | /* skip already set features */ |
| 689 | continue; |
| 690 | } else { |
| 691 | /* feature already set correctly */ |
| 692 | ly_set_free(changed, NULL); |
| 693 | return LY_SUCCESS; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if (value) { /* enable */ |
| 698 | /* check referenced features if they are enabled */ |
| 699 | LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) { |
| 700 | if (!lysc_iffeature_value(iff)) { |
| 701 | if (all) { |
| 702 | LOGWRN(mod->ctx, |
| 703 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| 704 | f->name); |
| 705 | goto next; |
| 706 | } else { |
| 707 | LOGERR(mod->ctx, LY_EDENIED, |
| 708 | "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).", |
| 709 | f->name); |
| 710 | ly_set_free(changed, NULL); |
| 711 | return LY_EDENIED; |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | /* enable the feature */ |
| 716 | f->flags |= LYS_FENABLED; |
| 717 | } else { /* disable */ |
| 718 | /* disable the feature */ |
| 719 | f->flags &= ~LYS_FENABLED; |
| 720 | } |
| 721 | |
| 722 | /* remember the changed feature */ |
| 723 | ly_set_add(changed, f, LY_SET_OPT_USEASLIST); |
| 724 | |
| 725 | if (!all) { |
| 726 | /* stop in case changing a single feature */ |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | next: |
| 731 | ; |
| 732 | } |
| 733 | |
| 734 | if (!all && !changed->count) { |
| 735 | LOGERR(mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name); |
| 736 | ly_set_free(changed, NULL); |
| 737 | return LY_EINVAL; |
| 738 | } |
| 739 | |
| 740 | /* reflect change(s) in the dependent features */ |
| 741 | for (u = 0; u < changed->count; ++u) { |
| 742 | /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of |
| 743 | * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled |
| 744 | * is not done - by default, features are disabled and must be explicitely enabled. */ |
| 745 | f = changed->objs[u]; |
| 746 | LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) { |
| 747 | if (!((*df)->flags & LYS_FENABLED)) { |
| 748 | /* not enabled, nothing to do */ |
| 749 | continue; |
| 750 | } |
| 751 | /* check the feature's if-features which could change by the previous change of our feature */ |
| 752 | LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) { |
| 753 | if (!lysc_iffeature_value(iff)) { |
| 754 | /* the feature must be disabled now */ |
| 755 | (*df)->flags &= ~LYS_FENABLED; |
| 756 | /* add the feature into the list of changed features */ |
| 757 | ly_set_add(changed, *df, LY_SET_OPT_USEASLIST); |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | ly_set_free(changed, NULL); |
| 765 | return LY_SUCCESS; |
| 766 | } |
| 767 | |
| 768 | API LY_ERR |
| 769 | lys_feature_enable(struct lys_module *module, const char *feature) |
| 770 | { |
| 771 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL); |
| 772 | |
| 773 | return lys_feature_change(module->compiled, feature, 1); |
| 774 | } |
| 775 | |
| 776 | API LY_ERR |
| 777 | lys_feature_disable(struct lys_module *module, const char *feature) |
| 778 | { |
| 779 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, LY_EINVAL); |
| 780 | |
| 781 | return lys_feature_change(module->compiled, feature, 0); |
| 782 | } |
| 783 | |
| 784 | API int |
| 785 | lys_feature_value(const struct lys_module *module, const char *feature) |
| 786 | { |
| 787 | struct lysc_feature *f; |
| 788 | struct lysc_module *mod; |
| 789 | unsigned int u; |
| 790 | |
| 791 | LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1); |
| 792 | mod = module->compiled; |
| 793 | |
| 794 | /* search for the specified feature */ |
| 795 | for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 796 | f = &mod->features[u]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 797 | if (!strcmp(f->name, feature)) { |
| 798 | if (f->flags & LYS_FENABLED) { |
| 799 | return 1; |
| 800 | } else { |
| 801 | return 0; |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | /* feature definition not found */ |
| 807 | return -1; |
| 808 | } |
| 809 | |
| 810 | static LY_ERR |
| 811 | lys_compile_iffeature(struct lysc_ctx *ctx, const char *value, int UNUSED(options), struct lysc_iffeature *iff, struct lysc_feature *parent) |
| 812 | { |
| 813 | const char *c = value; |
| 814 | int r, rc = EXIT_FAILURE; |
| 815 | int i, j, last_not, checkversion = 0; |
| 816 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 817 | uint8_t op; |
| 818 | struct iff_stack stack = {0, 0, NULL}; |
| 819 | struct lysc_feature *f, **df; |
| 820 | |
| 821 | assert(c); |
| 822 | |
| 823 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 824 | for (i = j = last_not = 0; c[i]; i++) { |
| 825 | if (c[i] == '(') { |
| 826 | j++; |
| 827 | checkversion = 1; |
| 828 | continue; |
| 829 | } else if (c[i] == ')') { |
| 830 | j--; |
| 831 | continue; |
| 832 | } else if (isspace(c[i])) { |
| 833 | checkversion = 1; |
| 834 | continue; |
| 835 | } |
| 836 | |
| 837 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 838 | if (c[i + r] == '\0') { |
| 839 | LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 840 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", value); |
| 841 | return LY_EVALID; |
| 842 | } else if (!isspace(c[i + r])) { |
| 843 | /* feature name starting with the not/and/or */ |
| 844 | last_not = 0; |
| 845 | f_size++; |
| 846 | } else if (c[i] == 'n') { /* not operation */ |
| 847 | if (last_not) { |
| 848 | /* double not */ |
| 849 | expr_size = expr_size - 2; |
| 850 | last_not = 0; |
| 851 | } else { |
| 852 | last_not = 1; |
| 853 | } |
| 854 | } else { /* and, or */ |
| 855 | f_exp++; |
| 856 | /* not a not operation */ |
| 857 | last_not = 0; |
| 858 | } |
| 859 | i += r; |
| 860 | } else { |
| 861 | f_size++; |
| 862 | last_not = 0; |
| 863 | } |
| 864 | expr_size++; |
| 865 | |
| 866 | while (!isspace(c[i])) { |
| 867 | if (!c[i] || c[i] == ')') { |
| 868 | i--; |
| 869 | break; |
| 870 | } |
| 871 | i++; |
| 872 | } |
| 873 | } |
| 874 | if (j || f_exp != f_size) { |
| 875 | /* not matching count of ( and ) */ |
| 876 | LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 877 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", value); |
| 878 | return LY_EVALID; |
| 879 | } |
| 880 | |
| 881 | if (checkversion || expr_size > 1) { |
| 882 | /* check that we have 1.1 module */ |
| 883 | if (ctx->mod->version != LYS_VERSION_1_1) { |
| 884 | LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 885 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", value); |
| 886 | return LY_EVALID; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* allocate the memory */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 891 | LY_ARRAY_CREATE_RET(ctx->mod->ctx, iff->features, f_size, LY_EMEM); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 892 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 893 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 894 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->mod->ctx), error); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 895 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 896 | stack.size = expr_size; |
| 897 | f_size--; expr_size--; /* used as indexes from now */ |
| 898 | |
| 899 | for (i--; i >= 0; i--) { |
| 900 | if (c[i] == ')') { |
| 901 | /* push it on stack */ |
| 902 | iff_stack_push(&stack, LYS_IFF_RP); |
| 903 | continue; |
| 904 | } else if (c[i] == '(') { |
| 905 | /* pop from the stack into result all operators until ) */ |
| 906 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 907 | iff_setop(iff->expr, op, expr_size--); |
| 908 | } |
| 909 | continue; |
| 910 | } else if (isspace(c[i])) { |
| 911 | continue; |
| 912 | } |
| 913 | |
| 914 | /* end of operator or operand -> find beginning and get what is it */ |
| 915 | j = i + 1; |
| 916 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 917 | i--; |
| 918 | } |
| 919 | i++; /* go back by one step */ |
| 920 | |
| 921 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 922 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 923 | /* double not */ |
| 924 | iff_stack_pop(&stack); |
| 925 | } else { |
| 926 | /* not has the highest priority, so do not pop from the stack |
| 927 | * as in case of AND and OR */ |
| 928 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 929 | } |
| 930 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 931 | /* as for OR - pop from the stack all operators with the same or higher |
| 932 | * priority and store them to the result, then push the AND to the stack */ |
| 933 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 934 | op = iff_stack_pop(&stack); |
| 935 | iff_setop(iff->expr, op, expr_size--); |
| 936 | } |
| 937 | iff_stack_push(&stack, LYS_IFF_AND); |
| 938 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 939 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 940 | op = iff_stack_pop(&stack); |
| 941 | iff_setop(iff->expr, op, expr_size--); |
| 942 | } |
| 943 | iff_stack_push(&stack, LYS_IFF_OR); |
| 944 | } else { |
| 945 | /* feature name, length is j - i */ |
| 946 | |
| 947 | /* add it to the expression */ |
| 948 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 949 | |
| 950 | /* now get the link to the feature definition */ |
| 951 | f = lysc_feature_find(ctx->mod, &c[i], j - i); |
| 952 | LY_CHECK_ERR_GOTO(!f, |
| 953 | LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 954 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", value, j - i, &c[i]); |
| 955 | rc = LY_EINVAL, |
| 956 | error) |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 957 | iff->features[f_size] = f; |
| 958 | LY_ARRAY_INCREMENT(iff->features); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 959 | if (parent) { |
| 960 | /* and add itself into the dependants list */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 961 | LY_ARRAY_NEW_RET(ctx->mod->ctx, f->depfeatures, df, LY_EMEM); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 962 | *df = parent; |
| 963 | |
| 964 | /* TODO check for circular dependency */ |
| 965 | } |
| 966 | f_size--; |
| 967 | } |
| 968 | } |
| 969 | while (stack.index) { |
| 970 | op = iff_stack_pop(&stack); |
| 971 | iff_setop(iff->expr, op, expr_size--); |
| 972 | } |
| 973 | |
| 974 | if (++expr_size || ++f_size) { |
| 975 | /* not all expected operators and operands found */ |
| 976 | LOGVAL(ctx->mod->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 977 | "Invalid value \"%s\" of if-feature - processing error.", value); |
| 978 | rc = LY_EINT; |
| 979 | } else { |
| 980 | rc = LY_SUCCESS; |
| 981 | } |
| 982 | |
| 983 | error: |
| 984 | /* cleanup */ |
| 985 | iff_stack_clean(&stack); |
| 986 | |
| 987 | return rc; |
| 988 | } |
| 989 | |
| 990 | static LY_ERR |
| 991 | lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature) |
| 992 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 993 | unsigned int u; |
| 994 | LY_ERR ret; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 995 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 996 | if (options & LYSC_OPT_FREE_SP) { |
| 997 | /* just switch the pointers */ |
| 998 | feature->name = feature_p->name; |
| 999 | } else { |
| 1000 | /* keep refcounts correct for lysp_module_free() */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1001 | feature->name = lydict_insert(ctx->mod->ctx, feature_p->name, 0); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1002 | } |
| 1003 | feature->flags = feature_p->flags; |
| 1004 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1005 | if (feature_p->iffeatures) { |
| 1006 | /* allocate everything now */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1007 | LY_ARRAY_CREATE_RET(ctx->mod->ctx, feature->iffeatures, LY_ARRAY_SIZE(feature_p->iffeatures), LY_EMEM); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1008 | |
| 1009 | for (u = 0; u < LY_ARRAY_SIZE(feature_p->iffeatures); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1010 | ret = lys_compile_iffeature(ctx, feature_p->iffeatures[u], options, &feature->iffeatures[u], feature); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1011 | LY_CHECK_RET(ret); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1012 | LY_ARRAY_INCREMENT(feature->iffeatures); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1013 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1016 | return LY_SUCCESS; |
| 1017 | } |
| 1018 | |
| 1019 | LY_ERR |
| 1020 | lys_compile(struct lysp_module *sp, int options, struct lysc_module **sc) |
| 1021 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1022 | struct lysc_ctx ctx = {0}; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1023 | struct lysc_module *mod_c; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1024 | unsigned int u; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1025 | LY_ERR ret; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1026 | |
| 1027 | LY_CHECK_ARG_RET(NULL, sc, sp, sp->ctx, LY_EINVAL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1028 | |
| 1029 | if (sp->submodule) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1030 | LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1031 | return LY_EINVAL; |
| 1032 | } |
| 1033 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1034 | ctx.mod = mod_c = calloc(1, sizeof *mod_c); |
| 1035 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM); |
| 1036 | mod_c->ctx = sp->ctx; |
| 1037 | mod_c->version = sp->version; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1038 | |
| 1039 | if (options & LYSC_OPT_FREE_SP) { |
| 1040 | /* just switch the pointers */ |
| 1041 | mod_c->name = sp->name; |
| 1042 | mod_c->ns = sp->ns; |
| 1043 | mod_c->prefix = sp->prefix; |
| 1044 | } else { |
| 1045 | /* keep refcounts correct for lysp_module_free() */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1046 | mod_c->name = lydict_insert(sp->ctx, sp->name, 0); |
| 1047 | mod_c->ns = lydict_insert(sp->ctx, sp->ns, 0); |
| 1048 | mod_c->prefix = lydict_insert(sp->ctx, sp->prefix, 0); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | if (sp->features) { |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1052 | /* allocate everything now */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1053 | LY_ARRAY_CREATE_RET(ctx.mod->ctx, mod_c->features, LY_ARRAY_SIZE(sp->features), LY_EMEM); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 1054 | |
| 1055 | for (u = 0; u < LY_ARRAY_SIZE(sp->features); ++u) { |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1056 | ret = lys_compile_feature(&ctx, &sp->features[u], options, &mod_c->features[u]); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1057 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1058 | LY_ARRAY_INCREMENT(mod_c->features); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | if (options & LYSC_OPT_FREE_SP) { |
| 1063 | lysp_module_free_(sp, 0); |
| 1064 | } |
| 1065 | |
| 1066 | (*sc) = mod_c; |
| 1067 | return LY_SUCCESS; |
| 1068 | |
| 1069 | error: |
| 1070 | |
| 1071 | if (options & LYSC_OPT_FREE_SP) { |
| 1072 | lysc_module_free_(mod_c, 0); |
| 1073 | } else { |
| 1074 | lysc_module_free_(mod_c, 1); |
| 1075 | } |
| 1076 | return ret; |
| 1077 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1078 | |
| 1079 | const struct lys_module * |
| 1080 | lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const char *revision, int implement) |
| 1081 | { |
| 1082 | struct lys_module *mod = NULL; |
| 1083 | LY_ERR ret; |
| 1084 | |
| 1085 | LY_CHECK_ARG_RET(ctx, ctx, data, NULL); |
| 1086 | |
| 1087 | mod = calloc(1, sizeof *mod); |
| 1088 | LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL); |
| 1089 | |
| 1090 | switch (format) { |
| 1091 | case LYS_IN_YIN: |
| 1092 | /* TODO not yet supported |
| 1093 | mod = yin_read_module(ctx, data, revision, implement); |
| 1094 | */ |
| 1095 | break; |
| 1096 | case LYS_IN_YANG: |
| 1097 | ret = yang_parse(ctx, data, &mod->parsed); |
| 1098 | LY_CHECK_RET(ret, NULL); |
| 1099 | break; |
| 1100 | default: |
| 1101 | LOGERR(ctx, LY_EINVAL, "Invalid schema input format."); |
| 1102 | break; |
| 1103 | } |
| 1104 | |
| 1105 | if (implement) { |
| 1106 | mod->parsed->implemented = 1; |
| 1107 | } |
| 1108 | |
| 1109 | if (revision) { |
| 1110 | /* check revision of the parsed model */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1111 | if (!mod->parsed->revs || strcmp(revision, mod->parsed->revs[0].rev)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1112 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1113 | mod->parsed->name, mod->parsed->revs[0].rev, revision); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1114 | lysp_module_free(mod->parsed); |
| 1115 | free(mod); |
| 1116 | return NULL; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /* check for duplicity in the context */ |
| 1121 | |
| 1122 | /* add into context */ |
| 1123 | ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST); |
| 1124 | |
| 1125 | #if 0 |
| 1126 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 1127 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 1128 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 1129 | * the anotation definitions available in the internal schema structure. There is another hack in schema |
| 1130 | * printers to do not print this internally added annotation. */ |
| 1131 | if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 1132 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 1133 | lys_free(mod, NULL, 1, 1); |
| 1134 | return NULL; |
| 1135 | } |
| 1136 | } |
| 1137 | #endif |
| 1138 | |
| 1139 | return mod; |
| 1140 | } |
| 1141 | |
| 1142 | API const struct lys_module * |
| 1143 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 1144 | { |
| 1145 | return lys_parse_mem_(ctx, data, format, NULL, 1); |
| 1146 | } |
| 1147 | |
| 1148 | static void |
| 1149 | lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd) |
| 1150 | { |
| 1151 | #ifdef __APPLE__ |
| 1152 | char path[MAXPATHLEN]; |
| 1153 | #else |
| 1154 | int len; |
| 1155 | char path[PATH_MAX], proc_path[32]; |
| 1156 | #endif |
| 1157 | |
| 1158 | #ifdef __APPLE__ |
| 1159 | if (fcntl(fd, F_GETPATH, path) != -1) { |
| 1160 | *filename = lydict_insert(ctx, path, 0); |
| 1161 | } |
| 1162 | #else |
| 1163 | /* get URI if there is /proc */ |
| 1164 | sprintf(proc_path, "/proc/self/fd/%d", fd); |
| 1165 | if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) { |
| 1166 | *filename = lydict_insert(ctx, path, len); |
| 1167 | } |
| 1168 | #endif |
| 1169 | } |
| 1170 | |
| 1171 | const struct lys_module * |
| 1172 | lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement) |
| 1173 | { |
| 1174 | const struct lys_module *mod; |
| 1175 | size_t length; |
| 1176 | char *addr; |
| 1177 | |
| 1178 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 1179 | if (fd < 0) { |
| 1180 | LOGARG(ctx, fd); |
| 1181 | return NULL; |
| 1182 | } |
| 1183 | |
| 1184 | LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL); |
| 1185 | if (!addr) { |
| 1186 | LOGERR(ctx, LY_EINVAL, "Empty schema file."); |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | |
| 1190 | mod = lys_parse_mem_(ctx, addr, format, revision, implement); |
| 1191 | ly_munmap(addr, length); |
| 1192 | |
| 1193 | if (mod && !mod->parsed->filepath) { |
| 1194 | lys_parse_set_filename(ctx, &mod->parsed->filepath, fd); |
| 1195 | } |
| 1196 | |
| 1197 | return mod; |
| 1198 | } |
| 1199 | |
| 1200 | API const struct lys_module * |
| 1201 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
| 1202 | { |
| 1203 | return lys_parse_fd_(ctx, fd, format, NULL, 1); |
| 1204 | } |
| 1205 | |
| 1206 | API const struct lys_module * |
| 1207 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1208 | { |
| 1209 | int fd; |
| 1210 | const struct lys_module *mod; |
| 1211 | const char *rev, *dot, *filename; |
| 1212 | size_t len; |
| 1213 | |
| 1214 | LY_CHECK_ARG_RET(ctx, ctx, path, NULL); |
| 1215 | |
| 1216 | fd = open(path, O_RDONLY); |
| 1217 | LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL); |
| 1218 | |
| 1219 | mod = lys_parse_fd(ctx, fd, format); |
| 1220 | close(fd); |
| 1221 | LY_CHECK_RET(!mod, NULL); |
| 1222 | |
| 1223 | /* check that name and revision match filename */ |
| 1224 | filename = strrchr(path, '/'); |
| 1225 | if (!filename) { |
| 1226 | filename = path; |
| 1227 | } else { |
| 1228 | filename++; |
| 1229 | } |
| 1230 | rev = strchr(filename, '@'); |
| 1231 | dot = strrchr(filename, '.'); |
| 1232 | |
| 1233 | /* name */ |
| 1234 | len = strlen(mod->parsed->name); |
| 1235 | if (strncmp(filename, mod->parsed->name, len) || |
| 1236 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
| 1237 | LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->parsed->name); |
| 1238 | } |
| 1239 | if (rev) { |
| 1240 | len = dot - ++rev; |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1241 | if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].rev, rev, len)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1242 | LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1243 | mod->parsed->revs ? mod->parsed->revs[0].rev : "none"); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | if (!mod->parsed->filepath) { |
| 1248 | /* store URI */ |
| 1249 | char rpath[PATH_MAX]; |
| 1250 | if (realpath(path, rpath) != NULL) { |
| 1251 | mod->parsed->filepath = lydict_insert(ctx, rpath, 0); |
| 1252 | } else { |
| 1253 | mod->parsed->filepath = lydict_insert(ctx, path, 0); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | return mod; |
| 1258 | } |
| 1259 | |