blob: 118ecf161ac30d435457446855b3879d1a61364b [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02004 * @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ákf824ad52018-10-14 23:58:15 +020013 */
David Sedlák3b4db242018-10-19 16:11:01 +020014#include <stdio.h>
15#include <stdlib.h>
16#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020017#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020018
19#include "common.h"
20#include "context.h"
21#include "libyang.h"
David Sedlák3b4db242018-10-19 16:11:01 +020022#include "xml.h"
23#include "tree_schema_internal.h"
David Sedlákf824ad52018-10-14 23:58:15 +020024
David Sedlák872c7b42018-10-26 13:15:20 +020025enum 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ákf824ad52018-10-14 23:58:15 +020037
David Sedlák872c7b42018-10-26 13:15:20 +020038#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ák3b4db242018-10-19 16:11:01 +020041#define IF_KW_PREFIX_END }
42
David Sedlák872c7b42018-10-26 13:15:20 +020043enum YIN_ARGUMENT
David Sedláke4889912018-11-02 09:52:40 +010044match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020045{
David Sedlák872c7b42018-10-26 13:15:20 +020046 enum YIN_ARGUMENT arg = YIN_ARG_NONE;
47 size_t already_read = 0;
David Sedlák3b4db242018-10-19 16:11:01 +020048
David Sedlák872c7b42018-10-26 13:15:20 +020049 switch(*name) {
50 case 'c':
51 MOVE_INPUT(name, 1);
52 IF_KW("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +020053 break;
David Sedlák872c7b42018-10-26 13:15:20 +020054
55 case 'd':
56 MOVE_INPUT(name, 1);
57 IF_KW("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +020058 break;
David Sedlák872c7b42018-10-26 13:15:20 +020059
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ák3b4db242018-10-19 16:11:01 +020075 IF_KW_PREFIX_END
David Sedlák872c7b42018-10-26 13:15:20 +020076 else IF_KW("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +020077 break;
David Sedlák872c7b42018-10-26 13:15:20 +020078
79 case 'u':
80 MOVE_INPUT(name, 1);
81 IF_KW("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +020082 break;
David Sedlák872c7b42018-10-26 13:15:20 +020083
84 case 'v':
85 MOVE_INPUT(name, 1);
86 IF_KW("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +020087 break;
88 }
89
David Sedlák872c7b42018-10-26 13:15:20 +020090 /* whole keyword must be matched */
91 if (already_read != len) {
92 arg = YIN_ARG_NONE;
93 }
94
95 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +020096}
97
98LY_ERR
David Sedlák4a4c0722018-11-26 17:03:10 +010099parse_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
129LY_ERR
130parse_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
160LY_ERR
David Sedlák3b4db242018-10-19 16:11:01 +0200161parse_submodule(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p)
162{
David Sedlákf3b24f62018-11-02 10:40:47 +0100163 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100164 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100165 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200166 size_t prefix_len, name_len;
167
David Sedlákf3b24f62018-11-02 10:40:47 +0100168 char *buf = NULL, *out = NULL;
169 size_t buf_len = 0, out_len = 0;
170 int dynamic;
171
David Sedlák872c7b42018-10-26 13:15:20 +0200172 /* check if module/submodule has argument "name" */
David Sedlák3b4db242018-10-19 16:11:01 +0200173 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ák872c7b42018-10-26 13:15:20 +0200175 if (match_argument_name(name, name_len) != YIN_ARG_NAME) {
David Sedlák3b4db242018-10-19 16:11:01 +0200176 LOGVAL(xml_ctx->ctx, xml_ctx->line, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"name\".", name);
177 }
178
David Sedlákf3b24f62018-11-02 10:40:47 +0100179 /* read module name */
David Sedlák4a4c0722018-11-26 17:03:10 +0100180 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ák3b4db242018-10-19 16:11:01 +0200183 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ák3b4db242018-10-19 16:11:01 +0200185 (*mod_p)->name = lydict_insert(xml_ctx->ctx, out, out_len);
David Sedláke4889912018-11-02 09:52:40 +0100186 LY_CHECK_ERR_RET(!(*mod_p)->name, LOGMEM(xml_ctx->ctx), LY_EMEM);
David Sedlákf3b24f62018-11-02 10:40:47 +0100187
David Sedlák4a4c0722018-11-26 17:03:10 +0100188 /* 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ákf3b24f62018-11-02 10:40:47 +0100216 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200217}
218
219LY_ERR
220yin_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
221{
David Sedláke4889912018-11-02 09:52:40 +0100222 LY_ERR ret = LY_SUCCESS;
223 enum yang_keyword kw = YANG_NONE;
David Sedlák3b4db242018-10-19 16:11:01 +0200224 struct lyxml_context xml_ctx;
David Sedlák3b4db242018-10-19 16:11:01 +0200225
David Sedláke4889912018-11-02 09:52:40 +0100226 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ák3b4db242018-10-19 16:11:01 +0200231 size_t prefix_len, name_len;
232
David Sedlákf3b24f62018-11-02 10:40:47 +0100233 /* check if root element is module or submodule */
David Sedláke4889912018-11-02 09:52:40 +0100234 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ák3b4db242018-10-19 16:11:01 +0200240
David Sedláke4889912018-11-02 09:52:40 +0100241 if (kw == YANG_SUBMODULE) {
242 (*mod_p)->submodule = 1;
David Sedlákf3b24f62018-11-02 10:40:47 +0100243
David Sedláke4889912018-11-02 09:52:40 +0100244 }
245
246 ret = parse_submodule(&xml_ctx, &data, mod_p);
247
David Sedlákf3b24f62018-11-02 10:40:47 +0100248 lyxml_context_clear(&xml_ctx);
David Sedláke4889912018-11-02 09:52:40 +0100249 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200250}