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