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