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_refine(struct lys_module *module, struct lys_node_uses *uses, char *value) |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1461 | { |
| 1462 | struct lys_refine *rfn; |
| 1463 | |
| 1464 | rfn = &uses->refine[uses->refine_size]; |
| 1465 | uses->refine_size++; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1466 | rfn->target_name = transform_schema2json(module, value); |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1467 | free(value); |
| 1468 | if (!rfn->target_name) { |
| 1469 | return NULL; |
| 1470 | } |
| 1471 | return rfn; |
| 1472 | } |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1473 | |
| 1474 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1475 | 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] | 1476 | { |
| 1477 | struct lys_node_augment *aug; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1478 | |
| 1479 | if (parent) { |
| 1480 | aug = &((struct lys_node_uses *)parent)->augment[((struct lys_node_uses *)parent)->augment_size]; |
| 1481 | } else { |
| 1482 | aug = &module->augment[module->augment_size]; |
| 1483 | } |
| 1484 | aug->nodetype = LYS_AUGMENT; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1485 | aug->target_name = transform_schema2json(module, value); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1486 | free(value); |
| 1487 | if (!aug->target_name) { |
| 1488 | return NULL; |
| 1489 | } |
| 1490 | aug->parent = parent; |
| 1491 | aug->module = module; |
| 1492 | if (parent) { |
| 1493 | ((struct lys_node_uses *)parent)->augment_size++; |
| 1494 | } else { |
| 1495 | module->augment_size++; |
| 1496 | } |
| 1497 | return aug; |
| 1498 | } |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1499 | |
| 1500 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1501 | yang_read_deviation(struct lys_module *module, char *value) |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1502 | { |
| 1503 | struct lys_node *dev_target = NULL; |
| 1504 | struct lys_deviation *dev; |
| 1505 | struct type_deviation *deviation = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1506 | int rc; |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1507 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1508 | dev = &module->deviation[module->deviation_size]; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1509 | dev->target_name = transform_schema2json(module, value); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1510 | free(value); |
| 1511 | if (!dev->target_name) { |
| 1512 | goto error; |
| 1513 | } |
| 1514 | |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1515 | deviation = calloc(1, sizeof *deviation); |
| 1516 | if (!deviation) { |
| 1517 | LOGMEM; |
| 1518 | goto error; |
| 1519 | } |
| 1520 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1521 | /* resolve target node */ |
Radek Krejci | df46e22 | 2016-11-08 11:57:37 +0100 | [diff] [blame] | 1522 | 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] | 1523 | if (rc || !dev_target) { |
Michal Vasko | 75c8daf | 2016-05-19 10:56:39 +0200 | [diff] [blame] | 1524 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1525 | goto error; |
| 1526 | } |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1527 | if (dev_target->module == lys_main_module(module)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1528 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
| 1529 | 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] | 1530 | goto error; |
| 1531 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1532 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1533 | /*save pointer to the deviation and deviated target*/ |
| 1534 | deviation->deviation = dev; |
| 1535 | deviation->target = dev_target; |
| 1536 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1537 | deviation->dflt_check = ly_set_new(); |
| 1538 | if (!deviation->dflt_check) { |
| 1539 | LOGMEM; |
| 1540 | goto error; |
| 1541 | } |
| 1542 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1543 | return deviation; |
| 1544 | |
| 1545 | error: |
| 1546 | free(deviation); |
| 1547 | lydict_remove(module->ctx, dev->target_name); |
| 1548 | return NULL; |
| 1549 | } |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1550 | |
| 1551 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1552 | yang_read_deviate_unsupported(struct type_deviation *dev) |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1553 | { |
| 1554 | int i; |
| 1555 | |
| 1556 | if (dev->deviation->deviate_size) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1557 | 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] | 1558 | return EXIT_FAILURE; |
| 1559 | } |
| 1560 | dev->deviation->deviate[dev->deviation->deviate_size].mod = LY_DEVIATE_NO; |
| 1561 | |
| 1562 | /* you cannot remove a key leaf */ |
| 1563 | if ((dev->target->nodetype == LYS_LEAF) && dev->target->parent && (dev->target->parent->nodetype == LYS_LIST)) { |
| 1564 | for (i = 0; i < ((struct lys_node_list *)dev->target->parent)->keys_size; ++i) { |
| 1565 | 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] | 1566 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation"); |
| 1567 | 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] | 1568 | return EXIT_FAILURE; |
| 1569 | } |
| 1570 | } |
| 1571 | } |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 1572 | |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1573 | /* unlink and store the original node */ |
Michal Vasko | d921d68 | 2016-05-19 10:56:51 +0200 | [diff] [blame] | 1574 | lys_node_unlink(dev->target); |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1575 | dev->deviation->orig_node = dev->target; |
| 1576 | |
| 1577 | dev->deviation->deviate_size = 1; |
| 1578 | return EXIT_SUCCESS; |
| 1579 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1580 | |
| 1581 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1582 | yang_read_deviate(struct type_deviation *dev, LYS_DEVIATE_TYPE mod) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1583 | { |
| 1584 | struct unres_schema tmp_unres; |
| 1585 | |
| 1586 | dev->deviation->deviate[dev->deviation->deviate_size].mod = mod; |
| 1587 | dev->deviate = &dev->deviation->deviate[dev->deviation->deviate_size]; |
| 1588 | dev->deviation->deviate_size++; |
Pavol Vican | 3388446 | 2016-09-27 21:04:26 +0200 | [diff] [blame] | 1589 | dev->trg_must_size = NULL; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1590 | if (dev->deviation->deviate[0].mod == LY_DEVIATE_NO) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1591 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported"); |
| 1592 | 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] | 1593 | return EXIT_FAILURE; |
| 1594 | } |
| 1595 | |
| 1596 | /* store a shallow copy of the original node */ |
| 1597 | if (!dev->deviation->orig_node) { |
| 1598 | memset(&tmp_unres, 0, sizeof tmp_unres); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 1599 | 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] | 1600 | /* just to be safe */ |
| 1601 | if (tmp_unres.count) { |
| 1602 | LOGINT; |
| 1603 | return EXIT_FAILURE; |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | return EXIT_SUCCESS; |
| 1608 | } |
| 1609 | |
| 1610 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1611 | 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] | 1612 | { |
| 1613 | const char **stritem; |
| 1614 | |
| 1615 | if (dev->deviate->units) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1616 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "units", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1617 | free(value); |
| 1618 | goto error; |
| 1619 | } |
| 1620 | |
| 1621 | /* check target node type */ |
| 1622 | if (dev->target->nodetype == LYS_LEAFLIST) { |
| 1623 | stritem = &((struct lys_node_leaflist *)dev->target)->units; |
| 1624 | } else if (dev->target->nodetype == LYS_LEAF) { |
| 1625 | stritem = &((struct lys_node_leaf *)dev->target)->units; |
| 1626 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1627 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1628 | 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] | 1629 | free(value); |
| 1630 | goto error; |
| 1631 | } |
| 1632 | |
| 1633 | dev->deviate->units = lydict_insert_zc(ctx, value); |
| 1634 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1635 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1636 | /* check values */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 1637 | if (!ly_strequal(*stritem, dev->deviate->units, 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1638 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->units, "units"); |
| 1639 | 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] | 1640 | goto error; |
| 1641 | } |
| 1642 | /* remove current units value of the target */ |
| 1643 | lydict_remove(ctx, *stritem); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1644 | } else { |
| 1645 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1646 | /* check that there is no current value */ |
| 1647 | if (*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1648 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1649 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1650 | goto error; |
| 1651 | } |
| 1652 | } else { /* replace */ |
| 1653 | if (!*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1654 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1655 | 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] | 1656 | goto error; |
| 1657 | } |
| 1658 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1659 | /* remove current units value of the target ... */ |
| 1660 | lydict_remove(ctx, *stritem); |
| 1661 | |
| 1662 | /* ... and replace it with the value specified in deviation */ |
| 1663 | *stritem = lydict_insert(ctx, dev->deviate->units, 0); |
| 1664 | } |
| 1665 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1666 | return EXIT_SUCCESS; |
| 1667 | |
| 1668 | error: |
| 1669 | return EXIT_FAILURE; |
| 1670 | } |
| 1671 | |
| 1672 | int |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1673 | yang_read_deviate_must(struct type_deviation *dev, uint8_t c_must) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1674 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1675 | /* check target node type */ |
| 1676 | switch (dev->target->nodetype) { |
| 1677 | case LYS_LEAF: |
| 1678 | dev->trg_must = &((struct lys_node_leaf *)dev->target)->must; |
| 1679 | dev->trg_must_size = &((struct lys_node_leaf *)dev->target)->must_size; |
| 1680 | break; |
| 1681 | case LYS_CONTAINER: |
| 1682 | dev->trg_must = &((struct lys_node_container *)dev->target)->must; |
| 1683 | dev->trg_must_size = &((struct lys_node_container *)dev->target)->must_size; |
| 1684 | break; |
| 1685 | case LYS_LEAFLIST: |
| 1686 | dev->trg_must = &((struct lys_node_leaflist *)dev->target)->must; |
| 1687 | dev->trg_must_size = &((struct lys_node_leaflist *)dev->target)->must_size; |
| 1688 | break; |
| 1689 | case LYS_LIST: |
| 1690 | dev->trg_must = &((struct lys_node_list *)dev->target)->must; |
| 1691 | dev->trg_must_size = &((struct lys_node_list *)dev->target)->must_size; |
| 1692 | break; |
| 1693 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1694 | case LYS_ANYDATA: |
| 1695 | dev->trg_must = &((struct lys_node_anydata *)dev->target)->must; |
| 1696 | dev->trg_must_size = &((struct lys_node_anydata *)dev->target)->must_size; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1697 | break; |
| 1698 | default: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1699 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must"); |
| 1700 | 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] | 1701 | goto error; |
| 1702 | } |
| 1703 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 1704 | /* flag will be checked again, clear it for now */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 1705 | dev->target->flags &= ~LYS_VALID_DEP; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 1706 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1707 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1708 | /* reallocate the must array of the target */ |
| 1709 | dev->deviate->must = ly_realloc(*dev->trg_must, (c_must + *dev->trg_must_size) * sizeof *dev->deviate->must); |
| 1710 | if (!dev->deviate->must) { |
| 1711 | LOGMEM; |
| 1712 | goto error; |
| 1713 | } |
| 1714 | *dev->trg_must = dev->deviate->must; |
| 1715 | dev->deviate->must = &((*dev->trg_must)[*dev->trg_must_size]); |
| 1716 | dev->deviate->must_size = c_must; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1717 | } else { |
| 1718 | /* LY_DEVIATE_DEL */ |
| 1719 | dev->deviate->must = calloc(c_must, sizeof *dev->deviate->must); |
| 1720 | if (!dev->deviate->must) { |
| 1721 | LOGMEM; |
| 1722 | goto error; |
| 1723 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | return EXIT_SUCCESS; |
| 1727 | |
| 1728 | error: |
| 1729 | return EXIT_FAILURE; |
| 1730 | } |
| 1731 | |
| 1732 | int |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1733 | yang_read_deviate_unique(struct type_deviation *dev, uint8_t c_uniq) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1734 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1735 | struct lys_node_list *list; |
| 1736 | |
| 1737 | /* check target node type */ |
| 1738 | if (dev->target->nodetype != LYS_LIST) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1739 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique"); |
| 1740 | 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] | 1741 | goto error; |
| 1742 | } |
| 1743 | |
| 1744 | list = (struct lys_node_list *)dev->target; |
| 1745 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1746 | /* reallocate the unique array of the target */ |
| 1747 | dev->deviate->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *dev->deviate->unique); |
| 1748 | if (!dev->deviate->unique) { |
| 1749 | LOGMEM; |
| 1750 | goto error; |
| 1751 | } |
| 1752 | list->unique = dev->deviate->unique; |
| 1753 | dev->deviate->unique = &list->unique[list->unique_size]; |
| 1754 | dev->deviate->unique_size = c_uniq; |
| 1755 | memset(dev->deviate->unique, 0, c_uniq * sizeof *dev->deviate->unique); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1756 | } else { |
| 1757 | /* LY_DEVIATE_DEL */ |
| 1758 | dev->deviate->unique = calloc(c_uniq, sizeof *dev->deviate->unique); |
| 1759 | if (!dev->deviate->unique) { |
| 1760 | LOGMEM; |
| 1761 | goto error; |
| 1762 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | return EXIT_SUCCESS; |
| 1766 | |
| 1767 | error: |
| 1768 | return EXIT_FAILURE; |
| 1769 | } |
| 1770 | |
| 1771 | int |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1772 | 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] | 1773 | { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1774 | int i; |
| 1775 | struct lys_node_leaflist *llist; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1776 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1777 | /* check target node type */ |
| 1778 | if (module->version < 2 && dev->target->nodetype == LYS_LEAFLIST) { |
| 1779 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1780 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property."); |
| 1781 | goto error; |
| 1782 | } else if (c_dflt > 1 && dev->target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */ |
| 1783 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1784 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties."); |
| 1785 | goto error; |
| 1786 | } else if (!(dev->target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 1787 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1788 | 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] | 1789 | goto error; |
| 1790 | } |
| 1791 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1792 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1793 | /* check that there is no current value */ |
| 1794 | if ((dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) || |
| 1795 | (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt)) { |
| 1796 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1797 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1798 | goto error; |
| 1799 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1800 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1801 | /* check collision with mandatory/min-elements */ |
| 1802 | if ((dev->target->flags & LYS_MAND_TRUE) || |
| 1803 | (dev->target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev->target)->min)) { |
| 1804 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "deviation"); |
| 1805 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 1806 | "Adding the \"default\" statement is forbidden on %s statement.", |
| 1807 | (dev->target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\""); |
| 1808 | goto error; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1809 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1810 | } else if (dev->deviate->mod == LY_DEVIATE_RPL) { |
| 1811 | /* check that there was a value before */ |
| 1812 | if (((dev->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((struct lys_node_leaf *)dev->target)->dflt) || |
| 1813 | (dev->target->nodetype == LYS_CHOICE && !((struct lys_node_choice *)dev->target)->dflt)) { |
| 1814 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1815 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
| 1816 | goto error; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1817 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | if (dev->target->nodetype == LYS_LEAFLIST) { |
| 1821 | /* reallocate default list in the target */ |
| 1822 | llist = (struct lys_node_leaflist *)dev->target; |
| 1823 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1824 | /* reallocate (enlarge) the unique array of the target */ |
| 1825 | llist->dflt = ly_realloc(llist->dflt, (c_dflt + llist->dflt_size) * sizeof *dev->deviate->dflt); |
| 1826 | } else if (dev->deviate->mod == LY_DEVIATE_RPL) { |
| 1827 | /* reallocate (replace) the unique array of the target */ |
| 1828 | for (i = 0; i < llist->dflt_size; i++) { |
| 1829 | lydict_remove(llist->module->ctx, llist->dflt[i]); |
| 1830 | } |
| 1831 | llist->dflt = ly_realloc(llist->dflt, c_dflt * sizeof *dev->deviate->dflt); |
| 1832 | llist->dflt_size = 0; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | dev->deviate->dflt = calloc(c_dflt, sizeof *dev->deviate->dflt); |
| 1837 | if (!dev->deviate->dflt) { |
| 1838 | LOGMEM; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1839 | goto error; |
| 1840 | } |
| 1841 | |
| 1842 | return EXIT_SUCCESS; |
| 1843 | |
| 1844 | error: |
| 1845 | return EXIT_FAILURE; |
| 1846 | } |
| 1847 | |
| 1848 | int |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1849 | yang_fill_deviate_default(struct ly_ctx *ctx, struct type_deviation *dev, char *exp) |
| 1850 | { |
| 1851 | struct lys_node *node; |
| 1852 | struct lys_node_choice *choice; |
| 1853 | struct lys_node_leaf *leaf; |
| 1854 | struct lys_node_leaflist *llist; |
| 1855 | int rc, i; |
| 1856 | unsigned int u; |
| 1857 | const char *value; |
| 1858 | |
| 1859 | value = lydict_insert_zc(ctx, exp); |
| 1860 | u = strlen(value); |
| 1861 | dev->deviate->dflt[dev->deviate->dflt_size++] = value; |
| 1862 | |
| 1863 | if (dev->target->nodetype == LYS_CHOICE) { |
| 1864 | choice = (struct lys_node_choice *)dev->target; |
| 1865 | rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node); |
| 1866 | if (rc || !node) { |
| 1867 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1868 | goto error; |
| 1869 | } |
| 1870 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1871 | if (!choice->dflt || (choice->dflt != node)) { |
| 1872 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1873 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
| 1874 | goto error; |
| 1875 | } |
| 1876 | } else { /* add or replace */ |
| 1877 | choice->dflt = node; |
| 1878 | if (!choice->dflt) { |
| 1879 | /* default branch not found */ |
| 1880 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1881 | goto error; |
| 1882 | } |
| 1883 | } |
| 1884 | } else if (dev->target->nodetype == LYS_LEAF) { |
| 1885 | leaf = (struct lys_node_leaf *)dev->target; |
| 1886 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1887 | if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) { |
| 1888 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1889 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
| 1890 | goto error; |
| 1891 | } |
| 1892 | /* remove value */ |
| 1893 | lydict_remove(ctx, leaf->dflt); |
| 1894 | leaf->dflt = NULL; |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1895 | leaf->flags &= ~LYS_DFLTJSON; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1896 | } else { /* add (already checked) and replace */ |
| 1897 | /* remove value */ |
| 1898 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1899 | leaf->flags &= ~LYS_DFLTJSON; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1900 | |
| 1901 | /* set new value */ |
| 1902 | leaf->dflt = lydict_insert(ctx, value, u); |
| 1903 | |
| 1904 | /* remember to check it later (it may not fit now, but the type can be deviated too) */ |
| 1905 | ly_set_add(dev->dflt_check, dev->target, 0); |
| 1906 | } |
| 1907 | } else { /* LYS_LEAFLIST */ |
| 1908 | llist = (struct lys_node_leaflist *)dev->target; |
| 1909 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1910 | /* find and remove the value in target list */ |
| 1911 | for (i = 0; i < llist->dflt_size; i++) { |
| 1912 | if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) { |
| 1913 | /* match, remove the value */ |
| 1914 | lydict_remove(llist->module->ctx, llist->dflt[i]); |
| 1915 | llist->dflt[i] = NULL; |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
| 1919 | if (i == llist->dflt_size) { |
| 1920 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1921 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node."); |
| 1922 | goto error; |
| 1923 | } |
| 1924 | } else { |
| 1925 | /* add or replace, anyway we place items into the deviate's list |
| 1926 | which propagates to the target */ |
| 1927 | /* we just want to check that the value isn't already in the list */ |
| 1928 | for (i = 0; i < llist->dflt_size; i++) { |
| 1929 | if (ly_strequal(llist->dflt[i], value, 1)) { |
| 1930 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 1931 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value); |
| 1932 | goto error; |
| 1933 | } |
| 1934 | } |
| 1935 | /* store it in target node */ |
| 1936 | llist->dflt[llist->dflt_size++] = lydict_insert(ctx, value, u); |
| 1937 | |
| 1938 | /* remember to check it later (it may not fit now, but the type can be deviated too) */ |
| 1939 | ly_set_add(dev->dflt_check, dev->target, 0); |
Radek Krejci | bd117f0 | 2016-11-04 16:28:08 +0100 | [diff] [blame] | 1940 | llist->flags &= ~LYS_DFLTJSON; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | return EXIT_SUCCESS; |
| 1945 | error: |
| 1946 | return EXIT_FAILURE; |
| 1947 | } |
| 1948 | |
| 1949 | |
| 1950 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1951 | yang_read_deviate_config(struct type_deviation *dev, uint8_t value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1952 | { |
| 1953 | if (dev->deviate->flags & LYS_CONFIG_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1954 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "config", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1955 | goto error; |
| 1956 | } |
| 1957 | |
| 1958 | /* for we deviate from RFC 6020 and allow config property even it is/is not |
| 1959 | * specified in the target explicitly since config property inherits. So we expect |
| 1960 | * that config is specified in every node. But for delete, we check that the value |
| 1961 | * is the same as here in deviation |
| 1962 | */ |
| 1963 | dev->deviate->flags |= value; |
| 1964 | |
| 1965 | /* add and replace are the same in this case */ |
| 1966 | /* remove current config value of the target ... */ |
| 1967 | dev->target->flags &= ~LYS_CONFIG_MASK; |
| 1968 | |
| 1969 | /* ... and replace it with the value specified in deviation */ |
| 1970 | dev->target->flags |= dev->deviate->flags & LYS_CONFIG_MASK; |
| 1971 | |
| 1972 | return EXIT_SUCCESS; |
| 1973 | |
| 1974 | error: |
| 1975 | return EXIT_FAILURE; |
| 1976 | } |
| 1977 | |
| 1978 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1979 | yang_read_deviate_mandatory(struct type_deviation *dev, uint8_t value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1980 | { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 1981 | struct lys_node *parent; |
| 1982 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1983 | if (dev->deviate->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1984 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "mandatory", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1985 | goto error; |
| 1986 | } |
| 1987 | |
| 1988 | /* check target node type */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1989 | if (!(dev->target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1990 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1991 | 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] | 1992 | goto error; |
| 1993 | } |
| 1994 | |
| 1995 | dev->deviate->flags |= value; |
| 1996 | |
| 1997 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1998 | /* check that there is no current value */ |
| 1999 | if (dev->target->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2000 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2001 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2002 | goto error; |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 2003 | } else { |
| 2004 | if (dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) { |
| 2005 | /* RFC 6020, 7.6.4 - default statement must not with mandatory true */ |
| 2006 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf"); |
| 2007 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 2008 | goto error; |
| 2009 | } else if (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt) { |
| 2010 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice"); |
| 2011 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 2012 | goto error; |
| 2013 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2014 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2015 | } else { /* replace */ |
| 2016 | if (!(dev->target->flags & LYS_MAND_MASK)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2017 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2018 | 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] | 2019 | goto error; |
| 2020 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2021 | } |
| 2022 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2023 | /* remove current mandatory value of the target ... */ |
| 2024 | dev->target->flags &= ~LYS_MAND_MASK; |
| 2025 | |
| 2026 | /* ... and replace it with the value specified in deviation */ |
| 2027 | dev->target->flags |= dev->deviate->flags & LYS_MAND_MASK; |
| 2028 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 2029 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
| 2030 | for (parent = dev->target->parent; |
| 2031 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION)); |
| 2032 | parent = parent->parent) { |
| 2033 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 2034 | /* stop also on presence containers */ |
| 2035 | break; |
| 2036 | } |
| 2037 | } |
| 2038 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 2039 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 2040 | if (lyp_check_mandatory_choice(parent)) { |
| 2041 | goto error; |
| 2042 | } |
| 2043 | } |
| 2044 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2045 | return EXIT_SUCCESS; |
| 2046 | |
| 2047 | error: |
| 2048 | return EXIT_FAILURE; |
| 2049 | } |
| 2050 | |
| 2051 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2052 | 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] | 2053 | { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2054 | uint32_t *ui32val, *min, *max; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2055 | |
| 2056 | /* check target node type */ |
| 2057 | if (dev->target->nodetype == LYS_LEAFLIST) { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2058 | max = &((struct lys_node_leaflist *)dev->target)->max; |
| 2059 | min = &((struct lys_node_leaflist *)dev->target)->min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2060 | } else if (dev->target->nodetype == LYS_LIST) { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2061 | max = &((struct lys_node_list *)dev->target)->max; |
| 2062 | min = &((struct lys_node_list *)dev->target)->min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2063 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2064 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 2065 | 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] | 2066 | goto error; |
| 2067 | } |
| 2068 | |
| 2069 | if (type) { |
| 2070 | dev->deviate->max = value; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2071 | dev->deviate->max_set = 1; |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2072 | ui32val = max; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2073 | } else { |
| 2074 | dev->deviate->min = value; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2075 | dev->deviate->min_set = 1; |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2076 | ui32val = min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 2080 | /* check that there is no current value */ |
| 2081 | if (*ui32val) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2082 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 2083 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2084 | goto error; |
| 2085 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2086 | } else if (dev->deviate->mod == LY_DEVIATE_RPL) { |
| 2087 | /* unfortunately, there is no way to check reliably that there |
| 2088 | * was a value before, it could have been the default */ |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | /* add (already checked) and replace */ |
| 2092 | /* set new value specified in deviation */ |
| 2093 | *ui32val = value; |
| 2094 | |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2095 | /* check min-elements is smaller than max-elements */ |
| 2096 | if (*max && *min > *max) { |
| 2097 | if (type) { |
| 2098 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"max-elements\".", value); |
| 2099 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\"."); |
| 2100 | } else { |
| 2101 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"min-elements\".", value); |
| 2102 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 2103 | } |
| 2104 | goto error; |
| 2105 | } |
| 2106 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2107 | return EXIT_SUCCESS; |
| 2108 | |
| 2109 | error: |
| 2110 | return EXIT_FAILURE; |
| 2111 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2112 | |
| 2113 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2114 | yang_check_deviate_must(struct ly_ctx *ctx, struct type_deviation *dev) |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2115 | { |
| 2116 | int i; |
| 2117 | |
| 2118 | /* find must to delete, we are ok with just matching conditions */ |
| 2119 | for (i = 0; i < *dev->trg_must_size; i++) { |
| 2120 | if (ly_strequal(dev->deviate->must[dev->deviate->must_size - 1].expr, (*dev->trg_must)[i].expr, 1)) { |
| 2121 | /* we have a match, free the must structure ... */ |
| 2122 | lys_restr_free(ctx, &((*dev->trg_must)[i])); |
| 2123 | /* ... and maintain the array */ |
| 2124 | (*dev->trg_must_size)--; |
| 2125 | if (i != *dev->trg_must_size) { |
| 2126 | (*dev->trg_must)[i].expr = (*dev->trg_must)[*dev->trg_must_size].expr; |
| 2127 | (*dev->trg_must)[i].dsc = (*dev->trg_must)[*dev->trg_must_size].dsc; |
| 2128 | (*dev->trg_must)[i].ref = (*dev->trg_must)[*dev->trg_must_size].ref; |
| 2129 | (*dev->trg_must)[i].eapptag = (*dev->trg_must)[*dev->trg_must_size].eapptag; |
| 2130 | (*dev->trg_must)[i].emsg = (*dev->trg_must)[*dev->trg_must_size].emsg; |
| 2131 | } |
| 2132 | if (!(*dev->trg_must_size)) { |
| 2133 | free(*dev->trg_must); |
| 2134 | *dev->trg_must = NULL; |
| 2135 | } else { |
| 2136 | (*dev->trg_must)[*dev->trg_must_size].expr = NULL; |
| 2137 | (*dev->trg_must)[*dev->trg_must_size].dsc = NULL; |
| 2138 | (*dev->trg_must)[*dev->trg_must_size].ref = NULL; |
| 2139 | (*dev->trg_must)[*dev->trg_must_size].eapptag = NULL; |
| 2140 | (*dev->trg_must)[*dev->trg_must_size].emsg = NULL; |
| 2141 | } |
| 2142 | |
| 2143 | i = -1; /* set match flag */ |
| 2144 | break; |
| 2145 | } |
| 2146 | } |
| 2147 | if (i != -1) { |
| 2148 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2149 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->must[dev->deviate->must_size - 1].expr, "must"); |
| 2150 | 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] | 2151 | return EXIT_FAILURE; |
| 2152 | } |
| 2153 | |
| 2154 | return EXIT_SUCCESS; |
| 2155 | } |
| 2156 | |
| 2157 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2158 | 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] | 2159 | { |
| 2160 | struct lys_node_list *list; |
| 2161 | int i, j; |
| 2162 | |
| 2163 | list = (struct lys_node_list *)dev->target; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2164 | 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] | 2165 | dev->deviate->unique_size++; |
| 2166 | goto error; |
| 2167 | } |
| 2168 | |
| 2169 | /* find unique structures to delete */ |
| 2170 | for (i = 0; i < list->unique_size; i++) { |
| 2171 | if (list->unique[i].expr_size != dev->deviate->unique[dev->deviate->unique_size].expr_size) { |
| 2172 | continue; |
| 2173 | } |
| 2174 | |
| 2175 | for (j = 0; j < dev->deviate->unique[dev->deviate->unique_size].expr_size; j++) { |
| 2176 | if (!ly_strequal(list->unique[i].expr[j], dev->deviate->unique[dev->deviate->unique_size].expr[j], 1)) { |
| 2177 | break; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | if (j == dev->deviate->unique[dev->deviate->unique_size].expr_size) { |
| 2182 | /* we have a match, free the unique structure ... */ |
| 2183 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2184 | lydict_remove(module->ctx, list->unique[i].expr[j]); |
| 2185 | } |
| 2186 | free(list->unique[i].expr); |
| 2187 | /* ... and maintain the array */ |
| 2188 | list->unique_size--; |
| 2189 | if (i != list->unique_size) { |
| 2190 | list->unique[i].expr_size = list->unique[list->unique_size].expr_size; |
| 2191 | list->unique[i].expr = list->unique[list->unique_size].expr; |
| 2192 | } |
| 2193 | |
| 2194 | if (!list->unique_size) { |
| 2195 | free(list->unique); |
| 2196 | list->unique = NULL; |
| 2197 | } else { |
| 2198 | list->unique[list->unique_size].expr_size = 0; |
| 2199 | list->unique[list->unique_size].expr = NULL; |
| 2200 | } |
| 2201 | |
| 2202 | i = -1; /* set match flag */ |
| 2203 | break; |
| 2204 | } |
| 2205 | } |
| 2206 | dev->deviate->unique_size++; |
| 2207 | |
| 2208 | if (i != -1) { |
| 2209 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2210 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique"); |
| 2211 | 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] | 2212 | goto error; |
| 2213 | } |
| 2214 | |
| 2215 | free(value); |
| 2216 | return EXIT_SUCCESS; |
| 2217 | |
| 2218 | error: |
| 2219 | free(value); |
| 2220 | return EXIT_FAILURE; |
| 2221 | } |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2222 | |
| 2223 | int |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2224 | 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] | 2225 | { |
| 2226 | int i, rc; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2227 | unsigned int u; |
| 2228 | const char *value, *target_name; |
| 2229 | struct lys_node_leaflist *llist; |
| 2230 | struct lys_node_leaf *leaf; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2231 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2232 | /* now check whether default value, if any, matches the type */ |
| 2233 | for (u = 0; u < dflt_check->number; ++u) { |
| 2234 | value = NULL; |
| 2235 | rc = EXIT_SUCCESS; |
| 2236 | if (dflt_check->set.s[u]->nodetype == LYS_LEAF) { |
| 2237 | leaf = (struct lys_node_leaf *)dflt_check->set.s[u]; |
| 2238 | target_name = leaf->name; |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2239 | value = leaf->dflt; |
| 2240 | 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] | 2241 | } else { /* LYS_LEAFLIST */ |
| 2242 | llist = (struct lys_node_leaflist *)dflt_check->set.s[u]; |
| 2243 | target_name = llist->name; |
| 2244 | for (i = 0; i < llist->dflt_size; i++) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2245 | rc = unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT, |
| 2246 | (struct lys_node *)(&llist->dflt[i])); |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2247 | if (rc == -1) { |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2248 | value = llist->dflt[i]; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2249 | break; |
| 2250 | } |
| 2251 | } |
| 2252 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2253 | if (rc == -1) { |
| 2254 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 2255 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 2256 | "The default value \"%s\" of the deviated node \"%s\"no longer matches its type.", |
| 2257 | target_name); |
| 2258 | return EXIT_FAILURE; |
| 2259 | } |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2260 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2261 | |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2262 | return EXIT_SUCCESS; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2263 | |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2266 | static int |
| 2267 | yang_fill_include(struct lys_module *trg, char *value, struct lys_include *inc, |
| 2268 | struct unres_schema *unres) |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2269 | { |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2270 | struct lys_submodule *submodule; |
| 2271 | struct lys_module *module; |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2272 | const char *str; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2273 | int rc; |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2274 | int ret = 0; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2275 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2276 | str = lydict_insert_zc(trg->ctx, value); |
| 2277 | if (trg->version) { |
| 2278 | submodule = (struct lys_submodule *)trg; |
| 2279 | module = ((struct lys_submodule *)trg)->belongsto; |
| 2280 | } else { |
| 2281 | submodule = NULL; |
| 2282 | module = trg; |
| 2283 | } |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2284 | rc = lyp_check_include(module, submodule, str, inc, unres); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2285 | if (!rc) { |
| 2286 | /* success, copy the filled data into the final array */ |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2287 | memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc); |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 2288 | trg->inc_size++; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2289 | } else if (rc == -1) { |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2290 | ret = -1; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2291 | } |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2292 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2293 | lydict_remove(trg->ctx, str); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2294 | return ret; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2295 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2296 | |
| 2297 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2298 | 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] | 2299 | { |
| 2300 | char *prefix; |
| 2301 | char *identif; |
| 2302 | const char *ns = NULL; |
| 2303 | int i; |
| 2304 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2305 | /* check to the same pointer */ |
| 2306 | if (data_node != actual) { |
| 2307 | return EXIT_SUCCESS; |
| 2308 | } |
| 2309 | |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2310 | prefix = strdup(value); |
| 2311 | if (!prefix) { |
| 2312 | LOGMEM; |
| 2313 | goto error; |
| 2314 | } |
| 2315 | /* find prefix anf identificator*/ |
| 2316 | identif = strchr(prefix, ':'); |
Pavol Vican | fbd0278 | 2016-08-29 11:14:45 +0200 | [diff] [blame] | 2317 | if (!identif) { |
| 2318 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, prefix); |
| 2319 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The extension must have prefix."); |
| 2320 | goto error; |
| 2321 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2322 | *identif = '\0'; |
| 2323 | identif++; |
| 2324 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2325 | for(i = 0; i < module->imp_size; ++i) { |
| 2326 | if (!strcmp(module->imp[i].prefix, prefix)) { |
| 2327 | ns = module->imp[i].module->ns; |
| 2328 | break; |
| 2329 | } |
| 2330 | } |
Pavol Vican | 1ff1b7b | 2016-04-07 09:51:49 +0200 | [diff] [blame] | 2331 | if (!ns && !strcmp(module->prefix, prefix)) { |
| 2332 | ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns; |
| 2333 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2334 | if (ns && !strcmp(ns, LY_NSNACM)) { |
| 2335 | if (!strcmp(identif, "default-deny-write")) { |
| 2336 | data_node->nacm |= LYS_NACM_DENYW; |
| 2337 | } else if (!strcmp(identif, "default-deny-all")) { |
| 2338 | data_node->nacm |= LYS_NACM_DENYA; |
| 2339 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2340 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif); |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2341 | goto error; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2342 | } |
| 2343 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2344 | free(prefix); |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2345 | return EXIT_SUCCESS; |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2346 | |
| 2347 | error: |
| 2348 | free(prefix); |
| 2349 | return EXIT_FAILURE; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | void |
| 2353 | nacm_inherit(struct lys_module *module) |
| 2354 | { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2355 | struct lys_node *next, *elem, *tmp_node, *tmp_child; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2356 | |
| 2357 | LY_TREE_DFS_BEGIN(module->data, next, elem) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2358 | tmp_node = NULL; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2359 | if (elem->parent) { |
| 2360 | switch (elem->nodetype) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2361 | case LYS_GROUPING: |
| 2362 | /* extension nacm not inherited*/ |
| 2363 | break; |
| 2364 | case LYS_CHOICE: |
| 2365 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2366 | case LYS_ANYDATA: |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2367 | case LYS_USES: |
| 2368 | if (elem->parent->nodetype != LYS_GROUPING) { |
| 2369 | elem->nacm |= elem->parent->nacm; |
| 2370 | } |
| 2371 | break; |
| 2372 | case LYS_CONTAINER: |
| 2373 | case LYS_LIST: |
| 2374 | case LYS_CASE: |
| 2375 | case LYS_NOTIF: |
| 2376 | case LYS_RPC: |
| 2377 | case LYS_INPUT: |
| 2378 | case LYS_OUTPUT: |
| 2379 | case LYS_AUGMENT: |
| 2380 | elem->nacm |= elem->parent->nacm; |
| 2381 | break; |
| 2382 | case LYS_LEAF: |
| 2383 | case LYS_LEAFLIST: |
| 2384 | tmp_node = elem; |
| 2385 | tmp_child = elem->child; |
| 2386 | elem->child = NULL; |
| 2387 | default: |
| 2388 | break; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | LY_TREE_DFS_END(module->data, next, elem); |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2392 | if (tmp_node) { |
| 2393 | tmp_node->child = tmp_child; |
| 2394 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2395 | } |
| 2396 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2397 | |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2398 | int |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2399 | store_flags(struct lys_node *node, uint8_t flags, int config_opt) |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2400 | { |
Michal Vasko | e10d14a | 2016-08-26 15:17:13 +0200 | [diff] [blame] | 2401 | struct lys_node *elem; |
| 2402 | |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2403 | node->flags |= (config_opt == CONFIG_IGNORE) ? flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET)): flags; |
| 2404 | if (config_opt == CONFIG_INHERIT_ENABLE) { |
| 2405 | if (!(node->flags & LYS_CONFIG_MASK)) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2406 | /* get config flag from parent */ |
Radek Krejci | f71f48f | 2016-10-25 16:37:24 +0200 | [diff] [blame] | 2407 | if (node->parent) { |
| 2408 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2409 | } else { |
| 2410 | /* default config is true */ |
| 2411 | node->flags |= LYS_CONFIG_W; |
| 2412 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2413 | } else { |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2414 | /* do we even care about config flags? */ |
| 2415 | 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] | 2416 | |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2417 | if (!elem && (node->flags & LYS_CONFIG_W) && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 2418 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 2419 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 2420 | return EXIT_FAILURE; |
| 2421 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2422 | } |
| 2423 | } |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2424 | |
| 2425 | return EXIT_SUCCESS; |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2426 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2427 | |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2428 | void |
| 2429 | store_config_flag(struct lys_node *node, int config_opt) |
| 2430 | { |
| 2431 | if (config_opt == CONFIG_IGNORE) { |
| 2432 | node->flags |= node->flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET)); |
| 2433 | } else if (config_opt == CONFIG_INHERIT_ENABLE) { |
| 2434 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 2435 | /* get config flag from parent */ |
| 2436 | if (node->parent) { |
| 2437 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 2438 | } else { |
| 2439 | /* default config is true */ |
| 2440 | node->flags |= LYS_CONFIG_W; |
| 2441 | } |
| 2442 | } |
| 2443 | } |
| 2444 | } |
| 2445 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2446 | int |
| 2447 | yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, |
| 2448 | const char *data, unsigned int size_data, struct lys_node **node) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2449 | { |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2450 | unsigned int size; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2451 | YY_BUFFER_STATE bp; |
Pavol Vican | 802af1e | 2016-03-23 20:42:26 +0100 | [diff] [blame] | 2452 | yyscan_t scanner = NULL; |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 2453 | int ret = EXIT_SUCCESS, remove_import = 1; |
| 2454 | struct lys_module *trg; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2455 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2456 | size = (size_data) ? size_data : strlen(data) + 2; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2457 | yylex_init(&scanner); |
| 2458 | bp = yy_scan_buffer((char *)data, size, scanner); |
| 2459 | yy_switch_to_buffer(bp, scanner); |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame] | 2460 | if (yyparse(scanner, NULL, module, submodule, unres, node, &remove_import)) { |
| 2461 | if (remove_import) { |
| 2462 | trg = (submodule) ? (struct lys_module *)submodule : module; |
| 2463 | yang_free_import(trg->ctx, trg->imp, 0, trg->imp_size); |
| 2464 | yang_free_include(trg->ctx, trg->inc, 0, trg->inc_size); |
| 2465 | trg->inc_size = 0; |
| 2466 | trg->imp_size = 0; |
| 2467 | } |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2468 | ret = EXIT_FAILURE; |
| 2469 | } |
| 2470 | yy_delete_buffer(bp, scanner); |
| 2471 | yylex_destroy(scanner); |
| 2472 | return ret; |
| 2473 | } |
| 2474 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2475 | struct lys_module * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2476 | 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] | 2477 | { |
| 2478 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2479 | struct lys_module *tmp_module, *module = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2480 | struct unres_schema *unres = NULL; |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2481 | struct lys_node *node = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2482 | |
| 2483 | unres = calloc(1, sizeof *unres); |
| 2484 | if (!unres) { |
| 2485 | LOGMEM; |
| 2486 | goto error; |
| 2487 | } |
| 2488 | |
| 2489 | module = calloc(1, sizeof *module); |
| 2490 | if (!module) { |
| 2491 | LOGMEM; |
Pavol Vican | f7994fb | 2016-04-05 21:55:53 +0200 | [diff] [blame] | 2492 | goto error; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | /* initiale module */ |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 2496 | module->ctx = ctx; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2497 | module->type = 0; |
| 2498 | module->implemented = (implement ? 1 : 0); |
| 2499 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2500 | if (yang_parse_mem(module, NULL, unres, data, size, &node)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2501 | free_yang_common(module, node); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2502 | goto error; |
| 2503 | } |
| 2504 | |
| 2505 | if (yang_check_sub_module(module, unres, node)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2506 | goto error; |
| 2507 | } |
| 2508 | |
| 2509 | if (module && unres->count && resolve_unres_schema(module, unres)) { |
| 2510 | goto error; |
| 2511 | } |
| 2512 | |
| 2513 | if (revision) { |
| 2514 | /* check revision of the parsed model */ |
| 2515 | if (!module->rev_size || strcmp(revision, module->rev[0].date)) { |
| 2516 | LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", |
| 2517 | module->name, module->rev[0].date, revision); |
| 2518 | goto error; |
| 2519 | } |
| 2520 | } |
| 2521 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2522 | tmp_module = module; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2523 | if (lyp_ctx_add_module(&module)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2524 | goto error; |
| 2525 | } |
| 2526 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2527 | if (module == tmp_module) { |
| 2528 | nacm_inherit(module); |
| 2529 | } |
| 2530 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 2531 | if (module->deviation_size && !module->implemented) { |
| 2532 | LOGVRB("Module \"%s\" includes deviations, changing its conformance to \"implement\".", module->name); |
| 2533 | /* deviations always causes target to be made implemented, |
| 2534 | * but augents and leafrefs not, so we have to apply them now */ |
| 2535 | if (lys_set_implemented(module)) { |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2536 | goto error; |
| 2537 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2538 | } |
| 2539 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2540 | unres_schema_free(NULL, &unres); |
| 2541 | LOGVRB("Module \"%s\" successfully parsed.", module->name); |
| 2542 | return module; |
| 2543 | |
| 2544 | error: |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2545 | /* cleanup */ |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2546 | unres_schema_free(module, &unres); |
| 2547 | if (!module || !module->name) { |
| 2548 | free(module); |
Radek Krejci | 48cfa0f | 2016-11-08 19:18:34 +0100 | [diff] [blame] | 2549 | if (ly_vecode != LYVE_SUBMODULE) { |
| 2550 | LOGERR(ly_errno, "Module parsing failed."); |
| 2551 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2552 | return NULL; |
| 2553 | } |
| 2554 | |
| 2555 | LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2556 | |
| 2557 | lys_sub_module_remove_devs_augs(module); |
| 2558 | lys_free(module, NULL, 1); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2559 | return NULL; |
| 2560 | } |
| 2561 | |
| 2562 | struct lys_submodule * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2563 | 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] | 2564 | { |
| 2565 | struct lys_submodule *submodule; |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2566 | struct lys_node *node = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2567 | |
| 2568 | submodule = calloc(1, sizeof *submodule); |
| 2569 | if (!submodule) { |
| 2570 | LOGMEM; |
| 2571 | goto error; |
| 2572 | } |
| 2573 | |
| 2574 | submodule->ctx = module->ctx; |
| 2575 | submodule->type = 1; |
| 2576 | submodule->belongsto = module; |
| 2577 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2578 | if (yang_parse_mem(module, submodule, unres, data, size, &node)) { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2579 | free_yang_common((struct lys_module *)submodule, node); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2580 | goto error; |
| 2581 | } |
| 2582 | |
| 2583 | if (yang_check_sub_module((struct lys_module *)submodule, unres, node)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2584 | goto error; |
| 2585 | } |
| 2586 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2587 | LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2588 | return submodule; |
| 2589 | |
| 2590 | error: |
| 2591 | /* cleanup */ |
| 2592 | unres_schema_free((struct lys_module *)submodule, &unres); |
| 2593 | |
| 2594 | if (!submodule || !submodule->name) { |
| 2595 | free(submodule); |
| 2596 | LOGERR(ly_errno, "Submodule parsing failed."); |
| 2597 | return NULL; |
| 2598 | } |
| 2599 | |
| 2600 | LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name); |
| 2601 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2602 | lys_sub_module_remove_devs_augs((struct lys_module *)submodule); |
| 2603 | lys_submodule_module_data_free(submodule); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2604 | lys_submodule_free(submodule, NULL); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2605 | return NULL; |
| 2606 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2607 | |
| 2608 | static int |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2609 | read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output) |
| 2610 | { |
| 2611 | int k = 0, j; |
| 2612 | |
| 2613 | while (in_index < size) { |
| 2614 | if (input[in_index] == ' ') { |
| 2615 | k++; |
| 2616 | } else if (input[in_index] == '\t') { |
| 2617 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2618 | k += 8; |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2619 | } else if (input[in_index] == '\\' && input[in_index + 1] == 't') { |
| 2620 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2621 | k += 8; |
| 2622 | ++in_index; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2623 | } else { |
| 2624 | break; |
| 2625 | } |
| 2626 | ++in_index; |
| 2627 | if (k >= indent) { |
| 2628 | for (j = k - indent; j > 0; --j) { |
| 2629 | output[*out_index] = ' '; |
| 2630 | ++(*out_index); |
| 2631 | } |
| 2632 | break; |
| 2633 | } |
| 2634 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2635 | return in_index - 1; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | char * |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2639 | yang_read_string(const char *input, char *output, int size, int offset, int indent, int version) { |
| 2640 | int i = 0, out_index = offset, space = 0; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2641 | |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2642 | while (i < size) { |
| 2643 | switch (input[i]) { |
| 2644 | case '\n': |
| 2645 | out_index -= space; |
| 2646 | output[out_index] = '\n'; |
| 2647 | space = 0; |
| 2648 | i = read_indent(input, indent, size, i + 1, &out_index, output); |
| 2649 | break; |
| 2650 | case ' ': |
| 2651 | case '\t': |
| 2652 | output[out_index] = input[i]; |
| 2653 | ++space; |
| 2654 | break; |
| 2655 | case '\\': |
| 2656 | if (input[i + 1] == 'n') { |
| 2657 | out_index -= space; |
| 2658 | output[out_index] = '\n'; |
| 2659 | space = 0; |
| 2660 | i = read_indent(input, indent, size, i + 2, &out_index, output); |
| 2661 | } else if (input[i + 1] == 't') { |
| 2662 | output[out_index] = '\t'; |
| 2663 | ++i; |
| 2664 | ++space; |
| 2665 | } else if (input[i + 1] == '\\') { |
| 2666 | output[out_index] = '\\'; |
| 2667 | ++i; |
| 2668 | } else if ((i + 1) != size && input[i + 1] == '"') { |
| 2669 | output[out_index] = '"'; |
| 2670 | ++i; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2671 | } else { |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2672 | if (version < 2) { |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2673 | output[out_index] = input[i]; |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2674 | } else { |
| 2675 | /* YANG 1.1 backslash must not be followed by any other character */ |
| 2676 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input); |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2677 | return NULL; |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2678 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2679 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2680 | break; |
| 2681 | default: |
| 2682 | output[out_index] = input[i]; |
| 2683 | space = 0; |
| 2684 | break; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2685 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2686 | ++i; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2687 | ++out_index; |
| 2688 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2689 | output[out_index] = '\0'; |
| 2690 | if (size != out_index) { |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2691 | output = realloc(output, out_index + 1); |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2692 | if (!output) { |
| 2693 | LOGMEM; |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2694 | return NULL; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2695 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2696 | } |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2697 | return output; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2698 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2699 | |
| 2700 | /* free function */ |
| 2701 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2702 | static void yang_type_free(struct ly_ctx *ctx, struct lys_type *type) |
| 2703 | { |
| 2704 | struct yang_type *stype = (struct yang_type *)type->der; |
| 2705 | int i; |
| 2706 | |
| 2707 | if (!stype) { |
| 2708 | return ; |
| 2709 | } |
| 2710 | lydict_remove(ctx, stype->name); |
| 2711 | type->base = stype->base; |
| 2712 | if (type->base == LY_TYPE_IDENT) { |
| 2713 | for (i = 0; i < type->info.ident.count; ++i) { |
| 2714 | free(type->info.ident.ref[i]); |
| 2715 | } |
| 2716 | } |
| 2717 | lys_type_free(ctx, type); |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2718 | type->base = LY_TYPE_DER; |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2719 | free(stype); |
| 2720 | } |
| 2721 | |
| 2722 | static void |
| 2723 | yang_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf, uint8_t start, uint8_t size) |
| 2724 | { |
| 2725 | uint8_t i; |
| 2726 | |
| 2727 | assert(ctx); |
| 2728 | if (!tpdf) { |
| 2729 | return; |
| 2730 | } |
| 2731 | |
| 2732 | for (i = start; i < size; ++i) { |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2733 | lydict_remove(ctx, tpdf[i].name); |
| 2734 | lydict_remove(ctx, tpdf[i].dsc); |
| 2735 | lydict_remove(ctx, tpdf[i].ref); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2736 | |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2737 | yang_type_free(ctx, &tpdf[i].type); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2738 | |
Pavol Vican | cee1080 | 2016-11-22 15:48:35 +0100 | [diff] [blame] | 2739 | lydict_remove(ctx, tpdf[i].units); |
| 2740 | lydict_remove(ctx, tpdf[i].dflt); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2741 | } |
| 2742 | } |
| 2743 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2744 | static void |
| 2745 | yang_free_import(struct ly_ctx *ctx, struct lys_import *imp, uint8_t start, uint8_t size) |
| 2746 | { |
| 2747 | uint8_t i; |
| 2748 | |
| 2749 | for (i = start; i < size; ++i){ |
| 2750 | free((char *)imp[i].module); |
| 2751 | lydict_remove(ctx, imp[i].prefix); |
| 2752 | lydict_remove(ctx, imp[i].dsc); |
| 2753 | lydict_remove(ctx, imp[i].ref); |
| 2754 | } |
| 2755 | } |
| 2756 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2757 | static void |
| 2758 | yang_free_include(struct ly_ctx *ctx, struct lys_include *inc, uint8_t start, uint8_t size) |
| 2759 | { |
| 2760 | uint8_t i; |
| 2761 | |
| 2762 | for (i = start; i < size; ++i){ |
| 2763 | free((char *)inc[i].submodule); |
| 2764 | lydict_remove(ctx, inc[i].dsc); |
| 2765 | lydict_remove(ctx, inc[i].ref); |
| 2766 | } |
| 2767 | } |
| 2768 | |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 2769 | static void |
| 2770 | yang_free_ident_base(struct lys_ident *ident, uint32_t start, uint32_t size) |
| 2771 | { |
| 2772 | uint32_t i; |
| 2773 | uint8_t j; |
| 2774 | |
| 2775 | /* free base name */ |
| 2776 | for (i = start; i < size; ++i) { |
| 2777 | for (j = 0; j < ident[i].base_size; ++j) { |
| 2778 | free(ident[i].base[j]); |
| 2779 | } |
| 2780 | } |
| 2781 | } |
| 2782 | |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2783 | static void |
| 2784 | yang_free_grouping(struct ly_ctx *ctx, struct lys_node_grp * grp) |
| 2785 | { |
| 2786 | yang_tpdf_free(ctx, grp->tpdf, 0, grp->tpdf_size); |
| 2787 | free(grp->tpdf); |
| 2788 | } |
| 2789 | |
| 2790 | static void |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2791 | yang_free_container(struct ly_ctx *ctx, struct lys_node_container * cont) |
| 2792 | { |
| 2793 | uint8_t i; |
| 2794 | |
| 2795 | yang_tpdf_free(ctx, cont->tpdf, 0, cont->tpdf_size); |
| 2796 | free(cont->tpdf); |
| 2797 | lydict_remove(ctx, cont->presence); |
| 2798 | |
| 2799 | for (i = 0; cont->must_size; ++i) { |
| 2800 | lys_restr_free(ctx, &cont->must[i]); |
| 2801 | } |
| 2802 | free(cont->must); |
| 2803 | |
| 2804 | lys_when_free(ctx, cont->when); |
| 2805 | } |
| 2806 | |
| 2807 | static void |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 2808 | yang_free_leaf(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
| 2809 | { |
| 2810 | uint8_t i; |
| 2811 | |
| 2812 | for (i = 0; i < leaf->must_size; i++) { |
| 2813 | lys_restr_free(ctx, &leaf->must[i]); |
| 2814 | } |
| 2815 | free(leaf->must); |
| 2816 | |
| 2817 | lys_when_free(ctx, leaf->when); |
| 2818 | |
| 2819 | yang_type_free(ctx, &leaf->type); |
| 2820 | lydict_remove(ctx, leaf->units); |
| 2821 | lydict_remove(ctx, leaf->dflt); |
| 2822 | } |
| 2823 | |
| 2824 | static void |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2825 | yang_free_leaflist(struct ly_ctx *ctx, struct lys_node_leaflist *leaflist) |
| 2826 | { |
| 2827 | uint8_t i; |
| 2828 | |
| 2829 | for (i = 0; i < leaflist->must_size; i++) { |
| 2830 | lys_restr_free(ctx, &leaflist->must[i]); |
| 2831 | } |
| 2832 | free(leaflist->must); |
| 2833 | |
| 2834 | for (i = 0; i < leaflist->dflt_size; i++) { |
| 2835 | lydict_remove(ctx, leaflist->dflt[i]); |
| 2836 | } |
| 2837 | free(leaflist->dflt); |
| 2838 | |
| 2839 | lys_when_free(ctx, leaflist->when); |
| 2840 | |
| 2841 | yang_type_free(ctx, &leaflist->type); |
| 2842 | lydict_remove(ctx, leaflist->units); |
| 2843 | } |
| 2844 | |
| 2845 | static void |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 2846 | yang_free_list(struct ly_ctx *ctx, struct lys_node_list *list) |
| 2847 | { |
| 2848 | uint8_t i; |
| 2849 | |
| 2850 | yang_tpdf_free(ctx, list->tpdf, 0, list->tpdf_size); |
| 2851 | free(list->tpdf); |
| 2852 | |
| 2853 | for (i = 0; i < list->must_size; ++i) { |
| 2854 | lys_restr_free(ctx, &list->must[i]); |
| 2855 | } |
| 2856 | free(list->must); |
| 2857 | |
| 2858 | lys_when_free(ctx, list->when); |
| 2859 | |
| 2860 | for (i = 0; i < list->unique_size; ++i) { |
| 2861 | free(list->unique[i].expr); |
| 2862 | } |
| 2863 | free(list->unique); |
| 2864 | |
| 2865 | free(list->keys); |
| 2866 | } |
| 2867 | |
| 2868 | static void |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 2869 | yang_free_choice(struct ly_ctx *ctx, struct lys_node_choice *choice) |
| 2870 | { |
| 2871 | free(choice->dflt); |
| 2872 | lys_when_free(ctx, choice->when); |
| 2873 | } |
| 2874 | |
| 2875 | static void |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 2876 | yang_free_anydata(struct ly_ctx *ctx, struct lys_node_anydata *anydata) |
| 2877 | { |
| 2878 | uint8_t i; |
| 2879 | |
| 2880 | for (i = 0; i < anydata->must_size; ++i) { |
| 2881 | lys_restr_free(ctx, &anydata->must[i]); |
| 2882 | } |
| 2883 | free(anydata->must); |
| 2884 | |
| 2885 | lys_when_free(ctx, anydata->when); |
| 2886 | } |
| 2887 | |
| 2888 | static void |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame^] | 2889 | yang_free_inout(struct ly_ctx *ctx, struct lys_node_inout *inout) |
| 2890 | { |
| 2891 | uint8_t i; |
| 2892 | |
| 2893 | yang_tpdf_free(ctx, inout->tpdf, 0, inout->tpdf_size); |
| 2894 | free(inout->tpdf); |
| 2895 | |
| 2896 | for (i = 0; i < inout->must_size; ++i) { |
| 2897 | lys_restr_free(ctx, &inout->must[i]); |
| 2898 | } |
| 2899 | free(inout->must); |
| 2900 | } |
| 2901 | |
| 2902 | static void |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2903 | yang_free_nodes(struct ly_ctx *ctx, struct lys_node *node) |
| 2904 | { |
| 2905 | struct lys_node *tmp, *child, *sibling; |
| 2906 | |
| 2907 | if (!node) { |
| 2908 | return; |
| 2909 | } |
| 2910 | tmp = node; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2911 | |
| 2912 | while (tmp) { |
Pavol Vican | e87ff8d | 2016-11-24 18:25:01 +0100 | [diff] [blame] | 2913 | child = tmp->child; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2914 | sibling = tmp->next; |
| 2915 | /* common part */ |
| 2916 | lydict_remove(ctx, tmp->name); |
| 2917 | if (!(tmp->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Pavol Vican | e87ff8d | 2016-11-24 18:25:01 +0100 | [diff] [blame] | 2918 | lys_iffeature_free(tmp->iffeature, tmp->iffeature_size); |
| 2919 | lydict_remove(ctx, tmp->dsc); |
| 2920 | lydict_remove(ctx, tmp->ref); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | switch (tmp->nodetype) { |
| 2924 | case LYS_GROUPING: |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 2925 | case LYS_RPC: |
| 2926 | case LYS_ACTION: |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2927 | yang_free_grouping(ctx, (struct lys_node_grp *)tmp); |
| 2928 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 2929 | case LYS_CONTAINER: |
| 2930 | yang_free_container(ctx, (struct lys_node_container *)tmp); |
| 2931 | break; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 2932 | case LYS_LEAF: |
| 2933 | yang_free_leaf(ctx, (struct lys_node_leaf *)tmp); |
| 2934 | break; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 2935 | case LYS_LEAFLIST: |
| 2936 | yang_free_leaflist(ctx, (struct lys_node_leaflist *)tmp); |
| 2937 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 2938 | case LYS_LIST: |
| 2939 | yang_free_list(ctx, (struct lys_node_list *)tmp); |
| 2940 | break; |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 2941 | case LYS_CHOICE: |
| 2942 | yang_free_choice(ctx, (struct lys_node_choice *)tmp); |
| 2943 | break; |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 2944 | case LYS_CASE: |
| 2945 | lys_when_free(ctx, ((struct lys_node_case *)tmp)->when); |
| 2946 | break; |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 2947 | case LYS_ANYXML: |
| 2948 | case LYS_ANYDATA: |
| 2949 | yang_free_anydata(ctx, (struct lys_node_anydata *)tmp); |
| 2950 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame^] | 2951 | case LYS_INPUT: |
| 2952 | case LYS_OUTPUT: |
| 2953 | yang_free_inout(ctx, (struct lys_node_inout *)tmp); |
| 2954 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2955 | default: |
| 2956 | break; |
| 2957 | } |
| 2958 | |
| 2959 | yang_free_nodes(ctx, child); |
| 2960 | free(tmp); |
| 2961 | tmp = sibling; |
| 2962 | } |
| 2963 | } |
| 2964 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2965 | /* free common item from module and submodule */ |
| 2966 | static void |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2967 | free_yang_common(struct lys_module *module, struct lys_node *node) |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2968 | { |
| 2969 | yang_tpdf_free(module->ctx, module->tpdf, 0, module->tpdf_size); |
| 2970 | module->tpdf_size = 0; |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 2971 | yang_free_ident_base(module->ident, 0, module->ident_size); |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 2972 | yang_free_nodes(module->ctx, node); |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 2973 | } |
| 2974 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2975 | /* check function*/ |
| 2976 | |
| 2977 | int |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2978 | yang_check_imports(struct lys_module *module, struct unres_schema *unres) |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2979 | { |
| 2980 | struct lys_import *imp; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2981 | struct lys_include *inc; |
| 2982 | uint8_t imp_size, inc_size, j = 0, i = 0; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2983 | size_t size; |
| 2984 | char *s; |
| 2985 | |
| 2986 | imp = module->imp; |
| 2987 | imp_size = module->imp_size; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2988 | inc = module->inc; |
| 2989 | inc_size = module->inc_size; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2990 | |
| 2991 | if (imp_size) { |
| 2992 | size = (imp_size * sizeof *module->imp) + sizeof(void*); |
| 2993 | module->imp_size = 0; |
| 2994 | module->imp = calloc(1, size); |
| 2995 | if (!module->imp) { |
| 2996 | LOGMEM; |
| 2997 | goto error; |
| 2998 | } |
| 2999 | /* set stop block for possible realloc */ |
| 3000 | module->imp[imp_size].module = (void*)0x1; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3001 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3002 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3003 | if (inc_size) { |
| 3004 | size = (inc_size * sizeof *module->inc) + sizeof(void*); |
| 3005 | module->inc_size = 0; |
| 3006 | module->inc = calloc(1, size); |
| 3007 | if (!module->inc) { |
| 3008 | LOGMEM; |
| 3009 | goto error; |
| 3010 | } |
| 3011 | /* set stop block for possible realloc */ |
| 3012 | module->inc[inc_size].submodule = (void*)0x1; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | for (i = 0; i < imp_size; ++i) { |
| 3016 | s = (char *) imp[i].module; |
| 3017 | imp[i].module = NULL; |
| 3018 | if (yang_fill_import(module, &imp[i], &module->imp[module->imp_size], s)) { |
| 3019 | ++i; |
| 3020 | goto error; |
| 3021 | } |
| 3022 | } |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3023 | for (j = 0; j < inc_size; ++j) { |
| 3024 | s = (char *) inc[i].submodule; |
| 3025 | inc[i].submodule = NULL; |
| 3026 | if (yang_fill_include(module, s, &inc[i], unres)) { |
| 3027 | ++i; |
| 3028 | goto error; |
| 3029 | } |
| 3030 | } |
| 3031 | free(inc); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3032 | free(imp); |
| 3033 | |
| 3034 | return EXIT_SUCCESS; |
| 3035 | |
| 3036 | error: |
| 3037 | yang_free_import(module->ctx, imp, i, imp_size); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3038 | yang_free_include(module->ctx, inc, j, inc_size); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3039 | free(imp); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 3040 | free(inc); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 3041 | return EXIT_FAILURE; |
| 3042 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3043 | |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3044 | static int |
| 3045 | yang_check_type_iffeature(struct lys_module *module, struct unres_schema *unres, struct lys_type *type) |
| 3046 | { |
| 3047 | uint i, j, size; |
| 3048 | uint8_t iffeature_size; |
| 3049 | LY_DATA_TYPE stype; |
| 3050 | |
| 3051 | if (((struct yang_type *)type->der)->base == LY_TYPE_ENUM) { |
| 3052 | stype = LY_TYPE_ENUM; |
| 3053 | size = type->info.enums.count; |
| 3054 | } else if (((struct yang_type *)type->der)->base == LY_TYPE_BITS) { |
| 3055 | stype = LY_TYPE_ENUM; |
| 3056 | size = type->info.enums.count; |
| 3057 | } else { |
| 3058 | return EXIT_SUCCESS; |
| 3059 | } |
| 3060 | |
| 3061 | for (i = 0; i < size; ++i) { |
| 3062 | if (stype == LY_TYPE_ENUM) { |
| 3063 | iffeature_size = type->info.enums.enm[i].iffeature_size; |
| 3064 | type->info.enums.enm[i].iffeature_size = 0; |
| 3065 | for (j = 0; j < iffeature_size; ++j) { |
| 3066 | if (yang_read_if_feature(module, &type->info.enums.enm[i], type->parent, |
| 3067 | (char *)type->info.enums.enm[i].iffeature[j].features, unres, ENUM_KEYWORD)) { |
| 3068 | goto error; |
| 3069 | } |
| 3070 | } |
| 3071 | } else { |
| 3072 | iffeature_size = type->info.bits.bit[i].iffeature_size; |
| 3073 | type->info.bits.bit[i].iffeature_size = 0; |
| 3074 | for (j = 0; j < iffeature_size; ++j) { |
| 3075 | if (yang_read_if_feature(module, &type->info.bits.bit[i], type->parent, |
| 3076 | (char *)type->info.bits.bit[i].iffeature[j].features, unres, BIT_KEYWORD)) { |
| 3077 | goto error; |
| 3078 | } |
| 3079 | } |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | return EXIT_SUCCESS; |
| 3084 | error: |
| 3085 | return EXIT_FAILURE; |
| 3086 | } |
| 3087 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3088 | int |
| 3089 | yang_check_typedef(struct lys_module *module, struct lys_node *parent, struct unres_schema *unres) |
| 3090 | { |
| 3091 | struct lys_tpdf *tpdf; |
| 3092 | uint8_t i, tpdf_size, *ptr_tpdf_size; |
| 3093 | int ret = EXIT_SUCCESS; |
| 3094 | |
| 3095 | if (!parent) { |
| 3096 | tpdf = module->tpdf; |
| 3097 | ptr_tpdf_size = &module->tpdf_size; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3098 | } else { |
| 3099 | switch (parent->nodetype) { |
| 3100 | case LYS_GROUPING: |
| 3101 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
| 3102 | ptr_tpdf_size = &((struct lys_node_grp *)parent)->tpdf_size; |
| 3103 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3104 | case LYS_CONTAINER: |
| 3105 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
| 3106 | ptr_tpdf_size = &((struct lys_node_container *)parent)->tpdf_size; |
| 3107 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3108 | case LYS_LIST: |
| 3109 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
| 3110 | ptr_tpdf_size = &((struct lys_node_list *)parent)->tpdf_size; |
| 3111 | break; |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3112 | case LYS_RPC: |
| 3113 | case LYS_ACTION: |
| 3114 | tpdf = ((struct lys_node_rpc_action *)parent)->tpdf; |
| 3115 | ptr_tpdf_size = &((struct lys_node_rpc_action *)parent)->tpdf_size; |
| 3116 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame^] | 3117 | case LYS_INPUT: |
| 3118 | case LYS_OUTPUT: |
| 3119 | tpdf = ((struct lys_node_inout *)parent)->tpdf; |
| 3120 | ptr_tpdf_size = &((struct lys_node_inout *)parent)->tpdf_size; |
| 3121 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3122 | default: |
| 3123 | LOGINT; |
| 3124 | return EXIT_FAILURE; |
| 3125 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | tpdf_size = *ptr_tpdf_size; |
| 3129 | *ptr_tpdf_size = 0; |
| 3130 | |
| 3131 | for (i = 0; i < tpdf_size; ++i) { |
| 3132 | tpdf[i].type.parent = &tpdf[i]; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3133 | if (yang_check_type_iffeature(module, unres, &tpdf[i].type)) { |
| 3134 | ret = EXIT_FAILURE; |
| 3135 | break; |
| 3136 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3137 | if (unres_schema_add_node(module, unres, &tpdf[i].type, UNRES_TYPE_DER_TPDF, parent) == -1) { |
| 3138 | ret = EXIT_FAILURE; |
| 3139 | break; |
| 3140 | } |
| 3141 | |
| 3142 | /* check default value*/ |
| 3143 | if (tpdf[i].dflt && unres_schema_add_str(module, unres, &tpdf[i].type, UNRES_TYPE_DFLT, tpdf[i].dflt) == -1) { |
| 3144 | ++i; |
| 3145 | ret = EXIT_FAILURE; |
| 3146 | break; |
| 3147 | } |
| 3148 | (*ptr_tpdf_size)++; |
| 3149 | } |
| 3150 | if (i < tpdf_size) { |
| 3151 | yang_tpdf_free(module->ctx, tpdf, i, tpdf_size); |
| 3152 | } |
| 3153 | |
| 3154 | return ret; |
| 3155 | } |
| 3156 | |
| 3157 | static int |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3158 | yang_check_identities(struct lys_module *module, struct unres_schema *unres) |
| 3159 | { |
| 3160 | uint32_t i, size, base_size; |
| 3161 | uint8_t j; |
| 3162 | |
| 3163 | size = module->ident_size; |
| 3164 | module->ident_size = 0; |
| 3165 | for (i = 0; i < size; ++i) { |
| 3166 | base_size = module->ident[i].base_size; |
| 3167 | module->ident[i].base_size = 0; |
| 3168 | for (j = 0; j < base_size; ++j) { |
| 3169 | if (yang_read_base(module, &module->ident[i], (char *)module->ident[i].base[j], unres)) { |
| 3170 | ++j; |
| 3171 | module->ident_size = size; |
| 3172 | goto error; |
| 3173 | } |
| 3174 | } |
| 3175 | module->ident_size++; |
| 3176 | } |
| 3177 | |
| 3178 | return EXIT_SUCCESS; |
| 3179 | |
| 3180 | error: |
| 3181 | for (; j< module->ident[i].base_size; ++j) { |
| 3182 | free(module->ident[i].base[j]); |
| 3183 | } |
| 3184 | yang_free_ident_base(module->ident, i + 1, size); |
| 3185 | return EXIT_FAILURE; |
| 3186 | } |
| 3187 | |
| 3188 | static int |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3189 | yang_check_grouping(struct lys_module *module, struct lys_node_grp *node, struct unres_schema *unres) |
| 3190 | { |
| 3191 | uint8_t i, size; |
| 3192 | |
| 3193 | if (yang_check_typedef(module, (struct lys_node *)node, unres)) { |
| 3194 | goto error; |
| 3195 | } |
| 3196 | |
| 3197 | size = node->iffeature_size; |
| 3198 | node->iffeature_size = 0; |
| 3199 | for (i = 0; i < size; ++i) { |
| 3200 | if (yang_read_if_feature(module, node, NULL, (char *)node->iffeature[i].features, unres, GROUPING_KEYWORD)) { |
| 3201 | node->iffeature_size = size; |
| 3202 | goto error; |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | return EXIT_SUCCESS; |
| 3207 | |
| 3208 | error: |
| 3209 | return EXIT_FAILURE; |
| 3210 | } |
| 3211 | |
| 3212 | static int |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3213 | yang_check_container(struct lys_module *module, struct lys_node_container *node, struct unres_schema *unres) |
| 3214 | { |
| 3215 | uint8_t i, size; |
| 3216 | |
| 3217 | if (yang_check_typedef(module, (struct lys_node *)node, unres)) { |
| 3218 | goto error; |
| 3219 | } |
| 3220 | |
| 3221 | size = node->iffeature_size; |
| 3222 | node->iffeature_size = 0; |
| 3223 | for (i = 0; i < size; ++i) { |
| 3224 | if (yang_read_if_feature(module, node, NULL, (char *)node->iffeature[i].features, unres, CONTAINER_KEYWORD)) { |
| 3225 | node->iffeature_size = size; |
| 3226 | goto error; |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | /* check XPath dependencies */ |
| 3231 | if ((node->when || node->must_size) && (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1)) { |
| 3232 | goto error; |
| 3233 | } |
| 3234 | |
| 3235 | return EXIT_SUCCESS; |
| 3236 | error: |
| 3237 | return EXIT_FAILURE; |
| 3238 | } |
| 3239 | |
| 3240 | static int |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3241 | yang_check_leaf(struct lys_module *module, struct lys_node_leaf *leaf, struct unres_schema *unres) |
| 3242 | { |
| 3243 | uint8_t i, size; |
| 3244 | |
| 3245 | if (yang_check_type_iffeature(module, unres, &leaf->type)) { |
| 3246 | yang_type_free(module->ctx, &leaf->type); |
| 3247 | goto error; |
| 3248 | } |
| 3249 | |
| 3250 | if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DER_TPDF, (struct lys_node *)leaf) == -1) { |
| 3251 | yang_type_free(module->ctx, &leaf->type); |
| 3252 | goto error; |
| 3253 | } |
| 3254 | |
| 3255 | if (leaf->dflt && unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)leaf->dflt) == -1) { |
| 3256 | goto error; |
| 3257 | } |
| 3258 | |
| 3259 | size = leaf->iffeature_size; |
| 3260 | leaf->iffeature_size = 0; |
| 3261 | for (i = 0; i < size; ++i) { |
| 3262 | if (yang_read_if_feature(module, leaf, NULL, (char *)leaf->iffeature[i].features, unres, LEAF_KEYWORD)) { |
| 3263 | leaf->iffeature_size = size; |
| 3264 | goto error; |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | /* check XPath dependencies */ |
| 3269 | if ((leaf->when || leaf->must_size) && (unres_schema_add_node(module, unres, leaf, UNRES_XPATH, NULL) == -1)) { |
| 3270 | goto error; |
| 3271 | } |
| 3272 | |
| 3273 | return EXIT_SUCCESS; |
| 3274 | error: |
| 3275 | return EXIT_FAILURE; |
| 3276 | } |
| 3277 | |
| 3278 | static int |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3279 | yang_check_leaflist(struct lys_module *module, struct lys_node_leaflist *leaflist, struct unres_schema *unres) |
| 3280 | { |
| 3281 | uint8_t i, size; |
| 3282 | |
| 3283 | if (yang_check_type_iffeature(module, unres, &leaflist->type)) { |
| 3284 | yang_type_free(module->ctx, &leaflist->type); |
| 3285 | goto error; |
| 3286 | } |
| 3287 | |
| 3288 | if (unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DER_TPDF, (struct lys_node *)leaflist) == -1) { |
| 3289 | yang_type_free(module->ctx, &leaflist->type); |
| 3290 | goto error; |
| 3291 | } |
| 3292 | |
| 3293 | /* check default value (if not defined, there still could be some restrictions |
| 3294 | * that need to be checked against a default value from a derived type) */ |
| 3295 | for (i = 0; i < leaflist->dflt_size; ++i) { |
| 3296 | if (unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaflist->dflt[i])) == -1) { |
| 3297 | goto error; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | size = leaflist->iffeature_size; |
| 3302 | leaflist->iffeature_size = 0; |
| 3303 | for (i = 0; i < size; ++i) { |
| 3304 | if (yang_read_if_feature(module, leaflist, NULL, (char *)leaflist->iffeature[i].features, unres, LEAF_LIST_KEYWORD)) { |
| 3305 | leaflist->iffeature_size = size; |
| 3306 | goto error; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | /* check XPath dependencies */ |
| 3311 | if ((leaflist->when || leaflist->must_size) && (unres_schema_add_node(module, unres, leaflist, UNRES_XPATH, NULL) == -1)) { |
| 3312 | goto error; |
| 3313 | } |
| 3314 | |
| 3315 | return EXIT_SUCCESS; |
| 3316 | error: |
| 3317 | return EXIT_FAILURE; |
| 3318 | } |
| 3319 | |
| 3320 | static int |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3321 | yang_check_list(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres) |
| 3322 | { |
| 3323 | uint8_t size, i; |
| 3324 | |
| 3325 | if (yang_check_typedef(module, (struct lys_node *)list, unres)) { |
| 3326 | goto error; |
| 3327 | } |
| 3328 | |
| 3329 | size = list->iffeature_size; |
| 3330 | list->iffeature_size = 0; |
| 3331 | for (i = 0; i < size; ++i) { |
| 3332 | if (yang_read_if_feature(module, list, NULL, (char *)list->iffeature[i].features, unres, LIST_KEYWORD)) { |
| 3333 | list->iffeature_size = size; |
| 3334 | goto error; |
| 3335 | } |
| 3336 | } |
| 3337 | |
| 3338 | if (list->keys && yang_read_key(module, list, unres)) { |
| 3339 | goto error; |
| 3340 | } |
| 3341 | |
| 3342 | if (yang_read_unique(module, list, unres)) { |
| 3343 | goto error; |
| 3344 | } |
| 3345 | |
| 3346 | /* check XPath dependencies */ |
| 3347 | if ((list->when || list->must_size) && (unres_schema_add_node(module, unres, list, UNRES_XPATH, NULL) == -1)) { |
| 3348 | goto error; |
| 3349 | } |
| 3350 | |
| 3351 | return EXIT_SUCCESS; |
| 3352 | error: |
| 3353 | return EXIT_FAILURE; |
| 3354 | } |
| 3355 | |
| 3356 | static int |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3357 | yang_check_choice(struct lys_module *module, struct lys_node_choice *choice, struct unres_schema *unres) |
| 3358 | { |
| 3359 | char *value; |
| 3360 | uint8_t size, i; |
| 3361 | |
| 3362 | if (choice->dflt) { |
| 3363 | value = (char *)choice->dflt; |
| 3364 | choice->dflt = NULL; |
| 3365 | if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, value) == -1) { |
| 3366 | free(value); |
| 3367 | goto error; |
| 3368 | } |
| 3369 | free(value); |
| 3370 | } |
| 3371 | |
| 3372 | size = choice->iffeature_size; |
| 3373 | choice->iffeature_size = 0; |
| 3374 | for (i = 0; i < size; ++i) { |
| 3375 | if (yang_read_if_feature(module, choice, NULL, (char *)choice->iffeature[i].features, unres, CHOICE_KEYWORD)) { |
| 3376 | choice->iffeature_size = size; |
| 3377 | goto error; |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | /* check XPath dependencies */ |
| 3382 | if ((choice->when) && (unres_schema_add_node(module, unres, choice, UNRES_XPATH, NULL) == -1)) { |
| 3383 | goto error; |
| 3384 | } |
| 3385 | |
| 3386 | return EXIT_SUCCESS; |
| 3387 | error: |
| 3388 | return EXIT_FAILURE; |
| 3389 | } |
| 3390 | |
| 3391 | static int |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 3392 | yang_check_case(struct lys_module *module, struct lys_node_case *cs, struct unres_schema *unres) |
| 3393 | { |
| 3394 | uint8_t size, i; |
| 3395 | |
| 3396 | size = cs->iffeature_size; |
| 3397 | cs->iffeature_size = 0; |
| 3398 | for (i = 0; i < size; ++i) { |
| 3399 | if (yang_read_if_feature(module, cs, NULL, (char *)cs->iffeature[i].features, unres, CASE_KEYWORD)) { |
| 3400 | cs->iffeature_size = size; |
| 3401 | return EXIT_FAILURE; |
| 3402 | } |
| 3403 | } |
| 3404 | return EXIT_SUCCESS; |
| 3405 | } |
| 3406 | |
| 3407 | static int |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 3408 | yang_check_anydata(struct lys_module *module, struct lys_node_anydata *anydata, struct unres_schema *unres) |
| 3409 | { |
| 3410 | uint8_t size, i; |
| 3411 | |
| 3412 | size = anydata->iffeature_size; |
| 3413 | anydata->iffeature_size = 0; |
| 3414 | for (i = 0; i < size; ++i) { |
| 3415 | if (yang_read_if_feature(module, anydata, NULL, (char *)anydata->iffeature[i].features, unres, ANYXML_KEYWORD)) { |
| 3416 | anydata->iffeature_size = size; |
| 3417 | goto error; |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | /* check XPath dependencies */ |
| 3422 | if ((anydata->when) && (unres_schema_add_node(module, unres, anydata, UNRES_XPATH, NULL) == -1)) { |
| 3423 | goto error; |
| 3424 | } |
| 3425 | |
| 3426 | return EXIT_SUCCESS; |
| 3427 | error: |
| 3428 | return EXIT_FAILURE; |
| 3429 | } |
| 3430 | |
| 3431 | static int |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3432 | yang_check_rpc_action(struct lys_module *module, struct lys_node_rpc_action *rpc, struct unres_schema *unres) |
| 3433 | { |
| 3434 | uint8_t size, i; |
| 3435 | struct lys_node *node; |
| 3436 | |
| 3437 | if (rpc->nodetype == LYS_ACTION) { |
| 3438 | for (node = rpc->parent; node; node = lys_parent(node)) { |
| 3439 | if ((node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
| 3440 | || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) { |
| 3441 | LOGVAL(LYE_INPAR, LY_VLOG_NONE, NULL, strnodetype(node->nodetype), "action"); |
| 3442 | goto error; |
| 3443 | } |
| 3444 | } |
| 3445 | } |
| 3446 | if (yang_check_typedef(module, (struct lys_node *)rpc, unres)) { |
| 3447 | goto error; |
| 3448 | } |
| 3449 | |
| 3450 | size = rpc->iffeature_size; |
| 3451 | rpc->iffeature_size = 0; |
| 3452 | for (i = 0; i < size; ++i) { |
| 3453 | if (yang_read_if_feature(module, rpc, NULL, (char *)rpc->iffeature[i].features, unres, RPC_KEYWORD)) { |
| 3454 | rpc->iffeature_size = size; |
| 3455 | goto error; |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | return EXIT_SUCCESS; |
| 3460 | error: |
| 3461 | return EXIT_FAILURE; |
| 3462 | } |
| 3463 | |
| 3464 | static int |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3465 | yang_check_nodes(struct lys_module *module, struct lys_node *nodes, struct unres_schema *unres) |
| 3466 | { |
Pavol Vican | 24ba7f6 | 2016-11-28 12:15:20 +0100 | [diff] [blame] | 3467 | struct lys_node *node = nodes, *sibling, *child, *parent; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3468 | |
| 3469 | while (node) { |
| 3470 | sibling = node->next; |
| 3471 | child = node->child; |
Pavol Vican | 24ba7f6 | 2016-11-28 12:15:20 +0100 | [diff] [blame] | 3472 | parent = node->parent; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3473 | node->next = NULL; |
| 3474 | node->child = NULL; |
Pavol Vican | 24ba7f6 | 2016-11-28 12:15:20 +0100 | [diff] [blame] | 3475 | node->parent = NULL; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3476 | node->prev = node; |
| 3477 | |
Pavol Vican | 24ba7f6 | 2016-11-28 12:15:20 +0100 | [diff] [blame] | 3478 | if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, node)) { |
| 3479 | lys_node_unlink(node); |
| 3480 | node->next = sibling; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3481 | sibling = node; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3482 | goto error; |
| 3483 | } |
| 3484 | switch (node->nodetype) { |
| 3485 | case LYS_GROUPING: |
| 3486 | if (yang_check_grouping(module, (struct lys_node_grp *)node, unres)) { |
| 3487 | goto error; |
| 3488 | } |
| 3489 | break; |
Pavol Vican | c54a8f7 | 2016-11-23 16:45:55 +0100 | [diff] [blame] | 3490 | case LYS_CONTAINER: |
| 3491 | if (yang_check_container(module, (struct lys_node_container *)node, unres)) { |
| 3492 | goto error; |
| 3493 | } |
| 3494 | break; |
Pavol Vican | a69aff2 | 2016-11-24 18:23:50 +0100 | [diff] [blame] | 3495 | case LYS_LEAF: |
| 3496 | if (yang_check_leaf(module, (struct lys_node_leaf *)node, unres)) { |
| 3497 | goto error; |
| 3498 | } |
| 3499 | break; |
Pavol Vican | 36aff86 | 2016-11-26 17:07:05 +0100 | [diff] [blame] | 3500 | case LYS_LEAFLIST: |
| 3501 | if (yang_check_leaflist(module, (struct lys_node_leaflist *)node, unres)) { |
| 3502 | goto error; |
| 3503 | } |
| 3504 | break; |
Pavol Vican | d8136a4 | 2016-11-27 13:28:04 +0100 | [diff] [blame] | 3505 | case LYS_LIST: |
| 3506 | if (yang_check_list(module, (struct lys_node_list *)node, unres)) { |
| 3507 | goto error; |
| 3508 | } |
| 3509 | break; |
Pavol Vican | 36ace10 | 2016-11-28 11:46:59 +0100 | [diff] [blame] | 3510 | case LYS_CHOICE: |
| 3511 | if (yang_check_choice(module, (struct lys_node_choice *)node, unres)) { |
| 3512 | goto error; |
| 3513 | } |
| 3514 | break; |
Pavol Vican | a420bac | 2016-11-28 14:51:54 +0100 | [diff] [blame] | 3515 | case LYS_CASE: |
| 3516 | if (yang_check_case(module, (struct lys_node_case *)node, unres)) { |
| 3517 | goto error; |
| 3518 | } |
| 3519 | break; |
Pavol Vican | bfa1a58 | 2016-11-28 15:35:59 +0100 | [diff] [blame] | 3520 | case LYS_ANYDATA: |
| 3521 | case LYS_ANYXML: |
| 3522 | if (yang_check_anydata(module, (struct lys_node_anydata *)node, unres)) { |
| 3523 | goto error; |
| 3524 | } |
| 3525 | break; |
Pavol Vican | ebc9ef8 | 2016-11-28 16:46:49 +0100 | [diff] [blame] | 3526 | case LYS_RPC: |
| 3527 | case LYS_ACTION: |
| 3528 | if (yang_check_rpc_action(module, (struct lys_node_rpc_action *)node, unres)){ |
| 3529 | goto error; |
| 3530 | } |
| 3531 | break; |
Pavol Vican | 7872939 | 2016-11-28 17:18:22 +0100 | [diff] [blame^] | 3532 | case LYS_INPUT: |
| 3533 | case LYS_OUTPUT: |
| 3534 | /* check XPath dependencies */ |
| 3535 | if (((struct lys_node_inout *)node)->must_size && |
| 3536 | (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1)) { |
| 3537 | goto error; |
| 3538 | } |
| 3539 | if (yang_check_typedef(module, node, unres)) { |
| 3540 | goto error; |
| 3541 | } |
| 3542 | break; |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3543 | default: |
| 3544 | LOGINT; |
| 3545 | sibling = node; |
| 3546 | child = NULL; |
| 3547 | goto error; |
| 3548 | } |
| 3549 | if (yang_check_nodes(module, child, unres)) { |
| 3550 | child = NULL; |
| 3551 | goto error; |
| 3552 | } |
| 3553 | node = sibling; |
| 3554 | } |
| 3555 | |
| 3556 | return EXIT_SUCCESS; |
| 3557 | error: |
| 3558 | yang_free_nodes(module->ctx, sibling); |
| 3559 | yang_free_nodes(module->ctx, child); |
| 3560 | return EXIT_FAILURE; |
| 3561 | } |
| 3562 | |
| 3563 | static int |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3564 | yang_check_sub_module(struct lys_module *module, struct unres_schema *unres, struct lys_node *node) |
| 3565 | { |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3566 | uint8_t i, j, size, erase_identities = 1, erase_nodes = 1; |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 3567 | struct lys_feature *feature; /* shortcut */ |
| 3568 | char *s; |
| 3569 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3570 | if (yang_check_typedef(module, NULL, unres)) { |
| 3571 | goto error; |
| 3572 | } |
| 3573 | |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 3574 | /* check features */ |
| 3575 | for (i = 0; i < module->features_size; ++i) { |
| 3576 | feature = &module->features[i]; |
| 3577 | size = feature->iffeature_size; |
| 3578 | feature->iffeature_size = 0; |
| 3579 | for (j = 0; j < size; ++j) { |
| 3580 | s = (char *)feature->iffeature[j].features; |
Pavol Vican | 5c8a9ad | 2016-11-22 11:50:54 +0100 | [diff] [blame] | 3581 | if (yang_read_if_feature(module, feature, NULL, s, unres, FEATURE_KEYWORD)) { |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 3582 | goto error; |
| 3583 | } |
| 3584 | if (unres_schema_add_node(module, unres, feature, UNRES_FEATURE, NULL) == -1) { |
| 3585 | goto error; |
| 3586 | } |
| 3587 | } |
| 3588 | } |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3589 | erase_identities = 0; |
| 3590 | if (yang_check_identities(module, unres)) { |
| 3591 | goto error; |
| 3592 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3593 | erase_nodes = 0; |
| 3594 | if (yang_check_nodes(module, node, unres)) { |
| 3595 | goto error; |
| 3596 | } |
Pavol Vican | 7a7916f | 2016-11-21 23:38:30 +0100 | [diff] [blame] | 3597 | |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3598 | return EXIT_SUCCESS; |
| 3599 | error: |
Pavol Vican | 36e2727 | 2016-11-22 15:47:28 +0100 | [diff] [blame] | 3600 | if (erase_identities) { |
| 3601 | yang_free_ident_base(module->ident, 0, module->ident_size); |
| 3602 | } |
Pavol Vican | 05810b6 | 2016-11-23 14:07:22 +0100 | [diff] [blame] | 3603 | if (erase_nodes) { |
| 3604 | yang_free_nodes(module->ctx, node); |
| 3605 | } |
Pavol Vican | 7313fc0 | 2016-11-14 01:10:31 +0100 | [diff] [blame] | 3606 | return EXIT_FAILURE; |
| 3607 | } |