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