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