blob: 2cd9604fa8dfa8785c5035b00c4068620b8c4897 [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
244 if (!ext->compiled && ext->yin) {
Michal Vasko5233e962020-08-14 14:26:20 +0200245 ly_print_(ctx->out, "%*s%s; // Model comes from different input format, extensions must be resolved first.\n", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200246 continue;
247 }
248
Radek Krejcif56e2a42019-09-09 14:15:25 +0200249 ypr_open(ctx->out, flag);
250 argument = NULL;
251 if (ext[u].compiled) {
252 argument = ext[u].compiled->argument;
253 } else {
254 argument = ext[u].argument;
255 }
256 if (argument) {
Michal Vasko5233e962020-08-14 14:26:20 +0200257 ly_print_(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200258 ypr_encode(ctx->out, argument, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200259 ly_print_(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200260 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200261 ly_print_(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200262 }
263
264 child_presence = 0;
265 LEVEL++;
266 LY_LIST_FOR(ext[u].child, stmt) {
267 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
268 continue;
269 }
270 if (!child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200271 ly_print_(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200272 child_presence = 1;
273 }
274 yprp_stmt(ctx, stmt);
275 }
276 LEVEL--;
277 if (child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200278 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200279 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200280 ly_print_(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200281 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200282 }
283}
284
Radek Krejci693262f2019-04-29 15:23:20 +0200285/**
286 * @param[in] count Number of extensions to print, 0 to print them all.
287 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200288static void
Radek Krejci693262f2019-04-29 15:23:20 +0200289yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci857189e2020-09-01 13:26:36 +0200290 struct lysc_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200291{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200292 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200293
294 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200295 count = LY_ARRAY_COUNT(ext);
Radek Krejci693262f2019-04-29 15:23:20 +0200296 }
297 LY_ARRAY_FOR(ext, u) {
298 if (!count) {
299 break;
300 }
301 /* TODO compiled extensions */
302 (void) ctx;
303 (void) substmt;
304 (void) substmt_index;
305 (void) flag;
306
307 count--;
308 }
309}
310
311static void
312ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200313{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200314 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200315 ly_bool extflag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200316
317 if (!text) {
318 /* nothing to print */
319 return;
320 }
321
322 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
Michal Vasko5233e962020-08-14 14:26:20 +0200323 ly_print_(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200324 } else {
325 ypr_text(ctx, ext_substmt_info[substmt].name, text,
Radek Krejci0f969882020-08-21 16:56:47 +0200326 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200327 }
328
329 LEVEL++;
330 LY_ARRAY_FOR(ext, u) {
Michal Vasko69730152020-10-09 16:30:07 +0200331 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 +0200332 continue;
333 }
Radek Krejci693262f2019-04-29 15:23:20 +0200334 if (ctx->schema == YPR_PARSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200335 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200336 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200337 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200338 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200339 }
340 LEVEL--;
341 ypr_close(ctx, extflag);
342}
343
344static void
Radek Krejci857189e2020-09-01 13:26:36 +0200345ypr_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 +0200346{
347 char *str;
348
Radek Krejci1deb5be2020-08-26 16:43:36 +0200349 if (asprintf(&str, "%lu", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200350 LOGMEM(ctx->module->ctx);
351 return;
352 }
353 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200354 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200355 free(str);
356}
357
358static void
Radek Krejci857189e2020-09-01 13:26:36 +0200359ypr_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 +0200360{
361 char *str;
362
Radek Krejci1deb5be2020-08-26 16:43:36 +0200363 if (asprintf(&str, "%ld", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200364 LOGMEM(ctx->module->ctx);
365 return;
366 }
367 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200368 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200369 free(str);
370}
371
372static void
Radek Krejci693262f2019-04-29 15:23:20 +0200373yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200374{
375 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +0200376 ly_print_(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200377 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200378 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
379 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
380 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200381 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200382 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200383 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200384 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200385 }
386}
387
388static void
Radek Krejci857189e2020-09-01 13:26:36 +0200389ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200390{
391 if (flags & LYS_MAND_MASK) {
392 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200393 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200394 }
395}
396
397static void
Radek Krejci857189e2020-09-01 13:26:36 +0200398ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200399{
400 if (flags & LYS_CONFIG_MASK) {
401 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200402 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200403 }
404}
405
406static void
Radek Krejci857189e2020-09-01 13:26:36 +0200407ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200408{
409 const char *status = NULL;
410
411 if (flags & LYS_STATUS_CURR) {
412 ypr_open(ctx->out, flag);
413 status = "current";
414 } else if (flags & LYS_STATUS_DEPRC) {
415 ypr_open(ctx->out, flag);
416 status = "deprecated";
417 } else if (flags & LYS_STATUS_OBSLT) {
418 ypr_open(ctx->out, flag);
419 status = "obsolete";
420 }
Radek Krejci693262f2019-04-29 15:23:20 +0200421
422 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200423}
424
425static void
Radek Krejci857189e2020-09-01 13:26:36 +0200426ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200427{
428 if (dsc) {
429 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200430 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200431 }
432}
433
434static void
Radek Krejci857189e2020-09-01 13:26:36 +0200435ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200436{
437 if (ref) {
438 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200439 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200440 }
441}
442
443static void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200444yprp_iffeatures(struct ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200445{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200446 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +0200447 ly_bool extflag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200448
Michal Vasko7f45cf22020-10-01 12:49:44 +0200449 LY_ARRAY_FOR(iffs, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200450 ypr_open(ctx->out, flag);
451 extflag = 0;
452
Michal Vasko7f45cf22020-10-01 12:49:44 +0200453 ly_print_(ctx->out, "%*sif-feature \"%s\"", INDENT, iffs[u].str);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200454
455 /* extensions */
456 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200457 LY_ARRAY_FOR(exts, v) {
458 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200459 }
460 LEVEL--;
461 ypr_close(ctx, extflag);
462 }
463}
464
465static void
Radek Krejci693262f2019-04-29 15:23:20 +0200466yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200467{
Radek Krejci857189e2020-09-01 13:26:36 +0200468 ly_bool flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200469 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200470
Michal Vasko5233e962020-08-14 14:26:20 +0200471 ly_print_(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200472 LEVEL++;
473
474 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200475 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200476 }
477
478 if (ext->argument) {
479 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200480 ly_print_(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200481 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200482 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200483 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200484 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 +0200485 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200486 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200487 }
488 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vasko69730152020-10-09 16:30:07 +0200489 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts)))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200490 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200491 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200492 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200493 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200494 ypr_close(ctx, flag2);
495 }
496
Radek Krejci693262f2019-04-29 15:23:20 +0200497 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200498 ypr_description(ctx, ext->dsc, ext->exts, &flag);
499 ypr_reference(ctx, ext->ref, ext->exts, &flag);
500
501 LEVEL--;
502 ypr_close(ctx, flag);
503}
504
505static void
Radek Krejci693262f2019-04-29 15:23:20 +0200506yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200507{
Radek Krejci857189e2020-09-01 13:26:36 +0200508 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200509
Michal Vasko5233e962020-08-14 14:26:20 +0200510 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200511 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200512 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
513 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
514 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200515 ypr_description(ctx, feat->dsc, feat->exts, &flag);
516 ypr_reference(ctx, feat->ref, feat->exts, &flag);
517 LEVEL--;
518 ypr_close(ctx, flag);
519}
520
521static void
Radek Krejci693262f2019-04-29 15:23:20 +0200522yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200523{
Radek Krejci857189e2020-09-01 13:26:36 +0200524 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200525 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200526
Michal Vasko5233e962020-08-14 14:26:20 +0200527 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200528 LEVEL++;
529
Radek Krejci693262f2019-04-29 15:23:20 +0200530 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
531 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200532
533 LY_ARRAY_FOR(ident->bases, u) {
534 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200535 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200536 }
537
Radek Krejci693262f2019-04-29 15:23:20 +0200538 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200539 ypr_description(ctx, ident->dsc, ident->exts, &flag);
540 ypr_reference(ctx, ident->ref, ident->exts, &flag);
541
542 LEVEL--;
543 ypr_close(ctx, flag);
544}
545
546static void
Radek Krejci693262f2019-04-29 15:23:20 +0200547yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
548{
Radek Krejci857189e2020-09-01 13:26:36 +0200549 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200550 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200551
Michal Vasko5233e962020-08-14 14:26:20 +0200552 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200553 LEVEL++;
554
555 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200556
557 LY_ARRAY_FOR(ident->derived, u) {
558 ypr_open(ctx->out, &flag);
559 if (ctx->module != ident->derived[u]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200560 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 +0200561 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200562 ly_print_(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200563 }
564 }
565
566 ypr_status(ctx, ident->flags, ident->exts, &flag);
567 ypr_description(ctx, ident->dsc, ident->exts, &flag);
568 ypr_reference(ctx, ident->ref, ident->exts, &flag);
569
570 LEVEL--;
571 ypr_close(ctx, flag);
572}
573
574static void
Radek Krejci857189e2020-09-01 13:26:36 +0200575yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200576{
Radek Krejci857189e2020-09-01 13:26:36 +0200577 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200578
579 if (!restr) {
580 return;
581 }
582
583 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200584 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100585 ypr_encode(ctx->out,
586 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
587 restr->arg.str : &restr->arg.str[1], -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200588 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200589
590 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200591 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100592 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200593 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
594 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200595 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200596 }
597 if (restr->emsg) {
598 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200599 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200600 }
601 if (restr->eapptag) {
602 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200603 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200604 }
Radek Krejci693262f2019-04-29 15:23:20 +0200605 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
606 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
607
Radek Krejcid3ca0632019-04-16 16:54:54 +0200608 LEVEL--;
609 ypr_close(ctx, inner_flag);
610}
611
612static void
Radek Krejci857189e2020-09-01 13:26:36 +0200613yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200614{
Radek Krejci857189e2020-09-01 13:26:36 +0200615 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200616
617 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200618 ly_print_(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200619 ypr_encode(ctx->out, must->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200620 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200621
622 LEVEL++;
623 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
624 if (must->emsg) {
625 ypr_open(ctx->out, &inner_flag);
626 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
627 }
628 if (must->eapptag) {
629 ypr_open(ctx->out, &inner_flag);
630 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
631 }
632 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
633 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
634
635 LEVEL--;
636 ypr_close(ctx, inner_flag);
637}
638
639static void
Radek Krejci857189e2020-09-01 13:26:36 +0200640yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200641{
Radek Krejci857189e2020-09-01 13:26:36 +0200642 ly_bool inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200643 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200644
Radek Krejci334ccc72019-06-12 13:49:29 +0200645 if (!range) {
646 return;
647 }
648
Radek Krejci693262f2019-04-29 15:23:20 +0200649 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200650 ly_print_(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200651 LY_ARRAY_FOR(range->parts, u) {
652 if (u > 0) {
Michal Vasko5233e962020-08-14 14:26:20 +0200653 ly_print_(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200654 }
655 if (range->parts[u].max_64 == range->parts[u].min_64) {
656 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200657 ly_print_(ctx->out, "%" PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200658 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200659 ly_print_(ctx->out, "%" PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200660 }
661 } else {
662 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200663 ly_print_(ctx->out, "%" PRIu64 "..%" PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200664 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200665 ly_print_(ctx->out, "%" PRId64 "..%" PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200666 }
667 }
668 }
Michal Vasko5233e962020-08-14 14:26:20 +0200669 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200670
671 LEVEL++;
672 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
673 if (range->emsg) {
674 ypr_open(ctx->out, &inner_flag);
675 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
676 }
677 if (range->eapptag) {
678 ypr_open(ctx->out, &inner_flag);
679 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
680 }
681 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
682 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
683
684 LEVEL--;
685 ypr_close(ctx, inner_flag);
686}
687
688static void
Radek Krejci857189e2020-09-01 13:26:36 +0200689yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200690{
Radek Krejci857189e2020-09-01 13:26:36 +0200691 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200692
693 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200694 ly_print_(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200695 ypr_encode(ctx->out, pattern->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200696 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200697
698 LEVEL++;
699 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
700 if (pattern->inverted) {
701 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
702 ypr_open(ctx->out, &inner_flag);
703 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
704 }
705 if (pattern->emsg) {
706 ypr_open(ctx->out, &inner_flag);
707 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
708 }
709 if (pattern->eapptag) {
710 ypr_open(ctx->out, &inner_flag);
711 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
712 }
713 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
714 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
715
716 LEVEL--;
717 ypr_close(ctx, inner_flag);
718}
719
720static void
Radek Krejci857189e2020-09-01 13:26:36 +0200721yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200722{
Radek Krejci857189e2020-09-01 13:26:36 +0200723 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200724
725 if (!when) {
726 return;
727 }
728 ypr_open(ctx->out, flag);
729
Michal Vasko5233e962020-08-14 14:26:20 +0200730 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200731 ypr_encode(ctx->out, when->cond, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200732 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200733
734 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200735 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200736 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
737 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
738 LEVEL--;
739 ypr_close(ctx, inner_flag);
740}
741
742static void
Radek Krejci857189e2020-09-01 13:26:36 +0200743yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200744{
Radek Krejci857189e2020-09-01 13:26:36 +0200745 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200746
747 if (!when) {
748 return;
749 }
750 ypr_open(ctx->out, flag);
751
Michal Vasko5233e962020-08-14 14:26:20 +0200752 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200753 ypr_encode(ctx->out, when->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200754 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200755
756 LEVEL++;
757 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
758 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
759 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
760 LEVEL--;
761 ypr_close(ctx, inner_flag);
762}
763
764static void
Radek Krejci857189e2020-09-01 13:26:36 +0200765yprp_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 +0200766{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200767 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200768 ly_bool inner_flag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200769
770 LY_ARRAY_FOR(items, u) {
771 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200772 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200773 ly_print_(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200774 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200775 ly_print_(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200776 ypr_encode(ctx->out, items[u].name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200777 ly_print_(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200778 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200779 inner_flag = 0;
780 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200781 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
782 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200783 if (items[u].flags & LYS_SET_VALUE) {
784 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200785 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200786 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200787 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200788 }
789 }
Radek Krejci693262f2019-04-29 15:23:20 +0200790 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200791 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
792 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
793 LEVEL--;
794 ypr_close(ctx, inner_flag);
795 }
796}
797
798static void
Radek Krejci693262f2019-04-29 15:23:20 +0200799yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200800{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200801 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200802 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200803
Michal Vasko5233e962020-08-14 14:26:20 +0200804 ly_print_(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200805 LEVEL++;
806
Radek Krejci693262f2019-04-29 15:23:20 +0200807 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200808
Radek Krejci693262f2019-04-29 15:23:20 +0200809 yprp_restr(ctx, type->range, "range", &flag);
810 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200811 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200812 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200813 }
Radek Krejci693262f2019-04-29 15:23:20 +0200814 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
815 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200816
817 if (type->path) {
818 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200819 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200820 }
821 if (type->flags & LYS_SET_REQINST) {
822 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200823 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200824 }
825 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200826 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200827 }
828 LY_ARRAY_FOR(type->bases, u) {
829 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200830 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200831 }
832 LY_ARRAY_FOR(type->types, u) {
833 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200834 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200835 }
836
837 LEVEL--;
838 ypr_close(ctx, flag);
839}
840
841static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200842yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200843{
Radek Krejci857189e2020-09-01 13:26:36 +0200844 ly_bool dynamic;
Radek Krejcia1911222019-07-22 17:24:50 +0200845 const char *str;
846
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200847 str = value->realtype->plugin->print(value, LY_PREF_JSON, NULL, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200848 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
849 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200850 free((void *)str);
Radek Krejcia1911222019-07-22 17:24:50 +0200851 }
852}
853
854static void
Radek Krejci693262f2019-04-29 15:23:20 +0200855yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
856{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200857 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200858 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200859
Michal Vasko5233e962020-08-14 14:26:20 +0200860 ly_print_(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200861 LEVEL++;
862
863 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200864
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200865 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200866 case LY_TYPE_BINARY: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200867 struct lysc_type_bin *bin = (struct lysc_type_bin *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200868 yprc_range(ctx, bin->length, type->basetype, &flag);
869 break;
870 }
871 case LY_TYPE_UINT8:
872 case LY_TYPE_UINT16:
873 case LY_TYPE_UINT32:
874 case LY_TYPE_UINT64:
875 case LY_TYPE_INT8:
876 case LY_TYPE_INT16:
877 case LY_TYPE_INT32:
878 case LY_TYPE_INT64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200879 struct lysc_type_num *num = (struct lysc_type_num *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200880 yprc_range(ctx, num->range, type->basetype, &flag);
881 break;
882 }
883 case LY_TYPE_STRING: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200884 struct lysc_type_str *str = (struct lysc_type_str *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200885 yprc_range(ctx, str->length, type->basetype, &flag);
886 LY_ARRAY_FOR(str->patterns, u) {
887 yprc_pattern(ctx, str->patterns[u], &flag);
888 }
889 break;
890 }
891 case LY_TYPE_BITS:
892 case LY_TYPE_ENUM: {
893 /* bits and enums structures are compatible */
Michal Vasko22df3f02020-08-24 13:29:22 +0200894 struct lysc_type_bits *bits = (struct lysc_type_bits *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200895 LY_ARRAY_FOR(bits->bits, u) {
896 struct lysc_type_bitenum_item *item = &bits->bits[u];
Radek Krejci857189e2020-09-01 13:26:36 +0200897 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200898
899 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200900 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200901 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200902 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200903 LEVEL++;
904 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200905 if (type->basetype == LY_TYPE_BITS) {
906 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
907 } else { /* LY_TYPE_ENUM */
908 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
909 }
910 ypr_status(ctx, item->flags, item->exts, &inner_flag);
911 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
912 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
913 LEVEL--;
914 ypr_close(ctx, inner_flag);
915 }
916 break;
917 }
918 case LY_TYPE_BOOL:
919 case LY_TYPE_EMPTY:
920 /* nothing to do */
921 break;
922 case LY_TYPE_DEC64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200923 struct lysc_type_dec *dec = (struct lysc_type_dec *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200924 ypr_open(ctx->out, &flag);
925 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
926 yprc_range(ctx, dec->range, dec->basetype, &flag);
927 break;
928 }
929 case LY_TYPE_IDENT: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200930 struct lysc_type_identityref *ident = (struct lysc_type_identityref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200931 LY_ARRAY_FOR(ident->bases, u) {
932 ypr_open(ctx->out, &flag);
933 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
934 }
935 break;
936 }
937 case LY_TYPE_INST: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200938 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200939 ypr_open(ctx->out, &flag);
940 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
941 break;
942 }
943 case LY_TYPE_LEAFREF: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200944 struct lysc_type_leafref *lr = (struct lysc_type_leafref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200945 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200946 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200947 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
948 yprc_type(ctx, lr->realtype);
949 break;
950 }
951 case LY_TYPE_UNION: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200952 struct lysc_type_union *un = (struct lysc_type_union *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200953 LY_ARRAY_FOR(un->types, u) {
954 ypr_open(ctx->out, &flag);
955 yprc_type(ctx, un->types[u]);
956 }
957 break;
958 }
959 default:
960 LOGINT(ctx->module->ctx);
961 }
962
963 LEVEL--;
964 ypr_close(ctx, flag);
965}
966
967static void
968yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200969{
Michal Vasko5233e962020-08-14 14:26:20 +0200970 ly_print_(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200971 LEVEL++;
972
Radek Krejci693262f2019-04-29 15:23:20 +0200973 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200974
Radek Krejci693262f2019-04-29 15:23:20 +0200975 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200976
977 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +0200978 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200979 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200980 if (tpdf->dflt.str) {
981 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200982 }
983
Radek Krejci693262f2019-04-29 15:23:20 +0200984 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200985 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
986 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
987
988 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200989 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200990}
991
Radek Krejci693262f2019-04-29 15:23:20 +0200992static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
993static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100994static void yprp_action(struct ypr_ctx *ctx, const struct lysp_node_action *action);
995static void yprp_notification(struct ypr_ctx *ctx, const struct lysp_node_notif *notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200996
997static void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100998yprp_grouping(struct ypr_ctx *ctx, const struct lysp_node_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200999{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001000 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001001 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001002 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001003 struct lysp_node_action *action;
1004 struct lysp_node_notif *notif;
1005 struct lysp_node_grp *subgrp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001006
Michal Vasko5233e962020-08-14 14:26:20 +02001007 ly_print_(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001008 LEVEL++;
1009
Radek Krejci693262f2019-04-29 15:23:20 +02001010 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1011 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001012 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1013 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001014
1015 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001016 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001017 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001018 }
1019
Radek Krejci2a9fc652021-01-22 17:44:34 +01001020 LY_LIST_FOR(grp->groupings, subgrp) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001021 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001022 yprp_grouping(ctx, subgrp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001023 }
1024
1025 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001026 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001027 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001028 }
1029
Radek Krejci2a9fc652021-01-22 17:44:34 +01001030 LY_LIST_FOR(grp->actions, action) {
1031 ypr_open(ctx->out, &flag);
1032 yprp_action(ctx, action);
1033 }
1034
1035 LY_LIST_FOR(grp->notifs, notif) {
1036 ypr_open(ctx->out, &flag);
1037 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001038 }
1039
1040 LEVEL--;
1041 ypr_close(ctx, flag);
1042}
1043
1044static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001045yprp_inout(struct ypr_ctx *ctx, const struct lysp_node_action_inout *inout, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001046{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001047 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001048 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001049 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001050
Michal Vasko7f45cf22020-10-01 12:49:44 +02001051 if (!inout->data) {
1052 /* no children */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001053 return;
1054 }
1055 ypr_open(ctx->out, flag);
1056
Michal Vasko5233e962020-08-14 14:26:20 +02001057 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001058 LEVEL++;
1059
Radek Krejci693262f2019-04-29 15:23:20 +02001060 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001061 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001062 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001063 }
1064 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001065 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001066 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001067 LY_LIST_FOR(inout->groupings, grp) {
1068 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001069 }
1070
1071 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001072 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001073 }
1074
1075 LEVEL--;
1076 ypr_close(ctx, 1);
1077}
1078
1079static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001080yprc_inout(struct ypr_ctx *ctx, const struct lysc_node_action *action, const struct lysc_node_action_inout *inout, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001081{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001082 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001083 struct lysc_node *data;
1084
1085 if (!inout->data) {
1086 /* input/output is empty */
1087 return;
1088 }
1089 ypr_open(ctx->out, flag);
1090
Michal Vasko5233e962020-08-14 14:26:20 +02001091 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001092 LEVEL++;
1093
Radek Krejci2a9fc652021-01-22 17:44:34 +01001094 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input.exts : action->output.exts, NULL, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001095 LY_ARRAY_FOR(inout->musts, u) {
1096 yprc_must(ctx, &inout->musts[u], NULL);
1097 }
1098
Radek Krejci52f65552020-09-01 17:03:35 +02001099 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001100 LY_LIST_FOR(inout->data, data) {
1101 yprc_node(ctx, data);
1102 }
Radek Krejci693262f2019-04-29 15:23:20 +02001103 }
1104
1105 LEVEL--;
1106 ypr_close(ctx, 1);
1107}
1108
1109static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001110yprp_notification(struct ypr_ctx *ctx, const struct lysp_node_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001111{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001112 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001113 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001114 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001115 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001116
Michal Vasko5233e962020-08-14 14:26:20 +02001117 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001118
1119 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001120 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1121 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122
1123 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001124 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001125 }
Radek Krejci693262f2019-04-29 15:23:20 +02001126 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001127 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1128 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1129
1130 LY_ARRAY_FOR(notif->typedefs, u) {
1131 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001132 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001133 }
1134
Radek Krejci2a9fc652021-01-22 17:44:34 +01001135 LY_LIST_FOR(notif->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001136 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001137 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001138 }
1139
1140 LY_LIST_FOR(notif->data, data) {
1141 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001142 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001143 }
1144
1145 LEVEL--;
1146 ypr_close(ctx, flag);
1147}
1148
1149static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001150yprc_notification(struct ypr_ctx *ctx, const struct lysc_node_notif *notif)
Radek Krejci693262f2019-04-29 15:23:20 +02001151{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001152 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001153 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001154 struct lysc_node *data;
1155
Michal Vasko5233e962020-08-14 14:26:20 +02001156 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001157
1158 LEVEL++;
1159 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001160
1161 LY_ARRAY_FOR(notif->musts, u) {
1162 yprc_must(ctx, &notif->musts[u], &flag);
1163 }
1164 ypr_status(ctx, notif->flags, notif->exts, &flag);
1165 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1166 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1167
Radek Krejci52f65552020-09-01 17:03:35 +02001168 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001169 LY_LIST_FOR(notif->data, data) {
1170 ypr_open(ctx->out, &flag);
1171 yprc_node(ctx, data);
1172 }
Radek Krejci693262f2019-04-29 15:23:20 +02001173 }
1174
1175 LEVEL--;
1176 ypr_close(ctx, flag);
1177}
1178
1179static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001180yprp_action(struct ypr_ctx *ctx, const struct lysp_node_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001181{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001182 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001183 ly_bool flag = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001184 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001185
Michal Vasko5233e962020-08-14 14:26:20 +02001186 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001187
1188 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001189 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1190 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1191 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001192 ypr_description(ctx, action->dsc, action->exts, &flag);
1193 ypr_reference(ctx, action->ref, action->exts, &flag);
1194
1195 LY_ARRAY_FOR(action->typedefs, u) {
1196 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001197 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001198 }
1199
Radek Krejci2a9fc652021-01-22 17:44:34 +01001200 LY_LIST_FOR(action->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001201 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001202 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001203 }
1204
Radek Krejci693262f2019-04-29 15:23:20 +02001205 yprp_inout(ctx, &action->input, &flag);
1206 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001207
1208 LEVEL--;
1209 ypr_close(ctx, flag);
1210}
1211
1212static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001213yprc_action(struct ypr_ctx *ctx, const struct lysc_node_action *action)
Radek Krejci693262f2019-04-29 15:23:20 +02001214{
Radek Krejci857189e2020-09-01 13:26:36 +02001215 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001216
Michal Vasko5233e962020-08-14 14:26:20 +02001217 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001218
1219 LEVEL++;
1220 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001221 ypr_status(ctx, action->flags, action->exts, &flag);
1222 ypr_description(ctx, action->dsc, action->exts, &flag);
1223 ypr_reference(ctx, action->ref, action->exts, &flag);
1224
1225 yprc_inout(ctx, action, &action->input, &flag);
1226 yprc_inout(ctx, action, &action->output, &flag);
1227
1228 LEVEL--;
1229 ypr_close(ctx, flag);
1230}
1231
1232static void
Radek Krejci857189e2020-09-01 13:26:36 +02001233yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001234{
Michal Vasko5233e962020-08-14 14:26:20 +02001235 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001236 LEVEL++;
1237
Radek Krejci693262f2019-04-29 15:23:20 +02001238 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1239 yprp_when(ctx, node->when, flag);
1240 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001241}
1242
1243static void
Radek Krejci857189e2020-09-01 13:26:36 +02001244yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001245{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001246 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001247
Michal Vasko5233e962020-08-14 14:26:20 +02001248 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001249 LEVEL++;
1250
1251 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1252 LY_ARRAY_FOR(node->when, u) {
1253 yprc_when(ctx, node->when[u], flag);
1254 }
Radek Krejci693262f2019-04-29 15:23:20 +02001255}
1256
1257/* macr oto unify the code */
1258#define YPR_NODE_COMMON2 \
1259 ypr_config(ctx, node->flags, node->exts, flag); \
1260 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1261 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1262 } \
1263 ypr_status(ctx, node->flags, node->exts, flag); \
1264 ypr_description(ctx, node->dsc, node->exts, flag); \
1265 ypr_reference(ctx, node->ref, node->exts, flag)
1266
1267static void
Radek Krejci857189e2020-09-01 13:26:36 +02001268yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001269{
1270 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001271}
1272
1273static void
Radek Krejci857189e2020-09-01 13:26:36 +02001274yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001275{
1276 YPR_NODE_COMMON2;
1277}
1278
1279#undef YPR_NODE_COMMON2
1280
1281static void
1282yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001283{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001284 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001285 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001286 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001287 struct lysp_node_action *action;
1288 struct lysp_node_notif *notif;
1289 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001290 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1291
Radek Krejci693262f2019-04-29 15:23:20 +02001292 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001293
1294 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001295 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001296 }
1297 if (cont->presence) {
1298 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001299 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001300 }
1301
Radek Krejci693262f2019-04-29 15:23:20 +02001302 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001303
1304 LY_ARRAY_FOR(cont->typedefs, u) {
1305 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001306 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001307 }
1308
Radek Krejci2a9fc652021-01-22 17:44:34 +01001309 LY_LIST_FOR(cont->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001310 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001311 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001312 }
1313
1314 LY_LIST_FOR(cont->child, child) {
1315 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001316 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001317 }
1318
Radek Krejci2a9fc652021-01-22 17:44:34 +01001319 LY_LIST_FOR(cont->actions, action) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001320 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001321 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001322 }
1323
Radek Krejci2a9fc652021-01-22 17:44:34 +01001324 LY_LIST_FOR(cont->notifs, notif) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001325 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001326 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001327 }
1328
1329 LEVEL--;
1330 ypr_close(ctx, flag);
1331}
1332
1333static void
Radek Krejci693262f2019-04-29 15:23:20 +02001334yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1335{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001336 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001337 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001338 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001339 struct lysc_node_action *action;
1340 struct lysc_node_notif *notif;
Radek Krejci693262f2019-04-29 15:23:20 +02001341 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1342
1343 yprc_node_common1(ctx, node, &flag);
1344
1345 LY_ARRAY_FOR(cont->musts, u) {
1346 yprc_must(ctx, &cont->musts[u], &flag);
1347 }
1348 if (cont->flags & LYS_PRESENCE) {
1349 ypr_open(ctx->out, &flag);
1350 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1351 }
1352
1353 yprc_node_common2(ctx, node, &flag);
1354
Radek Krejci52f65552020-09-01 17:03:35 +02001355 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001356 LY_LIST_FOR(cont->child, child) {
1357 ypr_open(ctx->out, &flag);
1358 yprc_node(ctx, child);
1359 }
Radek Krejci693262f2019-04-29 15:23:20 +02001360
Radek Krejci2a9fc652021-01-22 17:44:34 +01001361 LY_LIST_FOR(cont->actions, action) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001362 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001363 yprc_action(ctx, action);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001364 }
Radek Krejci693262f2019-04-29 15:23:20 +02001365
Radek Krejci2a9fc652021-01-22 17:44:34 +01001366 LY_LIST_FOR(cont->notifs, notif) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001367 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001368 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001369 }
Radek Krejci693262f2019-04-29 15:23:20 +02001370 }
1371
1372 LEVEL--;
1373 ypr_close(ctx, flag);
1374}
1375
1376static void
1377yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1378{
Radek Krejci857189e2020-09-01 13:26:36 +02001379 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001380 struct lysp_node *child;
1381 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1382
1383 yprp_node_common1(ctx, node, &flag);
1384 yprp_node_common2(ctx, node, &flag);
1385
1386 LY_LIST_FOR(cas->child, child) {
1387 ypr_open(ctx->out, &flag);
1388 yprp_node(ctx, child);
1389 }
1390
1391 LEVEL--;
1392 ypr_close(ctx, flag);
1393}
1394
1395static void
1396yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1397{
Radek Krejci857189e2020-09-01 13:26:36 +02001398 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001399 struct lysc_node *child;
1400
Michal Vasko22df3f02020-08-24 13:29:22 +02001401 yprc_node_common1(ctx, (struct lysc_node *)cs, &flag);
1402 yprc_node_common2(ctx, (struct lysc_node *)cs, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001403
Radek Krejci52f65552020-09-01 17:03:35 +02001404 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001405 for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001406 ypr_open(ctx->out, &flag);
1407 yprc_node(ctx, child);
1408 }
Radek Krejci693262f2019-04-29 15:23:20 +02001409 }
1410
1411 LEVEL--;
1412 ypr_close(ctx, flag);
1413}
1414
1415static void
1416yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001417{
Radek Krejci857189e2020-09-01 13:26:36 +02001418 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001419 struct lysp_node *child;
1420 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1421
Radek Krejci693262f2019-04-29 15:23:20 +02001422 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001423
Michal Vasko7f45cf22020-10-01 12:49:44 +02001424 if (choice->dflt.str) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001425 ypr_open(ctx->out, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001426 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt.str, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001427 }
1428
Radek Krejci693262f2019-04-29 15:23:20 +02001429 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001430
1431 LY_LIST_FOR(choice->child, child) {
1432 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001433 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001434 }
1435
1436 LEVEL--;
1437 ypr_close(ctx, flag);
1438}
1439
1440static void
Radek Krejci693262f2019-04-29 15:23:20 +02001441yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1442{
Radek Krejci857189e2020-09-01 13:26:36 +02001443 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001444 struct lysc_node_case *cs;
1445 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1446
1447 yprc_node_common1(ctx, node, &flag);
1448
1449 if (choice->dflt) {
1450 ypr_open(ctx->out, &flag);
1451 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1452 }
1453
1454 yprc_node_common2(ctx, node, &flag);
1455
Michal Vasko22df3f02020-08-24 13:29:22 +02001456 for (cs = choice->cases; cs; cs = (struct lysc_node_case *)cs->next) {
Radek Krejci693262f2019-04-29 15:23:20 +02001457 ypr_open(ctx->out, &flag);
1458 yprc_case(ctx, cs);
1459 }
1460
1461 LEVEL--;
1462 ypr_close(ctx, flag);
1463}
1464
1465static void
1466yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001467{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001468 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001469 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1470
Radek Krejci693262f2019-04-29 15:23:20 +02001471 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001472
Radek Krejci693262f2019-04-29 15:23:20 +02001473 yprp_type(ctx, &leaf->type);
1474 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001475 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001476 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001477 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001478 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001479
Radek Krejci693262f2019-04-29 15:23:20 +02001480 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001481
1482 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001483 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001484}
1485
1486static void
Radek Krejci693262f2019-04-29 15:23:20 +02001487yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1488{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001489 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001490 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1491
1492 yprc_node_common1(ctx, node, NULL);
1493
1494 yprc_type(ctx, leaf->type);
1495 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1496 LY_ARRAY_FOR(leaf->musts, u) {
1497 yprc_must(ctx, &leaf->musts[u], NULL);
1498 }
Radek Krejcia1911222019-07-22 17:24:50 +02001499
1500 if (leaf->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001501 yprc_dflt_value(ctx, leaf->dflt, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001502 }
Radek Krejci693262f2019-04-29 15:23:20 +02001503
1504 yprc_node_common2(ctx, node, NULL);
1505
1506 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001507 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001508}
1509
1510static void
1511yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001512{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001513 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001514 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1515
Radek Krejci693262f2019-04-29 15:23:20 +02001516 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001517
Radek Krejci693262f2019-04-29 15:23:20 +02001518 yprp_type(ctx, &llist->type);
1519 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001520 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001521 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001522 }
1523 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001524 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001525 }
1526
Radek Krejci693262f2019-04-29 15:23:20 +02001527 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001528
1529 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001530 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001531 }
1532 if (llist->flags & LYS_SET_MAX) {
1533 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001534 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001535 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001536 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001537 }
1538 }
1539
1540 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001541 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001542 }
1543
Radek Krejci693262f2019-04-29 15:23:20 +02001544 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001545 ypr_description(ctx, node->dsc, node->exts, NULL);
1546 ypr_reference(ctx, node->ref, node->exts, NULL);
1547
1548 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001549 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001550}
1551
1552static void
Radek Krejci693262f2019-04-29 15:23:20 +02001553yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1554{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001555 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001556 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1557
1558 yprc_node_common1(ctx, node, NULL);
1559
1560 yprc_type(ctx, llist->type);
1561 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1562 LY_ARRAY_FOR(llist->musts, u) {
1563 yprc_must(ctx, &llist->musts[u], NULL);
1564 }
1565 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001566 yprc_dflt_value(ctx, llist->dflts[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001567 }
1568
1569 ypr_config(ctx, node->flags, node->exts, NULL);
1570
1571 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1572 if (llist->max) {
1573 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1574 } else {
1575 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1576 }
1577
1578 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1579
1580 ypr_status(ctx, node->flags, node->exts, NULL);
1581 ypr_description(ctx, node->dsc, node->exts, NULL);
1582 ypr_reference(ctx, node->ref, node->exts, NULL);
1583
1584 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001585 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001586}
1587
1588static void
1589yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001590{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001591 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001592 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001593 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001594 struct lysp_node_action *action;
1595 struct lysp_node_notif *notif;
1596 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001597 struct lysp_node_list *list = (struct lysp_node_list *)node;
1598
Radek Krejci693262f2019-04-29 15:23:20 +02001599 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001600
1601 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001602 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001603 }
1604 if (list->key) {
1605 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001606 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001607 }
1608 LY_ARRAY_FOR(list->uniques, u) {
1609 ypr_open(ctx->out, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001610 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u].str, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001611 }
1612
Michal Vaskoacb8e742020-10-20 10:28:02 +02001613 ypr_config(ctx, node->flags, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001614
1615 if (list->flags & LYS_SET_MIN) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001616 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001617 }
1618 if (list->flags & LYS_SET_MAX) {
1619 if (list->max) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001620 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001621 } else {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001622 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001623 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001624 }
1625 }
1626
1627 if (list->flags & LYS_ORDBY_MASK) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001628 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001629 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001630 }
1631
Michal Vaskoacb8e742020-10-20 10:28:02 +02001632 ypr_status(ctx, node->flags, node->exts, &flag);
1633 ypr_description(ctx, node->dsc, node->exts, &flag);
1634 ypr_reference(ctx, node->ref, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001635
1636 LY_ARRAY_FOR(list->typedefs, u) {
1637 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001638 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001639 }
1640
Radek Krejci2a9fc652021-01-22 17:44:34 +01001641 LY_LIST_FOR(list->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001642 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001643 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001644 }
1645
1646 LY_LIST_FOR(list->child, child) {
1647 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001648 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001649 }
1650
Radek Krejci2a9fc652021-01-22 17:44:34 +01001651 LY_LIST_FOR(list->actions, action) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001652 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001653 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001654 }
1655
Radek Krejci2a9fc652021-01-22 17:44:34 +01001656 LY_LIST_FOR(list->notifs, notif) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001657 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001658 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001659 }
1660
1661 LEVEL--;
1662 ypr_close(ctx, flag);
1663}
1664
1665static void
Radek Krejci693262f2019-04-29 15:23:20 +02001666yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1667{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001668 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001669 struct lysc_node_list *list = (struct lysc_node_list *)node;
1670
Michal Vaskoacb8e742020-10-20 10:28:02 +02001671 yprc_node_common1(ctx, node, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001672
1673 LY_ARRAY_FOR(list->musts, u) {
1674 yprc_must(ctx, &list->musts[u], NULL);
1675 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001676 if (!(list->flags & LYS_KEYLESS)) {
Michal Vasko5233e962020-08-14 14:26:20 +02001677 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001678 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 +02001679 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001680 }
Michal Vasko5233e962020-08-14 14:26:20 +02001681 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001682 }
1683 LY_ARRAY_FOR(list->uniques, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001684 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001685 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001686 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001687 }
1688 ypr_close(ctx, 0);
1689 }
1690
1691 ypr_config(ctx, node->flags, node->exts, NULL);
1692
1693 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1694 if (list->max) {
1695 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1696 } else {
1697 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1698 }
1699
1700 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1701
1702 ypr_status(ctx, node->flags, node->exts, NULL);
1703 ypr_description(ctx, node->dsc, node->exts, NULL);
1704 ypr_reference(ctx, node->ref, node->exts, NULL);
1705
Radek Krejci52f65552020-09-01 17:03:35 +02001706 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001707 struct lysc_node *child;
1708 struct lysc_node_action *action;
1709 struct lysc_node_notif *notif;
1710
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001711 LY_LIST_FOR(list->child, child) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001712 yprc_node(ctx, child);
1713 }
Radek Krejci693262f2019-04-29 15:23:20 +02001714
Radek Krejci2a9fc652021-01-22 17:44:34 +01001715 LY_LIST_FOR(list->actions, action) {
1716 yprc_action(ctx, action);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001717 }
Radek Krejci693262f2019-04-29 15:23:20 +02001718
Radek Krejci2a9fc652021-01-22 17:44:34 +01001719 LY_LIST_FOR(list->notifs, notif) {
1720 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001721 }
Radek Krejci693262f2019-04-29 15:23:20 +02001722 }
1723
1724 LEVEL--;
Michal Vaskoacb8e742020-10-20 10:28:02 +02001725 ypr_close(ctx, 1);
Radek Krejci693262f2019-04-29 15:23:20 +02001726}
1727
1728static void
1729yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001730{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001731 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001732 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001733
Michal Vasko5233e962020-08-14 14:26:20 +02001734 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001735 LEVEL++;
1736
Radek Krejci693262f2019-04-29 15:23:20 +02001737 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1738 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001739
1740 LY_ARRAY_FOR(refine->musts, u) {
1741 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001742 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001743 }
1744
1745 if (refine->presence) {
1746 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001747 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001748 }
1749
1750 LY_ARRAY_FOR(refine->dflts, u) {
1751 ypr_open(ctx->out, &flag);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001752 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001753 }
1754
Radek Krejci693262f2019-04-29 15:23:20 +02001755 ypr_config(ctx, refine->flags, refine->exts, &flag);
1756 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001757
1758 if (refine->flags & LYS_SET_MIN) {
1759 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001760 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001761 }
1762 if (refine->flags & LYS_SET_MAX) {
1763 ypr_open(ctx->out, &flag);
1764 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001765 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001766 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001767 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001768 }
1769 }
1770
1771 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1772 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1773
1774 LEVEL--;
1775 ypr_close(ctx, flag);
1776}
1777
1778static void
Radek Krejci2a9fc652021-01-22 17:44:34 +01001779yprp_augment(struct ypr_ctx *ctx, const struct lysp_node_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001780{
Radek Krejcid3ca0632019-04-16 16:54:54 +02001781 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001782 struct lysp_node_action *action;
1783 struct lysp_node_notif *notif;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001784
Michal Vasko5233e962020-08-14 14:26:20 +02001785 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001786 LEVEL++;
1787
Radek Krejci693262f2019-04-29 15:23:20 +02001788 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1789 yprp_when(ctx, aug->when, NULL);
1790 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1791 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001792 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1793 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1794
1795 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001796 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001797 }
1798
Radek Krejci2a9fc652021-01-22 17:44:34 +01001799 LY_LIST_FOR(aug->actions, action) {
1800 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001801 }
1802
Radek Krejci2a9fc652021-01-22 17:44:34 +01001803 LY_LIST_FOR(aug->notifs, notif) {
1804 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001805 }
1806
1807 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001808 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001809}
1810
Radek Krejcid3ca0632019-04-16 16:54:54 +02001811static void
Radek Krejci693262f2019-04-29 15:23:20 +02001812yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001813{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001814 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001815 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001816 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001817 struct lysp_node_augment *aug;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001818
Radek Krejci693262f2019-04-29 15:23:20 +02001819 yprp_node_common1(ctx, node, &flag);
1820 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001821
1822 LY_ARRAY_FOR(uses->refines, u) {
1823 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001824 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001825 }
1826
Radek Krejci2a9fc652021-01-22 17:44:34 +01001827 LY_LIST_FOR(uses->augments, aug) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001828 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001829 yprp_augment(ctx, aug);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001830 }
1831
1832 LEVEL--;
1833 ypr_close(ctx, flag);
1834}
1835
1836static void
Radek Krejci693262f2019-04-29 15:23:20 +02001837yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001838{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001839 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001840 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001841 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1842
Radek Krejci693262f2019-04-29 15:23:20 +02001843 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001844
1845 LY_ARRAY_FOR(any->musts, u) {
1846 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001847 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001848 }
1849
Radek Krejci693262f2019-04-29 15:23:20 +02001850 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001851
1852 LEVEL--;
1853 ypr_close(ctx, flag);
1854}
1855
1856static void
Radek Krejci693262f2019-04-29 15:23:20 +02001857yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001858{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001859 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001860 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001861 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001862
Radek Krejci693262f2019-04-29 15:23:20 +02001863 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001864
Radek Krejci693262f2019-04-29 15:23:20 +02001865 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001866 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001867 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001868 }
1869
Radek Krejci693262f2019-04-29 15:23:20 +02001870 yprc_node_common2(ctx, node, &flag);
1871
Radek Krejcid3ca0632019-04-16 16:54:54 +02001872 LEVEL--;
1873 ypr_close(ctx, flag);
1874}
1875
1876static void
Radek Krejci693262f2019-04-29 15:23:20 +02001877yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001878{
1879 switch (node->nodetype) {
1880 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001881 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001882 break;
1883 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001884 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001885 break;
1886 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001887 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001888 break;
1889 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001890 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001891 break;
1892 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001893 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001894 break;
1895 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001896 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001897 break;
1898 case LYS_ANYXML:
1899 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001900 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001901 break;
1902 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001903 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001904 break;
1905 default:
1906 break;
1907 }
1908}
1909
1910static void
Radek Krejci693262f2019-04-29 15:23:20 +02001911yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
1912{
1913 switch (node->nodetype) {
1914 case LYS_CONTAINER:
1915 yprc_container(ctx, node);
1916 break;
1917 case LYS_CHOICE:
1918 yprc_choice(ctx, node);
1919 break;
1920 case LYS_LEAF:
1921 yprc_leaf(ctx, node);
1922 break;
1923 case LYS_LEAFLIST:
1924 yprc_leaflist(ctx, node);
1925 break;
1926 case LYS_LIST:
1927 yprc_list(ctx, node);
1928 break;
1929 case LYS_ANYXML:
1930 case LYS_ANYDATA:
1931 yprc_anydata(ctx, node);
1932 break;
1933 default:
1934 break;
1935 }
1936}
1937
1938static void
1939yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001940{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001941 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001942 struct lysp_deviate_add *add;
1943 struct lysp_deviate_rpl *rpl;
1944 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08001945 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001946
Michal Vasko5233e962020-08-14 14:26:20 +02001947 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001948 LEVEL++;
1949
Radek Krejci693262f2019-04-29 15:23:20 +02001950 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001951 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1952 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1953
fredgan2b11ddb2019-10-21 11:03:39 +08001954 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001955 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08001956 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1957 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001958 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001959 LEVEL++;
1960
fredgan2b11ddb2019-10-21 11:03:39 +08001961 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001962 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001963 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001964 continue;
1965 }
fredgan2b11ddb2019-10-21 11:03:39 +08001966 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001967 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001968 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001969 LEVEL++;
1970
Radek Krejci693262f2019-04-29 15:23:20 +02001971 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
1972 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001973 LY_ARRAY_FOR(add->musts, u) {
1974 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001975 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001976 LY_ARRAY_FOR(add->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001977 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001978 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001979 LY_ARRAY_FOR(add->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001980 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001981 }
Radek Krejci693262f2019-04-29 15:23:20 +02001982 ypr_config(ctx, add->flags, add->exts, NULL);
1983 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001984 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001985 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001986 }
1987 if (add->flags & LYS_SET_MAX) {
1988 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001989 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001990 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001991 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001992 }
1993 }
fredgan2b11ddb2019-10-21 11:03:39 +08001994 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001995 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001996 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001997 LEVEL++;
1998
Radek Krejci693262f2019-04-29 15:23:20 +02001999 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002000 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002001 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002002 }
Radek Krejci693262f2019-04-29 15:23:20 +02002003 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002004 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002005 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2006 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002007 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002008 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002009 }
2010 if (rpl->flags & LYS_SET_MAX) {
2011 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002012 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002013 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002014 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002015 }
2016 }
fredgan2b11ddb2019-10-21 11:03:39 +08002017 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002018 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002019 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002020 LEVEL++;
2021
Radek Krejci693262f2019-04-29 15:23:20 +02002022 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2023 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002024 LY_ARRAY_FOR(del->musts, u) {
2025 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002026 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002027 LY_ARRAY_FOR(del->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002028 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002029 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002030 LY_ARRAY_FOR(del->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002031 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002032 }
2033 }
2034
2035 LEVEL--;
2036 ypr_close(ctx, 1);
2037 }
2038
2039 LEVEL--;
2040 ypr_close(ctx, 1);
2041}
2042
Michal Vasko7c8439f2020-08-05 13:25:19 +02002043static void
2044yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002046 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002047
Radek Krejcid3ca0632019-04-16 16:54:54 +02002048 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01002049 if (modp->imports[u].flags & LYS_INTERNAL) {
2050 continue;
2051 }
2052
Michal Vasko5233e962020-08-14 14:26:20 +02002053 ly_print_(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002054 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002055 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2056 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002057 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002058 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002059 }
Radek Krejci693262f2019-04-29 15:23:20 +02002060 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2061 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002062 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002063 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002064 }
2065 LY_ARRAY_FOR(modp->includes, u) {
Radek Krejci771928a2021-01-19 13:42:36 +01002066 if (modp->includes[u].injected) {
2067 /* do not print the includes injected from submodules */
2068 continue;
2069 }
Radek Krejcid3ca0632019-04-16 16:54:54 +02002070 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 +02002071 ly_print_(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002072 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002073 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002074 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002075 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002076 }
Radek Krejci693262f2019-04-29 15:23:20 +02002077 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2078 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002079 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002080 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002081 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002082 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002083 }
2084 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002085}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002086
Michal Vasko7c8439f2020-08-05 13:25:19 +02002087static void
2088yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2089{
2090 LY_ARRAY_COUNT_TYPE u;
2091 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002092 struct lysp_node_action *action;
2093 struct lysp_node_notif *notif;
2094 struct lysp_node_grp *grp;
2095 struct lysp_node_augment *aug;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002096
Radek Krejcid3ca0632019-04-16 16:54:54 +02002097 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002098 ly_print_(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002099 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002100 }
2101 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002102 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002103 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002104 }
2105
2106 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002107 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002108 }
2109
2110 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002111 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002112 }
2113
2114 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002115 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002116 }
2117
Radek Krejci2a9fc652021-01-22 17:44:34 +01002118 LY_LIST_FOR(modp->groupings, grp) {
2119 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002120 }
2121
2122 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002123 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002124 }
2125
Radek Krejci2a9fc652021-01-22 17:44:34 +01002126 LY_LIST_FOR(modp->augments, aug) {
2127 yprp_augment(ctx, aug);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002128 }
2129
Radek Krejci2a9fc652021-01-22 17:44:34 +01002130 LY_LIST_FOR(modp->rpcs, action) {
2131 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002132 }
2133
Radek Krejci2a9fc652021-01-22 17:44:34 +01002134 LY_LIST_FOR(modp->notifs, notif) {
2135 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002136 }
2137
2138 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002139 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002140 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002141}
2142
2143LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002144yang_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002145{
2146 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02002147 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002148
Michal Vasko5233e962020-08-14 14:26:20 +02002149 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002150 LEVEL++;
2151
2152 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002153 if (modp->version) {
2154 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002155 }
2156
2157 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2158 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2159
2160 /* linkage-stmts (import/include) */
2161 yang_print_parsed_linkage(ctx, modp);
2162
2163 /* meta-stmts */
2164 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002165 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002166 }
2167 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2168 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2169 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2170 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2171
2172 /* revision-stmts */
2173 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002174 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002175 }
2176 LY_ARRAY_FOR(modp->revs, u) {
2177 yprp_revision(ctx, &modp->revs[u]);
2178 }
2179 /* body-stmts */
2180 yang_print_parsed_body(ctx, modp);
2181
2182 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002183 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002184 ly_print_flush(out);
2185
2186 return LY_SUCCESS;
2187}
2188
2189static void
2190yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2191{
Michal Vaskoc3781c32020-10-06 14:04:08 +02002192 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->mod->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002193 LEVEL++;
2194 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
2195 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2196 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002197 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002198}
2199
2200LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002201yang_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002202{
2203 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02002204 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .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) {
2211 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
2212 }
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}