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