blob: 0c4bc2265ec5a1c44919077a451c3b3b86260d32 [file] [log] [blame]
FredGand944bdc2019-11-05 21:57:07 +08001/**
2 * @file printer_yin.c
3 * @author Fred Gan <ganshaolong@vip.qq.com>
4 * @brief YIN printer
5 *
6 * Copyright (c) 2015 - 2019 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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
FredGand944bdc2019-11-05 21:57:07 +080016
FredGand944bdc2019-11-05 21:57:07 +080017#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
FredGand944bdc2019-11-05 21:57:07 +080020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020022#include "compat.h"
FredGand944bdc2019-11-05 21:57:07 +080023#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "out_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080026#include "printer_internal.h"
27#include "tree.h"
28#include "tree_schema.h"
29#include "tree_schema_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080030#include "xml.h"
Michal Vasko004d3152020-06-11 19:59:22 +020031#include "xpath.h"
FredGand944bdc2019-11-05 21:57:07 +080032
33/**
34 * @brief YIN printer context.
35 */
Radek Krejciadcf63d2021-02-09 10:21:18 +010036struct lys_ypr_ctx {
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010037 union {
38 struct {
39 struct ly_out *out; /**< output specification */
40 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
41 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
42 const struct lys_module *module; /**< schema to print */
43 };
44 struct lyspr_ctx printer_ctx;
45 };
46
47 /* YIN printer specific members */
FredGand944bdc2019-11-05 21:57:07 +080048};
49
Michal Vasko61ad1ff2022-02-10 15:48:39 +010050static void yprp_extension_instances(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +020051 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count);
FredGand944bdc2019-11-05 21:57:07 +080052
53static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010054ypr_open(struct lys_ypr_ctx *pctx, const char *elem_name, const char *attr_name, const char *attr_value, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080055{
Michal Vasko61ad1ff2022-02-10 15:48:39 +010056 ly_print_(pctx->out, "%*s<%s", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080057
58 if (attr_name) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010059 ly_print_(pctx->out, " %s=\"", attr_name);
60 lyxml_dump_text(pctx->out, attr_value, 1);
61 ly_print_(pctx->out, "\"%s", flag == -1 ? "/>\n" : flag == 1 ? ">\n" : "");
FredGand944bdc2019-11-05 21:57:07 +080062 } else if (flag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010063 ly_print_(pctx->out, flag == -1 ? "/>\n" : ">\n");
FredGand944bdc2019-11-05 21:57:07 +080064 }
65}
66
67static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010068ypr_close(struct lys_ypr_ctx *pctx, const char *elem_name, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080069{
70 if (flag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010071 ly_print_(pctx->out, "%*s</%s>\n", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080072 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010073 ly_print_(pctx->out, "/>\n");
FredGand944bdc2019-11-05 21:57:07 +080074 }
75}
76
77/*
78 * par_close_flag
79 * 0 - parent not yet closed, printing >\n, setting flag to 1
80 * 1 or NULL - parent already closed, do nothing
81 */
82static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010083ypr_close_parent(struct lys_ypr_ctx *pctx, int8_t *par_close_flag)
FredGand944bdc2019-11-05 21:57:07 +080084{
85 if (par_close_flag && !(*par_close_flag)) {
86 (*par_close_flag) = 1;
Michal Vasko61ad1ff2022-02-10 15:48:39 +010087 ly_print_(pctx->out, ">\n");
FredGand944bdc2019-11-05 21:57:07 +080088 }
89}
90
91static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010092ypr_yin_arg(struct lys_ypr_ctx *pctx, const char *arg, const char *text)
FredGand944bdc2019-11-05 21:57:07 +080093{
Michal Vasko61ad1ff2022-02-10 15:48:39 +010094 ly_print_(pctx->out, "%*s<%s>", INDENT, arg);
95 lyxml_dump_text(pctx->out, text, 0);
96 ly_print_(pctx->out, "</%s>\n", arg);
FredGand944bdc2019-11-05 21:57:07 +080097}
98
FredGand944bdc2019-11-05 21:57:07 +080099static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100100ypr_substmt(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, const char *text, void *ext)
FredGand944bdc2019-11-05 21:57:07 +0800101{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200102 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200103 int8_t extflag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800104
105 if (!text) {
106 /* nothing to print */
107 return;
108 }
109
Radek Krejcieccf6602021-02-05 19:42:54 +0100110 if (stmt_attr_info[substmt].flags & STMT_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +0800111 extflag = 1;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100112 ypr_open(pctx, stmt_attr_info[substmt].name, NULL, NULL, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800113 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100114 ypr_open(pctx, stmt_attr_info[substmt].name, stmt_attr_info[substmt].arg, text, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800115 }
116
117 LEVEL++;
118 LY_ARRAY_FOR(ext, u) {
Radek Krejciab430862021-03-02 20:13:40 +0100119 if ((((struct lysp_ext_instance *)ext)[u].parent_stmt != substmt) || (((struct lysp_ext_instance *)ext)[u].parent_stmt_index != substmt_index)) {
FredGand944bdc2019-11-05 21:57:07 +0800120 continue;
121 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100122 yprp_extension_instances(pctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800123 }
124
125 /* argument as yin-element */
Radek Krejcieccf6602021-02-05 19:42:54 +0100126 if (stmt_attr_info[substmt].flags & STMT_FLAG_YIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100127 ypr_yin_arg(pctx, stmt_attr_info[substmt].arg, text);
FredGand944bdc2019-11-05 21:57:07 +0800128 }
129
130 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100131 ypr_close(pctx, stmt_attr_info[substmt].name, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800132}
133
134static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100135ypr_unsigned(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, unsigned long int attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800136{
137 char *str;
Michal Vasko69730152020-10-09 16:30:07 +0200138
Radek Krejci1deb5be2020-08-26 16:43:36 +0200139 if (asprintf(&str, "%lu", attr_value) == -1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100140 LOGMEM(pctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800141 return;
142 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100143 ypr_substmt(pctx, substmt, substmt_index, str, exts);
FredGand944bdc2019-11-05 21:57:07 +0800144 free(str);
145}
146
147static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100148ypr_signed(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, signed long int attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800149{
150 char *str;
151
Radek Krejci1deb5be2020-08-26 16:43:36 +0200152 if (asprintf(&str, "%ld", attr_value) == -1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100153 LOGMEM(pctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800154 return;
155 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100156 ypr_substmt(pctx, substmt, substmt_index, str, exts);
FredGand944bdc2019-11-05 21:57:07 +0800157 free(str);
158}
159
160static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100161yprp_revision(struct lys_ypr_ctx *pctx, const struct lysp_revision *rev)
FredGand944bdc2019-11-05 21:57:07 +0800162{
163 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100164 ypr_open(pctx, "revision", "date", rev->date, 1);
FredGand944bdc2019-11-05 21:57:07 +0800165 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100166 yprp_extension_instances(pctx, LY_STMT_REVISION, 0, rev->exts, NULL, 0);
167 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, rev->dsc, rev->exts);
168 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, rev->ref, rev->exts);
FredGand944bdc2019-11-05 21:57:07 +0800169 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100170 ypr_close(pctx, "revision", 1);
FredGand944bdc2019-11-05 21:57:07 +0800171 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100172 ypr_open(pctx, "revision", "date", rev->date, -1);
FredGand944bdc2019-11-05 21:57:07 +0800173 }
174}
175
176static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100177ypr_mandatory(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800178{
179 if (flags & LYS_MAND_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100180 ypr_close_parent(pctx, flag);
181 ypr_substmt(pctx, LY_STMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
FredGand944bdc2019-11-05 21:57:07 +0800182 }
183}
184
185static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100186ypr_config(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800187{
188 if (flags & LYS_CONFIG_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100189 ypr_close_parent(pctx, flag);
190 ypr_substmt(pctx, LY_STMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
FredGand944bdc2019-11-05 21:57:07 +0800191 }
192}
193
194static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100195ypr_status(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800196{
197 const char *status = NULL;
198
199 if (flags & LYS_STATUS_CURR) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100200 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800201 status = "current";
202 } else if (flags & LYS_STATUS_DEPRC) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100203 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800204 status = "deprecated";
205 } else if (flags & LYS_STATUS_OBSLT) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100206 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800207 status = "obsolete";
208 }
209
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100210 ypr_substmt(pctx, LY_STMT_STATUS, 0, status, exts);
FredGand944bdc2019-11-05 21:57:07 +0800211}
212
213static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100214ypr_description(struct lys_ypr_ctx *pctx, const char *dsc, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800215{
216 if (dsc) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100217 ypr_close_parent(pctx, flag);
218 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, dsc, exts);
FredGand944bdc2019-11-05 21:57:07 +0800219 }
220}
221
222static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100223ypr_reference(struct lys_ypr_ctx *pctx, const char *ref, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800224{
225 if (ref) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100226 ypr_close_parent(pctx, flag);
227 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, ref, exts);
FredGand944bdc2019-11-05 21:57:07 +0800228 }
229}
230
231static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100232yprp_iffeatures(struct lys_ypr_ctx *pctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800233{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200234 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200235 int8_t extflag;
FredGand944bdc2019-11-05 21:57:07 +0800236
Michal Vasko7f45cf22020-10-01 12:49:44 +0200237 LY_ARRAY_FOR(iffs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100238 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800239 extflag = 0;
240
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100241 ly_print_(pctx->out, "%*s<if-feature name=\"%s", INDENT, iffs[u].str);
FredGand944bdc2019-11-05 21:57:07 +0800242
243 /* extensions */
244 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200245 LY_ARRAY_FOR(exts, v) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100246 yprp_extension_instances(pctx, LY_STMT_IF_FEATURE, u, &exts[v], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800247 }
248 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100249 ly_print_(pctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800250 }
251}
252
253static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100254yprp_extension(struct lys_ypr_ctx *pctx, const struct lysp_ext *ext)
FredGand944bdc2019-11-05 21:57:07 +0800255{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200256 int8_t flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200257 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800258
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100259 ypr_open(pctx, "extension", "name", ext->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800260 LEVEL++;
261
262 if (ext->exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100263 ypr_close_parent(pctx, &flag);
264 yprp_extension_instances(pctx, LY_STMT_EXTENSION, 0, ext->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800265 }
266
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100267 if (ext->argname) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100268 ypr_close_parent(pctx, &flag);
269 ypr_open(pctx, "argument", "name", ext->argname, flag2);
FredGand944bdc2019-11-05 21:57:07 +0800270
271 LEVEL++;
272 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200273 u = -1;
Radek Krejcifc596f92021-02-26 22:40:26 +0100274 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LY_STMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100275 ypr_close_parent(pctx, &flag2);
276 yprp_extension_instances(pctx, LY_STMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
FredGand944bdc2019-11-05 21:57:07 +0800277 }
278 }
279 if ((ext->flags & LYS_YINELEM_MASK) ||
Radek Krejcifc596f92021-02-26 22:40:26 +0100280 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LY_STMT_YIN_ELEMENT) != LY_ARRAY_COUNT(ext->exts)))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100281 ypr_close_parent(pctx, &flag2);
282 ypr_substmt(pctx, LY_STMT_YIN_ELEMENT, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
FredGand944bdc2019-11-05 21:57:07 +0800283 }
284 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100285 ypr_close(pctx, "argument", flag2);
FredGand944bdc2019-11-05 21:57:07 +0800286 }
287
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100288 ypr_status(pctx, ext->flags, ext->exts, &flag);
289 ypr_description(pctx, ext->dsc, ext->exts, &flag);
290 ypr_reference(pctx, ext->ref, ext->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800291
292 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100293 ypr_close(pctx, "extension", flag);
FredGand944bdc2019-11-05 21:57:07 +0800294}
295
296static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297yprp_feature(struct lys_ypr_ctx *pctx, const struct lysp_feature *feat)
FredGand944bdc2019-11-05 21:57:07 +0800298{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200299 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800300
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100301 ypr_open(pctx, "feature", "name", feat->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800302 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100303 yprp_extension_instances(pctx, LY_STMT_FEATURE, 0, feat->exts, &flag, 0);
304 yprp_iffeatures(pctx, feat->iffeatures, feat->exts, &flag);
305 ypr_status(pctx, feat->flags, feat->exts, &flag);
306 ypr_description(pctx, feat->dsc, feat->exts, &flag);
307 ypr_reference(pctx, feat->ref, feat->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800308 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100309 ypr_close(pctx, "feature", flag);
FredGand944bdc2019-11-05 21:57:07 +0800310}
311
312static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100313yprp_identity(struct lys_ypr_ctx *pctx, const struct lysp_ident *ident)
FredGand944bdc2019-11-05 21:57:07 +0800314{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200315 int8_t flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200316 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800317
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100318 ypr_open(pctx, "identity", "name", ident->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800319 LEVEL++;
320
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100321 yprp_extension_instances(pctx, LY_STMT_IDENTITY, 0, ident->exts, &flag, 0);
322 yprp_iffeatures(pctx, ident->iffeatures, ident->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800323
324 LY_ARRAY_FOR(ident->bases, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100325 ypr_close_parent(pctx, &flag);
326 ypr_substmt(pctx, LY_STMT_BASE, u, ident->bases[u], ident->exts);
FredGand944bdc2019-11-05 21:57:07 +0800327 }
328
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100329 ypr_status(pctx, ident->flags, ident->exts, &flag);
330 ypr_description(pctx, ident->dsc, ident->exts, &flag);
331 ypr_reference(pctx, ident->ref, ident->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800332
333 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100334 ypr_close(pctx, "identity", flag);
FredGand944bdc2019-11-05 21:57:07 +0800335}
336
337static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100338yprp_restr(struct lys_ypr_ctx *pctx, const struct lysp_restr *restr, enum ly_stmt stmt, const char *attr, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800339{
340 (void)flag;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200341 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800342
343 if (!restr) {
344 return;
345 }
346
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100347 ly_print_(pctx->out, "%*s<%s %s=\"", INDENT, ly_stmt2str(stmt), attr);
348 lyxml_dump_text(pctx->out,
Radek Krejcif13b87b2020-12-01 22:02:17 +0100349 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
350 restr->arg.str : &restr->arg.str[1], 1);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100351 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800352
353 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100354 yprp_extension_instances(pctx, stmt, 0, restr->exts, &inner_flag, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100355 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100356 ypr_close_parent(pctx, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800357 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100358 ypr_substmt(pctx, LY_STMT_MODIFIER, 0, "invert-match", restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800359 }
360 if (restr->emsg) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100361 ypr_close_parent(pctx, &inner_flag);
362 ypr_substmt(pctx, LY_STMT_ERROR_MESSAGE, 0, restr->emsg, restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800363 }
364 if (restr->eapptag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100365 ypr_close_parent(pctx, &inner_flag);
366 ypr_substmt(pctx, LY_STMT_ERROR_APP_TAG, 0, restr->eapptag, restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800367 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100368 ypr_description(pctx, restr->dsc, restr->exts, &inner_flag);
369 ypr_reference(pctx, restr->ref, restr->exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800370
371 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100372 ypr_close(pctx, ly_stmt2str(stmt), inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800373}
374
375static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100376yprp_when(struct lys_ypr_ctx *pctx, struct lysp_when *when, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800377{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200378 int8_t inner_flag = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200379
FredGand944bdc2019-11-05 21:57:07 +0800380 if (!when) {
381 return;
382 }
383
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100384 ypr_close_parent(pctx, flag);
385 ly_print_(pctx->out, "%*s<when condition=\"", INDENT);
386 lyxml_dump_text(pctx->out, when->cond, 1);
387 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800388
389 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100390 yprp_extension_instances(pctx, LY_STMT_WHEN, 0, when->exts, &inner_flag, 0);
391 ypr_description(pctx, when->dsc, when->exts, &inner_flag);
392 ypr_reference(pctx, when->ref, when->exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800393 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100394 ypr_close(pctx, "when", inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800395}
396
397static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100398yprp_enum(struct lys_ypr_ctx *pctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800399{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200400 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200401 int8_t inner_flag;
Michal Vasko69730152020-10-09 16:30:07 +0200402
FredGand944bdc2019-11-05 21:57:07 +0800403 (void)flag;
404
405 LY_ARRAY_FOR(items, u) {
406 if (type == LY_TYPE_BITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100407 ly_print_(pctx->out, "%*s<bit name=\"", INDENT);
408 lyxml_dump_text(pctx->out, items[u].name, 1);
409 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800410 } else { /* LY_TYPE_ENUM */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100411 ly_print_(pctx->out, "%*s<enum name=\"", INDENT);
412 lyxml_dump_text(pctx->out, items[u].name, 1);
413 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800414 }
415 inner_flag = 0;
416 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100417 yprp_extension_instances(pctx, LY_STMT_ENUM, 0, items[u].exts, &inner_flag, 0);
418 yprp_iffeatures(pctx, items[u].iffeatures, items[u].exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800419 if (items[u].flags & LYS_SET_VALUE) {
420 if (type == LY_TYPE_BITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100421 ypr_close_parent(pctx, &inner_flag);
422 ypr_unsigned(pctx, LY_STMT_POSITION, 0, items[u].exts, items[u].value);
FredGand944bdc2019-11-05 21:57:07 +0800423 } else { /* LY_TYPE_ENUM */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100424 ypr_close_parent(pctx, &inner_flag);
425 ypr_signed(pctx, LY_STMT_VALUE, 0, items[u].exts, items[u].value);
FredGand944bdc2019-11-05 21:57:07 +0800426 }
427 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100428 ypr_status(pctx, items[u].flags, items[u].exts, &inner_flag);
429 ypr_description(pctx, items[u].dsc, items[u].exts, &inner_flag);
430 ypr_reference(pctx, items[u].ref, items[u].exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800431 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100432 ypr_close(pctx, type == LY_TYPE_BITS ? "bit" : "enum", inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800433 }
434}
435
436static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100437yprp_type(struct lys_ypr_ctx *pctx, const struct lysp_type *type)
FredGand944bdc2019-11-05 21:57:07 +0800438{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200439 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200440 int8_t flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200441
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100442 if (!pctx || !type) {
FredGand944bdc2019-11-05 21:57:07 +0800443 return;
444 }
445
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100446 ypr_open(pctx, "type", "name", type->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800447 LEVEL++;
448
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100449 yprp_extension_instances(pctx, LY_STMT_TYPE, 0, type->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800450
451 if (type->range || type->length || type->patterns || type->bits || type->enums) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100452 ypr_close_parent(pctx, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800453 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100454 yprp_restr(pctx, type->range, LY_STMT_RANGE, "value", &flag);
455 yprp_restr(pctx, type->length, LY_STMT_LENGTH, "value", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800456 LY_ARRAY_FOR(type->patterns, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100457 yprp_restr(pctx, &type->patterns[u], LY_STMT_PATTERN, "value", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800458 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100459 yprp_enum(pctx, type->bits, LY_TYPE_BITS, &flag);
460 yprp_enum(pctx, type->enums, LY_TYPE_ENUM, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800461
462 if (type->path) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100463 ypr_close_parent(pctx, &flag);
464 ypr_substmt(pctx, LY_STMT_PATH, 0, type->path->expr, type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800465 }
466 if (type->flags & LYS_SET_REQINST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100467 ypr_close_parent(pctx, &flag);
468 ypr_substmt(pctx, LY_STMT_REQUIRE_INSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800469 }
470 if (type->flags & LYS_SET_FRDIGITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100471 ypr_close_parent(pctx, &flag);
472 ypr_unsigned(pctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, type->fraction_digits);
FredGand944bdc2019-11-05 21:57:07 +0800473 }
474 LY_ARRAY_FOR(type->bases, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100475 ypr_close_parent(pctx, &flag);
476 ypr_substmt(pctx, LY_STMT_BASE, u, type->bases[u], type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800477 }
478 LY_ARRAY_FOR(type->types, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100479 ypr_close_parent(pctx, &flag);
480 yprp_type(pctx, &type->types[u]);
FredGand944bdc2019-11-05 21:57:07 +0800481 }
482
483 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100484 ypr_close(pctx, "type", flag);
FredGand944bdc2019-11-05 21:57:07 +0800485}
486
487static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100488yprp_typedef(struct lys_ypr_ctx *pctx, const struct lysp_tpdf *tpdf)
FredGand944bdc2019-11-05 21:57:07 +0800489{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100490 ypr_open(pctx, "typedef", "name", tpdf->name, 1);
FredGand944bdc2019-11-05 21:57:07 +0800491 LEVEL++;
492
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100493 yprp_extension_instances(pctx, LY_STMT_TYPEDEF, 0, tpdf->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +0800494
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100495 yprp_type(pctx, &tpdf->type);
FredGand944bdc2019-11-05 21:57:07 +0800496
497 if (tpdf->units) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100498 ypr_substmt(pctx, LY_STMT_UNITS, 0, tpdf->units, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800499 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200500 if (tpdf->dflt.str) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100501 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800502 }
503
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100504 ypr_status(pctx, tpdf->flags, tpdf->exts, NULL);
505 ypr_description(pctx, tpdf->dsc, tpdf->exts, NULL);
506 ypr_reference(pctx, tpdf->ref, tpdf->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800507
508 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100509 ypr_close(pctx, "typedef", 1);
FredGand944bdc2019-11-05 21:57:07 +0800510}
511
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100512static void yprp_node(struct lys_ypr_ctx *pctx, const struct lysp_node *node);
513static void yprp_action(struct lys_ypr_ctx *pctx, const struct lysp_node_action *action);
514static void yprp_notification(struct lys_ypr_ctx *pctx, const struct lysp_node_notif *notif);
FredGand944bdc2019-11-05 21:57:07 +0800515
516static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100517yprp_grouping(struct lys_ypr_ctx *pctx, const struct lysp_node_grp *grp)
FredGand944bdc2019-11-05 21:57:07 +0800518{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200519 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200520 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800521 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100522 struct lysp_node_action *action;
523 struct lysp_node_notif *notif;
524 struct lysp_node_grp *subgrp;
FredGand944bdc2019-11-05 21:57:07 +0800525
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100526 ypr_open(pctx, "grouping", "name", grp->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800527 LEVEL++;
528
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100529 yprp_extension_instances(pctx, LY_STMT_GROUPING, 0, grp->exts, &flag, 0);
530 ypr_status(pctx, grp->flags, grp->exts, &flag);
531 ypr_description(pctx, grp->dsc, grp->exts, &flag);
532 ypr_reference(pctx, grp->ref, grp->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800533
534 LY_ARRAY_FOR(grp->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100535 ypr_close_parent(pctx, &flag);
536 yprp_typedef(pctx, &grp->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800537 }
538
Radek Krejci2a9fc652021-01-22 17:44:34 +0100539 LY_LIST_FOR(grp->groupings, subgrp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100540 ypr_close_parent(pctx, &flag);
541 yprp_grouping(pctx, subgrp);
FredGand944bdc2019-11-05 21:57:07 +0800542 }
543
Radek Krejci01180ac2021-01-27 08:48:22 +0100544 LY_LIST_FOR(grp->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100545 ypr_close_parent(pctx, &flag);
546 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800547 }
548
Radek Krejci2a9fc652021-01-22 17:44:34 +0100549 LY_LIST_FOR(grp->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100550 ypr_close_parent(pctx, &flag);
551 yprp_action(pctx, action);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100552 }
553
554 LY_LIST_FOR(grp->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100555 ypr_close_parent(pctx, &flag);
556 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800557 }
558
559 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100560 ypr_close(pctx, "grouping", flag);
FredGand944bdc2019-11-05 21:57:07 +0800561}
562
563static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100564yprp_inout(struct lys_ypr_ctx *pctx, const struct lysp_node_action_inout *inout, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800565{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200566 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800567 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100568 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800569
Radek Krejci01180ac2021-01-27 08:48:22 +0100570 if (!inout->child) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200571 /* input/output is empty */
FredGand944bdc2019-11-05 21:57:07 +0800572 return;
573 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100574 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800575
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100576 ypr_open(pctx, inout->name, NULL, NULL, *flag);
FredGand944bdc2019-11-05 21:57:07 +0800577 LEVEL++;
578
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100579 yprp_extension_instances(pctx, lys_nodetype2stmt(inout->nodetype), 0, inout->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +0800580 LY_ARRAY_FOR(inout->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100581 yprp_restr(pctx, &inout->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +0800582 }
583 LY_ARRAY_FOR(inout->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100584 yprp_typedef(pctx, &inout->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800585 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100586 LY_LIST_FOR(inout->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100587 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800588 }
589
Radek Krejci01180ac2021-01-27 08:48:22 +0100590 LY_LIST_FOR(inout->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100591 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800592 }
593
594 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100595 ypr_close(pctx, inout->name, 1);
FredGand944bdc2019-11-05 21:57:07 +0800596}
597
598static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100599yprp_notification(struct lys_ypr_ctx *pctx, const struct lysp_node_notif *notif)
FredGand944bdc2019-11-05 21:57:07 +0800600{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200601 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200602 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800603 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100604 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800605
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100606 ypr_open(pctx, "notification", "name", notif->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800607
608 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100609 yprp_extension_instances(pctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag, 0);
610 yprp_iffeatures(pctx, notif->iffeatures, notif->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800611
612 LY_ARRAY_FOR(notif->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100613 ypr_close_parent(pctx, &flag);
614 yprp_restr(pctx, &notif->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800615 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100616 ypr_status(pctx, notif->flags, notif->exts, &flag);
617 ypr_description(pctx, notif->dsc, notif->exts, &flag);
618 ypr_reference(pctx, notif->ref, notif->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800619
620 LY_ARRAY_FOR(notif->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100621 ypr_close_parent(pctx, &flag);
622 yprp_typedef(pctx, &notif->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800623 }
624
Radek Krejci2a9fc652021-01-22 17:44:34 +0100625 LY_LIST_FOR(notif->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100626 ypr_close_parent(pctx, &flag);
627 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800628 }
629
Radek Krejci01180ac2021-01-27 08:48:22 +0100630 LY_LIST_FOR(notif->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100631 ypr_close_parent(pctx, &flag);
632 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800633 }
634
635 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100636 ypr_close(pctx, "notification", flag);
FredGand944bdc2019-11-05 21:57:07 +0800637}
638
639static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100640yprp_action(struct lys_ypr_ctx *pctx, const struct lysp_node_action *action)
FredGand944bdc2019-11-05 21:57:07 +0800641{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200642 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200643 int8_t flag = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100644 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800645
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100646 ypr_open(pctx, action->parent ? "action" : "rpc", "name", action->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800647
648 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100649 yprp_extension_instances(pctx, lys_nodetype2stmt(action->nodetype), 0, action->exts, &flag, 0);
650 yprp_iffeatures(pctx, action->iffeatures, action->exts, &flag);
651 ypr_status(pctx, action->flags, action->exts, &flag);
652 ypr_description(pctx, action->dsc, action->exts, &flag);
653 ypr_reference(pctx, action->ref, action->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800654
655 LY_ARRAY_FOR(action->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100656 ypr_close_parent(pctx, &flag);
657 yprp_typedef(pctx, &action->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800658 }
659
Radek Krejci2a9fc652021-01-22 17:44:34 +0100660 LY_LIST_FOR(action->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100661 ypr_close_parent(pctx, &flag);
662 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800663 }
664
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100665 yprp_inout(pctx, &action->input, &flag);
666 yprp_inout(pctx, &action->output, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800667
668 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100669 ypr_close(pctx, action->parent ? "action" : "rpc", flag);
FredGand944bdc2019-11-05 21:57:07 +0800670}
671
672static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100673yprp_node_common1(struct lys_ypr_ctx *pctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800674{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100675 ypr_open(pctx, lys_nodetype2str(node->nodetype), "name", node->name, *flag);
FredGand944bdc2019-11-05 21:57:07 +0800676 LEVEL++;
677
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100678 yprp_extension_instances(pctx, lys_nodetype2stmt(node->nodetype), 0, node->exts, flag, 0);
679 yprp_when(pctx, lysp_node_when(node), flag);
680 yprp_iffeatures(pctx, node->iffeatures, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800681}
682
683static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100684yprp_node_common2(struct lys_ypr_ctx *pctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800685{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100686 ypr_config(pctx, node->flags, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800687 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100688 ypr_mandatory(pctx, node->flags, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800689 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100690 ypr_status(pctx, node->flags, node->exts, flag);
691 ypr_description(pctx, node->dsc, node->exts, flag);
692 ypr_reference(pctx, node->ref, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800693}
694
695static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100696yprp_container(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800697{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200698 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200699 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800700 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100701 struct lysp_node_action *action;
702 struct lysp_node_notif *notif;
703 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800704 struct lysp_node_container *cont = (struct lysp_node_container *)node;
705
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100706 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800707
708 LY_ARRAY_FOR(cont->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100709 ypr_close_parent(pctx, &flag);
710 yprp_restr(pctx, &cont->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800711 }
712 if (cont->presence) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100713 ypr_close_parent(pctx, &flag);
714 ypr_substmt(pctx, LY_STMT_PRESENCE, 0, cont->presence, cont->exts);
FredGand944bdc2019-11-05 21:57:07 +0800715 }
716
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100717 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800718
719 LY_ARRAY_FOR(cont->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100720 ypr_close_parent(pctx, &flag);
721 yprp_typedef(pctx, &cont->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800722 }
723
Radek Krejci2a9fc652021-01-22 17:44:34 +0100724 LY_LIST_FOR(cont->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100725 ypr_close_parent(pctx, &flag);
726 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800727 }
728
729 LY_LIST_FOR(cont->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100730 ypr_close_parent(pctx, &flag);
731 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800732 }
733
Radek Krejci2a9fc652021-01-22 17:44:34 +0100734 LY_LIST_FOR(cont->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100735 ypr_close_parent(pctx, &flag);
736 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +0800737 }
738
Radek Krejci2a9fc652021-01-22 17:44:34 +0100739 LY_LIST_FOR(cont->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100740 ypr_close_parent(pctx, &flag);
741 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800742 }
743
744 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100745 ypr_close(pctx, "container", flag);
FredGand944bdc2019-11-05 21:57:07 +0800746}
747
748static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100749yprp_case(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800750{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200751 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800752 struct lysp_node *child;
753 struct lysp_node_case *cas = (struct lysp_node_case *)node;
754
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100755 yprp_node_common1(pctx, node, &flag);
756 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800757
758 LY_LIST_FOR(cas->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100759 ypr_close_parent(pctx, &flag);
760 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800761 }
762
763 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100764 ypr_close(pctx, "case", flag);
FredGand944bdc2019-11-05 21:57:07 +0800765}
766
767static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100768yprp_choice(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800769{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200770 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800771 struct lysp_node *child;
772 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
773
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100774 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800775
Michal Vasko7f45cf22020-10-01 12:49:44 +0200776 if (choice->dflt.str) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100777 ypr_close_parent(pctx, &flag);
778 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, choice->dflt.str, choice->exts);
FredGand944bdc2019-11-05 21:57:07 +0800779 }
780
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100781 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800782
783 LY_LIST_FOR(choice->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100784 ypr_close_parent(pctx, &flag);
785 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800786 }
787
788 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100789 ypr_close(pctx, "choice", flag);
FredGand944bdc2019-11-05 21:57:07 +0800790}
791
792static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100793yprp_leaf(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800794{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200795 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800796 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
797
Radek Krejci1deb5be2020-08-26 16:43:36 +0200798 int8_t flag = 1;
Michal Vasko69730152020-10-09 16:30:07 +0200799
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100800 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800801
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100802 yprp_type(pctx, &leaf->type);
803 ypr_substmt(pctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800804 LY_ARRAY_FOR(leaf->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100805 yprp_restr(pctx, &leaf->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800806 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100807 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800808
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100809 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800810
811 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100812 ypr_close(pctx, "leaf", flag);
FredGand944bdc2019-11-05 21:57:07 +0800813}
814
815static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100816yprp_leaflist(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800817{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200818 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800819 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200820 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800821
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100822 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800823
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100824 yprp_type(pctx, &llist->type);
825 ypr_substmt(pctx, LY_STMT_UNITS, 0, llist->units, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800826 LY_ARRAY_FOR(llist->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100827 yprp_restr(pctx, &llist->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +0800828 }
829 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100830 ypr_substmt(pctx, LY_STMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800831 }
832
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100833 ypr_config(pctx, node->flags, node->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800834
835 if (llist->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100836 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min);
FredGand944bdc2019-11-05 21:57:07 +0800837 }
838 if (llist->flags & LYS_SET_MAX) {
839 if (llist->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100840 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max);
FredGand944bdc2019-11-05 21:57:07 +0800841 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100842 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800843 }
844 }
845
846 if (llist->flags & LYS_ORDBY_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100847 ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800848 }
849
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100850 ypr_status(pctx, node->flags, node->exts, &flag);
851 ypr_description(pctx, node->dsc, node->exts, &flag);
852 ypr_reference(pctx, node->ref, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800853
854 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100855 ypr_close(pctx, "leaf-list", flag);
FredGand944bdc2019-11-05 21:57:07 +0800856}
857
858static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100859yprp_list(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800860{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200861 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200862 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800863 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100864 struct lysp_node_action *action;
865 struct lysp_node_notif *notif;
866 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800867 struct lysp_node_list *list = (struct lysp_node_list *)node;
868
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100869 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800870
871 LY_ARRAY_FOR(list->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100872 ypr_close_parent(pctx, &flag);
873 yprp_restr(pctx, &list->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800874 }
875 if (list->key) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100876 ypr_close_parent(pctx, &flag);
877 ypr_substmt(pctx, LY_STMT_KEY, 0, list->key, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800878 }
879 LY_ARRAY_FOR(list->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100880 ypr_close_parent(pctx, &flag);
881 ypr_substmt(pctx, LY_STMT_UNIQUE, u, list->uniques[u].str, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800882 }
883
Michal Vasko846e3ab2022-03-01 09:54:27 +0100884 ypr_config(pctx, node->flags, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800885
886 if (list->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100887 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min);
FredGand944bdc2019-11-05 21:57:07 +0800888 }
889 if (list->flags & LYS_SET_MAX) {
890 if (list->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100891 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max);
FredGand944bdc2019-11-05 21:57:07 +0800892 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100893 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800894 }
895 }
896
897 if (list->flags & LYS_ORDBY_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100898 ypr_close_parent(pctx, &flag);
899 ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800900 }
901
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100902 ypr_status(pctx, node->flags, node->exts, &flag);
903 ypr_description(pctx, node->dsc, node->exts, &flag);
904 ypr_reference(pctx, node->ref, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800905
906 LY_ARRAY_FOR(list->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100907 ypr_close_parent(pctx, &flag);
908 yprp_typedef(pctx, &list->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800909 }
910
Radek Krejci2a9fc652021-01-22 17:44:34 +0100911 LY_LIST_FOR(list->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100912 ypr_close_parent(pctx, &flag);
913 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800914 }
915
916 LY_LIST_FOR(list->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100917 ypr_close_parent(pctx, &flag);
918 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800919 }
920
Radek Krejci2a9fc652021-01-22 17:44:34 +0100921 LY_LIST_FOR(list->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100922 ypr_close_parent(pctx, &flag);
923 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +0800924 }
925
Radek Krejci2a9fc652021-01-22 17:44:34 +0100926 LY_LIST_FOR(list->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100927 ypr_close_parent(pctx, &flag);
928 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800929 }
930
931 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100932 ypr_close(pctx, "list", flag);
FredGand944bdc2019-11-05 21:57:07 +0800933}
934
935static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100936yprp_refine(struct lys_ypr_ctx *pctx, struct lysp_refine *refine)
FredGand944bdc2019-11-05 21:57:07 +0800937{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200938 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200939 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800940
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100941 ypr_open(pctx, "refine", "target-node", refine->nodeid, flag);
FredGand944bdc2019-11-05 21:57:07 +0800942 LEVEL++;
943
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100944 yprp_extension_instances(pctx, LY_STMT_REFINE, 0, refine->exts, &flag, 0);
945 yprp_iffeatures(pctx, refine->iffeatures, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800946
947 LY_ARRAY_FOR(refine->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100948 ypr_close_parent(pctx, &flag);
949 yprp_restr(pctx, &refine->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800950 }
951
952 if (refine->presence) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100953 ypr_close_parent(pctx, &flag);
954 ypr_substmt(pctx, LY_STMT_PRESENCE, 0, refine->presence, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +0800955 }
956
957 LY_ARRAY_FOR(refine->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100958 ypr_close_parent(pctx, &flag);
959 ypr_substmt(pctx, LY_STMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +0800960 }
961
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100962 ypr_config(pctx, refine->flags, refine->exts, &flag);
963 ypr_mandatory(pctx, refine->flags, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800964
965 if (refine->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100966 ypr_close_parent(pctx, &flag);
967 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, refine->exts, refine->min);
FredGand944bdc2019-11-05 21:57:07 +0800968 }
969 if (refine->flags & LYS_SET_MAX) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100970 ypr_close_parent(pctx, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800971 if (refine->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100972 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, refine->exts, refine->max);
FredGand944bdc2019-11-05 21:57:07 +0800973 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100974 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", refine->exts);
FredGand944bdc2019-11-05 21:57:07 +0800975 }
976 }
977
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100978 ypr_description(pctx, refine->dsc, refine->exts, &flag);
979 ypr_reference(pctx, refine->ref, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800980
981 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100982 ypr_close(pctx, "refine", flag);
FredGand944bdc2019-11-05 21:57:07 +0800983}
984
985static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100986yprp_augment(struct lys_ypr_ctx *pctx, const struct lysp_node_augment *aug)
FredGand944bdc2019-11-05 21:57:07 +0800987{
FredGand944bdc2019-11-05 21:57:07 +0800988 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100989 struct lysp_node_action *action;
990 struct lysp_node_notif *notif;
FredGand944bdc2019-11-05 21:57:07 +0800991
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100992 ypr_open(pctx, "augment", "target-node", aug->nodeid, 1);
FredGand944bdc2019-11-05 21:57:07 +0800993 LEVEL++;
994
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100995 yprp_extension_instances(pctx, LY_STMT_AUGMENT, 0, aug->exts, NULL, 0);
996 yprp_when(pctx, aug->when, NULL);
997 yprp_iffeatures(pctx, aug->iffeatures, aug->exts, NULL);
998 ypr_status(pctx, aug->flags, aug->exts, NULL);
999 ypr_description(pctx, aug->dsc, aug->exts, NULL);
1000 ypr_reference(pctx, aug->ref, aug->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001001
1002 LY_LIST_FOR(aug->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001003 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +08001004 }
1005
Radek Krejci2a9fc652021-01-22 17:44:34 +01001006 LY_LIST_FOR(aug->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001007 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001008 }
1009
Radek Krejci2a9fc652021-01-22 17:44:34 +01001010 LY_LIST_FOR(aug->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001011 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001012 }
1013
1014 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001015 ypr_close(pctx, "augment", 1);
FredGand944bdc2019-11-05 21:57:07 +08001016}
1017
FredGand944bdc2019-11-05 21:57:07 +08001018static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001019yprp_uses(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001020{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001021 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001022 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001023 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001024 struct lysp_node_augment *aug;
FredGand944bdc2019-11-05 21:57:07 +08001025
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001026 yprp_node_common1(pctx, node, &flag);
1027 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001028
1029 LY_ARRAY_FOR(uses->refines, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001030 ypr_close_parent(pctx, &flag);
1031 yprp_refine(pctx, &uses->refines[u]);
FredGand944bdc2019-11-05 21:57:07 +08001032 }
1033
Radek Krejci2a9fc652021-01-22 17:44:34 +01001034 LY_LIST_FOR(uses->augments, aug) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001035 ypr_close_parent(pctx, &flag);
1036 yprp_augment(pctx, aug);
FredGand944bdc2019-11-05 21:57:07 +08001037 }
1038
1039 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001040 ypr_close(pctx, "uses", flag);
FredGand944bdc2019-11-05 21:57:07 +08001041}
1042
1043static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001044yprp_anydata(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001045{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001046 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001047 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001048 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1049
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001050 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001051
1052 LY_ARRAY_FOR(any->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001053 ypr_close_parent(pctx, &flag);
1054 yprp_restr(pctx, &any->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +08001055 }
1056
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001057 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001058
1059 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001060 ypr_close(pctx, lys_nodetype2str(node->nodetype), flag);
FredGand944bdc2019-11-05 21:57:07 +08001061}
1062
1063static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001064yprp_node(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001065{
FredGand944bdc2019-11-05 21:57:07 +08001066 switch (node->nodetype) {
1067 case LYS_CONTAINER:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001068 yprp_container(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001069 break;
1070 case LYS_CHOICE:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001071 yprp_choice(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001072 break;
1073 case LYS_LEAF:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001074 yprp_leaf(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001075 break;
1076 case LYS_LEAFLIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001077 yprp_leaflist(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001078 break;
1079 case LYS_LIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001080 yprp_list(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001081 break;
1082 case LYS_USES:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001083 yprp_uses(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001084 break;
1085 case LYS_ANYXML:
1086 case LYS_ANYDATA:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001087 yprp_anydata(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001088 break;
1089 case LYS_CASE:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001090 yprp_case(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001091 break;
1092 default:
1093 break;
1094 }
1095}
1096
1097static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001098yprp_deviation(struct lys_ypr_ctx *pctx, const struct lysp_deviation *deviation)
FredGand944bdc2019-11-05 21:57:07 +08001099{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001100 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001101 struct lysp_deviate_add *add;
1102 struct lysp_deviate_rpl *rpl;
1103 struct lysp_deviate_del *del;
1104 struct lysp_deviate *elem;
1105
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001106 ypr_open(pctx, "deviation", "target-node", deviation->nodeid, 1);
FredGand944bdc2019-11-05 21:57:07 +08001107 LEVEL++;
1108
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001109 yprp_extension_instances(pctx, LY_STMT_DEVIATION, 0, deviation->exts, NULL, 0);
1110 ypr_description(pctx, deviation->dsc, deviation->exts, NULL);
1111 ypr_reference(pctx, deviation->ref, deviation->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001112
1113 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001114 ly_print_(pctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001115 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1116 if (elem->exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001117 ly_print_(pctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001118 LEVEL++;
1119
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001120 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, elem->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001121 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001122 ly_print_(pctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001123 continue;
1124 }
1125 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001126 add = (struct lysp_deviate_add *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001127 ly_print_(pctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001128 LEVEL++;
1129
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001130 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, add->exts, NULL, 0);
1131 ypr_substmt(pctx, LY_STMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001132 LY_ARRAY_FOR(add->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001133 yprp_restr(pctx, &add->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001134 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001135 LY_ARRAY_FOR(add->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001136 ypr_substmt(pctx, LY_STMT_UNIQUE, u, add->uniques[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001137 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001138 LY_ARRAY_FOR(add->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001139 ypr_substmt(pctx, LY_STMT_DEFAULT, u, add->dflts[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001140 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001141 ypr_config(pctx, add->flags, add->exts, NULL);
1142 ypr_mandatory(pctx, add->flags, add->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001143 if (add->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001144 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, add->exts, add->min);
FredGand944bdc2019-11-05 21:57:07 +08001145 }
1146 if (add->flags & LYS_SET_MAX) {
1147 if (add->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001148 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, add->exts, add->max);
FredGand944bdc2019-11-05 21:57:07 +08001149 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001150 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001151 }
1152 }
1153 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001154 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001155 ly_print_(pctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001156 LEVEL++;
1157
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001158 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, rpl->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001159 if (rpl->type) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001160 yprp_type(pctx, rpl->type);
FredGand944bdc2019-11-05 21:57:07 +08001161 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001162 ypr_substmt(pctx, LY_STMT_UNITS, 0, rpl->units, rpl->exts);
1163 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
1164 ypr_config(pctx, rpl->flags, rpl->exts, NULL);
1165 ypr_mandatory(pctx, rpl->flags, rpl->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001166 if (rpl->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001167 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, rpl->exts, rpl->min);
FredGand944bdc2019-11-05 21:57:07 +08001168 }
1169 if (rpl->flags & LYS_SET_MAX) {
1170 if (rpl->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001171 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, rpl->exts, rpl->max);
FredGand944bdc2019-11-05 21:57:07 +08001172 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001173 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", rpl->exts);
FredGand944bdc2019-11-05 21:57:07 +08001174 }
1175 }
1176 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001177 del = (struct lysp_deviate_del *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001178 ly_print_(pctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001179 LEVEL++;
1180
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001181 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, del->exts, NULL, 0);
1182 ypr_substmt(pctx, LY_STMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001183 LY_ARRAY_FOR(del->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001184 yprp_restr(pctx, &del->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001185 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001186 LY_ARRAY_FOR(del->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001187 ypr_substmt(pctx, LY_STMT_UNIQUE, u, del->uniques[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001188 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001189 LY_ARRAY_FOR(del->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001190 ypr_substmt(pctx, LY_STMT_DEFAULT, u, del->dflts[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001191 }
1192 }
1193
1194 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001195 ypr_close(pctx, "deviate", 1);
FredGand944bdc2019-11-05 21:57:07 +08001196 }
1197
1198 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001199 ypr_close(pctx, "deviation", 1);
FredGand944bdc2019-11-05 21:57:07 +08001200}
1201
FredGand944bdc2019-11-05 21:57:07 +08001202static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001203ypr_xmlns(struct lys_ypr_ctx *pctx, const struct lys_module *module, uint16_t indent)
FredGand944bdc2019-11-05 21:57:07 +08001204{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001205 ly_print_(pctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1206 ly_print_(pctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001207}
FredGand944bdc2019-11-05 21:57:07 +08001208
Michal Vasko7c8439f2020-08-05 13:25:19 +02001209static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001210ypr_import_xmlns(struct lys_ypr_ctx *pctx, const struct lysp_module *modp, uint16_t indent)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001211{
1212 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001213
1214 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001215 ly_print_(pctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, modp->imports[u].prefix, modp->imports[u].module->ns);
FredGand944bdc2019-11-05 21:57:07 +08001216 }
1217}
1218
FredGand944bdc2019-11-05 21:57:07 +08001219static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001220yprp_stmt(struct lys_ypr_ctx *pctx, struct lysp_stmt *stmt)
FredGand944bdc2019-11-05 21:57:07 +08001221{
1222 struct lysp_stmt *childstmt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001223 int8_t flag = stmt->child ? 1 : -1;
FredGand944bdc2019-11-05 21:57:07 +08001224
1225 /* TODO:
1226 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
1227 cannot find the compiled information, so it is needed to be done,
1228 currently it is ignored */
Michal Vaskod989ba02020-08-24 10:59:24 +02001229 if (stmt_attr_info[stmt->kw].name) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001230 if (stmt_attr_info[stmt->kw].flags & STMT_FLAG_YIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001231 ypr_open(pctx, stmt->stmt, NULL, NULL, flag);
1232 ypr_yin_arg(pctx, stmt_attr_info[stmt->kw].arg, stmt->arg);
Radek Krejci0f969882020-08-21 16:56:47 +02001233 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001234 ypr_open(pctx, stmt->stmt, stmt_attr_info[stmt->kw].arg, stmt->arg, flag);
FredGand944bdc2019-11-05 21:57:07 +08001235 }
1236 }
1237
1238 if (stmt->child) {
1239 LEVEL++;
1240 LY_LIST_FOR(stmt->child, childstmt) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001241 yprp_stmt(pctx, childstmt);
FredGand944bdc2019-11-05 21:57:07 +08001242 }
1243 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001244 ypr_close(pctx, stmt->stmt, flag);
FredGand944bdc2019-11-05 21:57:07 +08001245 }
1246}
1247
1248/**
1249 * @param[in] count Number of extensions to print, 0 to print them all.
1250 */
1251static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001252yprp_extension_instances(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001253 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count)
FredGand944bdc2019-11-05 21:57:07 +08001254{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001255 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001256 struct lysp_stmt *stmt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001257 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001258
1259 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001260 count = LY_ARRAY_COUNT(ext);
FredGand944bdc2019-11-05 21:57:07 +08001261 }
1262 LY_ARRAY_FOR(ext, u) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001263 struct lysp_ext *ext_def = NULL;
1264
FredGand944bdc2019-11-05 21:57:07 +08001265 if (!count) {
1266 break;
1267 }
1268
1269 count--;
Radek Krejciab430862021-03-02 20:13:40 +01001270 if ((ext->flags & LYS_INTERNAL) || (ext->parent_stmt != substmt) || (ext->parent_stmt_index != substmt_index)) {
FredGand944bdc2019-11-05 21:57:07 +08001271 continue;
1272 }
1273
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001274 lysp_ext_find_definition(pctx->module->ctx, &ext[u], NULL, &ext_def);
Radek Krejci85ac8312021-03-03 20:21:33 +01001275 if (!ext_def) {
1276 continue;
FredGand944bdc2019-11-05 21:57:07 +08001277 }
1278
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001279 ypr_close_parent(pctx, flag);
Radek Krejci85ac8312021-03-03 20:21:33 +01001280 inner_flag = 0;
1281
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001282 if (ext_def->argname) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001283 lysp_ext_instance_resolve_argument(pctx->module->ctx, &ext[u], ext_def);
Radek Krejci85ac8312021-03-03 20:21:33 +01001284 }
1285
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001286 ypr_open(pctx, ext[u].name, (ext_def->flags & LYS_YINELEM_TRUE) ? NULL : ext_def->argname, ext[u].argument, inner_flag);
FredGand944bdc2019-11-05 21:57:07 +08001287 LEVEL++;
Radek Krejci85ac8312021-03-03 20:21:33 +01001288 if (ext_def->flags & LYS_YINELEM_TRUE) {
1289 const char *prefix, *name, *id;
1290 size_t prefix_len, name_len;
1291
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001292 ypr_close_parent(pctx, &inner_flag);
Radek Krejci85ac8312021-03-03 20:21:33 +01001293
1294 /* we need to use the same namespace as for the extension instance element */
1295 id = ext[u].name;
1296 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001297 ly_print_(pctx->out, "%*s<%.*s:%s>", INDENT, (int)prefix_len, prefix, ext_def->argname);
1298 lyxml_dump_text(pctx->out, ext[u].argument, 0);
1299 ly_print_(pctx->out, "</%.*s:%s>\n", (int)prefix_len, prefix, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001300 }
FredGand944bdc2019-11-05 21:57:07 +08001301 LY_LIST_FOR(ext[u].child, stmt) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001302 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
1303 continue;
1304 }
1305
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001306 ypr_close_parent(pctx, &inner_flag);
1307 yprp_stmt(pctx, stmt);
FredGand944bdc2019-11-05 21:57:07 +08001308 }
1309 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001310 ypr_close(pctx, ext[u].name, inner_flag);
FredGand944bdc2019-11-05 21:57:07 +08001311 }
1312}
1313
Michal Vasko7c8439f2020-08-05 13:25:19 +02001314static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001315yin_print_parsed_linkage(struct lys_ypr_ctx *pctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001316{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001317 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001318
FredGand944bdc2019-11-05 21:57:07 +08001319 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01001320 if (modp->imports[u].flags & LYS_INTERNAL) {
1321 continue;
1322 }
1323
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001324 ypr_open(pctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001325 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001326 yprp_extension_instances(pctx, LY_STMT_IMPORT, 0, modp->imports[u].exts, NULL, 0);
1327 ypr_substmt(pctx, LY_STMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001328 if (modp->imports[u].rev[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001329 ypr_substmt(pctx, LY_STMT_REVISION_DATE, 0, modp->imports[u].rev, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001330 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001331 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1332 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001333 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001334 ypr_close(pctx, "import", 1);
FredGand944bdc2019-11-05 21:57:07 +08001335 }
1336 LY_ARRAY_FOR(modp->includes, u) {
Radek Krejci771928a2021-01-19 13:42:36 +01001337 if (modp->includes[u].injected) {
1338 /* do not print the includes injected from submodules */
1339 continue;
1340 }
FredGand944bdc2019-11-05 21:57:07 +08001341 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001342 ypr_open(pctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001343 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001344 yprp_extension_instances(pctx, LY_STMT_INCLUDE, 0, modp->includes[u].exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001345 if (modp->includes[u].rev[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001346 ypr_substmt(pctx, LY_STMT_REVISION_DATE, 0, modp->includes[u].rev, modp->includes[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001347 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001348 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1349 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001350 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001351 ly_print_(pctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001352 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001353 ypr_open(pctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001354 }
1355 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001356}
FredGand944bdc2019-11-05 21:57:07 +08001357
Michal Vasko7c8439f2020-08-05 13:25:19 +02001358static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001359yin_print_parsed_body(struct lys_ypr_ctx *pctx, const struct lysp_module *modp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001360{
1361 LY_ARRAY_COUNT_TYPE u;
1362 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001363 struct lysp_node_action *action;
1364 struct lysp_node_notif *notif;
1365 struct lysp_node_grp *grp;
1366 struct lysp_node_augment *aug;
FredGand944bdc2019-11-05 21:57:07 +08001367
FredGand944bdc2019-11-05 21:57:07 +08001368 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001369 ly_print_(pctx->out, "\n");
1370 yprp_extension(pctx, &modp->extensions[u]);
FredGand944bdc2019-11-05 21:57:07 +08001371 }
1372 if (modp->exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001373 ly_print_(pctx->out, "\n");
1374 yprp_extension_instances(pctx, LY_STMT_MODULE, 0, modp->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001375 }
1376
1377 LY_ARRAY_FOR(modp->features, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001378 yprp_feature(pctx, &modp->features[u]);
FredGand944bdc2019-11-05 21:57:07 +08001379 }
1380
1381 LY_ARRAY_FOR(modp->identities, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001382 yprp_identity(pctx, &modp->identities[u]);
FredGand944bdc2019-11-05 21:57:07 +08001383 }
1384
1385 LY_ARRAY_FOR(modp->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001386 yprp_typedef(pctx, &modp->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +08001387 }
1388
Radek Krejci2a9fc652021-01-22 17:44:34 +01001389 LY_LIST_FOR(modp->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001390 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +08001391 }
1392
1393 LY_LIST_FOR(modp->data, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001394 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +08001395 }
1396
Radek Krejci2a9fc652021-01-22 17:44:34 +01001397 LY_LIST_FOR(modp->augments, aug) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001398 yprp_augment(pctx, aug);
FredGand944bdc2019-11-05 21:57:07 +08001399 }
1400
Radek Krejci2a9fc652021-01-22 17:44:34 +01001401 LY_LIST_FOR(modp->rpcs, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001402 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001403 }
1404
Radek Krejci2a9fc652021-01-22 17:44:34 +01001405 LY_LIST_FOR(modp->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001406 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001407 }
1408
1409 LY_ARRAY_FOR(modp->deviations, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001410 yprp_deviation(pctx, &modp->deviations[u]);
FredGand944bdc2019-11-05 21:57:07 +08001411 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001412}
1413
1414LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01001415yin_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001416{
1417 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7997d5a2021-02-22 10:55:56 +01001418 const struct lys_module *module = modp->mod;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001419 struct lys_ypr_ctx pctx_ = {.out = out, .level = 0, .module = module, .options = options}, *pctx = &pctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001420
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001421 ly_print_(pctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1422 ly_print_(pctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
1423 ypr_xmlns(pctx, module, XML_NS_INDENT);
1424 ypr_import_xmlns(pctx, modp, XML_NS_INDENT);
1425 ly_print_(pctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001426
1427 LEVEL++;
1428
1429 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001430 if (modp->version) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001431 ypr_substmt(pctx, LY_STMT_YANG_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001432 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001433 ypr_substmt(pctx, LY_STMT_NAMESPACE, 0, module->ns, modp->exts);
1434 ypr_substmt(pctx, LY_STMT_PREFIX, 0, module->prefix, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001435
1436 /* linkage-stmts (import/include) */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001437 yin_print_parsed_linkage(pctx, modp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001438
1439 /* meta-stmts */
1440 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001441 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001442 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001443 ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, module->org, modp->exts);
1444 ypr_substmt(pctx, LY_STMT_CONTACT, 0, module->contact, modp->exts);
1445 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, module->dsc, modp->exts);
1446 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, module->ref, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001447
1448 /* revision-stmts */
1449 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001450 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001451 }
1452 LY_ARRAY_FOR(modp->revs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001453 yprp_revision(pctx, &modp->revs[u]);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001454 }
1455
1456 /* body-stmts */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001457 yin_print_parsed_body(pctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001458
1459 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001460 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001461 ly_print_flush(out);
1462
1463 return LY_SUCCESS;
1464}
1465
Michal Vasko7c8439f2020-08-05 13:25:19 +02001466static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001467yprp_belongsto(struct lys_ypr_ctx *pctx, const struct lysp_submodule *submodp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001468{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001469 ypr_open(pctx, "belongs-to", "module", submodp->mod->name, 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001470 LEVEL++;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001471 yprp_extension_instances(pctx, LY_STMT_BELONGS_TO, 0, submodp->exts, NULL, 0);
1472 ypr_substmt(pctx, LY_STMT_PREFIX, 0, submodp->prefix, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001473 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001474 ypr_close(pctx, "belongs-to", 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001475}
1476
1477LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01001478yin_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001479{
1480 LY_ARRAY_COUNT_TYPE u;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001481 struct lys_ypr_ctx pctx_ = {.out = out, .level = 0, .module = submodp->mod, .options = options}, *pctx = &pctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001482
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001483 ly_print_(pctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1484 ly_print_(pctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
1485 ypr_xmlns(pctx, submodp->mod, XML_NS_INDENT);
1486 ypr_import_xmlns(pctx, (struct lysp_module *)submodp, XML_NS_INDENT);
1487 ly_print_(pctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001488
1489 LEVEL++;
1490
1491 /* submodule-header-stmts */
1492 if (submodp->version) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001493 ypr_substmt(pctx, LY_STMT_YANG_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001494 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001495 yprp_belongsto(pctx, submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001496
1497 /* linkage-stmts (import/include) */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001498 yin_print_parsed_linkage(pctx, (struct lysp_module *)submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001499
1500 /* meta-stmts */
1501 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001502 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001503 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001504 ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1505 ypr_substmt(pctx, LY_STMT_CONTACT, 0, submodp->contact, submodp->exts);
1506 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1507 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, submodp->ref, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001508
1509 /* revision-stmts */
1510 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001511 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001512 }
1513 LY_ARRAY_FOR(submodp->revs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001514 yprp_revision(pctx, &submodp->revs[u]);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001515 }
1516
1517 /* body-stmts */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001518 yin_print_parsed_body(pctx, (struct lysp_module *)submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001519
1520 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001521 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001522 ly_print_flush(out);
1523
1524 return LY_SUCCESS;
1525}