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