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 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 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 | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <unistd.h> |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 17 | #include <string.h> |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 18 | |
| 19 | #include "common.h" |
| 20 | #include "context.h" |
| 21 | #include "libyang.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 22 | #include "xml.h" |
| 23 | #include "tree_schema_internal.h" |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 24 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 25 | enum YIN_ARGUMENT { |
| 26 | YIN_ARG_NONE = 0, |
| 27 | YIN_ARG_NAME, |
| 28 | YIN_ARG_TARGET_NODE, |
| 29 | YIN_ARG_MODULE, |
| 30 | YIN_ARG_VALUE, |
| 31 | YIN_ARG_TEXT, |
| 32 | YIN_ARG_CONDITION, |
| 33 | YIN_ARG_URI, |
| 34 | YIN_ARG_DATE, |
| 35 | YIN_ARG_TAG, |
| 36 | }; |
David Sedlák | f824ad5 | 2018-10-14 23:58:15 +0200 | [diff] [blame] | 37 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 38 | #define MOVE_INPUT(DATA, COUNT) already_read+=COUNT; |
| 39 | #define IF_KW(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {MOVE_INPUT(name, LEN);arg=STMT;} |
| 40 | #define IF_KW_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {MOVE_INPUT(name, LEN); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 41 | #define IF_KW_PREFIX_END } |
| 42 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 43 | enum YIN_ARGUMENT |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 44 | match_argument_name(const char *name, size_t len) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 45 | { |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 46 | enum YIN_ARGUMENT arg = YIN_ARG_NONE; |
| 47 | size_t already_read = 0; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 48 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 49 | switch(*name) { |
| 50 | case 'c': |
| 51 | MOVE_INPUT(name, 1); |
| 52 | IF_KW("ondition", 8, YIN_ARG_CONDITION); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 53 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 54 | |
| 55 | case 'd': |
| 56 | MOVE_INPUT(name, 1); |
| 57 | IF_KW("ate", 3, YIN_ARG_DATE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 58 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 59 | |
| 60 | case 'm': |
| 61 | MOVE_INPUT(name, 1); |
| 62 | IF_KW("odule", 5, YIN_ARG_MODULE); |
| 63 | break; |
| 64 | |
| 65 | case 'n': |
| 66 | MOVE_INPUT(name, 1); |
| 67 | IF_KW("ame", 3, YIN_ARG_NAME); |
| 68 | break; |
| 69 | |
| 70 | case 't': |
| 71 | MOVE_INPUT(name, 1); |
| 72 | IF_KW_PREFIX("a", 1) |
| 73 | IF_KW("g", 1, YIN_ARG_TAG) |
| 74 | else IF_KW("rget-node", 9, YIN_ARG_TARGET_NODE) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 75 | IF_KW_PREFIX_END |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 76 | else IF_KW("ext", 3, YIN_ARG_TEXT) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 77 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 78 | |
| 79 | case 'u': |
| 80 | MOVE_INPUT(name, 1); |
| 81 | IF_KW("ri", 2, YIN_ARG_URI) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 82 | break; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 83 | |
| 84 | case 'v': |
| 85 | MOVE_INPUT(name, 1); |
| 86 | IF_KW("alue", 4, YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 90 | /* whole keyword must be matched */ |
| 91 | if (already_read != len) { |
| 92 | arg = YIN_ARG_NONE; |
| 93 | } |
| 94 | |
| 95 | return arg; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | LY_ERR |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame^] | 99 | parse_namespace(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p) |
| 100 | { |
| 101 | LY_ERR ret = LY_SUCCESS; |
| 102 | const char *prefix, *name; |
| 103 | size_t prefix_len, name_len; |
| 104 | |
| 105 | char *buf = NULL, *out = NULL; |
| 106 | size_t buf_len = 0, out_len = 0; |
| 107 | int dynamic; |
| 108 | |
| 109 | /* TODO read all in cycle */ |
| 110 | /* check if namespace has argument uri */ |
| 111 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 112 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 113 | if (match_argument_name(name, name_len) != YIN_ARG_URI) { |
| 114 | LOGVAL(xml_ctx->ctx, xml_ctx->line, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"uri\".", name); |
| 115 | } |
| 116 | |
| 117 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
| 118 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 119 | (*mod_p)->ns = lydict_insert(xml_ctx->ctx, out, out_len); |
| 120 | } else { |
| 121 | //error missing namespace |
| 122 | } |
| 123 | |
| 124 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 125 | |
| 126 | return LY_SUCCESS; |
| 127 | } |
| 128 | |
| 129 | LY_ERR |
| 130 | parse_prefix(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p) |
| 131 | { |
| 132 | LY_ERR ret = LY_SUCCESS; |
| 133 | const char *prefix, *name; |
| 134 | size_t prefix_len, name_len; |
| 135 | |
| 136 | char *buf = NULL, *out = NULL; |
| 137 | size_t buf_len = 0, out_len = 0; |
| 138 | int dynamic; |
| 139 | |
| 140 | /* TODO read all in cycle */ |
| 141 | /* check if prfix has argument value */ |
| 142 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 143 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 144 | if (match_argument_name(name, name_len) != YIN_ARG_VALUE) { |
| 145 | LOGVAL(xml_ctx->ctx, xml_ctx->line, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"value\".", name); |
| 146 | } |
| 147 | |
| 148 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
| 149 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 150 | (*mod_p)->prefix = lydict_insert(xml_ctx->ctx, out, out_len); |
| 151 | } else { |
| 152 | //error missing prefix |
| 153 | } |
| 154 | |
| 155 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 156 | |
| 157 | return LY_SUCCESS; |
| 158 | } |
| 159 | |
| 160 | LY_ERR |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 161 | parse_submodule(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p) |
| 162 | { |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 163 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame^] | 164 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 165 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 166 | size_t prefix_len, name_len; |
| 167 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 168 | char *buf = NULL, *out = NULL; |
| 169 | size_t buf_len = 0, out_len = 0; |
| 170 | int dynamic; |
| 171 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 172 | /* check if module/submodule has argument "name" */ |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 173 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 174 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 175 | if (match_argument_name(name, name_len) != YIN_ARG_NAME) { |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 176 | LOGVAL(xml_ctx->ctx, xml_ctx->line, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"name\".", name); |
| 177 | } |
| 178 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 179 | /* read module name */ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame^] | 180 | if (xml_ctx->status != LYXML_ATTR_CONTENT) { |
| 181 | LOGVAL(xml_ctx->ctx, xml_ctx->line, &xml_ctx->line, LYVE_SYNTAX, "Missing value of argument \"name\""); |
| 182 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 183 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 184 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 185 | (*mod_p)->name = lydict_insert(xml_ctx->ctx, out, out_len); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 186 | LY_CHECK_ERR_RET(!(*mod_p)->name, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 187 | |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame^] | 188 | /* read all attributes and their content temporary solution */ |
| 189 | while (xml_ctx->status == LYXML_ATTRIBUTE) { |
| 190 | lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 191 | if (xml_ctx->status == LYXML_ATTR_CONTENT) { |
| 192 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | while (xml_ctx->status == LYXML_ELEMENT || xml_ctx->status == LYXML_ELEM_CONTENT) { |
| 197 | ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 198 | printf("%s\n\n\n", name); |
| 199 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM); |
| 200 | kw = match_keyword(name); |
| 201 | |
| 202 | switch (kw) { |
| 203 | case YANG_PREFIX: |
| 204 | ret = parse_prefix(xml_ctx, data, mod_p); |
| 205 | break; |
| 206 | |
| 207 | case YANG_NAMESPACE: |
| 208 | ret = parse_namespace(xml_ctx, data, mod_p); |
| 209 | break; |
| 210 | |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 216 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | LY_ERR |
| 220 | yin_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p) |
| 221 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 222 | LY_ERR ret = LY_SUCCESS; |
| 223 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 224 | struct lyxml_context xml_ctx; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 225 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 226 | memset(&xml_ctx, 0, sizeof xml_ctx); |
| 227 | xml_ctx.ctx = ctx; |
| 228 | xml_ctx.line = 1; |
| 229 | |
| 230 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 231 | size_t prefix_len, name_len; |
| 232 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 233 | /* check if root element is module or submodule */ |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 234 | ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 235 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx.ctx), LY_EMEM); |
| 236 | kw = match_keyword(name); |
| 237 | if (kw != YANG_MODULE && kw != YANG_SUBMODULE) { |
| 238 | LOGVAL(xml_ctx.ctx, xml_ctx.line, &xml_ctx.line, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", name); |
| 239 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 240 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 241 | if (kw == YANG_SUBMODULE) { |
| 242 | (*mod_p)->submodule = 1; |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 243 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | ret = parse_submodule(&xml_ctx, &data, mod_p); |
| 247 | |
David Sedlák | f3b24f6 | 2018-11-02 10:40:47 +0100 | [diff] [blame] | 248 | lyxml_context_clear(&xml_ctx); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 249 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 250 | } |