Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_yang.c |
| 3 | * @author Pavol Vican |
| 4 | * @brief YANG parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 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 | |
| 22 | #include "parser_yang.h" |
| 23 | #include "parser_yang_bis.h" |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 24 | #include "parser.h" |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 25 | #include "xpath.h" |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 26 | |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 27 | static int |
| 28 | yang_check_string(struct lys_module *module, const char **target, char *what, char *where, char *value, int line) |
| 29 | { |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 30 | if (*target) { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 31 | LOGVAL(LYE_TOOMANY, line, what, where); |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 32 | free(value); |
| 33 | return 1; |
| 34 | } else { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 35 | *target = lydict_insert_zc(module->ctx, value); |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | } |
| 39 | |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 40 | int |
| 41 | yang_read_common(struct lys_module *module, char *value, int type, int line) |
| 42 | { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 43 | int ret = 0; |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 44 | |
| 45 | switch (type) { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 46 | case MODULE_KEYWORD: |
| 47 | module->name = lydict_insert_zc(module->ctx, value); |
| 48 | break; |
| 49 | case NAMESPACE_KEYWORD: |
| 50 | ret = yang_check_string(module, &module->ns, "namespace", "module", value, line); |
| 51 | break; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 52 | case ORGANIZATION_KEYWORD: |
| 53 | ret = yang_check_string(module, &module->org, "organization", "module", value, line); |
| 54 | break; |
| 55 | case CONTACT_KEYWORD: |
| 56 | ret = yang_check_string(module, &module->contact, "contact", "module", value, line); |
| 57 | break; |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 60 | return ret; |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 63 | int |
| 64 | yang_read_prefix(struct lys_module *module, void *save, char *value, int type, int line) |
| 65 | { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 66 | int ret = 0; |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 67 | |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 68 | if (lyp_check_identifier(value, LY_IDENT_PREFIX, line, module, NULL)) { |
| 69 | free(value); |
| 70 | return EXIT_FAILURE; |
| 71 | } |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 72 | switch (type){ |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 73 | case MODULE_KEYWORD: |
| 74 | ret = yang_check_string(module, &module->prefix, "prefix", "module", value, line); |
| 75 | break; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 76 | case IMPORT_KEYWORD: |
| 77 | ((struct lys_import *)save)->prefix = lydict_insert_zc(module->ctx, value); |
| 78 | break; |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 79 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 80 | |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 81 | return ret; |
| 82 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 83 | |
| 84 | void * |
| 85 | yang_elem_of_array(void **ptr, uint8_t *act_size, int type, int sizeof_struct) |
| 86 | { |
| 87 | void *retval; |
| 88 | |
| 89 | if (!(*act_size % LY_ARRAY_SIZE) && !(*ptr = ly_realloc(*ptr, (*act_size + LY_ARRAY_SIZE) * sizeof_struct))) { |
| 90 | LOGMEM; |
| 91 | return NULL; |
| 92 | } |
| 93 | switch (type) { |
| 94 | case IMPORT_KEYWORD: |
| 95 | retval = &((struct lys_import *)(*ptr))[*act_size]; |
| 96 | break; |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 97 | case REVISION_KEYWORD: |
| 98 | retval = &((struct lys_revision *)(*ptr))[*act_size]; |
| 99 | break; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 100 | } |
| 101 | (*act_size)++; |
| 102 | memset(retval,0,sizeof_struct); |
| 103 | return retval; |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | yang_fill_import(struct lys_module *module, struct lys_import *imp, char *value, int line) |
| 108 | { |
| 109 | int count, i; |
| 110 | |
| 111 | /* check for circular import, store it if passed */ |
| 112 | if (!module->ctx->models.parsing) { |
| 113 | count = 0; |
| 114 | } else { |
| 115 | for (count = 0; module->ctx->models.parsing[count]; ++count) { |
| 116 | if (value == module->ctx->models.parsing[count]) { |
| 117 | LOGERR(LY_EVALID, "Circular import dependency on the module \"%s\".", value); |
| 118 | goto error; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | ++count; |
| 123 | module->ctx->models.parsing = |
| 124 | ly_realloc(module->ctx->models.parsing, (count + 1) * sizeof *module->ctx->models.parsing); |
| 125 | if (!module->ctx->models.parsing) { |
| 126 | LOGMEM; |
| 127 | goto error; |
| 128 | } |
| 129 | module->ctx->models.parsing[count - 1] = value; |
| 130 | module->ctx->models.parsing[count] = NULL; |
| 131 | |
| 132 | /* try to load the module */ |
| 133 | imp->module = (struct lys_module *)ly_ctx_get_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL); |
| 134 | if (!imp->module) { |
| 135 | /* whether to use a user callback is decided in the function */ |
| 136 | imp->module = (struct lys_module *)ly_ctx_load_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL); |
| 137 | } |
| 138 | |
| 139 | /* remove the new module name now that its parsing is finished (even if failed) */ |
| 140 | if (module->ctx->models.parsing[count] || (module->ctx->models.parsing[count - 1] != value)) { |
| 141 | LOGINT; |
| 142 | } |
| 143 | --count; |
| 144 | if (count) { |
| 145 | module->ctx->models.parsing[count] = NULL; |
| 146 | } else { |
| 147 | free(module->ctx->models.parsing); |
| 148 | module->ctx->models.parsing = NULL; |
| 149 | } |
| 150 | |
| 151 | /* check the result */ |
| 152 | if (!imp->module) { |
| 153 | LOGERR(LY_EVALID, "Importing \"%s\" module into \"%s\" failed.", value, module->name); |
| 154 | goto error; |
| 155 | } |
| 156 | |
| 157 | module->imp_size++; |
| 158 | |
| 159 | /* check duplicities in imported modules */ |
| 160 | for (i = 0; i < module->imp_size - 1; i++) { |
| 161 | if (!strcmp(module->imp[i].module->name, module->imp[module->imp_size - 1].module->name)) { |
| 162 | LOGVAL(LYE_SPEC, line, "Importing module \"%s\" repeatedly.", module->imp[i].module->name); |
| 163 | goto error; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | free(value); |
| 168 | return EXIT_SUCCESS; |
| 169 | |
| 170 | error: |
| 171 | |
| 172 | free(value); |
| 173 | return EXIT_FAILURE; |
| 174 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 175 | |
| 176 | int |
| 177 | yang_read_description(struct lys_module *module, void *node, char *value, int type, int line) |
| 178 | { |
| 179 | int ret; |
| 180 | |
| 181 | if (!node) { |
| 182 | ret = yang_check_string(module, &module->dsc, "description", "module", value, line); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 183 | } else { |
| 184 | switch (type) { |
| 185 | case REVISION_KEYWORD: |
| 186 | ret = yang_check_string(module, &((struct lys_revision *) node)->dsc, "description", "revision", value, line); |
| 187 | break; |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 188 | case FEATURE_KEYWORD: |
| 189 | ret = yang_check_string(module, &((struct lys_feature *) node)->dsc, "description", "feature", value, line); |
| 190 | break; |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 191 | case IDENTITY_KEYWORD: |
| 192 | ret = yang_check_string(module, &((struct lys_ident *) node)->dsc, "description", "identity", value, line); |
| 193 | break; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 194 | case MUST_KEYWORD: |
| 195 | ret = yang_check_string(module, &((struct lys_restr *) node)->dsc, "description", "must", value, line); |
| 196 | break; |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 197 | case WHEN_KEYWORD: |
| 198 | ret = yang_check_string(module, &((struct lys_when *) node)->dsc, "description", "when" , value, line); |
| 199 | break; |
Pavol Vican | 7c8ae12 | 2016-02-10 11:01:50 +0100 | [diff] [blame] | 200 | case CONTAINER_KEYWORD: |
| 201 | ret = yang_check_string(module, &((struct lys_node_container *) node)->dsc, "description", "container", value, line); |
| 202 | break; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 203 | case ANYXML_KEYWORD: |
| 204 | ret = yang_check_string(module, &((struct lys_node_anyxml *) node)->dsc, "description", "anyxml", value, line); |
| 205 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 206 | case CHOICE_KEYWORD: |
| 207 | ret = yang_check_string(module, &((struct lys_node_choice *) node)->dsc, "description", "choice", value, line); |
| 208 | break; |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 209 | case CASE_KEYWORD: |
| 210 | ret = yang_check_string(module, &((struct lys_node_case *) node)->dsc, "description", "case", value, line); |
| 211 | break; |
Pavol Vican | 12f53c3 | 2016-02-11 11:40:00 +0100 | [diff] [blame] | 212 | case GROUPING_KEYWORD: |
| 213 | ret = yang_check_string(module, &((struct lys_node_grp *) node)->dsc, "description", "grouping", value, line); |
| 214 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 215 | case LEAF_KEYWORD: |
| 216 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dsc, "description", "leaf", value, line); |
| 217 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame^] | 218 | case LEAF_LIST_KEYWORD: |
| 219 | ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->dsc, "description", "leaflist", value, line); |
| 220 | break; |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 221 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 222 | } |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | int |
| 227 | yang_read_reference(struct lys_module *module, void *node, char *value, int type, int line) |
| 228 | { |
| 229 | int ret; |
| 230 | |
| 231 | if (!node) { |
| 232 | ret = yang_check_string(module, &module->ref, "reference", "module", value, line); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 233 | } else { |
| 234 | switch (type) { |
| 235 | case REVISION_KEYWORD: |
| 236 | ret = yang_check_string(module, &((struct lys_revision *) node)->ref, "reference", "revision", value, line); |
| 237 | break; |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 238 | case FEATURE_KEYWORD: |
| 239 | ret = yang_check_string(module, &((struct lys_feature *) node)->ref, "reference", "feature", value, line); |
| 240 | break; |
| 241 | case IDENTITY_KEYWORD: |
| 242 | ret = yang_check_string(module, &((struct lys_ident *) node)->ref, "reference", "identity", value, line); |
| 243 | break; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 244 | case MUST_KEYWORD: |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 245 | ret = yang_check_string(module, &((struct lys_restr *) node)->ref, "reference", "must", value, line); |
| 246 | break; |
| 247 | case WHEN_KEYWORD: |
| 248 | ret = yang_check_string(module, &((struct lys_when *) node)->ref, "reference", "when", value, line); |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 249 | break; |
Pavol Vican | 7c8ae12 | 2016-02-10 11:01:50 +0100 | [diff] [blame] | 250 | case CONTAINER_KEYWORD: |
| 251 | ret = yang_check_string(module, &((struct lys_node_container *) node)->ref, "reference", "container", value, line); |
| 252 | break; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 253 | case ANYXML_KEYWORD: |
| 254 | ret = yang_check_string(module, &((struct lys_node_anyxml *) node)->ref, "reference", "anyxml", value, line); |
| 255 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 256 | case CHOICE_KEYWORD: |
| 257 | ret = yang_check_string(module, &((struct lys_node_anyxml *) node)->ref, "reference", "choice", value, line); |
| 258 | break; |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 259 | case CASE_KEYWORD: |
| 260 | ret = yang_check_string(module, &((struct lys_node_anyxml *) node)->ref, "reference", "case", value, line); |
| 261 | break; |
Pavol Vican | 12f53c3 | 2016-02-11 11:40:00 +0100 | [diff] [blame] | 262 | case GROUPING_KEYWORD: |
| 263 | ret = yang_check_string(module, &((struct lys_node_grp *) node)->ref, "reference", "grouping", value, line); |
| 264 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 265 | case LEAF_KEYWORD: |
| 266 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->ref, "reference", "leaf", value, line); |
| 267 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame^] | 268 | case LEAF_LIST_KEYWORD: |
| 269 | ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->ref, "reference", "leaflist", value, line); |
| 270 | break; |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 271 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 272 | } |
| 273 | return ret; |
| 274 | } |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 275 | |
| 276 | void * |
| 277 | yang_read_revision(struct lys_module *module, char *value) |
| 278 | { |
| 279 | struct lys_revision *retval; |
| 280 | |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 281 | retval = &module->rev[module->rev_size]; |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 282 | |
| 283 | /* first member of array is last revision */ |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 284 | if (module->rev_size && strcmp(module->rev[0].date, value) < 0) { |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 285 | memcpy(retval->date, module->rev[0].date, LY_REV_SIZE); |
| 286 | memcpy(module->rev[0].date, value, LY_REV_SIZE); |
| 287 | retval->dsc = module->rev[0].dsc; |
| 288 | retval->ref = module->rev[0].ref; |
| 289 | retval = module->rev; |
| 290 | retval->dsc = NULL; |
| 291 | retval->ref = NULL; |
| 292 | } else { |
| 293 | memcpy(retval->date, value, LY_REV_SIZE); |
| 294 | } |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 295 | module->rev_size++; |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 296 | free(value); |
| 297 | return retval; |
| 298 | } |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 299 | |
| 300 | int |
| 301 | yang_add_elem(struct lys_node_array **node, int *size) |
| 302 | { |
| 303 | if (!*size % LY_ARRAY_SIZE) { |
| 304 | if (!(*node = ly_realloc(*node, (*size + LY_ARRAY_SIZE) * sizeof **node))) { |
| 305 | LOGMEM; |
| 306 | return EXIT_FAILURE; |
| 307 | } else { |
| 308 | memset(*node+*size,0,LY_ARRAY_SIZE*sizeof **node); |
| 309 | } |
| 310 | } |
| 311 | (*size)++; |
| 312 | return EXIT_SUCCESS; |
| 313 | } |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 314 | |
| 315 | void * |
| 316 | yang_read_feature(struct lys_module *module, char *value, int line) |
| 317 | { |
| 318 | struct lys_feature *retval; |
| 319 | |
| 320 | /* check uniqueness of feature's names */ |
| 321 | if (lyp_check_identifier(value, LY_IDENT_FEATURE, line, module, NULL)) { |
| 322 | goto error; |
| 323 | } |
| 324 | retval = &module->features[module->features_size]; |
| 325 | retval->name = lydict_insert_zc(module->ctx, value); |
| 326 | retval->module = module; |
| 327 | module->features_size++; |
| 328 | return retval; |
| 329 | |
| 330 | error: |
| 331 | free(value); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | int |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 336 | yang_read_if_feature(struct lys_module *module, void *ptr, char *value, struct unres_schema *unres, int type, int line) |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 337 | { |
| 338 | const char *exp; |
| 339 | int ret; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 340 | struct lys_feature *f; |
| 341 | struct lys_node *n; |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 342 | |
| 343 | if (!(exp = transform_schema2json(module, value, line))) { |
| 344 | free(value); |
| 345 | return EXIT_FAILURE; |
| 346 | } |
| 347 | free(value); |
| 348 | |
| 349 | /* hack - store pointer to the parent node for later status check */ |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 350 | if (type == FEATURE_KEYWORD) { |
| 351 | f = (struct lys_feature *) ptr; |
| 352 | f->features[f->features_size] = f; |
| 353 | ret = unres_schema_add_str(module, unres, &f->features[f->features_size], UNRES_IFFEAT, exp, line); |
| 354 | f->features_size++; |
| 355 | } else { |
| 356 | n = (struct lys_node *) ptr; |
| 357 | n->features[n->features_size] = (struct lys_feature *) n; |
| 358 | ret = unres_schema_add_str(module, unres, &n->features[n->features_size], UNRES_IFFEAT, exp, line); |
| 359 | n->features_size++; |
| 360 | } |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 361 | |
| 362 | lydict_remove(module->ctx, exp); |
| 363 | if (ret == -1) { |
| 364 | |
| 365 | return EXIT_FAILURE; |
| 366 | } |
| 367 | return EXIT_SUCCESS; |
| 368 | } |
| 369 | |
| 370 | static int |
| 371 | yang_check_flags(uint8_t *flags, uint8_t mask, char *what, char *where, int value, int line) |
| 372 | { |
| 373 | if (*flags & mask) { |
| 374 | LOGVAL(LYE_TOOMANY, line, what, where); |
| 375 | return EXIT_FAILURE; |
| 376 | } else { |
Pavol Vican | 945187f | 2016-02-11 10:12:33 +0100 | [diff] [blame] | 377 | *flags |= value; |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 378 | return EXIT_SUCCESS; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | int |
| 383 | yang_read_status(void *node, int value, int type, int line) |
| 384 | { |
| 385 | int retval; |
| 386 | |
| 387 | switch (type) { |
| 388 | case FEATURE_KEYWORD: |
| 389 | retval = yang_check_flags(&((struct lys_feature *) node)->flags, LYS_STATUS_MASK, "status", "feature", value, line); |
| 390 | break; |
Pavol Vican | 7c8ae12 | 2016-02-10 11:01:50 +0100 | [diff] [blame] | 391 | case IDENTITY_KEYWORD: |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 392 | retval = yang_check_flags(&((struct lys_ident *) node)->flags, LYS_STATUS_MASK, "status", "identity", value, line); |
| 393 | break; |
Pavol Vican | 7c8ae12 | 2016-02-10 11:01:50 +0100 | [diff] [blame] | 394 | case CONTAINER_KEYWORD: |
| 395 | retval = yang_check_flags(&((struct lys_node_container *) node)->flags, LYS_STATUS_MASK, "status", "container", value, line); |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 396 | break; |
| 397 | case ANYXML_KEYWORD: |
| 398 | retval = yang_check_flags(&((struct lys_node_anyxml *) node)->flags, LYS_STATUS_MASK, "status", "anyxml", value, line); |
| 399 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 400 | case CHOICE_KEYWORD: |
| 401 | retval = yang_check_flags(&((struct lys_node_anyxml *) node)->flags, LYS_STATUS_MASK, "status", "choice", value, line); |
| 402 | break; |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 403 | case CASE_KEYWORD: |
| 404 | retval = yang_check_flags(&((struct lys_node_case *) node)->flags, LYS_STATUS_MASK, "status", "case", value, line); |
| 405 | break; |
Pavol Vican | 12f53c3 | 2016-02-11 11:40:00 +0100 | [diff] [blame] | 406 | case GROUPING_KEYWORD: |
| 407 | retval = yang_check_flags(&((struct lys_node_grp *) node)->flags, LYS_STATUS_MASK, "status", "grouping", value, line); |
| 408 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 409 | case LEAF_KEYWORD: |
| 410 | retval = yang_check_flags(&((struct lys_node_leaf *) node)->flags, LYS_STATUS_MASK, "status", "leaf", value, line); |
| 411 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame^] | 412 | case LEAF_LIST_KEYWORD: |
| 413 | retval = yang_check_flags(&((struct lys_node_leaflist *) node)->flags, LYS_STATUS_MASK, "status", "leaflist", value, line); |
| 414 | break; |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 415 | } |
| 416 | return retval; |
| 417 | } |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 418 | |
| 419 | void * |
| 420 | yang_read_identity(struct lys_module *module, char *value) |
| 421 | { |
| 422 | struct lys_ident *ret; |
| 423 | |
| 424 | ret = &module->ident[module->ident_size]; |
| 425 | ret->name = lydict_insert_zc(module->ctx, value); |
| 426 | ret->module = module; |
| 427 | module->ident_size++; |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | int |
| 432 | yang_read_base(struct lys_module *module, struct lys_ident *ident, char *value, struct unres_schema *unres, int line) |
| 433 | { |
| 434 | const char *exp; |
| 435 | |
| 436 | if (ident->base) { |
| 437 | LOGVAL(LYE_TOOMANY, line, "base", "identity"); |
| 438 | return EXIT_FAILURE; |
| 439 | } |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 440 | exp = transform_schema2json(module, value, line); |
| 441 | free(value); |
| 442 | if (!exp) { |
| 443 | return EXIT_FAILURE; |
| 444 | } |
| 445 | if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, exp, line) == -1) { |
| 446 | lydict_remove(module->ctx, exp); |
| 447 | return EXIT_FAILURE; |
| 448 | } |
Pavol Vican | 44dde2c | 2016-02-10 11:18:14 +0100 | [diff] [blame] | 449 | |
| 450 | /* hack - store some address due to detection of unresolved base*/ |
| 451 | if (!ident->base) { |
| 452 | ident->base = (void *)1; |
| 453 | } |
| 454 | |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 455 | lydict_remove(module->ctx, exp); |
| 456 | return EXIT_SUCCESS; |
| 457 | } |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 458 | |
| 459 | void * |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 460 | yang_read_must(struct lys_module *module, struct lys_node *node, char *value, int type, int line) |
| 461 | { |
| 462 | struct lys_restr *retval; |
| 463 | |
| 464 | switch (type) { |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 465 | case CONTAINER_KEYWORD: |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 466 | retval = &((struct lys_node_container *)node)->must[((struct lys_node_container *)node)->must_size++]; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 467 | break; |
| 468 | case ANYXML_KEYWORD: |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 469 | retval = &((struct lys_node_anyxml *)node)->must[((struct lys_node_anyxml *)node)->must_size++]; |
| 470 | break; |
| 471 | case LEAF_KEYWORD: |
| 472 | retval = &((struct lys_node_leaf *)node)->must[((struct lys_node_leaf *)node)->must_size++]; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 473 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame^] | 474 | case LEAF_LIST_KEYWORD: |
| 475 | retval = &((struct lys_node_leaflist *)node)->must[((struct lys_node_leaflist *)node)->must_size++]; |
| 476 | break; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 477 | } |
| 478 | retval->expr = transform_schema2json(module, value, line); |
| 479 | if (!retval->expr || lyxp_syntax_check(retval->expr, line)) { |
| 480 | goto error; |
| 481 | } |
| 482 | free(value); |
| 483 | return retval; |
| 484 | |
| 485 | error: |
| 486 | free(value); |
| 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | int |
| 491 | yang_read_message(struct lys_module *module,struct lys_restr *save,char *value, int type, int message, int line) |
| 492 | { |
| 493 | int ret; |
| 494 | char *exp; |
| 495 | |
| 496 | switch (type) { |
| 497 | case MUST_KEYWORD: |
| 498 | exp = "must"; |
| 499 | break; |
| 500 | } |
| 501 | if (message==ERROR_APP_TAG_KEYWORD) { |
| 502 | ret = yang_check_string(module, &save->eapptag, "error_app_tag", exp, value, line); |
| 503 | } else { |
| 504 | ret = yang_check_string(module, &save->emsg, "error_app_tag", exp, value, line); |
| 505 | } |
| 506 | return ret; |
| 507 | } |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 508 | |
| 509 | int |
| 510 | yang_read_presence(struct lys_module *module, struct lys_node_container *cont, char *value, int line) |
| 511 | { |
| 512 | if (cont->presence) { |
| 513 | LOGVAL(LYE_TOOMANY, line, "presence", "container"); |
| 514 | free(value); |
| 515 | return EXIT_FAILURE; |
| 516 | } else { |
| 517 | cont->presence = lydict_insert_zc(module->ctx, value); |
| 518 | return EXIT_SUCCESS; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | int |
| 523 | yang_read_config(void *node, int value, int type, int line) |
| 524 | { |
| 525 | int ret; |
| 526 | |
| 527 | switch (type) { |
| 528 | case CONTAINER_KEYWORD: |
| 529 | ret = yang_check_flags(&((struct lys_node_container *)node)->flags, LYS_CONFIG_MASK, "config", "container", value, line); |
| 530 | break; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 531 | case ANYXML_KEYWORD: |
| 532 | ret = yang_check_flags(&((struct lys_node_anyxml *)node)->flags, LYS_CONFIG_MASK, "config", "anyxml", value, line); |
| 533 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 534 | case CHOICE_KEYWORD: |
| 535 | ret = yang_check_flags(&((struct lys_node_choice *)node)->flags, LYS_CONFIG_MASK, "config", "choice", value, line); |
| 536 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 537 | case LEAF_KEYWORD: |
| 538 | ret = yang_check_flags(&((struct lys_node_leaf *)node)->flags, LYS_CONFIG_MASK, "config", "leaf", value, line); |
| 539 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame^] | 540 | case LEAF_LIST_KEYWORD: |
| 541 | ret = yang_check_flags(&((struct lys_node_leaf *)node)->flags, LYS_CONFIG_MASK, "config", "leaflist", value, line); |
| 542 | break; |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 543 | } |
| 544 | return ret; |
| 545 | } |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 546 | |
| 547 | void * |
| 548 | yang_read_when(struct lys_module *module, struct lys_node *node, int type, char *value, int line) |
| 549 | { |
| 550 | struct lys_when *retval; |
| 551 | |
| 552 | retval = calloc(1, sizeof *retval); |
| 553 | if (!retval) { |
| 554 | LOGMEM; |
| 555 | free(value); |
| 556 | return NULL; |
| 557 | } |
| 558 | retval->cond = transform_schema2json(module, value, line); |
| 559 | if (!retval->cond || lyxp_syntax_check(retval->cond, line)) { |
| 560 | goto error; |
| 561 | } |
| 562 | switch (type) { |
| 563 | case CONTAINER_KEYWORD: |
| 564 | if (((struct lys_node_container *)node)->when) { |
| 565 | LOGVAL(LYE_TOOMANY,line,"when","container"); |
| 566 | goto error; |
| 567 | } |
| 568 | ((struct lys_node_container *)node)->when = retval; |
| 569 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 570 | case ANYXML_KEYWORD: |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 571 | if (((struct lys_node_anyxml *)node)->when) { |
| 572 | LOGVAL(LYE_TOOMANY,line,"when","anyxml"); |
| 573 | goto error; |
| 574 | } |
| 575 | ((struct lys_node_anyxml *)node)->when = retval; |
| 576 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 577 | case CHOICE_KEYWORD: |
| 578 | if (((struct lys_node_choice *)node)->when) { |
| 579 | LOGVAL(LYE_TOOMANY,line,"when","choice"); |
| 580 | goto error; |
| 581 | } |
| 582 | ((struct lys_node_choice *)node)->when = retval; |
| 583 | break; |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 584 | case CASE_KEYWORD: |
| 585 | if (((struct lys_node_case *)node)->when) { |
| 586 | LOGVAL(LYE_TOOMANY,line,"when","case"); |
| 587 | goto error; |
| 588 | } |
| 589 | ((struct lys_node_case *)node)->when = retval; |
| 590 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 591 | case LEAF_KEYWORD: |
| 592 | if (((struct lys_node_leaf *)node)->when) { |
| 593 | LOGVAL(LYE_TOOMANY,line,"when","leaf"); |
| 594 | goto error; |
| 595 | } |
| 596 | ((struct lys_node_leaf *)node)->when = retval; |
| 597 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame^] | 598 | case LEAF_LIST_KEYWORD: |
| 599 | if (((struct lys_node_leaflist *)node)->when) { |
| 600 | LOGVAL(LYE_TOOMANY,line,"when","leaflist"); |
| 601 | goto error; |
| 602 | } |
| 603 | ((struct lys_node_leaflist *)node)->when = retval; |
| 604 | break; |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 605 | } |
| 606 | free(value); |
| 607 | return retval; |
| 608 | |
| 609 | error: |
| 610 | free(value); |
| 611 | lys_when_free(module->ctx, retval); |
| 612 | return NULL; |
| 613 | } |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 614 | |
| 615 | void * |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 616 | yang_read_node(struct lys_module *module, struct lys_node *parent, char *value, int nodetype, int sizeof_struct) |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 617 | { |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 618 | struct lys_node *node; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 619 | |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 620 | node = calloc(1, sizeof_struct); |
| 621 | if (!node) { |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 622 | LOGMEM; |
| 623 | return NULL; |
| 624 | } |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 625 | node->module = module; |
| 626 | node->name = lydict_insert_zc(module->ctx, value); |
| 627 | node->nodetype = nodetype; |
| 628 | node->prev = node; |
| 629 | |
| 630 | /* insert the node into the schema tree */ |
| 631 | if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, node)) { |
| 632 | lydict_remove(module->ctx, node->name); |
| 633 | free(node); |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 634 | return NULL; |
| 635 | } |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 636 | return node; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | int |
| 640 | yang_read_mandatory(void *node, int value, int type, int line) |
| 641 | { |
| 642 | int ret; |
| 643 | |
| 644 | switch (type) { |
| 645 | case ANYXML_KEYWORD: |
| 646 | ret = yang_check_flags(&((struct lys_node_anyxml *)node)->flags, LYS_MAND_MASK, "mandatory", "anyxml", value, line); |
| 647 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 648 | case CHOICE_KEYWORD: |
| 649 | ret = yang_check_flags(&((struct lys_node_choice *)node)->flags, LYS_MAND_MASK, "mandatory", "choice", value, line); |
| 650 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 651 | case LEAF_KEYWORD: |
| 652 | ret = yang_check_flags(&((struct lys_node_leaf *)node)->flags, LYS_MAND_MASK, "mandatory", "leaf", value, line); |
| 653 | break; |
| 654 | } |
| 655 | return ret; |
| 656 | } |
| 657 | |
| 658 | int |
| 659 | yang_read_default(struct lys_module *module, void *node, char *value, int type, int line) |
| 660 | { |
| 661 | int ret; |
| 662 | |
| 663 | switch (type) { |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame^] | 664 | case LEAF_KEYWORD: |
| 665 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dflt, "default", "leaf", value, line); |
| 666 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 667 | } |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | int |
| 672 | yang_read_units(struct lys_module *module, void *node, char *value, int type, int line) |
| 673 | { |
| 674 | int ret; |
| 675 | |
| 676 | switch (type) { |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame^] | 677 | case LEAF_KEYWORD: |
| 678 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->units, "units", "leaf", value, line); |
| 679 | break; |
| 680 | case LEAF_LIST_KEYWORD: |
| 681 | ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->units, "units", "leaflist", value, line); |
| 682 | break; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 683 | } |
| 684 | return ret; |
| 685 | } |