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