Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file yin.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief YIN parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 22 | #include <assert.h> |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 23 | #include <ctype.h> |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <limits.h> |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 29 | #include <stddef.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 30 | |
Radek Krejci | efdd0ce | 2015-05-26 16:48:29 +0200 | [diff] [blame] | 31 | #include "../libyang.h" |
| 32 | #include "../common.h" |
| 33 | #include "../context.h" |
| 34 | #include "../dict.h" |
| 35 | #include "../parser.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 36 | |
Radek Krejci | efdd0ce | 2015-05-26 16:48:29 +0200 | [diff] [blame] | 37 | #include "../tree_internal.h" |
| 38 | #include "../xml.h" |
| 39 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 40 | enum LY_IDENT { |
| 41 | LY_IDENT_FEATURE, |
| 42 | LY_IDENT_IDENTITY, |
| 43 | LY_IDENT_TYPE, |
| 44 | LY_IDENT_NODE, |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 45 | LY_IDENT_NAME, |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 46 | LY_IDENT_PREFIX |
| 47 | }; |
| 48 | |
Radek Krejci | efdd0ce | 2015-05-26 16:48:29 +0200 | [diff] [blame] | 49 | #define LY_NSYIN "urn:ietf:params:xml:ns:yang:yin:1" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 50 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 51 | #define GETVAL(value, node, arg) \ |
| 52 | value = lyxml_get_attr(node, arg, NULL); \ |
| 53 | if (!value) { \ |
| 54 | LOGVAL(VE_MISSARG, LOGLINE(node), arg, node->name); \ |
| 55 | goto error; \ |
| 56 | } |
| 57 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 58 | #define OPT_IDENT 0x01 |
| 59 | #define OPT_CONFIG 0x02 |
| 60 | #define OPT_MODULE 0x04 |
| 61 | #define OPT_INHERIT 0x08 |
| 62 | static int read_yin_common(struct ly_module *, struct ly_mnode *, struct ly_mnode *, struct lyxml_elem *, int); |
| 63 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 64 | static struct ly_mnode *read_yin_choice(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
| 65 | static struct ly_mnode *read_yin_case(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 66 | static struct ly_mnode *read_yin_anyxml(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 67 | static struct ly_mnode *read_yin_container(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 68 | static struct ly_mnode *read_yin_leaf(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
| 69 | static struct ly_mnode *read_yin_leaflist(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 70 | static struct ly_mnode *read_yin_list(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 71 | static struct ly_mnode *read_yin_uses(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 72 | static struct ly_mnode *read_yin_grouping(struct ly_module *, struct ly_mnode *, struct lyxml_elem *, int); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 73 | |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 74 | static int dup_typedef_check(const char* type, struct ly_tpdf *tpdf, int size) |
| 75 | { |
| 76 | int i; |
| 77 | |
| 78 | for (i = 0; i < size; i++) { |
| 79 | if (!strcmp(type, tpdf[i].name)) { |
| 80 | /* name collision */ |
| 81 | return EXIT_FAILURE; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return EXIT_SUCCESS; |
| 86 | } |
| 87 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 88 | static int dup_prefix_check(const char* prefix, struct ly_module *module) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 89 | { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 90 | int i; |
| 91 | |
| 92 | if (!module->type && module->prefix && !strcmp(module->prefix, prefix)) { |
| 93 | return EXIT_FAILURE; |
| 94 | } |
| 95 | for (i = 0; i < module->imp_size; i++) { |
| 96 | if (!strcmp(module->imp[i].prefix, prefix)) { |
| 97 | return EXIT_FAILURE; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return EXIT_SUCCESS; |
| 102 | } |
| 103 | |
| 104 | static int check_identifier(const char *id, enum LY_IDENT type, unsigned int line, |
| 105 | struct ly_module *module, struct ly_mnode *parent) |
| 106 | { |
| 107 | int i; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 108 | int size; |
| 109 | struct ly_tpdf *tpdf; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 110 | struct ly_mnode *mnode; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 111 | |
| 112 | assert(id); |
| 113 | |
| 114 | /* check id syntax */ |
| 115 | if (!(id[0] >= 'A' && id[0] <= 'Z') && !(id[0] >= 'a' && id[0] <= 'z') && id[0] != '_') { |
| 116 | LOGVAL(VE_INID, line, id, "invalid start character"); |
| 117 | return EXIT_FAILURE; |
| 118 | } |
| 119 | for (i = 1; id[i]; i++) { |
| 120 | if (!(id[i] >= 'A' && id[i] <= 'Z') && !(id[i] >= 'a' && id[i] <= 'z') |
| 121 | && !(id[i] >= '0' && id[i] <= '9') && id[i] != '_' && id[i] != '-' && id[i] != '.') { |
Radek Krejci | d47904a | 2015-06-03 11:33:06 +0200 | [diff] [blame] | 122 | LOGVAL(VE_INID, line, id, "invalid character"); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 123 | return EXIT_FAILURE; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (i > 64) { |
| 128 | LOGWRN("Identifier \"%s\" is long, you should use something shorter.", id); |
| 129 | } |
| 130 | |
| 131 | switch(type) { |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 132 | case LY_IDENT_NAME: |
| 133 | /* check uniqueness of the node within its siblings */ |
| 134 | if (!parent) { |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | LY_TREE_FOR(parent->child, mnode) { |
| 139 | if (mnode->name == id) { |
| 140 | LOGVAL(VE_INID, line, id, "name duplication"); |
| 141 | return EXIT_FAILURE; |
| 142 | } |
| 143 | } |
| 144 | break; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 145 | case LY_IDENT_TYPE: |
| 146 | assert(module); |
| 147 | |
| 148 | /* check collision with the built-in types */ |
| 149 | if (!strcmp(id, "binary") || !strcmp(id,"bits") || |
| 150 | !strcmp(id, "boolean") || !strcmp(id, "decimal64") || |
| 151 | !strcmp(id, "empty") || !strcmp(id, "enumeration") || |
| 152 | !strcmp(id, "identityref") || !strcmp(id, "instance-identifier") || |
| 153 | !strcmp(id, "int8") || !strcmp(id, "int16") || |
| 154 | !strcmp(id, "int32") || !strcmp(id, "int64") || |
| 155 | !strcmp(id, "leafref") || !strcmp(id, "string") || |
| 156 | !strcmp(id, "uint8") || !strcmp(id, "uint16") || |
| 157 | !strcmp(id, "uint32") || !strcmp(id, "uint64") || |
| 158 | !strcmp(id, "union")) { |
| 159 | LOGVAL(VE_SPEC, line, "Typedef name duplicates built-in type."); |
| 160 | return EXIT_FAILURE; |
| 161 | } |
| 162 | |
| 163 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 164 | for ( ; parent; parent = parent->parent) { |
| 165 | switch(parent->nodetype) { |
| 166 | case LY_NODE_CONTAINER: |
| 167 | size = ((struct ly_mnode_container *)parent)->tpdf_size; |
| 168 | tpdf = ((struct ly_mnode_container *)parent)->tpdf; |
| 169 | break; |
| 170 | case LY_NODE_LIST: |
| 171 | size = ((struct ly_mnode_list *)parent)->tpdf_size; |
| 172 | tpdf = ((struct ly_mnode_list *)parent)->tpdf; |
| 173 | break; |
| 174 | case LY_NODE_GROUPING: |
| 175 | size = ((struct ly_mnode_grp *)parent)->tpdf_size; |
| 176 | tpdf = ((struct ly_mnode_grp *)parent)->tpdf; |
| 177 | break; |
| 178 | default: |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | if (dup_typedef_check(id, tpdf, size)) { |
| 183 | LOGVAL(VE_DUPID, line, "typedef", id); |
| 184 | return EXIT_FAILURE; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* check top-level names */ |
| 189 | if (dup_typedef_check(id, module->tpdf, module->tpdf_size)) { |
| 190 | LOGVAL(VE_DUPID, line, "typedef", id); |
| 191 | return EXIT_FAILURE; |
| 192 | } |
| 193 | |
| 194 | /* check submodule's top-level names */ |
| 195 | for (i = 0; i < module->inc_size; i++) { |
| 196 | if (dup_typedef_check(id, module->inc[i].submodule->tpdf, module->inc[i].submodule->tpdf_size)) { |
| 197 | LOGVAL(VE_DUPID, line, "typedef", id); |
| 198 | return EXIT_FAILURE; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* check top-level names in the main module */ |
| 203 | if (module->type) { |
| 204 | if (dup_typedef_check(id, ((struct ly_submodule *)module)->belongsto->tpdf, |
| 205 | ((struct ly_submodule *)module)->belongsto->tpdf_size)) { |
| 206 | LOGVAL(VE_DUPID, line, "typedef", id); |
| 207 | return EXIT_FAILURE; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | break; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 212 | case LY_IDENT_PREFIX: |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 213 | assert(module); |
| 214 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 215 | if (module->type) { |
| 216 | /* go to the main module */ |
| 217 | module = ((struct ly_submodule *)module)->belongsto; |
| 218 | } |
| 219 | |
| 220 | /* check the main module itself */ |
| 221 | if (dup_prefix_check(id, module)) { |
| 222 | LOGVAL(VE_DUPID, line, "prefix", id); |
| 223 | return EXIT_FAILURE; |
| 224 | } |
| 225 | |
| 226 | /* and all its submodules */ |
| 227 | for (i = 0; i < module->inc_size; i++) { |
| 228 | if (dup_prefix_check(id, (struct ly_module *)module->inc[i].submodule)) { |
| 229 | LOGVAL(VE_DUPID, line, "prefix", id); |
| 230 | return EXIT_FAILURE; |
| 231 | } |
| 232 | } |
| 233 | break; |
| 234 | default: |
| 235 | /* no check required */ |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | return EXIT_SUCCESS; |
| 240 | } |
| 241 | |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 242 | static int check_key(struct ly_mnode_leaf *key, uint8_t flags, struct ly_mnode_leaf **list, int index, unsigned int line, const char *name, int len) |
| 243 | { |
| 244 | char *dup = NULL; |
| 245 | int j; |
| 246 | |
| 247 | /* existence */ |
| 248 | if (!key) { |
| 249 | if (name[len] != '\0') { |
| 250 | dup = strdup(name); |
| 251 | dup[len] = '\0'; |
| 252 | name = dup; |
| 253 | } |
| 254 | LOGVAL(VE_KEY_MISS, line, name); |
| 255 | free(dup); |
| 256 | return EXIT_FAILURE; |
| 257 | } |
| 258 | |
| 259 | /* uniqueness */ |
| 260 | for (j = index - 1; j >= 0; j--) { |
| 261 | if (list[index] == list[j]) { |
| 262 | LOGVAL(VE_KEY_DUP, line, key->name); |
| 263 | return EXIT_FAILURE; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* key is a leaf */ |
| 268 | if (key->nodetype != LY_NODE_LEAF) { |
| 269 | LOGVAL(VE_KEY_NLEAF, line, key->name); |
| 270 | return EXIT_FAILURE; |
| 271 | } |
| 272 | |
| 273 | /* type of the leaf is not built-in empty */ |
| 274 | if (key->type.base == LY_TYPE_EMPTY) { |
| 275 | LOGVAL(VE_KEY_TYPE, line, key->name); |
| 276 | return EXIT_FAILURE; |
| 277 | } |
| 278 | |
| 279 | /* config attribute is the same as of the list */ |
| 280 | if ((flags & LY_NODE_CONFIG_MASK) != (key->flags & LY_NODE_CONFIG_MASK)) { |
| 281 | LOGVAL(VE_KEY_CONFIG, line, key->name); |
| 282 | return EXIT_FAILURE; |
| 283 | } |
| 284 | |
| 285 | return EXIT_SUCCESS; |
| 286 | } |
| 287 | |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 288 | static int check_default(struct ly_type *type, const char* value) |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 289 | { |
| 290 | /* TODO - RFC 6020, sec. 7.3.4 */ |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 291 | (void)type; |
| 292 | (void)value; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 293 | return EXIT_SUCCESS; |
| 294 | } |
| 295 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 296 | static int check_date(const char* date, unsigned int line) |
| 297 | { |
| 298 | int i; |
| 299 | |
| 300 | assert(date); |
| 301 | |
| 302 | if (strlen(date) != LY_REV_SIZE - 1) { |
| 303 | goto error; |
| 304 | } |
| 305 | |
| 306 | for (i = 0; i < LY_REV_SIZE - 1; i++) { |
| 307 | if (i == 4 || i == 7) { |
| 308 | if (date[i] != '-') { |
| 309 | goto error; |
| 310 | } |
| 311 | } else if (!isdigit(date[i])) { |
| 312 | goto error; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return EXIT_SUCCESS; |
| 317 | |
| 318 | error: |
| 319 | |
| 320 | LOGVAL(VE_INDATE, line, date); |
| 321 | return EXIT_FAILURE; |
| 322 | } |
| 323 | |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 324 | static const char *read_yin_subnode(struct ly_ctx *ctx, struct lyxml_elem *node, const char *name) |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 325 | { |
| 326 | int len; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 327 | |
| 328 | /* there should be <text> child */ |
| 329 | if (!node->child || !node->child->name |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 330 | || strcmp(node->child->name, name)) { |
| 331 | LOGWRN("Expected \"%s\" element in \"%s\" element.", name, node->name); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 332 | } else if (node->child->content) { |
| 333 | len = strlen(node->child->content); |
| 334 | return lydict_insert(ctx, node->child->content, len); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 335 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 336 | |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 337 | LOGVAL(VE_INARG, LOGLINE(node), name, node->name); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | static struct ly_tpdf *find_superior_type(const char *name, |
| 342 | struct ly_module *module, |
| 343 | struct ly_mnode *parent) |
| 344 | { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 345 | int i, j, found = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 346 | int prefix_len = 0; |
| 347 | const char *qname; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 348 | struct ly_tpdf *tpdf; |
| 349 | int tpdf_size; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 350 | |
| 351 | qname = strchr(name, ':'); |
| 352 | |
| 353 | if (!qname) { |
| 354 | /* no prefix, try built-in types */ |
| 355 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 356 | if (!strcmp(ly_types[i].def->name, name)) { |
| 357 | return ly_types[i].def; |
| 358 | } |
| 359 | } |
| 360 | qname = name; |
| 361 | } else { |
| 362 | /* set qname to correct position after colon */ |
| 363 | prefix_len = qname - name; |
| 364 | qname++; |
| 365 | |
| 366 | if (!strncmp(name, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 367 | /* prefix refers to the current module, ignore it */ |
| 368 | prefix_len = 0; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (!prefix_len && parent) { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 373 | /* search in local typedefs */ |
| 374 | while (parent) { |
| 375 | switch (parent->nodetype) { |
| 376 | case LY_NODE_CONTAINER: |
| 377 | tpdf_size = ((struct ly_mnode_container *)parent)->tpdf_size; |
| 378 | tpdf = ((struct ly_mnode_container *)parent)->tpdf; |
| 379 | break; |
| 380 | case LY_NODE_LIST: |
| 381 | tpdf_size = ((struct ly_mnode_list *)parent)->tpdf_size; |
| 382 | tpdf = ((struct ly_mnode_list *)parent)->tpdf; |
| 383 | break; |
| 384 | case LY_NODE_GROUPING: |
| 385 | tpdf_size = ((struct ly_mnode_grp *)parent)->tpdf_size; |
| 386 | tpdf = ((struct ly_mnode_grp *)parent)->tpdf; |
| 387 | break; |
| 388 | default: |
| 389 | parent = parent->parent; |
| 390 | continue; |
| 391 | } |
| 392 | |
| 393 | for (i = 0; i < tpdf_size; i++) { |
| 394 | if (!strcmp(tpdf[i].name, qname)) { |
| 395 | return &tpdf[i]; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | parent = parent->parent; |
| 400 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 401 | } else if (prefix_len) { |
| 402 | /* get module where to search */ |
| 403 | for (i = 0; i < module->imp_size; i++) { |
| 404 | if (!strncmp(module->imp[i].prefix, name, prefix_len) && !module->imp[i].prefix[prefix_len]) { |
| 405 | module = module->imp[i].module; |
| 406 | found = 1; |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | if (!found) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 411 | return NULL; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /* search in top level typedefs */ |
| 416 | for (i = 0; i < module->tpdf_size; i++) { |
| 417 | if (!strcmp(module->tpdf[i].name, qname)) { |
| 418 | return &module->tpdf[i]; |
| 419 | } |
| 420 | } |
| 421 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 422 | /* search in submodules */ |
| 423 | for (i = 0; i < module->inc_size; i++) { |
| 424 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
| 425 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, qname)) { |
| 426 | return &module->inc[i].submodule->tpdf[j]; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | static struct ly_ident *find_base_ident_sub(struct ly_module *module, struct ly_ident *ident, const char *basename) |
| 435 | { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 436 | unsigned int i; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 437 | struct ly_ident *base_iter; |
| 438 | struct ly_ident_der *der; |
| 439 | |
| 440 | for (i = 0; i < module->ident_size; i++) { |
| 441 | if (!strcmp(basename, module->ident[i].name)) { |
| 442 | /* we are done */ |
| 443 | |
| 444 | if (!ident) { |
| 445 | /* just search for type, so do not modify anything, just return |
| 446 | * the base identity pointer |
| 447 | */ |
| 448 | return &module->ident[i]; |
| 449 | } |
| 450 | |
| 451 | /* we are resolving identity definition, so now update structures */ |
| 452 | ident->base = base_iter = &module->ident[i]; |
| 453 | |
| 454 | while (base_iter) { |
| 455 | for (der = base_iter->der; der && der->next; der = der->next); |
| 456 | if (der) { |
| 457 | der->next = malloc(sizeof *der); |
| 458 | der = der->next; |
| 459 | } else { |
| 460 | ident->base->der = der = malloc(sizeof *der); |
| 461 | } |
| 462 | der->next = NULL; |
| 463 | der->ident = ident; |
| 464 | |
| 465 | base_iter = base_iter->base; |
| 466 | } |
| 467 | return ident->base; |
| 468 | } |
| 469 | } |
| 470 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 471 | return NULL; |
| 472 | } |
| 473 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 474 | static struct ly_ident *find_base_ident(struct ly_module *module, struct ly_ident *ident, struct lyxml_elem *node) |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 475 | { |
| 476 | const char *name; |
| 477 | int prefix_len = 0; |
| 478 | int i, found = 0; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 479 | struct ly_ident *result; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 480 | const char *basename; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 481 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 482 | basename = lyxml_get_attr(node, "name", NULL); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 483 | if (!basename) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 484 | LOGVAL(VE_MISSARG, LOGLINE(node), "name", "base"); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | /* search for the base identity */ |
| 489 | name = strchr(basename, ':'); |
| 490 | if (name) { |
| 491 | /* set name to correct position after colon */ |
| 492 | prefix_len = name - basename; |
| 493 | name++; |
| 494 | |
| 495 | if (!strncmp(basename, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 496 | /* prefix refers to the current module, ignore it */ |
| 497 | prefix_len = 0; |
| 498 | } |
| 499 | } else { |
| 500 | name = basename; |
| 501 | } |
| 502 | |
| 503 | if (prefix_len) { |
| 504 | /* get module where to search */ |
| 505 | for (i = 0; i < module->imp_size; i++) { |
| 506 | if (!strncmp(module->imp[i].prefix, basename, prefix_len) |
| 507 | && !module->imp[i].prefix[prefix_len]) { |
| 508 | module = module->imp[i].module; |
| 509 | found = 1; |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | if (!found) { |
| 514 | /* identity refers unknown data model */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 515 | LOGVAL(VE_INPREFIX, LOGLINE(node), basename); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 516 | return NULL; |
| 517 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 518 | } else { |
| 519 | /* search in submodules */ |
| 520 | for (i = 0; i < module->inc_size; i++) { |
| 521 | result = find_base_ident_sub((struct ly_module *)module->inc[i].submodule, ident, name); |
| 522 | if (result) { |
| 523 | return result; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 524 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 528 | /* search in the identified module */ |
| 529 | result = find_base_ident_sub(module, ident, name); |
| 530 | if (!result) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 531 | LOGVAL(VE_INARG, LOGLINE(node), basename, ident ? "identity" : "type"); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return result; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static int fill_yin_identity(struct ly_module *module, struct lyxml_elem *yin, struct ly_ident *ident) |
| 538 | { |
| 539 | struct lyxml_elem *node, *next; |
| 540 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 541 | if (read_yin_common(module, NULL, (struct ly_mnode *)ident, yin, OPT_IDENT | OPT_MODULE)) { |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 542 | return EXIT_FAILURE; |
| 543 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 544 | |
| 545 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 546 | if (!strcmp(node->name, "base")) { |
| 547 | if (ident->base) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 548 | LOGVAL(VE_TOOMANY, LOGLINE(node), "base", "identity"); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 549 | return EXIT_FAILURE; |
| 550 | } |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 551 | if (!find_base_ident(module, ident, node)) { |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 552 | return EXIT_FAILURE; |
| 553 | } |
| 554 | } else { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 555 | LOGVAL(VE_INSTMT, LOGLINE(node), node->name, "identity"); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 556 | return EXIT_FAILURE; |
| 557 | } |
| 558 | |
| 559 | lyxml_free_elem(module->ctx, node); |
| 560 | } |
| 561 | |
| 562 | return EXIT_SUCCESS; |
| 563 | } |
| 564 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 565 | static int fill_yin_type(struct ly_module *module, struct ly_mnode *parent, |
| 566 | struct lyxml_elem *yin, struct ly_type *type) |
| 567 | { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 568 | const char *value, *delim; |
| 569 | struct lyxml_elem *next, *node, root = {0}; |
| 570 | int i, j, r; |
| 571 | int64_t v, v_; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 572 | |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 573 | GETVAL(value, yin, "name") |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 574 | delim = strchr(value, ':'); |
| 575 | if (delim) { |
| 576 | type->prefix = lydict_insert(module->ctx, value, delim - value); |
| 577 | } |
Radek Krejci | 667b97f | 2015-05-25 15:03:30 +0200 | [diff] [blame] | 578 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 579 | type->der = find_superior_type(value, module, parent); |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 580 | if (!type->der) { |
| 581 | LOGVAL(VE_INARG, LOGLINE(yin), value, yin->name); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 582 | goto error; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 583 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 584 | type->base = type->der->type.base; |
| 585 | |
| 586 | switch (type->base) { |
| 587 | case LY_TYPE_BINARY: |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 588 | /* TODO length, 9.4.4 |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 589 | * - optional, 0..1, rekurzivni - omezuje, string (podobne jako range), |
| 590 | * hodnoty se musi vejit do 64b, podelementy |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 591 | */ |
| 592 | break; |
| 593 | case LY_TYPE_BITS: |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 594 | /* TODO bit, 9.7.4 |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 595 | * 1..n, nerekurzivni, stringy s podelementy */ |
| 596 | break; |
| 597 | case LY_TYPE_DEC64: |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 598 | /* TODO fraction-digits, 9.3.4 |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 599 | * - MUST, 1, nerekurzivni, hodnota 1-18 */ |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 600 | /* TODO range, 9.2.4 |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 601 | * - optional, 0..1, rekurzivne - omezuje, string, podelementy*/ |
| 602 | break; |
| 603 | case LY_TYPE_ENUM: |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 604 | /* RFC 6020 9.6 */ |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 605 | |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 606 | /* get enum specification, at least one must be present */ |
| 607 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 608 | if (!strcmp(node->name, "enum")) { |
| 609 | lyxml_unlink_elem(node); |
| 610 | lyxml_add_child(&root, node); |
| 611 | type->info.enums.count++; |
| 612 | } |
| 613 | } |
| 614 | if (yin->child) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 615 | LOGVAL(VE_INSTMT, LOGLINE(yin->child), yin->child->name); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 616 | goto error; |
| 617 | } |
| 618 | if (!type->info.enums.count) { |
| 619 | if (type->der->type.der) { |
| 620 | /* this is just a derived type with no enum specified */ |
| 621 | break; |
| 622 | } |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 623 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "enum", "type"); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 624 | goto error; |
| 625 | } |
| 626 | |
| 627 | type->info.enums.list = calloc(type->info.enums.count, sizeof *type->info.enums.list); |
| 628 | for (i = v = 0; root.child; i++) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 629 | r = read_yin_common(module, NULL, (struct ly_mnode *)&type->info.enums.list[i], root.child, OPT_IDENT); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 630 | if (r) { |
| 631 | type->info.enums.count = i + 1; |
| 632 | goto error; |
| 633 | } |
| 634 | /* the assigned name MUST NOT have any leading or trailing whitespace characters */ |
| 635 | value = type->info.enums.list[i].name; |
| 636 | if (isspace(value[0]) || isspace(value[strlen(value) - 1])) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 637 | LOGVAL(VE_ENUM_WS, LOGLINE(root.child), value); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 638 | type->info.enums.count = i + 1; |
| 639 | goto error; |
| 640 | } |
| 641 | |
| 642 | /* check the name uniqueness */ |
| 643 | for (j = 0; j < i; j++) { |
| 644 | if (!strcmp(type->info.enums.list[j].name, type->info.enums.list[i].name)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 645 | LOGVAL(VE_ENUM_DUPNAME, LOGLINE(root.child), type->info.enums.list[i].name); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 646 | type->info.enums.count = i + 1; |
| 647 | goto error; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | node = root.child->child; |
| 652 | if (node && !strcmp(node->name, "value")) { |
| 653 | value = lyxml_get_attr(node, "value", NULL); |
| 654 | v_ = strtol(value, NULL, 10); |
| 655 | |
| 656 | /* range check */ |
| 657 | if (v_ < INT32_MIN || v_ > INT32_MAX) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 658 | LOGVAL(VE_INARG, LOGLINE(node), value, "enum/value"); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 659 | type->info.enums.count = i + 1; |
| 660 | goto error; |
| 661 | } |
| 662 | type->info.enums.list[i].value = v_; |
| 663 | |
| 664 | /* keep the highest enum value for automatic increment */ |
| 665 | if (type->info.enums.list[i].value > v) { |
| 666 | v = type->info.enums.list[i].value; |
| 667 | v++; |
| 668 | } else { |
| 669 | /* check that the value is unique */ |
| 670 | for (j = 0; j < i; j++) { |
| 671 | if (type->info.enums.list[j].value == type->info.enums.list[i].value) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 672 | LOGVAL(VE_ENUM_DUPVAL, LOGLINE(node), type->info.enums.list[i].value, type->info.enums.list[i].name); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 673 | type->info.enums.count = i + 1; |
| 674 | goto error; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | } else { |
| 679 | /* assign value automatically */ |
| 680 | if (v > INT32_MAX) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 681 | LOGVAL(VE_INARG, LOGLINE(root.child), "2147483648", "enum/value"); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 682 | type->info.enums.count = i + 1; |
| 683 | goto error; |
| 684 | } |
| 685 | type->info.enums.list[i].value = v; |
| 686 | v++; |
| 687 | } |
| 688 | lyxml_free_elem(module->ctx, root.child); |
| 689 | } |
| 690 | break; |
| 691 | case LY_TYPE_IDENT: |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 692 | /* RFC 6020 9.10 */ |
| 693 | |
| 694 | /* get base specification, exactly one must be present */ |
| 695 | if (!yin->child) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 696 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "base", "type"); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 697 | goto error; |
| 698 | } |
| 699 | if (strcmp(yin->child->name, "base")) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 700 | LOGVAL(VE_INSTMT, LOGLINE(yin->child), yin->child->name); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 701 | goto error; |
| 702 | } |
| 703 | if (yin->child->next) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 704 | LOGVAL(VE_INSTMT, LOGLINE(yin->child->next), yin->child->next->name); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 705 | goto error; |
| 706 | } |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 707 | type->info.ident.ref = find_base_ident(module, NULL, yin->child); |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 708 | if (!type->info.ident.ref) { |
| 709 | return EXIT_FAILURE; |
| 710 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 711 | break; |
| 712 | case LY_TYPE_INST: |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 713 | /* TODO require-instance, 9.13.2 |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 714 | * - 0..1, true/false */ |
| 715 | break; |
| 716 | case LY_TYPE_INT8: |
| 717 | case LY_TYPE_INT16: |
| 718 | case LY_TYPE_INT32: |
| 719 | case LY_TYPE_INT64: |
| 720 | case LY_TYPE_UINT8: |
| 721 | case LY_TYPE_UINT16: |
| 722 | case LY_TYPE_UINT32: |
| 723 | case LY_TYPE_UINT64: |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 724 | /* TODO range, 9.2.4 |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 725 | * - optional, 0..1, i rekurzivne - omezuje, string, podelementy*/ |
| 726 | break; |
| 727 | case LY_TYPE_LEAFREF: |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 728 | /* TODO path, 9.9.2 |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 729 | * - 1, nerekurzivni, string */ |
| 730 | break; |
| 731 | case LY_TYPE_STRING: |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 732 | /* TODO length, 9.4.4 |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 733 | * - optional, 0..1, rekurzivni - omezuje, string (podobne jako range), hodnoty se musi vejit do 64b, podelementy |
| 734 | * pattern, 9.4.6 |
| 735 | * - optional, 0..n, rekurzivni - rozsiruje, string, podelementy */ |
| 736 | break; |
| 737 | case LY_TYPE_UNION: |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 738 | /* TODO type, 7.4 |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 739 | * - 1..n, nerekurzivni, resp rekurzivni pro union ale bez vazby na predky, nesmi byt empty nebo leafref */ |
| 740 | break; |
| 741 | default: |
| 742 | /* nothing needed : |
| 743 | * LY_TYPE_BOOL, LY_TYPE_EMPTY |
| 744 | */ |
| 745 | break; |
| 746 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 747 | |
| 748 | return EXIT_SUCCESS; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 749 | |
| 750 | error: |
| 751 | |
| 752 | while(root.child) { |
| 753 | lyxml_free_elem(module->ctx, root.child); |
| 754 | } |
| 755 | |
| 756 | return EXIT_FAILURE; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static int fill_yin_typedef(struct ly_module *module, struct ly_mnode *parent, |
| 760 | struct lyxml_elem *yin, struct ly_tpdf *tpdf) |
| 761 | { |
| 762 | const char *value; |
| 763 | struct lyxml_elem *node, *next; |
Radek Krejci | 76c45d3 | 2015-05-26 16:01:14 +0200 | [diff] [blame] | 764 | int r = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 765 | |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 766 | GETVAL(value, yin, "name"); |
| 767 | if (check_identifier(value, LY_IDENT_TYPE, LOGLINE(yin), module, parent)) { |
| 768 | goto error; |
| 769 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 770 | tpdf->name = lydict_insert(module->ctx, value, strlen(value)); |
| 771 | |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 772 | /* generic part - status, description, reference */ |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 773 | if (read_yin_common(module, NULL, (struct ly_mnode *)tpdf, yin, OPT_IDENT)) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 774 | goto error; |
| 775 | } |
| 776 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 777 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 778 | if (!strcmp(node->name, "type")) { |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 779 | if (tpdf->type.der) { |
| 780 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 781 | goto error; |
| 782 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 783 | r = fill_yin_type(module, parent, node, &tpdf->type); |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 784 | } else if (!strcmp(node->name, "default")) { |
| 785 | if (tpdf->dflt) { |
| 786 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 787 | goto error; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 788 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 789 | GETVAL(value, node, "value"); |
| 790 | tpdf->dflt = lydict_insert(module->ctx, value, strlen(value)); |
| 791 | } else if (!strcmp(node->name, "units")) { |
| 792 | if (tpdf->units) { |
| 793 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 794 | goto error; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 795 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 796 | GETVAL(value, node, "name"); |
| 797 | tpdf->units = lydict_insert(module->ctx, value, strlen(value)); |
| 798 | } else { |
| 799 | LOGVAL(VE_INSTMT, LOGLINE(node), value); |
| 800 | r = 1; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 801 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 802 | lyxml_free_elem(module->ctx, node); |
| 803 | if (r) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 804 | goto error; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 808 | /* check mandatory value */ |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 809 | if (!tpdf->type.der) { |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 810 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "type", yin->name); |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 811 | goto error; |
| 812 | } |
| 813 | |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 814 | /* check default value */ |
| 815 | if (check_default(&tpdf->type, tpdf->dflt)) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 816 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | return EXIT_SUCCESS; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 820 | |
| 821 | error: |
| 822 | |
| 823 | return EXIT_FAILURE; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 826 | static int fill_yin_must(struct ly_module *module, struct lyxml_elem *yin, struct ly_must *must) |
| 827 | { |
Radek Krejci | 8bf9936 | 2015-06-03 16:43:21 +0200 | [diff] [blame] | 828 | struct lyxml_elem *child, *next; |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 829 | const char *value; |
| 830 | |
| 831 | GETVAL(value, yin, "condition"); |
| 832 | must->cond = lydict_insert(module->ctx, value, strlen(value)); |
| 833 | |
Radek Krejci | 8bf9936 | 2015-06-03 16:43:21 +0200 | [diff] [blame] | 834 | LY_TREE_FOR_SAFE(yin->child, next, child) { |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 835 | if (!strcmp(child->name, "description")) { |
| 836 | if (must->dsc) { |
| 837 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 838 | goto error; |
| 839 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 840 | must->dsc = read_yin_subnode(module->ctx, child, "text"); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 841 | if (!must->dsc) { |
| 842 | goto error; |
| 843 | } |
| 844 | } else if (!strcmp(child->name, "reference")) { |
| 845 | if (must->ref) { |
| 846 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 847 | goto error; |
| 848 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 849 | must->ref = read_yin_subnode(module->ctx, child, "text"); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 850 | if (!must->ref) { |
| 851 | goto error; |
| 852 | } |
| 853 | } else if (!strcmp(child->name, "error-app-tag")) { |
| 854 | if (must->eapptag) { |
| 855 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 856 | goto error; |
| 857 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 858 | must->eapptag = read_yin_subnode(module->ctx, child, "value"); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 859 | if (!must->eapptag) { |
| 860 | goto error; |
| 861 | } |
| 862 | } else if (!strcmp(child->name, "error-message")) { |
| 863 | if (must->emsg) { |
| 864 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 865 | goto error; |
| 866 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 867 | must->emsg = read_yin_subnode(module->ctx, child, "value"); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 868 | if (!must->emsg) { |
| 869 | goto error; |
| 870 | } |
| 871 | } else { |
| 872 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 873 | goto error; |
| 874 | } |
| 875 | |
| 876 | lyxml_free_elem(module->ctx, child); |
| 877 | } |
| 878 | |
| 879 | |
| 880 | return EXIT_SUCCESS; |
| 881 | |
| 882 | error: |
| 883 | |
| 884 | return EXIT_FAILURE; |
| 885 | } |
| 886 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 887 | static int fill_yin_import(struct ly_module *module, struct lyxml_elem *yin, struct ly_import *imp) |
| 888 | { |
| 889 | struct lyxml_elem *child; |
| 890 | const char *value; |
| 891 | |
| 892 | LY_TREE_FOR(yin->child, child) { |
| 893 | if (!strcmp(child->name, "prefix")) { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 894 | GETVAL(value, child, "value"); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 895 | if (check_identifier(value, LY_IDENT_PREFIX, LOGLINE(child), module, NULL)) { |
| 896 | goto error; |
| 897 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 898 | imp->prefix = lydict_insert(module->ctx, value, strlen(value)); |
| 899 | } else if (!strcmp(child->name, "revision-date")) { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 900 | if (imp->rev[0]) { |
| 901 | LOGVAL(VE_TOOMANY, LOGLINE(child), "revision-date", yin->name); |
| 902 | goto error; |
| 903 | } |
| 904 | GETVAL(value, child, "date"); |
| 905 | if (check_date(value, LOGLINE(child))) { |
| 906 | goto error; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 907 | } |
| 908 | memcpy(imp->rev, value, LY_REV_SIZE - 1); |
| 909 | } else { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 910 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 911 | goto error; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 915 | /* check mandatory information */ |
| 916 | if (!imp->prefix) { |
| 917 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "prefix", yin->name); |
| 918 | goto error; |
| 919 | } |
| 920 | |
| 921 | GETVAL(value, yin, "module"); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 922 | imp->module = ly_ctx_get_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL, 1); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 923 | if (!imp->module) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 924 | LOGERR(LY_EVALID, "Importing \"%s\" module into \"%s\" failed.", |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 925 | value, module->name); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 926 | LOGVAL(VE_INARG, LOGLINE(yin), value, yin->name); |
| 927 | goto error; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | return EXIT_SUCCESS; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 931 | |
| 932 | error: |
| 933 | |
| 934 | return EXIT_FAILURE; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static int fill_yin_include(struct ly_module *module, struct lyxml_elem *yin, struct ly_include *inc) |
| 938 | { |
| 939 | struct lyxml_elem *child; |
| 940 | const char *value; |
| 941 | |
| 942 | LY_TREE_FOR(yin->child, child) { |
| 943 | if (!strcmp(child->name, "revision-date")) { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 944 | if (inc->rev[0]) { |
| 945 | LOGVAL(VE_TOOMANY, LOGLINE(child), "revision-date", yin->name); |
| 946 | goto error; |
| 947 | } |
| 948 | GETVAL(value, child, "date"); |
| 949 | if (check_date(value, LOGLINE(child))) { |
| 950 | goto error; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 951 | } |
| 952 | memcpy(inc->rev, value, LY_REV_SIZE - 1); |
| 953 | } else { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 954 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 955 | goto error; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 959 | GETVAL(value, yin, "module"); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 960 | inc->submodule = ly_ctx_get_submodule(module, value, inc->rev[0] ? inc->rev : NULL); |
| 961 | if (!inc->submodule) { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 962 | LOGERR(LY_EVALID, "Including \"%s\" module into \"%s\" failed.", |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 963 | value, module->name); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 964 | LOGVAL(VE_INARG, LOGLINE(yin), value, yin->name); |
| 965 | goto error; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 968 | /* check that belongs-to corresponds */ |
| 969 | if (module->type) { |
| 970 | module = ((struct ly_submodule *)module)->belongsto; |
| 971 | } |
| 972 | if (inc->submodule->belongsto != module) { |
| 973 | LOGVAL(VE_INARG, LOGLINE(yin), value, yin->name); |
| 974 | LOGVAL(VE_SPEC, 0, "The included module does not belongs-to the \"%s\" module", module->name); |
| 975 | goto error; |
| 976 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 977 | |
| 978 | return EXIT_SUCCESS; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 979 | |
| 980 | error: |
| 981 | |
| 982 | return EXIT_FAILURE; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 986 | /* |
| 987 | * Covers: |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 988 | * description, reference, status, optionaly config |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 989 | * |
| 990 | * ext: 0 - no config |
| 991 | * 1 - parse config, but not inherit it |
| 992 | * 2 - parse config and if not present, inherit it from parent |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 993 | */ |
| 994 | static int read_yin_common(struct ly_module *module, struct ly_mnode *parent, |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 995 | struct ly_mnode *mnode, struct lyxml_elem *xmlnode, int opt) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 996 | { |
| 997 | const char *value; |
| 998 | struct lyxml_elem *sub, *next; |
| 999 | struct ly_ctx * const ctx = module->ctx; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1000 | int r = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1001 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1002 | if (opt & OPT_MODULE) { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1003 | mnode->module = module; |
| 1004 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1005 | |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1006 | GETVAL(value, xmlnode, "name"); |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1007 | if ((opt & OPT_IDENT) && check_identifier(value, LY_IDENT_NAME, LOGLINE(xmlnode), NULL, NULL)) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1008 | goto error; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1009 | } |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1010 | mnode->name = lydict_insert(ctx, value, strlen(value)); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1011 | |
| 1012 | /* process local parameters */ |
| 1013 | LY_TREE_FOR_SAFE(xmlnode->child, next, sub) { |
| 1014 | if (!strcmp(sub->name, "description")) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1015 | if (mnode->dsc) { |
| 1016 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name); |
| 1017 | goto error; |
| 1018 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 1019 | mnode->dsc = read_yin_subnode(ctx, sub, "text"); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1020 | if (!mnode->dsc) { |
| 1021 | r = 1; |
| 1022 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1023 | } else if (!strcmp(sub->name, "reference")) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1024 | if (mnode->ref) { |
| 1025 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name); |
| 1026 | goto error; |
| 1027 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 1028 | mnode->ref = read_yin_subnode(ctx, sub, "text"); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1029 | if (!mnode->ref) { |
| 1030 | r = 1; |
| 1031 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1032 | } else if (!strcmp(sub->name, "status")) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1033 | if (mnode->flags & LY_NODE_STATUS_MASK) { |
| 1034 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name); |
| 1035 | goto error; |
| 1036 | } |
| 1037 | GETVAL(value, sub, "value"); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1038 | if (!strcmp(value, "current")) { |
| 1039 | mnode->flags |= LY_NODE_STATUS_CURR; |
| 1040 | } else if (!strcmp(value, "deprecated")) { |
| 1041 | mnode->flags |= LY_NODE_STATUS_DEPRC; |
| 1042 | } else if (!strcmp(value, "obsolete")) { |
| 1043 | mnode->flags |= LY_NODE_STATUS_OBSLT; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1044 | } else { |
| 1045 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1046 | r = 1; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1047 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1048 | } else if ((opt & OPT_CONFIG) && !strcmp(sub->name, "config")) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1049 | if (mnode->flags & LY_NODE_CONFIG_MASK) { |
| 1050 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name); |
| 1051 | goto error; |
| 1052 | } |
| 1053 | GETVAL(value, sub, "value"); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1054 | if (!strcmp(value, "false")) { |
| 1055 | mnode->flags |= LY_NODE_CONFIG_R; |
| 1056 | } else if (!strcmp(value, "false")) { |
| 1057 | mnode->flags |= LY_NODE_CONFIG_W; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1058 | } else { |
| 1059 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1060 | r = 1; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1061 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1062 | } else { |
| 1063 | /* skip the lyxml_free_elem */ |
| 1064 | continue; |
| 1065 | } |
| 1066 | lyxml_free_elem(ctx, sub); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1067 | if (r) { |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1068 | goto error; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1069 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1070 | } |
| 1071 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1072 | if ((opt & OPT_INHERIT) && !(mnode->flags & LY_NODE_CONFIG_MASK)) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1073 | /* get config flag from parent */ |
| 1074 | if (parent) { |
| 1075 | mnode->flags |= parent->flags & LY_NODE_CONFIG_MASK; |
| 1076 | } else { |
| 1077 | /* default config is true */ |
| 1078 | mnode->flags |= LY_NODE_CONFIG_W; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | return EXIT_SUCCESS; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1083 | |
| 1084 | error: |
| 1085 | |
| 1086 | return EXIT_FAILURE; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1089 | /* additional check in case statement - the child must be unique across |
| 1090 | * all other case names and its data children |
| 1091 | */ |
| 1092 | static int check_branch_id(struct ly_mnode *parent, struct ly_mnode *new, struct ly_mnode *excl, int line) |
| 1093 | { |
| 1094 | struct ly_mnode *mnode, *submnode; |
| 1095 | |
| 1096 | if (new->nodetype == LY_NODE_CHOICE) { |
| 1097 | /* we have nested choice in case, so we need recursion */ |
| 1098 | LY_TREE_FOR(new->child, mnode) { |
| 1099 | if (mnode->nodetype == LY_NODE_CASE) { |
| 1100 | LY_TREE_FOR(mnode->child, submnode) { |
| 1101 | if (check_branch_id(parent, submnode, new, line)) { |
| 1102 | return EXIT_FAILURE; |
| 1103 | } |
| 1104 | } |
| 1105 | } else if (check_branch_id(parent, mnode, new, line)) { |
| 1106 | return EXIT_FAILURE; |
| 1107 | } |
| 1108 | } |
| 1109 | } else { |
| 1110 | LY_TREE_FOR(parent->child, mnode) { |
| 1111 | if (mnode == excl) { |
| 1112 | continue; |
| 1113 | } |
| 1114 | |
| 1115 | if (!strcmp(new->name, mnode->name)) { |
| 1116 | LOGVAL(VE_INID, line, new->name, "duplicated identifier within a choice's cases"); |
| 1117 | return EXIT_FAILURE; |
| 1118 | } |
| 1119 | if (mnode->nodetype == LY_NODE_CASE) { |
| 1120 | LY_TREE_FOR(mnode->child, submnode) { |
| 1121 | if (!strcmp(new->name, submnode->name)) { |
| 1122 | LOGVAL(VE_INID, line, new->name, "duplicated identifier within a choice's cases"); |
| 1123 | return EXIT_FAILURE; |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | return EXIT_SUCCESS; |
| 1131 | } |
| 1132 | |
| 1133 | static struct ly_mnode *read_yin_case(struct ly_module *module, |
| 1134 | struct ly_mnode *parent, |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1135 | struct lyxml_elem *yin, |
| 1136 | int resolve) |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1137 | { |
| 1138 | struct lyxml_elem *sub, *next; |
| 1139 | struct ly_mnode_case *mcase; |
Radek Krejci | 5c7bff4 | 2015-06-03 16:25:23 +0200 | [diff] [blame] | 1140 | struct ly_mnode *retval, *mnode = NULL; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1141 | |
| 1142 | mcase = calloc(1, sizeof *mcase); |
| 1143 | mcase->nodetype = LY_NODE_CASE; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1144 | mcase->prev = (struct ly_mnode *)mcase; |
| 1145 | retval = (struct ly_mnode *)mcase; |
| 1146 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1147 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_INHERIT)) { |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1148 | goto error; |
| 1149 | } |
| 1150 | |
| 1151 | /* process choice's specific children */ |
| 1152 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 1153 | if (!strcmp(sub->name, "container")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1154 | mnode = read_yin_container(module, retval, sub, resolve); |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1155 | } else if (!strcmp(sub->name, "leaf-list")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1156 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1157 | } else if (!strcmp(sub->name, "leaf")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1158 | mnode = read_yin_leaf(module, retval, sub, resolve); |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1159 | } else if (!strcmp(sub->name, "list")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1160 | mnode = read_yin_list(module, retval, sub, resolve); |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1161 | } else if (!strcmp(sub->name, "uses")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1162 | mnode = read_yin_uses(module, retval, sub, resolve); |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1163 | } else if (!strcmp(sub->name, "choice")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1164 | mnode = read_yin_choice(module, retval, sub, resolve); |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1165 | } else if (!strcmp(sub->name, "anyxml")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1166 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1167 | #if 0 |
| 1168 | } else { |
| 1169 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 1170 | goto error; |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 1171 | #else |
| 1172 | } else { |
| 1173 | continue; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1174 | #endif |
| 1175 | } |
| 1176 | |
| 1177 | if (!mnode) { |
| 1178 | goto error; |
| 1179 | } else if (check_branch_id(parent, mnode, mnode, LOGLINE(sub))) { |
| 1180 | goto error; |
| 1181 | } |
| 1182 | |
| 1183 | mnode = NULL; |
| 1184 | lyxml_free_elem(module->ctx, sub); |
| 1185 | } |
| 1186 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1187 | /* inherit config flag */ |
| 1188 | if (parent) { |
| 1189 | retval->flags |= parent->flags & LY_NODE_CONFIG_MASK; |
| 1190 | } else { |
| 1191 | /* default config is true */ |
| 1192 | retval->flags |= LY_NODE_CONFIG_W; |
| 1193 | } |
| 1194 | |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1195 | /* insert the node into the schema tree */ |
| 1196 | ly_mnode_addchild(parent, retval); |
| 1197 | return retval; |
| 1198 | |
| 1199 | error: |
| 1200 | |
| 1201 | ly_mnode_free(retval); |
| 1202 | |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1206 | static struct ly_mnode *read_yin_choice(struct ly_module *module, |
| 1207 | struct ly_mnode *parent, |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1208 | struct lyxml_elem *yin, |
| 1209 | int resolve) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1210 | { |
| 1211 | struct lyxml_elem *sub, *next; |
| 1212 | struct ly_ctx * const ctx = module->ctx; |
Radek Krejci | 5dea2d5 | 2015-06-03 15:45:23 +0200 | [diff] [blame] | 1213 | struct ly_mnode *retval, *mnode = NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1214 | struct ly_mnode_choice *choice; |
Radek Krejci | a26eb0c | 2015-06-03 16:22:52 +0200 | [diff] [blame] | 1215 | const char *value; |
| 1216 | char *dflt_str = NULL; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1217 | int f_mand = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1218 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1219 | choice = calloc(1, sizeof *choice); |
| 1220 | choice->nodetype = LY_NODE_CHOICE; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1221 | choice->prev = (struct ly_mnode *)choice; |
| 1222 | retval = (struct ly_mnode *)choice; |
| 1223 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1224 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1225 | goto error; |
| 1226 | } |
| 1227 | |
| 1228 | /* process choice's specific children */ |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1229 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1230 | if (!strcmp(sub->name, "container")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1231 | if (!(mnode = read_yin_container(module, retval, sub, resolve))) { |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1232 | goto error; |
| 1233 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1234 | } else if (!strcmp(sub->name, "leaf-list")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1235 | if (!(mnode = read_yin_leaflist(module, retval, sub, resolve))) { |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1236 | goto error; |
| 1237 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1238 | } else if (!strcmp(sub->name, "leaf")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1239 | if (!(mnode = read_yin_leaf(module, retval, sub, resolve))) { |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1240 | goto error; |
| 1241 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1242 | } else if (!strcmp(sub->name, "list")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1243 | if (!(mnode = read_yin_list(module, retval, sub, resolve))) { |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1244 | goto error; |
| 1245 | } |
| 1246 | } else if (!strcmp(sub->name, "case")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1247 | if (!(mnode = read_yin_case(module, retval, sub, resolve))) { |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1248 | goto error; |
| 1249 | } |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1250 | } else if (!strcmp(sub->name, "anyxml")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1251 | if (!(mnode = read_yin_anyxml(module, retval, sub, resolve))) { |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1252 | goto error; |
| 1253 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1254 | } else if (!strcmp(sub->name, "default")) { |
| 1255 | if (dflt_str) { |
| 1256 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1257 | goto error; |
| 1258 | } |
| 1259 | GETVAL(value, sub, "value"); |
| 1260 | dflt_str = strdup(value); |
| 1261 | } else if (!strcmp(sub->name, "mandatory")) { |
| 1262 | if (f_mand) { |
| 1263 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1264 | goto error; |
| 1265 | } |
| 1266 | /* just checking the flags in leaf is not sufficient, we would allow |
| 1267 | * multiple mandatory statements with the "false" value |
| 1268 | */ |
| 1269 | f_mand = 1; |
| 1270 | |
| 1271 | GETVAL(value, sub, "value"); |
| 1272 | if (!strcmp(value, "true")) { |
| 1273 | choice->flags |= LY_NODE_MANDATORY; |
| 1274 | } else if (strcmp(value, "false")) { |
| 1275 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1276 | goto error; |
| 1277 | } /* else false is the default value, so we can ignore it */ |
| 1278 | #if 0 |
Radek Krejci | 667b97f | 2015-05-25 15:03:30 +0200 | [diff] [blame] | 1279 | } else { |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1280 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 1281 | goto error; |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 1282 | #else |
| 1283 | } else { |
| 1284 | continue; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1285 | #endif |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1286 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1287 | |
| 1288 | if (mnode && check_branch_id(retval, mnode, mnode,LOGLINE(sub))) { |
| 1289 | goto error; |
| 1290 | } |
| 1291 | mnode = NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1292 | lyxml_free_elem(ctx, sub); |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /* check - default is prohibited in combination with mandatory */ |
| 1296 | if (dflt_str && (choice->flags & LY_NODE_MANDATORY)) { |
| 1297 | LOGVAL(VE_SPEC, LOGLINE(yin), "The \"default\" statement MUST NOT be present on choices where \"mandatory\" is true."); |
| 1298 | goto error; |
| 1299 | } |
| 1300 | |
| 1301 | /* link default with the case */ |
| 1302 | if (dflt_str) { |
| 1303 | LY_TREE_FOR(choice->child, mnode) { |
| 1304 | if (!strcmp(mnode->name, dflt_str)) { |
| 1305 | choice->dflt = mnode; |
| 1306 | break; |
| 1307 | } |
| 1308 | } |
| 1309 | if (!choice->dflt) { |
| 1310 | /* default branch not found */ |
| 1311 | LOGVAL(VE_INARG, LOGLINE(yin), dflt_str, "default"); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1312 | goto error; |
| 1313 | } |
Radek Krejci | a26eb0c | 2015-06-03 16:22:52 +0200 | [diff] [blame] | 1314 | free(dflt_str); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1317 | /* insert the node into the schema tree */ |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1318 | if (parent) { |
| 1319 | ly_mnode_addchild(parent, retval); |
| 1320 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1321 | |
| 1322 | return retval; |
| 1323 | |
| 1324 | error: |
| 1325 | |
| 1326 | ly_mnode_free(retval); |
Radek Krejci | a26eb0c | 2015-06-03 16:22:52 +0200 | [diff] [blame] | 1327 | free(dflt_str); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1328 | |
| 1329 | return NULL; |
| 1330 | } |
| 1331 | |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1332 | static struct ly_mnode *read_yin_anyxml(struct ly_module *module, |
| 1333 | struct ly_mnode *parent, |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1334 | struct lyxml_elem *yin, |
| 1335 | int resolve) |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1336 | { |
| 1337 | struct ly_mnode *retval; |
| 1338 | struct ly_mnode_leaf *anyxml; |
| 1339 | struct lyxml_elem *sub, *next; |
| 1340 | const char *value; |
| 1341 | int r; |
| 1342 | int f_mand = 0; |
| 1343 | int c_must = 0; |
| 1344 | |
| 1345 | anyxml = calloc(1, sizeof *anyxml); |
| 1346 | anyxml->nodetype = LY_NODE_ANYXML; |
| 1347 | anyxml->prev = (struct ly_mnode *)anyxml; |
| 1348 | retval = (struct ly_mnode *)anyxml; |
| 1349 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1350 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1351 | goto error; |
| 1352 | } |
| 1353 | |
| 1354 | LY_TREE_FOR_SAFE(yin->child, next, sub) |
| 1355 | { |
| 1356 | if (!strcmp(sub->name, "mandatory")) { |
| 1357 | if (f_mand) { |
| 1358 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1359 | goto error; |
| 1360 | } |
| 1361 | /* just checking the flags in leaf is not sufficient, we would allow |
| 1362 | * multiple mandatory statements with the "false" value |
| 1363 | */ |
| 1364 | f_mand = 1; |
| 1365 | |
| 1366 | GETVAL(value, sub, "value"); |
| 1367 | if (!strcmp(value, "true")) { |
| 1368 | anyxml->flags |= LY_NODE_MANDATORY; |
| 1369 | } else if (strcmp(value, "false")) { |
| 1370 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1371 | goto error; |
| 1372 | } /* else false is the default value, so we can ignore it */ |
| 1373 | |
| 1374 | lyxml_free_elem(module->ctx, sub); |
| 1375 | } else if (!strcmp(sub->name, "must")) { |
| 1376 | c_must++; |
| 1377 | |
| 1378 | #if 0 |
| 1379 | } else { |
| 1380 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 1381 | goto error; |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 1382 | #else |
| 1383 | } else { |
| 1384 | continue; |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1385 | #endif |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | /* middle part - process nodes with cardinality of 0..n */ |
| 1390 | if (c_must) { |
| 1391 | anyxml->must = calloc(c_must, sizeof *anyxml->must); |
| 1392 | } |
| 1393 | |
| 1394 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 1395 | if (!strcmp(sub->name, "must")) { |
| 1396 | r = fill_yin_must(module, sub, &anyxml->must[anyxml->must_size]); |
| 1397 | anyxml->must_size++; |
| 1398 | |
| 1399 | if (r) { |
| 1400 | goto error; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | lyxml_free_elem(module->ctx, sub); |
| 1405 | } |
| 1406 | |
| 1407 | if (parent) { |
| 1408 | ly_mnode_addchild(parent, retval); |
| 1409 | } |
| 1410 | |
| 1411 | return retval; |
| 1412 | |
| 1413 | error: |
| 1414 | |
| 1415 | ly_mnode_free(retval); |
| 1416 | |
| 1417 | return NULL; |
| 1418 | } |
| 1419 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1420 | static struct ly_mnode *read_yin_leaf(struct ly_module *module, |
| 1421 | struct ly_mnode *parent, |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1422 | struct lyxml_elem *yin, |
| 1423 | int resolve) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1424 | { |
| 1425 | struct ly_mnode *retval; |
| 1426 | struct ly_mnode_leaf *leaf; |
| 1427 | struct lyxml_elem *sub, *next; |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1428 | const char *value; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1429 | int r; |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1430 | int c_must = 0, f_mand = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1431 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1432 | leaf = calloc(1, sizeof *leaf); |
| 1433 | leaf->nodetype = LY_NODE_LEAF; |
| 1434 | leaf->prev = (struct ly_mnode *)leaf; |
| 1435 | retval = (struct ly_mnode *)leaf; |
| 1436 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1437 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1438 | goto error; |
| 1439 | } |
| 1440 | |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1441 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1442 | if (!strcmp(sub->name, "type")) { |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1443 | if (leaf->type.der) { |
| 1444 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1445 | goto error; |
| 1446 | } |
| 1447 | if (fill_yin_type(module, parent, sub, &leaf->type)) { |
| 1448 | goto error; |
| 1449 | } |
| 1450 | } else if (!strcmp(sub->name, "default")) { |
| 1451 | if (leaf->dflt) { |
| 1452 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1453 | goto error; |
| 1454 | } |
| 1455 | GETVAL(value, sub, "value"); |
| 1456 | leaf->dflt = lydict_insert(module->ctx, value, strlen(value)); |
| 1457 | } else if (!strcmp(sub->name, "units")) { |
| 1458 | if (leaf->units) { |
| 1459 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1460 | goto error; |
| 1461 | } |
Radek Krejci | f4f4d9f | 2015-06-03 10:57:45 +0200 | [diff] [blame] | 1462 | GETVAL(value, sub, "name"); |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1463 | leaf->units = lydict_insert(module->ctx, value, strlen(value)); |
| 1464 | } else if (!strcmp(sub->name, "mandatory")) { |
| 1465 | if (f_mand) { |
| 1466 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1467 | goto error; |
| 1468 | } |
| 1469 | /* just checking the flags in leaf is not sufficient, we would allow |
| 1470 | * multiple mandatory statements with the "false" value |
| 1471 | */ |
| 1472 | f_mand = 1; |
| 1473 | |
| 1474 | GETVAL(value, sub, "value"); |
| 1475 | if (!strcmp(value, "true")) { |
| 1476 | leaf->flags |= LY_NODE_MANDATORY; |
| 1477 | } else if (strcmp(value, "false")) { |
| 1478 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1479 | goto error; |
| 1480 | } /* else false is the default value, so we can ignore it */ |
| 1481 | } else if (!strcmp(sub->name, "must")) { |
| 1482 | c_must++; |
| 1483 | |
| 1484 | /* skip element free at the end of the loop */ |
| 1485 | continue; |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 1486 | #if 0 |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1487 | } else { |
| 1488 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 1489 | goto error; |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 1490 | #else |
| 1491 | } else { |
| 1492 | continue; |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 1493 | #endif |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | lyxml_free_elem(module->ctx, sub); |
| 1497 | } |
| 1498 | |
| 1499 | /* check mandatory parameters */ |
| 1500 | if (!leaf->type.der) { |
| 1501 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "type", yin->name); |
| 1502 | goto error; |
| 1503 | } |
Radek Krejci | ccdec64 | 2015-06-03 10:58:45 +0200 | [diff] [blame] | 1504 | if (leaf->dflt && check_default(&leaf->type, leaf->dflt)) { |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1505 | goto error; |
| 1506 | } |
| 1507 | |
| 1508 | /* middle part - process nodes with cardinality of 0..n */ |
| 1509 | if (c_must) { |
| 1510 | leaf->must = calloc(c_must, sizeof *leaf->must); |
| 1511 | } |
| 1512 | |
| 1513 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 1514 | if (!strcmp(sub->name, "must")) { |
| 1515 | r = fill_yin_must(module, sub, &leaf->must[leaf->must_size]); |
| 1516 | leaf->must_size++; |
| 1517 | |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1518 | if (r) { |
| 1519 | goto error; |
| 1520 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1521 | } |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 1522 | |
| 1523 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1526 | if (parent) { |
| 1527 | ly_mnode_addchild(parent, retval); |
| 1528 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1529 | |
| 1530 | return retval; |
| 1531 | |
| 1532 | error: |
| 1533 | |
| 1534 | ly_mnode_free(retval); |
| 1535 | |
| 1536 | return NULL; |
| 1537 | } |
| 1538 | |
| 1539 | static struct ly_mnode *read_yin_leaflist(struct ly_module *module, |
| 1540 | struct ly_mnode *parent, |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1541 | struct lyxml_elem *yin, |
| 1542 | int resolve) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1543 | { |
| 1544 | struct ly_mnode *retval; |
| 1545 | struct ly_mnode_leaflist *llist; |
| 1546 | struct lyxml_elem *sub, *next; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1547 | const char *value; |
| 1548 | char *endptr; |
Radek Krejci | 17e93f9 | 2015-06-03 11:01:55 +0200 | [diff] [blame] | 1549 | unsigned long val; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1550 | int r; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1551 | int c_must = 0; |
Radek Krejci | 17e93f9 | 2015-06-03 11:01:55 +0200 | [diff] [blame] | 1552 | int f_ordr = 0, f_min = 0, f_max = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1553 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1554 | llist = calloc(1, sizeof *llist); |
| 1555 | llist->nodetype = LY_NODE_LEAFLIST; |
| 1556 | llist->prev = (struct ly_mnode *)llist; |
| 1557 | retval = (struct ly_mnode *)llist; |
| 1558 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1559 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1560 | goto error; |
| 1561 | } |
| 1562 | |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1563 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1564 | if (!strcmp(sub->name, "type")) { |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1565 | if (llist->type.der) { |
| 1566 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1567 | goto error; |
| 1568 | } |
| 1569 | if (fill_yin_type(module, parent, sub, &llist->type)) { |
| 1570 | goto error; |
| 1571 | } |
| 1572 | } else if (!strcmp(sub->name, "units")) { |
| 1573 | if (llist->units) { |
| 1574 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1575 | goto error; |
| 1576 | } |
Radek Krejci | f4f4d9f | 2015-06-03 10:57:45 +0200 | [diff] [blame] | 1577 | GETVAL(value, sub, "name"); |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1578 | llist->units = lydict_insert(module->ctx, value, strlen(value)); |
| 1579 | } else if (!strcmp(sub->name, "ordered-by")) { |
| 1580 | if (f_ordr) { |
| 1581 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1582 | goto error; |
| 1583 | } |
| 1584 | /* just checking the flags in llist is not sufficient, we would |
| 1585 | * allow multiple ordered-by statements with the "system" value |
| 1586 | */ |
| 1587 | f_ordr = 1; |
| 1588 | |
| 1589 | if (llist->flags & LY_NODE_CONFIG_R) { |
| 1590 | /* RFC 6020, 7.7.5 - ignore ordering when the list represents |
| 1591 | * state data |
| 1592 | */ |
| 1593 | lyxml_free_elem(module->ctx, sub); |
| 1594 | continue; |
| 1595 | } |
| 1596 | |
| 1597 | GETVAL(value, sub, "value"); |
| 1598 | if (!strcmp(value, "user")) { |
| 1599 | llist->flags |= LY_NODE_USERORDERED; |
| 1600 | } else if (strcmp(value, "system")) { |
| 1601 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1602 | goto error; |
| 1603 | } /* else system is the default value, so we can ignore it */ |
| 1604 | } else if (!strcmp(sub->name, "must")) { |
| 1605 | c_must++; |
| 1606 | |
| 1607 | /* skip element free at the end of the loop */ |
| 1608 | continue; |
| 1609 | } else if (!strcmp(sub->name, "min-elements")) { |
| 1610 | if (f_min) { |
| 1611 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1612 | goto error; |
| 1613 | } |
| 1614 | f_min = 1; |
| 1615 | |
| 1616 | GETVAL(value, sub, "value"); |
| 1617 | while(isspace(value[0])) { |
| 1618 | value++; |
| 1619 | } |
| 1620 | |
| 1621 | /* convert it to uint32_t */ |
| 1622 | errno = 0; |
| 1623 | endptr = NULL; |
| 1624 | val = strtoul(value, &endptr, 10); |
Radek Krejci | 78fc30e | 2015-06-03 11:02:50 +0200 | [diff] [blame] | 1625 | if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) { |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1626 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1627 | goto error; |
| 1628 | } |
Radek Krejci | 17e93f9 | 2015-06-03 11:01:55 +0200 | [diff] [blame] | 1629 | llist->min = (uint32_t)val; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1630 | } else if (!strcmp(sub->name, "max-elements")) { |
| 1631 | if (f_max) { |
| 1632 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1633 | goto error; |
| 1634 | } |
| 1635 | f_max = 1; |
| 1636 | |
| 1637 | GETVAL(value, sub, "value"); |
| 1638 | while(isspace(value[0])) { |
| 1639 | value++; |
| 1640 | } |
| 1641 | |
| 1642 | /* convert it to uint32_t */ |
| 1643 | errno = 0; |
| 1644 | endptr = NULL; |
| 1645 | val = strtoul(value, &endptr, 10); |
Radek Krejci | 78fc30e | 2015-06-03 11:02:50 +0200 | [diff] [blame] | 1646 | if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) { |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1647 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1648 | goto error; |
| 1649 | } |
Radek Krejci | 17e93f9 | 2015-06-03 11:01:55 +0200 | [diff] [blame] | 1650 | llist->max = (uint32_t)val; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1651 | |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 1652 | #if 0 |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 1653 | } else { |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1654 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 1655 | goto error; |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 1656 | #else |
| 1657 | } else { |
| 1658 | continue; |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 1659 | #endif |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | lyxml_free_elem(module->ctx, sub); |
| 1663 | } |
| 1664 | |
| 1665 | /* check constraints */ |
| 1666 | if (!llist->type.der) { |
| 1667 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "type", yin->name); |
| 1668 | goto error; |
| 1669 | } |
| 1670 | if (llist->max && llist->min > llist->max) { |
| 1671 | LOGVAL(VE_SPEC, LOGLINE(yin), "\"min-elements\" is bigger than \"max-elements\"."); |
| 1672 | goto error; |
| 1673 | } |
| 1674 | |
| 1675 | /* middle part - process nodes with cardinality of 0..n */ |
| 1676 | if (c_must) { |
| 1677 | llist->must = calloc(c_must, sizeof *llist->must); |
| 1678 | } |
| 1679 | |
| 1680 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 1681 | if (!strcmp(sub->name, "must")) { |
| 1682 | r = fill_yin_must(module, sub, &llist->must[llist->must_size]); |
| 1683 | llist->must_size++; |
| 1684 | |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1685 | if (r) { |
| 1686 | goto error; |
| 1687 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1688 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 1689 | |
| 1690 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1691 | } |
| 1692 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1693 | if (parent) { |
| 1694 | ly_mnode_addchild(parent, retval); |
| 1695 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1696 | |
| 1697 | return retval; |
| 1698 | |
| 1699 | error: |
| 1700 | |
| 1701 | ly_mnode_free(retval); |
| 1702 | |
| 1703 | return NULL; |
| 1704 | } |
| 1705 | |
| 1706 | static struct ly_mnode *read_yin_list(struct ly_module *module, |
| 1707 | struct ly_mnode *parent, |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1708 | struct lyxml_elem *yin, |
| 1709 | int resolve) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1710 | { |
| 1711 | struct ly_mnode *retval, *mnode; |
| 1712 | struct ly_mnode_list *list; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1713 | struct ly_unique *uniq_s; |
| 1714 | struct lyxml_elem *sub, *next, root = {0}, uniq = {0}; |
| 1715 | int i, r; |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1716 | size_t len; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1717 | int c_tpdf = 0, c_must = 0, c_uniq = 0; |
| 1718 | int f_ordr = 0, f_max = 0, f_min = 0; |
| 1719 | const char *key_str = NULL, *uniq_str, *value; |
| 1720 | char *auxs; |
| 1721 | unsigned long val; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1722 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1723 | list = calloc(1, sizeof *list); |
| 1724 | list->nodetype = LY_NODE_LIST; |
| 1725 | list->prev = (struct ly_mnode *)list; |
| 1726 | retval = (struct ly_mnode *)list; |
| 1727 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1728 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1729 | goto error; |
| 1730 | } |
| 1731 | |
| 1732 | /* process list's specific children */ |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1733 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1734 | /* data statements */ |
| 1735 | if (!strcmp(sub->name, "container") || |
| 1736 | !strcmp(sub->name, "leaf-list") || |
| 1737 | !strcmp(sub->name, "leaf") || |
| 1738 | !strcmp(sub->name, "list") || |
| 1739 | !strcmp(sub->name, "choice") || |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 1740 | !strcmp(sub->name, "uses") || |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1741 | !strcmp(sub->name, "grouping") || |
| 1742 | !strcmp(sub->name, "anyxml")) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1743 | lyxml_unlink_elem(sub); |
| 1744 | lyxml_add_child(&root, sub); |
| 1745 | |
| 1746 | /* array counters */ |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1747 | } else if (!strcmp(sub->name, "key")) { |
| 1748 | /* check cardinality 0..1 */ |
| 1749 | if (list->keys_size) { |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1750 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, list->name); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1751 | goto error; |
| 1752 | } |
| 1753 | |
| 1754 | /* count the number of keys */ |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1755 | GETVAL(value, sub, "value"); |
| 1756 | key_str = value; |
| 1757 | while((value = strpbrk(value, " \t\n"))) { |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1758 | list->keys_size++; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1759 | while(isspace(*value)) { |
| 1760 | value++; |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | list->keys_size++; |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1764 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1765 | } else if (!strcmp(sub->name, "unique")) { |
| 1766 | c_uniq++; |
| 1767 | lyxml_unlink_elem(sub); |
| 1768 | lyxml_add_child(&uniq, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1769 | } else if (!strcmp(sub->name, "typedef")) { |
| 1770 | c_tpdf++; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1771 | } else if (!strcmp(sub->name, "must")) { |
| 1772 | c_must++; |
| 1773 | |
| 1774 | /* optional stetments */ |
| 1775 | } else if (!strcmp(sub->name, "ordered-by")) { |
| 1776 | if (f_ordr) { |
| 1777 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1778 | goto error; |
| 1779 | } |
| 1780 | /* just checking the flags in llist is not sufficient, we would |
| 1781 | * allow multiple ordered-by statements with the "system" value |
| 1782 | */ |
| 1783 | f_ordr = 1; |
| 1784 | |
| 1785 | if (list->flags & LY_NODE_CONFIG_R) { |
| 1786 | /* RFC 6020, 7.7.5 - ignore ordering when the list represents |
| 1787 | * state data |
| 1788 | */ |
| 1789 | lyxml_free_elem(module->ctx, sub); |
| 1790 | continue; |
| 1791 | } |
| 1792 | |
| 1793 | GETVAL(value, sub, "value"); |
| 1794 | if (!strcmp(value, "user")) { |
| 1795 | list->flags |= LY_NODE_USERORDERED; |
| 1796 | } else if (strcmp(value, "system")) { |
| 1797 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1798 | goto error; |
| 1799 | } /* else system is the default value, so we can ignore it */ |
| 1800 | |
| 1801 | lyxml_free_elem(module->ctx, sub); |
| 1802 | } else if (!strcmp(sub->name, "min-elements")) { |
| 1803 | if (f_min) { |
| 1804 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1805 | goto error; |
| 1806 | } |
| 1807 | f_min = 1; |
| 1808 | |
| 1809 | GETVAL(value, sub, "value"); |
| 1810 | while(isspace(value[0])) { |
| 1811 | value++; |
| 1812 | } |
| 1813 | |
| 1814 | /* convert it to uint32_t */ |
| 1815 | errno = 0; |
| 1816 | auxs = NULL; |
| 1817 | val = strtoul(value, &auxs, 10); |
| 1818 | if (*auxs || value[0] == '-' || errno || val > UINT32_MAX) { |
| 1819 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1820 | goto error; |
| 1821 | } |
| 1822 | list->min = (uint32_t)val; |
| 1823 | lyxml_free_elem(module->ctx, sub); |
| 1824 | } else if (!strcmp(sub->name, "max-elements")) { |
| 1825 | if (f_max) { |
| 1826 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1827 | goto error; |
| 1828 | } |
| 1829 | f_max = 1; |
| 1830 | |
| 1831 | GETVAL(value, sub, "value"); |
| 1832 | while(isspace(value[0])) { |
| 1833 | value++; |
| 1834 | } |
| 1835 | |
| 1836 | /* convert it to uint32_t */ |
| 1837 | errno = 0; |
| 1838 | auxs = NULL; |
| 1839 | val = strtoul(value, &auxs, 10); |
| 1840 | if (*auxs || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) { |
| 1841 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1842 | goto error; |
| 1843 | } |
| 1844 | list->max = (uint32_t)val; |
| 1845 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1849 | /* check - if list is configuration, key statement is mandatory */ |
Radek Krejci | 3a734ed | 2015-05-26 15:23:18 +0200 | [diff] [blame] | 1850 | if ((list->flags & LY_NODE_CONFIG_W) && !key_str) { |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1851 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "key", "list"); |
| 1852 | goto error; |
| 1853 | } |
| 1854 | if (list->max && list->min > list->max) { |
| 1855 | LOGVAL(VE_SPEC, LOGLINE(yin), "\"min-elements\" is bigger than \"max-elements\"."); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1856 | goto error; |
| 1857 | } |
| 1858 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1859 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 1860 | if (c_tpdf) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1861 | list->tpdf = calloc(c_tpdf, sizeof *list->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1862 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1863 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1864 | if (!strcmp(sub->name, "typedef")) { |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1865 | r = fill_yin_typedef(module, retval, sub, &list->tpdf[list->tpdf_size]); |
| 1866 | list->tpdf_size++; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1867 | |
| 1868 | if (r) { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1869 | goto error; |
| 1870 | } |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1871 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1872 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1873 | } |
| 1874 | |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1875 | /* process unique statements */ |
| 1876 | if (c_uniq) { |
| 1877 | list->unique = calloc(c_uniq, sizeof *list->unique); |
| 1878 | } |
| 1879 | LY_TREE_FOR_SAFE(uniq.child, next, sub) { |
| 1880 | /* count the number of unique values */ |
| 1881 | GETVAL(value, sub, "value"); |
| 1882 | uniq_str = value; |
| 1883 | uniq_s = &list->unique[list->unique_size]; |
| 1884 | while((value = strpbrk(value, " \t\n"))) { |
| 1885 | uniq_s->leafs_size++; |
| 1886 | while(isspace(*value)) { |
| 1887 | value++; |
| 1888 | } |
| 1889 | } |
| 1890 | uniq_s->leafs_size++; |
| 1891 | uniq_s->leafs = calloc(uniq_s->leafs_size, sizeof *uniq_s->leafs); |
| 1892 | list->unique_size++; |
| 1893 | |
| 1894 | /* interconnect unique values with the leafs */ |
| 1895 | /* TODO - include searching in uses/grouping */ |
| 1896 | for (i = 0; i < uniq_s->leafs_size; i++) { |
| 1897 | if ((value = strpbrk(uniq_str, " \t\n"))) { |
| 1898 | len = value - uniq_str; |
| 1899 | while(isspace(*value)) { |
| 1900 | value++; |
| 1901 | } |
| 1902 | } else { |
| 1903 | len = strlen(uniq_str); |
| 1904 | } |
| 1905 | LY_TREE_FOR(list->child, mnode) { |
| 1906 | if (!strncmp(mnode->name, uniq_str, len) && !mnode->name[len]) { |
| 1907 | uniq_s->leafs[i] = (struct ly_mnode_leaf *)mnode; |
| 1908 | break; |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | if (check_key(uniq_s->leafs[i], list->flags, uniq_s->leafs, i, LOGLINE(yin), uniq_str, len)) { |
| 1913 | goto error; |
| 1914 | } |
| 1915 | |
| 1916 | /* prepare for next iteration */ |
| 1917 | while (value && isspace(*value)) { |
| 1918 | value++; |
| 1919 | } |
| 1920 | uniq_str = value; |
| 1921 | } |
| 1922 | } |
| 1923 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1924 | /* last part - process data nodes */ |
| 1925 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 1926 | if (!strcmp(sub->name, "container")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1927 | mnode = read_yin_container(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1928 | } else if (!strcmp(sub->name, "leaf-list")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1929 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1930 | } else if (!strcmp(sub->name, "leaf")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1931 | mnode = read_yin_leaf(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1932 | } else if (!strcmp(sub->name, "list")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1933 | mnode = read_yin_list(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1934 | } else if (!strcmp(sub->name, "choice")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1935 | mnode = read_yin_choice(module, retval, sub, resolve); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 1936 | } else if (!strcmp(sub->name, "uses")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1937 | mnode = read_yin_uses(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1938 | } else if (!strcmp(sub->name, "grouping")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1939 | mnode = read_yin_grouping(module, retval, sub, resolve); |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 1940 | } else if (!strcmp(sub->name, "anyxml")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 1941 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1942 | } else { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1943 | /* TODO error */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1944 | continue; |
| 1945 | } |
| 1946 | lyxml_free_elem(module->ctx, sub); |
| 1947 | |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1948 | if (!mnode) { |
| 1949 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1953 | if (parent) { |
| 1954 | ly_mnode_addchild(parent, retval); |
| 1955 | } |
| 1956 | |
Radek Krejci | 3a734ed | 2015-05-26 15:23:18 +0200 | [diff] [blame] | 1957 | if (!key_str) { |
| 1958 | /* config false list without a key */ |
| 1959 | return retval; |
| 1960 | } |
| 1961 | |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1962 | /* link key leafs into the list structure and check all constraints */ |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1963 | for (i = 0; i < list->keys_size; i++) { |
| 1964 | /* get the key name */ |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1965 | if ((value = strpbrk(key_str, " \t\n"))) { |
| 1966 | len = value - key_str; |
| 1967 | while(isspace(*value)) { |
| 1968 | value++; |
| 1969 | } |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1970 | } else { |
| 1971 | len = strlen(key_str); |
| 1972 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1973 | |
| 1974 | list->keys[i] = find_leaf(retval, key_str, len); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1975 | |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1976 | if (check_key(list->keys[i], list->flags, list->keys, i, LOGLINE(yin), key_str, len)) { |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1977 | goto error; |
| 1978 | } |
| 1979 | |
| 1980 | /* prepare for next iteration */ |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1981 | while (value && isspace(*value)) { |
| 1982 | value++; |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1983 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 1984 | key_str = value; |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1985 | } |
| 1986 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1987 | return retval; |
| 1988 | |
| 1989 | error: |
| 1990 | |
| 1991 | ly_mnode_free(retval); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1992 | while(root.child) { |
| 1993 | lyxml_free_elem(module->ctx, root.child); |
| 1994 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1995 | |
| 1996 | return NULL; |
| 1997 | } |
| 1998 | |
| 1999 | static struct ly_mnode *read_yin_container(struct ly_module *module, |
| 2000 | struct ly_mnode *parent, |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2001 | struct lyxml_elem *yin, |
| 2002 | int resolve) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2003 | { |
| 2004 | struct lyxml_elem *sub, *next, root = {0}; |
| 2005 | struct ly_mnode *mnode = NULL; |
| 2006 | struct ly_mnode *retval; |
| 2007 | struct ly_mnode_container *cont; |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2008 | const char *value; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2009 | int r; |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2010 | int c_tpdf = 0, c_must = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2011 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2012 | cont = calloc(1, sizeof *cont); |
| 2013 | cont->nodetype = LY_NODE_CONTAINER; |
| 2014 | cont->prev = (struct ly_mnode *)cont; |
| 2015 | retval = (struct ly_mnode *)cont; |
| 2016 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2017 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2018 | goto error; |
| 2019 | } |
| 2020 | |
| 2021 | /* process container's specific children */ |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2022 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2023 | if (!strcmp(sub->name, "presence")) { |
| 2024 | if (cont->presence) { |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2025 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2026 | goto error; |
| 2027 | } |
| 2028 | GETVAL(value, sub, "value"); |
| 2029 | cont->presence = lydict_insert(module->ctx, value, strlen(value)); |
| 2030 | |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2031 | lyxml_free_elem(module->ctx, sub); |
| 2032 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2033 | /* data statements */ |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2034 | } else if (!strcmp(sub->name, "container") || |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2035 | !strcmp(sub->name, "leaf-list") || |
| 2036 | !strcmp(sub->name, "leaf") || |
| 2037 | !strcmp(sub->name, "list") || |
| 2038 | !strcmp(sub->name, "choice") || |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2039 | !strcmp(sub->name, "uses") || |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2040 | !strcmp(sub->name, "grouping")|| |
| 2041 | !strcmp(sub->name, "anyxml")) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2042 | lyxml_unlink_elem(sub); |
| 2043 | lyxml_add_child(&root, sub); |
| 2044 | |
| 2045 | /* array counters */ |
| 2046 | } else if (!strcmp(sub->name, "typedef")) { |
| 2047 | c_tpdf++; |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2048 | } else if (!strcmp(sub->name, "must")) { |
| 2049 | c_must++; |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 2050 | #if 0 |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2051 | } else { |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2052 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 2053 | goto error; |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 2054 | #else |
| 2055 | } else { |
| 2056 | continue; |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 2057 | #endif |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 2062 | if (c_tpdf) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2063 | cont->tpdf = calloc(c_tpdf, sizeof *cont->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2064 | } |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2065 | if (c_must) { |
| 2066 | cont->must = calloc(c_must, sizeof *cont->must); |
| 2067 | } |
| 2068 | |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2069 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2070 | if (!strcmp(sub->name, "typedef")) { |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2071 | r = fill_yin_typedef(module, retval, sub, &cont->tpdf[cont->tpdf_size]); |
| 2072 | cont->tpdf_size++; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2073 | |
| 2074 | if (r) { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2075 | goto error; |
| 2076 | } |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2077 | } else if (!strcmp(sub->name, "must")) { |
| 2078 | r = fill_yin_must(module, sub, &cont->must[cont->must_size]); |
| 2079 | cont->must_size++; |
| 2080 | |
| 2081 | if (r) { |
| 2082 | goto error; |
| 2083 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | lyxml_free_elem(module->ctx, sub); |
| 2087 | } |
| 2088 | |
| 2089 | /* last part - process data nodes */ |
| 2090 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 2091 | if (!strcmp(sub->name, "container")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2092 | mnode = read_yin_container(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2093 | } else if (!strcmp(sub->name, "leaf-list")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2094 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2095 | } else if (!strcmp(sub->name, "leaf")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2096 | mnode = read_yin_leaf(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2097 | } else if (!strcmp(sub->name, "list")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2098 | mnode = read_yin_list(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2099 | } else if (!strcmp(sub->name, "choice")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2100 | mnode = read_yin_choice(module, retval, sub, resolve); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2101 | } else if (!strcmp(sub->name, "uses")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2102 | mnode = read_yin_uses(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2103 | } else if (!strcmp(sub->name, "grouping")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2104 | mnode = read_yin_grouping(module, retval, sub, resolve); |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2105 | } else if (!strcmp(sub->name, "anyxml")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2106 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2107 | } |
| 2108 | lyxml_free_elem(module->ctx, sub); |
| 2109 | |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2110 | if (!mnode) { |
| 2111 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2115 | if (parent) { |
| 2116 | ly_mnode_addchild(parent, retval); |
| 2117 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2118 | |
| 2119 | return retval; |
| 2120 | |
| 2121 | error: |
| 2122 | |
| 2123 | ly_mnode_free(retval); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2124 | while (root.child) { |
| 2125 | lyxml_free_elem(module->ctx, root.child); |
| 2126 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2127 | |
| 2128 | return NULL; |
| 2129 | } |
| 2130 | |
| 2131 | static struct ly_mnode *read_yin_grouping(struct ly_module *module, |
| 2132 | struct ly_mnode *parent, |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2133 | struct lyxml_elem *node, |
| 2134 | int resolve) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2135 | { |
| 2136 | struct lyxml_elem *sub, *next, root = {0}; |
| 2137 | struct ly_mnode *mnode = NULL; |
| 2138 | struct ly_mnode *retval; |
| 2139 | struct ly_mnode_grp *grp; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2140 | int r; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2141 | int c_tpdf = 0; |
| 2142 | |
| 2143 | grp = calloc(1, sizeof *grp); |
| 2144 | grp->nodetype = LY_NODE_GROUPING; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2145 | grp->prev = (struct ly_mnode *)grp; |
| 2146 | retval = (struct ly_mnode *)grp; |
| 2147 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2148 | if (read_yin_common(module, parent, retval, node, OPT_IDENT | OPT_MODULE)) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2149 | goto error; |
| 2150 | } |
| 2151 | |
| 2152 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 2153 | /* data statements */ |
| 2154 | if (!strcmp(sub->name, "container") || |
| 2155 | !strcmp(sub->name, "leaf-list") || |
| 2156 | !strcmp(sub->name, "leaf") || |
| 2157 | !strcmp(sub->name, "list") || |
| 2158 | !strcmp(sub->name, "choice") || |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2159 | !strcmp(sub->name, "uses") || |
Radek Krejci | 77d1dc6 | 2015-06-03 16:18:07 +0200 | [diff] [blame] | 2160 | !strcmp(sub->name, "grouping") || |
| 2161 | !strcmp(sub->name, "anyxml")) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2162 | lyxml_unlink_elem(sub); |
| 2163 | lyxml_add_child(&root, sub); |
| 2164 | |
| 2165 | /* array counters */ |
| 2166 | } else if (!strcmp(sub->name, "typedef")) { |
| 2167 | c_tpdf++; |
Radek Krejci | 77d1dc6 | 2015-06-03 16:18:07 +0200 | [diff] [blame] | 2168 | } else { |
| 2169 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 2170 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 2175 | if (c_tpdf) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2176 | grp->tpdf = calloc(c_tpdf, sizeof *grp->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2177 | } |
| 2178 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 2179 | if (!strcmp(sub->name, "typedef")) { |
Radek Krejci | 77d1dc6 | 2015-06-03 16:18:07 +0200 | [diff] [blame] | 2180 | r = fill_yin_typedef(module, retval, sub, &grp->tpdf[grp->tpdf_size]); |
| 2181 | grp->tpdf_size++; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2182 | |
| 2183 | if (r) { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2184 | goto error; |
| 2185 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | lyxml_free_elem(module->ctx, sub); |
| 2189 | } |
| 2190 | |
| 2191 | /* last part - process data nodes */ |
| 2192 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 2193 | if (!strcmp(sub->name, "container")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2194 | mnode = read_yin_container(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2195 | } else if (!strcmp(sub->name, "leaf-list")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2196 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2197 | } else if (!strcmp(sub->name, "leaf")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2198 | mnode = read_yin_leaf(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2199 | } else if (!strcmp(sub->name, "list")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2200 | mnode = read_yin_list(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2201 | } else if (!strcmp(sub->name, "choice")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2202 | mnode = read_yin_choice(module, retval, sub, resolve); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2203 | } else if (!strcmp(sub->name, "uses")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2204 | mnode = read_yin_uses(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2205 | } else if (!strcmp(sub->name, "grouping")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2206 | mnode = read_yin_grouping(module, retval, sub, resolve); |
Radek Krejci | 77d1dc6 | 2015-06-03 16:18:07 +0200 | [diff] [blame] | 2207 | } else if (!strcmp(sub->name, "anyxml")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2208 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2209 | } |
| 2210 | lyxml_free_elem(module->ctx, sub); |
| 2211 | |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2212 | if (!mnode) { |
| 2213 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2217 | if (parent) { |
| 2218 | ly_mnode_addchild(parent, retval); |
| 2219 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2220 | |
| 2221 | return retval; |
| 2222 | |
| 2223 | error: |
| 2224 | |
| 2225 | ly_mnode_free(retval); |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2226 | while (root.child) { |
| 2227 | lyxml_free_elem(module->ctx, root.child); |
| 2228 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2229 | |
| 2230 | return NULL; |
| 2231 | } |
| 2232 | |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2233 | /* |
| 2234 | * resolve - referenced grouping should be bounded to the namespace (resolved) |
| 2235 | * only when uses does not appear in grouping. In a case of grouping's uses, |
| 2236 | * we just get information but we do not apply augment or refine to it. |
| 2237 | */ |
| 2238 | static struct ly_mnode *read_yin_uses(struct ly_module *module, |
| 2239 | struct ly_mnode *parent, |
| 2240 | struct lyxml_elem *node, int resolve) |
| 2241 | { |
| 2242 | struct ly_mnode *retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2243 | struct ly_mnode *mnode = NULL, *mnode_aux; |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2244 | struct ly_mnode_uses *uses; |
| 2245 | struct ly_module *searchmod = NULL; |
| 2246 | const char *name; |
| 2247 | int prefix_len = 0; |
| 2248 | int i; |
| 2249 | |
| 2250 | uses = calloc(1, sizeof *uses); |
| 2251 | uses->nodetype = LY_NODE_USES; |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2252 | uses->prev = (struct ly_mnode *)uses; |
| 2253 | retval = (struct ly_mnode *)uses; |
| 2254 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2255 | if (read_yin_common(module, parent, retval, node, OPT_MODULE | (resolve ? OPT_INHERIT : 0))) { |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2256 | goto error; |
| 2257 | } |
| 2258 | |
| 2259 | /* get referenced grouping */ |
| 2260 | name = strchr(uses->name, ':'); |
| 2261 | if (!name) { |
| 2262 | /* no prefix, search in local tree */ |
| 2263 | name = uses->name; |
| 2264 | } else { |
| 2265 | /* there is some prefix, check if it refer the same data model */ |
| 2266 | |
| 2267 | /* set name to correct position after colon */ |
| 2268 | prefix_len = name - uses->name; |
| 2269 | name++; |
| 2270 | |
| 2271 | if (!strncmp(uses->name, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 2272 | /* prefix refers to the current module, ignore it */ |
| 2273 | prefix_len = 0; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | /* search */ |
| 2278 | if (prefix_len) { |
| 2279 | /* in top-level groupings of some other module */ |
| 2280 | for (i = 0; i < module->imp_size; i++) { |
| 2281 | if (!strncmp(module->imp[i].prefix, uses->name, prefix_len) |
| 2282 | && !module->imp[i].prefix[prefix_len]) { |
| 2283 | searchmod = module->imp[i].module; |
| 2284 | break; |
| 2285 | } |
| 2286 | } |
| 2287 | if (!searchmod) { |
| 2288 | /* uses refers unknown data model */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2289 | LOGVAL(VE_INPREFIX, LOGLINE(node), name); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2290 | goto error; |
| 2291 | } |
| 2292 | |
Radek Krejci | a1e97df | 2015-06-03 11:33:40 +0200 | [diff] [blame] | 2293 | LY_TREE_FOR(searchmod->data, mnode) { |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2294 | if (mnode->nodetype == LY_NODE_GROUPING && !strcmp(mnode->name, name)) { |
| 2295 | uses->grp = (struct ly_mnode_grp *)mnode; |
| 2296 | break; |
| 2297 | } |
| 2298 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2299 | } else { |
| 2300 | /* in local tree hierarchy */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2301 | for(mnode_aux = parent; mnode_aux; mnode_aux = mnode_aux->parent) { |
| 2302 | LY_TREE_FOR(mnode_aux->child, mnode) { |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2303 | if (mnode->nodetype == LY_NODE_GROUPING && !strcmp(mnode->name, name)) { |
| 2304 | uses->grp = (struct ly_mnode_grp *)mnode; |
| 2305 | break; |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | /* search in top level of the current module */ |
| 2311 | LY_TREE_FOR(module->data, mnode) { |
| 2312 | if (mnode->nodetype == LY_NODE_GROUPING && !strcmp(mnode->name, name)) { |
| 2313 | uses->grp = (struct ly_mnode_grp *)mnode; |
| 2314 | break; |
| 2315 | } |
| 2316 | } |
| 2317 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2318 | if (!uses->grp) { |
| 2319 | LOGVAL(VE_INARG, LOGLINE(node), uses->name, "uses"); |
| 2320 | goto error; |
| 2321 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2322 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2323 | if (parent) { |
| 2324 | ly_mnode_addchild(parent, retval); |
| 2325 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2326 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2327 | if (resolve) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2328 | /* inherit config flag */ |
| 2329 | if (parent) { |
| 2330 | retval->flags |= parent->flags & LY_NODE_CONFIG_MASK; |
| 2331 | } else { |
| 2332 | /* default config is true */ |
| 2333 | retval->flags |= LY_NODE_CONFIG_W; |
| 2334 | } |
| 2335 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2336 | /* copy the data nodes from grouping into the uses context */ |
| 2337 | if (resolve_uses(uses)) { |
| 2338 | goto error; |
| 2339 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2340 | } |
| 2341 | |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2342 | return retval; |
| 2343 | |
| 2344 | error: |
| 2345 | |
| 2346 | ly_mnode_free(retval); |
| 2347 | |
| 2348 | return NULL; |
| 2349 | } |
| 2350 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2351 | /* common code for yin_read_module() and yin_read_submodule() */ |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2352 | static int read_sub_module(struct ly_module *module, struct lyxml_elem *yin) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2353 | { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2354 | struct ly_ctx *ctx = module->ctx; |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2355 | struct ly_submodule *submodule = (struct ly_submodule *)module; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2356 | struct lyxml_elem *next, *node, *child, root = {0}; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2357 | struct ly_mnode *mnode = NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2358 | const char *value; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2359 | int c_imp = 0, c_rev = 0, c_tpdf = 0, c_ident = 0, c_inc = 0; /* counters */ |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2360 | int r; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2361 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2362 | |
| 2363 | /* |
| 2364 | * in the first run, we process elements with cardinality of 1 or 0..1 and |
| 2365 | * count elements with cardinality 0..n. Data elements (choices, containers, |
| 2366 | * leafs, lists, leaf-lists) are moved aside to be processed last, since we |
| 2367 | * need have all top-level and groupings already prepared at that time. In |
| 2368 | * the middle loop, we process other elements with carinality of 0..n since |
| 2369 | * we need to allocate arrays to store them. |
| 2370 | */ |
| 2371 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 2372 | if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) { |
| 2373 | lyxml_free_elem(ctx, node); |
| 2374 | continue; |
| 2375 | } |
| 2376 | |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2377 | if (!module->type && !strcmp(node->name, "namespace")) { |
| 2378 | if (module->ns) { |
| 2379 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 2380 | goto error; |
| 2381 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2382 | GETVAL(value, node, "uri"); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2383 | module->ns = lydict_insert(ctx, value, strlen(value)); |
| 2384 | lyxml_free_elem(ctx, node); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2385 | } else if (!module->type && !strcmp(node->name, "prefix")) { |
| 2386 | if (module->prefix) { |
| 2387 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 2388 | goto error; |
| 2389 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2390 | GETVAL(value, node, "value"); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2391 | if (check_identifier(value, LY_IDENT_PREFIX, LOGLINE(node), module, NULL)) { |
| 2392 | goto error; |
| 2393 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2394 | module->prefix = lydict_insert(ctx, value, strlen(value)); |
| 2395 | lyxml_free_elem(ctx, node); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2396 | } else if (module->type && !strcmp(node->name, "belongs-to")) { |
| 2397 | if (submodule->belongsto) { |
| 2398 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 2399 | goto error; |
| 2400 | } |
| 2401 | GETVAL(value, node, "module"); |
| 2402 | submodule->belongsto = ly_ctx_get_module(module->ctx, value, NULL, 0); |
| 2403 | if (!submodule->belongsto) { |
| 2404 | LOGVAL(VE_INARG, LOGLINE(node), value, node->name); |
| 2405 | goto error; |
| 2406 | } |
| 2407 | /* get the prefix substatement, start with checks */ |
| 2408 | if (!node->child) { |
| 2409 | LOGVAL(VE_MISSSTMT2, LOGLINE(node), "prefix", node->name); |
| 2410 | goto error; |
| 2411 | } else if (strcmp(node->child->name, "prefix")) { |
| 2412 | LOGVAL(VE_INSTMT, LOGLINE(node->child), node->child->name); |
| 2413 | goto error; |
| 2414 | } else if (node->child->next) { |
| 2415 | LOGVAL(VE_INSTMT, LOGLINE(node->child->next), node->child->next->name); |
| 2416 | goto error; |
| 2417 | } |
| 2418 | /* and now finally get the value */ |
| 2419 | GETVAL(value, node->child, "value"); |
| 2420 | /* check here differs from a generic prefix check, since this prefix |
| 2421 | * don't have to be unique |
| 2422 | */ |
| 2423 | if (check_identifier(value, LY_IDENT_NAME, LOGLINE(node->child), NULL, NULL)) { |
| 2424 | goto error; |
| 2425 | } |
| 2426 | module->prefix = lydict_insert(ctx, value, strlen(value)); |
| 2427 | |
| 2428 | /* we are done with belongs-to */ |
| 2429 | lyxml_free_elem(ctx, node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2430 | } else if (!strcmp(node->name, "import")) { |
| 2431 | c_imp++; |
| 2432 | } else if (!strcmp(node->name, "revision")) { |
| 2433 | c_rev++; |
| 2434 | } else if (!strcmp(node->name, "typedef")) { |
| 2435 | c_tpdf++; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2436 | } else if (!strcmp(node->name, "identity")) { |
| 2437 | c_ident++; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2438 | } else if (!strcmp(node->name, "include")) { |
| 2439 | c_inc++; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2440 | |
| 2441 | /* data statements */ |
| 2442 | } else if (!strcmp(node->name, "container") || |
| 2443 | !strcmp(node->name, "leaf-list") || |
| 2444 | !strcmp(node->name, "leaf") || |
| 2445 | !strcmp(node->name, "list") || |
| 2446 | !strcmp(node->name, "choice") || |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2447 | !strcmp(node->name, "uses") || |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2448 | !strcmp(node->name, "grouping") || |
| 2449 | !strcmp(node->name, "anyxml")) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2450 | lyxml_unlink_elem(node); |
| 2451 | lyxml_add_child(&root, node); |
| 2452 | |
| 2453 | /* optional statements */ |
| 2454 | } else if (!strcmp(node->name, "description")) { |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2455 | if (module->dsc) { |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2456 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2457 | goto error; |
| 2458 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 2459 | module->dsc = read_yin_subnode(ctx, node, "text"); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2460 | lyxml_free_elem(ctx, node); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2461 | if (!module->dsc) { |
| 2462 | goto error; |
| 2463 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2464 | } else if (!strcmp(node->name, "reference")) { |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2465 | if (module->ref) { |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2466 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2467 | goto error; |
| 2468 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 2469 | module->ref = read_yin_subnode(ctx, node, "text"); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2470 | lyxml_free_elem(ctx, node); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2471 | if (!module->ref) { |
| 2472 | goto error; |
| 2473 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2474 | } else if (!strcmp(node->name, "organization")) { |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2475 | if (module->org) { |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2476 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2477 | goto error; |
| 2478 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 2479 | module->org = read_yin_subnode(ctx, node, "text"); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2480 | lyxml_free_elem(ctx, node); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2481 | if (!module->org) { |
| 2482 | goto error; |
| 2483 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2484 | } else if (!strcmp(node->name, "contact")) { |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2485 | if (module->contact) { |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2486 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2487 | goto error; |
| 2488 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 2489 | module->contact = read_yin_subnode(ctx, node, "text"); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2490 | lyxml_free_elem(ctx, node); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2491 | if (!module->contact) { |
| 2492 | goto error; |
| 2493 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2494 | } else if (!strcmp(node->name, "yang-version")) { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2495 | /* TODO: support YANG 1.1 ? */ |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2496 | if (module->version) { |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2497 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2498 | goto error; |
| 2499 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2500 | GETVAL(value, node, "value"); |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2501 | if (strcmp(value, "1")) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2502 | LOGVAL(VE_INARG, LOGLINE(node), value, "yang-version"); |
Radek Krejci | b0594bf | 2015-05-21 23:51:27 +0200 | [diff] [blame] | 2503 | goto error; |
| 2504 | } |
| 2505 | module->version = 1; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2506 | lyxml_free_elem(ctx, node); |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 2507 | #if 0 |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2508 | } else { |
| 2509 | LOGVAL(VE_INSTMT, LOGLINE(node), node->name); |
| 2510 | goto error; |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 2511 | #else |
| 2512 | } else { |
| 2513 | continue; |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 2514 | #endif |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2518 | if (!submodule) { |
| 2519 | /* check for mandatory statements */ |
| 2520 | if (!module->ns) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2521 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "namespace", "module"); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2522 | goto error; |
| 2523 | } |
| 2524 | if (!module->prefix) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2525 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "prefix", "module"); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2526 | goto error; |
| 2527 | } |
Radek Krejci | bb3257d | 2015-05-21 23:03:51 +0200 | [diff] [blame] | 2528 | } |
Radek Krejci | bb3257d | 2015-05-21 23:03:51 +0200 | [diff] [blame] | 2529 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2530 | /* allocate arrays for elements with cardinality of 0..n */ |
| 2531 | if (c_imp) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2532 | module->imp = calloc(c_imp, sizeof *module->imp); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2533 | } |
| 2534 | if (c_rev) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2535 | module->rev = calloc(c_rev, sizeof *module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2536 | } |
| 2537 | if (c_tpdf) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2538 | module->tpdf = calloc(c_tpdf, sizeof *module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2539 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2540 | if (c_ident) { |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2541 | module->ident = calloc(c_ident, sizeof *module->ident); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2542 | } |
| 2543 | if (c_inc) { |
| 2544 | module->inc = calloc(c_inc, sizeof *module->inc); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2545 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2546 | |
| 2547 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 2548 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 2549 | if (!strcmp(node->name, "import")) { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2550 | r = fill_yin_import(module, node, &module->imp[module->imp_size]); |
| 2551 | module->imp_size++; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2552 | if (r) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2553 | goto error; |
| 2554 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2555 | |
| 2556 | /* check duplicities in imported modules */ |
| 2557 | for (i = 0; i < module->imp_size - 1; i++) { |
| 2558 | if (!strcmp(module->imp[i].module->name, module->imp[module->imp_size - 1].module->name)) { |
| 2559 | LOGVAL(VE_SPEC, LOGLINE(node), "Importing module \"%s\" repeatedly.", module->imp[i].module->name); |
| 2560 | goto error; |
| 2561 | } |
| 2562 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2563 | } else if (!strcmp(node->name, "include")) { |
| 2564 | r = fill_yin_include(module, node, &module->inc[module->inc_size]); |
| 2565 | module->inc_size++; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2566 | if (r) { |
| 2567 | goto error; |
| 2568 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2569 | |
| 2570 | /* check duplications in include submodules */ |
| 2571 | for (i = 0; i < module->inc_size - 1; i++) { |
Radek Krejci | 76e5a1b | 2015-05-29 17:01:08 +0200 | [diff] [blame] | 2572 | if (!strcmp(module->inc[i].submodule->name, module->inc[module->inc_size - 1].submodule->name)) { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2573 | LOGVAL(VE_SPEC, LOGLINE(node), "Importing module \"%s\" repeatedly.", module->inc[i].submodule->name); |
| 2574 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2575 | } |
| 2576 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2577 | } else if (!strcmp(node->name, "revision")) { |
| 2578 | GETVAL(value, node, "date"); |
| 2579 | if (check_date(value, LOGLINE(node))) { |
| 2580 | goto error; |
| 2581 | } |
| 2582 | memcpy(module->rev[module->rev_size].date, value, LY_REV_SIZE - 1); |
| 2583 | /* check uniqueness of the revision date - not required by RFC */ |
| 2584 | for (i = 0; i < module->rev_size; i++) { |
| 2585 | if (!strcmp(value, module->rev[i].date)) { |
| 2586 | LOGVAL(VE_INARG, LOGLINE(node), value, node->name); |
| 2587 | LOGVAL(VE_SPEC, 0, "Revision is not unique."); |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | LY_TREE_FOR(node->child, child) { |
| 2592 | if (!strcmp(child->name, "description")) { |
| 2593 | if (module->rev[module->rev_size].dsc) { |
| 2594 | LOGVAL(VE_TOOMANY, LOGLINE(node), child->name, node->name); |
| 2595 | goto error; |
| 2596 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 2597 | module->rev[module->rev_size].dsc = read_yin_subnode(ctx, child, "text"); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2598 | if (!module->rev[module->rev_size].dsc) { |
| 2599 | goto error; |
| 2600 | } |
| 2601 | } else if (!strcmp(child->name, "reference")) { |
| 2602 | if (module->rev[module->rev_size].ref) { |
| 2603 | LOGVAL(VE_TOOMANY, LOGLINE(node), child->name, node->name); |
| 2604 | goto error; |
| 2605 | } |
Radek Krejci | b832928 | 2015-06-04 11:04:08 +0200 | [diff] [blame] | 2606 | module->rev[module->rev_size].ref = read_yin_subnode(ctx, child, "text"); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2607 | if (!module->rev[module->rev_size].ref) { |
| 2608 | goto error; |
| 2609 | } |
| 2610 | } else { |
| 2611 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 2612 | goto error; |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | /* keep the latest revision at position 0 */ |
| 2617 | if (module->rev_size && strcmp(module->rev[module->rev_size].date, module->rev[0].date) > 0) { |
| 2618 | /* switch their position */ |
| 2619 | value = strdup(module->rev[0].date); |
| 2620 | memcpy(module->rev[0].date, module->rev[module->rev_size].date, LY_REV_SIZE - 1); |
| 2621 | memcpy(module->rev[module->rev_size].date, value, LY_REV_SIZE - 1); |
| 2622 | free((char*)value); |
| 2623 | |
| 2624 | if (module->rev[0].dsc != module->rev[module->rev_size].dsc) { |
| 2625 | value = module->rev[0].dsc; |
| 2626 | module->rev[0].dsc = module->rev[module->rev_size].dsc; |
| 2627 | module->rev[module->rev_size].dsc = value; |
| 2628 | } |
| 2629 | |
| 2630 | if (module->rev[0].ref != module->rev[module->rev_size].ref) { |
| 2631 | value = module->rev[0].ref; |
| 2632 | module->rev[0].ref = module->rev[module->rev_size].ref; |
| 2633 | module->rev[module->rev_size].ref = value; |
| 2634 | } |
| 2635 | } |
| 2636 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2637 | module->rev_size++; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2638 | } else if (!strcmp(node->name, "typedef")) { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2639 | r = fill_yin_typedef(module, NULL, node, &module->tpdf[module->tpdf_size]); |
| 2640 | module->tpdf_size++; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2641 | |
| 2642 | if (r) { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2643 | goto error; |
| 2644 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2645 | } else if (!strcmp(node->name, "identity")) { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2646 | r = fill_yin_identity(module, node, &module->ident[module->ident_size]); |
| 2647 | module->ident_size++; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2648 | |
| 2649 | if (r) { |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2650 | goto error; |
| 2651 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | lyxml_free_elem(ctx, node); |
| 2655 | } |
| 2656 | |
| 2657 | /* last part - process data nodes */ |
| 2658 | LY_TREE_FOR_SAFE(root.child, next, node) { |
| 2659 | |
| 2660 | if (!strcmp(node->name, "container")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2661 | mnode = read_yin_container(module, NULL, node, 1); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2662 | } else if (!strcmp(node->name, "leaf-list")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2663 | mnode = read_yin_leaflist(module, NULL, node, 1); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2664 | } else if (!strcmp(node->name, "leaf")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2665 | mnode = read_yin_leaf(module, NULL, node, 1); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2666 | } else if (!strcmp(node->name, "list")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2667 | mnode = read_yin_list(module, NULL, node, 1); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2668 | } else if (!strcmp(node->name, "choice")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2669 | mnode = read_yin_choice(module, NULL, node, 1); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2670 | } else if (!strcmp(node->name, "grouping")) { |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2671 | mnode = read_yin_grouping(module, NULL, node, 0); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 2672 | } else if (!strcmp(node->name, "uses")) { |
| 2673 | mnode = read_yin_uses(module, NULL, node, 1); |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2674 | } else if (!strcmp(node->name, "anyxml")) { |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame^] | 2675 | mnode = read_yin_anyxml(module, NULL, node, 1); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2676 | } else { |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2677 | /* TODO error */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2678 | continue; |
| 2679 | } |
| 2680 | lyxml_free_elem(ctx, node); |
| 2681 | |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2682 | if (!mnode) { |
| 2683 | goto error; |
| 2684 | } |
| 2685 | |
| 2686 | /* include data element */ |
| 2687 | if (module->data) { |
| 2688 | module->data->prev->next = mnode; |
| 2689 | mnode->prev = module->data->prev; |
| 2690 | module->data->prev = mnode; |
| 2691 | } else { |
| 2692 | module->data = mnode; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2696 | return EXIT_SUCCESS; |
| 2697 | |
| 2698 | error: |
| 2699 | /* cleanup */ |
| 2700 | while (root.child) { |
| 2701 | lyxml_free_elem(module->ctx, root.child); |
| 2702 | } |
| 2703 | |
| 2704 | return EXIT_FAILURE; |
| 2705 | } |
| 2706 | |
| 2707 | struct ly_submodule *yin_read_submodule(struct ly_module *module, const char *data) |
| 2708 | { |
| 2709 | struct lyxml_elem *yin; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2710 | struct ly_submodule *submodule = NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2711 | const char *value; |
| 2712 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2713 | assert(module->ctx); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2714 | |
| 2715 | yin = lyxml_read(module->ctx, data, 0); |
| 2716 | if (!yin) { |
| 2717 | return NULL; |
| 2718 | } |
| 2719 | |
| 2720 | /* check root element */ |
| 2721 | if (!yin->name || strcmp(yin->name, "submodule")) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2722 | LOGVAL(VE_INSTMT, LOGLINE(yin), yin->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2723 | goto error; |
| 2724 | } |
| 2725 | |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2726 | GETVAL(value, yin, "name"); |
| 2727 | if (check_identifier(value, LY_IDENT_NAME, LOGLINE(yin), NULL, NULL)) { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2728 | goto error; |
| 2729 | } |
| 2730 | |
| 2731 | submodule = calloc(1, sizeof *submodule); |
| 2732 | if (!submodule) { |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2733 | LOGMEM; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2734 | goto error; |
| 2735 | } |
| 2736 | |
| 2737 | submodule->ctx = module->ctx; |
| 2738 | submodule->name = lydict_insert(submodule->ctx, value, strlen(value)); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2739 | submodule->type = 1; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2740 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2741 | LOGVRB("reading submodule %s", submodule->name); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2742 | if (read_sub_module((struct ly_module *)submodule, yin)) { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2743 | goto error; |
| 2744 | } |
| 2745 | |
| 2746 | /* cleanup */ |
| 2747 | lyxml_free_elem(module->ctx, yin); |
| 2748 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2749 | LOGVRB("submodule %s successfully parsed", submodule->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2750 | |
| 2751 | return submodule; |
| 2752 | |
| 2753 | error: |
| 2754 | /* cleanup */ |
| 2755 | lyxml_free_elem(module->ctx, yin); |
| 2756 | ly_submodule_free(submodule); |
| 2757 | |
| 2758 | return NULL; |
| 2759 | } |
| 2760 | |
| 2761 | struct ly_module *yin_read_module(struct ly_ctx *ctx, const char *data) |
| 2762 | { |
| 2763 | struct lyxml_elem *yin; |
| 2764 | struct ly_module *module = NULL, **newlist = NULL; |
| 2765 | const char *value; |
| 2766 | int i; |
| 2767 | |
| 2768 | yin = lyxml_read(ctx, data, 0); |
| 2769 | if (!yin) { |
| 2770 | return NULL; |
| 2771 | } |
| 2772 | |
| 2773 | /* check root element */ |
| 2774 | if (!yin->name || strcmp(yin->name, "module")) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2775 | LOGVAL(VE_INSTMT, LOGLINE(yin), yin->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2776 | goto error; |
| 2777 | } |
| 2778 | |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2779 | GETVAL(value, yin, "name"); |
| 2780 | if (check_identifier(value, LY_IDENT_NAME, LOGLINE(yin), NULL, NULL)) { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2781 | goto error; |
| 2782 | } |
| 2783 | |
| 2784 | module = calloc(1, sizeof *module); |
| 2785 | if (!module) { |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2786 | LOGMEM; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2787 | goto error; |
| 2788 | } |
| 2789 | |
| 2790 | module->ctx = ctx; |
| 2791 | module->name = lydict_insert(ctx, value, strlen(value)); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2792 | module->type = 0; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2793 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2794 | LOGVRB("reading module %s", module->name); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 2795 | if (read_sub_module(module, yin)) { |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2796 | goto error; |
| 2797 | } |
| 2798 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2799 | /* add to the context's list of modules */ |
| 2800 | if (ctx->models.used == ctx->models.size) { |
| 2801 | newlist = realloc(ctx->models.list, ctx->models.size * 2); |
| 2802 | if (!newlist) { |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2803 | LOGMEM; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2804 | goto error; |
| 2805 | } |
| 2806 | for (i = ctx->models.size; i < ctx->models.size * 2; i++) { |
| 2807 | newlist[i] = NULL; |
| 2808 | } |
| 2809 | ctx->models.size *= 2; |
| 2810 | ctx->models.list = newlist; |
| 2811 | } |
Radek Krejci | f6e5e18 | 2015-05-27 17:18:07 +0200 | [diff] [blame] | 2812 | for (i = 0; ctx->models.list[i]; i++) { |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2813 | /* check name (name/revision) and namespace uniqueness */ |
Radek Krejci | f6e5e18 | 2015-05-27 17:18:07 +0200 | [diff] [blame] | 2814 | if (!strcmp(ctx->models.list[i]->name, module->name)) { |
| 2815 | if (!ctx->models.list[i]->rev_size && !module->rev_size) { |
| 2816 | /* both data models are same, with no revision specified */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2817 | LOGERR(LY_EINVAL, "Module \"%s\" (no revision in either of them specified) already in context.", module->name); |
Radek Krejci | f6e5e18 | 2015-05-27 17:18:07 +0200 | [diff] [blame] | 2818 | goto error; |
| 2819 | } else if (!ctx->models.list[i]->rev_size || !module->rev_size) { |
| 2820 | /* one of the models does not have a revision, so they differs */ |
| 2821 | continue; |
| 2822 | } else { |
| 2823 | /* both models have a revision statement which we have to |
| 2824 | * compare, revision at position 0 is the last revision |
| 2825 | */ |
| 2826 | if (!strcmp(ctx->models.list[i]->rev[0].date, module->rev[0].date)) { |
| 2827 | /* we have the same modules */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2828 | LOGERR(LY_EINVAL, "Module \"%s\", revision %s already in context.", module->name, module->rev[0].date); |
Radek Krejci | f6e5e18 | 2015-05-27 17:18:07 +0200 | [diff] [blame] | 2829 | goto error; |
| 2830 | } |
| 2831 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 2832 | } else if (!strcmp(ctx->models.list[i]->ns, module->ns)) { |
| 2833 | LOGERR(LY_EINVAL, "Two different modules (\"%s\" and \"%s\") have the same namespace \"%s\"", |
| 2834 | ctx->models.list[i]->name, module->name, module->ns); |
| 2835 | goto error; |
Radek Krejci | f6e5e18 | 2015-05-27 17:18:07 +0200 | [diff] [blame] | 2836 | } |
| 2837 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2838 | ctx->models.list[i] = module; |
| 2839 | ctx->models.used++; |
| 2840 | |
| 2841 | /* cleanup */ |
| 2842 | lyxml_free_elem(ctx, yin); |
| 2843 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2844 | LOGVRB("module %s successfully parsed", module->name); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2845 | |
| 2846 | return module; |
| 2847 | |
| 2848 | error: |
| 2849 | /* cleanup */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2850 | lyxml_free_elem(ctx, yin); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2851 | ly_module_free(module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2852 | |
| 2853 | return NULL; |
| 2854 | } |