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 | |
| 1494 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1495 | yang_read_enum(struct lys_module *module, struct yang_type *typ, char *value) |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1496 | { |
| 1497 | struct lys_type_enum *enm; |
| 1498 | int i; |
| 1499 | |
Pavol Vican | c666241 | 2016-08-30 08:06:28 +0200 | [diff] [blame] | 1500 | if (!value[0]) { |
| 1501 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name"); |
| 1502 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty."); |
| 1503 | free(value); |
| 1504 | goto error; |
| 1505 | } |
| 1506 | |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1507 | enm = &typ->type->info.enums.enm[typ->type->info.enums.count]; |
| 1508 | enm->name = lydict_insert_zc(module->ctx, value); |
| 1509 | |
| 1510 | /* the assigned name MUST NOT have any leading or trailing whitespace characters */ |
| 1511 | if (isspace(enm->name[0]) || isspace(enm->name[strlen(enm->name) - 1])) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1512 | LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1513 | goto error; |
| 1514 | } |
| 1515 | |
| 1516 | /* check the name uniqueness */ |
| 1517 | for (i = 0; i < typ->type->info.enums.count; i++) { |
| 1518 | if (!strcmp(typ->type->info.enums.enm[i].name, typ->type->info.enums.enm[typ->type->info.enums.count].name)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1519 | 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] | 1520 | goto error; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | typ->type->info.enums.count++; |
| 1525 | return enm; |
| 1526 | |
| 1527 | error: |
| 1528 | typ->type->info.enums.count++; |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | |
| 1532 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1533 | 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] | 1534 | { |
| 1535 | int i, j; |
| 1536 | |
| 1537 | if (!assign) { |
| 1538 | /* assign value automatically */ |
| 1539 | if (*value > INT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1540 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value"); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1541 | goto error; |
| 1542 | } |
| 1543 | enm->value = *value; |
| 1544 | enm->flags |= LYS_AUTOASSIGNED; |
| 1545 | (*value)++; |
Pavol Vican | 5de389c | 2016-08-30 08:55:15 +0200 | [diff] [blame] | 1546 | } else if (typ->type->info.enums.enm == enm) { |
| 1547 | /* change value, which is assigned automatically, if first enum has value. */ |
| 1548 | *value = typ->type->info.enums.enm[0].value; |
| 1549 | (*value)++; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | /* check that the value is unique */ |
| 1553 | j = typ->type->info.enums.count-1; |
| 1554 | for (i = 0; i < j; i++) { |
| 1555 | 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] | 1556 | LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL, |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1557 | typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name, |
| 1558 | typ->type->info.enums.enm[i].name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1559 | goto error; |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | return EXIT_SUCCESS; |
| 1564 | |
| 1565 | error: |
| 1566 | return EXIT_FAILURE; |
| 1567 | } |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1568 | |
| 1569 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1570 | 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] | 1571 | { |
| 1572 | int i; |
| 1573 | struct lys_type_bit *bit; |
| 1574 | |
| 1575 | bit = &typ->type->info.bits.bit[typ->type->info.bits.count]; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1576 | if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) { |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1577 | free(value); |
| 1578 | goto error; |
| 1579 | } |
| 1580 | bit->name = lydict_insert_zc(module->ctx, value); |
| 1581 | |
| 1582 | /* check the name uniqueness */ |
| 1583 | for (i = 0; i < typ->type->info.bits.count; i++) { |
| 1584 | if (!strcmp(typ->type->info.bits.bit[i].name, bit->name)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1585 | LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1586 | typ->type->info.bits.count++; |
| 1587 | goto error; |
| 1588 | } |
| 1589 | } |
| 1590 | typ->type->info.bits.count++; |
| 1591 | return bit; |
| 1592 | |
| 1593 | error: |
| 1594 | return NULL; |
| 1595 | } |
| 1596 | |
| 1597 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1598 | 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] | 1599 | { |
| 1600 | int i,j; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1601 | |
| 1602 | if (!assign) { |
| 1603 | /* assign value automatically */ |
| 1604 | if (*value > UINT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1605 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position"); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1606 | goto error; |
| 1607 | } |
| 1608 | bit->pos = (uint32_t)*value; |
| 1609 | bit->flags |= LYS_AUTOASSIGNED; |
| 1610 | (*value)++; |
| 1611 | } |
| 1612 | |
| 1613 | j = typ->type->info.bits.count - 1; |
| 1614 | /* check that the value is unique */ |
| 1615 | for (i = 0; i < j; i++) { |
| 1616 | if (typ->type->info.bits.bit[i].pos == bit->pos) { |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1617 | 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] | 1618 | goto error; |
| 1619 | } |
| 1620 | } |
| 1621 | |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1622 | return EXIT_SUCCESS; |
| 1623 | |
| 1624 | error: |
| 1625 | return EXIT_FAILURE; |
| 1626 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1627 | |
| 1628 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1629 | 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] | 1630 | { |
| 1631 | struct lys_tpdf *ret; |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1632 | struct lys_node *root; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1633 | |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1634 | root = (parent) ? NULL : lys_main_module(module)->data; |
| 1635 | 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] | 1636 | free(value); |
| 1637 | return NULL; |
| 1638 | } |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1639 | |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1640 | if (!parent) { |
| 1641 | ret = &module->tpdf[module->tpdf_size]; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1642 | module->tpdf_size++; |
Pavol Vican | 21238f5 | 2016-03-01 12:39:52 +0100 | [diff] [blame] | 1643 | } else { |
| 1644 | switch (parent->nodetype) { |
| 1645 | case LYS_GROUPING: |
| 1646 | 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] | 1647 | ((struct lys_node_grp *)parent)->tpdf_size++; |
| 1648 | break; |
Pavol Vican | 535d50e | 2016-03-01 13:05:33 +0100 | [diff] [blame] | 1649 | case LYS_CONTAINER: |
| 1650 | ret = &((struct lys_node_container *)parent)->tpdf[((struct lys_node_container *)parent)->tpdf_size]; |
| 1651 | ((struct lys_node_container *)parent)->tpdf_size++; |
| 1652 | break; |
Pavol Vican | 09f04b8 | 2016-03-01 14:02:28 +0100 | [diff] [blame] | 1653 | case LYS_LIST: |
| 1654 | ret = &((struct lys_node_list *)parent)->tpdf[((struct lys_node_list *)parent)->tpdf_size]; |
| 1655 | ((struct lys_node_list *)parent)->tpdf_size++; |
| 1656 | break; |
Pavol Vican | 52ed67d | 2016-03-02 17:46:15 +0100 | [diff] [blame] | 1657 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1658 | case LYS_ACTION: |
| 1659 | ret = &((struct lys_node_rpc_action *)parent)->tpdf[((struct lys_node_rpc_action *)parent)->tpdf_size]; |
| 1660 | ((struct lys_node_rpc_action *)parent)->tpdf_size++; |
Pavol Vican | 52ed67d | 2016-03-02 17:46:15 +0100 | [diff] [blame] | 1661 | break; |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 1662 | case LYS_INPUT: |
| 1663 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1664 | ret = &((struct lys_node_inout *)parent)->tpdf[((struct lys_node_inout *)parent)->tpdf_size]; |
| 1665 | ((struct lys_node_inout *)parent)->tpdf_size++; |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 1666 | break; |
Pavol Vican | 41267fd | 2016-03-03 10:47:42 +0100 | [diff] [blame] | 1667 | case LYS_NOTIF: |
| 1668 | ret = &((struct lys_node_notif *)parent)->tpdf[((struct lys_node_notif *)parent)->tpdf_size]; |
| 1669 | ((struct lys_node_notif *)parent)->tpdf_size++; |
| 1670 | break; |
Pavol Vican | d1dbfda | 2016-03-21 10:03:58 +0100 | [diff] [blame] | 1671 | default: |
| 1672 | /* another type of nodetype is error*/ |
| 1673 | LOGINT; |
| 1674 | free(value); |
| 1675 | return NULL; |
Pavol Vican | 21238f5 | 2016-03-01 12:39:52 +0100 | [diff] [blame] | 1676 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1677 | } |
| 1678 | |
Pavol Vican | 7943647 | 2016-04-04 11:22:02 +0200 | [diff] [blame] | 1679 | ret->type.parent = ret; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1680 | ret->name = lydict_insert_zc(module->ctx, value); |
| 1681 | ret->module = module; |
| 1682 | return ret; |
| 1683 | } |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1684 | |
| 1685 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1686 | 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] | 1687 | { |
| 1688 | struct lys_refine *rfn; |
| 1689 | |
| 1690 | rfn = &uses->refine[uses->refine_size]; |
| 1691 | uses->refine_size++; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1692 | rfn->target_name = transform_schema2json(module, value); |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1693 | free(value); |
| 1694 | if (!rfn->target_name) { |
| 1695 | return NULL; |
| 1696 | } |
| 1697 | return rfn; |
| 1698 | } |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1699 | |
| 1700 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1701 | 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] | 1702 | { |
| 1703 | struct lys_node_augment *aug; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1704 | |
| 1705 | if (parent) { |
| 1706 | aug = &((struct lys_node_uses *)parent)->augment[((struct lys_node_uses *)parent)->augment_size]; |
| 1707 | } else { |
| 1708 | aug = &module->augment[module->augment_size]; |
| 1709 | } |
| 1710 | aug->nodetype = LYS_AUGMENT; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1711 | aug->target_name = transform_schema2json(module, value); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1712 | free(value); |
| 1713 | if (!aug->target_name) { |
| 1714 | return NULL; |
| 1715 | } |
| 1716 | aug->parent = parent; |
| 1717 | aug->module = module; |
| 1718 | if (parent) { |
| 1719 | ((struct lys_node_uses *)parent)->augment_size++; |
| 1720 | } else { |
| 1721 | module->augment_size++; |
| 1722 | } |
| 1723 | return aug; |
| 1724 | } |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1725 | |
| 1726 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1727 | yang_read_deviation(struct lys_module *module, char *value) |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1728 | { |
| 1729 | struct lys_node *dev_target = NULL; |
| 1730 | struct lys_deviation *dev; |
| 1731 | struct type_deviation *deviation = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1732 | int rc; |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1733 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1734 | dev = &module->deviation[module->deviation_size]; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1735 | dev->target_name = transform_schema2json(module, value); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1736 | free(value); |
| 1737 | if (!dev->target_name) { |
| 1738 | goto error; |
| 1739 | } |
| 1740 | |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1741 | deviation = calloc(1, sizeof *deviation); |
| 1742 | if (!deviation) { |
| 1743 | LOGMEM; |
| 1744 | goto error; |
| 1745 | } |
| 1746 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1747 | /* resolve target node */ |
| 1748 | rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&dev_target); |
| 1749 | if (rc || !dev_target) { |
Michal Vasko | 75c8daf | 2016-05-19 10:56:39 +0200 | [diff] [blame] | 1750 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1751 | goto error; |
| 1752 | } |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1753 | if (dev_target->module == lys_main_module(module)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1754 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
| 1755 | 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] | 1756 | goto error; |
| 1757 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1758 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1759 | /*save pointer to the deviation and deviated target*/ |
| 1760 | deviation->deviation = dev; |
| 1761 | deviation->target = dev_target; |
| 1762 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1763 | deviation->dflt_check = ly_set_new(); |
| 1764 | if (!deviation->dflt_check) { |
| 1765 | LOGMEM; |
| 1766 | goto error; |
| 1767 | } |
| 1768 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1769 | return deviation; |
| 1770 | |
| 1771 | error: |
| 1772 | free(deviation); |
| 1773 | lydict_remove(module->ctx, dev->target_name); |
| 1774 | return NULL; |
| 1775 | } |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1776 | |
| 1777 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1778 | yang_read_deviate_unsupported(struct type_deviation *dev) |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1779 | { |
| 1780 | int i; |
| 1781 | |
| 1782 | if (dev->deviation->deviate_size) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1783 | 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] | 1784 | return EXIT_FAILURE; |
| 1785 | } |
| 1786 | dev->deviation->deviate[dev->deviation->deviate_size].mod = LY_DEVIATE_NO; |
| 1787 | |
| 1788 | /* you cannot remove a key leaf */ |
| 1789 | if ((dev->target->nodetype == LYS_LEAF) && dev->target->parent && (dev->target->parent->nodetype == LYS_LIST)) { |
| 1790 | for (i = 0; i < ((struct lys_node_list *)dev->target->parent)->keys_size; ++i) { |
| 1791 | 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] | 1792 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation"); |
| 1793 | 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] | 1794 | return EXIT_FAILURE; |
| 1795 | } |
| 1796 | } |
| 1797 | } |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 1798 | |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1799 | /* unlink and store the original node */ |
Michal Vasko | d921d68 | 2016-05-19 10:56:51 +0200 | [diff] [blame] | 1800 | lys_node_unlink(dev->target); |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1801 | dev->deviation->orig_node = dev->target; |
| 1802 | |
| 1803 | dev->deviation->deviate_size = 1; |
| 1804 | return EXIT_SUCCESS; |
| 1805 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1806 | |
| 1807 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1808 | yang_read_deviate(struct type_deviation *dev, LYS_DEVIATE_TYPE mod) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1809 | { |
| 1810 | struct unres_schema tmp_unres; |
| 1811 | |
| 1812 | dev->deviation->deviate[dev->deviation->deviate_size].mod = mod; |
| 1813 | dev->deviate = &dev->deviation->deviate[dev->deviation->deviate_size]; |
| 1814 | dev->deviation->deviate_size++; |
Pavol Vican | 3388446 | 2016-09-27 21:04:26 +0200 | [diff] [blame] | 1815 | dev->trg_must_size = NULL; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1816 | if (dev->deviation->deviate[0].mod == LY_DEVIATE_NO) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1817 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported"); |
| 1818 | 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] | 1819 | return EXIT_FAILURE; |
| 1820 | } |
| 1821 | |
| 1822 | /* store a shallow copy of the original node */ |
| 1823 | if (!dev->deviation->orig_node) { |
| 1824 | memset(&tmp_unres, 0, sizeof tmp_unres); |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 1825 | 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] | 1826 | /* just to be safe */ |
| 1827 | if (tmp_unres.count) { |
| 1828 | LOGINT; |
| 1829 | return EXIT_FAILURE; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | return EXIT_SUCCESS; |
| 1834 | } |
| 1835 | |
| 1836 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1837 | 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] | 1838 | { |
| 1839 | const char **stritem; |
| 1840 | |
| 1841 | if (dev->deviate->units) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1842 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "units", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1843 | free(value); |
| 1844 | goto error; |
| 1845 | } |
| 1846 | |
| 1847 | /* check target node type */ |
| 1848 | if (dev->target->nodetype == LYS_LEAFLIST) { |
| 1849 | stritem = &((struct lys_node_leaflist *)dev->target)->units; |
| 1850 | } else if (dev->target->nodetype == LYS_LEAF) { |
| 1851 | stritem = &((struct lys_node_leaf *)dev->target)->units; |
| 1852 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1853 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1854 | 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] | 1855 | free(value); |
| 1856 | goto error; |
| 1857 | } |
| 1858 | |
| 1859 | dev->deviate->units = lydict_insert_zc(ctx, value); |
| 1860 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1861 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1862 | /* check values */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 1863 | if (!ly_strequal(*stritem, dev->deviate->units, 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1864 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->units, "units"); |
| 1865 | 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] | 1866 | goto error; |
| 1867 | } |
| 1868 | /* remove current units value of the target */ |
| 1869 | lydict_remove(ctx, *stritem); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1870 | } else { |
| 1871 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1872 | /* check that there is no current value */ |
| 1873 | if (*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1874 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1875 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1876 | goto error; |
| 1877 | } |
| 1878 | } else { /* replace */ |
| 1879 | if (!*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1880 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1881 | 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] | 1882 | goto error; |
| 1883 | } |
| 1884 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1885 | /* remove current units value of the target ... */ |
| 1886 | lydict_remove(ctx, *stritem); |
| 1887 | |
| 1888 | /* ... and replace it with the value specified in deviation */ |
| 1889 | *stritem = lydict_insert(ctx, dev->deviate->units, 0); |
| 1890 | } |
| 1891 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1892 | return EXIT_SUCCESS; |
| 1893 | |
| 1894 | error: |
| 1895 | return EXIT_FAILURE; |
| 1896 | } |
| 1897 | |
| 1898 | int |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1899 | yang_read_deviate_must(struct type_deviation *dev, uint8_t c_must) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1900 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1901 | /* check target node type */ |
| 1902 | switch (dev->target->nodetype) { |
| 1903 | case LYS_LEAF: |
| 1904 | dev->trg_must = &((struct lys_node_leaf *)dev->target)->must; |
| 1905 | dev->trg_must_size = &((struct lys_node_leaf *)dev->target)->must_size; |
| 1906 | break; |
| 1907 | case LYS_CONTAINER: |
| 1908 | dev->trg_must = &((struct lys_node_container *)dev->target)->must; |
| 1909 | dev->trg_must_size = &((struct lys_node_container *)dev->target)->must_size; |
| 1910 | break; |
| 1911 | case LYS_LEAFLIST: |
| 1912 | dev->trg_must = &((struct lys_node_leaflist *)dev->target)->must; |
| 1913 | dev->trg_must_size = &((struct lys_node_leaflist *)dev->target)->must_size; |
| 1914 | break; |
| 1915 | case LYS_LIST: |
| 1916 | dev->trg_must = &((struct lys_node_list *)dev->target)->must; |
| 1917 | dev->trg_must_size = &((struct lys_node_list *)dev->target)->must_size; |
| 1918 | break; |
| 1919 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1920 | case LYS_ANYDATA: |
| 1921 | dev->trg_must = &((struct lys_node_anydata *)dev->target)->must; |
| 1922 | dev->trg_must_size = &((struct lys_node_anydata *)dev->target)->must_size; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1923 | break; |
| 1924 | default: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1925 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must"); |
| 1926 | 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] | 1927 | goto error; |
| 1928 | } |
| 1929 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 1930 | /* flag will be checked again, clear it for now */ |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 1931 | dev->target->flags &= ~LYS_VALID_DEP; |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 1932 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1933 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1934 | /* reallocate the must array of the target */ |
| 1935 | dev->deviate->must = ly_realloc(*dev->trg_must, (c_must + *dev->trg_must_size) * sizeof *dev->deviate->must); |
| 1936 | if (!dev->deviate->must) { |
| 1937 | LOGMEM; |
| 1938 | goto error; |
| 1939 | } |
| 1940 | *dev->trg_must = dev->deviate->must; |
| 1941 | dev->deviate->must = &((*dev->trg_must)[*dev->trg_must_size]); |
| 1942 | dev->deviate->must_size = c_must; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1943 | } else { |
| 1944 | /* LY_DEVIATE_DEL */ |
| 1945 | dev->deviate->must = calloc(c_must, sizeof *dev->deviate->must); |
| 1946 | if (!dev->deviate->must) { |
| 1947 | LOGMEM; |
| 1948 | goto error; |
| 1949 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | return EXIT_SUCCESS; |
| 1953 | |
| 1954 | error: |
| 1955 | return EXIT_FAILURE; |
| 1956 | } |
| 1957 | |
| 1958 | int |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1959 | yang_read_deviate_unique(struct type_deviation *dev, uint8_t c_uniq) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1960 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1961 | struct lys_node_list *list; |
| 1962 | |
| 1963 | /* check target node type */ |
| 1964 | if (dev->target->nodetype != LYS_LIST) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1965 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique"); |
| 1966 | 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] | 1967 | goto error; |
| 1968 | } |
| 1969 | |
| 1970 | list = (struct lys_node_list *)dev->target; |
| 1971 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1972 | /* reallocate the unique array of the target */ |
| 1973 | dev->deviate->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *dev->deviate->unique); |
| 1974 | if (!dev->deviate->unique) { |
| 1975 | LOGMEM; |
| 1976 | goto error; |
| 1977 | } |
| 1978 | list->unique = dev->deviate->unique; |
| 1979 | dev->deviate->unique = &list->unique[list->unique_size]; |
| 1980 | dev->deviate->unique_size = c_uniq; |
| 1981 | memset(dev->deviate->unique, 0, c_uniq * sizeof *dev->deviate->unique); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1982 | } else { |
| 1983 | /* LY_DEVIATE_DEL */ |
| 1984 | dev->deviate->unique = calloc(c_uniq, sizeof *dev->deviate->unique); |
| 1985 | if (!dev->deviate->unique) { |
| 1986 | LOGMEM; |
| 1987 | goto error; |
| 1988 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | return EXIT_SUCCESS; |
| 1992 | |
| 1993 | error: |
| 1994 | return EXIT_FAILURE; |
| 1995 | } |
| 1996 | |
| 1997 | int |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1998 | 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] | 1999 | { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2000 | int i; |
| 2001 | struct lys_node_leaflist *llist; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2002 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2003 | /* check target node type */ |
| 2004 | if (module->version < 2 && dev->target->nodetype == LYS_LEAFLIST) { |
| 2005 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 2006 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property."); |
| 2007 | goto error; |
| 2008 | } else if (c_dflt > 1 && dev->target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */ |
| 2009 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 2010 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties."); |
| 2011 | goto error; |
| 2012 | } else if (!(dev->target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 2013 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 2014 | 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] | 2015 | goto error; |
| 2016 | } |
| 2017 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2018 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 2019 | /* check that there is no current value */ |
| 2020 | if ((dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) || |
| 2021 | (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt)) { |
| 2022 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 2023 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2024 | goto error; |
| 2025 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2026 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2027 | /* check collision with mandatory/min-elements */ |
| 2028 | if ((dev->target->flags & LYS_MAND_TRUE) || |
| 2029 | (dev->target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev->target)->min)) { |
| 2030 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "deviation"); |
| 2031 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 2032 | "Adding the \"default\" statement is forbidden on %s statement.", |
| 2033 | (dev->target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\""); |
| 2034 | goto error; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2035 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2036 | } else if (dev->deviate->mod == LY_DEVIATE_RPL) { |
| 2037 | /* check that there was a value before */ |
| 2038 | if (((dev->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((struct lys_node_leaf *)dev->target)->dflt) || |
| 2039 | (dev->target->nodetype == LYS_CHOICE && !((struct lys_node_choice *)dev->target)->dflt)) { |
| 2040 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 2041 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
| 2042 | goto error; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2043 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | if (dev->target->nodetype == LYS_LEAFLIST) { |
| 2047 | /* reallocate default list in the target */ |
| 2048 | llist = (struct lys_node_leaflist *)dev->target; |
| 2049 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 2050 | /* reallocate (enlarge) the unique array of the target */ |
| 2051 | llist->dflt = ly_realloc(llist->dflt, (c_dflt + llist->dflt_size) * sizeof *dev->deviate->dflt); |
| 2052 | } else if (dev->deviate->mod == LY_DEVIATE_RPL) { |
| 2053 | /* reallocate (replace) the unique array of the target */ |
| 2054 | for (i = 0; i < llist->dflt_size; i++) { |
| 2055 | lydict_remove(llist->module->ctx, llist->dflt[i]); |
| 2056 | } |
| 2057 | llist->dflt = ly_realloc(llist->dflt, c_dflt * sizeof *dev->deviate->dflt); |
| 2058 | llist->dflt_size = 0; |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | dev->deviate->dflt = calloc(c_dflt, sizeof *dev->deviate->dflt); |
| 2063 | if (!dev->deviate->dflt) { |
| 2064 | LOGMEM; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2065 | goto error; |
| 2066 | } |
| 2067 | |
| 2068 | return EXIT_SUCCESS; |
| 2069 | |
| 2070 | error: |
| 2071 | return EXIT_FAILURE; |
| 2072 | } |
| 2073 | |
| 2074 | int |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2075 | yang_fill_deviate_default(struct ly_ctx *ctx, struct type_deviation *dev, char *exp) |
| 2076 | { |
| 2077 | struct lys_node *node; |
| 2078 | struct lys_node_choice *choice; |
| 2079 | struct lys_node_leaf *leaf; |
| 2080 | struct lys_node_leaflist *llist; |
| 2081 | int rc, i; |
| 2082 | unsigned int u; |
| 2083 | const char *value; |
| 2084 | |
| 2085 | value = lydict_insert_zc(ctx, exp); |
| 2086 | u = strlen(value); |
| 2087 | dev->deviate->dflt[dev->deviate->dflt_size++] = value; |
| 2088 | |
| 2089 | if (dev->target->nodetype == LYS_CHOICE) { |
| 2090 | choice = (struct lys_node_choice *)dev->target; |
| 2091 | rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node); |
| 2092 | if (rc || !node) { |
| 2093 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 2094 | goto error; |
| 2095 | } |
| 2096 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 2097 | if (!choice->dflt || (choice->dflt != node)) { |
| 2098 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 2099 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
| 2100 | goto error; |
| 2101 | } |
| 2102 | } else { /* add or replace */ |
| 2103 | choice->dflt = node; |
| 2104 | if (!choice->dflt) { |
| 2105 | /* default branch not found */ |
| 2106 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 2107 | goto error; |
| 2108 | } |
| 2109 | } |
| 2110 | } else if (dev->target->nodetype == LYS_LEAF) { |
| 2111 | leaf = (struct lys_node_leaf *)dev->target; |
| 2112 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 2113 | if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) { |
| 2114 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 2115 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
| 2116 | goto error; |
| 2117 | } |
| 2118 | /* remove value */ |
| 2119 | lydict_remove(ctx, leaf->dflt); |
| 2120 | leaf->dflt = NULL; |
| 2121 | } else { /* add (already checked) and replace */ |
| 2122 | /* remove value */ |
| 2123 | lydict_remove(ctx, leaf->dflt); |
| 2124 | |
| 2125 | /* set new value */ |
| 2126 | leaf->dflt = lydict_insert(ctx, value, u); |
| 2127 | |
| 2128 | /* remember to check it later (it may not fit now, but the type can be deviated too) */ |
| 2129 | ly_set_add(dev->dflt_check, dev->target, 0); |
| 2130 | } |
| 2131 | } else { /* LYS_LEAFLIST */ |
| 2132 | llist = (struct lys_node_leaflist *)dev->target; |
| 2133 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 2134 | /* find and remove the value in target list */ |
| 2135 | for (i = 0; i < llist->dflt_size; i++) { |
| 2136 | if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) { |
| 2137 | /* match, remove the value */ |
| 2138 | lydict_remove(llist->module->ctx, llist->dflt[i]); |
| 2139 | llist->dflt[i] = NULL; |
| 2140 | break; |
| 2141 | } |
| 2142 | } |
| 2143 | if (i == llist->dflt_size) { |
| 2144 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 2145 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node."); |
| 2146 | goto error; |
| 2147 | } |
| 2148 | } else { |
| 2149 | /* add or replace, anyway we place items into the deviate's list |
| 2150 | which propagates to the target */ |
| 2151 | /* we just want to check that the value isn't already in the list */ |
| 2152 | for (i = 0; i < llist->dflt_size; i++) { |
| 2153 | if (ly_strequal(llist->dflt[i], value, 1)) { |
| 2154 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 2155 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value); |
| 2156 | goto error; |
| 2157 | } |
| 2158 | } |
| 2159 | /* store it in target node */ |
| 2160 | llist->dflt[llist->dflt_size++] = lydict_insert(ctx, value, u); |
| 2161 | |
| 2162 | /* remember to check it later (it may not fit now, but the type can be deviated too) */ |
| 2163 | ly_set_add(dev->dflt_check, dev->target, 0); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | return EXIT_SUCCESS; |
| 2168 | error: |
| 2169 | return EXIT_FAILURE; |
| 2170 | } |
| 2171 | |
| 2172 | |
| 2173 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2174 | yang_read_deviate_config(struct type_deviation *dev, uint8_t value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2175 | { |
| 2176 | if (dev->deviate->flags & LYS_CONFIG_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2177 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "config", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2178 | goto error; |
| 2179 | } |
| 2180 | |
| 2181 | /* for we deviate from RFC 6020 and allow config property even it is/is not |
| 2182 | * specified in the target explicitly since config property inherits. So we expect |
| 2183 | * that config is specified in every node. But for delete, we check that the value |
| 2184 | * is the same as here in deviation |
| 2185 | */ |
| 2186 | dev->deviate->flags |= value; |
| 2187 | |
| 2188 | /* add and replace are the same in this case */ |
| 2189 | /* remove current config value of the target ... */ |
| 2190 | dev->target->flags &= ~LYS_CONFIG_MASK; |
| 2191 | |
| 2192 | /* ... and replace it with the value specified in deviation */ |
| 2193 | dev->target->flags |= dev->deviate->flags & LYS_CONFIG_MASK; |
| 2194 | |
| 2195 | return EXIT_SUCCESS; |
| 2196 | |
| 2197 | error: |
| 2198 | return EXIT_FAILURE; |
| 2199 | } |
| 2200 | |
| 2201 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2202 | yang_read_deviate_mandatory(struct type_deviation *dev, uint8_t value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2203 | { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 2204 | struct lys_node *parent; |
| 2205 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2206 | if (dev->deviate->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2207 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "mandatory", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2208 | goto error; |
| 2209 | } |
| 2210 | |
| 2211 | /* check target node type */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2212 | if (!(dev->target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2213 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2214 | 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] | 2215 | goto error; |
| 2216 | } |
| 2217 | |
| 2218 | dev->deviate->flags |= value; |
| 2219 | |
| 2220 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 2221 | /* check that there is no current value */ |
| 2222 | if (dev->target->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2223 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2224 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2225 | goto error; |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 2226 | } else { |
| 2227 | if (dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) { |
| 2228 | /* RFC 6020, 7.6.4 - default statement must not with mandatory true */ |
| 2229 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf"); |
| 2230 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 2231 | goto error; |
| 2232 | } else if (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt) { |
| 2233 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice"); |
| 2234 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 2235 | goto error; |
| 2236 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2237 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2238 | } else { /* replace */ |
| 2239 | if (!(dev->target->flags & LYS_MAND_MASK)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2240 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2241 | 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] | 2242 | goto error; |
| 2243 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2244 | } |
| 2245 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2246 | /* remove current mandatory value of the target ... */ |
| 2247 | dev->target->flags &= ~LYS_MAND_MASK; |
| 2248 | |
| 2249 | /* ... and replace it with the value specified in deviation */ |
| 2250 | dev->target->flags |= dev->deviate->flags & LYS_MAND_MASK; |
| 2251 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 2252 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
| 2253 | for (parent = dev->target->parent; |
| 2254 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION)); |
| 2255 | parent = parent->parent) { |
| 2256 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 2257 | /* stop also on presence containers */ |
| 2258 | break; |
| 2259 | } |
| 2260 | } |
| 2261 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 2262 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 2263 | if (lyp_check_mandatory_choice(parent)) { |
| 2264 | goto error; |
| 2265 | } |
| 2266 | } |
| 2267 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2268 | return EXIT_SUCCESS; |
| 2269 | |
| 2270 | error: |
| 2271 | return EXIT_FAILURE; |
| 2272 | } |
| 2273 | |
| 2274 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2275 | 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] | 2276 | { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2277 | uint32_t *ui32val, *min, *max; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2278 | |
| 2279 | /* check target node type */ |
| 2280 | if (dev->target->nodetype == LYS_LEAFLIST) { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2281 | max = &((struct lys_node_leaflist *)dev->target)->max; |
| 2282 | min = &((struct lys_node_leaflist *)dev->target)->min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2283 | } else if (dev->target->nodetype == LYS_LIST) { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2284 | max = &((struct lys_node_list *)dev->target)->max; |
| 2285 | min = &((struct lys_node_list *)dev->target)->min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2286 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2287 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 2288 | 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] | 2289 | goto error; |
| 2290 | } |
| 2291 | |
| 2292 | if (type) { |
| 2293 | dev->deviate->max = value; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2294 | dev->deviate->max_set = 1; |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2295 | ui32val = max; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2296 | } else { |
| 2297 | dev->deviate->min = value; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2298 | dev->deviate->min_set = 1; |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2299 | ui32val = min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 2303 | /* check that there is no current value */ |
| 2304 | if (*ui32val) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2305 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 2306 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2307 | goto error; |
| 2308 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2309 | } else if (dev->deviate->mod == LY_DEVIATE_RPL) { |
| 2310 | /* unfortunately, there is no way to check reliably that there |
| 2311 | * was a value before, it could have been the default */ |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | /* add (already checked) and replace */ |
| 2315 | /* set new value specified in deviation */ |
| 2316 | *ui32val = value; |
| 2317 | |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2318 | /* check min-elements is smaller than max-elements */ |
| 2319 | if (*max && *min > *max) { |
| 2320 | if (type) { |
| 2321 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"max-elements\".", value); |
| 2322 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\"."); |
| 2323 | } else { |
| 2324 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"min-elements\".", value); |
| 2325 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 2326 | } |
| 2327 | goto error; |
| 2328 | } |
| 2329 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2330 | return EXIT_SUCCESS; |
| 2331 | |
| 2332 | error: |
| 2333 | return EXIT_FAILURE; |
| 2334 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2335 | |
| 2336 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2337 | yang_check_deviate_must(struct ly_ctx *ctx, struct type_deviation *dev) |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2338 | { |
| 2339 | int i; |
| 2340 | |
| 2341 | /* find must to delete, we are ok with just matching conditions */ |
| 2342 | for (i = 0; i < *dev->trg_must_size; i++) { |
| 2343 | if (ly_strequal(dev->deviate->must[dev->deviate->must_size - 1].expr, (*dev->trg_must)[i].expr, 1)) { |
| 2344 | /* we have a match, free the must structure ... */ |
| 2345 | lys_restr_free(ctx, &((*dev->trg_must)[i])); |
| 2346 | /* ... and maintain the array */ |
| 2347 | (*dev->trg_must_size)--; |
| 2348 | if (i != *dev->trg_must_size) { |
| 2349 | (*dev->trg_must)[i].expr = (*dev->trg_must)[*dev->trg_must_size].expr; |
| 2350 | (*dev->trg_must)[i].dsc = (*dev->trg_must)[*dev->trg_must_size].dsc; |
| 2351 | (*dev->trg_must)[i].ref = (*dev->trg_must)[*dev->trg_must_size].ref; |
| 2352 | (*dev->trg_must)[i].eapptag = (*dev->trg_must)[*dev->trg_must_size].eapptag; |
| 2353 | (*dev->trg_must)[i].emsg = (*dev->trg_must)[*dev->trg_must_size].emsg; |
| 2354 | } |
| 2355 | if (!(*dev->trg_must_size)) { |
| 2356 | free(*dev->trg_must); |
| 2357 | *dev->trg_must = NULL; |
| 2358 | } else { |
| 2359 | (*dev->trg_must)[*dev->trg_must_size].expr = NULL; |
| 2360 | (*dev->trg_must)[*dev->trg_must_size].dsc = NULL; |
| 2361 | (*dev->trg_must)[*dev->trg_must_size].ref = NULL; |
| 2362 | (*dev->trg_must)[*dev->trg_must_size].eapptag = NULL; |
| 2363 | (*dev->trg_must)[*dev->trg_must_size].emsg = NULL; |
| 2364 | } |
| 2365 | |
| 2366 | i = -1; /* set match flag */ |
| 2367 | break; |
| 2368 | } |
| 2369 | } |
| 2370 | if (i != -1) { |
| 2371 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2372 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->must[dev->deviate->must_size - 1].expr, "must"); |
| 2373 | 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] | 2374 | return EXIT_FAILURE; |
| 2375 | } |
| 2376 | |
| 2377 | return EXIT_SUCCESS; |
| 2378 | } |
| 2379 | |
| 2380 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2381 | 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] | 2382 | { |
| 2383 | struct lys_node_list *list; |
| 2384 | int i, j; |
| 2385 | |
| 2386 | list = (struct lys_node_list *)dev->target; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2387 | 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] | 2388 | dev->deviate->unique_size++; |
| 2389 | goto error; |
| 2390 | } |
| 2391 | |
| 2392 | /* find unique structures to delete */ |
| 2393 | for (i = 0; i < list->unique_size; i++) { |
| 2394 | if (list->unique[i].expr_size != dev->deviate->unique[dev->deviate->unique_size].expr_size) { |
| 2395 | continue; |
| 2396 | } |
| 2397 | |
| 2398 | for (j = 0; j < dev->deviate->unique[dev->deviate->unique_size].expr_size; j++) { |
| 2399 | if (!ly_strequal(list->unique[i].expr[j], dev->deviate->unique[dev->deviate->unique_size].expr[j], 1)) { |
| 2400 | break; |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | if (j == dev->deviate->unique[dev->deviate->unique_size].expr_size) { |
| 2405 | /* we have a match, free the unique structure ... */ |
| 2406 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2407 | lydict_remove(module->ctx, list->unique[i].expr[j]); |
| 2408 | } |
| 2409 | free(list->unique[i].expr); |
| 2410 | /* ... and maintain the array */ |
| 2411 | list->unique_size--; |
| 2412 | if (i != list->unique_size) { |
| 2413 | list->unique[i].expr_size = list->unique[list->unique_size].expr_size; |
| 2414 | list->unique[i].expr = list->unique[list->unique_size].expr; |
| 2415 | } |
| 2416 | |
| 2417 | if (!list->unique_size) { |
| 2418 | free(list->unique); |
| 2419 | list->unique = NULL; |
| 2420 | } else { |
| 2421 | list->unique[list->unique_size].expr_size = 0; |
| 2422 | list->unique[list->unique_size].expr = NULL; |
| 2423 | } |
| 2424 | |
| 2425 | i = -1; /* set match flag */ |
| 2426 | break; |
| 2427 | } |
| 2428 | } |
| 2429 | dev->deviate->unique_size++; |
| 2430 | |
| 2431 | if (i != -1) { |
| 2432 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2433 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique"); |
| 2434 | 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] | 2435 | goto error; |
| 2436 | } |
| 2437 | |
| 2438 | free(value); |
| 2439 | return EXIT_SUCCESS; |
| 2440 | |
| 2441 | error: |
| 2442 | free(value); |
| 2443 | return EXIT_FAILURE; |
| 2444 | } |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2445 | |
| 2446 | int |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2447 | 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] | 2448 | { |
| 2449 | int i, rc; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2450 | unsigned int u; |
| 2451 | const char *value, *target_name; |
| 2452 | struct lys_node_leaflist *llist; |
| 2453 | struct lys_node_leaf *leaf; |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2454 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2455 | /* now check whether default value, if any, matches the type */ |
| 2456 | for (u = 0; u < dflt_check->number; ++u) { |
| 2457 | value = NULL; |
| 2458 | rc = EXIT_SUCCESS; |
| 2459 | if (dflt_check->set.s[u]->nodetype == LYS_LEAF) { |
| 2460 | leaf = (struct lys_node_leaf *)dflt_check->set.s[u]; |
| 2461 | target_name = leaf->name; |
| 2462 | rc = unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, value = leaf->dflt); |
| 2463 | } else { /* LYS_LEAFLIST */ |
| 2464 | llist = (struct lys_node_leaflist *)dflt_check->set.s[u]; |
| 2465 | target_name = llist->name; |
| 2466 | for (i = 0; i < llist->dflt_size; i++) { |
| 2467 | rc = unres_schema_add_str(module, unres, &llist->type, UNRES_TYPE_DFLT, value = llist->dflt[i]); |
| 2468 | if (rc == -1) { |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2469 | break; |
| 2470 | } |
| 2471 | } |
| 2472 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2473 | if (rc == -1) { |
| 2474 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default"); |
| 2475 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, |
| 2476 | "The default value \"%s\" of the deviated node \"%s\"no longer matches its type.", |
| 2477 | target_name); |
| 2478 | return EXIT_FAILURE; |
| 2479 | } |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2480 | } |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2481 | |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2482 | return EXIT_SUCCESS; |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 2483 | |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2484 | } |
| 2485 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2486 | static int |
| 2487 | yang_fill_include(struct lys_module *trg, char *value, struct lys_include *inc, |
| 2488 | struct unres_schema *unres) |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2489 | { |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2490 | struct lys_submodule *submodule; |
| 2491 | struct lys_module *module; |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2492 | const char *str; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2493 | int rc; |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2494 | int ret = 0; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2495 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2496 | str = lydict_insert_zc(trg->ctx, value); |
| 2497 | if (trg->version) { |
| 2498 | submodule = (struct lys_submodule *)trg; |
| 2499 | module = ((struct lys_submodule *)trg)->belongsto; |
| 2500 | } else { |
| 2501 | submodule = NULL; |
| 2502 | module = trg; |
| 2503 | } |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2504 | rc = lyp_check_include(module, submodule, str, inc, unres); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2505 | if (!rc) { |
| 2506 | /* success, copy the filled data into the final array */ |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2507 | memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc); |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 2508 | trg->inc_size++; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2509 | } else if (rc == -1) { |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2510 | ret = -1; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2511 | } |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2512 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2513 | lydict_remove(trg->ctx, str); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2514 | return ret; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2515 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2516 | |
| 2517 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2518 | 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] | 2519 | { |
| 2520 | char *prefix; |
| 2521 | char *identif; |
| 2522 | const char *ns = NULL; |
| 2523 | int i; |
| 2524 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2525 | /* check to the same pointer */ |
| 2526 | if (data_node != actual) { |
| 2527 | return EXIT_SUCCESS; |
| 2528 | } |
| 2529 | |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2530 | prefix = strdup(value); |
| 2531 | if (!prefix) { |
| 2532 | LOGMEM; |
| 2533 | goto error; |
| 2534 | } |
| 2535 | /* find prefix anf identificator*/ |
| 2536 | identif = strchr(prefix, ':'); |
Pavol Vican | fbd0278 | 2016-08-29 11:14:45 +0200 | [diff] [blame] | 2537 | if (!identif) { |
| 2538 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, prefix); |
| 2539 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The extension must have prefix."); |
| 2540 | goto error; |
| 2541 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2542 | *identif = '\0'; |
| 2543 | identif++; |
| 2544 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2545 | for(i = 0; i < module->imp_size; ++i) { |
| 2546 | if (!strcmp(module->imp[i].prefix, prefix)) { |
| 2547 | ns = module->imp[i].module->ns; |
| 2548 | break; |
| 2549 | } |
| 2550 | } |
Pavol Vican | 1ff1b7b | 2016-04-07 09:51:49 +0200 | [diff] [blame] | 2551 | if (!ns && !strcmp(module->prefix, prefix)) { |
| 2552 | ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns; |
| 2553 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2554 | if (ns && !strcmp(ns, LY_NSNACM)) { |
| 2555 | if (!strcmp(identif, "default-deny-write")) { |
| 2556 | data_node->nacm |= LYS_NACM_DENYW; |
| 2557 | } else if (!strcmp(identif, "default-deny-all")) { |
| 2558 | data_node->nacm |= LYS_NACM_DENYA; |
| 2559 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2560 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif); |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2561 | goto error; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2562 | } |
| 2563 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2564 | free(prefix); |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2565 | return EXIT_SUCCESS; |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2566 | |
| 2567 | error: |
| 2568 | free(prefix); |
| 2569 | return EXIT_FAILURE; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | void |
| 2573 | nacm_inherit(struct lys_module *module) |
| 2574 | { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2575 | struct lys_node *next, *elem, *tmp_node, *tmp_child; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2576 | |
| 2577 | LY_TREE_DFS_BEGIN(module->data, next, elem) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2578 | tmp_node = NULL; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2579 | if (elem->parent) { |
| 2580 | switch (elem->nodetype) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2581 | case LYS_GROUPING: |
| 2582 | /* extension nacm not inherited*/ |
| 2583 | break; |
| 2584 | case LYS_CHOICE: |
| 2585 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2586 | case LYS_ANYDATA: |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2587 | case LYS_USES: |
| 2588 | if (elem->parent->nodetype != LYS_GROUPING) { |
| 2589 | elem->nacm |= elem->parent->nacm; |
| 2590 | } |
| 2591 | break; |
| 2592 | case LYS_CONTAINER: |
| 2593 | case LYS_LIST: |
| 2594 | case LYS_CASE: |
| 2595 | case LYS_NOTIF: |
| 2596 | case LYS_RPC: |
| 2597 | case LYS_INPUT: |
| 2598 | case LYS_OUTPUT: |
| 2599 | case LYS_AUGMENT: |
| 2600 | elem->nacm |= elem->parent->nacm; |
| 2601 | break; |
| 2602 | case LYS_LEAF: |
| 2603 | case LYS_LEAFLIST: |
| 2604 | tmp_node = elem; |
| 2605 | tmp_child = elem->child; |
| 2606 | elem->child = NULL; |
| 2607 | default: |
| 2608 | break; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | LY_TREE_DFS_END(module->data, next, elem); |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2612 | if (tmp_node) { |
| 2613 | tmp_node->child = tmp_child; |
| 2614 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2615 | } |
| 2616 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2617 | |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2618 | int |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2619 | store_flags(struct lys_node *node, uint8_t flags, int config_opt) |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2620 | { |
Michal Vasko | e10d14a | 2016-08-26 15:17:13 +0200 | [diff] [blame] | 2621 | struct lys_node *elem; |
| 2622 | |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2623 | node->flags |= (config_opt == CONFIG_IGNORE) ? flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET)): flags; |
| 2624 | if (config_opt == CONFIG_INHERIT_ENABLE) { |
| 2625 | if (!(node->flags & LYS_CONFIG_MASK)) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2626 | /* get config flag from parent */ |
Pavol Vican | b507138 | 2016-09-30 14:54:23 +0200 | [diff] [blame] | 2627 | if (node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 2628 | node->flags |= LYS_CONFIG_R; |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2629 | } else { |
| 2630 | /* default config is true */ |
| 2631 | node->flags |= LYS_CONFIG_W; |
| 2632 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2633 | } else { |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2634 | /* do we even care about config flags? */ |
| 2635 | 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] | 2636 | |
Pavol Vican | 1dac40c | 2016-09-28 11:39:26 +0200 | [diff] [blame] | 2637 | if (!elem && (node->flags & LYS_CONFIG_W) && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 2638 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 2639 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 2640 | return EXIT_FAILURE; |
| 2641 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2642 | } |
| 2643 | } |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2644 | |
| 2645 | return EXIT_SUCCESS; |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2646 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2647 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2648 | int |
| 2649 | yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, |
| 2650 | const char *data, unsigned int size_data, struct lys_node **node) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2651 | { |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2652 | unsigned int size; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2653 | YY_BUFFER_STATE bp; |
Pavol Vican | 802af1e | 2016-03-23 20:42:26 +0100 | [diff] [blame] | 2654 | yyscan_t scanner = NULL; |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame^] | 2655 | int ret = EXIT_SUCCESS, remove_import = 1; |
| 2656 | struct lys_module *trg; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2657 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2658 | size = (size_data) ? size_data : strlen(data) + 2; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2659 | yylex_init(&scanner); |
| 2660 | bp = yy_scan_buffer((char *)data, size, scanner); |
| 2661 | yy_switch_to_buffer(bp, scanner); |
Pavol Vican | 082afd0 | 2016-10-25 12:39:15 +0200 | [diff] [blame^] | 2662 | if (yyparse(scanner, NULL, module, submodule, unres, node, &remove_import)) { |
| 2663 | if (remove_import) { |
| 2664 | trg = (submodule) ? (struct lys_module *)submodule : module; |
| 2665 | yang_free_import(trg->ctx, trg->imp, 0, trg->imp_size); |
| 2666 | yang_free_include(trg->ctx, trg->inc, 0, trg->inc_size); |
| 2667 | trg->inc_size = 0; |
| 2668 | trg->imp_size = 0; |
| 2669 | } |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2670 | ret = EXIT_FAILURE; |
| 2671 | } |
| 2672 | yy_delete_buffer(bp, scanner); |
| 2673 | yylex_destroy(scanner); |
| 2674 | return ret; |
| 2675 | } |
| 2676 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2677 | struct lys_module * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2678 | 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] | 2679 | { |
| 2680 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2681 | struct lys_module *tmp_module, *module = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2682 | struct unres_schema *unres = NULL; |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2683 | struct lys_node *node = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2684 | |
| 2685 | unres = calloc(1, sizeof *unres); |
| 2686 | if (!unres) { |
| 2687 | LOGMEM; |
| 2688 | goto error; |
| 2689 | } |
| 2690 | |
| 2691 | module = calloc(1, sizeof *module); |
| 2692 | if (!module) { |
| 2693 | LOGMEM; |
Pavol Vican | f7994fb | 2016-04-05 21:55:53 +0200 | [diff] [blame] | 2694 | goto error; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | /* initiale module */ |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 2698 | module->ctx = ctx; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2699 | module->type = 0; |
| 2700 | module->implemented = (implement ? 1 : 0); |
| 2701 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2702 | if (yang_parse_mem(module, NULL, unres, data, size, &node)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2703 | goto error; |
| 2704 | } |
| 2705 | |
| 2706 | if (module && unres->count && resolve_unres_schema(module, unres)) { |
| 2707 | goto error; |
| 2708 | } |
| 2709 | |
| 2710 | if (revision) { |
| 2711 | /* check revision of the parsed model */ |
| 2712 | if (!module->rev_size || strcmp(revision, module->rev[0].date)) { |
| 2713 | LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", |
| 2714 | module->name, module->rev[0].date, revision); |
| 2715 | goto error; |
| 2716 | } |
| 2717 | } |
| 2718 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2719 | tmp_module = module; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2720 | if (lyp_ctx_add_module(&module)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2721 | goto error; |
| 2722 | } |
| 2723 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2724 | if (module == tmp_module) { |
| 2725 | nacm_inherit(module); |
| 2726 | } |
| 2727 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 2728 | if (module->deviation_size && !module->implemented) { |
| 2729 | LOGVRB("Module \"%s\" includes deviations, changing its conformance to \"implement\".", module->name); |
| 2730 | /* deviations always causes target to be made implemented, |
| 2731 | * but augents and leafrefs not, so we have to apply them now */ |
| 2732 | if (lys_set_implemented(module)) { |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2733 | goto error; |
| 2734 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2735 | } |
| 2736 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2737 | unres_schema_free(NULL, &unres); |
| 2738 | LOGVRB("Module \"%s\" successfully parsed.", module->name); |
| 2739 | return module; |
| 2740 | |
| 2741 | error: |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2742 | /* cleanup */ |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2743 | unres_schema_free(module, &unres); |
| 2744 | if (!module || !module->name) { |
| 2745 | free(module); |
| 2746 | LOGERR(ly_errno, "Module parsing failed."); |
| 2747 | return NULL; |
| 2748 | } |
| 2749 | |
| 2750 | LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2751 | |
| 2752 | lys_sub_module_remove_devs_augs(module); |
| 2753 | lys_free(module, NULL, 1); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2754 | return NULL; |
| 2755 | } |
| 2756 | |
| 2757 | struct lys_submodule * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2758 | 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] | 2759 | { |
| 2760 | struct lys_submodule *submodule; |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2761 | struct lys_node *node = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2762 | |
| 2763 | submodule = calloc(1, sizeof *submodule); |
| 2764 | if (!submodule) { |
| 2765 | LOGMEM; |
| 2766 | goto error; |
| 2767 | } |
| 2768 | |
| 2769 | submodule->ctx = module->ctx; |
| 2770 | submodule->type = 1; |
| 2771 | submodule->belongsto = module; |
| 2772 | |
Pavol Vican | 9d50a77 | 2016-10-14 22:23:36 +0200 | [diff] [blame] | 2773 | if (yang_parse_mem(module, submodule, unres, data, size, &node)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2774 | goto error; |
| 2775 | } |
| 2776 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2777 | LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2778 | return submodule; |
| 2779 | |
| 2780 | error: |
| 2781 | /* cleanup */ |
| 2782 | unres_schema_free((struct lys_module *)submodule, &unres); |
| 2783 | |
| 2784 | if (!submodule || !submodule->name) { |
| 2785 | free(submodule); |
| 2786 | LOGERR(ly_errno, "Submodule parsing failed."); |
| 2787 | return NULL; |
| 2788 | } |
| 2789 | |
| 2790 | LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name); |
| 2791 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2792 | lys_sub_module_remove_devs_augs((struct lys_module *)submodule); |
| 2793 | lys_submodule_module_data_free(submodule); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2794 | lys_submodule_free(submodule, NULL); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2795 | return NULL; |
| 2796 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2797 | |
| 2798 | static int |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2799 | read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output) |
| 2800 | { |
| 2801 | int k = 0, j; |
| 2802 | |
| 2803 | while (in_index < size) { |
| 2804 | if (input[in_index] == ' ') { |
| 2805 | k++; |
| 2806 | } else if (input[in_index] == '\t') { |
| 2807 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2808 | k += 8; |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2809 | } else if (input[in_index] == '\\' && input[in_index + 1] == 't') { |
| 2810 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2811 | k += 8; |
| 2812 | ++in_index; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2813 | } else { |
| 2814 | break; |
| 2815 | } |
| 2816 | ++in_index; |
| 2817 | if (k >= indent) { |
| 2818 | for (j = k - indent; j > 0; --j) { |
| 2819 | output[*out_index] = ' '; |
| 2820 | ++(*out_index); |
| 2821 | } |
| 2822 | break; |
| 2823 | } |
| 2824 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2825 | return in_index - 1; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | char * |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2829 | yang_read_string(const char *input, char *output, int size, int offset, int indent, int version) { |
| 2830 | int i = 0, out_index = offset, space = 0; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2831 | |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2832 | while (i < size) { |
| 2833 | switch (input[i]) { |
| 2834 | case '\n': |
| 2835 | out_index -= space; |
| 2836 | output[out_index] = '\n'; |
| 2837 | space = 0; |
| 2838 | i = read_indent(input, indent, size, i + 1, &out_index, output); |
| 2839 | break; |
| 2840 | case ' ': |
| 2841 | case '\t': |
| 2842 | output[out_index] = input[i]; |
| 2843 | ++space; |
| 2844 | break; |
| 2845 | case '\\': |
| 2846 | if (input[i + 1] == 'n') { |
| 2847 | out_index -= space; |
| 2848 | output[out_index] = '\n'; |
| 2849 | space = 0; |
| 2850 | i = read_indent(input, indent, size, i + 2, &out_index, output); |
| 2851 | } else if (input[i + 1] == 't') { |
| 2852 | output[out_index] = '\t'; |
| 2853 | ++i; |
| 2854 | ++space; |
| 2855 | } else if (input[i + 1] == '\\') { |
| 2856 | output[out_index] = '\\'; |
| 2857 | ++i; |
| 2858 | } else if ((i + 1) != size && input[i + 1] == '"') { |
| 2859 | output[out_index] = '"'; |
| 2860 | ++i; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2861 | } else { |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2862 | if (version < 2) { |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2863 | output[out_index] = input[i]; |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2864 | } else { |
| 2865 | /* YANG 1.1 backslash must not be followed by any other character */ |
| 2866 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input); |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2867 | return NULL; |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2868 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2869 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2870 | break; |
| 2871 | default: |
| 2872 | output[out_index] = input[i]; |
| 2873 | space = 0; |
| 2874 | break; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2875 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2876 | ++i; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2877 | ++out_index; |
| 2878 | } |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2879 | output[out_index] = '\0'; |
| 2880 | if (size != out_index) { |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2881 | output = realloc(output, out_index + 1); |
Pavol Vican | 0fbb57b | 2016-09-27 21:46:12 +0200 | [diff] [blame] | 2882 | if (!output) { |
| 2883 | LOGMEM; |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2884 | return NULL; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2885 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2886 | } |
Pavol Vican | 3f59889 | 2016-09-28 15:41:07 +0200 | [diff] [blame] | 2887 | return output; |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2888 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2889 | |
| 2890 | /* free function */ |
| 2891 | |
| 2892 | static void |
| 2893 | yang_free_import(struct ly_ctx *ctx, struct lys_import *imp, uint8_t start, uint8_t size) |
| 2894 | { |
| 2895 | uint8_t i; |
| 2896 | |
| 2897 | for (i = start; i < size; ++i){ |
| 2898 | free((char *)imp[i].module); |
| 2899 | lydict_remove(ctx, imp[i].prefix); |
| 2900 | lydict_remove(ctx, imp[i].dsc); |
| 2901 | lydict_remove(ctx, imp[i].ref); |
| 2902 | } |
| 2903 | } |
| 2904 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2905 | static void |
| 2906 | yang_free_include(struct ly_ctx *ctx, struct lys_include *inc, uint8_t start, uint8_t size) |
| 2907 | { |
| 2908 | uint8_t i; |
| 2909 | |
| 2910 | for (i = start; i < size; ++i){ |
| 2911 | free((char *)inc[i].submodule); |
| 2912 | lydict_remove(ctx, inc[i].dsc); |
| 2913 | lydict_remove(ctx, inc[i].ref); |
| 2914 | } |
| 2915 | } |
| 2916 | |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2917 | /* check function*/ |
| 2918 | |
| 2919 | int |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2920 | yang_check_imports(struct lys_module *module, struct unres_schema *unres) |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2921 | { |
| 2922 | struct lys_import *imp; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2923 | struct lys_include *inc; |
| 2924 | uint8_t imp_size, inc_size, j = 0, i = 0; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2925 | size_t size; |
| 2926 | char *s; |
| 2927 | |
| 2928 | imp = module->imp; |
| 2929 | imp_size = module->imp_size; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2930 | inc = module->inc; |
| 2931 | inc_size = module->inc_size; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2932 | |
| 2933 | if (imp_size) { |
| 2934 | size = (imp_size * sizeof *module->imp) + sizeof(void*); |
| 2935 | module->imp_size = 0; |
| 2936 | module->imp = calloc(1, size); |
| 2937 | if (!module->imp) { |
| 2938 | LOGMEM; |
| 2939 | goto error; |
| 2940 | } |
| 2941 | /* set stop block for possible realloc */ |
| 2942 | module->imp[imp_size].module = (void*)0x1; |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2943 | } |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2944 | |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2945 | if (inc_size) { |
| 2946 | size = (inc_size * sizeof *module->inc) + sizeof(void*); |
| 2947 | module->inc_size = 0; |
| 2948 | module->inc = calloc(1, size); |
| 2949 | if (!module->inc) { |
| 2950 | LOGMEM; |
| 2951 | goto error; |
| 2952 | } |
| 2953 | /* set stop block for possible realloc */ |
| 2954 | module->inc[inc_size].submodule = (void*)0x1; |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | for (i = 0; i < imp_size; ++i) { |
| 2958 | s = (char *) imp[i].module; |
| 2959 | imp[i].module = NULL; |
| 2960 | if (yang_fill_import(module, &imp[i], &module->imp[module->imp_size], s)) { |
| 2961 | ++i; |
| 2962 | goto error; |
| 2963 | } |
| 2964 | } |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2965 | for (j = 0; j < inc_size; ++j) { |
| 2966 | s = (char *) inc[i].submodule; |
| 2967 | inc[i].submodule = NULL; |
| 2968 | if (yang_fill_include(module, s, &inc[i], unres)) { |
| 2969 | ++i; |
| 2970 | goto error; |
| 2971 | } |
| 2972 | } |
| 2973 | free(inc); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2974 | free(imp); |
| 2975 | |
| 2976 | return EXIT_SUCCESS; |
| 2977 | |
| 2978 | error: |
| 2979 | yang_free_import(module->ctx, imp, i, imp_size); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2980 | yang_free_include(module->ctx, inc, j, inc_size); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2981 | free(imp); |
Pavol Vican | ec423c9 | 2016-10-24 21:33:43 +0200 | [diff] [blame] | 2982 | free(inc); |
Pavol Vican | 1cc4e19 | 2016-10-24 16:38:31 +0200 | [diff] [blame] | 2983 | return EXIT_FAILURE; |
| 2984 | } |