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