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