blob: d3b9d5e2cefeae404ca5a26751aeb18abf82bee5 [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 Krejci47fab892020-11-05 17:02:41 +010022#include <sys/types.h>
Radek Krejci693262f2019-04-29 15:23:20 +020023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020025#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020028#include "out_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "plugins_types.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020030#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "tree_data.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020034#include "tree_schema.h"
35#include "tree_schema_internal.h"
Radek Krejci693262f2019-04-29 15:23:20 +020036#include "xpath.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020037
Radek Krejcie7b95092019-05-15 11:03:07 +020038/**
39 * @brief Types of the YANG printers
40 */
Radek Krejci693262f2019-04-29 15:23:20 +020041enum schema_type {
Radek Krejcie7b95092019-05-15 11:03:07 +020042 YPR_PARSED, /**< YANG printer of the parsed schema */
43 YPR_COMPILED /**< YANG printer of the compiled schema */
Radek Krejci693262f2019-04-29 15:23:20 +020044};
45
Radek Krejcie7b95092019-05-15 11:03:07 +020046/**
47 * @brief YANG printer context.
48 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020049struct ypr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020050 struct ly_out *out; /**< output specification */
51 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
52 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
Radek Krejcie7b95092019-05-15 11:03:07 +020053 const struct lys_module *module; /**< schema to print */
54 enum schema_type schema; /**< type of the schema to print */
Radek Krejcid3ca0632019-04-16 16:54:54 +020055};
56
Radek Krejcie7b95092019-05-15 11:03:07 +020057/**
58 * @brief Print the given text as content of a double quoted YANG string,
59 * including encoding characters that have special meanings. The quotation marks
60 * are not printed.
61 *
62 * Follows RFC 7950, section 6.1.3.
63 *
64 * @param[in] out Output specification.
65 * @param[in] text String to be printed.
Radek Krejcif56e2a42019-09-09 14:15:25 +020066 * @param[in] len Length of the string from @p text to be printed. In case of -1,
Radek Krejcie7b95092019-05-15 11:03:07 +020067 * the @p text is printed completely as a NULL-terminated string.
68 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020069static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020070ypr_encode(struct ly_out *out, const char *text, ssize_t len)
Radek Krejcid3ca0632019-04-16 16:54:54 +020071{
Radek Krejci1deb5be2020-08-26 16:43:36 +020072 size_t i, start_len;
Radek Krejcid3ca0632019-04-16 16:54:54 +020073 const char *start;
74 char special = 0;
75
76 if (!len) {
77 return;
78 }
79
80 if (len < 0) {
81 len = strlen(text);
82 }
83
84 start = text;
85 start_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +020086 for (i = 0; i < (size_t)len; ++i) {
Radek Krejcid3ca0632019-04-16 16:54:54 +020087 switch (text[i]) {
88 case '\n':
89 case '\t':
90 case '\"':
91 case '\\':
92 special = text[i];
93 break;
94 default:
95 ++start_len;
96 break;
97 }
98
99 if (special) {
Michal Vasko5233e962020-08-14 14:26:20 +0200100 ly_write_(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200101 switch (special) {
102 case '\n':
Michal Vasko5233e962020-08-14 14:26:20 +0200103 ly_write_(out, "\\n", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200104 break;
105 case '\t':
Michal Vasko5233e962020-08-14 14:26:20 +0200106 ly_write_(out, "\\t", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200107 break;
108 case '\"':
Michal Vasko5233e962020-08-14 14:26:20 +0200109 ly_write_(out, "\\\"", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200110 break;
111 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200112 ly_write_(out, "\\\\", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200113 break;
114 }
115
116 start += start_len + 1;
117 start_len = 0;
118
119 special = 0;
120 }
121 }
122
Michal Vasko5233e962020-08-14 14:26:20 +0200123 ly_write_(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200124}
125
126static void
Radek Krejci857189e2020-09-01 13:26:36 +0200127ypr_open(struct ly_out *out, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200128{
129 if (flag && !*flag) {
130 *flag = 1;
Michal Vasko5233e962020-08-14 14:26:20 +0200131 ly_print_(out, " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200132 }
133}
134
135static void
Radek Krejci857189e2020-09-01 13:26:36 +0200136ypr_close(struct ypr_ctx *ctx, ly_bool flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200137{
138 if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200139 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200140 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200141 ly_print_(ctx->out, ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200142 }
143}
144
145static void
Radek Krejci857189e2020-09-01 13:26:36 +0200146ypr_text(struct ypr_ctx *ctx, const char *name, const char *text, ly_bool singleline, ly_bool closed)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200147{
148 const char *s, *t;
149
150 if (singleline) {
Michal Vasko5233e962020-08-14 14:26:20 +0200151 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200152 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200153 ly_print_(ctx->out, "%*s%s\n", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200154 LEVEL++;
155
Michal Vasko5233e962020-08-14 14:26:20 +0200156 ly_print_(ctx->out, "%*s\"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200157 }
158 t = text;
159 while ((s = strchr(t, '\n'))) {
160 ypr_encode(ctx->out, t, s - t);
Michal Vasko5233e962020-08-14 14:26:20 +0200161 ly_print_(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200162 t = s + 1;
163 if (*t != '\n') {
Michal Vasko5233e962020-08-14 14:26:20 +0200164 ly_print_(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200165 }
166 }
167
168 ypr_encode(ctx->out, t, strlen(t));
169 if (closed) {
Michal Vasko5233e962020-08-14 14:26:20 +0200170 ly_print_(ctx->out, "\";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200171 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200172 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200173 }
174 if (!singleline) {
175 LEVEL--;
176 }
177}
178
179static void
Radek Krejci693262f2019-04-29 15:23:20 +0200180yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200181{
182 struct lysp_stmt *childstmt;
183 const char *s, *t;
184
185 if (stmt->arg) {
186 if (stmt->flags) {
Michal Vasko5233e962020-08-14 14:26:20 +0200187 ly_print_(ctx->out, "%*s%s\n", INDENT, stmt->stmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200188 LEVEL++;
Michal Vasko5233e962020-08-14 14:26:20 +0200189 ly_print_(ctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'');
Radek Krejcid3ca0632019-04-16 16:54:54 +0200190 t = stmt->arg;
191 while ((s = strchr(t, '\n'))) {
192 ypr_encode(ctx->out, t, s - t);
Michal Vasko5233e962020-08-14 14:26:20 +0200193 ly_print_(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200194 t = s + 1;
195 if (*t != '\n') {
Michal Vasko5233e962020-08-14 14:26:20 +0200196 ly_print_(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200197 }
198 }
199 LEVEL--;
200 ypr_encode(ctx->out, t, strlen(t));
Michal Vasko5233e962020-08-14 14:26:20 +0200201 ly_print_(ctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200202 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200203 ly_print_(ctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200204 }
205 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200206 ly_print_(ctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200207 }
208
209 if (stmt->child) {
210 LEVEL++;
211 LY_LIST_FOR(stmt->child, childstmt) {
Radek Krejci693262f2019-04-29 15:23:20 +0200212 yprp_stmt(ctx, childstmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200213 }
214 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200215 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200216 }
217}
218
219/**
220 * @param[in] count Number of extensions to print, 0 to print them all.
221 */
222static void
Radek Krejci693262f2019-04-29 15:23:20 +0200223yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci857189e2020-09-01 13:26:36 +0200224 struct lysp_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200226 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227 struct lysp_stmt *stmt;
Radek Krejci857189e2020-09-01 13:26:36 +0200228 ly_bool child_presence;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200229 const char *argument;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200230
231 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200232 count = LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200233 }
234 LY_ARRAY_FOR(ext, u) {
235 if (!count) {
236 break;
237 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200238
Radek Krejcid3ca0632019-04-16 16:54:54 +0200239 count--;
Michal Vasko3e9bc2f2020-11-04 17:13:56 +0100240 if ((ext->flags & LYS_INTERNAL) || (ext->insubstmt != substmt) || (ext->insubstmt_index != substmt_index)) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200241 continue;
242 }
243
Radek Krejcif56e2a42019-09-09 14:15:25 +0200244 ypr_open(ctx->out, flag);
245 argument = NULL;
246 if (ext[u].compiled) {
247 argument = ext[u].compiled->argument;
248 } else {
249 argument = ext[u].argument;
250 }
251 if (argument) {
Michal Vasko5233e962020-08-14 14:26:20 +0200252 ly_print_(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200253 ypr_encode(ctx->out, argument, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200254 ly_print_(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200255 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200256 ly_print_(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200257 }
258
259 child_presence = 0;
260 LEVEL++;
261 LY_LIST_FOR(ext[u].child, stmt) {
262 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
263 continue;
264 }
265 if (!child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200266 ly_print_(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200267 child_presence = 1;
268 }
269 yprp_stmt(ctx, stmt);
270 }
271 LEVEL--;
272 if (child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200273 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200274 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200275 ly_print_(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200276 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200277 }
278}
279
Radek Krejci693262f2019-04-29 15:23:20 +0200280/**
281 * @param[in] count Number of extensions to print, 0 to print them all.
282 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200283static void
Radek Krejci693262f2019-04-29 15:23:20 +0200284yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci857189e2020-09-01 13:26:36 +0200285 struct lysc_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200286{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200287 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200288
289 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200290 count = LY_ARRAY_COUNT(ext);
Radek Krejci693262f2019-04-29 15:23:20 +0200291 }
292 LY_ARRAY_FOR(ext, u) {
293 if (!count) {
294 break;
295 }
296 /* TODO compiled extensions */
297 (void) ctx;
298 (void) substmt;
299 (void) substmt_index;
300 (void) flag;
301
302 count--;
303 }
304}
305
306static void
307ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200308{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200309 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200310 ly_bool extflag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200311
312 if (!text) {
313 /* nothing to print */
314 return;
315 }
316
Radek Krejcieccf6602021-02-05 19:42:54 +0100317 if (stmt_attr_info[substmt].flags & STMT_FLAG_ID) {
318 ly_print_(ctx->out, "%*s%s %s", INDENT, stmt_attr_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200319 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +0100320 ypr_text(ctx, stmt_attr_info[substmt].name, text,
321 (stmt_attr_info[substmt].flags & STMT_FLAG_YIN) ? 0 : 1, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200322 }
323
324 LEVEL++;
325 LY_ARRAY_FOR(ext, u) {
Michal Vasko69730152020-10-09 16:30:07 +0200326 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 +0200327 continue;
328 }
Radek Krejci693262f2019-04-29 15:23:20 +0200329 if (ctx->schema == YPR_PARSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200330 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200331 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200332 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200333 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200334 }
335 LEVEL--;
336 ypr_close(ctx, extflag);
337}
338
339static void
Radek Krejci857189e2020-09-01 13:26:36 +0200340ypr_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 +0200341{
342 char *str;
343
Radek Krejci1deb5be2020-08-26 16:43:36 +0200344 if (asprintf(&str, "%lu", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200345 LOGMEM(ctx->module->ctx);
346 return;
347 }
348 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200349 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200350 free(str);
351}
352
353static void
Radek Krejci857189e2020-09-01 13:26:36 +0200354ypr_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 +0200355{
356 char *str;
357
Radek Krejci1deb5be2020-08-26 16:43:36 +0200358 if (asprintf(&str, "%ld", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200359 LOGMEM(ctx->module->ctx);
360 return;
361 }
362 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200363 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200364 free(str);
365}
366
367static void
Radek Krejci693262f2019-04-29 15:23:20 +0200368yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200369{
370 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +0200371 ly_print_(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200372 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200373 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
374 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
375 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200376 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200377 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200378 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200379 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200380 }
381}
382
383static void
Radek Krejci857189e2020-09-01 13:26:36 +0200384ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200385{
386 if (flags & LYS_MAND_MASK) {
387 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200388 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200389 }
390}
391
392static void
Radek Krejci857189e2020-09-01 13:26:36 +0200393ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200394{
395 if (flags & LYS_CONFIG_MASK) {
396 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200397 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200398 }
399}
400
401static void
Radek Krejci857189e2020-09-01 13:26:36 +0200402ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200403{
404 const char *status = NULL;
405
406 if (flags & LYS_STATUS_CURR) {
407 ypr_open(ctx->out, flag);
408 status = "current";
409 } else if (flags & LYS_STATUS_DEPRC) {
410 ypr_open(ctx->out, flag);
411 status = "deprecated";
412 } else if (flags & LYS_STATUS_OBSLT) {
413 ypr_open(ctx->out, flag);
414 status = "obsolete";
415 }
Radek Krejci693262f2019-04-29 15:23:20 +0200416
417 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200418}
419
420static void
Radek Krejci857189e2020-09-01 13:26:36 +0200421ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200422{
423 if (dsc) {
424 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200425 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200426 }
427}
428
429static void
Radek Krejci857189e2020-09-01 13:26:36 +0200430ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200431{
432 if (ref) {
433 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200434 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200435 }
436}
437
438static void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200439yprp_iffeatures(struct ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200440{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200441 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +0200442 ly_bool extflag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200443
Michal Vasko7f45cf22020-10-01 12:49:44 +0200444 LY_ARRAY_FOR(iffs, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200445 ypr_open(ctx->out, flag);
446 extflag = 0;
447
Michal Vasko7f45cf22020-10-01 12:49:44 +0200448 ly_print_(ctx->out, "%*sif-feature \"%s\"", INDENT, iffs[u].str);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200449
450 /* extensions */
451 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200452 LY_ARRAY_FOR(exts, v) {
Radek Krejcieccf6602021-02-05 19:42:54 +0100453 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IF_FEATURE, u, &exts[v], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200454 }
455 LEVEL--;
456 ypr_close(ctx, extflag);
457 }
458}
459
460static void
Radek Krejci693262f2019-04-29 15:23:20 +0200461yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200462{
Radek Krejci857189e2020-09-01 13:26:36 +0200463 ly_bool flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200464 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200465
Michal Vasko5233e962020-08-14 14:26:20 +0200466 ly_print_(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200467 LEVEL++;
468
469 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200470 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200471 }
472
473 if (ext->argument) {
474 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200475 ly_print_(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200476 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200477 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200478 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200479 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 +0200480 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200481 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200482 }
483 if ((ext->flags & LYS_YINELEM_MASK) ||
Radek Krejcieccf6602021-02-05 19:42:54 +0100484 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YIN_ELEMENT) != LY_ARRAY_COUNT(ext->exts)))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200485 ypr_open(ctx->out, &flag2);
Radek Krejcieccf6602021-02-05 19:42:54 +0100486 ypr_substmt(ctx, LYEXT_SUBSTMT_YIN_ELEMENT, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200487 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200488 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200489 ypr_close(ctx, flag2);
490 }
491
Radek Krejci693262f2019-04-29 15:23:20 +0200492 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200493 ypr_description(ctx, ext->dsc, ext->exts, &flag);
494 ypr_reference(ctx, ext->ref, ext->exts, &flag);
495
496 LEVEL--;
497 ypr_close(ctx, flag);
498}
499
500static void
Radek Krejci693262f2019-04-29 15:23:20 +0200501yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200502{
Radek Krejci857189e2020-09-01 13:26:36 +0200503 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200504
Michal Vasko5233e962020-08-14 14:26:20 +0200505 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200506 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200507 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
508 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
509 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200510 ypr_description(ctx, feat->dsc, feat->exts, &flag);
511 ypr_reference(ctx, feat->ref, feat->exts, &flag);
512 LEVEL--;
513 ypr_close(ctx, flag);
514}
515
516static void
Radek Krejci693262f2019-04-29 15:23:20 +0200517yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200518{
Radek Krejci857189e2020-09-01 13:26:36 +0200519 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200520 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200521
Michal Vasko5233e962020-08-14 14:26:20 +0200522 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200523 LEVEL++;
524
Radek Krejci693262f2019-04-29 15:23:20 +0200525 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
526 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200527
528 LY_ARRAY_FOR(ident->bases, u) {
529 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200530 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200531 }
532
Radek Krejci693262f2019-04-29 15:23:20 +0200533 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200534 ypr_description(ctx, ident->dsc, ident->exts, &flag);
535 ypr_reference(ctx, ident->ref, ident->exts, &flag);
536
537 LEVEL--;
538 ypr_close(ctx, flag);
539}
540
541static void
Radek Krejci693262f2019-04-29 15:23:20 +0200542yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
543{
Radek Krejci857189e2020-09-01 13:26:36 +0200544 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200545 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200546
Michal Vasko5233e962020-08-14 14:26:20 +0200547 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200548 LEVEL++;
549
550 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200551
552 LY_ARRAY_FOR(ident->derived, u) {
553 ypr_open(ctx->out, &flag);
554 if (ctx->module != ident->derived[u]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200555 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 +0200556 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200557 ly_print_(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200558 }
559 }
560
561 ypr_status(ctx, ident->flags, ident->exts, &flag);
562 ypr_description(ctx, ident->dsc, ident->exts, &flag);
563 ypr_reference(ctx, ident->ref, ident->exts, &flag);
564
565 LEVEL--;
566 ypr_close(ctx, flag);
567}
568
569static void
Radek Krejci857189e2020-09-01 13:26:36 +0200570yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200571{
Radek Krejci857189e2020-09-01 13:26:36 +0200572 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200573
574 if (!restr) {
575 return;
576 }
577
578 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200579 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100580 ypr_encode(ctx->out,
581 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
582 restr->arg.str : &restr->arg.str[1], -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200583 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200584
585 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200586 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100587 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200588 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
589 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200590 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200591 }
592 if (restr->emsg) {
593 ypr_open(ctx->out, &inner_flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100594 ypr_substmt(ctx, LYEXT_SUBSTMT_ERROR_MESSAGE, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200595 }
596 if (restr->eapptag) {
597 ypr_open(ctx->out, &inner_flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100598 ypr_substmt(ctx, LYEXT_SUBSTMT_ERROR_APP_TAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200599 }
Radek Krejci693262f2019-04-29 15:23:20 +0200600 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
601 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
602
Radek Krejcid3ca0632019-04-16 16:54:54 +0200603 LEVEL--;
604 ypr_close(ctx, inner_flag);
605}
606
607static void
Radek Krejci857189e2020-09-01 13:26:36 +0200608yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200609{
Radek Krejci857189e2020-09-01 13:26:36 +0200610 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200611
612 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200613 ly_print_(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200614 ypr_encode(ctx->out, must->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200615 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200616
617 LEVEL++;
618 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
619 if (must->emsg) {
620 ypr_open(ctx->out, &inner_flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100621 ypr_substmt(ctx, LYEXT_SUBSTMT_ERROR_MESSAGE, 0, must->emsg, must->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200622 }
623 if (must->eapptag) {
624 ypr_open(ctx->out, &inner_flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100625 ypr_substmt(ctx, LYEXT_SUBSTMT_ERROR_APP_TAG, 0, must->eapptag, must->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200626 }
627 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
628 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
629
630 LEVEL--;
631 ypr_close(ctx, inner_flag);
632}
633
634static void
Radek Krejci857189e2020-09-01 13:26:36 +0200635yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200636{
Radek Krejci857189e2020-09-01 13:26:36 +0200637 ly_bool inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200638 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200639
Radek Krejci334ccc72019-06-12 13:49:29 +0200640 if (!range) {
641 return;
642 }
643
Radek Krejci693262f2019-04-29 15:23:20 +0200644 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200645 ly_print_(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200646 LY_ARRAY_FOR(range->parts, u) {
647 if (u > 0) {
Michal Vasko5233e962020-08-14 14:26:20 +0200648 ly_print_(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200649 }
650 if (range->parts[u].max_64 == range->parts[u].min_64) {
651 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200652 ly_print_(ctx->out, "%" PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200653 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200654 ly_print_(ctx->out, "%" PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200655 }
656 } else {
657 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200658 ly_print_(ctx->out, "%" PRIu64 "..%" PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200659 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200660 ly_print_(ctx->out, "%" PRId64 "..%" PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200661 }
662 }
663 }
Michal Vasko5233e962020-08-14 14:26:20 +0200664 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200665
666 LEVEL++;
667 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
668 if (range->emsg) {
669 ypr_open(ctx->out, &inner_flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100670 ypr_substmt(ctx, LYEXT_SUBSTMT_ERROR_MESSAGE, 0, range->emsg, range->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200671 }
672 if (range->eapptag) {
673 ypr_open(ctx->out, &inner_flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100674 ypr_substmt(ctx, LYEXT_SUBSTMT_ERROR_APP_TAG, 0, range->eapptag, range->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200675 }
676 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
677 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
678
679 LEVEL--;
680 ypr_close(ctx, inner_flag);
681}
682
683static void
Radek Krejci857189e2020-09-01 13:26:36 +0200684yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200685{
Radek Krejci857189e2020-09-01 13:26:36 +0200686 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200687
688 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200689 ly_print_(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200690 ypr_encode(ctx->out, pattern->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200691 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200692
693 LEVEL++;
694 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
695 if (pattern->inverted) {
696 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
697 ypr_open(ctx->out, &inner_flag);
698 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
699 }
700 if (pattern->emsg) {
701 ypr_open(ctx->out, &inner_flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100702 ypr_substmt(ctx, LYEXT_SUBSTMT_ERROR_MESSAGE, 0, pattern->emsg, pattern->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200703 }
704 if (pattern->eapptag) {
705 ypr_open(ctx->out, &inner_flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100706 ypr_substmt(ctx, LYEXT_SUBSTMT_ERROR_APP_TAG, 0, pattern->eapptag, pattern->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200707 }
708 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
709 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
710
711 LEVEL--;
712 ypr_close(ctx, inner_flag);
713}
714
715static void
Radek Krejci857189e2020-09-01 13:26:36 +0200716yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200717{
Radek Krejci857189e2020-09-01 13:26:36 +0200718 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200719
720 if (!when) {
721 return;
722 }
723 ypr_open(ctx->out, flag);
724
Michal Vasko5233e962020-08-14 14:26:20 +0200725 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200726 ypr_encode(ctx->out, when->cond, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200727 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200728
729 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200730 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200731 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
732 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
733 LEVEL--;
734 ypr_close(ctx, inner_flag);
735}
736
737static void
Radek Krejci857189e2020-09-01 13:26:36 +0200738yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200739{
Radek Krejci857189e2020-09-01 13:26:36 +0200740 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200741
742 if (!when) {
743 return;
744 }
745 ypr_open(ctx->out, flag);
746
Michal Vasko5233e962020-08-14 14:26:20 +0200747 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200748 ypr_encode(ctx->out, when->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200749 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200750
751 LEVEL++;
752 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
753 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
754 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
755 LEVEL--;
756 ypr_close(ctx, inner_flag);
757}
758
759static void
Radek Krejci857189e2020-09-01 13:26:36 +0200760yprp_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 +0200761{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200762 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200763 ly_bool inner_flag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200764
765 LY_ARRAY_FOR(items, u) {
766 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200767 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200768 ly_print_(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200769 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200770 ly_print_(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200771 ypr_encode(ctx->out, items[u].name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200772 ly_print_(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200773 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200774 inner_flag = 0;
775 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200776 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
777 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200778 if (items[u].flags & LYS_SET_VALUE) {
779 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200780 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200781 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200782 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200783 }
784 }
Radek Krejci693262f2019-04-29 15:23:20 +0200785 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200786 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
787 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
788 LEVEL--;
789 ypr_close(ctx, inner_flag);
790 }
791}
792
793static void
Radek Krejci693262f2019-04-29 15:23:20 +0200794yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200795{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200796 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200797 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200798
Michal Vasko5233e962020-08-14 14:26:20 +0200799 ly_print_(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200800 LEVEL++;
801
Radek Krejci693262f2019-04-29 15:23:20 +0200802 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200803
Radek Krejci693262f2019-04-29 15:23:20 +0200804 yprp_restr(ctx, type->range, "range", &flag);
805 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200806 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200807 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200808 }
Radek Krejci693262f2019-04-29 15:23:20 +0200809 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
810 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200811
812 if (type->path) {
813 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200814 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200815 }
816 if (type->flags & LYS_SET_REQINST) {
817 ypr_open(ctx->out, &flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100818 ypr_substmt(ctx, LYEXT_SUBSTMT_REQUIRE_INSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200819 }
820 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejcieccf6602021-02-05 19:42:54 +0100821 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACTION_DIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200822 }
823 LY_ARRAY_FOR(type->bases, u) {
824 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200825 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200826 }
827 LY_ARRAY_FOR(type->types, u) {
828 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200829 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200830 }
831
832 LEVEL--;
833 ypr_close(ctx, flag);
834}
835
836static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200837yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200838{
Radek Krejci857189e2020-09-01 13:26:36 +0200839 ly_bool dynamic;
Radek Krejcia1911222019-07-22 17:24:50 +0200840 const char *str;
841
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200842 str = value->realtype->plugin->print(value, LY_PREF_JSON, NULL, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200843 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
844 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200845 free((void *)str);
Radek Krejcia1911222019-07-22 17:24:50 +0200846 }
847}
848
849static void
Radek Krejci693262f2019-04-29 15:23:20 +0200850yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
851{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200852 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200853 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200854
Michal Vasko5233e962020-08-14 14:26:20 +0200855 ly_print_(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200856 LEVEL++;
857
858 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200859
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200860 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200861 case LY_TYPE_BINARY: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200862 struct lysc_type_bin *bin = (struct lysc_type_bin *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200863 yprc_range(ctx, bin->length, type->basetype, &flag);
864 break;
865 }
866 case LY_TYPE_UINT8:
867 case LY_TYPE_UINT16:
868 case LY_TYPE_UINT32:
869 case LY_TYPE_UINT64:
870 case LY_TYPE_INT8:
871 case LY_TYPE_INT16:
872 case LY_TYPE_INT32:
873 case LY_TYPE_INT64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200874 struct lysc_type_num *num = (struct lysc_type_num *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200875 yprc_range(ctx, num->range, type->basetype, &flag);
876 break;
877 }
878 case LY_TYPE_STRING: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200879 struct lysc_type_str *str = (struct lysc_type_str *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200880 yprc_range(ctx, str->length, type->basetype, &flag);
881 LY_ARRAY_FOR(str->patterns, u) {
882 yprc_pattern(ctx, str->patterns[u], &flag);
883 }
884 break;
885 }
886 case LY_TYPE_BITS:
887 case LY_TYPE_ENUM: {
888 /* bits and enums structures are compatible */
Michal Vasko22df3f02020-08-24 13:29:22 +0200889 struct lysc_type_bits *bits = (struct lysc_type_bits *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200890 LY_ARRAY_FOR(bits->bits, u) {
891 struct lysc_type_bitenum_item *item = &bits->bits[u];
Radek Krejci857189e2020-09-01 13:26:36 +0200892 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200893
894 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200895 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200896 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200897 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200898 LEVEL++;
899 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200900 if (type->basetype == LY_TYPE_BITS) {
901 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
902 } else { /* LY_TYPE_ENUM */
903 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
904 }
905 ypr_status(ctx, item->flags, item->exts, &inner_flag);
906 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
907 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
908 LEVEL--;
909 ypr_close(ctx, inner_flag);
910 }
911 break;
912 }
913 case LY_TYPE_BOOL:
914 case LY_TYPE_EMPTY:
915 /* nothing to do */
916 break;
917 case LY_TYPE_DEC64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200918 struct lysc_type_dec *dec = (struct lysc_type_dec *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200919 ypr_open(ctx->out, &flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100920 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACTION_DIGITS, 0, type->exts, dec->fraction_digits, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200921 yprc_range(ctx, dec->range, dec->basetype, &flag);
922 break;
923 }
924 case LY_TYPE_IDENT: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200925 struct lysc_type_identityref *ident = (struct lysc_type_identityref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200926 LY_ARRAY_FOR(ident->bases, u) {
927 ypr_open(ctx->out, &flag);
928 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
929 }
930 break;
931 }
932 case LY_TYPE_INST: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200933 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200934 ypr_open(ctx->out, &flag);
Radek Krejcieccf6602021-02-05 19:42:54 +0100935 ypr_substmt(ctx, LYEXT_SUBSTMT_REQUIRE_INSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200936 break;
937 }
938 case LY_TYPE_LEAFREF: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200939 struct lysc_type_leafref *lr = (struct lysc_type_leafref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200940 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200941 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejcieccf6602021-02-05 19:42:54 +0100942 ypr_substmt(ctx, LYEXT_SUBSTMT_REQUIRE_INSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200943 yprc_type(ctx, lr->realtype);
944 break;
945 }
946 case LY_TYPE_UNION: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200947 struct lysc_type_union *un = (struct lysc_type_union *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200948 LY_ARRAY_FOR(un->types, u) {
949 ypr_open(ctx->out, &flag);
950 yprc_type(ctx, un->types[u]);
951 }
952 break;
953 }
954 default:
955 LOGINT(ctx->module->ctx);
956 }
957
958 LEVEL--;
959 ypr_close(ctx, flag);
960}
961
962static void
963yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200964{
Michal Vasko5233e962020-08-14 14:26:20 +0200965 ly_print_(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200966 LEVEL++;
967
Radek Krejci693262f2019-04-29 15:23:20 +0200968 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200969
Radek Krejci693262f2019-04-29 15:23:20 +0200970 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200971
972 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +0200973 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200974 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200975 if (tpdf->dflt.str) {
976 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200977 }
978
Radek Krejci693262f2019-04-29 15:23:20 +0200979 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200980 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
981 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
982
983 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200984 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200985}
986
Radek Krejci693262f2019-04-29 15:23:20 +0200987static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
988static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100989static void yprp_action(struct ypr_ctx *ctx, const struct lysp_node_action *action);
990static void yprp_notification(struct ypr_ctx *ctx, const struct lysp_node_notif *notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200991
992static void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100993yprp_grouping(struct ypr_ctx *ctx, const struct lysp_node_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200994{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200995 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200996 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200997 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100998 struct lysp_node_action *action;
999 struct lysp_node_notif *notif;
1000 struct lysp_node_grp *subgrp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001001
Michal Vasko5233e962020-08-14 14:26:20 +02001002 ly_print_(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001003 LEVEL++;
1004
Radek Krejci693262f2019-04-29 15:23:20 +02001005 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1006 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001007 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1008 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001009
1010 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001011 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001012 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001013 }
1014
Radek Krejci2a9fc652021-01-22 17:44:34 +01001015 LY_LIST_FOR(grp->groupings, subgrp) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001016 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001017 yprp_grouping(ctx, subgrp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001018 }
1019
Radek Krejci01180ac2021-01-27 08:48:22 +01001020 LY_LIST_FOR(grp->child, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001021 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001022 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001023 }
1024
Radek Krejci2a9fc652021-01-22 17:44:34 +01001025 LY_LIST_FOR(grp->actions, action) {
1026 ypr_open(ctx->out, &flag);
1027 yprp_action(ctx, action);
1028 }
1029
1030 LY_LIST_FOR(grp->notifs, notif) {
1031 ypr_open(ctx->out, &flag);
1032 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001033 }
1034
1035 LEVEL--;
1036 ypr_close(ctx, flag);
1037}
1038
1039static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001040yprp_inout(struct ypr_ctx *ctx, const struct lysp_node_action_inout *inout, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001041{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001042 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001043 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001044 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001045
Radek Krejci01180ac2021-01-27 08:48:22 +01001046 if (!inout->child) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001047 /* no children */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001048 return;
1049 }
1050 ypr_open(ctx->out, flag);
1051
Michal Vasko544e58a2021-01-28 14:33:41 +01001052 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, inout->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001053 LEVEL++;
1054
Radek Krejci693262f2019-04-29 15:23:20 +02001055 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001056 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001057 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001058 }
1059 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001060 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001061 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001062 LY_LIST_FOR(inout->groupings, grp) {
1063 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001064 }
1065
Radek Krejci01180ac2021-01-27 08:48:22 +01001066 LY_LIST_FOR(inout->child, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001067 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001068 }
1069
1070 LEVEL--;
1071 ypr_close(ctx, 1);
1072}
1073
1074static void
Michal Vasko544e58a2021-01-28 14:33:41 +01001075yprc_inout(struct ypr_ctx *ctx, const struct lysc_node_action_inout *inout, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001076{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001077 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001078 struct lysc_node *data;
1079
Radek Krejci01180ac2021-01-27 08:48:22 +01001080 if (!inout->child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001081 /* input/output is empty */
1082 return;
1083 }
1084 ypr_open(ctx->out, flag);
1085
Michal Vasko544e58a2021-01-28 14:33:41 +01001086 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, inout->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001087 LEVEL++;
1088
Michal Vasko544e58a2021-01-28 14:33:41 +01001089 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001090 LY_ARRAY_FOR(inout->musts, u) {
1091 yprc_must(ctx, &inout->musts[u], NULL);
1092 }
1093
Radek Krejci52f65552020-09-01 17:03:35 +02001094 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci01180ac2021-01-27 08:48:22 +01001095 LY_LIST_FOR(inout->child, data) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001096 yprc_node(ctx, data);
1097 }
Radek Krejci693262f2019-04-29 15:23:20 +02001098 }
1099
1100 LEVEL--;
1101 ypr_close(ctx, 1);
1102}
1103
1104static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001105yprp_notification(struct ypr_ctx *ctx, const struct lysp_node_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001106{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001107 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001108 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001109 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001110 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001111
Michal Vasko5233e962020-08-14 14:26:20 +02001112 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001113
1114 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001115 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1116 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001117
1118 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001119 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001120 }
Radek Krejci693262f2019-04-29 15:23:20 +02001121 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1123 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1124
1125 LY_ARRAY_FOR(notif->typedefs, u) {
1126 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001127 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001128 }
1129
Radek Krejci2a9fc652021-01-22 17:44:34 +01001130 LY_LIST_FOR(notif->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001131 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001132 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001133 }
1134
Radek Krejci01180ac2021-01-27 08:48:22 +01001135 LY_LIST_FOR(notif->child, data) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001136 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001137 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001138 }
1139
1140 LEVEL--;
1141 ypr_close(ctx, flag);
1142}
1143
1144static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001145yprc_notification(struct ypr_ctx *ctx, const struct lysc_node_notif *notif)
Radek Krejci693262f2019-04-29 15:23:20 +02001146{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001147 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001148 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001149 struct lysc_node *data;
1150
Michal Vasko5233e962020-08-14 14:26:20 +02001151 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001152
1153 LEVEL++;
1154 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001155
1156 LY_ARRAY_FOR(notif->musts, u) {
1157 yprc_must(ctx, &notif->musts[u], &flag);
1158 }
1159 ypr_status(ctx, notif->flags, notif->exts, &flag);
1160 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1161 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1162
Radek Krejci52f65552020-09-01 17:03:35 +02001163 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci01180ac2021-01-27 08:48:22 +01001164 LY_LIST_FOR(notif->child, data) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001165 ypr_open(ctx->out, &flag);
1166 yprc_node(ctx, data);
1167 }
Radek Krejci693262f2019-04-29 15:23:20 +02001168 }
1169
1170 LEVEL--;
1171 ypr_close(ctx, flag);
1172}
1173
1174static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001175yprp_action(struct ypr_ctx *ctx, const struct lysp_node_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001176{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001177 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001178 ly_bool flag = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001179 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001180
Michal Vasko5233e962020-08-14 14:26:20 +02001181 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001182
1183 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001184 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1185 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1186 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001187 ypr_description(ctx, action->dsc, action->exts, &flag);
1188 ypr_reference(ctx, action->ref, action->exts, &flag);
1189
1190 LY_ARRAY_FOR(action->typedefs, u) {
1191 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001192 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001193 }
1194
Radek Krejci2a9fc652021-01-22 17:44:34 +01001195 LY_LIST_FOR(action->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001196 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001197 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001198 }
1199
Radek Krejci693262f2019-04-29 15:23:20 +02001200 yprp_inout(ctx, &action->input, &flag);
1201 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001202
1203 LEVEL--;
1204 ypr_close(ctx, flag);
1205}
1206
1207static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001208yprc_action(struct ypr_ctx *ctx, const struct lysc_node_action *action)
Radek Krejci693262f2019-04-29 15:23:20 +02001209{
Radek Krejci857189e2020-09-01 13:26:36 +02001210 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001211
Michal Vasko5233e962020-08-14 14:26:20 +02001212 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001213
1214 LEVEL++;
1215 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001216 ypr_status(ctx, action->flags, action->exts, &flag);
1217 ypr_description(ctx, action->dsc, action->exts, &flag);
1218 ypr_reference(ctx, action->ref, action->exts, &flag);
1219
Michal Vasko544e58a2021-01-28 14:33:41 +01001220 yprc_inout(ctx, &action->input, &flag);
1221 yprc_inout(ctx, &action->output, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001222
1223 LEVEL--;
1224 ypr_close(ctx, flag);
1225}
1226
1227static void
Radek Krejci857189e2020-09-01 13:26:36 +02001228yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001229{
Michal Vasko5233e962020-08-14 14:26:20 +02001230 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001231 LEVEL++;
1232
Radek Krejci693262f2019-04-29 15:23:20 +02001233 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
Radek Krejci9a3823e2021-01-27 20:26:46 +01001234 yprp_when(ctx, lysp_node_when(node), flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001235 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001236}
1237
1238static void
Radek Krejci857189e2020-09-01 13:26:36 +02001239yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001240{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001241 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9a3823e2021-01-27 20:26:46 +01001242 struct lysc_when **when;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001243
Michal Vasko5233e962020-08-14 14:26:20 +02001244 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001245 LEVEL++;
1246
1247 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
Radek Krejci9a3823e2021-01-27 20:26:46 +01001248
1249 when = lysc_node_when(node);
1250 LY_ARRAY_FOR(when, u) {
1251 yprc_when(ctx, when[u], flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001252 }
Radek Krejci693262f2019-04-29 15:23:20 +02001253}
1254
1255/* macr oto unify the code */
1256#define YPR_NODE_COMMON2 \
1257 ypr_config(ctx, node->flags, node->exts, flag); \
1258 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1259 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1260 } \
1261 ypr_status(ctx, node->flags, node->exts, flag); \
1262 ypr_description(ctx, node->dsc, node->exts, flag); \
1263 ypr_reference(ctx, node->ref, node->exts, flag)
1264
1265static void
Radek Krejci857189e2020-09-01 13:26:36 +02001266yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001267{
1268 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001269}
1270
1271static void
Radek Krejci857189e2020-09-01 13:26:36 +02001272yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001273{
1274 YPR_NODE_COMMON2;
1275}
1276
1277#undef YPR_NODE_COMMON2
1278
1279static void
1280yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001281{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001282 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001283 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001284 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001285 struct lysp_node_action *action;
1286 struct lysp_node_notif *notif;
1287 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001288 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1289
Radek Krejci693262f2019-04-29 15:23:20 +02001290 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001291
1292 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001293 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001294 }
1295 if (cont->presence) {
1296 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001297 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001298 }
1299
Radek Krejci693262f2019-04-29 15:23:20 +02001300 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001301
1302 LY_ARRAY_FOR(cont->typedefs, u) {
1303 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001304 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001305 }
1306
Radek Krejci2a9fc652021-01-22 17:44:34 +01001307 LY_LIST_FOR(cont->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001308 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001309 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001310 }
1311
1312 LY_LIST_FOR(cont->child, child) {
1313 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001314 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001315 }
1316
Radek Krejci2a9fc652021-01-22 17:44:34 +01001317 LY_LIST_FOR(cont->actions, action) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001318 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001319 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001320 }
1321
Radek Krejci2a9fc652021-01-22 17:44:34 +01001322 LY_LIST_FOR(cont->notifs, notif) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001323 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001324 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001325 }
1326
1327 LEVEL--;
1328 ypr_close(ctx, flag);
1329}
1330
1331static void
Radek Krejci693262f2019-04-29 15:23:20 +02001332yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1333{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001334 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001335 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001336 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001337 struct lysc_node_action *action;
1338 struct lysc_node_notif *notif;
Radek Krejci693262f2019-04-29 15:23:20 +02001339 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1340
1341 yprc_node_common1(ctx, node, &flag);
1342
1343 LY_ARRAY_FOR(cont->musts, u) {
1344 yprc_must(ctx, &cont->musts[u], &flag);
1345 }
1346 if (cont->flags & LYS_PRESENCE) {
1347 ypr_open(ctx->out, &flag);
1348 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1349 }
1350
1351 yprc_node_common2(ctx, node, &flag);
1352
Radek Krejci52f65552020-09-01 17:03:35 +02001353 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001354 LY_LIST_FOR(cont->child, child) {
1355 ypr_open(ctx->out, &flag);
1356 yprc_node(ctx, child);
1357 }
Radek Krejci693262f2019-04-29 15:23:20 +02001358
Radek Krejci2a9fc652021-01-22 17:44:34 +01001359 LY_LIST_FOR(cont->actions, action) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001360 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001361 yprc_action(ctx, action);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001362 }
Radek Krejci693262f2019-04-29 15:23:20 +02001363
Radek Krejci2a9fc652021-01-22 17:44:34 +01001364 LY_LIST_FOR(cont->notifs, notif) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001365 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001366 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001367 }
Radek Krejci693262f2019-04-29 15:23:20 +02001368 }
1369
1370 LEVEL--;
1371 ypr_close(ctx, flag);
1372}
1373
1374static void
1375yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1376{
Radek Krejci857189e2020-09-01 13:26:36 +02001377 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001378 struct lysp_node *child;
1379 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1380
1381 yprp_node_common1(ctx, node, &flag);
1382 yprp_node_common2(ctx, node, &flag);
1383
1384 LY_LIST_FOR(cas->child, child) {
1385 ypr_open(ctx->out, &flag);
1386 yprp_node(ctx, child);
1387 }
1388
1389 LEVEL--;
1390 ypr_close(ctx, flag);
1391}
1392
1393static void
1394yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1395{
Radek Krejci857189e2020-09-01 13:26:36 +02001396 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001397 struct lysc_node *child;
1398
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001399 yprc_node_common1(ctx, &cs->node, &flag);
1400 yprc_node_common2(ctx, &cs->node, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001401
Radek Krejci52f65552020-09-01 17:03:35 +02001402 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001403 for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001404 ypr_open(ctx->out, &flag);
1405 yprc_node(ctx, child);
1406 }
Radek Krejci693262f2019-04-29 15:23:20 +02001407 }
1408
1409 LEVEL--;
1410 ypr_close(ctx, flag);
1411}
1412
1413static void
1414yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001415{
Radek Krejci857189e2020-09-01 13:26:36 +02001416 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001417 struct lysp_node *child;
1418 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1419
Radek Krejci693262f2019-04-29 15:23:20 +02001420 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001421
Michal Vasko7f45cf22020-10-01 12:49:44 +02001422 if (choice->dflt.str) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001423 ypr_open(ctx->out, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001424 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt.str, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001425 }
1426
Radek Krejci693262f2019-04-29 15:23:20 +02001427 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001428
1429 LY_LIST_FOR(choice->child, child) {
1430 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001431 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001432 }
1433
1434 LEVEL--;
1435 ypr_close(ctx, flag);
1436}
1437
1438static void
Radek Krejci693262f2019-04-29 15:23:20 +02001439yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1440{
Radek Krejci857189e2020-09-01 13:26:36 +02001441 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001442 struct lysc_node_case *cs;
1443 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1444
1445 yprc_node_common1(ctx, node, &flag);
1446
1447 if (choice->dflt) {
1448 ypr_open(ctx->out, &flag);
1449 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1450 }
1451
1452 yprc_node_common2(ctx, node, &flag);
1453
Michal Vasko22df3f02020-08-24 13:29:22 +02001454 for (cs = choice->cases; cs; cs = (struct lysc_node_case *)cs->next) {
Radek Krejci693262f2019-04-29 15:23:20 +02001455 ypr_open(ctx->out, &flag);
1456 yprc_case(ctx, cs);
1457 }
1458
1459 LEVEL--;
1460 ypr_close(ctx, flag);
1461}
1462
1463static void
1464yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001465{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001466 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001467 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1468
Radek Krejci693262f2019-04-29 15:23:20 +02001469 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001470
Radek Krejci693262f2019-04-29 15:23:20 +02001471 yprp_type(ctx, &leaf->type);
1472 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001473 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001474 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001475 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001476 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001477
Radek Krejci693262f2019-04-29 15:23:20 +02001478 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001479
1480 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001481 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001482}
1483
1484static void
Radek Krejci693262f2019-04-29 15:23:20 +02001485yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1486{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001487 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001488 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1489
1490 yprc_node_common1(ctx, node, NULL);
1491
1492 yprc_type(ctx, leaf->type);
1493 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1494 LY_ARRAY_FOR(leaf->musts, u) {
1495 yprc_must(ctx, &leaf->musts[u], NULL);
1496 }
Radek Krejcia1911222019-07-22 17:24:50 +02001497
1498 if (leaf->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001499 yprc_dflt_value(ctx, leaf->dflt, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001500 }
Radek Krejci693262f2019-04-29 15:23:20 +02001501
1502 yprc_node_common2(ctx, node, NULL);
1503
1504 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001505 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001506}
1507
1508static void
1509yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001510{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001511 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001512 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1513
Radek Krejci693262f2019-04-29 15:23:20 +02001514 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001515
Radek Krejci693262f2019-04-29 15:23:20 +02001516 yprp_type(ctx, &llist->type);
1517 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001518 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001519 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001520 }
1521 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001522 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001523 }
1524
Radek Krejci693262f2019-04-29 15:23:20 +02001525 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001526
1527 if (llist->flags & LYS_SET_MIN) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001528 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN_ELEMENTS, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001529 }
1530 if (llist->flags & LYS_SET_MAX) {
1531 if (llist->max) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001532 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001533 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +01001534 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001535 }
1536 }
1537
1538 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001539 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001540 }
1541
Radek Krejci693262f2019-04-29 15:23:20 +02001542 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001543 ypr_description(ctx, node->dsc, node->exts, NULL);
1544 ypr_reference(ctx, node->ref, node->exts, NULL);
1545
1546 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001547 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001548}
1549
1550static void
Radek Krejci693262f2019-04-29 15:23:20 +02001551yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1552{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001553 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001554 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1555
1556 yprc_node_common1(ctx, node, NULL);
1557
1558 yprc_type(ctx, llist->type);
1559 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1560 LY_ARRAY_FOR(llist->musts, u) {
1561 yprc_must(ctx, &llist->musts[u], NULL);
1562 }
1563 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001564 yprc_dflt_value(ctx, llist->dflts[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001565 }
1566
1567 ypr_config(ctx, node->flags, node->exts, NULL);
1568
Radek Krejcieccf6602021-02-05 19:42:54 +01001569 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN_ELEMENTS, 0, llist->exts, llist->min, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001570 if (llist->max) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001571 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, llist->exts, llist->max, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001572 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +01001573 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001574 }
1575
Radek Krejcieccf6602021-02-05 19:42:54 +01001576 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001577
1578 ypr_status(ctx, node->flags, node->exts, NULL);
1579 ypr_description(ctx, node->dsc, node->exts, NULL);
1580 ypr_reference(ctx, node->ref, node->exts, NULL);
1581
1582 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001583 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001584}
1585
1586static void
1587yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001588{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001589 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001590 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001591 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001592 struct lysp_node_action *action;
1593 struct lysp_node_notif *notif;
1594 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001595 struct lysp_node_list *list = (struct lysp_node_list *)node;
1596
Radek Krejci693262f2019-04-29 15:23:20 +02001597 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001598
1599 LY_ARRAY_FOR(list->musts, u) {
Michal Vaskoabf81a02021-02-03 11:30:41 +01001600 yprp_restr(ctx, &list->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001601 }
1602 if (list->key) {
1603 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001604 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001605 }
1606 LY_ARRAY_FOR(list->uniques, u) {
1607 ypr_open(ctx->out, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001608 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u].str, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001609 }
1610
Michal Vaskoacb8e742020-10-20 10:28:02 +02001611 ypr_config(ctx, node->flags, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001612
1613 if (list->flags & LYS_SET_MIN) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001614 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN_ELEMENTS, 0, list->exts, list->min, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001615 }
1616 if (list->flags & LYS_SET_MAX) {
1617 if (list->max) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001618 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, list->exts, list->max, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001619 } else {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001620 ypr_open(ctx->out, &flag);
Radek Krejcieccf6602021-02-05 19:42:54 +01001621 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001622 }
1623 }
1624
1625 if (list->flags & LYS_ORDBY_MASK) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001626 ypr_open(ctx->out, &flag);
Radek Krejcieccf6602021-02-05 19:42:54 +01001627 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001628 }
1629
Michal Vaskoacb8e742020-10-20 10:28:02 +02001630 ypr_status(ctx, node->flags, node->exts, &flag);
1631 ypr_description(ctx, node->dsc, node->exts, &flag);
1632 ypr_reference(ctx, node->ref, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001633
1634 LY_ARRAY_FOR(list->typedefs, u) {
1635 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001636 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001637 }
1638
Radek Krejci2a9fc652021-01-22 17:44:34 +01001639 LY_LIST_FOR(list->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001640 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001641 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001642 }
1643
1644 LY_LIST_FOR(list->child, child) {
1645 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001646 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001647 }
1648
Radek Krejci2a9fc652021-01-22 17:44:34 +01001649 LY_LIST_FOR(list->actions, action) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001650 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001651 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001652 }
1653
Radek Krejci2a9fc652021-01-22 17:44:34 +01001654 LY_LIST_FOR(list->notifs, notif) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001655 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001656 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001657 }
1658
1659 LEVEL--;
1660 ypr_close(ctx, flag);
1661}
1662
1663static void
Radek Krejci693262f2019-04-29 15:23:20 +02001664yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1665{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001666 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001667 struct lysc_node_list *list = (struct lysc_node_list *)node;
1668
Michal Vaskoacb8e742020-10-20 10:28:02 +02001669 yprc_node_common1(ctx, node, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001670
1671 LY_ARRAY_FOR(list->musts, u) {
1672 yprc_must(ctx, &list->musts[u], NULL);
1673 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001674 if (!(list->flags & LYS_KEYLESS)) {
Michal Vasko5233e962020-08-14 14:26:20 +02001675 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001676 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 +02001677 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001678 }
Michal Vasko5233e962020-08-14 14:26:20 +02001679 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001680 }
1681 LY_ARRAY_FOR(list->uniques, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001682 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001683 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001684 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001685 }
1686 ypr_close(ctx, 0);
1687 }
1688
1689 ypr_config(ctx, node->flags, node->exts, NULL);
1690
Radek Krejcieccf6602021-02-05 19:42:54 +01001691 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN_ELEMENTS, 0, list->exts, list->min, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001692 if (list->max) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001693 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, list->exts, list->max, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001694 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +01001695 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001696 }
1697
Radek Krejcieccf6602021-02-05 19:42:54 +01001698 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001699
1700 ypr_status(ctx, node->flags, node->exts, NULL);
1701 ypr_description(ctx, node->dsc, node->exts, NULL);
1702 ypr_reference(ctx, node->ref, node->exts, NULL);
1703
Radek Krejci52f65552020-09-01 17:03:35 +02001704 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001705 struct lysc_node *child;
1706 struct lysc_node_action *action;
1707 struct lysc_node_notif *notif;
1708
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001709 LY_LIST_FOR(list->child, child) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001710 yprc_node(ctx, child);
1711 }
Radek Krejci693262f2019-04-29 15:23:20 +02001712
Radek Krejci2a9fc652021-01-22 17:44:34 +01001713 LY_LIST_FOR(list->actions, action) {
1714 yprc_action(ctx, action);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001715 }
Radek Krejci693262f2019-04-29 15:23:20 +02001716
Radek Krejci2a9fc652021-01-22 17:44:34 +01001717 LY_LIST_FOR(list->notifs, notif) {
1718 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001719 }
Radek Krejci693262f2019-04-29 15:23:20 +02001720 }
1721
1722 LEVEL--;
Michal Vaskoacb8e742020-10-20 10:28:02 +02001723 ypr_close(ctx, 1);
Radek Krejci693262f2019-04-29 15:23:20 +02001724}
1725
1726static void
1727yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001728{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001729 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001730 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001731
Michal Vasko5233e962020-08-14 14:26:20 +02001732 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001733 LEVEL++;
1734
Radek Krejci693262f2019-04-29 15:23:20 +02001735 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1736 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001737
1738 LY_ARRAY_FOR(refine->musts, u) {
1739 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001740 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001741 }
1742
1743 if (refine->presence) {
1744 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001745 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001746 }
1747
1748 LY_ARRAY_FOR(refine->dflts, u) {
1749 ypr_open(ctx->out, &flag);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001750 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001751 }
1752
Radek Krejci693262f2019-04-29 15:23:20 +02001753 ypr_config(ctx, refine->flags, refine->exts, &flag);
1754 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001755
1756 if (refine->flags & LYS_SET_MIN) {
1757 ypr_open(ctx->out, &flag);
Radek Krejcieccf6602021-02-05 19:42:54 +01001758 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN_ELEMENTS, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001759 }
1760 if (refine->flags & LYS_SET_MAX) {
1761 ypr_open(ctx->out, &flag);
1762 if (refine->max) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001763 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001764 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +01001765 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001766 }
1767 }
1768
1769 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1770 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1771
1772 LEVEL--;
1773 ypr_close(ctx, flag);
1774}
1775
1776static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001777yprp_augment(struct ypr_ctx *ctx, const struct lysp_node_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001778{
Radek Krejcid3ca0632019-04-16 16:54:54 +02001779 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001780 struct lysp_node_action *action;
1781 struct lysp_node_notif *notif;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001782
Michal Vasko5233e962020-08-14 14:26:20 +02001783 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001784 LEVEL++;
1785
Radek Krejci693262f2019-04-29 15:23:20 +02001786 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1787 yprp_when(ctx, aug->when, NULL);
1788 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1789 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001790 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1791 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1792
1793 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001794 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001795 }
1796
Radek Krejci2a9fc652021-01-22 17:44:34 +01001797 LY_LIST_FOR(aug->actions, action) {
1798 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001799 }
1800
Radek Krejci2a9fc652021-01-22 17:44:34 +01001801 LY_LIST_FOR(aug->notifs, notif) {
1802 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001803 }
1804
1805 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001806 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001807}
1808
Radek Krejcid3ca0632019-04-16 16:54:54 +02001809static void
Radek Krejci693262f2019-04-29 15:23:20 +02001810yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001811{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001812 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001813 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001814 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001815 struct lysp_node_augment *aug;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001816
Radek Krejci693262f2019-04-29 15:23:20 +02001817 yprp_node_common1(ctx, node, &flag);
1818 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001819
1820 LY_ARRAY_FOR(uses->refines, u) {
1821 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001822 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001823 }
1824
Radek Krejci2a9fc652021-01-22 17:44:34 +01001825 LY_LIST_FOR(uses->augments, aug) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001826 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001827 yprp_augment(ctx, aug);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001828 }
1829
1830 LEVEL--;
1831 ypr_close(ctx, flag);
1832}
1833
1834static void
Radek Krejci693262f2019-04-29 15:23:20 +02001835yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001836{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001837 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001838 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001839 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1840
Radek Krejci693262f2019-04-29 15:23:20 +02001841 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001842
1843 LY_ARRAY_FOR(any->musts, u) {
1844 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001845 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001846 }
1847
Radek Krejci693262f2019-04-29 15:23:20 +02001848 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001849
1850 LEVEL--;
1851 ypr_close(ctx, flag);
1852}
1853
1854static void
Radek Krejci693262f2019-04-29 15:23:20 +02001855yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001856{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001857 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001858 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001859 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001860
Radek Krejci693262f2019-04-29 15:23:20 +02001861 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001862
Radek Krejci693262f2019-04-29 15:23:20 +02001863 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001864 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001865 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001866 }
1867
Radek Krejci693262f2019-04-29 15:23:20 +02001868 yprc_node_common2(ctx, node, &flag);
1869
Radek Krejcid3ca0632019-04-16 16:54:54 +02001870 LEVEL--;
1871 ypr_close(ctx, flag);
1872}
1873
1874static void
Radek Krejci693262f2019-04-29 15:23:20 +02001875yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001876{
1877 switch (node->nodetype) {
1878 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001879 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001880 break;
1881 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001882 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001883 break;
1884 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001885 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001886 break;
1887 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001888 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001889 break;
1890 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001891 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001892 break;
1893 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001894 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001895 break;
1896 case LYS_ANYXML:
1897 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001898 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001899 break;
1900 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001901 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001902 break;
1903 default:
1904 break;
1905 }
1906}
1907
1908static void
Radek Krejci693262f2019-04-29 15:23:20 +02001909yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
1910{
1911 switch (node->nodetype) {
1912 case LYS_CONTAINER:
1913 yprc_container(ctx, node);
1914 break;
1915 case LYS_CHOICE:
1916 yprc_choice(ctx, node);
1917 break;
1918 case LYS_LEAF:
1919 yprc_leaf(ctx, node);
1920 break;
1921 case LYS_LEAFLIST:
1922 yprc_leaflist(ctx, node);
1923 break;
1924 case LYS_LIST:
1925 yprc_list(ctx, node);
1926 break;
1927 case LYS_ANYXML:
1928 case LYS_ANYDATA:
1929 yprc_anydata(ctx, node);
1930 break;
1931 default:
1932 break;
1933 }
1934}
1935
1936static void
1937yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001938{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001939 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001940 struct lysp_deviate_add *add;
1941 struct lysp_deviate_rpl *rpl;
1942 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08001943 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001944
Michal Vasko5233e962020-08-14 14:26:20 +02001945 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001946 LEVEL++;
1947
Radek Krejci693262f2019-04-29 15:23:20 +02001948 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001949 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1950 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1951
fredgan2b11ddb2019-10-21 11:03:39 +08001952 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001953 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08001954 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1955 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001956 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001957 LEVEL++;
1958
fredgan2b11ddb2019-10-21 11:03:39 +08001959 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001960 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001961 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001962 continue;
1963 }
fredgan2b11ddb2019-10-21 11:03:39 +08001964 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001965 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001966 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001967 LEVEL++;
1968
Radek Krejci693262f2019-04-29 15:23:20 +02001969 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
1970 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001971 LY_ARRAY_FOR(add->musts, u) {
1972 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001973 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001974 LY_ARRAY_FOR(add->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001975 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001976 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001977 LY_ARRAY_FOR(add->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001978 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001979 }
Radek Krejci693262f2019-04-29 15:23:20 +02001980 ypr_config(ctx, add->flags, add->exts, NULL);
1981 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001982 if (add->flags & LYS_SET_MIN) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001983 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN_ELEMENTS, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001984 }
1985 if (add->flags & LYS_SET_MAX) {
1986 if (add->max) {
Radek Krejcieccf6602021-02-05 19:42:54 +01001987 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001988 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +01001989 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001990 }
1991 }
fredgan2b11ddb2019-10-21 11:03:39 +08001992 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001993 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001994 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001995 LEVEL++;
1996
Radek Krejci693262f2019-04-29 15:23:20 +02001997 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001998 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02001999 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002000 }
Radek Krejci693262f2019-04-29 15:23:20 +02002001 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002002 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002003 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2004 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002005 if (rpl->flags & LYS_SET_MIN) {
Radek Krejcieccf6602021-02-05 19:42:54 +01002006 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN_ELEMENTS, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002007 }
2008 if (rpl->flags & LYS_SET_MAX) {
2009 if (rpl->max) {
Radek Krejcieccf6602021-02-05 19:42:54 +01002010 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002011 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +01002012 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX_ELEMENTS, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002013 }
2014 }
fredgan2b11ddb2019-10-21 11:03:39 +08002015 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002016 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002017 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002018 LEVEL++;
2019
Radek Krejci693262f2019-04-29 15:23:20 +02002020 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2021 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002022 LY_ARRAY_FOR(del->musts, u) {
2023 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002024 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002025 LY_ARRAY_FOR(del->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002026 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002027 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002028 LY_ARRAY_FOR(del->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002029 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002030 }
2031 }
2032
2033 LEVEL--;
2034 ypr_close(ctx, 1);
2035 }
2036
2037 LEVEL--;
2038 ypr_close(ctx, 1);
2039}
2040
Michal Vasko7c8439f2020-08-05 13:25:19 +02002041static void
2042yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002043{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002044 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045
Radek Krejcid3ca0632019-04-16 16:54:54 +02002046 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01002047 if (modp->imports[u].flags & LYS_INTERNAL) {
2048 continue;
2049 }
2050
Michal Vasko5233e962020-08-14 14:26:20 +02002051 ly_print_(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002052 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002053 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2054 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002055 if (modp->imports[u].rev[0]) {
Radek Krejcieccf6602021-02-05 19:42:54 +01002056 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISION_DATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002057 }
Radek Krejci693262f2019-04-29 15:23:20 +02002058 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2059 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002060 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002061 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002062 }
2063 LY_ARRAY_FOR(modp->includes, u) {
Radek Krejci771928a2021-01-19 13:42:36 +01002064 if (modp->includes[u].injected) {
2065 /* do not print the includes injected from submodules */
2066 continue;
2067 }
Radek Krejcid3ca0632019-04-16 16:54:54 +02002068 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 +02002069 ly_print_(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002070 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002071 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002072 if (modp->includes[u].rev[0]) {
Radek Krejcieccf6602021-02-05 19:42:54 +01002073 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISION_DATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002074 }
Radek Krejci693262f2019-04-29 15:23:20 +02002075 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2076 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002077 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002078 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002079 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002080 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002081 }
2082 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002083}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002084
Michal Vasko7c8439f2020-08-05 13:25:19 +02002085static void
2086yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2087{
2088 LY_ARRAY_COUNT_TYPE u;
2089 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002090 struct lysp_node_action *action;
2091 struct lysp_node_notif *notif;
2092 struct lysp_node_grp *grp;
2093 struct lysp_node_augment *aug;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094
Radek Krejcid3ca0632019-04-16 16:54:54 +02002095 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002096 ly_print_(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002097 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002098 }
2099 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002100 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002101 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002102 }
2103
2104 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002105 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002106 }
2107
2108 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002109 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002110 }
2111
2112 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002113 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002114 }
2115
Radek Krejci2a9fc652021-01-22 17:44:34 +01002116 LY_LIST_FOR(modp->groupings, grp) {
2117 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002118 }
2119
2120 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002121 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002122 }
2123
Radek Krejci2a9fc652021-01-22 17:44:34 +01002124 LY_LIST_FOR(modp->augments, aug) {
2125 yprp_augment(ctx, aug);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002126 }
2127
Radek Krejci2a9fc652021-01-22 17:44:34 +01002128 LY_LIST_FOR(modp->rpcs, action) {
2129 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002130 }
2131
Radek Krejci2a9fc652021-01-22 17:44:34 +01002132 LY_LIST_FOR(modp->notifs, notif) {
2133 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002134 }
2135
2136 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002137 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002138 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002139}
2140
2141LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01002142yang_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002143{
2144 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7997d5a2021-02-22 10:55:56 +01002145 const struct lys_module *module = modp->mod;
Radek Krejci52f65552020-09-01 17:03:35 +02002146 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002147
Michal Vasko5233e962020-08-14 14:26:20 +02002148 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002149 LEVEL++;
2150
2151 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002152 if (modp->version) {
Radek Krejcieccf6602021-02-05 19:42:54 +01002153 ypr_substmt(ctx, LYEXT_SUBSTMT_YANG_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002154 }
2155
2156 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2157 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2158
2159 /* linkage-stmts (import/include) */
2160 yang_print_parsed_linkage(ctx, modp);
2161
2162 /* meta-stmts */
2163 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002164 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002165 }
2166 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2167 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2168 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2169 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2170
2171 /* revision-stmts */
2172 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002173 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002174 }
2175 LY_ARRAY_FOR(modp->revs, u) {
2176 yprp_revision(ctx, &modp->revs[u]);
2177 }
2178 /* body-stmts */
2179 yang_print_parsed_body(ctx, modp);
2180
2181 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002182 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002183 ly_print_flush(out);
2184
2185 return LY_SUCCESS;
2186}
2187
2188static void
2189yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2190{
Michal Vaskoc3781c32020-10-06 14:04:08 +02002191 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->mod->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002192 LEVEL++;
Radek Krejcieccf6602021-02-05 19:42:54 +01002193 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGS_TO, 0, submodp->exts, NULL, 0);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002194 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2195 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002196 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002197}
2198
2199LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01002200yang_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002201{
2202 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7997d5a2021-02-22 10:55:56 +01002203 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = submodp->mod, .schema = YPR_PARSED,
2204 .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002205
Michal Vasko5233e962020-08-14 14:26:20 +02002206 ly_print_(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002207 LEVEL++;
2208
2209 /* submodule-header-stmts */
2210 if (submodp->version) {
Radek Krejcieccf6602021-02-05 19:42:54 +01002211 ypr_substmt(ctx, LYEXT_SUBSTMT_YANG_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002212 }
2213
2214 yprp_belongsto(ctx, submodp);
2215
2216 /* linkage-stmts (import/include) */
2217 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2218
2219 /* meta-stmts */
2220 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002221 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002222 }
2223 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2224 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
2225 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2226 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
2227
2228 /* revision-stmts */
2229 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002230 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002231 }
2232 LY_ARRAY_FOR(submodp->revs, u) {
2233 yprp_revision(ctx, &submodp->revs[u]);
2234 }
2235 /* body-stmts */
2236 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002237
2238 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002239 ly_print_(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002240 ly_print_flush(out);
2241
2242 return LY_SUCCESS;
2243}
2244
2245LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002246yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002247{
Radek Krejci52f65552020-09-01 17:03:35 +02002248 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options}, *ctx = &ctx_;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002249
2250 yprc_node(ctx, node);
2251
2252 ly_print_flush(out);
2253 return LY_SUCCESS;
2254}
2255
2256LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002257yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002258{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002259 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002260 struct lysc_module *modc = module->compiled;
Radek Krejci52f65552020-09-01 17:03:35 +02002261 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002262
Michal Vasko5233e962020-08-14 14:26:20 +02002263 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002264 LEVEL++;
2265
Radek Krejci693262f2019-04-29 15:23:20 +02002266 /* module-header-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002267 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2268 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2269
Michal Vasko7c8439f2020-08-05 13:25:19 +02002270 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002271
2272 /* meta-stmts */
2273 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002274 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002275 }
2276 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2277 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2278 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2279 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2280
2281 /* revision-stmts */
2282 if (module->revision) {
Michal Vasko5233e962020-08-14 14:26:20 +02002283 ly_print_(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002284 }
2285
2286 /* body-stmts */
2287 if (modc->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002288 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002289 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2290 }
2291
Radek Krejci80d281e2020-09-14 17:42:54 +02002292 LY_ARRAY_FOR(module->identities, u) {
2293 yprc_identity(ctx, &module->identities[u]);
Radek Krejci693262f2019-04-29 15:23:20 +02002294 }
2295
Radek Krejci52f65552020-09-01 17:03:35 +02002296 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002297 struct lysc_node *data;
2298 struct lysc_node_action *rpc;
2299 struct lysc_node_notif *notif;
2300
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002301 LY_LIST_FOR(modc->data, data) {
2302 yprc_node(ctx, data);
2303 }
Radek Krejci693262f2019-04-29 15:23:20 +02002304
Radek Krejci2a9fc652021-01-22 17:44:34 +01002305 LY_LIST_FOR(modc->rpcs, rpc) {
2306 yprc_action(ctx, rpc);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002307 }
Radek Krejci693262f2019-04-29 15:23:20 +02002308
Radek Krejci2a9fc652021-01-22 17:44:34 +01002309 LY_LIST_FOR(modc->notifs, notif) {
2310 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002311 }
Radek Krejci693262f2019-04-29 15:23:20 +02002312 }
2313
2314 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002315 ly_print_(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002316 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002317
2318 return LY_SUCCESS;
2319}