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); |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 23 | static int yang_check_must(struct lys_module *module, struct lys_restr *must, uint size, struct unres_schema *unres); |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 24 | 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] | 25 | 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] | 26 | 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] | 27 | static int yang_check_nodes(struct lys_module *module, struct lys_node *parent, struct lys_node *nodes, |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 28 | int options, struct unres_schema *unres); |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 29 | static int yang_fill_ext_substm_index(struct lys_ext_instance_complex *ext, LY_STMT stmt, enum yytokentype keyword); |
PavolVican | f3091bf | 2017-02-19 18:27:01 +0100 | [diff] [blame] | 30 | static void yang_free_nodes(struct ly_ctx *ctx, struct lys_node *node); |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 31 | void lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size, int shallow, |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 32 | void (*private_destructor)(const struct lys_node *node, void *priv)); |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 33 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 34 | static int |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 35 | yang_check_string(struct lys_module *module, const char **target, char *what, |
| 36 | char *where, char *value, struct lys_node *node) |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 37 | { |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 38 | if (*target) { |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 39 | LOGVAL(LYE_TOOMANY, (node) ? LY_VLOG_LYS : LY_VLOG_NONE, node, what, where); |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 40 | free(value); |
| 41 | return 1; |
| 42 | } else { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 43 | *target = lydict_insert_zc(module->ctx, value); |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | } |
| 47 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 48 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 49 | yang_read_common(struct lys_module *module, char *value, enum yytokentype type) |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 50 | { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 51 | int ret = 0; |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 52 | |
| 53 | switch (type) { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 54 | case MODULE_KEYWORD: |
| 55 | module->name = lydict_insert_zc(module->ctx, value); |
| 56 | break; |
| 57 | case NAMESPACE_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 58 | ret = yang_check_string(module, &module->ns, "namespace", "module", value, NULL); |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 59 | break; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 60 | case ORGANIZATION_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 61 | ret = yang_check_string(module, &module->org, "organization", "module", value, NULL); |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 62 | break; |
| 63 | case CONTACT_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 64 | ret = yang_check_string(module, &module->contact, "contact", "module", value, NULL); |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 65 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 66 | default: |
| 67 | free(value); |
| 68 | LOGINT; |
| 69 | ret = EXIT_FAILURE; |
| 70 | break; |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 73 | return ret; |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 76 | int |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 77 | yang_check_version(struct lys_module *module, struct lys_submodule *submodule, char *value, int repeat) |
| 78 | { |
| 79 | int ret = EXIT_SUCCESS; |
| 80 | |
| 81 | if (repeat) { |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 82 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "yang version", "module"); |
| 83 | ret = EXIT_FAILURE; |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 84 | } else { |
| 85 | if (!strcmp(value, "1")) { |
| 86 | if (submodule) { |
| 87 | if (module->version > 1) { |
| 88 | LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL); |
| 89 | ret = EXIT_FAILURE; |
| 90 | } |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 91 | submodule->version = 1; |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 92 | } else { |
| 93 | module->version = 1; |
| 94 | } |
| 95 | } else if (!strcmp(value, "1.1")) { |
| 96 | if (submodule) { |
| 97 | if (module->version != 2) { |
| 98 | LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL); |
| 99 | ret = EXIT_FAILURE; |
| 100 | } |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 101 | submodule->version = 2; |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 102 | } else { |
| 103 | module->version = 2; |
| 104 | } |
| 105 | } else { |
| 106 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version"); |
| 107 | ret = EXIT_FAILURE; |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 108 | } |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 109 | } |
| 110 | free(value); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | int |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 115 | 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] | 116 | { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 117 | int ret = 0; |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 118 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 119 | if (!imp && lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 120 | free(value); |
| 121 | return EXIT_FAILURE; |
| 122 | } |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 123 | |
| 124 | if (imp) { |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 125 | ret = yang_check_string(module, &imp->prefix, "prefix", "import", value, NULL); |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 126 | } else { |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 127 | ret = yang_check_string(module, &module->prefix, "prefix", "module", value, NULL); |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 128 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 129 | |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 130 | return ret; |
| 131 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 132 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 133 | static int |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 134 | yang_fill_import(struct lys_module *module, struct lys_import *imp_old, struct lys_import *imp_new, |
| 135 | char *value, struct unres_schema *unres) |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 136 | { |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 137 | const char *exp; |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 138 | int rc; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 139 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 140 | if (!imp_old->prefix) { |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 141 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "import"); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 142 | goto error; |
| 143 | } else { |
| 144 | if (lyp_check_identifier(imp_old->prefix, LY_IDENT_PREFIX, module, NULL)) { |
| 145 | goto error; |
| 146 | } |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 147 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 148 | memcpy(imp_new, imp_old, sizeof *imp_old); |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 149 | exp = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 150 | rc = lyp_check_import(module, exp, imp_new); |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 151 | lydict_remove(module->ctx, exp); |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 152 | module->imp_size++; |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 153 | if (rc || yang_check_ext_instance(module, &imp_new->ext, imp_new->ext_size, imp_new, unres)) { |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 154 | return EXIT_FAILURE; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 155 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 156 | |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 157 | return EXIT_SUCCESS; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 158 | |
| 159 | error: |
| 160 | free(value); |
| 161 | lydict_remove(module->ctx, imp_old->dsc); |
| 162 | lydict_remove(module->ctx, imp_old->ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 163 | lys_extension_instances_free(module->ctx, imp_old->ext, imp_old->ext_size, NULL); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 164 | return EXIT_FAILURE; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 165 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 166 | |
| 167 | int |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 168 | yang_read_description(struct lys_module *module, void *node, char *value, char *where, enum yytokentype type) |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 169 | { |
| 170 | int ret; |
Pavol Vican | 9bcd7c6 | 2016-03-17 19:36:35 +0100 | [diff] [blame] | 171 | char *dsc = "description"; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 172 | |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 173 | switch (type) { |
| 174 | case MODULE_KEYWORD: |
| 175 | ret = yang_check_string(module, &module->dsc, dsc, "module", value, NULL); |
| 176 | break; |
| 177 | case REVISION_KEYWORD: |
| 178 | ret = yang_check_string(module, &((struct lys_revision *)node)->dsc, dsc, where, value, NULL); |
| 179 | break; |
| 180 | case IMPORT_KEYWORD: |
| 181 | ret = yang_check_string(module, &((struct lys_import *)node)->dsc, dsc, where, value, NULL); |
| 182 | break; |
| 183 | case INCLUDE_KEYWORD: |
| 184 | ret = yang_check_string(module, &((struct lys_include *)node)->dsc, dsc, where, value, NULL); |
| 185 | break; |
| 186 | case NODE_PRINT: |
| 187 | ret = yang_check_string(module, &((struct lys_node *)node)->dsc, dsc, where, value, node); |
| 188 | break; |
| 189 | default: |
| 190 | ret = yang_check_string(module, &((struct lys_node *)node)->dsc, dsc, where, value, NULL); |
| 191 | break; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 192 | } |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | int |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 197 | yang_read_reference(struct lys_module *module, void *node, char *value, char *where, enum yytokentype type) |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 198 | { |
| 199 | int ret; |
Pavol Vican | f5fe966 | 2016-03-17 20:00:16 +0100 | [diff] [blame] | 200 | char *ref = "reference"; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 201 | |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 202 | switch (type) { |
| 203 | case MODULE_KEYWORD: |
| 204 | ret = yang_check_string(module, &module->ref, ref, "module", value, NULL); |
| 205 | break; |
| 206 | case REVISION_KEYWORD: |
| 207 | ret = yang_check_string(module, &((struct lys_revision *)node)->ref, ref, where, value, NULL); |
| 208 | break; |
| 209 | case IMPORT_KEYWORD: |
| 210 | ret = yang_check_string(module, &((struct lys_import *)node)->ref, ref, where, value, NULL); |
| 211 | break; |
| 212 | case INCLUDE_KEYWORD: |
| 213 | ret = yang_check_string(module, &((struct lys_include *)node)->ref, ref, where, value, NULL); |
| 214 | break; |
| 215 | case NODE_PRINT: |
| 216 | ret = yang_check_string(module, &((struct lys_node *)node)->ref, ref, where, value, node); |
| 217 | break; |
| 218 | default: |
| 219 | ret = yang_check_string(module, &((struct lys_node *)node)->ref, ref, where, value, NULL); |
| 220 | break; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 221 | } |
| 222 | return ret; |
| 223 | } |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 224 | |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 225 | int |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 226 | yang_fill_iffeature(struct lys_module *module, struct lys_iffeature *iffeature, void *parent, |
| 227 | char *value, struct unres_schema *unres, int parent_is_feature) |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 228 | { |
| 229 | const char *exp; |
| 230 | int ret; |
| 231 | |
Michal Vasko | 97b32be | 2016-07-25 10:59:53 +0200 | [diff] [blame] | 232 | if ((module->version != 2) && ((value[0] == '(') || strchr(value, ' '))) { |
| 233 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 234 | free(value); |
| 235 | return EXIT_FAILURE; |
| 236 | } |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 237 | |
Michal Vasko | 56d082c | 2016-10-25 14:00:42 +0200 | [diff] [blame] | 238 | if (!(exp = transform_iffeat_schema2json(module, value))) { |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 239 | free(value); |
| 240 | return EXIT_FAILURE; |
| 241 | } |
| 242 | free(value); |
| 243 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 244 | 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] | 245 | lydict_remove(module->ctx, exp); |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 246 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 247 | return (ret) ? EXIT_FAILURE : EXIT_SUCCESS; |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 250 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 251 | 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] | 252 | { |
| 253 | const char *exp; |
| 254 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 255 | exp = transform_schema2json(module, value); |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 256 | free(value); |
| 257 | if (!exp) { |
| 258 | return EXIT_FAILURE; |
| 259 | } |
Pavol Vican | 4ffd0ab | 2016-08-27 16:35:04 +0200 | [diff] [blame] | 260 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 261 | if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, exp) == -1) { |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 262 | lydict_remove(module->ctx, exp); |
| 263 | return EXIT_FAILURE; |
| 264 | } |
Pavol Vican | 44dde2c | 2016-02-10 11:18:14 +0100 | [diff] [blame] | 265 | |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 266 | lydict_remove(module->ctx, exp); |
| 267 | return EXIT_SUCCESS; |
| 268 | } |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 269 | |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 270 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 271 | 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] | 272 | { |
| 273 | int ret; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 274 | |
Pavol Vican | dde090a | 2016-08-30 15:12:14 +0200 | [diff] [blame] | 275 | if (message == ERROR_APP_TAG_KEYWORD) { |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 276 | ret = yang_check_string(module, &save->eapptag, "error_app_tag", what, value, NULL); |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 277 | } else { |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 278 | ret = yang_check_string(module, &save->emsg, "error_message", what, value, NULL); |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 279 | } |
| 280 | return ret; |
| 281 | } |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 282 | |
| 283 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 284 | 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] | 285 | { |
| 286 | if (cont->presence) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 287 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, cont, "presence", "container"); |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 288 | free(value); |
| 289 | return EXIT_FAILURE; |
| 290 | } else { |
| 291 | cont->presence = lydict_insert_zc(module->ctx, value); |
| 292 | return EXIT_SUCCESS; |
| 293 | } |
| 294 | } |
| 295 | |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 296 | void * |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 297 | 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] | 298 | { |
| 299 | struct lys_when *retval; |
| 300 | |
| 301 | retval = calloc(1, sizeof *retval); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 302 | LY_CHECK_ERR_RETURN(!retval, LOGMEM; free(value), NULL); |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 303 | retval->cond = transform_schema2json(module, value); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 304 | if (!retval->cond) { |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 305 | goto error; |
| 306 | } |
| 307 | switch (type) { |
| 308 | case CONTAINER_KEYWORD: |
| 309 | if (((struct lys_node_container *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 310 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "container"); |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 311 | goto error; |
| 312 | } |
| 313 | ((struct lys_node_container *)node)->when = retval; |
| 314 | break; |
Pavol Vican | db7489e | 2016-08-23 17:23:39 +0200 | [diff] [blame] | 315 | case ANYDATA_KEYWORD: |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 316 | case ANYXML_KEYWORD: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 317 | if (((struct lys_node_anydata *)node)->when) { |
Pavol Vican | db7489e | 2016-08-23 17:23:39 +0200 | [diff] [blame] | 318 | 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] | 319 | goto error; |
| 320 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 321 | ((struct lys_node_anydata *)node)->when = retval; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 322 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 323 | case CHOICE_KEYWORD: |
| 324 | if (((struct lys_node_choice *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 325 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "choice"); |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 326 | goto error; |
| 327 | } |
| 328 | ((struct lys_node_choice *)node)->when = retval; |
| 329 | break; |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 330 | case CASE_KEYWORD: |
| 331 | if (((struct lys_node_case *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 332 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "case"); |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 333 | goto error; |
| 334 | } |
| 335 | ((struct lys_node_case *)node)->when = retval; |
| 336 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 337 | case LEAF_KEYWORD: |
| 338 | if (((struct lys_node_leaf *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 339 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaf"); |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 340 | goto error; |
| 341 | } |
| 342 | ((struct lys_node_leaf *)node)->when = retval; |
| 343 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 344 | case LEAF_LIST_KEYWORD: |
| 345 | if (((struct lys_node_leaflist *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 346 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaflist"); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 347 | goto error; |
| 348 | } |
| 349 | ((struct lys_node_leaflist *)node)->when = retval; |
| 350 | break; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 351 | case LIST_KEYWORD: |
| 352 | if (((struct lys_node_list *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 353 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "list"); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 354 | goto error; |
| 355 | } |
| 356 | ((struct lys_node_list *)node)->when = retval; |
| 357 | break; |
Pavol Vican | 14b1856 | 2016-03-01 16:04:29 +0100 | [diff] [blame] | 358 | case USES_KEYWORD: |
| 359 | if (((struct lys_node_uses *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 360 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "uses"); |
Pavol Vican | 14b1856 | 2016-03-01 16:04:29 +0100 | [diff] [blame] | 361 | goto error; |
| 362 | } |
| 363 | ((struct lys_node_uses *)node)->when = retval; |
| 364 | break; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 365 | case AUGMENT_KEYWORD: |
| 366 | if (((struct lys_node_augment *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 367 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "augment"); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 368 | goto error; |
| 369 | } |
| 370 | ((struct lys_node_augment *)node)->when = retval; |
| 371 | break; |
PavolVican | b031708 | 2017-02-19 01:29:22 +0100 | [diff] [blame] | 372 | case EXTENSION_INSTANCE: |
| 373 | *(struct lys_when **)node = retval; |
| 374 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 375 | default: |
| 376 | goto error; |
| 377 | break; |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 378 | } |
| 379 | free(value); |
| 380 | return retval; |
| 381 | |
| 382 | error: |
| 383 | free(value); |
PavolVican | b031708 | 2017-02-19 01:29:22 +0100 | [diff] [blame] | 384 | lydict_remove(module->ctx, retval->cond); |
| 385 | free(retval); |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 386 | return NULL; |
| 387 | } |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 388 | |
| 389 | void * |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 390 | yang_read_node(struct lys_module *module, struct lys_node *parent, struct lys_node **root, |
| 391 | char *value, int nodetype, int sizeof_struct) |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 392 | { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 393 | struct lys_node *node, **child; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 394 | |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 395 | node = calloc(1, sizeof_struct); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 396 | LY_CHECK_ERR_RETURN(!node, LOGMEM; free(value), NULL); |
| 397 | |
Michal Vasko | 3e3228d | 2017-02-24 14:55:32 +0100 | [diff] [blame] | 398 | LOGDBG(LY_LDGYANG, "parsing %s statement \"%s\"", strnodetype(nodetype), value); |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 399 | node->name = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 400 | node->module = module; |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 401 | node->nodetype = nodetype; |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 402 | |
| 403 | /* insert the node into the schema tree */ |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 404 | child = (parent) ? &parent->child : root; |
| 405 | if (*child) { |
| 406 | (*child)->prev->next = node; |
| 407 | (*child)->prev = node; |
| 408 | } else { |
| 409 | *child = node; |
| 410 | node->prev = node; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 411 | } |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 412 | return node; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 416 | 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] | 417 | { |
| 418 | int ret; |
| 419 | |
| 420 | switch (type) { |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 421 | case LEAF_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 422 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dflt, "default", "leaf", value, node); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 423 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 424 | case TYPEDEF_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 425 | ret = yang_check_string(module, &((struct lys_tpdf *) node)->dflt, "default", "typedef", value, NULL); |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 426 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 427 | default: |
| 428 | free(value); |
| 429 | LOGINT; |
| 430 | ret = EXIT_FAILURE; |
| 431 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 432 | } |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 437 | 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] | 438 | { |
| 439 | int ret; |
| 440 | |
| 441 | switch (type) { |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 442 | case LEAF_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 443 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->units, "units", "leaf", value, node); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 444 | break; |
| 445 | case LEAF_LIST_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 446 | ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->units, "units", "leaflist", value, node); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 447 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 448 | case TYPEDEF_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 449 | ret = yang_check_string(module, &((struct lys_tpdf *) node)->units, "units", "typedef", value, NULL); |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 450 | break; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 451 | case ADD_KEYWORD: |
| 452 | case REPLACE_KEYWORD: |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 453 | case DELETE_KEYWORD: |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 454 | ret = yang_check_string(module, &((struct lys_deviate *) node)->units, "units", "deviate", value, NULL); |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 455 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 456 | default: |
| 457 | free(value); |
| 458 | LOGINT; |
| 459 | ret = EXIT_FAILURE; |
| 460 | break; |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 461 | } |
| 462 | return ret; |
| 463 | } |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 464 | |
| 465 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 466 | 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] | 467 | { |
| 468 | char *exp, *value; |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 469 | struct lys_node *node; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 470 | |
| 471 | exp = value = (char *) list->keys; |
| 472 | while ((value = strpbrk(value, " \t\n"))) { |
| 473 | list->keys_size++; |
| 474 | while (isspace(*value)) { |
| 475 | value++; |
| 476 | } |
| 477 | } |
| 478 | list->keys_size++; |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 479 | |
| 480 | list->keys_str = lydict_insert_zc(module->ctx, exp); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 481 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 482 | LY_CHECK_ERR_RETURN(!list->keys, LOGMEM, EXIT_FAILURE); |
| 483 | |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 484 | for (node = list->parent; node && node->nodetype != LYS_GROUPING; node = lys_parent(node)); |
| 485 | if (!node && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) { |
| 486 | return EXIT_FAILURE; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 487 | } |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 488 | return EXIT_SUCCESS; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 492 | 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] | 493 | { |
| 494 | int i, j; |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 495 | char *vaux, c; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 496 | struct unres_list_uniq *unique_info; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 497 | |
| 498 | /* count the number of unique leafs in the value */ |
| 499 | vaux = value; |
| 500 | while ((vaux = strpbrk(vaux, " \t\n"))) { |
| 501 | unique->expr_size++; |
| 502 | while (isspace(*vaux)) { |
| 503 | vaux++; |
| 504 | } |
| 505 | } |
| 506 | unique->expr_size++; |
| 507 | unique->expr = calloc(unique->expr_size, sizeof *unique->expr); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 508 | LY_CHECK_ERR_GOTO(!unique->expr, LOGMEM, error); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 509 | |
| 510 | for (i = 0; i < unique->expr_size; i++) { |
| 511 | vaux = strpbrk(value, " \t\n"); |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 512 | if (vaux) { |
| 513 | c = *vaux; |
| 514 | *vaux = '\0'; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 517 | /* store token into unique structure (includes converting prefix to the module name) */ |
| 518 | unique->expr[i] = transform_schema2json(module, value); |
| 519 | if (vaux) { |
| 520 | *vaux = c; |
| 521 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 522 | |
| 523 | /* check that the expression does not repeat */ |
| 524 | for (j = 0; j < i; j++) { |
| 525 | if (ly_strequal(unique->expr[j], unique->expr[i], 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 526 | LOGVAL(LYE_INARG, LY_VLOG_LYS, list, unique->expr[i], "unique"); |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 527 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, list, "The identifier is not unique"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 528 | goto error; |
| 529 | } |
| 530 | } |
| 531 | /* try to resolve leaf */ |
| 532 | if (unres) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 533 | unique_info = malloc(sizeof *unique_info); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 534 | LY_CHECK_ERR_GOTO(!unique_info, LOGMEM, error); |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 535 | unique_info->list = (struct lys_node *)list; |
| 536 | unique_info->expr = unique->expr[i]; |
| 537 | unique_info->trg_type = &unique->trg_type; |
| 538 | 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] | 539 | goto error; |
| 540 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 541 | } else { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 542 | 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] | 543 | goto error; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* move to next token */ |
| 548 | value = vaux; |
Radek Krejci | 1a9c361 | 2017-04-24 14:49:43 +0200 | [diff] [blame] | 549 | while(value && isspace(*value)) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 550 | value++; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | return EXIT_SUCCESS; |
| 555 | |
| 556 | error: |
| 557 | return EXIT_FAILURE; |
| 558 | } |
| 559 | |
| 560 | int |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 561 | yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres) |
| 562 | { |
| 563 | uint8_t k; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 564 | char *str; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 565 | |
| 566 | for(k=0; k<list->unique_size; k++) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 567 | str = (char *)list->unique[k].expr; |
| 568 | if (yang_fill_unique(module, list, &list->unique[k], str, unres)) { |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 569 | goto error; |
| 570 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 571 | free(str); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 572 | } |
| 573 | return EXIT_SUCCESS; |
| 574 | |
| 575 | error: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 576 | free(str); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 577 | return EXIT_FAILURE; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Pavol Vican | 07f220f | 2016-09-02 13:04:37 +0200 | [diff] [blame] | 580 | int |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 581 | 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] | 582 | { |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 583 | if (stype->base && (stype->base != LY_TYPE_LEAFREF)) { |
| 584 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance"); |
| 585 | goto error; |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 586 | } |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 587 | if (stype->type->info.lref.path) { |
| 588 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "path", "type"); |
| 589 | goto error; |
| 590 | } |
| 591 | stype->type->info.lref.path = lydict_insert_zc(module->ctx, value); |
| 592 | stype->base = LY_TYPE_LEAFREF; |
| 593 | return EXIT_SUCCESS; |
| 594 | |
| 595 | error: |
| 596 | free(value); |
| 597 | return EXIT_FAILURE; |
| 598 | } |
| 599 | |
| 600 | int |
| 601 | yang_read_require_instance(struct yang_type *stype, int req) |
| 602 | { |
| 603 | if (stype->base && (stype->base != LY_TYPE_LEAFREF)) { |
| 604 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance"); |
| 605 | return EXIT_FAILURE; |
| 606 | } |
| 607 | if (stype->type->info.lref.req) { |
| 608 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "require-instance", "type"); |
| 609 | return EXIT_FAILURE; |
| 610 | } |
| 611 | stype->type->info.lref.req = req; |
| 612 | stype->base = LY_TYPE_LEAFREF; |
| 613 | return EXIT_SUCCESS; |
| 614 | } |
| 615 | |
| 616 | int |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 617 | 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] | 618 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 619 | int rc, ret = -1; |
| 620 | unsigned int i, j; |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 621 | int8_t req; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 622 | const char *name, *value; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 623 | LY_DATA_TYPE base = 0, base_tmp; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 624 | struct lys_node *siter; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 625 | struct lys_type *dertype; |
| 626 | struct lys_type_enum *enms_sc = NULL; |
| 627 | struct lys_type_bit *bits_sc = NULL; |
| 628 | struct lys_type_bit bit_tmp; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 629 | struct yang_type *yang; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 630 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 631 | value = transform_schema2json(module, typ->name); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 632 | if (!value) { |
| 633 | goto error; |
| 634 | } |
| 635 | |
| 636 | i = parse_identifier(value); |
| 637 | if (i < 1) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 638 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 639 | lydict_remove(module->ctx, value); |
| 640 | goto error; |
| 641 | } |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 642 | /* module name */ |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 643 | name = value; |
| 644 | if (value[i]) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 645 | type->module_name = lydict_insert(module->ctx, value, i); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 646 | name += i; |
| 647 | if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 648 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 649 | lydict_remove(module->ctx, value); |
| 650 | goto error; |
| 651 | } |
| 652 | ++name; |
| 653 | } |
| 654 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 655 | rc = resolve_superior_type(name, type->module_name, module, parent, &type->der); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 656 | if (rc == -1) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 657 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, type->module_name); |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 658 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 659 | goto error; |
| 660 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 661 | /* 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] | 662 | } else if (rc == EXIT_FAILURE) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 663 | LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name); |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 664 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 665 | ret = EXIT_FAILURE; |
| 666 | goto error; |
| 667 | } |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 668 | lydict_remove(module->ctx, value); |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 669 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 670 | if (type->base == LY_TYPE_ERR) { |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 671 | /* 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] | 672 | * 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] | 673 | for (siter = parent; siter && (siter->nodetype != LYS_GROUPING); siter = lys_parent(siter)); |
| 674 | if (siter) { |
Radek Krejci | 93def38 | 2017-05-24 15:33:48 +0200 | [diff] [blame] | 675 | assert(((struct lys_node_grp *)siter)->unres_count); |
| 676 | ((struct lys_node_grp *)siter)->unres_count--; |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 677 | } else { |
| 678 | LOGINT; |
| 679 | goto error; |
| 680 | } |
| 681 | } |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 682 | |
| 683 | /* check status */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 684 | if (lyp_check_status(type->parent->flags, type->parent->module, type->parent->name, |
| 685 | type->der->flags, type->der->module, type->der->name, parent)) { |
Pavol Vican | fdab9f9 | 2016-09-07 15:23:27 +0200 | [diff] [blame] | 686 | goto error; |
| 687 | } |
| 688 | |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 689 | base = typ->base; |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 690 | base_tmp = type->base; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 691 | type->base = type->der->type.base; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 692 | if (base == 0) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 693 | base = type->der->type.base; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 694 | } |
| 695 | switch (base) { |
| 696 | case LY_TYPE_STRING: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 697 | if (type->base == LY_TYPE_BINARY) { |
| 698 | if (type->info.str.pat_count) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 699 | 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] | 700 | goto error; |
| 701 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 702 | type->info.binary.length = type->info.str.length; |
| 703 | if (type->info.binary.length && lyp_check_length_range(type->info.binary.length->expr, type)) { |
| 704 | 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] | 705 | goto error; |
| 706 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 707 | } else if (type->base == LY_TYPE_STRING) { |
| 708 | if (type->info.str.length && lyp_check_length_range(type->info.str.length->expr, type)) { |
| 709 | 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] | 710 | goto error; |
| 711 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 712 | } else { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 713 | 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] | 714 | goto error; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 715 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 716 | break; |
| 717 | case LY_TYPE_DEC64: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 718 | if (type->base == LY_TYPE_DEC64) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 719 | /* mandatory sub-statement(s) check */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 720 | if (!type->info.dec64.dig && !type->der->type.der) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 721 | /* decimal64 type directly derived from built-in type requires fraction-digits */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 722 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 723 | goto error; |
| 724 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 725 | if (type->info.dec64.dig && type->der->type.der) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 726 | /* 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] | 727 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 728 | goto error; |
| 729 | } |
Radek Krejci | 4800f65 | 2016-09-08 14:02:52 +0200 | [diff] [blame] | 730 | |
| 731 | /* copy fraction-digits specification from parent type for easier internal use */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 732 | if (type->der->type.der) { |
| 733 | type->info.dec64.dig = type->der->type.info.dec64.dig; |
| 734 | type->info.dec64.div = type->der->type.info.dec64.div; |
Radek Krejci | 4800f65 | 2016-09-08 14:02:52 +0200 | [diff] [blame] | 735 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 736 | if (type->info.dec64.range && lyp_check_length_range(type->info.dec64.range->expr, type)) { |
| 737 | 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] | 738 | goto error; |
| 739 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 740 | } else if (type->base >= LY_TYPE_INT8 && type->base <=LY_TYPE_UINT64) { |
| 741 | if (type->info.dec64.dig) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 742 | 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] | 743 | goto error; |
| 744 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 745 | type->info.num.range = type->info.dec64.range; |
| 746 | if (type->info.num.range && lyp_check_length_range(type->info.num.range->expr, type)) { |
| 747 | 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] | 748 | goto error; |
| 749 | } |
| 750 | } else { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 751 | 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] | 752 | goto error; |
| 753 | } |
| 754 | break; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 755 | case LY_TYPE_ENUM: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 756 | if (type->base != LY_TYPE_ENUM) { |
| 757 | 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] | 758 | goto error; |
| 759 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 760 | dertype = &type->der->type; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 761 | |
| 762 | if (!dertype->der) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 763 | if (!type->info.enums.count) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 764 | /* type is derived directly from buit-in enumeartion type and enum statement is required */ |
| 765 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type"); |
| 766 | goto error; |
| 767 | } |
| 768 | } else { |
| 769 | for (; !dertype->info.enums.count; dertype = &dertype->der->type); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 770 | if (module->version < 2 && type->info.enums.count) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 771 | /* type is not directly derived from built-in enumeration type and enum statement is prohibited |
| 772 | * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */ |
| 773 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum"); |
| 774 | goto error; |
| 775 | } |
| 776 | |
| 777 | /* restricted enumeration type - the name MUST be used in the base type */ |
| 778 | enms_sc = dertype->info.enums.enm; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 779 | for(i = 0; i < type->info.enums.count; i++) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 780 | for (j = 0; j < dertype->info.enums.count; j++) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 781 | 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] | 782 | break; |
| 783 | } |
| 784 | } |
| 785 | if (j == dertype->info.enums.count) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 786 | 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] | 787 | goto error; |
| 788 | } |
| 789 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 790 | if (type->info.enums.enm[i].flags & LYS_AUTOASSIGNED) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 791 | /* automatically assign value from base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 792 | type->info.enums.enm[i].value = enms_sc[j].value; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 793 | } else { |
| 794 | /* check that the assigned value corresponds to the original |
| 795 | * value of the enum in the base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 796 | if (type->info.enums.enm[i].value != enms_sc[j].value) { |
| 797 | /* type->info.enums.enm[i].value - assigned value in restricted enum |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 798 | * 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] | 799 | LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL, type->info.enums.enm[i].value, |
| 800 | type->info.enums.enm[i].name, enms_sc[j].value); |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 801 | goto error; |
| 802 | } |
| 803 | } |
| 804 | } |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 805 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 806 | break; |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 807 | case LY_TYPE_BITS: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 808 | if (type->base != LY_TYPE_BITS) { |
| 809 | 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] | 810 | goto error; |
| 811 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 812 | dertype = &type->der->type; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 813 | |
| 814 | if (!dertype->der) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 815 | if (!type->info.bits.count) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 816 | /* type is derived directly from buit-in bits type and bit statement is required */ |
| 817 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type"); |
| 818 | goto error; |
| 819 | } |
| 820 | } else { |
| 821 | for (; !dertype->info.enums.count; dertype = &dertype->der->type); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 822 | if (module->version < 2 && type->info.bits.count) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 823 | /* type is not directly derived from buit-in bits type and bit statement is prohibited, |
| 824 | * since YANG 1.1 the bit statements can be used to restrict the base bits type */ |
| 825 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit"); |
| 826 | goto error; |
| 827 | } |
| 828 | |
| 829 | bits_sc = dertype->info.bits.bit; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 830 | for (i = 0; i < type->info.bits.count; i++) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 831 | for (j = 0; j < dertype->info.bits.count; j++) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 832 | 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] | 833 | break; |
| 834 | } |
| 835 | } |
| 836 | if (j == dertype->info.bits.count) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 837 | 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] | 838 | goto error; |
| 839 | } |
| 840 | |
| 841 | /* restricted bits type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 842 | if (type->info.bits.bit[i].flags & LYS_AUTOASSIGNED) { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 843 | /* automatically assign position from base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 844 | type->info.bits.bit[i].pos = bits_sc[j].pos; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 845 | } else { |
| 846 | /* check that the assigned position corresponds to the original |
| 847 | * position of the bit in the base type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 848 | if (type->info.bits.bit[i].pos != bits_sc[j].pos) { |
| 849 | /* type->info.bits.bit[i].pos - assigned position in restricted bits |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 850 | * 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] | 851 | LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, type->info.bits.bit[i].pos, |
| 852 | type->info.bits.bit[i].name, bits_sc[j].pos); |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 853 | goto error; |
| 854 | } |
| 855 | } |
| 856 | } |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 857 | } |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 858 | |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 859 | for (i = type->info.bits.count; i > 0; i--) { |
| 860 | j = i - 1; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 861 | |
| 862 | /* keep them ordered by position */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 863 | 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] | 864 | /* switch them */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 865 | memcpy(&bit_tmp, &type->info.bits.bit[j], sizeof bit_tmp); |
| 866 | memcpy(&type->info.bits.bit[j], &type->info.bits.bit[j - 1], sizeof bit_tmp); |
| 867 | memcpy(&type->info.bits.bit[j - 1], &bit_tmp, sizeof bit_tmp); |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 868 | j--; |
| 869 | } |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 870 | } |
| 871 | break; |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 872 | case LY_TYPE_LEAFREF: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 873 | if (type->base == LY_TYPE_INST) { |
| 874 | if (type->info.lref.path) { |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 875 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "path"); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 876 | goto error; |
| 877 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 878 | if ((req = type->info.lref.req)) { |
| 879 | type->info.inst.req = req; |
Pavol Vican | 81344ac | 2016-09-02 14:23:06 +0200 | [diff] [blame] | 880 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 881 | } else if (type->base == LY_TYPE_LEAFREF) { |
Pavol Vican | 92626d7 | 2016-09-21 09:36:09 +0200 | [diff] [blame] | 882 | /* require-instance only YANG 1.1 */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 883 | if (type->info.lref.req && (module->version < 2)) { |
Pavol Vican | 92626d7 | 2016-09-21 09:36:09 +0200 | [diff] [blame] | 884 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance"); |
| 885 | goto error; |
| 886 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 887 | /* flag resolving for later use */ |
| 888 | if (!tpdftype) { |
| 889 | for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter)); |
| 890 | if (siter) { |
| 891 | /* just a flag - do not resolve */ |
| 892 | tpdftype = 1; |
| 893 | } |
| 894 | } |
| 895 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 896 | if (type->info.lref.path) { |
| 897 | if (type->der->type.der) { |
Pavol Vican | 894ee0f | 2016-08-30 15:29:46 +0200 | [diff] [blame] | 898 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "path"); |
| 899 | goto error; |
| 900 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 901 | value = type->info.lref.path; |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 902 | /* store in the JSON format */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 903 | type->info.lref.path = transform_schema2json(module, value); |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 904 | lydict_remove(module->ctx, value); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 905 | if (!type->info.lref.path) { |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 906 | goto error; |
| 907 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 908 | /* try to resolve leafref path only when this is instantiated |
| 909 | * leaf, so it is not: |
| 910 | * - typedef's type, |
| 911 | * - in grouping definition, |
| 912 | * - just instantiated in a grouping definition, |
| 913 | * because in those cases the nodes referenced in path might not be present |
| 914 | * and it is not a bug. */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 915 | 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] | 916 | goto error; |
| 917 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 918 | } else if (!type->der->type.der) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 919 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type"); |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 920 | goto error; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 921 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 922 | /* copy leafref definition into the derived type */ |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 923 | 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] | 924 | /* 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] | 925 | 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] | 926 | goto error; |
| 927 | } |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 928 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 929 | } else { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 930 | 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] | 931 | goto error; |
| 932 | } |
| 933 | break; |
| 934 | case LY_TYPE_IDENT: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 935 | if (type->base != LY_TYPE_IDENT) { |
| 936 | 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] | 937 | goto error; |
| 938 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 939 | if (type->der->type.der) { |
| 940 | if (type->info.ident.ref) { |
Pavol Vican | 07f220f | 2016-09-02 13:04:37 +0200 | [diff] [blame] | 941 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "base"); |
| 942 | goto error; |
| 943 | } |
| 944 | } else { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 945 | if (!type->info.ident.ref) { |
Pavol Vican | 07f220f | 2016-09-02 13:04:37 +0200 | [diff] [blame] | 946 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type"); |
| 947 | goto error; |
| 948 | } |
| 949 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 950 | break; |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 951 | case LY_TYPE_UNION: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 952 | if (type->base != LY_TYPE_UNION) { |
| 953 | 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] | 954 | goto error; |
| 955 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 956 | if (!type->info.uni.types) { |
| 957 | if (type->der->type.der) { |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 958 | /* this is just a derived type with no additional type specified/required */ |
| 959 | break; |
| 960 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 961 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type"); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 962 | goto error; |
| 963 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 964 | for (i = 0; i < type->info.uni.count; i++) { |
PavolVican | 811f03b | 2016-12-29 23:03:20 +0100 | [diff] [blame] | 965 | dertype = &type->info.uni.types[i]; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 966 | if (dertype->base == LY_TYPE_DER || dertype->base == LY_TYPE_ERR) { |
| 967 | yang = (struct yang_type *)dertype->der; |
| 968 | dertype->der = NULL; |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 969 | dertype->parent = type->parent; |
| 970 | if (yang_check_type(module, parent, yang, dertype, tpdftype, unres)) { |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 971 | dertype->der = (struct lys_tpdf *)yang; |
| 972 | ret = EXIT_FAILURE; |
PavolVican | 811f03b | 2016-12-29 23:03:20 +0100 | [diff] [blame] | 973 | type->base = base_tmp; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 974 | base = 0; |
| 975 | goto error; |
| 976 | } else { |
| 977 | lydict_remove(module->ctx, yang->name); |
| 978 | free(yang); |
| 979 | } |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 980 | } |
Pavol Vican | 8e00e7a | 2016-09-20 11:07:48 +0200 | [diff] [blame] | 981 | if (module->version < 2) { |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 982 | if (dertype->base == LY_TYPE_EMPTY) { |
Pavol Vican | 8e00e7a | 2016-09-20 11:07:48 +0200 | [diff] [blame] | 983 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", typ->name); |
| 984 | goto error; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 985 | } else if (dertype->base == LY_TYPE_LEAFREF) { |
Pavol Vican | 8e00e7a | 2016-09-20 11:07:48 +0200 | [diff] [blame] | 986 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", typ->name); |
| 987 | goto error; |
| 988 | } |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 989 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 990 | if ((dertype->base == LY_TYPE_INST) || (dertype->base == LY_TYPE_LEAFREF) |
| 991 | || ((dertype->base == LY_TYPE_UNION) && dertype->info.uni.has_ptr_type)) { |
Radek Krejci | a726f9b | 2017-02-28 09:56:44 +0100 | [diff] [blame] | 992 | type->info.uni.has_ptr_type = 1; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 993 | } |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 994 | } |
| 995 | break; |
Pavol Vican | a182796 | 2016-02-29 15:39:42 +0100 | [diff] [blame] | 996 | |
| 997 | default: |
| 998 | if (base >= LY_TYPE_BINARY && base <= LY_TYPE_UINT64) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 999 | if (type->base != base) { |
| 1000 | 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] | 1001 | goto error; |
| 1002 | } |
| 1003 | } else { |
| 1004 | LOGINT; |
| 1005 | goto error; |
| 1006 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1007 | } |
| 1008 | return EXIT_SUCCESS; |
| 1009 | |
| 1010 | error: |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 1011 | if (type->module_name) { |
| 1012 | lydict_remove(module->ctx, type->module_name); |
| 1013 | type->module_name = NULL; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1014 | } |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 1015 | if (base) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 1016 | type->base = base_tmp; |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 1017 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1018 | return ret; |
| 1019 | } |
| 1020 | |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 1021 | void |
| 1022 | yang_free_type_union(struct ly_ctx *ctx, struct lys_type *type) |
| 1023 | { |
| 1024 | struct lys_type *stype; |
| 1025 | struct yang_type *yang; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 1026 | unsigned int i; |
Pavol Vican | cf2af4d | 2016-12-21 14:13:06 +0100 | [diff] [blame] | 1027 | |
| 1028 | for (i = 0; i < type->info.uni.count; ++i) { |
| 1029 | stype = &type->info.uni.types[i]; |
| 1030 | if (stype->base == LY_TYPE_DER || stype->base == LY_TYPE_ERR) { |
| 1031 | yang = (struct yang_type *)stype->der; |
| 1032 | stype->base = yang->base; |
| 1033 | lydict_remove(ctx, yang->name); |
| 1034 | free(yang); |
| 1035 | } else if (stype->base == LY_TYPE_UNION) { |
| 1036 | yang_free_type_union(ctx, stype); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1041 | void * |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1042 | 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] | 1043 | { |
| 1044 | struct yang_type *typ; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1045 | struct lys_deviate *dev; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1046 | |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 1047 | typ = calloc(1, sizeof *typ); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1048 | LY_CHECK_ERR_RETURN(!typ, LOGMEM, NULL); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1049 | |
| 1050 | typ->flags = LY_YANG_STRUCTURE_FLAG; |
| 1051 | switch (type) { |
| 1052 | case LEAF_KEYWORD: |
Pavol Vican | 85427f0 | 2016-09-26 15:21:05 +0200 | [diff] [blame] | 1053 | if (((struct lys_node_leaf *)parent)->type.der) { |
| 1054 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, parent, "type", "leaf"); |
| 1055 | goto error; |
| 1056 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1057 | ((struct lys_node_leaf *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1058 | ((struct lys_node_leaf *)parent)->type.parent = (struct lys_tpdf *)parent; |
| 1059 | typ->type = &((struct lys_node_leaf *)parent)->type; |
| 1060 | break; |
Pavol Vican | a55992a | 2016-03-01 13:37:17 +0100 | [diff] [blame] | 1061 | case LEAF_LIST_KEYWORD: |
Pavol Vican | 85427f0 | 2016-09-26 15:21:05 +0200 | [diff] [blame] | 1062 | if (((struct lys_node_leaflist *)parent)->type.der) { |
| 1063 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, parent, "type", "leaf-list"); |
| 1064 | goto error; |
| 1065 | } |
Pavol Vican | a55992a | 2016-03-01 13:37:17 +0100 | [diff] [blame] | 1066 | ((struct lys_node_leaflist *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1067 | ((struct lys_node_leaflist *)parent)->type.parent = (struct lys_tpdf *)parent; |
| 1068 | typ->type = &((struct lys_node_leaflist *)parent)->type; |
| 1069 | break; |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1070 | case UNION_KEYWORD: |
| 1071 | ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ; |
| 1072 | typ->type = (struct lys_type *)parent; |
| 1073 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1074 | case TYPEDEF_KEYWORD: |
Pavol Vican | 85427f0 | 2016-09-26 15:21:05 +0200 | [diff] [blame] | 1075 | if (((struct lys_tpdf *)parent)->type.der) { |
| 1076 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "typedef"); |
| 1077 | goto error; |
| 1078 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1079 | ((struct lys_tpdf *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1080 | typ->type = &((struct lys_tpdf *)parent)->type; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1081 | break; |
| 1082 | case REPLACE_KEYWORD: |
| 1083 | /* deviation replace type*/ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1084 | dev = (struct lys_deviate *)parent; |
| 1085 | if (dev->type) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1086 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "deviation"); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1087 | goto error; |
| 1088 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1089 | dev->type = calloc(1, sizeof *dev->type); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1090 | LY_CHECK_ERR_GOTO(!dev->type, LOGMEM, error); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1091 | dev->type->der = (struct lys_tpdf *)typ; |
| 1092 | typ->type = dev->type; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1093 | break; |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 1094 | case EXTENSION_INSTANCE: |
| 1095 | ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ; |
| 1096 | typ->type = parent; |
| 1097 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1098 | default: |
| 1099 | goto error; |
| 1100 | break; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1101 | } |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1102 | typ->name = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1103 | return typ; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1104 | |
| 1105 | error: |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1106 | free(value); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1107 | free(typ); |
| 1108 | return NULL; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | void * |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1112 | yang_read_length(struct lys_module *module, struct yang_type *stype, char *value, int is_ext_instance) |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1113 | { |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1114 | struct lys_restr *length; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1115 | |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1116 | if (is_ext_instance) { |
| 1117 | length = (struct lys_restr *)stype; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1118 | } else { |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1119 | if (stype->base == 0 || stype->base == LY_TYPE_STRING) { |
| 1120 | stype->base = LY_TYPE_STRING; |
| 1121 | } else { |
| 1122 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected length statement."); |
| 1123 | goto error; |
| 1124 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1125 | |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1126 | if (stype->type->info.str.length) { |
| 1127 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "length", "type"); |
| 1128 | goto error; |
| 1129 | } |
| 1130 | length = calloc(1, sizeof *length); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1131 | LY_CHECK_ERR_GOTO(!length, LOGMEM, error); |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1132 | stype->type->info.str.length = length; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1133 | } |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1134 | length->expr = lydict_insert_zc(module->ctx, value); |
| 1135 | return length; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1136 | |
| 1137 | error: |
| 1138 | free(value); |
| 1139 | return NULL; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1140 | } |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1141 | |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1142 | int |
| 1143 | 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] | 1144 | { |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1145 | char *buf; |
| 1146 | size_t len; |
| 1147 | |
Michal Vasko | 0aee5c1 | 2016-06-17 14:27:26 +0200 | [diff] [blame] | 1148 | if (lyp_check_pattern(value, NULL)) { |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1149 | free(value); |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1150 | return EXIT_FAILURE; |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1151 | } |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1152 | |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1153 | len = strlen(value); |
| 1154 | buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */ |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1155 | LY_CHECK_ERR_RETURN(!buf, LOGMEM; free(value), EXIT_FAILURE); |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1156 | |
| 1157 | buf[0] = modifier; |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1158 | strcpy(&buf[1], value); |
| 1159 | free(value); |
| 1160 | |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1161 | pattern->expr = lydict_insert_zc(module->ctx, buf); |
| 1162 | return EXIT_SUCCESS; |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1163 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1164 | |
| 1165 | void * |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1166 | yang_read_range(struct lys_module *module, struct yang_type *stype, char *value, int is_ext_instance) |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1167 | { |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1168 | struct lys_restr * range; |
| 1169 | |
| 1170 | if (is_ext_instance) { |
| 1171 | range = (struct lys_restr *)stype; |
| 1172 | } else { |
| 1173 | if (stype->base != 0 && stype->base != LY_TYPE_DEC64) { |
| 1174 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected range statement."); |
| 1175 | goto error; |
| 1176 | } |
| 1177 | stype->base = LY_TYPE_DEC64; |
| 1178 | if (stype->type->info.dec64.range) { |
| 1179 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "range", "type"); |
| 1180 | goto error; |
| 1181 | } |
| 1182 | range = calloc(1, sizeof *range); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1183 | LY_CHECK_ERR_GOTO(!range, LOGMEM, error); |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1184 | stype->type->info.dec64.range = range; |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1185 | } |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 1186 | range->expr = lydict_insert_zc(module->ctx, value); |
| 1187 | return range; |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1188 | |
| 1189 | error: |
| 1190 | free(value); |
| 1191 | return NULL; |
| 1192 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1193 | |
| 1194 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1195 | yang_read_fraction(struct yang_type *typ, uint32_t value) |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1196 | { |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1197 | if (typ->base == 0 || typ->base == LY_TYPE_DEC64) { |
| 1198 | typ->base = LY_TYPE_DEC64; |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1199 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1200 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected fraction-digits statement."); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1201 | goto error; |
| 1202 | } |
| 1203 | if (typ->type->info.dec64.dig) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1204 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "fraction-digits", "type"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1205 | goto error; |
| 1206 | } |
| 1207 | /* range check */ |
| 1208 | if (value < 1 || value > 18) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1209 | 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] | 1210 | goto error; |
| 1211 | } |
| 1212 | typ->type->info.dec64.dig = value; |
| 1213 | return EXIT_SUCCESS; |
| 1214 | |
| 1215 | error: |
| 1216 | return EXIT_FAILURE; |
| 1217 | } |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1218 | |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1219 | int |
| 1220 | 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] | 1221 | { |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1222 | int i, j; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1223 | |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 1224 | typ->base = LY_TYPE_ENUM; |
Pavol Vican | c666241 | 2016-08-30 08:06:28 +0200 | [diff] [blame] | 1225 | if (!value[0]) { |
| 1226 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name"); |
| 1227 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty."); |
| 1228 | free(value); |
| 1229 | goto error; |
| 1230 | } |
| 1231 | |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1232 | enm->name = lydict_insert_zc(module->ctx, value); |
| 1233 | |
| 1234 | /* the assigned name MUST NOT have any leading or trailing whitespace characters */ |
| 1235 | if (isspace(enm->name[0]) || isspace(enm->name[strlen(enm->name) - 1])) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1236 | LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1237 | goto error; |
| 1238 | } |
| 1239 | |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1240 | j = typ->type->info.enums.count - 1; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1241 | /* check the name uniqueness */ |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1242 | for (i = 0; i < j; i++) { |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 1243 | if (ly_strequal(typ->type->info.enums.enm[i].name, enm->name, 1)) { |
| 1244 | LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, enm->name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1245 | goto error; |
| 1246 | } |
| 1247 | } |
| 1248 | |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1249 | return EXIT_SUCCESS; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1250 | |
| 1251 | error: |
Pavol Vican | 874715f | 2016-10-25 14:52:08 +0200 | [diff] [blame] | 1252 | return EXIT_FAILURE; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1256 | 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] | 1257 | { |
| 1258 | int i, j; |
| 1259 | |
| 1260 | if (!assign) { |
| 1261 | /* assign value automatically */ |
| 1262 | if (*value > INT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1263 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value"); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1264 | goto error; |
| 1265 | } |
| 1266 | enm->value = *value; |
| 1267 | enm->flags |= LYS_AUTOASSIGNED; |
| 1268 | (*value)++; |
Pavol Vican | 5de389c | 2016-08-30 08:55:15 +0200 | [diff] [blame] | 1269 | } else if (typ->type->info.enums.enm == enm) { |
| 1270 | /* change value, which is assigned automatically, if first enum has value. */ |
| 1271 | *value = typ->type->info.enums.enm[0].value; |
| 1272 | (*value)++; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* check that the value is unique */ |
| 1276 | j = typ->type->info.enums.count-1; |
| 1277 | for (i = 0; i < j; i++) { |
| 1278 | 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] | 1279 | LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL, |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1280 | typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name, |
| 1281 | typ->type->info.enums.enm[i].name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1282 | goto error; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return EXIT_SUCCESS; |
| 1287 | |
| 1288 | error: |
| 1289 | return EXIT_FAILURE; |
| 1290 | } |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1291 | |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1292 | int |
| 1293 | 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] | 1294 | { |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1295 | int i, j; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1296 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 1297 | typ->base = LY_TYPE_BITS; |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1298 | bit->name = lydict_insert_zc(module->ctx, value); |
| 1299 | if (lyp_check_identifier(bit->name, LY_IDENT_SIMPLE, NULL, NULL)) { |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1300 | goto error; |
| 1301 | } |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1302 | |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1303 | j = typ->type->info.bits.count - 1; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1304 | /* check the name uniqueness */ |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1305 | for (i = 0; i < j; i++) { |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 1306 | if (ly_strequal(typ->type->info.bits.bit[i].name, bit->name, 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1307 | LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1308 | goto error; |
| 1309 | } |
| 1310 | } |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1311 | return EXIT_SUCCESS; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1312 | |
| 1313 | error: |
Pavol Vican | 59e8dee | 2016-10-25 15:29:38 +0200 | [diff] [blame] | 1314 | return EXIT_FAILURE; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1318 | 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] | 1319 | { |
| 1320 | int i,j; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1321 | |
| 1322 | if (!assign) { |
| 1323 | /* assign value automatically */ |
| 1324 | if (*value > UINT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1325 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position"); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1326 | goto error; |
| 1327 | } |
| 1328 | bit->pos = (uint32_t)*value; |
| 1329 | bit->flags |= LYS_AUTOASSIGNED; |
| 1330 | (*value)++; |
| 1331 | } |
| 1332 | |
| 1333 | j = typ->type->info.bits.count - 1; |
| 1334 | /* check that the value is unique */ |
| 1335 | for (i = 0; i < j; i++) { |
| 1336 | if (typ->type->info.bits.bit[i].pos == bit->pos) { |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1337 | 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] | 1338 | goto error; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1342 | return EXIT_SUCCESS; |
| 1343 | |
| 1344 | error: |
| 1345 | return EXIT_FAILURE; |
| 1346 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1347 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 1348 | int |
| 1349 | yang_read_augment(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *aug, char *value) |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1350 | { |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1351 | aug->nodetype = LYS_AUGMENT; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1352 | aug->target_name = transform_schema2json(module, value); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1353 | free(value); |
| 1354 | if (!aug->target_name) { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 1355 | return EXIT_FAILURE; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1356 | } |
| 1357 | aug->parent = parent; |
| 1358 | aug->module = module; |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 1359 | return EXIT_SUCCESS; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1360 | } |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1361 | |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1362 | void * |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1363 | yang_read_deviate_unsupported(struct lys_deviation *dev) |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1364 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1365 | if (dev->deviate_size) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1366 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation."); |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1367 | return NULL; |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1368 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1369 | dev->deviate = calloc(1, sizeof *dev->deviate); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1370 | LY_CHECK_ERR_RETURN(!dev->deviate, LOGMEM, NULL); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1371 | dev->deviate[dev->deviate_size].mod = LY_DEVIATE_NO; |
| 1372 | dev->deviate_size = 1; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1373 | return dev->deviate; |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | void * |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1377 | yang_read_deviate(struct lys_deviation *dev, LYS_DEVIATE_TYPE mod) |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1378 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1379 | struct lys_deviate *deviate; |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1380 | |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 1381 | if (dev->deviate_size && dev->deviate[0].mod == LY_DEVIATE_NO) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1382 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported"); |
| 1383 | 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] | 1384 | return NULL; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1385 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1386 | if (!(dev->deviate_size % LY_YANG_ARRAY_SIZE)) { |
| 1387 | deviate = realloc(dev->deviate, (LY_YANG_ARRAY_SIZE + dev->deviate_size) * sizeof *deviate); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1388 | LY_CHECK_ERR_RETURN(!deviate, LOGMEM, NULL); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1389 | memset(deviate + dev->deviate_size, 0, LY_YANG_ARRAY_SIZE * sizeof *deviate); |
| 1390 | dev->deviate = deviate; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1391 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1392 | dev->deviate[dev->deviate_size].mod = mod; |
| 1393 | return &dev->deviate[dev->deviate_size++]; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1397 | 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] | 1398 | { |
| 1399 | const char **stritem; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1400 | int j; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1401 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1402 | /* check target node type */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1403 | if (dev_target->nodetype == LYS_LEAFLIST) { |
| 1404 | stritem = &((struct lys_node_leaflist *)dev_target)->units; |
| 1405 | } else if (dev_target->nodetype == LYS_LEAF) { |
| 1406 | stritem = &((struct lys_node_leaf *)dev_target)->units; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1407 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1408 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1409 | 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] | 1410 | goto error; |
| 1411 | } |
| 1412 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1413 | if (deviate->mod == LY_DEVIATE_DEL) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1414 | /* check values */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1415 | if (!ly_strequal(*stritem, deviate->units, 1)) { |
| 1416 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, deviate->units, "units"); |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1417 | 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] | 1418 | goto error; |
| 1419 | } |
| 1420 | /* remove current units value of the target */ |
| 1421 | lydict_remove(ctx, *stritem); |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1422 | *stritem = NULL; |
| 1423 | /* remove its extensions */ |
| 1424 | j = -1; |
| 1425 | while ((j = lys_ext_iter(dev_target->ext, dev_target->ext_size, j + 1, LYEXT_SUBSTMT_UNITS)) != -1) { |
| 1426 | lyp_ext_instance_rm(ctx, &dev_target->ext, &dev_target->ext_size, j); |
| 1427 | --j; |
| 1428 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1429 | } else { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1430 | if (deviate->mod == LY_DEVIATE_ADD) { |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1431 | /* check that there is no current value */ |
| 1432 | if (*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1433 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1434 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1435 | goto error; |
| 1436 | } |
| 1437 | } else { /* replace */ |
| 1438 | if (!*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1439 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1440 | 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] | 1441 | goto error; |
| 1442 | } |
| 1443 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1444 | /* remove current units value of the target ... */ |
| 1445 | lydict_remove(ctx, *stritem); |
| 1446 | |
| 1447 | /* ... and replace it with the value specified in deviation */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1448 | *stritem = lydict_insert(ctx, deviate->units, 0); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1451 | return EXIT_SUCCESS; |
| 1452 | |
| 1453 | error: |
| 1454 | return EXIT_FAILURE; |
| 1455 | } |
| 1456 | |
| 1457 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1458 | 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] | 1459 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1460 | struct lys_node_list *list; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1461 | struct lys_unique *unique; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1462 | |
| 1463 | /* check target node type */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1464 | if (dev_target->nodetype != LYS_LIST) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1465 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique"); |
| 1466 | 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] | 1467 | goto error; |
| 1468 | } |
| 1469 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1470 | list = (struct lys_node_list *)dev_target; |
| 1471 | if (deviate->mod == LY_DEVIATE_ADD) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1472 | /* reallocate the unique array of the target */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1473 | unique = ly_realloc(list->unique, (deviate->unique_size + list->unique_size) * sizeof *unique); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1474 | LY_CHECK_ERR_GOTO(!unique, LOGMEM, error); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1475 | list->unique = unique; |
| 1476 | memset(unique + list->unique_size, 0, deviate->unique_size * sizeof *unique); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | return EXIT_SUCCESS; |
| 1480 | |
| 1481 | error: |
| 1482 | return EXIT_FAILURE; |
| 1483 | } |
| 1484 | |
| 1485 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1486 | yang_fill_deviate_default(struct ly_ctx *ctx, struct lys_deviate *deviate, struct lys_node *dev_target, |
| 1487 | struct ly_set *dflt_check, const char *value) |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1488 | { |
| 1489 | struct lys_node *node; |
| 1490 | struct lys_node_choice *choice; |
| 1491 | struct lys_node_leaf *leaf; |
| 1492 | struct lys_node_leaflist *llist; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1493 | int rc, i, j; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1494 | unsigned int u; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1495 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1496 | u = strlen(value); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1497 | if (dev_target->nodetype == LYS_CHOICE) { |
| 1498 | choice = (struct lys_node_choice *)dev_target; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1499 | rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node); |
| 1500 | if (rc || !node) { |
| 1501 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1502 | goto error; |
| 1503 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1504 | if (deviate->mod == LY_DEVIATE_DEL) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1505 | if (!choice->dflt || (choice->dflt != node)) { |
| 1506 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1507 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
| 1508 | goto error; |
| 1509 | } |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1510 | choice->dflt = NULL; |
| 1511 | /* remove extensions of this default instance from the target node */ |
| 1512 | j = -1; |
| 1513 | while ((j = lys_ext_iter(dev_target->ext, dev_target->ext_size, j + 1, LYEXT_SUBSTMT_DEFAULT)) != -1) { |
| 1514 | lyp_ext_instance_rm(ctx, &dev_target->ext, &dev_target->ext_size, j); |
| 1515 | --j; |
| 1516 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1517 | } else { /* add or replace */ |
| 1518 | choice->dflt = node; |
| 1519 | if (!choice->dflt) { |
| 1520 | /* default branch not found */ |
| 1521 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1522 | goto error; |
| 1523 | } |
| 1524 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1525 | } else if (dev_target->nodetype == LYS_LEAF) { |
| 1526 | leaf = (struct lys_node_leaf *)dev_target; |
| 1527 | if (deviate->mod == LY_DEVIATE_DEL) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1528 | if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) { |
| 1529 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1530 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
| 1531 | goto error; |
| 1532 | } |
| 1533 | /* remove value */ |
| 1534 | lydict_remove(ctx, leaf->dflt); |
| 1535 | leaf->dflt = NULL; |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1536 | leaf->flags &= ~LYS_DFLTJSON; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1537 | /* remove extensions of this default instance from the target node */ |
| 1538 | j = -1; |
| 1539 | while ((j = lys_ext_iter(dev_target->ext, dev_target->ext_size, j + 1, LYEXT_SUBSTMT_DEFAULT)) != -1) { |
| 1540 | lyp_ext_instance_rm(ctx, &dev_target->ext, &dev_target->ext_size, j); |
| 1541 | --j; |
| 1542 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1543 | } else { /* add (already checked) and replace */ |
| 1544 | /* remove value */ |
| 1545 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1546 | leaf->flags &= ~LYS_DFLTJSON; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1547 | |
| 1548 | /* set new value */ |
| 1549 | leaf->dflt = lydict_insert(ctx, value, u); |
| 1550 | |
| 1551 | /* 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] | 1552 | ly_set_add(dflt_check, dev_target, 0); |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1553 | } |
| 1554 | } else { /* LYS_LEAFLIST */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1555 | llist = (struct lys_node_leaflist *)dev_target; |
| 1556 | if (deviate->mod == LY_DEVIATE_DEL) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1557 | /* find and remove the value in target list */ |
| 1558 | for (i = 0; i < llist->dflt_size; i++) { |
| 1559 | if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) { |
| 1560 | /* match, remove the value */ |
| 1561 | lydict_remove(llist->module->ctx, llist->dflt[i]); |
| 1562 | llist->dflt[i] = NULL; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1563 | /* remove extensions of this default instance from the target node */ |
| 1564 | j = -1; |
| 1565 | while ((j = lys_ext_iter(dev_target->ext, dev_target->ext_size, j + 1, LYEXT_SUBSTMT_DEFAULT)) != -1) { |
| 1566 | if (dev_target->ext[j]->insubstmt_index == i) { |
| 1567 | lyp_ext_instance_rm(ctx, &dev_target->ext, &dev_target->ext_size, j); |
| 1568 | --j; |
| 1569 | } else if (dev_target->ext[j]->insubstmt_index > i) { |
| 1570 | /* decrease the substatement index of the extension because of the changed array of defaults */ |
| 1571 | dev_target->ext[j]->insubstmt_index--; |
| 1572 | } |
| 1573 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1574 | break; |
| 1575 | } |
| 1576 | } |
| 1577 | if (i == llist->dflt_size) { |
| 1578 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1579 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node."); |
| 1580 | goto error; |
| 1581 | } |
| 1582 | } else { |
| 1583 | /* add or replace, anyway we place items into the deviate's list |
| 1584 | which propagates to the target */ |
| 1585 | /* we just want to check that the value isn't already in the list */ |
| 1586 | for (i = 0; i < llist->dflt_size; i++) { |
| 1587 | if (ly_strequal(llist->dflt[i], value, 1)) { |
| 1588 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1589 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value); |
| 1590 | goto error; |
| 1591 | } |
| 1592 | } |
| 1593 | /* store it in target node */ |
| 1594 | llist->dflt[llist->dflt_size++] = lydict_insert(ctx, value, u); |
| 1595 | |
| 1596 | /* 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] | 1597 | ly_set_add(dflt_check, dev_target, 0); |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1598 | llist->flags &= ~LYS_DFLTJSON; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | return EXIT_SUCCESS; |
| 1603 | error: |
| 1604 | return EXIT_FAILURE; |
| 1605 | } |
| 1606 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1607 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1608 | yang_read_deviate_default(struct lys_module *module, struct lys_deviate *deviate, |
| 1609 | struct lys_node *dev_target, struct ly_set * dflt_check) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1610 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1611 | int i; |
| 1612 | struct lys_node_leaflist *llist; |
| 1613 | const char **dflt; |
| 1614 | |
| 1615 | /* check target node type */ |
| 1616 | if (module->version < 2 && dev_target->nodetype == LYS_LEAFLIST) { |
| 1617 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1618 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property."); |
| 1619 | goto error; |
| 1620 | } else if (deviate->dflt_size > 1 && dev_target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */ |
| 1621 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1622 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties."); |
| 1623 | goto error; |
| 1624 | } else if (!(dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 1625 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1626 | 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] | 1627 | goto error; |
| 1628 | } |
| 1629 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1630 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 1631 | /* check that there is no current value */ |
| 1632 | if ((dev_target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev_target)->dflt) || |
| 1633 | (dev_target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev_target)->dflt)) { |
| 1634 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1635 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
| 1636 | goto error; |
| 1637 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1638 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1639 | /* check collision with mandatory/min-elements */ |
| 1640 | if ((dev_target->flags & LYS_MAND_TRUE) || |
| 1641 | (dev_target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev_target)->min)) { |
| 1642 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "deviation"); |
| 1643 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 1644 | "Adding the \"default\" statement is forbidden on %s statement.", |
| 1645 | (dev_target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\""); |
| 1646 | goto error; |
| 1647 | } |
| 1648 | } else if (deviate->mod == LY_DEVIATE_RPL) { |
| 1649 | /* check that there was a value before */ |
| 1650 | if (((dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((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, "Replacing a property that does not exist."); |
| 1654 | goto error; |
| 1655 | } |
| 1656 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1657 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1658 | if (dev_target->nodetype == LYS_LEAFLIST) { |
| 1659 | /* reallocate default list in the target */ |
| 1660 | llist = (struct lys_node_leaflist *)dev_target; |
| 1661 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 1662 | /* reallocate (enlarge) the unique array of the target */ |
| 1663 | dflt = realloc(llist->dflt, (deviate->dflt_size + llist->dflt_size) * sizeof *dflt); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1664 | LY_CHECK_ERR_GOTO(!dflt, LOGMEM, error); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1665 | llist->dflt = dflt; |
| 1666 | } else if (deviate->mod == LY_DEVIATE_RPL) { |
| 1667 | /* reallocate (replace) the unique array of the target */ |
| 1668 | for (i = 0; i < llist->dflt_size; i++) { |
| 1669 | lydict_remove(llist->module->ctx, llist->dflt[i]); |
| 1670 | } |
| 1671 | dflt = realloc(llist->dflt, deviate->dflt_size * sizeof *dflt); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1672 | LY_CHECK_ERR_GOTO(!dflt, LOGMEM, error); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1673 | llist->dflt = dflt; |
| 1674 | llist->dflt_size = 0; |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | for (i = 0; i < deviate->dflt_size; ++i) { |
| 1679 | if (yang_fill_deviate_default(module->ctx, deviate, dev_target, dflt_check, deviate->dflt[i])) { |
| 1680 | goto error; |
| 1681 | } |
| 1682 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1683 | |
| 1684 | return EXIT_SUCCESS; |
| 1685 | |
| 1686 | error: |
| 1687 | return EXIT_FAILURE; |
| 1688 | } |
| 1689 | |
| 1690 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1691 | 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] | 1692 | { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 1693 | struct lys_node *parent; |
| 1694 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1695 | /* check target node type */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1696 | if (!(dev_target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1697 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1698 | 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] | 1699 | goto error; |
| 1700 | } |
| 1701 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1702 | if (deviate->mod == LY_DEVIATE_ADD) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1703 | /* check that there is no current value */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1704 | if (dev_target->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1705 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1706 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1707 | goto error; |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 1708 | } else { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1709 | 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] | 1710 | /* RFC 6020, 7.6.4 - default statement must not with mandatory true */ |
| 1711 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf"); |
| 1712 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 1713 | goto error; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1714 | } 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] | 1715 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice"); |
| 1716 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 1717 | goto error; |
| 1718 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1719 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1720 | } else { /* replace */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1721 | if (!(dev_target->flags & LYS_MAND_MASK)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1722 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1723 | 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] | 1724 | goto error; |
| 1725 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1726 | } |
| 1727 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1728 | /* remove current mandatory value of the target ... */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1729 | dev_target->flags &= ~LYS_MAND_MASK; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1730 | |
| 1731 | /* ... and replace it with the value specified in deviation */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1732 | dev_target->flags |= deviate->flags & LYS_MAND_MASK; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1733 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 1734 | /* 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] | 1735 | for (parent = dev_target->parent; |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 1736 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION)); |
| 1737 | parent = parent->parent) { |
| 1738 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 1739 | /* stop also on presence containers */ |
| 1740 | break; |
| 1741 | } |
| 1742 | } |
| 1743 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 1744 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 1745 | if (lyp_check_mandatory_choice(parent)) { |
| 1746 | goto error; |
| 1747 | } |
| 1748 | } |
| 1749 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1750 | return EXIT_SUCCESS; |
| 1751 | |
| 1752 | error: |
| 1753 | return EXIT_FAILURE; |
| 1754 | } |
| 1755 | |
| 1756 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1757 | 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] | 1758 | { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 1759 | uint32_t *ui32val, *min, *max; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1760 | |
| 1761 | /* check target node type */ |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1762 | if (dev_target->nodetype == LYS_LEAFLIST) { |
| 1763 | max = &((struct lys_node_leaflist *)dev_target)->max; |
| 1764 | min = &((struct lys_node_leaflist *)dev_target)->min; |
| 1765 | } else if (dev_target->nodetype == LYS_LIST) { |
| 1766 | max = &((struct lys_node_list *)dev_target)->max; |
| 1767 | min = &((struct lys_node_list *)dev_target)->min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1768 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1769 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 1770 | 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] | 1771 | goto error; |
| 1772 | } |
| 1773 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1774 | ui32val = (type) ? max : min; |
| 1775 | if (deviate->mod == LY_DEVIATE_ADD) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1776 | /* check that there is no current value */ |
| 1777 | if (*ui32val) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1778 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 1779 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1780 | goto error; |
| 1781 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1782 | } else if (deviate->mod == LY_DEVIATE_RPL) { |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1783 | /* unfortunately, there is no way to check reliably that there |
| 1784 | * was a value before, it could have been the default */ |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /* add (already checked) and replace */ |
| 1788 | /* set new value specified in deviation */ |
| 1789 | *ui32val = value; |
| 1790 | |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 1791 | /* check min-elements is smaller than max-elements */ |
| 1792 | if (*max && *min > *max) { |
| 1793 | if (type) { |
| 1794 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"max-elements\".", value); |
| 1795 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\"."); |
| 1796 | } else { |
| 1797 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"min-elements\".", value); |
| 1798 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 1799 | } |
| 1800 | goto error; |
| 1801 | } |
| 1802 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1803 | return EXIT_SUCCESS; |
| 1804 | |
| 1805 | error: |
| 1806 | return EXIT_FAILURE; |
| 1807 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1808 | |
| 1809 | int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1810 | yang_check_deviate_must(struct lys_module *module, struct unres_schema *unres, |
| 1811 | struct lys_deviate *deviate, struct lys_node *dev_target) |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1812 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1813 | int i, j, erase_must = 1; |
| 1814 | struct lys_restr **trg_must, *must; |
PavolVican | 214408f | 2017-02-03 11:54:05 +0100 | [diff] [blame] | 1815 | uint8_t *trg_must_size; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1816 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1817 | /* check target node type */ |
| 1818 | switch (dev_target->nodetype) { |
| 1819 | case LYS_LEAF: |
| 1820 | trg_must = &((struct lys_node_leaf *)dev_target)->must; |
| 1821 | trg_must_size = &((struct lys_node_leaf *)dev_target)->must_size; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1822 | break; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1823 | case LYS_CONTAINER: |
| 1824 | trg_must = &((struct lys_node_container *)dev_target)->must; |
| 1825 | trg_must_size = &((struct lys_node_container *)dev_target)->must_size; |
| 1826 | break; |
| 1827 | case LYS_LEAFLIST: |
| 1828 | trg_must = &((struct lys_node_leaflist *)dev_target)->must; |
| 1829 | trg_must_size = &((struct lys_node_leaflist *)dev_target)->must_size; |
| 1830 | break; |
| 1831 | case LYS_LIST: |
| 1832 | trg_must = &((struct lys_node_list *)dev_target)->must; |
| 1833 | trg_must_size = &((struct lys_node_list *)dev_target)->must_size; |
| 1834 | break; |
| 1835 | case LYS_ANYXML: |
| 1836 | case LYS_ANYDATA: |
| 1837 | trg_must = &((struct lys_node_anydata *)dev_target)->must; |
| 1838 | trg_must_size = &((struct lys_node_anydata *)dev_target)->must_size; |
| 1839 | break; |
| 1840 | default: |
| 1841 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must"); |
| 1842 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property."); |
| 1843 | goto error; |
| 1844 | } |
| 1845 | |
| 1846 | /* flag will be checked again, clear it for now */ |
PavolVican | cec1878 | 2017-01-26 21:48:46 +0100 | [diff] [blame] | 1847 | dev_target->flags &= ~LYS_XPATH_DEP; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1848 | |
| 1849 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 1850 | /* reallocate the must array of the target */ |
| 1851 | must = ly_realloc(*trg_must, (deviate->must_size + *trg_must_size) * sizeof *must); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1852 | LY_CHECK_ERR_GOTO(!must, LOGMEM, error); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1853 | *trg_must = must; |
PavolVican | 214408f | 2017-02-03 11:54:05 +0100 | [diff] [blame] | 1854 | memcpy(&(*trg_must)[*trg_must_size], deviate->must, deviate->must_size * sizeof *must); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1855 | free(deviate->must); |
| 1856 | deviate->must = &must[*trg_must_size]; |
PavolVican | 214408f | 2017-02-03 11:54:05 +0100 | [diff] [blame] | 1857 | *trg_must_size = *trg_must_size + deviate->must_size; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1858 | erase_must = 0; |
| 1859 | } else if (deviate->mod == LY_DEVIATE_DEL) { |
| 1860 | /* find must to delete, we are ok with just matching conditions */ |
| 1861 | for (j = 0; j < deviate->must_size; ++j) { |
| 1862 | for (i = 0; i < *trg_must_size; i++) { |
| 1863 | if (ly_strequal(deviate->must[j].expr, (*trg_must)[i].expr, 1)) { |
| 1864 | /* we have a match, free the must structure ... */ |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1865 | lys_restr_free(module->ctx, &((*trg_must)[i]), NULL); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1866 | /* ... and maintain the array */ |
| 1867 | (*trg_must_size)--; |
| 1868 | if (i != *trg_must_size) { |
PavolVican | 214408f | 2017-02-03 11:54:05 +0100 | [diff] [blame] | 1869 | memcpy(&(*trg_must)[i], &(*trg_must)[*trg_must_size], sizeof *must); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1870 | } |
| 1871 | if (!(*trg_must_size)) { |
| 1872 | free(*trg_must); |
| 1873 | *trg_must = NULL; |
| 1874 | } else { |
PavolVican | 214408f | 2017-02-03 11:54:05 +0100 | [diff] [blame] | 1875 | memset(&(*trg_must)[*trg_must_size], 0, sizeof *must); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | i = -1; /* set match flag */ |
| 1879 | break; |
| 1880 | } |
| 1881 | } |
| 1882 | if (i != -1) { |
| 1883 | /* no match found */ |
| 1884 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, deviate->must[j].expr, "must"); |
| 1885 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target."); |
| 1886 | goto error; |
| 1887 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1888 | } |
| 1889 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1890 | |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1891 | if (yang_check_must(module, deviate->must, deviate->must_size, unres)) { |
| 1892 | goto error; |
| 1893 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1894 | /* check XPath dependencies */ |
Michal Vasko | f96dfb6 | 2017-08-17 12:23:49 +0200 | [diff] [blame] | 1895 | if (*trg_must_size && (unres_schema_add_node(module, unres, dev_target, UNRES_XPATH, NULL) == -1)) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1896 | goto error; |
| 1897 | } |
| 1898 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1899 | return EXIT_SUCCESS; |
| 1900 | error: |
| 1901 | if (deviate->mod == LY_DEVIATE_ADD && erase_must) { |
| 1902 | for (i = 0; i < deviate->must_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 1903 | lys_restr_free(module->ctx, &deviate->must[i], NULL); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1904 | } |
| 1905 | free(deviate->must); |
| 1906 | } |
| 1907 | return EXIT_FAILURE; |
| 1908 | } |
| 1909 | |
| 1910 | int |
| 1911 | yang_deviate_delete_unique(struct lys_module *module, struct lys_deviate *deviate, |
| 1912 | struct lys_node_list *list, int index, char * value) |
| 1913 | { |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1914 | int i, j, k; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1915 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1916 | /* find unique structures to delete */ |
| 1917 | for (i = 0; i < list->unique_size; i++) { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1918 | if (list->unique[i].expr_size != deviate->unique[index].expr_size) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1919 | continue; |
| 1920 | } |
| 1921 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1922 | for (j = 0; j < deviate->unique[index].expr_size; j++) { |
| 1923 | 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] | 1924 | break; |
| 1925 | } |
| 1926 | } |
| 1927 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1928 | if (j == deviate->unique[index].expr_size) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1929 | /* we have a match, free the unique structure ... */ |
| 1930 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 1931 | lydict_remove(module->ctx, list->unique[i].expr[j]); |
| 1932 | } |
| 1933 | free(list->unique[i].expr); |
| 1934 | /* ... and maintain the array */ |
| 1935 | list->unique_size--; |
| 1936 | if (i != list->unique_size) { |
| 1937 | list->unique[i].expr_size = list->unique[list->unique_size].expr_size; |
| 1938 | list->unique[i].expr = list->unique[list->unique_size].expr; |
| 1939 | } |
| 1940 | |
| 1941 | if (!list->unique_size) { |
| 1942 | free(list->unique); |
| 1943 | list->unique = NULL; |
| 1944 | } else { |
| 1945 | list->unique[list->unique_size].expr_size = 0; |
| 1946 | list->unique[list->unique_size].expr = NULL; |
| 1947 | } |
| 1948 | |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1949 | k = i; /* remember index for removing extensions */ |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1950 | i = -1; /* set match flag */ |
| 1951 | break; |
| 1952 | } |
| 1953 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1954 | |
| 1955 | if (i != -1) { |
| 1956 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1957 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique"); |
| 1958 | 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] | 1959 | return EXIT_FAILURE; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 1962 | /* remove extensions of this unique instance from the target node */ |
| 1963 | j = -1; |
| 1964 | while ((j = lys_ext_iter(list->ext, list->ext_size, j + 1, LYEXT_SUBSTMT_UNIQUE)) != -1) { |
| 1965 | if (list->ext[j]->insubstmt_index == k) { |
| 1966 | lyp_ext_instance_rm(module->ctx, &list->ext, &list->ext_size, j); |
| 1967 | --j; |
| 1968 | } else if (list->ext[j]->insubstmt_index > k) { |
| 1969 | /* decrease the substatement index of the extension because of the changed array of uniques */ |
| 1970 | list->ext[j]->insubstmt_index--; |
| 1971 | } |
| 1972 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1973 | return EXIT_SUCCESS; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1974 | } |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 1975 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1976 | 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] | 1977 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1978 | struct lys_node_list *list; |
| 1979 | char *str; |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 1980 | uint i = 0; |
| 1981 | struct lys_unique *last_unique = NULL; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 1982 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 1983 | if (yang_read_deviate_unique(deviate, dev_target)) { |
| 1984 | goto error; |
| 1985 | } |
| 1986 | list = (struct lys_node_list *)dev_target; |
| 1987 | last_unique = &list->unique[list->unique_size]; |
| 1988 | for (i = 0; i < deviate->unique_size; ++i) { |
| 1989 | str = (char *) deviate->unique[i].expr; |
| 1990 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 1991 | if (yang_fill_unique(module, list, &list->unique[list->unique_size], str, NULL)) { |
| 1992 | free(str); |
| 1993 | goto error; |
| 1994 | } |
| 1995 | list->unique_size++; |
| 1996 | } else if (deviate->mod == LY_DEVIATE_DEL) { |
| 1997 | if (yang_fill_unique(module, list, &deviate->unique[i], str, NULL)) { |
| 1998 | free(str); |
| 1999 | goto error; |
| 2000 | } |
| 2001 | if (yang_deviate_delete_unique(module, deviate, list, i, str)) { |
| 2002 | free(str); |
| 2003 | goto error; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2004 | } |
| 2005 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2006 | free(str); |
| 2007 | } |
| 2008 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 2009 | free(deviate->unique); |
| 2010 | deviate->unique = last_unique; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2011 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2012 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2013 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2014 | return EXIT_SUCCESS; |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 2015 | error: |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 2016 | if (deviate->mod == LY_DEVIATE_ADD) { |
| 2017 | for (i = i + 1; i < deviate->unique_size; ++i) { |
| 2018 | free(deviate->unique[i].expr); |
| 2019 | } |
| 2020 | free(deviate->unique); |
| 2021 | deviate->unique = last_unique; |
| 2022 | |
| 2023 | } |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 2024 | return EXIT_FAILURE; |
| 2025 | } |
| 2026 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2027 | static int |
| 2028 | yang_fill_include(struct lys_module *trg, char *value, struct lys_include *inc, |
| 2029 | struct unres_schema *unres) |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 2030 | { |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2031 | const char *str; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2032 | int rc; |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2033 | int ret = 0; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2034 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2035 | str = lydict_insert_zc(trg->ctx, value); |
Radek Krejci | d4c1d0f | 2017-01-19 16:11:38 +0100 | [diff] [blame] | 2036 | rc = lyp_check_include(trg, str, inc, unres); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2037 | if (!rc) { |
| 2038 | /* success, copy the filled data into the final array */ |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2039 | memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc); |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 2040 | if (yang_check_ext_instance(trg, &trg->inc[trg->inc_size].ext, trg->inc[trg->inc_size].ext_size, |
| 2041 | &trg->inc[trg->inc_size], unres)) { |
| 2042 | ret = -1; |
| 2043 | } |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 2044 | trg->inc_size++; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2045 | } else if (rc == -1) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2046 | lys_extension_instances_free(trg->ctx, inc->ext, inc->ext_size, NULL); |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2047 | ret = -1; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2048 | } |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2049 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2050 | lydict_remove(trg->ctx, str); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2051 | return ret; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2052 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2053 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2054 | struct lys_ext_instance * |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2055 | yang_ext_instance(void *node, enum yytokentype type, int is_ext_instance) |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2056 | { |
| 2057 | struct lys_ext_instance ***ext, **tmp, *instance = NULL; |
| 2058 | LYEXT_PAR parent_type; |
| 2059 | uint8_t *size; |
| 2060 | |
| 2061 | switch (type) { |
| 2062 | case MODULE_KEYWORD: |
PavolVican | e6fa67b | 2017-02-01 11:06:57 +0100 | [diff] [blame] | 2063 | case SUBMODULE_KEYWORD: |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2064 | ext = &((struct lys_module *)node)->ext; |
| 2065 | size = &((struct lys_module *)node)->ext_size; |
| 2066 | parent_type = LYEXT_PAR_MODULE; |
| 2067 | break; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2068 | case BELONGS_TO_KEYWORD: |
| 2069 | if (is_ext_instance) { |
| 2070 | ext = &((struct lys_ext_instance *)node)->ext; |
| 2071 | size = &((struct lys_ext_instance *)node)->ext_size; |
| 2072 | parent_type = LYEXT_PAR_EXTINST; |
| 2073 | } else { |
| 2074 | ext = &((struct lys_module *)node)->ext; |
| 2075 | size = &((struct lys_module *)node)->ext_size; |
| 2076 | parent_type = LYEXT_PAR_MODULE; |
| 2077 | } |
| 2078 | break; |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 2079 | case IMPORT_KEYWORD: |
| 2080 | ext = &((struct lys_import *)node)->ext; |
| 2081 | size = &((struct lys_import *)node)->ext_size; |
| 2082 | parent_type = LYEXT_PAR_IMPORT; |
| 2083 | break; |
| 2084 | case INCLUDE_KEYWORD: |
| 2085 | ext = &((struct lys_include *)node)->ext; |
| 2086 | size = &((struct lys_include *)node)->ext_size; |
| 2087 | parent_type = LYEXT_PAR_INCLUDE; |
| 2088 | break; |
PavolVican | 171717d | 2017-02-01 14:49:55 +0100 | [diff] [blame] | 2089 | case REVISION_KEYWORD: |
| 2090 | ext = &((struct lys_revision *)node)->ext; |
| 2091 | size = &((struct lys_revision *)node)->ext_size; |
| 2092 | parent_type = LYEXT_PAR_REVISION; |
| 2093 | break; |
PavolVican | 70ce745 | 2017-02-01 15:39:39 +0100 | [diff] [blame] | 2094 | case GROUPING_KEYWORD: |
PavolVican | 59af9be | 2017-02-01 16:04:37 +0100 | [diff] [blame] | 2095 | case CONTAINER_KEYWORD: |
PavolVican | a6c3ac9 | 2017-02-03 13:15:13 +0100 | [diff] [blame] | 2096 | case LEAF_KEYWORD: |
| 2097 | case LEAF_LIST_KEYWORD: |
| 2098 | case LIST_KEYWORD: |
PavolVican | 91eb04a | 2017-02-03 13:45:52 +0100 | [diff] [blame] | 2099 | case CHOICE_KEYWORD: |
| 2100 | case CASE_KEYWORD: |
| 2101 | case ANYXML_KEYWORD: |
| 2102 | case ANYDATA_KEYWORD: |
PavolVican | 0759638 | 2017-02-03 14:05:12 +0100 | [diff] [blame] | 2103 | case USES_KEYWORD: |
| 2104 | case AUGMENT_KEYWORD: |
PavolVican | 97d1e6f | 2017-02-03 14:39:52 +0100 | [diff] [blame] | 2105 | case ACTION_KEYWORD: |
| 2106 | case RPC_KEYWORD: |
| 2107 | case INPUT_KEYWORD: |
| 2108 | case OUTPUT_KEYWORD: |
| 2109 | case NOTIFICATION_KEYWORD: |
PavolVican | 70ce745 | 2017-02-01 15:39:39 +0100 | [diff] [blame] | 2110 | ext = &((struct lys_node *)node)->ext; |
| 2111 | size = &((struct lys_node *)node)->ext_size; |
| 2112 | parent_type = LYEXT_PAR_NODE; |
| 2113 | break; |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 2114 | case ARGUMENT_KEYWORD: |
PavolVican | 5080974 | 2017-02-18 21:22:54 +0100 | [diff] [blame] | 2115 | if (is_ext_instance) { |
| 2116 | ext = &((struct lys_ext_instance *)node)->ext; |
| 2117 | size = &((struct lys_ext_instance *)node)->ext_size; |
| 2118 | parent_type = LYEXT_PAR_EXTINST; |
| 2119 | } else { |
| 2120 | ext = &((struct lys_ext *)node)->ext; |
| 2121 | size = &((struct lys_ext *)node)->ext_size; |
| 2122 | parent_type = LYEXT_PAR_EXT; |
| 2123 | } |
| 2124 | break; |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 2125 | case EXTENSION_KEYWORD: |
| 2126 | ext = &((struct lys_ext *)node)->ext; |
| 2127 | size = &((struct lys_ext *)node)->ext_size; |
| 2128 | parent_type = LYEXT_PAR_EXT; |
| 2129 | break; |
PavolVican | 5393d3f | 2017-02-06 23:30:55 +0100 | [diff] [blame] | 2130 | case FEATURE_KEYWORD: |
| 2131 | ext = &((struct lys_feature *)node)->ext; |
| 2132 | size = &((struct lys_feature *)node)->ext_size; |
| 2133 | parent_type = LYEXT_PAR_FEATURE; |
| 2134 | break; |
PavolVican | 8fa3124 | 2017-02-07 11:04:26 +0100 | [diff] [blame] | 2135 | case IDENTITY_KEYWORD: |
| 2136 | ext = &((struct lys_ident *)node)->ext; |
| 2137 | size = &((struct lys_ident *)node)->ext_size; |
| 2138 | parent_type = LYEXT_PAR_IDENT; |
| 2139 | break; |
| 2140 | case IF_FEATURE_KEYWORD: |
| 2141 | ext = &((struct lys_iffeature *)node)->ext; |
| 2142 | size = &((struct lys_iffeature *)node)->ext_size; |
| 2143 | parent_type = LYEXT_PAR_IFFEATURE; |
| 2144 | break; |
PavolVican | df9e797 | 2017-02-07 11:41:38 +0100 | [diff] [blame] | 2145 | case TYPEDEF_KEYWORD: |
| 2146 | ext = &((struct lys_tpdf *)node)->ext; |
| 2147 | size = &((struct lys_tpdf *)node)->ext_size; |
| 2148 | parent_type = LYEXT_PAR_TPDF; |
| 2149 | break; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2150 | case TYPE_KEYWORD: |
| 2151 | ext = &((struct yang_type *)node)->type->ext; |
| 2152 | size = &((struct yang_type *)node)->type->ext_size; |
| 2153 | parent_type = LYEXT_PAR_TYPE; |
| 2154 | break; |
| 2155 | case LENGTH_KEYWORD: |
| 2156 | case PATTERN_KEYWORD: |
| 2157 | case RANGE_KEYWORD: |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 2158 | case MUST_KEYWORD: |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2159 | ext = &((struct lys_restr *)node)->ext; |
| 2160 | size = &((struct lys_restr *)node)->ext_size; |
| 2161 | parent_type = LYEXT_PAR_RESTR; |
| 2162 | break; |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 2163 | case WHEN_KEYWORD: |
| 2164 | ext = &((struct lys_when *)node)->ext; |
| 2165 | size = &((struct lys_when *)node)->ext_size; |
| 2166 | parent_type = LYEXT_PAR_RESTR; |
| 2167 | break; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2168 | case ENUM_KEYWORD: |
| 2169 | ext = &((struct lys_type_enum *)node)->ext; |
| 2170 | size = &((struct lys_type_enum *)node)->ext_size; |
| 2171 | parent_type = LYEXT_PAR_TYPE_ENUM; |
| 2172 | break; |
| 2173 | case BIT_KEYWORD: |
| 2174 | ext = &((struct lys_type_bit *)node)->ext; |
| 2175 | size = &((struct lys_type_bit *)node)->ext_size; |
| 2176 | parent_type = LYEXT_PAR_TYPE_BIT; |
| 2177 | break; |
PavolVican | 77374ee | 2017-02-08 15:18:45 +0100 | [diff] [blame] | 2178 | case REFINE_KEYWORD: |
| 2179 | ext = &((struct lys_type_bit *)node)->ext; |
| 2180 | size = &((struct lys_type_bit *)node)->ext_size; |
| 2181 | parent_type = LYEXT_PAR_REFINE; |
| 2182 | break; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 2183 | case DEVIATION_KEYWORD: |
| 2184 | ext = &((struct lys_deviation *)node)->ext; |
| 2185 | size = &((struct lys_deviation *)node)->ext_size; |
| 2186 | parent_type = LYEXT_PAR_DEVIATION; |
| 2187 | break; |
| 2188 | case NOT_SUPPORTED_KEYWORD: |
| 2189 | case ADD_KEYWORD: |
| 2190 | case DELETE_KEYWORD: |
| 2191 | case REPLACE_KEYWORD: |
| 2192 | ext = &((struct lys_deviate *)node)->ext; |
| 2193 | size = &((struct lys_deviate *)node)->ext_size; |
| 2194 | parent_type = LYEXT_PAR_DEVIATE; |
| 2195 | break; |
PavolVican | defa485 | 2017-02-10 13:13:23 +0100 | [diff] [blame] | 2196 | case EXTENSION_INSTANCE: |
| 2197 | ext = &((struct lys_ext_instance *)node)->ext; |
| 2198 | size = &((struct lys_ext_instance *)node)->ext_size; |
| 2199 | parent_type = LYEXT_PAR_EXTINST; |
| 2200 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2201 | default: |
| 2202 | LOGINT; |
| 2203 | return NULL; |
| 2204 | } |
| 2205 | |
| 2206 | instance = calloc(1, sizeof *instance); |
| 2207 | if (!instance) { |
| 2208 | goto error; |
| 2209 | } |
| 2210 | instance->parent_type = parent_type; |
| 2211 | tmp = realloc(*ext, (*size + 1) * sizeof *tmp); |
| 2212 | if (!tmp) { |
| 2213 | goto error; |
| 2214 | } |
| 2215 | tmp[*size] = instance; |
| 2216 | *ext = tmp; |
| 2217 | (*size)++; |
| 2218 | return instance; |
| 2219 | |
| 2220 | error: |
| 2221 | LOGMEM; |
| 2222 | free(instance); |
| 2223 | return NULL; |
| 2224 | } |
| 2225 | |
| 2226 | void * |
| 2227 | yang_read_ext(struct lys_module *module, void *actual, char *ext_name, char *ext_arg, |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2228 | enum yytokentype actual_type, enum yytokentype backup_type, int is_ext_instance) |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2229 | { |
| 2230 | struct lys_ext_instance *instance; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2231 | LY_STMT stmt = LY_STMT_UNKNOWN; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2232 | |
| 2233 | if (backup_type != NODE) { |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 2234 | instance = yang_ext_instance(actual, backup_type, is_ext_instance); |
PavolVican | e6fa67b | 2017-02-01 11:06:57 +0100 | [diff] [blame] | 2235 | if (!instance) { |
| 2236 | return NULL; |
| 2237 | } |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2238 | switch (actual_type) { |
PavolVican | a4b79bc | 2017-02-08 13:47:00 +0100 | [diff] [blame] | 2239 | case YANG_VERSION_KEYWORD: |
| 2240 | instance->insubstmt = LYEXT_SUBSTMT_VERSION; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2241 | stmt = LY_STMT_VERSION; |
PavolVican | a4b79bc | 2017-02-08 13:47:00 +0100 | [diff] [blame] | 2242 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2243 | case NAMESPACE_KEYWORD: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2244 | instance->insubstmt = LYEXT_SUBSTMT_NAMESPACE; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2245 | stmt = LY_STMT_NAMESPACE; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2246 | break; |
PavolVican | e6fa67b | 2017-02-01 11:06:57 +0100 | [diff] [blame] | 2247 | case PREFIX_KEYWORD: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2248 | instance->insubstmt = LYEXT_SUBSTMT_PREFIX; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2249 | stmt = LY_STMT_PREFIX; |
PavolVican | e6fa67b | 2017-02-01 11:06:57 +0100 | [diff] [blame] | 2250 | break; |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 2251 | case REVISION_DATE_KEYWORD: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2252 | instance->insubstmt = LYEXT_SUBSTMT_REVISIONDATE; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2253 | stmt = LY_STMT_REVISIONDATE; |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 2254 | break; |
| 2255 | case DESCRIPTION_KEYWORD: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2256 | instance->insubstmt = LYEXT_SUBSTMT_DESCRIPTION; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2257 | stmt = LY_STMT_DESCRIPTION; |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 2258 | break; |
| 2259 | case REFERENCE_KEYWORD: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2260 | instance->insubstmt = LYEXT_SUBSTMT_REFERENCE; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2261 | stmt = LY_STMT_REFERENCE; |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 2262 | break; |
PavolVican | 171717d | 2017-02-01 14:49:55 +0100 | [diff] [blame] | 2263 | case CONTACT_KEYWORD: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2264 | instance->insubstmt = LYEXT_SUBSTMT_CONTACT; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2265 | stmt = LY_STMT_CONTACT; |
PavolVican | 171717d | 2017-02-01 14:49:55 +0100 | [diff] [blame] | 2266 | break; |
| 2267 | case ORGANIZATION_KEYWORD: |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2268 | instance->insubstmt = LYEXT_SUBSTMT_ORGANIZATION; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2269 | stmt = LY_STMT_ORGANIZATION; |
PavolVican | 171717d | 2017-02-01 14:49:55 +0100 | [diff] [blame] | 2270 | break; |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 2271 | case YIN_ELEMENT_KEYWORD: |
| 2272 | instance->insubstmt = LYEXT_SUBSTMT_YINELEM; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2273 | stmt = LY_STMT_YINELEM; |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 2274 | break; |
| 2275 | case STATUS_KEYWORD: |
| 2276 | instance->insubstmt = LYEXT_SUBSTMT_STATUS; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2277 | stmt = LY_STMT_STATUS; |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 2278 | break; |
PavolVican | 8fa3124 | 2017-02-07 11:04:26 +0100 | [diff] [blame] | 2279 | case BASE_KEYWORD: |
| 2280 | instance->insubstmt = LYEXT_SUBSTMT_BASE; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2281 | stmt = LY_STMT_BASE; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2282 | if (backup_type == IDENTITY_KEYWORD) { |
| 2283 | instance->insubstmt_index = ((struct lys_ident *)actual)->base_size; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2284 | } else if (backup_type == TYPE_KEYWORD) { |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2285 | instance->insubstmt_index = ((struct yang_type *)actual)->type->info.ident.count; |
| 2286 | } |
PavolVican | 8fa3124 | 2017-02-07 11:04:26 +0100 | [diff] [blame] | 2287 | break; |
PavolVican | df9e797 | 2017-02-07 11:41:38 +0100 | [diff] [blame] | 2288 | case DEFAULT_KEYWORD: |
| 2289 | instance->insubstmt = LYEXT_SUBSTMT_DEFAULT; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2290 | stmt = LY_STMT_DEFAULT; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 2291 | switch (backup_type) { |
| 2292 | case LEAF_LIST_KEYWORD: |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2293 | instance->insubstmt_index = ((struct lys_node_leaflist *)actual)->dflt_size; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 2294 | break; |
| 2295 | case REFINE_KEYWORD: |
PavolVican | 77374ee | 2017-02-08 15:18:45 +0100 | [diff] [blame] | 2296 | instance->insubstmt_index = ((struct lys_refine *)actual)->dflt_size; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 2297 | break; |
| 2298 | case ADD_KEYWORD: |
| 2299 | instance->insubstmt_index = ((struct lys_deviate *)actual)->dflt_size; |
| 2300 | break; |
| 2301 | default: |
| 2302 | /* nothing changes */ |
| 2303 | break; |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2304 | } |
PavolVican | df9e797 | 2017-02-07 11:41:38 +0100 | [diff] [blame] | 2305 | break; |
| 2306 | case UNITS_KEYWORD: |
| 2307 | instance->insubstmt = LYEXT_SUBSTMT_UNITS; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2308 | stmt = LY_STMT_UNITS; |
PavolVican | df9e797 | 2017-02-07 11:41:38 +0100 | [diff] [blame] | 2309 | break; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2310 | case REQUIRE_INSTANCE_KEYWORD: |
| 2311 | instance->insubstmt = LYEXT_SUBSTMT_REQINSTANCE; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2312 | stmt = LY_STMT_REQINSTANCE; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2313 | break; |
| 2314 | case PATH_KEYWORD: |
| 2315 | instance->insubstmt = LYEXT_SUBSTMT_PATH; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2316 | stmt = LY_STMT_PATH; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2317 | break; |
| 2318 | case ERROR_MESSAGE_KEYWORD: |
| 2319 | instance->insubstmt = LYEXT_SUBSTMT_ERRMSG; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2320 | stmt = LY_STMT_ERRMSG; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2321 | break; |
| 2322 | case ERROR_APP_TAG_KEYWORD: |
| 2323 | instance->insubstmt = LYEXT_SUBSTMT_ERRTAG; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2324 | stmt = LY_STMT_ERRTAG; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2325 | break; |
| 2326 | case MODIFIER_KEYWORD: |
| 2327 | instance->insubstmt = LYEXT_SUBSTMT_MODIFIER; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2328 | stmt = LY_STMT_MODIFIER; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2329 | break; |
| 2330 | case FRACTION_DIGITS_KEYWORD: |
| 2331 | instance->insubstmt = LYEXT_SUBSTMT_DIGITS; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2332 | stmt = LY_STMT_DIGITS; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2333 | break; |
| 2334 | case VALUE_KEYWORD: |
| 2335 | instance->insubstmt = LYEXT_SUBSTMT_VALUE; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2336 | stmt = LY_STMT_VALUE; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2337 | break; |
| 2338 | case POSITION_KEYWORD: |
| 2339 | instance->insubstmt = LYEXT_SUBSTMT_POSITION; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2340 | stmt = LY_STMT_POSITION; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 2341 | break; |
PavolVican | 5b91084 | 2017-02-08 13:08:47 +0100 | [diff] [blame] | 2342 | case PRESENCE_KEYWORD: |
| 2343 | instance->insubstmt = LYEXT_SUBSTMT_PRESENCE; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2344 | stmt = LY_STMT_PRESENCE; |
PavolVican | 5b91084 | 2017-02-08 13:08:47 +0100 | [diff] [blame] | 2345 | break; |
| 2346 | case CONFIG_KEYWORD: |
| 2347 | instance->insubstmt = LYEXT_SUBSTMT_CONFIG; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2348 | stmt = LY_STMT_CONFIG; |
PavolVican | 5b91084 | 2017-02-08 13:08:47 +0100 | [diff] [blame] | 2349 | break; |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2350 | case MANDATORY_KEYWORD: |
| 2351 | instance->insubstmt = LYEXT_SUBSTMT_MANDATORY; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2352 | stmt = LY_STMT_MANDATORY; |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2353 | break; |
| 2354 | case MIN_ELEMENTS_KEYWORD: |
| 2355 | instance->insubstmt = LYEXT_SUBSTMT_MIN; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2356 | stmt = LY_STMT_MIN; |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2357 | break; |
| 2358 | case MAX_ELEMENTS_KEYWORD: |
| 2359 | instance->insubstmt = LYEXT_SUBSTMT_MAX; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2360 | stmt = LY_STMT_MAX; |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2361 | break; |
| 2362 | case ORDERED_BY_KEYWORD: |
| 2363 | instance->insubstmt = LYEXT_SUBSTMT_ORDEREDBY; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2364 | stmt = LY_STMT_ORDEREDBY; |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2365 | break; |
| 2366 | case KEY_KEYWORD: |
| 2367 | instance->insubstmt = LYEXT_SUBSTMT_KEY; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2368 | stmt = LY_STMT_KEY; |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2369 | break; |
| 2370 | case UNIQUE_KEYWORD: |
| 2371 | instance->insubstmt = LYEXT_SUBSTMT_UNIQUE; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2372 | stmt = LY_STMT_UNIQUE; |
| 2373 | switch (backup_type) { |
| 2374 | case LIST_KEYWORD: |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 2375 | instance->insubstmt_index = ((struct lys_node_list *)actual)->unique_size; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2376 | break; |
| 2377 | case ADD_KEYWORD: |
| 2378 | case DELETE_KEYWORD: |
| 2379 | case REPLACE_KEYWORD: |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 2380 | instance->insubstmt_index = ((struct lys_deviate *)actual)->unique_size; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2381 | break; |
| 2382 | default: |
| 2383 | /* nothing changes */ |
| 2384 | break; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 2385 | } |
PavolVican | 3feb2f9 | 2017-02-08 13:44:39 +0100 | [diff] [blame] | 2386 | break; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2387 | default: |
| 2388 | LOGINT; |
| 2389 | return NULL; |
| 2390 | } |
| 2391 | } else { |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 2392 | instance = yang_ext_instance(actual, actual_type, is_ext_instance); |
PavolVican | e6fa67b | 2017-02-01 11:06:57 +0100 | [diff] [blame] | 2393 | if (!instance) { |
| 2394 | return NULL; |
| 2395 | } |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 2396 | instance->insubstmt = LYEXT_SUBSTMT_SELF; |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 2397 | switch (actual_type) { |
| 2398 | case ARGUMENT_KEYWORD: |
| 2399 | instance->insubstmt = LYEXT_SUBSTMT_ARGUMENT; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2400 | stmt = LY_STMT_ARGUMENT; |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 2401 | break; |
PavolVican | faa4970 | 2017-02-06 12:10:59 +0100 | [diff] [blame] | 2402 | case BELONGS_TO_KEYWORD: |
| 2403 | instance->insubstmt = LYEXT_SUBSTMT_BELONGSTO; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2404 | stmt = LY_STMT_BELONGSTO; |
PavolVican | faa4970 | 2017-02-06 12:10:59 +0100 | [diff] [blame] | 2405 | break; |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 2406 | default: |
| 2407 | instance->insubstmt = LYEXT_SUBSTMT_SELF; |
| 2408 | break; |
| 2409 | } |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2410 | } |
| 2411 | instance->flags |= LYEXT_OPT_YANG; |
| 2412 | instance->def = (struct lys_ext *)ext_name; /* hack for UNRES */ |
| 2413 | instance->arg_value = lydict_insert_zc(module->ctx, ext_arg); |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 2414 | if (is_ext_instance && stmt != LY_STMT_UNKNOWN && instance->parent_type == LYEXT_PAR_EXTINST) { |
| 2415 | instance->insubstmt_index = yang_fill_ext_substm_index(actual, stmt, backup_type); |
| 2416 | } |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 2417 | return instance; |
| 2418 | } |
| 2419 | |
PavolVican | 05c4f9b | 2017-09-07 13:33:54 +0200 | [diff] [blame] | 2420 | static int |
| 2421 | check_status_flag(struct lys_node *node, struct lys_node *parent) |
| 2422 | { |
| 2423 | char *str; |
| 2424 | |
| 2425 | if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) { |
| 2426 | /* status is not inherited by specification, but it not make sense to have |
| 2427 | * current in deprecated or deprecated in obsolete, so we print warning |
| 2428 | * and fix the schema by inheriting */ |
| 2429 | if (!(node->flags & (LYS_STATUS_MASK))) { |
| 2430 | /* status not explicitely specified on the current node -> inherit */ |
| 2431 | str = lys_path(parent); |
| 2432 | LOGWRN("Missing status in %s subtree (%s), inheriting.", |
| 2433 | parent->flags & LYS_STATUS_DEPRC ? "deprecated" : "obsolete", str); |
| 2434 | free(str); |
| 2435 | node->flags |= parent->flags & LYS_STATUS_MASK; |
| 2436 | } else if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) { |
| 2437 | /* invalid combination of statuses */ |
| 2438 | switch (node->flags & LYS_STATUS_MASK) { |
| 2439 | case 0: |
| 2440 | case LYS_STATUS_CURR: |
| 2441 | LOGVAL(LYE_INSTATUS, LY_VLOG_LYS, parent, "current", strnodetype(node->nodetype), "is child of", |
| 2442 | parent->flags & LYS_STATUS_DEPRC ? "deprecated" : "obsolete", parent->name); |
| 2443 | break; |
| 2444 | case LYS_STATUS_DEPRC: |
| 2445 | LOGVAL(LYE_INSTATUS, LY_VLOG_LYS, parent, "deprecated", strnodetype(node->nodetype), "is child of", |
| 2446 | "obsolete", parent->name); |
| 2447 | break; |
| 2448 | } |
| 2449 | return EXIT_FAILURE; |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | return EXIT_SUCCESS; |
| 2454 | } |
| 2455 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2456 | int |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 2457 | store_config_flag(struct lys_node *node, int options) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2458 | { |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2459 | switch (node->nodetype) { |
| 2460 | case LYS_CONTAINER: |
| 2461 | case LYS_LEAF: |
| 2462 | case LYS_LEAFLIST: |
| 2463 | case LYS_LIST: |
| 2464 | case LYS_CHOICE: |
| 2465 | case LYS_ANYDATA: |
| 2466 | case LYS_ANYXML: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 2467 | if (options & LYS_PARSE_OPT_CFG_IGNORE) { |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2468 | node->flags |= node->flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET)); |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 2469 | } else if (!(options & LYS_PARSE_OPT_CFG_NOINHERIT)) { |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2470 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 2471 | /* get config flag from parent */ |
| 2472 | if (node->parent) { |
| 2473 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 2474 | } else { |
| 2475 | /* default config is true */ |
| 2476 | node->flags |= LYS_CONFIG_W; |
| 2477 | } |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2478 | } |
| 2479 | } |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2480 | break; |
| 2481 | case LYS_CASE: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 2482 | if (!(options & (LYS_PARSE_OPT_CFG_IGNORE | LYS_PARSE_OPT_CFG_NOINHERIT))) { |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2483 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 2484 | /* get config flag from parent */ |
| 2485 | if (node->parent) { |
| 2486 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 2487 | } else { |
| 2488 | /* default config is true */ |
| 2489 | node->flags |= LYS_CONFIG_W; |
| 2490 | } |
| 2491 | } |
| 2492 | } |
| 2493 | break; |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2494 | default: |
| 2495 | break; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2496 | } |
Pavol Vican | ec59881 | 2016-11-30 14:13:38 +0100 | [diff] [blame] | 2497 | |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 2498 | return EXIT_SUCCESS; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | int |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2502 | yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, |
| 2503 | const char *data, unsigned int size_data, struct lys_node **node) |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2504 | { |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2505 | unsigned int size; |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2506 | YY_BUFFER_STATE bp; |
| 2507 | yyscan_t scanner = NULL; |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2508 | int ret = 0; |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 2509 | struct lys_module *trg; |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 2510 | struct yang_parameter param; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2511 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2512 | size = (size_data) ? size_data : strlen(data) + 2; |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2513 | yylex_init(&scanner); |
| 2514 | bp = yy_scan_buffer((char *)data, size, scanner); |
| 2515 | yy_switch_to_buffer(bp, scanner); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2516 | memset(¶m, 0, sizeof param); |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 2517 | param.module = module; |
| 2518 | param.submodule = submodule; |
| 2519 | param.unres = unres; |
| 2520 | param.node = node; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2521 | param.flags |= YANG_REMOVE_IMPORT; |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 2522 | if (yyparse(scanner, ¶m)) { |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2523 | if (param.flags & YANG_REMOVE_IMPORT) { |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 2524 | trg = (submodule) ? (struct lys_module *)submodule : module; |
| 2525 | yang_free_import(trg->ctx, trg->imp, 0, trg->imp_size); |
| 2526 | yang_free_include(trg->ctx, trg->inc, 0, trg->inc_size); |
| 2527 | trg->inc_size = 0; |
| 2528 | trg->imp_size = 0; |
| 2529 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2530 | ret = (param.flags & YANG_EXIST_MODULE) ? 1 : -1; |
| 2531 | } |
| 2532 | yy_delete_buffer(bp, scanner); |
| 2533 | yylex_destroy(scanner); |
| 2534 | return ret; |
| 2535 | } |
| 2536 | |
| 2537 | int |
| 2538 | yang_parse_ext_substatement(struct lys_module *module, struct unres_schema *unres, const char *data, |
| 2539 | char *ext_name, struct lys_ext_instance_complex *ext) |
| 2540 | { |
| 2541 | unsigned int size; |
| 2542 | YY_BUFFER_STATE bp; |
| 2543 | yyscan_t scanner = NULL; |
| 2544 | int ret = 0; |
| 2545 | struct yang_parameter param; |
PavolVican | f3091bf | 2017-02-19 18:27:01 +0100 | [diff] [blame] | 2546 | struct lys_node *node = NULL; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2547 | |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 2548 | if (!data) { |
| 2549 | return EXIT_SUCCESS; |
| 2550 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2551 | size = strlen(data) + 2; |
| 2552 | yylex_init(&scanner); |
| 2553 | bp = yy_scan_buffer((char *)data, size, scanner); |
| 2554 | yy_switch_to_buffer(bp, scanner); |
| 2555 | memset(¶m, 0, sizeof param); |
| 2556 | param.module = module; |
| 2557 | param.unres = unres; |
PavolVican | f3091bf | 2017-02-19 18:27:01 +0100 | [diff] [blame] | 2558 | param.node = &node; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2559 | param.actual_node = (void **)ext; |
| 2560 | param.data_node = (void **)ext_name; |
| 2561 | param.flags |= EXT_INSTANCE_SUBSTMT; |
| 2562 | if (yyparse(scanner, ¶m)) { |
PavolVican | f3091bf | 2017-02-19 18:27:01 +0100 | [diff] [blame] | 2563 | yang_free_nodes(module->ctx, node); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 2564 | ret = -1; |
PavolVican | f3091bf | 2017-02-19 18:27:01 +0100 | [diff] [blame] | 2565 | } else { |
| 2566 | /* success parse, but it needs some sematic controls */ |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 2567 | if (node && yang_check_nodes(module, (struct lys_node *)ext, node, LYS_PARSE_OPT_CFG_NOINHERIT, unres)) { |
PavolVican | f3091bf | 2017-02-19 18:27:01 +0100 | [diff] [blame] | 2568 | ret = -1; |
| 2569 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2570 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2571 | yy_delete_buffer(bp, scanner); |
| 2572 | yylex_destroy(scanner); |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2573 | return ret; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | struct lys_module * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2577 | 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] | 2578 | { |
| 2579 | |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2580 | struct lys_module *module = NULL, *tmp_mod; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2581 | struct unres_schema *unres = NULL; |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2582 | struct lys_node *node = NULL; |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2583 | int ret; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2584 | |
| 2585 | unres = calloc(1, sizeof *unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2586 | LY_CHECK_ERR_GOTO(!unres, LOGMEM, error); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2587 | |
| 2588 | module = calloc(1, sizeof *module); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2589 | LY_CHECK_ERR_GOTO(!module, LOGMEM, error); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2590 | |
| 2591 | /* initiale module */ |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 2592 | module->ctx = ctx; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2593 | module->type = 0; |
| 2594 | module->implemented = (implement ? 1 : 0); |
| 2595 | |
Radek Krejci | 9e757e0 | 2017-03-08 17:18:09 +0100 | [diff] [blame] | 2596 | /* add into the list of processed modules */ |
| 2597 | if (lyp_check_circmod_add(module)) { |
| 2598 | goto error; |
| 2599 | } |
| 2600 | |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2601 | ret = yang_parse_mem(module, NULL, unres, data, size, &node); |
| 2602 | if (ret == -1) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2603 | free_yang_common(module, node); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2604 | goto error; |
Michal Vasko | 7b460e5 | 2017-02-10 14:50:26 +0100 | [diff] [blame] | 2605 | } else if (ret == 1) { |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2606 | assert(!unres->count); |
| 2607 | } else { |
| 2608 | if (yang_check_sub_module(module, unres, node)) { |
| 2609 | goto error; |
| 2610 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2611 | |
Michal Vasko | 7b460e5 | 2017-02-10 14:50:26 +0100 | [diff] [blame] | 2612 | if (unres->count && resolve_unres_schema(module, unres)) { |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2613 | goto error; |
| 2614 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2615 | } |
| 2616 | |
PavolVican | fe83b15 | 2017-02-19 03:19:29 +0100 | [diff] [blame] | 2617 | lyp_sort_revisions(module); |
| 2618 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2619 | if (revision) { |
| 2620 | /* check revision of the parsed model */ |
| 2621 | if (!module->rev_size || strcmp(revision, module->rev[0].date)) { |
| 2622 | LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", |
| 2623 | module->name, module->rev[0].date, revision); |
| 2624 | goto error; |
| 2625 | } |
| 2626 | } |
| 2627 | |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2628 | /* add into context if not already there */ |
| 2629 | if (!ret) { |
Michal Vasko | e8c73dd | 2017-02-10 11:34:17 +0100 | [diff] [blame] | 2630 | /* check correctness of includes */ |
| 2631 | if (lyp_check_include_missing(module)) { |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2632 | goto error; |
| 2633 | } |
Michal Vasko | e8c73dd | 2017-02-10 11:34:17 +0100 | [diff] [blame] | 2634 | |
PavolVican | fd6f695 | 2017-02-14 00:09:42 +0100 | [diff] [blame] | 2635 | /* remove our submodules from the parsed submodules list */ |
| 2636 | lyp_del_includedup(module); |
| 2637 | |
| 2638 | |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 2639 | if (lyp_rfn_apply_ext(module) || lyp_deviation_apply_ext(module)) { |
Michal Vasko | e8c73dd | 2017-02-10 11:34:17 +0100 | [diff] [blame] | 2640 | goto error; |
| 2641 | } |
| 2642 | |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2643 | if (lyp_ctx_add_module(module)) { |
Michal Vasko | 7da5b0b | 2016-05-02 16:36:59 +0200 | [diff] [blame] | 2644 | goto error; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2645 | } |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2646 | |
| 2647 | if (module->deviation_size && !module->implemented) { |
| 2648 | LOGVRB("Module \"%s\" includes deviations, changing its conformance to \"implement\".", module->name); |
| 2649 | /* deviations always causes target to be made implemented, |
| 2650 | * but augents and leafrefs not, so we have to apply them now */ |
| 2651 | if (lys_set_implemented(module)) { |
| 2652 | goto error; |
| 2653 | } |
| 2654 | } |
| 2655 | } else { |
| 2656 | tmp_mod = module; |
| 2657 | |
| 2658 | /* get the model from the context */ |
| 2659 | module = (struct lys_module *)ly_ctx_get_module(ctx, module->name, revision); |
| 2660 | assert(module); |
| 2661 | |
| 2662 | /* free what was parsed */ |
| 2663 | lys_free(tmp_mod, NULL, 0); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2664 | } |
| 2665 | |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 2666 | unres_schema_free(NULL, &unres, 0); |
Radek Krejci | 9e757e0 | 2017-03-08 17:18:09 +0100 | [diff] [blame] | 2667 | lyp_check_circmod_pop(ctx); |
Michal Vasko | be136f6 | 2017-09-21 12:08:39 +0200 | [diff] [blame^] | 2668 | LOGVRB("Module \"%s%s%s\" successfully parsed as %s.", module->name, (module->rev_size ? "@" : ""), |
| 2669 | (module->rev_size ? module->rev[0].date : ""), (module->implemented ? "implemented" : "imported")); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2670 | return module; |
| 2671 | |
| 2672 | error: |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2673 | /* cleanup */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 2674 | unres_schema_free(module, &unres, 1); |
Radek Krejci | f505cd1 | 2017-06-13 10:32:48 +0200 | [diff] [blame] | 2675 | if (!module) { |
Radek Krejci | 48cfa0f | 2016-11-08 19:18:34 +0100 | [diff] [blame] | 2676 | if (ly_vecode != LYVE_SUBMODULE) { |
| 2677 | LOGERR(ly_errno, "Module parsing failed."); |
| 2678 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2679 | return NULL; |
| 2680 | } |
| 2681 | |
Radek Krejci | f505cd1 | 2017-06-13 10:32:48 +0200 | [diff] [blame] | 2682 | if (module->name) { |
| 2683 | LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name); |
| 2684 | } else { |
| 2685 | LOGERR(ly_errno, "Module parsing failed."); |
| 2686 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2687 | |
Radek Krejci | 9e757e0 | 2017-03-08 17:18:09 +0100 | [diff] [blame] | 2688 | lyp_check_circmod_pop(ctx); |
Michal Vasko | cf0489e | 2017-02-13 11:57:45 +0100 | [diff] [blame] | 2689 | lyp_del_includedup(module); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2690 | lys_sub_module_remove_devs_augs(module); |
| 2691 | lys_free(module, NULL, 1); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2692 | return NULL; |
| 2693 | } |
| 2694 | |
| 2695 | struct lys_submodule * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2696 | 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] | 2697 | { |
| 2698 | struct lys_submodule *submodule; |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2699 | struct lys_node *node = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2700 | |
| 2701 | submodule = calloc(1, sizeof *submodule); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2702 | LY_CHECK_ERR_GOTO(!submodule, LOGMEM, error); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2703 | |
| 2704 | submodule->ctx = module->ctx; |
| 2705 | submodule->type = 1; |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 2706 | submodule->implemented = module->implemented; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2707 | submodule->belongsto = module; |
| 2708 | |
Radek Krejci | 9e757e0 | 2017-03-08 17:18:09 +0100 | [diff] [blame] | 2709 | /* add into the list of processed modules */ |
| 2710 | if (lyp_check_circmod_add((struct lys_module *)submodule)) { |
| 2711 | goto error; |
| 2712 | } |
| 2713 | |
PavolVican | 9e81c6a | 2017-02-09 13:09:07 +0100 | [diff] [blame] | 2714 | /* module cannot be changed in this case and 1 cannot be returned */ |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2715 | if (yang_parse_mem(module, submodule, unres, data, size, &node)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2716 | free_yang_common((struct lys_module *)submodule, node); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2717 | goto error; |
| 2718 | } |
| 2719 | |
PavolVican | fe83b15 | 2017-02-19 03:19:29 +0100 | [diff] [blame] | 2720 | lyp_sort_revisions((struct lys_module *)submodule); |
| 2721 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2722 | if (yang_check_sub_module((struct lys_module *)submodule, unres, node)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2723 | goto error; |
| 2724 | } |
| 2725 | |
Radek Krejci | 9e757e0 | 2017-03-08 17:18:09 +0100 | [diff] [blame] | 2726 | lyp_check_circmod_pop(module->ctx); |
| 2727 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2728 | LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2729 | return submodule; |
| 2730 | |
| 2731 | error: |
| 2732 | /* cleanup */ |
Michal Vasko | 44ab146 | 2017-05-18 13:18:36 +0200 | [diff] [blame] | 2733 | unres_schema_free((struct lys_module *)submodule, &unres, 0); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2734 | |
| 2735 | if (!submodule || !submodule->name) { |
| 2736 | free(submodule); |
| 2737 | LOGERR(ly_errno, "Submodule parsing failed."); |
| 2738 | return NULL; |
| 2739 | } |
| 2740 | |
| 2741 | LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name); |
| 2742 | |
Radek Krejci | 9e757e0 | 2017-03-08 17:18:09 +0100 | [diff] [blame] | 2743 | lyp_check_circmod_pop(module->ctx); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2744 | lys_sub_module_remove_devs_augs((struct lys_module *)submodule); |
| 2745 | lys_submodule_module_data_free(submodule); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2746 | lys_submodule_free(submodule, NULL); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2747 | return NULL; |
| 2748 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2749 | |
| 2750 | static int |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2751 | read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output) |
| 2752 | { |
| 2753 | int k = 0, j; |
| 2754 | |
| 2755 | while (in_index < size) { |
| 2756 | if (input[in_index] == ' ') { |
| 2757 | k++; |
| 2758 | } else if (input[in_index] == '\t') { |
| 2759 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2760 | k += 8; |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2761 | } else if (input[in_index] == '\\' && input[in_index + 1] == 't') { |
| 2762 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2763 | k += 8; |
| 2764 | ++in_index; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2765 | } else { |
| 2766 | break; |
| 2767 | } |
| 2768 | ++in_index; |
| 2769 | if (k >= indent) { |
| 2770 | for (j = k - indent; j > 0; --j) { |
| 2771 | output[*out_index] = ' '; |
Pavol Vican | a7bf337 | 2016-12-01 15:58:18 +0100 | [diff] [blame] | 2772 | if (j > 1) { |
| 2773 | ++(*out_index); |
| 2774 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2775 | } |
| 2776 | break; |
| 2777 | } |
| 2778 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2779 | return in_index - 1; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | char * |
PavolVican | 1bc2206 | 2017-01-19 15:09:04 +0100 | [diff] [blame] | 2783 | yang_read_string(const char *input, char *output, int size, int offset, int indent) { |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2784 | int i = 0, out_index = offset, space = 0; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2785 | |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2786 | while (i < size) { |
| 2787 | switch (input[i]) { |
| 2788 | case '\n': |
| 2789 | out_index -= space; |
| 2790 | output[out_index] = '\n'; |
| 2791 | space = 0; |
| 2792 | i = read_indent(input, indent, size, i + 1, &out_index, output); |
| 2793 | break; |
| 2794 | case ' ': |
| 2795 | case '\t': |
| 2796 | output[out_index] = input[i]; |
| 2797 | ++space; |
| 2798 | break; |
| 2799 | case '\\': |
| 2800 | if (input[i + 1] == 'n') { |
| 2801 | out_index -= space; |
| 2802 | output[out_index] = '\n'; |
| 2803 | space = 0; |
| 2804 | i = read_indent(input, indent, size, i + 2, &out_index, output); |
| 2805 | } else if (input[i + 1] == 't') { |
| 2806 | output[out_index] = '\t'; |
| 2807 | ++i; |
| 2808 | ++space; |
| 2809 | } else if (input[i + 1] == '\\') { |
| 2810 | output[out_index] = '\\'; |
| 2811 | ++i; |
| 2812 | } else if ((i + 1) != size && input[i + 1] == '"') { |
| 2813 | output[out_index] = '"'; |
| 2814 | ++i; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2815 | } else { |
PavolVican | 1bc2206 | 2017-01-19 15:09:04 +0100 | [diff] [blame] | 2816 | /* backslash must not be followed by any other character */ |
| 2817 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input); |
| 2818 | return NULL; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2819 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2820 | break; |
| 2821 | default: |
| 2822 | output[out_index] = input[i]; |
| 2823 | space = 0; |
| 2824 | break; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2825 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2826 | ++i; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2827 | ++out_index; |
| 2828 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2829 | output[out_index] = '\0'; |
| 2830 | if (size != out_index) { |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2831 | output = realloc(output, out_index + 1); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 2832 | LY_CHECK_ERR_RETURN(!output, LOGMEM, NULL); |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2833 | } |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2834 | return output; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2835 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2836 | |
| 2837 | /* free function */ |
| 2838 | |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 2839 | void |
| 2840 | yang_type_free(struct ly_ctx *ctx, struct lys_type *type) |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2841 | { |
| 2842 | struct yang_type *stype = (struct yang_type *)type->der; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 2843 | unsigned int i; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2844 | |
| 2845 | if (!stype) { |
| 2846 | return ; |
| 2847 | } |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 2848 | if (type->base == LY_TYPE_DER || type->base == LY_TYPE_ERR || type->base == LY_TYPE_UNION) { |
| 2849 | lydict_remove(ctx, stype->name); |
| 2850 | if (stype->base == LY_TYPE_IDENT && (!(stype->flags & LYS_NO_ERASE_IDENTITY))) { |
| 2851 | for (i = 0; i < type->info.ident.count; ++i) { |
| 2852 | free(type->info.ident.ref[i]); |
| 2853 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2854 | } |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 2855 | if (stype->base == LY_TYPE_UNION) { |
| 2856 | for (i = 0; i < type->info.uni.count; ++i) { |
| 2857 | yang_type_free(ctx, &type->info.uni.types[i]); |
| 2858 | } |
| 2859 | free(type->info.uni.types); |
| 2860 | type->base = LY_TYPE_DER; |
| 2861 | } else { |
| 2862 | type->base = stype->base; |
| 2863 | } |
| 2864 | free(stype); |
| 2865 | type->der = NULL; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2866 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2867 | lys_type_free(ctx, type, NULL); |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2868 | type->base = LY_TYPE_DER; |
PavolVican | 575a934 | 2017-02-15 18:14:39 +0100 | [diff] [blame] | 2869 | type->ext_size = 0; |
| 2870 | type->ext = NULL; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | static void |
| 2874 | yang_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf, uint8_t start, uint8_t size) |
| 2875 | { |
| 2876 | uint8_t i; |
| 2877 | |
| 2878 | assert(ctx); |
| 2879 | if (!tpdf) { |
| 2880 | return; |
| 2881 | } |
| 2882 | |
| 2883 | for (i = start; i < size; ++i) { |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2884 | lydict_remove(ctx, tpdf[i].name); |
| 2885 | lydict_remove(ctx, tpdf[i].dsc); |
| 2886 | lydict_remove(ctx, tpdf[i].ref); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2887 | |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2888 | yang_type_free(ctx, &tpdf[i].type); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2889 | |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2890 | lydict_remove(ctx, tpdf[i].units); |
| 2891 | lydict_remove(ctx, tpdf[i].dflt); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2892 | lys_extension_instances_free(ctx, tpdf[i].ext, tpdf[i].ext_size, NULL); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2893 | } |
| 2894 | } |
| 2895 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2896 | static void |
| 2897 | yang_free_import(struct ly_ctx *ctx, struct lys_import *imp, uint8_t start, uint8_t size) |
| 2898 | { |
| 2899 | uint8_t i; |
| 2900 | |
| 2901 | for (i = start; i < size; ++i){ |
| 2902 | free((char *)imp[i].module); |
| 2903 | lydict_remove(ctx, imp[i].prefix); |
| 2904 | lydict_remove(ctx, imp[i].dsc); |
| 2905 | lydict_remove(ctx, imp[i].ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2906 | lys_extension_instances_free(ctx, imp[i].ext, imp[i].ext_size, NULL); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2910 | static void |
| 2911 | yang_free_include(struct ly_ctx *ctx, struct lys_include *inc, uint8_t start, uint8_t size) |
| 2912 | { |
| 2913 | uint8_t i; |
| 2914 | |
| 2915 | for (i = start; i < size; ++i){ |
| 2916 | free((char *)inc[i].submodule); |
| 2917 | lydict_remove(ctx, inc[i].dsc); |
| 2918 | lydict_remove(ctx, inc[i].ref); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2919 | lys_extension_instances_free(ctx, inc[i].ext, inc[i].ext_size, NULL); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2920 | } |
| 2921 | } |
| 2922 | |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 2923 | static void |
| 2924 | yang_free_ident_base(struct lys_ident *ident, uint32_t start, uint32_t size) |
| 2925 | { |
| 2926 | uint32_t i; |
| 2927 | uint8_t j; |
| 2928 | |
| 2929 | /* free base name */ |
| 2930 | for (i = start; i < size; ++i) { |
| 2931 | for (j = 0; j < ident[i].base_size; ++j) { |
| 2932 | free(ident[i].base[j]); |
| 2933 | } |
| 2934 | } |
| 2935 | } |
| 2936 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2937 | static void |
| 2938 | yang_free_grouping(struct ly_ctx *ctx, struct lys_node_grp * grp) |
| 2939 | { |
| 2940 | yang_tpdf_free(ctx, grp->tpdf, 0, grp->tpdf_size); |
| 2941 | free(grp->tpdf); |
| 2942 | } |
| 2943 | |
| 2944 | static void |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2945 | yang_free_container(struct ly_ctx *ctx, struct lys_node_container * cont) |
| 2946 | { |
| 2947 | uint8_t i; |
| 2948 | |
| 2949 | yang_tpdf_free(ctx, cont->tpdf, 0, cont->tpdf_size); |
| 2950 | free(cont->tpdf); |
| 2951 | lydict_remove(ctx, cont->presence); |
| 2952 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 2953 | for (i = 0; i < cont->must_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2954 | lys_restr_free(ctx, &cont->must[i], NULL); |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2955 | } |
| 2956 | free(cont->must); |
| 2957 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2958 | lys_when_free(ctx, cont->when, NULL); |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | static void |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 2962 | yang_free_leaf(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
| 2963 | { |
| 2964 | uint8_t i; |
| 2965 | |
| 2966 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2967 | lys_restr_free(ctx, &leaf->must[i], NULL); |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 2968 | } |
| 2969 | free(leaf->must); |
| 2970 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2971 | lys_when_free(ctx, leaf->when, NULL); |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 2972 | |
| 2973 | yang_type_free(ctx, &leaf->type); |
| 2974 | lydict_remove(ctx, leaf->units); |
| 2975 | lydict_remove(ctx, leaf->dflt); |
| 2976 | } |
| 2977 | |
| 2978 | static void |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2979 | yang_free_leaflist(struct ly_ctx *ctx, struct lys_node_leaflist *leaflist) |
| 2980 | { |
| 2981 | uint8_t i; |
| 2982 | |
| 2983 | for (i = 0; i < leaflist->must_size; i++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2984 | lys_restr_free(ctx, &leaflist->must[i], NULL); |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2985 | } |
| 2986 | free(leaflist->must); |
| 2987 | |
| 2988 | for (i = 0; i < leaflist->dflt_size; i++) { |
| 2989 | lydict_remove(ctx, leaflist->dflt[i]); |
| 2990 | } |
| 2991 | free(leaflist->dflt); |
| 2992 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 2993 | lys_when_free(ctx, leaflist->when, NULL); |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2994 | |
| 2995 | yang_type_free(ctx, &leaflist->type); |
| 2996 | lydict_remove(ctx, leaflist->units); |
| 2997 | } |
| 2998 | |
| 2999 | static void |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3000 | yang_free_list(struct ly_ctx *ctx, struct lys_node_list *list) |
| 3001 | { |
| 3002 | uint8_t i; |
| 3003 | |
| 3004 | yang_tpdf_free(ctx, list->tpdf, 0, list->tpdf_size); |
| 3005 | free(list->tpdf); |
| 3006 | |
| 3007 | for (i = 0; i < list->must_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3008 | lys_restr_free(ctx, &list->must[i], NULL); |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3009 | } |
| 3010 | free(list->must); |
| 3011 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3012 | lys_when_free(ctx, list->when, NULL); |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3013 | |
| 3014 | for (i = 0; i < list->unique_size; ++i) { |
| 3015 | free(list->unique[i].expr); |
| 3016 | } |
| 3017 | free(list->unique); |
| 3018 | |
| 3019 | free(list->keys); |
| 3020 | } |
| 3021 | |
| 3022 | static void |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3023 | yang_free_choice(struct ly_ctx *ctx, struct lys_node_choice *choice) |
| 3024 | { |
| 3025 | free(choice->dflt); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3026 | lys_when_free(ctx, choice->when, NULL); |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | static void |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 3030 | yang_free_anydata(struct ly_ctx *ctx, struct lys_node_anydata *anydata) |
| 3031 | { |
| 3032 | uint8_t i; |
| 3033 | |
| 3034 | for (i = 0; i < anydata->must_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3035 | lys_restr_free(ctx, &anydata->must[i], NULL); |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 3036 | } |
| 3037 | free(anydata->must); |
| 3038 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3039 | lys_when_free(ctx, anydata->when, NULL); |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | static void |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 3043 | yang_free_inout(struct ly_ctx *ctx, struct lys_node_inout *inout) |
| 3044 | { |
| 3045 | uint8_t i; |
| 3046 | |
| 3047 | yang_tpdf_free(ctx, inout->tpdf, 0, inout->tpdf_size); |
| 3048 | free(inout->tpdf); |
| 3049 | |
| 3050 | for (i = 0; i < inout->must_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3051 | lys_restr_free(ctx, &inout->must[i], NULL); |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 3052 | } |
| 3053 | free(inout->must); |
| 3054 | } |
| 3055 | |
| 3056 | static void |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3057 | yang_free_notif(struct ly_ctx *ctx, struct lys_node_notif *notif) |
| 3058 | { |
| 3059 | uint8_t i; |
| 3060 | |
| 3061 | yang_tpdf_free(ctx, notif->tpdf, 0, notif->tpdf_size); |
| 3062 | free(notif->tpdf); |
| 3063 | |
| 3064 | for (i = 0; i < notif->must_size; ++i) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3065 | lys_restr_free(ctx, ¬if->must[i], NULL); |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3066 | } |
| 3067 | free(notif->must); |
| 3068 | } |
| 3069 | |
| 3070 | static void |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3071 | yang_free_uses(struct ly_ctx *ctx, struct lys_node_uses *uses) |
| 3072 | { |
| 3073 | int i, j; |
| 3074 | |
| 3075 | for (i = 0; i < uses->refine_size; i++) { |
| 3076 | lydict_remove(ctx, uses->refine[i].target_name); |
| 3077 | lydict_remove(ctx, uses->refine[i].dsc); |
| 3078 | lydict_remove(ctx, uses->refine[i].ref); |
| 3079 | |
| 3080 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3081 | lys_restr_free(ctx, &uses->refine[i].must[j], NULL); |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3082 | } |
| 3083 | free(uses->refine[i].must); |
| 3084 | |
| 3085 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
| 3086 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
| 3087 | } |
| 3088 | free(uses->refine[i].dflt); |
| 3089 | |
| 3090 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
| 3091 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 3092 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3093 | lys_extension_instances_free(ctx, uses->refine[i].ext, uses->refine[i].ext_size, NULL); |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3094 | } |
| 3095 | free(uses->refine); |
| 3096 | |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3097 | lys_when_free(ctx, uses->when, NULL); |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3098 | } |
| 3099 | |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3100 | static void |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3101 | yang_free_nodes(struct ly_ctx *ctx, struct lys_node *node) |
| 3102 | { |
| 3103 | struct lys_node *tmp, *child, *sibling; |
| 3104 | |
| 3105 | if (!node) { |
| 3106 | return; |
| 3107 | } |
| 3108 | tmp = node; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3109 | |
| 3110 | while (tmp) { |
Pavol Vican | e87ff8d | 2016-11-24 18:25:01 +0100 | [diff] [blame] | 3111 | child = tmp->child; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3112 | sibling = tmp->next; |
| 3113 | /* common part */ |
| 3114 | lydict_remove(ctx, tmp->name); |
| 3115 | if (!(tmp->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 3116 | lys_iffeature_free(ctx, tmp->iffeature, tmp->iffeature_size, 0, NULL); |
Pavol Vican | e87ff8d | 2016-11-24 18:25:01 +0100 | [diff] [blame] | 3117 | lydict_remove(ctx, tmp->dsc); |
| 3118 | lydict_remove(ctx, tmp->ref); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | switch (tmp->nodetype) { |
| 3122 | case LYS_GROUPING: |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3123 | case LYS_RPC: |
| 3124 | case LYS_ACTION: |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3125 | yang_free_grouping(ctx, (struct lys_node_grp *)tmp); |
| 3126 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3127 | case LYS_CONTAINER: |
| 3128 | yang_free_container(ctx, (struct lys_node_container *)tmp); |
| 3129 | break; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3130 | case LYS_LEAF: |
| 3131 | yang_free_leaf(ctx, (struct lys_node_leaf *)tmp); |
| 3132 | break; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3133 | case LYS_LEAFLIST: |
| 3134 | yang_free_leaflist(ctx, (struct lys_node_leaflist *)tmp); |
| 3135 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3136 | case LYS_LIST: |
| 3137 | yang_free_list(ctx, (struct lys_node_list *)tmp); |
| 3138 | break; |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3139 | case LYS_CHOICE: |
| 3140 | yang_free_choice(ctx, (struct lys_node_choice *)tmp); |
| 3141 | break; |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 3142 | case LYS_CASE: |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3143 | lys_when_free(ctx, ((struct lys_node_case *)tmp)->when, NULL); |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 3144 | break; |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 3145 | case LYS_ANYXML: |
| 3146 | case LYS_ANYDATA: |
| 3147 | yang_free_anydata(ctx, (struct lys_node_anydata *)tmp); |
| 3148 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 3149 | case LYS_INPUT: |
| 3150 | case LYS_OUTPUT: |
| 3151 | yang_free_inout(ctx, (struct lys_node_inout *)tmp); |
| 3152 | break; |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3153 | case LYS_NOTIF: |
| 3154 | yang_free_notif(ctx, (struct lys_node_notif *)tmp); |
| 3155 | break; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3156 | case LYS_USES: |
| 3157 | yang_free_uses(ctx, (struct lys_node_uses *)tmp); |
| 3158 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3159 | default: |
| 3160 | break; |
| 3161 | } |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3162 | lys_extension_instances_free(ctx, tmp->ext, tmp->ext_size, NULL); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3163 | yang_free_nodes(ctx, child); |
| 3164 | free(tmp); |
| 3165 | tmp = sibling; |
| 3166 | } |
| 3167 | } |
| 3168 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3169 | static void |
| 3170 | yang_free_augment(struct ly_ctx *ctx, struct lys_node_augment *aug) |
| 3171 | { |
| 3172 | lydict_remove(ctx, aug->target_name); |
| 3173 | lydict_remove(ctx, aug->dsc); |
| 3174 | lydict_remove(ctx, aug->ref); |
| 3175 | |
Frank Rimpler | c4db1c7 | 2017-09-12 12:56:39 +0000 | [diff] [blame] | 3176 | lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, 0, NULL); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3177 | lys_when_free(ctx, aug->when, NULL); |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3178 | yang_free_nodes(ctx, aug->child); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3179 | lys_extension_instances_free(ctx, aug->ext, aug->ext_size, NULL); |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3180 | } |
| 3181 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3182 | static void |
| 3183 | yang_free_deviate(struct ly_ctx *ctx, struct lys_deviation *dev, uint index) |
| 3184 | { |
| 3185 | uint i, j; |
| 3186 | |
| 3187 | for (i = index; i < dev->deviate_size; ++i) { |
| 3188 | lydict_remove(ctx, dev->deviate[i].units); |
| 3189 | |
| 3190 | if (dev->deviate[i].type) { |
| 3191 | yang_type_free(ctx, dev->deviate[i].type); |
| 3192 | } |
| 3193 | |
| 3194 | for (j = 0; j < dev->deviate[i].dflt_size; ++j) { |
| 3195 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
| 3196 | } |
| 3197 | free(dev->deviate[i].dflt); |
| 3198 | |
| 3199 | for (j = 0; j < dev->deviate[i].must_size; ++j) { |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3200 | lys_restr_free(ctx, &dev->deviate[i].must[j], NULL); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3201 | } |
| 3202 | free(dev->deviate[i].must); |
| 3203 | |
| 3204 | for (j = 0; j < dev->deviate[i].unique_size; ++j) { |
| 3205 | free(dev->deviate[i].unique[j].expr); |
| 3206 | } |
| 3207 | free(dev->deviate[i].unique); |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 3208 | lys_extension_instances_free(ctx, dev->deviate[i].ext, dev->deviate[i].ext_size, NULL); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3209 | } |
| 3210 | } |
| 3211 | |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 3212 | void |
| 3213 | yang_free_ext_data(struct yang_ext_substmt *substmt) |
| 3214 | { |
| 3215 | int i; |
| 3216 | |
| 3217 | if (!substmt) { |
| 3218 | return; |
| 3219 | } |
| 3220 | |
| 3221 | free(substmt->ext_substmt); |
| 3222 | if (substmt->ext_modules) { |
| 3223 | for (i = 0; substmt->ext_modules[i]; ++i) { |
| 3224 | free(substmt->ext_modules[i]); |
| 3225 | } |
| 3226 | free(substmt->ext_modules); |
| 3227 | } |
| 3228 | free(substmt); |
| 3229 | } |
| 3230 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3231 | /* free common item from module and submodule */ |
| 3232 | static void |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3233 | free_yang_common(struct lys_module *module, struct lys_node *node) |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3234 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3235 | uint i; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3236 | yang_tpdf_free(module->ctx, module->tpdf, 0, module->tpdf_size); |
| 3237 | module->tpdf_size = 0; |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3238 | yang_free_ident_base(module->ident, 0, module->ident_size); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3239 | yang_free_nodes(module->ctx, node); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 3240 | for (i = 0; i < module->augment_size; ++i) { |
| 3241 | yang_free_augment(module->ctx, &module->augment[i]); |
| 3242 | } |
| 3243 | module->augment_size = 0; |
| 3244 | for (i = 0; i < module->deviation_size; ++i) { |
| 3245 | yang_free_deviate(module->ctx, &module->deviation[i], 0); |
| 3246 | free(module->deviation[i].deviate); |
| 3247 | } |
| 3248 | module->deviation_size = 0; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3249 | } |
| 3250 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3251 | /* check function*/ |
| 3252 | |
| 3253 | int |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 3254 | yang_check_ext_instance(struct lys_module *module, struct lys_ext_instance ***ext, uint size, |
| 3255 | void *parent, struct unres_schema *unres) |
| 3256 | { |
| 3257 | struct unres_ext *info; |
| 3258 | uint i; |
| 3259 | |
| 3260 | for (i = 0; i < size; ++i) { |
| 3261 | info = malloc(sizeof *info); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3262 | LY_CHECK_ERR_RETURN(!info, LOGMEM, EXIT_FAILURE); |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 3263 | info->data.yang = (*ext)[i]->parent; |
| 3264 | info->datatype = LYS_IN_YANG; |
| 3265 | info->parent = parent; |
| 3266 | info->mod = module; |
| 3267 | info->parent_type = (*ext)[i]->parent_type; |
Radek Krejci | febdad7 | 2017-02-06 11:35:51 +0100 | [diff] [blame] | 3268 | info->substmt = (*ext)[i]->insubstmt; |
| 3269 | info->substmt_index = (*ext)[i]->insubstmt_index; |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 3270 | info->ext_index = i; |
| 3271 | if (unres_schema_add_node(module, unres, ext, UNRES_EXT, (struct lys_node *)info) == -1) { |
| 3272 | return EXIT_FAILURE; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | return EXIT_SUCCESS; |
| 3277 | } |
| 3278 | |
| 3279 | int |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3280 | yang_check_imports(struct lys_module *module, struct unres_schema *unres) |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3281 | { |
| 3282 | struct lys_import *imp; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3283 | struct lys_include *inc; |
| 3284 | uint8_t imp_size, inc_size, j = 0, i = 0; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3285 | char *s; |
| 3286 | |
| 3287 | imp = module->imp; |
| 3288 | imp_size = module->imp_size; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3289 | inc = module->inc; |
| 3290 | inc_size = module->inc_size; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3291 | |
| 3292 | if (imp_size) { |
PavolVican | 5144fd8 | 2017-03-07 19:45:06 +0100 | [diff] [blame] | 3293 | module->imp = calloc(imp_size, sizeof *module->imp); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3294 | module->imp_size = 0; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3295 | LY_CHECK_ERR_GOTO(!module->imp, LOGMEM, error); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3296 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3297 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3298 | if (inc_size) { |
PavolVican | 5144fd8 | 2017-03-07 19:45:06 +0100 | [diff] [blame] | 3299 | module->inc = calloc(inc_size, sizeof *module->inc); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3300 | module->inc_size = 0; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 3301 | LY_CHECK_ERR_GOTO(!module->inc, LOGMEM, error); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | for (i = 0; i < imp_size; ++i) { |
| 3305 | s = (char *) imp[i].module; |
| 3306 | imp[i].module = NULL; |
PavolVican | 7d0b5ab | 2017-02-01 13:06:53 +0100 | [diff] [blame] | 3307 | if (yang_fill_import(module, &imp[i], &module->imp[module->imp_size], s, unres)) { |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3308 | ++i; |
| 3309 | goto error; |
| 3310 | } |
| 3311 | } |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3312 | for (j = 0; j < inc_size; ++j) { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3313 | s = (char *) inc[j].submodule; |
| 3314 | inc[j].submodule = NULL; |
| 3315 | if (yang_fill_include(module, s, &inc[j], unres)) { |
| 3316 | ++j; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3317 | goto error; |
| 3318 | } |
| 3319 | } |
| 3320 | free(inc); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3321 | free(imp); |
| 3322 | |
| 3323 | return EXIT_SUCCESS; |
| 3324 | |
| 3325 | error: |
| 3326 | yang_free_import(module->ctx, imp, i, imp_size); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3327 | yang_free_include(module->ctx, inc, j, inc_size); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3328 | free(imp); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3329 | free(inc); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3330 | return EXIT_FAILURE; |
| 3331 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3332 | |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3333 | static int |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3334 | 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] | 3335 | { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3336 | struct lys_iffeature *iffeature; |
| 3337 | uint8_t *ptr_size, size, i; |
| 3338 | char *s; |
| 3339 | int parent_is_feature = 0; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3340 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3341 | switch (type) { |
| 3342 | case FEATURE_KEYWORD: |
| 3343 | iffeature = ((struct lys_feature *)parent)->iffeature; |
| 3344 | size = ((struct lys_feature *)parent)->iffeature_size; |
| 3345 | ptr_size = &((struct lys_feature *)parent)->iffeature_size; |
| 3346 | parent_is_feature = 1; |
| 3347 | break; |
| 3348 | case IDENTITY_KEYWORD: |
| 3349 | iffeature = ((struct lys_ident *)parent)->iffeature; |
| 3350 | size = ((struct lys_ident *)parent)->iffeature_size; |
| 3351 | ptr_size = &((struct lys_ident *)parent)->iffeature_size; |
| 3352 | break; |
| 3353 | case ENUM_KEYWORD: |
| 3354 | iffeature = ((struct lys_type_enum *)ptr)->iffeature; |
| 3355 | size = ((struct lys_type_enum *)ptr)->iffeature_size; |
| 3356 | ptr_size = &((struct lys_type_enum *)ptr)->iffeature_size; |
| 3357 | break; |
| 3358 | case BIT_KEYWORD: |
| 3359 | iffeature = ((struct lys_type_bit *)ptr)->iffeature; |
| 3360 | size = ((struct lys_type_bit *)ptr)->iffeature_size; |
| 3361 | ptr_size = &((struct lys_type_bit *)ptr)->iffeature_size; |
| 3362 | break; |
| 3363 | case REFINE_KEYWORD: |
| 3364 | iffeature = ((struct lys_refine *)ptr)->iffeature; |
| 3365 | size = ((struct lys_refine *)ptr)->iffeature_size; |
| 3366 | ptr_size = &((struct lys_refine *)ptr)->iffeature_size; |
| 3367 | break; |
| 3368 | default: |
| 3369 | iffeature = ((struct lys_node *)parent)->iffeature; |
| 3370 | size = ((struct lys_node *)parent)->iffeature_size; |
| 3371 | ptr_size = &((struct lys_node *)parent)->iffeature_size; |
| 3372 | break; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3373 | } |
| 3374 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3375 | *ptr_size = 0; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3376 | for (i = 0; i < size; ++i) { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3377 | s = (char *)iffeature[i].features; |
| 3378 | iffeature[i].features = NULL; |
| 3379 | if (yang_fill_iffeature(module, &iffeature[i], parent, s, unres, parent_is_feature)) { |
| 3380 | *ptr_size = size; |
| 3381 | return EXIT_FAILURE; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3382 | } |
PavolVican | 8fa3124 | 2017-02-07 11:04:26 +0100 | [diff] [blame] | 3383 | if (yang_check_ext_instance(module, &iffeature[i].ext, iffeature[i].ext_size, &iffeature[i], unres)) { |
| 3384 | *ptr_size = size; |
| 3385 | return EXIT_FAILURE; |
| 3386 | } |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3387 | (*ptr_size)++; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | return EXIT_SUCCESS; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3391 | } |
| 3392 | |
Pavol Vican | b60a1d1 | 2016-12-03 01:56:55 +0100 | [diff] [blame] | 3393 | static int |
| 3394 | yang_check_identityref(struct lys_module *module, struct lys_type *type, struct unres_schema *unres) |
| 3395 | { |
| 3396 | uint size, i; |
| 3397 | int rc; |
| 3398 | struct lys_ident **ref; |
| 3399 | const char *value; |
| 3400 | char *expr; |
| 3401 | |
| 3402 | ref = type->info.ident.ref; |
| 3403 | size = type->info.ident.count; |
| 3404 | type->info.ident.count = 0; |
| 3405 | type->info.ident.ref = NULL; |
| 3406 | ((struct yang_type *)type->der)->flags |= LYS_NO_ERASE_IDENTITY; |
| 3407 | |
| 3408 | for (i = 0; i < size; ++i) { |
| 3409 | expr = (char *)ref[i]; |
| 3410 | /* store in the JSON format */ |
| 3411 | value = transform_schema2json(module, expr); |
| 3412 | free(expr); |
| 3413 | |
| 3414 | if (!value) { |
| 3415 | goto error; |
| 3416 | } |
| 3417 | rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value); |
| 3418 | lydict_remove(module->ctx, value); |
| 3419 | |
| 3420 | if (rc == -1) { |
| 3421 | goto error; |
| 3422 | } |
| 3423 | } |
| 3424 | free(ref); |
| 3425 | |
| 3426 | return EXIT_SUCCESS; |
| 3427 | error: |
| 3428 | for (i = i+1; i < size; ++i) { |
| 3429 | free(ref[i]); |
| 3430 | } |
| 3431 | free(ref); |
| 3432 | return EXIT_FAILURE; |
| 3433 | } |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3434 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3435 | int |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 3436 | yang_fill_type(struct lys_module *module, struct lys_type *type, struct yang_type *stype, |
| 3437 | void *parent, struct unres_schema *unres) |
| 3438 | { |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 3439 | unsigned int i; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 3440 | |
| 3441 | type->parent = parent; |
| 3442 | if (yang_check_ext_instance(module, &type->ext, type->ext_size, type, unres)) { |
| 3443 | return EXIT_FAILURE; |
| 3444 | } |
| 3445 | switch (stype->base) { |
| 3446 | case LY_TYPE_ENUM: |
| 3447 | for (i = 0; i < type->info.enums.count; ++i) { |
| 3448 | if (yang_check_iffeatures(module, &type->info.enums.enm[i], parent, ENUM_KEYWORD, unres)) { |
| 3449 | return EXIT_FAILURE; |
| 3450 | } |
| 3451 | if (yang_check_ext_instance(module, &type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size, |
| 3452 | &type->info.enums.enm[i], unres)) { |
| 3453 | return EXIT_FAILURE; |
| 3454 | } |
| 3455 | } |
| 3456 | break; |
| 3457 | case LY_TYPE_BITS: |
| 3458 | for (i = 0; i < type->info.bits.count; ++i) { |
| 3459 | if (yang_check_iffeatures(module, &type->info.bits.bit[i], parent, BIT_KEYWORD, unres)) { |
| 3460 | return EXIT_FAILURE; |
| 3461 | } |
| 3462 | if (yang_check_ext_instance(module, &type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size, |
| 3463 | &type->info.bits.bit[i], unres)) { |
| 3464 | return EXIT_FAILURE; |
| 3465 | } |
| 3466 | } |
| 3467 | break; |
| 3468 | case LY_TYPE_IDENT: |
| 3469 | if (yang_check_identityref(module, type, unres)) { |
| 3470 | return EXIT_FAILURE; |
| 3471 | } |
| 3472 | break; |
| 3473 | case LY_TYPE_STRING: |
| 3474 | if (type->info.str.length && yang_check_ext_instance(module, &type->info.str.length->ext, |
| 3475 | type->info.str.length->ext_size, type->info.str.length, unres)) { |
| 3476 | return EXIT_FAILURE; |
| 3477 | } |
| 3478 | for (i = 0; i < type->info.str.pat_count; ++i) { |
| 3479 | if (yang_check_ext_instance(module, &type->info.str.patterns[i].ext, type->info.str.patterns[i].ext_size, |
| 3480 | &type->info.str.patterns[i], unres)) { |
| 3481 | return EXIT_FAILURE; |
| 3482 | } |
| 3483 | } |
| 3484 | break; |
| 3485 | case LY_TYPE_DEC64: |
| 3486 | if (type->info.dec64.range && yang_check_ext_instance(module, &type->info.dec64.range->ext, |
| 3487 | type->info.dec64.range->ext_size, type->info.dec64.range, unres)) { |
| 3488 | return EXIT_FAILURE; |
| 3489 | } |
| 3490 | break; |
| 3491 | case LY_TYPE_UNION: |
| 3492 | for (i = 0; i < type->info.uni.count; ++i) { |
| 3493 | if (yang_fill_type(module, &type->info.uni.types[i], (struct yang_type *)type->info.uni.types[i].der, |
| 3494 | parent, unres)) { |
| 3495 | return EXIT_FAILURE; |
| 3496 | } |
| 3497 | } |
Radek Krejci | 57b4168 | 2017-03-07 12:33:24 +0100 | [diff] [blame] | 3498 | break; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 3499 | default: |
| 3500 | /* nothing checks */ |
| 3501 | break; |
| 3502 | } |
| 3503 | return EXIT_SUCCESS; |
| 3504 | } |
| 3505 | |
| 3506 | int |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3507 | yang_check_typedef(struct lys_module *module, struct lys_node *parent, struct unres_schema *unres) |
| 3508 | { |
| 3509 | struct lys_tpdf *tpdf; |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 3510 | uint8_t i, tpdf_size, *ptr_tpdf_size; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3511 | |
| 3512 | if (!parent) { |
| 3513 | tpdf = module->tpdf; |
| 3514 | ptr_tpdf_size = &module->tpdf_size; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3515 | } else { |
| 3516 | switch (parent->nodetype) { |
| 3517 | case LYS_GROUPING: |
| 3518 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
| 3519 | ptr_tpdf_size = &((struct lys_node_grp *)parent)->tpdf_size; |
| 3520 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3521 | case LYS_CONTAINER: |
| 3522 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
| 3523 | ptr_tpdf_size = &((struct lys_node_container *)parent)->tpdf_size; |
| 3524 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3525 | case LYS_LIST: |
| 3526 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
| 3527 | ptr_tpdf_size = &((struct lys_node_list *)parent)->tpdf_size; |
| 3528 | break; |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3529 | case LYS_RPC: |
| 3530 | case LYS_ACTION: |
| 3531 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
| 3532 | ptr_tpdf_size = &((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3533 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 3534 | case LYS_INPUT: |
| 3535 | case LYS_OUTPUT: |
| 3536 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
| 3537 | ptr_tpdf_size = &((struct lys_node_inout *)parent)->tpdf_size; |
| 3538 | break; |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3539 | case LYS_NOTIF: |
| 3540 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
| 3541 | ptr_tpdf_size = &((struct lys_node_notif *)parent)->tpdf_size; |
| 3542 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3543 | default: |
| 3544 | LOGINT; |
| 3545 | return EXIT_FAILURE; |
| 3546 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | tpdf_size = *ptr_tpdf_size; |
| 3550 | *ptr_tpdf_size = 0; |
| 3551 | |
| 3552 | for (i = 0; i < tpdf_size; ++i) { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3553 | if (lyp_check_identifier(tpdf[i].name, LY_IDENT_TYPE, module, parent)) { |
| 3554 | goto error; |
| 3555 | } |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3556 | |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 3557 | if (yang_fill_type(module, &tpdf[i].type, (struct yang_type *)tpdf[i].type.der, &tpdf[i], unres)) { |
| 3558 | goto error; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3559 | } |
PavolVican | df9e797 | 2017-02-07 11:41:38 +0100 | [diff] [blame] | 3560 | if (yang_check_ext_instance(module, &tpdf[i].ext, tpdf[i].ext_size, &tpdf[i], unres)) { |
| 3561 | goto error; |
| 3562 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3563 | 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] | 3564 | goto error; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3565 | } |
| 3566 | |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3567 | (*ptr_tpdf_size)++; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3568 | /* check default value*/ |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3569 | if (unres_schema_add_node(module, unres, &tpdf[i].type, UNRES_TYPEDEF_DFLT, |
| 3570 | (struct lys_node *)(&tpdf[i].dflt)) == -1) { |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3571 | ++i; |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3572 | goto error; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3573 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3574 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3575 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3576 | return EXIT_SUCCESS; |
| 3577 | |
| 3578 | error: |
| 3579 | yang_tpdf_free(module->ctx, tpdf, i, tpdf_size); |
| 3580 | return EXIT_FAILURE; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | static int |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3584 | yang_check_identities(struct lys_module *module, struct unres_schema *unres) |
| 3585 | { |
| 3586 | uint32_t i, size, base_size; |
| 3587 | uint8_t j; |
| 3588 | |
| 3589 | size = module->ident_size; |
| 3590 | module->ident_size = 0; |
| 3591 | for (i = 0; i < size; ++i) { |
| 3592 | base_size = module->ident[i].base_size; |
| 3593 | module->ident[i].base_size = 0; |
| 3594 | for (j = 0; j < base_size; ++j) { |
| 3595 | if (yang_read_base(module, &module->ident[i], (char *)module->ident[i].base[j], unres)) { |
| 3596 | ++j; |
| 3597 | module->ident_size = size; |
| 3598 | goto error; |
| 3599 | } |
| 3600 | } |
| 3601 | module->ident_size++; |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3602 | if (yang_check_iffeatures(module, NULL, &module->ident[i], IDENTITY_KEYWORD, unres)) { |
| 3603 | goto error; |
| 3604 | } |
PavolVican | 8fa3124 | 2017-02-07 11:04:26 +0100 | [diff] [blame] | 3605 | if (yang_check_ext_instance(module, &module->ident[i].ext, module->ident[i].ext_size, &module->ident[i], unres)) { |
| 3606 | goto error; |
| 3607 | } |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3608 | } |
| 3609 | |
PavolVican | 8fa3124 | 2017-02-07 11:04:26 +0100 | [diff] [blame] | 3610 | |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3611 | return EXIT_SUCCESS; |
| 3612 | |
| 3613 | error: |
| 3614 | for (; j< module->ident[i].base_size; ++j) { |
| 3615 | free(module->ident[i].base[j]); |
| 3616 | } |
| 3617 | yang_free_ident_base(module->ident, i + 1, size); |
| 3618 | return EXIT_FAILURE; |
| 3619 | } |
| 3620 | |
| 3621 | static int |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 3622 | yang_check_must(struct lys_module *module, struct lys_restr *must, uint size, struct unres_schema *unres) |
| 3623 | { |
| 3624 | uint i; |
| 3625 | |
| 3626 | for (i = 0; i < size; ++i) { |
| 3627 | if (yang_check_ext_instance(module, &must[i].ext, must[i].ext_size, &must[i], unres)) { |
| 3628 | return EXIT_FAILURE; |
| 3629 | } |
| 3630 | } |
| 3631 | return EXIT_SUCCESS; |
| 3632 | } |
| 3633 | |
| 3634 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3635 | yang_check_container(struct lys_module *module, struct lys_node_container *cont, struct lys_node **child, |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3636 | int options, struct unres_schema *unres) |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3637 | { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3638 | if (yang_check_typedef(module, (struct lys_node *)cont, unres)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3639 | goto error; |
| 3640 | } |
| 3641 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3642 | if (yang_check_iffeatures(module, NULL, cont, CONTAINER_KEYWORD, unres)) { |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3643 | goto error; |
| 3644 | } |
| 3645 | |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3646 | if (yang_check_nodes(module, (struct lys_node *)cont, *child, options, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3647 | *child = NULL; |
| 3648 | goto error; |
| 3649 | } |
| 3650 | *child = NULL; |
| 3651 | |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 3652 | if (cont->when && yang_check_ext_instance(module, &cont->when->ext, cont->when->ext_size, cont->when, unres)) { |
| 3653 | goto error; |
| 3654 | } |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 3655 | if (yang_check_must(module, cont->must, cont->must_size, unres)) { |
| 3656 | goto error; |
| 3657 | } |
| 3658 | |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3659 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3660 | if (cont->when || cont->must_size) { |
| 3661 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 3662 | if (lyxp_node_check_syntax((struct lys_node *)cont)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3663 | goto error; |
| 3664 | } |
| 3665 | } else { |
| 3666 | if (unres_schema_add_node(module, unres, cont, UNRES_XPATH, NULL) == -1) { |
| 3667 | goto error; |
| 3668 | } |
| 3669 | } |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | return EXIT_SUCCESS; |
| 3673 | error: |
| 3674 | return EXIT_FAILURE; |
| 3675 | } |
| 3676 | |
| 3677 | static int |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3678 | yang_check_leaf(struct lys_module *module, struct lys_node_leaf *leaf, int options, struct unres_schema *unres) |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3679 | { |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 3680 | if (yang_fill_type(module, &leaf->type, (struct yang_type *)leaf->type.der, leaf, unres)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3681 | yang_type_free(module->ctx, &leaf->type); |
| 3682 | goto error; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3683 | } |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3684 | if (yang_check_iffeatures(module, NULL, leaf, LEAF_KEYWORD, unres)) { |
| 3685 | yang_type_free(module->ctx, &leaf->type); |
| 3686 | goto error; |
| 3687 | } |
| 3688 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3689 | 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] | 3690 | yang_type_free(module->ctx, &leaf->type); |
| 3691 | goto error; |
| 3692 | } |
| 3693 | |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3694 | if (!(options & LYS_PARSE_OPT_INGRP) && |
| 3695 | (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] | 3696 | goto error; |
| 3697 | } |
| 3698 | |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 3699 | if (leaf->when && yang_check_ext_instance(module, &leaf->when->ext, leaf->when->ext_size, leaf->when, unres)) { |
| 3700 | goto error; |
| 3701 | } |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 3702 | if (yang_check_must(module, leaf->must, leaf->must_size, unres)) { |
| 3703 | goto error; |
| 3704 | } |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3705 | |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3706 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3707 | if (leaf->when || leaf->must_size) { |
| 3708 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 3709 | if (lyxp_node_check_syntax((struct lys_node *)leaf)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3710 | goto error; |
| 3711 | } |
| 3712 | } else { |
| 3713 | if (unres_schema_add_node(module, unres, leaf, UNRES_XPATH, NULL) == -1) { |
| 3714 | goto error; |
| 3715 | } |
| 3716 | } |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | return EXIT_SUCCESS; |
| 3720 | error: |
| 3721 | return EXIT_FAILURE; |
| 3722 | } |
| 3723 | |
| 3724 | static int |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3725 | yang_check_leaflist(struct lys_module *module, struct lys_node_leaflist *leaflist, int options, |
| 3726 | struct unres_schema *unres) |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3727 | { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3728 | int i, j; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3729 | |
PavolVican | 056fcd1 | 2017-02-07 15:36:53 +0100 | [diff] [blame] | 3730 | if (yang_fill_type(module, &leaflist->type, (struct yang_type *)leaflist->type.der, leaflist, unres)) { |
PavolVican | 6a2148f | 2017-02-07 23:09:55 +0100 | [diff] [blame] | 3731 | yang_type_free(module->ctx, &leaflist->type); |
| 3732 | goto error; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3733 | } |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3734 | if (yang_check_iffeatures(module, NULL, leaflist, LEAF_LIST_KEYWORD, unres)) { |
| 3735 | yang_type_free(module->ctx, &leaflist->type); |
| 3736 | goto error; |
| 3737 | } |
| 3738 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3739 | 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] | 3740 | yang_type_free(module->ctx, &leaflist->type); |
| 3741 | goto error; |
| 3742 | } |
| 3743 | |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3744 | for (i = 0; i < leaflist->dflt_size; ++i) { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3745 | /* check for duplicity in case of configuration data, |
| 3746 | * in case of status data duplicities are allowed */ |
| 3747 | if (leaflist->flags & LYS_CONFIG_W) { |
| 3748 | for (j = i +1; j < leaflist->dflt_size; ++j) { |
| 3749 | if (ly_strequal(leaflist->dflt[i], leaflist->dflt[j], 1)) { |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 3750 | LOGVAL(LYE_INARG, LY_VLOG_LYS, leaflist, leaflist->dflt[i], "default"); |
| 3751 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leaflist, "Duplicated default value \"%s\".", leaflist->dflt[i]); |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3752 | goto error; |
| 3753 | } |
| 3754 | } |
| 3755 | } |
| 3756 | /* check default value (if not defined, there still could be some restrictions |
| 3757 | * that need to be checked against a default value from a derived type) */ |
Radek Krejci | ab08f0f | 2017-03-09 16:37:15 +0100 | [diff] [blame] | 3758 | if (!(options & LYS_PARSE_OPT_INGRP) && |
| 3759 | (unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DFLT, |
| 3760 | (struct lys_node *)(&leaflist->dflt[i])) == -1)) { |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3761 | goto error; |
| 3762 | } |
| 3763 | } |
| 3764 | |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 3765 | if (leaflist->when && yang_check_ext_instance(module, &leaflist->when->ext, leaflist->when->ext_size, leaflist->when, unres)) { |
| 3766 | goto error; |
| 3767 | } |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 3768 | if (yang_check_must(module, leaflist->must, leaflist->must_size, unres)) { |
| 3769 | goto error; |
| 3770 | } |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 3771 | |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3772 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3773 | if (leaflist->when || leaflist->must_size) { |
| 3774 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 3775 | if (lyxp_node_check_syntax((struct lys_node *)leaflist)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3776 | goto error; |
| 3777 | } |
| 3778 | } else { |
| 3779 | if (unres_schema_add_node(module, unres, leaflist, UNRES_XPATH, NULL) == -1) { |
| 3780 | goto error; |
| 3781 | } |
| 3782 | } |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | return EXIT_SUCCESS; |
| 3786 | error: |
| 3787 | return EXIT_FAILURE; |
| 3788 | } |
| 3789 | |
| 3790 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3791 | yang_check_list(struct lys_module *module, struct lys_node_list *list, struct lys_node **child, |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3792 | int options, struct unres_schema *unres) |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3793 | { |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3794 | struct lys_node *node; |
| 3795 | |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3796 | if (yang_check_typedef(module, (struct lys_node *)list, unres)) { |
| 3797 | goto error; |
| 3798 | } |
| 3799 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3800 | if (yang_check_iffeatures(module, NULL, list, LIST_KEYWORD, unres)) { |
| 3801 | goto error; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3802 | } |
| 3803 | |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3804 | if (list->flags & LYS_CONFIG_R) { |
| 3805 | /* RFC 6020, 7.7.5 - ignore ordering when the list represents state data |
| 3806 | * ignore oredering MASK - 0x7F |
| 3807 | */ |
| 3808 | list->flags &= 0x7F; |
| 3809 | } |
| 3810 | /* check - if list is configuration, key statement is mandatory |
| 3811 | * (but only if we are not in a grouping or augment, then the check is deferred) */ |
PavolVican | c68dfea | 2017-02-21 15:40:43 +0100 | [diff] [blame] | 3812 | for (node = (struct lys_node *)list; node && !(node->nodetype & (LYS_GROUPING | LYS_AUGMENT | LYS_EXT)); node = node->parent); |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 3813 | if (!node && (list->flags & LYS_CONFIG_W) && !list->keys) { |
| 3814 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, list, "key", "list"); |
| 3815 | goto error; |
| 3816 | } |
| 3817 | |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3818 | if (yang_check_nodes(module, (struct lys_node *)list, *child, options, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3819 | *child = NULL; |
| 3820 | goto error; |
| 3821 | } |
| 3822 | *child = NULL; |
| 3823 | |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3824 | if (list->keys && yang_read_key(module, list, unres)) { |
| 3825 | goto error; |
| 3826 | } |
| 3827 | |
| 3828 | if (yang_read_unique(module, list, unres)) { |
| 3829 | goto error; |
| 3830 | } |
| 3831 | |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 3832 | if (list->when && yang_check_ext_instance(module, &list->when->ext, list->when->ext_size, list->when, unres)) { |
| 3833 | goto error; |
| 3834 | } |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 3835 | if (yang_check_must(module, list->must, list->must_size, unres)) { |
| 3836 | goto error; |
| 3837 | } |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3838 | |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3839 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3840 | if (list->when || list->must_size) { |
| 3841 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 3842 | if (lyxp_node_check_syntax((struct lys_node *)list)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3843 | goto error; |
| 3844 | } |
| 3845 | } else { |
| 3846 | if (unres_schema_add_node(module, unres, list, UNRES_XPATH, NULL) == -1) { |
| 3847 | goto error; |
| 3848 | } |
| 3849 | } |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | return EXIT_SUCCESS; |
| 3853 | error: |
| 3854 | return EXIT_FAILURE; |
| 3855 | } |
| 3856 | |
| 3857 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3858 | yang_check_choice(struct lys_module *module, struct lys_node_choice *choice, struct lys_node **child, |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3859 | int options, struct unres_schema *unres) |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3860 | { |
| 3861 | char *value; |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3862 | |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3863 | if (yang_check_iffeatures(module, NULL, choice, CHOICE_KEYWORD, unres)) { |
| 3864 | free(choice->dflt); |
| 3865 | choice->dflt = NULL; |
| 3866 | goto error; |
| 3867 | } |
| 3868 | |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3869 | if (yang_check_nodes(module, (struct lys_node *)choice, *child, options, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3870 | *child = NULL; |
| 3871 | free(choice->dflt); |
| 3872 | choice->dflt = NULL; |
| 3873 | goto error; |
| 3874 | } |
| 3875 | *child = NULL; |
| 3876 | |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3877 | if (choice->dflt) { |
| 3878 | value = (char *)choice->dflt; |
| 3879 | choice->dflt = NULL; |
| 3880 | if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, value) == -1) { |
| 3881 | free(value); |
| 3882 | goto error; |
| 3883 | } |
| 3884 | free(value); |
| 3885 | } |
| 3886 | |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 3887 | if (choice->when && yang_check_ext_instance(module, &choice->when->ext, choice->when->ext_size, choice->when, unres)) { |
| 3888 | goto error; |
| 3889 | } |
| 3890 | |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3891 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3892 | if (choice->when) { |
| 3893 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 3894 | if (lyxp_node_check_syntax((struct lys_node *)choice)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3895 | goto error; |
| 3896 | } |
| 3897 | } else { |
| 3898 | if (unres_schema_add_node(module, unres, choice, UNRES_XPATH, NULL) == -1) { |
| 3899 | goto error; |
| 3900 | } |
| 3901 | } |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | return EXIT_SUCCESS; |
| 3905 | error: |
| 3906 | return EXIT_FAILURE; |
| 3907 | } |
| 3908 | |
| 3909 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3910 | yang_check_rpc_action(struct lys_module *module, struct lys_node_rpc_action *rpc, struct lys_node **child, |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3911 | int options, struct unres_schema *unres) |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3912 | { |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3913 | struct lys_node *node; |
| 3914 | |
| 3915 | if (rpc->nodetype == LYS_ACTION) { |
| 3916 | for (node = rpc->parent; node; node = lys_parent(node)) { |
| 3917 | if ((node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
Pavol Vican | 73ff803 | 2016-12-04 15:03:51 +0100 | [diff] [blame] | 3918 | || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys)) { |
PavolVican | 196694c | 2017-01-27 10:33:09 +0100 | [diff] [blame] | 3919 | LOGVAL(LYE_INPAR, LY_VLOG_LYS, rpc->parent, strnodetype(node->nodetype), "action"); |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3920 | goto error; |
| 3921 | } |
| 3922 | } |
| 3923 | } |
| 3924 | if (yang_check_typedef(module, (struct lys_node *)rpc, unres)) { |
| 3925 | goto error; |
| 3926 | } |
| 3927 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3928 | if (yang_check_iffeatures(module, NULL, rpc, RPC_KEYWORD, unres)) { |
| 3929 | goto error; |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3930 | } |
| 3931 | |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3932 | if (yang_check_nodes(module, (struct lys_node *)rpc, *child, options | LYS_PARSE_OPT_CFG_IGNORE, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3933 | *child = NULL; |
| 3934 | goto error; |
| 3935 | } |
| 3936 | *child = NULL; |
| 3937 | |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3938 | return EXIT_SUCCESS; |
| 3939 | error: |
| 3940 | return EXIT_FAILURE; |
| 3941 | } |
| 3942 | |
| 3943 | static int |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3944 | yang_check_notif(struct lys_module *module, struct lys_node_notif *notif, struct lys_node **child, |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3945 | int options, struct unres_schema *unres) |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3946 | { |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3947 | if (yang_check_typedef(module, (struct lys_node *)notif, unres)) { |
| 3948 | goto error; |
| 3949 | } |
| 3950 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 3951 | if (yang_check_iffeatures(module, NULL, notif, NOTIFICATION_KEYWORD, unres)) { |
| 3952 | goto error; |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3953 | } |
| 3954 | |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3955 | if (yang_check_nodes(module, (struct lys_node *)notif, *child, options | LYS_PARSE_OPT_CFG_IGNORE, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 3956 | *child = NULL; |
| 3957 | goto error; |
| 3958 | } |
| 3959 | *child = NULL; |
| 3960 | |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3961 | if (yang_check_must(module, notif->must, notif->must_size, unres)) { |
| 3962 | goto error; |
| 3963 | } |
| 3964 | |
| 3965 | /* check XPath dependencies */ |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 3966 | if (notif->must_size) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3967 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 3968 | if (lyxp_node_check_syntax((struct lys_node *)notif)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 3969 | goto error; |
| 3970 | } |
| 3971 | } else { |
| 3972 | if (unres_schema_add_node(module, unres, notif, UNRES_XPATH, NULL) == -1) { |
| 3973 | goto error; |
| 3974 | } |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 3975 | } |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | return EXIT_SUCCESS; |
| 3979 | error: |
| 3980 | return EXIT_FAILURE; |
| 3981 | } |
| 3982 | |
| 3983 | static int |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3984 | yang_check_augment(struct lys_module *module, struct lys_node_augment *augment, int options, struct unres_schema *unres) |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3985 | { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3986 | struct lys_node *child; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 3987 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3988 | child = augment->child; |
| 3989 | augment->child = NULL; |
| 3990 | |
PavolVican | a08d365 | 2016-12-29 21:07:47 +0100 | [diff] [blame] | 3991 | if (yang_check_iffeatures(module, NULL, augment, AUGMENT_KEYWORD, unres)) { |
| 3992 | yang_free_nodes(module->ctx, child); |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3993 | goto error; |
| 3994 | } |
| 3995 | |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 3996 | if (yang_check_nodes(module, (struct lys_node *)augment, child, options, unres)) { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 3997 | goto error; |
| 3998 | } |
| 3999 | |
PavolVican | fa9510e | 2017-02-08 17:20:46 +0100 | [diff] [blame] | 4000 | if (yang_check_ext_instance(module, &augment->ext, augment->ext_size, augment, unres)) { |
| 4001 | goto error; |
| 4002 | } |
| 4003 | |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 4004 | if (augment->when && yang_check_ext_instance(module, &augment->when->ext, augment->when->ext_size, augment->when, unres)) { |
| 4005 | goto error; |
| 4006 | } |
| 4007 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4008 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4009 | if (augment->when) { |
| 4010 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 4011 | if (lyxp_node_check_syntax((struct lys_node *)augment)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4012 | goto error; |
| 4013 | } |
| 4014 | } else { |
| 4015 | if (unres_schema_add_node(module, unres, augment, UNRES_XPATH, NULL) == -1) { |
| 4016 | goto error; |
| 4017 | } |
| 4018 | } |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | return EXIT_SUCCESS; |
| 4022 | error: |
| 4023 | return EXIT_FAILURE; |
| 4024 | } |
| 4025 | |
| 4026 | static int |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4027 | yang_check_uses(struct lys_module *module, struct lys_node_uses *uses, int options, struct unres_schema *unres) |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4028 | { |
| 4029 | uint i, size; |
| 4030 | |
| 4031 | size = uses->augment_size; |
| 4032 | uses->augment_size = 0; |
| 4033 | |
| 4034 | if (yang_check_iffeatures(module, NULL, uses, USES_KEYWORD, unres)) { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 4035 | goto error; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 4036 | } |
| 4037 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 4038 | for (i = 0; i < uses->refine_size; ++i) { |
PavolVican | 77374ee | 2017-02-08 15:18:45 +0100 | [diff] [blame] | 4039 | if (yang_check_iffeatures(module, &uses->refine[i], uses, REFINE_KEYWORD, unres)) { |
| 4040 | goto error; |
| 4041 | } |
| 4042 | if (yang_check_must(module, uses->refine[i].must, uses->refine[i].must_size, unres)) { |
| 4043 | goto error; |
| 4044 | } |
| 4045 | if (yang_check_ext_instance(module, &uses->refine[i].ext, uses->refine[i].ext_size, &uses->refine[i], unres)) { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 4046 | goto error; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 4047 | } |
| 4048 | } |
| 4049 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4050 | for (i = 0; i < size; ++i) { |
| 4051 | uses->augment_size++; |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4052 | if (yang_check_augment(module, &uses->augment[i], options, unres)) { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4053 | goto error; |
| 4054 | } |
| 4055 | } |
| 4056 | |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 4057 | if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) { |
| 4058 | goto error; |
| 4059 | } |
| 4060 | |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 4061 | if (uses->when && yang_check_ext_instance(module, &uses->when->ext, uses->when->ext_size, uses->when, unres)) { |
| 4062 | goto error; |
| 4063 | } |
| 4064 | |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 4065 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4066 | if (uses->when) { |
| 4067 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 4068 | if (lyxp_node_check_syntax((struct lys_node *)uses)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4069 | goto error; |
| 4070 | } |
| 4071 | } else { |
| 4072 | if (unres_schema_add_node(module, unres, uses, UNRES_XPATH, NULL) == -1) { |
| 4073 | goto error; |
| 4074 | } |
| 4075 | } |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | return EXIT_SUCCESS; |
| 4079 | error: |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4080 | for (i = uses->augment_size; i < size; ++i) { |
| 4081 | yang_free_augment(module->ctx, &uses->augment[i]); |
| 4082 | } |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 4083 | return EXIT_FAILURE; |
| 4084 | } |
| 4085 | |
| 4086 | static int |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 4087 | yang_check_anydata(struct lys_module *module, struct lys_node_anydata *anydata, struct lys_node **child, |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4088 | int options, struct unres_schema *unres) |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 4089 | { |
| 4090 | if (yang_check_iffeatures(module, NULL, anydata, ANYDATA_KEYWORD, unres)) { |
| 4091 | goto error; |
| 4092 | } |
| 4093 | |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4094 | if (yang_check_nodes(module, (struct lys_node *)anydata, *child, options, unres)) { |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 4095 | *child = NULL; |
| 4096 | goto error; |
| 4097 | } |
| 4098 | *child = NULL; |
| 4099 | |
| 4100 | if (anydata->when && yang_check_ext_instance(module, &anydata->when->ext, anydata->when->ext_size, anydata->when, unres)) { |
| 4101 | goto error; |
| 4102 | } |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 4103 | if (yang_check_must(module, anydata->must, anydata->must_size, unres)) { |
| 4104 | goto error; |
| 4105 | } |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 4106 | |
| 4107 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4108 | if (anydata->when || anydata->must_size) { |
| 4109 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 4110 | if (lyxp_node_check_syntax((struct lys_node *)anydata)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4111 | goto error; |
| 4112 | } |
| 4113 | } else { |
| 4114 | if (unres_schema_add_node(module, unres, anydata, UNRES_XPATH, NULL) == -1) { |
| 4115 | goto error; |
| 4116 | } |
| 4117 | } |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 4118 | } |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4119 | |
PavolVican | 59ba460 | 2017-02-08 11:53:32 +0100 | [diff] [blame] | 4120 | return EXIT_SUCCESS; |
| 4121 | error: |
| 4122 | return EXIT_FAILURE; |
| 4123 | } |
| 4124 | |
| 4125 | static int |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4126 | yang_check_nodes(struct lys_module *module, struct lys_node *parent, struct lys_node *nodes, |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4127 | int options, struct unres_schema *unres) |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4128 | { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4129 | struct lys_node *node = nodes, *sibling, *child; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4130 | |
| 4131 | while (node) { |
| 4132 | sibling = node->next; |
| 4133 | child = node->child; |
| 4134 | node->next = NULL; |
| 4135 | node->child = NULL; |
| 4136 | node->prev = node; |
| 4137 | |
PavolVican | 05c4f9b | 2017-09-07 13:33:54 +0200 | [diff] [blame] | 4138 | if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, node) || |
| 4139 | check_status_flag(node, parent)) { |
Pavol Vican | 24ba7f6 | 2016-11-28 12:15:20 +0100 | [diff] [blame] | 4140 | lys_node_unlink(node); |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 4141 | yang_free_nodes(module->ctx, node); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4142 | goto error; |
| 4143 | } |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4144 | store_config_flag(node, options); |
PavolVican | 70ce745 | 2017-02-01 15:39:39 +0100 | [diff] [blame] | 4145 | if (yang_check_ext_instance(module, &node->ext, node->ext_size, node, unres)) { |
| 4146 | goto error; |
| 4147 | } |
Pavol Vican | fda8c80 | 2016-12-03 02:00:42 +0100 | [diff] [blame] | 4148 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4149 | switch (node->nodetype) { |
| 4150 | case LYS_GROUPING: |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 4151 | if (yang_check_typedef(module, node, unres)) { |
| 4152 | goto error; |
| 4153 | } |
| 4154 | if (yang_check_iffeatures(module, NULL, node, GROUPING_KEYWORD, unres)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4155 | goto error; |
| 4156 | } |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4157 | if (yang_check_nodes(module, node, child, options | LYS_PARSE_OPT_INGRP, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 4158 | child = NULL; |
| 4159 | goto error; |
| 4160 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4161 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 4162 | case LYS_CONTAINER: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4163 | if (yang_check_container(module, (struct lys_node_container *)node, &child, options, unres)) { |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 4164 | goto error; |
| 4165 | } |
| 4166 | break; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 4167 | case LYS_LEAF: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4168 | if (yang_check_leaf(module, (struct lys_node_leaf *)node, options, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 4169 | child = NULL; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 4170 | goto error; |
| 4171 | } |
| 4172 | break; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 4173 | case LYS_LEAFLIST: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4174 | if (yang_check_leaflist(module, (struct lys_node_leaflist *)node, options, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 4175 | child = NULL; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 4176 | goto error; |
| 4177 | } |
| 4178 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 4179 | case LYS_LIST: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4180 | if (yang_check_list(module, (struct lys_node_list *)node, &child, options, unres)) { |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 4181 | goto error; |
| 4182 | } |
| 4183 | break; |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 4184 | case LYS_CHOICE: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4185 | if (yang_check_choice(module, (struct lys_node_choice *)node, &child, options, unres)) { |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 4186 | goto error; |
| 4187 | } |
| 4188 | break; |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 4189 | case LYS_CASE: |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 4190 | if (yang_check_iffeatures(module, NULL, node, CASE_KEYWORD, unres)) { |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 4191 | goto error; |
| 4192 | } |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4193 | if (yang_check_nodes(module, node, child, options, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 4194 | child = NULL; |
| 4195 | goto error; |
| 4196 | } |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4197 | if (((struct lys_node_case *)node)->when) { |
| 4198 | if (yang_check_ext_instance(module, &((struct lys_node_case *)node)->when->ext, |
| 4199 | ((struct lys_node_case *)node)->when->ext_size, ((struct lys_node_case *)node)->when, unres)) { |
| 4200 | goto error; |
| 4201 | } |
| 4202 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4203 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 4204 | if (lyxp_node_check_syntax(node)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4205 | goto error; |
| 4206 | } |
| 4207 | } else { |
| 4208 | if (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4209 | goto error; |
| 4210 | } |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4211 | } |
| 4212 | } |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 4213 | break; |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 4214 | case LYS_ANYDATA: |
| 4215 | case LYS_ANYXML: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4216 | if (yang_check_anydata(module, (struct lys_node_anydata *)node, &child, options, unres)) { |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 4217 | goto error; |
| 4218 | } |
| 4219 | break; |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 4220 | case LYS_RPC: |
| 4221 | case LYS_ACTION: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4222 | if (yang_check_rpc_action(module, (struct lys_node_rpc_action *)node, &child, options, unres)){ |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 4223 | goto error; |
| 4224 | } |
| 4225 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 4226 | case LYS_INPUT: |
| 4227 | case LYS_OUTPUT: |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 4228 | if (yang_check_typedef(module, node, unres)) { |
| 4229 | goto error; |
| 4230 | } |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4231 | if (yang_check_nodes(module, node, child, options, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 4232 | child = NULL; |
| 4233 | goto error; |
| 4234 | } |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 4235 | if (((struct lys_node_inout *)node)->must_size) { |
| 4236 | if (yang_check_must(module, ((struct lys_node_inout *)node)->must, ((struct lys_node_inout *)node)->must_size, unres)) { |
| 4237 | goto error; |
| 4238 | } |
| 4239 | /* check XPath dependencies */ |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4240 | if (options & LYS_PARSE_OPT_INGRP) { |
Michal Vasko | 364918a | 2017-03-17 13:23:46 +0100 | [diff] [blame] | 4241 | if (lyxp_node_check_syntax(node)) { |
Michal Vasko | 89afc11 | 2017-03-16 13:57:28 +0100 | [diff] [blame] | 4242 | goto error; |
| 4243 | } |
| 4244 | } else { |
| 4245 | if (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1) { |
| 4246 | goto error; |
| 4247 | } |
PavolVican | 38104a3 | 2017-02-08 12:25:23 +0100 | [diff] [blame] | 4248 | } |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 4249 | } |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame] | 4250 | break; |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 4251 | case LYS_NOTIF: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4252 | if (yang_check_notif(module, (struct lys_node_notif *)node, &child, options, unres)) { |
Pavol Vican | 29bf880 | 2016-11-28 20:44:57 +0100 | [diff] [blame] | 4253 | goto error; |
| 4254 | } |
| 4255 | break; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 4256 | case LYS_USES: |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4257 | if (yang_check_uses(module, (struct lys_node_uses *)node, options, unres)) { |
PavolVican | e87cb93 | 2016-12-30 15:36:18 +0100 | [diff] [blame] | 4258 | child = NULL; |
Pavol Vican | 3b5e82a | 2016-11-29 21:41:56 +0100 | [diff] [blame] | 4259 | goto error; |
| 4260 | } |
| 4261 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4262 | default: |
| 4263 | LOGINT; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4264 | goto error; |
| 4265 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4266 | node = sibling; |
| 4267 | } |
| 4268 | |
| 4269 | return EXIT_SUCCESS; |
| 4270 | error: |
| 4271 | yang_free_nodes(module->ctx, sibling); |
| 4272 | yang_free_nodes(module->ctx, child); |
| 4273 | return EXIT_FAILURE; |
| 4274 | } |
| 4275 | |
| 4276 | static int |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4277 | yang_check_deviate(struct lys_module *module, struct unres_schema *unres, struct lys_deviate *deviate, |
| 4278 | struct lys_node *dev_target, struct ly_set *dflt_check) |
| 4279 | { |
| 4280 | struct lys_node_leaflist *llist; |
| 4281 | struct lys_type *type; |
| 4282 | struct lys_tpdf *tmp_parent; |
| 4283 | int i, j; |
| 4284 | |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 4285 | if (yang_check_ext_instance(module, &deviate->ext, deviate->ext_size, deviate, unres)) { |
| 4286 | goto error; |
| 4287 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4288 | if (deviate->must_size && yang_check_deviate_must(module, unres, deviate, dev_target)) { |
| 4289 | goto error; |
| 4290 | } |
| 4291 | if (deviate->unique && yang_check_deviate_unique(module, deviate, dev_target)) { |
| 4292 | goto error; |
| 4293 | } |
| 4294 | if (deviate->dflt_size) { |
| 4295 | if (yang_read_deviate_default(module, deviate, dev_target, dflt_check)) { |
| 4296 | goto error; |
| 4297 | } |
| 4298 | if (dev_target->nodetype == LYS_LEAFLIST && deviate->mod == LY_DEVIATE_DEL) { |
| 4299 | /* consolidate the final list in the target after removing items from it */ |
| 4300 | llist = (struct lys_node_leaflist *)dev_target; |
| 4301 | for (i = j = 0; j < llist->dflt_size; j++) { |
| 4302 | llist->dflt[i] = llist->dflt[j]; |
| 4303 | if (llist->dflt[i]) { |
| 4304 | i++; |
| 4305 | } |
| 4306 | } |
| 4307 | llist->dflt_size = i + 1; |
| 4308 | } |
| 4309 | } |
| 4310 | |
| 4311 | if (deviate->max_set && yang_read_deviate_minmax(deviate, dev_target, deviate->max, 1)) { |
| 4312 | goto error; |
| 4313 | } |
| 4314 | |
| 4315 | if (deviate->min_set && yang_read_deviate_minmax(deviate, dev_target, deviate->min, 0)) { |
| 4316 | goto error; |
| 4317 | } |
| 4318 | |
| 4319 | if (deviate->units && yang_read_deviate_units(module->ctx, deviate, dev_target)) { |
| 4320 | goto error; |
| 4321 | } |
| 4322 | |
| 4323 | if ((deviate->flags & LYS_CONFIG_MASK)) { |
| 4324 | /* add and replace are the same in this case */ |
| 4325 | /* remove current config value of the target ... */ |
| 4326 | dev_target->flags &= ~LYS_CONFIG_MASK; |
| 4327 | |
| 4328 | /* ... and replace it with the value specified in deviation */ |
| 4329 | dev_target->flags |= deviate->flags & LYS_CONFIG_MASK; |
| 4330 | } |
| 4331 | |
| 4332 | if ((deviate->flags & LYS_MAND_MASK) && yang_check_deviate_mandatory(deviate, dev_target)) { |
| 4333 | goto error; |
| 4334 | } |
| 4335 | |
| 4336 | if (deviate->type) { |
| 4337 | /* check target node type */ |
| 4338 | if (dev_target->nodetype == LYS_LEAF) { |
| 4339 | type = &((struct lys_node_leaf *)dev_target)->type; |
| 4340 | } else if (dev_target->nodetype == LYS_LEAFLIST) { |
| 4341 | type = &((struct lys_node_leaflist *)dev_target)->type; |
| 4342 | } else { |
| 4343 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "type"); |
| 4344 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"type\" property."); |
| 4345 | goto error; |
| 4346 | } |
| 4347 | /* remove type and initialize it */ |
| 4348 | tmp_parent = type->parent; |
Radek Krejci | 5138e9f | 2017-04-12 13:10:46 +0200 | [diff] [blame] | 4349 | lys_type_free(module->ctx, type, NULL); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4350 | memcpy(type, deviate->type, sizeof *deviate->type); |
| 4351 | free(deviate->type); |
| 4352 | deviate->type = type; |
| 4353 | deviate->type->parent = tmp_parent; |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 4354 | if (yang_fill_type(module, type, (struct yang_type *)type->der, tmp_parent, unres)) { |
| 4355 | goto error; |
| 4356 | } |
| 4357 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4358 | if (unres_schema_add_node(module, unres, deviate->type, UNRES_TYPE_DER, dev_target) == -1) { |
| 4359 | goto error; |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | return EXIT_SUCCESS; |
| 4364 | error: |
| 4365 | if (deviate->type) { |
| 4366 | yang_type_free(module->ctx, deviate->type); |
| 4367 | deviate->type = NULL; |
| 4368 | } |
| 4369 | return EXIT_FAILURE; |
| 4370 | } |
| 4371 | |
| 4372 | static int |
| 4373 | yang_check_deviation(struct lys_module *module, struct unres_schema *unres, struct lys_deviation *dev) |
| 4374 | { |
| 4375 | int rc; |
| 4376 | uint i; |
| 4377 | struct lys_node *dev_target = NULL, *parent; |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4378 | struct ly_set *dflt_check = ly_set_new(), *set; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4379 | unsigned int u; |
| 4380 | const char *value, *target_name; |
| 4381 | struct lys_node_leaflist *llist; |
| 4382 | struct lys_node_leaf *leaf; |
| 4383 | struct unres_schema tmp_unres; |
| 4384 | struct lys_module *mod; |
| 4385 | |
| 4386 | /* resolve target node */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4387 | rc = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1); |
| 4388 | if (rc == -1) { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4389 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4390 | ly_set_free(set); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4391 | goto error; |
| 4392 | } |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 4393 | dev_target = set->set.s[0]; |
| 4394 | ly_set_free(set); |
| 4395 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4396 | if (dev_target->module == lys_main_module(module)) { |
| 4397 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
| 4398 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed."); |
| 4399 | goto error; |
| 4400 | } |
| 4401 | |
| 4402 | if (!dflt_check) { |
| 4403 | LOGMEM; |
| 4404 | goto error; |
| 4405 | } |
| 4406 | |
| 4407 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 4408 | /* you cannot remove a key leaf */ |
| 4409 | if ((dev_target->nodetype == LYS_LEAF) && dev_target->parent && (dev_target->parent->nodetype == LYS_LIST)) { |
| 4410 | for (i = 0; i < ((struct lys_node_list *)dev_target->parent)->keys_size; ++i) { |
| 4411 | if (((struct lys_node_list *)dev_target->parent)->keys[i] == (struct lys_node_leaf *)dev_target) { |
| 4412 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation"); |
| 4413 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key."); |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 4414 | goto error; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4415 | } |
| 4416 | } |
| 4417 | } |
| 4418 | /* unlink and store the original node */ |
Radek Krejci | 9cf034c | 2017-08-24 17:10:35 +0200 | [diff] [blame] | 4419 | parent = dev_target->parent; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4420 | lys_node_unlink(dev_target); |
Radek Krejci | 9cf034c | 2017-08-24 17:10:35 +0200 | [diff] [blame] | 4421 | if (parent && parent->nodetype == LYS_AUGMENT) { |
| 4422 | /* hack for augment, because when the original will be sometime reconnected back, we actually need |
| 4423 | * to reconnect it to both - the augment and its target (which is deduced from the deviations target |
| 4424 | * path), so we need to remember the augment as an addition */ |
| 4425 | dev_target->parent = parent; |
| 4426 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4427 | dev->orig_node = dev_target; |
| 4428 | } else { |
| 4429 | /* store a shallow copy of the original node */ |
| 4430 | memset(&tmp_unres, 0, sizeof tmp_unres); |
PavolVican | 8c33b15 | 2017-01-27 12:45:34 +0100 | [diff] [blame] | 4431 | dev->orig_node = lys_node_dup(dev_target->module, NULL, dev_target, &tmp_unres, 1); |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4432 | /* just to be safe */ |
| 4433 | if (tmp_unres.count) { |
| 4434 | LOGINT; |
| 4435 | goto error; |
| 4436 | } |
| 4437 | } |
| 4438 | |
PavolVican | 6f00092 | 2017-02-10 12:56:59 +0100 | [diff] [blame] | 4439 | if (yang_check_ext_instance(module, &dev->ext, dev->ext_size, dev, unres)) { |
| 4440 | goto error; |
| 4441 | } |
| 4442 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4443 | for (i = 0; i < dev->deviate_size; ++i) { |
| 4444 | if (yang_check_deviate(module, unres, &dev->deviate[i], dev_target, dflt_check)) { |
| 4445 | yang_free_deviate(module->ctx, dev, i + 1); |
| 4446 | dev->deviate_size = i+1; |
| 4447 | goto error; // missing free unresolve type in deviate |
| 4448 | } |
| 4449 | } |
| 4450 | /* now check whether default value, if any, matches the type */ |
| 4451 | for (u = 0; u < dflt_check->number; ++u) { |
| 4452 | value = NULL; |
| 4453 | rc = EXIT_SUCCESS; |
| 4454 | if (dflt_check->set.s[u]->nodetype == LYS_LEAF) { |
| 4455 | leaf = (struct lys_node_leaf *)dflt_check->set.s[u]; |
| 4456 | target_name = leaf->name; |
| 4457 | value = leaf->dflt; |
| 4458 | rc = unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaf->dflt)); |
| 4459 | } else { /* LYS_LEAFLIST */ |
| 4460 | llist = (struct lys_node_leaflist *)dflt_check->set.s[u]; |
| 4461 | target_name = llist->name; |
| 4462 | for (i = 0; i < llist->dflt_size; i++) { |
| 4463 | rc = unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT, |
| 4464 | (struct lys_node *)(&llist->dflt[i])); |
| 4465 | if (rc == -1) { |
| 4466 | value = llist->dflt[i]; |
| 4467 | break; |
| 4468 | } |
| 4469 | } |
| 4470 | } |
| 4471 | if (rc == -1) { |
| 4472 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 4473 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 4474 | "The default value \"%s\" of the deviated node \"%s\"no longer matches its type.", |
| 4475 | target_name); |
| 4476 | goto error; |
| 4477 | } |
| 4478 | } |
| 4479 | ly_set_free(dflt_check); |
| 4480 | |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4481 | /* mark all the affected modules as deviated and implemented */ |
| 4482 | for (parent = dev_target; parent; parent = lys_parent(parent)) { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4483 | mod = lys_node_module(parent); |
| 4484 | if (module != mod) { |
Radek Krejci | 29eac3d | 2017-06-01 16:50:02 +0200 | [diff] [blame] | 4485 | mod->deviated = 1; /* main module */ |
| 4486 | parent->module->deviated = 1; /* possible submodule */ |
| 4487 | if (lys_set_implemented(mod)) { |
| 4488 | LOGERR(ly_errno, "Setting the deviated module \"%s\" implemented failed.", mod->name); |
| 4489 | goto error; |
| 4490 | } |
| 4491 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4492 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4493 | |
| 4494 | return EXIT_SUCCESS; |
| 4495 | error: |
| 4496 | ly_set_free(dflt_check); |
| 4497 | return EXIT_FAILURE; |
| 4498 | } |
| 4499 | |
| 4500 | static int |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 4501 | yang_check_sub_module(struct lys_module *module, struct unres_schema *unres, struct lys_node *node) |
| 4502 | { |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4503 | 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] | 4504 | |
| 4505 | aug_size = module->augment_size; |
| 4506 | module->augment_size = 0; |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4507 | dev_size = module->deviation_size; |
| 4508 | module->deviation_size = 0; |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 4509 | |
PavolVican | 6f87bf9 | 2017-02-15 19:38:55 +0100 | [diff] [blame] | 4510 | if (yang_check_typedef(module, NULL, unres)) { |
| 4511 | goto error; |
| 4512 | } |
| 4513 | |
PavolVican | c180726 | 2017-01-31 18:00:27 +0100 | [diff] [blame] | 4514 | if (yang_check_ext_instance(module, &module->ext, module->ext_size, module, unres)) { |
| 4515 | goto error; |
| 4516 | } |
| 4517 | |
PavolVican | 171717d | 2017-02-01 14:49:55 +0100 | [diff] [blame] | 4518 | /* check extension in revision */ |
| 4519 | for (i = 0; i < module->rev_size; ++i) { |
| 4520 | if (yang_check_ext_instance(module, &module->rev[i].ext, module->rev[i].ext_size, &module->rev[i], unres)) { |
| 4521 | goto error; |
| 4522 | } |
| 4523 | } |
| 4524 | |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 4525 | /* check extension in definition of extension */ |
PavolVican | 5393d3f | 2017-02-06 23:30:55 +0100 | [diff] [blame] | 4526 | for (i = 0; i < module->extensions_size; ++i) { |
PavolVican | 19dc615 | 2017-02-06 12:04:15 +0100 | [diff] [blame] | 4527 | if (yang_check_ext_instance(module, &module->extensions[i].ext, module->extensions[i].ext_size, &module->extensions[i], unres)) { |
| 4528 | goto error; |
| 4529 | } |
| 4530 | } |
| 4531 | |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 4532 | /* check features */ |
| 4533 | for (i = 0; i < module->features_size; ++i) { |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 4534 | if (yang_check_iffeatures(module, NULL, &module->features[i], FEATURE_KEYWORD, unres)) { |
| 4535 | goto error; |
| 4536 | } |
PavolVican | 5393d3f | 2017-02-06 23:30:55 +0100 | [diff] [blame] | 4537 | if (yang_check_ext_instance(module, &module->features[i].ext, module->features[i].ext_size, &module->features[i], unres)) { |
| 4538 | goto error; |
| 4539 | } |
| 4540 | |
Pavol Vican | df9a95c | 2016-12-02 23:34:51 +0100 | [diff] [blame] | 4541 | /* check for circular dependencies */ |
| 4542 | if (module->features[i].iffeature_size && (unres_schema_add_node(module, unres, &module->features[i], UNRES_FEATURE, NULL) == -1)) { |
| 4543 | goto error; |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 4544 | } |
| 4545 | } |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 4546 | erase_identities = 0; |
| 4547 | if (yang_check_identities(module, unres)) { |
| 4548 | goto error; |
| 4549 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4550 | erase_nodes = 0; |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4551 | if (yang_check_nodes(module, NULL, node, 0, unres)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4552 | goto error; |
| 4553 | } |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 4554 | |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4555 | /* check deviation */ |
| 4556 | for (i = 0; i < dev_size; ++i) { |
| 4557 | module->deviation_size++; |
| 4558 | if (yang_check_deviation(module, unres, &module->deviation[i])) { |
| 4559 | goto error; |
| 4560 | } |
| 4561 | } |
| 4562 | |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4563 | /* check augments */ |
| 4564 | for (i = 0; i < aug_size; ++i) { |
| 4565 | module->augment_size++; |
Radek Krejci | 7212e0a | 2017-03-08 15:58:22 +0100 | [diff] [blame] | 4566 | if (yang_check_augment(module, &module->augment[i], 0, unres)) { |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4567 | goto error; |
| 4568 | } |
| 4569 | if (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1) { |
| 4570 | goto error; |
| 4571 | } |
| 4572 | } |
| 4573 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 4574 | return EXIT_SUCCESS; |
| 4575 | error: |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 4576 | if (erase_identities) { |
| 4577 | yang_free_ident_base(module->ident, 0, module->ident_size); |
| 4578 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 4579 | if (erase_nodes) { |
| 4580 | yang_free_nodes(module->ctx, node); |
| 4581 | } |
Pavol Vican | 3ad50f8 | 2016-12-04 15:00:36 +0100 | [diff] [blame] | 4582 | for (i = module->augment_size; i < aug_size; ++i) { |
| 4583 | yang_free_augment(module->ctx, &module->augment[i]); |
| 4584 | } |
PavolVican | 75af21d | 2016-12-29 20:04:07 +0100 | [diff] [blame] | 4585 | for (i = module->deviation_size; i < dev_size; ++i) { |
| 4586 | yang_free_deviate(module->ctx, &module->deviation[i], 0); |
| 4587 | free(module->deviation[i].deviate); |
| 4588 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 4589 | return EXIT_FAILURE; |
| 4590 | } |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4591 | |
| 4592 | int |
Michal Vasko | 3e3228d | 2017-02-24 14:55:32 +0100 | [diff] [blame] | 4593 | yang_read_extcomplex_str(struct lys_module *module, struct lys_ext_instance_complex *ext, const char *arg_name, |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4594 | const char *parent_name, char *value, int parent_stmt, LY_STMT stmt) |
| 4595 | { |
| 4596 | int c; |
| 4597 | const char **str, ***p = NULL; |
| 4598 | void *reallocated; |
| 4599 | struct lyext_substmt *info; |
| 4600 | |
| 4601 | c = 0; |
| 4602 | if (stmt == LY_STMT_PREFIX && parent_stmt == LY_STMT_BELONGSTO) { |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 4603 | /* str contains no NULL value */ |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4604 | str = lys_ext_complex_get_substmt(LY_STMT_BELONGSTO, ext, &info); |
| 4605 | if (info->cardinality < LY_STMT_CARD_SOME) { |
| 4606 | str++; |
| 4607 | } else { |
| 4608 | /* get the index in the array to add new item */ |
| 4609 | p = (const char ***)str; |
| 4610 | for (c = 0; p[0][c + 1]; c++); |
| 4611 | str = p[1]; |
| 4612 | } |
| 4613 | str[c] = lydict_insert_zc(module->ctx, value); |
| 4614 | } else { |
| 4615 | str = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 4616 | if (!str) { |
| 4617 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, arg_name, parent_name); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4618 | goto error; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4619 | } |
| 4620 | if (info->cardinality < LY_STMT_CARD_SOME && *str) { |
| 4621 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, arg_name, parent_name); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4622 | goto error; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | if (info->cardinality >= LY_STMT_CARD_SOME) { |
| 4626 | /* there can be multiple instances, str is actually const char *** */ |
| 4627 | p = (const char ***)str; |
| 4628 | if (!p[0]) { |
| 4629 | /* allocate initial array */ |
PavolVican | 5080974 | 2017-02-18 21:22:54 +0100 | [diff] [blame] | 4630 | p[0] = calloc(2, sizeof(const char *)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4631 | LY_CHECK_ERR_GOTO(!p[0], LOGMEM, error); |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4632 | if (stmt == LY_STMT_BELONGSTO) { |
| 4633 | /* allocate another array for the belongs-to's prefixes */ |
| 4634 | p[1] = calloc(2, sizeof(const char *)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4635 | LY_CHECK_ERR_GOTO(!p[1], LOGMEM, error); |
PavolVican | 5080974 | 2017-02-18 21:22:54 +0100 | [diff] [blame] | 4636 | } else if (stmt == LY_STMT_ARGUMENT) { |
| 4637 | /* allocate another array for the yin element */ |
PavolVican | 4b80d04 | 2017-02-23 14:30:27 +0100 | [diff] [blame] | 4638 | ((uint8_t **)p)[1] = calloc(2, sizeof(uint8_t)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4639 | LY_CHECK_ERR_GOTO(!p[1], LOGMEM, error); |
PavolVican | 5080974 | 2017-02-18 21:22:54 +0100 | [diff] [blame] | 4640 | /* default value of yin element */ |
| 4641 | ((uint8_t *)p[1])[0] = 2; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4642 | } |
| 4643 | } else { |
| 4644 | /* get the index in the array to add new item */ |
| 4645 | for (c = 0; p[0][c]; c++); |
| 4646 | } |
| 4647 | str = p[0]; |
| 4648 | } |
| 4649 | |
| 4650 | str[c] = lydict_insert_zc(module->ctx, value); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4651 | value = NULL; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4652 | |
PavolVican | 5080974 | 2017-02-18 21:22:54 +0100 | [diff] [blame] | 4653 | if (c) { |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4654 | /* enlarge the array(s) */ |
| 4655 | reallocated = realloc(p[0], (c + 2) * sizeof(const char *)); |
| 4656 | if (!reallocated) { |
| 4657 | LOGMEM; |
| 4658 | lydict_remove(module->ctx, p[0][c]); |
| 4659 | p[0][c] = NULL; |
| 4660 | return EXIT_FAILURE; |
| 4661 | } |
| 4662 | p[0] = reallocated; |
| 4663 | p[0][c + 1] = NULL; |
| 4664 | |
| 4665 | if (stmt == LY_STMT_BELONGSTO) { |
| 4666 | /* enlarge the second belongs-to's array with prefixes */ |
| 4667 | reallocated = realloc(p[1], (c + 2) * sizeof(const char *)); |
| 4668 | if (!reallocated) { |
| 4669 | LOGMEM; |
| 4670 | lydict_remove(module->ctx, p[1][c]); |
| 4671 | p[1][c] = NULL; |
| 4672 | return EXIT_FAILURE; |
| 4673 | } |
| 4674 | p[1] = reallocated; |
| 4675 | p[1][c + 1] = NULL; |
PavolVican | 5080974 | 2017-02-18 21:22:54 +0100 | [diff] [blame] | 4676 | } else if (stmt == LY_STMT_ARGUMENT) { |
| 4677 | /* enlarge the second argument's array with yin element */ |
| 4678 | reallocated = realloc(p[1], (c + 2) * sizeof(uint8_t)); |
| 4679 | if (!reallocated) { |
| 4680 | LOGMEM; |
| 4681 | ((uint8_t *)p[1])[c] = 0; |
| 4682 | return EXIT_FAILURE; |
| 4683 | } |
| 4684 | p[1] = reallocated; |
| 4685 | ((uint8_t *)p[1])[c + 1] = 0; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | return EXIT_SUCCESS; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4691 | |
| 4692 | error: |
| 4693 | free(value); |
| 4694 | return EXIT_FAILURE; |
PavolVican | 22e8868 | 2017-02-14 22:38:18 +0100 | [diff] [blame] | 4695 | } |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 4696 | |
| 4697 | static int |
| 4698 | yang_fill_ext_substm_index(struct lys_ext_instance_complex *ext, LY_STMT stmt, enum yytokentype keyword) |
| 4699 | { |
| 4700 | int c = 0, decrement = 0; |
| 4701 | const char **str, ***p = NULL; |
| 4702 | struct lyext_substmt *info; |
| 4703 | |
| 4704 | |
| 4705 | if (keyword == BELONGS_TO_KEYWORD || stmt == LY_STMT_BELONGSTO) { |
| 4706 | stmt = LY_STMT_BELONGSTO; |
| 4707 | decrement = -1; |
PavolVican | 5080974 | 2017-02-18 21:22:54 +0100 | [diff] [blame] | 4708 | } else if (keyword == ARGUMENT_KEYWORD || stmt == LY_STMT_ARGUMENT) { |
| 4709 | stmt = LY_STMT_ARGUMENT; |
| 4710 | decrement = -1; |
PavolVican | 5334c89 | 2017-02-15 16:29:09 +0100 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | str = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 4714 | if (!str || info->cardinality < LY_STMT_CARD_SOME || !((const char ***)str)[0]) { |
| 4715 | return 0; |
| 4716 | } else { |
| 4717 | p = (const char ***)str; |
| 4718 | /* get the index in the array */ |
| 4719 | for (c = 0; p[0][c]; c++); |
| 4720 | return c + decrement; |
| 4721 | } |
| 4722 | } |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 4723 | |
| 4724 | void ** |
PavolVican | fe83b15 | 2017-02-19 03:19:29 +0100 | [diff] [blame] | 4725 | yang_getplace_for_extcomplex_struct(struct lys_ext_instance_complex *ext, int *index, |
| 4726 | char *parent_name, char *node_name, LY_STMT stmt) |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 4727 | { |
| 4728 | int c; |
| 4729 | void **data, ***p = NULL; |
| 4730 | void *reallocated; |
| 4731 | struct lyext_substmt *info; |
| 4732 | |
| 4733 | data = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 4734 | if (!data) { |
| 4735 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4736 | return NULL; |
| 4737 | } |
| 4738 | if (info->cardinality < LY_STMT_CARD_SOME && *data) { |
| 4739 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4740 | return NULL; |
| 4741 | } |
| 4742 | |
| 4743 | c = 0; |
| 4744 | if (info->cardinality >= LY_STMT_CARD_SOME) { |
| 4745 | /* there can be multiple instances, so instead of pointer to array, |
| 4746 | * we have in data pointer to pointer to array */ |
| 4747 | p = (void ***)data; |
| 4748 | data = *p; |
| 4749 | if (!data) { |
| 4750 | /* allocate initial array */ |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 4751 | *p = data = calloc(2, sizeof(void *)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4752 | LY_CHECK_ERR_RETURN(!data, LOGMEM, NULL); |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 4753 | } else { |
| 4754 | for (c = 0; *data; data++, c++); |
| 4755 | } |
| 4756 | } |
| 4757 | |
PavolVican | eef1d91 | 2017-02-19 00:19:15 +0100 | [diff] [blame] | 4758 | if (c) { |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 4759 | /* enlarge the array */ |
| 4760 | reallocated = realloc(*p, (c + 2) * sizeof(void *)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4761 | LY_CHECK_ERR_RETURN(!reallocated, LOGMEM, NULL); |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 4762 | *p = reallocated; |
| 4763 | data = *p; |
| 4764 | data[c + 1] = NULL; |
| 4765 | } |
| 4766 | |
PavolVican | fe83b15 | 2017-02-19 03:19:29 +0100 | [diff] [blame] | 4767 | if (index) { |
| 4768 | *index = c; |
| 4769 | return data; |
| 4770 | } else { |
| 4771 | return &data[c]; |
| 4772 | } |
PavolVican | a0fdbf3 | 2017-02-15 17:59:02 +0100 | [diff] [blame] | 4773 | } |
PavolVican | ff0f7f4 | 2017-02-16 11:35:42 +0100 | [diff] [blame] | 4774 | |
| 4775 | int |
| 4776 | yang_fill_extcomplex_flags(struct lys_ext_instance_complex *ext, char *parent_name, char *node_name, |
| 4777 | LY_STMT stmt, uint16_t value, uint16_t mask) |
| 4778 | { |
| 4779 | uint16_t *data; |
| 4780 | struct lyext_substmt *info; |
| 4781 | |
| 4782 | data = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 4783 | if (!data) { |
| 4784 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4785 | return EXIT_FAILURE; |
| 4786 | } |
| 4787 | if (info->cardinality < LY_STMT_CARD_SOME && (*data & mask)) { |
| 4788 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4789 | return EXIT_FAILURE; |
| 4790 | } |
| 4791 | |
| 4792 | *data |= value; |
| 4793 | return EXIT_SUCCESS; |
| 4794 | } |
PavolVican | 8e8dfd1 | 2017-02-16 13:51:41 +0100 | [diff] [blame] | 4795 | |
| 4796 | int |
PavolVican | 6a852a6 | 2017-02-16 15:36:01 +0100 | [diff] [blame] | 4797 | yang_fill_extcomplex_uint8(struct lys_ext_instance_complex *ext, char *parent_name, char *node_name, |
| 4798 | LY_STMT stmt, uint8_t value) |
PavolVican | 8e8dfd1 | 2017-02-16 13:51:41 +0100 | [diff] [blame] | 4799 | { |
PavolVican | 6a852a6 | 2017-02-16 15:36:01 +0100 | [diff] [blame] | 4800 | uint8_t *val, **pp = NULL, *reallocated; |
PavolVican | 8e8dfd1 | 2017-02-16 13:51:41 +0100 | [diff] [blame] | 4801 | struct lyext_substmt *info; |
PavolVican | 6a852a6 | 2017-02-16 15:36:01 +0100 | [diff] [blame] | 4802 | int i = 0; |
PavolVican | 8e8dfd1 | 2017-02-16 13:51:41 +0100 | [diff] [blame] | 4803 | |
| 4804 | val = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 4805 | if (!val) { |
| 4806 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4807 | return EXIT_FAILURE; |
| 4808 | } |
PavolVican | 6a852a6 | 2017-02-16 15:36:01 +0100 | [diff] [blame] | 4809 | if (stmt == LY_STMT_DIGITS) { |
| 4810 | if (info->cardinality < LY_STMT_CARD_SOME && *val) { |
| 4811 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4812 | return EXIT_FAILURE; |
| 4813 | } |
PavolVican | 8e8dfd1 | 2017-02-16 13:51:41 +0100 | [diff] [blame] | 4814 | |
PavolVican | 6a852a6 | 2017-02-16 15:36:01 +0100 | [diff] [blame] | 4815 | if (info->cardinality >= LY_STMT_CARD_SOME) { |
| 4816 | /* there can be multiple instances */ |
| 4817 | pp = (uint8_t**)val; |
| 4818 | if (!(*pp)) { |
| 4819 | *pp = calloc(2, sizeof(uint8_t)); /* allocate initial array */ |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4820 | LY_CHECK_ERR_RETURN(!*pp, LOGMEM, EXIT_FAILURE); |
PavolVican | 6a852a6 | 2017-02-16 15:36:01 +0100 | [diff] [blame] | 4821 | } else { |
| 4822 | for (i = 0; (*pp)[i]; i++); |
| 4823 | } |
| 4824 | val = &(*pp)[i]; |
| 4825 | } |
| 4826 | |
| 4827 | /* stored value */ |
| 4828 | *val = value; |
| 4829 | |
| 4830 | if (i) { |
| 4831 | /* enlarge the array */ |
| 4832 | reallocated = realloc(*pp, (i + 2) * sizeof *val); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4833 | LY_CHECK_ERR_RETURN(!reallocated, LOGMEM, EXIT_FAILURE); |
PavolVican | 6a852a6 | 2017-02-16 15:36:01 +0100 | [diff] [blame] | 4834 | *pp = reallocated; |
| 4835 | (*pp)[i + 1] = 0; |
| 4836 | } |
PavolVican | 8e8dfd1 | 2017-02-16 13:51:41 +0100 | [diff] [blame] | 4837 | } else { |
PavolVican | 6a852a6 | 2017-02-16 15:36:01 +0100 | [diff] [blame] | 4838 | if (*val) { |
| 4839 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4840 | return EXIT_FAILURE; |
| 4841 | } |
| 4842 | |
| 4843 | if (stmt == LY_STMT_REQINSTANCE) { |
| 4844 | *val = (value == 1) ? 1 : 2; |
| 4845 | } else if (stmt == LY_STMT_MODIFIER) { |
| 4846 | *val = 1; |
| 4847 | } else { |
| 4848 | LOGINT; |
| 4849 | return EXIT_FAILURE; |
| 4850 | } |
PavolVican | 8e8dfd1 | 2017-02-16 13:51:41 +0100 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | return EXIT_SUCCESS; |
| 4854 | } |
PavolVican | 3f47b29 | 2017-02-16 20:38:16 +0100 | [diff] [blame] | 4855 | |
PavolVican | f3091bf | 2017-02-19 18:27:01 +0100 | [diff] [blame] | 4856 | int |
| 4857 | yang_extcomplex_node(struct lys_ext_instance_complex *ext, char *parent_name, char *node_name, |
| 4858 | struct lys_node *node, LY_STMT stmt) |
| 4859 | { |
| 4860 | struct lyext_substmt *info; |
| 4861 | struct lys_node **snode, *siter; |
| 4862 | |
| 4863 | snode = lys_ext_complex_get_substmt(stmt, ext, &info); |
| 4864 | if (!snode) { |
| 4865 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4866 | return EXIT_FAILURE; |
| 4867 | } |
| 4868 | if (info->cardinality < LY_STMT_CARD_SOME) { |
| 4869 | LY_TREE_FOR(node, siter) { |
| 4870 | if (stmt == lys_snode2stmt(siter->nodetype)) { |
| 4871 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node_name, parent_name); |
| 4872 | return EXIT_FAILURE; |
| 4873 | } |
| 4874 | } |
| 4875 | } |
| 4876 | |
| 4877 | return EXIT_SUCCESS; |
| 4878 | } |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4879 | |
| 4880 | int |
| 4881 | yang_fill_extcomplex_module(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, |
| 4882 | char *parent_name, char **values, int implemented) |
| 4883 | { |
| 4884 | int c, i; |
| 4885 | struct lys_module **modules, ***p, *reallocated, **pp; |
| 4886 | struct lyext_substmt *info; |
| 4887 | |
| 4888 | if (!values) { |
| 4889 | return EXIT_SUCCESS; |
| 4890 | } |
| 4891 | pp = modules = lys_ext_complex_get_substmt(LY_STMT_MODULE, ext, &info); |
| 4892 | if (!modules) { |
| 4893 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "module", parent_name); |
| 4894 | return EXIT_FAILURE; |
| 4895 | } |
| 4896 | |
| 4897 | for (i = 0; values[i]; ++i) { |
| 4898 | c = 0; |
| 4899 | if (info->cardinality < LY_STMT_CARD_SOME && *modules) { |
| 4900 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "module", parent_name); |
| 4901 | return EXIT_FAILURE; |
| 4902 | } |
| 4903 | if (info->cardinality >= LY_STMT_CARD_SOME) { |
| 4904 | /* there can be multiple instances, so instead of pointer to array, |
| 4905 | * we have in modules pointer to pointer to array */ |
| 4906 | p = (struct lys_module ***)pp; |
| 4907 | modules = *p; |
| 4908 | if (!modules) { |
| 4909 | /* allocate initial array */ |
| 4910 | *p = modules = calloc(2, sizeof(struct lys_module *)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4911 | LY_CHECK_ERR_RETURN(!*p, LOGMEM, EXIT_FAILURE); |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4912 | } else { |
| 4913 | for (c = 0; *modules; modules++, c++); |
| 4914 | } |
| 4915 | } |
| 4916 | |
| 4917 | if (c) { |
| 4918 | /* enlarge the array */ |
| 4919 | reallocated = realloc(*p, (c + 2) * sizeof(struct lys_module *)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 4920 | LY_CHECK_ERR_RETURN(!reallocated, LOGMEM, EXIT_FAILURE); |
PavolVican | db0e817 | 2017-02-20 00:46:09 +0100 | [diff] [blame] | 4921 | *p = (struct lys_module **)reallocated; |
| 4922 | modules = *p; |
| 4923 | modules[c + 1] = NULL; |
| 4924 | } |
| 4925 | |
| 4926 | modules[c] = yang_read_module(ctx, values[i], 0, NULL, implemented); |
| 4927 | if (!modules[c]) { |
| 4928 | return EXIT_FAILURE; |
| 4929 | } |
| 4930 | } |
| 4931 | |
| 4932 | return EXIT_SUCCESS; |
| 4933 | } |