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