blob: 81aba8506cd338671b4c6278277afc55f82fa9e3 [file] [log] [blame]
Radek Krejcid3ca0632019-04-16 16:54:54 +02001/**
2 * @file printer_yang.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief YANG printer
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcid3ca0632019-04-16 16:54:54 +020016
Radek Krejci693262f2019-04-29 15:23:20 +020017#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Radek Krejci693262f2019-04-29 15:23:20 +020022
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020024#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "plugins_types.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "out_internal.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020028#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "tree_data.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020032#include "tree_schema.h"
33#include "tree_schema_internal.h"
Radek Krejci693262f2019-04-29 15:23:20 +020034#include "xpath.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020035
Radek Krejcie7b95092019-05-15 11:03:07 +020036/**
37 * @brief Types of the YANG printers
38 */
Radek Krejci693262f2019-04-29 15:23:20 +020039enum schema_type {
Radek Krejcie7b95092019-05-15 11:03:07 +020040 YPR_PARSED, /**< YANG printer of the parsed schema */
41 YPR_COMPILED /**< YANG printer of the compiled schema */
Radek Krejci693262f2019-04-29 15:23:20 +020042};
43
Radek Krejcie7b95092019-05-15 11:03:07 +020044/**
45 * @brief YANG printer context.
46 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020047struct ypr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020048 struct ly_out *out; /**< output specification */
49 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
50 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
Radek Krejcie7b95092019-05-15 11:03:07 +020051 const struct lys_module *module; /**< schema to print */
52 enum schema_type schema; /**< type of the schema to print */
Radek Krejcid3ca0632019-04-16 16:54:54 +020053};
54
Radek Krejcie7b95092019-05-15 11:03:07 +020055/**
56 * @brief Print the given text as content of a double quoted YANG string,
57 * including encoding characters that have special meanings. The quotation marks
58 * are not printed.
59 *
60 * Follows RFC 7950, section 6.1.3.
61 *
62 * @param[in] out Output specification.
63 * @param[in] text String to be printed.
Radek Krejcif56e2a42019-09-09 14:15:25 +020064 * @param[in] len Length of the string from @p text to be printed. In case of -1,
Radek Krejcie7b95092019-05-15 11:03:07 +020065 * the @p text is printed completely as a NULL-terminated string.
66 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020067static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020068ypr_encode(struct ly_out *out, const char *text, ssize_t len)
Radek Krejcid3ca0632019-04-16 16:54:54 +020069{
Radek Krejci1deb5be2020-08-26 16:43:36 +020070 size_t i, start_len;
Radek Krejcid3ca0632019-04-16 16:54:54 +020071 const char *start;
72 char special = 0;
73
74 if (!len) {
75 return;
76 }
77
78 if (len < 0) {
79 len = strlen(text);
80 }
81
82 start = text;
83 start_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +020084 for (i = 0; i < (size_t)len; ++i) {
Radek Krejcid3ca0632019-04-16 16:54:54 +020085 switch (text[i]) {
86 case '\n':
87 case '\t':
88 case '\"':
89 case '\\':
90 special = text[i];
91 break;
92 default:
93 ++start_len;
94 break;
95 }
96
97 if (special) {
Michal Vasko5233e962020-08-14 14:26:20 +020098 ly_write_(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +020099 switch (special) {
100 case '\n':
Michal Vasko5233e962020-08-14 14:26:20 +0200101 ly_write_(out, "\\n", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200102 break;
103 case '\t':
Michal Vasko5233e962020-08-14 14:26:20 +0200104 ly_write_(out, "\\t", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200105 break;
106 case '\"':
Michal Vasko5233e962020-08-14 14:26:20 +0200107 ly_write_(out, "\\\"", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200108 break;
109 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200110 ly_write_(out, "\\\\", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200111 break;
112 }
113
114 start += start_len + 1;
115 start_len = 0;
116
117 special = 0;
118 }
119 }
120
Michal Vasko5233e962020-08-14 14:26:20 +0200121 ly_write_(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200122}
123
124static void
Radek Krejci857189e2020-09-01 13:26:36 +0200125ypr_open(struct ly_out *out, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200126{
127 if (flag && !*flag) {
128 *flag = 1;
Michal Vasko5233e962020-08-14 14:26:20 +0200129 ly_print_(out, " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200130 }
131}
132
133static void
Radek Krejci857189e2020-09-01 13:26:36 +0200134ypr_close(struct ypr_ctx *ctx, ly_bool flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200135{
136 if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200137 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200138 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200139 ly_print_(ctx->out, ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200140 }
141}
142
143static void
Radek Krejci857189e2020-09-01 13:26:36 +0200144ypr_text(struct ypr_ctx *ctx, const char *name, const char *text, ly_bool singleline, ly_bool closed)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200145{
146 const char *s, *t;
147
148 if (singleline) {
Michal Vasko5233e962020-08-14 14:26:20 +0200149 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200150 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200151 ly_print_(ctx->out, "%*s%s\n", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200152 LEVEL++;
153
Michal Vasko5233e962020-08-14 14:26:20 +0200154 ly_print_(ctx->out, "%*s\"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200155 }
156 t = text;
157 while ((s = strchr(t, '\n'))) {
158 ypr_encode(ctx->out, t, s - t);
Michal Vasko5233e962020-08-14 14:26:20 +0200159 ly_print_(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200160 t = s + 1;
161 if (*t != '\n') {
Michal Vasko5233e962020-08-14 14:26:20 +0200162 ly_print_(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200163 }
164 }
165
166 ypr_encode(ctx->out, t, strlen(t));
167 if (closed) {
Michal Vasko5233e962020-08-14 14:26:20 +0200168 ly_print_(ctx->out, "\";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200169 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200170 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200171 }
172 if (!singleline) {
173 LEVEL--;
174 }
175}
176
177static void
Radek Krejci693262f2019-04-29 15:23:20 +0200178yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200179{
180 struct lysp_stmt *childstmt;
181 const char *s, *t;
182
183 if (stmt->arg) {
184 if (stmt->flags) {
Michal Vasko5233e962020-08-14 14:26:20 +0200185 ly_print_(ctx->out, "%*s%s\n", INDENT, stmt->stmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200186 LEVEL++;
Michal Vasko5233e962020-08-14 14:26:20 +0200187 ly_print_(ctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'');
Radek Krejcid3ca0632019-04-16 16:54:54 +0200188 t = stmt->arg;
189 while ((s = strchr(t, '\n'))) {
190 ypr_encode(ctx->out, t, s - t);
Michal Vasko5233e962020-08-14 14:26:20 +0200191 ly_print_(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200192 t = s + 1;
193 if (*t != '\n') {
Michal Vasko5233e962020-08-14 14:26:20 +0200194 ly_print_(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200195 }
196 }
197 LEVEL--;
198 ypr_encode(ctx->out, t, strlen(t));
Michal Vasko5233e962020-08-14 14:26:20 +0200199 ly_print_(ctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200200 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200201 ly_print_(ctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200202 }
203 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200204 ly_print_(ctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200205 }
206
207 if (stmt->child) {
208 LEVEL++;
209 LY_LIST_FOR(stmt->child, childstmt) {
Radek Krejci693262f2019-04-29 15:23:20 +0200210 yprp_stmt(ctx, childstmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200211 }
212 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200213 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200214 }
215}
216
217/**
218 * @param[in] count Number of extensions to print, 0 to print them all.
219 */
220static void
Radek Krejci693262f2019-04-29 15:23:20 +0200221yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci857189e2020-09-01 13:26:36 +0200222 struct lysp_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200223{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200224 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225 struct lysp_stmt *stmt;
Radek Krejci857189e2020-09-01 13:26:36 +0200226 ly_bool child_presence;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200227 const char *argument;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200228
229 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200230 count = LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200231 }
232 LY_ARRAY_FOR(ext, u) {
233 if (!count) {
234 break;
235 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200236
Radek Krejcid3ca0632019-04-16 16:54:54 +0200237 count--;
Michal Vasko3e9bc2f2020-11-04 17:13:56 +0100238 if ((ext->flags & LYS_INTERNAL) || (ext->insubstmt != substmt) || (ext->insubstmt_index != substmt_index)) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200239 continue;
240 }
241
242 if (!ext->compiled && ext->yin) {
Michal Vasko5233e962020-08-14 14:26:20 +0200243 ly_print_(ctx->out, "%*s%s; // Model comes from different input format, extensions must be resolved first.\n", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200244 continue;
245 }
246
Radek Krejcif56e2a42019-09-09 14:15:25 +0200247 ypr_open(ctx->out, flag);
248 argument = NULL;
249 if (ext[u].compiled) {
250 argument = ext[u].compiled->argument;
251 } else {
252 argument = ext[u].argument;
253 }
254 if (argument) {
Michal Vasko5233e962020-08-14 14:26:20 +0200255 ly_print_(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200256 ypr_encode(ctx->out, argument, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200257 ly_print_(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200258 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200259 ly_print_(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200260 }
261
262 child_presence = 0;
263 LEVEL++;
264 LY_LIST_FOR(ext[u].child, stmt) {
265 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
266 continue;
267 }
268 if (!child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200269 ly_print_(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200270 child_presence = 1;
271 }
272 yprp_stmt(ctx, stmt);
273 }
274 LEVEL--;
275 if (child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200276 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200277 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200278 ly_print_(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200279 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200280 }
281}
282
Radek Krejci693262f2019-04-29 15:23:20 +0200283/**
284 * @param[in] count Number of extensions to print, 0 to print them all.
285 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200286static void
Radek Krejci693262f2019-04-29 15:23:20 +0200287yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci857189e2020-09-01 13:26:36 +0200288 struct lysc_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200289{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200290 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200291
292 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200293 count = LY_ARRAY_COUNT(ext);
Radek Krejci693262f2019-04-29 15:23:20 +0200294 }
295 LY_ARRAY_FOR(ext, u) {
296 if (!count) {
297 break;
298 }
299 /* TODO compiled extensions */
300 (void) ctx;
301 (void) substmt;
302 (void) substmt_index;
303 (void) flag;
304
305 count--;
306 }
307}
308
309static void
310ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200311{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200312 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200313 ly_bool extflag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200314
315 if (!text) {
316 /* nothing to print */
317 return;
318 }
319
320 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
Michal Vasko5233e962020-08-14 14:26:20 +0200321 ly_print_(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200322 } else {
323 ypr_text(ctx, ext_substmt_info[substmt].name, text,
Radek Krejci0f969882020-08-21 16:56:47 +0200324 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200325 }
326
327 LEVEL++;
328 LY_ARRAY_FOR(ext, u) {
Michal Vasko69730152020-10-09 16:30:07 +0200329 if ((((struct lysp_ext_instance *)ext)[u].insubstmt != substmt) || (((struct lysp_ext_instance *)ext)[u].insubstmt_index != substmt_index)) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200330 continue;
331 }
Radek Krejci693262f2019-04-29 15:23:20 +0200332 if (ctx->schema == YPR_PARSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200333 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200334 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200335 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200336 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200337 }
338 LEVEL--;
339 ypr_close(ctx, extflag);
340}
341
342static void
Radek Krejci857189e2020-09-01 13:26:36 +0200343ypr_unsigned(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, unsigned long int attr_value, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200344{
345 char *str;
346
Radek Krejci1deb5be2020-08-26 16:43:36 +0200347 if (asprintf(&str, "%lu", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200348 LOGMEM(ctx->module->ctx);
349 return;
350 }
351 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200352 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200353 free(str);
354}
355
356static void
Radek Krejci857189e2020-09-01 13:26:36 +0200357ypr_signed(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, signed long int attr_value, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200358{
359 char *str;
360
Radek Krejci1deb5be2020-08-26 16:43:36 +0200361 if (asprintf(&str, "%ld", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200362 LOGMEM(ctx->module->ctx);
363 return;
364 }
365 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200366 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200367 free(str);
368}
369
370static void
Radek Krejci693262f2019-04-29 15:23:20 +0200371yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200372{
373 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +0200374 ly_print_(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200375 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200376 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
377 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
378 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200379 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200380 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200381 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200382 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200383 }
384}
385
386static void
Radek Krejci857189e2020-09-01 13:26:36 +0200387ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200388{
389 if (flags & LYS_MAND_MASK) {
390 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200391 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200392 }
393}
394
395static void
Radek Krejci857189e2020-09-01 13:26:36 +0200396ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200397{
398 if (flags & LYS_CONFIG_MASK) {
399 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200400 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200401 }
402}
403
404static void
Radek Krejci857189e2020-09-01 13:26:36 +0200405ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200406{
407 const char *status = NULL;
408
409 if (flags & LYS_STATUS_CURR) {
410 ypr_open(ctx->out, flag);
411 status = "current";
412 } else if (flags & LYS_STATUS_DEPRC) {
413 ypr_open(ctx->out, flag);
414 status = "deprecated";
415 } else if (flags & LYS_STATUS_OBSLT) {
416 ypr_open(ctx->out, flag);
417 status = "obsolete";
418 }
Radek Krejci693262f2019-04-29 15:23:20 +0200419
420 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200421}
422
423static void
Radek Krejci857189e2020-09-01 13:26:36 +0200424ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200425{
426 if (dsc) {
427 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200428 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200429 }
430}
431
432static void
Radek Krejci857189e2020-09-01 13:26:36 +0200433ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200434{
435 if (ref) {
436 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200437 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200438 }
439}
440
441static void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200442yprp_iffeatures(struct ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200443{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200444 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +0200445 ly_bool extflag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200446
Michal Vasko7f45cf22020-10-01 12:49:44 +0200447 LY_ARRAY_FOR(iffs, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200448 ypr_open(ctx->out, flag);
449 extflag = 0;
450
Michal Vasko7f45cf22020-10-01 12:49:44 +0200451 ly_print_(ctx->out, "%*sif-feature \"%s\"", INDENT, iffs[u].str);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200452
453 /* extensions */
454 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200455 LY_ARRAY_FOR(exts, v) {
456 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200457 }
458 LEVEL--;
459 ypr_close(ctx, extflag);
460 }
461}
462
463static void
Radek Krejci693262f2019-04-29 15:23:20 +0200464yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200465{
Radek Krejci857189e2020-09-01 13:26:36 +0200466 ly_bool flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200467 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200468
Michal Vasko5233e962020-08-14 14:26:20 +0200469 ly_print_(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200470 LEVEL++;
471
472 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200473 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200474 }
475
476 if (ext->argument) {
477 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200478 ly_print_(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200479 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200480 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200481 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200482 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 +0200483 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200484 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200485 }
486 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vasko69730152020-10-09 16:30:07 +0200487 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts)))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200488 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200489 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200490 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200491 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200492 ypr_close(ctx, flag2);
493 }
494
Radek Krejci693262f2019-04-29 15:23:20 +0200495 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200496 ypr_description(ctx, ext->dsc, ext->exts, &flag);
497 ypr_reference(ctx, ext->ref, ext->exts, &flag);
498
499 LEVEL--;
500 ypr_close(ctx, flag);
501}
502
503static void
Radek Krejci693262f2019-04-29 15:23:20 +0200504yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200505{
Radek Krejci857189e2020-09-01 13:26:36 +0200506 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200507
Michal Vasko5233e962020-08-14 14:26:20 +0200508 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200509 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200510 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
511 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
512 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200513 ypr_description(ctx, feat->dsc, feat->exts, &flag);
514 ypr_reference(ctx, feat->ref, feat->exts, &flag);
515 LEVEL--;
516 ypr_close(ctx, flag);
517}
518
519static void
Radek Krejci693262f2019-04-29 15:23:20 +0200520yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200521{
Radek Krejci857189e2020-09-01 13:26:36 +0200522 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200523 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200524
Michal Vasko5233e962020-08-14 14:26:20 +0200525 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200526 LEVEL++;
527
Radek Krejci693262f2019-04-29 15:23:20 +0200528 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
529 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200530
531 LY_ARRAY_FOR(ident->bases, u) {
532 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200533 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200534 }
535
Radek Krejci693262f2019-04-29 15:23:20 +0200536 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200537 ypr_description(ctx, ident->dsc, ident->exts, &flag);
538 ypr_reference(ctx, ident->ref, ident->exts, &flag);
539
540 LEVEL--;
541 ypr_close(ctx, flag);
542}
543
544static void
Radek Krejci693262f2019-04-29 15:23:20 +0200545yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
546{
Radek Krejci857189e2020-09-01 13:26:36 +0200547 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200548 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200549
Michal Vasko5233e962020-08-14 14:26:20 +0200550 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200551 LEVEL++;
552
553 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200554
555 LY_ARRAY_FOR(ident->derived, u) {
556 ypr_open(ctx->out, &flag);
557 if (ctx->module != ident->derived[u]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200558 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 +0200559 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200560 ly_print_(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200561 }
562 }
563
564 ypr_status(ctx, ident->flags, ident->exts, &flag);
565 ypr_description(ctx, ident->dsc, ident->exts, &flag);
566 ypr_reference(ctx, ident->ref, ident->exts, &flag);
567
568 LEVEL--;
569 ypr_close(ctx, flag);
570}
571
572static void
Radek Krejci857189e2020-09-01 13:26:36 +0200573yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200574{
Radek Krejci857189e2020-09-01 13:26:36 +0200575 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200576
577 if (!restr) {
578 return;
579 }
580
581 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200582 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200583 ypr_encode(ctx->out, (restr->arg.str[0] != 0x15 && restr->arg.str[0] != 0x06) ? restr->arg.str : &restr->arg.str[1], -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200584 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200585
586 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200587 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200588 if (restr->arg.str[0] == 0x15) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200589 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
590 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200591 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200592 }
593 if (restr->emsg) {
594 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200595 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200596 }
597 if (restr->eapptag) {
598 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200599 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200600 }
Radek Krejci693262f2019-04-29 15:23:20 +0200601 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
602 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
603
Radek Krejcid3ca0632019-04-16 16:54:54 +0200604 LEVEL--;
605 ypr_close(ctx, inner_flag);
606}
607
608static void
Radek Krejci857189e2020-09-01 13:26:36 +0200609yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200610{
Radek Krejci857189e2020-09-01 13:26:36 +0200611 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200612
613 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200614 ly_print_(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200615 ypr_encode(ctx->out, must->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200616 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200617
618 LEVEL++;
619 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
620 if (must->emsg) {
621 ypr_open(ctx->out, &inner_flag);
622 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
623 }
624 if (must->eapptag) {
625 ypr_open(ctx->out, &inner_flag);
626 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
627 }
628 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
629 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
630
631 LEVEL--;
632 ypr_close(ctx, inner_flag);
633}
634
635static void
Radek Krejci857189e2020-09-01 13:26:36 +0200636yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200637{
Radek Krejci857189e2020-09-01 13:26:36 +0200638 ly_bool inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200639 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200640
Radek Krejci334ccc72019-06-12 13:49:29 +0200641 if (!range) {
642 return;
643 }
644
Radek Krejci693262f2019-04-29 15:23:20 +0200645 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200646 ly_print_(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200647 LY_ARRAY_FOR(range->parts, u) {
648 if (u > 0) {
Michal Vasko5233e962020-08-14 14:26:20 +0200649 ly_print_(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200650 }
651 if (range->parts[u].max_64 == range->parts[u].min_64) {
652 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200653 ly_print_(ctx->out, "%" PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200654 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200655 ly_print_(ctx->out, "%" PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200656 }
657 } else {
658 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200659 ly_print_(ctx->out, "%" PRIu64 "..%" PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200660 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200661 ly_print_(ctx->out, "%" PRId64 "..%" PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200662 }
663 }
664 }
Michal Vasko5233e962020-08-14 14:26:20 +0200665 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200666
667 LEVEL++;
668 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
669 if (range->emsg) {
670 ypr_open(ctx->out, &inner_flag);
671 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
672 }
673 if (range->eapptag) {
674 ypr_open(ctx->out, &inner_flag);
675 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
676 }
677 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
678 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
679
680 LEVEL--;
681 ypr_close(ctx, inner_flag);
682}
683
684static void
Radek Krejci857189e2020-09-01 13:26:36 +0200685yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200686{
Radek Krejci857189e2020-09-01 13:26:36 +0200687 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200688
689 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200690 ly_print_(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200691 ypr_encode(ctx->out, pattern->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200692 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200693
694 LEVEL++;
695 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
696 if (pattern->inverted) {
697 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
698 ypr_open(ctx->out, &inner_flag);
699 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
700 }
701 if (pattern->emsg) {
702 ypr_open(ctx->out, &inner_flag);
703 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
704 }
705 if (pattern->eapptag) {
706 ypr_open(ctx->out, &inner_flag);
707 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
708 }
709 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
710 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
711
712 LEVEL--;
713 ypr_close(ctx, inner_flag);
714}
715
716static void
Radek Krejci857189e2020-09-01 13:26:36 +0200717yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200718{
Radek Krejci857189e2020-09-01 13:26:36 +0200719 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200720
721 if (!when) {
722 return;
723 }
724 ypr_open(ctx->out, flag);
725
Michal Vasko5233e962020-08-14 14:26:20 +0200726 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200727 ypr_encode(ctx->out, when->cond, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200728 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200729
730 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200731 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200732 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
733 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
734 LEVEL--;
735 ypr_close(ctx, inner_flag);
736}
737
738static void
Radek Krejci857189e2020-09-01 13:26:36 +0200739yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200740{
Radek Krejci857189e2020-09-01 13:26:36 +0200741 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200742
743 if (!when) {
744 return;
745 }
746 ypr_open(ctx->out, flag);
747
Michal Vasko5233e962020-08-14 14:26:20 +0200748 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200749 ypr_encode(ctx->out, when->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200750 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200751
752 LEVEL++;
753 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
754 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
755 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
756 LEVEL--;
757 ypr_close(ctx, inner_flag);
758}
759
760static void
Radek Krejci857189e2020-09-01 13:26:36 +0200761yprp_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 +0200762{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200763 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200764 ly_bool inner_flag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200765
766 LY_ARRAY_FOR(items, u) {
767 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200768 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200769 ly_print_(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200770 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200771 ly_print_(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200772 ypr_encode(ctx->out, items[u].name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200773 ly_print_(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200774 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200775 inner_flag = 0;
776 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200777 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
778 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200779 if (items[u].flags & LYS_SET_VALUE) {
780 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200781 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200782 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200783 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200784 }
785 }
Radek Krejci693262f2019-04-29 15:23:20 +0200786 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200787 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
788 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
789 LEVEL--;
790 ypr_close(ctx, inner_flag);
791 }
792}
793
794static void
Radek Krejci693262f2019-04-29 15:23:20 +0200795yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200796{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200797 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200798 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200799
Michal Vasko5233e962020-08-14 14:26:20 +0200800 ly_print_(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200801 LEVEL++;
802
Radek Krejci693262f2019-04-29 15:23:20 +0200803 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200804
Radek Krejci693262f2019-04-29 15:23:20 +0200805 yprp_restr(ctx, type->range, "range", &flag);
806 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200807 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200808 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200809 }
Radek Krejci693262f2019-04-29 15:23:20 +0200810 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
811 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200812
813 if (type->path) {
814 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200815 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200816 }
817 if (type->flags & LYS_SET_REQINST) {
818 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200819 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200820 }
821 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200822 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200823 }
824 LY_ARRAY_FOR(type->bases, u) {
825 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200826 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200827 }
828 LY_ARRAY_FOR(type->types, u) {
829 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200830 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200831 }
832
833 LEVEL--;
834 ypr_close(ctx, flag);
835}
836
837static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200838yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200839{
Radek Krejci857189e2020-09-01 13:26:36 +0200840 ly_bool dynamic;
Radek Krejcia1911222019-07-22 17:24:50 +0200841 const char *str;
842
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200843 str = value->realtype->plugin->print(value, LY_PREF_JSON, NULL, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200844 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
845 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200846 free((void *)str);
Radek Krejcia1911222019-07-22 17:24:50 +0200847 }
848}
849
850static void
Radek Krejci693262f2019-04-29 15:23:20 +0200851yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
852{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200853 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200854 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200855
Michal Vasko5233e962020-08-14 14:26:20 +0200856 ly_print_(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200857 LEVEL++;
858
859 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200860
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200861 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200862 case LY_TYPE_BINARY: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200863 struct lysc_type_bin *bin = (struct lysc_type_bin *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200864 yprc_range(ctx, bin->length, type->basetype, &flag);
865 break;
866 }
867 case LY_TYPE_UINT8:
868 case LY_TYPE_UINT16:
869 case LY_TYPE_UINT32:
870 case LY_TYPE_UINT64:
871 case LY_TYPE_INT8:
872 case LY_TYPE_INT16:
873 case LY_TYPE_INT32:
874 case LY_TYPE_INT64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200875 struct lysc_type_num *num = (struct lysc_type_num *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200876 yprc_range(ctx, num->range, type->basetype, &flag);
877 break;
878 }
879 case LY_TYPE_STRING: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200880 struct lysc_type_str *str = (struct lysc_type_str *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200881 yprc_range(ctx, str->length, type->basetype, &flag);
882 LY_ARRAY_FOR(str->patterns, u) {
883 yprc_pattern(ctx, str->patterns[u], &flag);
884 }
885 break;
886 }
887 case LY_TYPE_BITS:
888 case LY_TYPE_ENUM: {
889 /* bits and enums structures are compatible */
Michal Vasko22df3f02020-08-24 13:29:22 +0200890 struct lysc_type_bits *bits = (struct lysc_type_bits *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200891 LY_ARRAY_FOR(bits->bits, u) {
892 struct lysc_type_bitenum_item *item = &bits->bits[u];
Radek Krejci857189e2020-09-01 13:26:36 +0200893 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200894
895 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200896 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200897 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200898 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200899 LEVEL++;
900 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200901 if (type->basetype == LY_TYPE_BITS) {
902 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
903 } else { /* LY_TYPE_ENUM */
904 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
905 }
906 ypr_status(ctx, item->flags, item->exts, &inner_flag);
907 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
908 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
909 LEVEL--;
910 ypr_close(ctx, inner_flag);
911 }
912 break;
913 }
914 case LY_TYPE_BOOL:
915 case LY_TYPE_EMPTY:
916 /* nothing to do */
917 break;
918 case LY_TYPE_DEC64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200919 struct lysc_type_dec *dec = (struct lysc_type_dec *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200920 ypr_open(ctx->out, &flag);
921 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
922 yprc_range(ctx, dec->range, dec->basetype, &flag);
923 break;
924 }
925 case LY_TYPE_IDENT: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200926 struct lysc_type_identityref *ident = (struct lysc_type_identityref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200927 LY_ARRAY_FOR(ident->bases, u) {
928 ypr_open(ctx->out, &flag);
929 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
930 }
931 break;
932 }
933 case LY_TYPE_INST: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200934 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200935 ypr_open(ctx->out, &flag);
936 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
937 break;
938 }
939 case LY_TYPE_LEAFREF: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200940 struct lysc_type_leafref *lr = (struct lysc_type_leafref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200941 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200942 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200943 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
944 yprc_type(ctx, lr->realtype);
945 break;
946 }
947 case LY_TYPE_UNION: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200948 struct lysc_type_union *un = (struct lysc_type_union *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200949 LY_ARRAY_FOR(un->types, u) {
950 ypr_open(ctx->out, &flag);
951 yprc_type(ctx, un->types[u]);
952 }
953 break;
954 }
955 default:
956 LOGINT(ctx->module->ctx);
957 }
958
959 LEVEL--;
960 ypr_close(ctx, flag);
961}
962
963static void
964yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200965{
Michal Vasko5233e962020-08-14 14:26:20 +0200966 ly_print_(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200967 LEVEL++;
968
Radek Krejci693262f2019-04-29 15:23:20 +0200969 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200970
Radek Krejci693262f2019-04-29 15:23:20 +0200971 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200972
973 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +0200974 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200975 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200976 if (tpdf->dflt.str) {
977 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200978 }
979
Radek Krejci693262f2019-04-29 15:23:20 +0200980 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200981 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
982 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
983
984 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200985 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200986}
987
Radek Krejci693262f2019-04-29 15:23:20 +0200988static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
989static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
990static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200991
992static void
Radek Krejci693262f2019-04-29 15:23:20 +0200993yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200994{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200995 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200996 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200997 struct lysp_node *data;
998
Michal Vasko5233e962020-08-14 14:26:20 +0200999 ly_print_(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001000 LEVEL++;
1001
Radek Krejci693262f2019-04-29 15:23:20 +02001002 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1003 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001004 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1005 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001006
1007 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001008 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001009 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001010 }
1011
1012 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001013 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001014 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001015 }
1016
1017 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001018 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001019 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001020 }
1021
1022 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001023 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001024 }
1025
1026 LEVEL--;
1027 ypr_close(ctx, flag);
1028}
1029
1030static void
Radek Krejci857189e2020-09-01 13:26:36 +02001031yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001032{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001033 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001034 struct lysp_node *data;
1035
Michal Vasko7f45cf22020-10-01 12:49:44 +02001036 if (!inout->data) {
1037 /* no children */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001038 return;
1039 }
1040 ypr_open(ctx->out, flag);
1041
Michal Vasko5233e962020-08-14 14:26:20 +02001042 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001043 LEVEL++;
1044
Radek Krejci693262f2019-04-29 15:23:20 +02001045 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001046 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001047 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001048 }
1049 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001050 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001051 }
1052 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001053 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001054 }
1055
1056 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001057 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001058 }
1059
1060 LEVEL--;
1061 ypr_close(ctx, 1);
1062}
1063
1064static void
Radek Krejci857189e2020-09-01 13:26:36 +02001065yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001066{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001067 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001068 struct lysc_node *data;
1069
1070 if (!inout->data) {
1071 /* input/output is empty */
1072 return;
1073 }
1074 ypr_open(ctx->out, flag);
1075
Michal Vasko5233e962020-08-14 14:26:20 +02001076 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001077 LEVEL++;
1078
1079 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1080 LY_ARRAY_FOR(inout->musts, u) {
1081 yprc_must(ctx, &inout->musts[u], NULL);
1082 }
1083
Radek Krejci52f65552020-09-01 17:03:35 +02001084 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001085 LY_LIST_FOR(inout->data, data) {
1086 yprc_node(ctx, data);
1087 }
Radek Krejci693262f2019-04-29 15:23:20 +02001088 }
1089
1090 LEVEL--;
1091 ypr_close(ctx, 1);
1092}
1093
1094static void
1095yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001096{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001097 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001098 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001099 struct lysp_node *data;
1100
Michal Vasko5233e962020-08-14 14:26:20 +02001101 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001102
1103 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001104 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1105 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001106
1107 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001108 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001109 }
Radek Krejci693262f2019-04-29 15:23:20 +02001110 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001111 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1112 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1113
1114 LY_ARRAY_FOR(notif->typedefs, u) {
1115 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001116 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001117 }
1118
1119 LY_ARRAY_FOR(notif->groupings, u) {
1120 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001121 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122 }
1123
1124 LY_LIST_FOR(notif->data, data) {
1125 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001126 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001127 }
1128
1129 LEVEL--;
1130 ypr_close(ctx, flag);
1131}
1132
1133static void
Radek Krejci693262f2019-04-29 15:23:20 +02001134yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1135{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001136 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001137 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001138 struct lysc_node *data;
1139
Michal Vasko5233e962020-08-14 14:26:20 +02001140 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001141
1142 LEVEL++;
1143 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001144
1145 LY_ARRAY_FOR(notif->musts, u) {
1146 yprc_must(ctx, &notif->musts[u], &flag);
1147 }
1148 ypr_status(ctx, notif->flags, notif->exts, &flag);
1149 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1150 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1151
Radek Krejci52f65552020-09-01 17:03:35 +02001152 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001153 LY_LIST_FOR(notif->data, data) {
1154 ypr_open(ctx->out, &flag);
1155 yprc_node(ctx, data);
1156 }
Radek Krejci693262f2019-04-29 15:23:20 +02001157 }
1158
1159 LEVEL--;
1160 ypr_close(ctx, flag);
1161}
1162
1163static void
1164yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001165{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001166 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001167 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001168
Michal Vasko5233e962020-08-14 14:26:20 +02001169 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001170
1171 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001172 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1173 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1174 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001175 ypr_description(ctx, action->dsc, action->exts, &flag);
1176 ypr_reference(ctx, action->ref, action->exts, &flag);
1177
1178 LY_ARRAY_FOR(action->typedefs, u) {
1179 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001180 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001181 }
1182
1183 LY_ARRAY_FOR(action->groupings, u) {
1184 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001185 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001186 }
1187
Radek Krejci693262f2019-04-29 15:23:20 +02001188 yprp_inout(ctx, &action->input, &flag);
1189 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001190
1191 LEVEL--;
1192 ypr_close(ctx, flag);
1193}
1194
1195static void
Radek Krejci693262f2019-04-29 15:23:20 +02001196yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1197{
Radek Krejci857189e2020-09-01 13:26:36 +02001198 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001199
Michal Vasko5233e962020-08-14 14:26:20 +02001200 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001201
1202 LEVEL++;
1203 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001204 ypr_status(ctx, action->flags, action->exts, &flag);
1205 ypr_description(ctx, action->dsc, action->exts, &flag);
1206 ypr_reference(ctx, action->ref, action->exts, &flag);
1207
1208 yprc_inout(ctx, action, &action->input, &flag);
1209 yprc_inout(ctx, action, &action->output, &flag);
1210
1211 LEVEL--;
1212 ypr_close(ctx, flag);
1213}
1214
1215static void
Radek Krejci857189e2020-09-01 13:26:36 +02001216yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001217{
Michal Vasko5233e962020-08-14 14:26:20 +02001218 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001219 LEVEL++;
1220
Radek Krejci693262f2019-04-29 15:23:20 +02001221 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1222 yprp_when(ctx, node->when, flag);
1223 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001224}
1225
1226static void
Radek Krejci857189e2020-09-01 13:26:36 +02001227yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001228{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001229 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001230
Michal Vasko5233e962020-08-14 14:26:20 +02001231 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001232 LEVEL++;
1233
1234 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1235 LY_ARRAY_FOR(node->when, u) {
1236 yprc_when(ctx, node->when[u], flag);
1237 }
Radek Krejci693262f2019-04-29 15:23:20 +02001238}
1239
1240/* macr oto unify the code */
1241#define YPR_NODE_COMMON2 \
1242 ypr_config(ctx, node->flags, node->exts, flag); \
1243 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1244 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1245 } \
1246 ypr_status(ctx, node->flags, node->exts, flag); \
1247 ypr_description(ctx, node->dsc, node->exts, flag); \
1248 ypr_reference(ctx, node->ref, node->exts, flag)
1249
1250static void
Radek Krejci857189e2020-09-01 13:26:36 +02001251yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001252{
1253 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001254}
1255
1256static void
Radek Krejci857189e2020-09-01 13:26:36 +02001257yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001258{
1259 YPR_NODE_COMMON2;
1260}
1261
1262#undef YPR_NODE_COMMON2
1263
1264static void
1265yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001266{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001267 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001268 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001269 struct lysp_node *child;
1270 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1271
Radek Krejci693262f2019-04-29 15:23:20 +02001272 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001273
1274 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001275 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001276 }
1277 if (cont->presence) {
1278 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001279 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001280 }
1281
Radek Krejci693262f2019-04-29 15:23:20 +02001282 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001283
1284 LY_ARRAY_FOR(cont->typedefs, u) {
1285 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001286 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001287 }
1288
1289 LY_ARRAY_FOR(cont->groupings, u) {
1290 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001291 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001292 }
1293
1294 LY_LIST_FOR(cont->child, child) {
1295 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001296 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001297 }
1298
1299 LY_ARRAY_FOR(cont->actions, u) {
1300 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001301 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001302 }
1303
1304 LY_ARRAY_FOR(cont->notifs, u) {
1305 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001306 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001307 }
1308
1309 LEVEL--;
1310 ypr_close(ctx, flag);
1311}
1312
1313static void
Radek Krejci693262f2019-04-29 15:23:20 +02001314yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1315{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001316 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001317 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001318 struct lysc_node *child;
1319 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1320
1321 yprc_node_common1(ctx, node, &flag);
1322
1323 LY_ARRAY_FOR(cont->musts, u) {
1324 yprc_must(ctx, &cont->musts[u], &flag);
1325 }
1326 if (cont->flags & LYS_PRESENCE) {
1327 ypr_open(ctx->out, &flag);
1328 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1329 }
1330
1331 yprc_node_common2(ctx, node, &flag);
1332
Radek Krejci52f65552020-09-01 17:03:35 +02001333 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001334 LY_LIST_FOR(cont->child, child) {
1335 ypr_open(ctx->out, &flag);
1336 yprc_node(ctx, child);
1337 }
Radek Krejci693262f2019-04-29 15:23:20 +02001338
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001339 LY_ARRAY_FOR(cont->actions, u) {
1340 ypr_open(ctx->out, &flag);
1341 yprc_action(ctx, &cont->actions[u]);
1342 }
Radek Krejci693262f2019-04-29 15:23:20 +02001343
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001344 LY_ARRAY_FOR(cont->notifs, u) {
1345 ypr_open(ctx->out, &flag);
1346 yprc_notification(ctx, &cont->notifs[u]);
1347 }
Radek Krejci693262f2019-04-29 15:23:20 +02001348 }
1349
1350 LEVEL--;
1351 ypr_close(ctx, flag);
1352}
1353
1354static void
1355yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1356{
Radek Krejci857189e2020-09-01 13:26:36 +02001357 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001358 struct lysp_node *child;
1359 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1360
1361 yprp_node_common1(ctx, node, &flag);
1362 yprp_node_common2(ctx, node, &flag);
1363
1364 LY_LIST_FOR(cas->child, child) {
1365 ypr_open(ctx->out, &flag);
1366 yprp_node(ctx, child);
1367 }
1368
1369 LEVEL--;
1370 ypr_close(ctx, flag);
1371}
1372
1373static void
1374yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1375{
Radek Krejci857189e2020-09-01 13:26:36 +02001376 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001377 struct lysc_node *child;
1378
Michal Vasko22df3f02020-08-24 13:29:22 +02001379 yprc_node_common1(ctx, (struct lysc_node *)cs, &flag);
1380 yprc_node_common2(ctx, (struct lysc_node *)cs, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001381
Radek Krejci52f65552020-09-01 17:03:35 +02001382 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001383 for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001384 ypr_open(ctx->out, &flag);
1385 yprc_node(ctx, child);
1386 }
Radek Krejci693262f2019-04-29 15:23:20 +02001387 }
1388
1389 LEVEL--;
1390 ypr_close(ctx, flag);
1391}
1392
1393static void
1394yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001395{
Radek Krejci857189e2020-09-01 13:26:36 +02001396 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001397 struct lysp_node *child;
1398 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1399
Radek Krejci693262f2019-04-29 15:23:20 +02001400 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001401
Michal Vasko7f45cf22020-10-01 12:49:44 +02001402 if (choice->dflt.str) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001403 ypr_open(ctx->out, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001404 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt.str, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001405 }
1406
Radek Krejci693262f2019-04-29 15:23:20 +02001407 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001408
1409 LY_LIST_FOR(choice->child, child) {
1410 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001411 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001412 }
1413
1414 LEVEL--;
1415 ypr_close(ctx, flag);
1416}
1417
1418static void
Radek Krejci693262f2019-04-29 15:23:20 +02001419yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1420{
Radek Krejci857189e2020-09-01 13:26:36 +02001421 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001422 struct lysc_node_case *cs;
1423 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1424
1425 yprc_node_common1(ctx, node, &flag);
1426
1427 if (choice->dflt) {
1428 ypr_open(ctx->out, &flag);
1429 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1430 }
1431
1432 yprc_node_common2(ctx, node, &flag);
1433
Michal Vasko22df3f02020-08-24 13:29:22 +02001434 for (cs = choice->cases; cs; cs = (struct lysc_node_case *)cs->next) {
Radek Krejci693262f2019-04-29 15:23:20 +02001435 ypr_open(ctx->out, &flag);
1436 yprc_case(ctx, cs);
1437 }
1438
1439 LEVEL--;
1440 ypr_close(ctx, flag);
1441}
1442
1443static void
1444yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001445{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001446 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001447 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1448
Radek Krejci693262f2019-04-29 15:23:20 +02001449 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001450
Radek Krejci693262f2019-04-29 15:23:20 +02001451 yprp_type(ctx, &leaf->type);
1452 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001453 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001454 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001455 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02001456 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001457
Radek Krejci693262f2019-04-29 15:23:20 +02001458 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001459
1460 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001461 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001462}
1463
1464static void
Radek Krejci693262f2019-04-29 15:23:20 +02001465yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1466{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001467 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001468 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1469
1470 yprc_node_common1(ctx, node, NULL);
1471
1472 yprc_type(ctx, leaf->type);
1473 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1474 LY_ARRAY_FOR(leaf->musts, u) {
1475 yprc_must(ctx, &leaf->musts[u], NULL);
1476 }
Radek Krejcia1911222019-07-22 17:24:50 +02001477
1478 if (leaf->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001479 yprc_dflt_value(ctx, leaf->dflt, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001480 }
Radek Krejci693262f2019-04-29 15:23:20 +02001481
1482 yprc_node_common2(ctx, node, NULL);
1483
1484 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001485 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001486}
1487
1488static void
1489yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001490{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001491 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001492 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1493
Radek Krejci693262f2019-04-29 15:23:20 +02001494 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001495
Radek Krejci693262f2019-04-29 15:23:20 +02001496 yprp_type(ctx, &llist->type);
1497 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001498 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001499 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001500 }
1501 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001502 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001503 }
1504
Radek Krejci693262f2019-04-29 15:23:20 +02001505 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001506
1507 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001508 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001509 }
1510 if (llist->flags & LYS_SET_MAX) {
1511 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001512 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001513 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001514 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001515 }
1516 }
1517
1518 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001519 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001520 }
1521
Radek Krejci693262f2019-04-29 15:23:20 +02001522 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001523 ypr_description(ctx, node->dsc, node->exts, NULL);
1524 ypr_reference(ctx, node->ref, node->exts, NULL);
1525
1526 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001527 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001528}
1529
1530static void
Radek Krejci693262f2019-04-29 15:23:20 +02001531yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1532{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001533 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001534 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1535
1536 yprc_node_common1(ctx, node, NULL);
1537
1538 yprc_type(ctx, llist->type);
1539 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1540 LY_ARRAY_FOR(llist->musts, u) {
1541 yprc_must(ctx, &llist->musts[u], NULL);
1542 }
1543 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001544 yprc_dflt_value(ctx, llist->dflts[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001545 }
1546
1547 ypr_config(ctx, node->flags, node->exts, NULL);
1548
1549 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1550 if (llist->max) {
1551 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1552 } else {
1553 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1554 }
1555
1556 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1557
1558 ypr_status(ctx, node->flags, node->exts, NULL);
1559 ypr_description(ctx, node->dsc, node->exts, NULL);
1560 ypr_reference(ctx, node->ref, node->exts, NULL);
1561
1562 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001563 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001564}
1565
1566static void
1567yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001568{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001569 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001570 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001571 struct lysp_node *child;
1572 struct lysp_node_list *list = (struct lysp_node_list *)node;
1573
Radek Krejci693262f2019-04-29 15:23:20 +02001574 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001575
1576 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001577 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001578 }
1579 if (list->key) {
1580 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001581 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001582 }
1583 LY_ARRAY_FOR(list->uniques, u) {
1584 ypr_open(ctx->out, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001585 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u].str, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001586 }
1587
Michal Vaskoacb8e742020-10-20 10:28:02 +02001588 ypr_config(ctx, node->flags, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001589
1590 if (list->flags & LYS_SET_MIN) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001591 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001592 }
1593 if (list->flags & LYS_SET_MAX) {
1594 if (list->max) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001595 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001596 } else {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001597 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001598 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001599 }
1600 }
1601
1602 if (list->flags & LYS_ORDBY_MASK) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001603 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001604 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001605 }
1606
Michal Vaskoacb8e742020-10-20 10:28:02 +02001607 ypr_status(ctx, node->flags, node->exts, &flag);
1608 ypr_description(ctx, node->dsc, node->exts, &flag);
1609 ypr_reference(ctx, node->ref, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001610
1611 LY_ARRAY_FOR(list->typedefs, u) {
1612 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001613 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001614 }
1615
1616 LY_ARRAY_FOR(list->groupings, u) {
1617 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001618 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001619 }
1620
1621 LY_LIST_FOR(list->child, child) {
1622 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001623 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001624 }
1625
1626 LY_ARRAY_FOR(list->actions, u) {
1627 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001628 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001629 }
1630
1631 LY_ARRAY_FOR(list->notifs, u) {
1632 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001633 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001634 }
1635
1636 LEVEL--;
1637 ypr_close(ctx, flag);
1638}
1639
1640static void
Radek Krejci693262f2019-04-29 15:23:20 +02001641yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1642{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001643 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001644 struct lysc_node *child;
1645 struct lysc_node_list *list = (struct lysc_node_list *)node;
1646
Michal Vaskoacb8e742020-10-20 10:28:02 +02001647 yprc_node_common1(ctx, node, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001648
1649 LY_ARRAY_FOR(list->musts, u) {
1650 yprc_must(ctx, &list->musts[u], NULL);
1651 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001652 if (!(list->flags & LYS_KEYLESS)) {
Michal Vasko5233e962020-08-14 14:26:20 +02001653 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001654 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 +02001655 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001656 }
Michal Vasko5233e962020-08-14 14:26:20 +02001657 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001658 }
1659 LY_ARRAY_FOR(list->uniques, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001660 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001661 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001662 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001663 }
1664 ypr_close(ctx, 0);
1665 }
1666
1667 ypr_config(ctx, node->flags, node->exts, NULL);
1668
1669 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1670 if (list->max) {
1671 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1672 } else {
1673 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1674 }
1675
1676 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1677
1678 ypr_status(ctx, node->flags, node->exts, NULL);
1679 ypr_description(ctx, node->dsc, node->exts, NULL);
1680 ypr_reference(ctx, node->ref, node->exts, NULL);
1681
Radek Krejci52f65552020-09-01 17:03:35 +02001682 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001683 LY_LIST_FOR(list->child, child) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001684 yprc_node(ctx, child);
1685 }
Radek Krejci693262f2019-04-29 15:23:20 +02001686
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001687 LY_ARRAY_FOR(list->actions, u) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001688 yprc_action(ctx, &list->actions[u]);
1689 }
Radek Krejci693262f2019-04-29 15:23:20 +02001690
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001691 LY_ARRAY_FOR(list->notifs, u) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001692 yprc_notification(ctx, &list->notifs[u]);
1693 }
Radek Krejci693262f2019-04-29 15:23:20 +02001694 }
1695
1696 LEVEL--;
Michal Vaskoacb8e742020-10-20 10:28:02 +02001697 ypr_close(ctx, 1);
Radek Krejci693262f2019-04-29 15:23:20 +02001698}
1699
1700static void
1701yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001702{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001703 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001704 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001705
Michal Vasko5233e962020-08-14 14:26:20 +02001706 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001707 LEVEL++;
1708
Radek Krejci693262f2019-04-29 15:23:20 +02001709 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1710 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001711
1712 LY_ARRAY_FOR(refine->musts, u) {
1713 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001714 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001715 }
1716
1717 if (refine->presence) {
1718 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001719 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001720 }
1721
1722 LY_ARRAY_FOR(refine->dflts, u) {
1723 ypr_open(ctx->out, &flag);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001724 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001725 }
1726
Radek Krejci693262f2019-04-29 15:23:20 +02001727 ypr_config(ctx, refine->flags, refine->exts, &flag);
1728 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001729
1730 if (refine->flags & LYS_SET_MIN) {
1731 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001732 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001733 }
1734 if (refine->flags & LYS_SET_MAX) {
1735 ypr_open(ctx->out, &flag);
1736 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001737 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001738 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001739 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001740 }
1741 }
1742
1743 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1744 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1745
1746 LEVEL--;
1747 ypr_close(ctx, flag);
1748}
1749
1750static void
Radek Krejci693262f2019-04-29 15:23:20 +02001751yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001752{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001753 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001754 struct lysp_node *child;
1755
Michal Vasko5233e962020-08-14 14:26:20 +02001756 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001757 LEVEL++;
1758
Radek Krejci693262f2019-04-29 15:23:20 +02001759 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1760 yprp_when(ctx, aug->when, NULL);
1761 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1762 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001763 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1764 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1765
1766 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001767 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001768 }
1769
1770 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001771 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001772 }
1773
1774 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001775 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001776 }
1777
1778 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001779 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001780}
1781
Radek Krejcid3ca0632019-04-16 16:54:54 +02001782static void
Radek Krejci693262f2019-04-29 15:23:20 +02001783yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001784{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001785 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001786 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001787 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1788
Radek Krejci693262f2019-04-29 15:23:20 +02001789 yprp_node_common1(ctx, node, &flag);
1790 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001791
1792 LY_ARRAY_FOR(uses->refines, u) {
1793 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001794 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001795 }
1796
1797 LY_ARRAY_FOR(uses->augments, u) {
1798 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001799 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001800 }
1801
1802 LEVEL--;
1803 ypr_close(ctx, flag);
1804}
1805
1806static void
Radek Krejci693262f2019-04-29 15:23:20 +02001807yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001808{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001809 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001810 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001811 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1812
Radek Krejci693262f2019-04-29 15:23:20 +02001813 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001814
1815 LY_ARRAY_FOR(any->musts, u) {
1816 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001817 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001818 }
1819
Radek Krejci693262f2019-04-29 15:23:20 +02001820 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001821
1822 LEVEL--;
1823 ypr_close(ctx, flag);
1824}
1825
1826static void
Radek Krejci693262f2019-04-29 15:23:20 +02001827yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001828{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001829 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001830 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001831 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001832
Radek Krejci693262f2019-04-29 15:23:20 +02001833 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001834
Radek Krejci693262f2019-04-29 15:23:20 +02001835 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001836 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001837 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001838 }
1839
Radek Krejci693262f2019-04-29 15:23:20 +02001840 yprc_node_common2(ctx, node, &flag);
1841
Radek Krejcid3ca0632019-04-16 16:54:54 +02001842 LEVEL--;
1843 ypr_close(ctx, flag);
1844}
1845
1846static void
Radek Krejci693262f2019-04-29 15:23:20 +02001847yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001848{
1849 switch (node->nodetype) {
1850 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001851 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001852 break;
1853 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001854 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001855 break;
1856 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001857 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001858 break;
1859 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001860 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001861 break;
1862 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001863 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001864 break;
1865 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001866 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001867 break;
1868 case LYS_ANYXML:
1869 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001870 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001871 break;
1872 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001873 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001874 break;
1875 default:
1876 break;
1877 }
1878}
1879
1880static void
Radek Krejci693262f2019-04-29 15:23:20 +02001881yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
1882{
1883 switch (node->nodetype) {
1884 case LYS_CONTAINER:
1885 yprc_container(ctx, node);
1886 break;
1887 case LYS_CHOICE:
1888 yprc_choice(ctx, node);
1889 break;
1890 case LYS_LEAF:
1891 yprc_leaf(ctx, node);
1892 break;
1893 case LYS_LEAFLIST:
1894 yprc_leaflist(ctx, node);
1895 break;
1896 case LYS_LIST:
1897 yprc_list(ctx, node);
1898 break;
1899 case LYS_ANYXML:
1900 case LYS_ANYDATA:
1901 yprc_anydata(ctx, node);
1902 break;
1903 default:
1904 break;
1905 }
1906}
1907
1908static void
1909yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001910{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001911 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001912 struct lysp_deviate_add *add;
1913 struct lysp_deviate_rpl *rpl;
1914 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08001915 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001916
Michal Vasko5233e962020-08-14 14:26:20 +02001917 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001918 LEVEL++;
1919
Radek Krejci693262f2019-04-29 15:23:20 +02001920 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001921 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1922 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1923
fredgan2b11ddb2019-10-21 11:03:39 +08001924 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001925 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08001926 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1927 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001928 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001929 LEVEL++;
1930
fredgan2b11ddb2019-10-21 11:03:39 +08001931 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001932 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001933 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001934 continue;
1935 }
fredgan2b11ddb2019-10-21 11:03:39 +08001936 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001937 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001938 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001939 LEVEL++;
1940
Radek Krejci693262f2019-04-29 15:23:20 +02001941 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
1942 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001943 LY_ARRAY_FOR(add->musts, u) {
1944 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001945 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001946 LY_ARRAY_FOR(add->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001947 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001948 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001949 LY_ARRAY_FOR(add->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001950 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001951 }
Radek Krejci693262f2019-04-29 15:23:20 +02001952 ypr_config(ctx, add->flags, add->exts, NULL);
1953 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001954 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001955 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001956 }
1957 if (add->flags & LYS_SET_MAX) {
1958 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001959 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001960 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001961 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001962 }
1963 }
fredgan2b11ddb2019-10-21 11:03:39 +08001964 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001965 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001966 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001967 LEVEL++;
1968
Radek Krejci693262f2019-04-29 15:23:20 +02001969 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001970 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02001971 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001972 }
Radek Krejci693262f2019-04-29 15:23:20 +02001973 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001974 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001975 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
1976 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001977 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001978 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001979 }
1980 if (rpl->flags & LYS_SET_MAX) {
1981 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001982 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001983 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001984 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001985 }
1986 }
fredgan2b11ddb2019-10-21 11:03:39 +08001987 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001988 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001989 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001990 LEVEL++;
1991
Radek Krejci693262f2019-04-29 15:23:20 +02001992 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
1993 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001994 LY_ARRAY_FOR(del->musts, u) {
1995 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001996 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001997 LY_ARRAY_FOR(del->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001998 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001999 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002000 LY_ARRAY_FOR(del->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002001 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002002 }
2003 }
2004
2005 LEVEL--;
2006 ypr_close(ctx, 1);
2007 }
2008
2009 LEVEL--;
2010 ypr_close(ctx, 1);
2011}
2012
Michal Vasko7c8439f2020-08-05 13:25:19 +02002013static void
2014yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002015{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002016 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002017
Radek Krejcid3ca0632019-04-16 16:54:54 +02002018 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01002019 if (modp->imports[u].flags & LYS_INTERNAL) {
2020 continue;
2021 }
2022
Michal Vasko5233e962020-08-14 14:26:20 +02002023 ly_print_(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002024 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002025 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2026 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002027 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002028 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002029 }
Radek Krejci693262f2019-04-29 15:23:20 +02002030 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2031 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002032 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002033 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002034 }
2035 LY_ARRAY_FOR(modp->includes, u) {
2036 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 +02002037 ly_print_(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002038 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002039 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002040 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002041 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002042 }
Radek Krejci693262f2019-04-29 15:23:20 +02002043 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2044 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002046 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002047 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002048 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002049 }
2050 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002051}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002052
Michal Vasko7c8439f2020-08-05 13:25:19 +02002053static void
2054yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2055{
2056 LY_ARRAY_COUNT_TYPE u;
2057 struct lysp_node *data;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002058
Radek Krejcid3ca0632019-04-16 16:54:54 +02002059 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002060 ly_print_(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002061 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002062 }
2063 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002064 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002065 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002066 }
2067
2068 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002069 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002070 }
2071
2072 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002073 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002074 }
2075
2076 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002077 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002078 }
2079
2080 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002081 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002082 }
2083
2084 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002085 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002086 }
2087
2088 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002089 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002090 }
2091
2092 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002093 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094 }
2095
2096 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002097 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002098 }
2099
2100 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002101 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002102 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002103}
2104
2105LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002106yang_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 +02002107{
2108 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02002109 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002110
Michal Vasko5233e962020-08-14 14:26:20 +02002111 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002112 LEVEL++;
2113
2114 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002115 if (modp->version) {
2116 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 +02002117 }
2118
2119 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2120 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2121
2122 /* linkage-stmts (import/include) */
2123 yang_print_parsed_linkage(ctx, modp);
2124
2125 /* meta-stmts */
2126 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002127 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002128 }
2129 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2130 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2131 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2132 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2133
2134 /* revision-stmts */
2135 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002136 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002137 }
2138 LY_ARRAY_FOR(modp->revs, u) {
2139 yprp_revision(ctx, &modp->revs[u]);
2140 }
2141 /* body-stmts */
2142 yang_print_parsed_body(ctx, modp);
2143
2144 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002145 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002146 ly_print_flush(out);
2147
2148 return LY_SUCCESS;
2149}
2150
2151static void
2152yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2153{
Michal Vaskoc3781c32020-10-06 14:04:08 +02002154 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->mod->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002155 LEVEL++;
2156 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
2157 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2158 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002159 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002160}
2161
2162LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002163yang_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 +02002164{
2165 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02002166 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002167
Michal Vasko5233e962020-08-14 14:26:20 +02002168 ly_print_(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002169 LEVEL++;
2170
2171 /* submodule-header-stmts */
2172 if (submodp->version) {
2173 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
2174 }
2175
2176 yprp_belongsto(ctx, submodp);
2177
2178 /* linkage-stmts (import/include) */
2179 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2180
2181 /* meta-stmts */
2182 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002183 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002184 }
2185 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2186 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
2187 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2188 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
2189
2190 /* revision-stmts */
2191 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002192 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002193 }
2194 LY_ARRAY_FOR(submodp->revs, u) {
2195 yprp_revision(ctx, &submodp->revs[u]);
2196 }
2197 /* body-stmts */
2198 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002199
2200 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002201 ly_print_(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002202 ly_print_flush(out);
2203
2204 return LY_SUCCESS;
2205}
2206
2207LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002208yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002209{
Radek Krejci52f65552020-09-01 17:03:35 +02002210 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options}, *ctx = &ctx_;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002211
2212 yprc_node(ctx, node);
2213
2214 ly_print_flush(out);
2215 return LY_SUCCESS;
2216}
2217
2218LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002219yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002220{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002221 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002222 struct lysc_node *data;
2223 struct lysc_module *modc = module->compiled;
Radek Krejci52f65552020-09-01 17:03:35 +02002224 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002225
Michal Vasko5233e962020-08-14 14:26:20 +02002226 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002227 LEVEL++;
2228
Radek Krejci693262f2019-04-29 15:23:20 +02002229 /* module-header-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002230 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2231 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2232
Michal Vasko7c8439f2020-08-05 13:25:19 +02002233 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002234
2235 /* meta-stmts */
2236 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002237 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002238 }
2239 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2240 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2241 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2242 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2243
2244 /* revision-stmts */
2245 if (module->revision) {
Michal Vasko5233e962020-08-14 14:26:20 +02002246 ly_print_(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002247 }
2248
2249 /* body-stmts */
2250 if (modc->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002251 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002252 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2253 }
2254
Radek Krejci80d281e2020-09-14 17:42:54 +02002255 LY_ARRAY_FOR(module->identities, u) {
2256 yprc_identity(ctx, &module->identities[u]);
Radek Krejci693262f2019-04-29 15:23:20 +02002257 }
2258
Radek Krejci52f65552020-09-01 17:03:35 +02002259 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002260 LY_LIST_FOR(modc->data, data) {
2261 yprc_node(ctx, data);
2262 }
Radek Krejci693262f2019-04-29 15:23:20 +02002263
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002264 LY_ARRAY_FOR(modc->rpcs, u) {
2265 yprc_action(ctx, &modc->rpcs[u]);
2266 }
Radek Krejci693262f2019-04-29 15:23:20 +02002267
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002268 LY_ARRAY_FOR(modc->notifs, u) {
2269 yprc_notification(ctx, &modc->notifs[u]);
2270 }
Radek Krejci693262f2019-04-29 15:23:20 +02002271 }
2272
2273 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002274 ly_print_(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002275 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002276
2277 return LY_SUCCESS;
2278}