Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_yang.c |
| 3 | * @author Pavol Vican |
| 4 | * @brief YANG parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Pavol Vican | 9a3a721 | 2016-03-23 10:04:00 +0100 | [diff] [blame] | 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 |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 16 | #include <assert.h> |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 17 | #include "parser_yang.h" |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 18 | #include "parser_yang_lex.h" |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 19 | #include "parser.h" |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 20 | #include "xpath.h" |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 21 | |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 22 | static void yang_free_import(struct ly_ctx *ctx, struct lys_import *imp, uint8_t start, uint8_t size); |
| 23 | static void yang_free_include(struct ly_ctx *ctx, struct lys_include *inc, uint8_t start, uint8_t size); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 24 | static int yang_check_sub_module(struct lys_module *module, struct unres_schema *unres, struct lys_node *node); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 25 | static void free_yang_common(struct lys_module *module, struct lys_node *node); |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 26 | static int yang_check_nodes(struct lys_module *module, struct lys_node *parent, struct lys_node *nodes, |
| 27 | int config_opt, struct unres_schema *unres); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 28 | void lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size); |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 29 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 30 | static int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 31 | yang_check_string(struct lys_module *module, const char **target, char *what, char *where, char *value) |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 32 | { |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 33 | if (*target) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 34 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where); |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 35 | free(value); |
| 36 | return 1; |
| 37 | } else { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 38 | *target = lydict_insert_zc(module->ctx, value); |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | } |
| 42 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 43 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 44 | yang_read_common(struct lys_module *module, char *value, enum yytokentype type) |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 45 | { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 46 | int ret = 0; |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 47 | |
| 48 | switch (type) { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 49 | case MODULE_KEYWORD: |
| 50 | module->name = lydict_insert_zc(module->ctx, value); |
| 51 | break; |
| 52 | case NAMESPACE_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 53 | ret = yang_check_string(module, &module->ns, "namespace", "module", value); |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 54 | break; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 55 | case ORGANIZATION_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 56 | ret = yang_check_string(module, &module->org, "organization", "module", value); |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 57 | break; |
| 58 | case CONTACT_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 59 | ret = yang_check_string(module, &module->contact, "contact", "module", value); |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 60 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 61 | default: |
| 62 | free(value); |
| 63 | LOGINT; |
| 64 | ret = EXIT_FAILURE; |
| 65 | break; |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 68 | return ret; |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 71 | int |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 72 | yang_check_version(struct lys_module *module, struct lys_submodule *submodule, char *value, int repeat) |
| 73 | { |
| 74 | int ret = EXIT_SUCCESS; |
| 75 | |
| 76 | if (repeat) { |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 77 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "yang version", "module"); |
| 78 | ret = EXIT_FAILURE; |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 79 | } else { |
| 80 | if (!strcmp(value, "1")) { |
| 81 | if (submodule) { |
| 82 | if (module->version > 1) { |
| 83 | LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL); |
| 84 | ret = EXIT_FAILURE; |
| 85 | } |
| 86 | } else { |
| 87 | module->version = 1; |
| 88 | } |
| 89 | } else if (!strcmp(value, "1.1")) { |
| 90 | if (submodule) { |
| 91 | if (module->version != 2) { |
| 92 | LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL); |
| 93 | ret = EXIT_FAILURE; |
| 94 | } |
| 95 | } else { |
| 96 | module->version = 2; |
| 97 | } |
| 98 | } else { |
| 99 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version"); |
| 100 | ret = EXIT_FAILURE; |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 101 | } |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 102 | } |
| 103 | free(value); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | int |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 108 | yang_read_prefix(struct lys_module *module, struct lys_import *imp, char *value) |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 109 | { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 110 | int ret = 0; |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 111 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 112 | if (!imp && lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 113 | free(value); |
| 114 | return EXIT_FAILURE; |
| 115 | } |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 116 | |
| 117 | if (imp) { |
| 118 | ret = yang_check_string(module, &imp->prefix, "prefix", "import", value); |
| 119 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 120 | ret = yang_check_string(module, &module->prefix, "prefix", "module", value); |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 121 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 122 | |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 123 | return ret; |
| 124 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 125 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 126 | static int |
| 127 | yang_fill_import(struct lys_module *module, struct lys_import *imp_old, struct lys_import *imp_new, char *value) |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 128 | { |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 129 | const char *exp; |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 130 | int rc; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 131 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 132 | if (!imp_old->prefix) { |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 133 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "import"); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 134 | goto error; |
| 135 | } else { |
| 136 | if (lyp_check_identifier(imp_old->prefix, LY_IDENT_PREFIX, module, NULL)) { |
| 137 | goto error; |
| 138 | } |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 139 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 140 | memcpy(imp_new, imp_old, sizeof *imp_old); |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 141 | exp = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 142 | rc = lyp_check_import(module, exp, imp_new); |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 143 | lydict_remove(module->ctx, exp); |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 144 | module->imp_size++; |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 145 | if (rc) { |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 146 | return EXIT_FAILURE; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 147 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 148 | |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 149 | return EXIT_SUCCESS; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 150 | |
| 151 | error: |
| 152 | free(value); |
| 153 | lydict_remove(module->ctx, imp_old->dsc); |
| 154 | lydict_remove(module->ctx, imp_old->ref); |
| 155 | return EXIT_FAILURE; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 156 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 157 | |
| 158 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 159 | yang_read_description(struct lys_module *module, void *node, char *value, char *where) |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 160 | { |
| 161 | int ret; |
Pavol Vican | 9bcd7c6 | 2016-03-17 19:36:35 +0100 | [diff] [blame] | 162 | char *dsc = "description"; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 163 | |
| 164 | if (!node) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 165 | ret = yang_check_string(module, &module->dsc, dsc, "module", value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 166 | } else { |
Pavol Vican | 9bcd7c6 | 2016-03-17 19:36:35 +0100 | [diff] [blame] | 167 | if (!strcmp("revision", where)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 168 | ret = yang_check_string(module, &((struct lys_revision *)node)->dsc, dsc, where, value); |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 169 | } else if (!strcmp("import", where)){ |
| 170 | ret = yang_check_string(module, &((struct lys_import *)node)->dsc, dsc, where, value); |
| 171 | } else if (!strcmp("include", where)){ |
| 172 | ret = yang_check_string(module, &((struct lys_include *)node)->dsc, dsc, where, value); |
Pavol Vican | 9bcd7c6 | 2016-03-17 19:36:35 +0100 | [diff] [blame] | 173 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 174 | ret = yang_check_string(module, &((struct lys_node *)node)->dsc, dsc, where, value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 175 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 176 | } |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 181 | yang_read_reference(struct lys_module *module, void *node, char *value, char *where) |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 182 | { |
| 183 | int ret; |
Pavol Vican | f5fe966 | 2016-03-17 20:00:16 +0100 | [diff] [blame] | 184 | char *ref = "reference"; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 185 | |
| 186 | if (!node) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 187 | ret = yang_check_string(module, &module->ref, "reference", "module", value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 188 | } else { |
Pavol Vican | f5fe966 | 2016-03-17 20:00:16 +0100 | [diff] [blame] | 189 | if (!strcmp("revision", where)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 190 | ret = yang_check_string(module, &((struct lys_revision *)node)->ref, ref, where, value); |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 191 | } else if (!strcmp("import", where)){ |
| 192 | ret = yang_check_string(module, &((struct lys_import *)node)->ref, ref, where, value); |
| 193 | } else if (!strcmp("include", where)){ |
| 194 | ret = yang_check_string(module, &((struct lys_include *)node)->ref, ref, where, value); |
Pavol Vican | f5fe966 | 2016-03-17 20:00:16 +0100 | [diff] [blame] | 195 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 196 | ret = yang_check_string(module, &((struct lys_node *)node)->ref, ref, where, value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 197 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 198 | } |
| 199 | return ret; |
| 200 | } |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 201 | |
Pavol Vican | 866d991 | 2016-10-25 09:13:30 +0200 | [diff] [blame] | 202 | void |
| 203 | yang_read_revision(struct lys_module *module, char *value, struct lys_revision *retval) |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 204 | { |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 205 | /* first member of array is last revision */ |
Pavol Vican | 866d991 | 2016-10-25 09:13:30 +0200 | [diff] [blame] | 206 | if ((module->rev_size - 1) && strcmp(module->rev[0].date, value) < 0) { |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 207 | memcpy(retval->date, module->rev[0].date, LY_REV_SIZE); |
| 208 | memcpy(module->rev[0].date, value, LY_REV_SIZE); |
| 209 | retval->dsc = module->rev[0].dsc; |
| 210 | retval->ref = module->rev[0].ref; |
| 211 | retval = module->rev; |
| 212 | retval->dsc = NULL; |
| 213 | retval->ref = NULL; |
| 214 | } else { |
| 215 | memcpy(retval->date, value, LY_REV_SIZE); |
| 216 | } |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 217 | free(value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 218 | } |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 219 | |
| 220 | int |
Pavol Vican | a182796 | 2016-02-29 15:39:42 +0100 | [diff] [blame] | 221 | yang_add_elem(struct lys_node_array **node, uint32_t *size) |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 222 | { |
Pavol Vican | 45ccc59 | 2016-03-09 18:53:48 +0100 | [diff] [blame] | 223 | if (!(*size % LY_ARRAY_SIZE)) { |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 224 | if (!(*node = ly_realloc(*node, (*size + LY_ARRAY_SIZE) * sizeof **node))) { |
| 225 | LOGMEM; |
| 226 | return EXIT_FAILURE; |
| 227 | } else { |
Pavol Vican | 45ccc59 | 2016-03-09 18:53:48 +0100 | [diff] [blame] | 228 | memset(*node + *size, 0, LY_ARRAY_SIZE * sizeof **node); |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | (*size)++; |
| 232 | return EXIT_SUCCESS; |
| 233 | } |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 234 | |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 235 | int |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 236 | yang_fill_iffeature(struct lys_module *module, struct lys_iffeature *iffeature, void *parent, |
| 237 | char *value, struct unres_schema *unres, int parent_is_feature) |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 238 | { |
| 239 | const char *exp; |
| 240 | int ret; |
| 241 | |
Michal Vasko | 97b32be | 2016-07-25 10:59:53 +0200 | [diff] [blame] | 242 | if ((module->version != 2) && ((value[0] == '(') || strchr(value, ' '))) { |
| 243 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 244 | free(value); |
| 245 | return EXIT_FAILURE; |
| 246 | } |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 247 | |
Michal Vasko | 56d082c | 2016-10-25 14:00:42 +0200 | [diff] [blame] | 248 | if (!(exp = transform_iffeat_schema2json(module, value))) { |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 249 | free(value); |
| 250 | return EXIT_FAILURE; |
| 251 | } |
| 252 | free(value); |
| 253 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 254 | ret = resolve_iffeature_compile(iffeature, exp, (struct lys_node *)parent, parent_is_feature, unres); |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 255 | lydict_remove(module->ctx, exp); |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 256 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 257 | return (ret) ? EXIT_FAILURE : EXIT_SUCCESS; |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 260 | int |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 261 | yang_check_flags(uint16_t *flags, uint16_t mask, char *what, char *where, uint16_t value, int shortint) |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 262 | { |
| 263 | if (*flags & mask) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 264 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where); |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 265 | return EXIT_FAILURE; |
| 266 | } else { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 267 | if (shortint) { |
| 268 | *((uint8_t *)flags) |= (uint8_t)value; |
| 269 | } else { |
| 270 | *flags |= value; |
| 271 | } |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 272 | return EXIT_SUCCESS; |
| 273 | } |
| 274 | } |
| 275 | |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 276 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 277 | yang_read_base(struct lys_module *module, struct lys_ident *ident, char *value, struct unres_schema *unres) |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 278 | { |
| 279 | const char *exp; |
| 280 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 281 | exp = transform_schema2json(module, value); |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 282 | free(value); |
| 283 | if (!exp) { |
| 284 | return EXIT_FAILURE; |
| 285 | } |
Pavol Vican | 4ffd0ab | 2016-08-27 16:35:04 +0200 | [diff] [blame] | 286 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 287 | if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, exp) == -1) { |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 288 | lydict_remove(module->ctx, exp); |
| 289 | return EXIT_FAILURE; |
| 290 | } |
Pavol Vican | 44dde2c | 2016-02-10 11:18:14 +0100 | [diff] [blame] | 291 | |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 292 | lydict_remove(module->ctx, exp); |
| 293 | return EXIT_SUCCESS; |
| 294 | } |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 295 | |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 296 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 297 | yang_read_message(struct lys_module *module,struct lys_restr *save,char *value, char *what, int message) |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 298 | { |
| 299 | int ret; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 300 | |
Pavol Vican | dde090a | 2016-08-30 15:12:14 +0200 | [diff] [blame] | 301 | if (message == ERROR_APP_TAG_KEYWORD) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 302 | ret = yang_check_string(module, &save->eapptag, "error_app_tag", what, value); |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 303 | } else { |
Pavol Vican | dde090a | 2016-08-30 15:12:14 +0200 | [diff] [blame] | 304 | ret = yang_check_string(module, &save->emsg, "error_message", what, value); |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 305 | } |
| 306 | return ret; |
| 307 | } |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 308 | |
| 309 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 310 | yang_read_presence(struct lys_module *module, struct lys_node_container *cont, char *value) |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 311 | { |
| 312 | if (cont->presence) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 313 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, cont, "presence", "container"); |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 314 | free(value); |
| 315 | return EXIT_FAILURE; |
| 316 | } else { |
| 317 | cont->presence = lydict_insert_zc(module->ctx, value); |
| 318 | return EXIT_SUCCESS; |
| 319 | } |
| 320 | } |
| 321 | |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 322 | void * |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 323 | yang_read_when(struct lys_module *module, struct lys_node *node, enum yytokentype type, char *value) |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 324 | { |
| 325 | struct lys_when *retval; |
| 326 | |
| 327 | retval = calloc(1, sizeof *retval); |
| 328 | if (!retval) { |
| 329 | LOGMEM; |
| 330 | free(value); |
| 331 | return NULL; |
| 332 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 333 | retval->cond = transform_schema2json(module, value); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 334 | if (!retval->cond) { |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 335 | goto error; |
| 336 | } |
| 337 | switch (type) { |
| 338 | case CONTAINER_KEYWORD: |
| 339 | if (((struct lys_node_container *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 340 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "container"); |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 341 | goto error; |
| 342 | } |
| 343 | ((struct lys_node_container *)node)->when = retval; |
| 344 | break; |
Pavol Vican | db7489e | 2016-08-23 17:23:39 +0200 | [diff] [blame] | 345 | case ANYDATA_KEYWORD: |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 346 | case ANYXML_KEYWORD: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 347 | if (((struct lys_node_anydata *)node)->when) { |
Pavol Vican | db7489e | 2016-08-23 17:23:39 +0200 | [diff] [blame] | 348 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", (type == ANYXML_KEYWORD) ? "anyxml" : "anydata"); |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 349 | goto error; |
| 350 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 351 | ((struct lys_node_anydata *)node)->when = retval; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 352 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 353 | case CHOICE_KEYWORD: |
| 354 | if (((struct lys_node_choice *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 355 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "choice"); |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 356 | goto error; |
| 357 | } |
| 358 | ((struct lys_node_choice *)node)->when = retval; |
| 359 | break; |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 360 | case CASE_KEYWORD: |
| 361 | if (((struct lys_node_case *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 362 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "case"); |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 363 | goto error; |
| 364 | } |
| 365 | ((struct lys_node_case *)node)->when = retval; |
| 366 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 367 | case LEAF_KEYWORD: |
| 368 | if (((struct lys_node_leaf *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 369 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaf"); |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 370 | goto error; |
| 371 | } |
| 372 | ((struct lys_node_leaf *)node)->when = retval; |
| 373 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 374 | case LEAF_LIST_KEYWORD: |
| 375 | if (((struct lys_node_leaflist *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 376 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaflist"); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 377 | goto error; |
| 378 | } |
| 379 | ((struct lys_node_leaflist *)node)->when = retval; |
| 380 | break; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 381 | case LIST_KEYWORD: |
| 382 | if (((struct lys_node_list *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 383 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "list"); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 384 | goto error; |
| 385 | } |
| 386 | ((struct lys_node_list *)node)->when = retval; |
| 387 | break; |
Pavol Vican | 14b1856 | 2016-03-01 16:04:29 +0100 | [diff] [blame] | 388 | case USES_KEYWORD: |
| 389 | if (((struct lys_node_uses *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 390 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "uses"); |
Pavol Vican | 14b1856 | 2016-03-01 16:04:29 +0100 | [diff] [blame] | 391 | goto error; |
| 392 | } |
| 393 | ((struct lys_node_uses *)node)->when = retval; |
| 394 | break; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 395 | case AUGMENT_KEYWORD: |
| 396 | if (((struct lys_node_augment *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 397 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "augment"); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 398 | goto error; |
| 399 | } |
| 400 | ((struct lys_node_augment *)node)->when = retval; |
| 401 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 402 | default: |
| 403 | goto error; |
| 404 | break; |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 405 | } |
| 406 | free(value); |
| 407 | return retval; |
| 408 | |
| 409 | error: |
| 410 | free(value); |
| 411 | lys_when_free(module->ctx, retval); |
| 412 | return NULL; |
| 413 | } |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 414 | |
| 415 | void * |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 416 | yang_read_node(struct lys_module *module, struct lys_node *parent, struct lys_node **root, |
| 417 | char *value, int nodetype, int sizeof_struct) |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 418 | { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 419 | struct lys_node *node, **child; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 420 | |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 421 | node = calloc(1, sizeof_struct); |
| 422 | if (!node) { |
Pavol Vican | d1dbfda | 2016-03-21 10:03:58 +0100 | [diff] [blame] | 423 | free(value); |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 424 | LOGMEM; |
| 425 | return NULL; |
| 426 | } |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 427 | if (value) { |
| 428 | node->name = lydict_insert_zc(module->ctx, value); |
| 429 | } |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 430 | node->module = module; |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 431 | node->nodetype = nodetype; |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 432 | |
| 433 | /* insert the node into the schema tree */ |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 434 | child = (parent) ? &parent->child : root; |
| 435 | if (*child) { |
| 436 | (*child)->prev->next = node; |
| 437 | (*child)->prev = node; |
| 438 | } else { |
| 439 | *child = node; |
| 440 | node->prev = node; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 441 | } |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 442 | return node; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 446 | yang_read_default(struct lys_module *module, void *node, char *value, enum yytokentype type) |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 447 | { |
| 448 | int ret; |
| 449 | |
| 450 | switch (type) { |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 451 | case LEAF_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 452 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dflt, "default", "leaf", value); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 453 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 454 | case TYPEDEF_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 455 | ret = yang_check_string(module, &((struct lys_tpdf *) node)->dflt, "default", "typedef", value); |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 456 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 457 | default: |
| 458 | free(value); |
| 459 | LOGINT; |
| 460 | ret = EXIT_FAILURE; |
| 461 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 462 | } |
| 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 467 | yang_read_units(struct lys_module *module, void *node, char *value, enum yytokentype type) |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 468 | { |
| 469 | int ret; |
| 470 | |
| 471 | switch (type) { |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 472 | case LEAF_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 473 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->units, "units", "leaf", value); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 474 | break; |
| 475 | case LEAF_LIST_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 476 | ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->units, "units", "leaflist", value); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 477 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 478 | case TYPEDEF_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 479 | ret = yang_check_string(module, &((struct lys_tpdf *) node)->units, "units", "typedef", value); |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 480 | break; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 481 | case ADD_KEYWORD: |
| 482 | case REPLACE_KEYWORD: |
| 483 | ret = yang_check_string(module, &((struct lys_deviate *) node)->units, "units", "deviate", value); |
| 484 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 485 | default: |
| 486 | free(value); |
| 487 | LOGINT; |
| 488 | ret = EXIT_FAILURE; |
| 489 | break; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 490 | } |
| 491 | return ret; |
| 492 | } |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 493 | |
| 494 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 495 | yang_read_key(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres) |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 496 | { |
| 497 | char *exp, *value; |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 498 | struct lys_node *node; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 499 | |
| 500 | exp = value = (char *) list->keys; |
| 501 | while ((value = strpbrk(value, " \t\n"))) { |
| 502 | list->keys_size++; |
| 503 | while (isspace(*value)) { |
| 504 | value++; |
| 505 | } |
| 506 | } |
| 507 | list->keys_size++; |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 508 | |
| 509 | list->keys_str = lydict_insert_zc(module->ctx, exp); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 510 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
| 511 | if (!list->keys) { |
| 512 | LOGMEM; |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 513 | return EXIT_FAILURE; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 514 | } |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 515 | for (node = list->parent; node && node->nodetype != LYS_GROUPING; node = lys_parent(node)); |
| 516 | if (!node && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) { |
| 517 | return EXIT_FAILURE; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 518 | } |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 519 | return EXIT_SUCCESS; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 523 | yang_fill_unique(struct lys_module *module, struct lys_node_list *list, struct lys_unique *unique, char *value, struct unres_schema *unres) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 524 | { |
| 525 | int i, j; |
| 526 | char *vaux; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 527 | struct unres_list_uniq *unique_info; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 528 | |
| 529 | /* count the number of unique leafs in the value */ |
| 530 | vaux = value; |
| 531 | while ((vaux = strpbrk(vaux, " \t\n"))) { |
| 532 | unique->expr_size++; |
| 533 | while (isspace(*vaux)) { |
| 534 | vaux++; |
| 535 | } |
| 536 | } |
| 537 | unique->expr_size++; |
| 538 | unique->expr = calloc(unique->expr_size, sizeof *unique->expr); |
| 539 | if (!unique->expr) { |
| 540 | LOGMEM; |
| 541 | goto error; |
| 542 | } |
| 543 | |
| 544 | for (i = 0; i < unique->expr_size; i++) { |
| 545 | vaux = strpbrk(value, " \t\n"); |
| 546 | if (!vaux) { |
| 547 | /* the last token, lydict_insert() will count its size on its own */ |
| 548 | vaux = value; |
| 549 | } |
| 550 | |
| 551 | /* store token into unique structure */ |
| 552 | unique->expr[i] = lydict_insert(module->ctx, value, vaux - value); |
| 553 | |
| 554 | /* check that the expression does not repeat */ |
| 555 | for (j = 0; j < i; j++) { |
| 556 | if (ly_strequal(unique->expr[j], unique->expr[i], 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 557 | LOGVAL(LYE_INARG, LY_VLOG_LYS, list, unique->expr[i], "unique"); |
| 558 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 559 | goto error; |
| 560 | } |
| 561 | } |
| 562 | /* try to resolve leaf */ |
| 563 | if (unres) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 564 | unique_info = malloc(sizeof *unique_info); |
| 565 | unique_info->list = (struct lys_node *)list; |
| 566 | unique_info->expr = unique->expr[i]; |
| 567 | unique_info->trg_type = &unique->trg_type; |
| 568 | if (unres_schema_add_node(module, unres, unique_info, UNRES_LIST_UNIQ, NULL) == -1) { |
Pavol Vican | 18b1021 | 2016-04-11 15:41:52 +0200 | [diff] [blame] | 569 | goto error; |
| 570 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 571 | } else { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 572 | if (resolve_unique((struct lys_node *)list, unique->expr[i], &unique->trg_type)) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 573 | goto error; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* move to next token */ |
| 578 | value = vaux; |
| 579 | while(isspace(*value)) { |
| 580 | value++; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return EXIT_SUCCESS; |
| 585 | |
| 586 | error: |
| 587 | return EXIT_FAILURE; |
| 588 | } |
| 589 | |
| 590 | int |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 591 | yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres) |
| 592 | { |
| 593 | uint8_t k; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 594 | char *str; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 595 | |
| 596 | for(k=0; k<list->unique_size; k++) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 597 | str = (char *)list->unique[k].expr; |
| 598 | if (yang_fill_unique(module, list, &list->unique[k], str, unres)) { |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 599 | goto error; |
| 600 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 601 | free(str); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 602 | } |
| 603 | return EXIT_SUCCESS; |
| 604 | |
| 605 | error: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 606 | free(str); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 607 | return EXIT_FAILURE; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Pavol Vican | 07f220f | 2016-09-02 13:04:37 +0200 | [diff] [blame] | 610 | int |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 611 | yang_read_leafref_path(struct lys_module *module, struct yang_type *stype, char *value) |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 612 | { |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 613 | if (stype->base && (stype->base != LY_TYPE_LEAFREF)) { |
| 614 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance"); |
| 615 | goto error; |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 616 | } |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 617 | if (stype->type->info.lref.path) { |
| 618 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "path", "type"); |
| 619 | goto error; |
| 620 | } |
| 621 | stype->type->info.lref.path = lydict_insert_zc(module->ctx, value); |
| 622 | stype->base = LY_TYPE_LEAFREF; |
| 623 | return EXIT_SUCCESS; |
| 624 | |
| 625 | error: |
| 626 | free(value); |
| 627 | return EXIT_FAILURE; |
| 628 | } |
| 629 | |
| 630 | int |
| 631 | yang_read_require_instance(struct yang_type *stype, int req) |
| 632 | { |
| 633 | if (stype->base && (stype->base != LY_TYPE_LEAFREF)) { |
| 634 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance"); |
| 635 | return EXIT_FAILURE; |
| 636 | } |
| 637 | if (stype->type->info.lref.req) { |
| 638 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "require-instance", "type"); |
| 639 | return EXIT_FAILURE; |
| 640 | } |
| 641 | stype->type->info.lref.req = req; |
| 642 | stype->base = LY_TYPE_LEAFREF; |
| 643 | return EXIT_SUCCESS; |
| 644 | } |
| 645 | |
| 646 | int |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 647 | yang_check_type(struct lys_module *module, struct lys_node *parent, struct yang_type *typ, struct lys_type *type, int tpdftype, struct unres_schema *unres) |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 648 | { |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 649 | int i, j, rc, ret = -1; |
| 650 | int8_t req; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 651 | const char *name, *value; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 652 | LY_DATA_TYPE base = 0, base_tmp; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 653 | struct lys_node *siter; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 654 | struct lys_type *dertype; |
| 655 | struct lys_type_enum *enms_sc = NULL; |
| 656 | struct lys_type_bit *bits_sc = NULL; |
| 657 | struct lys_type_bit bit_tmp; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 658 | struct yang_type *yang; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 659 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 660 | value = transform_schema2json(module, typ->name); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 661 | if (!value) { |
| 662 | goto error; |
| 663 | } |
| 664 | |
| 665 | i = parse_identifier(value); |
| 666 | if (i < 1) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 667 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 668 | lydict_remove(module->ctx, value); |
| 669 | goto error; |
| 670 | } |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 671 | /* module name */ |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 672 | name = value; |
| 673 | if (value[i]) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 674 | type->module_name = lydict_insert(module->ctx, value, i); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 675 | name += i; |
| 676 | if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 677 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 678 | lydict_remove(module->ctx, value); |
| 679 | goto error; |
| 680 | } |
| 681 | ++name; |
| 682 | } |
| 683 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 684 | rc = resolve_superior_type(name, type->module_name, module, parent, &type->der); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 685 | if (rc == -1) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 686 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, type->module_name); |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 687 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 688 | goto error; |
| 689 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 690 | /* the type could not be resolved or it was resolved to an unresolved typedef or leafref */ |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 691 | } else if (rc == EXIT_FAILURE) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 692 | LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name); |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 693 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 694 | ret = EXIT_FAILURE; |
| 695 | goto error; |
| 696 | } |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 697 | lydict_remove(module->ctx, value); |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 698 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 699 | if (type->base == LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 700 | /* resolved type in grouping, decrease the grouping's nacm number to indicate that one less |
Radek Krejci | 9b6aad2 | 2016-09-20 15:55:51 +0200 | [diff] [blame] | 701 | * unresolved item left inside the grouping, LY_TYPE_ERR used as a flag for types inside a grouping. */ |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 702 | for (siter = parent; siter && (siter->nodetype != LYS_GROUPING); siter = lys_parent(siter)); |
| 703 | if (siter) { |
| 704 | if (!((struct lys_node_grp *)siter)->nacm) { |
| 705 | LOGINT; |
| 706 | goto error; |
| 707 | } |
| 708 | ((struct lys_node_grp *)siter)->nacm--; |
| 709 | } else { |
| 710 | LOGINT; |
| 711 | goto error; |
| 712 | } |
| 713 | } |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 714 | |
| 715 | /* check status */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 716 | if (lyp_check_status(type->parent->flags, type->parent->module, type->parent->name, |
| 717 | type->der->flags, type->der->module, type->der->name, parent)) { |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 718 | goto error; |
| 719 | } |
| 720 | |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 721 | base = typ->base; |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 722 | base_tmp = type->base; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 723 | type->base = type->der->type.base; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 724 | if (base == 0) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 725 | base = type->der->type.base; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 726 | } |
| 727 | switch (base) { |
| 728 | case LY_TYPE_STRING: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 729 | if (type->base == LY_TYPE_BINARY) { |
| 730 | if (type->info.str.pat_count) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 731 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Binary type could not include pattern statement."); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 732 | goto error; |
| 733 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 734 | type->info.binary.length = type->info.str.length; |
| 735 | if (type->info.binary.length && lyp_check_length_range(type->info.binary.length->expr, type)) { |
| 736 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, type->info.binary.length->expr, "length"); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 737 | goto error; |
| 738 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 739 | } else if (type->base == LY_TYPE_STRING) { |
| 740 | if (type->info.str.length && lyp_check_length_range(type->info.str.length->expr, type)) { |
| 741 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, type->info.str.length->expr, "length"); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 742 | goto error; |
| 743 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 744 | } else { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 745 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 746 | goto error; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 747 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 748 | break; |
| 749 | case LY_TYPE_DEC64: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 750 | if (type->base == LY_TYPE_DEC64) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 751 | /* mandatory sub-statement(s) check */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 752 | if (!type->info.dec64.dig && !type->der->type.der) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 753 | /* decimal64 type directly derived from built-in type requires fraction-digits */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 754 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 755 | goto error; |
| 756 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 757 | if (type->info.dec64.dig && type->der->type.der) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 758 | /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 759 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 760 | goto error; |
| 761 | } |
Radek Krejci | 4800f65 | 2016-09-08 14:02:52 +0200 | [diff] [blame] | 762 | |
| 763 | /* copy fraction-digits specification from parent type for easier internal use */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 764 | if (type->der->type.der) { |
| 765 | type->info.dec64.dig = type->der->type.info.dec64.dig; |
| 766 | type->info.dec64.div = type->der->type.info.dec64.div; |
Radek Krejci | 4800f65 | 2016-09-08 14:02:52 +0200 | [diff] [blame] | 767 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 768 | if (type->info.dec64.range && lyp_check_length_range(type->info.dec64.range->expr, type)) { |
| 769 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, type->info.dec64.range->expr, "range"); |
Pavol Vican | 3c8ee2b | 2016-09-29 13:18:13 +0200 | [diff] [blame] | 770 | goto error; |
| 771 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 772 | } else if (type->base >= LY_TYPE_INT8 && type->base <=LY_TYPE_UINT64) { |
| 773 | if (type->info.dec64.dig) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 774 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Numerical type could not include fraction statement."); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 775 | goto error; |
| 776 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 777 | type->info.num.range = type->info.dec64.range; |
| 778 | if (type->info.num.range && lyp_check_length_range(type->info.num.range->expr, type)) { |
| 779 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, type->info.num.range->expr, "range"); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 780 | goto error; |
| 781 | } |
| 782 | } else { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 783 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 784 | goto error; |
| 785 | } |
| 786 | break; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 787 | case LY_TYPE_ENUM: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 788 | if (type->base != LY_TYPE_ENUM) { |
| 789 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 790 | goto error; |
| 791 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 792 | dertype = &type->der->type; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 793 | |
| 794 | if (!dertype->der) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 795 | if (!type->info.enums.count) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 796 | /* type is derived directly from buit-in enumeartion type and enum statement is required */ |
| 797 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type"); |
| 798 | goto error; |
| 799 | } |
| 800 | } else { |
| 801 | for (; !dertype->info.enums.count; dertype = &dertype->der->type); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 802 | if (module->version < 2 && type->info.enums.count) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 803 | /* type is not directly derived from built-in enumeration type and enum statement is prohibited |
| 804 | * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */ |
| 805 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum"); |
| 806 | goto error; |
| 807 | } |
| 808 | |
| 809 | /* restricted enumeration type - the name MUST be used in the base type */ |
| 810 | enms_sc = dertype->info.enums.enm; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 811 | for(i = 0; i < type->info.enums.count; i++) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 812 | for (j = 0; j < dertype->info.enums.count; j++) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 813 | if (ly_strequal(enms_sc[j].name, type->info.enums.enm[i].name, 1)) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 814 | break; |
| 815 | } |
| 816 | } |
| 817 | if (j == dertype->info.enums.count) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 818 | LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, type->info.enums.enm[i].name); |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 819 | goto error; |
| 820 | } |
| 821 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 822 | if (type->info.enums.enm[i].flags & LYS_AUTOASSIGNED) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 823 | /* automatically assign value from base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 824 | type->info.enums.enm[i].value = enms_sc[j].value; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 825 | } else { |
| 826 | /* check that the assigned value corresponds to the original |
| 827 | * value of the enum in the base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 828 | if (type->info.enums.enm[i].value != enms_sc[j].value) { |
| 829 | /* type->info.enums.enm[i].value - assigned value in restricted enum |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 830 | * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 831 | LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL, type->info.enums.enm[i].value, |
| 832 | type->info.enums.enm[i].name, enms_sc[j].value); |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 833 | goto error; |
| 834 | } |
| 835 | } |
| 836 | } |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 837 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 838 | break; |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 839 | case LY_TYPE_BITS: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 840 | if (type->base != LY_TYPE_BITS) { |
| 841 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name); |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 842 | goto error; |
| 843 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 844 | dertype = &type->der->type; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 845 | |
| 846 | if (!dertype->der) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 847 | if (!type->info.bits.count) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 848 | /* type is derived directly from buit-in bits type and bit statement is required */ |
| 849 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type"); |
| 850 | goto error; |
| 851 | } |
| 852 | } else { |
| 853 | for (; !dertype->info.enums.count; dertype = &dertype->der->type); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 854 | if (module->version < 2 && type->info.bits.count) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 855 | /* type is not directly derived from buit-in bits type and bit statement is prohibited, |
| 856 | * since YANG 1.1 the bit statements can be used to restrict the base bits type */ |
| 857 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit"); |
| 858 | goto error; |
| 859 | } |
| 860 | |
| 861 | bits_sc = dertype->info.bits.bit; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 862 | for (i = 0; i < type->info.bits.count; i++) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 863 | for (j = 0; j < dertype->info.bits.count; j++) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 864 | if (ly_strequal(bits_sc[j].name, type->info.bits.bit[i].name, 1)) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | } |
| 868 | if (j == dertype->info.bits.count) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 869 | LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, type->info.bits.bit[i].name); |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 870 | goto error; |
| 871 | } |
| 872 | |
| 873 | /* restricted bits type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 874 | if (type->info.bits.bit[i].flags & LYS_AUTOASSIGNED) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 875 | /* automatically assign position from base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 876 | type->info.bits.bit[i].pos = bits_sc[j].pos; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 877 | } else { |
| 878 | /* check that the assigned position corresponds to the original |
| 879 | * position of the bit in the base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 880 | if (type->info.bits.bit[i].pos != bits_sc[j].pos) { |
| 881 | /* type->info.bits.bit[i].pos - assigned position in restricted bits |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 882 | * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 883 | LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, type->info.bits.bit[i].pos, |
| 884 | type->info.bits.bit[i].name, bits_sc[j].pos); |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 885 | goto error; |
| 886 | } |
| 887 | } |
| 888 | } |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 889 | } |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 890 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 891 | for (i = type->info.bits.count - 1; i > 0; i--) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 892 | j = i; |
| 893 | |
| 894 | /* keep them ordered by position */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 895 | while (j && type->info.bits.bit[j - 1].pos > type->info.bits.bit[j].pos) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 896 | /* switch them */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 897 | memcpy(&bit_tmp, &type->info.bits.bit[j], sizeof bit_tmp); |
| 898 | memcpy(&type->info.bits.bit[j], &type->info.bits.bit[j - 1], sizeof bit_tmp); |
| 899 | memcpy(&type->info.bits.bit[j - 1], &bit_tmp, sizeof bit_tmp); |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 900 | j--; |
| 901 | } |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 902 | } |
| 903 | break; |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 904 | case LY_TYPE_LEAFREF: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 905 | if (type->base == LY_TYPE_INST) { |
| 906 | if (type->info.lref.path) { |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 907 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "path"); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 908 | goto error; |
| 909 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 910 | if ((req = type->info.lref.req)) { |
| 911 | type->info.inst.req = req; |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 912 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 913 | } else if (type->base == LY_TYPE_LEAFREF) { |
Pavol Vican | 92626d7 | 2016-09-21 09:36:09 +0200 | [diff] [blame] | 914 | /* require-instance only YANG 1.1 */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 915 | if (type->info.lref.req && (module->version < 2)) { |
Pavol Vican | 92626d7 | 2016-09-21 09:36:09 +0200 | [diff] [blame] | 916 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance"); |
| 917 | goto error; |
| 918 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 919 | /* flag resolving for later use */ |
| 920 | if (!tpdftype) { |
| 921 | for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter)); |
| 922 | if (siter) { |
| 923 | /* just a flag - do not resolve */ |
| 924 | tpdftype = 1; |
| 925 | } |
| 926 | } |
| 927 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 928 | if (type->info.lref.path) { |
| 929 | if (type->der->type.der) { |
Pavol Vican | 894ee0f | 2016-08-30 15:29:46 +0200 | [diff] [blame] | 930 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "path"); |
| 931 | goto error; |
| 932 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 933 | value = type->info.lref.path; |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 934 | /* store in the JSON format */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 935 | type->info.lref.path = transform_schema2json(module, value); |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 936 | lydict_remove(module->ctx, value); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 937 | if (!type->info.lref.path) { |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 938 | goto error; |
| 939 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 940 | /* try to resolve leafref path only when this is instantiated |
| 941 | * leaf, so it is not: |
| 942 | * - typedef's type, |
| 943 | * - in grouping definition, |
| 944 | * - just instantiated in a grouping definition, |
| 945 | * because in those cases the nodes referenced in path might not be present |
| 946 | * and it is not a bug. */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 947 | if (!tpdftype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) { |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 948 | goto error; |
| 949 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 950 | } else if (!type->der->type.der) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 951 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type"); |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 952 | goto error; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 953 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 954 | /* copy leafref definition into the derived type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 955 | type->info.lref.path = lydict_insert(module->ctx, type->der->type.info.lref.path, 0); |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 956 | /* and resolve the path at the place we are (if not in grouping/typedef) */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 957 | if (!tpdftype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 958 | goto error; |
| 959 | } |
Radek Krejci | 742be35 | 2016-07-17 12:18:54 +0200 | [diff] [blame] | 960 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 961 | /* add pointer to leafref target, only on leaves (not in typedefs) */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 962 | if (type->info.lref.target && lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 963 | goto error; |
Radek Krejci | 742be35 | 2016-07-17 12:18:54 +0200 | [diff] [blame] | 964 | } |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 965 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 966 | } else { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 967 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 968 | goto error; |
| 969 | } |
| 970 | break; |
| 971 | case LY_TYPE_IDENT: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 972 | if (type->base != LY_TYPE_IDENT) { |
| 973 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 974 | goto error; |
| 975 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 976 | if (type->der->type.der) { |
| 977 | if (type->info.ident.ref) { |
Pavol Vican | 07f220f | 2016-09-02 13:04:37 +0200 | [diff] [blame] | 978 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "base"); |
| 979 | goto error; |
| 980 | } |
| 981 | } else { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 982 | if (!type->info.ident.ref) { |
Pavol Vican | 07f220f | 2016-09-02 13:04:37 +0200 | [diff] [blame] | 983 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type"); |
| 984 | goto error; |
| 985 | } |
| 986 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 987 | break; |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 988 | case LY_TYPE_UNION: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 989 | if (type->base != LY_TYPE_UNION) { |
| 990 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 991 | goto error; |
| 992 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 993 | if (!type->info.uni.types) { |
| 994 | if (type->der->type.der) { |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 995 | /* this is just a derived type with no additional type specified/required */ |
| 996 | break; |
| 997 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 998 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type"); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 999 | goto error; |
| 1000 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 1001 | for (i = 0; i < type->info.uni.count; i++) { |
PavolVican | 811f03b | 2016-12-29 23:03:20 +0100 | [diff] [blame] | 1002 | dertype = &type->info.uni.types[i]; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 1003 | if (dertype->base == LY_TYPE_DER || dertype->base == LY_TYPE_ERR) { |
| 1004 | yang = (struct yang_type *)dertype->der; |
| 1005 | dertype->der = NULL; |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 1006 | dertype->parent = type->parent; |
| 1007 | if (yang_check_type(module, parent, yang, dertype, tpdftype, unres)) { |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 1008 | dertype->der = (struct lys_tpdf *)yang; |
| 1009 | ret = EXIT_FAILURE; |
PavolVican | 811f03b | 2016-12-29 23:03:20 +0100 | [diff] [blame] | 1010 | type->base = base_tmp; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 1011 | base = 0; |
| 1012 | goto error; |
| 1013 | } else { |
| 1014 | lydict_remove(module->ctx, yang->name); |
| 1015 | free(yang); |
| 1016 | } |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1017 | } |
Pavol Vican | 8e00e7a | 2016-09-20 11:07:48 +0200 | [diff] [blame] | 1018 | if (module->version < 2) { |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 1019 | if (dertype->base == LY_TYPE_EMPTY) { |
Pavol Vican | 8e00e7a | 2016-09-20 11:07:48 +0200 | [diff] [blame] | 1020 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", typ->name); |
| 1021 | goto error; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 1022 | } else if (dertype->base == LY_TYPE_LEAFREF) { |
Pavol Vican | 8e00e7a | 2016-09-20 11:07:48 +0200 | [diff] [blame] | 1023 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", typ->name); |
| 1024 | goto error; |
| 1025 | } |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | break; |
Pavol Vican | a182796 | 2016-02-29 15:39:42 +0100 | [diff] [blame] | 1029 | |
| 1030 | default: |
| 1031 | if (base >= LY_TYPE_BINARY && base <= LY_TYPE_UINT64) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 1032 | if (type->base != base) { |
| 1033 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name); |
Pavol Vican | a182796 | 2016-02-29 15:39:42 +0100 | [diff] [blame] | 1034 | goto error; |
| 1035 | } |
| 1036 | } else { |
| 1037 | LOGINT; |
| 1038 | goto error; |
| 1039 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1040 | } |
| 1041 | return EXIT_SUCCESS; |
| 1042 | |
| 1043 | error: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 1044 | if (type->module_name) { |
| 1045 | lydict_remove(module->ctx, type->module_name); |
| 1046 | type->module_name = NULL; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1047 | } |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 1048 | if (base) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 1049 | type->base = base_tmp; |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 1050 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1051 | return ret; |
| 1052 | } |
| 1053 | |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 1054 | void |
| 1055 | yang_free_type_union(struct ly_ctx *ctx, struct lys_type *type) |
| 1056 | { |
| 1057 | struct lys_type *stype; |
| 1058 | struct yang_type *yang; |
| 1059 | int i; |
| 1060 | |
| 1061 | for (i = 0; i < type->info.uni.count; ++i) { |
| 1062 | stype = &type->info.uni.types[i]; |
| 1063 | if (stype->base == LY_TYPE_DER || stype->base == LY_TYPE_ERR) { |
| 1064 | yang = (struct yang_type *)stype->der; |
| 1065 | stype->base = yang->base; |
| 1066 | lydict_remove(ctx, yang->name); |
| 1067 | free(yang); |
| 1068 | } else if (stype->base == LY_TYPE_UNION) { |
| 1069 | yang_free_type_union(ctx, stype); |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1074 | void * |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1075 | yang_read_type(struct lys_module *module, void *parent, char *value, enum yytokentype type) |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1076 | { |
| 1077 | struct yang_type *typ; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1078 | struct lys_deviate *dev; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1079 | |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 1080 | typ = calloc(1, sizeof *typ); |
| 1081 | if (!typ) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1082 | LOGMEM; |
| 1083 | return NULL; |
| 1084 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1085 | |
| 1086 | typ->flags = LY_YANG_STRUCTURE_FLAG; |
| 1087 | switch (type) { |
| 1088 | case LEAF_KEYWORD: |
Pavol Vican | 85427f0 | 2016-09-26 15:21:05 +0200 | [diff] [blame] | 1089 | if (((struct lys_node_leaf *)parent)->type.der) { |
| 1090 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, parent, "type", "leaf"); |
| 1091 | goto error; |
| 1092 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1093 | ((struct lys_node_leaf *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1094 | ((struct lys_node_leaf *)parent)->type.parent = (struct lys_tpdf *)parent; |
| 1095 | typ->type = &((struct lys_node_leaf *)parent)->type; |
| 1096 | break; |
Pavol Vican | a55992a | 2016-03-01 13:37:17 +0100 | [diff] [blame] | 1097 | case LEAF_LIST_KEYWORD: |
Pavol Vican | 85427f0 | 2016-09-26 15:21:05 +0200 | [diff] [blame] | 1098 | if (((struct lys_node_leaflist *)parent)->type.der) { |
| 1099 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, parent, "type", "leaf-list"); |
| 1100 | goto error; |
| 1101 | } |
Pavol Vican | a55992a | 2016-03-01 13:37:17 +0100 | [diff] [blame] | 1102 | ((struct lys_node_leaflist *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1103 | ((struct lys_node_leaflist *)parent)->type.parent = (struct lys_tpdf *)parent; |
| 1104 | typ->type = &((struct lys_node_leaflist *)parent)->type; |
| 1105 | break; |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1106 | case UNION_KEYWORD: |
| 1107 | ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ; |
| 1108 | typ->type = (struct lys_type *)parent; |
| 1109 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1110 | case TYPEDEF_KEYWORD: |
Pavol Vican | 85427f0 | 2016-09-26 15:21:05 +0200 | [diff] [blame] | 1111 | if (((struct lys_tpdf *)parent)->type.der) { |
| 1112 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "typedef"); |
| 1113 | goto error; |
| 1114 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1115 | ((struct lys_tpdf *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1116 | typ->type = &((struct lys_tpdf *)parent)->type; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1117 | break; |
| 1118 | case REPLACE_KEYWORD: |
| 1119 | /* deviation replace type*/ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1120 | dev = (struct lys_deviate *)parent; |
| 1121 | if (dev->type) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1122 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "deviation"); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1123 | goto error; |
| 1124 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1125 | dev->type = calloc(1, sizeof *dev->type); |
| 1126 | if (!dev->type) { |
| 1127 | LOGMEM; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1128 | goto error; |
| 1129 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1130 | dev->type->der = (struct lys_tpdf *)typ; |
| 1131 | typ->type = dev->type; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1132 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1133 | default: |
| 1134 | goto error; |
| 1135 | break; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1136 | } |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1137 | typ->name = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1138 | return typ; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1139 | |
| 1140 | error: |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1141 | free(value); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1142 | free(typ); |
| 1143 | return NULL; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1147 | yang_read_length(struct lys_module *module, struct yang_type *typ, char *value) |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1148 | { |
| 1149 | struct lys_restr **length; |
| 1150 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1151 | if (typ->base == 0 || typ->base == LY_TYPE_STRING) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1152 | length = &typ->type->info.str.length; |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1153 | typ->base = LY_TYPE_STRING; |
| 1154 | } else if (typ->base == LY_TYPE_BINARY) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1155 | length = &typ->type->info.binary.length; |
| 1156 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1157 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected length statement."); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1158 | goto error; |
| 1159 | } |
| 1160 | |
| 1161 | if (*length) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1162 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "length", "type"); |
Pavol Vican | be9e383 | 2016-04-28 02:21:37 +0200 | [diff] [blame] | 1163 | goto error; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1164 | } |
| 1165 | *length = calloc(1, sizeof **length); |
| 1166 | if (!*length) { |
| 1167 | LOGMEM; |
| 1168 | goto error; |
| 1169 | } |
| 1170 | (*length)->expr = lydict_insert_zc(module->ctx, value); |
| 1171 | return *length; |
| 1172 | |
| 1173 | error: |
| 1174 | free(value); |
| 1175 | return NULL; |
| 1176 | |
| 1177 | } |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1178 | |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1179 | int |
| 1180 | yang_read_pattern(struct lys_module *module, struct lys_restr *pattern, char *value, char modifier) |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1181 | { |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1182 | char *buf; |
| 1183 | size_t len; |
| 1184 | |
Michal Vasko | 0aee5c1 | 2016-06-17 14:27:26 +0200 | [diff] [blame] | 1185 | if (lyp_check_pattern(value, NULL)) { |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1186 | free(value); |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1187 | return EXIT_FAILURE; |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1188 | } |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1189 | |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1190 | len = strlen(value); |
| 1191 | buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */ |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1192 | |
| 1193 | if (!buf) { |
| 1194 | LOGMEM; |
| 1195 | free(value); |
| 1196 | return EXIT_FAILURE; |
| 1197 | } |
| 1198 | |
| 1199 | buf[0] = modifier; |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1200 | strcpy(&buf[1], value); |
| 1201 | free(value); |
| 1202 | |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1203 | pattern->expr = lydict_insert_zc(module->ctx, buf); |
| 1204 | return EXIT_SUCCESS; |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1205 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1206 | |
| 1207 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1208 | yang_read_range(struct lys_module *module, struct yang_type *typ, char *value) |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1209 | { |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1210 | if (typ->base != 0 && typ->base != LY_TYPE_DEC64) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1211 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected range statement."); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1212 | goto error; |
| 1213 | } |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1214 | typ->base = LY_TYPE_DEC64; |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1215 | if (typ->type->info.dec64.range) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1216 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "range", "type"); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1217 | goto error; |
| 1218 | } |
| 1219 | typ->type->info.dec64.range = calloc(1, sizeof *typ->type->info.dec64.range); |
| 1220 | if (!typ->type->info.dec64.range) { |
| 1221 | LOGMEM; |
| 1222 | goto error; |
| 1223 | } |
| 1224 | typ->type->info.dec64.range->expr = lydict_insert_zc(module->ctx, value); |
| 1225 | return typ->type->info.dec64.range; |
| 1226 | |
| 1227 | error: |
| 1228 | free(value); |
| 1229 | return NULL; |
| 1230 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1231 | |
| 1232 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1233 | yang_read_fraction(struct yang_type *typ, uint32_t value) |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1234 | { |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1235 | unsigned int i; |
| 1236 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1237 | if (typ->base == 0 || typ->base == LY_TYPE_DEC64) { |
| 1238 | typ->base = LY_TYPE_DEC64; |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1239 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1240 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected fraction-digits statement."); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1241 | goto error; |
| 1242 | } |
| 1243 | if (typ->type->info.dec64.dig) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1244 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "fraction-digits", "type"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1245 | goto error; |
| 1246 | } |
| 1247 | /* range check */ |
| 1248 | if (value < 1 || value > 18) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1249 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", value, "fraction-digits"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1250 | goto error; |
| 1251 | } |
| 1252 | typ->type->info.dec64.dig = value; |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1253 | typ->type->info.dec64.div = 10; |
| 1254 | for (i = 1; i < value; i++) { |
| 1255 | typ->type->info.dec64.div *= 10; |
| 1256 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1257 | return EXIT_SUCCESS; |
| 1258 | |
| 1259 | error: |
| 1260 | return EXIT_FAILURE; |
| 1261 | } |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1262 | |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1263 | int |
| 1264 | yang_read_enum(struct lys_module *module, struct yang_type *typ, struct lys_type_enum *enm, char *value) |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1265 | { |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1266 | int i, j; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1267 | |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 1268 | typ->base = LY_TYPE_ENUM; |
Pavol Vican | c666241 | 2016-08-30 08:06:28 +0200 | [diff] [blame] | 1269 | if (!value[0]) { |
| 1270 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name"); |
| 1271 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty."); |
| 1272 | free(value); |
| 1273 | goto error; |
| 1274 | } |
| 1275 | |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1276 | enm->name = lydict_insert_zc(module->ctx, value); |
| 1277 | |
| 1278 | /* the assigned name MUST NOT have any leading or trailing whitespace characters */ |
| 1279 | if (isspace(enm->name[0]) || isspace(enm->name[strlen(enm->name) - 1])) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1280 | LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1281 | goto error; |
| 1282 | } |
| 1283 | |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1284 | j = typ->type->info.enums.count - 1; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1285 | /* check the name uniqueness */ |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1286 | for (i = 0; i < j; i++) { |
| 1287 | if (!strcmp(typ->type->info.enums.enm[i].name, typ->type->info.enums.enm[j].name)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1288 | LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1289 | goto error; |
| 1290 | } |
| 1291 | } |
| 1292 | |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1293 | return EXIT_SUCCESS; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1294 | |
| 1295 | error: |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1296 | return EXIT_FAILURE; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1300 | yang_check_enum(struct yang_type *typ, struct lys_type_enum *enm, int64_t *value, int assign) |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1301 | { |
| 1302 | int i, j; |
| 1303 | |
| 1304 | if (!assign) { |
| 1305 | /* assign value automatically */ |
| 1306 | if (*value > INT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1307 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value"); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1308 | goto error; |
| 1309 | } |
| 1310 | enm->value = *value; |
| 1311 | enm->flags |= LYS_AUTOASSIGNED; |
| 1312 | (*value)++; |
Pavol Vican | 5de389c | 2016-08-30 08:55:15 +0200 | [diff] [blame] | 1313 | } else if (typ->type->info.enums.enm == enm) { |
| 1314 | /* change value, which is assigned automatically, if first enum has value. */ |
| 1315 | *value = typ->type->info.enums.enm[0].value; |
| 1316 | (*value)++; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | /* check that the value is unique */ |
| 1320 | j = typ->type->info.enums.count-1; |
| 1321 | for (i = 0; i < j; i++) { |
| 1322 | if (typ->type->info.enums.enm[i].value == typ->type->info.enums.enm[j].value) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1323 | LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL, |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1324 | typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name, |
| 1325 | typ->type->info.enums.enm[i].name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1326 | goto error; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | return EXIT_SUCCESS; |
| 1331 | |
| 1332 | error: |
| 1333 | return EXIT_FAILURE; |
| 1334 | } |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1335 | |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1336 | int |
| 1337 | yang_read_bit(struct lys_module *module, struct yang_type *typ, struct lys_type_bit *bit, char *value) |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1338 | { |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1339 | int i, j; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1340 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 1341 | typ->base = LY_TYPE_BITS; |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1342 | bit->name = lydict_insert_zc(module->ctx, value); |
| 1343 | if (lyp_check_identifier(bit->name, LY_IDENT_SIMPLE, NULL, NULL)) { |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1344 | free(value); |
| 1345 | goto error; |
| 1346 | } |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1347 | |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1348 | j = typ->type->info.bits.count - 1; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1349 | /* check the name uniqueness */ |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1350 | for (i = 0; i < j; i++) { |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1351 | if (!strcmp(typ->type->info.bits.bit[i].name, bit->name)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1352 | LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1353 | goto error; |
| 1354 | } |
| 1355 | } |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1356 | return EXIT_SUCCESS; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1357 | |
| 1358 | error: |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1359 | return EXIT_FAILURE; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1363 | yang_check_bit(struct yang_type *typ, struct lys_type_bit *bit, int64_t *value, int assign) |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1364 | { |
| 1365 | int i,j; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1366 | |
| 1367 | if (!assign) { |
| 1368 | /* assign value automatically */ |
| 1369 | if (*value > UINT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1370 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position"); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1371 | goto error; |
| 1372 | } |
| 1373 | bit->pos = (uint32_t)*value; |
| 1374 | bit->flags |= LYS_AUTOASSIGNED; |
| 1375 | (*value)++; |
| 1376 | } |
| 1377 | |
| 1378 | j = typ->type->info.bits.count - 1; |
| 1379 | /* check that the value is unique */ |
| 1380 | for (i = 0; i < j; i++) { |
| 1381 | if (typ->type->info.bits.bit[i].pos == bit->pos) { |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1382 | LOGVAL(LYE_BITS_DUPVAL, LY_VLOG_NONE, NULL, bit->pos, bit->name, typ->type->info.bits.bit[i].name); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1383 | goto error; |
| 1384 | } |
| 1385 | } |
| 1386 | |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1387 | return EXIT_SUCCESS; |
| 1388 | |
| 1389 | error: |
| 1390 | return EXIT_FAILURE; |
| 1391 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1392 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 1393 | int |
| 1394 | yang_read_augment(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *aug, char *value) |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1395 | { |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1396 | aug->nodetype = LYS_AUGMENT; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1397 | aug->target_name = transform_schema2json(module, value); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1398 | free(value); |
| 1399 | if (!aug->target_name) { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 1400 | return EXIT_FAILURE; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1401 | } |
| 1402 | aug->parent = parent; |
| 1403 | aug->module = module; |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 1404 | return EXIT_SUCCESS; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1405 | } |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1406 | |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1407 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1408 | yang_read_deviate_unsupported(struct lys_deviation *dev) |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1409 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1410 | if (dev->deviate_size) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1411 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation."); |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1412 | return EXIT_FAILURE; |
| 1413 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1414 | dev->deviate = calloc(1, sizeof *dev->deviate); |
| 1415 | dev->deviate[dev->deviate_size].mod = LY_DEVIATE_NO; |
| 1416 | dev->deviate_size = 1; |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1417 | return EXIT_SUCCESS; |
| 1418 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1419 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1420 | void * |
| 1421 | yang_read_deviate(struct lys_deviation *dev, LYS_DEVIATE_TYPE mod) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1422 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1423 | struct lys_deviate *deviate; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1424 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1425 | if (dev->deviate && dev->deviate[0].mod == LY_DEVIATE_NO) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1426 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported"); |
| 1427 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation."); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1428 | return NULL; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1429 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1430 | if (!(dev->deviate_size % LY_YANG_ARRAY_SIZE)) { |
| 1431 | deviate = realloc(dev->deviate, (LY_YANG_ARRAY_SIZE + dev->deviate_size) * sizeof *deviate); |
| 1432 | if (!deviate) { |
| 1433 | LOGMEM; |
| 1434 | return NULL; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1435 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1436 | memset(deviate + dev->deviate_size, 0, LY_YANG_ARRAY_SIZE * sizeof *deviate); |
| 1437 | dev->deviate = deviate; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1438 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1439 | dev->deviate[dev->deviate_size].mod = mod; |
| 1440 | return &dev->deviate[dev->deviate_size++]; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1444 | yang_read_deviate_units(struct ly_ctx *ctx, struct lys_deviate *deviate, struct lys_node *dev_target) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1445 | { |
| 1446 | const char **stritem; |
| 1447 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1448 | /* check target node type */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1449 | if (dev_target->nodetype == LYS_LEAFLIST) { |
| 1450 | stritem = &((struct lys_node_leaflist *)dev_target)->units; |
| 1451 | } else if (dev_target->nodetype == LYS_LEAF) { |
| 1452 | stritem = &((struct lys_node_leaf *)dev_target)->units; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1453 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1454 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1455 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"units\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1456 | goto error; |
| 1457 | } |
| 1458 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1459 | if (deviate->mod == LY_DEVIATE_DEL) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1460 | /* check values */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1461 | if (!ly_strequal(*stritem, deviate->units, 1)) { |
| 1462 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, deviate->units, "units"); |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1463 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1464 | goto error; |
| 1465 | } |
| 1466 | /* remove current units value of the target */ |
| 1467 | lydict_remove(ctx, *stritem); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1468 | } else { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1469 | if (deviate->mod == LY_DEVIATE_ADD) { |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1470 | /* check that there is no current value */ |
| 1471 | if (*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1472 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1473 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1474 | goto error; |
| 1475 | } |
| 1476 | } else { /* replace */ |
| 1477 | if (!*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1478 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1479 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1480 | goto error; |
| 1481 | } |
| 1482 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1483 | /* remove current units value of the target ... */ |
| 1484 | lydict_remove(ctx, *stritem); |
| 1485 | |
| 1486 | /* ... and replace it with the value specified in deviation */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1487 | *stritem = lydict_insert(ctx, deviate->units, 0); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1488 | } |
| 1489 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1490 | return EXIT_SUCCESS; |
| 1491 | |
| 1492 | error: |
| 1493 | return EXIT_FAILURE; |
| 1494 | } |
| 1495 | |
| 1496 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1497 | yang_read_deviate_unique(struct lys_deviate *deviate, struct lys_node *dev_target) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1498 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1499 | struct lys_node_list *list; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1500 | struct lys_unique *unique; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1501 | |
| 1502 | /* check target node type */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1503 | if (dev_target->nodetype != LYS_LIST) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1504 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique"); |
| 1505 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1506 | goto error; |
| 1507 | } |
| 1508 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1509 | list = (struct lys_node_list *)dev_target; |
| 1510 | if (deviate->mod == LY_DEVIATE_ADD) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1511 | /* reallocate the unique array of the target */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1512 | unique = ly_realloc(list->unique, (deviate->unique_size + list->unique_size) * sizeof *unique); |
| 1513 | if (!unique) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1514 | LOGMEM; |
| 1515 | goto error; |
| 1516 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1517 | list->unique = unique; |
| 1518 | memset(unique + list->unique_size, 0, deviate->unique_size * sizeof *unique); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | return EXIT_SUCCESS; |
| 1522 | |
| 1523 | error: |
| 1524 | return EXIT_FAILURE; |
| 1525 | } |
| 1526 | |
| 1527 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1528 | yang_fill_deviate_default(struct ly_ctx *ctx, struct lys_deviate *deviate, struct lys_node *dev_target, |
| 1529 | struct ly_set *dflt_check, const char *value) |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1530 | { |
| 1531 | struct lys_node *node; |
| 1532 | struct lys_node_choice *choice; |
| 1533 | struct lys_node_leaf *leaf; |
| 1534 | struct lys_node_leaflist *llist; |
| 1535 | int rc, i; |
| 1536 | unsigned int u; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1537 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1538 | u = strlen(value); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1539 | if (dev_target->nodetype == LYS_CHOICE) { |
| 1540 | choice = (struct lys_node_choice *)dev_target; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1541 | rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node); |
| 1542 | if (rc || !node) { |
| 1543 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1544 | goto error; |
| 1545 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1546 | if (deviate->mod == LY_DEVIATE_DEL) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1547 | if (!choice->dflt || (choice->dflt != node)) { |
| 1548 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1549 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
| 1550 | goto error; |
| 1551 | } |
| 1552 | } else { /* add or replace */ |
| 1553 | choice->dflt = node; |
| 1554 | if (!choice->dflt) { |
| 1555 | /* default branch not found */ |
| 1556 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1557 | goto error; |
| 1558 | } |
| 1559 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1560 | } else if (dev_target->nodetype == LYS_LEAF) { |
| 1561 | leaf = (struct lys_node_leaf *)dev_target; |
| 1562 | if (deviate->mod == LY_DEVIATE_DEL) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1563 | if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) { |
| 1564 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1565 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
| 1566 | goto error; |
| 1567 | } |
| 1568 | /* remove value */ |
| 1569 | lydict_remove(ctx, leaf->dflt); |
| 1570 | leaf->dflt = NULL; |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1571 | leaf->flags &= ~LYS_DFLTJSON; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1572 | } else { /* add (already checked) and replace */ |
| 1573 | /* remove value */ |
| 1574 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1575 | leaf->flags &= ~LYS_DFLTJSON; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1576 | |
| 1577 | /* set new value */ |
| 1578 | leaf->dflt = lydict_insert(ctx, value, u); |
| 1579 | |
| 1580 | /* remember to check it later (it may not fit now, but the type can be deviated too) */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1581 | ly_set_add(dflt_check, dev_target, 0); |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1582 | } |
| 1583 | } else { /* LYS_LEAFLIST */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1584 | llist = (struct lys_node_leaflist *)dev_target; |
| 1585 | if (deviate->mod == LY_DEVIATE_DEL) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1586 | /* find and remove the value in target list */ |
| 1587 | for (i = 0; i < llist->dflt_size; i++) { |
| 1588 | if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) { |
| 1589 | /* match, remove the value */ |
| 1590 | lydict_remove(llist->module->ctx, llist->dflt[i]); |
| 1591 | llist->dflt[i] = NULL; |
| 1592 | break; |
| 1593 | } |
| 1594 | } |
| 1595 | if (i == llist->dflt_size) { |
| 1596 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1597 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node."); |
| 1598 | goto error; |
| 1599 | } |
| 1600 | } else { |
| 1601 | /* add or replace, anyway we place items into the deviate's list |
| 1602 | which propagates to the target */ |
| 1603 | /* we just want to check that the value isn't already in the list */ |
| 1604 | for (i = 0; i < llist->dflt_size; i++) { |
| 1605 | if (ly_strequal(llist->dflt[i], value, 1)) { |
| 1606 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1607 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value); |
| 1608 | goto error; |
| 1609 | } |
| 1610 | } |
| 1611 | /* store it in target node */ |
| 1612 | llist->dflt[llist->dflt_size++] = lydict_insert(ctx, value, u); |
| 1613 | |
| 1614 | /* remember to check it later (it may not fit now, but the type can be deviated too) */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1615 | ly_set_add(dflt_check, dev_target, 0); |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1616 | llist->flags &= ~LYS_DFLTJSON; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | return EXIT_SUCCESS; |
| 1621 | error: |
| 1622 | return EXIT_FAILURE; |
| 1623 | } |
| 1624 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1625 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1626 | yang_read_deviate_default(struct lys_module *module, struct lys_deviate *deviate, |
| 1627 | struct lys_node *dev_target, struct ly_set * dflt_check) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1628 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1629 | int i; |
| 1630 | struct lys_node_leaflist *llist; |
| 1631 | const char **dflt; |
| 1632 | |
| 1633 | /* check target node type */ |
| 1634 | if (module->version < 2 && dev_target->nodetype == LYS_LEAFLIST) { |
| 1635 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1636 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property."); |
| 1637 | goto error; |
| 1638 | } else if (deviate->dflt_size > 1 && dev_target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */ |
| 1639 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1640 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties."); |
| 1641 | goto error; |
| 1642 | } else if (!(dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 1643 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1644 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1645 | goto error; |
| 1646 | } |
| 1647 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1648 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 1649 | /* check that there is no current value */ |
| 1650 | if ((dev_target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev_target)->dflt) || |
| 1651 | (dev_target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev_target)->dflt)) { |
| 1652 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1653 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
| 1654 | goto error; |
| 1655 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1656 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1657 | /* check collision with mandatory/min-elements */ |
| 1658 | if ((dev_target->flags & LYS_MAND_TRUE) || |
| 1659 | (dev_target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev_target)->min)) { |
| 1660 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "deviation"); |
| 1661 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 1662 | "Adding the \"default\" statement is forbidden on %s statement.", |
| 1663 | (dev_target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\""); |
| 1664 | goto error; |
| 1665 | } |
| 1666 | } else if (deviate->mod == LY_DEVIATE_RPL) { |
| 1667 | /* check that there was a value before */ |
| 1668 | if (((dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((struct lys_node_leaf *)dev_target)->dflt) || |
| 1669 | (dev_target->nodetype == LYS_CHOICE && !((struct lys_node_choice *)dev_target)->dflt)) { |
| 1670 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1671 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
| 1672 | goto error; |
| 1673 | } |
| 1674 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1675 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1676 | if (dev_target->nodetype == LYS_LEAFLIST) { |
| 1677 | /* reallocate default list in the target */ |
| 1678 | llist = (struct lys_node_leaflist *)dev_target; |
| 1679 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 1680 | /* reallocate (enlarge) the unique array of the target */ |
| 1681 | dflt = realloc(llist->dflt, (deviate->dflt_size + llist->dflt_size) * sizeof *dflt); |
| 1682 | if (!dflt) { |
| 1683 | LOGMEM; |
| 1684 | goto error; |
| 1685 | } |
| 1686 | llist->dflt = dflt; |
| 1687 | } else if (deviate->mod == LY_DEVIATE_RPL) { |
| 1688 | /* reallocate (replace) the unique array of the target */ |
| 1689 | for (i = 0; i < llist->dflt_size; i++) { |
| 1690 | lydict_remove(llist->module->ctx, llist->dflt[i]); |
| 1691 | } |
| 1692 | dflt = realloc(llist->dflt, deviate->dflt_size * sizeof *dflt); |
| 1693 | if (!dflt) { |
| 1694 | LOGMEM; |
| 1695 | goto error; |
| 1696 | } |
| 1697 | llist->dflt = dflt; |
| 1698 | llist->dflt_size = 0; |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | for (i = 0; i < deviate->dflt_size; ++i) { |
| 1703 | if (yang_fill_deviate_default(module->ctx, deviate, dev_target, dflt_check, deviate->dflt[i])) { |
| 1704 | goto error; |
| 1705 | } |
| 1706 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1707 | |
| 1708 | return EXIT_SUCCESS; |
| 1709 | |
| 1710 | error: |
| 1711 | return EXIT_FAILURE; |
| 1712 | } |
| 1713 | |
| 1714 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1715 | yang_check_deviate_mandatory(struct lys_deviate *deviate, struct lys_node *dev_target) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1716 | { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 1717 | struct lys_node *parent; |
| 1718 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1719 | /* check target node type */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1720 | if (!(dev_target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1721 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1722 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"mandatory\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1723 | goto error; |
| 1724 | } |
| 1725 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1726 | if (deviate->mod == LY_DEVIATE_ADD) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1727 | /* check that there is no current value */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1728 | if (dev_target->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1729 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1730 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1731 | goto error; |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 1732 | } else { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1733 | if (dev_target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev_target)->dflt) { |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 1734 | /* RFC 6020, 7.6.4 - default statement must not with mandatory true */ |
| 1735 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf"); |
| 1736 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 1737 | goto error; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1738 | } else if (dev_target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev_target)->dflt) { |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 1739 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice"); |
| 1740 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 1741 | goto error; |
| 1742 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1743 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1744 | } else { /* replace */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1745 | if (!(dev_target->flags & LYS_MAND_MASK)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1746 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1747 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1748 | goto error; |
| 1749 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1750 | } |
| 1751 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1752 | /* remove current mandatory value of the target ... */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1753 | dev_target->flags &= ~LYS_MAND_MASK; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1754 | |
| 1755 | /* ... and replace it with the value specified in deviation */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1756 | dev_target->flags |= deviate->flags & LYS_MAND_MASK; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1757 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 1758 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1759 | for (parent = dev_target->parent; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 1760 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION)); |
| 1761 | parent = parent->parent) { |
| 1762 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 1763 | /* stop also on presence containers */ |
| 1764 | break; |
| 1765 | } |
| 1766 | } |
| 1767 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 1768 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 1769 | if (lyp_check_mandatory_choice(parent)) { |
| 1770 | goto error; |
| 1771 | } |
| 1772 | } |
| 1773 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1774 | return EXIT_SUCCESS; |
| 1775 | |
| 1776 | error: |
| 1777 | return EXIT_FAILURE; |
| 1778 | } |
| 1779 | |
| 1780 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1781 | yang_read_deviate_minmax(struct lys_deviate *deviate, struct lys_node *dev_target, uint32_t value, int type) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1782 | { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 1783 | uint32_t *ui32val, *min, *max; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1784 | |
| 1785 | /* check target node type */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1786 | if (dev_target->nodetype == LYS_LEAFLIST) { |
| 1787 | max = &((struct lys_node_leaflist *)dev_target)->max; |
| 1788 | min = &((struct lys_node_leaflist *)dev_target)->min; |
| 1789 | } else if (dev_target->nodetype == LYS_LIST) { |
| 1790 | max = &((struct lys_node_list *)dev_target)->max; |
| 1791 | min = &((struct lys_node_list *)dev_target)->min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1792 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1793 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 1794 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", (type) ? "max-elements" : "min-elements"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1795 | goto error; |
| 1796 | } |
| 1797 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1798 | ui32val = (type) ? max : min; |
| 1799 | if (deviate->mod == LY_DEVIATE_ADD) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1800 | /* check that there is no current value */ |
| 1801 | if (*ui32val) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1802 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 1803 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1804 | goto error; |
| 1805 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1806 | } else if (deviate->mod == LY_DEVIATE_RPL) { |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1807 | /* unfortunately, there is no way to check reliably that there |
| 1808 | * was a value before, it could have been the default */ |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /* add (already checked) and replace */ |
| 1812 | /* set new value specified in deviation */ |
| 1813 | *ui32val = value; |
| 1814 | |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 1815 | /* check min-elements is smaller than max-elements */ |
| 1816 | if (*max && *min > *max) { |
| 1817 | if (type) { |
| 1818 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"max-elements\".", value); |
| 1819 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\"."); |
| 1820 | } else { |
| 1821 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"min-elements\".", value); |
| 1822 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 1823 | } |
| 1824 | goto error; |
| 1825 | } |
| 1826 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1827 | return EXIT_SUCCESS; |
| 1828 | |
| 1829 | error: |
| 1830 | return EXIT_FAILURE; |
| 1831 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1832 | |
| 1833 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1834 | yang_check_deviate_must(struct lys_module *module, struct unres_schema *unres, |
| 1835 | struct lys_deviate *deviate, struct lys_node *dev_target) |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1836 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1837 | int i, j, erase_must = 1; |
| 1838 | struct lys_restr **trg_must, *must; |
| 1839 | uint8_t *trg_must_size, must_size; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1840 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1841 | /* check target node type */ |
| 1842 | switch (dev_target->nodetype) { |
| 1843 | case LYS_LEAF: |
| 1844 | trg_must = &((struct lys_node_leaf *)dev_target)->must; |
| 1845 | trg_must_size = &((struct lys_node_leaf *)dev_target)->must_size; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1846 | break; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1847 | case LYS_CONTAINER: |
| 1848 | trg_must = &((struct lys_node_container *)dev_target)->must; |
| 1849 | trg_must_size = &((struct lys_node_container *)dev_target)->must_size; |
| 1850 | break; |
| 1851 | case LYS_LEAFLIST: |
| 1852 | trg_must = &((struct lys_node_leaflist *)dev_target)->must; |
| 1853 | trg_must_size = &((struct lys_node_leaflist *)dev_target)->must_size; |
| 1854 | break; |
| 1855 | case LYS_LIST: |
| 1856 | trg_must = &((struct lys_node_list *)dev_target)->must; |
| 1857 | trg_must_size = &((struct lys_node_list *)dev_target)->must_size; |
| 1858 | break; |
| 1859 | case LYS_ANYXML: |
| 1860 | case LYS_ANYDATA: |
| 1861 | trg_must = &((struct lys_node_anydata *)dev_target)->must; |
| 1862 | trg_must_size = &((struct lys_node_anydata *)dev_target)->must_size; |
| 1863 | break; |
| 1864 | default: |
| 1865 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must"); |
| 1866 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property."); |
| 1867 | goto error; |
| 1868 | } |
| 1869 | |
| 1870 | /* flag will be checked again, clear it for now */ |
| 1871 | dev_target->flags &= ~LYS_VALID_DEP; |
| 1872 | |
| 1873 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 1874 | /* reallocate the must array of the target */ |
| 1875 | must = ly_realloc(*trg_must, (deviate->must_size + *trg_must_size) * sizeof *must); |
| 1876 | if (!must) { |
| 1877 | LOGMEM; |
| 1878 | goto error; |
| 1879 | } |
| 1880 | *trg_must = must; |
| 1881 | must_size = *trg_must_size; |
| 1882 | for (i = 0; i < deviate->must_size; ++i) { |
| 1883 | (*trg_must)[must_size].expr = deviate->must[i].expr; |
| 1884 | (*trg_must)[must_size].dsc = deviate->must[i].dsc; |
| 1885 | (*trg_must)[must_size].ref = deviate->must[i].ref; |
| 1886 | (*trg_must)[must_size].emsg = deviate->must[i].emsg; |
| 1887 | (*trg_must)[must_size].eapptag = deviate->must[i].eapptag; |
| 1888 | ++must_size; |
| 1889 | } |
| 1890 | free(deviate->must); |
| 1891 | deviate->must = &must[*trg_must_size]; |
| 1892 | *trg_must_size = must_size; |
| 1893 | erase_must = 0; |
| 1894 | } else if (deviate->mod == LY_DEVIATE_DEL) { |
| 1895 | /* find must to delete, we are ok with just matching conditions */ |
| 1896 | for (j = 0; j < deviate->must_size; ++j) { |
| 1897 | for (i = 0; i < *trg_must_size; i++) { |
| 1898 | if (ly_strequal(deviate->must[j].expr, (*trg_must)[i].expr, 1)) { |
| 1899 | /* we have a match, free the must structure ... */ |
| 1900 | lys_restr_free(module->ctx, &((*trg_must)[i])); |
| 1901 | /* ... and maintain the array */ |
| 1902 | (*trg_must_size)--; |
| 1903 | if (i != *trg_must_size) { |
| 1904 | (*trg_must)[i].expr = (*trg_must)[*trg_must_size].expr; |
| 1905 | (*trg_must)[i].dsc = (*trg_must)[*trg_must_size].dsc; |
| 1906 | (*trg_must)[i].ref = (*trg_must)[*trg_must_size].ref; |
| 1907 | (*trg_must)[i].eapptag = (*trg_must)[*trg_must_size].eapptag; |
| 1908 | (*trg_must)[i].emsg = (*trg_must)[*trg_must_size].emsg; |
| 1909 | } |
| 1910 | if (!(*trg_must_size)) { |
| 1911 | free(*trg_must); |
| 1912 | *trg_must = NULL; |
| 1913 | } else { |
| 1914 | (*trg_must)[*trg_must_size].expr = NULL; |
| 1915 | (*trg_must)[*trg_must_size].dsc = NULL; |
| 1916 | (*trg_must)[*trg_must_size].ref = NULL; |
| 1917 | (*trg_must)[*trg_must_size].eapptag = NULL; |
| 1918 | (*trg_must)[*trg_must_size].emsg = NULL; |
| 1919 | } |
| 1920 | |
| 1921 | i = -1; /* set match flag */ |
| 1922 | break; |
| 1923 | } |
| 1924 | } |
| 1925 | if (i != -1) { |
| 1926 | /* no match found */ |
| 1927 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, deviate->must[j].expr, "must"); |
| 1928 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target."); |
| 1929 | goto error; |
| 1930 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1931 | } |
| 1932 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1933 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1934 | /* check XPath dependencies */ |
| 1935 | if (*trg_must_size && unres_schema_add_node(module, unres, dev_target, UNRES_XPATH, NULL)) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1936 | goto error; |
| 1937 | } |
| 1938 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1939 | return EXIT_SUCCESS; |
| 1940 | error: |
| 1941 | if (deviate->mod == LY_DEVIATE_ADD && erase_must) { |
| 1942 | for (i = 0; i < deviate->must_size; ++i) { |
| 1943 | lys_restr_free(module->ctx, &deviate->must[i]); |
| 1944 | } |
| 1945 | free(deviate->must); |
| 1946 | } |
| 1947 | return EXIT_FAILURE; |
| 1948 | } |
| 1949 | |
| 1950 | int |
| 1951 | yang_deviate_delete_unique(struct lys_module *module, struct lys_deviate *deviate, |
| 1952 | struct lys_node_list *list, int index, char * value) |
| 1953 | { |
| 1954 | int i, j; |
| 1955 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1956 | /* find unique structures to delete */ |
| 1957 | for (i = 0; i < list->unique_size; i++) { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1958 | if (list->unique[i].expr_size != deviate->unique[index].expr_size) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1959 | continue; |
| 1960 | } |
| 1961 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1962 | for (j = 0; j < deviate->unique[index].expr_size; j++) { |
| 1963 | if (!ly_strequal(list->unique[i].expr[j], deviate->unique[index].expr[j], 1)) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1964 | break; |
| 1965 | } |
| 1966 | } |
| 1967 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1968 | if (j == deviate->unique[index].expr_size) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1969 | /* we have a match, free the unique structure ... */ |
| 1970 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 1971 | lydict_remove(module->ctx, list->unique[i].expr[j]); |
| 1972 | } |
| 1973 | free(list->unique[i].expr); |
| 1974 | /* ... and maintain the array */ |
| 1975 | list->unique_size--; |
| 1976 | if (i != list->unique_size) { |
| 1977 | list->unique[i].expr_size = list->unique[list->unique_size].expr_size; |
| 1978 | list->unique[i].expr = list->unique[list->unique_size].expr; |
| 1979 | } |
| 1980 | |
| 1981 | if (!list->unique_size) { |
| 1982 | free(list->unique); |
| 1983 | list->unique = NULL; |
| 1984 | } else { |
| 1985 | list->unique[list->unique_size].expr_size = 0; |
| 1986 | list->unique[list->unique_size].expr = NULL; |
| 1987 | } |
| 1988 | |
| 1989 | i = -1; /* set match flag */ |
| 1990 | break; |
| 1991 | } |
| 1992 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1993 | |
| 1994 | if (i != -1) { |
| 1995 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1996 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique"); |
| 1997 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1998 | return EXIT_FAILURE; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1999 | } |
| 2000 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2001 | return EXIT_SUCCESS; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2002 | } |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2003 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2004 | int yang_check_deviate_unique(struct lys_module *module, struct lys_deviate *deviate, struct lys_node *dev_target) |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2005 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2006 | struct lys_node_list *list; |
| 2007 | char *str; |
| 2008 | uint i; |
| 2009 | struct lys_unique *last_unique; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2010 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2011 | if (yang_read_deviate_unique(deviate, dev_target)) { |
| 2012 | goto error; |
| 2013 | } |
| 2014 | list = (struct lys_node_list *)dev_target; |
| 2015 | last_unique = &list->unique[list->unique_size]; |
| 2016 | for (i = 0; i < deviate->unique_size; ++i) { |
| 2017 | str = (char *) deviate->unique[i].expr; |
| 2018 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 2019 | if (yang_fill_unique(module, list, &list->unique[list->unique_size], str, NULL)) { |
| 2020 | free(str); |
| 2021 | goto error; |
| 2022 | } |
| 2023 | list->unique_size++; |
| 2024 | } else if (deviate->mod == LY_DEVIATE_DEL) { |
| 2025 | if (yang_fill_unique(module, list, &deviate->unique[i], str, NULL)) { |
| 2026 | free(str); |
| 2027 | goto error; |
| 2028 | } |
| 2029 | if (yang_deviate_delete_unique(module, deviate, list, i, str)) { |
| 2030 | free(str); |
| 2031 | goto error; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2032 | } |
| 2033 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2034 | free(str); |
| 2035 | } |
| 2036 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 2037 | free(deviate->unique); |
| 2038 | deviate->unique = last_unique; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2039 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2040 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2041 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2042 | return EXIT_SUCCESS; |
| 2043 | error: |
| 2044 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 2045 | for (i = i + 1; i < deviate->unique_size; ++i) { |
| 2046 | free(deviate->unique[i].expr); |
| 2047 | } |
| 2048 | free(deviate->unique); |
| 2049 | deviate->unique = last_unique; |
| 2050 | |
| 2051 | } |
| 2052 | return EXIT_FAILURE; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2055 | static int |
| 2056 | yang_fill_include(struct lys_module *trg, char *value, struct lys_include *inc, |
| 2057 | struct unres_schema *unres) |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2058 | { |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2059 | struct lys_submodule *submodule; |
| 2060 | struct lys_module *module; |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2061 | const char *str; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2062 | int rc; |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2063 | int ret = 0; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2064 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2065 | str = lydict_insert_zc(trg->ctx, value); |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 2066 | if (trg->type) { |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2067 | submodule = (struct lys_submodule *)trg; |
| 2068 | module = ((struct lys_submodule *)trg)->belongsto; |
| 2069 | } else { |
| 2070 | submodule = NULL; |
| 2071 | module = trg; |
| 2072 | } |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2073 | rc = lyp_check_include(module, submodule, str, inc, unres); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2074 | if (!rc) { |
| 2075 | /* success, copy the filled data into the final array */ |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2076 | memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc); |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 2077 | trg->inc_size++; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2078 | } else if (rc == -1) { |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2079 | ret = -1; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2080 | } |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2081 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2082 | lydict_remove(trg->ctx, str); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2083 | return ret; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2084 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2085 | |
| 2086 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2087 | yang_use_extension(struct lys_module *module, struct lys_node *data_node, void *actual, char *value) |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2088 | { |
| 2089 | char *prefix; |
| 2090 | char *identif; |
| 2091 | const char *ns = NULL; |
| 2092 | int i; |
| 2093 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2094 | /* check to the same pointer */ |
| 2095 | if (data_node != actual) { |
| 2096 | return EXIT_SUCCESS; |
| 2097 | } |
| 2098 | |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2099 | prefix = strdup(value); |
| 2100 | if (!prefix) { |
| 2101 | LOGMEM; |
| 2102 | goto error; |
| 2103 | } |
| 2104 | /* find prefix anf identificator*/ |
| 2105 | identif = strchr(prefix, ':'); |
Pavol Vican | fbd0278 | 2016-08-29 11:14:45 +0200 | [diff] [blame] | 2106 | if (!identif) { |
| 2107 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, prefix); |
| 2108 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The extension must have prefix."); |
| 2109 | goto error; |
| 2110 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2111 | *identif = '\0'; |
| 2112 | identif++; |
| 2113 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2114 | for(i = 0; i < module->imp_size; ++i) { |
| 2115 | if (!strcmp(module->imp[i].prefix, prefix)) { |
| 2116 | ns = module->imp[i].module->ns; |
| 2117 | break; |
| 2118 | } |
| 2119 | } |
Pavol Vican | 1ff1b7b | 2016-04-07 09:51:49 +0200 | [diff] [blame] | 2120 | if (!ns && !strcmp(module->prefix, prefix)) { |
| 2121 | ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns; |
| 2122 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2123 | if (ns && !strcmp(ns, LY_NSNACM)) { |
| 2124 | if (!strcmp(identif, "default-deny-write")) { |
| 2125 | data_node->nacm |= LYS_NACM_DENYW; |
| 2126 | } else if (!strcmp(identif, "default-deny-all")) { |
| 2127 | data_node->nacm |= LYS_NACM_DENYA; |
| 2128 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2129 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif); |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2130 | goto error; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2131 | } |
| 2132 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2133 | free(prefix); |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2134 | return EXIT_SUCCESS; |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2135 | |
| 2136 | error: |
| 2137 | free(prefix); |
| 2138 | return EXIT_FAILURE; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | void |
| 2142 | nacm_inherit(struct lys_module *module) |
| 2143 | { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2144 | struct lys_node *next, *elem, *tmp_node, *tmp_child; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2145 | |
| 2146 | LY_TREE_DFS_BEGIN(module->data, next, elem) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2147 | tmp_node = NULL; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2148 | if (elem->parent) { |
| 2149 | switch (elem->nodetype) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2150 | case LYS_GROUPING: |
| 2151 | /* extension nacm not inherited*/ |
| 2152 | break; |
| 2153 | case LYS_CHOICE: |
| 2154 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2155 | case LYS_ANYDATA: |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2156 | case LYS_USES: |
| 2157 | if (elem->parent->nodetype != LYS_GROUPING) { |
| 2158 | elem->nacm |= elem->parent->nacm; |
| 2159 | } |
| 2160 | break; |
| 2161 | case LYS_CONTAINER: |
| 2162 | case LYS_LIST: |
| 2163 | case LYS_CASE: |
| 2164 | case LYS_NOTIF: |
| 2165 | case LYS_RPC: |
| 2166 | case LYS_INPUT: |
| 2167 | case LYS_OUTPUT: |
| 2168 | case LYS_AUGMENT: |
| 2169 | elem->nacm |= elem->parent->nacm; |
| 2170 | break; |
| 2171 | case LYS_LEAF: |
| 2172 | case LYS_LEAFLIST: |
| 2173 | tmp_node = elem; |
| 2174 | tmp_child = elem->child; |
| 2175 | elem->child = NULL; |
| 2176 | default: |
| 2177 | break; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2178 | } |
| 2179 | } |
| 2180 | LY_TREE_DFS_END(module->data, next, elem); |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2181 | if (tmp_node) { |
| 2182 | tmp_node->child = tmp_child; |
| 2183 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2184 | } |
| 2185 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2186 | |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2187 | int |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2188 | store_flags(struct lys_node *node, uint8_t flags, int config_opt) |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2189 | { |
Michal Vasko | e10d14a | 2016-08-26 15:17:13 +0200 | [diff] [blame] | 2190 | struct lys_node *elem; |
| 2191 | |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2192 | node->flags |= (config_opt == CONFIG_IGNORE) ? flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET)): flags; |
| 2193 | if (config_opt == CONFIG_INHERIT_ENABLE) { |
| 2194 | if (!(node->flags & LYS_CONFIG_MASK)) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2195 | /* get config flag from parent */ |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 2196 | if (node->parent) { |
| 2197 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2198 | } else { |
| 2199 | /* default config is true */ |
| 2200 | node->flags |= LYS_CONFIG_W; |
| 2201 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2202 | } else { |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2203 | /* do we even care about config flags? */ |
| 2204 | for (elem = node; elem && !(elem->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); elem = elem->parent); |
Michal Vasko | e10d14a | 2016-08-26 15:17:13 +0200 | [diff] [blame] | 2205 | |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2206 | if (!elem && (node->flags & LYS_CONFIG_W) && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 2207 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 2208 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 2209 | return EXIT_FAILURE; |
| 2210 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2211 | } |
| 2212 | } |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2213 | |
| 2214 | return EXIT_SUCCESS; |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2215 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2216 | |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2217 | int |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2218 | store_config_flag(struct lys_node *node, int config_opt) |
| 2219 | { |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2220 | int ret = config_opt; |
| 2221 | |
| 2222 | switch (node->nodetype) { |
| 2223 | case LYS_CONTAINER: |
| 2224 | case LYS_LEAF: |
| 2225 | case LYS_LEAFLIST: |
| 2226 | case LYS_LIST: |
| 2227 | case LYS_CHOICE: |
| 2228 | case LYS_ANYDATA: |
| 2229 | case LYS_ANYXML: |
| 2230 | if (config_opt == CONFIG_IGNORE) { |
| 2231 | node->flags |= node->flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET)); |
| 2232 | } else if (config_opt == CONFIG_INHERIT_ENABLE) { |
| 2233 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 2234 | /* get config flag from parent */ |
| 2235 | if (node->parent) { |
| 2236 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 2237 | } else { |
| 2238 | /* default config is true */ |
| 2239 | node->flags |= LYS_CONFIG_W; |
| 2240 | } |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2241 | } |
| 2242 | } |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2243 | break; |
| 2244 | case LYS_CASE: |
| 2245 | if (config_opt == CONFIG_INHERIT_ENABLE) { |
| 2246 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 2247 | /* get config flag from parent */ |
| 2248 | if (node->parent) { |
| 2249 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 2250 | } else { |
| 2251 | /* default config is true */ |
| 2252 | node->flags |= LYS_CONFIG_W; |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | break; |
| 2257 | case LYS_RPC: |
| 2258 | case LYS_ACTION: |
| 2259 | case LYS_NOTIF: |
| 2260 | ret = CONFIG_IGNORE; |
| 2261 | break; |
| 2262 | default: |
| 2263 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2264 | } |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2265 | |
| 2266 | return ret; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2267 | } |
| 2268 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2269 | int |
| 2270 | yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, |
| 2271 | const char *data, unsigned int size_data, struct lys_node **node) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2272 | { |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2273 | unsigned int size; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2274 | YY_BUFFER_STATE bp; |
Pavol Vican | 802af1e | 2016-03-23 20:42:26 +0100 | [diff] [blame] | 2275 | yyscan_t scanner = NULL; |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 2276 | int ret = EXIT_SUCCESS, remove_import = 1; |
| 2277 | struct lys_module *trg; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2278 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2279 | size = (size_data) ? size_data : strlen(data) + 2; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2280 | yylex_init(&scanner); |
| 2281 | bp = yy_scan_buffer((char *)data, size, scanner); |
| 2282 | yy_switch_to_buffer(bp, scanner); |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 2283 | if (yyparse(scanner, NULL, module, submodule, unres, node, &remove_import)) { |
| 2284 | if (remove_import) { |
| 2285 | trg = (submodule) ? (struct lys_module *)submodule : module; |
| 2286 | yang_free_import(trg->ctx, trg->imp, 0, trg->imp_size); |
| 2287 | yang_free_include(trg->ctx, trg->inc, 0, trg->inc_size); |
| 2288 | trg->inc_size = 0; |
| 2289 | trg->imp_size = 0; |
| 2290 | } |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2291 | ret = EXIT_FAILURE; |
| 2292 | } |
| 2293 | yy_delete_buffer(bp, scanner); |
| 2294 | yylex_destroy(scanner); |
| 2295 | return ret; |
| 2296 | } |
| 2297 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2298 | struct lys_module * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2299 | yang_read_module(struct ly_ctx *ctx, const char* data, unsigned int size, const char *revision, int implement) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2300 | { |
| 2301 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2302 | struct lys_module *tmp_module, *module = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2303 | struct unres_schema *unres = NULL; |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2304 | struct lys_node *node = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2305 | |
| 2306 | unres = calloc(1, sizeof *unres); |
| 2307 | if (!unres) { |
| 2308 | LOGMEM; |
| 2309 | goto error; |
| 2310 | } |
| 2311 | |
| 2312 | module = calloc(1, sizeof *module); |
| 2313 | if (!module) { |
| 2314 | LOGMEM; |
Pavol Vican | f7994fb | 2016-04-05 21:55:53 +0200 | [diff] [blame] | 2315 | goto error; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | /* initiale module */ |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 2319 | module->ctx = ctx; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2320 | module->type = 0; |
| 2321 | module->implemented = (implement ? 1 : 0); |
| 2322 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2323 | if (yang_parse_mem(module, NULL, unres, data, size, &node)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2324 | free_yang_common(module, node); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2325 | goto error; |
| 2326 | } |
| 2327 | |
| 2328 | if (yang_check_sub_module(module, unres, node)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2329 | goto error; |
| 2330 | } |
| 2331 | |
| 2332 | if (module && unres->count && resolve_unres_schema(module, unres)) { |
| 2333 | goto error; |
| 2334 | } |
| 2335 | |
| 2336 | if (revision) { |
| 2337 | /* check revision of the parsed model */ |
| 2338 | if (!module->rev_size || strcmp(revision, module->rev[0].date)) { |
| 2339 | LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", |
| 2340 | module->name, module->rev[0].date, revision); |
| 2341 | goto error; |
| 2342 | } |
| 2343 | } |
| 2344 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2345 | tmp_module = module; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2346 | if (lyp_ctx_add_module(&module)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2347 | goto error; |
| 2348 | } |
| 2349 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2350 | if (module == tmp_module) { |
| 2351 | nacm_inherit(module); |
| 2352 | } |
| 2353 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 2354 | if (module->deviation_size && !module->implemented) { |
| 2355 | LOGVRB("Module \"%s\" includes deviations, changing its conformance to \"implement\".", module->name); |
| 2356 | /* deviations always causes target to be made implemented, |
| 2357 | * but augents and leafrefs not, so we have to apply them now */ |
| 2358 | if (lys_set_implemented(module)) { |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2359 | goto error; |
| 2360 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2361 | } |
| 2362 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2363 | unres_schema_free(NULL, &unres); |
| 2364 | LOGVRB("Module \"%s\" successfully parsed.", module->name); |
| 2365 | return module; |
| 2366 | |
| 2367 | error: |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2368 | /* cleanup */ |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2369 | unres_schema_free(module, &unres); |
| 2370 | if (!module || !module->name) { |
| 2371 | free(module); |
Radek Krejci | 48cfa0f | 2016-11-08 19:18:34 +0100 | [diff] [blame] | 2372 | if (ly_vecode != LYVE_SUBMODULE) { |
| 2373 | LOGERR(ly_errno, "Module parsing failed."); |
| 2374 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2375 | return NULL; |
| 2376 | } |
| 2377 | |
| 2378 | LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2379 | |
| 2380 | lys_sub_module_remove_devs_augs(module); |
| 2381 | lys_free(module, NULL, 1); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2382 | return NULL; |
| 2383 | } |
| 2384 | |
| 2385 | struct lys_submodule * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2386 | yang_read_submodule(struct lys_module *module, const char *data, unsigned int size, struct unres_schema *unres) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2387 | { |
| 2388 | struct lys_submodule *submodule; |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2389 | struct lys_node *node = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2390 | |
| 2391 | submodule = calloc(1, sizeof *submodule); |
| 2392 | if (!submodule) { |
| 2393 | LOGMEM; |
| 2394 | goto error; |
| 2395 | } |
| 2396 | |
| 2397 | submodule->ctx = module->ctx; |
| 2398 | submodule->type = 1; |
| 2399 | submodule->belongsto = module; |
| 2400 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2401 | if (yang_parse_mem(module, submodule, unres, data, size, &node)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2402 | free_yang_common((struct lys_module *)submodule, node); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2403 | goto error; |
| 2404 | } |
| 2405 | |
| 2406 | if (yang_check_sub_module((struct lys_module *)submodule, unres, node)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2407 | goto error; |
| 2408 | } |
| 2409 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2410 | LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2411 | return submodule; |
| 2412 | |
| 2413 | error: |
| 2414 | /* cleanup */ |
| 2415 | unres_schema_free((struct lys_module *)submodule, &unres); |
| 2416 | |
| 2417 | if (!submodule || !submodule->name) { |
| 2418 | free(submodule); |
| 2419 | LOGERR(ly_errno, "Submodule parsing failed."); |
| 2420 | return NULL; |
| 2421 | } |
| 2422 | |
| 2423 | LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name); |
| 2424 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2425 | lys_sub_module_remove_devs_augs((struct lys_module *)submodule); |
| 2426 | lys_submodule_module_data_free(submodule); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2427 | lys_submodule_free(submodule, NULL); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2428 | return NULL; |
| 2429 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2430 | |
| 2431 | static int |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2432 | read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output) |
| 2433 | { |
| 2434 | int k = 0, j; |
| 2435 | |
| 2436 | while (in_index < size) { |
| 2437 | if (input[in_index] == ' ') { |
| 2438 | k++; |
| 2439 | } else if (input[in_index] == '\t') { |
| 2440 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2441 | k += 8; |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2442 | } else if (input[in_index] == '\\' && input[in_index + 1] == 't') { |
| 2443 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2444 | k += 8; |
| 2445 | ++in_index; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2446 | } else { |
| 2447 | break; |
| 2448 | } |
| 2449 | ++in_index; |
| 2450 | if (k >= indent) { |
| 2451 | for (j = k - indent; j > 0; --j) { |
| 2452 | output[*out_index] = ' '; |
Pavol Vican | a7bf337 | 2016-12-01 15:58:18 +0100 | [diff] [blame] | 2453 | if (j > 1) { |
| 2454 | ++(*out_index); |
| 2455 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2456 | } |
| 2457 | break; |
| 2458 | } |
| 2459 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2460 | return in_index - 1; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | char * |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2464 | yang_read_string(const char *input, char *output, int size, int offset, int indent, int version) { |
| 2465 | int i = 0, out_index = offset, space = 0; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2466 | |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2467 | while (i < size) { |
| 2468 | switch (input[i]) { |
| 2469 | case '\n': |
| 2470 | out_index -= space; |
| 2471 | output[out_index] = '\n'; |
| 2472 | space = 0; |
| 2473 | i = read_indent(input, indent, size, i + 1, &out_index, output); |
| 2474 | break; |
| 2475 | case ' ': |
| 2476 | case '\t': |
| 2477 | output[out_index] = input[i]; |
| 2478 | ++space; |
| 2479 | break; |
| 2480 | case '\\': |
| 2481 | if (input[i + 1] == 'n') { |
| 2482 | out_index -= space; |
| 2483 | output[out_index] = '\n'; |
| 2484 | space = 0; |
| 2485 | i = read_indent(input, indent, size, i + 2, &out_index, output); |
| 2486 | } else if (input[i + 1] == 't') { |
| 2487 | output[out_index] = '\t'; |
| 2488 | ++i; |
| 2489 | ++space; |
| 2490 | } else if (input[i + 1] == '\\') { |
| 2491 | output[out_index] = '\\'; |
| 2492 | ++i; |
| 2493 | } else if ((i + 1) != size && input[i + 1] == '"') { |
| 2494 | output[out_index] = '"'; |
| 2495 | ++i; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2496 | } else { |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2497 | if (version < 2) { |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2498 | output[out_index] = input[i]; |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2499 | } else { |
| 2500 | /* YANG 1.1 backslash must not be followed by any other character */ |
| 2501 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input); |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2502 | return NULL; |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2503 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2504 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2505 | break; |
| 2506 | default: |
| 2507 | output[out_index] = input[i]; |
| 2508 | space = 0; |
| 2509 | break; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2510 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2511 | ++i; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2512 | ++out_index; |
| 2513 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2514 | output[out_index] = '\0'; |
| 2515 | if (size != out_index) { |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2516 | output = realloc(output, out_index + 1); |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2517 | if (!output) { |
| 2518 | LOGMEM; |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2519 | return NULL; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2520 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2521 | } |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2522 | return output; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2523 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2524 | |
| 2525 | /* free function */ |
| 2526 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2527 | static void yang_type_free(struct ly_ctx *ctx, struct lys_type *type) |
| 2528 | { |
| 2529 | struct yang_type *stype = (struct yang_type *)type->der; |
| 2530 | int i; |
| 2531 | |
| 2532 | if (!stype) { |
| 2533 | return ; |
| 2534 | } |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 2535 | if (type->base == LY_TYPE_DER || type->base == LY_TYPE_ERR || type->base == LY_TYPE_UNION) { |
| 2536 | lydict_remove(ctx, stype->name); |
| 2537 | if (stype->base == LY_TYPE_IDENT && (!(stype->flags & LYS_NO_ERASE_IDENTITY))) { |
| 2538 | for (i = 0; i < type->info.ident.count; ++i) { |
| 2539 | free(type->info.ident.ref[i]); |
| 2540 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2541 | } |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 2542 | if (stype->base == LY_TYPE_UNION) { |
| 2543 | for (i = 0; i < type->info.uni.count; ++i) { |
| 2544 | yang_type_free(ctx, &type->info.uni.types[i]); |
| 2545 | } |
| 2546 | free(type->info.uni.types); |
| 2547 | type->base = LY_TYPE_DER; |
| 2548 | } else { |
| 2549 | type->base = stype->base; |
| 2550 | } |
| 2551 | free(stype); |
| 2552 | type->der = NULL; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2553 | } |
| 2554 | lys_type_free(ctx, type); |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2555 | type->base = LY_TYPE_DER; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | static void |
| 2559 | yang_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf, uint8_t start, uint8_t size) |
| 2560 | { |
| 2561 | uint8_t i; |
| 2562 | |
| 2563 | assert(ctx); |
| 2564 | if (!tpdf) { |
| 2565 | return; |
| 2566 | } |
| 2567 | |
| 2568 | for (i = start; i < size; ++i) { |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2569 | lydict_remove(ctx, tpdf[i].name); |
| 2570 | lydict_remove(ctx, tpdf[i].dsc); |
| 2571 | lydict_remove(ctx, tpdf[i].ref); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2572 | |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2573 | yang_type_free(ctx, &tpdf[i].type); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2574 | |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2575 | lydict_remove(ctx, tpdf[i].units); |
| 2576 | lydict_remove(ctx, tpdf[i].dflt); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2580 | static void |
| 2581 | yang_free_import(struct ly_ctx *ctx, struct lys_import *imp, uint8_t start, uint8_t size) |
| 2582 | { |
| 2583 | uint8_t i; |
| 2584 | |
| 2585 | for (i = start; i < size; ++i){ |
| 2586 | free((char *)imp[i].module); |
| 2587 | lydict_remove(ctx, imp[i].prefix); |
| 2588 | lydict_remove(ctx, imp[i].dsc); |
| 2589 | lydict_remove(ctx, imp[i].ref); |
| 2590 | } |
| 2591 | } |
| 2592 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2593 | static void |
| 2594 | yang_free_include(struct ly_ctx *ctx, struct lys_include *inc, uint8_t start, uint8_t size) |
| 2595 | { |
| 2596 | uint8_t i; |
| 2597 | |
| 2598 | for (i = start; i < size; ++i){ |
| 2599 | free((char *)inc[i].submodule); |
| 2600 | lydict_remove(ctx, inc[i].dsc); |
| 2601 | lydict_remove(ctx, inc[i].ref); |
| 2602 | } |
| 2603 | } |
| 2604 | |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 2605 | static void |
| 2606 | yang_free_ident_base(struct lys_ident *ident, uint32_t start, uint32_t size) |
| 2607 | { |
| 2608 | uint32_t i; |
| 2609 | uint8_t j; |
| 2610 | |
| 2611 | /* free base name */ |
| 2612 | for (i = start; i < size; ++i) { |
| 2613 | for (j = 0; j < ident[i].base_size; ++j) { |
| 2614 | free(ident[i].base[j]); |
| 2615 | } |
| 2616 | } |
| 2617 | } |
| 2618 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2619 | static void |
| 2620 | yang_free_grouping(struct ly_ctx *ctx, struct lys_node_grp * grp) |
| 2621 | { |
| 2622 | yang_tpdf_free(ctx, grp->tpdf, 0, grp->tpdf_size); |
| 2623 | free(grp->tpdf); |
| 2624 | } |
| 2625 | |
| 2626 | static void |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2627 | yang_free_container(struct ly_ctx *ctx, struct lys_node_container * cont) |
| 2628 | { |
| 2629 | uint8_t i; |
| 2630 | |
| 2631 | yang_tpdf_free(ctx, cont->tpdf, 0, cont->tpdf_size); |
| 2632 | free(cont->tpdf); |
| 2633 | lydict_remove(ctx, cont->presence); |
| 2634 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 2635 | for (i = 0; i < cont->must_size; ++i) { |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2636 | lys_restr_free(ctx, &cont->must[i]); |
| 2637 | } |
| 2638 | free(cont->must); |
| 2639 | |
| 2640 | lys_when_free(ctx, cont->when); |
| 2641 | } |
| 2642 | |
| 2643 | static void |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 2644 | yang_free_leaf(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
| 2645 | { |
| 2646 | uint8_t i; |
| 2647 | |
| 2648 | for (i = 0; i < leaf->must_size; i++) { |
| 2649 | lys_restr_free(ctx, &leaf->must[i]); |
| 2650 | } |
| 2651 | free(leaf->must); |
| 2652 | |
| 2653 | lys_when_free(ctx, leaf->when); |
| 2654 | |
| 2655 | yang_type_free(ctx, &leaf->type); |
| 2656 | lydict_remove(ctx, leaf->units); |
| 2657 | lydict_remove(ctx, leaf->dflt); |
| 2658 | } |
| 2659 | |
| 2660 | static void |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2661 | yang_free_leaflist(struct ly_ctx *ctx, struct lys_node_leaflist *leaflist) |
| 2662 | { |
| 2663 | uint8_t i; |
| 2664 | |
| 2665 | for (i = 0; i < leaflist->must_size; i++) { |
| 2666 | lys_restr_free(ctx, &leaflist->must[i]); |
| 2667 | } |
| 2668 | free(leaflist->must); |
| 2669 | |
| 2670 | for (i = 0; i < leaflist->dflt_size; i++) { |
| 2671 | lydict_remove(ctx, leaflist->dflt[i]); |
| 2672 | } |
| 2673 | free(leaflist->dflt); |
| 2674 | |
| 2675 | lys_when_free(ctx, leaflist->when); |
| 2676 | |
| 2677 | yang_type_free(ctx, &leaflist->type); |
| 2678 | lydict_remove(ctx, leaflist->units); |
| 2679 | } |
| 2680 | |
| 2681 | static void |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 2682 | yang_free_list(struct ly_ctx *ctx, struct lys_node_list *list) |
| 2683 | { |
| 2684 | uint8_t i; |
| 2685 | |
| 2686 | yang_tpdf_free(ctx, list->tpdf, 0, list->tpdf_size); |
| 2687 | free(list->tpdf); |
| 2688 | |
| 2689 | for (i = 0; i < list->must_size; ++i) { |
| 2690 | lys_restr_free(ctx, &list->must[i]); |
| 2691 | } |
| 2692 | free(list->must); |
| 2693 | |
| 2694 | lys_when_free(ctx, list->when); |
| 2695 | |
| 2696 | for (i = 0; i < list->unique_size; ++i) { |
| 2697 | free(list->unique[i].expr); |
| 2698 | } |
| 2699 | free(list->unique); |
| 2700 | |
| 2701 | free(list->keys); |
| 2702 | } |
| 2703 | |
| 2704 | static void |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 2705 | yang_free_choice(struct ly_ctx *ctx, struct lys_node_choice *choice) |
| 2706 | { |
| 2707 | free(choice->dflt); |
| 2708 | lys_when_free(ctx, choice->when); |
| 2709 | } |
| 2710 | |
| 2711 | static void |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 2712 | yang_free_anydata(struct ly_ctx *ctx, struct lys_node_anydata *anydata) |
| 2713 | { |
| 2714 | uint8_t i; |
| 2715 | |
| 2716 | for (i = 0; i < anydata->must_size; ++i) { |
| 2717 | lys_restr_free(ctx, &anydata->must[i]); |
| 2718 | } |
| 2719 | free(anydata->must); |
| 2720 | |
| 2721 | lys_when_free(ctx, anydata->when); |
| 2722 | } |
| 2723 | |
| 2724 | static void |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 2725 | yang_free_inout(struct ly_ctx *ctx, struct lys_node_inout *inout) |
| 2726 | { |
| 2727 | uint8_t i; |
| 2728 | |
| 2729 | yang_tpdf_free(ctx, inout->tpdf, 0, inout->tpdf_size); |
| 2730 | free(inout->tpdf); |
| 2731 | |
| 2732 | for (i = 0; i < inout->must_size; ++i) { |
| 2733 | lys_restr_free(ctx, &inout->must[i]); |
| 2734 | } |
| 2735 | free(inout->must); |
| 2736 | } |
| 2737 | |
| 2738 | static void |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 2739 | yang_free_notif(struct ly_ctx *ctx, struct lys_node_notif *notif) |
| 2740 | { |
| 2741 | uint8_t i; |
| 2742 | |
| 2743 | yang_tpdf_free(ctx, notif->tpdf, 0, notif->tpdf_size); |
| 2744 | free(notif->tpdf); |
| 2745 | |
| 2746 | for (i = 0; i < notif->must_size; ++i) { |
| 2747 | lys_restr_free(ctx, ¬if->must[i]); |
| 2748 | } |
| 2749 | free(notif->must); |
| 2750 | } |
| 2751 | |
| 2752 | static void |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 2753 | yang_free_uses(struct ly_ctx *ctx, struct lys_node_uses *uses) |
| 2754 | { |
| 2755 | int i, j; |
| 2756 | |
| 2757 | for (i = 0; i < uses->refine_size; i++) { |
| 2758 | lydict_remove(ctx, uses->refine[i].target_name); |
| 2759 | lydict_remove(ctx, uses->refine[i].dsc); |
| 2760 | lydict_remove(ctx, uses->refine[i].ref); |
| 2761 | |
| 2762 | for (j = 0; j < uses->refine[i].must_size; j++) { |
| 2763 | lys_restr_free(ctx, &uses->refine[i].must[j]); |
| 2764 | } |
| 2765 | free(uses->refine[i].must); |
| 2766 | |
| 2767 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
| 2768 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
| 2769 | } |
| 2770 | free(uses->refine[i].dflt); |
| 2771 | |
| 2772 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
| 2773 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 2774 | } |
| 2775 | } |
| 2776 | free(uses->refine); |
| 2777 | |
| 2778 | lys_when_free(ctx, uses->when); |
| 2779 | } |
| 2780 | |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 2781 | static void |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2782 | yang_free_nodes(struct ly_ctx *ctx, struct lys_node *node) |
| 2783 | { |
| 2784 | struct lys_node *tmp, *child, *sibling; |
| 2785 | |
| 2786 | if (!node) { |
| 2787 | return; |
| 2788 | } |
| 2789 | tmp = node; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2790 | |
| 2791 | while (tmp) { |
Pavol Vican | e87ff8d | 2016-11-24 18:25:01 +0100 | [diff] [blame] | 2792 | child = tmp->child; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2793 | sibling = tmp->next; |
| 2794 | /* common part */ |
| 2795 | lydict_remove(ctx, tmp->name); |
| 2796 | if (!(tmp->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Pavol Vican | e87ff8d | 2016-11-24 18:25:01 +0100 | [diff] [blame] | 2797 | lys_iffeature_free(tmp->iffeature, tmp->iffeature_size); |
| 2798 | lydict_remove(ctx, tmp->dsc); |
| 2799 | lydict_remove(ctx, tmp->ref); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | switch (tmp->nodetype) { |
| 2803 | case LYS_GROUPING: |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 2804 | case LYS_RPC: |
| 2805 | case LYS_ACTION: |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2806 | yang_free_grouping(ctx, (struct lys_node_grp *)tmp); |
| 2807 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2808 | case LYS_CONTAINER: |
| 2809 | yang_free_container(ctx, (struct lys_node_container *)tmp); |
| 2810 | break; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 2811 | case LYS_LEAF: |
| 2812 | yang_free_leaf(ctx, (struct lys_node_leaf *)tmp); |
| 2813 | break; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2814 | case LYS_LEAFLIST: |
| 2815 | yang_free_leaflist(ctx, (struct lys_node_leaflist *)tmp); |
| 2816 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 2817 | case LYS_LIST: |
| 2818 | yang_free_list(ctx, (struct lys_node_list *)tmp); |
| 2819 | break; |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 2820 | case LYS_CHOICE: |
| 2821 | yang_free_choice(ctx, (struct lys_node_choice *)tmp); |
| 2822 | break; |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 2823 | case LYS_CASE: |
| 2824 | lys_when_free(ctx, ((struct lys_node_case *)tmp)->when); |
| 2825 | break; |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 2826 | case LYS_ANYXML: |
| 2827 | case LYS_ANYDATA: |
| 2828 | yang_free_anydata(ctx, (struct lys_node_anydata *)tmp); |
| 2829 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 2830 | case LYS_INPUT: |
| 2831 | case LYS_OUTPUT: |
| 2832 | yang_free_inout(ctx, (struct lys_node_inout *)tmp); |
| 2833 | break; |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 2834 | case LYS_NOTIF: |
| 2835 | yang_free_notif(ctx, (struct lys_node_notif *)tmp); |
| 2836 | break; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 2837 | case LYS_USES: |
| 2838 | yang_free_uses(ctx, (struct lys_node_uses *)tmp); |
| 2839 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2840 | default: |
| 2841 | break; |
| 2842 | } |
| 2843 | |
| 2844 | yang_free_nodes(ctx, child); |
| 2845 | free(tmp); |
| 2846 | tmp = sibling; |
| 2847 | } |
| 2848 | } |
| 2849 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 2850 | static void |
| 2851 | yang_free_augment(struct ly_ctx *ctx, struct lys_node_augment *aug) |
| 2852 | { |
| 2853 | lydict_remove(ctx, aug->target_name); |
| 2854 | lydict_remove(ctx, aug->dsc); |
| 2855 | lydict_remove(ctx, aug->ref); |
| 2856 | |
| 2857 | lys_iffeature_free(aug->iffeature, aug->iffeature_size); |
| 2858 | lys_when_free(ctx, aug->when); |
| 2859 | yang_free_nodes(ctx, aug->child); |
| 2860 | } |
| 2861 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2862 | static void |
| 2863 | yang_free_deviate(struct ly_ctx *ctx, struct lys_deviation *dev, uint index) |
| 2864 | { |
| 2865 | uint i, j; |
| 2866 | |
| 2867 | for (i = index; i < dev->deviate_size; ++i) { |
| 2868 | lydict_remove(ctx, dev->deviate[i].units); |
| 2869 | |
| 2870 | if (dev->deviate[i].type) { |
| 2871 | yang_type_free(ctx, dev->deviate[i].type); |
| 2872 | } |
| 2873 | |
| 2874 | for (j = 0; j < dev->deviate[i].dflt_size; ++j) { |
| 2875 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
| 2876 | } |
| 2877 | free(dev->deviate[i].dflt); |
| 2878 | |
| 2879 | for (j = 0; j < dev->deviate[i].must_size; ++j) { |
| 2880 | lys_restr_free(ctx, &dev->deviate[i].must[j]); |
| 2881 | } |
| 2882 | free(dev->deviate[i].must); |
| 2883 | |
| 2884 | for (j = 0; j < dev->deviate[i].unique_size; ++j) { |
| 2885 | free(dev->deviate[i].unique[j].expr); |
| 2886 | } |
| 2887 | free(dev->deviate[i].unique); |
| 2888 | } |
| 2889 | } |
| 2890 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2891 | /* free common item from module and submodule */ |
| 2892 | static void |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2893 | free_yang_common(struct lys_module *module, struct lys_node *node) |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2894 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2895 | uint i; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2896 | yang_tpdf_free(module->ctx, module->tpdf, 0, module->tpdf_size); |
| 2897 | module->tpdf_size = 0; |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 2898 | yang_free_ident_base(module->ident, 0, module->ident_size); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2899 | yang_free_nodes(module->ctx, node); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2900 | for (i = 0; i < module->augment_size; ++i) { |
| 2901 | yang_free_augment(module->ctx, &module->augment[i]); |
| 2902 | } |
| 2903 | module->augment_size = 0; |
| 2904 | for (i = 0; i < module->deviation_size; ++i) { |
| 2905 | yang_free_deviate(module->ctx, &module->deviation[i], 0); |
| 2906 | free(module->deviation[i].deviate); |
| 2907 | } |
| 2908 | module->deviation_size = 0; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2909 | } |
| 2910 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2911 | /* check function*/ |
| 2912 | |
| 2913 | int |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2914 | yang_check_imports(struct lys_module *module, struct unres_schema *unres) |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2915 | { |
| 2916 | struct lys_import *imp; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2917 | struct lys_include *inc; |
| 2918 | uint8_t imp_size, inc_size, j = 0, i = 0; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2919 | size_t size; |
| 2920 | char *s; |
| 2921 | |
| 2922 | imp = module->imp; |
| 2923 | imp_size = module->imp_size; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2924 | inc = module->inc; |
| 2925 | inc_size = module->inc_size; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2926 | |
| 2927 | if (imp_size) { |
| 2928 | size = (imp_size * sizeof *module->imp) + sizeof(void*); |
| 2929 | module->imp_size = 0; |
| 2930 | module->imp = calloc(1, size); |
| 2931 | if (!module->imp) { |
| 2932 | LOGMEM; |
| 2933 | goto error; |
| 2934 | } |
| 2935 | /* set stop block for possible realloc */ |
| 2936 | module->imp[imp_size].module = (void*)0x1; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2937 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2938 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2939 | if (inc_size) { |
| 2940 | size = (inc_size * sizeof *module->inc) + sizeof(void*); |
| 2941 | module->inc_size = 0; |
| 2942 | module->inc = calloc(1, size); |
| 2943 | if (!module->inc) { |
| 2944 | LOGMEM; |
| 2945 | goto error; |
| 2946 | } |
| 2947 | /* set stop block for possible realloc */ |
| 2948 | module->inc[inc_size].submodule = (void*)0x1; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | for (i = 0; i < imp_size; ++i) { |
| 2952 | s = (char *) imp[i].module; |
| 2953 | imp[i].module = NULL; |
| 2954 | if (yang_fill_import(module, &imp[i], &module->imp[module->imp_size], s)) { |
| 2955 | ++i; |
| 2956 | goto error; |
| 2957 | } |
| 2958 | } |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2959 | for (j = 0; j < inc_size; ++j) { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 2960 | s = (char *) inc[j].submodule; |
| 2961 | inc[j].submodule = NULL; |
| 2962 | if (yang_fill_include(module, s, &inc[j], unres)) { |
| 2963 | ++j; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2964 | goto error; |
| 2965 | } |
| 2966 | } |
| 2967 | free(inc); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2968 | free(imp); |
| 2969 | |
| 2970 | return EXIT_SUCCESS; |
| 2971 | |
| 2972 | error: |
| 2973 | yang_free_import(module->ctx, imp, i, imp_size); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2974 | yang_free_include(module->ctx, inc, j, inc_size); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2975 | free(imp); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2976 | free(inc); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2977 | return EXIT_FAILURE; |
| 2978 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2979 | |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 2980 | static int |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 2981 | yang_check_iffeatures(struct lys_module *module, void *ptr, void *parent, enum yytokentype type, struct unres_schema *unres) |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 2982 | { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 2983 | struct lys_iffeature *iffeature; |
| 2984 | uint8_t *ptr_size, size, i; |
| 2985 | char *s; |
| 2986 | int parent_is_feature = 0; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 2987 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 2988 | switch (type) { |
| 2989 | case FEATURE_KEYWORD: |
| 2990 | iffeature = ((struct lys_feature *)parent)->iffeature; |
| 2991 | size = ((struct lys_feature *)parent)->iffeature_size; |
| 2992 | ptr_size = &((struct lys_feature *)parent)->iffeature_size; |
| 2993 | parent_is_feature = 1; |
| 2994 | break; |
| 2995 | case IDENTITY_KEYWORD: |
| 2996 | iffeature = ((struct lys_ident *)parent)->iffeature; |
| 2997 | size = ((struct lys_ident *)parent)->iffeature_size; |
| 2998 | ptr_size = &((struct lys_ident *)parent)->iffeature_size; |
| 2999 | break; |
| 3000 | case ENUM_KEYWORD: |
| 3001 | iffeature = ((struct lys_type_enum *)ptr)->iffeature; |
| 3002 | size = ((struct lys_type_enum *)ptr)->iffeature_size; |
| 3003 | ptr_size = &((struct lys_type_enum *)ptr)->iffeature_size; |
| 3004 | break; |
| 3005 | case BIT_KEYWORD: |
| 3006 | iffeature = ((struct lys_type_bit *)ptr)->iffeature; |
| 3007 | size = ((struct lys_type_bit *)ptr)->iffeature_size; |
| 3008 | ptr_size = &((struct lys_type_bit *)ptr)->iffeature_size; |
| 3009 | break; |
| 3010 | case REFINE_KEYWORD: |
| 3011 | iffeature = ((struct lys_refine *)ptr)->iffeature; |
| 3012 | size = ((struct lys_refine *)ptr)->iffeature_size; |
| 3013 | ptr_size = &((struct lys_refine *)ptr)->iffeature_size; |
| 3014 | break; |
| 3015 | default: |
| 3016 | iffeature = ((struct lys_node *)parent)->iffeature; |
| 3017 | size = ((struct lys_node *)parent)->iffeature_size; |
| 3018 | ptr_size = &((struct lys_node *)parent)->iffeature_size; |
| 3019 | break; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3020 | } |
| 3021 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3022 | *ptr_size = 0; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3023 | for (i = 0; i < size; ++i) { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3024 | s = (char *)iffeature[i].features; |
| 3025 | iffeature[i].features = NULL; |
| 3026 | if (yang_fill_iffeature(module, &iffeature[i], parent, s, unres, parent_is_feature)) { |
| 3027 | *ptr_size = size; |
| 3028 | return EXIT_FAILURE; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3029 | } |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3030 | (*ptr_size)++; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | return EXIT_SUCCESS; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3034 | } |
| 3035 | |
Pavol Vican | b60a1d1 | 2016-12-03 01:56:55 +0100 | [diff] [blame] | 3036 | static int |
| 3037 | yang_check_identityref(struct lys_module *module, struct lys_type *type, struct unres_schema *unres) |
| 3038 | { |
| 3039 | uint size, i; |
| 3040 | int rc; |
| 3041 | struct lys_ident **ref; |
| 3042 | const char *value; |
| 3043 | char *expr; |
| 3044 | |
| 3045 | ref = type->info.ident.ref; |
| 3046 | size = type->info.ident.count; |
| 3047 | type->info.ident.count = 0; |
| 3048 | type->info.ident.ref = NULL; |
| 3049 | ((struct yang_type *)type->der)->flags |= LYS_NO_ERASE_IDENTITY; |
| 3050 | |
| 3051 | for (i = 0; i < size; ++i) { |
| 3052 | expr = (char *)ref[i]; |
| 3053 | /* store in the JSON format */ |
| 3054 | value = transform_schema2json(module, expr); |
| 3055 | free(expr); |
| 3056 | |
| 3057 | if (!value) { |
| 3058 | goto error; |
| 3059 | } |
| 3060 | rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value); |
| 3061 | lydict_remove(module->ctx, value); |
| 3062 | |
| 3063 | if (rc == -1) { |
| 3064 | goto error; |
| 3065 | } |
| 3066 | } |
| 3067 | free(ref); |
| 3068 | |
| 3069 | return EXIT_SUCCESS; |
| 3070 | error: |
| 3071 | for (i = i+1; i < size; ++i) { |
| 3072 | free(ref[i]); |
| 3073 | } |
| 3074 | free(ref); |
| 3075 | return EXIT_FAILURE; |
| 3076 | } |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3077 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3078 | int |
| 3079 | yang_check_typedef(struct lys_module *module, struct lys_node *parent, struct unres_schema *unres) |
| 3080 | { |
| 3081 | struct lys_tpdf *tpdf; |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3082 | uint8_t j, i, tpdf_size, *ptr_tpdf_size; |
| 3083 | struct yang_type *stype; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3084 | |
| 3085 | if (!parent) { |
| 3086 | tpdf = module->tpdf; |
| 3087 | ptr_tpdf_size = &module->tpdf_size; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3088 | } else { |
| 3089 | switch (parent->nodetype) { |
| 3090 | case LYS_GROUPING: |
| 3091 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
| 3092 | ptr_tpdf_size = &((struct lys_node_grp *)parent)->tpdf_size; |
| 3093 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3094 | case LYS_CONTAINER: |
| 3095 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
| 3096 | ptr_tpdf_size = &((struct lys_node_container *)parent)->tpdf_size; |
| 3097 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3098 | case LYS_LIST: |
| 3099 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
| 3100 | ptr_tpdf_size = &((struct lys_node_list *)parent)->tpdf_size; |
| 3101 | break; |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3102 | case LYS_RPC: |
| 3103 | case LYS_ACTION: |
| 3104 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
| 3105 | ptr_tpdf_size = &((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3106 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 3107 | case LYS_INPUT: |
| 3108 | case LYS_OUTPUT: |
| 3109 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
| 3110 | ptr_tpdf_size = &((struct lys_node_inout *)parent)->tpdf_size; |
| 3111 | break; |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3112 | case LYS_NOTIF: |
| 3113 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
| 3114 | ptr_tpdf_size = &((struct lys_node_notif *)parent)->tpdf_size; |
| 3115 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3116 | default: |
| 3117 | LOGINT; |
| 3118 | return EXIT_FAILURE; |
| 3119 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | tpdf_size = *ptr_tpdf_size; |
| 3123 | *ptr_tpdf_size = 0; |
| 3124 | |
| 3125 | for (i = 0; i < tpdf_size; ++i) { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3126 | if (lyp_check_identifier(tpdf[i].name, LY_IDENT_TYPE, module, parent)) { |
| 3127 | goto error; |
| 3128 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3129 | tpdf[i].type.parent = &tpdf[i]; |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3130 | |
| 3131 | stype = (struct yang_type *)tpdf[i].type.der; |
| 3132 | if (stype->base == LY_TYPE_ENUM) { |
| 3133 | for (j = 0; j < tpdf[i].type.info.enums.count; ++j) { |
| 3134 | if (yang_check_iffeatures(module, &tpdf[i].type.info.enums.enm[j], &tpdf[i], ENUM_KEYWORD, unres)) { |
| 3135 | goto error; |
| 3136 | } |
| 3137 | } |
| 3138 | } else if (stype->base == LY_TYPE_BITS) { |
| 3139 | for (j = 0; j < tpdf[i].type.info.bits.count; ++j) { |
| 3140 | if (yang_check_iffeatures(module, &tpdf[i].type.info.bits.bit[j], &tpdf[i], BIT_KEYWORD, unres)) { |
| 3141 | goto error; |
| 3142 | } |
| 3143 | } |
Pavol Vican | b60a1d1 | 2016-12-03 01:56:55 +0100 | [diff] [blame] | 3144 | } else if (stype->base == LY_TYPE_IDENT) { |
| 3145 | if (yang_check_identityref(module, &tpdf[i].type, unres)) { |
| 3146 | goto error; |
| 3147 | } |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3148 | } |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3149 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3150 | if (unres_schema_add_node(module, unres, &tpdf[i].type, UNRES_TYPE_DER_TPDF, parent) == -1) { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3151 | goto error; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3152 | } |
| 3153 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3154 | (*ptr_tpdf_size)++; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3155 | /* check default value*/ |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3156 | if (unres_schema_add_node(module, unres, &tpdf[i].type, UNRES_TYPE_DFLT, (struct lys_node *)(&tpdf[i].dflt)) == -1) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3157 | ++i; |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3158 | goto error; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3159 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3160 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3161 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3162 | return EXIT_SUCCESS; |
| 3163 | |
| 3164 | error: |
| 3165 | yang_tpdf_free(module->ctx, tpdf, i, tpdf_size); |
| 3166 | return EXIT_FAILURE; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | static int |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3170 | yang_check_identities(struct lys_module *module, struct unres_schema *unres) |
| 3171 | { |
| 3172 | uint32_t i, size, base_size; |
| 3173 | uint8_t j; |
| 3174 | |
| 3175 | size = module->ident_size; |
| 3176 | module->ident_size = 0; |
| 3177 | for (i = 0; i < size; ++i) { |
| 3178 | base_size = module->ident[i].base_size; |
| 3179 | module->ident[i].base_size = 0; |
| 3180 | for (j = 0; j < base_size; ++j) { |
| 3181 | if (yang_read_base(module, &module->ident[i], (char *)module->ident[i].base[j], unres)) { |
| 3182 | ++j; |
| 3183 | module->ident_size = size; |
| 3184 | goto error; |
| 3185 | } |
| 3186 | } |
| 3187 | module->ident_size++; |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3188 | if (yang_check_iffeatures(module, NULL, &module->ident[i], IDENTITY_KEYWORD, unres)) { |
| 3189 | goto error; |
| 3190 | } |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | return EXIT_SUCCESS; |
| 3194 | |
| 3195 | error: |
| 3196 | for (; j< module->ident[i].base_size; ++j) { |
| 3197 | free(module->ident[i].base[j]); |
| 3198 | } |
| 3199 | yang_free_ident_base(module->ident, i + 1, size); |
| 3200 | return EXIT_FAILURE; |
| 3201 | } |
| 3202 | |
| 3203 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3204 | yang_check_container(struct lys_module *module, struct lys_node_container *cont, struct lys_node **child, |
| 3205 | int config_opt, struct unres_schema *unres) |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3206 | { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3207 | if (yang_check_typedef(module, (struct lys_node *)cont, unres)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3208 | goto error; |
| 3209 | } |
| 3210 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3211 | if (yang_check_iffeatures(module, NULL, cont, CONTAINER_KEYWORD, unres)) { |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3212 | goto error; |
| 3213 | } |
| 3214 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3215 | if (yang_check_nodes(module, (struct lys_node *)cont, *child, config_opt, unres)) { |
| 3216 | *child = NULL; |
| 3217 | goto error; |
| 3218 | } |
| 3219 | *child = NULL; |
| 3220 | |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3221 | /* check XPath dependencies */ |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3222 | if ((cont->when || cont->must_size) && (unres_schema_add_node(module, unres, cont, UNRES_XPATH, NULL) == -1)) { |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3223 | goto error; |
| 3224 | } |
| 3225 | |
| 3226 | return EXIT_SUCCESS; |
| 3227 | error: |
| 3228 | return EXIT_FAILURE; |
| 3229 | } |
| 3230 | |
| 3231 | static int |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3232 | yang_check_leaf(struct lys_module *module, struct lys_node_leaf *leaf, struct unres_schema *unres) |
| 3233 | { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3234 | int i; |
| 3235 | struct yang_type *stype; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3236 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3237 | stype = (struct yang_type *)leaf->type.der; |
| 3238 | if (stype->base == LY_TYPE_ENUM) { |
| 3239 | for (i = 0; i < leaf->type.info.enums.count; ++i) { |
| 3240 | if (yang_check_iffeatures(module, &leaf->type.info.enums.enm[i], leaf, ENUM_KEYWORD, unres)) { |
| 3241 | yang_type_free(module->ctx, &leaf->type); |
| 3242 | goto error; |
| 3243 | } |
| 3244 | } |
| 3245 | } else if (stype->base == LY_TYPE_BITS) { |
| 3246 | for (i = 0; i < leaf->type.info.bits.count; ++i) { |
| 3247 | if (yang_check_iffeatures(module, &leaf->type.info.bits.bit[i], leaf, BIT_KEYWORD, unres)) { |
| 3248 | yang_type_free(module->ctx, &leaf->type); |
| 3249 | goto error; |
| 3250 | } |
| 3251 | } |
Pavol Vican | b60a1d1 | 2016-12-03 01:56:55 +0100 | [diff] [blame] | 3252 | } else if (stype->base == LY_TYPE_IDENT) { |
| 3253 | if (yang_check_identityref(module, &leaf->type, unres)) { |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3254 | yang_type_free(module->ctx, &leaf->type); |
Pavol Vican | b60a1d1 | 2016-12-03 01:56:55 +0100 | [diff] [blame] | 3255 | goto error; |
| 3256 | } |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3257 | } |
| 3258 | |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3259 | if (yang_check_iffeatures(module, NULL, leaf, LEAF_KEYWORD, unres)) { |
| 3260 | yang_type_free(module->ctx, &leaf->type); |
| 3261 | goto error; |
| 3262 | } |
| 3263 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3264 | if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DER, (struct lys_node *)leaf) == -1) { |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3265 | yang_type_free(module->ctx, &leaf->type); |
| 3266 | goto error; |
| 3267 | } |
| 3268 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3269 | if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)&leaf->dflt) == -1) { |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3270 | goto error; |
| 3271 | } |
| 3272 | |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3273 | /* check XPath dependencies */ |
| 3274 | if ((leaf->when || leaf->must_size) && (unres_schema_add_node(module, unres, leaf, UNRES_XPATH, NULL) == -1)) { |
| 3275 | goto error; |
| 3276 | } |
| 3277 | |
| 3278 | return EXIT_SUCCESS; |
| 3279 | error: |
| 3280 | return EXIT_FAILURE; |
| 3281 | } |
| 3282 | |
| 3283 | static int |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3284 | yang_check_leaflist(struct lys_module *module, struct lys_node_leaflist *leaflist, struct unres_schema *unres) |
| 3285 | { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3286 | int i, j; |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3287 | struct yang_type *stype; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3288 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3289 | stype = (struct yang_type *)leaflist->type.der; |
| 3290 | if (stype->base == LY_TYPE_ENUM) { |
| 3291 | for (i = 0; i < leaflist->type.info.enums.count; ++i) { |
| 3292 | if (yang_check_iffeatures(module, &leaflist->type.info.enums.enm[i], leaflist, ENUM_KEYWORD, unres)) { |
| 3293 | yang_type_free(module->ctx, &leaflist->type); |
| 3294 | goto error; |
| 3295 | } |
| 3296 | } |
| 3297 | } else if (stype->base == LY_TYPE_BITS) { |
| 3298 | for (i = 0; i < leaflist->type.info.bits.count; ++i) { |
| 3299 | if (yang_check_iffeatures(module, &leaflist->type.info.bits.bit[i], leaflist, BIT_KEYWORD, unres)) { |
| 3300 | yang_type_free(module->ctx, &leaflist->type); |
| 3301 | goto error; |
| 3302 | } |
| 3303 | } |
Pavol Vican | b60a1d1 | 2016-12-03 01:56:55 +0100 | [diff] [blame] | 3304 | } else if (stype->base == LY_TYPE_IDENT) { |
| 3305 | if (yang_check_identityref(module, &leaflist->type, unres)) { |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3306 | yang_type_free(module->ctx, &leaflist->type); |
Pavol Vican | b60a1d1 | 2016-12-03 01:56:55 +0100 | [diff] [blame] | 3307 | goto error; |
| 3308 | } |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3309 | } |
| 3310 | |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3311 | if (yang_check_iffeatures(module, NULL, leaflist, LEAF_LIST_KEYWORD, unres)) { |
| 3312 | yang_type_free(module->ctx, &leaflist->type); |
| 3313 | goto error; |
| 3314 | } |
| 3315 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3316 | if (unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DER, (struct lys_node *)leaflist) == -1) { |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3317 | yang_type_free(module->ctx, &leaflist->type); |
| 3318 | goto error; |
| 3319 | } |
| 3320 | |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3321 | for (i = 0; i < leaflist->dflt_size; ++i) { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3322 | /* check for duplicity in case of configuration data, |
| 3323 | * in case of status data duplicities are allowed */ |
| 3324 | if (leaflist->flags & LYS_CONFIG_W) { |
| 3325 | for (j = i +1; j < leaflist->dflt_size; ++j) { |
| 3326 | if (ly_strequal(leaflist->dflt[i], leaflist->dflt[j], 1)) { |
| 3327 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, leaflist->dflt[i], "default"); |
| 3328 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", leaflist->dflt[i]); |
| 3329 | goto error; |
| 3330 | } |
| 3331 | } |
| 3332 | } |
| 3333 | /* check default value (if not defined, there still could be some restrictions |
| 3334 | * that need to be checked against a default value from a derived type) */ |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3335 | if (unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaflist->dflt[i])) == -1) { |
| 3336 | goto error; |
| 3337 | } |
| 3338 | } |
| 3339 | |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3340 | /* check XPath dependencies */ |
| 3341 | if ((leaflist->when || leaflist->must_size) && (unres_schema_add_node(module, unres, leaflist, UNRES_XPATH, NULL) == -1)) { |
| 3342 | goto error; |
| 3343 | } |
| 3344 | |
| 3345 | return EXIT_SUCCESS; |
| 3346 | error: |
| 3347 | return EXIT_FAILURE; |
| 3348 | } |
| 3349 | |
| 3350 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3351 | yang_check_list(struct lys_module *module, struct lys_node_list *list, struct lys_node **child, |
| 3352 | int config_opt, struct unres_schema *unres) |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3353 | { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3354 | struct lys_node *node; |
| 3355 | |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3356 | if (yang_check_typedef(module, (struct lys_node *)list, unres)) { |
| 3357 | goto error; |
| 3358 | } |
| 3359 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3360 | if (yang_check_iffeatures(module, NULL, list, LIST_KEYWORD, unres)) { |
| 3361 | goto error; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3362 | } |
| 3363 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3364 | if (list->flags & LYS_CONFIG_R) { |
| 3365 | /* RFC 6020, 7.7.5 - ignore ordering when the list represents state data |
| 3366 | * ignore oredering MASK - 0x7F |
| 3367 | */ |
| 3368 | list->flags &= 0x7F; |
| 3369 | } |
| 3370 | /* check - if list is configuration, key statement is mandatory |
| 3371 | * (but only if we are not in a grouping or augment, then the check is deferred) */ |
| 3372 | for (node = (struct lys_node *)list; node && !(node->nodetype & (LYS_GROUPING | LYS_AUGMENT)); node = node->parent); |
| 3373 | if (!node && (list->flags & LYS_CONFIG_W) && !list->keys) { |
| 3374 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, list, "key", "list"); |
| 3375 | goto error; |
| 3376 | } |
| 3377 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3378 | if (yang_check_nodes(module, (struct lys_node *)list, *child, config_opt, unres)) { |
| 3379 | *child = NULL; |
| 3380 | goto error; |
| 3381 | } |
| 3382 | *child = NULL; |
| 3383 | |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3384 | if (list->keys && yang_read_key(module, list, unres)) { |
| 3385 | goto error; |
| 3386 | } |
| 3387 | |
| 3388 | if (yang_read_unique(module, list, unres)) { |
| 3389 | goto error; |
| 3390 | } |
| 3391 | |
| 3392 | /* check XPath dependencies */ |
| 3393 | if ((list->when || list->must_size) && (unres_schema_add_node(module, unres, list, UNRES_XPATH, NULL) == -1)) { |
| 3394 | goto error; |
| 3395 | } |
| 3396 | |
| 3397 | return EXIT_SUCCESS; |
| 3398 | error: |
| 3399 | return EXIT_FAILURE; |
| 3400 | } |
| 3401 | |
| 3402 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3403 | yang_check_choice(struct lys_module *module, struct lys_node_choice *choice, struct lys_node **child, |
| 3404 | int config_opt, struct unres_schema *unres) |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3405 | { |
| 3406 | char *value; |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3407 | |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3408 | if (yang_check_iffeatures(module, NULL, choice, CHOICE_KEYWORD, unres)) { |
| 3409 | free(choice->dflt); |
| 3410 | choice->dflt = NULL; |
| 3411 | goto error; |
| 3412 | } |
| 3413 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3414 | if (yang_check_nodes(module, (struct lys_node *)choice, *child, config_opt, unres)) { |
| 3415 | *child = NULL; |
| 3416 | free(choice->dflt); |
| 3417 | choice->dflt = NULL; |
| 3418 | goto error; |
| 3419 | } |
| 3420 | *child = NULL; |
| 3421 | |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3422 | if (choice->dflt) { |
| 3423 | value = (char *)choice->dflt; |
| 3424 | choice->dflt = NULL; |
| 3425 | if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, value) == -1) { |
| 3426 | free(value); |
| 3427 | goto error; |
| 3428 | } |
| 3429 | free(value); |
| 3430 | } |
| 3431 | |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3432 | /* check XPath dependencies */ |
| 3433 | if ((choice->when) && (unres_schema_add_node(module, unres, choice, UNRES_XPATH, NULL) == -1)) { |
| 3434 | goto error; |
| 3435 | } |
| 3436 | |
| 3437 | return EXIT_SUCCESS; |
| 3438 | error: |
| 3439 | return EXIT_FAILURE; |
| 3440 | } |
| 3441 | |
| 3442 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3443 | yang_check_rpc_action(struct lys_module *module, struct lys_node_rpc_action *rpc, struct lys_node **child, |
| 3444 | int config_opt, struct unres_schema *unres) |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3445 | { |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3446 | struct lys_node *node; |
| 3447 | |
| 3448 | if (rpc->nodetype == LYS_ACTION) { |
| 3449 | for (node = rpc->parent; node; node = lys_parent(node)) { |
| 3450 | if ((node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
Pavol Vican | 73ff803 | 2016-12-04 15:03:51 +0100 | [diff] [blame] | 3451 | || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys)) { |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3452 | LOGVAL(LYE_INPAR, LY_VLOG_NONE, NULL, strnodetype(node->nodetype), "action"); |
| 3453 | goto error; |
| 3454 | } |
| 3455 | } |
| 3456 | } |
| 3457 | if (yang_check_typedef(module, (struct lys_node *)rpc, unres)) { |
| 3458 | goto error; |
| 3459 | } |
| 3460 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3461 | if (yang_check_iffeatures(module, NULL, rpc, RPC_KEYWORD, unres)) { |
| 3462 | goto error; |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3463 | } |
| 3464 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3465 | if (yang_check_nodes(module, (struct lys_node *)rpc, *child, config_opt, unres)) { |
| 3466 | *child = NULL; |
| 3467 | goto error; |
| 3468 | } |
| 3469 | *child = NULL; |
| 3470 | |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3471 | return EXIT_SUCCESS; |
| 3472 | error: |
| 3473 | return EXIT_FAILURE; |
| 3474 | } |
| 3475 | |
| 3476 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3477 | yang_check_notif(struct lys_module *module, struct lys_node_notif *notif, struct lys_node **child, |
| 3478 | int config_opt, struct unres_schema *unres) |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3479 | { |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3480 | if (yang_check_typedef(module, (struct lys_node *)notif, unres)) { |
| 3481 | goto error; |
| 3482 | } |
| 3483 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3484 | if (yang_check_iffeatures(module, NULL, notif, NOTIFICATION_KEYWORD, unres)) { |
| 3485 | goto error; |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3486 | } |
| 3487 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3488 | if (yang_check_nodes(module, (struct lys_node *)notif, *child, config_opt, unres)) { |
| 3489 | *child = NULL; |
| 3490 | goto error; |
| 3491 | } |
| 3492 | *child = NULL; |
| 3493 | |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3494 | /* check XPath dependencies */ |
| 3495 | if ((notif->must_size) && (unres_schema_add_node(module, unres, notif, UNRES_XPATH, NULL) == -1)) { |
| 3496 | goto error; |
| 3497 | } |
| 3498 | |
| 3499 | return EXIT_SUCCESS; |
| 3500 | error: |
| 3501 | return EXIT_FAILURE; |
| 3502 | } |
| 3503 | |
| 3504 | static int |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3505 | yang_check_augment(struct lys_module *module, struct lys_node_augment *augment, int config_opt, struct unres_schema *unres) |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3506 | { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3507 | struct lys_node *child; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3508 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3509 | child = augment->child; |
| 3510 | augment->child = NULL; |
| 3511 | |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3512 | if (yang_check_iffeatures(module, NULL, augment, AUGMENT_KEYWORD, unres)) { |
| 3513 | yang_free_nodes(module->ctx, child); |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3514 | goto error; |
| 3515 | } |
| 3516 | |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3517 | if (yang_check_nodes(module, (struct lys_node *)augment, child, config_opt, unres)) { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3518 | goto error; |
| 3519 | } |
| 3520 | |
| 3521 | /* check XPath dependencies */ |
| 3522 | if (augment->when && (unres_schema_add_node(module, unres, augment, UNRES_XPATH, NULL) == -1)) { |
| 3523 | goto error; |
| 3524 | } |
| 3525 | |
| 3526 | return EXIT_SUCCESS; |
| 3527 | error: |
| 3528 | return EXIT_FAILURE; |
| 3529 | } |
| 3530 | |
| 3531 | static int |
| 3532 | yang_check_uses(struct lys_module *module, struct lys_node_uses *uses, int config_opt, struct unres_schema *unres) |
| 3533 | { |
| 3534 | uint i, size; |
| 3535 | |
| 3536 | size = uses->augment_size; |
| 3537 | uses->augment_size = 0; |
| 3538 | |
| 3539 | if (yang_check_iffeatures(module, NULL, uses, USES_KEYWORD, unres)) { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3540 | goto error; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3541 | } |
| 3542 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3543 | for (i = 0; i < uses->refine_size; ++i) { |
| 3544 | if (yang_check_iffeatures(module, &uses->refine[i], uses, REFINE_KEYWORD, unres)) { |
| 3545 | goto error; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3546 | } |
| 3547 | } |
| 3548 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3549 | for (i = 0; i < size; ++i) { |
| 3550 | uses->augment_size++; |
| 3551 | if (yang_check_augment(module, &uses->augment[i], config_opt, unres)) { |
| 3552 | goto error; |
| 3553 | } |
| 3554 | } |
| 3555 | |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3556 | if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) { |
| 3557 | goto error; |
| 3558 | } |
| 3559 | |
| 3560 | /* check XPath dependencies */ |
| 3561 | if (uses->when && (unres_schema_add_node(module, unres, uses, UNRES_XPATH, NULL) == -1)) { |
| 3562 | goto error; |
| 3563 | } |
| 3564 | |
| 3565 | return EXIT_SUCCESS; |
| 3566 | error: |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3567 | for (i = uses->augment_size; i < size; ++i) { |
| 3568 | yang_free_augment(module->ctx, &uses->augment[i]); |
| 3569 | } |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3570 | return EXIT_FAILURE; |
| 3571 | } |
| 3572 | |
| 3573 | static int |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3574 | yang_check_nodes(struct lys_module *module, struct lys_node *parent, struct lys_node *nodes, |
| 3575 | int config_opt, struct unres_schema *unres) |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3576 | { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3577 | struct lys_node *node = nodes, *sibling, *child; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3578 | |
| 3579 | while (node) { |
| 3580 | sibling = node->next; |
| 3581 | child = node->child; |
| 3582 | node->next = NULL; |
| 3583 | node->child = NULL; |
| 3584 | node->prev = node; |
| 3585 | |
Pavol Vican | 24ba7f6 | 2016-11-28 12:15:20 +0100 | [diff] [blame] | 3586 | if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, node)) { |
| 3587 | lys_node_unlink(node); |
| 3588 | node->next = sibling; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3589 | sibling = node; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3590 | goto error; |
| 3591 | } |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 3592 | config_opt = store_config_flag(node, config_opt); |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3593 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3594 | switch (node->nodetype) { |
| 3595 | case LYS_GROUPING: |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3596 | if (yang_check_typedef(module, node, unres)) { |
| 3597 | goto error; |
| 3598 | } |
| 3599 | if (yang_check_iffeatures(module, NULL, node, GROUPING_KEYWORD, unres)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3600 | goto error; |
| 3601 | } |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3602 | if (yang_check_nodes(module, node, child, config_opt, unres)) { |
| 3603 | child = NULL; |
| 3604 | goto error; |
| 3605 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3606 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3607 | case LYS_CONTAINER: |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3608 | if (yang_check_container(module, (struct lys_node_container *)node, &child, config_opt, unres)) { |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3609 | goto error; |
| 3610 | } |
| 3611 | break; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3612 | case LYS_LEAF: |
| 3613 | if (yang_check_leaf(module, (struct lys_node_leaf *)node, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3614 | child = NULL; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3615 | goto error; |
| 3616 | } |
| 3617 | break; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3618 | case LYS_LEAFLIST: |
| 3619 | if (yang_check_leaflist(module, (struct lys_node_leaflist *)node, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3620 | child = NULL; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3621 | goto error; |
| 3622 | } |
| 3623 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3624 | case LYS_LIST: |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3625 | if (yang_check_list(module, (struct lys_node_list *)node, &child, config_opt, unres)) { |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3626 | goto error; |
| 3627 | } |
| 3628 | break; |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3629 | case LYS_CHOICE: |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3630 | if (yang_check_choice(module, (struct lys_node_choice *)node, &child, config_opt, unres)) { |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3631 | goto error; |
| 3632 | } |
| 3633 | break; |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 3634 | case LYS_CASE: |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3635 | if (yang_check_iffeatures(module, NULL, node, CASE_KEYWORD, unres)) { |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 3636 | goto error; |
| 3637 | } |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3638 | if (yang_check_nodes(module, node, child, config_opt, unres)) { |
| 3639 | child = NULL; |
| 3640 | goto error; |
| 3641 | } |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 3642 | break; |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 3643 | case LYS_ANYDATA: |
| 3644 | case LYS_ANYXML: |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3645 | if (yang_check_iffeatures(module, NULL, node, CHOICE_KEYWORD, unres)) { |
| 3646 | goto error; |
| 3647 | } |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3648 | if (yang_check_nodes(module, node, child, config_opt, unres)) { |
| 3649 | child = NULL; |
| 3650 | goto error; |
| 3651 | } |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3652 | /* check XPath dependencies */ |
| 3653 | if ((((struct lys_node_anydata *)node)->when) && (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1)) { |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 3654 | goto error; |
| 3655 | } |
| 3656 | break; |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3657 | case LYS_RPC: |
| 3658 | case LYS_ACTION: |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3659 | if (yang_check_rpc_action(module, (struct lys_node_rpc_action *)node, &child, config_opt, unres)){ |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3660 | goto error; |
| 3661 | } |
| 3662 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 3663 | case LYS_INPUT: |
| 3664 | case LYS_OUTPUT: |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3665 | if (yang_check_typedef(module, node, unres)) { |
| 3666 | goto error; |
| 3667 | } |
| 3668 | if (yang_check_nodes(module, node, child, config_opt, unres)) { |
| 3669 | child = NULL; |
| 3670 | goto error; |
| 3671 | } |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 3672 | /* check XPath dependencies */ |
| 3673 | if (((struct lys_node_inout *)node)->must_size && |
| 3674 | (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1)) { |
| 3675 | goto error; |
| 3676 | } |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 3677 | break; |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3678 | case LYS_NOTIF: |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3679 | if (yang_check_notif(module, (struct lys_node_notif *)node, &child, config_opt, unres)) { |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3680 | goto error; |
| 3681 | } |
| 3682 | break; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3683 | case LYS_USES: |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3684 | if (yang_check_uses(module, (struct lys_node_uses *)node, config_opt, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame^] | 3685 | child = NULL; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3686 | goto error; |
| 3687 | } |
| 3688 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3689 | default: |
| 3690 | LOGINT; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3691 | goto error; |
| 3692 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3693 | node = sibling; |
| 3694 | } |
| 3695 | |
| 3696 | return EXIT_SUCCESS; |
| 3697 | error: |
| 3698 | yang_free_nodes(module->ctx, sibling); |
| 3699 | yang_free_nodes(module->ctx, child); |
| 3700 | return EXIT_FAILURE; |
| 3701 | } |
| 3702 | |
| 3703 | static int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3704 | yang_check_deviate(struct lys_module *module, struct unres_schema *unres, struct lys_deviate *deviate, |
| 3705 | struct lys_node *dev_target, struct ly_set *dflt_check) |
| 3706 | { |
| 3707 | struct lys_node_leaflist *llist; |
| 3708 | struct lys_type *type; |
| 3709 | struct lys_tpdf *tmp_parent; |
| 3710 | int i, j; |
| 3711 | |
| 3712 | if (deviate->must_size && yang_check_deviate_must(module, unres, deviate, dev_target)) { |
| 3713 | goto error; |
| 3714 | } |
| 3715 | if (deviate->unique && yang_check_deviate_unique(module, deviate, dev_target)) { |
| 3716 | goto error; |
| 3717 | } |
| 3718 | if (deviate->dflt_size) { |
| 3719 | if (yang_read_deviate_default(module, deviate, dev_target, dflt_check)) { |
| 3720 | goto error; |
| 3721 | } |
| 3722 | if (dev_target->nodetype == LYS_LEAFLIST && deviate->mod == LY_DEVIATE_DEL) { |
| 3723 | /* consolidate the final list in the target after removing items from it */ |
| 3724 | llist = (struct lys_node_leaflist *)dev_target; |
| 3725 | for (i = j = 0; j < llist->dflt_size; j++) { |
| 3726 | llist->dflt[i] = llist->dflt[j]; |
| 3727 | if (llist->dflt[i]) { |
| 3728 | i++; |
| 3729 | } |
| 3730 | } |
| 3731 | llist->dflt_size = i + 1; |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | if (deviate->max_set && yang_read_deviate_minmax(deviate, dev_target, deviate->max, 1)) { |
| 3736 | goto error; |
| 3737 | } |
| 3738 | |
| 3739 | if (deviate->min_set && yang_read_deviate_minmax(deviate, dev_target, deviate->min, 0)) { |
| 3740 | goto error; |
| 3741 | } |
| 3742 | |
| 3743 | if (deviate->units && yang_read_deviate_units(module->ctx, deviate, dev_target)) { |
| 3744 | goto error; |
| 3745 | } |
| 3746 | |
| 3747 | if ((deviate->flags & LYS_CONFIG_MASK)) { |
| 3748 | /* add and replace are the same in this case */ |
| 3749 | /* remove current config value of the target ... */ |
| 3750 | dev_target->flags &= ~LYS_CONFIG_MASK; |
| 3751 | |
| 3752 | /* ... and replace it with the value specified in deviation */ |
| 3753 | dev_target->flags |= deviate->flags & LYS_CONFIG_MASK; |
| 3754 | } |
| 3755 | |
| 3756 | if ((deviate->flags & LYS_MAND_MASK) && yang_check_deviate_mandatory(deviate, dev_target)) { |
| 3757 | goto error; |
| 3758 | } |
| 3759 | |
| 3760 | if (deviate->type) { |
| 3761 | /* check target node type */ |
| 3762 | if (dev_target->nodetype == LYS_LEAF) { |
| 3763 | type = &((struct lys_node_leaf *)dev_target)->type; |
| 3764 | } else if (dev_target->nodetype == LYS_LEAFLIST) { |
| 3765 | type = &((struct lys_node_leaflist *)dev_target)->type; |
| 3766 | } else { |
| 3767 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "type"); |
| 3768 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"type\" property."); |
| 3769 | goto error; |
| 3770 | } |
| 3771 | /* remove type and initialize it */ |
| 3772 | tmp_parent = type->parent; |
| 3773 | lys_type_free(module->ctx, type); |
| 3774 | memcpy(type, deviate->type, sizeof *deviate->type); |
| 3775 | free(deviate->type); |
| 3776 | deviate->type = type; |
| 3777 | deviate->type->parent = tmp_parent; |
| 3778 | if (unres_schema_add_node(module, unres, deviate->type, UNRES_TYPE_DER, dev_target) == -1) { |
| 3779 | goto error; |
| 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | return EXIT_SUCCESS; |
| 3784 | error: |
| 3785 | if (deviate->type) { |
| 3786 | yang_type_free(module->ctx, deviate->type); |
| 3787 | deviate->type = NULL; |
| 3788 | } |
| 3789 | return EXIT_FAILURE; |
| 3790 | } |
| 3791 | |
| 3792 | static int |
| 3793 | yang_check_deviation(struct lys_module *module, struct unres_schema *unres, struct lys_deviation *dev) |
| 3794 | { |
| 3795 | int rc; |
| 3796 | uint i; |
| 3797 | struct lys_node *dev_target = NULL, *parent; |
| 3798 | struct ly_set *dflt_check = ly_set_new(); |
| 3799 | unsigned int u; |
| 3800 | const char *value, *target_name; |
| 3801 | struct lys_node_leaflist *llist; |
| 3802 | struct lys_node_leaf *leaf; |
| 3803 | struct unres_schema tmp_unres; |
| 3804 | struct lys_module *mod; |
| 3805 | |
| 3806 | /* resolve target node */ |
| 3807 | rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, 1, (const struct lys_node **)&dev_target); |
| 3808 | if (rc || !dev_target) { |
| 3809 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
| 3810 | goto error; |
| 3811 | } |
| 3812 | if (dev_target->module == lys_main_module(module)) { |
| 3813 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
| 3814 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed."); |
| 3815 | goto error; |
| 3816 | } |
| 3817 | |
| 3818 | if (!dflt_check) { |
| 3819 | LOGMEM; |
| 3820 | goto error; |
| 3821 | } |
| 3822 | |
| 3823 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 3824 | /* you cannot remove a key leaf */ |
| 3825 | if ((dev_target->nodetype == LYS_LEAF) && dev_target->parent && (dev_target->parent->nodetype == LYS_LIST)) { |
| 3826 | for (i = 0; i < ((struct lys_node_list *)dev_target->parent)->keys_size; ++i) { |
| 3827 | if (((struct lys_node_list *)dev_target->parent)->keys[i] == (struct lys_node_leaf *)dev_target) { |
| 3828 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation"); |
| 3829 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key."); |
| 3830 | return EXIT_FAILURE; |
| 3831 | } |
| 3832 | } |
| 3833 | } |
| 3834 | /* unlink and store the original node */ |
| 3835 | lys_node_unlink(dev_target); |
| 3836 | dev->orig_node = dev_target; |
| 3837 | } else { |
| 3838 | /* store a shallow copy of the original node */ |
| 3839 | memset(&tmp_unres, 0, sizeof tmp_unres); |
| 3840 | dev->orig_node = lys_node_dup(dev_target->module, NULL, dev_target, 0, &tmp_unres, 1); |
| 3841 | /* just to be safe */ |
| 3842 | if (tmp_unres.count) { |
| 3843 | LOGINT; |
| 3844 | goto error; |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | for (i = 0; i < dev->deviate_size; ++i) { |
| 3849 | if (yang_check_deviate(module, unres, &dev->deviate[i], dev_target, dflt_check)) { |
| 3850 | yang_free_deviate(module->ctx, dev, i + 1); |
| 3851 | dev->deviate_size = i+1; |
| 3852 | goto error; // missing free unresolve type in deviate |
| 3853 | } |
| 3854 | } |
| 3855 | /* now check whether default value, if any, matches the type */ |
| 3856 | for (u = 0; u < dflt_check->number; ++u) { |
| 3857 | value = NULL; |
| 3858 | rc = EXIT_SUCCESS; |
| 3859 | if (dflt_check->set.s[u]->nodetype == LYS_LEAF) { |
| 3860 | leaf = (struct lys_node_leaf *)dflt_check->set.s[u]; |
| 3861 | target_name = leaf->name; |
| 3862 | value = leaf->dflt; |
| 3863 | rc = unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaf->dflt)); |
| 3864 | } else { /* LYS_LEAFLIST */ |
| 3865 | llist = (struct lys_node_leaflist *)dflt_check->set.s[u]; |
| 3866 | target_name = llist->name; |
| 3867 | for (i = 0; i < llist->dflt_size; i++) { |
| 3868 | rc = unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT, |
| 3869 | (struct lys_node *)(&llist->dflt[i])); |
| 3870 | if (rc == -1) { |
| 3871 | value = llist->dflt[i]; |
| 3872 | break; |
| 3873 | } |
| 3874 | } |
| 3875 | } |
| 3876 | if (rc == -1) { |
| 3877 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 3878 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 3879 | "The default value \"%s\" of the deviated node \"%s\"no longer matches its type.", |
| 3880 | target_name); |
| 3881 | goto error; |
| 3882 | } |
| 3883 | } |
| 3884 | ly_set_free(dflt_check); |
| 3885 | |
| 3886 | /* mark all the affected modules as deviated and implemented*/ |
| 3887 | for(parent = dev_target; parent; parent = lys_parent(parent)) { |
| 3888 | mod = lys_node_module(parent); |
| 3889 | if (module != mod) { |
| 3890 | mod->deviated = 1; |
| 3891 | lys_set_implemented(mod); |
| 3892 | } |
| 3893 | } |
| 3894 | |
| 3895 | return EXIT_SUCCESS; |
| 3896 | error: |
| 3897 | ly_set_free(dflt_check); |
| 3898 | return EXIT_FAILURE; |
| 3899 | } |
| 3900 | |
| 3901 | static int |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3902 | yang_check_sub_module(struct lys_module *module, struct unres_schema *unres, struct lys_node *node) |
| 3903 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3904 | uint i, erase_identities = 1, erase_nodes = 1, aug_size, dev_size = 0; |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3905 | |
| 3906 | aug_size = module->augment_size; |
| 3907 | module->augment_size = 0; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3908 | dev_size = module->deviation_size; |
| 3909 | module->deviation_size = 0; |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 3910 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3911 | if (yang_check_typedef(module, NULL, unres)) { |
| 3912 | goto error; |
| 3913 | } |
| 3914 | |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 3915 | /* check features */ |
| 3916 | for (i = 0; i < module->features_size; ++i) { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3917 | if (yang_check_iffeatures(module, NULL, &module->features[i], FEATURE_KEYWORD, unres)) { |
| 3918 | goto error; |
| 3919 | } |
| 3920 | /* check for circular dependencies */ |
| 3921 | if (module->features[i].iffeature_size && (unres_schema_add_node(module, unres, &module->features[i], UNRES_FEATURE, NULL) == -1)) { |
| 3922 | goto error; |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 3923 | } |
| 3924 | } |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3925 | erase_identities = 0; |
| 3926 | if (yang_check_identities(module, unres)) { |
| 3927 | goto error; |
| 3928 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3929 | erase_nodes = 0; |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3930 | if (yang_check_nodes(module, NULL, node, CONFIG_INHERIT_ENABLE, unres)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3931 | goto error; |
| 3932 | } |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 3933 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3934 | /* check deviation */ |
| 3935 | for (i = 0; i < dev_size; ++i) { |
| 3936 | module->deviation_size++; |
| 3937 | if (yang_check_deviation(module, unres, &module->deviation[i])) { |
| 3938 | goto error; |
| 3939 | } |
| 3940 | } |
| 3941 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3942 | /* check augments */ |
| 3943 | for (i = 0; i < aug_size; ++i) { |
| 3944 | module->augment_size++; |
| 3945 | if (yang_check_augment(module, &module->augment[i], CONFIG_INHERIT_ENABLE, unres)) { |
| 3946 | goto error; |
| 3947 | } |
| 3948 | if (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1) { |
| 3949 | goto error; |
| 3950 | } |
| 3951 | } |
| 3952 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3953 | return EXIT_SUCCESS; |
| 3954 | error: |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3955 | if (erase_identities) { |
| 3956 | yang_free_ident_base(module->ident, 0, module->ident_size); |
| 3957 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3958 | if (erase_nodes) { |
| 3959 | yang_free_nodes(module->ctx, node); |
| 3960 | } |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3961 | for (i = module->augment_size; i < aug_size; ++i) { |
| 3962 | yang_free_augment(module->ctx, &module->augment[i]); |
| 3963 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3964 | for (i = module->deviation_size; i < dev_size; ++i) { |
| 3965 | yang_free_deviate(module->ctx, &module->deviation[i], 0); |
| 3966 | free(module->deviation[i].deviate); |
| 3967 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3968 | return EXIT_FAILURE; |
| 3969 | } |