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