blob: 74e0f0ede0a292e16c44d1efe80d23ecda7e3385 [file] [log] [blame]
FredGand944bdc2019-11-05 21:57:07 +08001/**
2 * @file printer_yin.c
3 * @author Fred Gan <ganshaolong@vip.qq.com>
Michal Vaskob26d09d2022-08-22 09:52:19 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
FredGand944bdc2019-11-05 21:57:07 +08005 * @brief YIN printer
6 *
Michal Vaskob26d09d2022-08-22 09:52:19 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
FredGand944bdc2019-11-05 21:57:07 +08008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
FredGand944bdc2019-11-05 21:57:07 +080017
FredGand944bdc2019-11-05 21:57:07 +080018#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
FredGand944bdc2019-11-05 21:57:07 +080021
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020023#include "compat.h"
FredGand944bdc2019-11-05 21:57:07 +080024#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010025#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "out_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080027#include "printer_internal.h"
28#include "tree.h"
29#include "tree_schema.h"
30#include "tree_schema_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080031#include "xml.h"
Michal Vasko004d3152020-06-11 19:59:22 +020032#include "xpath.h"
FredGand944bdc2019-11-05 21:57:07 +080033
34/**
35 * @brief YIN printer context.
36 */
Radek Krejciadcf63d2021-02-09 10:21:18 +010037struct lys_ypr_ctx {
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010038 union {
39 struct {
40 struct ly_out *out; /**< output specification */
41 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
42 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
43 const struct lys_module *module; /**< schema to print */
44 };
45 struct lyspr_ctx printer_ctx;
46 };
47
48 /* YIN printer specific members */
FredGand944bdc2019-11-05 21:57:07 +080049};
50
FredGand944bdc2019-11-05 21:57:07 +080051static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010052ypr_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 +080053{
Michal Vasko61ad1ff2022-02-10 15:48:39 +010054 ly_print_(pctx->out, "%*s<%s", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080055
56 if (attr_name) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010057 ly_print_(pctx->out, " %s=\"", attr_name);
58 lyxml_dump_text(pctx->out, attr_value, 1);
59 ly_print_(pctx->out, "\"%s", flag == -1 ? "/>\n" : flag == 1 ? ">\n" : "");
FredGand944bdc2019-11-05 21:57:07 +080060 } else if (flag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010061 ly_print_(pctx->out, flag == -1 ? "/>\n" : ">\n");
FredGand944bdc2019-11-05 21:57:07 +080062 }
63}
64
65static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010066ypr_close(struct lys_ypr_ctx *pctx, const char *elem_name, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080067{
68 if (flag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010069 ly_print_(pctx->out, "%*s</%s>\n", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080070 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010071 ly_print_(pctx->out, "/>\n");
FredGand944bdc2019-11-05 21:57:07 +080072 }
73}
74
75/*
76 * par_close_flag
77 * 0 - parent not yet closed, printing >\n, setting flag to 1
78 * 1 or NULL - parent already closed, do nothing
79 */
80static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010081ypr_close_parent(struct lys_ypr_ctx *pctx, int8_t *par_close_flag)
FredGand944bdc2019-11-05 21:57:07 +080082{
83 if (par_close_flag && !(*par_close_flag)) {
84 (*par_close_flag) = 1;
Michal Vasko61ad1ff2022-02-10 15:48:39 +010085 ly_print_(pctx->out, ">\n");
FredGand944bdc2019-11-05 21:57:07 +080086 }
87}
88
89static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010090ypr_yin_arg(struct lys_ypr_ctx *pctx, const char *arg, const char *text)
FredGand944bdc2019-11-05 21:57:07 +080091{
Michal Vasko61ad1ff2022-02-10 15:48:39 +010092 ly_print_(pctx->out, "%*s<%s>", INDENT, arg);
93 lyxml_dump_text(pctx->out, text, 0);
94 ly_print_(pctx->out, "</%s>\n", arg);
FredGand944bdc2019-11-05 21:57:07 +080095}
96
FredGand944bdc2019-11-05 21:57:07 +080097static void
Michal Vaskob26d09d2022-08-22 09:52:19 +020098yprp_stmt(struct lys_ypr_ctx *pctx, struct lysp_stmt *stmt)
99{
100 struct lysp_stmt *childstmt;
101 int8_t flag = stmt->child ? 1 : -1;
102
103 /* TODO:
104 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
105 cannot find the compiled information, so it is needed to be done,
106 currently it is ignored */
Michal Vaskob872d0f2022-12-08 09:36:54 +0100107 if (lys_stmt_str(stmt->kw)) {
108 if (lys_stmt_flags(stmt->kw) & LY_STMT_FLAG_YIN) {
Michal Vaskob26d09d2022-08-22 09:52:19 +0200109 ypr_open(pctx, stmt->stmt, NULL, NULL, flag);
Michal Vaskob872d0f2022-12-08 09:36:54 +0100110 ypr_yin_arg(pctx, lys_stmt_arg(stmt->kw), stmt->arg);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200111 } else {
Michal Vaskob872d0f2022-12-08 09:36:54 +0100112 ypr_open(pctx, stmt->stmt, lys_stmt_arg(stmt->kw), stmt->arg, flag);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200113 }
114 }
115
116 if (stmt->child) {
117 LEVEL++;
118 LY_LIST_FOR(stmt->child, childstmt) {
119 yprp_stmt(pctx, childstmt);
120 }
121 LEVEL--;
122 ypr_close(pctx, stmt->stmt, flag);
123 }
124}
125
126static void
127yprp_extension_instance(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index,
128 struct lysp_ext_instance *ext, int8_t *flag)
129{
130 struct lysp_stmt *stmt;
131 int8_t inner_flag;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200132
133 if ((ext->flags & LYS_INTERNAL) || (ext->parent_stmt != substmt) || (ext->parent_stmt_index != substmt_index)) {
134 return;
135 }
136
Michal Vaskob26d09d2022-08-22 09:52:19 +0200137 ypr_close_parent(pctx, flag);
138 inner_flag = 0;
139
Michal Vasko193dacd2022-10-13 08:43:05 +0200140 ypr_open(pctx, ext->name, (ext->def->flags & LYS_YINELEM_TRUE) ? NULL : ext->def->argname, ext->argument, inner_flag);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200141 LEVEL++;
Michal Vasko193dacd2022-10-13 08:43:05 +0200142 if (ext->def->flags & LYS_YINELEM_TRUE) {
Michal Vaskob26d09d2022-08-22 09:52:19 +0200143 const char *prefix, *name, *id;
144 size_t prefix_len, name_len;
145
146 ypr_close_parent(pctx, &inner_flag);
147
148 /* we need to use the same namespace as for the extension instance element */
149 id = ext->name;
150 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
Michal Vasko193dacd2022-10-13 08:43:05 +0200151 ly_print_(pctx->out, "%*s<%.*s:%s>", INDENT, (int)prefix_len, prefix, ext->def->argname);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200152 lyxml_dump_text(pctx->out, ext->argument, 0);
Michal Vasko193dacd2022-10-13 08:43:05 +0200153 ly_print_(pctx->out, "</%.*s:%s>\n", (int)prefix_len, prefix, ext->def->argname);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200154 }
155 LY_LIST_FOR(ext->child, stmt) {
156 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
157 continue;
158 }
159
160 ypr_close_parent(pctx, &inner_flag);
161 yprp_stmt(pctx, stmt);
162 }
163 LEVEL--;
164 ypr_close(pctx, ext->name, inner_flag);
165}
166
167static void
168yprp_extension_instances(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index,
169 struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800170{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200171 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200172
173 LY_ARRAY_FOR(exts, u) {
174 yprp_extension_instance(pctx, substmt, substmt_index, &exts[u], flag);
175 }
176}
177
178static void
179ypr_substmt(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, const char *text, void *exts)
180{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200181 int8_t extflag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800182
183 if (!text) {
184 /* nothing to print */
185 return;
186 }
187
Michal Vaskob872d0f2022-12-08 09:36:54 +0100188 if (lys_stmt_flags(substmt) & LY_STMT_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +0800189 extflag = 1;
Michal Vaskob872d0f2022-12-08 09:36:54 +0100190 ypr_open(pctx, lys_stmt_str(substmt), NULL, NULL, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800191 } else {
Michal Vaskob872d0f2022-12-08 09:36:54 +0100192 ypr_open(pctx, lys_stmt_str(substmt), lys_stmt_arg(substmt), text, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800193 }
194
195 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200196 yprp_extension_instances(pctx, substmt, substmt_index, exts, &extflag);
FredGand944bdc2019-11-05 21:57:07 +0800197
198 /* argument as yin-element */
Michal Vaskob872d0f2022-12-08 09:36:54 +0100199 if (lys_stmt_flags(substmt) & LY_STMT_FLAG_YIN) {
200 ypr_yin_arg(pctx, lys_stmt_arg(substmt), text);
FredGand944bdc2019-11-05 21:57:07 +0800201 }
202
203 LEVEL--;
Michal Vaskob872d0f2022-12-08 09:36:54 +0100204 ypr_close(pctx, lys_stmt_str(substmt), extflag);
FredGand944bdc2019-11-05 21:57:07 +0800205}
206
207static void
Michal Vasko2bf4af42023-01-04 12:08:38 +0100208ypr_unsigned(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, unsigned long attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800209{
210 char *str;
Michal Vasko69730152020-10-09 16:30:07 +0200211
Radek Krejci1deb5be2020-08-26 16:43:36 +0200212 if (asprintf(&str, "%lu", attr_value) == -1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100213 LOGMEM(pctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800214 return;
215 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100216 ypr_substmt(pctx, substmt, substmt_index, str, exts);
FredGand944bdc2019-11-05 21:57:07 +0800217 free(str);
218}
219
220static void
Michal Vasko2bf4af42023-01-04 12:08:38 +0100221ypr_signed(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, long attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800222{
223 char *str;
224
Radek Krejci1deb5be2020-08-26 16:43:36 +0200225 if (asprintf(&str, "%ld", attr_value) == -1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100226 LOGMEM(pctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800227 return;
228 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100229 ypr_substmt(pctx, substmt, substmt_index, str, exts);
FredGand944bdc2019-11-05 21:57:07 +0800230 free(str);
231}
232
233static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100234yprp_revision(struct lys_ypr_ctx *pctx, const struct lysp_revision *rev)
FredGand944bdc2019-11-05 21:57:07 +0800235{
236 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100237 ypr_open(pctx, "revision", "date", rev->date, 1);
FredGand944bdc2019-11-05 21:57:07 +0800238 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200239 yprp_extension_instances(pctx, LY_STMT_REVISION, 0, rev->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100240 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, rev->dsc, rev->exts);
241 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, rev->ref, rev->exts);
FredGand944bdc2019-11-05 21:57:07 +0800242 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100243 ypr_close(pctx, "revision", 1);
FredGand944bdc2019-11-05 21:57:07 +0800244 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100245 ypr_open(pctx, "revision", "date", rev->date, -1);
FredGand944bdc2019-11-05 21:57:07 +0800246 }
247}
248
249static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100250ypr_mandatory(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800251{
252 if (flags & LYS_MAND_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100253 ypr_close_parent(pctx, flag);
254 ypr_substmt(pctx, LY_STMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
FredGand944bdc2019-11-05 21:57:07 +0800255 }
256}
257
258static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100259ypr_config(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800260{
261 if (flags & LYS_CONFIG_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100262 ypr_close_parent(pctx, flag);
263 ypr_substmt(pctx, LY_STMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
FredGand944bdc2019-11-05 21:57:07 +0800264 }
265}
266
267static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100268ypr_status(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800269{
270 const char *status = NULL;
271
272 if (flags & LYS_STATUS_CURR) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100273 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800274 status = "current";
275 } else if (flags & LYS_STATUS_DEPRC) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100276 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800277 status = "deprecated";
278 } else if (flags & LYS_STATUS_OBSLT) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100279 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800280 status = "obsolete";
281 }
282
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100283 ypr_substmt(pctx, LY_STMT_STATUS, 0, status, exts);
FredGand944bdc2019-11-05 21:57:07 +0800284}
285
286static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100287ypr_description(struct lys_ypr_ctx *pctx, const char *dsc, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800288{
289 if (dsc) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100290 ypr_close_parent(pctx, flag);
291 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, dsc, exts);
FredGand944bdc2019-11-05 21:57:07 +0800292 }
293}
294
295static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100296ypr_reference(struct lys_ypr_ctx *pctx, const char *ref, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800297{
298 if (ref) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100299 ypr_close_parent(pctx, flag);
300 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, ref, exts);
FredGand944bdc2019-11-05 21:57:07 +0800301 }
302}
303
304static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100305yprp_iffeatures(struct lys_ypr_ctx *pctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800306{
Michal Vaskob26d09d2022-08-22 09:52:19 +0200307 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200308 int8_t extflag;
FredGand944bdc2019-11-05 21:57:07 +0800309
Michal Vasko7f45cf22020-10-01 12:49:44 +0200310 LY_ARRAY_FOR(iffs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100311 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800312 extflag = 0;
313
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100314 ly_print_(pctx->out, "%*s<if-feature name=\"%s", INDENT, iffs[u].str);
FredGand944bdc2019-11-05 21:57:07 +0800315
316 /* extensions */
317 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200318 yprp_extension_instances(pctx, LY_STMT_IF_FEATURE, u, exts, &extflag);
FredGand944bdc2019-11-05 21:57:07 +0800319 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100320 ly_print_(pctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800321 }
322}
323
324static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100325yprp_extension(struct lys_ypr_ctx *pctx, const struct lysp_ext *ext)
FredGand944bdc2019-11-05 21:57:07 +0800326{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200327 int8_t flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200328 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800329
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100330 ypr_open(pctx, "extension", "name", ext->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800331 LEVEL++;
332
333 if (ext->exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100334 ypr_close_parent(pctx, &flag);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200335 yprp_extension_instances(pctx, LY_STMT_EXTENSION, 0, ext->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800336 }
337
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100338 if (ext->argname) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100339 ypr_close_parent(pctx, &flag);
340 ypr_open(pctx, "argument", "name", ext->argname, flag2);
FredGand944bdc2019-11-05 21:57:07 +0800341
342 LEVEL++;
343 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200344 u = -1;
Radek Krejcifc596f92021-02-26 22:40:26 +0100345 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 +0100346 ypr_close_parent(pctx, &flag2);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200347 yprp_extension_instance(pctx, LY_STMT_ARGUMENT, 0, &ext->exts[u], &flag2);
FredGand944bdc2019-11-05 21:57:07 +0800348 }
349 }
350 if ((ext->flags & LYS_YINELEM_MASK) ||
Radek Krejcifc596f92021-02-26 22:40:26 +0100351 (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 +0100352 ypr_close_parent(pctx, &flag2);
353 ypr_substmt(pctx, LY_STMT_YIN_ELEMENT, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
FredGand944bdc2019-11-05 21:57:07 +0800354 }
355 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100356 ypr_close(pctx, "argument", flag2);
FredGand944bdc2019-11-05 21:57:07 +0800357 }
358
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100359 ypr_status(pctx, ext->flags, ext->exts, &flag);
360 ypr_description(pctx, ext->dsc, ext->exts, &flag);
361 ypr_reference(pctx, ext->ref, ext->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800362
363 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100364 ypr_close(pctx, "extension", flag);
FredGand944bdc2019-11-05 21:57:07 +0800365}
366
367static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100368yprp_feature(struct lys_ypr_ctx *pctx, const struct lysp_feature *feat)
FredGand944bdc2019-11-05 21:57:07 +0800369{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200370 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800371
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100372 ypr_open(pctx, "feature", "name", feat->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800373 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200374 yprp_extension_instances(pctx, LY_STMT_FEATURE, 0, feat->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100375 yprp_iffeatures(pctx, feat->iffeatures, feat->exts, &flag);
376 ypr_status(pctx, feat->flags, feat->exts, &flag);
377 ypr_description(pctx, feat->dsc, feat->exts, &flag);
378 ypr_reference(pctx, feat->ref, feat->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800379 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100380 ypr_close(pctx, "feature", flag);
FredGand944bdc2019-11-05 21:57:07 +0800381}
382
383static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100384yprp_identity(struct lys_ypr_ctx *pctx, const struct lysp_ident *ident)
FredGand944bdc2019-11-05 21:57:07 +0800385{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200386 int8_t flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200387 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800388
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100389 ypr_open(pctx, "identity", "name", ident->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800390 LEVEL++;
391
Michal Vaskob26d09d2022-08-22 09:52:19 +0200392 yprp_extension_instances(pctx, LY_STMT_IDENTITY, 0, ident->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100393 yprp_iffeatures(pctx, ident->iffeatures, ident->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800394
395 LY_ARRAY_FOR(ident->bases, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100396 ypr_close_parent(pctx, &flag);
397 ypr_substmt(pctx, LY_STMT_BASE, u, ident->bases[u], ident->exts);
FredGand944bdc2019-11-05 21:57:07 +0800398 }
399
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100400 ypr_status(pctx, ident->flags, ident->exts, &flag);
401 ypr_description(pctx, ident->dsc, ident->exts, &flag);
402 ypr_reference(pctx, ident->ref, ident->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800403
404 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100405 ypr_close(pctx, "identity", flag);
FredGand944bdc2019-11-05 21:57:07 +0800406}
407
408static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100409yprp_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 +0800410{
411 (void)flag;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200412 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800413
414 if (!restr) {
415 return;
416 }
417
Michal Vasko193dacd2022-10-13 08:43:05 +0200418 ly_print_(pctx->out, "%*s<%s %s=\"", INDENT, lyplg_ext_stmt2str(stmt), attr);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100419 lyxml_dump_text(pctx->out,
Radek Krejcif13b87b2020-12-01 22:02:17 +0100420 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
421 restr->arg.str : &restr->arg.str[1], 1);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100422 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800423
424 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200425 yprp_extension_instances(pctx, stmt, 0, restr->exts, &inner_flag);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100426 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100427 ypr_close_parent(pctx, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800428 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100429 ypr_substmt(pctx, LY_STMT_MODIFIER, 0, "invert-match", restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800430 }
431 if (restr->emsg) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100432 ypr_close_parent(pctx, &inner_flag);
433 ypr_substmt(pctx, LY_STMT_ERROR_MESSAGE, 0, restr->emsg, restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800434 }
435 if (restr->eapptag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100436 ypr_close_parent(pctx, &inner_flag);
437 ypr_substmt(pctx, LY_STMT_ERROR_APP_TAG, 0, restr->eapptag, restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800438 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100439 ypr_description(pctx, restr->dsc, restr->exts, &inner_flag);
440 ypr_reference(pctx, restr->ref, restr->exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800441
442 LEVEL--;
Michal Vasko193dacd2022-10-13 08:43:05 +0200443 ypr_close(pctx, lyplg_ext_stmt2str(stmt), inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800444}
445
446static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100447yprp_when(struct lys_ypr_ctx *pctx, struct lysp_when *when, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800448{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200449 int8_t inner_flag = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200450
FredGand944bdc2019-11-05 21:57:07 +0800451 if (!when) {
452 return;
453 }
454
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100455 ypr_close_parent(pctx, flag);
456 ly_print_(pctx->out, "%*s<when condition=\"", INDENT);
457 lyxml_dump_text(pctx->out, when->cond, 1);
458 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800459
460 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200461 yprp_extension_instances(pctx, LY_STMT_WHEN, 0, when->exts, &inner_flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100462 ypr_description(pctx, when->dsc, when->exts, &inner_flag);
463 ypr_reference(pctx, when->ref, when->exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800464 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100465 ypr_close(pctx, "when", inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800466}
467
468static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100469yprp_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 +0800470{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200471 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200472 int8_t inner_flag;
Michal Vasko69730152020-10-09 16:30:07 +0200473
FredGand944bdc2019-11-05 21:57:07 +0800474 (void)flag;
475
476 LY_ARRAY_FOR(items, u) {
477 if (type == LY_TYPE_BITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100478 ly_print_(pctx->out, "%*s<bit name=\"", INDENT);
479 lyxml_dump_text(pctx->out, items[u].name, 1);
480 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800481 } else { /* LY_TYPE_ENUM */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100482 ly_print_(pctx->out, "%*s<enum name=\"", INDENT);
483 lyxml_dump_text(pctx->out, items[u].name, 1);
484 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800485 }
486 inner_flag = 0;
487 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200488 yprp_extension_instances(pctx, LY_STMT_ENUM, 0, items[u].exts, &inner_flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100489 yprp_iffeatures(pctx, items[u].iffeatures, items[u].exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800490 if (items[u].flags & LYS_SET_VALUE) {
491 if (type == LY_TYPE_BITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100492 ypr_close_parent(pctx, &inner_flag);
493 ypr_unsigned(pctx, LY_STMT_POSITION, 0, items[u].exts, items[u].value);
FredGand944bdc2019-11-05 21:57:07 +0800494 } else { /* LY_TYPE_ENUM */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100495 ypr_close_parent(pctx, &inner_flag);
496 ypr_signed(pctx, LY_STMT_VALUE, 0, items[u].exts, items[u].value);
FredGand944bdc2019-11-05 21:57:07 +0800497 }
498 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100499 ypr_status(pctx, items[u].flags, items[u].exts, &inner_flag);
500 ypr_description(pctx, items[u].dsc, items[u].exts, &inner_flag);
501 ypr_reference(pctx, items[u].ref, items[u].exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800502 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100503 ypr_close(pctx, type == LY_TYPE_BITS ? "bit" : "enum", inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800504 }
505}
506
507static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100508yprp_type(struct lys_ypr_ctx *pctx, const struct lysp_type *type)
FredGand944bdc2019-11-05 21:57:07 +0800509{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200510 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200511 int8_t flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200512
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100513 if (!pctx || !type) {
FredGand944bdc2019-11-05 21:57:07 +0800514 return;
515 }
516
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100517 ypr_open(pctx, "type", "name", type->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800518 LEVEL++;
519
Michal Vaskob26d09d2022-08-22 09:52:19 +0200520 yprp_extension_instances(pctx, LY_STMT_TYPE, 0, type->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800521
522 if (type->range || type->length || type->patterns || type->bits || type->enums) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100523 ypr_close_parent(pctx, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800524 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100525 yprp_restr(pctx, type->range, LY_STMT_RANGE, "value", &flag);
526 yprp_restr(pctx, type->length, LY_STMT_LENGTH, "value", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800527 LY_ARRAY_FOR(type->patterns, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100528 yprp_restr(pctx, &type->patterns[u], LY_STMT_PATTERN, "value", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800529 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100530 yprp_enum(pctx, type->bits, LY_TYPE_BITS, &flag);
531 yprp_enum(pctx, type->enums, LY_TYPE_ENUM, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800532
533 if (type->path) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100534 ypr_close_parent(pctx, &flag);
535 ypr_substmt(pctx, LY_STMT_PATH, 0, type->path->expr, type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800536 }
537 if (type->flags & LYS_SET_REQINST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100538 ypr_close_parent(pctx, &flag);
539 ypr_substmt(pctx, LY_STMT_REQUIRE_INSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800540 }
541 if (type->flags & LYS_SET_FRDIGITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100542 ypr_close_parent(pctx, &flag);
543 ypr_unsigned(pctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, type->fraction_digits);
FredGand944bdc2019-11-05 21:57:07 +0800544 }
545 LY_ARRAY_FOR(type->bases, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100546 ypr_close_parent(pctx, &flag);
547 ypr_substmt(pctx, LY_STMT_BASE, u, type->bases[u], type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800548 }
549 LY_ARRAY_FOR(type->types, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100550 ypr_close_parent(pctx, &flag);
551 yprp_type(pctx, &type->types[u]);
FredGand944bdc2019-11-05 21:57:07 +0800552 }
553
554 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100555 ypr_close(pctx, "type", flag);
FredGand944bdc2019-11-05 21:57:07 +0800556}
557
558static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100559yprp_typedef(struct lys_ypr_ctx *pctx, const struct lysp_tpdf *tpdf)
FredGand944bdc2019-11-05 21:57:07 +0800560{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100561 ypr_open(pctx, "typedef", "name", tpdf->name, 1);
FredGand944bdc2019-11-05 21:57:07 +0800562 LEVEL++;
563
Michal Vaskob26d09d2022-08-22 09:52:19 +0200564 yprp_extension_instances(pctx, LY_STMT_TYPEDEF, 0, tpdf->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800565
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100566 yprp_type(pctx, &tpdf->type);
FredGand944bdc2019-11-05 21:57:07 +0800567
568 if (tpdf->units) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100569 ypr_substmt(pctx, LY_STMT_UNITS, 0, tpdf->units, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800570 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200571 if (tpdf->dflt.str) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100572 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800573 }
574
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100575 ypr_status(pctx, tpdf->flags, tpdf->exts, NULL);
576 ypr_description(pctx, tpdf->dsc, tpdf->exts, NULL);
577 ypr_reference(pctx, tpdf->ref, tpdf->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800578
579 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100580 ypr_close(pctx, "typedef", 1);
FredGand944bdc2019-11-05 21:57:07 +0800581}
582
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100583static void yprp_node(struct lys_ypr_ctx *pctx, const struct lysp_node *node);
584static void yprp_action(struct lys_ypr_ctx *pctx, const struct lysp_node_action *action);
585static void yprp_notification(struct lys_ypr_ctx *pctx, const struct lysp_node_notif *notif);
FredGand944bdc2019-11-05 21:57:07 +0800586
587static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100588yprp_grouping(struct lys_ypr_ctx *pctx, const struct lysp_node_grp *grp)
FredGand944bdc2019-11-05 21:57:07 +0800589{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200590 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200591 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800592 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100593 struct lysp_node_action *action;
594 struct lysp_node_notif *notif;
595 struct lysp_node_grp *subgrp;
FredGand944bdc2019-11-05 21:57:07 +0800596
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100597 ypr_open(pctx, "grouping", "name", grp->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800598 LEVEL++;
599
Michal Vaskob26d09d2022-08-22 09:52:19 +0200600 yprp_extension_instances(pctx, LY_STMT_GROUPING, 0, grp->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100601 ypr_status(pctx, grp->flags, grp->exts, &flag);
602 ypr_description(pctx, grp->dsc, grp->exts, &flag);
603 ypr_reference(pctx, grp->ref, grp->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800604
605 LY_ARRAY_FOR(grp->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100606 ypr_close_parent(pctx, &flag);
607 yprp_typedef(pctx, &grp->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800608 }
609
Radek Krejci2a9fc652021-01-22 17:44:34 +0100610 LY_LIST_FOR(grp->groupings, subgrp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100611 ypr_close_parent(pctx, &flag);
612 yprp_grouping(pctx, subgrp);
FredGand944bdc2019-11-05 21:57:07 +0800613 }
614
Radek Krejci01180ac2021-01-27 08:48:22 +0100615 LY_LIST_FOR(grp->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100616 ypr_close_parent(pctx, &flag);
617 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800618 }
619
Radek Krejci2a9fc652021-01-22 17:44:34 +0100620 LY_LIST_FOR(grp->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100621 ypr_close_parent(pctx, &flag);
622 yprp_action(pctx, action);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100623 }
624
625 LY_LIST_FOR(grp->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100626 ypr_close_parent(pctx, &flag);
627 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800628 }
629
630 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100631 ypr_close(pctx, "grouping", flag);
FredGand944bdc2019-11-05 21:57:07 +0800632}
633
634static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100635yprp_inout(struct lys_ypr_ctx *pctx, const struct lysp_node_action_inout *inout, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800636{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200637 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800638 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100639 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800640
Radek Krejci01180ac2021-01-27 08:48:22 +0100641 if (!inout->child) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200642 /* input/output is empty */
FredGand944bdc2019-11-05 21:57:07 +0800643 return;
644 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100645 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800646
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100647 ypr_open(pctx, inout->name, NULL, NULL, *flag);
FredGand944bdc2019-11-05 21:57:07 +0800648 LEVEL++;
649
Michal Vasko193dacd2022-10-13 08:43:05 +0200650 yprp_extension_instances(pctx, lyplg_ext_nodetype2stmt(inout->nodetype), 0, inout->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800651 LY_ARRAY_FOR(inout->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100652 yprp_restr(pctx, &inout->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +0800653 }
654 LY_ARRAY_FOR(inout->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100655 yprp_typedef(pctx, &inout->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800656 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100657 LY_LIST_FOR(inout->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100658 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800659 }
660
Radek Krejci01180ac2021-01-27 08:48:22 +0100661 LY_LIST_FOR(inout->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100662 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800663 }
664
665 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100666 ypr_close(pctx, inout->name, 1);
FredGand944bdc2019-11-05 21:57:07 +0800667}
668
669static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100670yprp_notification(struct lys_ypr_ctx *pctx, const struct lysp_node_notif *notif)
FredGand944bdc2019-11-05 21:57:07 +0800671{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200672 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200673 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800674 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100675 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800676
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100677 ypr_open(pctx, "notification", "name", notif->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800678
679 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200680 yprp_extension_instances(pctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100681 yprp_iffeatures(pctx, notif->iffeatures, notif->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800682
683 LY_ARRAY_FOR(notif->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100684 ypr_close_parent(pctx, &flag);
685 yprp_restr(pctx, &notif->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800686 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100687 ypr_status(pctx, notif->flags, notif->exts, &flag);
688 ypr_description(pctx, notif->dsc, notif->exts, &flag);
689 ypr_reference(pctx, notif->ref, notif->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800690
691 LY_ARRAY_FOR(notif->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100692 ypr_close_parent(pctx, &flag);
693 yprp_typedef(pctx, &notif->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800694 }
695
Radek Krejci2a9fc652021-01-22 17:44:34 +0100696 LY_LIST_FOR(notif->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100697 ypr_close_parent(pctx, &flag);
698 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800699 }
700
Radek Krejci01180ac2021-01-27 08:48:22 +0100701 LY_LIST_FOR(notif->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100702 ypr_close_parent(pctx, &flag);
703 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800704 }
705
706 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100707 ypr_close(pctx, "notification", flag);
FredGand944bdc2019-11-05 21:57:07 +0800708}
709
710static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100711yprp_action(struct lys_ypr_ctx *pctx, const struct lysp_node_action *action)
FredGand944bdc2019-11-05 21:57:07 +0800712{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200713 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200714 int8_t flag = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100715 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800716
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100717 ypr_open(pctx, action->parent ? "action" : "rpc", "name", action->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800718
719 LEVEL++;
Michal Vasko193dacd2022-10-13 08:43:05 +0200720 yprp_extension_instances(pctx, lyplg_ext_nodetype2stmt(action->nodetype), 0, action->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100721 yprp_iffeatures(pctx, action->iffeatures, action->exts, &flag);
722 ypr_status(pctx, action->flags, action->exts, &flag);
723 ypr_description(pctx, action->dsc, action->exts, &flag);
724 ypr_reference(pctx, action->ref, action->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800725
726 LY_ARRAY_FOR(action->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100727 ypr_close_parent(pctx, &flag);
728 yprp_typedef(pctx, &action->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800729 }
730
Radek Krejci2a9fc652021-01-22 17:44:34 +0100731 LY_LIST_FOR(action->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100732 ypr_close_parent(pctx, &flag);
733 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800734 }
735
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100736 yprp_inout(pctx, &action->input, &flag);
737 yprp_inout(pctx, &action->output, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800738
739 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100740 ypr_close(pctx, action->parent ? "action" : "rpc", flag);
FredGand944bdc2019-11-05 21:57:07 +0800741}
742
743static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100744yprp_node_common1(struct lys_ypr_ctx *pctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800745{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100746 ypr_open(pctx, lys_nodetype2str(node->nodetype), "name", node->name, *flag);
FredGand944bdc2019-11-05 21:57:07 +0800747 LEVEL++;
748
Michal Vasko193dacd2022-10-13 08:43:05 +0200749 yprp_extension_instances(pctx, lyplg_ext_nodetype2stmt(node->nodetype), 0, node->exts, flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100750 yprp_when(pctx, lysp_node_when(node), flag);
751 yprp_iffeatures(pctx, node->iffeatures, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800752}
753
754static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100755yprp_node_common2(struct lys_ypr_ctx *pctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800756{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100757 ypr_config(pctx, node->flags, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800758 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100759 ypr_mandatory(pctx, node->flags, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800760 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100761 ypr_status(pctx, node->flags, node->exts, flag);
762 ypr_description(pctx, node->dsc, node->exts, flag);
763 ypr_reference(pctx, node->ref, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800764}
765
766static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100767yprp_container(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800768{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200769 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200770 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800771 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100772 struct lysp_node_action *action;
773 struct lysp_node_notif *notif;
774 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800775 struct lysp_node_container *cont = (struct lysp_node_container *)node;
776
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100777 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800778
779 LY_ARRAY_FOR(cont->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100780 ypr_close_parent(pctx, &flag);
781 yprp_restr(pctx, &cont->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800782 }
783 if (cont->presence) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100784 ypr_close_parent(pctx, &flag);
785 ypr_substmt(pctx, LY_STMT_PRESENCE, 0, cont->presence, cont->exts);
FredGand944bdc2019-11-05 21:57:07 +0800786 }
787
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100788 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800789
790 LY_ARRAY_FOR(cont->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100791 ypr_close_parent(pctx, &flag);
792 yprp_typedef(pctx, &cont->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800793 }
794
Radek Krejci2a9fc652021-01-22 17:44:34 +0100795 LY_LIST_FOR(cont->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100796 ypr_close_parent(pctx, &flag);
797 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800798 }
799
800 LY_LIST_FOR(cont->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100801 ypr_close_parent(pctx, &flag);
802 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800803 }
804
Radek Krejci2a9fc652021-01-22 17:44:34 +0100805 LY_LIST_FOR(cont->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100806 ypr_close_parent(pctx, &flag);
807 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +0800808 }
809
Radek Krejci2a9fc652021-01-22 17:44:34 +0100810 LY_LIST_FOR(cont->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100811 ypr_close_parent(pctx, &flag);
812 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800813 }
814
815 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100816 ypr_close(pctx, "container", flag);
FredGand944bdc2019-11-05 21:57:07 +0800817}
818
819static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100820yprp_case(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800821{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200822 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800823 struct lysp_node *child;
824 struct lysp_node_case *cas = (struct lysp_node_case *)node;
825
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100826 yprp_node_common1(pctx, node, &flag);
827 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800828
829 LY_LIST_FOR(cas->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100830 ypr_close_parent(pctx, &flag);
831 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800832 }
833
834 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100835 ypr_close(pctx, "case", flag);
FredGand944bdc2019-11-05 21:57:07 +0800836}
837
838static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100839yprp_choice(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800840{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200841 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800842 struct lysp_node *child;
843 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
844
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100845 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800846
Michal Vasko7f45cf22020-10-01 12:49:44 +0200847 if (choice->dflt.str) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100848 ypr_close_parent(pctx, &flag);
849 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, choice->dflt.str, choice->exts);
FredGand944bdc2019-11-05 21:57:07 +0800850 }
851
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100852 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800853
854 LY_LIST_FOR(choice->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100855 ypr_close_parent(pctx, &flag);
856 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800857 }
858
859 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100860 ypr_close(pctx, "choice", flag);
FredGand944bdc2019-11-05 21:57:07 +0800861}
862
863static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100864yprp_leaf(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800865{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200866 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800867 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
868
Radek Krejci1deb5be2020-08-26 16:43:36 +0200869 int8_t flag = 1;
Michal Vasko69730152020-10-09 16:30:07 +0200870
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100871 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800872
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100873 yprp_type(pctx, &leaf->type);
874 ypr_substmt(pctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800875 LY_ARRAY_FOR(leaf->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100876 yprp_restr(pctx, &leaf->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800877 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100878 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800879
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100880 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800881
882 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100883 ypr_close(pctx, "leaf", flag);
FredGand944bdc2019-11-05 21:57:07 +0800884}
885
886static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100887yprp_leaflist(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800888{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200889 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800890 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200891 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800892
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100893 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800894
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100895 yprp_type(pctx, &llist->type);
896 ypr_substmt(pctx, LY_STMT_UNITS, 0, llist->units, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800897 LY_ARRAY_FOR(llist->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100898 yprp_restr(pctx, &llist->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +0800899 }
900 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100901 ypr_substmt(pctx, LY_STMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800902 }
903
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100904 ypr_config(pctx, node->flags, node->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800905
906 if (llist->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100907 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min);
FredGand944bdc2019-11-05 21:57:07 +0800908 }
909 if (llist->flags & LYS_SET_MAX) {
910 if (llist->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100911 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max);
FredGand944bdc2019-11-05 21:57:07 +0800912 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100913 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800914 }
915 }
916
917 if (llist->flags & LYS_ORDBY_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100918 ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800919 }
920
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100921 ypr_status(pctx, node->flags, node->exts, &flag);
922 ypr_description(pctx, node->dsc, node->exts, &flag);
923 ypr_reference(pctx, node->ref, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800924
925 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100926 ypr_close(pctx, "leaf-list", flag);
FredGand944bdc2019-11-05 21:57:07 +0800927}
928
929static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100930yprp_list(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800931{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200932 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200933 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800934 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100935 struct lysp_node_action *action;
936 struct lysp_node_notif *notif;
937 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800938 struct lysp_node_list *list = (struct lysp_node_list *)node;
939
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100940 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800941
942 LY_ARRAY_FOR(list->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100943 ypr_close_parent(pctx, &flag);
944 yprp_restr(pctx, &list->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800945 }
946 if (list->key) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100947 ypr_close_parent(pctx, &flag);
948 ypr_substmt(pctx, LY_STMT_KEY, 0, list->key, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800949 }
950 LY_ARRAY_FOR(list->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100951 ypr_close_parent(pctx, &flag);
952 ypr_substmt(pctx, LY_STMT_UNIQUE, u, list->uniques[u].str, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800953 }
954
Michal Vasko846e3ab2022-03-01 09:54:27 +0100955 ypr_config(pctx, node->flags, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800956
957 if (list->flags & LYS_SET_MIN) {
Michal Vasko07937f02022-11-29 08:24:05 +0100958 ypr_close_parent(pctx, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100959 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min);
FredGand944bdc2019-11-05 21:57:07 +0800960 }
961 if (list->flags & LYS_SET_MAX) {
Michal Vasko07937f02022-11-29 08:24:05 +0100962 ypr_close_parent(pctx, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800963 if (list->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100964 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max);
FredGand944bdc2019-11-05 21:57:07 +0800965 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100966 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800967 }
968 }
969
970 if (list->flags & LYS_ORDBY_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100971 ypr_close_parent(pctx, &flag);
972 ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800973 }
974
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100975 ypr_status(pctx, node->flags, node->exts, &flag);
976 ypr_description(pctx, node->dsc, node->exts, &flag);
977 ypr_reference(pctx, node->ref, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800978
979 LY_ARRAY_FOR(list->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100980 ypr_close_parent(pctx, &flag);
981 yprp_typedef(pctx, &list->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800982 }
983
Radek Krejci2a9fc652021-01-22 17:44:34 +0100984 LY_LIST_FOR(list->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100985 ypr_close_parent(pctx, &flag);
986 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800987 }
988
989 LY_LIST_FOR(list->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100990 ypr_close_parent(pctx, &flag);
991 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800992 }
993
Radek Krejci2a9fc652021-01-22 17:44:34 +0100994 LY_LIST_FOR(list->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100995 ypr_close_parent(pctx, &flag);
996 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +0800997 }
998
Radek Krejci2a9fc652021-01-22 17:44:34 +0100999 LY_LIST_FOR(list->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001000 ypr_close_parent(pctx, &flag);
1001 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001002 }
1003
1004 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001005 ypr_close(pctx, "list", flag);
FredGand944bdc2019-11-05 21:57:07 +08001006}
1007
1008static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001009yprp_refine(struct lys_ypr_ctx *pctx, struct lysp_refine *refine)
FredGand944bdc2019-11-05 21:57:07 +08001010{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001011 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001012 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001013
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001014 ypr_open(pctx, "refine", "target-node", refine->nodeid, flag);
FredGand944bdc2019-11-05 21:57:07 +08001015 LEVEL++;
1016
Michal Vaskob26d09d2022-08-22 09:52:19 +02001017 yprp_extension_instances(pctx, LY_STMT_REFINE, 0, refine->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001018 yprp_iffeatures(pctx, refine->iffeatures, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001019
1020 LY_ARRAY_FOR(refine->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001021 ypr_close_parent(pctx, &flag);
1022 yprp_restr(pctx, &refine->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +08001023 }
1024
1025 if (refine->presence) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001026 ypr_close_parent(pctx, &flag);
1027 ypr_substmt(pctx, LY_STMT_PRESENCE, 0, refine->presence, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +08001028 }
1029
1030 LY_ARRAY_FOR(refine->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001031 ypr_close_parent(pctx, &flag);
1032 ypr_substmt(pctx, LY_STMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +08001033 }
1034
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001035 ypr_config(pctx, refine->flags, refine->exts, &flag);
1036 ypr_mandatory(pctx, refine->flags, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001037
1038 if (refine->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001039 ypr_close_parent(pctx, &flag);
1040 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, refine->exts, refine->min);
FredGand944bdc2019-11-05 21:57:07 +08001041 }
1042 if (refine->flags & LYS_SET_MAX) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001043 ypr_close_parent(pctx, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001044 if (refine->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001045 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, refine->exts, refine->max);
FredGand944bdc2019-11-05 21:57:07 +08001046 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001047 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", refine->exts);
FredGand944bdc2019-11-05 21:57:07 +08001048 }
1049 }
1050
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001051 ypr_description(pctx, refine->dsc, refine->exts, &flag);
1052 ypr_reference(pctx, refine->ref, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001053
1054 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001055 ypr_close(pctx, "refine", flag);
FredGand944bdc2019-11-05 21:57:07 +08001056}
1057
1058static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001059yprp_augment(struct lys_ypr_ctx *pctx, const struct lysp_node_augment *aug)
FredGand944bdc2019-11-05 21:57:07 +08001060{
FredGand944bdc2019-11-05 21:57:07 +08001061 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001062 struct lysp_node_action *action;
1063 struct lysp_node_notif *notif;
FredGand944bdc2019-11-05 21:57:07 +08001064
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001065 ypr_open(pctx, "augment", "target-node", aug->nodeid, 1);
FredGand944bdc2019-11-05 21:57:07 +08001066 LEVEL++;
1067
Michal Vaskob26d09d2022-08-22 09:52:19 +02001068 yprp_extension_instances(pctx, LY_STMT_AUGMENT, 0, aug->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001069 yprp_when(pctx, aug->when, NULL);
1070 yprp_iffeatures(pctx, aug->iffeatures, aug->exts, NULL);
1071 ypr_status(pctx, aug->flags, aug->exts, NULL);
1072 ypr_description(pctx, aug->dsc, aug->exts, NULL);
1073 ypr_reference(pctx, aug->ref, aug->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001074
1075 LY_LIST_FOR(aug->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001076 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +08001077 }
1078
Radek Krejci2a9fc652021-01-22 17:44:34 +01001079 LY_LIST_FOR(aug->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001080 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001081 }
1082
Radek Krejci2a9fc652021-01-22 17:44:34 +01001083 LY_LIST_FOR(aug->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001084 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001085 }
1086
1087 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001088 ypr_close(pctx, "augment", 1);
FredGand944bdc2019-11-05 21:57:07 +08001089}
1090
FredGand944bdc2019-11-05 21:57:07 +08001091static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001092yprp_uses(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001093{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001094 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001095 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001096 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001097 struct lysp_node_augment *aug;
FredGand944bdc2019-11-05 21:57:07 +08001098
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001099 yprp_node_common1(pctx, node, &flag);
1100 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001101
1102 LY_ARRAY_FOR(uses->refines, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001103 ypr_close_parent(pctx, &flag);
1104 yprp_refine(pctx, &uses->refines[u]);
FredGand944bdc2019-11-05 21:57:07 +08001105 }
1106
Radek Krejci2a9fc652021-01-22 17:44:34 +01001107 LY_LIST_FOR(uses->augments, aug) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001108 ypr_close_parent(pctx, &flag);
1109 yprp_augment(pctx, aug);
FredGand944bdc2019-11-05 21:57:07 +08001110 }
1111
1112 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001113 ypr_close(pctx, "uses", flag);
FredGand944bdc2019-11-05 21:57:07 +08001114}
1115
1116static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001117yprp_anydata(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001118{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001119 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001120 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001121 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1122
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001123 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001124
1125 LY_ARRAY_FOR(any->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001126 ypr_close_parent(pctx, &flag);
1127 yprp_restr(pctx, &any->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +08001128 }
1129
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001130 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001131
1132 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001133 ypr_close(pctx, lys_nodetype2str(node->nodetype), flag);
FredGand944bdc2019-11-05 21:57:07 +08001134}
1135
1136static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001137yprp_node(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001138{
FredGand944bdc2019-11-05 21:57:07 +08001139 switch (node->nodetype) {
1140 case LYS_CONTAINER:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001141 yprp_container(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001142 break;
1143 case LYS_CHOICE:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001144 yprp_choice(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001145 break;
1146 case LYS_LEAF:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001147 yprp_leaf(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001148 break;
1149 case LYS_LEAFLIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001150 yprp_leaflist(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001151 break;
1152 case LYS_LIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001153 yprp_list(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001154 break;
1155 case LYS_USES:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001156 yprp_uses(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001157 break;
1158 case LYS_ANYXML:
1159 case LYS_ANYDATA:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001160 yprp_anydata(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001161 break;
1162 case LYS_CASE:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001163 yprp_case(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001164 break;
1165 default:
1166 break;
1167 }
1168}
1169
1170static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001171yprp_deviation(struct lys_ypr_ctx *pctx, const struct lysp_deviation *deviation)
FredGand944bdc2019-11-05 21:57:07 +08001172{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001173 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001174 struct lysp_deviate_add *add;
1175 struct lysp_deviate_rpl *rpl;
1176 struct lysp_deviate_del *del;
1177 struct lysp_deviate *elem;
1178
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001179 ypr_open(pctx, "deviation", "target-node", deviation->nodeid, 1);
FredGand944bdc2019-11-05 21:57:07 +08001180 LEVEL++;
1181
Michal Vaskob26d09d2022-08-22 09:52:19 +02001182 yprp_extension_instances(pctx, LY_STMT_DEVIATION, 0, deviation->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001183 ypr_description(pctx, deviation->dsc, deviation->exts, NULL);
1184 ypr_reference(pctx, deviation->ref, deviation->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001185
1186 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001187 ly_print_(pctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001188 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1189 if (elem->exts) {
Michal Vasko12ef5362022-09-16 15:13:58 +02001190 ly_print_(pctx->out, "not-supported\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001191 LEVEL++;
1192
Michal Vaskob26d09d2022-08-22 09:52:19 +02001193 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, elem->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001194 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001195 ly_print_(pctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001196 continue;
1197 }
1198 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001199 add = (struct lysp_deviate_add *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001200 ly_print_(pctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001201 LEVEL++;
1202
Michal Vaskob26d09d2022-08-22 09:52:19 +02001203 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, add->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001204 ypr_substmt(pctx, LY_STMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001205 LY_ARRAY_FOR(add->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001206 yprp_restr(pctx, &add->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001207 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001208 LY_ARRAY_FOR(add->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001209 ypr_substmt(pctx, LY_STMT_UNIQUE, u, add->uniques[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001210 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001211 LY_ARRAY_FOR(add->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001212 ypr_substmt(pctx, LY_STMT_DEFAULT, u, add->dflts[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001213 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001214 ypr_config(pctx, add->flags, add->exts, NULL);
1215 ypr_mandatory(pctx, add->flags, add->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001216 if (add->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001217 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, add->exts, add->min);
FredGand944bdc2019-11-05 21:57:07 +08001218 }
1219 if (add->flags & LYS_SET_MAX) {
1220 if (add->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001221 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, add->exts, add->max);
FredGand944bdc2019-11-05 21:57:07 +08001222 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001223 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001224 }
1225 }
1226 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001227 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001228 ly_print_(pctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001229 LEVEL++;
1230
Michal Vaskob26d09d2022-08-22 09:52:19 +02001231 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, rpl->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001232 if (rpl->type) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001233 yprp_type(pctx, rpl->type);
FredGand944bdc2019-11-05 21:57:07 +08001234 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001235 ypr_substmt(pctx, LY_STMT_UNITS, 0, rpl->units, rpl->exts);
1236 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
1237 ypr_config(pctx, rpl->flags, rpl->exts, NULL);
1238 ypr_mandatory(pctx, rpl->flags, rpl->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001239 if (rpl->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001240 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, rpl->exts, rpl->min);
FredGand944bdc2019-11-05 21:57:07 +08001241 }
1242 if (rpl->flags & LYS_SET_MAX) {
1243 if (rpl->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001244 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, rpl->exts, rpl->max);
FredGand944bdc2019-11-05 21:57:07 +08001245 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001246 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", rpl->exts);
FredGand944bdc2019-11-05 21:57:07 +08001247 }
1248 }
1249 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001250 del = (struct lysp_deviate_del *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001251 ly_print_(pctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001252 LEVEL++;
1253
Michal Vaskob26d09d2022-08-22 09:52:19 +02001254 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, del->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001255 ypr_substmt(pctx, LY_STMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001256 LY_ARRAY_FOR(del->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001257 yprp_restr(pctx, &del->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001258 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001259 LY_ARRAY_FOR(del->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001260 ypr_substmt(pctx, LY_STMT_UNIQUE, u, del->uniques[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001261 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001262 LY_ARRAY_FOR(del->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001263 ypr_substmt(pctx, LY_STMT_DEFAULT, u, del->dflts[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001264 }
1265 }
1266
1267 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001268 ypr_close(pctx, "deviate", 1);
FredGand944bdc2019-11-05 21:57:07 +08001269 }
1270
1271 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001272 ypr_close(pctx, "deviation", 1);
FredGand944bdc2019-11-05 21:57:07 +08001273}
1274
FredGand944bdc2019-11-05 21:57:07 +08001275static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001276ypr_xmlns(struct lys_ypr_ctx *pctx, const struct lys_module *module, uint16_t indent)
FredGand944bdc2019-11-05 21:57:07 +08001277{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001278 ly_print_(pctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1279 ly_print_(pctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001280}
FredGand944bdc2019-11-05 21:57:07 +08001281
Michal Vasko7c8439f2020-08-05 13:25:19 +02001282static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001283ypr_import_xmlns(struct lys_ypr_ctx *pctx, const struct lysp_module *modp, uint16_t indent)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001284{
1285 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001286
1287 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001288 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 +08001289 }
1290}
1291
FredGand944bdc2019-11-05 21:57:07 +08001292static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001293yin_print_parsed_linkage(struct lys_ypr_ctx *pctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001294{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001295 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001296
FredGand944bdc2019-11-05 21:57:07 +08001297 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01001298 if (modp->imports[u].flags & LYS_INTERNAL) {
1299 continue;
1300 }
1301
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001302 ypr_open(pctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001303 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +02001304 yprp_extension_instances(pctx, LY_STMT_IMPORT, 0, modp->imports[u].exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001305 ypr_substmt(pctx, LY_STMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001306 if (modp->imports[u].rev[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001307 ypr_substmt(pctx, LY_STMT_REVISION_DATE, 0, modp->imports[u].rev, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001308 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001309 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1310 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001311 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001312 ypr_close(pctx, "import", 1);
FredGand944bdc2019-11-05 21:57:07 +08001313 }
1314 LY_ARRAY_FOR(modp->includes, u) {
Radek Krejci771928a2021-01-19 13:42:36 +01001315 if (modp->includes[u].injected) {
1316 /* do not print the includes injected from submodules */
1317 continue;
1318 }
FredGand944bdc2019-11-05 21:57:07 +08001319 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 +01001320 ypr_open(pctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001321 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +02001322 yprp_extension_instances(pctx, LY_STMT_INCLUDE, 0, modp->includes[u].exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001323 if (modp->includes[u].rev[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001324 ypr_substmt(pctx, LY_STMT_REVISION_DATE, 0, modp->includes[u].rev, modp->includes[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001325 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001326 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1327 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001328 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001329 ly_print_(pctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001330 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001331 ypr_open(pctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001332 }
1333 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001334}
FredGand944bdc2019-11-05 21:57:07 +08001335
Michal Vasko7c8439f2020-08-05 13:25:19 +02001336static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001337yin_print_parsed_body(struct lys_ypr_ctx *pctx, const struct lysp_module *modp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001338{
1339 LY_ARRAY_COUNT_TYPE u;
1340 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001341 struct lysp_node_action *action;
1342 struct lysp_node_notif *notif;
1343 struct lysp_node_grp *grp;
1344 struct lysp_node_augment *aug;
FredGand944bdc2019-11-05 21:57:07 +08001345
FredGand944bdc2019-11-05 21:57:07 +08001346 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001347 yprp_extension(pctx, &modp->extensions[u]);
FredGand944bdc2019-11-05 21:57:07 +08001348 }
1349 if (modp->exts) {
Michal Vaskob26d09d2022-08-22 09:52:19 +02001350 yprp_extension_instances(pctx, LY_STMT_MODULE, 0, modp->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001351 }
1352
1353 LY_ARRAY_FOR(modp->features, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001354 yprp_feature(pctx, &modp->features[u]);
FredGand944bdc2019-11-05 21:57:07 +08001355 }
1356
1357 LY_ARRAY_FOR(modp->identities, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001358 yprp_identity(pctx, &modp->identities[u]);
FredGand944bdc2019-11-05 21:57:07 +08001359 }
1360
1361 LY_ARRAY_FOR(modp->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001362 yprp_typedef(pctx, &modp->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +08001363 }
1364
Radek Krejci2a9fc652021-01-22 17:44:34 +01001365 LY_LIST_FOR(modp->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001366 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +08001367 }
1368
1369 LY_LIST_FOR(modp->data, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001370 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +08001371 }
1372
Radek Krejci2a9fc652021-01-22 17:44:34 +01001373 LY_LIST_FOR(modp->augments, aug) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001374 yprp_augment(pctx, aug);
FredGand944bdc2019-11-05 21:57:07 +08001375 }
1376
Radek Krejci2a9fc652021-01-22 17:44:34 +01001377 LY_LIST_FOR(modp->rpcs, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001378 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001379 }
1380
Radek Krejci2a9fc652021-01-22 17:44:34 +01001381 LY_LIST_FOR(modp->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001382 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001383 }
1384
1385 LY_ARRAY_FOR(modp->deviations, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001386 yprp_deviation(pctx, &modp->deviations[u]);
FredGand944bdc2019-11-05 21:57:07 +08001387 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001388}
1389
1390LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01001391yin_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001392{
1393 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7997d5a2021-02-22 10:55:56 +01001394 const struct lys_module *module = modp->mod;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001395 struct lys_ypr_ctx pctx_ = {.out = out, .level = 0, .module = module, .options = options}, *pctx = &pctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001396
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001397 ly_print_(pctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1398 ly_print_(pctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
1399 ypr_xmlns(pctx, module, XML_NS_INDENT);
1400 ypr_import_xmlns(pctx, modp, XML_NS_INDENT);
1401 ly_print_(pctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001402
1403 LEVEL++;
1404
1405 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001406 if (modp->version) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001407 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 +02001408 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001409 ypr_substmt(pctx, LY_STMT_NAMESPACE, 0, module->ns, modp->exts);
1410 ypr_substmt(pctx, LY_STMT_PREFIX, 0, module->prefix, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001411
1412 /* linkage-stmts (import/include) */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001413 yin_print_parsed_linkage(pctx, modp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001414
1415 /* meta-stmts */
1416 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001417 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001418 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001419 ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, module->org, modp->exts);
1420 ypr_substmt(pctx, LY_STMT_CONTACT, 0, module->contact, modp->exts);
1421 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, module->dsc, modp->exts);
1422 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, module->ref, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001423
1424 /* revision-stmts */
1425 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001426 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001427 }
1428 LY_ARRAY_FOR(modp->revs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001429 yprp_revision(pctx, &modp->revs[u]);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001430 }
1431
1432 /* body-stmts */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001433 yin_print_parsed_body(pctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001434
1435 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001436 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001437 ly_print_flush(out);
1438
1439 return LY_SUCCESS;
1440}
1441
Michal Vasko7c8439f2020-08-05 13:25:19 +02001442static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001443yprp_belongsto(struct lys_ypr_ctx *pctx, const struct lysp_submodule *submodp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001444{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001445 ypr_open(pctx, "belongs-to", "module", submodp->mod->name, 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001446 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +02001447 yprp_extension_instances(pctx, LY_STMT_BELONGS_TO, 0, submodp->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001448 ypr_substmt(pctx, LY_STMT_PREFIX, 0, submodp->prefix, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001449 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001450 ypr_close(pctx, "belongs-to", 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001451}
1452
1453LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01001454yin_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001455{
1456 LY_ARRAY_COUNT_TYPE u;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001457 struct lys_ypr_ctx pctx_ = {.out = out, .level = 0, .module = submodp->mod, .options = options}, *pctx = &pctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001458
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001459 ly_print_(pctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1460 ly_print_(pctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
1461 ypr_xmlns(pctx, submodp->mod, XML_NS_INDENT);
1462 ypr_import_xmlns(pctx, (struct lysp_module *)submodp, XML_NS_INDENT);
1463 ly_print_(pctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001464
1465 LEVEL++;
1466
1467 /* submodule-header-stmts */
1468 if (submodp->version) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001469 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 +02001470 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001471 yprp_belongsto(pctx, submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001472
1473 /* linkage-stmts (import/include) */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001474 yin_print_parsed_linkage(pctx, (struct lysp_module *)submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001475
1476 /* meta-stmts */
1477 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001478 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001479 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001480 ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1481 ypr_substmt(pctx, LY_STMT_CONTACT, 0, submodp->contact, submodp->exts);
1482 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1483 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, submodp->ref, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001484
1485 /* revision-stmts */
1486 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001487 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001488 }
1489 LY_ARRAY_FOR(submodp->revs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001490 yprp_revision(pctx, &submodp->revs[u]);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001491 }
1492
1493 /* body-stmts */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001494 yin_print_parsed_body(pctx, (struct lysp_module *)submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001495
1496 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001497 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001498 ly_print_flush(out);
1499
1500 return LY_SUCCESS;
1501}