blob: 5ccea3073019899a537287cdaa9491ad07ffc9b5 [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 Krejci1deb5be2020-08-26 16:43:36 +020037 struct ly_out *out; /**< output specification */
38 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
39 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
FredGand944bdc2019-11-05 21:57:07 +080040 const struct lys_module *module; /**< schema to print */
41};
42
Radek Krejcifc596f92021-02-26 22:40:26 +010043static void yprp_extension_instances(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +020044 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count);
FredGand944bdc2019-11-05 21:57:07 +080045
46static void
Radek Krejciadcf63d2021-02-09 10:21:18 +010047ypr_open(struct lys_ypr_ctx *ctx, const char *elem_name, const char *attr_name, const char *attr_value, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080048{
Michal Vasko5233e962020-08-14 14:26:20 +020049 ly_print_(ctx->out, "%*s<%s", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080050
51 if (attr_name) {
Michal Vasko5233e962020-08-14 14:26:20 +020052 ly_print_(ctx->out, " %s=\"", attr_name);
FredGand944bdc2019-11-05 21:57:07 +080053 lyxml_dump_text(ctx->out, attr_value, 1);
Michal Vasko5233e962020-08-14 14:26:20 +020054 ly_print_(ctx->out, "\"%s", flag == -1 ? "/>\n" : flag == 1 ? ">\n" : "");
FredGand944bdc2019-11-05 21:57:07 +080055 } else if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +020056 ly_print_(ctx->out, flag == -1 ? "/>\n" : ">\n");
FredGand944bdc2019-11-05 21:57:07 +080057 }
58}
59
60static void
Radek Krejciadcf63d2021-02-09 10:21:18 +010061ypr_close(struct lys_ypr_ctx *ctx, const char *elem_name, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080062{
63 if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +020064 ly_print_(ctx->out, "%*s</%s>\n", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080065 } else {
Michal Vasko5233e962020-08-14 14:26:20 +020066 ly_print_(ctx->out, "/>\n");
FredGand944bdc2019-11-05 21:57:07 +080067 }
68}
69
70/*
71 * par_close_flag
72 * 0 - parent not yet closed, printing >\n, setting flag to 1
73 * 1 or NULL - parent already closed, do nothing
74 */
75static void
Radek Krejciadcf63d2021-02-09 10:21:18 +010076ypr_close_parent(struct lys_ypr_ctx *ctx, int8_t *par_close_flag)
FredGand944bdc2019-11-05 21:57:07 +080077{
78 if (par_close_flag && !(*par_close_flag)) {
79 (*par_close_flag) = 1;
Michal Vasko5233e962020-08-14 14:26:20 +020080 ly_print_(ctx->out, ">\n");
FredGand944bdc2019-11-05 21:57:07 +080081 }
82}
83
84static void
Radek Krejciadcf63d2021-02-09 10:21:18 +010085ypr_yin_arg(struct lys_ypr_ctx *ctx, const char *arg, const char *text)
FredGand944bdc2019-11-05 21:57:07 +080086{
Michal Vasko5233e962020-08-14 14:26:20 +020087 ly_print_(ctx->out, "%*s<%s>", INDENT, arg);
FredGand944bdc2019-11-05 21:57:07 +080088 lyxml_dump_text(ctx->out, text, 0);
Michal Vasko5233e962020-08-14 14:26:20 +020089 ly_print_(ctx->out, "</%s>\n", arg);
FredGand944bdc2019-11-05 21:57:07 +080090}
91
FredGand944bdc2019-11-05 21:57:07 +080092static void
Radek Krejcifc596f92021-02-26 22:40:26 +010093ypr_substmt(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index, const char *text, void *ext)
FredGand944bdc2019-11-05 21:57:07 +080094{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020095 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +020096 int8_t extflag = 0;
FredGand944bdc2019-11-05 21:57:07 +080097
98 if (!text) {
99 /* nothing to print */
100 return;
101 }
102
Radek Krejcieccf6602021-02-05 19:42:54 +0100103 if (stmt_attr_info[substmt].flags & STMT_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +0800104 extflag = 1;
Radek Krejcieccf6602021-02-05 19:42:54 +0100105 ypr_open(ctx, stmt_attr_info[substmt].name, NULL, NULL, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800106 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +0100107 ypr_open(ctx, stmt_attr_info[substmt].name, stmt_attr_info[substmt].arg, text, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800108 }
109
110 LEVEL++;
111 LY_ARRAY_FOR(ext, u) {
Radek Krejciab430862021-03-02 20:13:40 +0100112 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 +0800113 continue;
114 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200115 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800116 }
117
118 /* argument as yin-element */
Radek Krejcieccf6602021-02-05 19:42:54 +0100119 if (stmt_attr_info[substmt].flags & STMT_FLAG_YIN) {
120 ypr_yin_arg(ctx, stmt_attr_info[substmt].arg, text);
FredGand944bdc2019-11-05 21:57:07 +0800121 }
122
123 LEVEL--;
Radek Krejcieccf6602021-02-05 19:42:54 +0100124 ypr_close(ctx, stmt_attr_info[substmt].name, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800125}
126
127static void
Radek Krejcifc596f92021-02-26 22:40:26 +0100128ypr_unsigned(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, unsigned long int attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800129{
130 char *str;
Michal Vasko69730152020-10-09 16:30:07 +0200131
Radek Krejci1deb5be2020-08-26 16:43:36 +0200132 if (asprintf(&str, "%lu", attr_value) == -1) {
FredGand944bdc2019-11-05 21:57:07 +0800133 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800134 return;
135 }
136 ypr_substmt(ctx, substmt, substmt_index, str, exts);
137 free(str);
138}
139
140static void
Radek Krejcifc596f92021-02-26 22:40:26 +0100141ypr_signed(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, signed long int attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800142{
143 char *str;
144
Radek Krejci1deb5be2020-08-26 16:43:36 +0200145 if (asprintf(&str, "%ld", attr_value) == -1) {
FredGand944bdc2019-11-05 21:57:07 +0800146 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800147 return;
148 }
149 ypr_substmt(ctx, substmt, substmt_index, str, exts);
150 free(str);
151}
152
153static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100154yprp_revision(struct lys_ypr_ctx *ctx, const struct lysp_revision *rev)
FredGand944bdc2019-11-05 21:57:07 +0800155{
156 if (rev->dsc || rev->ref || rev->exts) {
157 ypr_open(ctx, "revision", "date", rev->date, 1);
158 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100159 yprp_extension_instances(ctx, LY_STMT_REVISION, 0, rev->exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +0100160 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, rev->dsc, rev->exts);
161 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, rev->ref, rev->exts);
FredGand944bdc2019-11-05 21:57:07 +0800162 LEVEL--;
163 ypr_close(ctx, "revision", 1);
164 } else {
165 ypr_open(ctx, "revision", "date", rev->date, -1);
166 }
167}
168
169static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100170ypr_mandatory(struct lys_ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800171{
172 if (flags & LYS_MAND_MASK) {
173 ypr_close_parent(ctx, flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100174 ypr_substmt(ctx, LY_STMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
FredGand944bdc2019-11-05 21:57:07 +0800175 }
176}
177
178static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100179ypr_config(struct lys_ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800180{
181 if (flags & LYS_CONFIG_MASK) {
182 ypr_close_parent(ctx, flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100183 ypr_substmt(ctx, LY_STMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
FredGand944bdc2019-11-05 21:57:07 +0800184 }
185}
186
187static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100188ypr_status(struct lys_ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800189{
190 const char *status = NULL;
191
192 if (flags & LYS_STATUS_CURR) {
193 ypr_close_parent(ctx, flag);
194 status = "current";
195 } else if (flags & LYS_STATUS_DEPRC) {
196 ypr_close_parent(ctx, flag);
197 status = "deprecated";
198 } else if (flags & LYS_STATUS_OBSLT) {
199 ypr_close_parent(ctx, flag);
200 status = "obsolete";
201 }
202
Radek Krejcifc596f92021-02-26 22:40:26 +0100203 ypr_substmt(ctx, LY_STMT_STATUS, 0, status, exts);
FredGand944bdc2019-11-05 21:57:07 +0800204}
205
206static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100207ypr_description(struct lys_ypr_ctx *ctx, const char *dsc, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800208{
209 if (dsc) {
210 ypr_close_parent(ctx, flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100211 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, dsc, exts);
FredGand944bdc2019-11-05 21:57:07 +0800212 }
213}
214
215static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100216ypr_reference(struct lys_ypr_ctx *ctx, const char *ref, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800217{
218 if (ref) {
219 ypr_close_parent(ctx, flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100220 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, ref, exts);
FredGand944bdc2019-11-05 21:57:07 +0800221 }
222}
223
224static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100225yprp_iffeatures(struct lys_ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800226{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200227 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200228 int8_t extflag;
FredGand944bdc2019-11-05 21:57:07 +0800229
Michal Vasko7f45cf22020-10-01 12:49:44 +0200230 LY_ARRAY_FOR(iffs, u) {
FredGand944bdc2019-11-05 21:57:07 +0800231 ypr_close_parent(ctx, flag);
232 extflag = 0;
233
Michal Vasko7f45cf22020-10-01 12:49:44 +0200234 ly_print_(ctx->out, "%*s<if-feature name=\"%s", INDENT, iffs[u].str);
FredGand944bdc2019-11-05 21:57:07 +0800235
236 /* extensions */
237 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200238 LY_ARRAY_FOR(exts, v) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100239 yprp_extension_instances(ctx, LY_STMT_IF_FEATURE, u, &exts[v], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800240 }
241 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200242 ly_print_(ctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800243 }
244}
245
246static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100247yprp_extension(struct lys_ypr_ctx *ctx, const struct lysp_ext *ext)
FredGand944bdc2019-11-05 21:57:07 +0800248{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200249 int8_t flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200250 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800251
252 ypr_open(ctx, "extension", "name", ext->name, flag);
253 LEVEL++;
254
255 if (ext->exts) {
256 ypr_close_parent(ctx, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +0100257 yprp_extension_instances(ctx, LY_STMT_EXTENSION, 0, ext->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800258 }
259
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100260 if (ext->argname) {
FredGand944bdc2019-11-05 21:57:07 +0800261 ypr_close_parent(ctx, &flag);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100262 ypr_open(ctx, "argument", "name", ext->argname, flag2);
FredGand944bdc2019-11-05 21:57:07 +0800263
264 LEVEL++;
265 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200266 u = -1;
Radek Krejcifc596f92021-02-26 22:40:26 +0100267 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LY_STMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
FredGand944bdc2019-11-05 21:57:07 +0800268 ypr_close_parent(ctx, &flag2);
Radek Krejcifc596f92021-02-26 22:40:26 +0100269 yprp_extension_instances(ctx, LY_STMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
FredGand944bdc2019-11-05 21:57:07 +0800270 }
271 }
272 if ((ext->flags & LYS_YINELEM_MASK) ||
Radek Krejcifc596f92021-02-26 22:40:26 +0100273 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LY_STMT_YIN_ELEMENT) != LY_ARRAY_COUNT(ext->exts)))) {
FredGand944bdc2019-11-05 21:57:07 +0800274 ypr_close_parent(ctx, &flag2);
Radek Krejcifc596f92021-02-26 22:40:26 +0100275 ypr_substmt(ctx, LY_STMT_YIN_ELEMENT, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
FredGand944bdc2019-11-05 21:57:07 +0800276 }
277 LEVEL--;
278 ypr_close(ctx, "argument", flag2);
279 }
280
281 ypr_status(ctx, ext->flags, ext->exts, &flag);
282 ypr_description(ctx, ext->dsc, ext->exts, &flag);
283 ypr_reference(ctx, ext->ref, ext->exts, &flag);
284
285 LEVEL--;
286 ypr_close(ctx, "extension", flag);
287}
288
289static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100290yprp_feature(struct lys_ypr_ctx *ctx, const struct lysp_feature *feat)
FredGand944bdc2019-11-05 21:57:07 +0800291{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200292 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800293
294 ypr_open(ctx, "feature", "name", feat->name, flag);
295 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100296 yprp_extension_instances(ctx, LY_STMT_FEATURE, 0, feat->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800297 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
298 ypr_status(ctx, feat->flags, feat->exts, &flag);
299 ypr_description(ctx, feat->dsc, feat->exts, &flag);
300 ypr_reference(ctx, feat->ref, feat->exts, &flag);
301 LEVEL--;
302 ypr_close(ctx, "feature", flag);
303}
304
305static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100306yprp_identity(struct lys_ypr_ctx *ctx, const struct lysp_ident *ident)
FredGand944bdc2019-11-05 21:57:07 +0800307{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200308 int8_t flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200309 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800310
311 ypr_open(ctx, "identity", "name", ident->name, flag);
312 LEVEL++;
313
Radek Krejci39b7fc22021-02-26 23:29:18 +0100314 yprp_extension_instances(ctx, LY_STMT_IDENTITY, 0, ident->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800315 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
316
317 LY_ARRAY_FOR(ident->bases, u) {
318 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100319 ypr_substmt(ctx, LY_STMT_BASE, u, ident->bases[u], ident->exts);
FredGand944bdc2019-11-05 21:57:07 +0800320 }
321
322 ypr_status(ctx, ident->flags, ident->exts, &flag);
323 ypr_description(ctx, ident->dsc, ident->exts, &flag);
324 ypr_reference(ctx, ident->ref, ident->exts, &flag);
325
326 LEVEL--;
327 ypr_close(ctx, "identity", flag);
328}
329
330static void
Radek Krejci39b7fc22021-02-26 23:29:18 +0100331yprp_restr(struct lys_ypr_ctx *ctx, const struct lysp_restr *restr, enum ly_stmt stmt, const char *attr, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800332{
333 (void)flag;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200334 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800335
336 if (!restr) {
337 return;
338 }
339
Radek Krejci39b7fc22021-02-26 23:29:18 +0100340 ly_print_(ctx->out, "%*s<%s %s=\"", INDENT, ly_stmt2str(stmt), attr);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 lyxml_dump_text(ctx->out,
342 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
343 restr->arg.str : &restr->arg.str[1], 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200344 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800345
346 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100347 yprp_extension_instances(ctx, stmt, 0, restr->exts, &inner_flag, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100348 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
FredGand944bdc2019-11-05 21:57:07 +0800349 ypr_close_parent(ctx, &inner_flag);
350 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
Radek Krejcifc596f92021-02-26 22:40:26 +0100351 ypr_substmt(ctx, LY_STMT_MODIFIER, 0, "invert-match", restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800352 }
353 if (restr->emsg) {
354 ypr_close_parent(ctx, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100355 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, restr->emsg, restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800356 }
357 if (restr->eapptag) {
358 ypr_close_parent(ctx, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100359 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, restr->eapptag, restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800360 }
361 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
362 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
363
364 LEVEL--;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100365 ypr_close(ctx, ly_stmt2str(stmt), inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800366}
367
368static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100369yprp_when(struct lys_ypr_ctx *ctx, struct lysp_when *when, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800370{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200371 int8_t inner_flag = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200372
FredGand944bdc2019-11-05 21:57:07 +0800373 (void)flag;
374
375 if (!when) {
376 return;
377 }
378
Michal Vasko5233e962020-08-14 14:26:20 +0200379 ly_print_(ctx->out, "%*s<when condition=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800380 lyxml_dump_text(ctx->out, when->cond, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200381 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800382
383 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100384 yprp_extension_instances(ctx, LY_STMT_WHEN, 0, when->exts, &inner_flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800385 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
386 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
387 LEVEL--;
388 ypr_close(ctx, "when", inner_flag);
389}
390
391static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100392yprp_enum(struct lys_ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800393{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200394 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200395 int8_t inner_flag;
Michal Vasko69730152020-10-09 16:30:07 +0200396
FredGand944bdc2019-11-05 21:57:07 +0800397 (void)flag;
398
399 LY_ARRAY_FOR(items, u) {
400 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200401 ly_print_(ctx->out, "%*s<bit name=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800402 lyxml_dump_text(ctx->out, items[u].name, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200403 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800404 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200405 ly_print_(ctx->out, "%*s<enum name=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800406 lyxml_dump_text(ctx->out, items[u].name, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200407 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800408 }
409 inner_flag = 0;
410 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100411 yprp_extension_instances(ctx, LY_STMT_ENUM, 0, items[u].exts, &inner_flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800412 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
413 if (items[u].flags & LYS_SET_VALUE) {
414 if (type == LY_TYPE_BITS) {
415 ypr_close_parent(ctx, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100416 ypr_unsigned(ctx, LY_STMT_POSITION, 0, items[u].exts, items[u].value);
FredGand944bdc2019-11-05 21:57:07 +0800417 } else { /* LY_TYPE_ENUM */
418 ypr_close_parent(ctx, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100419 ypr_signed(ctx, LY_STMT_VALUE, 0, items[u].exts, items[u].value);
FredGand944bdc2019-11-05 21:57:07 +0800420 }
421 }
422 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
423 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
424 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
425 LEVEL--;
426 ypr_close(ctx, type == LY_TYPE_BITS ? "bit" : "enum", inner_flag);
427 }
428}
429
430static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100431yprp_type(struct lys_ypr_ctx *ctx, const struct lysp_type *type)
FredGand944bdc2019-11-05 21:57:07 +0800432{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200433 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200434 int8_t flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200435
FredGand944bdc2019-11-05 21:57:07 +0800436 if (!ctx || !type) {
437 return;
438 }
439
FredGand944bdc2019-11-05 21:57:07 +0800440 ypr_open(ctx, "type", "name", type->name, flag);
441 LEVEL++;
442
Radek Krejci39b7fc22021-02-26 23:29:18 +0100443 yprp_extension_instances(ctx, LY_STMT_TYPE, 0, type->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800444
445 if (type->range || type->length || type->patterns || type->bits || type->enums) {
446 ypr_close_parent(ctx, &flag);
447 }
Radek Krejci39b7fc22021-02-26 23:29:18 +0100448 yprp_restr(ctx, type->range, LY_STMT_RANGE, "value", &flag);
449 yprp_restr(ctx, type->length, LY_STMT_LENGTH, "value", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800450 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100451 yprp_restr(ctx, &type->patterns[u], LY_STMT_PATTERN, "value", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800452 }
453 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
454 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
455
456 if (type->path) {
457 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100458 ypr_substmt(ctx, LY_STMT_PATH, 0, type->path->expr, type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800459 }
460 if (type->flags & LYS_SET_REQINST) {
461 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100462 ypr_substmt(ctx, LY_STMT_REQUIRE_INSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800463 }
464 if (type->flags & LYS_SET_FRDIGITS) {
465 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100466 ypr_unsigned(ctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, type->fraction_digits);
FredGand944bdc2019-11-05 21:57:07 +0800467 }
468 LY_ARRAY_FOR(type->bases, u) {
469 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100470 ypr_substmt(ctx, LY_STMT_BASE, u, type->bases[u], type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800471 }
472 LY_ARRAY_FOR(type->types, u) {
473 ypr_close_parent(ctx, &flag);
474 yprp_type(ctx, &type->types[u]);
475 }
476
477 LEVEL--;
478 ypr_close(ctx, "type", flag);
479}
480
481static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100482yprp_typedef(struct lys_ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
FredGand944bdc2019-11-05 21:57:07 +0800483{
FredGand944bdc2019-11-05 21:57:07 +0800484 ypr_open(ctx, "typedef", "name", tpdf->name, 1);
485 LEVEL++;
486
Radek Krejci39b7fc22021-02-26 23:29:18 +0100487 yprp_extension_instances(ctx, LY_STMT_TYPEDEF, 0, tpdf->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +0800488
489 yprp_type(ctx, &tpdf->type);
490
491 if (tpdf->units) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100492 ypr_substmt(ctx, LY_STMT_UNITS, 0, tpdf->units, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800493 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200494 if (tpdf->dflt.str) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100495 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800496 }
497
498 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
499 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
500 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
501
502 LEVEL--;
503 ypr_close(ctx, "typedef", 1);
504}
505
Radek Krejciadcf63d2021-02-09 10:21:18 +0100506static void yprp_node(struct lys_ypr_ctx *ctx, const struct lysp_node *node);
507static void yprp_action(struct lys_ypr_ctx *ctx, const struct lysp_node_action *action);
508static void yprp_notification(struct lys_ypr_ctx *ctx, const struct lysp_node_notif *notif);
FredGand944bdc2019-11-05 21:57:07 +0800509
510static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100511yprp_grouping(struct lys_ypr_ctx *ctx, const struct lysp_node_grp *grp)
FredGand944bdc2019-11-05 21:57:07 +0800512{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200513 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200514 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800515 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100516 struct lysp_node_action *action;
517 struct lysp_node_notif *notif;
518 struct lysp_node_grp *subgrp;
FredGand944bdc2019-11-05 21:57:07 +0800519
FredGand944bdc2019-11-05 21:57:07 +0800520 ypr_open(ctx, "grouping", "name", grp->name, flag);
521 LEVEL++;
522
Radek Krejci39b7fc22021-02-26 23:29:18 +0100523 yprp_extension_instances(ctx, LY_STMT_GROUPING, 0, grp->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800524 ypr_status(ctx, grp->flags, grp->exts, &flag);
525 ypr_description(ctx, grp->dsc, grp->exts, &flag);
526 ypr_reference(ctx, grp->ref, grp->exts, &flag);
527
528 LY_ARRAY_FOR(grp->typedefs, u) {
529 ypr_close_parent(ctx, &flag);
530 yprp_typedef(ctx, &grp->typedefs[u]);
531 }
532
Radek Krejci2a9fc652021-01-22 17:44:34 +0100533 LY_LIST_FOR(grp->groupings, subgrp) {
FredGand944bdc2019-11-05 21:57:07 +0800534 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100535 yprp_grouping(ctx, subgrp);
FredGand944bdc2019-11-05 21:57:07 +0800536 }
537
Radek Krejci01180ac2021-01-27 08:48:22 +0100538 LY_LIST_FOR(grp->child, data) {
FredGand944bdc2019-11-05 21:57:07 +0800539 ypr_close_parent(ctx, &flag);
540 yprp_node(ctx, data);
541 }
542
Radek Krejci2a9fc652021-01-22 17:44:34 +0100543 LY_LIST_FOR(grp->actions, action) {
FredGand944bdc2019-11-05 21:57:07 +0800544 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100545 yprp_action(ctx, action);
546 }
547
548 LY_LIST_FOR(grp->notifs, notif) {
549 ypr_close_parent(ctx, &flag);
550 yprp_notification(ctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800551 }
552
553 LEVEL--;
554 ypr_close(ctx, "grouping", flag);
555}
556
557static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100558yprp_inout(struct lys_ypr_ctx *ctx, const struct lysp_node_action_inout *inout, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800559{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200560 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800561 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100562 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800563
Radek Krejci01180ac2021-01-27 08:48:22 +0100564 if (!inout->child) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200565 /* input/output is empty */
FredGand944bdc2019-11-05 21:57:07 +0800566 return;
567 }
568 ypr_close_parent(ctx, flag);
569
Michal Vasko544e58a2021-01-28 14:33:41 +0100570 ypr_open(ctx, inout->name, NULL, NULL, *flag);
FredGand944bdc2019-11-05 21:57:07 +0800571 LEVEL++;
572
Radek Krejci39b7fc22021-02-26 23:29:18 +0100573 yprp_extension_instances(ctx, lys_nodetype2stmt(inout->nodetype), 0, inout->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +0800574 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100575 yprp_restr(ctx, &inout->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +0800576 }
577 LY_ARRAY_FOR(inout->typedefs, u) {
578 yprp_typedef(ctx, &inout->typedefs[u]);
579 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100580 LY_LIST_FOR(inout->groupings, grp) {
581 yprp_grouping(ctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800582 }
583
Radek Krejci01180ac2021-01-27 08:48:22 +0100584 LY_LIST_FOR(inout->child, data) {
FredGand944bdc2019-11-05 21:57:07 +0800585 yprp_node(ctx, data);
586 }
587
588 LEVEL--;
Michal Vasko544e58a2021-01-28 14:33:41 +0100589 ypr_close(ctx, inout->name, 1);
FredGand944bdc2019-11-05 21:57:07 +0800590}
591
592static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100593yprp_notification(struct lys_ypr_ctx *ctx, const struct lysp_node_notif *notif)
FredGand944bdc2019-11-05 21:57:07 +0800594{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200595 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200596 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800597 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100598 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800599
FredGand944bdc2019-11-05 21:57:07 +0800600 ypr_open(ctx, "notification", "name", notif->name, flag);
601
602 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100603 yprp_extension_instances(ctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800604 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
605
606 LY_ARRAY_FOR(notif->musts, u) {
607 ypr_close_parent(ctx, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +0100608 yprp_restr(ctx, &notif->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800609 }
610 ypr_status(ctx, notif->flags, notif->exts, &flag);
611 ypr_description(ctx, notif->dsc, notif->exts, &flag);
612 ypr_reference(ctx, notif->ref, notif->exts, &flag);
613
614 LY_ARRAY_FOR(notif->typedefs, u) {
615 ypr_close_parent(ctx, &flag);
616 yprp_typedef(ctx, &notif->typedefs[u]);
617 }
618
Radek Krejci2a9fc652021-01-22 17:44:34 +0100619 LY_LIST_FOR(notif->groupings, grp) {
FredGand944bdc2019-11-05 21:57:07 +0800620 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100621 yprp_grouping(ctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800622 }
623
Radek Krejci01180ac2021-01-27 08:48:22 +0100624 LY_LIST_FOR(notif->child, data) {
FredGand944bdc2019-11-05 21:57:07 +0800625 ypr_close_parent(ctx, &flag);
626 yprp_node(ctx, data);
627 }
628
629 LEVEL--;
630 ypr_close(ctx, "notification", flag);
631}
632
633static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100634yprp_action(struct lys_ypr_ctx *ctx, const struct lysp_node_action *action)
FredGand944bdc2019-11-05 21:57:07 +0800635{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200636 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200637 int8_t flag = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100638 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800639
FredGand944bdc2019-11-05 21:57:07 +0800640 ypr_open(ctx, action->parent ? "action" : "rpc", "name", action->name, flag);
641
642 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100643 yprp_extension_instances(ctx, lys_nodetype2stmt(action->nodetype), 0, action->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800644 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
645 ypr_status(ctx, action->flags, action->exts, &flag);
646 ypr_description(ctx, action->dsc, action->exts, &flag);
647 ypr_reference(ctx, action->ref, action->exts, &flag);
648
649 LY_ARRAY_FOR(action->typedefs, u) {
650 ypr_close_parent(ctx, &flag);
651 yprp_typedef(ctx, &action->typedefs[u]);
652 }
653
Radek Krejci2a9fc652021-01-22 17:44:34 +0100654 LY_LIST_FOR(action->groupings, grp) {
FredGand944bdc2019-11-05 21:57:07 +0800655 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100656 yprp_grouping(ctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800657 }
658
659 yprp_inout(ctx, &action->input, &flag);
660 yprp_inout(ctx, &action->output, &flag);
661
662 LEVEL--;
663 ypr_close(ctx, action->parent ? "action" : "rpc", flag);
664}
665
666static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100667yprp_node_common1(struct lys_ypr_ctx *ctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800668{
669 ypr_open(ctx, lys_nodetype2str(node->nodetype), "name", node->name, *flag);
670 LEVEL++;
671
Radek Krejci39b7fc22021-02-26 23:29:18 +0100672 yprp_extension_instances(ctx, lys_nodetype2stmt(node->nodetype), 0, node->exts, flag, 0);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100673 yprp_when(ctx, lysp_node_when(node), flag);
FredGand944bdc2019-11-05 21:57:07 +0800674 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
675}
676
677static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100678yprp_node_common2(struct lys_ypr_ctx *ctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800679{
680 ypr_config(ctx, node->flags, node->exts, flag);
681 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) {
682 ypr_mandatory(ctx, node->flags, node->exts, flag);
683 }
684 ypr_status(ctx, node->flags, node->exts, flag);
685 ypr_description(ctx, node->dsc, node->exts, flag);
686 ypr_reference(ctx, node->ref, node->exts, flag);
687}
688
689static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100690yprp_container(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800691{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200692 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200693 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800694 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100695 struct lysp_node_action *action;
696 struct lysp_node_notif *notif;
697 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800698 struct lysp_node_container *cont = (struct lysp_node_container *)node;
699
700 yprp_node_common1(ctx, node, &flag);
701
702 LY_ARRAY_FOR(cont->musts, u) {
703 ypr_close_parent(ctx, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +0100704 yprp_restr(ctx, &cont->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800705 }
706 if (cont->presence) {
707 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100708 ypr_substmt(ctx, LY_STMT_PRESENCE, 0, cont->presence, cont->exts);
FredGand944bdc2019-11-05 21:57:07 +0800709 }
710
711 yprp_node_common2(ctx, node, &flag);
712
713 LY_ARRAY_FOR(cont->typedefs, u) {
714 ypr_close_parent(ctx, &flag);
715 yprp_typedef(ctx, &cont->typedefs[u]);
716 }
717
Radek Krejci2a9fc652021-01-22 17:44:34 +0100718 LY_LIST_FOR(cont->groupings, grp) {
FredGand944bdc2019-11-05 21:57:07 +0800719 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100720 yprp_grouping(ctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800721 }
722
723 LY_LIST_FOR(cont->child, child) {
724 ypr_close_parent(ctx, &flag);
725 yprp_node(ctx, child);
726 }
727
Radek Krejci2a9fc652021-01-22 17:44:34 +0100728 LY_LIST_FOR(cont->actions, action) {
FredGand944bdc2019-11-05 21:57:07 +0800729 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100730 yprp_action(ctx, action);
FredGand944bdc2019-11-05 21:57:07 +0800731 }
732
Radek Krejci2a9fc652021-01-22 17:44:34 +0100733 LY_LIST_FOR(cont->notifs, notif) {
FredGand944bdc2019-11-05 21:57:07 +0800734 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100735 yprp_notification(ctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800736 }
737
738 LEVEL--;
739 ypr_close(ctx, "container", flag);
740}
741
742static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100743yprp_case(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800744{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200745 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800746 struct lysp_node *child;
747 struct lysp_node_case *cas = (struct lysp_node_case *)node;
748
749 yprp_node_common1(ctx, node, &flag);
750 yprp_node_common2(ctx, node, &flag);
751
752 LY_LIST_FOR(cas->child, child) {
753 ypr_close_parent(ctx, &flag);
754 yprp_node(ctx, child);
755 }
756
757 LEVEL--;
758 ypr_close(ctx, "case", flag);
759}
760
761static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100762yprp_choice(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800763{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200764 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800765 struct lysp_node *child;
766 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
767
768 yprp_node_common1(ctx, node, &flag);
769
Michal Vasko7f45cf22020-10-01 12:49:44 +0200770 if (choice->dflt.str) {
FredGand944bdc2019-11-05 21:57:07 +0800771 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100772 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, choice->dflt.str, choice->exts);
FredGand944bdc2019-11-05 21:57:07 +0800773 }
774
775 yprp_node_common2(ctx, node, &flag);
776
777 LY_LIST_FOR(choice->child, child) {
778 ypr_close_parent(ctx, &flag);
779 yprp_node(ctx, child);
780 }
781
782 LEVEL--;
783 ypr_close(ctx, "choice", flag);
784}
785
786static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100787yprp_leaf(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800788{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200789 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800790 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
791
Radek Krejci1deb5be2020-08-26 16:43:36 +0200792 int8_t flag = 1;
Michal Vasko69730152020-10-09 16:30:07 +0200793
FredGand944bdc2019-11-05 21:57:07 +0800794 yprp_node_common1(ctx, node, &flag);
795
796 yprp_type(ctx, &leaf->type);
Radek Krejcifc596f92021-02-26 22:40:26 +0100797 ypr_substmt(ctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800798 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100799 yprp_restr(ctx, &leaf->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800800 }
Radek Krejcifc596f92021-02-26 22:40:26 +0100801 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800802
803 yprp_node_common2(ctx, node, &flag);
804
805 LEVEL--;
806 ypr_close(ctx, "leaf", flag);
807}
808
809static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100810yprp_leaflist(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800811{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200812 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800813 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200814 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800815
816 yprp_node_common1(ctx, node, &flag);
817
818 yprp_type(ctx, &llist->type);
Radek Krejcifc596f92021-02-26 22:40:26 +0100819 ypr_substmt(ctx, LY_STMT_UNITS, 0, llist->units, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800820 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100821 yprp_restr(ctx, &llist->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +0800822 }
823 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100824 ypr_substmt(ctx, LY_STMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800825 }
826
827 ypr_config(ctx, node->flags, node->exts, NULL);
828
829 if (llist->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100830 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min);
FredGand944bdc2019-11-05 21:57:07 +0800831 }
832 if (llist->flags & LYS_SET_MAX) {
833 if (llist->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100834 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max);
FredGand944bdc2019-11-05 21:57:07 +0800835 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +0100836 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800837 }
838 }
839
840 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100841 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800842 }
843
844 ypr_status(ctx, node->flags, node->exts, &flag);
845 ypr_description(ctx, node->dsc, node->exts, &flag);
846 ypr_reference(ctx, node->ref, node->exts, &flag);
847
848 LEVEL--;
849 ypr_close(ctx, "leaf-list", flag);
850}
851
852static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100853yprp_list(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800854{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200855 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200856 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800857 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100858 struct lysp_node_action *action;
859 struct lysp_node_notif *notif;
860 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800861 struct lysp_node_list *list = (struct lysp_node_list *)node;
862
863 yprp_node_common1(ctx, node, &flag);
864
865 LY_ARRAY_FOR(list->musts, u) {
866 ypr_close_parent(ctx, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +0100867 yprp_restr(ctx, &list->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800868 }
869 if (list->key) {
870 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100871 ypr_substmt(ctx, LY_STMT_KEY, 0, list->key, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800872 }
873 LY_ARRAY_FOR(list->uniques, u) {
874 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100875 ypr_substmt(ctx, LY_STMT_UNIQUE, u, list->uniques[u].str, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800876 }
877
878 ypr_config(ctx, node->flags, node->exts, NULL);
879
880 if (list->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100881 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min);
FredGand944bdc2019-11-05 21:57:07 +0800882 }
883 if (list->flags & LYS_SET_MAX) {
884 if (list->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100885 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max);
FredGand944bdc2019-11-05 21:57:07 +0800886 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +0100887 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800888 }
889 }
890
891 if (list->flags & LYS_ORDBY_MASK) {
892 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100893 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800894 }
895
896 ypr_status(ctx, node->flags, node->exts, &flag);
897 ypr_description(ctx, node->dsc, node->exts, &flag);
898 ypr_reference(ctx, node->ref, node->exts, &flag);
899
900 LY_ARRAY_FOR(list->typedefs, u) {
901 ypr_close_parent(ctx, &flag);
902 yprp_typedef(ctx, &list->typedefs[u]);
903 }
904
Radek Krejci2a9fc652021-01-22 17:44:34 +0100905 LY_LIST_FOR(list->groupings, grp) {
FredGand944bdc2019-11-05 21:57:07 +0800906 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100907 yprp_grouping(ctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800908 }
909
910 LY_LIST_FOR(list->child, child) {
911 ypr_close_parent(ctx, &flag);
912 yprp_node(ctx, child);
913 }
914
Radek Krejci2a9fc652021-01-22 17:44:34 +0100915 LY_LIST_FOR(list->actions, action) {
FredGand944bdc2019-11-05 21:57:07 +0800916 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100917 yprp_action(ctx, action);
FredGand944bdc2019-11-05 21:57:07 +0800918 }
919
Radek Krejci2a9fc652021-01-22 17:44:34 +0100920 LY_LIST_FOR(list->notifs, notif) {
FredGand944bdc2019-11-05 21:57:07 +0800921 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100922 yprp_notification(ctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800923 }
924
925 LEVEL--;
926 ypr_close(ctx, "list", flag);
927}
928
929static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100930yprp_refine(struct lys_ypr_ctx *ctx, struct lysp_refine *refine)
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
935 ypr_open(ctx, "refine", "target-node", refine->nodeid, flag);
936 LEVEL++;
937
Radek Krejci39b7fc22021-02-26 23:29:18 +0100938 yprp_extension_instances(ctx, LY_STMT_REFINE, 0, refine->exts, &flag, 0);
FredGand944bdc2019-11-05 21:57:07 +0800939 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
940
941 LY_ARRAY_FOR(refine->musts, u) {
942 ypr_close_parent(ctx, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +0100943 yprp_restr(ctx, &refine->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800944 }
945
946 if (refine->presence) {
947 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100948 ypr_substmt(ctx, LY_STMT_PRESENCE, 0, refine->presence, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +0800949 }
950
951 LY_ARRAY_FOR(refine->dflts, u) {
952 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100953 ypr_substmt(ctx, LY_STMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +0800954 }
955
956 ypr_config(ctx, refine->flags, refine->exts, &flag);
957 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
958
959 if (refine->flags & LYS_SET_MIN) {
960 ypr_close_parent(ctx, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100961 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, refine->exts, refine->min);
FredGand944bdc2019-11-05 21:57:07 +0800962 }
963 if (refine->flags & LYS_SET_MAX) {
964 ypr_close_parent(ctx, &flag);
965 if (refine->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100966 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, refine->exts, refine->max);
FredGand944bdc2019-11-05 21:57:07 +0800967 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +0100968 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", refine->exts);
FredGand944bdc2019-11-05 21:57:07 +0800969 }
970 }
971
972 ypr_description(ctx, refine->dsc, refine->exts, &flag);
973 ypr_reference(ctx, refine->ref, refine->exts, &flag);
974
975 LEVEL--;
976 ypr_close(ctx, "refine", flag);
977}
978
979static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100980yprp_augment(struct lys_ypr_ctx *ctx, const struct lysp_node_augment *aug)
FredGand944bdc2019-11-05 21:57:07 +0800981{
FredGand944bdc2019-11-05 21:57:07 +0800982 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100983 struct lysp_node_action *action;
984 struct lysp_node_notif *notif;
FredGand944bdc2019-11-05 21:57:07 +0800985
986 ypr_open(ctx, "augment", "target-node", aug->nodeid, 1);
987 LEVEL++;
988
Radek Krejci39b7fc22021-02-26 23:29:18 +0100989 yprp_extension_instances(ctx, LY_STMT_AUGMENT, 0, aug->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +0800990 yprp_when(ctx, aug->when, NULL);
991 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
992 ypr_status(ctx, aug->flags, aug->exts, NULL);
993 ypr_description(ctx, aug->dsc, aug->exts, NULL);
994 ypr_reference(ctx, aug->ref, aug->exts, NULL);
995
996 LY_LIST_FOR(aug->child, child) {
997 yprp_node(ctx, child);
998 }
999
Radek Krejci2a9fc652021-01-22 17:44:34 +01001000 LY_LIST_FOR(aug->actions, action) {
1001 yprp_action(ctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001002 }
1003
Radek Krejci2a9fc652021-01-22 17:44:34 +01001004 LY_LIST_FOR(aug->notifs, notif) {
1005 yprp_notification(ctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001006 }
1007
1008 LEVEL--;
1009 ypr_close(ctx, "augment", 1);
1010}
1011
FredGand944bdc2019-11-05 21:57:07 +08001012static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001013yprp_uses(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001014{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001015 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001016 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001017 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001018 struct lysp_node_augment *aug;
FredGand944bdc2019-11-05 21:57:07 +08001019
1020 yprp_node_common1(ctx, node, &flag);
1021 yprp_node_common2(ctx, node, &flag);
1022
1023 LY_ARRAY_FOR(uses->refines, u) {
1024 ypr_close_parent(ctx, &flag);
1025 yprp_refine(ctx, &uses->refines[u]);
1026 }
1027
Radek Krejci2a9fc652021-01-22 17:44:34 +01001028 LY_LIST_FOR(uses->augments, aug) {
FredGand944bdc2019-11-05 21:57:07 +08001029 ypr_close_parent(ctx, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001030 yprp_augment(ctx, aug);
FredGand944bdc2019-11-05 21:57:07 +08001031 }
1032
1033 LEVEL--;
1034 ypr_close(ctx, "uses", flag);
1035}
1036
1037static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001038yprp_anydata(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001039{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001040 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001041 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001042 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1043
1044 yprp_node_common1(ctx, node, &flag);
1045
1046 LY_ARRAY_FOR(any->musts, u) {
1047 ypr_close_parent(ctx, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001048 yprp_restr(ctx, &any->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +08001049 }
1050
1051 yprp_node_common2(ctx, node, &flag);
1052
1053 LEVEL--;
1054 ypr_close(ctx, lys_nodetype2str(node->nodetype), flag);
1055}
1056
1057static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001058yprp_node(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001059{
FredGand944bdc2019-11-05 21:57:07 +08001060 switch (node->nodetype) {
1061 case LYS_CONTAINER:
1062 yprp_container(ctx, node);
1063 break;
1064 case LYS_CHOICE:
1065 yprp_choice(ctx, node);
1066 break;
1067 case LYS_LEAF:
1068 yprp_leaf(ctx, node);
1069 break;
1070 case LYS_LEAFLIST:
1071 yprp_leaflist(ctx, node);
1072 break;
1073 case LYS_LIST:
1074 yprp_list(ctx, node);
1075 break;
1076 case LYS_USES:
1077 yprp_uses(ctx, node);
1078 break;
1079 case LYS_ANYXML:
1080 case LYS_ANYDATA:
1081 yprp_anydata(ctx, node);
1082 break;
1083 case LYS_CASE:
1084 yprp_case(ctx, node);
1085 break;
1086 default:
1087 break;
1088 }
1089}
1090
1091static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001092yprp_deviation(struct lys_ypr_ctx *ctx, const struct lysp_deviation *deviation)
FredGand944bdc2019-11-05 21:57:07 +08001093{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001094 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001095 struct lysp_deviate_add *add;
1096 struct lysp_deviate_rpl *rpl;
1097 struct lysp_deviate_del *del;
1098 struct lysp_deviate *elem;
1099
1100 ypr_open(ctx, "deviation", "target-node", deviation->nodeid, 1);
1101 LEVEL++;
1102
Radek Krejci39b7fc22021-02-26 23:29:18 +01001103 yprp_extension_instances(ctx, LY_STMT_DEVIATION, 0, deviation->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001104 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1105 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1106
1107 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001108 ly_print_(ctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001109 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1110 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001111 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001112 LEVEL++;
1113
Radek Krejci39b7fc22021-02-26 23:29:18 +01001114 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, elem->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001115 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001116 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001117 continue;
1118 }
1119 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001120 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001121 ly_print_(ctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001122 LEVEL++;
1123
Radek Krejci39b7fc22021-02-26 23:29:18 +01001124 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, add->exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01001125 ypr_substmt(ctx, LY_STMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001126 LY_ARRAY_FOR(add->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001127 yprp_restr(ctx, &add->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001128 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001129 LY_ARRAY_FOR(add->uniques, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001130 ypr_substmt(ctx, LY_STMT_UNIQUE, u, add->uniques[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001131 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001132 LY_ARRAY_FOR(add->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001133 ypr_substmt(ctx, LY_STMT_DEFAULT, u, add->dflts[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001134 }
1135 ypr_config(ctx, add->flags, add->exts, NULL);
1136 ypr_mandatory(ctx, add->flags, add->exts, NULL);
1137 if (add->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001138 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, add->exts, add->min);
FredGand944bdc2019-11-05 21:57:07 +08001139 }
1140 if (add->flags & LYS_SET_MAX) {
1141 if (add->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001142 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, add->exts, add->max);
FredGand944bdc2019-11-05 21:57:07 +08001143 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001144 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001145 }
1146 }
1147 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001148 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001149 ly_print_(ctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001150 LEVEL++;
1151
Radek Krejci39b7fc22021-02-26 23:29:18 +01001152 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, rpl->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001153 if (rpl->type) {
1154 yprp_type(ctx, rpl->type);
1155 }
Radek Krejcifc596f92021-02-26 22:40:26 +01001156 ypr_substmt(ctx, LY_STMT_UNITS, 0, rpl->units, rpl->exts);
1157 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
FredGand944bdc2019-11-05 21:57:07 +08001158 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
1159 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
1160 if (rpl->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001161 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, rpl->exts, rpl->min);
FredGand944bdc2019-11-05 21:57:07 +08001162 }
1163 if (rpl->flags & LYS_SET_MAX) {
1164 if (rpl->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001165 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, rpl->exts, rpl->max);
FredGand944bdc2019-11-05 21:57:07 +08001166 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001167 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", rpl->exts);
FredGand944bdc2019-11-05 21:57:07 +08001168 }
1169 }
1170 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001171 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001172 ly_print_(ctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001173 LEVEL++;
1174
Radek Krejci39b7fc22021-02-26 23:29:18 +01001175 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, del->exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01001176 ypr_substmt(ctx, LY_STMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001177 LY_ARRAY_FOR(del->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001178 yprp_restr(ctx, &del->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001179 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001180 LY_ARRAY_FOR(del->uniques, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001181 ypr_substmt(ctx, LY_STMT_UNIQUE, u, del->uniques[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001182 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001183 LY_ARRAY_FOR(del->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001184 ypr_substmt(ctx, LY_STMT_DEFAULT, u, del->dflts[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001185 }
1186 }
1187
1188 LEVEL--;
1189 ypr_close(ctx, "deviate", 1);
1190 }
1191
1192 LEVEL--;
1193 ypr_close(ctx, "deviation", 1);
1194}
1195
FredGand944bdc2019-11-05 21:57:07 +08001196static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001197ypr_xmlns(struct lys_ypr_ctx *ctx, const struct lys_module *module, uint16_t indent)
FredGand944bdc2019-11-05 21:57:07 +08001198{
Michal Vasko5233e962020-08-14 14:26:20 +02001199 ly_print_(ctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1200 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001201}
FredGand944bdc2019-11-05 21:57:07 +08001202
Michal Vasko7c8439f2020-08-05 13:25:19 +02001203static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001204ypr_import_xmlns(struct lys_ypr_ctx *ctx, const struct lysp_module *modp, uint16_t indent)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001205{
1206 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001207
1208 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko5233e962020-08-14 14:26:20 +02001209 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, modp->imports[u].prefix, modp->imports[u].module->ns);
FredGand944bdc2019-11-05 21:57:07 +08001210 }
1211}
1212
FredGand944bdc2019-11-05 21:57:07 +08001213static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001214yprp_stmt(struct lys_ypr_ctx *ctx, struct lysp_stmt *stmt)
FredGand944bdc2019-11-05 21:57:07 +08001215{
1216 struct lysp_stmt *childstmt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001217 int8_t flag = stmt->child ? 1 : -1;
FredGand944bdc2019-11-05 21:57:07 +08001218
1219 /* TODO:
1220 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
1221 cannot find the compiled information, so it is needed to be done,
1222 currently it is ignored */
Michal Vaskod989ba02020-08-24 10:59:24 +02001223 if (stmt_attr_info[stmt->kw].name) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001224 if (stmt_attr_info[stmt->kw].flags & STMT_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +08001225 ypr_open(ctx, stmt->stmt, NULL, NULL, flag);
1226 ypr_yin_arg(ctx, stmt_attr_info[stmt->kw].arg, stmt->arg);
Radek Krejci0f969882020-08-21 16:56:47 +02001227 } else {
FredGand944bdc2019-11-05 21:57:07 +08001228 ypr_open(ctx, stmt->stmt, stmt_attr_info[stmt->kw].arg, stmt->arg, flag);
1229 }
1230 }
1231
1232 if (stmt->child) {
1233 LEVEL++;
1234 LY_LIST_FOR(stmt->child, childstmt) {
1235 yprp_stmt(ctx, childstmt);
1236 }
1237 LEVEL--;
1238 ypr_close(ctx, stmt->stmt, flag);
1239 }
1240}
1241
1242/**
1243 * @param[in] count Number of extensions to print, 0 to print them all.
1244 */
1245static void
Radek Krejcifc596f92021-02-26 22:40:26 +01001246yprp_extension_instances(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001247 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count)
FredGand944bdc2019-11-05 21:57:07 +08001248{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001249 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001250 struct lysp_stmt *stmt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001251 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001252
1253 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001254 count = LY_ARRAY_COUNT(ext);
FredGand944bdc2019-11-05 21:57:07 +08001255 }
1256 LY_ARRAY_FOR(ext, u) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001257 struct lysp_ext *ext_def = NULL;
1258
FredGand944bdc2019-11-05 21:57:07 +08001259 if (!count) {
1260 break;
1261 }
1262
1263 count--;
Radek Krejciab430862021-03-02 20:13:40 +01001264 if ((ext->flags & LYS_INTERNAL) || (ext->parent_stmt != substmt) || (ext->parent_stmt_index != substmt_index)) {
FredGand944bdc2019-11-05 21:57:07 +08001265 continue;
1266 }
1267
Radek Krejci85ac8312021-03-03 20:21:33 +01001268 lysp_ext_find_definition(ctx->module->ctx, &ext[u], NULL, &ext_def);
1269 if (!ext_def) {
1270 continue;
FredGand944bdc2019-11-05 21:57:07 +08001271 }
1272
Radek Krejci85ac8312021-03-03 20:21:33 +01001273 ypr_close_parent(ctx, flag);
1274 inner_flag = 0;
1275
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001276 if (ext_def->argname) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001277 lysp_ext_instance_resolve_argument(ctx->module->ctx, &ext[u], ext_def);
1278 }
1279
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001280 ypr_open(ctx, ext[u].name, (ext_def->flags & LYS_YINELEM_TRUE) ? NULL : ext_def->argname, ext[u].argument, inner_flag);
FredGand944bdc2019-11-05 21:57:07 +08001281 LEVEL++;
Radek Krejci85ac8312021-03-03 20:21:33 +01001282 if (ext_def->flags & LYS_YINELEM_TRUE) {
1283 const char *prefix, *name, *id;
1284 size_t prefix_len, name_len;
1285
1286 ypr_close_parent(ctx, &inner_flag);
1287
1288 /* we need to use the same namespace as for the extension instance element */
1289 id = ext[u].name;
1290 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001291 ly_print_(ctx->out, "%*s<%.*s:%s>", INDENT, (int)prefix_len, prefix, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001292 lyxml_dump_text(ctx->out, ext[u].argument, 0);
Radek Krejci9f87b0c2021-03-05 14:45:26 +01001293 ly_print_(ctx->out, "</%.*s:%s>\n", (int)prefix_len, prefix, ext_def->argname);
Radek Krejci85ac8312021-03-03 20:21:33 +01001294 }
FredGand944bdc2019-11-05 21:57:07 +08001295 LY_LIST_FOR(ext[u].child, stmt) {
Radek Krejci85ac8312021-03-03 20:21:33 +01001296 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
1297 continue;
1298 }
1299
FredGand944bdc2019-11-05 21:57:07 +08001300 ypr_close_parent(ctx, &inner_flag);
1301 yprp_stmt(ctx, stmt);
1302 }
1303 LEVEL--;
1304 ypr_close(ctx, ext[u].name, inner_flag);
1305 }
1306}
1307
Michal Vasko7c8439f2020-08-05 13:25:19 +02001308static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001309yin_print_parsed_linkage(struct lys_ypr_ctx *ctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001310{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001311 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001312
FredGand944bdc2019-11-05 21:57:07 +08001313 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01001314 if (modp->imports[u].flags & LYS_INTERNAL) {
1315 continue;
1316 }
1317
Michal Vasko7c8439f2020-08-05 13:25:19 +02001318 ypr_open(ctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001319 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001320 yprp_extension_instances(ctx, LY_STMT_IMPORT, 0, modp->imports[u].exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01001321 ypr_substmt(ctx, LY_STMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001322 if (modp->imports[u].rev[0]) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001323 ypr_substmt(ctx, LY_STMT_REVISION_DATE, 0, modp->imports[u].rev, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001324 }
Radek Krejcifc596f92021-02-26 22:40:26 +01001325 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1326 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001327 LEVEL--;
1328 ypr_close(ctx, "import", 1);
1329 }
1330 LY_ARRAY_FOR(modp->includes, u) {
Radek Krejci771928a2021-01-19 13:42:36 +01001331 if (modp->includes[u].injected) {
1332 /* do not print the includes injected from submodules */
1333 continue;
1334 }
FredGand944bdc2019-11-05 21:57:07 +08001335 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001336 ypr_open(ctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001337 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001338 yprp_extension_instances(ctx, LY_STMT_INCLUDE, 0, modp->includes[u].exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001339 if (modp->includes[u].rev[0]) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001340 ypr_substmt(ctx, LY_STMT_REVISION_DATE, 0, modp->includes[u].rev, modp->includes[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001341 }
Radek Krejcifc596f92021-02-26 22:40:26 +01001342 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1343 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001344 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001345 ly_print_(ctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001346 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001347 ypr_open(ctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001348 }
1349 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001350}
FredGand944bdc2019-11-05 21:57:07 +08001351
Michal Vasko7c8439f2020-08-05 13:25:19 +02001352static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001353yin_print_parsed_body(struct lys_ypr_ctx *ctx, const struct lysp_module *modp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001354{
1355 LY_ARRAY_COUNT_TYPE u;
1356 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001357 struct lysp_node_action *action;
1358 struct lysp_node_notif *notif;
1359 struct lysp_node_grp *grp;
1360 struct lysp_node_augment *aug;
FredGand944bdc2019-11-05 21:57:07 +08001361
FredGand944bdc2019-11-05 21:57:07 +08001362 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001363 ly_print_(ctx->out, "\n");
FredGand944bdc2019-11-05 21:57:07 +08001364 yprp_extension(ctx, &modp->extensions[u]);
1365 }
1366 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001367 ly_print_(ctx->out, "\n");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001368 yprp_extension_instances(ctx, LY_STMT_MODULE, 0, modp->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001369 }
1370
1371 LY_ARRAY_FOR(modp->features, u) {
1372 yprp_feature(ctx, &modp->features[u]);
1373 }
1374
1375 LY_ARRAY_FOR(modp->identities, u) {
1376 yprp_identity(ctx, &modp->identities[u]);
1377 }
1378
1379 LY_ARRAY_FOR(modp->typedefs, u) {
1380 yprp_typedef(ctx, &modp->typedefs[u]);
1381 }
1382
Radek Krejci2a9fc652021-01-22 17:44:34 +01001383 LY_LIST_FOR(modp->groupings, grp) {
1384 yprp_grouping(ctx, grp);
FredGand944bdc2019-11-05 21:57:07 +08001385 }
1386
1387 LY_LIST_FOR(modp->data, data) {
1388 yprp_node(ctx, data);
1389 }
1390
Radek Krejci2a9fc652021-01-22 17:44:34 +01001391 LY_LIST_FOR(modp->augments, aug) {
1392 yprp_augment(ctx, aug);
FredGand944bdc2019-11-05 21:57:07 +08001393 }
1394
Radek Krejci2a9fc652021-01-22 17:44:34 +01001395 LY_LIST_FOR(modp->rpcs, action) {
1396 yprp_action(ctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001397 }
1398
Radek Krejci2a9fc652021-01-22 17:44:34 +01001399 LY_LIST_FOR(modp->notifs, notif) {
1400 yprp_notification(ctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001401 }
1402
1403 LY_ARRAY_FOR(modp->deviations, u) {
1404 yprp_deviation(ctx, &modp->deviations[u]);
1405 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001406}
1407
1408LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01001409yin_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001410{
1411 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7997d5a2021-02-22 10:55:56 +01001412 const struct lys_module *module = modp->mod;
Radek Krejciadcf63d2021-02-09 10:21:18 +01001413 struct lys_ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001414
Michal Vasko5233e962020-08-14 14:26:20 +02001415 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1416 ly_print_(ctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001417 ypr_xmlns(ctx, module, XML_NS_INDENT);
1418 ypr_import_xmlns(ctx, modp, XML_NS_INDENT);
Michal Vasko5233e962020-08-14 14:26:20 +02001419 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001420
1421 LEVEL++;
1422
1423 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001424 if (modp->version) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001425 ypr_substmt(ctx, LY_STMT_YANG_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001426 }
Radek Krejcifc596f92021-02-26 22:40:26 +01001427 ypr_substmt(ctx, LY_STMT_NAMESPACE, 0, module->ns, modp->exts);
1428 ypr_substmt(ctx, LY_STMT_PREFIX, 0, module->prefix, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001429
1430 /* linkage-stmts (import/include) */
1431 yin_print_parsed_linkage(ctx, modp);
1432
1433 /* meta-stmts */
1434 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001435 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001436 }
Radek Krejcifc596f92021-02-26 22:40:26 +01001437 ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, module->org, modp->exts);
1438 ypr_substmt(ctx, LY_STMT_CONTACT, 0, module->contact, modp->exts);
1439 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, module->dsc, modp->exts);
1440 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, module->ref, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001441
1442 /* revision-stmts */
1443 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001444 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001445 }
1446 LY_ARRAY_FOR(modp->revs, u) {
1447 yprp_revision(ctx, &modp->revs[u]);
1448 }
1449
1450 /* body-stmts */
1451 yin_print_parsed_body(ctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001452
1453 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001454 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001455 ly_print_flush(out);
1456
1457 return LY_SUCCESS;
1458}
1459
Michal Vasko7c8439f2020-08-05 13:25:19 +02001460static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001461yprp_belongsto(struct lys_ypr_ctx *ctx, const struct lysp_submodule *submodp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001462{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001463 ypr_open(ctx, "belongs-to", "module", submodp->mod->name, 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001464 LEVEL++;
Radek Krejcifc596f92021-02-26 22:40:26 +01001465 yprp_extension_instances(ctx, LY_STMT_BELONGS_TO, 0, submodp->exts, NULL, 0);
1466 ypr_substmt(ctx, LY_STMT_PREFIX, 0, submodp->prefix, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001467 LEVEL--;
1468 ypr_close(ctx, "belongs-to", 1);
1469}
1470
1471LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01001472yin_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001473{
1474 LY_ARRAY_COUNT_TYPE u;
Radek Krejciadcf63d2021-02-09 10:21:18 +01001475 struct lys_ypr_ctx ctx_ = {.out = out, .level = 0, .module = submodp->mod, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001476
Michal Vasko5233e962020-08-14 14:26:20 +02001477 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1478 ly_print_(ctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
Michal Vasko7997d5a2021-02-22 10:55:56 +01001479 ypr_xmlns(ctx, submodp->mod, XML_NS_INDENT);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001480 ypr_import_xmlns(ctx, (struct lysp_module *)submodp, XML_NS_INDENT);
Michal Vasko5233e962020-08-14 14:26:20 +02001481 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001482
1483 LEVEL++;
1484
1485 /* submodule-header-stmts */
1486 if (submodp->version) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001487 ypr_substmt(ctx, LY_STMT_YANG_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001488 }
1489 yprp_belongsto(ctx, submodp);
1490
1491 /* linkage-stmts (import/include) */
1492 yin_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
1493
1494 /* meta-stmts */
1495 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001496 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001497 }
Radek Krejcifc596f92021-02-26 22:40:26 +01001498 ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1499 ypr_substmt(ctx, LY_STMT_CONTACT, 0, submodp->contact, submodp->exts);
1500 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1501 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, submodp->ref, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001502
1503 /* revision-stmts */
1504 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001505 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001506 }
1507 LY_ARRAY_FOR(submodp->revs, u) {
1508 yprp_revision(ctx, &submodp->revs[u]);
1509 }
1510
1511 /* body-stmts */
1512 yin_print_parsed_body(ctx, (struct lysp_module *)submodp);
1513
1514 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001515 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001516 ly_print_flush(out);
1517
1518 return LY_SUCCESS;
1519}