blob: 0fa9998506a7cbb22954e3c1cb62913948b4732f [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
99parse_submodule(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p)
100{
101 LY_ERR ret = 0;
102
David Sedláke4889912018-11-02 09:52:40 +0100103 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200104 size_t prefix_len, name_len;
105
David Sedlák872c7b42018-10-26 13:15:20 +0200106 /* check if module/submodule has argument "name" */
David Sedlák3b4db242018-10-19 16:11:01 +0200107 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ák872c7b42018-10-26 13:15:20 +0200109 if (match_argument_name(name, name_len) != YIN_ARG_NAME) {
David Sedlák3b4db242018-10-19 16:11:01 +0200110 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áke4889912018-11-02 09:52:40 +0100117 /* read name of module */
David Sedlák3b4db242018-10-19 16:11:01 +0200118 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ák3b4db242018-10-19 16:11:01 +0200120 (*mod_p)->name = lydict_insert(xml_ctx->ctx, out, out_len);
David Sedláke4889912018-11-02 09:52:40 +0100121 LY_CHECK_ERR_RET(!(*mod_p)->name, LOGMEM(xml_ctx->ctx), LY_EMEM);
David Sedlák3b4db242018-10-19 16:11:01 +0200122 return 0;
123}
124
125LY_ERR
126yin_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
127{
David Sedláke4889912018-11-02 09:52:40 +0100128 LY_ERR ret = LY_SUCCESS;
129 enum yang_keyword kw = YANG_NONE;
David Sedlák3b4db242018-10-19 16:11:01 +0200130 struct lyxml_context xml_ctx;
David Sedlák3b4db242018-10-19 16:11:01 +0200131
David Sedláke4889912018-11-02 09:52:40 +0100132 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ák3b4db242018-10-19 16:11:01 +0200137 size_t prefix_len, name_len;
138
David Sedláke4889912018-11-02 09:52:40 +0100139 /* 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ák3b4db242018-10-19 16:11:01 +0200146
David Sedláke4889912018-11-02 09:52:40 +0100147 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ák3b4db242018-10-19 16:11:01 +0200154}