David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_yin.c |
| 3 | * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz> |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 4 | * @brief YIN parser. |
| 5 | * |
David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 13 | */ |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 14 | #include "common.h" |
| 15 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 19 | #include <string.h> |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 20 | |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 21 | #include "context.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 22 | #include "dict.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 23 | #include "xml.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 24 | #include "tree.h" |
| 25 | #include "tree_schema.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 26 | #include "tree_schema_internal.h" |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 27 | #include "parser_yin.h" |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 28 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 29 | /** |
| 30 | * @brief check if given string is URI of yin namespace. |
| 31 | * @param ns Namespace URI to check. |
| 32 | * |
| 33 | * @return true if ns equals YIN_NS_URI false otherwise. |
| 34 | */ |
| 35 | #define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0) |
| 36 | |
David Sedlák | f625118 | 2019-06-06 10:22:13 +0200 | [diff] [blame] | 37 | const char *const yin_attr_list[] = { |
| 38 | [YIN_ARG_NAME] = "name", |
| 39 | [YIN_ARG_TARGET_NODE] = "target-node", |
| 40 | [YIN_ARG_MODULE] = "module", |
| 41 | [YIN_ARG_VALUE] = "value", |
| 42 | [YIN_ARG_TEXT] = "text", |
| 43 | [YIN_ARG_CONDITION] = "condition", |
| 44 | [YIN_ARG_URI] = "uri", |
| 45 | [YIN_ARG_DATE] = "date", |
| 46 | [YIN_ARG_TAG] = "tag", |
| 47 | [YIN_ARG_XMLNS] = "xmlns", |
| 48 | }; |
| 49 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 50 | enum yang_keyword |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 51 | yin_match_keyword(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len) |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 52 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 53 | const char *start = NULL; |
| 54 | enum yang_keyword kw = YANG_NONE; |
| 55 | const struct lyxml_ns *ns = NULL; |
| 56 | |
| 57 | if (!name || name_len == 0) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 58 | return YANG_NONE; |
| 59 | } |
| 60 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 61 | ns = lyxml_ns_get(xml_ctx, prefix, prefix_len); |
| 62 | if (ns) { |
| 63 | if (!IS_YIN_NS(ns->uri)) { |
| 64 | return YANG_CUSTOM; |
| 65 | } |
| 66 | } else { |
| 67 | /* elements without namespace are automatically unknown */ |
| 68 | return YANG_NONE; |
| 69 | } |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 70 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 71 | |
| 72 | start = name; |
| 73 | kw = lysp_match_kw(NULL, &name); |
| 74 | |
| 75 | if (name - start == (long int)name_len) { |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 76 | return kw; |
| 77 | } else { |
| 78 | return YANG_NONE; |
| 79 | } |
| 80 | } |
| 81 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 82 | enum YIN_ARGUMENT |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 83 | yin_match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 84 | { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 85 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 86 | size_t already_read = 0; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 87 | LY_CHECK_RET(len == 0, YIN_ARG_NONE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 88 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 89 | #define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;} |
| 90 | #define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN; |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 91 | #define IF_ARG_PREFIX_END } |
| 92 | |
David Sedlák | 1c8b270 | 2019-02-22 11:03:02 +0100 | [diff] [blame] | 93 | switch (*name) { |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 94 | case 'x': |
| 95 | already_read += 1; |
| 96 | IF_ARG("mlns", 4, YIN_ARG_XMLNS); |
| 97 | break; |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 98 | case 'c': |
| 99 | already_read += 1; |
| 100 | IF_ARG("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 101 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 102 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 103 | case 'd': |
| 104 | already_read += 1; |
| 105 | IF_ARG("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 106 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 107 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 108 | case 'm': |
| 109 | already_read += 1; |
| 110 | IF_ARG("odule", 5, YIN_ARG_MODULE); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 111 | break; |
| 112 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 113 | case 'n': |
| 114 | already_read += 1; |
| 115 | IF_ARG("ame", 3, YIN_ARG_NAME); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 116 | break; |
| 117 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 118 | case 't': |
| 119 | already_read += 1; |
| 120 | IF_ARG_PREFIX("a", 1) |
| 121 | IF_ARG("g", 1, YIN_ARG_TAG) |
| 122 | else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE) |
| 123 | IF_ARG_PREFIX_END |
| 124 | else IF_ARG("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 125 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 126 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 127 | case 'u': |
| 128 | already_read += 1; |
| 129 | IF_ARG("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 130 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 131 | |
David Sedlák | 94de2aa | 2019-02-15 12:42:11 +0100 | [diff] [blame] | 132 | case 'v': |
| 133 | already_read += 1; |
| 134 | IF_ARG("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | |
David Sedlák | c10e790 | 2018-12-17 02:17:59 +0100 | [diff] [blame] | 138 | /* whole argument must be matched */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 139 | if (already_read != len) { |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 140 | arg = YIN_ARG_UNKNOWN; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 141 | } |
| 142 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 143 | #undef IF_ARG |
| 144 | #undef IF_ARG_PREFIX |
| 145 | #undef IF_ARG_PREFIX_END |
| 146 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 147 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 148 | } |
| 149 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 150 | static void free_arg_rec(struct lyxml_context *xml_ctx, struct yin_arg_record *record) { |
| 151 | (void)xml_ctx; /* unused */ |
| 152 | if (record->dynamic_content) { |
| 153 | free(record->content); |
| 154 | } |
| 155 | } |
| 156 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 157 | LY_ERR |
| 158 | yin_load_attributes(struct lyxml_context *xml_ctx, const char **data, struct yin_arg_record **args) |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 159 | { |
| 160 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 161 | struct yin_arg_record *argument_record = NULL; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 162 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 163 | /* load all attributes first */ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 164 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 165 | LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *args, argument_record, ret, cleanup); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 166 | ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len, |
| 167 | &argument_record->name, &argument_record->name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 168 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 169 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 170 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 171 | argument_record->content = NULL; |
| 172 | argument_record->content_len = 0; |
| 173 | argument_record->dynamic_content = 0; |
| 174 | ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len, |
| 175 | &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 176 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 180 | cleanup: |
| 181 | if (ret != LY_SUCCESS) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 182 | FREE_ARRAY(xml_ctx, *args, free_arg_rec); |
| 183 | *args = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 184 | } |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | LY_ERR |
| 189 | yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type, const char **arg_val) |
| 190 | { |
| 191 | LY_ERR ret = LY_SUCCESS; |
| 192 | enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN; |
| 193 | struct yin_arg_record *iter = NULL; |
| 194 | const struct lyxml_ns *ns = NULL; |
| 195 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 196 | /* validation of attributes */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 197 | LY_ARRAY_FOR(*args, struct yin_arg_record, iter) { |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 198 | ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 199 | /* yin arguments represented as attributes have no namespace, which in this case means no prefix */ |
| 200 | if (!iter->prefix) { |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 201 | arg = yin_match_argument_name(iter->name, iter->name_len); |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 202 | if (arg == YIN_ARG_NONE) { |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 203 | continue; |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 204 | } else if (arg == arg_type) { |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 205 | if (iter->dynamic_content) { |
| 206 | *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 207 | LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup); |
| 208 | /* string is no longer supposed to be freed when the sized array is freed */ |
| 209 | iter->dynamic_content = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 210 | } else { |
| 211 | *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len); |
| 212 | LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup); |
| 213 | } |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 214 | } else { |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 215 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name); |
| 216 | ret = LY_EVALID; |
| 217 | goto cleanup; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 222 | cleanup: |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 223 | return ret; |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 224 | } |
| 225 | |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 226 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 227 | yin_parse_text_element(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, const char **value) |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 228 | { |
| 229 | LY_ERR ret = LY_SUCCESS; |
| 230 | char *buf = NULL, *out = NULL; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 231 | const char *prefix = NULL, *name = NULL; |
| 232 | size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 233 | int dynamic = 0; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 234 | |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 235 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 236 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 237 | LY_CHECK_RET(ret); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 238 | if (dynamic) { |
| 239 | *value = lydict_insert_zc(xml_ctx->ctx, buf); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 240 | LY_CHECK_RET(!(*value), LY_EMEM); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 241 | } else { |
| 242 | *value = lydict_insert(xml_ctx->ctx, out, out_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 243 | LY_CHECK_RET(!(*value), LY_EMEM); |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 244 | } |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 245 | } |
| 246 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 247 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 248 | /* probably should never happen, mixed content should be caught by lyxml_get_element call but |
| 249 | * I will just leave it here for now to be sure */ |
| 250 | LY_CHECK_RET(name, LY_EVALID); |
| 251 | return ret; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 252 | } |
| 253 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 254 | /** |
| 255 | * @brief function to parse meta tags eg. elements with text element as child |
| 256 | * |
| 257 | * @param[in] xml_ctx Xml context. |
| 258 | * @param[in] args Sized array of arguments of current elements. |
| 259 | * @param[in,out] data Data to read from. |
| 260 | * @param[out] value Where the content of meta tag should be stored. |
| 261 | * |
| 262 | * @return LY_ERR values. |
| 263 | */ |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 264 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 265 | yin_parse_meta_element(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, const char **value) |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 266 | { |
| 267 | LY_ERR ret = LY_SUCCESS; |
| 268 | char *buf = NULL, *out = NULL; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 269 | const char *prefix = NULL, *name = NULL; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 270 | size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0; |
| 271 | int dynamic = 0; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 272 | enum YIN_ARGUMENT arg = YANG_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 273 | struct yin_arg_record *subelem_args = NULL; |
| 274 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 275 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 276 | LY_CHECK_ERR_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected content of meta element."), LY_EVALID); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 277 | |
| 278 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 279 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element."), LY_EINVAL); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 280 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 281 | /* first element should be argument element <text> */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 282 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 283 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 284 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 285 | arg = yin_match_argument_name(name, name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 286 | LY_CHECK_ERR_GOTO(arg != YIN_ARG_TEXT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element."); |
| 287 | ret = LY_EINVAL, cleanup); |
| 288 | ret = yin_parse_text_element(xml_ctx, &subelem_args, data, value); |
| 289 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 290 | |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 291 | /* loop over all child elements and parse them */ |
| 292 | while (xml_ctx->status == LYXML_ELEMENT) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 293 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 294 | LY_CHECK_GOTO(ret, cleanup); |
| 295 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 296 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 297 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 298 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 299 | if (!name) { |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 300 | /* end of meta element reached */ |
| 301 | break; |
| 302 | } |
| 303 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 304 | switch (kw) { |
| 305 | case YANG_CUSTOM: |
| 306 | // TODO parse extension instance |
| 307 | break; |
| 308 | |
| 309 | default: |
| 310 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 311 | return LY_EVALID; |
| 312 | } |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 313 | } |
| 314 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 315 | cleanup: |
| 316 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 317 | return ret; |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 318 | } |
| 319 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 320 | /** |
| 321 | * @brief Parse revision date. |
| 322 | * |
| 323 | * @param[in] xml_ctx Xml context. |
| 324 | * @param[in] args Sized array of arguments of current element. |
| 325 | * @param[in,out] data Data to read from. |
| 326 | * @param[in,out] rev Array to store the parsed value in. |
| 327 | * @param[in,out] exts Extension instances to add to. |
| 328 | * |
| 329 | * @return LY_ERR values. |
| 330 | */ |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 331 | static LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 332 | yin_parse_revision_date(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, char *rev, struct lysp_ext_instance **exts) |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 333 | { |
| 334 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 335 | const char *temp_rev, *prefix = NULL, *name = NULL; |
| 336 | char *out; |
| 337 | struct yin_arg_record *subelem_args = NULL; |
| 338 | size_t prefix_len = 0, name_len = 0; |
| 339 | enum yang_keyword kw = YANG_NONE; |
| 340 | int dynamic; |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 341 | |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 342 | if (rev[0]) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 343 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date"); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 344 | return LY_EVALID; |
| 345 | } |
| 346 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 347 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, args, YIN_ARG_DATE, &temp_rev)) |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 348 | LY_CHECK_RET(ret != LY_SUCCESS, ret); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 349 | LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID); |
| 350 | |
| 351 | strcpy(rev, temp_rev); |
| 352 | lydict_remove(xml_ctx->ctx, temp_rev); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 353 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 354 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 355 | ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic); |
| 356 | /* unknown element text content is ignored */ |
| 357 | if (ret == LY_EINVAL) { |
| 358 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 359 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 360 | LY_CHECK_GOTO(ret, cleanup); |
| 361 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 362 | LY_CHECK_GOTO(ret, cleanup); |
| 363 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
| 364 | |
| 365 | if (!name) { |
| 366 | /* end of revisin-date element reached */ |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | switch (kw) { |
| 371 | case YANG_CUSTOM: |
| 372 | // TODO parse extension instance |
| 373 | break; |
| 374 | |
| 375 | default: |
| 376 | LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name); |
| 377 | return LY_EVALID; |
| 378 | } |
| 379 | } |
| 380 | } else { |
| 381 | /* load closing element */ |
| 382 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 383 | LY_CHECK_RET(name, LY_EINVAL); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | cleanup: |
| 388 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 81e0402 | 2019-04-05 15:05:46 +0200 | [diff] [blame] | 389 | return ret; |
| 390 | } |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 391 | |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 392 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 393 | yin_parse_import(struct lyxml_context *xml_ctx, struct yin_arg_record **import_args, const char *module_prefix, const char **data, struct lysp_import **imports) |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 394 | { |
| 395 | LY_ERR ret = LY_SUCCESS; |
| 396 | enum yang_keyword kw; |
| 397 | struct lysp_import *imp; |
| 398 | const char *prefix, *name; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 399 | char *out; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 400 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 401 | size_t prefix_len, name_len; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 402 | int dynamic; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 403 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 404 | /* allocate new element in sized array for import */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 405 | LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *imports, imp, ret, validation_err); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 406 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 407 | /* parse import attributes */ |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 408 | LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name) != LY_SUCCESS, validation_err); |
| 409 | |
| 410 | ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic); |
| 411 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after import element"), LY_EINVAL); |
| 412 | |
| 413 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 414 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 415 | LY_CHECK_GOTO(ret, validation_err); |
| 416 | if (!name) { |
| 417 | /* end of import element reached */ |
| 418 | break; |
| 419 | } |
| 420 | LY_CHECK_GOTO(yin_load_attributes(xml_ctx, data, &subelem_args), validation_err); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 421 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 422 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 423 | switch (kw) { |
| 424 | case YANG_PREFIX: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 425 | LY_CHECK_ERR_GOTO(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), validation_err); |
| 426 | LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, &subelem_args, YIN_ARG_VALUE, &imp->prefix) != LY_SUCCESS, validation_err); |
| 427 | LY_CHECK_GOTO(lysp_check_prefix((struct lys_parser_ctx *)xml_ctx, *imports, module_prefix, &imp->prefix) != LY_SUCCESS, validation_err); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 428 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 429 | case YANG_DESCRIPTION: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 430 | LY_CHECK_ERR_GOTO(imp->dsc, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "description"), validation_err); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 431 | LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->dsc), validation_err); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 432 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 433 | case YANG_REFERENCE: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 434 | LY_CHECK_ERR_GOTO(imp->ref, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "reference"), validation_err); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 435 | LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->ref), validation_err); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 436 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 437 | case YANG_REVISION_DATE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 438 | LY_CHECK_GOTO(yin_parse_revision_date(xml_ctx, &subelem_args, data, imp->rev, &imp->exts), validation_err); |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 439 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 440 | case YANG_CUSTOM: |
| 441 | /* TODO parse extension */ |
David Sedlák | 3a55bc1 | 2019-06-03 08:57:29 +0200 | [diff] [blame] | 442 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 443 | default: |
David Sedlák | 916b190 | 2019-06-06 11:35:05 +0200 | [diff] [blame] | 444 | LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import"); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 445 | goto validation_err; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 446 | } |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 447 | LY_ARRAY_FREE(subelem_args); |
| 448 | subelem_args = NULL; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 449 | } |
| 450 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 451 | LY_CHECK_ERR_GOTO(!imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_MISSATTR, "prefix", "import"), validation_err); |
| 452 | LY_ARRAY_FREE(subelem_args); |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 453 | return ret; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 454 | |
| 455 | validation_err: |
| 456 | LY_ARRAY_FREE(subelem_args); |
| 457 | return LY_EVALID; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 458 | } |
| 459 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 460 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 461 | yin_parse_status(struct lyxml_context *xml_ctx, struct yin_arg_record **status_args, const char **data, uint16_t *flags, struct lysp_ext_instance **exts) |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 462 | { |
| 463 | LY_ERR ret = LY_SUCCESS; |
| 464 | enum yang_keyword kw = YANG_NONE; |
| 465 | const char *value = NULL, *prefix = NULL, *name = NULL; |
| 466 | char *out; |
| 467 | size_t prefix_len = 0, name_len = 0, out_len = 0; |
| 468 | int dynamic = 0; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 469 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 470 | if (*flags & LYS_STATUS_MASK) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 471 | LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 472 | return LY_EVALID; |
| 473 | } |
| 474 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 475 | LY_CHECK_RET(yin_parse_attribute(xml_ctx, status_args, YIN_ARG_VALUE, &value)); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 476 | if (strcmp(value, "current") == 0) { |
| 477 | *flags |= LYS_STATUS_CURR; |
| 478 | } else if (strcmp(value, "deprecated") == 0) { |
| 479 | *flags |= LYS_STATUS_DEPRC; |
| 480 | } else if (strcmp(value, "obsolete") == 0) { |
| 481 | *flags |= LYS_STATUS_OBSLT; |
| 482 | } else { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 483 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status"); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 484 | lydict_remove(xml_ctx->ctx, value); |
| 485 | return LY_EVALID; |
| 486 | } |
| 487 | lydict_remove(xml_ctx->ctx, value); |
| 488 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 489 | /* TODO check if dynamic was set to 1 in case of error */ |
| 490 | if (xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 491 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
| 492 | /* if there are any xml subelements parse them, unknown text content is silently ignored */ |
| 493 | if (ret == LY_EINVAL) { |
| 494 | /* load subelements */ |
| 495 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 496 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 497 | if (!name) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 498 | /* end of status reached */ |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 499 | break; |
| 500 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 501 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 502 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 503 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 504 | switch (kw) { |
| 505 | case YANG_CUSTOM: |
| 506 | /* TODO parse extension instance */ |
| 507 | break; |
| 508 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 509 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status"); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 510 | ret = LY_EVALID; |
| 511 | goto cleanup; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 512 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 513 | |
| 514 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 515 | subelem_args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 516 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 517 | } else { |
| 518 | /* load closing element */ |
| 519 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
| 520 | LY_CHECK_RET(name, LY_EINVAL); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | return ret; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 525 | |
| 526 | cleanup: |
| 527 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
| 528 | return ret; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 529 | } |
| 530 | |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 531 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 532 | yin_parse_extension(struct lyxml_context *xml_ctx, struct yin_arg_record **extension_args, const char **data, struct lysp_ext **extensions) |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 533 | { |
| 534 | LY_ERR ret = LY_SUCCESS; |
| 535 | struct lysp_ext *ex; |
| 536 | const char *prefix = NULL, *name = NULL; |
| 537 | char *out = NULL; |
| 538 | size_t out_len = 0, prefix_len = 0, name_len = 0; |
| 539 | int dynamic = 0; |
| 540 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 541 | struct yin_arg_record *subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 542 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 543 | LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *extensions, ex, ret, cleanup); |
| 544 | ret = yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name); |
| 545 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 546 | ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 547 | LY_CHECK_ERR_GOTO(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element."); |
| 548 | ret = LY_EINVAL, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 549 | |
| 550 | while (xml_ctx->status == LYXML_ELEMENT) { |
| 551 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 552 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 553 | if (!name) { |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 554 | /* end of extension element reached */ |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 555 | break; |
| 556 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 557 | ret = yin_load_attributes(xml_ctx, data, &subelem_args); |
| 558 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 559 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 560 | switch (kw) { |
| 561 | case YANG_ARGUMENT: |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 562 | /* TODO */ |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 563 | break; |
| 564 | case YANG_DESCRIPTION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 565 | ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc); |
| 566 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 567 | break; |
| 568 | case YANG_REFERENCE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 569 | ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref); |
| 570 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 571 | break; |
| 572 | case YANG_STATUS: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 573 | ret = yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts); |
| 574 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 575 | break; |
| 576 | case YANG_CUSTOM: |
| 577 | /* TODO parse extension instance */ |
| 578 | break; |
| 579 | default: |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 580 | LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension"); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 581 | ret = LY_EVALID; |
| 582 | goto cleanup; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 583 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 584 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 585 | subelem_args = NULL; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 586 | } |
| 587 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 588 | cleanup: |
| 589 | FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 590 | return ret; |
| 591 | } |
| 592 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 593 | /** |
| 594 | * @brief Parse module substatements. |
| 595 | * |
| 596 | * @param[in] xml_ctx Xml context. |
| 597 | * @param[in,out] data Data to read from. |
| 598 | * @param[out] mod Parsed module structure. |
| 599 | * |
| 600 | * @return LY_ERR values. |
| 601 | */ |
| 602 | LY_ERR |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 603 | yin_parse_mod(struct lyxml_context *xml_ctx, struct yin_arg_record **mod_args, const char **data, struct lysp_module **mod) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 604 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 605 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 606 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 607 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 608 | size_t prefix_len, name_len; |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 609 | enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 610 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 611 | char *buf = NULL, *out = NULL; |
| 612 | size_t buf_len = 0, out_len = 0; |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 613 | int dynamic = 0; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 614 | struct yin_arg_record *substmt_args = NULL; |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 615 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 616 | yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 617 | LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Missing argument name of a module"), LY_EVALID); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 618 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 619 | LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new xml element after module element."), LY_EINVAL); |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 620 | |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 621 | /* loop over all elements and parse them */ |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 622 | while (xml_ctx->status != LYXML_END) { |
David Sedlák | 4b4713f | 2019-02-15 13:47:45 +0100 | [diff] [blame] | 623 | #define CHECK_ORDER(SECTION) \ |
| 624 | if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION |
| 625 | |
| 626 | switch (kw) { |
| 627 | /* module header */ |
| 628 | case YANG_NAMESPACE: |
| 629 | case YANG_PREFIX: |
| 630 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 631 | break; |
| 632 | case YANG_YANG_VERSION: |
| 633 | CHECK_ORDER(Y_MOD_MODULE_HEADER); |
| 634 | break; |
| 635 | /* linkage */ |
| 636 | case YANG_INCLUDE: |
| 637 | case YANG_IMPORT: |
| 638 | CHECK_ORDER(Y_MOD_LINKAGE); |
| 639 | break; |
| 640 | /* meta */ |
| 641 | case YANG_ORGANIZATION: |
| 642 | case YANG_CONTACT: |
| 643 | case YANG_DESCRIPTION: |
| 644 | case YANG_REFERENCE: |
| 645 | CHECK_ORDER(Y_MOD_META); |
| 646 | break; |
| 647 | |
| 648 | /* revision */ |
| 649 | case YANG_REVISION: |
| 650 | CHECK_ORDER(Y_MOD_REVISION); |
| 651 | break; |
| 652 | /* body */ |
| 653 | case YANG_ANYDATA: |
| 654 | case YANG_ANYXML: |
| 655 | case YANG_AUGMENT: |
| 656 | case YANG_CHOICE: |
| 657 | case YANG_CONTAINER: |
| 658 | case YANG_DEVIATION: |
| 659 | case YANG_EXTENSION: |
| 660 | case YANG_FEATURE: |
| 661 | case YANG_GROUPING: |
| 662 | case YANG_IDENTITY: |
| 663 | case YANG_LEAF: |
| 664 | case YANG_LEAF_LIST: |
| 665 | case YANG_LIST: |
| 666 | case YANG_NOTIFICATION: |
| 667 | case YANG_RPC: |
| 668 | case YANG_TYPEDEF: |
| 669 | case YANG_USES: |
| 670 | case YANG_CUSTOM: |
| 671 | mod_stmt = Y_MOD_BODY; |
| 672 | break; |
| 673 | default: |
| 674 | /* error will be handled in the next switch */ |
| 675 | break; |
| 676 | } |
| 677 | #undef CHECK_ORDER |
| 678 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 679 | LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len)); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 680 | if (name) { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 681 | LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args)); |
| 682 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 683 | switch (kw) { |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 684 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 685 | /* module header */ |
| 686 | case YANG_NAMESPACE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 687 | ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns); |
| 688 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 689 | break; |
| 690 | case YANG_PREFIX: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 691 | ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix); |
| 692 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 693 | break; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 694 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 695 | /* linkage */ |
| 696 | case YANG_IMPORT: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 697 | ret = yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports); |
| 698 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 699 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 700 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 701 | /* meta */ |
| 702 | case YANG_ORGANIZATION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 703 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org); |
| 704 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 705 | break; |
| 706 | case YANG_CONTACT: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 707 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact); |
| 708 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 709 | break; |
| 710 | case YANG_DESCRIPTION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 711 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc); |
| 712 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 713 | break; |
| 714 | case YANG_REFERENCE: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 715 | ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref); |
| 716 | LY_CHECK_GOTO(ret, error); |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 717 | break; |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 718 | /* revision */ |
| 719 | |
| 720 | /*body */ |
| 721 | case YANG_EXTENSION: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 722 | ret = yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions); |
| 723 | LY_CHECK_GOTO(ret, error); |
David Sedlák | 11900c8 | 2019-06-18 16:29:12 +0200 | [diff] [blame] | 724 | break; |
David Sedlák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 725 | |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 726 | default: |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 727 | ret = LY_EVALID; |
| 728 | goto error; |
David Sedlák | c136b97 | 2019-03-08 13:39:06 +0100 | [diff] [blame] | 729 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 730 | FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 731 | substmt_args = NULL; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 735 | return LY_SUCCESS; |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 736 | |
| 737 | error: |
| 738 | FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec); |
| 739 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | LY_ERR |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 743 | yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 744 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 745 | LY_ERR ret = LY_SUCCESS; |
| 746 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 747 | struct lys_parser_ctx parser_ctx; |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 748 | struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 749 | struct lysp_module *mod_p = NULL; |
| 750 | const char *prefix, *name; |
| 751 | size_t prefix_len, name_len; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 752 | struct yin_arg_record *args = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 753 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 754 | /* initialize xml context */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 755 | memset(&parser_ctx, 0, sizeof parser_ctx); |
| 756 | xml_ctx->ctx = ctx; |
| 757 | xml_ctx->line = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 758 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 759 | /* check submodule */ |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 760 | ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 761 | LY_CHECK_GOTO(ret, cleanup); |
| 762 | ret = yin_load_attributes(xml_ctx, &data, &args); |
| 763 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 764 | kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 765 | if (kw == YANG_SUBMODULE) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 766 | LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module."); |
| 767 | ret = LY_EINVAL; |
| 768 | goto cleanup; |
| 769 | } else if (kw != YANG_MODULE) { |
David Sedlák | b307719 | 2019-06-19 10:55:37 +0200 | [diff] [blame] | 770 | LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 771 | ly_stmt2str(kw)); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 772 | ret = LY_EVALID; |
| 773 | goto cleanup; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 774 | } |
| 775 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 776 | /* allocate module */ |
| 777 | mod_p = calloc(1, sizeof *mod_p); |
| 778 | LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup); |
| 779 | mod_p->mod = mod; |
| 780 | mod_p->parsing = 1; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 781 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 782 | /* parse module substatements */ |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 783 | ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p); |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 784 | LY_CHECK_GOTO(ret, cleanup); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 785 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 786 | mod_p->parsing = 0; |
| 787 | mod->parsed = mod_p; |
| 788 | |
| 789 | cleanup: |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 790 | if (ret != LY_SUCCESS) { |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 791 | lysp_module_free(mod_p); |
| 792 | } |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame^] | 793 | FREE_ARRAY(xml_ctx, args, free_arg_rec); |
David Sedlák | aadab9c | 2019-04-05 15:01:27 +0200 | [diff] [blame] | 794 | lyxml_context_clear(xml_ctx); |
David Sedlák | 2e41142 | 2018-12-17 02:35:39 +0100 | [diff] [blame] | 795 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 796 | } |