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