blob: 703a24e30f89aedff10977760bfa93f9928d2d63 [file] [log] [blame]
Michal Vasko7fbc8162018-09-17 10:35:16 +02001/**
2 * @file parser_yang.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG parser
5 *
6 * Copyright (c) 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
13 */
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <unistd.h>
17#include <fcntl.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <errno.h>
22#include <ctype.h>
23#include <string.h>
24#include <dirent.h>
25#include <assert.h>
26
27#include "common.h"
28#include "context.h"
29#include "libyang.h"
30
Michal Vaskoea5abea2018-09-18 13:10:54 +020031/**
32 * @brief Loop through all substatements providing, return if there are none.
33 *
34 * @param[in] CTX libyang context for logging.
35 * @param[in] DATA Raw data to read from.
36 * @param[out] KW YANG keyword read.
37 * @param[out] WORD Pointer to the keyword itself.
38 * @param[out] WORD_LEN Length of the keyword.
39 * @param[out] ERR Variable for error storing.
40 *
41 * @return In case there are no substatements or a fatal error encountered.
42 */
Michal Vasko7fbc8162018-09-17 10:35:16 +020043#define YANG_READ_SUBSTMT_FOR(CTX, DATA, KW, WORD, WORD_LEN, ERR) \
44 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
Radek Krejcic59bc972018-09-17 16:13:06 +020045 LY_CHECK_RET(ERR); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020046 \
47 if (KW == YANG_SEMICOLON) { \
48 return ERR; \
49 } \
50 if (KW != YANG_LEFT_BRACE) { \
Radek Krejcic59bc972018-09-17 16:13:06 +020051 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\", expected \";\" or \"{\".", ly_stmt2str(KW)); \
Michal Vasko7fbc8162018-09-17 10:35:16 +020052 return LY_EVALID; \
53 } \
54 for (ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN); \
55 !ERR && (KW != YANG_RIGHT_BRACE); \
56 ERR = get_keyword(CTX, DATA, &KW, &WORD, &WORD_LEN))
57
Michal Vasko7fbc8162018-09-17 10:35:16 +020058static LY_ERR parse_container(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings);
59static LY_ERR parse_uses(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings);
60static LY_ERR parse_choice(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings);
61static LY_ERR parse_case(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings);
62static LY_ERR parse_list(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings);
63static LY_ERR parse_grouping(struct ly_ctx *ctx, const char **data, struct lysp_grp **groupings);
64
Michal Vaskoea5abea2018-09-18 13:10:54 +020065/**
66 * @brief Add another character to dynamic buffer, a low-level function.
67 *
68 * Enlarge if needed. Does not update \p buf_used !
69 *
70 * @param[in] ctx libyang context for logging.
71 * @param[in] c Character to store.
72 * @param[in,out] buf Buffer to use, can be moved by realloc().
73 * @param[in,out] buf_len Current size of the buffer.
74 * @param[in] buf_used Currently used characters of the buffer.
75 *
76 * @return LY_ERR values.
77 */
Michal Vasko7fbc8162018-09-17 10:35:16 +020078static LY_ERR
79buf_add_char(struct ly_ctx *ctx, char c, char **buf, int *buf_len, int buf_used)
80{
81 if (*buf_len == buf_used) {
82 *buf_len += 16;
83 *buf = ly_realloc(*buf, *buf_len);
84 LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
85 }
86
87 (*buf)[buf_used] = c;
88 return LY_SUCCESS;
89}
90
Michal Vaskoea5abea2018-09-18 13:10:54 +020091/**
92 * @brief Store a character. It depends whether in a dynamically-allocated buffer or just as a pointer to the data.
93 *
94 * @param[in] ctx libyang context for logging.
95 * @param[in] c Pointer to the character to store.
96 * @param[out] word_p Word pointer. If buffer (\p word_b) was not yet needed, it is just a pointer to the first
97 * stored character. If buffer was needed (\p word_b is non-NULL or \p need_buf is set), it is pointing to the buffer.
98 * @param[out] word_len Current length of the word pointed to by \p word_p.
99 * @param[in,out] word_b Word buffer. Is kept NULL as long as it is not needed (word is a substring of the data).
100 * @param[in,out] buf_len Current length of \p word_b.
101 * @param[in] need_buf Whether we need a buffer or not.
102 *
103 * @return LY_ERR values.
104 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200105static LY_ERR
106buf_store_char(struct ly_ctx *ctx, const char *c, char **word_p, int *word_len, char **word_b, int *buf_len, int need_buf)
107{
108 if (word_b && *word_b) {
109 /* add another character into buffer */
110 if (buf_add_char(ctx, *c, word_b, buf_len, *word_len)) {
111 return LY_EMEM;
112 }
113
114 /* in case of realloc */
115 *word_p = *word_b;
116 } else if (need_buf) {
117 /* first time we need a buffer, copy everything read up to now */
118 if (*word_len) {
119 *word_b = malloc(*word_len);
120 LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx), LY_EMEM);
121 *buf_len = *word_len;
122 memcpy(*word_b, *word_p, *word_len);
123 }
124
125 /* add this new character into buffer */
126 if (buf_add_char(ctx, *c, word_b, buf_len, *word_len)) {
127 return LY_EMEM;
128 }
129
130 /* in case of realloc */
131 *word_p = *word_b;
132 } else {
133 /* just remember the first character pointer */
134 if (!*word_p) {
135 *word_p = (char *)c;
136 }
137 }
138
139 ++(*word_len);
140 return LY_SUCCESS;
141}
142
Michal Vaskoea5abea2018-09-18 13:10:54 +0200143/**
144 * @brief Check that a character is correct and valid.
145 *
146 * @param[in] ctx libyang context for logging.
147 * @param[in] c Character to check.
148 * @param[in] arg Type of YANG keyword argument expected. Affects character validity.
149 * @param[in] first Whether the character is the first of the word. May affect validity.
150 * @param[in,out] prefix Set to 0 on first call. Used for internal tracking of a prefix presence.
151 *
152 * @return LY_ERR values.
153 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200154static LY_ERR
155check_char(struct ly_ctx *ctx, char c, enum yang_arg arg, int first, int *prefix)
156{
157 if ((arg == Y_STR_ARG) || (arg == Y_MAYBE_STR_ARG)) {
158 if ((c < ' ') && (c != '\t') && (c != '\n')) {
159 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHAR, c);
160 return LY_EVALID;
161 }
162 } else if ((arg == Y_IDENTIF_ARG) || (arg == Y_PREF_IDENTIF_ARG)) {
163 if (c < ' ') {
164 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHAR, c);
165 return LY_EVALID;
166 }
167
168 if (first || (*prefix == 2)) {
169 /* either first character of the whole identifier or of the name after prefix */
170 if (!isalpha(c) && (c != '_')) {
171 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c'.", c);
172 return LY_EVALID;
173 }
174 if (*prefix == 2) {
175 *prefix = 1;
176 }
177 } else {
178 /* other characters */
179 if (!isalnum(c) && (c != '_') && (c != '-') && (c != '.')) {
180 if ((arg == Y_PREF_IDENTIF_ARG) && !*prefix && (c == ':')) {
181 /* this is fine */
182 *prefix = 2;
183 } else {
184 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG, "Invalid identifier character '%c'.", c);
185 return LY_EVALID;
186 }
187 }
188 }
189 }
190
191 return LY_SUCCESS;
192}
193
Michal Vaskoea5abea2018-09-18 13:10:54 +0200194/**
195 * @brief Skip YANG comment in data.
196 *
197 * @param[in] ctx libyang context for logging.
198 * @param[in,out] data Data to read from, always moved to currently handled character.
199 * @param[in] comment 1 for a one-line comment, 2 for a block comment.
200 *
201 * @return LY_ERR values.
202 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200203static LY_ERR
204skip_comment(struct ly_ctx *ctx, const char **data, int comment)
205{
Radek Krejci80dd33e2018-09-26 15:57:18 +0200206 /* internal statuses: 0 - comment ended,
207 * 1 - in line comment,
208 * 2 - in block comment,
209 * 3 - in block comment with last read character '*'
210 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200211 while (**data && comment) {
212 switch (comment) {
213 case 1:
214 if (**data == '\n') {
215 comment = 0;
216 }
217 break;
218 case 2:
219 if (**data == '*') {
220 comment = 3;
221 }
222 break;
223 case 3:
224 if (**data == '/') {
225 comment = 0;
226 } else {
227 comment = 2;
228 }
229 break;
230 default:
231 LOGINT_RET(ctx);
232 }
233
234 ++(*data);
235 }
236
237 if (!**data && (comment > 1)) {
238 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX, "Unexpected end-of-file, non-terminated comment.");
239 return LY_EVALID;
240 }
241
242 return LY_SUCCESS;
243}
244
Michal Vaskoea5abea2018-09-18 13:10:54 +0200245/**
246 * @brief Read a quoted string from data.
247 *
248 * @param[in] ctx libyang context for logging.
249 * @param[in,out] data Data to read from, always moved to currently handled character.
250 * @param[in] arg Type of YANG keyword argument expected.
251 * @param[out] word_p Pointer to the read quoted string.
252 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read quoted string. If not needed,
253 * set to NULL. Otherwise equal to \p word_p.
254 * @param[out] word_len Length of the read quoted string.
255 * @param[out] buf_len Length of the dynamically-allocated buffer \p word_b.
256 * @param[in] indent Current indent (number of YANG spaces). Needed for correct multi-line string
257 * indenation in the final quoted string.
258 *
259 * @return LY_ERR values.
260 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200261static LY_ERR
262read_qstring(struct ly_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, int *word_len,
263 int *buf_len, int indent)
264{
265 /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
266 * 4 - string finished, now skipping whitespaces looking for +,
267 * 5 - string continues after +, skipping whitespaces */
268 int string, str_nl_indent = 0, need_buf = 0, prefix = 0;
269 const char *c;
270
271 if (**data == '\"') {
272 /* indent of the " itself */
273 ++indent;
274 string = 2;
275 } else {
276 assert(**data == '\'');
277 string = 1;
278 }
279 ++(*data);
280
281 while (**data && string) {
282 switch (string) {
283 case 1:
284 switch (**data) {
285 case '\'':
286 /* string may be finished, but check for + */
287 string = 4;
288 break;
289 default:
290 /* check and store character */
291 if (check_char(ctx, **data, arg, !*word_len, &prefix)) {
292 return LY_EVALID;
293 }
294 if (buf_store_char(ctx, *data, word_p, word_len, word_b, buf_len, need_buf)) {
295 return LY_EMEM;
296 }
297 break;
298 }
299 break;
300 case 2:
301 switch (**data) {
302 case '\"':
303 /* string may be finished, but check for + */
304 string = 4;
305 break;
306 case '\\':
307 /* special character following */
308 string = 3;
309 break;
310 case ' ':
311 if (str_nl_indent) {
312 --str_nl_indent;
313 } else {
314 /* check and store character */
315 if (check_char(ctx, **data, arg, !*word_len, &prefix)) {
316 return LY_EVALID;
317 }
318 if (buf_store_char(ctx, *data, word_p, word_len, word_b, buf_len, need_buf)) {
319 return LY_EMEM;
320 }
321 }
322 break;
323 case '\t':
324 if (str_nl_indent) {
325 if (str_nl_indent < 9) {
326 str_nl_indent = 0;
327 } else {
328 str_nl_indent -= 8;
329 }
330 } else {
331 /* check and store character */
332 if (check_char(ctx, **data, arg, !*word_len, &prefix)) {
333 return LY_EVALID;
334 }
335 if (buf_store_char(ctx, *data, word_p, word_len, word_b, buf_len, need_buf)) {
336 return LY_EMEM;
337 }
338 }
339 break;
340 case '\n':
341 str_nl_indent = indent;
342 if (indent) {
343 /* we will be removing the indents so we need our own buffer */
344 need_buf = 1;
345 }
346
347 /* check and store character */
348 if (check_char(ctx, **data, arg, !*word_len, &prefix)) {
349 return LY_EVALID;
350 }
351 if (buf_store_char(ctx, *data, word_p, word_len, word_b, buf_len, need_buf)) {
352 return LY_EMEM;
353 }
354 break;
355 default:
356 /* first non-whitespace character, clear current indent */
357 str_nl_indent = 0;
358
359 /* check and store character */
360 if (check_char(ctx, **data, arg, !*word_len, &prefix)) {
361 return LY_EVALID;
362 }
363 if (buf_store_char(ctx, *data, word_p, word_len, word_b, buf_len, need_buf)) {
364 return LY_EMEM;
365 }
366 break;
367 }
368 break;
369 case 3:
370 /* string encoded characters */
371 switch (**data) {
372 case 'n':
373 c = "\n";
374 break;
375 case 't':
376 c = "\t";
377 break;
378 case '\"':
379 c = *data;
380 break;
381 case '\\':
382 c = *data;
383 break;
384 default:
385 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
386 "Double-quoted string unknown special character '\\%c'.\n", **data);
387 return LY_EVALID;
388 }
389
390 /* check and store character */
391 if (check_char(ctx, *c, arg, !*word_len, &prefix)) {
392 return LY_EVALID;
393 }
394 if (buf_store_char(ctx, c, word_p, word_len, word_b, buf_len, need_buf)) {
395 return LY_EMEM;
396 }
397
398 string = 2;
399 break;
400 case 4:
401 switch (**data) {
402 case '+':
403 /* string continues */
404 string = 5;
405 break;
406 case ' ':
407 case '\t':
408 case '\n':
409 /* just skip */
410 break;
411 default:
412 /* string is finished */
413 goto string_end;
414 }
415 break;
416 case 5:
417 switch (**data) {
418 case ' ':
419 case '\t':
420 case '\n':
421 /* skip */
422 break;
423 case '\'':
424 string = 1;
425 break;
426 case '\"':
427 string = 2;
428 break;
429 default:
430 /* it must be quoted again */
431 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
432 "Both string parts divided by '+' must be quoted.\n");
433 return LY_EVALID;
434 }
435 break;
436 default:
437 return LY_EINT;
438 }
439
440 ++(*data);
441 }
442
443string_end:
444 return LY_SUCCESS;
445}
446
Michal Vaskoea5abea2018-09-18 13:10:54 +0200447/**
448 * @brief Get another YANG string from the raw data.
449 *
450 * @param[in] ctx libyang context for logging.
451 * @param[in,out] data Data to read from, always moved to currently handled character.
452 * @param[in] arg Type of YANG keyword argument expected.
Michal Vasko2ca70f52018-09-27 11:04:51 +0200453 * @param[out] word_p Pointer to the read string. Can return NULL if \p arg is #Y_MAYBE_STR_ARG.
Michal Vaskoea5abea2018-09-18 13:10:54 +0200454 * @param[out] word_b Pointer to a dynamically-allocated buffer holding the read string. If not needed,
455 * set to NULL. Otherwise equal to \p word_p.
456 * @param[out] word_len Length of the read string.
457 *
458 * @return LY_ERR values.
Michal Vasko7fbc8162018-09-17 10:35:16 +0200459 */
460static LY_ERR
461get_string(struct ly_ctx *ctx, const char **data, enum yang_arg arg, char **word_p, char **word_b, int *word_len)
462{
463 int buf_len = 0, slash = 0, indent = 0, prefix = 0;
464 LY_ERR ret;
465
466 /* word buffer - dynamically allocated */
467 *word_b = NULL;
468
469 /* word pointer - just a pointer to data */
470 *word_p = NULL;
471
472 *word_len = 0;
473 while (**data) {
474 if (slash) {
475 if (**data == '/') {
476 /* one-line comment */
477 ret = skip_comment(ctx, data, 1);
478 if (ret) {
479 return ret;
480 }
481 } else if (**data == '*') {
482 /* block comment */
483 ret = skip_comment(ctx, data, 2);
484 if (ret) {
485 return ret;
486 }
487 } else {
488 /* not a comment after all */
489 ret = buf_store_char(ctx, (*data) - 1, word_p, word_len, word_b, &buf_len, 0);
490 if (ret) {
491 return ret;
492 }
493 }
494 slash = 0;
495 }
496
497 switch (**data) {
498 case '\'':
499 case '\"':
500 if (*word_len) {
501 /* we want strings always in a separate word, leave it */
502 goto str_end;
503 }
504 ret = read_qstring(ctx, data, arg, word_p, word_b, word_len, &buf_len, indent);
505 if (ret) {
506 return ret;
507 }
508 goto str_end;
509 case '/':
510 slash = 1;
511 break;
512 case ' ':
513 if (*word_len) {
514 /* word is finished */
515 goto str_end;
516 }
517 /* increase indent */
518 ++indent;
519 break;
520 case '\t':
521 if (*word_len) {
522 /* word is finished */
523 goto str_end;
524 }
525 /* tabs count for 8 spaces */
526 indent += 8;
527 break;
528 case '\n':
529 if (*word_len) {
530 /* word is finished */
531 goto str_end;
532 }
533 /* reset indent */
534 indent = 0;
535 break;
536 case ';':
537 case '{':
538 if (*word_len || (arg == Y_MAYBE_STR_ARG)) {
539 /* word is finished */
540 goto str_end;
541 }
542
543 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INSTREXP, 1, *data, "an argument");
544 return LY_EVALID;
545 default:
546 if (check_char(ctx, **data, arg, !*word_len, &prefix)) {
547 return LY_EVALID;
548 }
549
550 if (buf_store_char(ctx, *data, word_p, word_len, word_b, &buf_len, 0)) {
551 return LY_EMEM;
552 }
553 break;
554 }
555
556 ++(*data);
557 }
558
559str_end:
560 /* ending '\0' for buf */
561 if (*word_b) {
562 if (buf_add_char(ctx, '\0', word_b, &buf_len, *word_len)) {
563 return LY_EMEM;
564 }
565 *word_p = *word_b;
566 }
567
568 return LY_SUCCESS;
569}
570
Michal Vaskoea5abea2018-09-18 13:10:54 +0200571/**
572 * @brief Get another YANG keyword from the raw data.
573 *
574 * @param[in] ctx libyang context for logging.
575 * @param[in,out] data Data to read from, always moved to currently handled character.
576 * @param[out] kw YANG keyword read.
577 * @param[out] word_p Pointer to the keyword in the data. Useful for extension instances.
578 * @param[out] word_len Length of the keyword in the data. Useful for extension instances.
579 *
580 * @return LY_ERR values.
581 */
Michal Vasko7fbc8162018-09-17 10:35:16 +0200582static LY_ERR
583get_keyword(struct ly_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, int *word_len)
584{
585 LY_ERR ret;
586 int prefix;
587 const char *word_start;
588 /* slash: 0 - nothing, 1 - last character was '/' */
589 int slash = 0;
590
591 if (word_p) {
592 *word_p = NULL;
593 *word_len = 0;
594 }
595
596 /* first skip "optsep", comments */
597 while (**data) {
598 if (slash) {
599 if (**data == '/') {
600 /* one-line comment */
601 ret = skip_comment(ctx, data, 1);
602 if (ret) {
603 return ret;
604 }
605 } else if (**data == '*') {
606 /* block comment */
607 ret = skip_comment(ctx, data, 2);
608 if (ret) {
609 return ret;
610 }
611 } else {
612 /* not a comment after all */
613 goto keyword_start;
614 }
615 slash = 0;
616 }
617
618 switch (**data) {
619 case '/':
620 slash = 1;
621 break;
622 case ' ':
623 case '\t':
624 case '\n':
625 /* skip whitespaces (optsep) */
626 break;
627 default:
628 /* either a keyword start or an invalid character */
629 goto keyword_start;
630 }
631
632 ++(*data);
633 }
634
635keyword_start:
636 word_start = *data;
637 *kw = YANG_NONE;
638
639 /* read the keyword itself */
640 switch (**data) {
641 case 'a':
642 ++(*data);
643 if (!strncmp(*data, "rgument", 7)) {
644 *data += 7;
645 *kw = YANG_ARGUMENT;
646 } else if (!strncmp(*data, "ugment", 6)) {
647 *data += 6;
648 *kw = YANG_AUGMENT;
649 } else if (!strncmp(*data, "ction", 5)) {
650 *data += 5;
651 *kw = YANG_ACTION;
652 } else if (!strncmp(*data, "ny", 2)) {
653 *data += 2;
654 if (!strncmp(*data, "data", 4)) {
655 *data += 4;
656 *kw = YANG_ANYDATA;
657 } else if (!strncmp(*data, "xml", 3)) {
658 *data += 3;
659 *kw = YANG_ANYXML;
660 }
661 }
662 break;
663 case 'b':
664 ++(*data);
665 if (!strncmp(*data, "ase", 3)) {
666 *data += 3;
667 *kw = YANG_BASE;
668 } else if (!strncmp(*data, "elongs-to", 9)) {
669 *data += 9;
670 *kw = YANG_BELONGS_TO;
671 } else if (!strncmp(*data, "it", 2)) {
672 *data += 2;
673 *kw = YANG_BIT;
674 }
675 break;
676 case 'c':
677 ++(*data);
678 if (!strncmp(*data, "ase", 3)) {
679 *data += 3;
680 *kw = YANG_CASE;
681 } else if (!strncmp(*data, "hoice", 5)) {
682 *data += 5;
683 *kw = YANG_CHOICE;
684 } else if (!strncmp(*data, "on", 2)) {
685 *data += 2;
686 if (!strncmp(*data, "fig", 3)) {
687 *data += 3;
688 *kw = YANG_CONFIG;
689 } else if (!strncmp(*data, "ta", 2)) {
690 *data += 2;
691 if (!strncmp(*data, "ct", 2)) {
692 *data += 2;
693 *kw = YANG_CONTACT;
694 } else if (!strncmp(*data, "iner", 4)) {
695 *data += 4;
696 *kw = YANG_CONTAINER;
697 }
698 }
699 }
700 break;
701 case 'd':
702 ++(*data);
703 if (**data == 'e') {
704 ++(*data);
705 if (!strncmp(*data, "fault", 5)) {
706 *data += 5;
707 *kw = YANG_DEFAULT;
708 } else if (!strncmp(*data, "scription", 9)) {
709 *data += 9;
710 *kw = YANG_DESCRIPTION;
711 } else if (!strncmp(*data, "viat", 4)) {
712 *data += 4;
713 if (**data == 'e') {
714 ++(*data);
715 *kw = YANG_DEVIATE;
716 } else if (!strncmp(*data, "ion", 3)) {
717 *data += 3;
718 *kw = YANG_DEVIATION;
719 }
720 }
721 }
722 break;
723 case 'e':
724 ++(*data);
725 if (!strncmp(*data, "num", 3)) {
726 *data += 3;
727 *kw = YANG_ENUM;
728 } else if (!strncmp(*data, "rror-", 5)) {
729 *data += 5;
730 if (!strncmp(*data, "app-tag", 7)) {
731 *data += 7;
732 *kw = YANG_ERROR_APP_TAG;
733 } else if (!strncmp(*data, "message", 7)) {
734 *data += 7;
735 *kw = YANG_ERROR_MESSAGE;
736 }
737 } else if (!strncmp(*data, "xtension", 8)) {
738 *data += 8;
739 *kw = YANG_EXTENSION;
740 }
741 break;
742 case 'f':
743 ++(*data);
744 if (!strncmp(*data, "eature", 6)) {
745 *data += 6;
746 *kw = YANG_FEATURE;
747 } else if (!strncmp(*data, "raction-digits", 14)) {
748 *data += 14;
749 *kw = YANG_FRACTION_DIGITS;
750 }
751 break;
752 case 'g':
753 ++(*data);
754 if (!strncmp(*data, "rouping", 7)) {
755 *data += 7;
756 *kw = YANG_GROUPING;
757 }
758 break;
759 case 'i':
760 ++(*data);
761 if (!strncmp(*data, "dentity", 7)) {
762 *data += 7;
763 *kw = YANG_IDENTITY;
764 } else if (!strncmp(*data, "f-feature", 9)) {
765 *data += 9;
766 *kw = YANG_IF_FEATURE;
767 } else if (!strncmp(*data, "mport", 5)) {
768 *data += 5;
769 *kw = YANG_IMPORT;
770 } else if (**data == 'n') {
771 ++(*data);
772 if (!strncmp(*data, "clude", 5)) {
773 *data += 5;
774 *kw = YANG_INCLUDE;
775 } else if (!strncmp(*data, "put", 3)) {
776 *data += 3;
777 *kw = YANG_INPUT;
778 }
779 }
780 break;
781 case 'k':
782 ++(*data);
783 if (!strncmp(*data, "ey", 2)) {
784 *data += 2;
785 *kw = YANG_KEY;
786 }
787 break;
788 case 'l':
789 ++(*data);
790 if (**data == 'e') {
791 ++(*data);
792 if (!strncmp(*data, "af", 2)) {
793 *data += 2;
794 if (**data != '-') {
795 *kw = YANG_LEAF;
796 } else if (!strncmp(*data, "-list", 5)) {
797 *data += 5;
798 *kw = YANG_LEAF_LIST;
799 }
800 } else if (!strncmp(*data, "ngth", 4)) {
801 *data += 4;
802 *kw = YANG_LENGTH;
803 }
804 } else if (!strncmp(*data, "ist", 3)) {
805 *data += 3;
806 *kw = YANG_LIST;
807 }
808 break;
809 case 'm':
810 ++(*data);
811 if (**data == 'a') {
812 ++(*data);
813 if (!strncmp(*data, "ndatory", 7)) {
814 *data += 7;
815 *kw = YANG_MANDATORY;
816 } else if (!strncmp(*data, "x-elements", 10)) {
817 *data += 10;
818 *kw = YANG_MAX_ELEMENTS;
819 }
820 } else if (!strncmp(*data, "in-elements", 11)) {
821 *data += 11;
822 *kw = YANG_MIN_ELEMENTS;
823 } else if (!strncmp(*data, "ust", 3)) {
824 *data += 3;
825 *kw = YANG_MUST;
826 } else if (!strncmp(*data, "od", 2)) {
827 *data += 2;
828 if (!strncmp(*data, "ule", 3)) {
829 *data += 3;
830 *kw = YANG_MODULE;
831 } else if (!strncmp(*data, "ifier", 5)) {
832 *data += 3;
833 *kw = YANG_MODIFIER;
834 }
835 }
836 break;
837 case 'n':
838 ++(*data);
839 if (!strncmp(*data, "amespace", 8)) {
840 *data += 8;
841 *kw = YANG_NAMESPACE;
842 } else if (!strncmp(*data, "otification", 11)) {
843 *data += 11;
844 *kw = YANG_NOTIFICATION;
845 }
846 break;
847 case 'o':
848 ++(*data);
849 if (**data == 'r') {
850 ++(*data);
851 if (!strncmp(*data, "dered-by", 8)) {
852 *data += 8;
853 *kw = YANG_ORDERED_BY;
854 } else if (!strncmp(*data, "ganization", 10)) {
855 *data += 10;
856 *kw = YANG_ORGANIZATION;
857 }
858 } else if (!strncmp(*data, "utput", 5)) {
859 *data += 5;
860 *kw = YANG_OUTPUT;
861 }
862 break;
863 case 'p':
864 ++(*data);
865 if (!strncmp(*data, "at", 2)) {
866 *data += 2;
867 if (**data == 'h') {
868 ++(*data);
869 *kw = YANG_PATH;
870 } else if (!strncmp(*data, "tern", 4)) {
871 *data += 4;
872 *kw = YANG_PATTERN;
873 }
874 } else if (!strncmp(*data, "osition", 7)) {
875 *data += 7;
876 *kw = YANG_POSITION;
877 } else if (!strncmp(*data, "re", 2)) {
878 *data += 2;
879 if (!strncmp(*data, "fix", 3)) {
880 *data += 3;
881 *kw = YANG_PREFIX;
882 } else if (!strncmp(*data, "sence", 5)) {
883 *data += 5;
884 *kw = YANG_PRESENCE;
885 }
886 }
887 break;
888 case 'r':
889 ++(*data);
890 if (!strncmp(*data, "ange", 4)) {
891 *data += 4;
892 *kw = YANG_RANGE;
893 } else if (**data == 'e') {
894 ++(*data);
895 if (**data == 'f') {
896 ++(*data);
897 if (!strncmp(*data, "erence", 6)) {
898 *data += 6;
899 *kw = YANG_REFERENCE;
900 } else if (!strncmp(*data, "ine", 3)) {
901 *data += 3;
902 *kw = YANG_REFINE;
903 }
904 } else if (!strncmp(*data, "quire-instance", 14)) {
905 *data += 14;
906 *kw = YANG_REQUIRE_INSTANCE;
907 } else if (!strncmp(*data, "vision", 6)) {
908 *data += 6;
909 if (**data != '-') {
910 *kw = YANG_REVISION;
911 } else if (!strncmp(*data, "-date", 5)) {
912 *data += 5;
913 *kw = YANG_REVISION_DATE;
914 }
915 }
916 } else if (!strncmp(*data, "pc", 2)) {
917 *data += 2;
918 *kw = YANG_RPC;
919 }
920 break;
921 case 's':
922 ++(*data);
923 if (!strncmp(*data, "tatus", 5)) {
924 *data += 5;
925 *kw = YANG_STATUS;
926 } else if (!strncmp(*data, "ubmodule", 8)) {
927 *data += 8;
928 *kw = YANG_SUBMODULE;
929 }
930 break;
931 case 't':
932 ++(*data);
933 if (!strncmp(*data, "ype", 3)) {
934 *data += 3;
935 if (**data != 'd') {
936 *kw = YANG_TYPE;
937 } else if (!strncmp(*data, "def", 3)) {
938 *data += 3;
939 *kw = YANG_TYPEDEF;
940 }
941 }
942 break;
943 case 'u':
944 ++(*data);
945 if (!strncmp(*data, "ni", 2)) {
946 *data += 2;
947 if (!strncmp(*data, "que", 3)) {
948 *data += 3;
949 *kw = YANG_UNIQUE;
950 } else if (!strncmp(*data, "ts", 2)) {
951 *data += 2;
952 *kw = YANG_UNITS;
953 }
954 } else if (!strncmp(*data, "ses", 3)) {
955 *data += 3;
956 *kw = YANG_USES;
957 }
958 break;
959 case 'v':
960 ++(*data);
961 if (!strncmp(*data, "alue", 4)) {
962 *data += 4;
963 *kw = YANG_VALUE;
964 }
965 break;
966 case 'w':
967 ++(*data);
968 if (!strncmp(*data, "hen", 3)) {
969 *data += 3;
970 *kw = YANG_WHEN;
971 }
972 break;
973 case 'y':
974 ++(*data);
975 if (!strncmp(*data, "ang-version", 11)) {
976 *data += 11;
977 *kw = YANG_YANG_VERSION;
978 } else if (!strncmp(*data, "in-element", 10)) {
979 *data += 10;
980 *kw = YANG_YIN_ELEMENT;
981 }
982 break;
983 case ';':
984 ++(*data);
985 *kw = YANG_SEMICOLON;
986 break;
987 case '{':
988 ++(*data);
989 *kw = YANG_LEFT_BRACE;
990 break;
991 case '}':
992 ++(*data);
993 *kw = YANG_RIGHT_BRACE;
994 break;
995 default:
996 break;
997 }
998
999 if (*kw != YANG_NONE) {
1000 /* make sure we have the whole keyword */
1001 switch (**data) {
1002 case ' ':
1003 case '\t':
1004 case '\n':
1005 /* mandatory "sep" */
1006 break;
1007 default:
1008 ++(*data);
1009 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start,
1010 "a keyword followed by a separator");
1011 return LY_EVALID;
1012 }
1013 } else {
1014 /* still can be an extension */
1015 prefix = 0;
1016 while (**data && (**data != ' ') && (**data != '\t') && (**data != '\n') && (**data != '{') && (**data != ';')) {
1017 if (check_char(ctx, **data, Y_PREF_IDENTIF_ARG, *data == word_start ? 1 : 0, &prefix)) {
1018 return LY_EVALID;
1019 }
1020 ++(*data);
1021 }
1022 if (!**data) {
Radek Krejcic07921a2018-09-17 11:40:15 +02001023 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_EOF);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001024 return LY_EVALID;
1025 }
1026
1027 /* prefix is mandatory for extension instances */
1028 if (prefix != 1) {
1029 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INSTREXP, (int)(*data - word_start), word_start, "a keyword");
1030 return LY_EVALID;
1031 }
1032
1033 *kw = YANG_CUSTOM;
1034 }
1035
1036 if (word_p) {
1037 *word_p = (char *)word_start;
1038 *word_len = *data - word_start;
1039 }
1040
1041 return LY_SUCCESS;
1042}
1043
Michal Vaskoea5abea2018-09-18 13:10:54 +02001044/**
1045 * @brief Parse extension instance substatements.
1046 *
1047 * @param[in] ctx libyang context for logging.
1048 * @param[in,out] data Data to read from, always moved to currently handled character.
1049 * @param[in] word Extension instance substatement name (keyword).
1050 * @param[in] word_len Extension instance substatement name length.
1051 * @param[in,out] child Children of this extension instance to add to.
1052 *
1053 * @return LY_ERR values.
1054 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001055static LY_ERR
1056parse_ext_substmt(struct ly_ctx *ctx, const char **data, char *word, int word_len,
1057 struct lysp_stmt **child)
1058{
1059 char *buf;
1060 LY_ERR ret = 0;
1061 enum yang_keyword kw;
1062 struct lysp_stmt *stmt, *par_child;
1063
1064 stmt = calloc(1, sizeof *stmt);
1065 LY_CHECK_ERR_RET(!stmt, LOGMEM(NULL), LY_EMEM);
1066
1067 stmt->stmt = lydict_insert(ctx, word, word_len);
1068
1069 /* get optional argument */
1070 ret = get_string(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001071 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001072
Radek Krejci0ae092d2018-09-20 16:43:19 +02001073 if (word) {
1074 if (buf) {
1075 stmt->arg = lydict_insert_zc(ctx, word);
1076 } else {
1077 stmt->arg = lydict_insert(ctx, word, word_len);
1078 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001079 }
1080
1081 /* insert into parent statements */
1082 if (!*child) {
1083 *child = stmt;
1084 } else {
1085 for (par_child = *child; par_child->next; par_child = par_child->next);
1086 par_child->next = stmt;
1087 }
1088
1089 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001090 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001091
1092 ret = parse_ext_substmt(ctx, data, word, word_len, &stmt->child);
Radek Krejcic59bc972018-09-17 16:13:06 +02001093 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001094 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001095 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001096
1097 return ret;
1098}
1099
Michal Vaskoea5abea2018-09-18 13:10:54 +02001100/**
1101 * @brief Parse extension instance.
1102 *
1103 * @param[in] ctx libyang context for logging.
1104 * @param[in,out] data Data to read from, always moved to currently handled character.
1105 * @param[in] ext_name Extension instance substatement name (keyword).
1106 * @param[in] ext_name_len Extension instance substatement name length.
1107 * @param[in] insubstmt Type of the keyword this extension instance is a substatement of.
1108 * @param[in] insubstmt_index Index of the keyword instance this extension instance is a substatement of.
1109 * @param[in,out] exts Extension instances to add to.
1110 *
1111 * @return LY_ERR values.
1112 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001113static LY_ERR
1114parse_ext(struct ly_ctx *ctx, const char **data, const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
1115 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
1116{
1117 LY_ERR ret = 0;
1118 char *buf, *word;
1119 int word_len;
1120 struct lysp_ext_instance *e;
1121 enum yang_keyword kw;
1122
1123 LYSP_ARRAY_NEW_RET(ctx, exts, e, LY_EMEM);
1124
1125 /* store name and insubstmt info */
1126 e->name = lydict_insert(ctx, ext_name, ext_name_len);
1127 e->insubstmt = insubstmt;
1128 e->insubstmt_index = insubstmt_index;
1129
1130 /* get optional argument */
1131 ret = get_string(ctx, data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001132 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001133
Radek Krejci0ae092d2018-09-20 16:43:19 +02001134 if (word) {
1135 if (buf) {
1136 e->argument = lydict_insert_zc(ctx, word);
1137 } else {
1138 e->argument = lydict_insert(ctx, word, word_len);
1139 }
Michal Vasko7fbc8162018-09-17 10:35:16 +02001140 }
1141
1142 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001143 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001144
1145 ret = parse_ext_substmt(ctx, data, word, word_len, &e->child);
Radek Krejcic59bc972018-09-17 16:13:06 +02001146 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001147 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001148 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001149
1150 return ret;
1151}
1152
Michal Vaskoea5abea2018-09-18 13:10:54 +02001153/**
1154 * @brief Parse a generic text field without specific constraints. Those are contact, organization,
1155 * description, etc...
1156 *
1157 * @param[in] ctx libyang context for logging.
1158 * @param[in,out] data Data to read from, always moved to currently handled character.
1159 * @param[in] substmt Type of this substatement.
1160 * @param[in] substmt_index Index of this substatement.
1161 * @param[in,out] value Place to store the parsed value.
1162 * @param[in] arg Type of the YANG keyword argument (of the value).
1163 * @param[in,out] exts Extension instances to add to.
1164 *
1165 * @return LY_ERR values.
1166 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001167static LY_ERR
1168parse_text_field(struct ly_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, uint32_t substmt_index,
1169 const char **value, enum yang_arg arg, struct lysp_ext_instance **exts)
1170{
1171 LY_ERR ret = 0;
1172 char *buf, *word;
1173 int word_len;
1174 enum yang_keyword kw;
1175
1176 if (*value) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001177 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001178 return LY_EVALID;
1179 }
1180
1181 /* get value */
1182 ret = get_string(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001183 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001184
1185 /* store value and spend buf if allocated */
1186 if (buf) {
1187 *value = lydict_insert_zc(ctx, word);
1188 } else {
1189 *value = lydict_insert(ctx, word, word_len);
1190 }
1191
1192 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001193 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001194
1195 switch (kw) {
1196 case YANG_CUSTOM:
1197 ret = parse_ext(ctx, data, word, word_len, substmt, substmt_index, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001198 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001199 break;
1200 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001201 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001202 return LY_EVALID;
1203 }
1204 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001205 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001206
1207 return ret;
1208}
1209
Michal Vaskoea5abea2018-09-18 13:10:54 +02001210/**
1211 * @brief Parse the yang-version statement.
1212 *
1213 * @param[in] ctx libyang context for logging.
1214 * @param[in,out] data Data to read from, always moved to currently handled character.
1215 * @param[in] mod Module to store the parsed information in.
1216 *
1217 * @return LY_ERR values.
1218 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001219static LY_ERR
1220parse_yangversion(struct ly_ctx *ctx, const char **data, struct lysp_module *mod)
1221{
1222 LY_ERR ret = 0;
1223 char *buf, *word;
1224 int word_len;
1225 enum yang_keyword kw;
1226
1227 if (mod->version) {
1228 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "yang-version");
1229 return LY_EVALID;
1230 }
1231
1232 /* get value */
1233 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001234 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001235
1236 if ((word_len == 3) && !strncmp(word, "1.0", word_len)) {
1237 mod->version = LYS_VERSION_1_0;
1238 } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
1239 mod->version = LYS_VERSION_1_1;
1240 } else {
1241 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "yang-version");
1242 free(buf);
1243 return LY_EVALID;
1244 }
1245 free(buf);
1246
1247 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001248 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001249
1250 switch (kw) {
1251 case YANG_CUSTOM:
1252 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_VERSION, 0, &mod->exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001253 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001254 break;
1255 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001256 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yang-version");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001257 return LY_EVALID;
1258 }
1259 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001260 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001261
1262 return ret;
1263}
1264
Michal Vaskoea5abea2018-09-18 13:10:54 +02001265/**
1266 * @brief Parse the belongs-to statement.
1267 *
1268 * @param[in] ctx libyang context for logging.
1269 * @param[in,out] data Data to read from, always moved to currently handled character.
1270 * @param[in,out] belongsto Place to store the parsed value.
1271 * @param[in,out] prefix Place to store the parsed belongs-to prefix value.
1272 * @param[in,out] exts Extension instances to add to.
1273 *
1274 * @return LY_ERR values.
1275 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001276static LY_ERR
1277parse_belongsto(struct ly_ctx *ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext_instance **exts)
1278{
1279 LY_ERR ret = 0;
1280 char *buf, *word;
1281 int word_len;
1282 enum yang_keyword kw;
1283
1284 if (*belongsto) {
1285 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "belongs-to");
1286 return LY_EVALID;
1287 }
1288
1289 /* get value */
1290 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001291 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001292
1293 if (buf) {
1294 *belongsto = lydict_insert_zc(ctx, word);
1295 } else {
1296 *belongsto = lydict_insert(ctx, word, word_len);
1297 }
1298
1299 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001300 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001301
1302 switch (kw) {
1303 case YANG_PREFIX:
1304 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, prefix, Y_IDENTIF_ARG, exts);
1305 break;
1306 case YANG_CUSTOM:
1307 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_BELONGSTO, 0, exts);
1308 break;
1309 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001310 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "belongs-to");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001311 return LY_EVALID;
1312 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001313 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001314 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001315 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001316
1317 /* mandatory substatements */
1318 if (!*prefix) {
1319 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "prefix", "belongs-to");
1320 return LY_EVALID;
1321 }
1322
1323 return ret;
1324}
1325
Michal Vaskoea5abea2018-09-18 13:10:54 +02001326/**
1327 * @brief Parse the revision-date statement.
1328 *
1329 * @param[in] ctx libyang context for logging.
1330 * @param[in,out] data Data to read from, always moved to currently handled character.
1331 * @param[in,out] rev Array to store the parsed value in.
1332 * @param[in,out] exts Extension instances to add to.
1333 *
1334 * @return LY_ERR values.
1335 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001336static LY_ERR
1337parse_revisiondate(struct ly_ctx *ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
1338{
1339 LY_ERR ret = 0;
1340 char *buf, *word;
1341 int word_len;
1342 enum yang_keyword kw;
1343
1344 if (rev[0]) {
1345 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "revision-date");
1346 return LY_EVALID;
1347 }
1348
1349 /* get value */
1350 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001351 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001352
1353 /* check value */
1354 if (lysp_check_date(ctx, word, word_len, "revision-date")) {
1355 free(buf);
1356 return LY_EVALID;
1357 }
1358
1359 /* store value and spend buf if allocated */
1360 strncpy(rev, word, word_len);
1361 free(buf);
1362
1363 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001364 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001365
1366 switch (kw) {
1367 case YANG_CUSTOM:
1368 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REVISIONDATE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001369 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001370 break;
1371 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001372 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision-date");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001373 return LY_EVALID;
1374 }
1375 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001376 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001377
1378 return ret;
1379}
1380
Michal Vaskoea5abea2018-09-18 13:10:54 +02001381/**
1382 * @brief Parse the include statement.
1383 *
1384 * @param[in] ctx libyang context for logging.
1385 * @param[in,out] data Data to read from, always moved to currently handled character.
1386 * @param[in,out] includes Parsed includes to add to.
1387 *
1388 * @return LY_ERR values.
1389 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001390static LY_ERR
1391parse_include(struct ly_ctx *ctx, const char **data, struct lysp_include **includes)
1392{
1393 LY_ERR ret = 0;
1394 char *buf, *word;
1395 int word_len;
1396 enum yang_keyword kw;
1397 struct lysp_include *inc;
1398
1399 LYSP_ARRAY_NEW_RET(ctx, includes, inc, LY_EMEM);
1400
1401 /* get value */
1402 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001403 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001404
1405 if (buf) {
1406 inc->name = lydict_insert_zc(ctx, word);
1407 } else {
1408 inc->name = lydict_insert(ctx, word, word_len);
1409 }
1410
1411 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001412 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001413
1414 switch (kw) {
1415 case YANG_DESCRIPTION:
1416 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &inc->dsc, Y_STR_ARG, &inc->exts);
1417 break;
1418 case YANG_REFERENCE:
1419 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &inc->ref, Y_STR_ARG, &inc->exts);
1420 break;
1421 case YANG_REVISION_DATE:
1422 ret = parse_revisiondate(ctx, data, inc->rev, &inc->exts);
1423 break;
1424 case YANG_CUSTOM:
1425 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inc->exts);
1426 break;
1427 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001428 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "include");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001429 return LY_EVALID;
1430 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001431 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001432 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001433 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001434
1435 return ret;
1436}
1437
Michal Vaskoea5abea2018-09-18 13:10:54 +02001438/**
1439 * @brief Parse the import statement.
1440 *
1441 * @param[in] ctx libyang context for logging.
1442 * @param[in,out] data Data to read from, always moved to currently handled character.
1443 * @param[in,out] imports Parsed imports to add to.
1444 *
1445 * @return LY_ERR values.
1446 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001447static LY_ERR
1448parse_import(struct ly_ctx *ctx, const char **data, struct lysp_import **imports)
1449{
1450 LY_ERR ret = 0;
1451 char *buf, *word;
1452 int word_len;
1453 enum yang_keyword kw;
1454 struct lysp_import *imp;
1455
1456 LYSP_ARRAY_NEW_RET(ctx, imports, imp, LY_EVALID);
1457
1458 /* get value */
1459 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001460 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001461
1462 if (buf) {
1463 imp->name = lydict_insert_zc(ctx, word);
1464 } else {
1465 imp->name = lydict_insert(ctx, word, word_len);
1466 }
1467
1468 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001469 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001470
1471 switch (kw) {
1472 case YANG_PREFIX:
1473 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &imp->prefix, Y_IDENTIF_ARG, &imp->exts);
1474 break;
1475 case YANG_DESCRIPTION:
1476 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &imp->dsc, Y_STR_ARG, &imp->exts);
1477 break;
1478 case YANG_REFERENCE:
1479 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &imp->ref, Y_STR_ARG, &imp->exts);
1480 break;
1481 case YANG_REVISION_DATE:
1482 ret = parse_revisiondate(ctx, data, imp->rev, &imp->exts);
1483 break;
1484 case YANG_CUSTOM:
1485 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &imp->exts);
1486 break;
1487 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001488 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "import");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001489 return LY_EVALID;
1490 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001491 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001492 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001493 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001494
1495 /* mandatory substatements */
1496 if (!imp->prefix) {
1497 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "prefix", "import");
1498 return LY_EVALID;
1499 }
1500
1501 return ret;
1502}
1503
Michal Vaskoea5abea2018-09-18 13:10:54 +02001504/**
1505 * @brief Parse the revision statement.
1506 *
1507 * @param[in] ctx libyang context for logging.
1508 * @param[in,out] data Data to read from, always moved to currently handled character.
1509 * @param[in,out] revs Parsed revisions to add to.
1510 *
1511 * @return LY_ERR values.
1512 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001513static LY_ERR
1514parse_revision(struct ly_ctx *ctx, const char **data, struct lysp_revision **revs)
1515{
1516 LY_ERR ret = 0;
1517 char *buf, *word;
1518 int word_len;
1519 enum yang_keyword kw;
1520 struct lysp_revision *rev;
1521
1522 LYSP_ARRAY_NEW_RET(ctx, revs, rev, LY_EMEM);
1523
1524 /* get value */
1525 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001526 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001527
1528 /* check value */
1529 if (lysp_check_date(ctx, word, word_len, "revision")) {
1530 return LY_EVALID;
1531 }
1532
1533 strncpy(rev->rev, word, word_len);
1534 free(buf);
1535
1536 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001537 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001538
1539 switch (kw) {
1540 case YANG_DESCRIPTION:
1541 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rev->dsc, Y_STR_ARG, &rev->exts);
1542 break;
1543 case YANG_REFERENCE:
1544 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rev->ref, Y_STR_ARG, &rev->exts);
1545 break;
1546 case YANG_CUSTOM:
1547 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rev->exts);
1548 break;
1549 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001550 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "revision");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001551 return LY_EVALID;
1552 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001553 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001554 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001555 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001556
1557 return ret;
1558}
1559
Michal Vaskoea5abea2018-09-18 13:10:54 +02001560/**
1561 * @brief Parse a generic text field that can have more instances such as base.
1562 *
1563 * @param[in] ctx libyang context for logging.
1564 * @param[in,out] data Data to read from, always moved to currently handled character.
1565 * @param[in] substmt Type of this substatement.
1566 * @param[in,out] texts Parsed values to add to.
1567 * @param[in] arg Type of the expected argument.
1568 * @param[in,out] exts Extension instances to add to.
1569 *
1570 * @return LY_ERR values.
1571 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001572static LY_ERR
1573parse_text_fields(struct ly_ctx *ctx, const char **data, LYEXT_SUBSTMT substmt, const char ***texts, enum yang_arg arg,
1574 struct lysp_ext_instance **exts)
1575{
1576 LY_ERR ret = 0;
1577 char *buf, *word;
1578 int count, word_len;
1579 enum yang_keyword kw;
1580
1581 /* allocate new pointer */
1582 for (count = 1; (*texts) && (*texts)[count - 1]; ++count);
1583 *texts = realloc(*texts, count * sizeof **texts);
1584 LY_CHECK_ERR_RET(!*texts, LOGMEM(ctx), LY_EMEM);
1585
1586 /* get value */
1587 ret = get_string(ctx, data, arg, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001588 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001589
1590 if (buf) {
1591 (*texts)[count - 1] = lydict_insert_zc(ctx, word);
1592 } else {
1593 (*texts)[count - 1] = lydict_insert(ctx, word, word_len);
1594 }
1595
1596 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001597 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001598
1599 switch (kw) {
1600 case YANG_CUSTOM:
1601 ret = parse_ext(ctx, data, word, word_len, substmt, count - 1, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001602 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001603 break;
1604 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001605 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), lyext_substmt2str(substmt));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001606 return LY_EVALID;
1607 }
1608 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001609 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001610
1611 return ret;
1612}
1613
Michal Vaskoea5abea2018-09-18 13:10:54 +02001614/**
1615 * @brief Parse the config statement.
1616 *
1617 * @param[in] ctx libyang context for logging.
1618 * @param[in,out] data Data to read from, always moved to currently handled character.
1619 * @param[in,out] flags Flags to add to.
1620 * @param[in,out] exts Extension instances to add to.
1621 *
1622 * @return LY_ERR values.
1623 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001624static LY_ERR
1625parse_config(struct ly_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
1626{
1627 LY_ERR ret = 0;
1628 char *buf, *word;
1629 int word_len;
1630 enum yang_keyword kw;
1631
1632 if (*flags & LYS_CONFIG_MASK) {
1633 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "config");
1634 return LY_EVALID;
1635 }
1636
1637 /* get value */
1638 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001639 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001640
1641 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1642 *flags |= LYS_CONFIG_W;
1643 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1644 *flags |= LYS_CONFIG_R;
1645 } else {
1646 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "config");
1647 free(buf);
1648 return LY_EVALID;
1649 }
1650 free(buf);
1651
1652 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001653 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001654
1655 switch (kw) {
1656 case YANG_CUSTOM:
1657 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_CONFIG, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001658 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001659 break;
1660 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001661 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "config");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001662 return LY_EVALID;
1663 }
1664 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001665 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001666
1667 return ret;
1668}
1669
Michal Vaskoea5abea2018-09-18 13:10:54 +02001670/**
1671 * @brief Parse the mandatory statement.
1672 *
1673 * @param[in] ctx libyang context for logging.
1674 * @param[in,out] data Data to read from, always moved to currently handled character.
1675 * @param[in,out] flags Flags to add to.
1676 * @param[in,out] exts Extension instances to add to.
1677 *
1678 * @return LY_ERR values.
1679 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001680static LY_ERR
1681parse_mandatory(struct ly_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
1682{
1683 LY_ERR ret = 0;
1684 char *buf, *word;
1685 int word_len;
1686 enum yang_keyword kw;
1687
1688 if (*flags & LYS_MAND_MASK) {
1689 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "mandatory");
1690 return LY_EVALID;
1691 }
1692
1693 /* get value */
1694 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001695 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001696
1697 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
1698 *flags |= LYS_MAND_TRUE;
1699 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
1700 *flags |= LYS_MAND_FALSE;
1701 } else {
1702 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "mandatory");
1703 free(buf);
1704 return LY_EVALID;
1705 }
1706 free(buf);
1707
1708 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001709 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001710
1711 switch (kw) {
1712 case YANG_CUSTOM:
1713 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MANDATORY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001714 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001715 break;
1716 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001717 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "mandatory");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001718 return LY_EVALID;
1719 }
1720 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001721 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001722
1723 return ret;
1724}
1725
Michal Vaskoea5abea2018-09-18 13:10:54 +02001726/**
1727 * @brief Parse a restriction such as range or length.
1728 *
1729 * @param[in] ctx libyang context for logging.
1730 * @param[in,out] data Data to read from, always moved to currently handled character.
1731 * @param[in] restr_kw Type of this particular restriction.
1732 * @param[in,out] exts Extension instances to add to.
1733 *
1734 * @return LY_ERR values.
1735 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001736static LY_ERR
1737parse_restr(struct ly_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr *restr)
1738{
1739 LY_ERR ret = 0;
1740 char *buf, *word;
1741 int word_len;
1742 enum yang_keyword kw;
1743
1744 /* get value */
1745 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001746 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001747
1748 if (buf) {
1749 restr->arg = lydict_insert_zc(ctx, word);
1750 } else {
1751 restr->arg = lydict_insert(ctx, word, word_len);
1752 }
1753
1754 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001755 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001756
1757 switch (kw) {
1758 case YANG_DESCRIPTION:
1759 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
1760 break;
1761 case YANG_REFERENCE:
1762 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
1763 break;
1764 case YANG_ERROR_APP_TAG:
1765 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
1766 break;
1767 case YANG_ERROR_MESSAGE:
1768 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
1769 break;
1770 case YANG_CUSTOM:
1771 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
1772 break;
1773 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001774 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(restr_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02001775 return LY_EVALID;
1776 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001777 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001778 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001779 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001780
1781 return ret;
1782}
1783
Michal Vaskoea5abea2018-09-18 13:10:54 +02001784/**
1785 * @brief Parse a restriction that can have more instances such as must.
1786 *
1787 * @param[in] ctx libyang context for logging.
1788 * @param[in,out] data Data to read from, always moved to currently handled character.
1789 * @param[in] restr_kw Type of this particular restriction.
1790 * @param[in,out] restrs Restrictions to add to.
1791 *
1792 * @return LY_ERR values.
1793 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001794static LY_ERR
1795parse_restrs(struct ly_ctx *ctx, const char **data, enum yang_keyword restr_kw, struct lysp_restr **restrs)
1796{
1797 struct lysp_restr *restr;
1798
1799 LYSP_ARRAY_NEW_RET(ctx, restrs, restr, LY_EMEM);
1800
1801 return parse_restr(ctx, data, restr_kw, restr);
1802}
1803
Michal Vaskoea5abea2018-09-18 13:10:54 +02001804/**
1805 * @brief Parse the status statement.
1806 *
1807 * @param[in] ctx libyang context for logging.
1808 * @param[in,out] data Data to read from, always moved to currently handled character.
1809 * @param[in,out] flags Flags to add to.
1810 * @param[in,out] exts Extension instances to add to.
1811 *
1812 * @return LY_ERR values.
1813 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001814static LY_ERR
1815parse_status(struct ly_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
1816{
1817 LY_ERR ret = 0;
1818 char *buf, *word;
1819 int word_len;
1820 enum yang_keyword kw;
1821
1822 if (*flags & LYS_STATUS_MASK) {
1823 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "status");
1824 return LY_EVALID;
1825 }
1826
1827 /* get value */
1828 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001829 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001830
1831 if ((word_len == 7) && !strncmp(word, "current", word_len)) {
1832 *flags |= LYS_STATUS_CURR;
1833 } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
1834 *flags |= LYS_STATUS_DEPRC;
1835 } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
1836 *flags |= LYS_STATUS_OBSLT;
1837 } else {
1838 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "status");
1839 free(buf);
1840 return LY_EVALID;
1841 }
1842 free(buf);
1843
1844 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001845 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001846
1847 switch (kw) {
1848 case YANG_CUSTOM:
1849 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_STATUS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02001850 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001851 break;
1852 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001853 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "status");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001854 return LY_EVALID;
1855 }
1856 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001857 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001858
1859 return ret;
1860}
1861
Michal Vaskoea5abea2018-09-18 13:10:54 +02001862/**
1863 * @brief Parse the when statement.
1864 *
1865 * @param[in] ctx libyang context for logging.
1866 * @param[in,out] data Data to read from, always moved to currently handled character.
1867 * @param[in,out] when_p When pointer to parse to.
1868 *
1869 * @return LY_ERR values.
1870 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001871static LY_ERR
1872parse_when(struct ly_ctx *ctx, const char **data, struct lysp_when **when_p)
1873{
1874 LY_ERR ret = 0;
1875 char *buf, *word;
1876 int word_len;
1877 enum yang_keyword kw;
1878 struct lysp_when *when;
1879
1880 if (*when_p) {
1881 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "when");
1882 return LY_EVALID;
1883 }
1884
1885 when = calloc(1, sizeof *when);
1886 LY_CHECK_ERR_RET(!when, LOGMEM(ctx), LY_EMEM);
1887 *when_p = when;
1888
1889 /* get value */
1890 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001891 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001892
1893 if (buf) {
1894 when->cond = lydict_insert_zc(ctx, word);
1895 } else {
1896 when->cond = lydict_insert(ctx, word, word_len);
1897 }
1898
1899 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001900 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001901
1902 switch (kw) {
1903 case YANG_DESCRIPTION:
1904 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &when->dsc, Y_STR_ARG, &when->exts);
1905 break;
1906 case YANG_REFERENCE:
1907 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &when->ref, Y_STR_ARG, &when->exts);
1908 break;
1909 case YANG_CUSTOM:
1910 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &when->exts);
1911 break;
1912 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001913 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02001914 return LY_EVALID;
1915 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001916 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001917 }
Radek Krejcic59bc972018-09-17 16:13:06 +02001918 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001919
1920 return ret;
1921}
1922
Michal Vaskoea5abea2018-09-18 13:10:54 +02001923/**
1924 * @brief Parse the anydata or anyxml statement.
1925 *
1926 * @param[in] ctx libyang context for logging.
1927 * @param[in,out] data Data to read from, always moved to currently handled character.
1928 * @param[in] kw Type of this particular keyword.
1929 * @param[in,out] siblings Siblings to add to.
1930 *
1931 * @return LY_ERR values.
1932 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02001933static LY_ERR
1934parse_any(struct ly_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node **siblings)
1935{
1936 LY_ERR ret = 0;
1937 char *buf, *word;
1938 int word_len;
1939 struct lysp_node *iter;
1940 struct lysp_node_anydata *any;
1941
1942 /* create structure */
1943 any = calloc(1, sizeof *any);
1944 LY_CHECK_ERR_RET(!any, LOGMEM(ctx), LY_EMEM);
1945 any->nodetype = kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1946
1947 /* insert into siblings */
1948 if (!*siblings) {
1949 *siblings = (struct lysp_node *)any;
1950 } else {
1951 for (iter = *siblings; iter->next; iter = iter->next);
1952 iter->next = (struct lysp_node *)any;
1953 }
1954
1955 /* get name */
1956 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02001957 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001958
1959 if (buf) {
1960 any->name = lydict_insert_zc(ctx, word);
1961 } else {
1962 any->name = lydict_insert(ctx, word, word_len);
1963 }
1964
1965 /* parse substatements */
1966 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02001967 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02001968
1969 switch (kw) {
1970 case YANG_CONFIG:
1971 ret = parse_config(ctx, data, &any->flags, &any->exts);
1972 break;
1973 case YANG_DESCRIPTION:
1974 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &any->dsc, Y_STR_ARG, &any->exts);
1975 break;
1976 case YANG_IF_FEATURE:
1977 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &any->iffeatures, Y_STR_ARG, &any->exts);
1978 break;
1979 case YANG_MANDATORY:
1980 ret = parse_mandatory(ctx, data, &any->flags, &any->exts);
1981 break;
1982 case YANG_MUST:
1983 ret = parse_restrs(ctx, data, kw, &any->musts);
1984 break;
1985 case YANG_REFERENCE:
1986 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &any->ref, Y_STR_ARG, &any->exts);
1987 break;
1988 case YANG_STATUS:
1989 ret = parse_status(ctx, data, &any->flags, &any->exts);
1990 break;
1991 case YANG_WHEN:
1992 ret = parse_when(ctx, data, &any->when);
1993 break;
1994 case YANG_CUSTOM:
1995 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &any->exts);
1996 break;
1997 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02001998 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw),
1999 (any->nodetype & LYS_ANYDATA) == LYS_ANYDATA ? ly_stmt2str(YANG_ANYDATA) : ly_stmt2str(YANG_ANYXML));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002000 return LY_EVALID;
2001 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002002 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002003 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002004 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002005
2006 return ret;
2007}
2008
Michal Vaskoea5abea2018-09-18 13:10:54 +02002009/**
2010 * @brief Parse the value or position statement. Substatement of type enum statement.
2011 *
2012 * @param[in] ctx libyang context for logging.
2013 * @param[in,out] data Data to read from, always moved to currently handled character.
2014 * @param[in] val_kw Type of this particular keyword.
2015 * @param[in,out] value Value to write to.
2016 * @param[in,out] flags Flags to write to.
2017 * @param[in,out] exts Extension instances to add to.
2018 *
2019 * @return LY_ERR values.
2020 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002021static LY_ERR
2022parse_type_enum_value_pos(struct ly_ctx *ctx, const char **data, enum yang_keyword val_kw, int64_t *value, uint16_t *flags,
2023 struct lysp_ext_instance **exts)
2024{
2025 LY_ERR ret = 0;
2026 char *buf, *word, *ptr;
2027 int word_len;
2028 long int num;
2029 unsigned long int unum;
2030 enum yang_keyword kw;
2031
2032 if (*flags & LYS_SET_VALUE) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002033 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002034 return LY_EVALID;
2035 }
2036 *flags |= LYS_SET_VALUE;
2037
2038 /* get value */
2039 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002040 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002041
2042 if (!word_len || (word[0] == '+') || ((word[0] == '0') && (word_len > 1)) || ((val_kw == YANG_VALUE) && !strncmp(word, "-0", 2))) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002043 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002044 free(buf);
2045 return LY_EVALID;
2046 }
2047
2048 errno = 0;
2049 if (val_kw == YANG_VALUE) {
2050 num = strtol(word, &ptr, 10);
2051 } else {
2052 unum = strtoul(word, &ptr, 10);
2053 }
2054 /* we have not parsed the whole argument */
2055 if (ptr - word != word_len) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002056 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002057 free(buf);
2058 return LY_EVALID;
2059 }
2060 if (errno == ERANGE) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002061 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_OOB, word_len, word, ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002062 free(buf);
2063 return LY_EVALID;
2064 }
2065 if (val_kw == YANG_VALUE) {
2066 *value = num;
2067 } else {
2068 *value = unum;
2069 }
2070 free(buf);
2071
2072 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002073 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002074
2075 switch (kw) {
2076 case YANG_CUSTOM:
2077 ret = parse_ext(ctx, data, word, word_len, val_kw == YANG_VALUE ? LYEXT_SUBSTMT_VALUE : LYEXT_SUBSTMT_POSITION, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002078 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002079 break;
2080 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002081 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(val_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002082 return LY_EVALID;
2083 }
2084 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002085 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002086
2087 return ret;
2088}
2089
Michal Vaskoea5abea2018-09-18 13:10:54 +02002090/**
2091 * @brief Parse the enum or bit statement. Substatement of type statement.
2092 *
2093 * @param[in] ctx libyang context for logging.
2094 * @param[in,out] data Data to read from, always moved to currently handled character.
2095 * @param[in] enum_kw Type of this particular keyword.
2096 * @param[in,out] enums Enums or bits to add to.
2097 *
2098 * @return LY_ERR values.
2099 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002100static LY_ERR
2101parse_type_enum(struct ly_ctx *ctx, const char **data, enum yang_keyword enum_kw, struct lysp_type_enum **enums)
2102{
2103 LY_ERR ret = 0;
2104 char *buf, *word;
2105 int word_len;
2106 enum yang_keyword kw;
2107 struct lysp_type_enum *enm;
2108
2109 LYSP_ARRAY_NEW_RET(ctx, enums, enm, LY_EMEM);
2110
2111 /* get value */
2112 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002113 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002114
2115 if (buf) {
2116 enm->name = lydict_insert_zc(ctx, word);
2117 } else {
2118 enm->name = lydict_insert(ctx, word, word_len);
2119 }
2120
2121 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002122 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002123
2124 switch (kw) {
2125 case YANG_DESCRIPTION:
2126 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &enm->dsc, Y_STR_ARG, &enm->exts);
2127 break;
2128 case YANG_IF_FEATURE:
2129 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &enm->iffeatures, Y_STR_ARG, &enm->exts);
2130 break;
2131 case YANG_REFERENCE:
2132 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &enm->ref, Y_STR_ARG, &enm->exts);
2133 break;
2134 case YANG_STATUS:
2135 ret = parse_status(ctx, data, &enm->flags, &enm->exts);
2136 break;
2137 case YANG_VALUE:
2138 case YANG_POSITION:
2139 ret = parse_type_enum_value_pos(ctx, data, kw, &enm->value, &enm->flags, &enm->exts);
2140 break;
2141 case YANG_CUSTOM:
2142 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &enm->exts);
2143 break;
2144 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002145 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), ly_stmt2str(enum_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002146 return LY_EVALID;
2147 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002148 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002149 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002150 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002151
2152 return ret;
2153}
2154
Michal Vaskoea5abea2018-09-18 13:10:54 +02002155/**
2156 * @brief Parse the fraction-digits statement. Substatement of type statement.
2157 *
2158 * @param[in] ctx libyang context for logging.
2159 * @param[in,out] data Data to read from, always moved to currently handled character.
2160 * @param[in,out] fracdig Value to write to.
2161 * @param[in,out] exts Extension instances to add to.
2162 *
2163 * @return LY_ERR values.
2164 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002165static LY_ERR
2166parse_type_fracdigits(struct ly_ctx *ctx, const char **data, uint8_t *fracdig, struct lysp_ext_instance **exts)
2167{
2168 LY_ERR ret = 0;
2169 char *buf, *word, *ptr;
2170 int word_len;
2171 unsigned long int num;
2172 enum yang_keyword kw;
2173
2174 if (*fracdig) {
2175 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "fraction-digits");
2176 return LY_EVALID;
2177 }
2178
2179 /* get value */
2180 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002181 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002182
2183 if (!word_len || (word[0] == '0') || !isdigit(word[0])) {
2184 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "fraction-digits");
2185 free(buf);
2186 return LY_EVALID;
2187 }
2188
2189 errno = 0;
2190 num = strtoul(word, &ptr, 10);
2191 /* we have not parsed the whole argument */
2192 if (ptr - word != word_len) {
2193 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "fraction-digits");
2194 free(buf);
2195 return LY_EVALID;
2196 }
2197 if ((errno == ERANGE) || (num > 18)) {
2198 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_OOB, word_len, word, "fraction-digits");
2199 free(buf);
2200 return LY_EVALID;
2201 }
2202 *fracdig = num;
2203 free(buf);
2204
2205 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002206 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002207
2208 switch (kw) {
2209 case YANG_CUSTOM:
2210 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_FRACDIGITS, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002211 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002212 break;
2213 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002214 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "fraction-digits");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002215 return LY_EVALID;
2216 }
2217 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002218 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002219
2220 return ret;
2221}
2222
Michal Vaskoea5abea2018-09-18 13:10:54 +02002223/**
2224 * @brief Parse the require-instance statement. Substatement of type statement.
2225 *
2226 * @param[in] ctx libyang context for logging.
2227 * @param[in,out] data Data to read from, always moved to currently handled character.
2228 * @param[in,out] reqinst Value to write to.
2229 * @param[in,out] flags Flags to write to.
2230 * @param[in,out] exts Extension instances to add to.
2231 *
2232 * @return LY_ERR values.
2233 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002234static LY_ERR
2235parse_type_reqinstance(struct ly_ctx *ctx, const char **data, uint8_t *reqinst, uint16_t *flags,
2236 struct lysp_ext_instance **exts)
2237{
2238 LY_ERR ret = 0;
2239 char *buf, *word;
2240 int word_len;
2241 enum yang_keyword kw;
2242
2243 if (*flags & LYS_SET_REQINST) {
2244 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "require-instance");
2245 return LY_EVALID;
2246 }
2247 *flags |= LYS_SET_REQINST;
2248
2249 /* get value */
2250 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002251 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002252
2253 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
2254 *reqinst = 1;
2255 } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
2256 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "require-instance");
2257 free(buf);
2258 return LY_EVALID;
2259 }
2260 free(buf);
2261
2262 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002263 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002264
2265 switch (kw) {
2266 case YANG_CUSTOM:
2267 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_REQINSTANCE, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002268 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002269 break;
2270 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002271 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "require-instance");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002272 return LY_EVALID;
2273 }
2274 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002275 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002276
2277 return ret;
2278}
2279
Michal Vaskoea5abea2018-09-18 13:10:54 +02002280/**
2281 * @brief Parse the modifier statement. Substatement of type pattern statement.
2282 *
2283 * @param[in] ctx libyang context for logging.
2284 * @param[in,out] data Data to read from, always moved to currently handled character.
2285 * @param[in,out] pat Value to write to.
2286 * @param[in,out] exts Extension instances to add to.
2287 *
2288 * @return LY_ERR values.
2289 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002290static LY_ERR
2291parse_type_pattern_modifier(struct ly_ctx *ctx, const char **data, const char **pat, struct lysp_ext_instance **exts)
2292{
2293 LY_ERR ret = 0;
2294 char *buf, *word;
2295 int word_len;
2296 enum yang_keyword kw;
2297
2298 if ((*pat)[0] == 0x15) {
2299 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "modifier");
2300 return LY_EVALID;
2301 }
2302
2303 /* get value */
2304 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002305 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002306
2307 if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
2308 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "modifier");
2309 free(buf);
2310 return LY_EVALID;
2311 }
2312 free(buf);
2313
2314 /* replace the value in the dictionary */
2315 buf = malloc(strlen(*pat) + 1);
2316 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
2317 strcpy(buf, *pat);
2318 lydict_remove(ctx, *pat);
2319
2320 assert(buf[0] == 0x06);
2321 buf[0] = 0x15;
2322 *pat = lydict_insert_zc(ctx, buf);
2323
2324 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002325 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002326
2327 switch (kw) {
2328 case YANG_CUSTOM:
2329 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MODIFIER, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002330 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002331 break;
2332 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002333 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "modifier");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002334 return LY_EVALID;
2335 }
2336 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002337 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002338
2339 return ret;
2340}
2341
Michal Vaskoea5abea2018-09-18 13:10:54 +02002342/**
2343 * @brief Parse the pattern statement. Substatement of type statement.
2344 *
2345 * @param[in] ctx libyang context for logging.
2346 * @param[in,out] data Data to read from, always moved to currently handled character.
2347 * @param[in,out] patterns Restrictions to add to.
2348 *
2349 * @return LY_ERR values.
2350 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002351static LY_ERR
2352parse_type_pattern(struct ly_ctx *ctx, const char **data, struct lysp_restr **patterns)
2353{
2354 LY_ERR ret = 0;
2355 char *buf, *word;
2356 int word_len;
2357 enum yang_keyword kw;
2358 struct lysp_restr *restr;
2359
2360 LYSP_ARRAY_NEW_RET(ctx, patterns, restr, LY_EMEM);
2361
2362 /* get value */
2363 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002364 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002365
2366 /* add special meaning first byte */
2367 if (buf) {
2368 buf = realloc(buf, word_len + 2);
2369 word = buf;
2370 } else {
2371 buf = malloc(word_len + 2);
2372 }
2373 LY_CHECK_ERR_RET(!buf, LOGMEM(ctx), LY_EMEM);
2374 memmove(buf + 1, word, word_len + 1);
2375 word[0] = 0x06;
2376 restr->arg = lydict_insert_zc(ctx, word);
2377
2378 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002379 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002380
2381 switch (kw) {
2382 case YANG_DESCRIPTION:
2383 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &restr->dsc, Y_STR_ARG, &restr->exts);
2384 break;
2385 case YANG_REFERENCE:
2386 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &restr->ref, Y_STR_ARG, &restr->exts);
2387 break;
2388 case YANG_ERROR_APP_TAG:
2389 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRTAG, 0, &restr->eapptag, Y_STR_ARG, &restr->exts);
2390 break;
2391 case YANG_ERROR_MESSAGE:
2392 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ERRMSG, 0, &restr->emsg, Y_STR_ARG, &restr->exts);
2393 break;
2394 case YANG_MODIFIER:
2395 ret = parse_type_pattern_modifier(ctx, data, &restr->arg, &restr->exts);
2396 break;
2397 case YANG_CUSTOM:
2398 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &restr->exts);
2399 break;
2400 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002401 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "pattern");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002402 return LY_EVALID;
2403 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002404 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002405 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002406 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002407
2408 return ret;
2409}
2410
Michal Vaskoea5abea2018-09-18 13:10:54 +02002411/**
2412 * @brief Parse the type statement.
2413 *
2414 * @param[in] ctx libyang context for logging.
2415 * @param[in,out] data Data to read from, always moved to currently handled character.
2416 * @param[in,out] type Type to wrote to.
2417 *
2418 * @return LY_ERR values.
2419 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002420static LY_ERR
2421parse_type(struct ly_ctx *ctx, const char **data, struct lysp_type *type)
2422{
2423 LY_ERR ret = 0;
2424 char *buf, *word;
2425 int word_len;
2426 enum yang_keyword kw;
2427 struct lysp_type *nest_type;
2428
2429 if (type->name) {
2430 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "type");
2431 return LY_EVALID;
2432 }
2433
2434 /* get value */
2435 ret = get_string(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002436 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002437
2438 if (buf) {
2439 type->name = lydict_insert_zc(ctx, word);
2440 } else {
2441 type->name = lydict_insert(ctx, word, word_len);
2442 }
2443
2444 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002445 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002446
2447 switch (kw) {
2448 case YANG_BASE:
2449 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &type->bases, Y_PREF_IDENTIF_ARG, &type->exts);
2450 break;
2451 case YANG_BIT:
2452 ret = parse_type_enum(ctx, data, kw, &type->bits);
2453 break;
2454 case YANG_ENUM:
2455 ret = parse_type_enum(ctx, data, kw, &type->enums);
2456 break;
2457 case YANG_FRACTION_DIGITS:
2458 ret = parse_type_fracdigits(ctx, data, &type->fraction_digits, &type->exts);
2459 break;
2460 case YANG_LENGTH:
2461 if (type->length) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002462 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002463 return LY_EVALID;
2464 }
2465 type->length = calloc(1, sizeof *type->length);
2466 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
2467
2468 ret = parse_restr(ctx, data, kw, type->length);
2469 break;
2470 case YANG_PATH:
2471 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PATH, 0, &type->path, Y_STR_ARG, &type->exts);
2472 break;
2473 case YANG_PATTERN:
2474 ret = parse_type_pattern(ctx, data, &type->patterns);
2475 break;
2476 case YANG_RANGE:
2477 if (type->range) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002478 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02002479 return LY_EVALID;
2480 }
2481 type->range = calloc(1, sizeof *type->range);
2482 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EVALID);
2483
2484 ret = parse_restr(ctx, data, kw, type->range);
2485 break;
2486 case YANG_REQUIRE_INSTANCE:
2487 ret = parse_type_reqinstance(ctx, data, &type->require_instance, &type->flags, &type->exts);
2488 break;
2489 case YANG_TYPE:
2490 {
2491 LYSP_ARRAY_NEW_RET(ctx, &type->types, nest_type, LY_EMEM);
2492 }
2493 ret = parse_type(ctx, data, nest_type);
2494 break;
2495 case YANG_CUSTOM:
2496 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &type->exts);
2497 break;
2498 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002499 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "when");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002500 return LY_EVALID;
2501 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002502 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002503 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002504 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002505
2506 return ret;
2507}
2508
Michal Vaskoea5abea2018-09-18 13:10:54 +02002509/**
2510 * @brief Parse the leaf statement.
2511 *
2512 * @param[in] ctx libyang context for logging.
2513 * @param[in,out] data Data to read from, always moved to currently handled character.
2514 * @param[in,out] siblings Siblings to add to.
2515 *
2516 * @return LY_ERR values.
2517 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002518static LY_ERR
2519parse_leaf(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings)
2520{
2521 LY_ERR ret = 0;
2522 char *buf, *word;
2523 int word_len;
2524 enum yang_keyword kw;
2525 struct lysp_node *iter;
2526 struct lysp_node_leaf *leaf;
2527
2528 /* create structure */
2529 leaf = calloc(1, sizeof *leaf);
2530 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx), LY_EMEM);
2531 leaf->nodetype = LYS_LEAF;
2532
2533 /* insert into siblings */
2534 if (!*siblings) {
2535 *siblings = (struct lysp_node *)leaf;
2536 } else {
2537 for (iter = *siblings; iter->next; iter = iter->next);
2538 iter->next = (struct lysp_node *)leaf;
2539 }
2540
2541 /* get name */
2542 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002543 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002544
2545 if (buf) {
2546 leaf->name = lydict_insert_zc(ctx, word);
2547 } else {
2548 leaf->name = lydict_insert(ctx, word, word_len);
2549 }
2550
2551 /* parse substatements */
2552 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002553 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002554
2555 switch (kw) {
2556 case YANG_CONFIG:
2557 ret = parse_config(ctx, data, &leaf->flags, &leaf->exts);
2558 break;
2559 case YANG_DEFAULT:
2560 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt, Y_STR_ARG, &leaf->exts);
2561 break;
2562 case YANG_DESCRIPTION:
2563 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts);
2564 break;
2565 case YANG_IF_FEATURE:
2566 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &leaf->iffeatures, Y_STR_ARG, &leaf->exts);
2567 break;
2568 case YANG_MANDATORY:
2569 ret = parse_mandatory(ctx, data, &leaf->flags, &leaf->exts);
2570 break;
2571 case YANG_MUST:
2572 ret = parse_restrs(ctx, data, kw, &leaf->musts);
2573 break;
2574 case YANG_REFERENCE:
2575 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &leaf->ref, Y_STR_ARG, &leaf->exts);
2576 break;
2577 case YANG_STATUS:
2578 ret = parse_status(ctx, data, &leaf->flags, &leaf->exts);
2579 break;
2580 case YANG_TYPE:
2581 ret = parse_type(ctx, data, &leaf->type);
2582 break;
2583 case YANG_UNITS:
2584 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &leaf->units, Y_STR_ARG, &leaf->exts);
2585 break;
2586 case YANG_WHEN:
2587 ret = parse_when(ctx, data, &leaf->when);
2588 break;
2589 case YANG_CUSTOM:
2590 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &leaf->exts);
2591 break;
2592 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002593 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "leaf");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002594 return LY_EVALID;
2595 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002596 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002597 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002598 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002599
2600 /* mandatory substatements */
2601 if (!leaf->type.name) {
2602 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "type", "leaf");
2603 return LY_EVALID;
2604 }
2605
2606 return ret;
2607}
2608
Michal Vaskoea5abea2018-09-18 13:10:54 +02002609/**
2610 * @brief Parse the max-elements statement.
2611 *
2612 * @param[in] ctx libyang context for logging.
2613 * @param[in,out] data Data to read from, always moved to currently handled character.
2614 * @param[in,out] max Value to write to.
2615 * @param[in,out] flags Flags to write to.
2616 * @param[in,out] exts Extension instances to add to.
2617 *
2618 * @return LY_ERR values.
2619 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002620static LY_ERR
2621parse_maxelements(struct ly_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
2622{
2623 LY_ERR ret = 0;
2624 char *buf, *word, *ptr;
2625 int word_len;
2626 unsigned long int num;
2627 enum yang_keyword kw;
2628
2629 if (*flags & LYS_SET_MAX) {
2630 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "max-elements");
2631 return LY_EVALID;
2632 }
2633 *flags |= LYS_SET_MAX;
2634
2635 /* get value */
2636 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002637 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002638
2639 if (!word_len || (word[0] == '0') || ((word[0] != 'u') && !isdigit(word[0]))) {
2640 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "max-elements");
2641 free(buf);
2642 return LY_EVALID;
2643 }
2644
2645 if (strncmp(word, "unbounded", word_len)) {
2646 errno = 0;
2647 num = strtoul(word, &ptr, 10);
2648 /* we have not parsed the whole argument */
2649 if (ptr - word != word_len) {
2650 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "max-elements");
2651 free(buf);
2652 return LY_EVALID;
2653 }
2654 if ((errno == ERANGE) || (num > UINT32_MAX)) {
2655 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_OOB, word_len, word, "max-elements");
2656 free(buf);
2657 return LY_EVALID;
2658 }
2659
2660 *max = num;
2661 }
2662 free(buf);
2663
2664 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002665 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002666
2667 switch (kw) {
2668 case YANG_CUSTOM:
2669 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MAX, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002670 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002671 break;
2672 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002673 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "max-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002674 return LY_EVALID;
2675 }
2676 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002677 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002678
2679 return ret;
2680}
2681
Michal Vaskoea5abea2018-09-18 13:10:54 +02002682/**
2683 * @brief Parse the min-elements statement.
2684 *
2685 * @param[in] ctx libyang context for logging.
2686 * @param[in,out] data Data to read from, always moved to currently handled character.
2687 * @param[in,out] min Value to write to.
2688 * @param[in,out] flags Flags to write to.
2689 * @param[in,out] exts Extension instances to add to.
2690 *
2691 * @return LY_ERR values.
2692 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002693static LY_ERR
2694parse_minelements(struct ly_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
2695{
2696 LY_ERR ret = 0;
2697 char *buf, *word, *ptr;
2698 int word_len;
2699 unsigned long int num;
2700 enum yang_keyword kw;
2701
2702 if (*flags & LYS_SET_MIN) {
2703 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "min-elements");
2704 return LY_EVALID;
2705 }
2706 *flags |= LYS_SET_MIN;
2707
2708 /* get value */
2709 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002710 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002711
2712 if (!word_len || !isdigit(word[0]) || ((word[0] == '0') && (word_len > 1))) {
2713 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "min-elements");
2714 free(buf);
2715 return LY_EVALID;
2716 }
2717
2718 errno = 0;
2719 num = strtoul(word, &ptr, 10);
2720 /* we have not parsed the whole argument */
2721 if (ptr - word != word_len) {
2722 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "min-elements");
2723 free(buf);
2724 return LY_EVALID;
2725 }
2726 if ((errno == ERANGE) || (num > UINT32_MAX)) {
2727 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_OOB, word_len, word, "min-elements");
2728 free(buf);
2729 return LY_EVALID;
2730 }
2731 *min = num;
2732 free(buf);
2733
2734 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002735 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002736
2737 switch (kw) {
2738 case YANG_CUSTOM:
2739 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_MIN, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002740 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002741 break;
2742 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002743 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "min-elements");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002744 return LY_EVALID;
2745 }
2746 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002747 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002748
2749 return ret;
2750}
2751
Michal Vaskoea5abea2018-09-18 13:10:54 +02002752/**
2753 * @brief Parse the ordered-by statement.
2754 *
2755 * @param[in] ctx libyang context for logging.
2756 * @param[in,out] data Data to read from, always moved to currently handled character.
2757 * @param[in,out] flags Flags to write to.
2758 * @param[in,out] exts Extension instances to add to.
2759 *
2760 * @return LY_ERR values.
2761 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002762static LY_ERR
2763parse_orderedby(struct ly_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
2764{
2765 LY_ERR ret = 0;
2766 char *buf, *word;
2767 int word_len;
2768 enum yang_keyword kw;
2769
2770 if (*flags & LYS_ORDBY_MASK) {
2771 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "ordered-by");
2772 return LY_EVALID;
2773 }
2774
2775 /* get value */
2776 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002777 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002778
2779 if ((word_len == 6) && !strncmp(word, "system", word_len)) {
2780 *flags |= LYS_ORDBY_SYSTEM;
2781 } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
2782 *flags |= LYS_ORDBY_USER;
2783 } else {
2784 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "ordered-by");
2785 free(buf);
2786 return LY_EVALID;
2787 }
2788 free(buf);
2789
2790 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002791 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002792
2793 switch (kw) {
2794 case YANG_CUSTOM:
2795 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ORDEREDBY, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02002796 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002797 break;
2798 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002799 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "ordered-by");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002800 return LY_EVALID;
2801 }
2802 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002803 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002804
2805 return ret;
2806}
2807
Michal Vaskoea5abea2018-09-18 13:10:54 +02002808/**
2809 * @brief Parse the leaf-list statement.
2810 *
2811 * @param[in] ctx libyang context for logging.
2812 * @param[in,out] data Data to read from, always moved to currently handled character.
2813 * @param[in,out] siblings Siblings to add to.
2814 *
2815 * @return LY_ERR values.
2816 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002817static LY_ERR
2818parse_leaflist(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings)
2819{
2820 LY_ERR ret = 0;
2821 char *buf, *word;
2822 int word_len;
2823 enum yang_keyword kw;
2824 struct lysp_node *iter;
2825 struct lysp_node_leaflist *llist;
2826
2827 /* create structure */
2828 llist = calloc(1, sizeof *llist);
2829 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx), LY_EMEM);
2830 llist->nodetype = LYS_LEAFLIST;
2831
2832 /* insert into siblings */
2833 if (!*siblings) {
2834 *siblings = (struct lysp_node *)llist;
2835 } else {
2836 for (iter = *siblings; iter->next; iter = iter->next);
2837 iter->next = (struct lysp_node *)llist;
2838 }
2839
2840 /* get name */
2841 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002842 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002843
2844 if (buf) {
2845 llist->name = lydict_insert_zc(ctx, word);
2846 } else {
2847 llist->name = lydict_insert(ctx, word, word_len);
2848 }
2849
2850 /* parse substatements */
2851 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002852 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002853
2854 switch (kw) {
2855 case YANG_CONFIG:
2856 ret = parse_config(ctx, data, &llist->flags, &llist->exts);
2857 break;
2858 case YANG_DEFAULT:
2859 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &llist->dflts, Y_STR_ARG, &llist->exts);
2860 break;
2861 case YANG_DESCRIPTION:
2862 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &llist->dsc, Y_STR_ARG, &llist->exts);
2863 break;
2864 case YANG_IF_FEATURE:
2865 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &llist->iffeatures, Y_STR_ARG, &llist->exts);
2866 break;
2867 case YANG_MAX_ELEMENTS:
2868 ret = parse_maxelements(ctx, data, &llist->max, &llist->flags, &llist->exts);
2869 break;
2870 case YANG_MIN_ELEMENTS:
2871 ret = parse_minelements(ctx, data, &llist->min, &llist->flags, &llist->exts);
2872 break;
2873 case YANG_MUST:
2874 ret = parse_restrs(ctx, data, kw, &llist->musts);
2875 break;
2876 case YANG_ORDERED_BY:
2877 ret = parse_orderedby(ctx, data, &llist->flags, &llist->exts);
2878 break;
2879 case YANG_REFERENCE:
2880 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &llist->ref, Y_STR_ARG, &llist->exts);
2881 break;
2882 case YANG_STATUS:
2883 ret = parse_status(ctx, data, &llist->flags, &llist->exts);
2884 break;
2885 case YANG_TYPE:
2886 ret = parse_type(ctx, data, &llist->type);
2887 break;
2888 case YANG_UNITS:
2889 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &llist->units, Y_STR_ARG, &llist->exts);
2890 break;
2891 case YANG_WHEN:
2892 ret = parse_when(ctx, data, &llist->when);
2893 break;
2894 case YANG_CUSTOM:
2895 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &llist->exts);
2896 break;
2897 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002898 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "llist");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002899 return LY_EVALID;
2900 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002901 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002902 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002903 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002904
2905 /* mandatory substatements */
2906 if (!llist->type.name) {
2907 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "type", "leaf-list");
2908 return LY_EVALID;
2909 }
2910
2911 return ret;
2912}
2913
Michal Vaskoea5abea2018-09-18 13:10:54 +02002914/**
2915 * @brief Parse the refine statement.
2916 *
2917 * @param[in] ctx libyang context for logging.
2918 * @param[in,out] data Data to read from, always moved to currently handled character.
2919 * @param[in,out] refines Refines to add to.
2920 *
2921 * @return LY_ERR values.
2922 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02002923static LY_ERR
2924parse_refine(struct ly_ctx *ctx, const char **data, struct lysp_refine **refines)
2925{
2926 LY_ERR ret = 0;
2927 char *buf, *word;
2928 int word_len;
2929 enum yang_keyword kw;
2930 struct lysp_refine *rf;
2931
2932 LYSP_ARRAY_NEW_RET(ctx, refines, rf, LY_EMEM);
2933
2934 /* get value */
2935 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02002936 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002937
2938 if (buf) {
2939 rf->nodeid = lydict_insert_zc(ctx, word);
2940 } else {
2941 rf->nodeid = lydict_insert(ctx, word, word_len);
2942 }
2943
2944 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02002945 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002946
2947 switch (kw) {
2948 case YANG_CONFIG:
2949 ret = parse_config(ctx, data, &rf->flags, &rf->exts);
2950 break;
2951 case YANG_DEFAULT:
2952 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts);
2953 break;
2954 case YANG_DESCRIPTION:
2955 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts);
2956 break;
2957 case YANG_IF_FEATURE:
2958 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &rf->iffeatures, Y_STR_ARG, &rf->exts);
2959 break;
2960 case YANG_MAX_ELEMENTS:
2961 ret = parse_maxelements(ctx, data, &rf->max, &rf->flags, &rf->exts);
2962 break;
2963 case YANG_MIN_ELEMENTS:
2964 ret = parse_minelements(ctx, data, &rf->min, &rf->flags, &rf->exts);
2965 break;
2966 case YANG_MUST:
2967 ret = parse_restrs(ctx, data, kw, &rf->musts);
2968 break;
2969 case YANG_MANDATORY:
2970 ret = parse_mandatory(ctx, data, &rf->flags, &rf->exts);
2971 break;
2972 case YANG_REFERENCE:
2973 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &rf->ref, Y_STR_ARG, &rf->exts);
2974 break;
2975 case YANG_PRESENCE:
2976 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &rf->presence, Y_STR_ARG, &rf->exts);
2977 break;
2978 case YANG_CUSTOM:
2979 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &rf->exts);
2980 break;
2981 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02002982 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "refine");
Michal Vasko7fbc8162018-09-17 10:35:16 +02002983 return LY_EVALID;
2984 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002985 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002986 }
Radek Krejcic59bc972018-09-17 16:13:06 +02002987 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02002988
2989 return ret;
2990}
2991
Michal Vaskoea5abea2018-09-18 13:10:54 +02002992/**
2993 * @brief Parse the typedef statement.
2994 *
2995 * @param[in] ctx libyang context for logging.
2996 * @param[in,out] data Data to read from, always moved to currently handled character.
2997 * @param[in,out] typedefs Typedefs to add to.
2998 *
2999 * @return LY_ERR values.
3000 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003001static LY_ERR
3002parse_typedef(struct ly_ctx *ctx, const char **data, struct lysp_tpdf **typedefs)
3003{
3004 LY_ERR ret = 0;
3005 char *buf, *word;
3006 int word_len;
3007 enum yang_keyword kw;
3008 struct lysp_tpdf *tpdf;
3009
3010 LYSP_ARRAY_NEW_RET(ctx, typedefs, tpdf, LY_EMEM);
3011
3012 /* get value */
3013 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003014 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003015
3016 if (buf) {
3017 tpdf->name = lydict_insert_zc(ctx, word);
3018 } else {
3019 tpdf->name = lydict_insert(ctx, word, word_len);
3020 }
3021
3022 /* parse substatements */
3023 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003024 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003025
3026 switch (kw) {
3027 case YANG_DEFAULT:
3028 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt, Y_STR_ARG, &tpdf->exts);
3029 break;
3030 case YANG_DESCRIPTION:
3031 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts);
3032 break;
3033 case YANG_REFERENCE:
3034 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &tpdf->ref, Y_STR_ARG, &tpdf->exts);
3035 break;
3036 case YANG_STATUS:
3037 ret = parse_status(ctx, data, &tpdf->flags, &tpdf->exts);
3038 break;
3039 case YANG_TYPE:
3040 ret = parse_type(ctx, data, &tpdf->type);
3041 break;
3042 case YANG_UNITS:
3043 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, &tpdf->units, Y_STR_ARG, &tpdf->exts);
3044 break;
3045 case YANG_CUSTOM:
3046 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &tpdf->exts);
3047 break;
3048 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003049 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "typedef");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003050 return LY_EVALID;
3051 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003052 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003053 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003054 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003055
3056 /* mandatory substatements */
3057 if (!tpdf->type.name) {
3058 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "type", "typedef");
3059 return LY_EVALID;
3060 }
3061
3062 return ret;
3063}
3064
Michal Vaskoea5abea2018-09-18 13:10:54 +02003065/**
3066 * @brief Parse the input or output statement.
3067 *
3068 * @param[in] ctx libyang context for logging.
3069 * @param[in,out] data Data to read from, always moved to currently handled character.
3070 * @param[in] kw Type of this particular keyword
3071 * @param[in,out] inout_p Input/output pointer to write to.
3072 *
3073 * @return LY_ERR values.
3074 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003075static LY_ERR
3076parse_inout(struct ly_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_action_inout **inout_p)
3077{
3078 LY_ERR ret = 0;
3079 char *word;
3080 int word_len;
3081 struct lysp_action_inout *inout;
3082
3083 if (*inout_p) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003084 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02003085 return LY_EVALID;
3086 }
3087
3088 /* create structure */
3089 inout = calloc(1, sizeof *inout);
3090 LY_CHECK_ERR_RET(!inout, LOGMEM(ctx), LY_EMEM);
3091 *inout_p = inout;
3092
3093 /* parse substatements */
3094 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003095 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003096
3097 switch (kw) {
3098 case YANG_ANYDATA:
3099 case YANG_ANYXML:
3100 ret = parse_any(ctx, data, kw, &inout->data);
3101 break;
3102 case YANG_CHOICE:
3103 ret = parse_choice(ctx, data, &inout->data);
3104 break;
3105 case YANG_CONTAINER:
3106 ret = parse_container(ctx, data, &inout->data);
3107 break;
3108 case YANG_LEAF:
3109 ret = parse_leaf(ctx, data, &inout->data);
3110 break;
3111 case YANG_LEAF_LIST:
3112 ret = parse_leaflist(ctx, data, &inout->data);
3113 break;
3114 case YANG_LIST:
3115 ret = parse_list(ctx, data, &inout->data);
3116 break;
3117 case YANG_USES:
3118 ret = parse_uses(ctx, data, &inout->data);
3119 break;
3120
3121 case YANG_TYPEDEF:
3122 ret = parse_typedef(ctx, data, &inout->typedefs);
3123 break;
3124 case YANG_MUST:
3125 ret = parse_restrs(ctx, data, kw, &inout->musts);
3126 break;
3127 case YANG_GROUPING:
3128 ret = parse_grouping(ctx, data, &inout->groupings);
3129 break;
3130 case YANG_CUSTOM:
3131 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &inout->exts);
3132 break;
3133 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003134 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "input/output");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003135 return LY_EVALID;
3136 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003137 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003138 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003139 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003140
3141 return ret;
3142}
3143
Michal Vaskoea5abea2018-09-18 13:10:54 +02003144/**
3145 * @brief Parse the action statement.
3146 *
3147 * @param[in] ctx libyang context for logging.
3148 * @param[in,out] data Data to read from, always moved to currently handled character.
3149 * @param[in,out] actions Actions to add to.
3150 *
3151 * @return LY_ERR values.
3152 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003153static LY_ERR
3154parse_action(struct ly_ctx *ctx, const char **data, struct lysp_action **actions)
3155{
3156 LY_ERR ret = 0;
3157 char *buf, *word;
3158 int word_len;
3159 enum yang_keyword kw;
3160 struct lysp_action *act;
3161
3162 LYSP_ARRAY_NEW_RET(ctx, actions, act, LY_EMEM);
3163
3164 /* get value */
3165 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003166 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003167
3168 if (buf) {
3169 act->name = lydict_insert_zc(ctx, word);
3170 } else {
3171 act->name = lydict_insert(ctx, word, word_len);
3172 }
3173
3174 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003175 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003176
3177 switch (kw) {
3178 case YANG_DESCRIPTION:
3179 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &act->dsc, Y_STR_ARG, &act->exts);
3180 break;
3181 case YANG_IF_FEATURE:
3182 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &act->iffeatures, Y_STR_ARG, &act->exts);
3183 break;
3184 case YANG_REFERENCE:
3185 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &act->ref, Y_STR_ARG, &act->exts);
3186 break;
3187 case YANG_STATUS:
3188 ret = parse_status(ctx, data, &act->flags, &act->exts);
3189 break;
3190
3191 case YANG_INPUT:
3192 ret = parse_inout(ctx, data, kw, &act->input);
3193 break;
3194 case YANG_OUTPUT:
3195 ret = parse_inout(ctx, data, kw, &act->output);
3196 break;
3197
3198 case YANG_TYPEDEF:
3199 ret = parse_typedef(ctx, data, &act->typedefs);
3200 break;
3201 case YANG_GROUPING:
3202 ret = parse_grouping(ctx, data, &act->groupings);
3203 break;
3204 case YANG_CUSTOM:
3205 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &act->exts);
3206 break;
3207 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003208 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "action");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003209 return LY_EVALID;
3210 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003211 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003212 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003213 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003214
3215 return ret;
3216}
3217
Michal Vaskoea5abea2018-09-18 13:10:54 +02003218/**
3219 * @brief Parse the notification statement.
3220 *
3221 * @param[in] ctx libyang context for logging.
3222 * @param[in,out] data Data to read from, always moved to currently handled character.
3223 * @param[in,out] notifs Notifications to add to.
3224 *
3225 * @return LY_ERR values.
3226 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003227static LY_ERR
3228parse_notif(struct ly_ctx *ctx, const char **data, struct lysp_notif **notifs)
3229{
3230 LY_ERR ret = 0;
3231 char *buf, *word;
3232 int word_len;
3233 enum yang_keyword kw;
3234 struct lysp_notif *notif;
3235
3236 LYSP_ARRAY_NEW_RET(ctx, notifs, notif, LY_EMEM);
3237
3238 /* get value */
3239 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003240 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003241
3242 if (buf) {
3243 notif->name = lydict_insert_zc(ctx, word);
3244 } else {
3245 notif->name = lydict_insert(ctx, word, word_len);
3246 }
3247
3248 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003249 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003250
3251 switch (kw) {
3252 case YANG_DESCRIPTION:
3253 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &notif->dsc, Y_STR_ARG, &notif->exts);
3254 break;
3255 case YANG_IF_FEATURE:
3256 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &notif->iffeatures, Y_STR_ARG, &notif->exts);
3257 break;
3258 case YANG_REFERENCE:
3259 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &notif->ref, Y_STR_ARG, &notif->exts);
3260 break;
3261 case YANG_STATUS:
3262 ret = parse_status(ctx, data, &notif->flags, &notif->exts);
3263 break;
3264
3265 case YANG_ANYDATA:
3266 case YANG_ANYXML:
3267 ret = parse_any(ctx, data, kw, &notif->data);
3268 break;
3269 case YANG_CHOICE:
3270 ret = parse_case(ctx, data, &notif->data);
3271 break;
3272 case YANG_CONTAINER:
3273 ret = parse_container(ctx, data, &notif->data);
3274 break;
3275 case YANG_LEAF:
3276 ret = parse_leaf(ctx, data, &notif->data);
3277 break;
3278 case YANG_LEAF_LIST:
3279 ret = parse_leaflist(ctx, data, &notif->data);
3280 break;
3281 case YANG_LIST:
3282 ret = parse_list(ctx, data, &notif->data);
3283 break;
3284 case YANG_USES:
3285 ret = parse_uses(ctx, data, &notif->data);
3286 break;
3287
3288 case YANG_MUST:
3289 ret = parse_restrs(ctx, data, kw, &notif->musts);
3290 break;
3291 case YANG_TYPEDEF:
3292 ret = parse_typedef(ctx, data, &notif->typedefs);
3293 break;
3294 case YANG_GROUPING:
3295 ret = parse_grouping(ctx, data, &notif->groupings);
3296 break;
3297 case YANG_CUSTOM:
3298 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &notif->exts);
3299 break;
3300 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003301 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "notification");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003302 return LY_EVALID;
3303 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003304 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003305 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003306 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003307
3308 return ret;
3309}
3310
Michal Vaskoea5abea2018-09-18 13:10:54 +02003311/**
3312 * @brief Parse the grouping statement.
3313 *
3314 * @param[in] ctx libyang context for logging.
3315 * @param[in,out] data Data to read from, always moved to currently handled character.
3316 * @param[in,out] groupings Groupings to add to.
3317 *
3318 * @return LY_ERR values.
3319 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003320static LY_ERR
3321parse_grouping(struct ly_ctx *ctx, const char **data, struct lysp_grp **groupings)
3322{
3323 LY_ERR ret = 0;
3324 char *buf, *word;
3325 int word_len;
3326 enum yang_keyword kw;
3327 struct lysp_grp *grp;
3328
3329 LYSP_ARRAY_NEW_RET(ctx, groupings, grp, LY_EMEM);
3330
3331 /* get value */
3332 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003333 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003334
3335 if (buf) {
3336 grp->name = lydict_insert_zc(ctx, word);
3337 } else {
3338 grp->name = lydict_insert(ctx, word, word_len);
3339 }
3340
3341 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003342 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003343
3344 switch (kw) {
3345 case YANG_DESCRIPTION:
3346 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &grp->dsc, Y_STR_ARG, &grp->exts);
3347 break;
3348 case YANG_REFERENCE:
3349 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &grp->ref, Y_STR_ARG, &grp->exts);
3350 break;
3351 case YANG_STATUS:
3352 ret = parse_status(ctx, data, &grp->flags, &grp->exts);
3353 break;
3354
3355 case YANG_ANYDATA:
3356 case YANG_ANYXML:
3357 ret = parse_any(ctx, data, kw, &grp->data);
3358 break;
3359 case YANG_CHOICE:
3360 ret = parse_choice(ctx, data, &grp->data);
3361 break;
3362 case YANG_CONTAINER:
3363 ret = parse_container(ctx, data, &grp->data);
3364 break;
3365 case YANG_LEAF:
3366 ret = parse_leaf(ctx, data, &grp->data);
3367 break;
3368 case YANG_LEAF_LIST:
3369 ret = parse_leaflist(ctx, data, &grp->data);
3370 break;
3371 case YANG_LIST:
3372 ret = parse_list(ctx, data, &grp->data);
3373 break;
3374 case YANG_USES:
3375 ret = parse_uses(ctx, data, &grp->data);
3376 break;
3377
3378 case YANG_TYPEDEF:
3379 ret = parse_typedef(ctx, data, &grp->typedefs);
3380 break;
3381 case YANG_ACTION:
3382 ret = parse_action(ctx, data, &grp->actions);
3383 break;
3384 case YANG_GROUPING:
3385 ret = parse_grouping(ctx, data, &grp->groupings);
3386 break;
3387 case YANG_NOTIFICATION:
3388 ret = parse_notif(ctx, data, &grp->notifs);
3389 break;
3390 case YANG_CUSTOM:
3391 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &grp->exts);
3392 break;
3393 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003394 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003395 return LY_EVALID;
3396 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003397 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003398 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003399 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003400
3401 return ret;
3402}
3403
Michal Vaskoea5abea2018-09-18 13:10:54 +02003404/**
3405 * @brief Parse the refine statement.
3406 *
3407 * @param[in] ctx libyang context for logging.
3408 * @param[in,out] data Data to read from, always moved to currently handled character.
3409 * @param[in,out] augments Augments to add to.
3410 *
3411 * @return LY_ERR values.
3412 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003413static LY_ERR
3414parse_augment(struct ly_ctx *ctx, const char **data, struct lysp_augment **augments)
3415{
3416 LY_ERR ret = 0;
3417 char *buf, *word;
3418 int word_len;
3419 enum yang_keyword kw;
3420 struct lysp_augment *aug;
3421
3422 LYSP_ARRAY_NEW_RET(ctx, augments, aug, LY_EMEM);
3423
3424 /* get value */
3425 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003426 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003427
3428 if (buf) {
3429 aug->nodeid = lydict_insert_zc(ctx, word);
3430 } else {
3431 aug->nodeid = lydict_insert(ctx, word, word_len);
3432 }
3433
3434 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003435 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003436
3437 switch (kw) {
3438 case YANG_DESCRIPTION:
3439 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &aug->dsc, Y_STR_ARG, &aug->exts);
3440 break;
3441 case YANG_IF_FEATURE:
3442 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &aug->iffeatures, Y_STR_ARG, &aug->exts);
3443 break;
3444 case YANG_REFERENCE:
3445 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &aug->ref, Y_STR_ARG, &aug->exts);
3446 break;
3447 case YANG_STATUS:
3448 ret = parse_status(ctx, data, &aug->flags, &aug->exts);
3449 break;
3450 case YANG_WHEN:
3451 ret = parse_when(ctx, data, &aug->when);
3452 break;
3453
3454 case YANG_ANYDATA:
3455 case YANG_ANYXML:
3456 ret = parse_any(ctx, data, kw, &aug->child);
3457 break;
3458 case YANG_CASE:
3459 ret = parse_case(ctx, data, &aug->child);
3460 break;
3461 case YANG_CHOICE:
3462 ret = parse_choice(ctx, data, &aug->child);
3463 break;
3464 case YANG_CONTAINER:
3465 ret = parse_container(ctx, data, &aug->child);
3466 break;
3467 case YANG_LEAF:
3468 ret = parse_leaf(ctx, data, &aug->child);
3469 break;
3470 case YANG_LEAF_LIST:
3471 ret = parse_leaflist(ctx, data, &aug->child);
3472 break;
3473 case YANG_LIST:
3474 ret = parse_list(ctx, data, &aug->child);
3475 break;
3476 case YANG_USES:
3477 ret = parse_uses(ctx, data, &aug->child);
3478 break;
3479
3480 case YANG_ACTION:
3481 ret = parse_action(ctx, data, &aug->actions);
3482 break;
3483 case YANG_NOTIFICATION:
3484 ret = parse_notif(ctx, data, &aug->notifs);
3485 break;
3486 case YANG_CUSTOM:
3487 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &aug->exts);
3488 break;
3489 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003490 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "augment");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003491 return LY_EVALID;
3492 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003493 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003494 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003495 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003496
3497 return ret;
3498}
3499
Michal Vaskoea5abea2018-09-18 13:10:54 +02003500/**
3501 * @brief Parse the uses statement.
3502 *
3503 * @param[in] ctx libyang context for logging.
3504 * @param[in,out] data Data to read from, always moved to currently handled character.
3505 * @param[in,out] siblings Siblings to add to.
3506 *
3507 * @return LY_ERR values.
3508 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003509static LY_ERR
3510parse_uses(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings)
3511{
3512 LY_ERR ret = 0;
3513 char *buf, *word;
3514 int word_len;
3515 enum yang_keyword kw;
3516 struct lysp_node *iter;
3517 struct lysp_node_uses *uses;
3518
3519 /* create structure */
3520 uses = calloc(1, sizeof *uses);
3521 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx), LY_EMEM);
3522 uses->nodetype = LYS_USES;
3523
3524 /* insert into siblings */
3525 if (!*siblings) {
3526 *siblings = (struct lysp_node *)uses;
3527 } else {
3528 for (iter = *siblings; iter->next; iter = iter->next);
3529 iter->next = (struct lysp_node *)uses;
3530 }
3531
3532 /* get name */
3533 ret = get_string(ctx, data, Y_PREF_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003534 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003535
3536 if (buf) {
3537 uses->name = lydict_insert_zc(ctx, word);
3538 } else {
3539 uses->name = lydict_insert(ctx, word, word_len);
3540 }
3541
3542 /* parse substatements */
3543 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003544 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003545
3546 switch (kw) {
3547 case YANG_DESCRIPTION:
3548 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &uses->dsc, Y_STR_ARG, &uses->exts);
3549 break;
3550 case YANG_IF_FEATURE:
3551 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &uses->iffeatures, Y_STR_ARG, &uses->exts);
3552 break;
3553 case YANG_REFERENCE:
3554 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &uses->ref, Y_STR_ARG, &uses->exts);
3555 break;
3556 case YANG_STATUS:
3557 ret = parse_status(ctx, data, &uses->flags, &uses->exts);
3558 break;
3559 case YANG_WHEN:
3560 ret = parse_when(ctx, data, &uses->when);
3561 break;
3562
3563 case YANG_REFINE:
3564 ret = parse_refine(ctx, data, &uses->refines);
3565 break;
3566 case YANG_AUGMENT:
3567 ret = parse_augment(ctx, data, &uses->augments);
3568 break;
3569 case YANG_CUSTOM:
3570 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &uses->exts);
3571 break;
3572 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003573 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "uses");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003574 return LY_EVALID;
3575 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003576 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003577 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003578 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003579
3580 return ret;
3581}
3582
Michal Vaskoea5abea2018-09-18 13:10:54 +02003583/**
3584 * @brief Parse the case statement.
3585 *
3586 * @param[in] ctx libyang context for logging.
3587 * @param[in,out] data Data to read from, always moved to currently handled character.
3588 * @param[in,out] siblings Siblings to add to.
3589 *
3590 * @return LY_ERR values.
3591 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003592static LY_ERR
3593parse_case(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings)
3594{
3595 LY_ERR ret = 0;
3596 char *buf, *word;
3597 int word_len;
3598 enum yang_keyword kw;
3599 struct lysp_node *iter;
3600 struct lysp_node_case *cas;
3601
3602 /* create structure */
3603 cas = calloc(1, sizeof *cas);
3604 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx), LY_EMEM);
3605 cas->nodetype = LYS_CASE;
3606
3607 /* insert into siblings */
3608 if (!*siblings) {
3609 *siblings = (struct lysp_node *)cas;
3610 } else {
3611 for (iter = *siblings; iter->next; iter = iter->next);
3612 iter->next = (struct lysp_node *)cas;
3613 }
3614
3615 /* get name */
3616 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003617 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003618
3619 if (buf) {
3620 cas->name = lydict_insert_zc(ctx, word);
3621 } else {
3622 cas->name = lydict_insert(ctx, word, word_len);
3623 }
3624
3625 /* parse substatements */
3626 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003627 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003628
3629 switch (kw) {
3630 case YANG_DESCRIPTION:
3631 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cas->dsc, Y_STR_ARG, &cas->exts);
3632 break;
3633 case YANG_IF_FEATURE:
3634 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cas->iffeatures, Y_STR_ARG, &cas->exts);
3635 break;
3636 case YANG_REFERENCE:
3637 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cas->ref, Y_STR_ARG, &cas->exts);
3638 break;
3639 case YANG_STATUS:
3640 ret = parse_status(ctx, data, &cas->flags, &cas->exts);
3641 break;
3642 case YANG_WHEN:
3643 ret = parse_when(ctx, data, &cas->when);
3644 break;
3645
3646 case YANG_ANYDATA:
3647 case YANG_ANYXML:
3648 ret = parse_any(ctx, data, kw, &cas->child);
3649 break;
3650 case YANG_CHOICE:
3651 ret = parse_case(ctx, data, &cas->child);
3652 break;
3653 case YANG_CONTAINER:
3654 ret = parse_container(ctx, data, &cas->child);
3655 break;
3656 case YANG_LEAF:
3657 ret = parse_leaf(ctx, data, &cas->child);
3658 break;
3659 case YANG_LEAF_LIST:
3660 ret = parse_leaflist(ctx, data, &cas->child);
3661 break;
3662 case YANG_LIST:
3663 ret = parse_list(ctx, data, &cas->child);
3664 break;
3665 case YANG_USES:
3666 ret = parse_uses(ctx, data, &cas->child);
3667 break;
3668 case YANG_CUSTOM:
3669 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cas->exts);
3670 break;
3671 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003672 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "case");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003673 return LY_EVALID;
3674 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003675 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003676 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003677 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003678
3679 return ret;
3680}
3681
Michal Vaskoea5abea2018-09-18 13:10:54 +02003682/**
3683 * @brief Parse the choice statement.
3684 *
3685 * @param[in] ctx libyang context for logging.
3686 * @param[in,out] data Data to read from, always moved to currently handled character.
3687 * @param[in,out] siblings Siblings to add to.
3688 *
3689 * @return LY_ERR values.
3690 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003691static LY_ERR
3692parse_choice(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings)
3693{
3694 LY_ERR ret = 0;
3695 char *buf, *word;
3696 int word_len;
3697 enum yang_keyword kw;
3698 struct lysp_node *iter;
3699 struct lysp_node_choice *choic;
3700
3701 /* create structure */
3702 choic = calloc(1, sizeof *choic);
3703 LY_CHECK_ERR_RET(!choic, LOGMEM(ctx), LY_EMEM);
3704 choic->nodetype = LYS_CHOICE;
3705
3706 /* insert into siblings */
3707 if (!*siblings) {
3708 *siblings = (struct lysp_node *)choic;
3709 } else {
3710 for (iter = *siblings; iter->next; iter = iter->next);
3711 iter->next = (struct lysp_node *)choic;
3712 }
3713
3714 /* get name */
3715 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003716 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003717
3718 if (buf) {
3719 choic->name = lydict_insert_zc(ctx, word);
3720 } else {
3721 choic->name = lydict_insert(ctx, word, word_len);
3722 }
3723
3724 /* parse substatements */
3725 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003726 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003727
3728 switch (kw) {
3729 case YANG_CONFIG:
3730 ret = parse_config(ctx, data, &choic->flags, &choic->exts);
3731 break;
3732 case YANG_DESCRIPTION:
3733 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &choic->dsc, Y_STR_ARG, &choic->exts);
3734 break;
3735 case YANG_IF_FEATURE:
3736 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &choic->iffeatures, Y_STR_ARG, &choic->exts);
3737 break;
3738 case YANG_MANDATORY:
3739 ret = parse_mandatory(ctx, data, &choic->flags, &choic->exts);
3740 break;
3741 case YANG_REFERENCE:
3742 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &choic->ref, Y_STR_ARG, &choic->exts);
3743 break;
3744 case YANG_STATUS:
3745 ret = parse_status(ctx, data, &choic->flags, &choic->exts);
3746 break;
3747 case YANG_WHEN:
3748 ret = parse_when(ctx, data, &choic->when);
3749 break;
3750 case YANG_DEFAULT:
3751 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &choic->dflt, Y_IDENTIF_ARG, &choic->exts);
3752 break;
3753
3754 case YANG_ANYDATA:
3755 case YANG_ANYXML:
3756 ret = parse_any(ctx, data, kw, &choic->child);
3757 break;
3758 case YANG_CASE:
3759 ret = parse_case(ctx, data, &choic->child);
3760 break;
3761 case YANG_CHOICE:
3762 ret = parse_choice(ctx, data, &choic->child);
3763 break;
3764 case YANG_CONTAINER:
3765 ret = parse_container(ctx, data, &choic->child);
3766 break;
3767 case YANG_LEAF:
3768 ret = parse_leaf(ctx, data, &choic->child);
3769 break;
3770 case YANG_LEAF_LIST:
3771 ret = parse_leaflist(ctx, data, &choic->child);
3772 break;
3773 case YANG_LIST:
3774 ret = parse_list(ctx, data, &choic->child);
3775 break;
3776 case YANG_CUSTOM:
3777 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &choic->exts);
3778 break;
3779 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003780 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "choice");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003781 return LY_EVALID;
3782 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003783 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003784 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003785 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003786
3787 return ret;
3788}
3789
Michal Vaskoea5abea2018-09-18 13:10:54 +02003790/**
3791 * @brief Parse the container statement.
3792 *
3793 * @param[in] ctx libyang context for logging.
3794 * @param[in,out] data Data to read from, always moved to currently handled character.
3795 * @param[in,out] siblings Siblings to add to.
3796 *
3797 * @return LY_ERR values.
3798 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003799static LY_ERR
3800parse_container(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings)
3801{
3802 LY_ERR ret = 0;
3803 char *buf, *word;
3804 int word_len;
3805 enum yang_keyword kw;
3806 struct lysp_node *iter;
3807 struct lysp_node_container *cont;
3808
3809 /* create structure */
3810 cont = calloc(1, sizeof *cont);
3811 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx), LY_EMEM);
3812 cont->nodetype = LYS_CONTAINER;
3813
3814 /* insert into siblings */
3815 if (!*siblings) {
3816 *siblings = (struct lysp_node *)cont;
3817 } else {
3818 for (iter = *siblings; iter->next; iter = iter->next);
3819 iter->next = (struct lysp_node *)cont;
3820 }
3821
3822 /* get name */
3823 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003824 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003825
3826 if (buf) {
3827 cont->name = lydict_insert_zc(ctx, word);
3828 } else {
3829 cont->name = lydict_insert(ctx, word, word_len);
3830 }
3831
3832 /* parse substatements */
3833 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003834 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003835
3836 switch (kw) {
3837 case YANG_CONFIG:
3838 ret = parse_config(ctx, data, &cont->flags, &cont->exts);
3839 break;
3840 case YANG_DESCRIPTION:
3841 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &cont->dsc, Y_STR_ARG, &cont->exts);
3842 break;
3843 case YANG_IF_FEATURE:
3844 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &cont->iffeatures, Y_STR_ARG, &cont->exts);
3845 break;
3846 case YANG_REFERENCE:
3847 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &cont->ref, Y_STR_ARG, &cont->exts);
3848 break;
3849 case YANG_STATUS:
3850 ret = parse_status(ctx, data, &cont->flags, &cont->exts);
3851 break;
3852 case YANG_WHEN:
3853 ret = parse_when(ctx, data, &cont->when);
3854 break;
3855 case YANG_PRESENCE:
3856 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PRESENCE, 0, &cont->presence, Y_STR_ARG, &cont->exts);
3857 break;
3858
3859 case YANG_ANYDATA:
3860 case YANG_ANYXML:
3861 ret = parse_any(ctx, data, kw, &cont->child);
3862 break;
3863 case YANG_CHOICE:
3864 ret = parse_choice(ctx, data, &cont->child);
3865 break;
3866 case YANG_CONTAINER:
3867 ret = parse_container(ctx, data, &cont->child);
3868 break;
3869 case YANG_LEAF:
3870 ret = parse_leaf(ctx, data, &cont->child);
3871 break;
3872 case YANG_LEAF_LIST:
3873 ret = parse_leaflist(ctx, data, &cont->child);
3874 break;
3875 case YANG_LIST:
3876 ret = parse_list(ctx, data, &cont->child);
3877 break;
3878 case YANG_USES:
3879 ret = parse_uses(ctx, data, &cont->child);
3880 break;
3881
3882 case YANG_TYPEDEF:
3883 ret = parse_typedef(ctx, data, &cont->typedefs);
3884 break;
3885 case YANG_MUST:
3886 ret = parse_restrs(ctx, data, kw, &cont->musts);
3887 break;
3888 case YANG_ACTION:
3889 ret = parse_action(ctx, data, &cont->actions);
3890 break;
3891 case YANG_GROUPING:
3892 ret = parse_grouping(ctx, data, &cont->groupings);
3893 break;
3894 case YANG_NOTIFICATION:
3895 ret = parse_notif(ctx, data, &cont->notifs);
3896 break;
3897 case YANG_CUSTOM:
3898 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &cont->exts);
3899 break;
3900 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02003901 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02003902 return LY_EVALID;
3903 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003904 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003905 }
Radek Krejcic59bc972018-09-17 16:13:06 +02003906 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003907
3908 return ret;
3909}
3910
Michal Vaskoea5abea2018-09-18 13:10:54 +02003911/**
3912 * @brief Parse the list statement.
3913 *
3914 * @param[in] ctx libyang context for logging.
3915 * @param[in,out] data Data to read from, always moved to currently handled character.
3916 * @param[in,out] siblings Siblings to add to.
3917 *
3918 * @return LY_ERR values.
3919 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02003920static LY_ERR
3921parse_list(struct ly_ctx *ctx, const char **data, struct lysp_node **siblings)
3922{
3923 LY_ERR ret = 0;
3924 char *buf, *word;
3925 int word_len;
3926 enum yang_keyword kw;
3927 struct lysp_node *iter;
3928 struct lysp_node_list *list;
3929
3930 /* create structure */
3931 list = calloc(1, sizeof *list);
3932 LY_CHECK_ERR_RET(!list, LOGMEM(ctx), LY_EMEM);
3933 list->nodetype = LYS_LIST;
3934
3935 /* insert into siblings */
3936 if (!*siblings) {
3937 *siblings = (struct lysp_node *)list;
3938 } else {
3939 for (iter = *siblings; iter->next; iter = iter->next);
3940 iter->next = (struct lysp_node *)list;
3941 }
3942
3943 /* get name */
3944 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02003945 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003946
3947 if (buf) {
3948 list->name = lydict_insert_zc(ctx, word);
3949 } else {
3950 list->name = lydict_insert(ctx, word, word_len);
3951 }
3952
3953 /* parse substatements */
3954 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02003955 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02003956
3957 switch (kw) {
3958 case YANG_CONFIG:
3959 ret = parse_config(ctx, data, &list->flags, &list->exts);
3960 break;
3961 case YANG_DESCRIPTION:
3962 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &list->dsc, Y_STR_ARG, &list->exts);
3963 break;
3964 case YANG_IF_FEATURE:
3965 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &list->iffeatures, Y_STR_ARG, &list->exts);
3966 break;
3967 case YANG_REFERENCE:
3968 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &list->ref, Y_STR_ARG, &list->exts);
3969 break;
3970 case YANG_STATUS:
3971 ret = parse_status(ctx, data, &list->flags, &list->exts);
3972 break;
3973 case YANG_WHEN:
3974 ret = parse_when(ctx, data, &list->when);
3975 break;
3976 case YANG_KEY:
3977 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_KEY, 0, &list->key, Y_STR_ARG, &list->exts);
3978 break;
3979 case YANG_MAX_ELEMENTS:
3980 ret = parse_maxelements(ctx, data, &list->max, &list->flags, &list->exts);
3981 break;
3982 case YANG_MIN_ELEMENTS:
3983 ret = parse_minelements(ctx, data, &list->min, &list->flags, &list->exts);
3984 break;
3985 case YANG_ORDERED_BY:
3986 ret = parse_orderedby(ctx, data, &list->flags, &list->exts);
3987 break;
3988 case YANG_UNIQUE:
3989 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, &list->uniques, Y_STR_ARG, &list->exts);
3990 break;
3991
3992 case YANG_ANYDATA:
3993 case YANG_ANYXML:
3994 ret = parse_any(ctx, data, kw, &list->child);
3995 break;
3996 case YANG_CHOICE:
3997 ret = parse_choice(ctx, data, &list->child);
3998 break;
3999 case YANG_CONTAINER:
4000 ret = parse_container(ctx, data, &list->child);
4001 break;
4002 case YANG_LEAF:
4003 ret = parse_leaf(ctx, data, &list->child);
4004 break;
4005 case YANG_LEAF_LIST:
4006 ret = parse_leaflist(ctx, data, &list->child);
4007 break;
4008 case YANG_LIST:
4009 ret = parse_list(ctx, data, &list->child);
4010 break;
4011 case YANG_USES:
4012 ret = parse_uses(ctx, data, &list->child);
4013 break;
4014
4015 case YANG_TYPEDEF:
4016 ret = parse_typedef(ctx, data, &list->typedefs);
4017 break;
4018 case YANG_MUST:
4019 ret = parse_restrs(ctx, data, kw, &list->musts);
4020 break;
4021 case YANG_ACTION:
4022 ret = parse_action(ctx, data, &list->actions);
4023 break;
4024 case YANG_GROUPING:
4025 ret = parse_grouping(ctx, data, &list->groupings);
4026 break;
4027 case YANG_NOTIFICATION:
4028 ret = parse_notif(ctx, data, &list->notifs);
4029 break;
4030 case YANG_CUSTOM:
4031 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &list->exts);
4032 break;
4033 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004034 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "container");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004035 return LY_EVALID;
4036 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004037 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004038 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004039 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004040
4041 return ret;
4042}
4043
Michal Vaskoea5abea2018-09-18 13:10:54 +02004044/**
4045 * @brief Parse the yin-element statement.
4046 *
4047 * @param[in] ctx libyang context for logging.
4048 * @param[in,out] data Data to read from, always moved to currently handled character.
4049 * @param[in,out] flags Flags to write to.
4050 * @param[in,out] exts Extension instances to add to.
4051 *
4052 * @return LY_ERR values.
4053 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004054static LY_ERR
4055parse_yinelement(struct ly_ctx *ctx, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
4056{
4057 LY_ERR ret = 0;
4058 char *buf, *word;
4059 int word_len;
4060 enum yang_keyword kw;
4061
4062 if (*flags & LYS_YINELEM_MASK) {
4063 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "yin-element");
4064 return LY_EVALID;
4065 }
4066
4067 /* get value */
4068 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004069 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004070
4071 if ((word_len == 4) && !strncmp(word, "true", word_len)) {
4072 *flags |= LYS_YINELEM_TRUE;
4073 } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
4074 *flags |= LYS_YINELEM_FALSE;
4075 } else {
4076 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "yin-element");
4077 free(buf);
4078 return LY_EVALID;
4079 }
4080 free(buf);
4081
4082 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004083 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004084
4085 switch (kw) {
4086 case YANG_CUSTOM:
4087 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_YINELEM, 0, exts);
Radek Krejcic59bc972018-09-17 16:13:06 +02004088 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004089 break;
4090 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004091 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "yin-element");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004092 return LY_EVALID;
4093 }
4094 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004095 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004096
4097 return ret;
4098}
4099
Michal Vaskoea5abea2018-09-18 13:10:54 +02004100/**
4101 * @brief Parse the yin-element statement.
4102 *
4103 * @param[in] ctx libyang context for logging.
4104 * @param[in,out] data Data to read from, always moved to currently handled character.
4105 * @param[in,out] argument Value to write to.
4106 * @param[in,out] flags Flags to write to.
4107 * @param[in,out] exts Extension instances to add to.
4108 *
4109 * @return LY_ERR values.
4110 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004111static LY_ERR
4112parse_argument(struct ly_ctx *ctx, const char **data, const char **argument, uint16_t *flags, struct lysp_ext_instance **exts)
4113{
4114 LY_ERR ret = 0;
4115 char *buf, *word;
4116 int word_len;
4117 enum yang_keyword kw;
4118
4119 if (*argument) {
4120 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_DUPSTMT, "argument");
4121 return LY_EVALID;
4122 }
4123
4124 /* get value */
4125 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004126 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004127
4128 if (buf) {
4129 *argument = lydict_insert_zc(ctx, word);
4130 } else {
4131 *argument = lydict_insert(ctx, word, word_len);
4132 }
4133
4134 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004135 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004136
4137 switch (kw) {
4138 case YANG_YIN_ELEMENT:
4139 ret = parse_yinelement(ctx, data, flags, exts);
4140 break;
4141 case YANG_CUSTOM:
4142 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_ARGUMENT, 0, exts);
4143 break;
4144 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004145 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "argument");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004146 return LY_EVALID;
4147 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004148 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004149 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004150 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004151
4152 return ret;
4153}
4154
Michal Vaskoea5abea2018-09-18 13:10:54 +02004155/**
4156 * @brief Parse the extension statement.
4157 *
4158 * @param[in] ctx libyang context for logging.
4159 * @param[in,out] data Data to read from, always moved to currently handled character.
4160 * @param[in,out] extensions Extensions to add to.
4161 *
4162 * @return LY_ERR values.
4163 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004164static LY_ERR
4165parse_extension(struct ly_ctx *ctx, const char **data, struct lysp_ext **extensions)
4166{
4167 LY_ERR ret = 0;
4168 char *buf, *word;
4169 int word_len;
4170 enum yang_keyword kw;
4171 struct lysp_ext *ex;
4172
4173 LYSP_ARRAY_NEW_RET(ctx, extensions, ex, LY_EMEM);
4174
4175 /* get value */
4176 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004177 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004178
4179 if (buf) {
4180 ex->name = lydict_insert_zc(ctx, word);
4181 } else {
4182 ex->name = lydict_insert(ctx, word, word_len);
4183 }
4184
4185 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004186 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004187
4188 switch (kw) {
4189 case YANG_DESCRIPTION:
4190 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ex->dsc, Y_STR_ARG, &ex->exts);
4191 break;
4192 case YANG_REFERENCE:
4193 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ex->ref, Y_STR_ARG, &ex->exts);
4194 break;
4195 case YANG_STATUS:
4196 ret = parse_status(ctx, data, &ex->flags, &ex->exts);
4197 break;
4198 case YANG_ARGUMENT:
4199 ret = parse_argument(ctx, data, &ex->argument, &ex->flags, &ex->exts);
4200 break;
4201 case YANG_CUSTOM:
4202 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ex->exts);
4203 break;
4204 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004205 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "extension");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004206 return LY_EVALID;
4207 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004208 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004209 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004210 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004211
4212 return ret;
4213}
4214
Michal Vaskoea5abea2018-09-18 13:10:54 +02004215/**
4216 * @brief Parse the deviate statement.
4217 *
4218 * @param[in] ctx libyang context for logging.
4219 * @param[in,out] data Data to read from, always moved to currently handled character.
4220 * @param[in,out] deviates Deviates to add to.
4221 *
4222 * @return LY_ERR values.
4223 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004224static LY_ERR
4225parse_deviate(struct ly_ctx *ctx, const char **data, struct lysp_deviate **deviates)
4226{
4227 LY_ERR ret = 0;
4228 char *buf, *word;
4229 int word_len, dev_mod;
4230 enum yang_keyword kw;
4231 struct lysp_deviate *iter, *d;
4232 struct lysp_deviate_add *d_add = NULL;
4233 struct lysp_deviate_rpl *d_rpl = NULL;
4234 struct lysp_deviate_del *d_del = NULL;
4235 const char **d_units, ***d_uniques, ***d_dflts;
4236 struct lysp_restr **d_musts;
4237 uint16_t *d_flags;
4238 uint32_t *d_min, *d_max;
4239
4240 /* get value */
4241 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004242 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004243
4244 if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
4245 dev_mod = LYS_DEV_NOT_SUPPORTED;
4246 } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
4247 dev_mod = LYS_DEV_ADD;
4248 } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
4249 dev_mod = LYS_DEV_REPLACE;
4250 } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
4251 dev_mod = LYS_DEV_DELETE;
4252 } else {
4253 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, word_len, word, "deviate");
4254 free(buf);
4255 return LY_EVALID;
4256 }
4257 free(buf);
4258
4259 /* create structure */
4260 switch (dev_mod) {
4261 case LYS_DEV_NOT_SUPPORTED:
4262 d = calloc(1, sizeof *d);
4263 LY_CHECK_ERR_RET(!d, LOGMEM(ctx), LY_EMEM);
4264 break;
4265 case LYS_DEV_ADD:
4266 d_add = calloc(1, sizeof *d_add);
4267 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx), LY_EMEM);
4268 d = (struct lysp_deviate *)d_add;
4269 d_units = &d_add->units;
4270 d_uniques = &d_add->uniques;
4271 d_dflts = &d_add->dflts;
4272 d_musts = &d_add->musts;
4273 d_flags = &d_add->flags;
4274 d_min = &d_add->min;
4275 d_max = &d_add->max;
4276 break;
4277 case LYS_DEV_REPLACE:
4278 d_rpl = calloc(1, sizeof *d_rpl);
4279 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx), LY_EMEM);
4280 d = (struct lysp_deviate *)d_rpl;
4281 d_units = &d_rpl->units;
4282 d_flags = &d_rpl->flags;
4283 d_min = &d_rpl->min;
4284 d_max = &d_rpl->max;
4285 break;
4286 case LYS_DEV_DELETE:
4287 d_del = calloc(1, sizeof *d_del);
4288 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx), LY_EMEM);
4289 d = (struct lysp_deviate *)d_del;
4290 d_units = &d_del->units;
4291 d_uniques = &d_del->uniques;
4292 d_dflts = &d_del->dflts;
4293 d_musts = &d_del->musts;
4294 d_flags = &d_del->flags;
Michal Vasko7fbc8162018-09-17 10:35:16 +02004295 break;
4296 default:
4297 assert(0);
4298 LOGINT_RET(ctx);
4299 }
4300 d->mod = dev_mod;
4301
4302 /* insert into siblings */
4303 if (!*deviates) {
4304 *deviates = d;
4305 } else {
4306 for (iter = *deviates; iter->next; iter = iter->next);
4307 iter->next = d;
4308 }
4309
4310 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004311 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004312
4313 switch (kw) {
4314 case YANG_CONFIG:
4315 switch (dev_mod) {
4316 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004317 case LYS_DEV_DELETE:
Radek Krejcic59bc972018-09-17 16:13:06 +02004318 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004319 return LY_EVALID;
4320 default:
4321 ret = parse_config(ctx, data, d_flags, &d->exts);
4322 break;
4323 }
4324 break;
4325 case YANG_DEFAULT:
4326 switch (dev_mod) {
4327 case LYS_DEV_NOT_SUPPORTED:
Radek Krejcic59bc972018-09-17 16:13:06 +02004328 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004329 return LY_EVALID;
4330 case LYS_DEV_REPLACE:
4331 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts);
4332 break;
4333 default:
4334 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts);
4335 break;
4336 }
4337 break;
4338 case YANG_MANDATORY:
4339 switch (dev_mod) {
4340 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004341 case LYS_DEV_DELETE:
Radek Krejcic59bc972018-09-17 16:13:06 +02004342 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004343 return LY_EVALID;
4344 default:
4345 ret = parse_mandatory(ctx, data, d_flags, &d->exts);
4346 break;
4347 }
4348 break;
4349 case YANG_MAX_ELEMENTS:
4350 switch (dev_mod) {
4351 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004352 case LYS_DEV_DELETE:
Radek Krejcic59bc972018-09-17 16:13:06 +02004353 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004354 return LY_EVALID;
4355 default:
4356 ret = parse_maxelements(ctx, data, d_max, d_flags, &d->exts);
4357 break;
4358 }
4359 break;
4360 case YANG_MIN_ELEMENTS:
4361 switch (dev_mod) {
4362 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004363 case LYS_DEV_DELETE:
Radek Krejcic59bc972018-09-17 16:13:06 +02004364 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004365 return LY_EVALID;
4366 default:
4367 ret = parse_minelements(ctx, data, d_min, d_flags, &d->exts);
4368 break;
4369 }
4370 break;
4371 case YANG_MUST:
4372 switch (dev_mod) {
4373 case LYS_DEV_NOT_SUPPORTED:
Michal Vasko492bec72018-09-18 13:11:10 +02004374 case LYS_DEV_REPLACE:
Radek Krejcic59bc972018-09-17 16:13:06 +02004375 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004376 return LY_EVALID;
4377 default:
4378 ret = parse_restrs(ctx, data, kw, d_musts);
4379 break;
4380 }
4381 break;
4382 case YANG_TYPE:
4383 switch (dev_mod) {
4384 case LYS_DEV_NOT_SUPPORTED:
4385 case LYS_DEV_ADD:
4386 case LYS_DEV_DELETE:
Radek Krejcic59bc972018-09-17 16:13:06 +02004387 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004388 return LY_EVALID;
4389 default:
4390 d_rpl->type = calloc(1, sizeof *d_rpl->type);
4391 LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx), LY_EMEM);
4392 ret = parse_type(ctx, data, d_rpl->type);
4393 break;
4394 }
4395 break;
4396 case YANG_UNIQUE:
4397 switch (dev_mod) {
4398 case LYS_DEV_NOT_SUPPORTED:
4399 case LYS_DEV_REPLACE:
Radek Krejcic59bc972018-09-17 16:13:06 +02004400 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004401 return LY_EVALID;
4402 default:
4403 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts);
4404 break;
4405 }
4406 break;
4407 case YANG_UNITS:
4408 switch (dev_mod) {
4409 case LYS_DEV_NOT_SUPPORTED:
Radek Krejcic59bc972018-09-17 16:13:06 +02004410 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004411 return LY_EVALID;
4412 default:
4413 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_UNITS, 0, d_units, Y_STR_ARG, &d->exts);
4414 break;
4415 }
4416 break;
4417 case YANG_CUSTOM:
4418 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &d->exts);
4419 break;
4420 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004421 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviate");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004422 return LY_EVALID;
4423 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004424 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004425 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004426 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004427
4428 return ret;
4429}
4430
Michal Vaskoea5abea2018-09-18 13:10:54 +02004431/**
4432 * @brief Parse the deviation statement.
4433 *
4434 * @param[in] ctx libyang context for logging.
4435 * @param[in,out] data Data to read from, always moved to currently handled character.
4436 * @param[in,out] deviations Deviations to add to.
4437 *
4438 * @return LY_ERR values.
4439 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004440static LY_ERR
4441parse_deviation(struct ly_ctx *ctx, const char **data, struct lysp_deviation **deviations)
4442{
4443 LY_ERR ret = 0;
4444 char *buf, *word;
4445 int word_len;
4446 enum yang_keyword kw;
4447 struct lysp_deviation *dev;
4448
4449 LYSP_ARRAY_NEW_RET(ctx, deviations, dev, LY_EMEM);
4450
4451 /* get value */
4452 ret = get_string(ctx, data, Y_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004453 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004454
4455 if (buf) {
4456 dev->nodeid = lydict_insert_zc(ctx, word);
4457 } else {
4458 dev->nodeid = lydict_insert(ctx, word, word_len);
4459 }
4460
4461 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004462 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004463
4464 switch (kw) {
4465 case YANG_DESCRIPTION:
4466 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &dev->dsc, Y_STR_ARG, &dev->exts);
4467 break;
4468 case YANG_DEVIATE:
4469 ret = parse_deviate(ctx, data, &dev->deviates);
4470 break;
4471 case YANG_REFERENCE:
4472 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &dev->ref, Y_STR_ARG, &dev->exts);
4473 break;
4474 case YANG_CUSTOM:
4475 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &dev->exts);
4476 break;
4477 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004478 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "deviation");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004479 return LY_EVALID;
4480 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004481 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004482 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004483 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004484
4485 /* mandatory substatements */
4486 if (!dev->deviates) {
4487 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "deviate", "deviation");
4488 return LY_EVALID;
4489 }
4490
4491 return ret;
4492}
4493
Michal Vaskoea5abea2018-09-18 13:10:54 +02004494/**
4495 * @brief Parse the feature statement.
4496 *
4497 * @param[in] ctx libyang context for logging.
4498 * @param[in,out] data Data to read from, always moved to currently handled character.
4499 * @param[in,out] features Features to add to.
4500 *
4501 * @return LY_ERR values.
4502 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004503static LY_ERR
4504parse_feature(struct ly_ctx *ctx, const char **data, struct lysp_feature **features)
4505{
4506 LY_ERR ret = 0;
4507 char *buf, *word;
4508 int word_len;
4509 enum yang_keyword kw;
4510 struct lysp_feature *feat;
4511
4512 LYSP_ARRAY_NEW_RET(ctx, features, feat, LY_EMEM);
4513
4514 /* get value */
4515 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004516 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004517
4518 if (buf) {
4519 feat->name = lydict_insert_zc(ctx, word);
4520 } else {
4521 feat->name = lydict_insert(ctx, word, word_len);
4522 }
4523
4524 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004525 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004526
4527 switch (kw) {
4528 case YANG_DESCRIPTION:
4529 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &feat->dsc, Y_STR_ARG, &feat->exts);
4530 break;
4531 case YANG_IF_FEATURE:
4532 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &feat->iffeatures, Y_STR_ARG, &feat->exts);
4533 break;
4534 case YANG_REFERENCE:
4535 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &feat->ref, Y_STR_ARG, &feat->exts);
4536 break;
4537 case YANG_STATUS:
4538 ret = parse_status(ctx, data, &feat->flags, &feat->exts);
4539 break;
4540 case YANG_CUSTOM:
4541 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &feat->exts);
4542 break;
4543 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004544 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "feature");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004545 return LY_EMEM;
4546 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004547 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004548 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004549 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004550
4551 return ret;
4552}
4553
Michal Vaskoea5abea2018-09-18 13:10:54 +02004554/**
4555 * @brief Parse the identity statement.
4556 *
4557 * @param[in] ctx libyang context for logging.
4558 * @param[in,out] data Data to read from, always moved to currently handled character.
4559 * @param[in,out] identities Identities to add to.
4560 *
4561 * @return LY_ERR values.
4562 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004563static LY_ERR
4564parse_identity(struct ly_ctx *ctx, const char **data, struct lysp_ident **identities)
4565{
4566 LY_ERR ret = 0;
4567 char *buf, *word;
4568 int word_len;
4569 enum yang_keyword kw;
4570 struct lysp_ident *ident;
4571
4572 LYSP_ARRAY_NEW_RET(ctx, identities, ident, LY_EMEM);
4573
4574 /* get value */
4575 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004576 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004577
4578 if (buf) {
4579 ident->name = lydict_insert_zc(ctx, word);
4580 } else {
4581 ident->name = lydict_insert(ctx, word, word_len);
4582 }
4583
4584 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004585 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004586
4587 switch (kw) {
4588 case YANG_DESCRIPTION:
4589 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &ident->dsc, Y_STR_ARG, &ident->exts);
4590 break;
4591 case YANG_IF_FEATURE:
4592 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_IFFEATURE, &ident->iffeatures, Y_STR_ARG, &ident->exts);
4593 break;
4594 case YANG_REFERENCE:
4595 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &ident->ref, Y_STR_ARG, &ident->exts);
4596 break;
4597 case YANG_STATUS:
4598 ret = parse_status(ctx, data, &ident->flags, &ident->exts);
4599 break;
4600 case YANG_BASE:
4601 ret = parse_text_fields(ctx, data, LYEXT_SUBSTMT_BASE, &ident->bases, Y_PREF_IDENTIF_ARG, &ident->exts);
4602 break;
4603 case YANG_CUSTOM:
4604 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &ident->exts);
4605 break;
4606 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004607 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "identity");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004608 return LY_EVALID;
4609 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004610 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004611 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004612 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004613
4614 return ret;
4615}
4616
Michal Vaskoea5abea2018-09-18 13:10:54 +02004617/**
4618 * @brief Parse the module or submodule statement.
4619 *
4620 * @param[in] ctx libyang context for logging.
4621 * @param[in,out] data Data to read from, always moved to currently handled character.
4622 * @param[in,out] mod Module to write to.
4623 *
4624 * @return LY_ERR values.
4625 */
Michal Vasko7fbc8162018-09-17 10:35:16 +02004626static LY_ERR
4627parse_sub_module(struct ly_ctx *ctx, const char **data, struct lysp_module *mod)
4628{
4629 LY_ERR ret = 0;
4630 char *buf, *word;
4631 int word_len;
4632 enum yang_keyword kw, prev_kw = 0;
4633 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
4634
4635 /* (sub)module name */
4636 ret = get_string(ctx, data, Y_IDENTIF_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004637 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004638
4639 if (buf) {
4640 mod->name = lydict_insert_zc(ctx, word);
4641 } else {
4642 mod->name = lydict_insert(ctx, word, word_len);
4643 }
4644
4645 YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004646 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004647
4648 switch (kw) {
4649 /* module header */
4650 case YANG_NAMESPACE:
4651 case YANG_PREFIX:
4652 if (mod->submodule) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004653 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004654 return LY_EVALID;
4655 }
4656 /* fallthrough */
4657 case YANG_BELONGS_TO:
4658 if ((kw == YANG_BELONGS_TO) && !mod->submodule) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004659 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004660 return LY_EVALID;
4661 }
4662 /* fallthrough */
4663 case YANG_YANG_VERSION:
4664 if (mod_stmt > Y_MOD_MODULE_HEADER) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004665 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004666 return LY_EVALID;
4667 }
4668 break;
4669 /* linkage */
4670 case YANG_INCLUDE:
4671 case YANG_IMPORT:
4672 if (mod_stmt > Y_MOD_LINKAGE) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004673 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004674 return LY_EVALID;
4675 }
4676 mod_stmt = Y_MOD_LINKAGE;
4677 break;
4678 /* meta */
4679 case YANG_ORGANIZATION:
4680 case YANG_CONTACT:
4681 case YANG_DESCRIPTION:
4682 case YANG_REFERENCE:
4683 if (mod_stmt > Y_MOD_META) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004684 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004685 return LY_EVALID;
4686 }
4687 mod_stmt = Y_MOD_META;
4688 break;
4689
4690 /* revision */
4691 case YANG_REVISION:
4692 if (mod_stmt > Y_MOD_REVISION) {
Radek Krejcic59bc972018-09-17 16:13:06 +02004693 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004694 return LY_EVALID;
4695 }
4696 mod_stmt = Y_MOD_REVISION;
4697 break;
4698
4699 /* body */
4700 case YANG_ANYDATA:
4701 case YANG_ANYXML:
4702 case YANG_AUGMENT:
4703 case YANG_CHOICE:
4704 case YANG_CONTAINER:
4705 case YANG_DEVIATION:
4706 case YANG_EXTENSION:
4707 case YANG_FEATURE:
4708 case YANG_GROUPING:
4709 case YANG_IDENTITY:
4710 case YANG_LEAF:
4711 case YANG_LEAF_LIST:
4712 case YANG_LIST:
4713 case YANG_NOTIFICATION:
4714 case YANG_RPC:
4715 case YANG_TYPEDEF:
4716 case YANG_USES:
4717 case YANG_CUSTOM:
4718 mod_stmt = Y_MOD_BODY;
4719 break;
4720 default:
4721 /* error handled in the next switch */
4722 break;
4723 }
4724 prev_kw = kw;
4725
4726 switch (kw) {
4727 /* module header */
4728 case YANG_YANG_VERSION:
4729 ret = parse_yangversion(ctx, data, mod);
4730 break;
4731 case YANG_NAMESPACE:
4732 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->ns, Y_STR_ARG, &mod->exts);
4733 break;
4734 case YANG_PREFIX:
4735 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_PREFIX, 0, &mod->prefix, Y_IDENTIF_ARG, &mod->exts);
4736 break;
4737 case YANG_BELONGS_TO:
4738 ret = parse_belongsto(ctx, data, &mod->belongsto, &mod->prefix, &mod->exts);
4739 break;
4740
4741 /* linkage */
4742 case YANG_INCLUDE:
4743 ret = parse_include(ctx, data, &mod->includes);
4744 break;
4745 case YANG_IMPORT:
4746 ret = parse_import(ctx, data, &mod->imports);
4747 break;
4748
4749 /* meta */
4750 case YANG_ORGANIZATION:
4751 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_ORGANIZATION, 0, &mod->org, Y_STR_ARG, &mod->exts);
4752 break;
4753 case YANG_CONTACT:
4754 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_CONTACT, 0, &mod->contact, Y_STR_ARG, &mod->exts);
4755 break;
4756 case YANG_DESCRIPTION:
4757 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_DESCRIPTION, 0, &mod->dsc, Y_STR_ARG, &mod->exts);
4758 break;
4759 case YANG_REFERENCE:
4760 ret = parse_text_field(ctx, data, LYEXT_SUBSTMT_REFERENCE, 0, &mod->ref, Y_STR_ARG, &mod->exts);
4761 break;
4762
4763 /* revision */
4764 case YANG_REVISION:
4765 ret = parse_revision(ctx, data, &mod->revs);
4766 break;
4767
4768 /* body */
4769 case YANG_ANYDATA:
4770 case YANG_ANYXML:
4771 ret = parse_any(ctx, data, kw, &mod->data);
4772 break;
4773 case YANG_CHOICE:
4774 ret = parse_choice(ctx, data, &mod->data);
4775 break;
4776 case YANG_CONTAINER:
4777 ret = parse_container(ctx, data, &mod->data);
4778 break;
4779 case YANG_LEAF:
4780 ret = parse_leaf(ctx, data, &mod->data);
4781 break;
4782 case YANG_LEAF_LIST:
4783 ret = parse_leaflist(ctx, data, &mod->data);
4784 break;
4785 case YANG_LIST:
4786 ret = parse_list(ctx, data, &mod->data);
4787 break;
4788 case YANG_USES:
4789 ret = parse_uses(ctx, data, &mod->data);
4790 break;
4791
4792 case YANG_AUGMENT:
4793 ret = parse_augment(ctx, data, &mod->augments);
4794 break;
4795 case YANG_DEVIATION:
4796 ret = parse_deviation(ctx, data, &mod->deviations);
4797 break;
4798 case YANG_EXTENSION:
4799 ret = parse_extension(ctx, data, &mod->extensions);
4800 break;
4801 case YANG_FEATURE:
4802 ret = parse_feature(ctx, data, &mod->features);
4803 break;
4804 case YANG_GROUPING:
4805 ret = parse_grouping(ctx, data, &mod->groupings);
4806 break;
4807 case YANG_IDENTITY:
4808 ret = parse_identity(ctx, data, &mod->identities);
4809 break;
4810 case YANG_NOTIFICATION:
4811 ret = parse_notif(ctx, data, &mod->notifs);
4812 break;
4813 case YANG_RPC:
4814 ret = parse_action(ctx, data, &mod->rpcs);
4815 break;
4816 case YANG_TYPEDEF:
4817 ret = parse_typedef(ctx, data, &mod->typedefs);
4818 break;
4819 case YANG_CUSTOM:
4820 ret = parse_ext(ctx, data, word, word_len, LYEXT_SUBSTMT_SELF, 0, &mod->exts);
4821 break;
4822
4823 default:
Radek Krejcic59bc972018-09-17 16:13:06 +02004824 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), mod->submodule ? "submodule" : "module");
Michal Vasko7fbc8162018-09-17 10:35:16 +02004825 return LY_EVALID;
4826 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004827 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004828 }
Radek Krejcic59bc972018-09-17 16:13:06 +02004829 LY_CHECK_RET(ret);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004830
4831 /* mandatory substatements */
4832 if (mod->submodule) {
4833 if (!mod->belongsto) {
4834 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "belongs-to", "submodule");
4835 return LY_EVALID;
4836 }
4837 } else {
4838 if (!mod->ns) {
4839 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "namespace", "module");
4840 return LY_EVALID;
4841 } else if (!mod->prefix) {
4842 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_MISSTMT, "prefix", "module");
4843 return LY_EVALID;
4844 }
4845 }
4846
4847 return ret;
4848}
4849
Radek Krejci531fe5b2018-09-19 11:14:13 +02004850static unsigned int
4851lysp_get_data_line(const char *start, const char *stop)
4852{
4853 unsigned int i, length, line = 1;
4854
4855 length = stop - start;
4856 for (i = 0; i < length; ++i) {
4857 if (start[i] == '\n') {
4858 ++line;
4859 }
4860 }
4861
4862 return line;
4863}
4864
Radek Krejcid4557c62018-09-17 11:42:09 +02004865LY_ERR
Michal Vasko7fbc8162018-09-17 10:35:16 +02004866yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
4867{
4868 LY_ERR ret = 0;
4869 char *word, *buf;
4870 int word_len;
4871 const char *data_start;
4872 enum yang_keyword kw;
4873 struct lysp_module *mod;
4874
4875 data_start = data;
4876
4877 /* "module"/"submodule" */
4878 ret = get_keyword(ctx, &data, &kw, &word, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004879 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004880
4881 if ((kw != YANG_MODULE) && (kw != YANG_SUBMODULE)) {
4882 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".\n",
Radek Krejcic59bc972018-09-17 16:13:06 +02004883 ly_stmt2str(kw));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004884 goto error;
4885 }
4886
4887 mod = calloc(1, sizeof *mod);
4888 LY_CHECK_ERR_GOTO(!mod, LOGMEM(ctx), error);
4889 if (kw == YANG_SUBMODULE) {
4890 mod->submodule = 1;
4891 }
4892
4893 /* substatements */
4894 ret = parse_sub_module(ctx, &data, mod);
Radek Krejcic59bc972018-09-17 16:13:06 +02004895 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004896
4897 /* read some trailing spaces or new lines */
4898 ret = get_string(ctx, &data, Y_MAYBE_STR_ARG, &word, &buf, &word_len);
Radek Krejcic59bc972018-09-17 16:13:06 +02004899 LY_CHECK_GOTO(ret, error);
Michal Vasko7fbc8162018-09-17 10:35:16 +02004900
4901 if (word) {
4902 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX, "Invalid character sequence \"%.*s\", expected end-of-file.",
4903 word_len, word);
4904 free(buf);
4905 goto error;
4906 }
4907 assert(!buf);
4908
4909 *mod_p = mod;
4910 return ret;
4911
4912error:
Radek Krejci531fe5b2018-09-19 11:14:13 +02004913 LOGERR(ctx, LY_EINVAL, "Module parsing failed on line %u.", lysp_get_data_line(data_start, data));
Michal Vasko7fbc8162018-09-17 10:35:16 +02004914 /* TODO free module */
4915 return ret;
4916}