blob: 586b65c48e478c459723f34565cd66191f91f930 [file] [log] [blame]
Radek Krejcid3ca0632019-04-16 16:54:54 +02001/**
2 * @file printer_yang.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief YANG 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
Radek Krejcid3ca0632019-04-16 16:54:54 +020016
Radek Krejci693262f2019-04-29 15:23:20 +020017#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Radek Krejci693262f2019-04-29 15:23:20 +020022
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020024#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "plugins_types.h"
27#include "printer.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020028#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "tree_data.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020032#include "tree_schema.h"
33#include "tree_schema_internal.h"
Radek Krejci693262f2019-04-29 15:23:20 +020034#include "xpath.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020035
Radek Krejcie7b95092019-05-15 11:03:07 +020036/**
37 * @brief Types of the YANG printers
38 */
Radek Krejci693262f2019-04-29 15:23:20 +020039enum schema_type {
Radek Krejcie7b95092019-05-15 11:03:07 +020040 YPR_PARSED, /**< YANG printer of the parsed schema */
41 YPR_COMPILED /**< YANG printer of the compiled schema */
Radek Krejci693262f2019-04-29 15:23:20 +020042};
43
Radek Krejcie7b95092019-05-15 11:03:07 +020044/**
45 * @brief YANG printer context.
46 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020047struct ypr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020048 struct ly_out *out; /**< output specification */
49 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
50 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
Radek Krejcie7b95092019-05-15 11:03:07 +020051 const struct lys_module *module; /**< schema to print */
52 enum schema_type schema; /**< type of the schema to print */
Radek Krejcid3ca0632019-04-16 16:54:54 +020053};
54
Radek Krejcie7b95092019-05-15 11:03:07 +020055/**
56 * @brief Print the given text as content of a double quoted YANG string,
57 * including encoding characters that have special meanings. The quotation marks
58 * are not printed.
59 *
60 * Follows RFC 7950, section 6.1.3.
61 *
62 * @param[in] out Output specification.
63 * @param[in] text String to be printed.
Radek Krejcif56e2a42019-09-09 14:15:25 +020064 * @param[in] len Length of the string from @p text to be printed. In case of -1,
Radek Krejcie7b95092019-05-15 11:03:07 +020065 * the @p text is printed completely as a NULL-terminated string.
66 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020067static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020068ypr_encode(struct ly_out *out, const char *text, ssize_t len)
Radek Krejcid3ca0632019-04-16 16:54:54 +020069{
Radek Krejci1deb5be2020-08-26 16:43:36 +020070 size_t i, start_len;
Radek Krejcid3ca0632019-04-16 16:54:54 +020071 const char *start;
72 char special = 0;
73
74 if (!len) {
75 return;
76 }
77
78 if (len < 0) {
79 len = strlen(text);
80 }
81
82 start = text;
83 start_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +020084 for (i = 0; i < (size_t)len; ++i) {
Radek Krejcid3ca0632019-04-16 16:54:54 +020085 switch (text[i]) {
86 case '\n':
87 case '\t':
88 case '\"':
89 case '\\':
90 special = text[i];
91 break;
92 default:
93 ++start_len;
94 break;
95 }
96
97 if (special) {
Michal Vasko5233e962020-08-14 14:26:20 +020098 ly_write_(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +020099 switch (special) {
100 case '\n':
Michal Vasko5233e962020-08-14 14:26:20 +0200101 ly_write_(out, "\\n", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200102 break;
103 case '\t':
Michal Vasko5233e962020-08-14 14:26:20 +0200104 ly_write_(out, "\\t", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200105 break;
106 case '\"':
Michal Vasko5233e962020-08-14 14:26:20 +0200107 ly_write_(out, "\\\"", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200108 break;
109 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200110 ly_write_(out, "\\\\", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200111 break;
112 }
113
114 start += start_len + 1;
115 start_len = 0;
116
117 special = 0;
118 }
119 }
120
Michal Vasko5233e962020-08-14 14:26:20 +0200121 ly_write_(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200122}
123
124static void
Radek Krejci857189e2020-09-01 13:26:36 +0200125ypr_open(struct ly_out *out, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200126{
127 if (flag && !*flag) {
128 *flag = 1;
Michal Vasko5233e962020-08-14 14:26:20 +0200129 ly_print_(out, " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200130 }
131}
132
133static void
Radek Krejci857189e2020-09-01 13:26:36 +0200134ypr_close(struct ypr_ctx *ctx, ly_bool flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200135{
136 if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200137 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200138 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200139 ly_print_(ctx->out, ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200140 }
141}
142
143static void
Radek Krejci857189e2020-09-01 13:26:36 +0200144ypr_text(struct ypr_ctx *ctx, const char *name, const char *text, ly_bool singleline, ly_bool closed)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200145{
146 const char *s, *t;
147
148 if (singleline) {
Michal Vasko5233e962020-08-14 14:26:20 +0200149 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200150 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200151 ly_print_(ctx->out, "%*s%s\n", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200152 LEVEL++;
153
Michal Vasko5233e962020-08-14 14:26:20 +0200154 ly_print_(ctx->out, "%*s\"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200155 }
156 t = text;
157 while ((s = strchr(t, '\n'))) {
158 ypr_encode(ctx->out, t, s - t);
Michal Vasko5233e962020-08-14 14:26:20 +0200159 ly_print_(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200160 t = s + 1;
161 if (*t != '\n') {
Michal Vasko5233e962020-08-14 14:26:20 +0200162 ly_print_(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200163 }
164 }
165
166 ypr_encode(ctx->out, t, strlen(t));
167 if (closed) {
Michal Vasko5233e962020-08-14 14:26:20 +0200168 ly_print_(ctx->out, "\";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200169 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200170 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200171 }
172 if (!singleline) {
173 LEVEL--;
174 }
175}
176
177static void
Radek Krejci693262f2019-04-29 15:23:20 +0200178yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200179{
180 struct lysp_stmt *childstmt;
181 const char *s, *t;
182
183 if (stmt->arg) {
184 if (stmt->flags) {
Michal Vasko5233e962020-08-14 14:26:20 +0200185 ly_print_(ctx->out, "%*s%s\n", INDENT, stmt->stmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200186 LEVEL++;
Michal Vasko5233e962020-08-14 14:26:20 +0200187 ly_print_(ctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'');
Radek Krejcid3ca0632019-04-16 16:54:54 +0200188 t = stmt->arg;
189 while ((s = strchr(t, '\n'))) {
190 ypr_encode(ctx->out, t, s - t);
Michal Vasko5233e962020-08-14 14:26:20 +0200191 ly_print_(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200192 t = s + 1;
193 if (*t != '\n') {
Michal Vasko5233e962020-08-14 14:26:20 +0200194 ly_print_(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200195 }
196 }
197 LEVEL--;
198 ypr_encode(ctx->out, t, strlen(t));
Michal Vasko5233e962020-08-14 14:26:20 +0200199 ly_print_(ctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200200 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200201 ly_print_(ctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200202 }
203 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200204 ly_print_(ctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200205 }
206
207 if (stmt->child) {
208 LEVEL++;
209 LY_LIST_FOR(stmt->child, childstmt) {
Radek Krejci693262f2019-04-29 15:23:20 +0200210 yprp_stmt(ctx, childstmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200211 }
212 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200213 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200214 }
215}
216
217/**
218 * @param[in] count Number of extensions to print, 0 to print them all.
219 */
220static void
Radek Krejci693262f2019-04-29 15:23:20 +0200221yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci857189e2020-09-01 13:26:36 +0200222 struct lysp_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200223{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200224 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225 struct lysp_stmt *stmt;
Radek Krejci857189e2020-09-01 13:26:36 +0200226 ly_bool child_presence;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200227 const char *argument;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200228
229 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200230 count = LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200231 }
232 LY_ARRAY_FOR(ext, u) {
233 if (!count) {
234 break;
235 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200236
Radek Krejcid3ca0632019-04-16 16:54:54 +0200237 count--;
Michal Vasko69730152020-10-09 16:30:07 +0200238 if ((ext->insubstmt != substmt) || (ext->insubstmt_index != substmt_index)) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200239 continue;
240 }
241
242 if (!ext->compiled && ext->yin) {
Michal Vasko5233e962020-08-14 14:26:20 +0200243 ly_print_(ctx->out, "%*s%s; // Model comes from different input format, extensions must be resolved first.\n", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200244 continue;
245 }
246
Radek Krejcif56e2a42019-09-09 14:15:25 +0200247 ypr_open(ctx->out, flag);
248 argument = NULL;
249 if (ext[u].compiled) {
250 argument = ext[u].compiled->argument;
251 } else {
252 argument = ext[u].argument;
253 }
254 if (argument) {
Michal Vasko5233e962020-08-14 14:26:20 +0200255 ly_print_(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200256 ypr_encode(ctx->out, argument, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200257 ly_print_(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200258 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200259 ly_print_(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200260 }
261
262 child_presence = 0;
263 LEVEL++;
264 LY_LIST_FOR(ext[u].child, stmt) {
265 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
266 continue;
267 }
268 if (!child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200269 ly_print_(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200270 child_presence = 1;
271 }
272 yprp_stmt(ctx, stmt);
273 }
274 LEVEL--;
275 if (child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200276 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200277 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200278 ly_print_(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200279 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200280 }
281}
282
Radek Krejci693262f2019-04-29 15:23:20 +0200283/**
284 * @param[in] count Number of extensions to print, 0 to print them all.
285 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200286static void
Radek Krejci693262f2019-04-29 15:23:20 +0200287yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci857189e2020-09-01 13:26:36 +0200288 struct lysc_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200289{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200290 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200291
292 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200293 count = LY_ARRAY_COUNT(ext);
Radek Krejci693262f2019-04-29 15:23:20 +0200294 }
295 LY_ARRAY_FOR(ext, u) {
296 if (!count) {
297 break;
298 }
299 /* TODO compiled extensions */
300 (void) ctx;
301 (void) substmt;
302 (void) substmt_index;
303 (void) flag;
304
305 count--;
306 }
307}
308
309static void
310ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200311{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200312 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200313 ly_bool extflag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200314
315 if (!text) {
316 /* nothing to print */
317 return;
318 }
319
320 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
Michal Vasko5233e962020-08-14 14:26:20 +0200321 ly_print_(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200322 } else {
323 ypr_text(ctx, ext_substmt_info[substmt].name, text,
Radek Krejci0f969882020-08-21 16:56:47 +0200324 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200325 }
326
327 LEVEL++;
328 LY_ARRAY_FOR(ext, u) {
Michal Vasko69730152020-10-09 16:30:07 +0200329 if ((((struct lysp_ext_instance *)ext)[u].insubstmt != substmt) || (((struct lysp_ext_instance *)ext)[u].insubstmt_index != substmt_index)) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200330 continue;
331 }
Radek Krejci693262f2019-04-29 15:23:20 +0200332 if (ctx->schema == YPR_PARSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200333 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200334 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200335 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200336 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200337 }
338 LEVEL--;
339 ypr_close(ctx, extflag);
340}
341
342static void
Radek Krejci857189e2020-09-01 13:26:36 +0200343ypr_unsigned(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, unsigned long int attr_value, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200344{
345 char *str;
346
Radek Krejci1deb5be2020-08-26 16:43:36 +0200347 if (asprintf(&str, "%lu", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200348 LOGMEM(ctx->module->ctx);
349 return;
350 }
351 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200352 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200353 free(str);
354}
355
356static void
Radek Krejci857189e2020-09-01 13:26:36 +0200357ypr_signed(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, signed long int attr_value, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200358{
359 char *str;
360
Radek Krejci1deb5be2020-08-26 16:43:36 +0200361 if (asprintf(&str, "%ld", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200362 LOGMEM(ctx->module->ctx);
363 return;
364 }
365 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200366 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200367 free(str);
368}
369
370static void
Radek Krejci693262f2019-04-29 15:23:20 +0200371yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200372{
373 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +0200374 ly_print_(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200375 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200376 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
377 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
378 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200379 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200380 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200381 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200382 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200383 }
384}
385
386static void
Radek Krejci857189e2020-09-01 13:26:36 +0200387ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200388{
389 if (flags & LYS_MAND_MASK) {
390 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200391 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200392 }
393}
394
395static void
Radek Krejci857189e2020-09-01 13:26:36 +0200396ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200397{
398 if (flags & LYS_CONFIG_MASK) {
399 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200400 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200401 }
402}
403
404static void
Radek Krejci857189e2020-09-01 13:26:36 +0200405ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200406{
407 const char *status = NULL;
408
409 if (flags & LYS_STATUS_CURR) {
410 ypr_open(ctx->out, flag);
411 status = "current";
412 } else if (flags & LYS_STATUS_DEPRC) {
413 ypr_open(ctx->out, flag);
414 status = "deprecated";
415 } else if (flags & LYS_STATUS_OBSLT) {
416 ypr_open(ctx->out, flag);
417 status = "obsolete";
418 }
Radek Krejci693262f2019-04-29 15:23:20 +0200419
420 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200421}
422
423static void
Radek Krejci857189e2020-09-01 13:26:36 +0200424ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200425{
426 if (dsc) {
427 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200428 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200429 }
430}
431
432static void
Radek Krejci857189e2020-09-01 13:26:36 +0200433ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200434{
435 if (ref) {
436 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200437 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200438 }
439}
440
441static void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200442yprp_iffeatures(struct ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200443{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200444 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +0200445 ly_bool extflag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200446
Michal Vasko7f45cf22020-10-01 12:49:44 +0200447 LY_ARRAY_FOR(iffs, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200448 ypr_open(ctx->out, flag);
449 extflag = 0;
450
Michal Vasko7f45cf22020-10-01 12:49:44 +0200451 ly_print_(ctx->out, "%*sif-feature \"%s\"", INDENT, iffs[u].str);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200452
453 /* extensions */
454 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200455 LY_ARRAY_FOR(exts, v) {
456 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200457 }
458 LEVEL--;
459 ypr_close(ctx, extflag);
460 }
461}
462
463static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200464yprc_iffeature(struct ypr_ctx *ctx, struct lysc_iffeature *feat, size_t *index_e, size_t *index_f)
Radek Krejci693262f2019-04-29 15:23:20 +0200465{
Radek Krejci857189e2020-09-01 13:26:36 +0200466 ly_bool brackets_flag = *index_e ? 1 : 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200467 uint8_t op;
468
469 op = lysc_iff_getop(feat->expr, *index_e);
470 (*index_e)++;
471
472 switch (op) {
473 case LYS_IFF_F:
474 if (ctx->module == feat->features[*index_f]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200475 ly_print_(ctx->out, "%s", feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200476 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200477 ly_print_(ctx->out, "%s:%s", feat->features[*index_f]->module->prefix, feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200478 }
479 (*index_f)++;
480 break;
481 case LYS_IFF_NOT:
Michal Vasko5233e962020-08-14 14:26:20 +0200482 ly_print_(ctx->out, "not ");
Radek Krejci693262f2019-04-29 15:23:20 +0200483 yprc_iffeature(ctx, feat, index_e, index_f);
484 break;
485 case LYS_IFF_AND:
486 if (brackets_flag) {
487 /* AND need brackets only if previous op was not */
Michal Vasko69730152020-10-09 16:30:07 +0200488 if ((*index_e < 2) || (lysc_iff_getop(feat->expr, *index_e - 2) != LYS_IFF_NOT)) {
Radek Krejci693262f2019-04-29 15:23:20 +0200489 brackets_flag = 0;
490 }
491 }
Radek Krejci0f969882020-08-21 16:56:47 +0200492 /* falls through */
Radek Krejci693262f2019-04-29 15:23:20 +0200493 case LYS_IFF_OR:
494 if (brackets_flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200495 ly_print_(ctx->out, "(");
Radek Krejci693262f2019-04-29 15:23:20 +0200496 }
497 yprc_iffeature(ctx, feat, index_e, index_f);
Michal Vasko5233e962020-08-14 14:26:20 +0200498 ly_print_(ctx->out, " %s ", op == LYS_IFF_OR ? "or" : "and");
Radek Krejci693262f2019-04-29 15:23:20 +0200499 yprc_iffeature(ctx, feat, index_e, index_f);
500 if (brackets_flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200501 ly_print_(ctx->out, ")");
Radek Krejci693262f2019-04-29 15:23:20 +0200502 }
503 }
504}
505
506static void
Radek Krejci857189e2020-09-01 13:26:36 +0200507yprc_iffeatures(struct ypr_ctx *ctx, struct lysc_iffeature *iff, struct lysc_ext_instance *exts, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200508{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200509 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +0200510 ly_bool extflag;
Radek Krejci693262f2019-04-29 15:23:20 +0200511
512 LY_ARRAY_FOR(iff, u) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200513 size_t index_e = 0, index_f = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200514
515 ypr_open(ctx->out, flag);
516 extflag = 0;
517
Michal Vasko5233e962020-08-14 14:26:20 +0200518 ly_print_(ctx->out, "%*sif-feature \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200519 yprc_iffeature(ctx, iff, &index_e, &index_f);
Michal Vasko5233e962020-08-14 14:26:20 +0200520 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200521
522 /* extensions */
523 LEVEL++;
Radek Krejci334ccc72019-06-12 13:49:29 +0200524 LY_ARRAY_FOR(exts, v) {
Michal Vasko69730152020-10-09 16:30:07 +0200525 if ((exts[v].insubstmt != LYEXT_SUBSTMT_IFFEATURE) || (exts[v].insubstmt_index != u)) {
Radek Krejci693262f2019-04-29 15:23:20 +0200526 continue;
527 }
Radek Krejci334ccc72019-06-12 13:49:29 +0200528 yprc_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200529 }
530 LEVEL--;
531 ypr_close(ctx, extflag);
532 }
533}
534
535static void
536yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200537{
Radek Krejci857189e2020-09-01 13:26:36 +0200538 ly_bool flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200539 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200540
Michal Vasko5233e962020-08-14 14:26:20 +0200541 ly_print_(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200542 LEVEL++;
543
544 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200545 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200546 }
547
548 if (ext->argument) {
549 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200550 ly_print_(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200551 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200552 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200553 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200554 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LYEXT_SUBSTMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200555 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200556 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200557 }
558 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vasko69730152020-10-09 16:30:07 +0200559 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts)))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200560 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200561 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200562 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200563 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200564 ypr_close(ctx, flag2);
565 }
566
Radek Krejci693262f2019-04-29 15:23:20 +0200567 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200568 ypr_description(ctx, ext->dsc, ext->exts, &flag);
569 ypr_reference(ctx, ext->ref, ext->exts, &flag);
570
571 LEVEL--;
572 ypr_close(ctx, flag);
573}
574
575static void
Radek Krejci693262f2019-04-29 15:23:20 +0200576yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200577{
Radek Krejci857189e2020-09-01 13:26:36 +0200578 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200579
Michal Vasko5233e962020-08-14 14:26:20 +0200580 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200581 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200582 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
583 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
584 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200585 ypr_description(ctx, feat->dsc, feat->exts, &flag);
586 ypr_reference(ctx, feat->ref, feat->exts, &flag);
587 LEVEL--;
588 ypr_close(ctx, flag);
589}
590
591static void
Radek Krejci693262f2019-04-29 15:23:20 +0200592yprc_feature(struct ypr_ctx *ctx, const struct lysc_feature *feat)
593{
Radek Krejci857189e2020-09-01 13:26:36 +0200594 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200595
Michal Vasko5233e962020-08-14 14:26:20 +0200596 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200597 LEVEL++;
598 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
599 yprc_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
600 ypr_status(ctx, feat->flags, feat->exts, &flag);
601 ypr_description(ctx, feat->dsc, feat->exts, &flag);
602 ypr_reference(ctx, feat->ref, feat->exts, &flag);
603 LEVEL--;
604 ypr_close(ctx, flag);
605}
606
607static void
608yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200609{
Radek Krejci857189e2020-09-01 13:26:36 +0200610 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200611 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200612
Michal Vasko5233e962020-08-14 14:26:20 +0200613 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200614 LEVEL++;
615
Radek Krejci693262f2019-04-29 15:23:20 +0200616 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
617 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200618
619 LY_ARRAY_FOR(ident->bases, u) {
620 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200621 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200622 }
623
Radek Krejci693262f2019-04-29 15:23:20 +0200624 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200625 ypr_description(ctx, ident->dsc, ident->exts, &flag);
626 ypr_reference(ctx, ident->ref, ident->exts, &flag);
627
628 LEVEL--;
629 ypr_close(ctx, flag);
630}
631
632static void
Radek Krejci693262f2019-04-29 15:23:20 +0200633yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
634{
Radek Krejci857189e2020-09-01 13:26:36 +0200635 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200636 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200637
Michal Vasko5233e962020-08-14 14:26:20 +0200638 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200639 LEVEL++;
640
641 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
642 yprc_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
643
644 LY_ARRAY_FOR(ident->derived, u) {
645 ypr_open(ctx->out, &flag);
646 if (ctx->module != ident->derived[u]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200647 ly_print_(ctx->out, "%*sderived %s:%s;\n", INDENT, ident->derived[u]->module->prefix, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200648 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200649 ly_print_(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200650 }
651 }
652
653 ypr_status(ctx, ident->flags, ident->exts, &flag);
654 ypr_description(ctx, ident->dsc, ident->exts, &flag);
655 ypr_reference(ctx, ident->ref, ident->exts, &flag);
656
657 LEVEL--;
658 ypr_close(ctx, flag);
659}
660
661static void
Radek Krejci857189e2020-09-01 13:26:36 +0200662yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200663{
Radek Krejci857189e2020-09-01 13:26:36 +0200664 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200665
666 if (!restr) {
667 return;
668 }
669
670 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200671 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200672 ypr_encode(ctx->out, (restr->arg.str[0] != 0x15 && restr->arg.str[0] != 0x06) ? restr->arg.str : &restr->arg.str[1], -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200673 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200674
675 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200676 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200677 if (restr->arg.str[0] == 0x15) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200678 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
679 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200680 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200681 }
682 if (restr->emsg) {
683 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200684 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200685 }
686 if (restr->eapptag) {
687 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200688 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200689 }
Radek Krejci693262f2019-04-29 15:23:20 +0200690 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
691 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
692
Radek Krejcid3ca0632019-04-16 16:54:54 +0200693 LEVEL--;
694 ypr_close(ctx, inner_flag);
695}
696
697static void
Radek Krejci857189e2020-09-01 13:26:36 +0200698yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200699{
Radek Krejci857189e2020-09-01 13:26:36 +0200700 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200701
702 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200703 ly_print_(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200704 ypr_encode(ctx->out, must->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200705 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200706
707 LEVEL++;
708 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
709 if (must->emsg) {
710 ypr_open(ctx->out, &inner_flag);
711 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
712 }
713 if (must->eapptag) {
714 ypr_open(ctx->out, &inner_flag);
715 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
716 }
717 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
718 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
719
720 LEVEL--;
721 ypr_close(ctx, inner_flag);
722}
723
724static void
Radek Krejci857189e2020-09-01 13:26:36 +0200725yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200726{
Radek Krejci857189e2020-09-01 13:26:36 +0200727 ly_bool inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200728 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200729
Radek Krejci334ccc72019-06-12 13:49:29 +0200730 if (!range) {
731 return;
732 }
733
Radek Krejci693262f2019-04-29 15:23:20 +0200734 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200735 ly_print_(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200736 LY_ARRAY_FOR(range->parts, u) {
737 if (u > 0) {
Michal Vasko5233e962020-08-14 14:26:20 +0200738 ly_print_(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200739 }
740 if (range->parts[u].max_64 == range->parts[u].min_64) {
741 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200742 ly_print_(ctx->out, "%" PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200743 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200744 ly_print_(ctx->out, "%" PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200745 }
746 } else {
747 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200748 ly_print_(ctx->out, "%" PRIu64 "..%" PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200749 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200750 ly_print_(ctx->out, "%" PRId64 "..%" PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200751 }
752 }
753 }
Michal Vasko5233e962020-08-14 14:26:20 +0200754 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200755
756 LEVEL++;
757 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
758 if (range->emsg) {
759 ypr_open(ctx->out, &inner_flag);
760 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
761 }
762 if (range->eapptag) {
763 ypr_open(ctx->out, &inner_flag);
764 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
765 }
766 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
767 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
768
769 LEVEL--;
770 ypr_close(ctx, inner_flag);
771}
772
773static void
Radek Krejci857189e2020-09-01 13:26:36 +0200774yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200775{
Radek Krejci857189e2020-09-01 13:26:36 +0200776 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200777
778 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200779 ly_print_(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200780 ypr_encode(ctx->out, pattern->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200781 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200782
783 LEVEL++;
784 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
785 if (pattern->inverted) {
786 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
787 ypr_open(ctx->out, &inner_flag);
788 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
789 }
790 if (pattern->emsg) {
791 ypr_open(ctx->out, &inner_flag);
792 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
793 }
794 if (pattern->eapptag) {
795 ypr_open(ctx->out, &inner_flag);
796 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
797 }
798 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
799 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
800
801 LEVEL--;
802 ypr_close(ctx, inner_flag);
803}
804
805static void
Radek Krejci857189e2020-09-01 13:26:36 +0200806yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200807{
Radek Krejci857189e2020-09-01 13:26:36 +0200808 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200809
810 if (!when) {
811 return;
812 }
813 ypr_open(ctx->out, flag);
814
Michal Vasko5233e962020-08-14 14:26:20 +0200815 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200816 ypr_encode(ctx->out, when->cond, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200817 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200818
819 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200820 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200821 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
822 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
823 LEVEL--;
824 ypr_close(ctx, inner_flag);
825}
826
827static void
Radek Krejci857189e2020-09-01 13:26:36 +0200828yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200829{
Radek Krejci857189e2020-09-01 13:26:36 +0200830 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200831
832 if (!when) {
833 return;
834 }
835 ypr_open(ctx->out, flag);
836
Michal Vasko5233e962020-08-14 14:26:20 +0200837 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200838 ypr_encode(ctx->out, when->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200839 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200840
841 LEVEL++;
842 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
843 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
844 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
845 LEVEL--;
846 ypr_close(ctx, inner_flag);
847}
848
849static void
Radek Krejci857189e2020-09-01 13:26:36 +0200850yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200851{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200852 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200853 ly_bool inner_flag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200854
855 LY_ARRAY_FOR(items, u) {
856 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200857 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200858 ly_print_(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200859 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200860 ly_print_(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200861 ypr_encode(ctx->out, items[u].name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200862 ly_print_(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200863 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200864 inner_flag = 0;
865 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200866 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
867 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200868 if (items[u].flags & LYS_SET_VALUE) {
869 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200870 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200871 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200872 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200873 }
874 }
Radek Krejci693262f2019-04-29 15:23:20 +0200875 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200876 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
877 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
878 LEVEL--;
879 ypr_close(ctx, inner_flag);
880 }
881}
882
883static void
Radek Krejci693262f2019-04-29 15:23:20 +0200884yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200885{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200886 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200887 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200888
Michal Vasko5233e962020-08-14 14:26:20 +0200889 ly_print_(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200890 LEVEL++;
891
Radek Krejci693262f2019-04-29 15:23:20 +0200892 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200893
Radek Krejci693262f2019-04-29 15:23:20 +0200894 yprp_restr(ctx, type->range, "range", &flag);
895 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200896 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200897 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200898 }
Radek Krejci693262f2019-04-29 15:23:20 +0200899 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
900 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200901
902 if (type->path) {
903 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200904 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200905 }
906 if (type->flags & LYS_SET_REQINST) {
907 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200908 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200909 }
910 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200911 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200912 }
913 LY_ARRAY_FOR(type->bases, u) {
914 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200915 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200916 }
917 LY_ARRAY_FOR(type->types, u) {
918 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200919 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200920 }
921
922 LEVEL--;
923 ypr_close(ctx, flag);
924}
925
926static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200927yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200928{
Radek Krejci857189e2020-09-01 13:26:36 +0200929 ly_bool dynamic;
Radek Krejcia1911222019-07-22 17:24:50 +0200930 const char *str;
931
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200932 str = value->realtype->plugin->print(value, LY_PREF_JSON, NULL, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200933 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
934 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200935 free((void *)str);
Radek Krejcia1911222019-07-22 17:24:50 +0200936 }
937}
938
939static void
Radek Krejci693262f2019-04-29 15:23:20 +0200940yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
941{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200942 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200943 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200944
Michal Vasko5233e962020-08-14 14:26:20 +0200945 ly_print_(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200946 LEVEL++;
947
948 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200949
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200950 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200951 case LY_TYPE_BINARY: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200952 struct lysc_type_bin *bin = (struct lysc_type_bin *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200953 yprc_range(ctx, bin->length, type->basetype, &flag);
954 break;
955 }
956 case LY_TYPE_UINT8:
957 case LY_TYPE_UINT16:
958 case LY_TYPE_UINT32:
959 case LY_TYPE_UINT64:
960 case LY_TYPE_INT8:
961 case LY_TYPE_INT16:
962 case LY_TYPE_INT32:
963 case LY_TYPE_INT64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200964 struct lysc_type_num *num = (struct lysc_type_num *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200965 yprc_range(ctx, num->range, type->basetype, &flag);
966 break;
967 }
968 case LY_TYPE_STRING: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200969 struct lysc_type_str *str = (struct lysc_type_str *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200970 yprc_range(ctx, str->length, type->basetype, &flag);
971 LY_ARRAY_FOR(str->patterns, u) {
972 yprc_pattern(ctx, str->patterns[u], &flag);
973 }
974 break;
975 }
976 case LY_TYPE_BITS:
977 case LY_TYPE_ENUM: {
978 /* bits and enums structures are compatible */
Michal Vasko22df3f02020-08-24 13:29:22 +0200979 struct lysc_type_bits *bits = (struct lysc_type_bits *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200980 LY_ARRAY_FOR(bits->bits, u) {
981 struct lysc_type_bitenum_item *item = &bits->bits[u];
Radek Krejci857189e2020-09-01 13:26:36 +0200982 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200983
984 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200985 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200986 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200987 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200988 LEVEL++;
989 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
990 yprc_iffeatures(ctx, item->iffeatures, item->exts, &inner_flag);
991 if (type->basetype == LY_TYPE_BITS) {
992 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
993 } else { /* LY_TYPE_ENUM */
994 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
995 }
996 ypr_status(ctx, item->flags, item->exts, &inner_flag);
997 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
998 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
999 LEVEL--;
1000 ypr_close(ctx, inner_flag);
1001 }
1002 break;
1003 }
1004 case LY_TYPE_BOOL:
1005 case LY_TYPE_EMPTY:
1006 /* nothing to do */
1007 break;
1008 case LY_TYPE_DEC64: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001009 struct lysc_type_dec *dec = (struct lysc_type_dec *)type;
Radek Krejci693262f2019-04-29 15:23:20 +02001010 ypr_open(ctx->out, &flag);
1011 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
1012 yprc_range(ctx, dec->range, dec->basetype, &flag);
1013 break;
1014 }
1015 case LY_TYPE_IDENT: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001016 struct lysc_type_identityref *ident = (struct lysc_type_identityref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +02001017 LY_ARRAY_FOR(ident->bases, u) {
1018 ypr_open(ctx->out, &flag);
1019 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
1020 }
1021 break;
1022 }
1023 case LY_TYPE_INST: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001024 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid *)type;
Radek Krejci693262f2019-04-29 15:23:20 +02001025 ypr_open(ctx->out, &flag);
1026 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
1027 break;
1028 }
1029 case LY_TYPE_LEAFREF: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001030 struct lysc_type_leafref *lr = (struct lysc_type_leafref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +02001031 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +02001032 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001033 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
1034 yprc_type(ctx, lr->realtype);
1035 break;
1036 }
1037 case LY_TYPE_UNION: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001038 struct lysc_type_union *un = (struct lysc_type_union *)type;
Radek Krejci693262f2019-04-29 15:23:20 +02001039 LY_ARRAY_FOR(un->types, u) {
1040 ypr_open(ctx->out, &flag);
1041 yprc_type(ctx, un->types[u]);
1042 }
1043 break;
1044 }
1045 default:
1046 LOGINT(ctx->module->ctx);
1047 }
1048
1049 LEVEL--;
1050 ypr_close(ctx, flag);
1051}
1052
1053static void
1054yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001055{
Michal Vasko5233e962020-08-14 14:26:20 +02001056 ly_print_(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001057 LEVEL++;
1058
Radek Krejci693262f2019-04-29 15:23:20 +02001059 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001060
Radek Krejci693262f2019-04-29 15:23:20 +02001061 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001062
1063 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +02001064 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001065 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001066 if (tpdf->dflt.str) {
1067 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001068 }
1069
Radek Krejci693262f2019-04-29 15:23:20 +02001070 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001071 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
1072 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
1073
1074 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001075 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001076}
1077
Radek Krejci693262f2019-04-29 15:23:20 +02001078static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
1079static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
1080static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001081
1082static void
Radek Krejci693262f2019-04-29 15:23:20 +02001083yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001084{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001085 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001086 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001087 struct lysp_node *data;
1088
Michal Vasko5233e962020-08-14 14:26:20 +02001089 ly_print_(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001090 LEVEL++;
1091
Radek Krejci693262f2019-04-29 15:23:20 +02001092 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1093 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001094 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1095 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001096
1097 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001098 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001099 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001100 }
1101
1102 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001103 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001104 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001105 }
1106
1107 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001108 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001109 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001110 }
1111
1112 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001113 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001114 }
1115
1116 LEVEL--;
1117 ypr_close(ctx, flag);
1118}
1119
1120static void
Radek Krejci857189e2020-09-01 13:26:36 +02001121yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001123 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001124 struct lysp_node *data;
1125
Michal Vasko7f45cf22020-10-01 12:49:44 +02001126 if (!inout->data) {
1127 /* no children */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001128 return;
1129 }
1130 ypr_open(ctx->out, flag);
1131
Michal Vasko5233e962020-08-14 14:26:20 +02001132 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001133 LEVEL++;
1134
Radek Krejci693262f2019-04-29 15:23:20 +02001135 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001136 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001137 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001138 }
1139 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001140 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001141 }
1142 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001143 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001144 }
1145
1146 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001147 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001148 }
1149
1150 LEVEL--;
1151 ypr_close(ctx, 1);
1152}
1153
1154static void
Radek Krejci857189e2020-09-01 13:26:36 +02001155yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001156{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001157 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001158 struct lysc_node *data;
1159
1160 if (!inout->data) {
1161 /* input/output is empty */
1162 return;
1163 }
1164 ypr_open(ctx->out, flag);
1165
Michal Vasko5233e962020-08-14 14:26:20 +02001166 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001167 LEVEL++;
1168
1169 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1170 LY_ARRAY_FOR(inout->musts, u) {
1171 yprc_must(ctx, &inout->musts[u], NULL);
1172 }
1173
Radek Krejci52f65552020-09-01 17:03:35 +02001174 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001175 LY_LIST_FOR(inout->data, data) {
1176 yprc_node(ctx, data);
1177 }
Radek Krejci693262f2019-04-29 15:23:20 +02001178 }
1179
1180 LEVEL--;
1181 ypr_close(ctx, 1);
1182}
1183
1184static void
1185yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001186{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001187 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001188 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001189 struct lysp_node *data;
1190
Michal Vasko5233e962020-08-14 14:26:20 +02001191 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001192
1193 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001194 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1195 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001196
1197 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001198 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001199 }
Radek Krejci693262f2019-04-29 15:23:20 +02001200 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001201 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1202 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1203
1204 LY_ARRAY_FOR(notif->typedefs, u) {
1205 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001206 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001207 }
1208
1209 LY_ARRAY_FOR(notif->groupings, u) {
1210 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001211 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001212 }
1213
1214 LY_LIST_FOR(notif->data, data) {
1215 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001216 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001217 }
1218
1219 LEVEL--;
1220 ypr_close(ctx, flag);
1221}
1222
1223static void
Radek Krejci693262f2019-04-29 15:23:20 +02001224yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1225{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001226 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001227 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001228 struct lysc_node *data;
1229
Michal Vasko5233e962020-08-14 14:26:20 +02001230 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001231
1232 LEVEL++;
1233 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1234 yprc_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
1235
1236 LY_ARRAY_FOR(notif->musts, u) {
1237 yprc_must(ctx, &notif->musts[u], &flag);
1238 }
1239 ypr_status(ctx, notif->flags, notif->exts, &flag);
1240 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1241 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1242
Radek Krejci52f65552020-09-01 17:03:35 +02001243 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001244 LY_LIST_FOR(notif->data, data) {
1245 ypr_open(ctx->out, &flag);
1246 yprc_node(ctx, data);
1247 }
Radek Krejci693262f2019-04-29 15:23:20 +02001248 }
1249
1250 LEVEL--;
1251 ypr_close(ctx, flag);
1252}
1253
1254static void
1255yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001256{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001257 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001258 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001259
Michal Vasko5233e962020-08-14 14:26:20 +02001260 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001261
1262 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001263 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1264 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1265 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001266 ypr_description(ctx, action->dsc, action->exts, &flag);
1267 ypr_reference(ctx, action->ref, action->exts, &flag);
1268
1269 LY_ARRAY_FOR(action->typedefs, u) {
1270 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001271 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001272 }
1273
1274 LY_ARRAY_FOR(action->groupings, u) {
1275 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001276 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001277 }
1278
Radek Krejci693262f2019-04-29 15:23:20 +02001279 yprp_inout(ctx, &action->input, &flag);
1280 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001281
1282 LEVEL--;
1283 ypr_close(ctx, flag);
1284}
1285
1286static void
Radek Krejci693262f2019-04-29 15:23:20 +02001287yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1288{
Radek Krejci857189e2020-09-01 13:26:36 +02001289 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001290
Michal Vasko5233e962020-08-14 14:26:20 +02001291 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001292
1293 LEVEL++;
1294 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1295 yprc_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1296 ypr_status(ctx, action->flags, action->exts, &flag);
1297 ypr_description(ctx, action->dsc, action->exts, &flag);
1298 ypr_reference(ctx, action->ref, action->exts, &flag);
1299
1300 yprc_inout(ctx, action, &action->input, &flag);
1301 yprc_inout(ctx, action, &action->output, &flag);
1302
1303 LEVEL--;
1304 ypr_close(ctx, flag);
1305}
1306
1307static void
Radek Krejci857189e2020-09-01 13:26:36 +02001308yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001309{
Michal Vasko5233e962020-08-14 14:26:20 +02001310 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001311 LEVEL++;
1312
Radek Krejci693262f2019-04-29 15:23:20 +02001313 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1314 yprp_when(ctx, node->when, flag);
1315 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001316}
1317
1318static void
Radek Krejci857189e2020-09-01 13:26:36 +02001319yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001320{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001321 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001322
Michal Vasko5233e962020-08-14 14:26:20 +02001323 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001324 LEVEL++;
1325
1326 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1327 LY_ARRAY_FOR(node->when, u) {
1328 yprc_when(ctx, node->when[u], flag);
1329 }
1330 yprc_iffeatures(ctx, node->iffeatures, node->exts, flag);
1331}
1332
1333/* macr oto unify the code */
1334#define YPR_NODE_COMMON2 \
1335 ypr_config(ctx, node->flags, node->exts, flag); \
1336 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1337 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1338 } \
1339 ypr_status(ctx, node->flags, node->exts, flag); \
1340 ypr_description(ctx, node->dsc, node->exts, flag); \
1341 ypr_reference(ctx, node->ref, node->exts, flag)
1342
1343static void
Radek Krejci857189e2020-09-01 13:26:36 +02001344yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001345{
1346 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001347}
1348
1349static void
Radek Krejci857189e2020-09-01 13:26:36 +02001350yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001351{
1352 YPR_NODE_COMMON2;
1353}
1354
1355#undef YPR_NODE_COMMON2
1356
1357static void
1358yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001359{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001360 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001361 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001362 struct lysp_node *child;
1363 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1364
Radek Krejci693262f2019-04-29 15:23:20 +02001365 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001366
1367 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001368 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001369 }
1370 if (cont->presence) {
1371 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001372 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001373 }
1374
Radek Krejci693262f2019-04-29 15:23:20 +02001375 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001376
1377 LY_ARRAY_FOR(cont->typedefs, u) {
1378 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001379 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001380 }
1381
1382 LY_ARRAY_FOR(cont->groupings, u) {
1383 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001384 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001385 }
1386
1387 LY_LIST_FOR(cont->child, child) {
1388 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001389 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001390 }
1391
1392 LY_ARRAY_FOR(cont->actions, u) {
1393 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001394 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001395 }
1396
1397 LY_ARRAY_FOR(cont->notifs, u) {
1398 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001399 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001400 }
1401
1402 LEVEL--;
1403 ypr_close(ctx, flag);
1404}
1405
1406static void
Radek Krejci693262f2019-04-29 15:23:20 +02001407yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1408{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001409 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001410 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001411 struct lysc_node *child;
1412 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1413
1414 yprc_node_common1(ctx, node, &flag);
1415
1416 LY_ARRAY_FOR(cont->musts, u) {
1417 yprc_must(ctx, &cont->musts[u], &flag);
1418 }
1419 if (cont->flags & LYS_PRESENCE) {
1420 ypr_open(ctx->out, &flag);
1421 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1422 }
1423
1424 yprc_node_common2(ctx, node, &flag);
1425
Radek Krejci52f65552020-09-01 17:03:35 +02001426 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001427 LY_LIST_FOR(cont->child, child) {
1428 ypr_open(ctx->out, &flag);
1429 yprc_node(ctx, child);
1430 }
Radek Krejci693262f2019-04-29 15:23:20 +02001431
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001432 LY_ARRAY_FOR(cont->actions, u) {
1433 ypr_open(ctx->out, &flag);
1434 yprc_action(ctx, &cont->actions[u]);
1435 }
Radek Krejci693262f2019-04-29 15:23:20 +02001436
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001437 LY_ARRAY_FOR(cont->notifs, u) {
1438 ypr_open(ctx->out, &flag);
1439 yprc_notification(ctx, &cont->notifs[u]);
1440 }
Radek Krejci693262f2019-04-29 15:23:20 +02001441 }
1442
1443 LEVEL--;
1444 ypr_close(ctx, flag);
1445}
1446
1447static void
1448yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1449{
Radek Krejci857189e2020-09-01 13:26:36 +02001450 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001451 struct lysp_node *child;
1452 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1453
1454 yprp_node_common1(ctx, node, &flag);
1455 yprp_node_common2(ctx, node, &flag);
1456
1457 LY_LIST_FOR(cas->child, child) {
1458 ypr_open(ctx->out, &flag);
1459 yprp_node(ctx, child);
1460 }
1461
1462 LEVEL--;
1463 ypr_close(ctx, flag);
1464}
1465
1466static void
1467yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1468{
Radek Krejci857189e2020-09-01 13:26:36 +02001469 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001470 struct lysc_node *child;
1471
Michal Vasko22df3f02020-08-24 13:29:22 +02001472 yprc_node_common1(ctx, (struct lysc_node *)cs, &flag);
1473 yprc_node_common2(ctx, (struct lysc_node *)cs, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001474
Radek Krejci52f65552020-09-01 17:03:35 +02001475 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001476 for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001477 ypr_open(ctx->out, &flag);
1478 yprc_node(ctx, child);
1479 }
Radek Krejci693262f2019-04-29 15:23:20 +02001480 }
1481
1482 LEVEL--;
1483 ypr_close(ctx, flag);
1484}
1485
1486static void
1487yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001488{
Radek Krejci857189e2020-09-01 13:26:36 +02001489 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001490 struct lysp_node *child;
1491 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1492
Radek Krejci693262f2019-04-29 15:23:20 +02001493 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001494
Michal Vasko7f45cf22020-10-01 12:49:44 +02001495 if (choice->dflt.str) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001496 ypr_open(ctx->out, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001497 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt.str, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001498 }
1499
Radek Krejci693262f2019-04-29 15:23:20 +02001500 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001501
1502 LY_LIST_FOR(choice->child, child) {
1503 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001504 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001505 }
1506
1507 LEVEL--;
1508 ypr_close(ctx, flag);
1509}
1510
1511static void
Radek Krejci693262f2019-04-29 15:23:20 +02001512yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1513{
Radek Krejci857189e2020-09-01 13:26:36 +02001514 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001515 struct lysc_node_case *cs;
1516 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1517
1518 yprc_node_common1(ctx, node, &flag);
1519
1520 if (choice->dflt) {
1521 ypr_open(ctx->out, &flag);
1522 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1523 }
1524
1525 yprc_node_common2(ctx, node, &flag);
1526
Michal Vasko22df3f02020-08-24 13:29:22 +02001527 for (cs = choice->cases; cs; cs = (struct lysc_node_case *)cs->next) {
Radek Krejci693262f2019-04-29 15:23:20 +02001528 ypr_open(ctx->out, &flag);
1529 yprc_case(ctx, cs);
1530 }
1531
1532 LEVEL--;
1533 ypr_close(ctx, flag);
1534}
1535
1536static void
1537yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001538{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001539 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001540 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1541
Radek Krejci693262f2019-04-29 15:23:20 +02001542 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001543
Radek Krejci693262f2019-04-29 15:23:20 +02001544 yprp_type(ctx, &leaf->type);
1545 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001546 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001547 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001548 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001549 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001550
Radek Krejci693262f2019-04-29 15:23:20 +02001551 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001552
1553 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001554 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001555}
1556
1557static void
Radek Krejci693262f2019-04-29 15:23:20 +02001558yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1559{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001560 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001561 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1562
1563 yprc_node_common1(ctx, node, NULL);
1564
1565 yprc_type(ctx, leaf->type);
1566 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1567 LY_ARRAY_FOR(leaf->musts, u) {
1568 yprc_must(ctx, &leaf->musts[u], NULL);
1569 }
Radek Krejcia1911222019-07-22 17:24:50 +02001570
1571 if (leaf->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001572 yprc_dflt_value(ctx, leaf->dflt, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001573 }
Radek Krejci693262f2019-04-29 15:23:20 +02001574
1575 yprc_node_common2(ctx, node, NULL);
1576
1577 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001578 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001579}
1580
1581static void
1582yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001583{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001584 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001585 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1586
Radek Krejci693262f2019-04-29 15:23:20 +02001587 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001588
Radek Krejci693262f2019-04-29 15:23:20 +02001589 yprp_type(ctx, &llist->type);
1590 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001591 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001592 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001593 }
1594 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001595 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001596 }
1597
Radek Krejci693262f2019-04-29 15:23:20 +02001598 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001599
1600 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001601 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001602 }
1603 if (llist->flags & LYS_SET_MAX) {
1604 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001605 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001606 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001607 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001608 }
1609 }
1610
1611 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001612 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001613 }
1614
Radek Krejci693262f2019-04-29 15:23:20 +02001615 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001616 ypr_description(ctx, node->dsc, node->exts, NULL);
1617 ypr_reference(ctx, node->ref, node->exts, NULL);
1618
1619 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001620 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001621}
1622
1623static void
Radek Krejci693262f2019-04-29 15:23:20 +02001624yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1625{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001626 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001627 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1628
1629 yprc_node_common1(ctx, node, NULL);
1630
1631 yprc_type(ctx, llist->type);
1632 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1633 LY_ARRAY_FOR(llist->musts, u) {
1634 yprc_must(ctx, &llist->musts[u], NULL);
1635 }
1636 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001637 yprc_dflt_value(ctx, llist->dflts[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001638 }
1639
1640 ypr_config(ctx, node->flags, node->exts, NULL);
1641
1642 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1643 if (llist->max) {
1644 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1645 } else {
1646 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1647 }
1648
1649 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1650
1651 ypr_status(ctx, node->flags, node->exts, NULL);
1652 ypr_description(ctx, node->dsc, node->exts, NULL);
1653 ypr_reference(ctx, node->ref, node->exts, NULL);
1654
1655 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001656 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001657}
1658
1659static void
1660yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001661{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001662 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001663 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001664 struct lysp_node *child;
1665 struct lysp_node_list *list = (struct lysp_node_list *)node;
1666
Radek Krejci693262f2019-04-29 15:23:20 +02001667 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001668
1669 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001670 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001671 }
1672 if (list->key) {
1673 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001674 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001675 }
1676 LY_ARRAY_FOR(list->uniques, u) {
1677 ypr_open(ctx->out, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001678 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u].str, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001679 }
1680
Radek Krejci693262f2019-04-29 15:23:20 +02001681 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001682
1683 if (list->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001684 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001685 }
1686 if (list->flags & LYS_SET_MAX) {
1687 if (list->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001688 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001689 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001690 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001691 }
1692 }
1693
1694 if (list->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001695 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001696 }
1697
Radek Krejci693262f2019-04-29 15:23:20 +02001698 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001699 ypr_description(ctx, node->dsc, node->exts, NULL);
1700 ypr_reference(ctx, node->ref, node->exts, NULL);
1701
1702 LY_ARRAY_FOR(list->typedefs, u) {
1703 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001704 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001705 }
1706
1707 LY_ARRAY_FOR(list->groupings, u) {
1708 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001709 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001710 }
1711
1712 LY_LIST_FOR(list->child, child) {
1713 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001714 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001715 }
1716
1717 LY_ARRAY_FOR(list->actions, u) {
1718 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001719 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001720 }
1721
1722 LY_ARRAY_FOR(list->notifs, u) {
1723 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001724 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001725 }
1726
1727 LEVEL--;
1728 ypr_close(ctx, flag);
1729}
1730
1731static void
Radek Krejci693262f2019-04-29 15:23:20 +02001732yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1733{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001734 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +02001735 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001736 struct lysc_node *child;
1737 struct lysc_node_list *list = (struct lysc_node_list *)node;
1738
1739 yprc_node_common1(ctx, node, &flag);
1740
1741 LY_ARRAY_FOR(list->musts, u) {
1742 yprc_must(ctx, &list->musts[u], NULL);
1743 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001744 if (!(list->flags & LYS_KEYLESS)) {
Radek Krejci693262f2019-04-29 15:23:20 +02001745 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +02001746 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001747 for (struct lysc_node *key = list->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko5233e962020-08-14 14:26:20 +02001748 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001749 }
Michal Vasko5233e962020-08-14 14:26:20 +02001750 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001751 }
1752 LY_ARRAY_FOR(list->uniques, u) {
1753 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +02001754 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001755 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001756 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001757 }
1758 ypr_close(ctx, 0);
1759 }
1760
1761 ypr_config(ctx, node->flags, node->exts, NULL);
1762
1763 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1764 if (list->max) {
1765 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1766 } else {
1767 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1768 }
1769
1770 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1771
1772 ypr_status(ctx, node->flags, node->exts, NULL);
1773 ypr_description(ctx, node->dsc, node->exts, NULL);
1774 ypr_reference(ctx, node->ref, node->exts, NULL);
1775
Radek Krejci52f65552020-09-01 17:03:35 +02001776 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001777 LY_LIST_FOR(list->child, child) {
1778 ypr_open(ctx->out, &flag);
1779 yprc_node(ctx, child);
1780 }
Radek Krejci693262f2019-04-29 15:23:20 +02001781
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001782 LY_ARRAY_FOR(list->actions, u) {
1783 ypr_open(ctx->out, &flag);
1784 yprc_action(ctx, &list->actions[u]);
1785 }
Radek Krejci693262f2019-04-29 15:23:20 +02001786
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001787 LY_ARRAY_FOR(list->notifs, u) {
1788 ypr_open(ctx->out, &flag);
1789 yprc_notification(ctx, &list->notifs[u]);
1790 }
Radek Krejci693262f2019-04-29 15:23:20 +02001791 }
1792
1793 LEVEL--;
1794 ypr_close(ctx, flag);
1795}
1796
1797static void
1798yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001799{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001800 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001801 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001802
Michal Vasko5233e962020-08-14 14:26:20 +02001803 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001804 LEVEL++;
1805
Radek Krejci693262f2019-04-29 15:23:20 +02001806 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1807 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001808
1809 LY_ARRAY_FOR(refine->musts, u) {
1810 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001811 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001812 }
1813
1814 if (refine->presence) {
1815 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001816 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001817 }
1818
1819 LY_ARRAY_FOR(refine->dflts, u) {
1820 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001821 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001822 }
1823
Radek Krejci693262f2019-04-29 15:23:20 +02001824 ypr_config(ctx, refine->flags, refine->exts, &flag);
1825 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001826
1827 if (refine->flags & LYS_SET_MIN) {
1828 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001829 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001830 }
1831 if (refine->flags & LYS_SET_MAX) {
1832 ypr_open(ctx->out, &flag);
1833 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001834 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001835 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001836 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001837 }
1838 }
1839
1840 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1841 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1842
1843 LEVEL--;
1844 ypr_close(ctx, flag);
1845}
1846
1847static void
Radek Krejci693262f2019-04-29 15:23:20 +02001848yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001849{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001850 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001851 struct lysp_node *child;
1852
Michal Vasko5233e962020-08-14 14:26:20 +02001853 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001854 LEVEL++;
1855
Radek Krejci693262f2019-04-29 15:23:20 +02001856 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1857 yprp_when(ctx, aug->when, NULL);
1858 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1859 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001860 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1861 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1862
1863 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001864 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001865 }
1866
1867 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001868 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001869 }
1870
1871 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001872 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001873 }
1874
1875 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001876 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877}
1878
Radek Krejcid3ca0632019-04-16 16:54:54 +02001879static void
Radek Krejci693262f2019-04-29 15:23:20 +02001880yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001881{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001882 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001883 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001884 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1885
Radek Krejci693262f2019-04-29 15:23:20 +02001886 yprp_node_common1(ctx, node, &flag);
1887 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001888
1889 LY_ARRAY_FOR(uses->refines, u) {
1890 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001891 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001892 }
1893
1894 LY_ARRAY_FOR(uses->augments, u) {
1895 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001896 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001897 }
1898
1899 LEVEL--;
1900 ypr_close(ctx, flag);
1901}
1902
1903static void
Radek Krejci693262f2019-04-29 15:23:20 +02001904yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001905{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001906 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001907 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001908 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1909
Radek Krejci693262f2019-04-29 15:23:20 +02001910 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001911
1912 LY_ARRAY_FOR(any->musts, u) {
1913 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001914 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001915 }
1916
Radek Krejci693262f2019-04-29 15:23:20 +02001917 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001918
1919 LEVEL--;
1920 ypr_close(ctx, flag);
1921}
1922
1923static void
Radek Krejci693262f2019-04-29 15:23:20 +02001924yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001925{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001926 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001927 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001928 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001929
Radek Krejci693262f2019-04-29 15:23:20 +02001930 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001931
Radek Krejci693262f2019-04-29 15:23:20 +02001932 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001933 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001934 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001935 }
1936
Radek Krejci693262f2019-04-29 15:23:20 +02001937 yprc_node_common2(ctx, node, &flag);
1938
Radek Krejcid3ca0632019-04-16 16:54:54 +02001939 LEVEL--;
1940 ypr_close(ctx, flag);
1941}
1942
1943static void
Radek Krejci693262f2019-04-29 15:23:20 +02001944yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001945{
1946 switch (node->nodetype) {
1947 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001948 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001949 break;
1950 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001951 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001952 break;
1953 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001954 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001955 break;
1956 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001957 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001958 break;
1959 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001960 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001961 break;
1962 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001963 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001964 break;
1965 case LYS_ANYXML:
1966 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001967 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001968 break;
1969 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001970 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001971 break;
1972 default:
1973 break;
1974 }
1975}
1976
1977static void
Radek Krejci693262f2019-04-29 15:23:20 +02001978yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
1979{
1980 switch (node->nodetype) {
1981 case LYS_CONTAINER:
1982 yprc_container(ctx, node);
1983 break;
1984 case LYS_CHOICE:
1985 yprc_choice(ctx, node);
1986 break;
1987 case LYS_LEAF:
1988 yprc_leaf(ctx, node);
1989 break;
1990 case LYS_LEAFLIST:
1991 yprc_leaflist(ctx, node);
1992 break;
1993 case LYS_LIST:
1994 yprc_list(ctx, node);
1995 break;
1996 case LYS_ANYXML:
1997 case LYS_ANYDATA:
1998 yprc_anydata(ctx, node);
1999 break;
2000 default:
2001 break;
2002 }
2003}
2004
2005static void
2006yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002007{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002008 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002009 struct lysp_deviate_add *add;
2010 struct lysp_deviate_rpl *rpl;
2011 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08002012 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002013
Michal Vasko5233e962020-08-14 14:26:20 +02002014 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002015 LEVEL++;
2016
Radek Krejci693262f2019-04-29 15:23:20 +02002017 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002018 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
2019 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
2020
fredgan2b11ddb2019-10-21 11:03:39 +08002021 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02002022 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08002023 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
2024 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002025 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002026 LEVEL++;
2027
fredgan2b11ddb2019-10-21 11:03:39 +08002028 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002029 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002030 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002031 continue;
2032 }
fredgan2b11ddb2019-10-21 11:03:39 +08002033 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002034 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002035 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002036 LEVEL++;
2037
Radek Krejci693262f2019-04-29 15:23:20 +02002038 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
2039 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002040 LY_ARRAY_FOR(add->musts, u) {
2041 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002042 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002043 LY_ARRAY_FOR(add->uniques, u) {
2044 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002046 LY_ARRAY_FOR(add->dflts, u) {
2047 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002048 }
Radek Krejci693262f2019-04-29 15:23:20 +02002049 ypr_config(ctx, add->flags, add->exts, NULL);
2050 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002051 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002052 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002053 }
2054 if (add->flags & LYS_SET_MAX) {
2055 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002056 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002057 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002058 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002059 }
2060 }
fredgan2b11ddb2019-10-21 11:03:39 +08002061 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002062 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002063 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002064 LEVEL++;
2065
Radek Krejci693262f2019-04-29 15:23:20 +02002066 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002067 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002068 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002069 }
Radek Krejci693262f2019-04-29 15:23:20 +02002070 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
2071 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
2072 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2073 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002074 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002075 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002076 }
2077 if (rpl->flags & LYS_SET_MAX) {
2078 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002079 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002080 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002081 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002082 }
2083 }
fredgan2b11ddb2019-10-21 11:03:39 +08002084 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002085 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002086 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002087 LEVEL++;
2088
Radek Krejci693262f2019-04-29 15:23:20 +02002089 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2090 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002091 LY_ARRAY_FOR(del->musts, u) {
2092 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002093 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002094 LY_ARRAY_FOR(del->uniques, u) {
2095 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002096 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002097 LY_ARRAY_FOR(del->dflts, u) {
2098 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002099 }
2100 }
2101
2102 LEVEL--;
2103 ypr_close(ctx, 1);
2104 }
2105
2106 LEVEL--;
2107 ypr_close(ctx, 1);
2108}
2109
Michal Vasko7c8439f2020-08-05 13:25:19 +02002110static void
2111yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002112{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002113 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002114
Radek Krejcid3ca0632019-04-16 16:54:54 +02002115 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002116 ly_print_(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002117 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002118 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2119 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002120 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002121 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002122 }
Radek Krejci693262f2019-04-29 15:23:20 +02002123 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2124 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002125 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002126 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002127 }
2128 LY_ARRAY_FOR(modp->includes, u) {
2129 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002130 ly_print_(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002131 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002132 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002133 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002134 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002135 }
Radek Krejci693262f2019-04-29 15:23:20 +02002136 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2137 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002138 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002139 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002140 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002141 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002142 }
2143 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002144}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002145
Michal Vasko7c8439f2020-08-05 13:25:19 +02002146static void
2147yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2148{
2149 LY_ARRAY_COUNT_TYPE u;
2150 struct lysp_node *data;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002151
Radek Krejcid3ca0632019-04-16 16:54:54 +02002152 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002153 ly_print_(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002154 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002155 }
2156 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002157 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002158 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002159 }
2160
2161 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002162 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002163 }
2164
2165 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002166 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002167 }
2168
2169 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002170 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002171 }
2172
2173 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002174 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002175 }
2176
2177 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002178 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002179 }
2180
2181 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002182 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002183 }
2184
2185 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002186 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002187 }
2188
2189 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002190 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002191 }
2192
2193 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002194 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002195 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002196}
2197
2198LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002199yang_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002200{
2201 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02002202 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002203
Michal Vasko5233e962020-08-14 14:26:20 +02002204 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002205 LEVEL++;
2206
2207 /* module-header-stmts */
2208 if (module->version) {
2209 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
2210 }
2211
2212 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2213 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2214
2215 /* linkage-stmts (import/include) */
2216 yang_print_parsed_linkage(ctx, modp);
2217
2218 /* meta-stmts */
2219 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002220 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002221 }
2222 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2223 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2224 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2225 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2226
2227 /* revision-stmts */
2228 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002229 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002230 }
2231 LY_ARRAY_FOR(modp->revs, u) {
2232 yprp_revision(ctx, &modp->revs[u]);
2233 }
2234 /* body-stmts */
2235 yang_print_parsed_body(ctx, modp);
2236
2237 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002238 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002239 ly_print_flush(out);
2240
2241 return LY_SUCCESS;
2242}
2243
2244static void
2245yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2246{
Michal Vaskoc3781c32020-10-06 14:04:08 +02002247 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->mod->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002248 LEVEL++;
2249 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
2250 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2251 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002252 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002253}
2254
2255LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002256yang_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002257{
2258 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02002259 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002260
Michal Vasko5233e962020-08-14 14:26:20 +02002261 ly_print_(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002262 LEVEL++;
2263
2264 /* submodule-header-stmts */
2265 if (submodp->version) {
2266 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
2267 }
2268
2269 yprp_belongsto(ctx, submodp);
2270
2271 /* linkage-stmts (import/include) */
2272 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2273
2274 /* meta-stmts */
2275 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002276 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002277 }
2278 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2279 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
2280 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2281 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
2282
2283 /* revision-stmts */
2284 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002285 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002286 }
2287 LY_ARRAY_FOR(submodp->revs, u) {
2288 yprp_revision(ctx, &submodp->revs[u]);
2289 }
2290 /* body-stmts */
2291 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002292
2293 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002294 ly_print_(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002295 ly_print_flush(out);
2296
2297 return LY_SUCCESS;
2298}
2299
2300LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002301yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002302{
Radek Krejci52f65552020-09-01 17:03:35 +02002303 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options}, *ctx = &ctx_;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002304
2305 yprc_node(ctx, node);
2306
2307 ly_print_flush(out);
2308 return LY_SUCCESS;
2309}
2310
2311LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002312yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002313{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002314 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002315 struct lysc_node *data;
2316 struct lysc_module *modc = module->compiled;
Radek Krejci52f65552020-09-01 17:03:35 +02002317 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002318
Michal Vasko5233e962020-08-14 14:26:20 +02002319 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002320 LEVEL++;
2321
Radek Krejci693262f2019-04-29 15:23:20 +02002322 /* module-header-stmts */
2323 if (module->version) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002324 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modc->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002325 }
2326 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2327 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2328
Michal Vasko7c8439f2020-08-05 13:25:19 +02002329 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002330
2331 /* meta-stmts */
2332 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002333 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002334 }
2335 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2336 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2337 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2338 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2339
2340 /* revision-stmts */
2341 if (module->revision) {
Michal Vasko5233e962020-08-14 14:26:20 +02002342 ly_print_(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002343 }
2344
2345 /* body-stmts */
2346 if (modc->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002347 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002348 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2349 }
2350
Radek Krejci14915cc2020-09-14 17:28:13 +02002351 LY_ARRAY_FOR(module->features, u) {
2352 yprc_feature(ctx, &module->features[u]);
Radek Krejci693262f2019-04-29 15:23:20 +02002353 }
2354
Radek Krejci80d281e2020-09-14 17:42:54 +02002355 LY_ARRAY_FOR(module->identities, u) {
2356 yprc_identity(ctx, &module->identities[u]);
Radek Krejci693262f2019-04-29 15:23:20 +02002357 }
2358
Radek Krejci52f65552020-09-01 17:03:35 +02002359 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002360 LY_LIST_FOR(modc->data, data) {
2361 yprc_node(ctx, data);
2362 }
Radek Krejci693262f2019-04-29 15:23:20 +02002363
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002364 LY_ARRAY_FOR(modc->rpcs, u) {
2365 yprc_action(ctx, &modc->rpcs[u]);
2366 }
Radek Krejci693262f2019-04-29 15:23:20 +02002367
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002368 LY_ARRAY_FOR(modc->notifs, u) {
2369 yprc_notification(ctx, &modc->notifs[u]);
2370 }
Radek Krejci693262f2019-04-29 15:23:20 +02002371 }
2372
2373 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002374 ly_print_(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002375 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002376
2377 return LY_SUCCESS;
2378}