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