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