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 |
| 99 | parse_submodule(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p) |
| 100 | { |
| 101 | LY_ERR ret = 0; |
| 102 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame^] | 103 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 104 | size_t prefix_len, name_len; |
| 105 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 106 | /* check if module/submodule has argument "name" */ |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 107 | ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 108 | 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] | 109 | if (match_argument_name(name, name_len) != YIN_ARG_NAME) { |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 110 | LOGVAL(xml_ctx->ctx, xml_ctx->line, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"name\".", name); |
| 111 | } |
| 112 | |
| 113 | char *buf = NULL, *out = NULL; |
| 114 | size_t buf_len = 0, out_len = 0; |
| 115 | int dynamic; |
| 116 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame^] | 117 | /* read name of module */ |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 118 | ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic); |
| 119 | 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] | 120 | (*mod_p)->name = lydict_insert(xml_ctx->ctx, out, out_len); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame^] | 121 | LY_CHECK_ERR_RET(!(*mod_p)->name, LOGMEM(xml_ctx->ctx), LY_EMEM); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | LY_ERR |
| 126 | yin_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p) |
| 127 | { |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame^] | 128 | LY_ERR ret = LY_SUCCESS; |
| 129 | enum yang_keyword kw = YANG_NONE; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 130 | struct lyxml_context xml_ctx; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 131 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame^] | 132 | memset(&xml_ctx, 0, sizeof xml_ctx); |
| 133 | xml_ctx.ctx = ctx; |
| 134 | xml_ctx.line = 1; |
| 135 | |
| 136 | const char *prefix, *name; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 137 | size_t prefix_len, name_len; |
| 138 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame^] | 139 | /* check if root keyword is module or submodule */ |
| 140 | ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 141 | LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx.ctx), LY_EMEM); |
| 142 | kw = match_keyword(name); |
| 143 | if (kw != YANG_MODULE && kw != YANG_SUBMODULE) { |
| 144 | LOGVAL(xml_ctx.ctx, xml_ctx.line, &xml_ctx.line, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", name); |
| 145 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 146 | |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame^] | 147 | if (kw == YANG_SUBMODULE) { |
| 148 | (*mod_p)->submodule = 1; |
| 149 | } |
| 150 | |
| 151 | ret = parse_submodule(&xml_ctx, &data, mod_p); |
| 152 | |
| 153 | return ret; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 154 | } |