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 { |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 41 | LY_IDENT_SIMPLE, /* only syntax rules */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 42 | LY_IDENT_FEATURE, |
| 43 | LY_IDENT_IDENTITY, |
| 44 | LY_IDENT_TYPE, |
| 45 | LY_IDENT_NODE, |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 46 | LY_IDENT_NAME, /* uniqueness across the siblings */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 47 | LY_IDENT_PREFIX |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Radek Krejci | efdd0ce | 2015-05-26 16:48:29 +0200 | [diff] [blame] | 50 | #define LY_NSYIN "urn:ietf:params:xml:ns:yang:yin:1" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 51 | |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 52 | #define GETVAL(value, node, arg) \ |
| 53 | value = lyxml_get_attr(node, arg, NULL); \ |
| 54 | if (!value) { \ |
| 55 | LOGVAL(VE_MISSARG, LOGLINE(node), arg, node->name); \ |
| 56 | goto error; \ |
| 57 | } |
| 58 | |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 59 | #define OPT_IDENT 0x01 |
| 60 | #define OPT_CONFIG 0x02 |
| 61 | #define OPT_MODULE 0x04 |
| 62 | #define OPT_INHERIT 0x08 |
| 63 | static int read_yin_common(struct ly_module *, struct ly_mnode *, struct ly_mnode *, struct lyxml_elem *, int); |
| 64 | |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 65 | struct mnode_list { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 66 | struct ly_mnode *mnode; |
| 67 | struct mnode_list *next; |
| 68 | unsigned int line; |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 71 | static struct ly_mnode *read_yin_choice(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, |
| 72 | int resolve, struct mnode_list **unres); |
| 73 | static struct ly_mnode *read_yin_case(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, |
| 74 | int resolve, struct mnode_list **unres); |
| 75 | static struct ly_mnode *read_yin_anyxml(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, |
| 76 | int resolve); |
| 77 | static struct ly_mnode *read_yin_container(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, |
| 78 | int resolve, struct mnode_list **unres); |
| 79 | static struct ly_mnode *read_yin_leaf(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, |
| 80 | int resolve); |
| 81 | static struct ly_mnode *read_yin_leaflist(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, |
| 82 | int resolve); |
| 83 | static struct ly_mnode *read_yin_list(struct ly_module *module,struct ly_mnode *parent, struct lyxml_elem *yin, |
| 84 | int resolve, struct mnode_list **unres); |
| 85 | static struct ly_mnode *read_yin_uses(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *node, |
| 86 | int resolve, struct mnode_list **unres); |
| 87 | static struct ly_mnode *read_yin_grouping(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *node, |
| 88 | int resolve, struct mnode_list **unres); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 89 | static struct ly_when *read_yin_when(struct ly_module *module,struct lyxml_elem *yin); |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 90 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 91 | static int |
| 92 | dup_typedef_check(const char *type, struct ly_tpdf *tpdf, int size) |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 93 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 94 | int i; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 95 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 96 | for (i = 0; i < size; i++) { |
| 97 | if (!strcmp(type, tpdf[i].name)) { |
| 98 | /* name collision */ |
| 99 | return EXIT_FAILURE; |
| 100 | } |
| 101 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 102 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 103 | return EXIT_SUCCESS; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Radek Krejci | b05774c | 2015-06-18 13:52:59 +0200 | [diff] [blame] | 106 | |
| 107 | static int |
| 108 | dup_feature_check(const char *id, struct ly_module *module) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | for (i = 0; i < module->features_size; i++) { |
| 113 | if (!strcmp(id, module->features[i].name)) { |
| 114 | return EXIT_FAILURE; |
| 115 | } |
| 116 | } |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 117 | |
| 118 | return EXIT_SUCCESS; |
Radek Krejci | b05774c | 2015-06-18 13:52:59 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 121 | static int |
| 122 | dup_prefix_check(const char *prefix, struct ly_module *module) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 123 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 124 | int i; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 125 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 126 | if (!module->type && module->prefix && !strcmp(module->prefix, prefix)) { |
| 127 | return EXIT_FAILURE; |
| 128 | } |
| 129 | for (i = 0; i < module->imp_size; i++) { |
| 130 | if (!strcmp(module->imp[i].prefix, prefix)) { |
| 131 | return EXIT_FAILURE; |
| 132 | } |
| 133 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 134 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 135 | return EXIT_SUCCESS; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 138 | static int |
| 139 | check_identifier(const char *id, enum LY_IDENT type, unsigned int line, |
| 140 | struct ly_module *module, struct ly_mnode *parent) |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 141 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 142 | int i; |
| 143 | int size; |
| 144 | struct ly_tpdf *tpdf; |
| 145 | struct ly_mnode *mnode; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 146 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 147 | assert(id); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 148 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 149 | /* check id syntax */ |
| 150 | if (!(id[0] >= 'A' && id[0] <= 'Z') && !(id[0] >= 'a' && id[0] <= 'z') && id[0] != '_') { |
| 151 | LOGVAL(VE_INID, line, id, "invalid start character"); |
| 152 | return EXIT_FAILURE; |
| 153 | } |
| 154 | for (i = 1; id[i]; i++) { |
| 155 | if (!(id[i] >= 'A' && id[i] <= 'Z') && !(id[i] >= 'a' && id[i] <= 'z') |
| 156 | && !(id[i] >= '0' && id[i] <= '9') && id[i] != '_' && id[i] != '-' && id[i] != '.') { |
| 157 | LOGVAL(VE_INID, line, id, "invalid character"); |
| 158 | return EXIT_FAILURE; |
| 159 | } |
| 160 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 161 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 162 | if (i > 64) { |
| 163 | LOGWRN("Identifier \"%s\" is long, you should use something shorter.", id); |
| 164 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 165 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 166 | switch (type) { |
| 167 | case LY_IDENT_NAME: |
| 168 | /* check uniqueness of the node within its siblings */ |
| 169 | if (!parent) { |
| 170 | break; |
| 171 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 172 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 173 | LY_TREE_FOR(parent->child, mnode) { |
| 174 | if (mnode->name == id) { |
| 175 | LOGVAL(VE_INID, line, id, "name duplication"); |
| 176 | return EXIT_FAILURE; |
| 177 | } |
| 178 | } |
| 179 | break; |
| 180 | case LY_IDENT_TYPE: |
| 181 | assert(module); |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 182 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 183 | /* check collision with the built-in types */ |
| 184 | if (!strcmp(id, "binary") || !strcmp(id, "bits") || |
| 185 | !strcmp(id, "boolean") || !strcmp(id, "decimal64") || |
| 186 | !strcmp(id, "empty") || !strcmp(id, "enumeration") || |
| 187 | !strcmp(id, "identityref") || !strcmp(id, "instance-identifier") || |
| 188 | !strcmp(id, "int8") || !strcmp(id, "int16") || |
| 189 | !strcmp(id, "int32") || !strcmp(id, "int64") || |
| 190 | !strcmp(id, "leafref") || !strcmp(id, "string") || |
| 191 | !strcmp(id, "uint8") || !strcmp(id, "uint16") || |
| 192 | !strcmp(id, "uint32") || !strcmp(id, "uint64") || !strcmp(id, "union")) { |
| 193 | LOGVAL(VE_SPEC, line, "Typedef name duplicates built-in type."); |
| 194 | return EXIT_FAILURE; |
| 195 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 196 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 197 | /* check locally scoped typedefs (avoid name shadowing) */ |
| 198 | for (; parent; parent = parent->parent) { |
| 199 | switch (parent->nodetype) { |
| 200 | case LY_NODE_CONTAINER: |
| 201 | size = ((struct ly_mnode_container *)parent)->tpdf_size; |
| 202 | tpdf = ((struct ly_mnode_container *)parent)->tpdf; |
| 203 | break; |
| 204 | case LY_NODE_LIST: |
| 205 | size = ((struct ly_mnode_list *)parent)->tpdf_size; |
| 206 | tpdf = ((struct ly_mnode_list *)parent)->tpdf; |
| 207 | break; |
| 208 | case LY_NODE_GROUPING: |
| 209 | size = ((struct ly_mnode_grp *)parent)->tpdf_size; |
| 210 | tpdf = ((struct ly_mnode_grp *)parent)->tpdf; |
| 211 | break; |
| 212 | default: |
| 213 | continue; |
| 214 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 215 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 216 | if (dup_typedef_check(id, tpdf, size)) { |
| 217 | LOGVAL(VE_DUPID, line, "typedef", id); |
| 218 | return EXIT_FAILURE; |
| 219 | } |
| 220 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 221 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 222 | /* check top-level names */ |
| 223 | if (dup_typedef_check(id, module->tpdf, module->tpdf_size)) { |
| 224 | LOGVAL(VE_DUPID, line, "typedef", id); |
| 225 | return EXIT_FAILURE; |
| 226 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 227 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 228 | /* check submodule's top-level names */ |
| 229 | for (i = 0; i < module->inc_size; i++) { |
| 230 | if (dup_typedef_check(id, module->inc[i].submodule->tpdf, module->inc[i].submodule->tpdf_size)) { |
| 231 | LOGVAL(VE_DUPID, line, "typedef", id); |
| 232 | return EXIT_FAILURE; |
| 233 | } |
| 234 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 235 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 236 | /* check top-level names in the main module */ |
| 237 | if (module->type) { |
| 238 | if (dup_typedef_check(id, ((struct ly_submodule *)module)->belongsto->tpdf, |
| 239 | ((struct ly_submodule *)module)->belongsto->tpdf_size)) { |
| 240 | LOGVAL(VE_DUPID, line, "typedef", id); |
| 241 | return EXIT_FAILURE; |
| 242 | } |
| 243 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 244 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 245 | break; |
| 246 | case LY_IDENT_PREFIX: |
| 247 | assert(module); |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 248 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 249 | if (module->type) { |
| 250 | /* go to the main module */ |
| 251 | module = ((struct ly_submodule *)module)->belongsto; |
| 252 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 253 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 254 | /* check the main module itself */ |
| 255 | if (dup_prefix_check(id, module)) { |
| 256 | LOGVAL(VE_DUPID, line, "prefix", id); |
| 257 | return EXIT_FAILURE; |
| 258 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 259 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 260 | /* and all its submodules */ |
| 261 | for (i = 0; i < module->inc_size; i++) { |
| 262 | if (dup_prefix_check(id, (struct ly_module *)module->inc[i].submodule)) { |
| 263 | LOGVAL(VE_DUPID, line, "prefix", id); |
| 264 | return EXIT_FAILURE; |
| 265 | } |
| 266 | } |
| 267 | break; |
Radek Krejci | b05774c | 2015-06-18 13:52:59 +0200 | [diff] [blame] | 268 | case LY_IDENT_FEATURE: |
| 269 | assert(module); |
| 270 | |
| 271 | /* check feature name uniqness*/ |
Radek Krejci | 49babf3 | 2015-06-18 13:56:17 +0200 | [diff] [blame] | 272 | /* check features in the current module */ |
Radek Krejci | b05774c | 2015-06-18 13:52:59 +0200 | [diff] [blame] | 273 | if (dup_feature_check(id, module)) { |
| 274 | LOGVAL(VE_DUPID, line, "feature", id); |
| 275 | return EXIT_FAILURE; |
| 276 | } |
| 277 | |
| 278 | /* and all its submodules */ |
| 279 | for (i = 0; i < module->inc_size; i++) { |
| 280 | if (dup_feature_check(id, (struct ly_module *)module->inc[i].submodule)) { |
| 281 | LOGVAL(VE_DUPID, line, "feature", id); |
| 282 | return EXIT_FAILURE; |
| 283 | } |
| 284 | } |
| 285 | break; |
| 286 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 287 | default: |
| 288 | /* no check required */ |
| 289 | break; |
| 290 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 291 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 292 | return EXIT_SUCCESS; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 295 | static int |
| 296 | check_key(struct ly_mnode_leaf *key, uint8_t flags, struct ly_mnode_leaf **list, int index, unsigned int line, |
| 297 | const char *name, int len) |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 298 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 299 | char *dup = NULL; |
| 300 | int j; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 301 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 302 | /* existence */ |
| 303 | if (!key) { |
| 304 | if (name[len] != '\0') { |
| 305 | dup = strdup(name); |
| 306 | dup[len] = '\0'; |
| 307 | name = dup; |
| 308 | } |
| 309 | LOGVAL(VE_KEY_MISS, line, name); |
| 310 | free(dup); |
| 311 | return EXIT_FAILURE; |
| 312 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 313 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 314 | /* uniqueness */ |
| 315 | for (j = index - 1; j >= 0; j--) { |
| 316 | if (list[index] == list[j]) { |
| 317 | LOGVAL(VE_KEY_DUP, line, key->name); |
| 318 | return EXIT_FAILURE; |
| 319 | } |
| 320 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 321 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 322 | /* key is a leaf */ |
| 323 | if (key->nodetype != LY_NODE_LEAF) { |
| 324 | LOGVAL(VE_KEY_NLEAF, line, key->name); |
| 325 | return EXIT_FAILURE; |
| 326 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 327 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 328 | /* type of the leaf is not built-in empty */ |
| 329 | if (key->type.base == LY_TYPE_EMPTY) { |
| 330 | LOGVAL(VE_KEY_TYPE, line, key->name); |
| 331 | return EXIT_FAILURE; |
| 332 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 333 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 334 | /* config attribute is the same as of the list */ |
| 335 | if ((flags & LY_NODE_CONFIG_MASK) != (key->flags & LY_NODE_CONFIG_MASK)) { |
| 336 | LOGVAL(VE_KEY_CONFIG, line, key->name); |
| 337 | return EXIT_FAILURE; |
| 338 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 339 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 340 | return EXIT_SUCCESS; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 343 | static int |
Radek Krejci | 3de29a7 | 2015-06-16 15:23:03 +0200 | [diff] [blame] | 344 | check_mandatory(struct ly_mnode *mnode) |
| 345 | { |
| 346 | struct ly_mnode *child; |
| 347 | |
| 348 | assert(mnode); |
| 349 | |
| 350 | if (mnode->flags & LY_NODE_MAND_TRUE) { |
| 351 | return EXIT_FAILURE; |
| 352 | } |
| 353 | |
Radek Krejci | fd0bb0a | 2015-06-18 13:18:24 +0200 | [diff] [blame] | 354 | if (mnode->nodetype == LY_NODE_CASE || mnode->nodetype == LY_NODE_CHOICE) { |
| 355 | LY_TREE_FOR(mnode->child, child) { |
| 356 | if (check_mandatory(child)) { |
| 357 | return EXIT_FAILURE; |
| 358 | } |
Radek Krejci | 3de29a7 | 2015-06-16 15:23:03 +0200 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | return EXIT_SUCCESS; |
| 363 | } |
| 364 | |
| 365 | static int |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 366 | check_default(struct ly_type *type, const char *value) |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 367 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 368 | /* TODO - RFC 6020, sec. 7.3.4 */ |
| 369 | (void)type; |
| 370 | (void)value; |
| 371 | return EXIT_SUCCESS; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 374 | static int |
| 375 | check_date(const char *date, unsigned int line) |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 376 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 377 | int i; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 378 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 379 | assert(date); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 380 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 381 | if (strlen(date) != LY_REV_SIZE - 1) { |
| 382 | goto error; |
| 383 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 384 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 385 | for (i = 0; i < LY_REV_SIZE - 1; i++) { |
| 386 | if (i == 4 || i == 7) { |
| 387 | if (date[i] != '-') { |
| 388 | goto error; |
| 389 | } |
| 390 | } else if (!isdigit(date[i])) { |
| 391 | goto error; |
| 392 | } |
| 393 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 394 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 395 | return EXIT_SUCCESS; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 396 | |
| 397 | error: |
| 398 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 399 | LOGVAL(VE_INDATE, line, date); |
| 400 | return EXIT_FAILURE; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 403 | static int |
| 404 | check_length(const char *expr, struct ly_type *type, unsigned int line) |
| 405 | { |
| 406 | const char *c = expr; |
| 407 | char *tail; |
| 408 | uint64_t limit = 0, n; |
| 409 | int flg = 1; /* first run flag */ |
| 410 | |
| 411 | assert(expr); |
| 412 | |
| 413 | /* TODO check compatibility with the restriction defined on type from which this type is derived, |
| 414 | * it will be the same function to check that the value from instance data respect the restriction */ |
| 415 | (void)type; |
| 416 | |
| 417 | lengthpart: |
| 418 | |
| 419 | while (isspace(*c)) { |
| 420 | c++; |
| 421 | } |
| 422 | |
| 423 | /* lower boundary or explicit number */ |
| 424 | if (!strncmp(c, "max", 3)) { |
| 425 | max: |
| 426 | c += 3; |
| 427 | while (isspace(*c)) { |
| 428 | c++; |
| 429 | } |
| 430 | if (*c != '\0') { |
| 431 | goto error; |
| 432 | } |
| 433 | |
| 434 | return EXIT_SUCCESS; |
| 435 | |
| 436 | } else if (!strncmp(c, "min", 3)) { |
| 437 | if (!flg) { |
| 438 | /* min cannot be used elsewhere than in the first length-part */ |
| 439 | goto error; |
| 440 | } else { |
| 441 | flg = 0; |
| 442 | /* remember value/lower boundary */ |
| 443 | limit = 0; |
| 444 | } |
| 445 | c += 3; |
| 446 | while (isspace(*c)) { |
| 447 | c++; |
| 448 | } |
| 449 | |
| 450 | if (*c == '|') { |
| 451 | c++; |
| 452 | /* process next length-parth */ |
| 453 | goto lengthpart; |
| 454 | } else if (*c == '\0') { |
| 455 | return EXIT_SUCCESS; |
| 456 | } else if (!strncmp(c, "..", 2)) { |
| 457 | upper: |
| 458 | c += 2; |
| 459 | while (isspace(*c)) { |
| 460 | c++; |
| 461 | } |
| 462 | if (*c == '\0') { |
| 463 | goto error; |
| 464 | } |
| 465 | |
| 466 | /* upper boundary */ |
| 467 | if (!strncmp(c, "max", 3)) { |
| 468 | goto max; |
| 469 | } |
| 470 | |
| 471 | if (!isdigit(*c)) { |
| 472 | goto error; |
| 473 | } |
| 474 | |
| 475 | n = strtol(c, &tail, 10); |
| 476 | c = tail; |
| 477 | while (isspace(*c)) { |
| 478 | c++; |
| 479 | } |
| 480 | if (n <= limit) { |
| 481 | goto error; |
| 482 | } |
| 483 | if (*c == '\0') { |
| 484 | return EXIT_SUCCESS; |
| 485 | } else if (*c == '|') { |
| 486 | c++; |
| 487 | /* remember the uppre boundary for check in next part */ |
| 488 | limit = n; |
| 489 | /* process next length-parth */ |
| 490 | goto lengthpart; |
| 491 | } else { |
| 492 | goto error; |
| 493 | } |
| 494 | } else { |
| 495 | goto error; |
| 496 | } |
| 497 | |
| 498 | } else if (isdigit(*c)) { |
| 499 | /* number */ |
| 500 | n = strtol(c, &tail, 10); |
| 501 | c = tail; |
| 502 | while (isspace(*c)) { |
| 503 | c++; |
| 504 | } |
| 505 | /* skip limit check in first length-part check */ |
| 506 | if (!flg && n <= limit) { |
| 507 | goto error; |
| 508 | } |
| 509 | flg = 0; |
| 510 | limit = n; |
| 511 | |
| 512 | if (*c == '|') { |
| 513 | c++; |
| 514 | /* process next length-parth */ |
| 515 | goto lengthpart; |
| 516 | } else if (*c == '\0') { |
| 517 | return EXIT_SUCCESS; |
| 518 | } else if (!strncmp(c, "..", 2)) { |
| 519 | goto upper; |
| 520 | } |
| 521 | } /* else error */ |
| 522 | |
| 523 | error: |
| 524 | |
| 525 | LOGVAL(VE_INARG, line, expr, "length"); |
| 526 | return EXIT_FAILURE; |
| 527 | } |
| 528 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 529 | static const char * |
| 530 | 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] | 531 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 532 | int len; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 533 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 534 | /* there should be <text> child */ |
| 535 | if (!node->child || !node->child->name || strcmp(node->child->name, name)) { |
| 536 | LOGWRN("Expected \"%s\" element in \"%s\" element.", name, node->name); |
| 537 | } else if (node->child->content) { |
| 538 | len = strlen(node->child->content); |
| 539 | return lydict_insert(ctx, node->child->content, len); |
| 540 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 541 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 542 | LOGVAL(VE_INARG, LOGLINE(node), name, node->name); |
| 543 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 546 | static struct ly_tpdf * |
| 547 | find_superior_type(const char *name, struct ly_module *module, struct ly_mnode *parent) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 548 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 549 | int i, j, found = 0; |
| 550 | int prefix_len = 0; |
| 551 | const char *qname; |
| 552 | struct ly_tpdf *tpdf; |
| 553 | int tpdf_size; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 554 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 555 | qname = strchr(name, ':'); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 556 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 557 | if (!qname) { |
| 558 | /* no prefix, try built-in types */ |
| 559 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 560 | if (!strcmp(ly_types[i].def->name, name)) { |
| 561 | return ly_types[i].def; |
| 562 | } |
| 563 | } |
| 564 | qname = name; |
| 565 | } else { |
| 566 | /* set qname to correct position after colon */ |
| 567 | prefix_len = qname - name; |
| 568 | qname++; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 569 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 570 | if (!strncmp(name, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 571 | /* prefix refers to the current module, ignore it */ |
| 572 | prefix_len = 0; |
| 573 | } |
| 574 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 575 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 576 | if (!prefix_len && parent) { |
| 577 | /* search in local typedefs */ |
| 578 | while (parent) { |
| 579 | switch (parent->nodetype) { |
| 580 | case LY_NODE_CONTAINER: |
| 581 | tpdf_size = ((struct ly_mnode_container *)parent)->tpdf_size; |
| 582 | tpdf = ((struct ly_mnode_container *)parent)->tpdf; |
| 583 | break; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 584 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 585 | case LY_NODE_LIST: |
| 586 | tpdf_size = ((struct ly_mnode_list *)parent)->tpdf_size; |
| 587 | tpdf = ((struct ly_mnode_list *)parent)->tpdf; |
| 588 | break; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 589 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 590 | case LY_NODE_GROUPING: |
| 591 | tpdf_size = ((struct ly_mnode_grp *)parent)->tpdf_size; |
| 592 | tpdf = ((struct ly_mnode_grp *)parent)->tpdf; |
| 593 | break; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 594 | |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 595 | /* TODO add rpc, notification, input, output */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 596 | default: |
| 597 | parent = parent->parent; |
| 598 | continue; |
| 599 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 600 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 601 | for (i = 0; i < tpdf_size; i++) { |
| 602 | if (!strcmp(tpdf[i].name, qname)) { |
| 603 | return &tpdf[i]; |
| 604 | } |
| 605 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 606 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 607 | parent = parent->parent; |
| 608 | } |
| 609 | } else if (prefix_len) { |
| 610 | /* get module where to search */ |
| 611 | for (i = 0; i < module->imp_size; i++) { |
| 612 | if (!strncmp(module->imp[i].prefix, name, prefix_len) && !module->imp[i].prefix[prefix_len]) { |
| 613 | module = module->imp[i].module; |
| 614 | found = 1; |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | if (!found) { |
| 619 | return NULL; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /* search in top level typedefs */ |
| 624 | for (i = 0; i < module->tpdf_size; i++) { |
| 625 | if (!strcmp(module->tpdf[i].name, qname)) { |
| 626 | return &module->tpdf[i]; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /* search in submodules */ |
| 631 | for (i = 0; i < module->inc_size; i++) { |
| 632 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
| 633 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, qname)) { |
| 634 | return &module->inc[i].submodule->tpdf[j]; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | return NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 642 | static struct ly_ident * |
| 643 | find_base_ident_sub(struct ly_module *module, struct ly_ident *ident, const char *basename) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 644 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 645 | unsigned int i; |
| 646 | struct ly_ident *base_iter; |
| 647 | struct ly_ident_der *der; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 648 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 649 | for (i = 0; i < module->ident_size; i++) { |
| 650 | if (!strcmp(basename, module->ident[i].name)) { |
| 651 | /* we are done */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 652 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 653 | if (!ident) { |
| 654 | /* just search for type, so do not modify anything, just return |
| 655 | * the base identity pointer |
| 656 | */ |
| 657 | return &module->ident[i]; |
| 658 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 659 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 660 | /* we are resolving identity definition, so now update structures */ |
| 661 | ident->base = base_iter = &module->ident[i]; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 662 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 663 | while (base_iter) { |
| 664 | for (der = base_iter->der; der && der->next; der = der->next); |
| 665 | if (der) { |
| 666 | der->next = malloc(sizeof *der); |
| 667 | der = der->next; |
| 668 | } else { |
| 669 | ident->base->der = der = malloc(sizeof *der); |
| 670 | } |
| 671 | der->next = NULL; |
| 672 | der->ident = ident; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 673 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 674 | base_iter = base_iter->base; |
| 675 | } |
| 676 | return ident->base; |
| 677 | } |
| 678 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 679 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 680 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 683 | static struct ly_ident * |
| 684 | 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] | 685 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 686 | const char *name; |
| 687 | int prefix_len = 0; |
| 688 | int i, found = 0; |
| 689 | struct ly_ident *result; |
| 690 | const char *basename; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 691 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 692 | basename = lyxml_get_attr(node, "name", NULL); |
| 693 | if (!basename) { |
| 694 | LOGVAL(VE_MISSARG, LOGLINE(node), "name", "base"); |
| 695 | return NULL; |
| 696 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 697 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 698 | /* search for the base identity */ |
| 699 | name = strchr(basename, ':'); |
| 700 | if (name) { |
| 701 | /* set name to correct position after colon */ |
| 702 | prefix_len = name - basename; |
| 703 | name++; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 704 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 705 | if (!strncmp(basename, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 706 | /* prefix refers to the current module, ignore it */ |
| 707 | prefix_len = 0; |
| 708 | } |
| 709 | } else { |
| 710 | name = basename; |
| 711 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 712 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 713 | if (prefix_len) { |
| 714 | /* get module where to search */ |
| 715 | for (i = 0; i < module->imp_size; i++) { |
| 716 | if (!strncmp(module->imp[i].prefix, basename, prefix_len) |
| 717 | && !module->imp[i].prefix[prefix_len]) { |
| 718 | module = module->imp[i].module; |
| 719 | found = 1; |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | if (!found) { |
| 724 | /* identity refers unknown data model */ |
| 725 | LOGVAL(VE_INPREFIX, LOGLINE(node), basename); |
| 726 | return NULL; |
| 727 | } |
| 728 | } else { |
| 729 | /* search in submodules */ |
| 730 | for (i = 0; i < module->inc_size; i++) { |
| 731 | result = find_base_ident_sub((struct ly_module *)module->inc[i].submodule, ident, name); |
| 732 | if (result) { |
| 733 | return result; |
| 734 | } |
| 735 | } |
| 736 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 737 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 738 | /* search in the identified module */ |
| 739 | result = find_base_ident_sub(module, ident, name); |
| 740 | if (!result) { |
| 741 | LOGVAL(VE_INARG, LOGLINE(node), basename, ident ? "identity" : "type"); |
| 742 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 743 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 744 | return result; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 747 | static int |
| 748 | fill_yin_identity(struct ly_module *module, struct lyxml_elem *yin, struct ly_ident *ident) |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 749 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 750 | struct lyxml_elem *node, *next; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 751 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 752 | if (read_yin_common(module, NULL, (struct ly_mnode *)ident, yin, OPT_IDENT | OPT_MODULE)) { |
| 753 | return EXIT_FAILURE; |
| 754 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 755 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 756 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 757 | if (!strcmp(node->name, "base")) { |
| 758 | if (ident->base) { |
| 759 | LOGVAL(VE_TOOMANY, LOGLINE(node), "base", "identity"); |
| 760 | return EXIT_FAILURE; |
| 761 | } |
| 762 | if (!find_base_ident(module, ident, node)) { |
| 763 | return EXIT_FAILURE; |
| 764 | } |
| 765 | } else { |
| 766 | LOGVAL(VE_INSTMT, LOGLINE(node), node->name, "identity"); |
| 767 | return EXIT_FAILURE; |
| 768 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 769 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 770 | lyxml_free_elem(module->ctx, node); |
| 771 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 772 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 773 | return EXIT_SUCCESS; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 774 | } |
| 775 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 776 | static int |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 777 | read_restr_substmt(struct ly_ctx *ctx, struct ly_restr *restr, struct lyxml_elem *yin) |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 778 | { |
| 779 | struct lyxml_elem *next, *child; |
| 780 | |
| 781 | LY_TREE_FOR_SAFE(yin->child, next, child) { |
| 782 | if (!strcmp(child->name, "description")) { |
| 783 | if (restr->dsc) { |
| 784 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 785 | return EXIT_FAILURE; |
| 786 | } |
| 787 | restr->dsc = read_yin_subnode(ctx, child, "text"); |
| 788 | if (!restr->dsc) { |
| 789 | return EXIT_FAILURE; |
| 790 | } |
| 791 | } else if (!strcmp(child->name, "reference")) { |
| 792 | if (restr->ref) { |
| 793 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 794 | return EXIT_FAILURE; |
| 795 | } |
| 796 | restr->ref = read_yin_subnode(ctx, child, "text"); |
| 797 | if (!restr->ref) { |
| 798 | return EXIT_FAILURE; |
| 799 | } |
| 800 | } else if (!strcmp(child->name, "error-app-tag")) { |
| 801 | if (restr->eapptag) { |
| 802 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 803 | return EXIT_FAILURE; |
| 804 | } |
| 805 | restr->eapptag = read_yin_subnode(ctx, child, "value"); |
| 806 | if (!restr->eapptag) { |
| 807 | return EXIT_FAILURE; |
| 808 | } |
| 809 | } else if (!strcmp(child->name, "error-message")) { |
| 810 | if (restr->emsg) { |
| 811 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 812 | return EXIT_FAILURE; |
| 813 | } |
| 814 | restr->emsg = read_yin_subnode(ctx, child, "value"); |
| 815 | if (!restr->emsg) { |
| 816 | return EXIT_FAILURE; |
| 817 | } |
| 818 | } else { |
| 819 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 820 | return EXIT_FAILURE; |
| 821 | } |
| 822 | |
| 823 | lyxml_free_elem(ctx, child); |
| 824 | } |
| 825 | |
| 826 | return EXIT_SUCCESS; |
| 827 | } |
| 828 | |
| 829 | static int |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 830 | fill_yin_type(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, struct ly_type *type) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 831 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 832 | const char *value, *delim; |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 833 | struct lyxml_elem *next, *node; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 834 | int i, j; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 835 | int64_t v, v_; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 836 | int64_t p, p_; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 837 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 838 | GETVAL(value, yin, "name") |
| 839 | delim = strchr(value, ':'); |
| 840 | if (delim) { |
| 841 | type->prefix = lydict_insert(module->ctx, value, delim - value); |
| 842 | } |
Radek Krejci | 667b97f | 2015-05-25 15:03:30 +0200 | [diff] [blame] | 843 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 844 | type->der = find_superior_type(value, module, parent); |
| 845 | if (!type->der) { |
| 846 | LOGVAL(VE_INARG, LOGLINE(yin), value, yin->name); |
| 847 | goto error; |
| 848 | } |
| 849 | type->base = type->der->type.base; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 850 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 851 | switch (type->base) { |
| 852 | case LY_TYPE_BINARY: |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 853 | /* RFC 6020 9.8.1, 9.4.4 - length, number of octets it contains */ |
| 854 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 855 | if (!strcmp(node->name, "length")) { |
| 856 | if (type->info.binary.length) { |
| 857 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 858 | goto error; |
| 859 | } |
| 860 | |
| 861 | GETVAL(value, node, "value"); |
| 862 | if (check_length(value, type, LOGLINE(node))) { |
| 863 | goto error; |
| 864 | } |
| 865 | type->info.binary.length = calloc(1, sizeof *type->info.binary.length); |
| 866 | type->info.binary.length->expr = lydict_insert(module->ctx, value, 0); |
| 867 | |
| 868 | /* get possible substatements */ |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 869 | if (read_restr_substmt(module->ctx, (struct ly_restr *)type->info.binary.length, node)) { |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 870 | goto error; |
| 871 | } |
| 872 | } else { |
Radek Krejci | 82d971d | 2015-06-19 14:20:50 +0200 | [diff] [blame] | 873 | LOGVAL(VE_INSTMT, LOGLINE(node), node->name); |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 874 | goto error; |
| 875 | } |
Radek Krejci | 82d971d | 2015-06-19 14:20:50 +0200 | [diff] [blame] | 876 | lyxml_free_elem(module->ctx, node); |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 877 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 878 | break; |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 879 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 880 | case LY_TYPE_BITS: |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 881 | /* RFC 6020 9.7.4 - bit */ |
| 882 | |
| 883 | /* get bit specifications, at least one must be present */ |
| 884 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 885 | if (!strcmp(node->name, "bit")) { |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 886 | type->info.bits.count++; |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 887 | } else { |
| 888 | LOGVAL(VE_INSTMT, LOGLINE(yin->child), yin->child->name); |
| 889 | goto error; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 890 | } |
| 891 | } |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 892 | if (!type->info.bits.count) { |
| 893 | if (type->der->type.der) { |
| 894 | /* this is just a derived type with no bit specified */ |
| 895 | break; |
| 896 | } |
| 897 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "bit", "type"); |
| 898 | goto error; |
| 899 | } |
| 900 | |
| 901 | type->info.bits.bit = calloc(type->info.bits.count, sizeof *type->info.bits.bit); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 902 | for (i = p = 0; yin->child; i++) { |
| 903 | GETVAL(value, yin->child, "name"); |
| 904 | if (check_identifier(value, LY_IDENT_SIMPLE, LOGLINE(yin->child), NULL, NULL)) { |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 905 | goto error; |
| 906 | } |
| 907 | type->info.bits.bit[i].name = lydict_insert(module->ctx, value, strlen(value)); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 908 | if (read_yin_common(module, NULL, (struct ly_mnode *)&type->info.bits.bit[i], yin->child, 0)) { |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 909 | type->info.bits.count = i + 1; |
| 910 | goto error; |
| 911 | } |
| 912 | |
| 913 | /* check the name uniqueness */ |
| 914 | for (j = 0; j < i; j++) { |
| 915 | if (!strcmp(type->info.bits.bit[j].name, type->info.bits.bit[i].name)) { |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 916 | LOGVAL(VE_BITS_DUPNAME, LOGLINE(yin->child), type->info.bits.bit[i].name); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 917 | type->info.bits.count = i + 1; |
| 918 | goto error; |
| 919 | } |
| 920 | } |
| 921 | |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 922 | node = yin->child->child; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 923 | if (node && !strcmp(node->name, "position")) { |
Radek Krejci | 3e7b618 | 2015-06-19 14:25:20 +0200 | [diff] [blame] | 924 | GETVAL(value, node, "value"); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 925 | p_ = strtol(value, NULL, 10); |
| 926 | |
| 927 | /* range check */ |
| 928 | if (p_ < 0 || p_ > UINT32_MAX) { |
| 929 | LOGVAL(VE_INARG, LOGLINE(node), value, "bit/position"); |
| 930 | type->info.bits.count = i + 1; |
| 931 | goto error; |
| 932 | } |
| 933 | type->info.bits.bit[i].pos = (uint32_t)p_; |
| 934 | |
| 935 | /* keep the highest enum value for automatic increment */ |
| 936 | if (type->info.bits.bit[i].pos > p) { |
| 937 | p = type->info.bits.bit[i].pos; |
| 938 | p++; |
| 939 | } else { |
| 940 | /* check that the value is unique */ |
| 941 | for (j = 0; j < i; j++) { |
| 942 | if (type->info.bits.bit[j].pos == type->info.bits.bit[i].pos) { |
| 943 | LOGVAL(VE_BITS_DUPVAL, LOGLINE(node), type->info.bits.bit[i].pos, type->info.bits.bit[i].name); |
| 944 | type->info.bits.count = i + 1; |
| 945 | goto error; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | } else { |
| 950 | /* assign value automatically */ |
| 951 | if (p > UINT32_MAX) { |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 952 | LOGVAL(VE_INARG, LOGLINE(yin->child), "4294967295", "bit/position"); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 953 | type->info.bits.count = i + 1; |
| 954 | goto error; |
| 955 | } |
| 956 | type->info.bits.bit[i].pos = (uint32_t)p; |
| 957 | p++; |
| 958 | } |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 959 | lyxml_free_elem(module->ctx, yin->child); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 960 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 961 | break; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 962 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 963 | case LY_TYPE_DEC64: |
| 964 | /* TODO fraction-digits, 9.3.4 |
| 965 | * - MUST, 1, nerekurzivni, hodnota 1-18 */ |
| 966 | /* TODO range, 9.2.4 |
| 967 | * - optional, 0..1, rekurzivne - omezuje, string, podelementy*/ |
| 968 | break; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 969 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 970 | case LY_TYPE_ENUM: |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 971 | /* RFC 6020 9.6 - enum */ |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 972 | |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 973 | /* get enum specifications, at least one must be present */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 974 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 975 | if (!strcmp(node->name, "enum")) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 976 | type->info.enums.count++; |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 977 | } else { |
| 978 | LOGVAL(VE_INSTMT, LOGLINE(yin->child), yin->child->name); |
| 979 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 980 | } |
| 981 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 982 | if (!type->info.enums.count) { |
| 983 | if (type->der->type.der) { |
| 984 | /* this is just a derived type with no enum specified */ |
| 985 | break; |
| 986 | } |
| 987 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "enum", "type"); |
| 988 | goto error; |
| 989 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 990 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 991 | type->info.enums.list = calloc(type->info.enums.count, sizeof *type->info.enums.list); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 992 | for (i = v = 0; yin->child; i++) { |
| 993 | GETVAL(value, yin->child, "name"); |
| 994 | if (check_identifier(value, LY_IDENT_SIMPLE, LOGLINE(yin->child), NULL, NULL)) { |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 995 | goto error; |
| 996 | } |
| 997 | type->info.enums.list[i].name = lydict_insert(module->ctx, value, strlen(value)); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 998 | if (read_yin_common(module, NULL, (struct ly_mnode *)&type->info.enums.list[i], yin->child, 0)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 999 | type->info.enums.count = i + 1; |
| 1000 | goto error; |
| 1001 | } |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1002 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1003 | /* the assigned name MUST NOT have any leading or trailing whitespace characters */ |
| 1004 | value = type->info.enums.list[i].name; |
| 1005 | if (isspace(value[0]) || isspace(value[strlen(value) - 1])) { |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1006 | LOGVAL(VE_ENUM_WS, LOGLINE(yin->child), value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1007 | type->info.enums.count = i + 1; |
| 1008 | goto error; |
| 1009 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1010 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1011 | /* check the name uniqueness */ |
| 1012 | for (j = 0; j < i; j++) { |
| 1013 | if (!strcmp(type->info.enums.list[j].name, type->info.enums.list[i].name)) { |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1014 | LOGVAL(VE_ENUM_DUPNAME, LOGLINE(yin->child), type->info.enums.list[i].name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1015 | type->info.enums.count = i + 1; |
| 1016 | goto error; |
| 1017 | } |
| 1018 | } |
Radek Krejci | 04581c6 | 2015-05-22 21:24:00 +0200 | [diff] [blame] | 1019 | |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1020 | node = yin->child->child; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1021 | if (node && !strcmp(node->name, "value")) { |
Radek Krejci | 3e7b618 | 2015-06-19 14:25:20 +0200 | [diff] [blame] | 1022 | GETVAL(value, node, "value"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1023 | v_ = strtol(value, NULL, 10); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1024 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1025 | /* range check */ |
| 1026 | if (v_ < INT32_MIN || v_ > INT32_MAX) { |
| 1027 | LOGVAL(VE_INARG, LOGLINE(node), value, "enum/value"); |
| 1028 | type->info.enums.count = i + 1; |
| 1029 | goto error; |
| 1030 | } |
| 1031 | type->info.enums.list[i].value = v_; |
| 1032 | |
| 1033 | /* keep the highest enum value for automatic increment */ |
| 1034 | if (type->info.enums.list[i].value > v) { |
| 1035 | v = type->info.enums.list[i].value; |
| 1036 | v++; |
| 1037 | } else { |
| 1038 | /* check that the value is unique */ |
| 1039 | for (j = 0; j < i; j++) { |
| 1040 | if (type->info.enums.list[j].value == type->info.enums.list[i].value) { |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1041 | LOGVAL(VE_ENUM_DUPVAL, LOGLINE(node), type->info.enums.list[i].value, type->info.enums.list[i].name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1042 | type->info.enums.count = i + 1; |
| 1043 | goto error; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | } else { |
| 1048 | /* assign value automatically */ |
| 1049 | if (v > INT32_MAX) { |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1050 | LOGVAL(VE_INARG, LOGLINE(yin->child), "2147483648", "enum/value"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1051 | type->info.enums.count = i + 1; |
| 1052 | goto error; |
| 1053 | } |
| 1054 | type->info.enums.list[i].value = v; |
| 1055 | v++; |
| 1056 | } |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1057 | lyxml_free_elem(module->ctx, yin->child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1058 | } |
| 1059 | break; |
| 1060 | |
| 1061 | case LY_TYPE_IDENT: |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1062 | /* RFC 6020 9.10 - base */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1063 | |
| 1064 | /* get base specification, exactly one must be present */ |
| 1065 | if (!yin->child) { |
| 1066 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "base", "type"); |
| 1067 | goto error; |
| 1068 | } |
| 1069 | if (strcmp(yin->child->name, "base")) { |
| 1070 | LOGVAL(VE_INSTMT, LOGLINE(yin->child), yin->child->name); |
| 1071 | goto error; |
| 1072 | } |
| 1073 | if (yin->child->next) { |
| 1074 | LOGVAL(VE_INSTMT, LOGLINE(yin->child->next), yin->child->next->name); |
| 1075 | goto error; |
| 1076 | } |
| 1077 | type->info.ident.ref = find_base_ident(module, NULL, yin->child); |
| 1078 | if (!type->info.ident.ref) { |
| 1079 | return EXIT_FAILURE; |
| 1080 | } |
| 1081 | break; |
| 1082 | |
| 1083 | case LY_TYPE_INST: |
Radek Krejci | af35142 | 2015-06-19 14:49:38 +0200 | [diff] [blame^] | 1084 | /* RFC 6020 9.13.2 - require-instance */ |
| 1085 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 1086 | if (!strcmp(node->name, "require-instance")) { |
| 1087 | if (type->info.inst.req) { |
| 1088 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 1089 | goto error; |
| 1090 | } |
| 1091 | GETVAL(value, node, "value"); |
| 1092 | if (strcmp(value, "true")) { |
| 1093 | type->info.inst.req = 1; |
| 1094 | } else if (strcmp(value, "false")) { |
| 1095 | type->info.inst.req = -1; |
| 1096 | } else { |
| 1097 | LOGVAL(VE_INARG, LOGLINE(node), value, node->name); |
| 1098 | goto error; |
| 1099 | } |
| 1100 | } else { |
| 1101 | LOGVAL(VE_INSTMT, LOGLINE(node), node->name); |
| 1102 | goto error; |
| 1103 | } |
| 1104 | lyxml_free_elem(module->ctx, node); |
| 1105 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1106 | break; |
| 1107 | |
| 1108 | case LY_TYPE_INT8: |
| 1109 | case LY_TYPE_INT16: |
| 1110 | case LY_TYPE_INT32: |
| 1111 | case LY_TYPE_INT64: |
| 1112 | case LY_TYPE_UINT8: |
| 1113 | case LY_TYPE_UINT16: |
| 1114 | case LY_TYPE_UINT32: |
| 1115 | case LY_TYPE_UINT64: |
| 1116 | /* TODO range, 9.2.4 |
| 1117 | * - optional, 0..1, i rekurzivne - omezuje, string, podelementy*/ |
| 1118 | break; |
| 1119 | |
| 1120 | case LY_TYPE_LEAFREF: |
| 1121 | /* TODO path, 9.9.2 |
| 1122 | * - 1, nerekurzivni, string */ |
| 1123 | break; |
| 1124 | |
| 1125 | case LY_TYPE_STRING: |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1126 | /* RFC 6020 9.4.4 - length */ |
| 1127 | /* RFC 6020 9.4.6 - pattern */ |
| 1128 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 1129 | if (!strcmp(node->name, "length")) { |
| 1130 | if (type->info.str.length) { |
| 1131 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 1132 | goto error; |
| 1133 | } |
| 1134 | |
| 1135 | GETVAL(value, node, "value"); |
| 1136 | if (check_length(value, type, LOGLINE(node))) { |
| 1137 | goto error; |
| 1138 | } |
| 1139 | type->info.str.length = calloc(1, sizeof *type->info.str.length); |
| 1140 | type->info.str.length->expr = lydict_insert(module->ctx, value, 0); |
| 1141 | |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1142 | /* get possible sub-statements */ |
| 1143 | if (read_restr_substmt(module->ctx, type->info.str.length, node)) { |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1144 | goto error; |
| 1145 | } |
Radek Krejci | 82d971d | 2015-06-19 14:20:50 +0200 | [diff] [blame] | 1146 | lyxml_free_elem(module->ctx, node); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1147 | } else if (!strcmp(node->name, "pattern")) { |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1148 | type->info.str.pat_count++; |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1149 | } else { |
Radek Krejci | 82d971d | 2015-06-19 14:20:50 +0200 | [diff] [blame] | 1150 | LOGVAL(VE_INSTMT, LOGLINE(node), node->name); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1151 | goto error; |
| 1152 | } |
| 1153 | } |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1154 | /* store patterns in array */ |
| 1155 | if (type->info.str.pat_count) { |
| 1156 | type->info.str.patterns = calloc(type->info.str.pat_count, sizeof *type->info.str.patterns); |
| 1157 | for (i = 0; yin->child; i++) { |
| 1158 | GETVAL(value, yin->child, "value"); |
| 1159 | type->info.str.patterns[i].expr = lydict_insert(module->ctx, value, 0); |
| 1160 | |
| 1161 | /* get possible sub-statements */ |
| 1162 | if (read_restr_substmt(module->ctx, &type->info.str.patterns[i], yin->child)) { |
| 1163 | goto error; |
| 1164 | } |
| 1165 | lyxml_free_elem(module->ctx, yin->child); |
| 1166 | } |
| 1167 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1168 | break; |
| 1169 | |
| 1170 | case LY_TYPE_UNION: |
| 1171 | /* TODO type, 7.4 |
| 1172 | * - 1..n, nerekurzivni, resp rekurzivni pro union ale bez vazby na predky, nesmi byt empty nebo leafref */ |
| 1173 | break; |
| 1174 | |
| 1175 | default: |
| 1176 | /* nothing needed : |
| 1177 | * LY_TYPE_BOOL, LY_TYPE_EMPTY |
| 1178 | */ |
| 1179 | break; |
| 1180 | } |
| 1181 | |
| 1182 | return EXIT_SUCCESS; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1183 | |
| 1184 | error: |
| 1185 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1186 | return EXIT_FAILURE; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1189 | static int |
| 1190 | fill_yin_typedef(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, struct ly_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1191 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1192 | const char *value; |
| 1193 | struct lyxml_elem *node, *next; |
| 1194 | int r = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1195 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1196 | GETVAL(value, yin, "name"); |
| 1197 | if (check_identifier(value, LY_IDENT_TYPE, LOGLINE(yin), module, parent)) { |
| 1198 | goto error; |
| 1199 | } |
| 1200 | tpdf->name = lydict_insert(module->ctx, value, strlen(value)); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1201 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1202 | /* generic part - status, description, reference */ |
| 1203 | if (read_yin_common(module, NULL, (struct ly_mnode *)tpdf, yin, OPT_IDENT)) { |
| 1204 | goto error; |
| 1205 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1206 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1207 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 1208 | if (!strcmp(node->name, "type")) { |
| 1209 | if (tpdf->type.der) { |
| 1210 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 1211 | goto error; |
| 1212 | } |
| 1213 | r = fill_yin_type(module, parent, node, &tpdf->type); |
| 1214 | } else if (!strcmp(node->name, "default")) { |
| 1215 | if (tpdf->dflt) { |
| 1216 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 1217 | goto error; |
| 1218 | } |
| 1219 | GETVAL(value, node, "value"); |
| 1220 | tpdf->dflt = lydict_insert(module->ctx, value, strlen(value)); |
| 1221 | } else if (!strcmp(node->name, "units")) { |
| 1222 | if (tpdf->units) { |
| 1223 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 1224 | goto error; |
| 1225 | } |
| 1226 | GETVAL(value, node, "name"); |
| 1227 | tpdf->units = lydict_insert(module->ctx, value, strlen(value)); |
| 1228 | } else { |
| 1229 | LOGVAL(VE_INSTMT, LOGLINE(node), value); |
| 1230 | r = 1; |
| 1231 | } |
| 1232 | lyxml_free_elem(module->ctx, node); |
| 1233 | if (r) { |
| 1234 | goto error; |
| 1235 | } |
| 1236 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1237 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1238 | /* check mandatory value */ |
| 1239 | if (!tpdf->type.der) { |
| 1240 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "type", yin->name); |
| 1241 | goto error; |
| 1242 | } |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1243 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1244 | /* check default value */ |
| 1245 | if (check_default(&tpdf->type, tpdf->dflt)) { |
| 1246 | goto error; |
| 1247 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1248 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1249 | return EXIT_SUCCESS; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1250 | |
| 1251 | error: |
| 1252 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1253 | return EXIT_FAILURE; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1256 | static struct ly_feature * |
| 1257 | resolve_feature(const char *name, struct ly_module *module, unsigned int line) |
| 1258 | { |
| 1259 | const char *prefix; |
| 1260 | unsigned int prefix_len = 0; |
| 1261 | int i, j, found = 0; |
| 1262 | |
| 1263 | assert(name); |
| 1264 | assert(module); |
| 1265 | |
| 1266 | /* check prefix */ |
| 1267 | prefix = name; |
| 1268 | name = strchr(prefix, ':'); |
| 1269 | if (name) { |
| 1270 | /* there is prefix */ |
| 1271 | prefix_len = name - prefix; |
| 1272 | name++; |
| 1273 | |
| 1274 | /* check whether the prefix points to the current module */ |
| 1275 | if (!strncmp(prefix, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 1276 | /* then ignore prefix and works as there is no prefix */ |
| 1277 | prefix_len = 0; |
| 1278 | } |
| 1279 | } else { |
| 1280 | /* no prefix, set pointers correctly */ |
| 1281 | name = prefix; |
| 1282 | } |
| 1283 | |
| 1284 | if (prefix_len) { |
| 1285 | /* search in imported modules */ |
| 1286 | for (i = 0; i < module->imp_size; i++) { |
| 1287 | if (!strncmp(module->imp[i].prefix, prefix, prefix_len) && !module->imp[i].prefix[prefix_len]) { |
| 1288 | module = module->imp[i].module; |
| 1289 | found = 1; |
| 1290 | break; |
| 1291 | } |
| 1292 | } |
| 1293 | if (!found) { |
| 1294 | /* identity refers unknown data model */ |
| 1295 | LOGVAL(VE_INPREFIX, line, prefix); |
| 1296 | return NULL; |
| 1297 | } |
| 1298 | } else { |
| 1299 | /* search in submodules */ |
| 1300 | for (i = 0; i < module->inc_size; i++) { |
| 1301 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
| 1302 | if (!strcmp(name, module->inc[i].submodule->features[j].name)) { |
| 1303 | return &(module->inc[i].submodule->features[j]); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | /* search in the identified module */ |
| 1310 | for (j = 0; j < module->features_size; j++) { |
| 1311 | if (!strcmp(name, module->features[j].name)) { |
| 1312 | return &module->features[j]; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* not found */ |
| 1317 | return NULL; |
| 1318 | } |
| 1319 | |
| 1320 | static int |
| 1321 | fill_yin_feature(struct ly_module *module, struct lyxml_elem *yin, struct ly_feature *f) |
| 1322 | { |
| 1323 | const char *value; |
| 1324 | struct lyxml_elem *child, *next; |
| 1325 | int c = 0; |
| 1326 | |
Radek Krejci | b05774c | 2015-06-18 13:52:59 +0200 | [diff] [blame] | 1327 | GETVAL(value, yin, "name"); |
| 1328 | if (check_identifier(value, LY_IDENT_FEATURE, LOGLINE(yin), module, NULL)) { |
| 1329 | goto error; |
| 1330 | } |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1331 | f->name = lydict_insert(module->ctx, value, strlen(value)); |
Radek Krejci | b05774c | 2015-06-18 13:52:59 +0200 | [diff] [blame] | 1332 | |
| 1333 | if (read_yin_common(module, NULL, (struct ly_mnode *)f, yin, 0)) { |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1334 | goto error; |
| 1335 | } |
| 1336 | |
| 1337 | LY_TREE_FOR_SAFE(yin->child, next, child) { |
| 1338 | if (!strcmp(child->name, "if-feature")) { |
| 1339 | c++; |
| 1340 | } else { |
| 1341 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 1342 | goto error; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | if (c) { |
| 1347 | f->features = calloc(c, sizeof *f->features); |
| 1348 | } |
| 1349 | |
| 1350 | LY_TREE_FOR_SAFE(yin->child, next, child) { |
| 1351 | GETVAL(value, child, "name"); |
| 1352 | f->features[f->features_size] = resolve_feature(value, module, LOGLINE(child)); |
| 1353 | if (!f->features[f->features_size]) { |
| 1354 | goto error; |
| 1355 | } |
| 1356 | f->features_size++; |
| 1357 | } |
| 1358 | |
| 1359 | /* tmp TODO remove */ |
| 1360 | f->flags |= LY_NODE_FENABLED; |
| 1361 | |
| 1362 | return EXIT_SUCCESS; |
| 1363 | |
| 1364 | error: |
| 1365 | |
| 1366 | return EXIT_FAILURE; |
| 1367 | } |
| 1368 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1369 | static int |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1370 | fill_yin_must(struct ly_module *module, struct lyxml_elem *yin, struct ly_restr *must) |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1371 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1372 | const char *value; |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1373 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1374 | GETVAL(value, yin, "condition"); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1375 | must->expr = lydict_insert(module->ctx, value, strlen(value)); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1376 | |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 1377 | return read_restr_substmt(module->ctx, must, yin); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1378 | |
Radek Krejci | 41726f9 | 2015-06-19 13:11:05 +0200 | [diff] [blame] | 1379 | error: /* GETVAL requires this label */ |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1380 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1381 | return EXIT_FAILURE; |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1384 | static int |
| 1385 | fill_yin_augment(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, struct ly_augment *aug) |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1386 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1387 | const char *value; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1388 | struct lyxml_elem *next, *child; |
| 1389 | int c = 0; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1390 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1391 | GETVAL(value, yin, "target-node"); |
| 1392 | aug->target_name = lydict_insert(module->ctx, value, 0); |
| 1393 | aug->parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1394 | |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1395 | if (read_yin_common(module, NULL, (struct ly_mnode *)aug, yin, 0)) { |
| 1396 | goto error; |
| 1397 | } |
| 1398 | |
| 1399 | LY_TREE_FOR_SAFE(yin->child, next, child) { |
| 1400 | if (!strcmp(child->name, "if-feature")) { |
| 1401 | c++; |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1402 | } else if (!strcmp(child->name, "when")) { |
| 1403 | if (aug->when) { |
| 1404 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 1405 | goto error; |
| 1406 | } |
| 1407 | |
| 1408 | aug->when = read_yin_when(module, child); |
| 1409 | lyxml_free_elem(module->ctx, child); |
| 1410 | |
| 1411 | if (!aug->when) { |
| 1412 | goto error; |
| 1413 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1414 | |
| 1415 | /* check allowed sub-statements */ |
| 1416 | } else if (strcmp(child->name, "anyxml") && strcmp(child->name, "case") && strcmp(child->name, "choice") && |
| 1417 | strcmp(child->name, "container") && strcmp(child->name, "leaf-list") && strcmp(child->name, "leaf") && |
| 1418 | strcmp(child->name, "list") && strcmp(child->name, "uses")) { |
| 1419 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 1420 | goto error; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | if (c) { |
| 1425 | aug->features = calloc(c, sizeof *aug->features); |
| 1426 | } |
| 1427 | |
| 1428 | LY_TREE_FOR_SAFE(yin->child, next, child) { |
| 1429 | if (!strcmp(child->name, "if-feature")) { |
| 1430 | GETVAL(value, child, "name"); |
| 1431 | aug->features[aug->features_size] = resolve_feature(value, module, LOGLINE(child)); |
| 1432 | if (!aug->features[aug->features_size]) { |
| 1433 | goto error; |
| 1434 | } |
| 1435 | aug->features_size++; |
| 1436 | } else { |
| 1437 | /* keep the data nodes */ |
| 1438 | continue; |
| 1439 | } |
| 1440 | |
| 1441 | lyxml_free_elem(module->ctx, child); |
| 1442 | } |
| 1443 | |
| 1444 | /* do not resolve data now, just keep the definition which will be parsed later |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1445 | * when we will have the target node |
| 1446 | */ |
| 1447 | lyxml_unlink_elem(yin); |
| 1448 | aug->child = (struct ly_mnode *)yin; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1449 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1450 | return EXIT_SUCCESS; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1451 | |
| 1452 | error: |
| 1453 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1454 | return EXIT_FAILURE; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1457 | static int |
| 1458 | fill_yin_refine(struct ly_module *module, struct lyxml_elem *yin, struct ly_refine *rfn) |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1459 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1460 | struct lyxml_elem *sub, *next; |
| 1461 | const char *value; |
| 1462 | char *endptr; |
| 1463 | int f_mand = 0, f_min = 0, f_max = 0; |
| 1464 | int c_must = 0; |
| 1465 | int r; |
| 1466 | unsigned long int val; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1467 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1468 | GETVAL(value, yin, "target-node"); |
| 1469 | rfn->target = lydict_insert(module->ctx, value, strlen(value)); |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1470 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1471 | if (read_yin_common(module, NULL, (struct ly_mnode *)rfn, yin, OPT_CONFIG)) { |
| 1472 | goto error; |
| 1473 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1474 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1475 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 1476 | /* limited applicability */ |
| 1477 | if (!strcmp(sub->name, "default")) { |
| 1478 | /* leaf or choice */ |
| 1479 | if (rfn->mod.dflt) { |
| 1480 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1481 | goto error; |
| 1482 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1483 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1484 | /* check possibility of statements combination */ |
| 1485 | if (rfn->target_type) { |
| 1486 | rfn->target_type &= (LY_NODE_LEAF | LY_NODE_CHOICE); |
| 1487 | if (!rfn->target_type) { |
| 1488 | LOGVAL(VE_SPEC, LOGLINE(sub), "invalid combination of refine substatements"); |
| 1489 | goto error; |
| 1490 | } |
| 1491 | } else { |
| 1492 | rfn->target_type = LY_NODE_LEAF | LY_NODE_CHOICE; |
| 1493 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1494 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1495 | GETVAL(value, sub, "value"); |
| 1496 | rfn->mod.dflt = lydict_insert(module->ctx, value, strlen(value)); |
| 1497 | } else if (!strcmp(sub->name, "mandatory")) { |
| 1498 | /* leaf, choice or anyxml */ |
| 1499 | if (f_mand) { |
| 1500 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1501 | goto error; |
| 1502 | } |
| 1503 | /* just checking the flags in leaf is not sufficient, we would allow |
| 1504 | * multiple mandatory statements with the "false" value |
| 1505 | */ |
| 1506 | f_mand = 1; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1507 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1508 | /* check possibility of statements combination */ |
| 1509 | if (rfn->target_type) { |
| 1510 | rfn->target_type &= (LY_NODE_LEAF | LY_NODE_CHOICE | LY_NODE_ANYXML); |
| 1511 | if (!rfn->target_type) { |
| 1512 | LOGVAL(VE_SPEC, LOGLINE(sub), "invalid combination of refine substatements"); |
| 1513 | goto error; |
| 1514 | } |
| 1515 | } else { |
| 1516 | rfn->target_type = LY_NODE_LEAF | LY_NODE_CHOICE | LY_NODE_ANYXML; |
| 1517 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1518 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1519 | GETVAL(value, sub, "value"); |
| 1520 | if (!strcmp(value, "true")) { |
| 1521 | rfn->flags |= LY_NODE_MAND_TRUE; |
| 1522 | } else if (!strcmp(value, "false")) { |
| 1523 | rfn->flags |= LY_NODE_MAND_FALSE; |
| 1524 | } else { |
| 1525 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1526 | goto error; |
| 1527 | } |
| 1528 | } else if (!strcmp(sub->name, "min-elements")) { |
| 1529 | /* list or leaf-list */ |
| 1530 | if (f_min) { |
| 1531 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1532 | goto error; |
| 1533 | } |
| 1534 | f_min = 1; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1535 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1536 | /* check possibility of statements combination */ |
| 1537 | if (rfn->target_type) { |
| 1538 | rfn->target_type &= (LY_NODE_LIST | LY_NODE_LEAFLIST); |
| 1539 | if (!rfn->target_type) { |
| 1540 | LOGVAL(VE_SPEC, LOGLINE(sub), "invalid combination of refine substatements"); |
| 1541 | goto error; |
| 1542 | } |
| 1543 | } else { |
| 1544 | rfn->target_type = LY_NODE_LIST | LY_NODE_LEAFLIST; |
| 1545 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1546 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1547 | GETVAL(value, sub, "value"); |
| 1548 | while (isspace(value[0])) { |
| 1549 | value++; |
| 1550 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1551 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1552 | /* convert it to uint32_t */ |
| 1553 | errno = 0; |
| 1554 | endptr = NULL; |
| 1555 | val = strtoul(value, &endptr, 10); |
| 1556 | if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) { |
| 1557 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1558 | goto error; |
| 1559 | } |
| 1560 | rfn->mod.list.min = (uint32_t) val; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1561 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1562 | /* magic - bit 3 in flags means min set */ |
| 1563 | rfn->flags |= 0x04; |
| 1564 | } else if (!strcmp(sub->name, "max-elements")) { |
| 1565 | /* list or leaf-list */ |
| 1566 | if (f_max) { |
| 1567 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1568 | goto error; |
| 1569 | } |
| 1570 | f_max = 1; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1571 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1572 | /* check possibility of statements combination */ |
| 1573 | if (rfn->target_type) { |
| 1574 | rfn->target_type &= (LY_NODE_LIST | LY_NODE_LEAFLIST); |
| 1575 | if (!rfn->target_type) { |
| 1576 | LOGVAL(VE_SPEC, LOGLINE(sub), "invalid combination of refine substatements"); |
| 1577 | goto error; |
| 1578 | } |
| 1579 | } else { |
| 1580 | rfn->target_type = LY_NODE_LIST | LY_NODE_LEAFLIST; |
| 1581 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1582 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1583 | GETVAL(value, sub, "value"); |
| 1584 | while (isspace(value[0])) { |
| 1585 | value++; |
| 1586 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1587 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1588 | /* convert it to uint32_t */ |
| 1589 | errno = 0; |
| 1590 | endptr = NULL; |
| 1591 | val = strtoul(value, &endptr, 10); |
| 1592 | if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) { |
| 1593 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1594 | goto error; |
| 1595 | } |
| 1596 | rfn->mod.list.max = (uint32_t) val; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1597 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1598 | /* magic - bit 4 in flags means min set */ |
| 1599 | rfn->flags |= 0x08; |
| 1600 | } else if (!strcmp(sub->name, "presence")) { |
| 1601 | /* container */ |
| 1602 | if (rfn->mod.presence) { |
| 1603 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 1604 | goto error; |
| 1605 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1606 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1607 | /* check possibility of statements combination */ |
| 1608 | if (rfn->target_type) { |
| 1609 | rfn->target_type &= LY_NODE_CONTAINER; |
| 1610 | if (!rfn->target_type) { |
| 1611 | LOGVAL(VE_SPEC, LOGLINE(sub), "invalid combination of refine substatements"); |
| 1612 | goto error; |
| 1613 | } |
| 1614 | } else { |
| 1615 | rfn->target_type = LY_NODE_CONTAINER; |
| 1616 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1617 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1618 | GETVAL(value, sub, "value"); |
| 1619 | rfn->mod.presence = lydict_insert(module->ctx, value, strlen(value)); |
| 1620 | } else if (!strcmp(sub->name, "must")) { |
| 1621 | /* leaf-list, list, container or anyxml */ |
| 1622 | /* check possibility of statements combination */ |
| 1623 | if (rfn->target_type) { |
| 1624 | rfn->target_type &= (LY_NODE_LIST | LY_NODE_LEAFLIST | LY_NODE_CONTAINER | LY_NODE_ANYXML); |
| 1625 | if (!rfn->target_type) { |
| 1626 | LOGVAL(VE_SPEC, LOGLINE(sub), "invalid combination of refine substatements"); |
| 1627 | goto error; |
| 1628 | } |
| 1629 | } else { |
| 1630 | rfn->target_type = LY_NODE_LIST | LY_NODE_LEAFLIST | LY_NODE_CONTAINER | LY_NODE_ANYXML; |
| 1631 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1632 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1633 | c_must++; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1634 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1635 | } else { |
| 1636 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 1637 | goto error; |
| 1638 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1639 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1640 | lyxml_free_elem(module->ctx, sub); |
| 1641 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1642 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1643 | /* process nodes with cardinality of 0..n */ |
| 1644 | if (c_must) { |
| 1645 | rfn->must = calloc(c_must, sizeof *rfn->must); |
| 1646 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1647 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1648 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 1649 | if (!strcmp(sub->name, "must")) { |
| 1650 | r = fill_yin_must(module, sub, &rfn->must[rfn->must_size]); |
| 1651 | rfn->must_size++; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1652 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1653 | if (r) { |
| 1654 | goto error; |
| 1655 | } |
| 1656 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1657 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1658 | lyxml_free_elem(module->ctx, sub); |
| 1659 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1660 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1661 | return EXIT_SUCCESS; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1662 | |
| 1663 | error: |
| 1664 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1665 | return EXIT_FAILURE; |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 1666 | } |
| 1667 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1668 | static int |
| 1669 | fill_yin_import(struct ly_module *module, struct lyxml_elem *yin, struct ly_import *imp) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1670 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1671 | struct lyxml_elem *child; |
| 1672 | const char *value; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1673 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1674 | LY_TREE_FOR(yin->child, child) { |
| 1675 | if (!strcmp(child->name, "prefix")) { |
| 1676 | GETVAL(value, child, "value"); |
| 1677 | if (check_identifier(value, LY_IDENT_PREFIX, LOGLINE(child), module, NULL)) { |
| 1678 | goto error; |
| 1679 | } |
| 1680 | imp->prefix = lydict_insert(module->ctx, value, strlen(value)); |
| 1681 | } else if (!strcmp(child->name, "revision-date")) { |
| 1682 | if (imp->rev[0]) { |
| 1683 | LOGVAL(VE_TOOMANY, LOGLINE(child), "revision-date", yin->name); |
| 1684 | goto error; |
| 1685 | } |
| 1686 | GETVAL(value, child, "date"); |
| 1687 | if (check_date(value, LOGLINE(child))) { |
| 1688 | goto error; |
| 1689 | } |
| 1690 | memcpy(imp->rev, value, LY_REV_SIZE - 1); |
| 1691 | } else { |
| 1692 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 1693 | goto error; |
| 1694 | } |
| 1695 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1696 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1697 | /* check mandatory information */ |
| 1698 | if (!imp->prefix) { |
| 1699 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "prefix", yin->name); |
| 1700 | goto error; |
| 1701 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1702 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1703 | GETVAL(value, yin, "module"); |
| 1704 | imp->module = ly_ctx_get_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL, 1); |
| 1705 | if (!imp->module) { |
| 1706 | LOGERR(LY_EVALID, "Importing \"%s\" module into \"%s\" failed.", value, module->name); |
| 1707 | LOGVAL(VE_INARG, LOGLINE(yin), value, yin->name); |
| 1708 | goto error; |
| 1709 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1710 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1711 | return EXIT_SUCCESS; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1712 | |
| 1713 | error: |
| 1714 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1715 | return EXIT_FAILURE; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1718 | static int |
| 1719 | fill_yin_include(struct ly_module *module, struct lyxml_elem *yin, struct ly_include *inc) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1720 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1721 | struct lyxml_elem *child; |
| 1722 | const char *value; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1723 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1724 | LY_TREE_FOR(yin->child, child) { |
| 1725 | if (!strcmp(child->name, "revision-date")) { |
| 1726 | if (inc->rev[0]) { |
| 1727 | LOGVAL(VE_TOOMANY, LOGLINE(child), "revision-date", yin->name); |
| 1728 | goto error; |
| 1729 | } |
| 1730 | GETVAL(value, child, "date"); |
| 1731 | if (check_date(value, LOGLINE(child))) { |
| 1732 | goto error; |
| 1733 | } |
| 1734 | memcpy(inc->rev, value, LY_REV_SIZE - 1); |
| 1735 | } else { |
| 1736 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 1737 | goto error; |
| 1738 | } |
| 1739 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1740 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1741 | GETVAL(value, yin, "module"); |
| 1742 | inc->submodule = ly_ctx_get_submodule(module, value, inc->rev[0] ? inc->rev : NULL); |
| 1743 | if (!inc->submodule) { |
| 1744 | LOGERR(LY_EVALID, "Including \"%s\" module into \"%s\" failed.", value, module->name); |
| 1745 | LOGVAL(VE_INARG, LOGLINE(yin), value, yin->name); |
| 1746 | goto error; |
| 1747 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1748 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1749 | /* check that belongs-to corresponds */ |
| 1750 | if (module->type) { |
| 1751 | module = ((struct ly_submodule *)module)->belongsto; |
| 1752 | } |
| 1753 | if (inc->submodule->belongsto != module) { |
| 1754 | LOGVAL(VE_INARG, LOGLINE(yin), value, yin->name); |
| 1755 | LOGVAL(VE_SPEC, 0, "The included module does not belongs-to the \"%s\" module", module->name); |
| 1756 | goto error; |
| 1757 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1758 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1759 | return EXIT_SUCCESS; |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 1760 | |
| 1761 | error: |
| 1762 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1763 | return EXIT_FAILURE; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1764 | } |
| 1765 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1766 | /* |
| 1767 | * Covers: |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 1768 | * description, reference, status, optionaly config |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 1769 | * |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1770 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1771 | static int |
| 1772 | read_yin_common(struct ly_module *module, struct ly_mnode *parent, |
| 1773 | struct ly_mnode *mnode, struct lyxml_elem *xmlnode, int opt) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1774 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1775 | const char *value; |
| 1776 | struct lyxml_elem *sub, *next; |
| 1777 | struct ly_ctx *const ctx = module->ctx; |
| 1778 | int r = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1779 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1780 | if (opt & OPT_MODULE) { |
| 1781 | mnode->module = module; |
| 1782 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1783 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1784 | if (opt & OPT_IDENT) { |
| 1785 | GETVAL(value, xmlnode, "name"); |
| 1786 | if (check_identifier(value, LY_IDENT_NAME, LOGLINE(xmlnode), NULL, NULL)) { |
| 1787 | goto error; |
| 1788 | } |
| 1789 | mnode->name = lydict_insert(ctx, value, strlen(value)); |
| 1790 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1791 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1792 | /* process local parameters */ |
| 1793 | LY_TREE_FOR_SAFE(xmlnode->child, next, sub) { |
| 1794 | if (!strcmp(sub->name, "description")) { |
| 1795 | if (mnode->dsc) { |
| 1796 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name); |
| 1797 | goto error; |
| 1798 | } |
| 1799 | mnode->dsc = read_yin_subnode(ctx, sub, "text"); |
| 1800 | if (!mnode->dsc) { |
| 1801 | r = 1; |
| 1802 | } |
| 1803 | } else if (!strcmp(sub->name, "reference")) { |
| 1804 | if (mnode->ref) { |
| 1805 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name); |
| 1806 | goto error; |
| 1807 | } |
| 1808 | mnode->ref = read_yin_subnode(ctx, sub, "text"); |
| 1809 | if (!mnode->ref) { |
| 1810 | r = 1; |
| 1811 | } |
| 1812 | } else if (!strcmp(sub->name, "status")) { |
| 1813 | if (mnode->flags & LY_NODE_STATUS_MASK) { |
| 1814 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name); |
| 1815 | goto error; |
| 1816 | } |
| 1817 | GETVAL(value, sub, "value"); |
| 1818 | if (!strcmp(value, "current")) { |
| 1819 | mnode->flags |= LY_NODE_STATUS_CURR; |
| 1820 | } else if (!strcmp(value, "deprecated")) { |
| 1821 | mnode->flags |= LY_NODE_STATUS_DEPRC; |
| 1822 | } else if (!strcmp(value, "obsolete")) { |
| 1823 | mnode->flags |= LY_NODE_STATUS_OBSLT; |
| 1824 | } else { |
| 1825 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1826 | r = 1; |
| 1827 | } |
| 1828 | } else if ((opt & OPT_CONFIG) && !strcmp(sub->name, "config")) { |
| 1829 | if (mnode->flags & LY_NODE_CONFIG_MASK) { |
| 1830 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name); |
| 1831 | goto error; |
| 1832 | } |
| 1833 | GETVAL(value, sub, "value"); |
| 1834 | if (!strcmp(value, "false")) { |
| 1835 | mnode->flags |= LY_NODE_CONFIG_R; |
| 1836 | } else if (!strcmp(value, "true")) { |
| 1837 | mnode->flags |= LY_NODE_CONFIG_W; |
| 1838 | } else { |
| 1839 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 1840 | r = 1; |
| 1841 | } |
| 1842 | } else { |
| 1843 | /* skip the lyxml_free_elem */ |
| 1844 | continue; |
| 1845 | } |
| 1846 | lyxml_free_elem(ctx, sub); |
| 1847 | if (r) { |
| 1848 | goto error; |
| 1849 | } |
| 1850 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1851 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1852 | if ((opt & OPT_INHERIT) && !(mnode->flags & LY_NODE_CONFIG_MASK)) { |
| 1853 | /* get config flag from parent */ |
| 1854 | if (parent) { |
| 1855 | mnode->flags |= parent->flags & LY_NODE_CONFIG_MASK; |
| 1856 | } else { |
| 1857 | /* default config is true */ |
| 1858 | mnode->flags |= LY_NODE_CONFIG_W; |
| 1859 | } |
| 1860 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1861 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1862 | return EXIT_SUCCESS; |
Radek Krejci | eac3553 | 2015-05-31 19:09:15 +0200 | [diff] [blame] | 1863 | |
| 1864 | error: |
| 1865 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1866 | return EXIT_FAILURE; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1869 | static struct ly_when * |
| 1870 | read_yin_when(struct ly_module *module,struct lyxml_elem *yin) |
| 1871 | { |
Radek Krejci | 53ebfb1 | 2015-06-19 09:35:59 +0200 | [diff] [blame] | 1872 | struct ly_when *retval = NULL; |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1873 | struct lyxml_elem *next, *child; |
| 1874 | const char *value; |
| 1875 | |
| 1876 | retval = calloc(1, sizeof *retval); |
| 1877 | |
| 1878 | GETVAL(value, yin, "condition"); |
| 1879 | retval->cond = lydict_insert(module->ctx, value, 0); |
| 1880 | |
| 1881 | LY_TREE_FOR_SAFE(yin->child, next, child) { |
| 1882 | if (!strcmp(child->name, "description")) { |
| 1883 | if (retval->dsc) { |
| 1884 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 1885 | goto error; |
| 1886 | } |
| 1887 | retval->dsc = read_yin_subnode(module->ctx, child, "text"); |
| 1888 | if (!retval->dsc) { |
| 1889 | goto error; |
| 1890 | } |
| 1891 | } else if (!strcmp(child->name, "reference")) { |
| 1892 | if (retval->ref) { |
| 1893 | LOGVAL(VE_TOOMANY, LOGLINE(child), child->name, yin->name); |
| 1894 | goto error; |
| 1895 | } |
| 1896 | retval->ref = read_yin_subnode(module->ctx, child, "text"); |
| 1897 | if (!retval->ref) { |
| 1898 | goto error; |
| 1899 | } |
| 1900 | } else { |
| 1901 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 1902 | goto error; |
| 1903 | } |
| 1904 | |
| 1905 | lyxml_free_elem(module->ctx, child); |
| 1906 | } |
| 1907 | |
| 1908 | return retval; |
| 1909 | |
| 1910 | error: |
| 1911 | |
Radek Krejci | 53ebfb1 | 2015-06-19 09:35:59 +0200 | [diff] [blame] | 1912 | ly_mnode_free((struct ly_mnode *)retval); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1913 | return NULL; |
| 1914 | } |
| 1915 | |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1916 | /* additional check in case statement - the child must be unique across |
| 1917 | * all other case names and its data children |
| 1918 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1919 | static int |
| 1920 | check_branch_id(struct ly_mnode *parent, struct ly_mnode *new, struct ly_mnode *excl, int line) |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1921 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1922 | struct ly_mnode *mnode, *submnode; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1923 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1924 | if (new->nodetype == LY_NODE_CHOICE) { |
| 1925 | /* we have nested choice in case, so we need recursion */ |
| 1926 | LY_TREE_FOR(new->child, mnode) { |
| 1927 | if (mnode->nodetype == LY_NODE_CASE) { |
| 1928 | LY_TREE_FOR(mnode->child, submnode) { |
| 1929 | if (check_branch_id(parent, submnode, new, line)) { |
| 1930 | return EXIT_FAILURE; |
| 1931 | } |
| 1932 | } |
| 1933 | } else if (check_branch_id(parent, mnode, new, line)) { |
| 1934 | return EXIT_FAILURE; |
| 1935 | } |
| 1936 | } |
| 1937 | } else { |
| 1938 | LY_TREE_FOR(parent->child, mnode) { |
| 1939 | if (mnode == excl) { |
| 1940 | continue; |
| 1941 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1942 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1943 | if (!strcmp(new->name, mnode->name)) { |
| 1944 | LOGVAL(VE_INID, line, new->name, "duplicated identifier within a choice's cases"); |
| 1945 | return EXIT_FAILURE; |
| 1946 | } |
| 1947 | if (mnode->nodetype == LY_NODE_CASE) { |
| 1948 | LY_TREE_FOR(mnode->child, submnode) { |
| 1949 | if (!strcmp(new->name, submnode->name)) { |
| 1950 | LOGVAL(VE_INID, line, new->name, "duplicated identifier within a choice's cases"); |
| 1951 | return EXIT_FAILURE; |
| 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1957 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1958 | return EXIT_SUCCESS; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1959 | } |
| 1960 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1961 | static struct ly_mnode * |
| 1962 | read_yin_case(struct ly_module *module, |
| 1963 | struct ly_mnode *parent, struct lyxml_elem *yin, int resolve, struct mnode_list **unres) |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1964 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1965 | struct lyxml_elem *sub, *next; |
| 1966 | struct ly_mnode_case *mcase; |
| 1967 | struct ly_mnode *retval, *mnode = NULL; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1968 | int c_ftrs = 0; |
| 1969 | const char *value; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1970 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1971 | mcase = calloc(1, sizeof *mcase); |
| 1972 | mcase->nodetype = LY_NODE_CASE; |
| 1973 | mcase->prev = (struct ly_mnode *)mcase; |
| 1974 | retval = (struct ly_mnode *)mcase; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1975 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1976 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_INHERIT)) { |
| 1977 | goto error; |
| 1978 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 1979 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1980 | /* process choice's specific children */ |
| 1981 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 1982 | if (!strcmp(sub->name, "container")) { |
| 1983 | mnode = read_yin_container(module, retval, sub, resolve, unres); |
| 1984 | } else if (!strcmp(sub->name, "leaf-list")) { |
| 1985 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
| 1986 | } else if (!strcmp(sub->name, "leaf")) { |
| 1987 | mnode = read_yin_leaf(module, retval, sub, resolve); |
| 1988 | } else if (!strcmp(sub->name, "list")) { |
| 1989 | mnode = read_yin_list(module, retval, sub, resolve, unres); |
| 1990 | } else if (!strcmp(sub->name, "uses")) { |
| 1991 | mnode = read_yin_uses(module, retval, sub, resolve, unres); |
| 1992 | } else if (!strcmp(sub->name, "choice")) { |
| 1993 | mnode = read_yin_choice(module, retval, sub, resolve, unres); |
| 1994 | } else if (!strcmp(sub->name, "anyxml")) { |
| 1995 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1996 | } else if (!strcmp(sub->name, "if-feature")) { |
| 1997 | c_ftrs++; |
| 1998 | |
| 1999 | /* skip lyxml_free_elem() at the end of the loop, sub is processed later */ |
| 2000 | continue; |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2001 | } else if (!strcmp(sub->name, "when")) { |
| 2002 | if (mcase->when) { |
| 2003 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2004 | goto error; |
| 2005 | } |
| 2006 | |
| 2007 | mcase->when = read_yin_when(module, sub); |
| 2008 | if (!mcase->when) { |
| 2009 | goto error; |
| 2010 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2011 | } else { |
| 2012 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 2013 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2014 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2015 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2016 | if (!mnode) { |
| 2017 | goto error; |
| 2018 | } else if (check_branch_id(parent, mnode, mnode, LOGLINE(sub))) { |
| 2019 | goto error; |
| 2020 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2021 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2022 | mnode = NULL; |
| 2023 | lyxml_free_elem(module->ctx, sub); |
| 2024 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2025 | |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2026 | if (c_ftrs) { |
| 2027 | mcase->features = calloc(c_ftrs, sizeof *mcase->features); |
| 2028 | } |
| 2029 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2030 | GETVAL(value, sub, "name"); |
| 2031 | mcase->features[mcase->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 2032 | if (!mcase->features[mcase->features_size]) { |
| 2033 | goto error; |
| 2034 | } |
| 2035 | mcase->features_size++; |
| 2036 | lyxml_free_elem(module->ctx, sub); |
| 2037 | } |
| 2038 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2039 | /* inherit config flag */ |
| 2040 | if (parent) { |
| 2041 | retval->flags |= parent->flags & LY_NODE_CONFIG_MASK; |
| 2042 | } else { |
| 2043 | /* default config is true */ |
| 2044 | retval->flags |= LY_NODE_CONFIG_W; |
| 2045 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2046 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2047 | /* insert the node into the schema tree */ |
| 2048 | if (ly_mnode_addchild(parent, retval)) { |
| 2049 | goto error; |
| 2050 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 2051 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2052 | return retval; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2053 | |
| 2054 | error: |
| 2055 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2056 | ly_mnode_free(retval); |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2057 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2058 | return NULL; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2059 | } |
| 2060 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2061 | static struct ly_mnode * |
| 2062 | read_yin_choice(struct ly_module *module, |
| 2063 | struct ly_mnode *parent, struct lyxml_elem *yin, int resolve, struct mnode_list **unres) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2064 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2065 | struct lyxml_elem *sub, *next; |
| 2066 | struct ly_ctx *const ctx = module->ctx; |
| 2067 | struct ly_mnode *retval, *mnode = NULL; |
| 2068 | struct ly_mnode_choice *choice; |
| 2069 | const char *value; |
| 2070 | char *dflt_str = NULL; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2071 | int f_mand = 0, c_ftrs = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2072 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2073 | choice = calloc(1, sizeof *choice); |
| 2074 | choice->nodetype = LY_NODE_CHOICE; |
| 2075 | choice->prev = (struct ly_mnode *)choice; |
| 2076 | retval = (struct ly_mnode *)choice; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2077 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2078 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
| 2079 | goto error; |
| 2080 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2081 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2082 | /* process choice's specific children */ |
| 2083 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2084 | if (!strcmp(sub->name, "container")) { |
| 2085 | if (!(mnode = read_yin_container(module, retval, sub, resolve, unres))) { |
| 2086 | goto error; |
| 2087 | } |
| 2088 | } else if (!strcmp(sub->name, "leaf-list")) { |
| 2089 | if (!(mnode = read_yin_leaflist(module, retval, sub, resolve))) { |
| 2090 | goto error; |
| 2091 | } |
| 2092 | } else if (!strcmp(sub->name, "leaf")) { |
| 2093 | if (!(mnode = read_yin_leaf(module, retval, sub, resolve))) { |
| 2094 | goto error; |
| 2095 | } |
| 2096 | } else if (!strcmp(sub->name, "list")) { |
| 2097 | if (!(mnode = read_yin_list(module, retval, sub, resolve, unres))) { |
| 2098 | goto error; |
| 2099 | } |
| 2100 | } else if (!strcmp(sub->name, "case")) { |
| 2101 | if (!(mnode = read_yin_case(module, retval, sub, resolve, unres))) { |
| 2102 | goto error; |
| 2103 | } |
| 2104 | } else if (!strcmp(sub->name, "anyxml")) { |
| 2105 | if (!(mnode = read_yin_anyxml(module, retval, sub, resolve))) { |
| 2106 | goto error; |
| 2107 | } |
| 2108 | } else if (!strcmp(sub->name, "default")) { |
| 2109 | if (dflt_str) { |
| 2110 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2111 | goto error; |
| 2112 | } |
| 2113 | GETVAL(value, sub, "value"); |
| 2114 | dflt_str = strdup(value); |
| 2115 | } else if (!strcmp(sub->name, "mandatory")) { |
| 2116 | if (f_mand) { |
| 2117 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2118 | goto error; |
| 2119 | } |
| 2120 | /* just checking the flags in leaf is not sufficient, we would allow |
| 2121 | * multiple mandatory statements with the "false" value |
| 2122 | */ |
| 2123 | f_mand = 1; |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2124 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2125 | GETVAL(value, sub, "value"); |
| 2126 | if (!strcmp(value, "true")) { |
| 2127 | choice->flags |= LY_NODE_MAND_TRUE; |
| 2128 | } else if (strcmp(value, "false")) { |
| 2129 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2130 | goto error; |
| 2131 | } /* else false is the default value, so we can ignore it */ |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2132 | } else if (!strcmp(sub->name, "when")) { |
| 2133 | if (choice->when) { |
| 2134 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2135 | goto error; |
| 2136 | } |
| 2137 | |
| 2138 | choice->when = read_yin_when(module, sub); |
| 2139 | if (!choice->when) { |
| 2140 | goto error; |
| 2141 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2142 | } else if (!strcmp(sub->name, "if-feature")) { |
| 2143 | c_ftrs++; |
| 2144 | |
| 2145 | /* skip lyxml_free_elem() at the end of the loop, the sub node is processed later */ |
| 2146 | continue; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2147 | } else { |
| 2148 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 2149 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2150 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2151 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2152 | if (mnode && check_branch_id(retval, mnode, mnode, LOGLINE(sub))) { |
| 2153 | goto error; |
| 2154 | } |
| 2155 | mnode = NULL; |
| 2156 | lyxml_free_elem(ctx, sub); |
| 2157 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2158 | |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2159 | if (c_ftrs) { |
| 2160 | choice->features = calloc(c_ftrs, sizeof *choice->features); |
| 2161 | } |
| 2162 | |
| 2163 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2164 | GETVAL(value, sub, "name"); |
| 2165 | choice->features[choice->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 2166 | if (!choice->features[choice->features_size]) { |
| 2167 | goto error; |
| 2168 | } |
| 2169 | choice->features_size++; |
| 2170 | lyxml_free_elem(ctx, sub); |
| 2171 | } |
| 2172 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2173 | /* check - default is prohibited in combination with mandatory */ |
| 2174 | if (dflt_str && (choice->flags & LY_NODE_MAND_TRUE)) { |
| 2175 | LOGVAL(VE_SPEC, LOGLINE(yin), |
| 2176 | "The \"default\" statement MUST NOT be present on choices where \"mandatory\" is true."); |
| 2177 | goto error; |
| 2178 | } |
Radek Krejci | b4cf202 | 2015-06-03 14:40:05 +0200 | [diff] [blame] | 2179 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2180 | /* link default with the case */ |
| 2181 | if (dflt_str) { |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 2182 | choice->dflt = resolve_schema_nodeid(dflt_str, retval, module, LY_NODE_CHOICE); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2183 | if (!choice->dflt) { |
| 2184 | /* default branch not found */ |
| 2185 | LOGVAL(VE_INARG, LOGLINE(yin), dflt_str, "default"); |
| 2186 | goto error; |
| 2187 | } |
| 2188 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2189 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2190 | /* insert the node into the schema tree */ |
| 2191 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 2192 | goto error; |
| 2193 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2194 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2195 | free(dflt_str); |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 2196 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2197 | return retval; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2198 | |
| 2199 | error: |
| 2200 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2201 | ly_mnode_free(retval); |
| 2202 | free(dflt_str); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2203 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2204 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2205 | } |
| 2206 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2207 | static struct ly_mnode * |
| 2208 | read_yin_anyxml(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, int resolve) |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2209 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2210 | struct ly_mnode *retval; |
| 2211 | struct ly_mnode_leaf *anyxml; |
| 2212 | struct lyxml_elem *sub, *next; |
| 2213 | const char *value; |
| 2214 | int r; |
| 2215 | int f_mand = 0; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2216 | int c_must = 0, c_ftrs = 0; |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2217 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2218 | anyxml = calloc(1, sizeof *anyxml); |
| 2219 | anyxml->nodetype = LY_NODE_ANYXML; |
| 2220 | anyxml->prev = (struct ly_mnode *)anyxml; |
| 2221 | retval = (struct ly_mnode *)anyxml; |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2222 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2223 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
| 2224 | goto error; |
| 2225 | } |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2226 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2227 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2228 | if (!strcmp(sub->name, "mandatory")) { |
| 2229 | if (f_mand) { |
| 2230 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2231 | goto error; |
| 2232 | } |
| 2233 | /* just checking the flags in leaf is not sufficient, we would allow |
| 2234 | * multiple mandatory statements with the "false" value |
| 2235 | */ |
| 2236 | f_mand = 1; |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2237 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2238 | GETVAL(value, sub, "value"); |
| 2239 | if (!strcmp(value, "true")) { |
| 2240 | anyxml->flags |= LY_NODE_MAND_TRUE; |
| 2241 | } else if (strcmp(value, "false")) { |
| 2242 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2243 | goto error; |
| 2244 | } |
| 2245 | /* else false is the default value, so we can ignore it */ |
| 2246 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2247 | } else if (!strcmp(sub->name, "when")) { |
| 2248 | if (anyxml->when) { |
| 2249 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2250 | goto error; |
| 2251 | } |
| 2252 | |
| 2253 | anyxml->when = read_yin_when(module, sub); |
| 2254 | lyxml_free_elem(module->ctx, sub); |
| 2255 | |
| 2256 | if (!anyxml->when) { |
| 2257 | goto error; |
| 2258 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2259 | } else if (!strcmp(sub->name, "must")) { |
| 2260 | c_must++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2261 | } else if (!strcmp(sub->name, "if-feature")) { |
| 2262 | c_ftrs++; |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2263 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2264 | } else { |
| 2265 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 2266 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2267 | } |
| 2268 | } |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2269 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2270 | /* middle part - process nodes with cardinality of 0..n */ |
| 2271 | if (c_must) { |
| 2272 | anyxml->must = calloc(c_must, sizeof *anyxml->must); |
| 2273 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2274 | if (c_ftrs) { |
| 2275 | anyxml->features = calloc(c_ftrs, sizeof *anyxml->features); |
| 2276 | } |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2277 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2278 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2279 | if (!strcmp(sub->name, "must")) { |
| 2280 | r = fill_yin_must(module, sub, &anyxml->must[anyxml->must_size]); |
| 2281 | anyxml->must_size++; |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2282 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2283 | if (r) { |
| 2284 | goto error; |
| 2285 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2286 | } else if (!strcmp(sub->name, "must")) { |
| 2287 | GETVAL(value, sub, "name"); |
| 2288 | anyxml->features[anyxml->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 2289 | if (!anyxml->features[anyxml->features_size]) { |
| 2290 | goto error; |
| 2291 | } |
| 2292 | anyxml->features_size++; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2293 | } |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2294 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2295 | lyxml_free_elem(module->ctx, sub); |
| 2296 | } |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2297 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2298 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 2299 | goto error; |
| 2300 | } |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2301 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2302 | return retval; |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2303 | |
| 2304 | error: |
| 2305 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2306 | ly_mnode_free(retval); |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2307 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2308 | return NULL; |
Radek Krejci | 863c285 | 2015-06-03 15:47:11 +0200 | [diff] [blame] | 2309 | } |
| 2310 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2311 | static struct ly_mnode * |
| 2312 | read_yin_leaf(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, int resolve) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2313 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2314 | struct ly_mnode *retval; |
| 2315 | struct ly_mnode_leaf *leaf; |
| 2316 | struct lyxml_elem *sub, *next; |
| 2317 | const char *value; |
| 2318 | int r; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2319 | int c_must = 0, c_ftrs = 0, f_mand = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2320 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2321 | leaf = calloc(1, sizeof *leaf); |
| 2322 | leaf->nodetype = LY_NODE_LEAF; |
| 2323 | leaf->prev = (struct ly_mnode *)leaf; |
| 2324 | retval = (struct ly_mnode *)leaf; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2325 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2326 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
| 2327 | goto error; |
| 2328 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2329 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2330 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2331 | if (!strcmp(sub->name, "type")) { |
| 2332 | if (leaf->type.der) { |
| 2333 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2334 | goto error; |
| 2335 | } |
| 2336 | if (fill_yin_type(module, parent, sub, &leaf->type)) { |
| 2337 | goto error; |
| 2338 | } |
| 2339 | } else if (!strcmp(sub->name, "default")) { |
| 2340 | if (leaf->dflt) { |
| 2341 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2342 | goto error; |
| 2343 | } |
| 2344 | GETVAL(value, sub, "value"); |
| 2345 | leaf->dflt = lydict_insert(module->ctx, value, strlen(value)); |
| 2346 | } else if (!strcmp(sub->name, "units")) { |
| 2347 | if (leaf->units) { |
| 2348 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2349 | goto error; |
| 2350 | } |
| 2351 | GETVAL(value, sub, "name"); |
| 2352 | leaf->units = lydict_insert(module->ctx, value, strlen(value)); |
| 2353 | } else if (!strcmp(sub->name, "mandatory")) { |
| 2354 | if (f_mand) { |
| 2355 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2356 | goto error; |
| 2357 | } |
| 2358 | /* just checking the flags in leaf is not sufficient, we would allow |
| 2359 | * multiple mandatory statements with the "false" value |
| 2360 | */ |
| 2361 | f_mand = 1; |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2362 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2363 | GETVAL(value, sub, "value"); |
| 2364 | if (!strcmp(value, "true")) { |
| 2365 | leaf->flags |= LY_NODE_MAND_TRUE; |
| 2366 | } else if (strcmp(value, "false")) { |
| 2367 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2368 | goto error; |
| 2369 | } /* else false is the default value, so we can ignore it */ |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2370 | } else if (!strcmp(sub->name, "when")) { |
| 2371 | if (leaf->when) { |
| 2372 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2373 | goto error; |
| 2374 | } |
| 2375 | |
| 2376 | leaf->when = read_yin_when(module, sub); |
| 2377 | if (!leaf->when) { |
| 2378 | goto error; |
| 2379 | } |
| 2380 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2381 | } else if (!strcmp(sub->name, "must")) { |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2382 | c_must++; /* else false is the default value, so we can ignore it */ |
| 2383 | } else if (!strcmp(sub->name, "if-feature")) { |
| 2384 | c_ftrs++; |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2385 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2386 | /* skip element free at the end of the loop */ |
| 2387 | continue; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2388 | } else { |
| 2389 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 2390 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2391 | } |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2392 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2393 | lyxml_free_elem(module->ctx, sub); |
| 2394 | } |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2395 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2396 | /* check mandatory parameters */ |
| 2397 | if (!leaf->type.der) { |
| 2398 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "type", yin->name); |
| 2399 | goto error; |
| 2400 | } |
| 2401 | if (leaf->dflt && check_default(&leaf->type, leaf->dflt)) { |
| 2402 | goto error; |
| 2403 | } |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2404 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2405 | /* middle part - process nodes with cardinality of 0..n */ |
| 2406 | if (c_must) { |
| 2407 | leaf->must = calloc(c_must, sizeof *leaf->must); |
| 2408 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2409 | if (c_ftrs) { |
| 2410 | leaf->features = calloc(c_ftrs, sizeof *leaf->features); |
| 2411 | } |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2412 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2413 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2414 | if (!strcmp(sub->name, "must")) { |
| 2415 | r = fill_yin_must(module, sub, &leaf->must[leaf->must_size]); |
| 2416 | leaf->must_size++; |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2417 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2418 | if (r) { |
| 2419 | goto error; |
| 2420 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2421 | } else if (!strcmp(sub->name, "if-feature")) { |
| 2422 | GETVAL(value, sub, "name"); |
| 2423 | leaf->features[leaf->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 2424 | if (!leaf->features[leaf->features_size]) { |
| 2425 | goto error; |
| 2426 | } |
| 2427 | leaf->features_size++; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2428 | } |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 2429 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2430 | lyxml_free_elem(module->ctx, sub); |
| 2431 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2432 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2433 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 2434 | goto error; |
| 2435 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2436 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2437 | return retval; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2438 | |
| 2439 | error: |
| 2440 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2441 | ly_mnode_free(retval); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2442 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2443 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2444 | } |
| 2445 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2446 | static struct ly_mnode * |
| 2447 | read_yin_leaflist(struct ly_module *module, struct ly_mnode *parent, struct lyxml_elem *yin, int resolve) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2448 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2449 | struct ly_mnode *retval; |
| 2450 | struct ly_mnode_leaflist *llist; |
| 2451 | struct lyxml_elem *sub, *next; |
| 2452 | const char *value; |
| 2453 | char *endptr; |
| 2454 | unsigned long val; |
| 2455 | int r; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2456 | int c_must = 0, c_ftrs = 0; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2457 | int f_ordr = 0, f_min = 0, f_max = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2458 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2459 | llist = calloc(1, sizeof *llist); |
| 2460 | llist->nodetype = LY_NODE_LEAFLIST; |
| 2461 | llist->prev = (struct ly_mnode *)llist; |
| 2462 | retval = (struct ly_mnode *)llist; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2463 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2464 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
| 2465 | goto error; |
| 2466 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2467 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2468 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2469 | if (!strcmp(sub->name, "type")) { |
| 2470 | if (llist->type.der) { |
| 2471 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2472 | goto error; |
| 2473 | } |
| 2474 | if (fill_yin_type(module, parent, sub, &llist->type)) { |
| 2475 | goto error; |
| 2476 | } |
| 2477 | } else if (!strcmp(sub->name, "units")) { |
| 2478 | if (llist->units) { |
| 2479 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2480 | goto error; |
| 2481 | } |
| 2482 | GETVAL(value, sub, "name"); |
| 2483 | llist->units = lydict_insert(module->ctx, value, strlen(value)); |
| 2484 | } else if (!strcmp(sub->name, "ordered-by")) { |
| 2485 | if (f_ordr) { |
| 2486 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2487 | goto error; |
| 2488 | } |
| 2489 | /* just checking the flags in llist is not sufficient, we would |
| 2490 | * allow multiple ordered-by statements with the "system" value |
| 2491 | */ |
| 2492 | f_ordr = 1; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2493 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2494 | if (llist->flags & LY_NODE_CONFIG_R) { |
| 2495 | /* RFC 6020, 7.7.5 - ignore ordering when the list represents |
| 2496 | * state data |
| 2497 | */ |
| 2498 | lyxml_free_elem(module->ctx, sub); |
| 2499 | continue; |
| 2500 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2501 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2502 | GETVAL(value, sub, "value"); |
| 2503 | if (!strcmp(value, "user")) { |
| 2504 | llist->flags |= LY_NODE_USERORDERED; |
| 2505 | } else if (strcmp(value, "system")) { |
| 2506 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2507 | goto error; |
| 2508 | } /* else system is the default value, so we can ignore it */ |
| 2509 | } else if (!strcmp(sub->name, "must")) { |
| 2510 | c_must++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2511 | } else if (!strcmp(sub->name, "if-feature")) { |
| 2512 | c_ftrs++; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2513 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2514 | /* skip element free at the end of the loop */ |
| 2515 | continue; |
| 2516 | } else if (!strcmp(sub->name, "min-elements")) { |
| 2517 | if (f_min) { |
| 2518 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2519 | goto error; |
| 2520 | } |
| 2521 | f_min = 1; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2522 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2523 | GETVAL(value, sub, "value"); |
| 2524 | while (isspace(value[0])) { |
| 2525 | value++; |
| 2526 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2527 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2528 | /* convert it to uint32_t */ |
| 2529 | errno = 0; |
| 2530 | endptr = NULL; |
| 2531 | val = strtoul(value, &endptr, 10); |
| 2532 | if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) { |
| 2533 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2534 | goto error; |
| 2535 | } |
| 2536 | llist->min = (uint32_t) val; |
| 2537 | } else if (!strcmp(sub->name, "max-elements")) { |
| 2538 | if (f_max) { |
| 2539 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2540 | goto error; |
| 2541 | } |
| 2542 | f_max = 1; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2543 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2544 | GETVAL(value, sub, "value"); |
| 2545 | while (isspace(value[0])) { |
| 2546 | value++; |
| 2547 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2548 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2549 | /* convert it to uint32_t */ |
| 2550 | errno = 0; |
| 2551 | endptr = NULL; |
| 2552 | val = strtoul(value, &endptr, 10); |
| 2553 | if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) { |
| 2554 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2555 | goto error; |
| 2556 | } |
| 2557 | llist->max = (uint32_t) val; |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2558 | } else if (!strcmp(sub->name, "when")) { |
| 2559 | if (llist->when) { |
| 2560 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2561 | goto error; |
| 2562 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2563 | |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2564 | llist->when = read_yin_when(module, sub); |
| 2565 | if (!llist->when) { |
| 2566 | goto error; |
| 2567 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2568 | } else { |
| 2569 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 2570 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2571 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2572 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2573 | lyxml_free_elem(module->ctx, sub); |
| 2574 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2575 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2576 | /* check constraints */ |
| 2577 | if (!llist->type.der) { |
| 2578 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "type", yin->name); |
| 2579 | goto error; |
| 2580 | } |
| 2581 | if (llist->max && llist->min > llist->max) { |
| 2582 | LOGVAL(VE_SPEC, LOGLINE(yin), "\"min-elements\" is bigger than \"max-elements\"."); |
| 2583 | goto error; |
| 2584 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2585 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2586 | /* middle part - process nodes with cardinality of 0..n */ |
| 2587 | if (c_must) { |
| 2588 | llist->must = calloc(c_must, sizeof *llist->must); |
| 2589 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2590 | if (c_ftrs) { |
| 2591 | llist->features = calloc(c_ftrs, sizeof *llist->features); |
| 2592 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2593 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2594 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2595 | if (!strcmp(sub->name, "must")) { |
| 2596 | r = fill_yin_must(module, sub, &llist->must[llist->must_size]); |
| 2597 | llist->must_size++; |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2598 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2599 | if (r) { |
| 2600 | goto error; |
| 2601 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2602 | } else if (!strcmp(sub->name, "if-feature")) { |
| 2603 | GETVAL(value, sub, "name"); |
| 2604 | llist->features[llist->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 2605 | if (!llist->features[llist->features_size]) { |
| 2606 | goto error; |
| 2607 | } |
| 2608 | llist->features_size++; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2609 | } |
Radek Krejci | 8b4f23c | 2015-06-02 16:09:25 +0200 | [diff] [blame] | 2610 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2611 | lyxml_free_elem(module->ctx, sub); |
| 2612 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2613 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2614 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 2615 | goto error; |
| 2616 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2617 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2618 | return retval; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2619 | |
| 2620 | error: |
| 2621 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2622 | ly_mnode_free(retval); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2623 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2624 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2627 | static struct ly_mnode * |
| 2628 | read_yin_list(struct ly_module *module, |
| 2629 | struct ly_mnode *parent, struct lyxml_elem *yin, int resolve, struct mnode_list **unres) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2630 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2631 | struct ly_mnode *retval, *mnode; |
| 2632 | struct ly_mnode_list *list; |
| 2633 | struct ly_unique *uniq_s; |
| 2634 | struct lyxml_elem *sub, *next, root, uniq; |
| 2635 | int i, r; |
| 2636 | size_t len; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2637 | int c_tpdf = 0, c_must = 0, c_uniq = 0, c_ftrs = 0; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2638 | int f_ordr = 0, f_max = 0, f_min = 0; |
| 2639 | const char *key_str = NULL, *uniq_str, *value; |
| 2640 | char *auxs; |
| 2641 | unsigned long val; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2642 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2643 | /* init */ |
| 2644 | memset(&root, 0, sizeof root); |
| 2645 | memset(&uniq, 0, sizeof uniq); |
Radek Krejci | e0674f8 | 2015-06-15 13:58:51 +0200 | [diff] [blame] | 2646 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2647 | list = calloc(1, sizeof *list); |
| 2648 | list->nodetype = LY_NODE_LIST; |
| 2649 | list->prev = (struct ly_mnode *)list; |
| 2650 | retval = (struct ly_mnode *)list; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2651 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2652 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
| 2653 | goto error; |
| 2654 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2655 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2656 | /* process list's specific children */ |
| 2657 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2658 | /* data statements */ |
| 2659 | if (!strcmp(sub->name, "container") || |
| 2660 | !strcmp(sub->name, "leaf-list") || |
| 2661 | !strcmp(sub->name, "leaf") || |
| 2662 | !strcmp(sub->name, "list") || |
| 2663 | !strcmp(sub->name, "choice") || |
| 2664 | !strcmp(sub->name, "uses") || |
| 2665 | !strcmp(sub->name, "grouping") || |
| 2666 | !strcmp(sub->name, "anyxml")) { |
| 2667 | lyxml_unlink_elem(sub); |
| 2668 | lyxml_add_child(&root, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2669 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2670 | /* array counters */ |
| 2671 | } else if (!strcmp(sub->name, "key")) { |
| 2672 | /* check cardinality 0..1 */ |
| 2673 | if (list->keys_size) { |
| 2674 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, list->name); |
| 2675 | goto error; |
| 2676 | } |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2677 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2678 | /* count the number of keys */ |
| 2679 | GETVAL(value, sub, "value"); |
| 2680 | key_str = value; |
| 2681 | while ((value = strpbrk(value, " \t\n"))) { |
| 2682 | list->keys_size++; |
| 2683 | while (isspace(*value)) { |
| 2684 | value++; |
| 2685 | } |
| 2686 | } |
| 2687 | list->keys_size++; |
| 2688 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
| 2689 | } else if (!strcmp(sub->name, "unique")) { |
| 2690 | c_uniq++; |
| 2691 | lyxml_unlink_elem(sub); |
| 2692 | lyxml_add_child(&uniq, sub); |
| 2693 | } else if (!strcmp(sub->name, "typedef")) { |
| 2694 | c_tpdf++; |
| 2695 | } else if (!strcmp(sub->name, "must")) { |
| 2696 | c_must++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2697 | } else if (!strcmp(sub->name, "if-feature")) { |
| 2698 | c_ftrs++; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 2699 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2700 | /* optional stetments */ |
| 2701 | } else if (!strcmp(sub->name, "ordered-by")) { |
| 2702 | if (f_ordr) { |
| 2703 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2704 | goto error; |
| 2705 | } |
| 2706 | /* just checking the flags in llist is not sufficient, we would |
| 2707 | * allow multiple ordered-by statements with the "system" value |
| 2708 | */ |
| 2709 | f_ordr = 1; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 2710 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2711 | if (list->flags & LY_NODE_CONFIG_R) { |
| 2712 | /* RFC 6020, 7.7.5 - ignore ordering when the list represents |
| 2713 | * state data |
| 2714 | */ |
| 2715 | lyxml_free_elem(module->ctx, sub); |
| 2716 | continue; |
| 2717 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 2718 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2719 | GETVAL(value, sub, "value"); |
| 2720 | if (!strcmp(value, "user")) { |
| 2721 | list->flags |= LY_NODE_USERORDERED; |
| 2722 | } else if (strcmp(value, "system")) { |
| 2723 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2724 | goto error; |
| 2725 | } |
| 2726 | /* else system is the default value, so we can ignore it */ |
| 2727 | lyxml_free_elem(module->ctx, sub); |
| 2728 | } else if (!strcmp(sub->name, "min-elements")) { |
| 2729 | if (f_min) { |
| 2730 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2731 | goto error; |
| 2732 | } |
| 2733 | f_min = 1; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 2734 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2735 | GETVAL(value, sub, "value"); |
| 2736 | while (isspace(value[0])) { |
| 2737 | value++; |
| 2738 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 2739 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2740 | /* convert it to uint32_t */ |
| 2741 | errno = 0; |
| 2742 | auxs = NULL; |
| 2743 | val = strtoul(value, &auxs, 10); |
| 2744 | if (*auxs || value[0] == '-' || errno || val > UINT32_MAX) { |
| 2745 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2746 | goto error; |
| 2747 | } |
| 2748 | list->min = (uint32_t) val; |
| 2749 | lyxml_free_elem(module->ctx, sub); |
| 2750 | } else if (!strcmp(sub->name, "max-elements")) { |
| 2751 | if (f_max) { |
| 2752 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2753 | goto error; |
| 2754 | } |
| 2755 | f_max = 1; |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 2756 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2757 | GETVAL(value, sub, "value"); |
| 2758 | while (isspace(value[0])) { |
| 2759 | value++; |
| 2760 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 2761 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2762 | /* convert it to uint32_t */ |
| 2763 | errno = 0; |
| 2764 | auxs = NULL; |
| 2765 | val = strtoul(value, &auxs, 10); |
| 2766 | if (*auxs || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) { |
| 2767 | LOGVAL(VE_INARG, LOGLINE(sub), value, sub->name); |
| 2768 | goto error; |
| 2769 | } |
| 2770 | list->max = (uint32_t) val; |
| 2771 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2772 | } else if (!strcmp(sub->name, "when")) { |
| 2773 | if (list->when) { |
| 2774 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2775 | goto error; |
| 2776 | } |
| 2777 | |
| 2778 | list->when = read_yin_when(module, sub); |
| 2779 | lyxml_free_elem(module->ctx, sub); |
| 2780 | |
| 2781 | if (!list->when) { |
| 2782 | goto error; |
| 2783 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2784 | } else { |
| 2785 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 2786 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2787 | } |
| 2788 | } |
Radek Krejci | 345ad74 | 2015-06-03 11:04:18 +0200 | [diff] [blame] | 2789 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2790 | /* check - if list is configuration, key statement is mandatory */ |
| 2791 | if ((list->flags & LY_NODE_CONFIG_W) && !key_str) { |
| 2792 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "key", "list"); |
| 2793 | goto error; |
| 2794 | } |
| 2795 | if (list->max && list->min > list->max) { |
| 2796 | LOGVAL(VE_SPEC, LOGLINE(yin), "\"min-elements\" is bigger than \"max-elements\"."); |
| 2797 | goto error; |
| 2798 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2799 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2800 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 2801 | if (c_tpdf) { |
| 2802 | list->tpdf = calloc(c_tpdf, sizeof *list->tpdf); |
| 2803 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2804 | if (c_must) { |
| 2805 | list->must = calloc(c_must, sizeof *list->must); |
| 2806 | } |
| 2807 | if (c_ftrs) { |
| 2808 | list->features = calloc(c_ftrs, sizeof *list->features); |
| 2809 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2810 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2811 | if (!strcmp(sub->name, "typedef")) { |
| 2812 | r = fill_yin_typedef(module, retval, sub, &list->tpdf[list->tpdf_size]); |
| 2813 | list->tpdf_size++; |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2814 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2815 | if (r) { |
| 2816 | goto error; |
| 2817 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2818 | } else if (!strcmp(sub->name, "if-feature")) { |
| 2819 | GETVAL(value, sub, "name"); |
| 2820 | list->features[list->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 2821 | if (!list->features[list->features_size]) { |
| 2822 | goto error; |
| 2823 | } |
| 2824 | list->features_size++; |
| 2825 | } else if (!strcmp(sub->name, "must")) { |
| 2826 | r = fill_yin_must(module, sub, &list->must[list->must_size]); |
| 2827 | list->must_size++; |
| 2828 | |
| 2829 | if (r) { |
| 2830 | goto error; |
| 2831 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2832 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2833 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2834 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 2835 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2836 | /* last part - process data nodes */ |
| 2837 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 2838 | if (!strcmp(sub->name, "container")) { |
| 2839 | mnode = read_yin_container(module, retval, sub, resolve, unres); |
| 2840 | } else if (!strcmp(sub->name, "leaf-list")) { |
| 2841 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
| 2842 | } else if (!strcmp(sub->name, "leaf")) { |
| 2843 | mnode = read_yin_leaf(module, retval, sub, resolve); |
| 2844 | } else if (!strcmp(sub->name, "list")) { |
| 2845 | mnode = read_yin_list(module, retval, sub, resolve, unres); |
| 2846 | } else if (!strcmp(sub->name, "choice")) { |
| 2847 | mnode = read_yin_choice(module, retval, sub, resolve, unres); |
| 2848 | } else if (!strcmp(sub->name, "uses")) { |
| 2849 | mnode = read_yin_uses(module, retval, sub, resolve, unres); |
| 2850 | } else if (!strcmp(sub->name, "grouping")) { |
| 2851 | mnode = read_yin_grouping(module, retval, sub, resolve, unres); |
| 2852 | } else if (!strcmp(sub->name, "anyxml")) { |
| 2853 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2854 | } |
| 2855 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2856 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2857 | if (!mnode) { |
| 2858 | goto error; |
| 2859 | } |
| 2860 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2861 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2862 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 2863 | goto error; |
| 2864 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2865 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2866 | if (!key_str) { |
| 2867 | /* config false list without a key */ |
| 2868 | return retval; |
| 2869 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 2870 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2871 | /* link key leafs into the list structure and check all constraints */ |
| 2872 | for (i = 0; i < list->keys_size; i++) { |
| 2873 | /* get the key name */ |
| 2874 | if ((value = strpbrk(key_str, " \t\n"))) { |
| 2875 | len = value - key_str; |
| 2876 | while (isspace(*value)) { |
| 2877 | value++; |
| 2878 | } |
| 2879 | } else { |
| 2880 | len = strlen(key_str); |
| 2881 | } |
Radek Krejci | 3a734ed | 2015-05-26 15:23:18 +0200 | [diff] [blame] | 2882 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2883 | list->keys[i] = find_leaf(retval, key_str, len); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2884 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2885 | if (check_key(list->keys[i], list->flags, list->keys, i, LOGLINE(yin), key_str, len)) { |
| 2886 | goto error; |
| 2887 | } |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2888 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2889 | /* prepare for next iteration */ |
| 2890 | while (value && isspace(*value)) { |
| 2891 | value++; |
| 2892 | } |
| 2893 | key_str = value; |
| 2894 | } |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2895 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2896 | /* process unique statements */ |
| 2897 | if (c_uniq) { |
| 2898 | list->unique = calloc(c_uniq, sizeof *list->unique); |
| 2899 | } |
| 2900 | LY_TREE_FOR_SAFE(uniq.child, next, sub) { |
| 2901 | /* count the number of unique values */ |
| 2902 | GETVAL(value, sub, "tag"); |
| 2903 | uniq_str = value; |
| 2904 | uniq_s = &list->unique[list->unique_size]; |
| 2905 | while ((value = strpbrk(value, " \t\n"))) { |
| 2906 | uniq_s->leafs_size++; |
| 2907 | while (isspace(*value)) { |
| 2908 | value++; |
| 2909 | } |
| 2910 | } |
| 2911 | uniq_s->leafs_size++; |
| 2912 | uniq_s->leafs = calloc(uniq_s->leafs_size, sizeof *uniq_s->leafs); |
| 2913 | list->unique_size++; |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 2914 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2915 | /* interconnect unique values with the leafs */ |
| 2916 | for (i = 0; i < uniq_s->leafs_size; i++) { |
| 2917 | if ((value = strpbrk(uniq_str, " \t\n"))) { |
| 2918 | len = value - uniq_str; |
| 2919 | while (isspace(*value)) { |
| 2920 | value++; |
| 2921 | } |
| 2922 | } else { |
| 2923 | len = strlen(uniq_str); |
| 2924 | } |
Radek Krejci | 1e9b999 | 2015-06-04 17:57:04 +0200 | [diff] [blame] | 2925 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2926 | uniq_s->leafs[i] = find_leaf(retval, uniq_str, len); |
| 2927 | if (check_key(uniq_s->leafs[i], list->flags, uniq_s->leafs, i, LOGLINE(yin), uniq_str, len)) { |
| 2928 | goto error; |
| 2929 | } |
Radek Krejci | 1e9b999 | 2015-06-04 17:57:04 +0200 | [diff] [blame] | 2930 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2931 | /* prepare for next iteration */ |
| 2932 | while (value && isspace(*value)) { |
| 2933 | value++; |
| 2934 | } |
| 2935 | uniq_str = value; |
| 2936 | } |
Radek Krejci | 1e9b999 | 2015-06-04 17:57:04 +0200 | [diff] [blame] | 2937 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2938 | lyxml_free_elem(module->ctx, sub); |
| 2939 | } |
Radek Krejci | 1e9b999 | 2015-06-04 17:57:04 +0200 | [diff] [blame] | 2940 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2941 | return retval; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2942 | |
| 2943 | error: |
| 2944 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2945 | ly_mnode_free(retval); |
| 2946 | while (root.child) { |
| 2947 | lyxml_free_elem(module->ctx, root.child); |
| 2948 | } |
| 2949 | while (uniq.child) { |
| 2950 | lyxml_free_elem(module->ctx, uniq.child); |
| 2951 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2952 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2953 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2954 | } |
| 2955 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2956 | static struct ly_mnode * |
| 2957 | read_yin_container(struct ly_module *module, |
| 2958 | struct ly_mnode *parent, struct lyxml_elem *yin, int resolve, struct mnode_list **unres) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2959 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2960 | struct lyxml_elem *sub, *next, root; |
| 2961 | struct ly_mnode *mnode = NULL; |
| 2962 | struct ly_mnode *retval; |
| 2963 | struct ly_mnode_container *cont; |
| 2964 | const char *value; |
| 2965 | int r; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2966 | int c_tpdf = 0, c_must = 0, c_ftrs = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2967 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2968 | /* init */ |
| 2969 | memset(&root, 0, sizeof root); |
Radek Krejci | e0674f8 | 2015-06-15 13:58:51 +0200 | [diff] [blame] | 2970 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2971 | cont = calloc(1, sizeof *cont); |
| 2972 | cont->nodetype = LY_NODE_CONTAINER; |
| 2973 | cont->prev = (struct ly_mnode *)cont; |
| 2974 | retval = (struct ly_mnode *)cont; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2975 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2976 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG | (resolve ? OPT_INHERIT : 0))) { |
| 2977 | goto error; |
| 2978 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2979 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2980 | /* process container's specific children */ |
| 2981 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 2982 | if (!strcmp(sub->name, "presence")) { |
| 2983 | if (cont->presence) { |
| 2984 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2985 | goto error; |
| 2986 | } |
| 2987 | GETVAL(value, sub, "value"); |
| 2988 | cont->presence = lydict_insert(module->ctx, value, strlen(value)); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 2989 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2990 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 2991 | } else if (!strcmp(sub->name, "when")) { |
| 2992 | if (cont->when) { |
| 2993 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 2994 | goto error; |
| 2995 | } |
| 2996 | |
| 2997 | cont->when = read_yin_when(module, sub); |
| 2998 | lyxml_free_elem(module->ctx, sub); |
| 2999 | |
| 3000 | if (!cont->when) { |
| 3001 | goto error; |
| 3002 | } |
Radek Krejci | 4c31f12 | 2015-06-02 14:51:22 +0200 | [diff] [blame] | 3003 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3004 | /* data statements */ |
| 3005 | } else if (!strcmp(sub->name, "container") || |
| 3006 | !strcmp(sub->name, "leaf-list") || |
| 3007 | !strcmp(sub->name, "leaf") || |
| 3008 | !strcmp(sub->name, "list") || |
| 3009 | !strcmp(sub->name, "choice") || |
| 3010 | !strcmp(sub->name, "uses") || |
| 3011 | !strcmp(sub->name, "grouping") || |
| 3012 | !strcmp(sub->name, "anyxml")) { |
| 3013 | lyxml_unlink_elem(sub); |
| 3014 | lyxml_add_child(&root, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3015 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3016 | /* array counters */ |
| 3017 | } else if (!strcmp(sub->name, "typedef")) { |
| 3018 | c_tpdf++; |
| 3019 | } else if (!strcmp(sub->name, "must")) { |
| 3020 | c_must++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3021 | } else if (!strcmp(sub->name, "if-feature")) { |
| 3022 | c_ftrs++; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3023 | } else { |
| 3024 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 3025 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3026 | } |
| 3027 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3028 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3029 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 3030 | if (c_tpdf) { |
| 3031 | cont->tpdf = calloc(c_tpdf, sizeof *cont->tpdf); |
| 3032 | } |
| 3033 | if (c_must) { |
| 3034 | cont->must = calloc(c_must, sizeof *cont->must); |
| 3035 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3036 | if (c_ftrs) { |
| 3037 | cont->features = calloc(c_ftrs, sizeof *cont->features); |
| 3038 | } |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 3039 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3040 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 3041 | if (!strcmp(sub->name, "typedef")) { |
| 3042 | r = fill_yin_typedef(module, retval, sub, &cont->tpdf[cont->tpdf_size]); |
| 3043 | cont->tpdf_size++; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 3044 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3045 | if (r) { |
| 3046 | goto error; |
| 3047 | } |
| 3048 | } else if (!strcmp(sub->name, "must")) { |
| 3049 | r = fill_yin_must(module, sub, &cont->must[cont->must_size]); |
| 3050 | cont->must_size++; |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 3051 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3052 | if (r) { |
| 3053 | goto error; |
| 3054 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3055 | } else if (!strcmp(sub->name, "if-feature")) { |
| 3056 | GETVAL(value, sub, "name"); |
| 3057 | cont->features[cont->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 3058 | if (!cont->features[cont->features_size]) { |
| 3059 | goto error; |
| 3060 | } |
| 3061 | cont->features_size++; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3062 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3063 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3064 | lyxml_free_elem(module->ctx, sub); |
| 3065 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3066 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3067 | /* last part - process data nodes */ |
| 3068 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 3069 | if (!strcmp(sub->name, "container")) { |
| 3070 | mnode = read_yin_container(module, retval, sub, resolve, unres); |
| 3071 | } else if (!strcmp(sub->name, "leaf-list")) { |
| 3072 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
| 3073 | } else if (!strcmp(sub->name, "leaf")) { |
| 3074 | mnode = read_yin_leaf(module, retval, sub, resolve); |
| 3075 | } else if (!strcmp(sub->name, "list")) { |
| 3076 | mnode = read_yin_list(module, retval, sub, resolve, unres); |
| 3077 | } else if (!strcmp(sub->name, "choice")) { |
| 3078 | mnode = read_yin_choice(module, retval, sub, resolve, unres); |
| 3079 | } else if (!strcmp(sub->name, "uses")) { |
| 3080 | mnode = read_yin_uses(module, retval, sub, resolve, unres); |
| 3081 | } else if (!strcmp(sub->name, "grouping")) { |
| 3082 | mnode = read_yin_grouping(module, retval, sub, resolve, unres); |
| 3083 | } else if (!strcmp(sub->name, "anyxml")) { |
| 3084 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
| 3085 | } |
| 3086 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3087 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3088 | if (!mnode) { |
| 3089 | goto error; |
| 3090 | } |
| 3091 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3092 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3093 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 3094 | goto error; |
| 3095 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3096 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3097 | return retval; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3098 | |
| 3099 | error: |
| 3100 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3101 | ly_mnode_free(retval); |
| 3102 | while (root.child) { |
| 3103 | lyxml_free_elem(module->ctx, root.child); |
| 3104 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3105 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3106 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3107 | } |
| 3108 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3109 | static struct ly_mnode * |
| 3110 | read_yin_grouping(struct ly_module *module, |
| 3111 | struct ly_mnode *parent, struct lyxml_elem *node, int resolve, struct mnode_list **unres) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3112 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3113 | struct lyxml_elem *sub, *next, root; |
| 3114 | struct ly_mnode *mnode = NULL; |
| 3115 | struct ly_mnode *retval; |
| 3116 | struct ly_mnode_grp *grp; |
| 3117 | int r; |
| 3118 | int c_tpdf = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3119 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3120 | /* init */ |
| 3121 | memset(&root, 0, sizeof root); |
Radek Krejci | e0674f8 | 2015-06-15 13:58:51 +0200 | [diff] [blame] | 3122 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3123 | grp = calloc(1, sizeof *grp); |
| 3124 | grp->nodetype = LY_NODE_GROUPING; |
| 3125 | grp->prev = (struct ly_mnode *)grp; |
| 3126 | retval = (struct ly_mnode *)grp; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3127 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3128 | if (read_yin_common(module, parent, retval, node, OPT_IDENT | OPT_MODULE)) { |
| 3129 | goto error; |
| 3130 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3131 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3132 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 3133 | /* data statements */ |
| 3134 | if (!strcmp(sub->name, "container") || |
| 3135 | !strcmp(sub->name, "leaf-list") || |
| 3136 | !strcmp(sub->name, "leaf") || |
| 3137 | !strcmp(sub->name, "list") || |
| 3138 | !strcmp(sub->name, "choice") || |
| 3139 | !strcmp(sub->name, "uses") || |
| 3140 | !strcmp(sub->name, "grouping") || |
| 3141 | !strcmp(sub->name, "anyxml")) { |
| 3142 | lyxml_unlink_elem(sub); |
| 3143 | lyxml_add_child(&root, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3144 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3145 | /* array counters */ |
| 3146 | } else if (!strcmp(sub->name, "typedef")) { |
| 3147 | c_tpdf++; |
| 3148 | } else { |
| 3149 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 3150 | goto error; |
| 3151 | } |
| 3152 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3153 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3154 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 3155 | if (c_tpdf) { |
| 3156 | grp->tpdf = calloc(c_tpdf, sizeof *grp->tpdf); |
| 3157 | } |
| 3158 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 3159 | if (!strcmp(sub->name, "typedef")) { |
| 3160 | r = fill_yin_typedef(module, retval, sub, &grp->tpdf[grp->tpdf_size]); |
| 3161 | grp->tpdf_size++; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 3162 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3163 | if (r) { |
| 3164 | goto error; |
| 3165 | } |
| 3166 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3167 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3168 | lyxml_free_elem(module->ctx, sub); |
| 3169 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3170 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3171 | /* last part - process data nodes */ |
| 3172 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 3173 | if (!strcmp(sub->name, "container")) { |
| 3174 | mnode = read_yin_container(module, retval, sub, resolve, unres); |
| 3175 | } else if (!strcmp(sub->name, "leaf-list")) { |
| 3176 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
| 3177 | } else if (!strcmp(sub->name, "leaf")) { |
| 3178 | mnode = read_yin_leaf(module, retval, sub, resolve); |
| 3179 | } else if (!strcmp(sub->name, "list")) { |
| 3180 | mnode = read_yin_list(module, retval, sub, resolve, unres); |
| 3181 | } else if (!strcmp(sub->name, "choice")) { |
| 3182 | mnode = read_yin_choice(module, retval, sub, resolve, unres); |
| 3183 | } else if (!strcmp(sub->name, "uses")) { |
| 3184 | mnode = read_yin_uses(module, retval, sub, resolve, unres); |
| 3185 | } else if (!strcmp(sub->name, "grouping")) { |
| 3186 | mnode = read_yin_grouping(module, retval, sub, resolve, unres); |
| 3187 | } else if (!strcmp(sub->name, "anyxml")) { |
| 3188 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
| 3189 | } |
| 3190 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3191 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3192 | if (!mnode) { |
| 3193 | goto error; |
| 3194 | } |
| 3195 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3196 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3197 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 3198 | goto error; |
| 3199 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3200 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3201 | return retval; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3202 | |
| 3203 | error: |
| 3204 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3205 | ly_mnode_free(retval); |
| 3206 | while (root.child) { |
| 3207 | lyxml_free_elem(module->ctx, root.child); |
| 3208 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3209 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3210 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3211 | } |
| 3212 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3213 | static struct ly_mnode * |
| 3214 | read_yin_input_output(struct ly_module *module, |
| 3215 | struct ly_mnode *parent, struct lyxml_elem *yin, int resolve, struct mnode_list **unres) |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3216 | { |
Radek Krejci | e0674f8 | 2015-06-15 13:58:51 +0200 | [diff] [blame] | 3217 | struct lyxml_elem *sub, *next, root; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3218 | struct ly_mnode *mnode = NULL; |
| 3219 | struct ly_mnode *retval; |
| 3220 | struct ly_mnode_input_output *inout; |
| 3221 | int r; |
| 3222 | int c_tpdf = 0; |
| 3223 | |
Radek Krejci | e0674f8 | 2015-06-15 13:58:51 +0200 | [diff] [blame] | 3224 | /* init */ |
| 3225 | memset(&root, 0, sizeof root); |
| 3226 | |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3227 | inout = calloc(1, sizeof *inout); |
| 3228 | |
| 3229 | if (!strcmp(yin->name, "input")) { |
| 3230 | inout->nodetype = LY_NODE_INPUT; |
| 3231 | } else if (!strcmp(yin->name, "output")) { |
| 3232 | inout->nodetype = LY_NODE_OUTPUT; |
| 3233 | } else { |
| 3234 | assert(0); |
| 3235 | } |
| 3236 | |
| 3237 | inout->prev = (struct ly_mnode *)inout; |
| 3238 | retval = (struct ly_mnode *)inout; |
| 3239 | |
Michal Vasko | ebeac94 | 2015-06-15 12:11:50 +0200 | [diff] [blame] | 3240 | if (read_yin_common(module, parent, retval, yin, OPT_MODULE)) { |
| 3241 | goto error; |
| 3242 | } |
| 3243 | |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3244 | /* data statements */ |
| 3245 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 3246 | if (!strcmp(sub->name, "container") || |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3247 | !strcmp(sub->name, "leaf-list") || |
| 3248 | !strcmp(sub->name, "leaf") || |
| 3249 | !strcmp(sub->name, "list") || |
| 3250 | !strcmp(sub->name, "choice") || |
| 3251 | !strcmp(sub->name, "uses") || |
| 3252 | !strcmp(sub->name, "grouping") || |
| 3253 | !strcmp(sub->name, "anyxml")) { |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3254 | lyxml_unlink_elem(sub); |
| 3255 | lyxml_add_child(&root, sub); |
| 3256 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3257 | /* array counters */ |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3258 | } else if (!strcmp(sub->name, "typedef")) { |
| 3259 | c_tpdf++; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3260 | #if 0 |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3261 | } else { |
| 3262 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 3263 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3264 | #else |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3265 | } else { |
| 3266 | continue; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3267 | #endif |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 3272 | if (c_tpdf) { |
| 3273 | inout->tpdf = calloc(c_tpdf, sizeof *inout->tpdf); |
| 3274 | } |
| 3275 | |
| 3276 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 3277 | if (!strcmp(sub->name, "typedef")) { |
| 3278 | r = fill_yin_typedef(module, retval, sub, &inout->tpdf[inout->tpdf_size]); |
| 3279 | inout->tpdf_size++; |
| 3280 | |
| 3281 | if (r) { |
| 3282 | goto error; |
| 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | lyxml_free_elem(module->ctx, sub); |
| 3287 | } |
| 3288 | |
| 3289 | /* last part - process data nodes */ |
| 3290 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 3291 | if (!strcmp(sub->name, "container")) { |
| 3292 | mnode = read_yin_container(module, retval, sub, resolve, unres); |
| 3293 | } else if (!strcmp(sub->name, "leaf-list")) { |
| 3294 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
| 3295 | } else if (!strcmp(sub->name, "leaf")) { |
| 3296 | mnode = read_yin_leaf(module, retval, sub, resolve); |
| 3297 | } else if (!strcmp(sub->name, "list")) { |
| 3298 | mnode = read_yin_list(module, retval, sub, resolve, unres); |
| 3299 | } else if (!strcmp(sub->name, "choice")) { |
| 3300 | mnode = read_yin_choice(module, retval, sub, resolve, unres); |
| 3301 | } else if (!strcmp(sub->name, "uses")) { |
| 3302 | mnode = read_yin_uses(module, retval, sub, resolve, unres); |
| 3303 | } else if (!strcmp(sub->name, "grouping")) { |
| 3304 | mnode = read_yin_grouping(module, retval, sub, resolve, unres); |
| 3305 | } else if (!strcmp(sub->name, "anyxml")) { |
| 3306 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
| 3307 | } |
| 3308 | lyxml_free_elem(module->ctx, sub); |
| 3309 | |
| 3310 | if (!mnode) { |
| 3311 | goto error; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 3316 | goto error; |
| 3317 | } |
| 3318 | |
| 3319 | return retval; |
| 3320 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3321 | error: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3322 | |
| 3323 | ly_mnode_free(retval); |
Michal Vasko | 3a9abf8 | 2015-06-17 10:18:26 +0200 | [diff] [blame] | 3324 | while (root.child) { |
| 3325 | lyxml_free_elem(module->ctx, root.child); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | return NULL; |
| 3329 | } |
| 3330 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3331 | static struct ly_mnode * |
| 3332 | read_yin_notif(struct ly_module *module, |
| 3333 | struct ly_mnode *parent, struct lyxml_elem *yin, int resolve, struct mnode_list **unres) |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3334 | { |
Michal Vasko | c6551b3 | 2015-06-16 10:51:43 +0200 | [diff] [blame] | 3335 | struct lyxml_elem *sub, *next, root; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3336 | struct ly_mnode *mnode = NULL; |
| 3337 | struct ly_mnode *retval; |
| 3338 | struct ly_mnode_notif *notif; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3339 | const char *value; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3340 | int r; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3341 | int c_tpdf = 0, c_ftrs = 0; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3342 | |
Michal Vasko | c6551b3 | 2015-06-16 10:51:43 +0200 | [diff] [blame] | 3343 | memset(&root, 0, sizeof root); |
| 3344 | |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3345 | notif = calloc(1, sizeof *notif); |
| 3346 | notif->nodetype = LY_NODE_NOTIF; |
| 3347 | notif->prev = (struct ly_mnode *)notif; |
| 3348 | retval = (struct ly_mnode *)notif; |
| 3349 | |
| 3350 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE)) { |
| 3351 | goto error; |
| 3352 | } |
| 3353 | |
| 3354 | /* process rpc's specific children */ |
| 3355 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 3356 | /* data statements */ |
| 3357 | if (!strcmp(sub->name, "container") || |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3358 | !strcmp(sub->name, "leaf-list") || |
| 3359 | !strcmp(sub->name, "leaf") || |
| 3360 | !strcmp(sub->name, "list") || |
| 3361 | !strcmp(sub->name, "choice") || |
| 3362 | !strcmp(sub->name, "uses") || |
| 3363 | !strcmp(sub->name, "grouping") || |
| 3364 | !strcmp(sub->name, "anyxml")) { |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3365 | lyxml_unlink_elem(sub); |
| 3366 | lyxml_add_child(&root, sub); |
| 3367 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3368 | /* array counters */ |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3369 | } else if (!strcmp(sub->name, "typedef")) { |
| 3370 | c_tpdf++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3371 | } else if (!strcmp(sub->name, "if-feature")) { |
| 3372 | c_ftrs++; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3373 | } else { |
| 3374 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 3375 | goto error; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 3380 | if (c_tpdf) { |
| 3381 | notif->tpdf = calloc(c_tpdf, sizeof *notif->tpdf); |
| 3382 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3383 | if (c_ftrs) { |
| 3384 | notif->features = calloc(c_ftrs, sizeof *notif->features); |
| 3385 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3386 | |
| 3387 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 3388 | if (!strcmp(sub->name, "typedef")) { |
| 3389 | r = fill_yin_typedef(module, retval, sub, ¬if->tpdf[notif->tpdf_size]); |
| 3390 | notif->tpdf_size++; |
| 3391 | |
| 3392 | if (r) { |
| 3393 | goto error; |
| 3394 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3395 | } else if (!strcmp(sub->name, "typedef")) { |
| 3396 | GETVAL(value, sub, "name"); |
| 3397 | notif->features[notif->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 3398 | if (!notif->features[notif->features_size]) { |
| 3399 | goto error; |
| 3400 | } |
| 3401 | notif->features_size++; |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | lyxml_free_elem(module->ctx, sub); |
| 3405 | } |
| 3406 | |
| 3407 | /* last part - process data nodes */ |
| 3408 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 3409 | if (!strcmp(sub->name, "container")) { |
| 3410 | mnode = read_yin_container(module, retval, sub, resolve, unres); |
| 3411 | } else if (!strcmp(sub->name, "leaf-list")) { |
| 3412 | mnode = read_yin_leaflist(module, retval, sub, resolve); |
| 3413 | } else if (!strcmp(sub->name, "leaf")) { |
| 3414 | mnode = read_yin_leaf(module, retval, sub, resolve); |
| 3415 | } else if (!strcmp(sub->name, "list")) { |
| 3416 | mnode = read_yin_list(module, retval, sub, resolve, unres); |
| 3417 | } else if (!strcmp(sub->name, "choice")) { |
| 3418 | mnode = read_yin_choice(module, retval, sub, resolve, unres); |
| 3419 | } else if (!strcmp(sub->name, "uses")) { |
| 3420 | mnode = read_yin_uses(module, retval, sub, resolve, unres); |
| 3421 | } else if (!strcmp(sub->name, "grouping")) { |
| 3422 | mnode = read_yin_grouping(module, retval, sub, resolve, unres); |
| 3423 | } else if (!strcmp(sub->name, "anyxml")) { |
| 3424 | mnode = read_yin_anyxml(module, retval, sub, resolve); |
| 3425 | } |
| 3426 | lyxml_free_elem(module->ctx, sub); |
| 3427 | |
| 3428 | if (!mnode) { |
| 3429 | goto error; |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 3434 | goto error; |
| 3435 | } |
| 3436 | |
| 3437 | return retval; |
| 3438 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3439 | error: |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 3440 | |
| 3441 | ly_mnode_free(retval); |
| 3442 | while (root.child) { |
| 3443 | lyxml_free_elem(module->ctx, root.child); |
| 3444 | } |
| 3445 | |
| 3446 | return NULL; |
| 3447 | } |
| 3448 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3449 | static struct ly_mnode * |
| 3450 | read_yin_rpc(struct ly_module *module, |
| 3451 | struct ly_mnode *parent, struct lyxml_elem *yin, int resolve, struct mnode_list **unres) |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3452 | { |
Radek Krejci | e0674f8 | 2015-06-15 13:58:51 +0200 | [diff] [blame] | 3453 | struct lyxml_elem *sub, *next, root; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3454 | struct ly_mnode *mnode = NULL; |
| 3455 | struct ly_mnode *retval; |
| 3456 | struct ly_mnode_rpc *rpc; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3457 | const char *value; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3458 | int r; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3459 | int c_tpdf = 0, c_ftrs = 0; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3460 | |
Radek Krejci | e0674f8 | 2015-06-15 13:58:51 +0200 | [diff] [blame] | 3461 | /* init */ |
| 3462 | memset(&root, 0, sizeof root); |
| 3463 | |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3464 | rpc = calloc(1, sizeof *rpc); |
| 3465 | rpc->nodetype = LY_NODE_RPC; |
| 3466 | rpc->prev = (struct ly_mnode *)rpc; |
| 3467 | retval = (struct ly_mnode *)rpc; |
| 3468 | |
| 3469 | if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE)) { |
| 3470 | goto error; |
| 3471 | } |
| 3472 | |
| 3473 | /* process rpc's specific children */ |
| 3474 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 3475 | if (!strcmp(sub->name, "input")) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3476 | if (rpc->child |
| 3477 | && (rpc->child->nodetype == LY_NODE_INPUT |
| 3478 | || (rpc->child->next && rpc->child->next->nodetype == LY_NODE_INPUT))) { |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3479 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 3480 | goto error; |
| 3481 | } |
| 3482 | lyxml_unlink_elem(sub); |
| 3483 | lyxml_add_child(&root, sub); |
| 3484 | } else if (!strcmp(sub->name, "output")) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3485 | if (rpc->child |
| 3486 | && (rpc->child->nodetype == LY_NODE_INPUT |
| 3487 | || (rpc->child->next && rpc->child->next->nodetype == LY_NODE_INPUT))) { |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3488 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, yin->name); |
| 3489 | goto error; |
| 3490 | } |
| 3491 | lyxml_unlink_elem(sub); |
| 3492 | lyxml_add_child(&root, sub); |
| 3493 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3494 | /* data statements */ |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3495 | } else if (!strcmp(sub->name, "grouping")) { |
| 3496 | lyxml_unlink_elem(sub); |
| 3497 | lyxml_add_child(&root, sub); |
| 3498 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3499 | /* array counters */ |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3500 | } else if (!strcmp(sub->name, "typedef")) { |
| 3501 | c_tpdf++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3502 | } else if (!strcmp(sub->name, "if-feature")) { |
| 3503 | c_ftrs++; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3504 | } else { |
| 3505 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 3506 | goto error; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 3511 | if (c_tpdf) { |
| 3512 | rpc->tpdf = calloc(c_tpdf, sizeof *rpc->tpdf); |
| 3513 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3514 | if (c_ftrs) { |
| 3515 | rpc->features = calloc(c_ftrs, sizeof *rpc->features); |
| 3516 | } |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3517 | |
| 3518 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 3519 | if (!strcmp(sub->name, "typedef")) { |
| 3520 | r = fill_yin_typedef(module, retval, sub, &rpc->tpdf[rpc->tpdf_size]); |
| 3521 | rpc->tpdf_size++; |
| 3522 | |
| 3523 | if (r) { |
| 3524 | goto error; |
| 3525 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3526 | } else if (!strcmp(sub->name, "if-feature")) { |
| 3527 | GETVAL(value, sub, "name"); |
| 3528 | rpc->features[rpc->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 3529 | if (!rpc->features[rpc->features_size]) { |
| 3530 | goto error; |
| 3531 | } |
| 3532 | rpc->features_size++; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | lyxml_free_elem(module->ctx, sub); |
| 3536 | } |
| 3537 | |
| 3538 | /* last part - process data nodes */ |
| 3539 | LY_TREE_FOR_SAFE(root.child, next, sub) { |
| 3540 | if (!strcmp(sub->name, "grouping")) { |
| 3541 | mnode = read_yin_grouping(module, retval, sub, resolve, unres); |
| 3542 | } else if (!strcmp(sub->name, "input")) { |
| 3543 | mnode = read_yin_input_output(module, retval, sub, resolve, unres); |
| 3544 | } else if (!strcmp(sub->name, "output")) { |
| 3545 | mnode = read_yin_input_output(module, retval, sub, resolve, unres); |
| 3546 | } |
| 3547 | lyxml_free_elem(module->ctx, sub); |
| 3548 | |
| 3549 | if (!mnode) { |
| 3550 | goto error; |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 3555 | goto error; |
| 3556 | } |
| 3557 | |
| 3558 | return retval; |
| 3559 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3560 | error: |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3561 | |
| 3562 | ly_mnode_free(retval); |
Michal Vasko | 3a9abf8 | 2015-06-17 10:18:26 +0200 | [diff] [blame] | 3563 | while (root.child) { |
| 3564 | lyxml_free_elem(module->ctx, root.child); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | return NULL; |
| 3568 | } |
| 3569 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3570 | static int |
| 3571 | find_grouping(struct ly_mnode *parent, struct ly_mnode_uses *uses, int line) |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3572 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3573 | struct ly_module *searchmod = NULL, *module = uses->module; |
| 3574 | struct ly_mnode *mnode, *mnode_aux; |
| 3575 | const char *name; |
| 3576 | int prefix_len = 0; |
| 3577 | int i; |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3578 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3579 | /* get referenced grouping */ |
| 3580 | name = strchr(uses->name, ':'); |
| 3581 | if (!name) { |
| 3582 | /* no prefix, search in local tree */ |
| 3583 | name = uses->name; |
| 3584 | } else { |
| 3585 | /* there is some prefix, check if it refer the same data model */ |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3586 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3587 | /* set name to correct position after colon */ |
| 3588 | prefix_len = name - uses->name; |
| 3589 | name++; |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3590 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3591 | if (!strncmp(uses->name, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 3592 | /* prefix refers to the current module, ignore it */ |
| 3593 | prefix_len = 0; |
| 3594 | } |
| 3595 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3596 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3597 | /* search */ |
| 3598 | if (prefix_len) { |
| 3599 | /* in top-level groupings of some other module */ |
| 3600 | for (i = 0; i < module->imp_size; i++) { |
| 3601 | if (!strncmp(module->imp[i].prefix, uses->name, prefix_len) |
| 3602 | && !module->imp[i].prefix[prefix_len]) { |
| 3603 | searchmod = module->imp[i].module; |
| 3604 | break; |
| 3605 | } |
| 3606 | } |
| 3607 | if (!searchmod) { |
| 3608 | /* uses refers unknown data model */ |
| 3609 | LOGVAL(VE_INPREFIX, line, name); |
| 3610 | return EXIT_FAILURE; |
| 3611 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3612 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3613 | LY_TREE_FOR(searchmod->data, mnode) { |
| 3614 | if (mnode->nodetype == LY_NODE_GROUPING && !strcmp(mnode->name, name)) { |
| 3615 | uses->grp = (struct ly_mnode_grp *)mnode; |
| 3616 | return EXIT_SUCCESS; |
| 3617 | } |
| 3618 | } |
| 3619 | } else { |
| 3620 | /* in local tree hierarchy */ |
| 3621 | for (mnode_aux = parent; mnode_aux; mnode_aux = mnode_aux->parent) { |
| 3622 | LY_TREE_FOR(mnode_aux->child, mnode) { |
| 3623 | if (mnode->nodetype == LY_NODE_GROUPING && !strcmp(mnode->name, name)) { |
| 3624 | uses->grp = (struct ly_mnode_grp *)mnode; |
| 3625 | return EXIT_SUCCESS; |
| 3626 | } |
| 3627 | } |
| 3628 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3629 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3630 | /* search in top level of the current module */ |
| 3631 | LY_TREE_FOR(module->data, mnode) { |
| 3632 | if (mnode->nodetype == LY_NODE_GROUPING && !strcmp(mnode->name, name)) { |
| 3633 | uses->grp = (struct ly_mnode_grp *)mnode; |
| 3634 | return EXIT_SUCCESS; |
| 3635 | } |
| 3636 | } |
Radek Krejci | 1e9b999 | 2015-06-04 17:57:04 +0200 | [diff] [blame] | 3637 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3638 | /* search in top-level of included modules */ |
| 3639 | for (i = 0; i < module->inc_size; i++) { |
| 3640 | LY_TREE_FOR(module->inc[i].submodule->data, mnode) { |
| 3641 | if (mnode->nodetype == LY_NODE_GROUPING && !strcmp(mnode->name, name)) { |
| 3642 | uses->grp = (struct ly_mnode_grp *)mnode; |
| 3643 | return EXIT_SUCCESS; |
| 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | } |
Radek Krejci | 1e9b999 | 2015-06-04 17:57:04 +0200 | [diff] [blame] | 3648 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3649 | /* not found, but no explicit error occured */ |
| 3650 | return EXIT_SUCCESS; |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 3651 | } |
| 3652 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 3653 | static int |
| 3654 | resolve_augment(struct ly_augment *aug, struct ly_mnode *parent, struct ly_module *module, unsigned int line) |
| 3655 | { |
| 3656 | struct lyxml_elem *yin, *next, *sub; |
| 3657 | struct ly_mnode *mnode; |
| 3658 | |
| 3659 | assert(module); |
| 3660 | |
| 3661 | /* resolve target node */ |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 3662 | aug->target = resolve_schema_nodeid(aug->target_name, parent, module, LY_NODE_AUGMENT); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 3663 | if (!aug->target) { |
| 3664 | LOGVAL(VE_INARG, line, aug->target, "uses"); |
| 3665 | return EXIT_FAILURE; |
| 3666 | } |
| 3667 | |
| 3668 | if (!aug->child) { |
| 3669 | /* nothing to do */ |
| 3670 | return EXIT_SUCCESS; |
| 3671 | } |
| 3672 | |
| 3673 | yin = (struct lyxml_elem *)aug->child; |
| 3674 | |
| 3675 | if (read_yin_common(module, aug->target, (struct ly_mnode *)aug, yin, OPT_CONFIG)) { |
| 3676 | return EXIT_FAILURE; |
| 3677 | } |
| 3678 | |
| 3679 | LY_TREE_FOR_SAFE(yin->child, next, sub) { |
| 3680 | if (!strcmp(sub->name, "container")) { |
| 3681 | mnode = read_yin_container(module, aug->target, sub, 1, NULL); |
| 3682 | } else if (!strcmp(sub->name, "leaf-list")) { |
| 3683 | mnode = read_yin_leaflist(module, aug->target, sub, 1); |
| 3684 | } else if (!strcmp(sub->name, "leaf")) { |
| 3685 | mnode = read_yin_leaf(module, aug->target, sub, 1); |
| 3686 | } else if (!strcmp(sub->name, "list")) { |
| 3687 | mnode = read_yin_list(module, aug->target, sub, 1, NULL); |
| 3688 | } else if (!strcmp(sub->name, "uses")) { |
| 3689 | mnode = read_yin_uses(module, aug->target, sub, 1, NULL); |
| 3690 | } else if (!strcmp(sub->name, "choice")) { |
| 3691 | mnode = read_yin_case(module, aug->target, sub, 1, NULL); |
| 3692 | } else if (!strcmp(sub->name, "case")) { |
| 3693 | mnode = read_yin_case(module, aug->target, sub, 1, NULL); |
| 3694 | } else if (!strcmp(sub->name, "anyxml")) { |
| 3695 | mnode = read_yin_anyxml(module, aug->target, sub, 1); |
| 3696 | #if 0 |
| 3697 | } else { |
| 3698 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
Radek Krejci | 3de29a7 | 2015-06-16 15:23:03 +0200 | [diff] [blame] | 3699 | return EXIT_FAILURE; |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 3700 | #else |
| 3701 | } else { |
| 3702 | continue; |
| 3703 | #endif |
| 3704 | } |
| 3705 | |
| 3706 | if (!mnode) { |
| 3707 | return EXIT_FAILURE; |
| 3708 | } |
Radek Krejci | 3de29a7 | 2015-06-16 15:23:03 +0200 | [diff] [blame] | 3709 | /* check for mandatory nodes - if the target node is in another module |
| 3710 | * the added nodes cannot be mandatory |
| 3711 | */ |
| 3712 | if (check_mandatory(mnode)) { |
| 3713 | LOGVAL(VE_SPEC, LOGLINE(sub), "When augmenting data in another module, mandatory statement is not allowed."); |
| 3714 | return EXIT_FAILURE; |
| 3715 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 3716 | |
| 3717 | lyxml_free_elem(module->ctx, sub); |
| 3718 | |
| 3719 | /* the parent pointer will point to the augment node, but all |
| 3720 | * siblings pointers and possibly the child node in target does |
| 3721 | * not know about the augment and follow the standard schema tree |
| 3722 | * structure |
| 3723 | */ |
| 3724 | mnode->parent = (struct ly_mnode *)aug; |
| 3725 | mnode = NULL; |
| 3726 | } |
| 3727 | |
| 3728 | lyxml_free_elem(module->ctx, yin); |
| 3729 | aug->child = NULL; |
| 3730 | |
| 3731 | return EXIT_SUCCESS; |
| 3732 | } |
| 3733 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3734 | int |
| 3735 | resolve_uses(struct ly_mnode_uses *uses, unsigned int line) |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3736 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3737 | struct ly_ctx *ctx; |
| 3738 | struct ly_mnode *mnode = NULL, *mnode_aux; |
| 3739 | struct ly_refine *rfn; |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 3740 | struct ly_restr *newmust; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3741 | int i, j; |
| 3742 | uint8_t size; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3743 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3744 | /* copy the data nodes from grouping into the uses context */ |
| 3745 | LY_TREE_FOR(uses->grp->child, mnode) { |
| 3746 | mnode_aux = ly_mnode_dup(uses->module, mnode, uses->flags, 1, line); |
| 3747 | if (!mnode_aux) { |
| 3748 | LOGVAL(VE_SPEC, line, "Copying data from grouping failed"); |
| 3749 | return EXIT_FAILURE; |
| 3750 | } |
| 3751 | if (ly_mnode_addchild((struct ly_mnode *)uses, mnode_aux)) { |
| 3752 | ly_mnode_free(mnode_aux); |
| 3753 | return EXIT_FAILURE; |
| 3754 | } |
| 3755 | } |
| 3756 | ctx = uses->module->ctx; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3757 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3758 | /* apply refines */ |
| 3759 | for (i = 0; i < uses->refine_size; i++) { |
| 3760 | rfn = &uses->refine[i]; |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 3761 | mnode = resolve_schema_nodeid(rfn->target, (struct ly_mnode *)uses, uses->module, LY_NODE_USES); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3762 | if (!mnode) { |
| 3763 | LOGVAL(VE_INARG, line, rfn->target, "uses"); |
| 3764 | return EXIT_FAILURE; |
| 3765 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3766 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3767 | if (rfn->target_type && !(mnode->nodetype & rfn->target_type)) { |
| 3768 | LOGVAL(VE_SPEC, line, "refine substatements not applicable to the target-node"); |
| 3769 | return EXIT_FAILURE; |
| 3770 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3771 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3772 | /* description on any nodetype */ |
| 3773 | if (rfn->dsc) { |
| 3774 | lydict_remove(ctx, mnode->dsc); |
| 3775 | mnode->dsc = lydict_insert(ctx, rfn->dsc, 0); |
| 3776 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3777 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3778 | /* reference on any nodetype */ |
| 3779 | if (rfn->ref) { |
| 3780 | lydict_remove(ctx, mnode->ref); |
| 3781 | mnode->ref = lydict_insert(ctx, rfn->ref, 0); |
| 3782 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3783 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3784 | /* config on any nodetype */ |
| 3785 | if (rfn->flags & LY_NODE_CONFIG_MASK) { |
| 3786 | mnode->flags &= ~LY_NODE_CONFIG_MASK; |
| 3787 | mnode->flags |= (rfn->flags & LY_NODE_CONFIG_MASK); |
| 3788 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3789 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3790 | /* default value ... */ |
| 3791 | if (rfn->mod.dflt) { |
| 3792 | if (mnode->nodetype == LY_NODE_LEAF) { |
| 3793 | /* leaf */ |
| 3794 | lydict_remove(ctx, ((struct ly_mnode_leaf *)mnode)->dflt); |
| 3795 | ((struct ly_mnode_leaf *)mnode)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0); |
| 3796 | } else if (mnode->nodetype == LY_NODE_CHOICE) { |
| 3797 | /* choice */ |
Michal Vasko | 6f6ac23 | 2015-06-18 11:11:46 +0200 | [diff] [blame] | 3798 | ((struct ly_mnode_choice *)mnode)->dflt = resolve_schema_nodeid(rfn->mod.dflt, mnode, mnode->module, LY_NODE_CHOICE); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3799 | if (!((struct ly_mnode_choice *)mnode)->dflt) { |
| 3800 | LOGVAL(VE_INARG, line, rfn->mod.dflt, "default"); |
| 3801 | return EXIT_FAILURE; |
| 3802 | } |
| 3803 | } |
| 3804 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3805 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3806 | /* mandatory on leaf, anyxml or choice */ |
| 3807 | if (mnode->nodetype & (LY_NODE_LEAF | LY_NODE_ANYXML | LY_NODE_CHOICE)) { |
| 3808 | if (rfn->flags & LY_NODE_MAND_FALSE) { |
| 3809 | /* erase mandatory true flag, we don't use false flag in schema nodes */ |
| 3810 | mnode->flags &= ~LY_NODE_MAND_TRUE; |
| 3811 | } else if (rfn->flags & LY_NODE_MAND_TRUE) { |
| 3812 | /* set mandatory true flag */ |
| 3813 | mnode->flags |= LY_NODE_MAND_TRUE; |
| 3814 | } |
| 3815 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3816 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3817 | /* presence on container */ |
| 3818 | if ((mnode->nodetype & LY_NODE_CONTAINER) && rfn->mod.presence) { |
| 3819 | lydict_remove(ctx, ((struct ly_mnode_container *)mnode)->presence); |
| 3820 | ((struct ly_mnode_container *)mnode)->presence = lydict_insert(ctx, rfn->mod.presence, 0); |
| 3821 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3822 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3823 | /* min/max-elements on list or leaf-list */ |
| 3824 | if (mnode->nodetype & (LY_NODE_LEAFLIST | LY_NODE_LIST)) { |
| 3825 | /* magic - bit 3 in flags means min set, bit 4 says max set */ |
| 3826 | if (rfn->flags & 0x04) { |
| 3827 | ((struct ly_mnode_list *)mnode)->min = rfn->mod.list.min; |
| 3828 | } |
| 3829 | if (rfn->flags & 0x08) { |
| 3830 | ((struct ly_mnode_list *)mnode)->max = rfn->mod.list.max; |
| 3831 | } |
| 3832 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3833 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3834 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 3835 | if (rfn->must_size) { |
| 3836 | size = ((struct ly_mnode_leaf *)mnode)->must_size + rfn->must_size; |
| 3837 | newmust = realloc(((struct ly_mnode_leaf *)mnode)->must, size * sizeof *rfn->must); |
| 3838 | if (!newmust) { |
| 3839 | LOGMEM; |
| 3840 | return EXIT_FAILURE; |
| 3841 | } |
| 3842 | for (i = 0, j = ((struct ly_mnode_leaf *)mnode)->must_size; i < rfn->must_size; i++, j++) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 3843 | newmust[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3844 | newmust[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0); |
| 3845 | newmust[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0); |
| 3846 | newmust[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0); |
| 3847 | newmust[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0); |
| 3848 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3849 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3850 | ((struct ly_mnode_leaf *)mnode)->must = newmust; |
| 3851 | ((struct ly_mnode_leaf *)mnode)->must_size = size; |
| 3852 | } |
| 3853 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3854 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3855 | /* apply augments */ |
| 3856 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 3857 | if (resolve_augment(&uses->augment[i], (struct ly_mnode *)uses, uses->module, line)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3858 | goto error; |
| 3859 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3860 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3861 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3862 | return EXIT_SUCCESS; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3863 | |
| 3864 | error: |
| 3865 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3866 | return EXIT_FAILURE; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3867 | } |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 3868 | |
| 3869 | /* |
| 3870 | * resolve - referenced grouping should be bounded to the namespace (resolved) |
| 3871 | * only when uses does not appear in grouping. In a case of grouping's uses, |
| 3872 | * we just get information but we do not apply augment or refine to it. |
| 3873 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3874 | static struct ly_mnode * |
| 3875 | read_yin_uses(struct ly_module *module, |
| 3876 | struct ly_mnode *parent, struct lyxml_elem *node, int resolve, struct mnode_list **unres) |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 3877 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3878 | struct lyxml_elem *sub, *next; |
| 3879 | struct ly_mnode *retval; |
| 3880 | struct ly_mnode_uses *uses; |
| 3881 | struct mnode_list *unres_new; |
| 3882 | const char *value; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3883 | int c_ref = 0, c_aug = 0, c_ftrs = 0; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3884 | int r; |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 3885 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3886 | uses = calloc(1, sizeof *uses); |
| 3887 | uses->nodetype = LY_NODE_USES; |
| 3888 | uses->prev = (struct ly_mnode *)uses; |
| 3889 | retval = (struct ly_mnode *)uses; |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 3890 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3891 | GETVAL(value, node, "name"); |
| 3892 | uses->name = lydict_insert(module->ctx, value, 0); |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 3893 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3894 | if (read_yin_common(module, parent, retval, node, OPT_MODULE | (resolve ? OPT_INHERIT : 0))) { |
| 3895 | goto error; |
| 3896 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3897 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3898 | /* get other properties of uses */ |
| 3899 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 3900 | if (!strcmp(sub->name, "refine")) { |
| 3901 | c_ref++; |
| 3902 | } else if (!strcmp(sub->name, "augment")) { |
| 3903 | c_aug++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3904 | } else if (!strcmp(sub->name, "if-feature")) { |
Radek Krejci | 56e8977 | 2015-06-19 10:00:54 +0200 | [diff] [blame] | 3905 | c_ftrs++; |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 3906 | } else if (!strcmp(sub->name, "when")) { |
| 3907 | if (uses->when) { |
| 3908 | LOGVAL(VE_TOOMANY, LOGLINE(sub), sub->name, node->name); |
| 3909 | goto error; |
| 3910 | } |
| 3911 | |
| 3912 | uses->when = read_yin_when(module, sub); |
| 3913 | lyxml_free_elem(module->ctx, sub); |
| 3914 | |
| 3915 | if (!uses->when) { |
| 3916 | goto error; |
| 3917 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3918 | } else { |
| 3919 | LOGVAL(VE_INSTMT, LOGLINE(sub), sub->name); |
| 3920 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3921 | } |
| 3922 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 3923 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3924 | /* process properties with cardinality 0..n */ |
| 3925 | if (c_ref) { |
| 3926 | uses->refine = calloc(c_ref, sizeof *uses->refine); |
| 3927 | } |
| 3928 | if (c_aug) { |
| 3929 | uses->augment = calloc(c_aug, sizeof *uses->augment); |
| 3930 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3931 | if (c_ftrs) { |
| 3932 | uses->features = calloc(c_ftrs, sizeof *uses->features); |
| 3933 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 3934 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3935 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
| 3936 | if (!strcmp(sub->name, "refine")) { |
| 3937 | r = fill_yin_refine(module, sub, &uses->refine[uses->refine_size]); |
| 3938 | uses->refine_size++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3939 | lyxml_free_elem(module->ctx, sub); |
| 3940 | } else if (!strcmp(sub->name, "augment")) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3941 | r = fill_yin_augment(module, retval, sub, &uses->augment[uses->augment_size]); |
| 3942 | uses->augment_size++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3943 | } else if (!strcmp(sub->name, "if-feature")) { |
| 3944 | GETVAL(value, sub, "name"); |
| 3945 | uses->features[uses->features_size] = resolve_feature(value, module, LOGLINE(sub)); |
| 3946 | if (!uses->features[uses->features_size]) { |
| 3947 | goto error; |
| 3948 | } |
Radek Krejci | b1f1da6 | 2015-06-19 10:09:53 +0200 | [diff] [blame] | 3949 | r = 0; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 3950 | uses->features_size++; |
| 3951 | lyxml_free_elem(module->ctx, sub); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3952 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 3953 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3954 | if (r) { |
| 3955 | goto error; |
| 3956 | } |
| 3957 | } |
Radek Krejci | 3bde87f | 2015-06-05 16:51:58 +0200 | [diff] [blame] | 3958 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3959 | if (find_grouping(parent, uses, LOGLINE(node))) { |
| 3960 | goto error; |
| 3961 | } |
| 3962 | if (!uses->grp) { |
| 3963 | LOGVRB("Unresolved uses of \"%s\" (line %d), trying to resolve it later", uses->name, LOGLINE(node)); |
| 3964 | unres_new = calloc(1, sizeof *unres_new); |
| 3965 | if (*unres) { |
| 3966 | unres_new->next = *unres; |
| 3967 | } |
| 3968 | unres_new->mnode = retval; |
| 3969 | unres_new->line = LOGLINE(node); |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 3970 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3971 | /* put it at the beginning of the unresolved list */ |
| 3972 | *unres = unres_new; |
| 3973 | } |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 3974 | |
Radek Krejci | 368c38f | 2015-06-15 15:09:55 +0200 | [diff] [blame] | 3975 | if (parent && ly_mnode_addchild(parent, retval)) { |
| 3976 | goto error; |
| 3977 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3978 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3979 | if (resolve) { |
| 3980 | /* inherit config flag */ |
| 3981 | if (parent) { |
| 3982 | retval->flags |= parent->flags & LY_NODE_CONFIG_MASK; |
| 3983 | } else { |
| 3984 | /* default config is true */ |
| 3985 | retval->flags |= LY_NODE_CONFIG_W; |
| 3986 | } |
| 3987 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 3988 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3989 | if (resolve && uses->grp) { |
| 3990 | /* copy the data nodes from grouping into the uses context */ |
| 3991 | if (resolve_uses(uses, LOGLINE(node))) { |
| 3992 | goto error; |
| 3993 | } |
| 3994 | } |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3995 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 3996 | return retval; |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 3997 | |
| 3998 | error: |
| 3999 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4000 | ly_mnode_free(retval); |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 4001 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4002 | return NULL; |
Radek Krejci | c7c9a6c | 2015-05-25 16:35:06 +0200 | [diff] [blame] | 4003 | } |
| 4004 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4005 | /* common code for yin_read_module() and yin_read_submodule() */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4006 | static int |
| 4007 | read_sub_module(struct ly_module *module, struct lyxml_elem *yin) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4008 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4009 | struct ly_ctx *ctx = module->ctx; |
| 4010 | struct ly_submodule *submodule = (struct ly_submodule *)module; |
| 4011 | struct lyxml_elem *next, *node, *child, root, grps, rpcs, notifs; |
| 4012 | struct ly_mnode *mnode = NULL; |
| 4013 | struct mnode_list *unres = NULL, *unres_next; /* unresolved uses */ |
| 4014 | const char *value; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4015 | int r; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 4016 | int i; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4017 | int belongsto_flag = 0; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 4018 | /* counters */ |
| 4019 | int c_imp = 0, c_rev = 0, c_tpdf = 0, c_ident = 0, c_inc = 0, c_aug = 0, c_ftrs = 0; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4020 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4021 | /* init */ |
| 4022 | memset(&root, 0, sizeof root); |
| 4023 | memset(&grps, 0, sizeof grps); |
| 4024 | memset(&rpcs, 0, sizeof rpcs); |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 4025 | memset(¬ifs, 0, sizeof notifs); |
Radek Krejci | e0674f8 | 2015-06-15 13:58:51 +0200 | [diff] [blame] | 4026 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4027 | /* |
| 4028 | * in the first run, we process elements with cardinality of 1 or 0..1 and |
| 4029 | * count elements with cardinality 0..n. Data elements (choices, containers, |
| 4030 | * leafs, lists, leaf-lists) are moved aside to be processed last, since we |
| 4031 | * need have all top-level and groupings already prepared at that time. In |
| 4032 | * the middle loop, we process other elements with carinality of 0..n since |
| 4033 | * we need to allocate arrays to store them. |
| 4034 | */ |
| 4035 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 4036 | if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) { |
| 4037 | lyxml_free_elem(ctx, node); |
| 4038 | continue; |
| 4039 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4040 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4041 | if (!module->type && !strcmp(node->name, "namespace")) { |
| 4042 | if (module->ns) { |
| 4043 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 4044 | goto error; |
| 4045 | } |
| 4046 | GETVAL(value, node, "uri"); |
| 4047 | module->ns = lydict_insert(ctx, value, strlen(value)); |
| 4048 | lyxml_free_elem(ctx, node); |
| 4049 | } else if (!module->type && !strcmp(node->name, "prefix")) { |
| 4050 | if (module->prefix) { |
| 4051 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 4052 | goto error; |
| 4053 | } |
| 4054 | GETVAL(value, node, "value"); |
| 4055 | if (check_identifier(value, LY_IDENT_PREFIX, LOGLINE(node), module, NULL)) { |
| 4056 | goto error; |
| 4057 | } |
| 4058 | module->prefix = lydict_insert(ctx, value, strlen(value)); |
| 4059 | lyxml_free_elem(ctx, node); |
| 4060 | } else if (module->type && !strcmp(node->name, "belongs-to")) { |
| 4061 | if (belongsto_flag) { |
| 4062 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 4063 | goto error; |
| 4064 | } |
| 4065 | belongsto_flag = 1; |
| 4066 | GETVAL(value, node, "module"); |
| 4067 | while (submodule->belongsto->type) { |
| 4068 | submodule->belongsto = ((struct ly_submodule *)submodule->belongsto)->belongsto; |
| 4069 | } |
| 4070 | if (value != submodule->belongsto->name) { |
| 4071 | LOGVAL(VE_INARG, LOGLINE(node), value, node->name); |
| 4072 | goto error; |
| 4073 | } |
Radek Krejci | f388693 | 2015-06-04 17:36:06 +0200 | [diff] [blame] | 4074 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4075 | /* get the prefix substatement, start with checks */ |
| 4076 | if (!node->child) { |
| 4077 | LOGVAL(VE_MISSSTMT2, LOGLINE(node), "prefix", node->name); |
| 4078 | goto error; |
| 4079 | } else if (strcmp(node->child->name, "prefix")) { |
| 4080 | LOGVAL(VE_INSTMT, LOGLINE(node->child), node->child->name); |
| 4081 | goto error; |
| 4082 | } else if (node->child->next) { |
| 4083 | LOGVAL(VE_INSTMT, LOGLINE(node->child->next), node->child->next->name); |
| 4084 | goto error; |
| 4085 | } |
| 4086 | /* and now finally get the value */ |
| 4087 | GETVAL(value, node->child, "value"); |
| 4088 | /* check here differs from a generic prefix check, since this prefix |
| 4089 | * don't have to be unique |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 4090 | */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4091 | if (check_identifier(value, LY_IDENT_NAME, LOGLINE(node->child), NULL, NULL)) { |
| 4092 | goto error; |
| 4093 | } |
| 4094 | module->prefix = lydict_insert(ctx, value, strlen(value)); |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 4095 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4096 | /* we are done with belongs-to */ |
| 4097 | lyxml_free_elem(ctx, node); |
| 4098 | } else if (!strcmp(node->name, "import")) { |
| 4099 | c_imp++; |
| 4100 | } else if (!strcmp(node->name, "revision")) { |
| 4101 | c_rev++; |
| 4102 | } else if (!strcmp(node->name, "typedef")) { |
| 4103 | c_tpdf++; |
| 4104 | } else if (!strcmp(node->name, "identity")) { |
| 4105 | c_ident++; |
| 4106 | } else if (!strcmp(node->name, "include")) { |
| 4107 | c_inc++; |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 4108 | } else if (!strcmp(node->name, "augment")) { |
| 4109 | c_aug++; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 4110 | } else if (!strcmp(node->name, "feature")) { |
| 4111 | c_ftrs++; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4112 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4113 | /* data statements */ |
| 4114 | } else if (!strcmp(node->name, "container") || |
| 4115 | !strcmp(node->name, "leaf-list") || |
| 4116 | !strcmp(node->name, "leaf") || |
| 4117 | !strcmp(node->name, "list") || |
| 4118 | !strcmp(node->name, "choice") || |
| 4119 | !strcmp(node->name, "uses") || |
| 4120 | !strcmp(node->name, "anyxml")) { |
| 4121 | lyxml_unlink_elem(node); |
| 4122 | lyxml_add_child(&root, node); |
| 4123 | } else if (!strcmp(node->name, "grouping")) { |
| 4124 | /* keep groupings separated and process them before other data statements */ |
| 4125 | lyxml_unlink_elem(node); |
| 4126 | lyxml_add_child(&grps, node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4127 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4128 | /* optional statements */ |
| 4129 | } else if (!strcmp(node->name, "description")) { |
| 4130 | if (module->dsc) { |
| 4131 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 4132 | goto error; |
| 4133 | } |
| 4134 | module->dsc = read_yin_subnode(ctx, node, "text"); |
| 4135 | lyxml_free_elem(ctx, node); |
| 4136 | if (!module->dsc) { |
| 4137 | goto error; |
| 4138 | } |
| 4139 | } else if (!strcmp(node->name, "reference")) { |
| 4140 | if (module->ref) { |
| 4141 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 4142 | goto error; |
| 4143 | } |
| 4144 | module->ref = read_yin_subnode(ctx, node, "text"); |
| 4145 | lyxml_free_elem(ctx, node); |
| 4146 | if (!module->ref) { |
| 4147 | goto error; |
| 4148 | } |
| 4149 | } else if (!strcmp(node->name, "organization")) { |
| 4150 | if (module->org) { |
| 4151 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 4152 | goto error; |
| 4153 | } |
| 4154 | module->org = read_yin_subnode(ctx, node, "text"); |
| 4155 | lyxml_free_elem(ctx, node); |
| 4156 | if (!module->org) { |
| 4157 | goto error; |
| 4158 | } |
| 4159 | } else if (!strcmp(node->name, "contact")) { |
| 4160 | if (module->contact) { |
| 4161 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 4162 | goto error; |
| 4163 | } |
| 4164 | module->contact = read_yin_subnode(ctx, node, "text"); |
| 4165 | lyxml_free_elem(ctx, node); |
| 4166 | if (!module->contact) { |
| 4167 | goto error; |
| 4168 | } |
| 4169 | } else if (!strcmp(node->name, "yang-version")) { |
| 4170 | /* TODO: support YANG 1.1 ? */ |
| 4171 | if (module->version) { |
| 4172 | LOGVAL(VE_TOOMANY, LOGLINE(node), node->name, yin->name); |
| 4173 | goto error; |
| 4174 | } |
| 4175 | GETVAL(value, node, "value"); |
| 4176 | if (strcmp(value, "1")) { |
| 4177 | LOGVAL(VE_INARG, LOGLINE(node), value, "yang-version"); |
| 4178 | goto error; |
| 4179 | } |
| 4180 | module->version = 1; |
| 4181 | lyxml_free_elem(ctx, node); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 4182 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4183 | /* rpcs & notifications */ |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 4184 | } else if (!strcmp(node->name, "rpc")) { |
| 4185 | lyxml_unlink_elem(node); |
| 4186 | lyxml_add_child(&rpcs, node); |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 4187 | } else if (!strcmp(node->name, "notification")) { |
| 4188 | lyxml_unlink_elem(node); |
| 4189 | lyxml_add_child(¬ifs, node); |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 4190 | #if 0 |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4191 | } else { |
| 4192 | LOGVAL(VE_INSTMT, LOGLINE(node), node->name); |
| 4193 | goto error; |
Radek Krejci | 8eaf9bd | 2015-06-04 11:05:18 +0200 | [diff] [blame] | 4194 | #else |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4195 | } else { |
| 4196 | continue; |
Radek Krejci | 1e3f890 | 2015-06-03 11:00:11 +0200 | [diff] [blame] | 4197 | #endif |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4198 | } |
| 4199 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4200 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4201 | if (!submodule) { |
| 4202 | /* check for mandatory statements */ |
| 4203 | if (!module->ns) { |
| 4204 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "namespace", "module"); |
| 4205 | goto error; |
| 4206 | } |
| 4207 | if (!module->prefix) { |
| 4208 | LOGVAL(VE_MISSSTMT2, LOGLINE(yin), "prefix", "module"); |
| 4209 | goto error; |
| 4210 | } |
| 4211 | } |
Radek Krejci | bb3257d | 2015-05-21 23:03:51 +0200 | [diff] [blame] | 4212 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4213 | /* allocate arrays for elements with cardinality of 0..n */ |
| 4214 | if (c_imp) { |
| 4215 | module->imp = calloc(c_imp, sizeof *module->imp); |
| 4216 | } |
| 4217 | if (c_rev) { |
| 4218 | module->rev = calloc(c_rev, sizeof *module->rev); |
| 4219 | } |
| 4220 | if (c_tpdf) { |
| 4221 | module->tpdf = calloc(c_tpdf, sizeof *module->tpdf); |
| 4222 | } |
| 4223 | if (c_ident) { |
| 4224 | module->ident = calloc(c_ident, sizeof *module->ident); |
| 4225 | } |
| 4226 | if (c_inc) { |
| 4227 | module->inc = calloc(c_inc, sizeof *module->inc); |
| 4228 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 4229 | if (c_aug) { |
| 4230 | module->augment = calloc(c_aug, sizeof *module->augment); |
| 4231 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 4232 | if (c_ftrs) { |
| 4233 | module->features = calloc(c_ftrs, sizeof *module->features); |
| 4234 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4235 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4236 | /* middle part - process nodes with cardinality of 0..n except the data nodes */ |
| 4237 | LY_TREE_FOR_SAFE(yin->child, next, node) { |
| 4238 | if (!strcmp(node->name, "import")) { |
| 4239 | r = fill_yin_import(module, node, &module->imp[module->imp_size]); |
| 4240 | module->imp_size++; |
| 4241 | if (r) { |
| 4242 | goto error; |
| 4243 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 4244 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4245 | /* check duplicities in imported modules */ |
| 4246 | for (i = 0; i < module->imp_size - 1; i++) { |
| 4247 | if (!strcmp(module->imp[i].module->name, module->imp[module->imp_size - 1].module->name)) { |
| 4248 | LOGVAL(VE_SPEC, LOGLINE(node), "Importing module \"%s\" repeatedly.", module->imp[i].module->name); |
| 4249 | goto error; |
| 4250 | } |
| 4251 | } |
| 4252 | } else if (!strcmp(node->name, "include")) { |
| 4253 | r = fill_yin_include(module, node, &module->inc[module->inc_size]); |
| 4254 | module->inc_size++; |
| 4255 | if (r) { |
| 4256 | goto error; |
| 4257 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 4258 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4259 | /* check duplications in include submodules */ |
| 4260 | for (i = 0; i < module->inc_size - 1; i++) { |
| 4261 | if (!strcmp(module->inc[i].submodule->name, module->inc[module->inc_size - 1].submodule->name)) { |
| 4262 | LOGVAL(VE_SPEC, LOGLINE(node), "Importing module \"%s\" repeatedly.", |
| 4263 | module->inc[i].submodule->name); |
| 4264 | goto error; |
| 4265 | } |
| 4266 | } |
| 4267 | } else if (!strcmp(node->name, "revision")) { |
| 4268 | GETVAL(value, node, "date"); |
| 4269 | if (check_date(value, LOGLINE(node))) { |
| 4270 | goto error; |
| 4271 | } |
| 4272 | memcpy(module->rev[module->rev_size].date, value, LY_REV_SIZE - 1); |
| 4273 | /* check uniqueness of the revision date - not required by RFC */ |
| 4274 | for (i = 0; i < module->rev_size; i++) { |
| 4275 | if (!strcmp(value, module->rev[i].date)) { |
| 4276 | LOGVAL(VE_INARG, LOGLINE(node), value, node->name); |
| 4277 | LOGVAL(VE_SPEC, 0, "Revision is not unique."); |
| 4278 | } |
| 4279 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 4280 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4281 | LY_TREE_FOR(node->child, child) { |
| 4282 | if (!strcmp(child->name, "description")) { |
| 4283 | if (module->rev[module->rev_size].dsc) { |
| 4284 | LOGVAL(VE_TOOMANY, LOGLINE(node), child->name, node->name); |
| 4285 | goto error; |
| 4286 | } |
| 4287 | module->rev[module->rev_size].dsc = read_yin_subnode(ctx, child, "text"); |
| 4288 | if (!module->rev[module->rev_size].dsc) { |
| 4289 | goto error; |
| 4290 | } |
| 4291 | } else if (!strcmp(child->name, "reference")) { |
| 4292 | if (module->rev[module->rev_size].ref) { |
| 4293 | LOGVAL(VE_TOOMANY, LOGLINE(node), child->name, node->name); |
| 4294 | goto error; |
| 4295 | } |
| 4296 | module->rev[module->rev_size].ref = read_yin_subnode(ctx, child, "text"); |
| 4297 | if (!module->rev[module->rev_size].ref) { |
| 4298 | goto error; |
| 4299 | } |
| 4300 | } else { |
| 4301 | LOGVAL(VE_INSTMT, LOGLINE(child), child->name); |
| 4302 | goto error; |
| 4303 | } |
| 4304 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 4305 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4306 | /* keep the latest revision at position 0 */ |
| 4307 | if (module->rev_size && strcmp(module->rev[module->rev_size].date, module->rev[0].date) > 0) { |
| 4308 | /* switch their position */ |
| 4309 | value = strdup(module->rev[0].date); |
| 4310 | memcpy(module->rev[0].date, module->rev[module->rev_size].date, LY_REV_SIZE - 1); |
| 4311 | memcpy(module->rev[module->rev_size].date, value, LY_REV_SIZE - 1); |
| 4312 | free((char *)value); |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 4313 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4314 | if (module->rev[0].dsc != module->rev[module->rev_size].dsc) { |
| 4315 | value = module->rev[0].dsc; |
| 4316 | module->rev[0].dsc = module->rev[module->rev_size].dsc; |
| 4317 | module->rev[module->rev_size].dsc = value; |
| 4318 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 4319 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4320 | if (module->rev[0].ref != module->rev[module->rev_size].ref) { |
| 4321 | value = module->rev[0].ref; |
| 4322 | module->rev[0].ref = module->rev[module->rev_size].ref; |
| 4323 | module->rev[module->rev_size].ref = value; |
| 4324 | } |
| 4325 | } |
Radek Krejci | ce7fb78 | 2015-05-29 16:52:34 +0200 | [diff] [blame] | 4326 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4327 | module->rev_size++; |
| 4328 | } else if (!strcmp(node->name, "typedef")) { |
| 4329 | r = fill_yin_typedef(module, NULL, node, &module->tpdf[module->tpdf_size]); |
| 4330 | module->tpdf_size++; |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 4331 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4332 | if (r) { |
| 4333 | goto error; |
| 4334 | } |
| 4335 | } else if (!strcmp(node->name, "identity")) { |
| 4336 | r = fill_yin_identity(module, node, &module->ident[module->ident_size]); |
| 4337 | module->ident_size++; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 4338 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4339 | if (r) { |
| 4340 | goto error; |
| 4341 | } |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 4342 | } else if (!strcmp(node->name, "feature")) { |
| 4343 | r = fill_yin_feature(module, node, &module->features[module->features_size]); |
| 4344 | module->features_size++; |
| 4345 | |
| 4346 | if (r) { |
| 4347 | goto error; |
| 4348 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 4349 | } else if (!strcmp(node->name, "augment")) { |
| 4350 | r = fill_yin_augment(module, NULL, node, &module->augment[module->augment_size]); |
| 4351 | module->augment_size++; |
| 4352 | |
| 4353 | if (r) { |
| 4354 | goto error; |
| 4355 | } |
| 4356 | |
| 4357 | /* node is reconnected into the augment, so we have to skip its free at the end of the loop */ |
| 4358 | continue; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4359 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4360 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4361 | lyxml_free_elem(ctx, node); |
| 4362 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4363 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 4364 | /* process data nodes. Start with groupings to allow uses |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4365 | * refer to them |
| 4366 | */ |
| 4367 | LY_TREE_FOR_SAFE(grps.child, next, node) { |
| 4368 | mnode = read_yin_grouping(module, NULL, node, 0, &unres); |
| 4369 | lyxml_free_elem(ctx, node); |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 4370 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4371 | if (!mnode) { |
| 4372 | goto error; |
| 4373 | } |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 4374 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4375 | /* include data element */ |
| 4376 | if (module->data) { |
| 4377 | module->data->prev->next = mnode; |
| 4378 | mnode->prev = module->data->prev; |
| 4379 | module->data->prev = mnode; |
| 4380 | } else { |
| 4381 | module->data = mnode; |
| 4382 | } |
| 4383 | } |
| 4384 | while (unres) { |
| 4385 | if (find_grouping(unres->mnode->parent, (struct ly_mnode_uses *)unres->mnode, unres->line)) { |
| 4386 | goto error; |
| 4387 | } |
| 4388 | if (!((struct ly_mnode_uses *)unres->mnode)->grp) { |
| 4389 | LOGVAL(VE_INARG, unres->line, unres->mnode->name, "uses"); |
| 4390 | goto error; |
| 4391 | } |
| 4392 | unres_next = unres->next; |
| 4393 | free(unres); |
| 4394 | unres = unres_next; |
| 4395 | } |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 4396 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 4397 | /* parse data nodes, ... */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4398 | LY_TREE_FOR_SAFE(root.child, next, node) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4399 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4400 | if (!strcmp(node->name, "container")) { |
| 4401 | mnode = read_yin_container(module, NULL, node, 1, &unres); |
| 4402 | } else if (!strcmp(node->name, "leaf-list")) { |
| 4403 | mnode = read_yin_leaflist(module, NULL, node, 1); |
| 4404 | } else if (!strcmp(node->name, "leaf")) { |
| 4405 | mnode = read_yin_leaf(module, NULL, node, 1); |
| 4406 | } else if (!strcmp(node->name, "list")) { |
| 4407 | mnode = read_yin_list(module, NULL, node, 1, &unres); |
| 4408 | } else if (!strcmp(node->name, "choice")) { |
| 4409 | mnode = read_yin_choice(module, NULL, node, 1, &unres); |
| 4410 | } else if (!strcmp(node->name, "uses")) { |
| 4411 | mnode = read_yin_uses(module, NULL, node, 1, &unres); |
| 4412 | } else if (!strcmp(node->name, "anyxml")) { |
| 4413 | mnode = read_yin_anyxml(module, NULL, node, 1); |
| 4414 | } |
| 4415 | lyxml_free_elem(ctx, node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4416 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4417 | if (!mnode) { |
| 4418 | goto error; |
| 4419 | } |
Radek Krejci | 25d782a | 2015-05-22 15:03:23 +0200 | [diff] [blame] | 4420 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4421 | /* include data element */ |
| 4422 | if (module->data) { |
| 4423 | module->data->prev->next = mnode; |
| 4424 | mnode->prev = module->data->prev; |
| 4425 | module->data->prev = mnode; |
| 4426 | } else { |
| 4427 | module->data = mnode; |
| 4428 | } |
| 4429 | } |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 4430 | |
| 4431 | /* ... rpcs ... */ |
| 4432 | LY_TREE_FOR_SAFE(rpcs.child, next, node) { |
| 4433 | mnode = read_yin_rpc(module, NULL, node, 0, &unres); |
| 4434 | lyxml_free_elem(ctx, node); |
| 4435 | |
| 4436 | if (!mnode) { |
| 4437 | goto error; |
| 4438 | } |
| 4439 | |
| 4440 | /* include rpc element */ |
| 4441 | if (module->rpc) { |
| 4442 | module->rpc->prev->next = mnode; |
| 4443 | mnode->prev = module->rpc->prev; |
| 4444 | module->rpc->prev = mnode; |
| 4445 | } else { |
| 4446 | module->rpc = mnode; |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | /* ... and notifications */ |
| 4451 | LY_TREE_FOR_SAFE(notifs.child, next, node) { |
| 4452 | mnode = read_yin_notif(module, NULL, node, 0, &unres); |
| 4453 | lyxml_free_elem(ctx, node); |
| 4454 | |
| 4455 | if (!mnode) { |
| 4456 | goto error; |
| 4457 | } |
| 4458 | |
| 4459 | /* include notification element */ |
| 4460 | if (module->notif) { |
| 4461 | module->notif->prev->next = mnode; |
| 4462 | mnode->prev = module->notif->prev; |
| 4463 | module->notif->prev = mnode; |
| 4464 | } else { |
| 4465 | module->notif = mnode; |
| 4466 | } |
| 4467 | } |
| 4468 | |
| 4469 | /* and now try to resolve unresolved uses, if any */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4470 | while (unres) { |
| 4471 | /* find referenced grouping */ |
| 4472 | if (find_grouping(unres->mnode->parent, (struct ly_mnode_uses *)unres->mnode, unres->line)) { |
| 4473 | goto error; |
| 4474 | } |
| 4475 | if (!((struct ly_mnode_uses *)unres->mnode)->grp) { |
| 4476 | LOGVAL(VE_INARG, unres->line, unres->mnode->name, "uses"); |
| 4477 | goto error; |
| 4478 | } |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 4479 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4480 | /* resolve uses by copying grouping content under the uses */ |
| 4481 | if (resolve_uses((struct ly_mnode_uses *)unres->mnode, unres->line)) { |
| 4482 | goto error; |
| 4483 | } |
Radek Krejci | 7470511 | 2015-06-05 10:25:44 +0200 | [diff] [blame] | 4484 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4485 | unres_next = unres->next; |
| 4486 | free(unres); |
| 4487 | unres = unres_next; |
| 4488 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4489 | |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 4490 | /* and finally apply augments */ |
| 4491 | for (i = 0; i < module->augment_size; i++) { |
| 4492 | if (resolve_augment(&module->augment[i], NULL, module, 0)) { |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 4493 | goto error; |
| 4494 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 4495 | } |
| 4496 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4497 | return EXIT_SUCCESS; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4498 | |
| 4499 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4500 | /* cleanup */ |
| 4501 | while (root.child) { |
| 4502 | lyxml_free_elem(module->ctx, root.child); |
| 4503 | } |
| 4504 | while (grps.child) { |
| 4505 | lyxml_free_elem(module->ctx, grps.child); |
| 4506 | } |
| 4507 | while (rpcs.child) { |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 4508 | lyxml_free_elem(module->ctx, rpcs.child); |
| 4509 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4510 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4511 | return EXIT_FAILURE; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4512 | } |
| 4513 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4514 | struct ly_submodule * |
| 4515 | yin_read_submodule(struct ly_module *module, const char *data) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4516 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4517 | struct lyxml_elem *yin; |
| 4518 | struct ly_submodule *submodule = NULL; |
| 4519 | const char *value; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4520 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4521 | assert(module->ctx); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4522 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4523 | yin = lyxml_read(module->ctx, data, 0); |
| 4524 | if (!yin) { |
| 4525 | return NULL; |
| 4526 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4527 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4528 | /* check root element */ |
| 4529 | if (!yin->name || strcmp(yin->name, "submodule")) { |
| 4530 | LOGVAL(VE_INSTMT, LOGLINE(yin), yin->name); |
| 4531 | goto error; |
| 4532 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4533 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4534 | GETVAL(value, yin, "name"); |
| 4535 | if (check_identifier(value, LY_IDENT_NAME, LOGLINE(yin), NULL, NULL)) { |
| 4536 | goto error; |
| 4537 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4538 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4539 | submodule = calloc(1, sizeof *submodule); |
| 4540 | if (!submodule) { |
| 4541 | LOGMEM; |
| 4542 | goto error; |
| 4543 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4544 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4545 | submodule->ctx = module->ctx; |
| 4546 | submodule->name = lydict_insert(submodule->ctx, value, strlen(value)); |
| 4547 | submodule->type = 1; |
| 4548 | submodule->belongsto = module; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4549 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4550 | LOGVRB("reading submodule %s", submodule->name); |
| 4551 | if (read_sub_module((struct ly_module *)submodule, yin)) { |
| 4552 | goto error; |
| 4553 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4554 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4555 | /* cleanup */ |
| 4556 | lyxml_free_elem(module->ctx, yin); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4557 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4558 | LOGVRB("submodule %s successfully parsed", submodule->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4559 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4560 | return submodule; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4561 | |
| 4562 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4563 | /* cleanup */ |
| 4564 | lyxml_free_elem(module->ctx, yin); |
| 4565 | ly_submodule_free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4566 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4567 | return NULL; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4568 | } |
| 4569 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4570 | struct ly_module * |
| 4571 | yin_read_module(struct ly_ctx *ctx, const char *data) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4572 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4573 | struct lyxml_elem *yin; |
| 4574 | struct ly_module *module = NULL, **newlist = NULL; |
| 4575 | const char *value; |
| 4576 | int i; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4577 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4578 | yin = lyxml_read(ctx, data, 0); |
| 4579 | if (!yin) { |
| 4580 | return NULL; |
| 4581 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4582 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4583 | /* check root element */ |
| 4584 | if (!yin->name || strcmp(yin->name, "module")) { |
| 4585 | LOGVAL(VE_INSTMT, LOGLINE(yin), yin->name); |
| 4586 | goto error; |
| 4587 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4588 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4589 | GETVAL(value, yin, "name"); |
| 4590 | if (check_identifier(value, LY_IDENT_NAME, LOGLINE(yin), NULL, NULL)) { |
| 4591 | goto error; |
| 4592 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4593 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4594 | module = calloc(1, sizeof *module); |
| 4595 | if (!module) { |
| 4596 | LOGMEM; |
| 4597 | goto error; |
| 4598 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4599 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4600 | module->ctx = ctx; |
| 4601 | module->name = lydict_insert(ctx, value, strlen(value)); |
| 4602 | module->type = 0; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4603 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4604 | LOGVRB("reading module %s", module->name); |
| 4605 | if (read_sub_module(module, yin)) { |
| 4606 | goto error; |
| 4607 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 4608 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4609 | /* add to the context's list of modules */ |
| 4610 | if (ctx->models.used == ctx->models.size) { |
| 4611 | newlist = realloc(ctx->models.list, ctx->models.size * 2); |
| 4612 | if (!newlist) { |
| 4613 | LOGMEM; |
| 4614 | goto error; |
| 4615 | } |
| 4616 | for (i = ctx->models.size; i < ctx->models.size * 2; i++) { |
| 4617 | newlist[i] = NULL; |
| 4618 | } |
| 4619 | ctx->models.size *= 2; |
| 4620 | ctx->models.list = newlist; |
| 4621 | } |
| 4622 | for (i = 0; ctx->models.list[i]; i++) { |
| 4623 | /* check name (name/revision) and namespace uniqueness */ |
| 4624 | if (!strcmp(ctx->models.list[i]->name, module->name)) { |
| 4625 | if (!ctx->models.list[i]->rev_size && !module->rev_size) { |
| 4626 | /* both data models are same, with no revision specified */ |
| 4627 | LOGERR(LY_EINVAL, "Module \"%s\" (no revision in either of them specified) already in context.", |
| 4628 | module->name); |
| 4629 | goto error; |
| 4630 | } else if (!ctx->models.list[i]->rev_size || !module->rev_size) { |
| 4631 | /* one of the models does not have a revision, so they differs */ |
| 4632 | continue; |
| 4633 | } else { |
| 4634 | /* both models have a revision statement which we have to |
| 4635 | * compare, revision at position 0 is the last revision |
| 4636 | */ |
| 4637 | if (!strcmp(ctx->models.list[i]->rev[0].date, module->rev[0].date)) { |
| 4638 | /* we have the same modules */ |
| 4639 | LOGERR(LY_EINVAL, "Module \"%s\", revision %s already in context.", module->name, |
| 4640 | module->rev[0].date); |
| 4641 | goto error; |
| 4642 | } |
| 4643 | } |
| 4644 | } else if (!strcmp(ctx->models.list[i]->ns, module->ns)) { |
| 4645 | LOGERR(LY_EINVAL, "Two different modules (\"%s\" and \"%s\") have the same namespace \"%s\"", |
| 4646 | ctx->models.list[i]->name, module->name, module->ns); |
| 4647 | goto error; |
| 4648 | } |
| 4649 | } |
| 4650 | ctx->models.list[i] = module; |
| 4651 | ctx->models.used++; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4652 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4653 | /* cleanup */ |
| 4654 | lyxml_free_elem(ctx, yin); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4655 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4656 | LOGVRB("module %s successfully parsed", module->name); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4657 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4658 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4659 | |
| 4660 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4661 | /* cleanup */ |
| 4662 | lyxml_free_elem(ctx, yin); |
| 4663 | ly_module_free(module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4664 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 4665 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4666 | } |