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; |
| 704 | |
| 705 | /* count the number of unique leafs in the value */ |
| 706 | vaux = value; |
| 707 | while ((vaux = strpbrk(vaux, " \t\n"))) { |
| 708 | unique->expr_size++; |
| 709 | while (isspace(*vaux)) { |
| 710 | vaux++; |
| 711 | } |
| 712 | } |
| 713 | unique->expr_size++; |
| 714 | unique->expr = calloc(unique->expr_size, sizeof *unique->expr); |
| 715 | if (!unique->expr) { |
| 716 | LOGMEM; |
| 717 | goto error; |
| 718 | } |
| 719 | |
| 720 | for (i = 0; i < unique->expr_size; i++) { |
| 721 | vaux = strpbrk(value, " \t\n"); |
| 722 | if (!vaux) { |
| 723 | /* the last token, lydict_insert() will count its size on its own */ |
| 724 | vaux = value; |
| 725 | } |
| 726 | |
| 727 | /* store token into unique structure */ |
| 728 | unique->expr[i] = lydict_insert(module->ctx, value, vaux - value); |
| 729 | |
| 730 | /* check that the expression does not repeat */ |
| 731 | for (j = 0; j < i; j++) { |
| 732 | if (ly_strequal(unique->expr[j], unique->expr[i], 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 733 | LOGVAL(LYE_INARG, LY_VLOG_LYS, list, unique->expr[i], "unique"); |
| 734 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 735 | goto error; |
| 736 | } |
| 737 | } |
| 738 | /* try to resolve leaf */ |
| 739 | if (unres) { |
Pavol Vican | 18b1021 | 2016-04-11 15:41:52 +0200 | [diff] [blame] | 740 | if (unres_schema_add_str(module, unres, (struct lys_node *) list, UNRES_LIST_UNIQ, unique->expr[i]) == -1) { |
| 741 | goto error; |
| 742 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 743 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 744 | if (resolve_unique((struct lys_node *)list, unique->expr[i])) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 745 | goto error; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /* move to next token */ |
| 750 | value = vaux; |
| 751 | while(isspace(*value)) { |
| 752 | value++; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | return EXIT_SUCCESS; |
| 757 | |
| 758 | error: |
| 759 | return EXIT_FAILURE; |
| 760 | } |
| 761 | |
| 762 | int |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 763 | yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres) |
| 764 | { |
| 765 | uint8_t k; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 766 | char *str; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 767 | |
| 768 | for(k=0; k<list->unique_size; k++) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 769 | str = (char *)list->unique[k].expr; |
| 770 | if (yang_fill_unique(module, list, &list->unique[k], str, unres)) { |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 771 | goto error; |
| 772 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 773 | free(str); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 774 | } |
| 775 | return EXIT_SUCCESS; |
| 776 | |
| 777 | error: |
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 | return EXIT_FAILURE; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 780 | } |
| 781 | |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 782 | static int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 783 | 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] | 784 | { |
| 785 | const char *value, *tmp; |
| 786 | int rc, ret = EXIT_FAILURE; |
| 787 | |
| 788 | value = tmp = type->info.lref.path; |
| 789 | /* store in the JSON format */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 790 | value = transform_schema2json(module, value); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 791 | if (!value) { |
| 792 | goto end; |
| 793 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 794 | rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 795 | lydict_remove(module->ctx, value); |
| 796 | |
| 797 | if (rc == -1) { |
| 798 | goto end; |
| 799 | } |
| 800 | |
| 801 | ret = EXIT_SUCCESS; |
| 802 | |
| 803 | end: |
| 804 | lydict_remove(module->ctx, tmp); |
| 805 | return ret; |
| 806 | } |
| 807 | |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 808 | int |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 809 | 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] | 810 | { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 811 | int i, j, rc; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 812 | int ret = -1; |
| 813 | const char *name, *value; |
| 814 | LY_DATA_TYPE base; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 815 | struct lys_node *siter; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 816 | struct lys_type *dertype; |
| 817 | struct lys_type_enum *enms_sc = NULL; |
| 818 | struct lys_type_bit *bits_sc = NULL; |
| 819 | struct lys_type_bit bit_tmp; |
| 820 | |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 821 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 822 | base = typ->base; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 823 | value = transform_schema2json(module, typ->name); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 824 | if (!value) { |
| 825 | goto error; |
| 826 | } |
| 827 | |
| 828 | i = parse_identifier(value); |
| 829 | if (i < 1) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 830 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 831 | lydict_remove(module->ctx, value); |
| 832 | goto error; |
| 833 | } |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 834 | /* module name */ |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 835 | name = value; |
| 836 | if (value[i]) { |
| 837 | typ->type->module_name = lydict_insert(module->ctx, value, i); |
| 838 | name += i; |
| 839 | if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 840 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 841 | lydict_remove(module->ctx, value); |
| 842 | goto error; |
| 843 | } |
| 844 | ++name; |
| 845 | } |
| 846 | |
| 847 | 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] | 848 | if (rc == -1) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 849 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, typ->type->module_name); |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 850 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 851 | goto error; |
| 852 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 853 | /* 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] | 854 | } else if (rc == EXIT_FAILURE) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 855 | LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name); |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 856 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 857 | ret = EXIT_FAILURE; |
| 858 | goto error; |
| 859 | } |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 860 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 861 | typ->type->base = typ->type->der->type.base; |
| 862 | if (base == 0) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 863 | base = typ->type->der->type.base; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 864 | } |
| 865 | switch (base) { |
| 866 | case LY_TYPE_STRING: |
| 867 | if (typ->type->base == LY_TYPE_BINARY) { |
| 868 | if (typ->type->info.str.pat_count) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 869 | 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] | 870 | goto error; |
| 871 | } |
| 872 | typ->type->info.binary.length = typ->type->info.str.length; |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 873 | 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] | 874 | 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] | 875 | goto error; |
| 876 | } |
| 877 | } else if (typ->type->base == LY_TYPE_STRING) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 878 | 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] | 879 | 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] | 880 | goto error; |
| 881 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 882 | } else { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 883 | 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] | 884 | goto error; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 885 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 886 | break; |
| 887 | case LY_TYPE_DEC64: |
| 888 | if (typ->type->base == LY_TYPE_DEC64) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 889 | 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] | 890 | 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] | 891 | goto error; |
| 892 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 893 | /* mandatory sub-statement(s) check */ |
| 894 | if (!typ->type->info.dec64.dig && !typ->type->der->type.der) { |
| 895 | /* decimal64 type directly derived from built-in type requires fraction-digits */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 896 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 897 | goto error; |
| 898 | } |
| 899 | if (typ->type->info.dec64.dig && typ->type->der->type.der) { |
| 900 | /* 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] | 901 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 902 | goto error; |
| 903 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 904 | } else if (typ->type->base >= LY_TYPE_INT8 && typ->type->base <=LY_TYPE_UINT64) { |
| 905 | if (typ->type->info.dec64.dig) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 906 | 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] | 907 | goto error; |
| 908 | } |
| 909 | typ->type->info.num.range = typ->type->info.dec64.range; |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 910 | 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] | 911 | 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] | 912 | goto error; |
| 913 | } |
| 914 | } else { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 915 | 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] | 916 | goto error; |
| 917 | } |
| 918 | break; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 919 | case LY_TYPE_ENUM: |
| 920 | if (typ->type->base != LY_TYPE_ENUM) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 921 | 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] | 922 | goto error; |
| 923 | } |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 924 | dertype = &typ->type->der->type; |
| 925 | |
| 926 | if (!dertype->der) { |
| 927 | if (!typ->type->info.enums.count) { |
| 928 | /* type is derived directly from buit-in enumeartion type and enum statement is required */ |
| 929 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type"); |
| 930 | goto error; |
| 931 | } |
| 932 | } else { |
| 933 | for (; !dertype->info.enums.count; dertype = &dertype->der->type); |
| 934 | if (module->version < 2 && typ->type->info.enums.count) { |
| 935 | /* type is not directly derived from built-in enumeration type and enum statement is prohibited |
| 936 | * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */ |
| 937 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum"); |
| 938 | goto error; |
| 939 | } |
| 940 | |
| 941 | /* restricted enumeration type - the name MUST be used in the base type */ |
| 942 | enms_sc = dertype->info.enums.enm; |
| 943 | for(i = 0; i < typ->type->info.enums.count; i++) { |
| 944 | for (j = 0; j < dertype->info.enums.count; j++) { |
| 945 | if (ly_strequal(enms_sc[j].name, typ->type->info.enums.enm[i].name, 1)) { |
| 946 | break; |
| 947 | } |
| 948 | } |
| 949 | if (j == dertype->info.enums.count) { |
| 950 | LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name); |
| 951 | goto error; |
| 952 | } |
| 953 | |
| 954 | if (typ->type->info.enums.enm[i].flags & LYS_AUTOASSIGNED) { |
| 955 | /* automatically assign value from base type */ |
| 956 | typ->type->info.enums.enm[i].value = enms_sc[j].value; |
| 957 | } else { |
| 958 | /* check that the assigned value corresponds to the original |
| 959 | * value of the enum in the base type */ |
| 960 | if (typ->type->info.enums.enm[i].value != enms_sc[j].value) { |
| 961 | /* typ->type->info.enums.enm[i].value - assigned value in restricted enum |
| 962 | * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */ |
| 963 | LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].value, |
| 964 | typ->type->info.enums.enm[i].name, enms_sc[j].value); |
| 965 | goto error; |
| 966 | } |
| 967 | } |
| 968 | } |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 969 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 970 | break; |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 971 | case LY_TYPE_BITS: |
| 972 | if (typ->type->base != LY_TYPE_BITS) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 973 | 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] | 974 | goto error; |
| 975 | } |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 976 | dertype = &typ->type->der->type; |
| 977 | |
| 978 | if (!dertype->der) { |
| 979 | if (!typ->type->info.bits.count) { |
| 980 | /* type is derived directly from buit-in bits type and bit statement is required */ |
| 981 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type"); |
| 982 | goto error; |
| 983 | } |
| 984 | } else { |
| 985 | for (; !dertype->info.enums.count; dertype = &dertype->der->type); |
| 986 | if (module->version < 2 && typ->type->info.bits.count) { |
| 987 | /* type is not directly derived from buit-in bits type and bit statement is prohibited, |
| 988 | * since YANG 1.1 the bit statements can be used to restrict the base bits type */ |
| 989 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit"); |
| 990 | goto error; |
| 991 | } |
| 992 | |
| 993 | bits_sc = dertype->info.bits.bit; |
| 994 | for (i = 0; i < typ->type->info.bits.count; i++) { |
| 995 | for (j = 0; j < dertype->info.bits.count; j++) { |
| 996 | if (ly_strequal(bits_sc[j].name, typ->type->info.bits.bit[i].name, 1)) { |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | if (j == dertype->info.bits.count) { |
| 1001 | LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, typ->type->info.bits.bit[i].name); |
| 1002 | goto error; |
| 1003 | } |
| 1004 | |
| 1005 | /* restricted bits type */ |
| 1006 | if (typ->type->info.bits.bit[i].flags & LYS_AUTOASSIGNED) { |
| 1007 | /* automatically assign position from base type */ |
| 1008 | typ->type->info.bits.bit[i].pos = bits_sc[j].pos; |
| 1009 | } else { |
| 1010 | /* check that the assigned position corresponds to the original |
| 1011 | * position of the bit in the base type */ |
| 1012 | if (typ->type->info.bits.bit[i].pos != bits_sc[j].pos) { |
| 1013 | /* typ->type->info.bits.bit[i].pos - assigned position in restricted bits |
| 1014 | * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */ |
| 1015 | LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, typ->type->info.bits.bit[i].pos, |
| 1016 | typ->type->info.bits.bit[i].name, bits_sc[j].pos); |
| 1017 | goto error; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 1021 | } |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 1022 | |
| 1023 | for (i = typ->type->info.bits.count - 1; i > 0; i--) { |
| 1024 | j = i; |
| 1025 | |
| 1026 | /* keep them ordered by position */ |
| 1027 | while (j && typ->type->info.bits.bit[j - 1].pos > typ->type->info.bits.bit[j].pos) { |
| 1028 | /* switch them */ |
| 1029 | memcpy(&bit_tmp, &typ->type->info.bits.bit[j], sizeof bit_tmp); |
| 1030 | memcpy(&typ->type->info.bits.bit[j], &typ->type->info.bits.bit[j - 1], sizeof bit_tmp); |
| 1031 | memcpy(&typ->type->info.bits.bit[j - 1], &bit_tmp, sizeof bit_tmp); |
| 1032 | j--; |
| 1033 | } |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 1034 | } |
| 1035 | break; |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1036 | case LY_TYPE_LEAFREF: |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1037 | if (typ->type->base == LY_TYPE_IDENT && (typ->flags & LYS_TYPE_BASE)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1038 | if (yang_read_identyref(module, typ->type, unres)) { |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1039 | goto error; |
| 1040 | } |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1041 | } else if (typ->type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1042 | /* flag resolving for later use */ |
| 1043 | if (!tpdftype) { |
| 1044 | for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter)); |
| 1045 | if (siter) { |
| 1046 | /* just a flag - do not resolve */ |
| 1047 | tpdftype = 1; |
| 1048 | } |
| 1049 | } |
| 1050 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1051 | if (typ->type->info.lref.path) { |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1052 | value = typ->type->info.lref.path; |
| 1053 | /* store in the JSON format */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1054 | typ->type->info.lref.path = transform_schema2json(module, value); |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1055 | lydict_remove(module->ctx, value); |
| 1056 | if (!typ->type->info.lref.path) { |
| 1057 | goto error; |
| 1058 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1059 | /* try to resolve leafref path only when this is instantiated |
| 1060 | * leaf, so it is not: |
| 1061 | * - typedef's type, |
| 1062 | * - in grouping definition, |
| 1063 | * - just instantiated in a grouping definition, |
| 1064 | * because in those cases the nodes referenced in path might not be present |
| 1065 | * and it is not a bug. */ |
| 1066 | 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] | 1067 | goto error; |
| 1068 | } |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1069 | } else if (!typ->type->der->type.der) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1070 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type"); |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1071 | goto error; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 1072 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1073 | /* copy leafref definition into the derived type */ |
| 1074 | typ->type->info.lref.path = lydict_insert(module->ctx, typ->type->der->type.info.lref.path, 0); |
| 1075 | /* and resolve the path at the place we are (if not in grouping/typedef) */ |
| 1076 | 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] | 1077 | goto error; |
| 1078 | } |
Radek Krejci | 742be35 | 2016-07-17 12:18:54 +0200 | [diff] [blame] | 1079 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 1080 | /* add pointer to leafref target, only on leaves (not in typedefs) */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1081 | 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] | 1082 | goto error; |
| 1083 | } |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1084 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1085 | } else { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 1086 | 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] | 1087 | goto error; |
| 1088 | } |
| 1089 | break; |
| 1090 | case LY_TYPE_IDENT: |
| 1091 | if (typ->type->der->type.der) { |
| 1092 | /* this is just a derived type with no base specified/required */ |
| 1093 | break; |
| 1094 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1095 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type"); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1096 | goto error; |
| 1097 | } |
| 1098 | break; |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1099 | case LY_TYPE_UNION: |
| 1100 | if (typ->type->base != LY_TYPE_UNION) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 1101 | 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] | 1102 | goto error; |
| 1103 | } |
| 1104 | if (!typ->type->info.uni.types) { |
| 1105 | if (typ->type->der->type.der) { |
| 1106 | /* this is just a derived type with no additional type specified/required */ |
| 1107 | break; |
| 1108 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1109 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type"); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1110 | goto error; |
| 1111 | } |
| 1112 | for (i = 0; i < typ->type->info.uni.count; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1113 | 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] | 1114 | tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) { |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1115 | goto error; |
| 1116 | } |
| 1117 | if (typ->type->info.uni.types[i].base == LY_TYPE_EMPTY) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1118 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", typ->name); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1119 | goto error; |
| 1120 | } else if (typ->type->info.uni.types[i].base == LY_TYPE_LEAFREF) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1121 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", typ->name); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1122 | goto error; |
| 1123 | } |
| 1124 | } |
| 1125 | break; |
Pavol Vican | a182796 | 2016-02-29 15:39:42 +0100 | [diff] [blame] | 1126 | |
| 1127 | default: |
| 1128 | if (base >= LY_TYPE_BINARY && base <= LY_TYPE_UINT64) { |
| 1129 | if (typ->type->base != base) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 1130 | 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] | 1131 | goto error; |
| 1132 | } |
| 1133 | } else { |
| 1134 | LOGINT; |
| 1135 | goto error; |
| 1136 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1137 | } |
| 1138 | return EXIT_SUCCESS; |
| 1139 | |
| 1140 | error: |
| 1141 | if (typ->type->module_name) { |
| 1142 | lydict_remove(module->ctx, typ->type->module_name); |
| 1143 | typ->type->module_name = NULL; |
| 1144 | } |
| 1145 | return ret; |
| 1146 | } |
| 1147 | |
| 1148 | void * |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1149 | 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] | 1150 | { |
| 1151 | struct yang_type *typ; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1152 | struct type_deviation *dev; |
| 1153 | struct lys_tpdf *tmp_parent; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1154 | |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 1155 | typ = calloc(1, sizeof *typ); |
| 1156 | if (!typ) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1157 | LOGMEM; |
| 1158 | return NULL; |
| 1159 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1160 | |
| 1161 | typ->flags = LY_YANG_STRUCTURE_FLAG; |
| 1162 | switch (type) { |
| 1163 | case LEAF_KEYWORD: |
| 1164 | ((struct lys_node_leaf *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1165 | ((struct lys_node_leaf *)parent)->type.parent = (struct lys_tpdf *)parent; |
| 1166 | typ->type = &((struct lys_node_leaf *)parent)->type; |
| 1167 | break; |
Pavol Vican | a55992a | 2016-03-01 13:37:17 +0100 | [diff] [blame] | 1168 | case LEAF_LIST_KEYWORD: |
| 1169 | ((struct lys_node_leaflist *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1170 | ((struct lys_node_leaflist *)parent)->type.parent = (struct lys_tpdf *)parent; |
| 1171 | typ->type = &((struct lys_node_leaflist *)parent)->type; |
| 1172 | break; |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1173 | case UNION_KEYWORD: |
| 1174 | ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ; |
| 1175 | typ->type = (struct lys_type *)parent; |
| 1176 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1177 | case TYPEDEF_KEYWORD: |
| 1178 | ((struct lys_tpdf *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1179 | typ->type = &((struct lys_tpdf *)parent)->type; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1180 | break; |
| 1181 | case REPLACE_KEYWORD: |
| 1182 | /* deviation replace type*/ |
| 1183 | dev = (struct type_deviation *)parent; |
| 1184 | if (dev->deviate->type) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1185 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "deviation"); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1186 | goto error; |
| 1187 | } |
| 1188 | /* check target node type */ |
| 1189 | if (dev->target->nodetype == LYS_LEAF) { |
| 1190 | typ->type = &((struct lys_node_leaf *)dev->target)->type; |
| 1191 | } else if (dev->target->nodetype == LYS_LEAFLIST) { |
| 1192 | typ->type = &((struct lys_node_leaflist *)dev->target)->type; |
| 1193 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1194 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "type"); |
| 1195 | 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] | 1196 | goto error; |
| 1197 | } |
| 1198 | |
| 1199 | /* remove type and initialize it */ |
| 1200 | lys_type_free(module->ctx, typ->type); |
| 1201 | tmp_parent = typ->type->parent; |
| 1202 | memset(typ->type, 0, sizeof *typ->type); |
| 1203 | typ->type->parent = tmp_parent; |
| 1204 | |
| 1205 | /* replace it with the value specified in deviation */ |
| 1206 | /* HACK for unres */ |
| 1207 | typ->type->der = (struct lys_tpdf *)typ; |
| 1208 | dev->deviate->type = typ->type; |
| 1209 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1210 | default: |
| 1211 | goto error; |
| 1212 | break; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1213 | } |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1214 | typ->name = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1215 | return typ; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1216 | |
| 1217 | error: |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1218 | free(value); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1219 | free(typ); |
| 1220 | return NULL; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1221 | } |
| 1222 | |
Pavol Vican | be9e383 | 2016-04-28 02:21:37 +0200 | [diff] [blame] | 1223 | void |
| 1224 | yang_delete_type(struct lys_module *module, struct yang_type *stype) |
| 1225 | { |
| 1226 | int i; |
| 1227 | |
| 1228 | stype->type->base = stype->base; |
| 1229 | stype->type->der = NULL; |
| 1230 | lydict_remove(module->ctx, stype->name); |
| 1231 | if (stype->base == LY_TYPE_UNION) { |
| 1232 | for (i = 0; i < stype->type->info.uni.count; i++) { |
| 1233 | if (stype->type->info.uni.types[i].der) { |
| 1234 | yang_delete_type(module, (struct yang_type *)stype->type->info.uni.types[i].der); |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | free(stype); |
| 1239 | } |
| 1240 | |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1241 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1242 | 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] | 1243 | { |
| 1244 | struct lys_restr **length; |
| 1245 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1246 | if (typ->base == 0 || typ->base == LY_TYPE_STRING) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1247 | length = &typ->type->info.str.length; |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1248 | typ->base = LY_TYPE_STRING; |
| 1249 | } else if (typ->base == LY_TYPE_BINARY) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1250 | length = &typ->type->info.binary.length; |
| 1251 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1252 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected length statement."); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1253 | goto error; |
| 1254 | } |
| 1255 | |
| 1256 | if (*length) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1257 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "length", "type"); |
Pavol Vican | be9e383 | 2016-04-28 02:21:37 +0200 | [diff] [blame] | 1258 | goto error; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1259 | } |
| 1260 | *length = calloc(1, sizeof **length); |
| 1261 | if (!*length) { |
| 1262 | LOGMEM; |
| 1263 | goto error; |
| 1264 | } |
| 1265 | (*length)->expr = lydict_insert_zc(module->ctx, value); |
| 1266 | return *length; |
| 1267 | |
| 1268 | error: |
| 1269 | free(value); |
| 1270 | return NULL; |
| 1271 | |
| 1272 | } |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1273 | |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame^] | 1274 | int |
| 1275 | 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] | 1276 | { |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1277 | char *buf; |
| 1278 | size_t len; |
| 1279 | |
Michal Vasko | 0aee5c1 | 2016-06-17 14:27:26 +0200 | [diff] [blame] | 1280 | if (lyp_check_pattern(value, NULL)) { |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1281 | free(value); |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame^] | 1282 | return EXIT_FAILURE; |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1283 | } |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1284 | |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1285 | len = strlen(value); |
| 1286 | buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */ |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame^] | 1287 | |
| 1288 | if (!buf) { |
| 1289 | LOGMEM; |
| 1290 | free(value); |
| 1291 | return EXIT_FAILURE; |
| 1292 | } |
| 1293 | |
| 1294 | buf[0] = modifier; |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1295 | strcpy(&buf[1], value); |
| 1296 | free(value); |
| 1297 | |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame^] | 1298 | pattern->expr = lydict_insert_zc(module->ctx, buf); |
| 1299 | return EXIT_SUCCESS; |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1300 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1301 | |
| 1302 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1303 | 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] | 1304 | { |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1305 | if (typ->base != 0 && typ->base != LY_TYPE_DEC64) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1306 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected range statement."); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1307 | goto error; |
| 1308 | } |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1309 | typ->base = LY_TYPE_DEC64; |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1310 | if (typ->type->info.dec64.range) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1311 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "range", "type"); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1312 | goto error; |
| 1313 | } |
| 1314 | typ->type->info.dec64.range = calloc(1, sizeof *typ->type->info.dec64.range); |
| 1315 | if (!typ->type->info.dec64.range) { |
| 1316 | LOGMEM; |
| 1317 | goto error; |
| 1318 | } |
| 1319 | typ->type->info.dec64.range->expr = lydict_insert_zc(module->ctx, value); |
| 1320 | return typ->type->info.dec64.range; |
| 1321 | |
| 1322 | error: |
| 1323 | free(value); |
| 1324 | return NULL; |
| 1325 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1326 | |
| 1327 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1328 | yang_read_fraction(struct yang_type *typ, uint32_t value) |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1329 | { |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1330 | unsigned int i; |
| 1331 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1332 | if (typ->base == 0 || typ->base == LY_TYPE_DEC64) { |
| 1333 | typ->base = LY_TYPE_DEC64; |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1334 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1335 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected fraction-digits statement."); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1336 | goto error; |
| 1337 | } |
| 1338 | if (typ->type->info.dec64.dig) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1339 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "fraction-digits", "type"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1340 | goto error; |
| 1341 | } |
| 1342 | /* range check */ |
| 1343 | if (value < 1 || value > 18) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1344 | 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] | 1345 | goto error; |
| 1346 | } |
| 1347 | typ->type->info.dec64.dig = value; |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1348 | typ->type->info.dec64.div = 10; |
| 1349 | for (i = 1; i < value; i++) { |
| 1350 | typ->type->info.dec64.div *= 10; |
| 1351 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1352 | return EXIT_SUCCESS; |
| 1353 | |
| 1354 | error: |
| 1355 | return EXIT_FAILURE; |
| 1356 | } |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1357 | |
| 1358 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1359 | 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] | 1360 | { |
| 1361 | struct lys_type_enum *enm; |
| 1362 | int i; |
| 1363 | |
| 1364 | enm = &typ->type->info.enums.enm[typ->type->info.enums.count]; |
| 1365 | enm->name = lydict_insert_zc(module->ctx, value); |
| 1366 | |
| 1367 | /* the assigned name MUST NOT have any leading or trailing whitespace characters */ |
| 1368 | if (isspace(enm->name[0]) || isspace(enm->name[strlen(enm->name) - 1])) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1369 | LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1370 | goto error; |
| 1371 | } |
| 1372 | |
| 1373 | /* check the name uniqueness */ |
| 1374 | for (i = 0; i < typ->type->info.enums.count; i++) { |
| 1375 | 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] | 1376 | 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] | 1377 | goto error; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | typ->type->info.enums.count++; |
| 1382 | return enm; |
| 1383 | |
| 1384 | error: |
| 1385 | typ->type->info.enums.count++; |
| 1386 | return NULL; |
| 1387 | } |
| 1388 | |
| 1389 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1390 | 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] | 1391 | { |
| 1392 | int i, j; |
| 1393 | |
| 1394 | if (!assign) { |
| 1395 | /* assign value automatically */ |
| 1396 | if (*value > INT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1397 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value"); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1398 | goto error; |
| 1399 | } |
| 1400 | enm->value = *value; |
| 1401 | enm->flags |= LYS_AUTOASSIGNED; |
| 1402 | (*value)++; |
| 1403 | } |
| 1404 | |
| 1405 | /* check that the value is unique */ |
| 1406 | j = typ->type->info.enums.count-1; |
| 1407 | for (i = 0; i < j; i++) { |
| 1408 | 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] | 1409 | LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL, |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1410 | typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name, |
| 1411 | typ->type->info.enums.enm[i].name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1412 | goto error; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | return EXIT_SUCCESS; |
| 1417 | |
| 1418 | error: |
| 1419 | return EXIT_FAILURE; |
| 1420 | } |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1421 | |
| 1422 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1423 | 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] | 1424 | { |
| 1425 | int i; |
| 1426 | struct lys_type_bit *bit; |
| 1427 | |
| 1428 | bit = &typ->type->info.bits.bit[typ->type->info.bits.count]; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1429 | if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) { |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1430 | free(value); |
| 1431 | goto error; |
| 1432 | } |
| 1433 | bit->name = lydict_insert_zc(module->ctx, value); |
| 1434 | |
| 1435 | /* check the name uniqueness */ |
| 1436 | for (i = 0; i < typ->type->info.bits.count; i++) { |
| 1437 | if (!strcmp(typ->type->info.bits.bit[i].name, bit->name)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1438 | LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1439 | typ->type->info.bits.count++; |
| 1440 | goto error; |
| 1441 | } |
| 1442 | } |
| 1443 | typ->type->info.bits.count++; |
| 1444 | return bit; |
| 1445 | |
| 1446 | error: |
| 1447 | return NULL; |
| 1448 | } |
| 1449 | |
| 1450 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1451 | 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] | 1452 | { |
| 1453 | int i,j; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1454 | |
| 1455 | if (!assign) { |
| 1456 | /* assign value automatically */ |
| 1457 | if (*value > UINT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1458 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position"); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1459 | goto error; |
| 1460 | } |
| 1461 | bit->pos = (uint32_t)*value; |
| 1462 | bit->flags |= LYS_AUTOASSIGNED; |
| 1463 | (*value)++; |
| 1464 | } |
| 1465 | |
| 1466 | j = typ->type->info.bits.count - 1; |
| 1467 | /* check that the value is unique */ |
| 1468 | for (i = 0; i < j; i++) { |
| 1469 | if (typ->type->info.bits.bit[i].pos == bit->pos) { |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1470 | 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] | 1471 | goto error; |
| 1472 | } |
| 1473 | } |
| 1474 | |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1475 | return EXIT_SUCCESS; |
| 1476 | |
| 1477 | error: |
| 1478 | return EXIT_FAILURE; |
| 1479 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1480 | |
| 1481 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1482 | 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] | 1483 | { |
| 1484 | struct lys_tpdf *ret; |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1485 | struct lys_node *root; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1486 | |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1487 | root = (parent) ? NULL : lys_main_module(module)->data; |
| 1488 | 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] | 1489 | free(value); |
| 1490 | return NULL; |
| 1491 | } |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1492 | |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1493 | if (!parent) { |
| 1494 | ret = &module->tpdf[module->tpdf_size]; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1495 | module->tpdf_size++; |
Pavol Vican | 21238f5 | 2016-03-01 12:39:52 +0100 | [diff] [blame] | 1496 | } else { |
| 1497 | switch (parent->nodetype) { |
| 1498 | case LYS_GROUPING: |
| 1499 | 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] | 1500 | ((struct lys_node_grp *)parent)->tpdf_size++; |
| 1501 | break; |
Pavol Vican | 535d50e | 2016-03-01 13:05:33 +0100 | [diff] [blame] | 1502 | case LYS_CONTAINER: |
| 1503 | ret = &((struct lys_node_container *)parent)->tpdf[((struct lys_node_container *)parent)->tpdf_size]; |
| 1504 | ((struct lys_node_container *)parent)->tpdf_size++; |
| 1505 | break; |
Pavol Vican | 09f04b8 | 2016-03-01 14:02:28 +0100 | [diff] [blame] | 1506 | case LYS_LIST: |
| 1507 | ret = &((struct lys_node_list *)parent)->tpdf[((struct lys_node_list *)parent)->tpdf_size]; |
| 1508 | ((struct lys_node_list *)parent)->tpdf_size++; |
| 1509 | break; |
Pavol Vican | 52ed67d | 2016-03-02 17:46:15 +0100 | [diff] [blame] | 1510 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1511 | case LYS_ACTION: |
| 1512 | ret = &((struct lys_node_rpc_action *)parent)->tpdf[((struct lys_node_rpc_action *)parent)->tpdf_size]; |
| 1513 | ((struct lys_node_rpc_action *)parent)->tpdf_size++; |
Pavol Vican | 52ed67d | 2016-03-02 17:46:15 +0100 | [diff] [blame] | 1514 | break; |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 1515 | case LYS_INPUT: |
| 1516 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1517 | ret = &((struct lys_node_inout *)parent)->tpdf[((struct lys_node_inout *)parent)->tpdf_size]; |
| 1518 | ((struct lys_node_inout *)parent)->tpdf_size++; |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 1519 | break; |
Pavol Vican | 41267fd | 2016-03-03 10:47:42 +0100 | [diff] [blame] | 1520 | case LYS_NOTIF: |
| 1521 | ret = &((struct lys_node_notif *)parent)->tpdf[((struct lys_node_notif *)parent)->tpdf_size]; |
| 1522 | ((struct lys_node_notif *)parent)->tpdf_size++; |
| 1523 | break; |
Pavol Vican | d1dbfda | 2016-03-21 10:03:58 +0100 | [diff] [blame] | 1524 | default: |
| 1525 | /* another type of nodetype is error*/ |
| 1526 | LOGINT; |
| 1527 | free(value); |
| 1528 | return NULL; |
Pavol Vican | 21238f5 | 2016-03-01 12:39:52 +0100 | [diff] [blame] | 1529 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1530 | } |
| 1531 | |
Pavol Vican | 7943647 | 2016-04-04 11:22:02 +0200 | [diff] [blame] | 1532 | ret->type.parent = ret; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1533 | ret->name = lydict_insert_zc(module->ctx, value); |
| 1534 | ret->module = module; |
| 1535 | return ret; |
| 1536 | } |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1537 | |
| 1538 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1539 | 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] | 1540 | { |
| 1541 | struct lys_refine *rfn; |
| 1542 | |
| 1543 | rfn = &uses->refine[uses->refine_size]; |
| 1544 | uses->refine_size++; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1545 | rfn->target_name = transform_schema2json(module, value); |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1546 | free(value); |
| 1547 | if (!rfn->target_name) { |
| 1548 | return NULL; |
| 1549 | } |
| 1550 | return rfn; |
| 1551 | } |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1552 | |
| 1553 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1554 | 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] | 1555 | { |
| 1556 | struct lys_node_augment *aug; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1557 | |
| 1558 | if (parent) { |
| 1559 | aug = &((struct lys_node_uses *)parent)->augment[((struct lys_node_uses *)parent)->augment_size]; |
| 1560 | } else { |
| 1561 | aug = &module->augment[module->augment_size]; |
| 1562 | } |
| 1563 | aug->nodetype = LYS_AUGMENT; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1564 | aug->target_name = transform_schema2json(module, value); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1565 | free(value); |
| 1566 | if (!aug->target_name) { |
| 1567 | return NULL; |
| 1568 | } |
| 1569 | aug->parent = parent; |
| 1570 | aug->module = module; |
| 1571 | if (parent) { |
| 1572 | ((struct lys_node_uses *)parent)->augment_size++; |
| 1573 | } else { |
| 1574 | module->augment_size++; |
| 1575 | } |
| 1576 | return aug; |
| 1577 | } |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1578 | |
| 1579 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1580 | yang_read_deviation(struct lys_module *module, char *value) |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1581 | { |
| 1582 | struct lys_node *dev_target = NULL; |
| 1583 | struct lys_deviation *dev; |
| 1584 | struct type_deviation *deviation = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1585 | int rc; |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1586 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1587 | dev = &module->deviation[module->deviation_size]; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1588 | dev->target_name = transform_schema2json(module, value); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1589 | free(value); |
| 1590 | if (!dev->target_name) { |
| 1591 | goto error; |
| 1592 | } |
| 1593 | |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1594 | deviation = calloc(1, sizeof *deviation); |
| 1595 | if (!deviation) { |
| 1596 | LOGMEM; |
| 1597 | goto error; |
| 1598 | } |
| 1599 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1600 | /* resolve target node */ |
| 1601 | rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&dev_target); |
| 1602 | if (rc || !dev_target) { |
Michal Vasko | 75c8daf | 2016-05-19 10:56:39 +0200 | [diff] [blame] | 1603 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1604 | goto error; |
| 1605 | } |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1606 | if (dev_target->module == lys_main_module(module)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1607 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
| 1608 | 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] | 1609 | goto error; |
| 1610 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1611 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1612 | lys_node_module(dev_target)->deviated = 1; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1613 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1614 | /*save pointer to the deviation and deviated target*/ |
| 1615 | deviation->deviation = dev; |
| 1616 | deviation->target = dev_target; |
| 1617 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1618 | return deviation; |
| 1619 | |
| 1620 | error: |
| 1621 | free(deviation); |
| 1622 | lydict_remove(module->ctx, dev->target_name); |
| 1623 | return NULL; |
| 1624 | } |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1625 | |
| 1626 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1627 | yang_read_deviate_unsupported(struct type_deviation *dev) |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1628 | { |
| 1629 | int i; |
| 1630 | |
| 1631 | if (dev->deviation->deviate_size) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1632 | 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] | 1633 | return EXIT_FAILURE; |
| 1634 | } |
| 1635 | dev->deviation->deviate[dev->deviation->deviate_size].mod = LY_DEVIATE_NO; |
| 1636 | |
| 1637 | /* you cannot remove a key leaf */ |
| 1638 | if ((dev->target->nodetype == LYS_LEAF) && dev->target->parent && (dev->target->parent->nodetype == LYS_LIST)) { |
| 1639 | for (i = 0; i < ((struct lys_node_list *)dev->target->parent)->keys_size; ++i) { |
| 1640 | 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] | 1641 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation"); |
| 1642 | 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] | 1643 | return EXIT_FAILURE; |
| 1644 | } |
| 1645 | } |
| 1646 | } |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 1647 | |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1648 | /* unlink and store the original node */ |
Michal Vasko | d921d68 | 2016-05-19 10:56:51 +0200 | [diff] [blame] | 1649 | lys_node_unlink(dev->target); |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1650 | dev->deviation->orig_node = dev->target; |
| 1651 | |
| 1652 | dev->deviation->deviate_size = 1; |
| 1653 | return EXIT_SUCCESS; |
| 1654 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1655 | |
| 1656 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1657 | yang_read_deviate(struct type_deviation *dev, LYS_DEVIATE_TYPE mod) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1658 | { |
| 1659 | struct unres_schema tmp_unres; |
| 1660 | |
| 1661 | dev->deviation->deviate[dev->deviation->deviate_size].mod = mod; |
| 1662 | dev->deviate = &dev->deviation->deviate[dev->deviation->deviate_size]; |
| 1663 | dev->deviation->deviate_size++; |
| 1664 | if (dev->deviation->deviate[0].mod == LY_DEVIATE_NO) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1665 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported"); |
| 1666 | 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] | 1667 | return EXIT_FAILURE; |
| 1668 | } |
| 1669 | |
| 1670 | /* store a shallow copy of the original node */ |
| 1671 | if (!dev->deviation->orig_node) { |
| 1672 | memset(&tmp_unres, 0, sizeof tmp_unres); |
| 1673 | dev->deviation->orig_node = lys_node_dup(dev->target->module, NULL, dev->target, 0, 0, &tmp_unres, 1); |
| 1674 | /* just to be safe */ |
| 1675 | if (tmp_unres.count) { |
| 1676 | LOGINT; |
| 1677 | return EXIT_FAILURE; |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | return EXIT_SUCCESS; |
| 1682 | } |
| 1683 | |
| 1684 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1685 | 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] | 1686 | { |
| 1687 | const char **stritem; |
| 1688 | |
| 1689 | if (dev->deviate->units) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1690 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "units", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1691 | free(value); |
| 1692 | goto error; |
| 1693 | } |
| 1694 | |
| 1695 | /* check target node type */ |
| 1696 | if (dev->target->nodetype == LYS_LEAFLIST) { |
| 1697 | stritem = &((struct lys_node_leaflist *)dev->target)->units; |
| 1698 | } else if (dev->target->nodetype == LYS_LEAF) { |
| 1699 | stritem = &((struct lys_node_leaf *)dev->target)->units; |
| 1700 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1701 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1702 | 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] | 1703 | free(value); |
| 1704 | goto error; |
| 1705 | } |
| 1706 | |
| 1707 | dev->deviate->units = lydict_insert_zc(ctx, value); |
| 1708 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1709 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1710 | /* check values */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 1711 | if (!ly_strequal(*stritem, dev->deviate->units, 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1712 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->units, "units"); |
| 1713 | 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] | 1714 | goto error; |
| 1715 | } |
| 1716 | /* remove current units value of the target */ |
| 1717 | lydict_remove(ctx, *stritem); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1718 | } else { |
| 1719 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1720 | /* check that there is no current value */ |
| 1721 | if (*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1722 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1723 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1724 | goto error; |
| 1725 | } |
| 1726 | } else { /* replace */ |
| 1727 | if (!*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1728 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1729 | 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] | 1730 | goto error; |
| 1731 | } |
| 1732 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1733 | /* remove current units value of the target ... */ |
| 1734 | lydict_remove(ctx, *stritem); |
| 1735 | |
| 1736 | /* ... and replace it with the value specified in deviation */ |
| 1737 | *stritem = lydict_insert(ctx, dev->deviate->units, 0); |
| 1738 | } |
| 1739 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1740 | return EXIT_SUCCESS; |
| 1741 | |
| 1742 | error: |
| 1743 | return EXIT_FAILURE; |
| 1744 | } |
| 1745 | |
| 1746 | int |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1747 | yang_read_deviate_must(struct type_deviation *dev, uint8_t c_must) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1748 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1749 | /* check target node type */ |
| 1750 | switch (dev->target->nodetype) { |
| 1751 | case LYS_LEAF: |
| 1752 | dev->trg_must = &((struct lys_node_leaf *)dev->target)->must; |
| 1753 | dev->trg_must_size = &((struct lys_node_leaf *)dev->target)->must_size; |
| 1754 | break; |
| 1755 | case LYS_CONTAINER: |
| 1756 | dev->trg_must = &((struct lys_node_container *)dev->target)->must; |
| 1757 | dev->trg_must_size = &((struct lys_node_container *)dev->target)->must_size; |
| 1758 | break; |
| 1759 | case LYS_LEAFLIST: |
| 1760 | dev->trg_must = &((struct lys_node_leaflist *)dev->target)->must; |
| 1761 | dev->trg_must_size = &((struct lys_node_leaflist *)dev->target)->must_size; |
| 1762 | break; |
| 1763 | case LYS_LIST: |
| 1764 | dev->trg_must = &((struct lys_node_list *)dev->target)->must; |
| 1765 | dev->trg_must_size = &((struct lys_node_list *)dev->target)->must_size; |
| 1766 | break; |
| 1767 | case LYS_ANYXML: |
| 1768 | dev->trg_must = &((struct lys_node_anyxml *)dev->target)->must; |
| 1769 | dev->trg_must_size = &((struct lys_node_anyxml *)dev->target)->must_size; |
| 1770 | break; |
| 1771 | default: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1772 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must"); |
| 1773 | 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] | 1774 | goto error; |
| 1775 | } |
| 1776 | |
| 1777 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1778 | /* reallocate the must array of the target */ |
| 1779 | dev->deviate->must = ly_realloc(*dev->trg_must, (c_must + *dev->trg_must_size) * sizeof *dev->deviate->must); |
| 1780 | if (!dev->deviate->must) { |
| 1781 | LOGMEM; |
| 1782 | goto error; |
| 1783 | } |
| 1784 | *dev->trg_must = dev->deviate->must; |
| 1785 | dev->deviate->must = &((*dev->trg_must)[*dev->trg_must_size]); |
| 1786 | dev->deviate->must_size = c_must; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1787 | } else { |
| 1788 | /* LY_DEVIATE_DEL */ |
| 1789 | dev->deviate->must = calloc(c_must, sizeof *dev->deviate->must); |
| 1790 | if (!dev->deviate->must) { |
| 1791 | LOGMEM; |
| 1792 | goto error; |
| 1793 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | return EXIT_SUCCESS; |
| 1797 | |
| 1798 | error: |
| 1799 | return EXIT_FAILURE; |
| 1800 | } |
| 1801 | |
| 1802 | int |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1803 | yang_read_deviate_unique(struct type_deviation *dev, uint8_t c_uniq) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1804 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1805 | struct lys_node_list *list; |
| 1806 | |
| 1807 | /* check target node type */ |
| 1808 | if (dev->target->nodetype != LYS_LIST) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1809 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique"); |
| 1810 | 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] | 1811 | goto error; |
| 1812 | } |
| 1813 | |
| 1814 | list = (struct lys_node_list *)dev->target; |
| 1815 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1816 | /* reallocate the unique array of the target */ |
| 1817 | dev->deviate->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *dev->deviate->unique); |
| 1818 | if (!dev->deviate->unique) { |
| 1819 | LOGMEM; |
| 1820 | goto error; |
| 1821 | } |
| 1822 | list->unique = dev->deviate->unique; |
| 1823 | dev->deviate->unique = &list->unique[list->unique_size]; |
| 1824 | dev->deviate->unique_size = c_uniq; |
| 1825 | memset(dev->deviate->unique, 0, c_uniq * sizeof *dev->deviate->unique); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1826 | } else { |
| 1827 | /* LY_DEVIATE_DEL */ |
| 1828 | dev->deviate->unique = calloc(c_uniq, sizeof *dev->deviate->unique); |
| 1829 | if (!dev->deviate->unique) { |
| 1830 | LOGMEM; |
| 1831 | goto error; |
| 1832 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | return EXIT_SUCCESS; |
| 1836 | |
| 1837 | error: |
| 1838 | return EXIT_FAILURE; |
| 1839 | } |
| 1840 | |
| 1841 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1842 | 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] | 1843 | { |
| 1844 | int rc; |
| 1845 | struct lys_node_choice *choice; |
| 1846 | struct lys_node_leaf *leaf; |
| 1847 | struct lys_node *node; |
| 1848 | |
| 1849 | if (dev->deviate->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1850 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "default", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1851 | free(value); |
| 1852 | goto error; |
| 1853 | } |
| 1854 | |
| 1855 | dev->deviate->dflt = lydict_insert_zc(ctx, value); |
| 1856 | |
| 1857 | if (dev->target->nodetype == LYS_CHOICE) { |
| 1858 | choice = (struct lys_node_choice *)dev->target; |
| 1859 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1860 | rc = resolve_choice_default_schema_nodeid(dev->deviate->dflt, choice->child, (const struct lys_node **)&node); |
| 1861 | if (rc || !node) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1862 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1863 | goto error; |
| 1864 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1865 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1866 | if (!choice->dflt || (choice->dflt != 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"); |
| 1868 | 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] | 1869 | goto error; |
| 1870 | } |
| 1871 | choice->dflt = NULL; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1872 | } else { |
| 1873 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1874 | /* check that there is no current value */ |
| 1875 | if (choice->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1876 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1877 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1878 | goto error; |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 1879 | } else if (choice->flags & LYS_MAND_TRUE) { |
| 1880 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "choice"); |
| 1881 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"default\" statement is forbidden on choices with \"mandatory\"."); |
| 1882 | goto error; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1883 | } |
| 1884 | } else { /* replace*/ |
| 1885 | if (!choice->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1886 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1887 | 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] | 1888 | goto error; |
| 1889 | } |
| 1890 | } |
| 1891 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1892 | choice->dflt = node; |
| 1893 | if (!choice->dflt) { |
| 1894 | /* default branch not found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1895 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default"); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1896 | goto error; |
| 1897 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1898 | } |
| 1899 | } else if (dev->target->nodetype == LYS_LEAF) { |
| 1900 | leaf = (struct lys_node_leaf *)dev->target; |
| 1901 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1902 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 1903 | if (!leaf->dflt || !ly_strequal(leaf->dflt, dev->deviate->dflt, 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1904 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default"); |
| 1905 | 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] | 1906 | goto error; |
| 1907 | } |
| 1908 | /* remove value */ |
| 1909 | lydict_remove(ctx, leaf->dflt); |
| 1910 | leaf->dflt = NULL; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1911 | } else { |
| 1912 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1913 | /* check that there is no current value */ |
| 1914 | if (leaf->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1915 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1916 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1917 | goto error; |
Pavol Vican | fdf81c4 | 2016-04-05 15:55:31 +0200 | [diff] [blame] | 1918 | } else if (leaf->flags & LYS_MAND_TRUE) { |
| 1919 | /* RFC 6020, 7.6.4 - default statement must not with mandatory true */ |
| 1920 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "leaf"); |
| 1921 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"default\" statement is forbidden on leaf with \"mandatory\"."); |
| 1922 | goto error; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1923 | } |
| 1924 | } else { /* replace*/ |
| 1925 | if (!leaf->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1926 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1927 | 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] | 1928 | goto error; |
| 1929 | } |
| 1930 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1931 | /* remove value */ |
| 1932 | lydict_remove(ctx, leaf->dflt); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1933 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1934 | /* set new value */ |
| 1935 | leaf->dflt = lydict_insert(ctx, dev->deviate->dflt, 0); |
| 1936 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1937 | } else { |
| 1938 | /* invalid target for default value */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1939 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1940 | 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] | 1941 | goto error; |
| 1942 | } |
| 1943 | |
| 1944 | return EXIT_SUCCESS; |
| 1945 | |
| 1946 | error: |
| 1947 | return EXIT_FAILURE; |
| 1948 | } |
| 1949 | |
| 1950 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1951 | yang_read_deviate_config(struct type_deviation *dev, uint8_t value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1952 | { |
| 1953 | if (dev->deviate->flags & LYS_CONFIG_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1954 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "config", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1955 | goto error; |
| 1956 | } |
| 1957 | |
| 1958 | /* for we deviate from RFC 6020 and allow config property even it is/is not |
| 1959 | * specified in the target explicitly since config property inherits. So we expect |
| 1960 | * that config is specified in every node. But for delete, we check that the value |
| 1961 | * is the same as here in deviation |
| 1962 | */ |
| 1963 | dev->deviate->flags |= value; |
| 1964 | |
| 1965 | /* add and replace are the same in this case */ |
| 1966 | /* remove current config value of the target ... */ |
| 1967 | dev->target->flags &= ~LYS_CONFIG_MASK; |
| 1968 | |
| 1969 | /* ... and replace it with the value specified in deviation */ |
| 1970 | dev->target->flags |= dev->deviate->flags & LYS_CONFIG_MASK; |
| 1971 | |
| 1972 | return EXIT_SUCCESS; |
| 1973 | |
| 1974 | error: |
| 1975 | return EXIT_FAILURE; |
| 1976 | } |
| 1977 | |
| 1978 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1979 | yang_read_deviate_mandatory(struct type_deviation *dev, uint8_t value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1980 | { |
| 1981 | if (dev->deviate->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1982 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "mandatory", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1983 | goto error; |
| 1984 | } |
| 1985 | |
| 1986 | /* check target node type */ |
| 1987 | if (!(dev->target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYXML))) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1988 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1989 | 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] | 1990 | goto error; |
| 1991 | } |
| 1992 | |
| 1993 | dev->deviate->flags |= value; |
| 1994 | |
| 1995 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1996 | /* check that there is no current value */ |
| 1997 | if (dev->target->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1998 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 1999 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2000 | goto error; |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 2001 | } else { |
| 2002 | if (dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) { |
| 2003 | /* RFC 6020, 7.6.4 - default statement must not with mandatory true */ |
| 2004 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf"); |
| 2005 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 2006 | goto error; |
| 2007 | } else if (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt) { |
| 2008 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice"); |
| 2009 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 2010 | goto error; |
| 2011 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2012 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2013 | } else { /* replace */ |
| 2014 | if (!(dev->target->flags & LYS_MAND_MASK)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2015 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2016 | 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] | 2017 | goto error; |
| 2018 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2019 | } |
| 2020 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2021 | /* remove current mandatory value of the target ... */ |
| 2022 | dev->target->flags &= ~LYS_MAND_MASK; |
| 2023 | |
| 2024 | /* ... and replace it with the value specified in deviation */ |
| 2025 | dev->target->flags |= dev->deviate->flags & LYS_MAND_MASK; |
| 2026 | |
| 2027 | return EXIT_SUCCESS; |
| 2028 | |
| 2029 | error: |
| 2030 | return EXIT_FAILURE; |
| 2031 | } |
| 2032 | |
| 2033 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2034 | 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] | 2035 | { |
| 2036 | uint32_t *ui32val; |
| 2037 | |
| 2038 | /* check target node type */ |
| 2039 | if (dev->target->nodetype == LYS_LEAFLIST) { |
| 2040 | if (type) { |
| 2041 | ui32val = &((struct lys_node_leaflist *)dev->target)->max; |
| 2042 | } else { |
| 2043 | ui32val = &((struct lys_node_leaflist *)dev->target)->min; |
| 2044 | } |
| 2045 | } else if (dev->target->nodetype == LYS_LIST) { |
| 2046 | if (type) { |
| 2047 | ui32val = &((struct lys_node_list *)dev->target)->max; |
| 2048 | } else { |
| 2049 | ui32val = &((struct lys_node_list *)dev->target)->min; |
| 2050 | } |
| 2051 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2052 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 2053 | 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] | 2054 | goto error; |
| 2055 | } |
| 2056 | |
| 2057 | if (type) { |
| 2058 | dev->deviate->max = value; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2059 | dev->deviate->max_set = 1; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2060 | } else { |
| 2061 | dev->deviate->min = value; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2062 | dev->deviate->min_set = 1; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 2066 | /* check that there is no current value */ |
| 2067 | if (*ui32val) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2068 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 2069 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2070 | goto error; |
| 2071 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2072 | } else if (dev->deviate->mod == LY_DEVIATE_RPL) { |
| 2073 | /* unfortunately, there is no way to check reliably that there |
| 2074 | * was a value before, it could have been the default */ |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | /* add (already checked) and replace */ |
| 2078 | /* set new value specified in deviation */ |
| 2079 | *ui32val = value; |
| 2080 | |
| 2081 | return EXIT_SUCCESS; |
| 2082 | |
| 2083 | error: |
| 2084 | return EXIT_FAILURE; |
| 2085 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2086 | |
| 2087 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2088 | yang_check_deviate_must(struct ly_ctx *ctx, struct type_deviation *dev) |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2089 | { |
| 2090 | int i; |
| 2091 | |
| 2092 | /* find must to delete, we are ok with just matching conditions */ |
| 2093 | for (i = 0; i < *dev->trg_must_size; i++) { |
| 2094 | if (ly_strequal(dev->deviate->must[dev->deviate->must_size - 1].expr, (*dev->trg_must)[i].expr, 1)) { |
| 2095 | /* we have a match, free the must structure ... */ |
| 2096 | lys_restr_free(ctx, &((*dev->trg_must)[i])); |
| 2097 | /* ... and maintain the array */ |
| 2098 | (*dev->trg_must_size)--; |
| 2099 | if (i != *dev->trg_must_size) { |
| 2100 | (*dev->trg_must)[i].expr = (*dev->trg_must)[*dev->trg_must_size].expr; |
| 2101 | (*dev->trg_must)[i].dsc = (*dev->trg_must)[*dev->trg_must_size].dsc; |
| 2102 | (*dev->trg_must)[i].ref = (*dev->trg_must)[*dev->trg_must_size].ref; |
| 2103 | (*dev->trg_must)[i].eapptag = (*dev->trg_must)[*dev->trg_must_size].eapptag; |
| 2104 | (*dev->trg_must)[i].emsg = (*dev->trg_must)[*dev->trg_must_size].emsg; |
| 2105 | } |
| 2106 | if (!(*dev->trg_must_size)) { |
| 2107 | free(*dev->trg_must); |
| 2108 | *dev->trg_must = NULL; |
| 2109 | } else { |
| 2110 | (*dev->trg_must)[*dev->trg_must_size].expr = NULL; |
| 2111 | (*dev->trg_must)[*dev->trg_must_size].dsc = NULL; |
| 2112 | (*dev->trg_must)[*dev->trg_must_size].ref = NULL; |
| 2113 | (*dev->trg_must)[*dev->trg_must_size].eapptag = NULL; |
| 2114 | (*dev->trg_must)[*dev->trg_must_size].emsg = NULL; |
| 2115 | } |
| 2116 | |
| 2117 | i = -1; /* set match flag */ |
| 2118 | break; |
| 2119 | } |
| 2120 | } |
| 2121 | if (i != -1) { |
| 2122 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2123 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->must[dev->deviate->must_size - 1].expr, "must"); |
| 2124 | 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] | 2125 | return EXIT_FAILURE; |
| 2126 | } |
| 2127 | |
| 2128 | return EXIT_SUCCESS; |
| 2129 | } |
| 2130 | |
| 2131 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2132 | 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] | 2133 | { |
| 2134 | struct lys_node_list *list; |
| 2135 | int i, j; |
| 2136 | |
| 2137 | list = (struct lys_node_list *)dev->target; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2138 | 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] | 2139 | dev->deviate->unique_size++; |
| 2140 | goto error; |
| 2141 | } |
| 2142 | |
| 2143 | /* find unique structures to delete */ |
| 2144 | for (i = 0; i < list->unique_size; i++) { |
| 2145 | if (list->unique[i].expr_size != dev->deviate->unique[dev->deviate->unique_size].expr_size) { |
| 2146 | continue; |
| 2147 | } |
| 2148 | |
| 2149 | for (j = 0; j < dev->deviate->unique[dev->deviate->unique_size].expr_size; j++) { |
| 2150 | if (!ly_strequal(list->unique[i].expr[j], dev->deviate->unique[dev->deviate->unique_size].expr[j], 1)) { |
| 2151 | break; |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | if (j == dev->deviate->unique[dev->deviate->unique_size].expr_size) { |
| 2156 | /* we have a match, free the unique structure ... */ |
| 2157 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2158 | lydict_remove(module->ctx, list->unique[i].expr[j]); |
| 2159 | } |
| 2160 | free(list->unique[i].expr); |
| 2161 | /* ... and maintain the array */ |
| 2162 | list->unique_size--; |
| 2163 | if (i != list->unique_size) { |
| 2164 | list->unique[i].expr_size = list->unique[list->unique_size].expr_size; |
| 2165 | list->unique[i].expr = list->unique[list->unique_size].expr; |
| 2166 | } |
| 2167 | |
| 2168 | if (!list->unique_size) { |
| 2169 | free(list->unique); |
| 2170 | list->unique = NULL; |
| 2171 | } else { |
| 2172 | list->unique[list->unique_size].expr_size = 0; |
| 2173 | list->unique[list->unique_size].expr = NULL; |
| 2174 | } |
| 2175 | |
| 2176 | i = -1; /* set match flag */ |
| 2177 | break; |
| 2178 | } |
| 2179 | } |
| 2180 | dev->deviate->unique_size++; |
| 2181 | |
| 2182 | if (i != -1) { |
| 2183 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2184 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique"); |
| 2185 | 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] | 2186 | goto error; |
| 2187 | } |
| 2188 | |
| 2189 | free(value); |
| 2190 | return EXIT_SUCCESS; |
| 2191 | |
| 2192 | error: |
| 2193 | free(value); |
| 2194 | return EXIT_FAILURE; |
| 2195 | } |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2196 | |
| 2197 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2198 | 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] | 2199 | { |
| 2200 | int i, rc; |
| 2201 | |
| 2202 | if (dev->target->nodetype == LYS_LEAF) { |
| 2203 | for(i = 0; i < dev->deviation->deviate_size; ++i) { |
| 2204 | if (dev->deviation->deviate[i].mod != LY_DEVIATE_DEL) { |
| 2205 | if (dev->deviation->deviate[i].dflt || dev->deviation->deviate[i].type) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2206 | 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] | 2207 | if (rc == -1) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2208 | 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] | 2209 | return EXIT_FAILURE; |
| 2210 | } |
| 2211 | break; |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | } |
| 2216 | return EXIT_SUCCESS; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | int |
| 2220 | 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] | 2221 | struct lys_include *inc, struct unres_schema *unres) |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2222 | { |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2223 | struct lys_module *trg; |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2224 | const char *str; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2225 | int rc; |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2226 | int ret = 0; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2227 | |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2228 | str = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2229 | trg = (submodule) ? (struct lys_module *)submodule : module; |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2230 | rc = lyp_check_include(module, submodule, str, inc, unres); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2231 | if (!rc) { |
| 2232 | /* success, copy the filled data into the final array */ |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2233 | memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc); |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 2234 | trg->inc_size++; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2235 | } else if (rc == -1) { |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2236 | ret = -1; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2237 | } |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2238 | |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2239 | lydict_remove(module->ctx, str); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2240 | return ret; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2241 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2242 | |
| 2243 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2244 | 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] | 2245 | { |
| 2246 | char *prefix; |
| 2247 | char *identif; |
| 2248 | const char *ns = NULL; |
| 2249 | int i; |
| 2250 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2251 | /* check to the same pointer */ |
| 2252 | if (data_node != actual) { |
| 2253 | return EXIT_SUCCESS; |
| 2254 | } |
| 2255 | |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2256 | prefix = strdup(value); |
| 2257 | if (!prefix) { |
| 2258 | LOGMEM; |
| 2259 | goto error; |
| 2260 | } |
| 2261 | /* find prefix anf identificator*/ |
| 2262 | identif = strchr(prefix, ':'); |
| 2263 | *identif = '\0'; |
| 2264 | identif++; |
| 2265 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2266 | for(i = 0; i < module->imp_size; ++i) { |
| 2267 | if (!strcmp(module->imp[i].prefix, prefix)) { |
| 2268 | ns = module->imp[i].module->ns; |
| 2269 | break; |
| 2270 | } |
| 2271 | } |
Pavol Vican | 1ff1b7b | 2016-04-07 09:51:49 +0200 | [diff] [blame] | 2272 | if (!ns && !strcmp(module->prefix, prefix)) { |
| 2273 | ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns; |
| 2274 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2275 | if (ns && !strcmp(ns, LY_NSNACM)) { |
| 2276 | if (!strcmp(identif, "default-deny-write")) { |
| 2277 | data_node->nacm |= LYS_NACM_DENYW; |
| 2278 | } else if (!strcmp(identif, "default-deny-all")) { |
| 2279 | data_node->nacm |= LYS_NACM_DENYA; |
| 2280 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2281 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif); |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2282 | goto error; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2283 | } |
| 2284 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2285 | free(prefix); |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2286 | return EXIT_SUCCESS; |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2287 | |
| 2288 | error: |
| 2289 | free(prefix); |
| 2290 | return EXIT_FAILURE; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | void |
| 2294 | nacm_inherit(struct lys_module *module) |
| 2295 | { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2296 | struct lys_node *next, *elem, *tmp_node, *tmp_child; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2297 | |
| 2298 | LY_TREE_DFS_BEGIN(module->data, next, elem) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2299 | tmp_node = NULL; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2300 | if (elem->parent) { |
| 2301 | switch (elem->nodetype) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2302 | case LYS_GROUPING: |
| 2303 | /* extension nacm not inherited*/ |
| 2304 | break; |
| 2305 | case LYS_CHOICE: |
| 2306 | case LYS_ANYXML: |
| 2307 | case LYS_USES: |
| 2308 | if (elem->parent->nodetype != LYS_GROUPING) { |
| 2309 | elem->nacm |= elem->parent->nacm; |
| 2310 | } |
| 2311 | break; |
| 2312 | case LYS_CONTAINER: |
| 2313 | case LYS_LIST: |
| 2314 | case LYS_CASE: |
| 2315 | case LYS_NOTIF: |
| 2316 | case LYS_RPC: |
| 2317 | case LYS_INPUT: |
| 2318 | case LYS_OUTPUT: |
| 2319 | case LYS_AUGMENT: |
| 2320 | elem->nacm |= elem->parent->nacm; |
| 2321 | break; |
| 2322 | case LYS_LEAF: |
| 2323 | case LYS_LEAFLIST: |
| 2324 | tmp_node = elem; |
| 2325 | tmp_child = elem->child; |
| 2326 | elem->child = NULL; |
| 2327 | default: |
| 2328 | break; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2329 | } |
| 2330 | } |
| 2331 | LY_TREE_DFS_END(module->data, next, elem); |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2332 | if (tmp_node) { |
| 2333 | tmp_node->child = tmp_child; |
| 2334 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2335 | } |
| 2336 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2337 | |
| 2338 | void |
| 2339 | store_flags(struct lys_node *node, uint8_t flags, int config_inherit) |
| 2340 | { |
| 2341 | node->flags |= flags; |
| 2342 | if (!(node->flags & LYS_CONFIG_MASK) && config_inherit) { |
| 2343 | /* get config flag from parent */ |
| 2344 | if (node->parent) { |
| 2345 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 2346 | } else { |
| 2347 | /* default config is true */ |
| 2348 | node->flags |= LYS_CONFIG_W; |
| 2349 | } |
| 2350 | } |
| 2351 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2352 | |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2353 | static int |
| 2354 | yang_parse(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data, |
| 2355 | unsigned int size, struct lys_array_size *size_arrays, int type_read) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2356 | { |
| 2357 | YY_BUFFER_STATE bp; |
Pavol Vican | 802af1e | 2016-03-23 20:42:26 +0100 | [diff] [blame] | 2358 | yyscan_t scanner = NULL; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2359 | int ret = EXIT_SUCCESS; |
| 2360 | |
| 2361 | yylex_init(&scanner); |
| 2362 | bp = yy_scan_buffer((char *)data, size, scanner); |
| 2363 | yy_switch_to_buffer(bp, scanner); |
| 2364 | if (yyparse(scanner, module, submodule, unres, size_arrays, type_read)) { |
| 2365 | ret = EXIT_FAILURE; |
| 2366 | } |
| 2367 | yy_delete_buffer(bp, scanner); |
| 2368 | yylex_destroy(scanner); |
| 2369 | return ret; |
| 2370 | } |
| 2371 | |
| 2372 | int |
| 2373 | yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data, unsigned int size_data) |
| 2374 | { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2375 | struct lys_array_size *size_arrays=NULL; |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2376 | unsigned int size; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2377 | int ret; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2378 | |
| 2379 | size_arrays = calloc(1, sizeof *size_arrays); |
| 2380 | if (!size_arrays) { |
| 2381 | LOGMEM; |
Pavol Vican | f7994fb | 2016-04-05 21:55:53 +0200 | [diff] [blame] | 2382 | return EXIT_FAILURE; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2383 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2384 | size = (size_data) ? size_data : strlen(data) + 2; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2385 | ret = yang_parse(module, submodule, unres, data, size, size_arrays, LY_READ_ONLY_SIZE); |
| 2386 | if (!ret) { |
| 2387 | 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] | 2388 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2389 | free(size_arrays->node); |
| 2390 | free(size_arrays); |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2391 | return ret; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | struct lys_module * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2395 | 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] | 2396 | { |
| 2397 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2398 | struct lys_module *tmp_module, *module = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2399 | struct unres_schema *unres = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2400 | int i; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2401 | |
| 2402 | unres = calloc(1, sizeof *unres); |
| 2403 | if (!unres) { |
| 2404 | LOGMEM; |
| 2405 | goto error; |
| 2406 | } |
| 2407 | |
| 2408 | module = calloc(1, sizeof *module); |
| 2409 | if (!module) { |
| 2410 | LOGMEM; |
Pavol Vican | f7994fb | 2016-04-05 21:55:53 +0200 | [diff] [blame] | 2411 | goto error; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | /* initiale module */ |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 2415 | module->ctx = ctx; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2416 | module->type = 0; |
| 2417 | module->implemented = (implement ? 1 : 0); |
| 2418 | |
| 2419 | if (yang_parse_mem(module, NULL, unres, data, size)) { |
| 2420 | goto error; |
| 2421 | } |
| 2422 | |
| 2423 | if (module && unres->count && resolve_unres_schema(module, unres)) { |
| 2424 | goto error; |
| 2425 | } |
| 2426 | |
| 2427 | if (revision) { |
| 2428 | /* check revision of the parsed model */ |
| 2429 | if (!module->rev_size || strcmp(revision, module->rev[0].date)) { |
| 2430 | LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", |
| 2431 | module->name, module->rev[0].date, revision); |
| 2432 | goto error; |
| 2433 | } |
| 2434 | } |
| 2435 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2436 | tmp_module = module; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2437 | if (lyp_ctx_add_module(&module)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2438 | goto error; |
| 2439 | } |
| 2440 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2441 | if (module == tmp_module) { |
| 2442 | nacm_inherit(module); |
| 2443 | } |
| 2444 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2445 | if (module->augment_size || module->deviation_size) { |
Michal Vasko | 5cec331 | 2016-05-04 08:59:41 +0200 | [diff] [blame] | 2446 | if (!module->implemented) { |
| 2447 | LOGVRB("Module \"%s\" includes augments or deviations, changing conformance to \"implement\".", module->name); |
| 2448 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2449 | if (lys_module_set_implement(module)) { |
| 2450 | goto error; |
| 2451 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2452 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2453 | if (lys_sub_module_set_dev_aug_target_implement(module)) { |
| 2454 | goto error; |
| 2455 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2456 | for (i = 0; i < module->inc_size; ++i) { |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 2457 | if (!module->inc[i].submodule) { |
| 2458 | continue; |
| 2459 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2460 | if (lys_sub_module_set_dev_aug_target_implement((struct lys_module *)module->inc[i].submodule)) { |
| 2461 | goto error; |
| 2462 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2463 | } |
| 2464 | } |
| 2465 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2466 | unres_schema_free(NULL, &unres); |
| 2467 | LOGVRB("Module \"%s\" successfully parsed.", module->name); |
| 2468 | return module; |
| 2469 | |
| 2470 | error: |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2471 | /* cleanup */ |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2472 | unres_schema_free(module, &unres); |
| 2473 | if (!module || !module->name) { |
| 2474 | free(module); |
| 2475 | LOGERR(ly_errno, "Module parsing failed."); |
| 2476 | return NULL; |
| 2477 | } |
| 2478 | |
| 2479 | LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2480 | |
| 2481 | lys_sub_module_remove_devs_augs(module); |
| 2482 | lys_free(module, NULL, 1); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2483 | return NULL; |
| 2484 | } |
| 2485 | |
| 2486 | struct lys_submodule * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2487 | 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] | 2488 | { |
| 2489 | struct lys_submodule *submodule; |
| 2490 | |
| 2491 | submodule = calloc(1, sizeof *submodule); |
| 2492 | if (!submodule) { |
| 2493 | LOGMEM; |
| 2494 | goto error; |
| 2495 | } |
| 2496 | |
| 2497 | submodule->ctx = module->ctx; |
| 2498 | submodule->type = 1; |
| 2499 | submodule->belongsto = module; |
| 2500 | |
| 2501 | if (yang_parse_mem(module, submodule, unres, data, size)) { |
| 2502 | goto error; |
| 2503 | } |
| 2504 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2505 | LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2506 | return submodule; |
| 2507 | |
| 2508 | error: |
| 2509 | /* cleanup */ |
| 2510 | unres_schema_free((struct lys_module *)submodule, &unres); |
| 2511 | |
| 2512 | if (!submodule || !submodule->name) { |
| 2513 | free(submodule); |
| 2514 | LOGERR(ly_errno, "Submodule parsing failed."); |
| 2515 | return NULL; |
| 2516 | } |
| 2517 | |
| 2518 | LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name); |
| 2519 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2520 | lys_sub_module_remove_devs_augs((struct lys_module *)submodule); |
| 2521 | lys_submodule_module_data_free(submodule); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2522 | lys_submodule_free(submodule, NULL); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2523 | return NULL; |
| 2524 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2525 | |
| 2526 | static int |
| 2527 | count_substring(const char *str, char c) |
| 2528 | { |
| 2529 | const char *tmp = str; |
| 2530 | int count = 0; |
| 2531 | |
| 2532 | while ((tmp = strchr(tmp, c))) { |
| 2533 | tmp++; |
| 2534 | count++; |
| 2535 | } |
| 2536 | return count; |
| 2537 | } |
| 2538 | |
| 2539 | static int |
| 2540 | read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output) |
| 2541 | { |
| 2542 | int k = 0, j; |
| 2543 | |
| 2544 | while (in_index < size) { |
| 2545 | if (input[in_index] == ' ') { |
| 2546 | k++; |
| 2547 | } else if (input[in_index] == '\t') { |
| 2548 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2549 | k += 8; |
| 2550 | } else { |
| 2551 | break; |
| 2552 | } |
| 2553 | ++in_index; |
| 2554 | if (k >= indent) { |
| 2555 | for (j = k - indent; j > 0; --j) { |
| 2556 | output[*out_index] = ' '; |
| 2557 | ++(*out_index); |
| 2558 | } |
| 2559 | break; |
| 2560 | } |
| 2561 | } |
| 2562 | return in_index; |
| 2563 | } |
| 2564 | |
| 2565 | char * |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2566 | yang_read_string(const char *input, int size, int indent, int version) |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2567 | { |
| 2568 | int space, count; |
| 2569 | int in_index, out_index; |
| 2570 | char *value; |
| 2571 | char *retval = NULL; |
| 2572 | |
| 2573 | value = malloc(size + 1); |
| 2574 | if (!value) { |
| 2575 | LOGMEM; |
| 2576 | return NULL; |
| 2577 | } |
| 2578 | /* replace special character in escape sequence */ |
| 2579 | in_index = out_index = 0; |
| 2580 | while (in_index < size) { |
| 2581 | if (input[in_index] == '\\') { |
| 2582 | if (input[in_index + 1] == 'n') { |
| 2583 | value[out_index] = '\n'; |
| 2584 | ++in_index; |
| 2585 | } else if (input[in_index + 1] == 't') { |
| 2586 | value[out_index] = '\t'; |
| 2587 | ++in_index; |
| 2588 | } else if (input[in_index + 1] == '\\') { |
| 2589 | value[out_index] = '\\'; |
| 2590 | ++in_index; |
| 2591 | } else if ((in_index + 1) != size && input[in_index + 1] == '"') { |
| 2592 | value[out_index] = '"'; |
| 2593 | ++in_index; |
| 2594 | } else { |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2595 | if (version < 2) { |
| 2596 | value[out_index] = input[in_index]; |
| 2597 | } else { |
| 2598 | /* YANG 1.1 backslash must not be followed by any other character */ |
| 2599 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input); |
| 2600 | goto error; |
| 2601 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2602 | } |
| 2603 | } else { |
| 2604 | value[out_index] = input[in_index]; |
| 2605 | } |
| 2606 | ++in_index; |
| 2607 | ++out_index; |
| 2608 | } |
| 2609 | value[out_index] = '\0'; |
| 2610 | size = out_index; |
| 2611 | count = count_substring(value, '\t'); |
| 2612 | |
| 2613 | /* extend size of string due to replacing character '\t' with 8 spaces */ |
| 2614 | retval = malloc(size + 1 + 7 * count); |
| 2615 | if (!retval) { |
| 2616 | LOGMEM; |
| 2617 | goto error; |
| 2618 | } |
| 2619 | in_index = out_index = space = 0; |
| 2620 | while (in_index < size) { |
| 2621 | if (value[in_index] == '\n') { |
| 2622 | out_index -= space; |
| 2623 | space = 0; |
| 2624 | retval[out_index] = '\n'; |
| 2625 | ++out_index; |
| 2626 | ++in_index; |
| 2627 | in_index = read_indent(value, indent, size, in_index, &out_index, retval); |
| 2628 | continue; |
| 2629 | } else { |
| 2630 | space = (value[in_index] == ' ' || value[in_index] == '\t') ? space + 1 : 0; |
| 2631 | retval[out_index] = value[in_index]; |
| 2632 | ++out_index; |
| 2633 | } |
| 2634 | ++in_index; |
| 2635 | } |
| 2636 | retval[out_index] = '\0'; |
| 2637 | if (out_index != size) { |
| 2638 | retval = ly_realloc(retval, out_index + 1); |
| 2639 | } |
| 2640 | free(value); |
| 2641 | return retval; |
| 2642 | error: |
| 2643 | free(value); |
| 2644 | return NULL; |
| 2645 | } |