blob: fb806dea4d5e36746582a5c3e5782be2192b60bf [file] [log] [blame]
Radek Krejcid3ca0632019-04-16 16:54:54 +02001/**
2 * @file printer_yang.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief YANG printer
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcid3ca0632019-04-16 16:54:54 +020016
Radek Krejci693262f2019-04-29 15:23:20 +020017#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Radek Krejci693262f2019-04-29 15:23:20 +020022
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020024#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "plugins_types.h"
27#include "printer.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020028#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "tree_data.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020032#include "tree_schema.h"
33#include "tree_schema_internal.h"
Radek Krejci693262f2019-04-29 15:23:20 +020034#include "xpath.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020035
Radek Krejcie7b95092019-05-15 11:03:07 +020036/**
37 * @brief Types of the YANG printers
38 */
Radek Krejci693262f2019-04-29 15:23:20 +020039enum schema_type {
Radek Krejcie7b95092019-05-15 11:03:07 +020040 YPR_PARSED, /**< YANG printer of the parsed schema */
41 YPR_COMPILED /**< YANG printer of the compiled schema */
Radek Krejci693262f2019-04-29 15:23:20 +020042};
43
Radek Krejcie7b95092019-05-15 11:03:07 +020044/**
45 * @brief YANG printer context.
46 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020047struct ypr_ctx {
Radek Krejci241f6b52020-05-21 18:13:49 +020048 struct ly_out *out; /**< output specification */
Radek Krejcie7b95092019-05-15 11:03:07 +020049 unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
50 const struct lys_module *module; /**< schema to print */
51 enum schema_type schema; /**< type of the schema to print */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080052 int options; /**< Schema output options (see @ref schemaprinterflags). */
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 Krejci241f6b52020-05-21 18:13:49 +020068ypr_encode(struct ly_out *out, const char *text, int len)
Radek Krejcid3ca0632019-04-16 16:54:54 +020069{
70 int i, start_len;
71 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;
84 for (i = 0; i < len; ++i) {
85 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 Krejci241f6b52020-05-21 18:13:49 +0200125ypr_open(struct ly_out *out, int *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
134ypr_close(struct ypr_ctx *ctx, int flag)
135{
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
144ypr_text(struct ypr_ctx *ctx, const char *name, const char *text, int singleline, int closed)
145{
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,
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200222 struct lysp_ext_instance *ext, int *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 Krejcif56e2a42019-09-09 14:15:25 +0200226 int child_presence;
227 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--;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200238 if (ext->insubstmt != substmt || ext->insubstmt_index != substmt_index) {
239 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
247
248 ypr_open(ctx->out, flag);
249 argument = NULL;
250 if (ext[u].compiled) {
251 argument = ext[u].compiled->argument;
252 } else {
253 argument = ext[u].argument;
254 }
255 if (argument) {
Michal Vasko5233e962020-08-14 14:26:20 +0200256 ly_print_(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200257 ypr_encode(ctx->out, argument, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200258 ly_print_(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200259 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200260 ly_print_(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200261 }
262
263 child_presence = 0;
264 LEVEL++;
265 LY_LIST_FOR(ext[u].child, stmt) {
266 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
267 continue;
268 }
269 if (!child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200270 ly_print_(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200271 child_presence = 1;
272 }
273 yprp_stmt(ctx, stmt);
274 }
275 LEVEL--;
276 if (child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200277 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200278 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200279 ly_print_(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200280 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200281 }
282}
283
Radek Krejci693262f2019-04-29 15:23:20 +0200284/**
285 * @param[in] count Number of extensions to print, 0 to print them all.
286 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200287static void
Radek Krejci693262f2019-04-29 15:23:20 +0200288yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200289 struct lysc_ext_instance *ext, int *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200290{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200291 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200292
293 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200294 count = LY_ARRAY_COUNT(ext);
Radek Krejci693262f2019-04-29 15:23:20 +0200295 }
296 LY_ARRAY_FOR(ext, u) {
297 if (!count) {
298 break;
299 }
300 /* TODO compiled extensions */
301 (void) ctx;
302 (void) substmt;
303 (void) substmt_index;
304 (void) flag;
305
306 count--;
307 }
308}
309
310static void
311ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200312{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200313 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200314 int extflag = 0;
315
316 if (!text) {
317 /* nothing to print */
318 return;
319 }
320
321 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
Michal Vasko5233e962020-08-14 14:26:20 +0200322 ly_print_(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200323 } else {
324 ypr_text(ctx, ext_substmt_info[substmt].name, text,
325 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
326 }
327
328 LEVEL++;
329 LY_ARRAY_FOR(ext, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200330 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 +0200331 continue;
332 }
Radek Krejci693262f2019-04-29 15:23:20 +0200333 if (ctx->schema == YPR_PARSED) {
334 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance*)ext)[u], &extflag, 1);
335 } else {
336 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance*)ext)[u], &extflag, 1);
337 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200338 }
339 LEVEL--;
340 ypr_close(ctx, extflag);
341}
342
343static void
Radek Krejci693262f2019-04-29 15:23:20 +0200344ypr_unsigned(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, unsigned int attr_value, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200345{
346 char *str;
347
348 if (asprintf(&str, "%u", attr_value) == -1) {
349 LOGMEM(ctx->module->ctx);
350 return;
351 }
352 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200353 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200354 free(str);
355}
356
357static void
Radek Krejci693262f2019-04-29 15:23:20 +0200358ypr_signed(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, signed int attr_value, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200359{
360 char *str;
361
362 if (asprintf(&str, "%d", attr_value) == -1) {
363 LOGMEM(ctx->module->ctx);
364 return;
365 }
366 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200367 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200368 free(str);
369}
370
371static void
Radek Krejci693262f2019-04-29 15:23:20 +0200372yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200373{
374 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +0200375 ly_print_(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200376 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200377 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
378 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
379 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200380 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200381 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200382 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200383 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200384 }
385}
386
387static void
Radek Krejci693262f2019-04-29 15:23:20 +0200388ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200389{
390 if (flags & LYS_MAND_MASK) {
391 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200392 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200393 }
394}
395
396static void
Radek Krejci693262f2019-04-29 15:23:20 +0200397ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200398{
399 if (flags & LYS_CONFIG_MASK) {
400 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200401 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200402 }
403}
404
405static void
Radek Krejci693262f2019-04-29 15:23:20 +0200406ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200407{
408 const char *status = NULL;
409
410 if (flags & LYS_STATUS_CURR) {
411 ypr_open(ctx->out, flag);
412 status = "current";
413 } else if (flags & LYS_STATUS_DEPRC) {
414 ypr_open(ctx->out, flag);
415 status = "deprecated";
416 } else if (flags & LYS_STATUS_OBSLT) {
417 ypr_open(ctx->out, flag);
418 status = "obsolete";
419 }
Radek Krejci693262f2019-04-29 15:23:20 +0200420
421 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200422}
423
424static void
Radek Krejci693262f2019-04-29 15:23:20 +0200425ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200426{
427 if (dsc) {
428 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200429 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200430 }
431}
432
433static void
Radek Krejci693262f2019-04-29 15:23:20 +0200434ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200435{
436 if (ref) {
437 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200438 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200439 }
440}
441
442static void
Radek Krejci693262f2019-04-29 15:23:20 +0200443yprp_iffeatures(struct ypr_ctx *ctx, const char **iff, struct lysp_ext_instance *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200444{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200445 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200446 int extflag;
447
448 LY_ARRAY_FOR(iff, u) {
449 ypr_open(ctx->out, flag);
450 extflag = 0;
451
Michal Vasko5233e962020-08-14 14:26:20 +0200452 ly_print_(ctx->out, "%*sif-feature \"%s\"", INDENT, iff[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200453
454 /* extensions */
455 LEVEL++;
456 LY_ARRAY_FOR(exts, u) {
457 if (exts[u].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[u].insubstmt_index != u) {
458 continue;
459 }
Radek Krejci693262f2019-04-29 15:23:20 +0200460 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[u], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200461 }
462 LEVEL--;
463 ypr_close(ctx, extflag);
464 }
465}
466
467static void
Radek Krejci693262f2019-04-29 15:23:20 +0200468yprc_iffeature(struct ypr_ctx *ctx, struct lysc_iffeature *feat, int *index_e, int *index_f)
469{
470 int brackets_flag = *index_e;
471 uint8_t op;
472
473 op = lysc_iff_getop(feat->expr, *index_e);
474 (*index_e)++;
475
476 switch (op) {
477 case LYS_IFF_F:
478 if (ctx->module == feat->features[*index_f]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200479 ly_print_(ctx->out, "%s", feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200480 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200481 ly_print_(ctx->out, "%s:%s", feat->features[*index_f]->module->prefix, feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200482 }
483 (*index_f)++;
484 break;
485 case LYS_IFF_NOT:
Michal Vasko5233e962020-08-14 14:26:20 +0200486 ly_print_(ctx->out, "not ");
Radek Krejci693262f2019-04-29 15:23:20 +0200487 yprc_iffeature(ctx, feat, index_e, index_f);
488 break;
489 case LYS_IFF_AND:
490 if (brackets_flag) {
491 /* AND need brackets only if previous op was not */
492 if (*index_e < 2 || lysc_iff_getop(feat->expr, *index_e - 2) != LYS_IFF_NOT) {
493 brackets_flag = 0;
494 }
495 }
496 /* falls through */
497 case LYS_IFF_OR:
498 if (brackets_flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200499 ly_print_(ctx->out, "(");
Radek Krejci693262f2019-04-29 15:23:20 +0200500 }
501 yprc_iffeature(ctx, feat, index_e, index_f);
Michal Vasko5233e962020-08-14 14:26:20 +0200502 ly_print_(ctx->out, " %s ", op == LYS_IFF_OR ? "or" : "and");
Radek Krejci693262f2019-04-29 15:23:20 +0200503 yprc_iffeature(ctx, feat, index_e, index_f);
504 if (brackets_flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200505 ly_print_(ctx->out, ")");
Radek Krejci693262f2019-04-29 15:23:20 +0200506 }
507 }
508}
509
510static void
511yprc_iffeatures(struct ypr_ctx *ctx, struct lysc_iffeature *iff, struct lysc_ext_instance *exts, int *flag)
512{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200513 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +0200514 int extflag;
515
516 LY_ARRAY_FOR(iff, u) {
517 int index_e = 0, index_f = 0;
518
519 ypr_open(ctx->out, flag);
520 extflag = 0;
521
Michal Vasko5233e962020-08-14 14:26:20 +0200522 ly_print_(ctx->out, "%*sif-feature \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200523 yprc_iffeature(ctx, iff, &index_e, &index_f);
Michal Vasko5233e962020-08-14 14:26:20 +0200524 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200525
526 /* extensions */
527 LEVEL++;
Radek Krejci334ccc72019-06-12 13:49:29 +0200528 LY_ARRAY_FOR(exts, v) {
529 if (exts[v].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[v].insubstmt_index != u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200530 continue;
531 }
Radek Krejci334ccc72019-06-12 13:49:29 +0200532 yprc_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200533 }
534 LEVEL--;
535 ypr_close(ctx, extflag);
536 }
537}
538
539static void
540yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200541{
542 int flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200543 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200544
Michal Vasko5233e962020-08-14 14:26:20 +0200545 ly_print_(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200546 LEVEL++;
547
548 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200549 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200550 }
551
552 if (ext->argument) {
553 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200554 ly_print_(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200555 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200556 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200557 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200558 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 +0200559 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200560 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200561 }
562 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200563 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200564 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200565 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200566 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200567 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200568 ypr_close(ctx, flag2);
569 }
570
Radek Krejci693262f2019-04-29 15:23:20 +0200571 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200572 ypr_description(ctx, ext->dsc, ext->exts, &flag);
573 ypr_reference(ctx, ext->ref, ext->exts, &flag);
574
575 LEVEL--;
576 ypr_close(ctx, flag);
577}
578
579static void
Radek Krejci693262f2019-04-29 15:23:20 +0200580yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200581{
582 int flag = 0;
583
Michal Vasko5233e962020-08-14 14:26:20 +0200584 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200585 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200586 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
587 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
588 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200589 ypr_description(ctx, feat->dsc, feat->exts, &flag);
590 ypr_reference(ctx, feat->ref, feat->exts, &flag);
591 LEVEL--;
592 ypr_close(ctx, flag);
593}
594
595static void
Radek Krejci693262f2019-04-29 15:23:20 +0200596yprc_feature(struct ypr_ctx *ctx, const struct lysc_feature *feat)
597{
598 int flag = 0;
599
Michal Vasko5233e962020-08-14 14:26:20 +0200600 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200601 LEVEL++;
602 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
603 yprc_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
604 ypr_status(ctx, feat->flags, feat->exts, &flag);
605 ypr_description(ctx, feat->dsc, feat->exts, &flag);
606 ypr_reference(ctx, feat->ref, feat->exts, &flag);
607 LEVEL--;
608 ypr_close(ctx, flag);
609}
610
611static void
612yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200613{
614 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200615 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200616
Michal Vasko5233e962020-08-14 14:26:20 +0200617 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200618 LEVEL++;
619
Radek Krejci693262f2019-04-29 15:23:20 +0200620 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
621 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200622
623 LY_ARRAY_FOR(ident->bases, u) {
624 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200625 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200626 }
627
Radek Krejci693262f2019-04-29 15:23:20 +0200628 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200629 ypr_description(ctx, ident->dsc, ident->exts, &flag);
630 ypr_reference(ctx, ident->ref, ident->exts, &flag);
631
632 LEVEL--;
633 ypr_close(ctx, flag);
634}
635
636static void
Radek Krejci693262f2019-04-29 15:23:20 +0200637yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
638{
639 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200640 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200641
Michal Vasko5233e962020-08-14 14:26:20 +0200642 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200643 LEVEL++;
644
645 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
646 yprc_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
647
648 LY_ARRAY_FOR(ident->derived, u) {
649 ypr_open(ctx->out, &flag);
650 if (ctx->module != ident->derived[u]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200651 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 +0200652 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200653 ly_print_(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200654 }
655 }
656
657 ypr_status(ctx, ident->flags, ident->exts, &flag);
658 ypr_description(ctx, ident->dsc, ident->exts, &flag);
659 ypr_reference(ctx, ident->ref, ident->exts, &flag);
660
661 LEVEL--;
662 ypr_close(ctx, flag);
663}
664
665static void
666yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200667{
668 int inner_flag = 0;
669
670 if (!restr) {
671 return;
672 }
673
674 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200675 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200676 ypr_encode(ctx->out, (restr->arg[0] != 0x15 && restr->arg[0] != 0x06) ? restr->arg : &restr->arg[1], -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200677 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200678
679 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200680 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200681 if (restr->arg[0] == 0x15) {
682 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
683 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200684 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200685 }
686 if (restr->emsg) {
687 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200688 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200689 }
690 if (restr->eapptag) {
691 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200692 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200693 }
Radek Krejci693262f2019-04-29 15:23:20 +0200694 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
695 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
696
Radek Krejcid3ca0632019-04-16 16:54:54 +0200697 LEVEL--;
698 ypr_close(ctx, inner_flag);
699}
700
701static void
Radek Krejci693262f2019-04-29 15:23:20 +0200702yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, int *flag)
703{
704 int inner_flag = 0;
705
706 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200707 ly_print_(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200708 ypr_encode(ctx->out, must->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200709 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200710
711 LEVEL++;
712 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
713 if (must->emsg) {
714 ypr_open(ctx->out, &inner_flag);
715 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
716 }
717 if (must->eapptag) {
718 ypr_open(ctx->out, &inner_flag);
719 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
720 }
721 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
722 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
723
724 LEVEL--;
725 ypr_close(ctx, inner_flag);
726}
727
728static void
729yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, int *flag)
730{
731 int inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200732 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200733
Radek Krejci334ccc72019-06-12 13:49:29 +0200734 if (!range) {
735 return;
736 }
737
Radek Krejci693262f2019-04-29 15:23:20 +0200738 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200739 ly_print_(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200740 LY_ARRAY_FOR(range->parts, u) {
741 if (u > 0) {
Michal Vasko5233e962020-08-14 14:26:20 +0200742 ly_print_(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200743 }
744 if (range->parts[u].max_64 == range->parts[u].min_64) {
745 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Michal Vasko5233e962020-08-14 14:26:20 +0200746 ly_print_(ctx->out, "%"PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200747 } else { /* signed values */
Michal Vasko5233e962020-08-14 14:26:20 +0200748 ly_print_(ctx->out, "%"PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200749 }
750 } else {
751 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Michal Vasko5233e962020-08-14 14:26:20 +0200752 ly_print_(ctx->out, "%"PRIu64"..%"PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200753 } else { /* signed values */
Michal Vasko5233e962020-08-14 14:26:20 +0200754 ly_print_(ctx->out, "%"PRId64"..%"PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200755 }
756 }
757 }
Michal Vasko5233e962020-08-14 14:26:20 +0200758 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200759
760 LEVEL++;
761 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
762 if (range->emsg) {
763 ypr_open(ctx->out, &inner_flag);
764 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
765 }
766 if (range->eapptag) {
767 ypr_open(ctx->out, &inner_flag);
768 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
769 }
770 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
771 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
772
773 LEVEL--;
774 ypr_close(ctx, inner_flag);
775}
776
777static void
778yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, int *flag)
779{
780 int inner_flag = 0;
781
782 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200783 ly_print_(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200784 ypr_encode(ctx->out, pattern->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200785 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200786
787 LEVEL++;
788 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
789 if (pattern->inverted) {
790 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
791 ypr_open(ctx->out, &inner_flag);
792 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
793 }
794 if (pattern->emsg) {
795 ypr_open(ctx->out, &inner_flag);
796 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
797 }
798 if (pattern->eapptag) {
799 ypr_open(ctx->out, &inner_flag);
800 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
801 }
802 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
803 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
804
805 LEVEL--;
806 ypr_close(ctx, inner_flag);
807}
808
809static void
810yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200811{
812 int inner_flag = 0;
813
814 if (!when) {
815 return;
816 }
817 ypr_open(ctx->out, flag);
818
Michal Vasko5233e962020-08-14 14:26:20 +0200819 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200820 ypr_encode(ctx->out, when->cond, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200821 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200822
823 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200824 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200825 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
826 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
827 LEVEL--;
828 ypr_close(ctx, inner_flag);
829}
830
831static void
Radek Krejci693262f2019-04-29 15:23:20 +0200832yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, int *flag)
833{
834 int inner_flag = 0;
835
836 if (!when) {
837 return;
838 }
839 ypr_open(ctx->out, flag);
840
Michal Vasko5233e962020-08-14 14:26:20 +0200841 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200842 ypr_encode(ctx->out, when->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200843 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200844
845 LEVEL++;
846 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
847 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
848 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
849 LEVEL--;
850 ypr_close(ctx, inner_flag);
851}
852
853static void
854yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200855{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200856 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200857 int inner_flag;
858
859 LY_ARRAY_FOR(items, u) {
860 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200861 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200862 ly_print_(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200863 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200864 ly_print_(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200865 ypr_encode(ctx->out, items[u].name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200866 ly_print_(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200867 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200868 inner_flag = 0;
869 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200870 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
871 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200872 if (items[u].flags & LYS_SET_VALUE) {
873 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200874 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200875 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200876 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200877 }
878 }
Radek Krejci693262f2019-04-29 15:23:20 +0200879 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200880 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
881 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
882 LEVEL--;
883 ypr_close(ctx, inner_flag);
884 }
885}
886
887static void
Radek Krejci693262f2019-04-29 15:23:20 +0200888yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200889{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200890 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200891 int flag = 0;
892
Michal Vasko5233e962020-08-14 14:26:20 +0200893 ly_print_(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200894 LEVEL++;
895
Radek Krejci693262f2019-04-29 15:23:20 +0200896 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200897
Radek Krejci693262f2019-04-29 15:23:20 +0200898 yprp_restr(ctx, type->range, "range", &flag);
899 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200900 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200901 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200902 }
Radek Krejci693262f2019-04-29 15:23:20 +0200903 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
904 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200905
906 if (type->path) {
907 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200908 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200909 }
910 if (type->flags & LYS_SET_REQINST) {
911 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200912 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200913 }
914 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200915 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200916 }
917 LY_ARRAY_FOR(type->bases, u) {
918 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200919 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200920 }
921 LY_ARRAY_FOR(type->types, u) {
922 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200923 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200924 }
925
926 LEVEL--;
927 ypr_close(ctx, flag);
928}
929
930static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200931yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200932{
933 int dynamic;
934 const char *str;
935
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200936 str = value->realtype->plugin->print(value, LY_PREF_JSON, NULL, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200937 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
938 if (dynamic) {
939 free((void*)str);
940 }
941}
942
943static void
Radek Krejci693262f2019-04-29 15:23:20 +0200944yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
945{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200946 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200947 int flag = 0;
948
Michal Vasko5233e962020-08-14 14:26:20 +0200949 ly_print_(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200950 LEVEL++;
951
952 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200953
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200954 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200955 case LY_TYPE_BINARY: {
956 struct lysc_type_bin *bin = (struct lysc_type_bin*)type;
957 yprc_range(ctx, bin->length, type->basetype, &flag);
958 break;
959 }
960 case LY_TYPE_UINT8:
961 case LY_TYPE_UINT16:
962 case LY_TYPE_UINT32:
963 case LY_TYPE_UINT64:
964 case LY_TYPE_INT8:
965 case LY_TYPE_INT16:
966 case LY_TYPE_INT32:
967 case LY_TYPE_INT64: {
968 struct lysc_type_num *num = (struct lysc_type_num*)type;
969 yprc_range(ctx, num->range, type->basetype, &flag);
970 break;
971 }
972 case LY_TYPE_STRING: {
973 struct lysc_type_str *str = (struct lysc_type_str*)type;
974 yprc_range(ctx, str->length, type->basetype, &flag);
975 LY_ARRAY_FOR(str->patterns, u) {
976 yprc_pattern(ctx, str->patterns[u], &flag);
977 }
978 break;
979 }
980 case LY_TYPE_BITS:
981 case LY_TYPE_ENUM: {
982 /* bits and enums structures are compatible */
983 struct lysc_type_bits *bits = (struct lysc_type_bits*)type;
984 LY_ARRAY_FOR(bits->bits, u) {
985 struct lysc_type_bitenum_item *item = &bits->bits[u];
986 int inner_flag = 0;
987
988 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200989 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200990 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200991 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200992 LEVEL++;
993 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
994 yprc_iffeatures(ctx, item->iffeatures, item->exts, &inner_flag);
995 if (type->basetype == LY_TYPE_BITS) {
996 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
997 } else { /* LY_TYPE_ENUM */
998 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
999 }
1000 ypr_status(ctx, item->flags, item->exts, &inner_flag);
1001 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
1002 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
1003 LEVEL--;
1004 ypr_close(ctx, inner_flag);
1005 }
1006 break;
1007 }
1008 case LY_TYPE_BOOL:
1009 case LY_TYPE_EMPTY:
1010 /* nothing to do */
1011 break;
1012 case LY_TYPE_DEC64: {
1013 struct lysc_type_dec *dec = (struct lysc_type_dec*)type;
1014 ypr_open(ctx->out, &flag);
1015 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
1016 yprc_range(ctx, dec->range, dec->basetype, &flag);
1017 break;
1018 }
1019 case LY_TYPE_IDENT: {
1020 struct lysc_type_identityref *ident = (struct lysc_type_identityref*)type;
1021 LY_ARRAY_FOR(ident->bases, u) {
1022 ypr_open(ctx->out, &flag);
1023 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
1024 }
1025 break;
1026 }
1027 case LY_TYPE_INST: {
1028 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid*)type;
1029 ypr_open(ctx->out, &flag);
1030 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
1031 break;
1032 }
1033 case LY_TYPE_LEAFREF: {
1034 struct lysc_type_leafref *lr = (struct lysc_type_leafref*)type;
1035 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +02001036 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001037 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
1038 yprc_type(ctx, lr->realtype);
1039 break;
1040 }
1041 case LY_TYPE_UNION: {
1042 struct lysc_type_union *un = (struct lysc_type_union*)type;
1043 LY_ARRAY_FOR(un->types, u) {
1044 ypr_open(ctx->out, &flag);
1045 yprc_type(ctx, un->types[u]);
1046 }
1047 break;
1048 }
1049 default:
1050 LOGINT(ctx->module->ctx);
1051 }
1052
1053 LEVEL--;
1054 ypr_close(ctx, flag);
1055}
1056
1057static void
1058yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001059{
Michal Vasko5233e962020-08-14 14:26:20 +02001060 ly_print_(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001061 LEVEL++;
1062
Radek Krejci693262f2019-04-29 15:23:20 +02001063 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001064
Radek Krejci693262f2019-04-29 15:23:20 +02001065 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001066
1067 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +02001068 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001069 }
1070 if (tpdf->dflt) {
Radek Krejci693262f2019-04-29 15:23:20 +02001071 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001072 }
1073
Radek Krejci693262f2019-04-29 15:23:20 +02001074 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001075 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
1076 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
1077
1078 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001079 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001080}
1081
Radek Krejci693262f2019-04-29 15:23:20 +02001082static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
1083static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
1084static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001085
1086static void
Radek Krejci693262f2019-04-29 15:23:20 +02001087yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001088{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001089 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001090 int flag = 0;
1091 struct lysp_node *data;
1092
Michal Vasko5233e962020-08-14 14:26:20 +02001093 ly_print_(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001094 LEVEL++;
1095
Radek Krejci693262f2019-04-29 15:23:20 +02001096 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1097 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001098 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1099 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001100
1101 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001102 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001103 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001104 }
1105
1106 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001107 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001108 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001109 }
1110
1111 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001112 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001113 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001114 }
1115
1116 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001117 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001118 }
1119
1120 LEVEL--;
1121 ypr_close(ctx, flag);
1122}
1123
1124static void
Radek Krejci693262f2019-04-29 15:23:20 +02001125yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001126{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001127 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001128 struct lysp_node *data;
1129
1130 if (!inout->nodetype) {
1131 /* nodetype not set -> input/output is empty */
1132 return;
1133 }
1134 ypr_open(ctx->out, flag);
1135
Michal Vasko5233e962020-08-14 14:26:20 +02001136 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001137 LEVEL++;
1138
Radek Krejci693262f2019-04-29 15:23:20 +02001139 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001140 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001141 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001142 }
1143 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001144 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001145 }
1146 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001147 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001148 }
1149
1150 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001151 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001152 }
1153
1154 LEVEL--;
1155 ypr_close(ctx, 1);
1156}
1157
1158static void
Radek Krejci693262f2019-04-29 15:23:20 +02001159yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, int *flag)
1160{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001161 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001162 struct lysc_node *data;
1163
1164 if (!inout->data) {
1165 /* input/output is empty */
1166 return;
1167 }
1168 ypr_open(ctx->out, flag);
1169
Michal Vasko5233e962020-08-14 14:26:20 +02001170 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001171 LEVEL++;
1172
1173 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1174 LY_ARRAY_FOR(inout->musts, u) {
1175 yprc_must(ctx, &inout->musts[u], NULL);
1176 }
1177
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001178 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001179 LY_LIST_FOR(inout->data, data) {
1180 yprc_node(ctx, data);
1181 }
Radek Krejci693262f2019-04-29 15:23:20 +02001182 }
1183
1184 LEVEL--;
1185 ypr_close(ctx, 1);
1186}
1187
1188static void
1189yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001190{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001191 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001192 int flag = 0;
1193 struct lysp_node *data;
1194
Michal Vasko5233e962020-08-14 14:26:20 +02001195 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001196
1197 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001198 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1199 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001200
1201 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001202 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001203 }
Radek Krejci693262f2019-04-29 15:23:20 +02001204 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001205 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1206 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1207
1208 LY_ARRAY_FOR(notif->typedefs, u) {
1209 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001210 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001211 }
1212
1213 LY_ARRAY_FOR(notif->groupings, u) {
1214 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001215 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001216 }
1217
1218 LY_LIST_FOR(notif->data, data) {
1219 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001220 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001221 }
1222
1223 LEVEL--;
1224 ypr_close(ctx, flag);
1225}
1226
1227static void
Radek Krejci693262f2019-04-29 15:23:20 +02001228yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1229{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001230 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001231 int flag = 0;
1232 struct lysc_node *data;
1233
Michal Vasko5233e962020-08-14 14:26:20 +02001234 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001235
1236 LEVEL++;
1237 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1238 yprc_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
1239
1240 LY_ARRAY_FOR(notif->musts, u) {
1241 yprc_must(ctx, &notif->musts[u], &flag);
1242 }
1243 ypr_status(ctx, notif->flags, notif->exts, &flag);
1244 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1245 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1246
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001247 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001248 LY_LIST_FOR(notif->data, data) {
1249 ypr_open(ctx->out, &flag);
1250 yprc_node(ctx, data);
1251 }
Radek Krejci693262f2019-04-29 15:23:20 +02001252 }
1253
1254 LEVEL--;
1255 ypr_close(ctx, flag);
1256}
1257
1258static void
1259yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001260{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001261 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001262 int flag = 0;
1263
Michal Vasko5233e962020-08-14 14:26:20 +02001264 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001265
1266 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001267 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1268 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1269 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001270 ypr_description(ctx, action->dsc, action->exts, &flag);
1271 ypr_reference(ctx, action->ref, action->exts, &flag);
1272
1273 LY_ARRAY_FOR(action->typedefs, u) {
1274 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001275 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001276 }
1277
1278 LY_ARRAY_FOR(action->groupings, u) {
1279 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001280 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001281 }
1282
Radek Krejci693262f2019-04-29 15:23:20 +02001283 yprp_inout(ctx, &action->input, &flag);
1284 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001285
1286 LEVEL--;
1287 ypr_close(ctx, flag);
1288}
1289
1290static void
Radek Krejci693262f2019-04-29 15:23:20 +02001291yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1292{
1293 int flag = 0;
1294
Michal Vasko5233e962020-08-14 14:26:20 +02001295 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001296
1297 LEVEL++;
1298 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1299 yprc_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1300 ypr_status(ctx, action->flags, action->exts, &flag);
1301 ypr_description(ctx, action->dsc, action->exts, &flag);
1302 ypr_reference(ctx, action->ref, action->exts, &flag);
1303
1304 yprc_inout(ctx, action, &action->input, &flag);
1305 yprc_inout(ctx, action, &action->output, &flag);
1306
1307 LEVEL--;
1308 ypr_close(ctx, flag);
1309}
1310
1311static void
1312yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001313{
Michal Vasko5233e962020-08-14 14:26:20 +02001314 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001315 LEVEL++;
1316
Radek Krejci693262f2019-04-29 15:23:20 +02001317 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1318 yprp_when(ctx, node->when, flag);
1319 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001320}
1321
1322static void
Radek Krejci693262f2019-04-29 15:23:20 +02001323yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001324{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001325 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001326
Michal Vasko5233e962020-08-14 14:26:20 +02001327 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001328 LEVEL++;
1329
1330 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1331 LY_ARRAY_FOR(node->when, u) {
1332 yprc_when(ctx, node->when[u], flag);
1333 }
1334 yprc_iffeatures(ctx, node->iffeatures, node->exts, flag);
1335}
1336
1337/* macr oto unify the code */
1338#define YPR_NODE_COMMON2 \
1339 ypr_config(ctx, node->flags, node->exts, flag); \
1340 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1341 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1342 } \
1343 ypr_status(ctx, node->flags, node->exts, flag); \
1344 ypr_description(ctx, node->dsc, node->exts, flag); \
1345 ypr_reference(ctx, node->ref, node->exts, flag)
1346
1347static void
1348yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
1349{
1350 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001351}
1352
1353static void
Radek Krejci693262f2019-04-29 15:23:20 +02001354yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
1355{
1356 YPR_NODE_COMMON2;
1357}
1358
1359#undef YPR_NODE_COMMON2
1360
1361static void
1362yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001363{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001364 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001365 int flag = 0;
1366 struct lysp_node *child;
1367 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1368
Radek Krejci693262f2019-04-29 15:23:20 +02001369 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001370
1371 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001372 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001373 }
1374 if (cont->presence) {
1375 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001376 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001377 }
1378
Radek Krejci693262f2019-04-29 15:23:20 +02001379 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001380
1381 LY_ARRAY_FOR(cont->typedefs, u) {
1382 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001383 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001384 }
1385
1386 LY_ARRAY_FOR(cont->groupings, u) {
1387 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001388 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001389 }
1390
1391 LY_LIST_FOR(cont->child, child) {
1392 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001393 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001394 }
1395
1396 LY_ARRAY_FOR(cont->actions, u) {
1397 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001398 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001399 }
1400
1401 LY_ARRAY_FOR(cont->notifs, u) {
1402 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001403 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001404 }
1405
1406 LEVEL--;
1407 ypr_close(ctx, flag);
1408}
1409
1410static void
Radek Krejci693262f2019-04-29 15:23:20 +02001411yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1412{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001413 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001414 int flag = 0;
1415 struct lysc_node *child;
1416 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1417
1418 yprc_node_common1(ctx, node, &flag);
1419
1420 LY_ARRAY_FOR(cont->musts, u) {
1421 yprc_must(ctx, &cont->musts[u], &flag);
1422 }
1423 if (cont->flags & LYS_PRESENCE) {
1424 ypr_open(ctx->out, &flag);
1425 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1426 }
1427
1428 yprc_node_common2(ctx, node, &flag);
1429
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001430 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001431 LY_LIST_FOR(cont->child, child) {
1432 ypr_open(ctx->out, &flag);
1433 yprc_node(ctx, child);
1434 }
Radek Krejci693262f2019-04-29 15:23:20 +02001435
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001436 LY_ARRAY_FOR(cont->actions, u) {
1437 ypr_open(ctx->out, &flag);
1438 yprc_action(ctx, &cont->actions[u]);
1439 }
Radek Krejci693262f2019-04-29 15:23:20 +02001440
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001441 LY_ARRAY_FOR(cont->notifs, u) {
1442 ypr_open(ctx->out, &flag);
1443 yprc_notification(ctx, &cont->notifs[u]);
1444 }
Radek Krejci693262f2019-04-29 15:23:20 +02001445 }
1446
1447 LEVEL--;
1448 ypr_close(ctx, flag);
1449}
1450
1451static void
1452yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1453{
1454 int flag = 0;
1455 struct lysp_node *child;
1456 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1457
1458 yprp_node_common1(ctx, node, &flag);
1459 yprp_node_common2(ctx, node, &flag);
1460
1461 LY_LIST_FOR(cas->child, child) {
1462 ypr_open(ctx->out, &flag);
1463 yprp_node(ctx, child);
1464 }
1465
1466 LEVEL--;
1467 ypr_close(ctx, flag);
1468}
1469
1470static void
1471yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1472{
1473 int flag = 0;
1474 struct lysc_node *child;
1475
1476 yprc_node_common1(ctx, (struct lysc_node*)cs, &flag);
1477 yprc_node_common2(ctx, (struct lysc_node*)cs, &flag);
1478
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001479 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001480 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
1481 ypr_open(ctx->out, &flag);
1482 yprc_node(ctx, child);
1483 }
Radek Krejci693262f2019-04-29 15:23:20 +02001484 }
1485
1486 LEVEL--;
1487 ypr_close(ctx, flag);
1488}
1489
1490static void
1491yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001492{
1493 int flag = 0;
1494 struct lysp_node *child;
1495 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1496
Radek Krejci693262f2019-04-29 15:23:20 +02001497 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001498
1499 if (choice->dflt) {
1500 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001501 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001502 }
1503
Radek Krejci693262f2019-04-29 15:23:20 +02001504 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001505
1506 LY_LIST_FOR(choice->child, child) {
1507 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001508 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001509 }
1510
1511 LEVEL--;
1512 ypr_close(ctx, flag);
1513}
1514
1515static void
Radek Krejci693262f2019-04-29 15:23:20 +02001516yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1517{
1518 int flag = 0;
1519 struct lysc_node_case *cs;
1520 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1521
1522 yprc_node_common1(ctx, node, &flag);
1523
1524 if (choice->dflt) {
1525 ypr_open(ctx->out, &flag);
1526 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1527 }
1528
1529 yprc_node_common2(ctx, node, &flag);
1530
1531 for (cs = choice->cases; cs; cs = (struct lysc_node_case*)cs->next) {
1532 ypr_open(ctx->out, &flag);
1533 yprc_case(ctx, cs);
1534 }
1535
1536 LEVEL--;
1537 ypr_close(ctx, flag);
1538}
1539
1540static void
1541yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001542{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001543 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001544 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1545
Radek Krejci693262f2019-04-29 15:23:20 +02001546 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001547
Radek Krejci693262f2019-04-29 15:23:20 +02001548 yprp_type(ctx, &leaf->type);
1549 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001550 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001551 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001552 }
Radek Krejci693262f2019-04-29 15:23:20 +02001553 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001554
Radek Krejci693262f2019-04-29 15:23:20 +02001555 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001556
1557 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001558 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001559}
1560
1561static void
Radek Krejci693262f2019-04-29 15:23:20 +02001562yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1563{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001564 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001565 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1566
1567 yprc_node_common1(ctx, node, NULL);
1568
1569 yprc_type(ctx, leaf->type);
1570 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1571 LY_ARRAY_FOR(leaf->musts, u) {
1572 yprc_must(ctx, &leaf->musts[u], NULL);
1573 }
Radek Krejcia1911222019-07-22 17:24:50 +02001574
1575 if (leaf->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001576 yprc_dflt_value(ctx, leaf->dflt, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001577 }
Radek Krejci693262f2019-04-29 15:23:20 +02001578
1579 yprc_node_common2(ctx, node, NULL);
1580
1581 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001582 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001583}
1584
1585static void
1586yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001587{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001588 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001589 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1590
Radek Krejci693262f2019-04-29 15:23:20 +02001591 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001592
Radek Krejci693262f2019-04-29 15:23:20 +02001593 yprp_type(ctx, &llist->type);
1594 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001595 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001596 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001597 }
1598 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001599 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001600 }
1601
Radek Krejci693262f2019-04-29 15:23:20 +02001602 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001603
1604 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001605 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001606 }
1607 if (llist->flags & LYS_SET_MAX) {
1608 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001609 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001610 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001611 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001612 }
1613 }
1614
1615 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001616 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001617 }
1618
Radek Krejci693262f2019-04-29 15:23:20 +02001619 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001620 ypr_description(ctx, node->dsc, node->exts, NULL);
1621 ypr_reference(ctx, node->ref, node->exts, NULL);
1622
1623 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001624 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001625}
1626
1627static void
Radek Krejci693262f2019-04-29 15:23:20 +02001628yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1629{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001630 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001631 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1632
1633 yprc_node_common1(ctx, node, NULL);
1634
1635 yprc_type(ctx, llist->type);
1636 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1637 LY_ARRAY_FOR(llist->musts, u) {
1638 yprc_must(ctx, &llist->musts[u], NULL);
1639 }
1640 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001641 yprc_dflt_value(ctx, llist->dflts[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001642 }
1643
1644 ypr_config(ctx, node->flags, node->exts, NULL);
1645
1646 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1647 if (llist->max) {
1648 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1649 } else {
1650 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1651 }
1652
1653 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1654
1655 ypr_status(ctx, node->flags, node->exts, NULL);
1656 ypr_description(ctx, node->dsc, node->exts, NULL);
1657 ypr_reference(ctx, node->ref, node->exts, NULL);
1658
1659 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001660 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001661}
1662
1663static void
1664yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001665{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001666 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001667 int flag = 0;
1668 struct lysp_node *child;
1669 struct lysp_node_list *list = (struct lysp_node_list *)node;
1670
Radek Krejci693262f2019-04-29 15:23:20 +02001671 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001672
1673 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001674 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001675 }
1676 if (list->key) {
1677 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001678 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001679 }
1680 LY_ARRAY_FOR(list->uniques, u) {
1681 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001682 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001683 }
1684
Radek Krejci693262f2019-04-29 15:23:20 +02001685 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001686
1687 if (list->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001688 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001689 }
1690 if (list->flags & LYS_SET_MAX) {
1691 if (list->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001692 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001693 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001694 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001695 }
1696 }
1697
1698 if (list->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001699 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001700 }
1701
Radek Krejci693262f2019-04-29 15:23:20 +02001702 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001703 ypr_description(ctx, node->dsc, node->exts, NULL);
1704 ypr_reference(ctx, node->ref, node->exts, NULL);
1705
1706 LY_ARRAY_FOR(list->typedefs, u) {
1707 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001708 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001709 }
1710
1711 LY_ARRAY_FOR(list->groupings, u) {
1712 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001713 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001714 }
1715
1716 LY_LIST_FOR(list->child, child) {
1717 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001718 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001719 }
1720
1721 LY_ARRAY_FOR(list->actions, u) {
1722 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001723 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001724 }
1725
1726 LY_ARRAY_FOR(list->notifs, u) {
1727 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001728 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001729 }
1730
1731 LEVEL--;
1732 ypr_close(ctx, flag);
1733}
1734
1735static void
Radek Krejci693262f2019-04-29 15:23:20 +02001736yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1737{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001738 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001739 int flag = 0;
1740 struct lysc_node *child;
1741 struct lysc_node_list *list = (struct lysc_node_list *)node;
1742
1743 yprc_node_common1(ctx, node, &flag);
1744
1745 LY_ARRAY_FOR(list->musts, u) {
1746 yprc_must(ctx, &list->musts[u], NULL);
1747 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001748 if (!(list->flags & LYS_KEYLESS)) {
Radek Krejci693262f2019-04-29 15:23:20 +02001749 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +02001750 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001751 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 +02001752 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001753 }
Michal Vasko5233e962020-08-14 14:26:20 +02001754 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001755 }
1756 LY_ARRAY_FOR(list->uniques, u) {
1757 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +02001758 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001759 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001760 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001761 }
1762 ypr_close(ctx, 0);
1763 }
1764
1765 ypr_config(ctx, node->flags, node->exts, NULL);
1766
1767 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1768 if (list->max) {
1769 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1770 } else {
1771 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1772 }
1773
1774 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1775
1776 ypr_status(ctx, node->flags, node->exts, NULL);
1777 ypr_description(ctx, node->dsc, node->exts, NULL);
1778 ypr_reference(ctx, node->ref, node->exts, NULL);
1779
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001780 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001781 LY_LIST_FOR(list->child, child) {
1782 ypr_open(ctx->out, &flag);
1783 yprc_node(ctx, child);
1784 }
Radek Krejci693262f2019-04-29 15:23:20 +02001785
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001786 LY_ARRAY_FOR(list->actions, u) {
1787 ypr_open(ctx->out, &flag);
1788 yprc_action(ctx, &list->actions[u]);
1789 }
Radek Krejci693262f2019-04-29 15:23:20 +02001790
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001791 LY_ARRAY_FOR(list->notifs, u) {
1792 ypr_open(ctx->out, &flag);
1793 yprc_notification(ctx, &list->notifs[u]);
1794 }
Radek Krejci693262f2019-04-29 15:23:20 +02001795 }
1796
1797 LEVEL--;
1798 ypr_close(ctx, flag);
1799}
1800
1801static void
1802yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001803{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001804 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001805 int flag = 0;
1806
Michal Vasko5233e962020-08-14 14:26:20 +02001807 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001808 LEVEL++;
1809
Radek Krejci693262f2019-04-29 15:23:20 +02001810 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1811 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001812
1813 LY_ARRAY_FOR(refine->musts, u) {
1814 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001815 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001816 }
1817
1818 if (refine->presence) {
1819 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001820 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001821 }
1822
1823 LY_ARRAY_FOR(refine->dflts, u) {
1824 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001825 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001826 }
1827
Radek Krejci693262f2019-04-29 15:23:20 +02001828 ypr_config(ctx, refine->flags, refine->exts, &flag);
1829 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001830
1831 if (refine->flags & LYS_SET_MIN) {
1832 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001833 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001834 }
1835 if (refine->flags & LYS_SET_MAX) {
1836 ypr_open(ctx->out, &flag);
1837 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001838 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001839 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001840 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001841 }
1842 }
1843
1844 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1845 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1846
1847 LEVEL--;
1848 ypr_close(ctx, flag);
1849}
1850
1851static void
Radek Krejci693262f2019-04-29 15:23:20 +02001852yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001853{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001854 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001855 struct lysp_node *child;
1856
Michal Vasko5233e962020-08-14 14:26:20 +02001857 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001858 LEVEL++;
1859
Radek Krejci693262f2019-04-29 15:23:20 +02001860 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1861 yprp_when(ctx, aug->when, NULL);
1862 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1863 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001864 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1865 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1866
1867 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001868 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001869 }
1870
1871 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001872 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001873 }
1874
1875 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001876 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877 }
1878
1879 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001880 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001881}
1882
Radek Krejcid3ca0632019-04-16 16:54:54 +02001883static void
Radek Krejci693262f2019-04-29 15:23:20 +02001884yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001885{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001886 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001887 int flag = 0;
1888 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1889
Radek Krejci693262f2019-04-29 15:23:20 +02001890 yprp_node_common1(ctx, node, &flag);
1891 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001892
1893 LY_ARRAY_FOR(uses->refines, u) {
1894 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001895 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001896 }
1897
1898 LY_ARRAY_FOR(uses->augments, u) {
1899 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001900 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001901 }
1902
1903 LEVEL--;
1904 ypr_close(ctx, flag);
1905}
1906
1907static void
Radek Krejci693262f2019-04-29 15:23:20 +02001908yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001909{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001910 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001911 int flag = 0;
1912 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1913
Radek Krejci693262f2019-04-29 15:23:20 +02001914 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001915
1916 LY_ARRAY_FOR(any->musts, u) {
1917 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001918 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001919 }
1920
Radek Krejci693262f2019-04-29 15:23:20 +02001921 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001922
1923 LEVEL--;
1924 ypr_close(ctx, flag);
1925}
1926
1927static void
Radek Krejci693262f2019-04-29 15:23:20 +02001928yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001929{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001930 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001931 int flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001932 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001933
Radek Krejci693262f2019-04-29 15:23:20 +02001934 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001935
Radek Krejci693262f2019-04-29 15:23:20 +02001936 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001937 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001938 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001939 }
1940
Radek Krejci693262f2019-04-29 15:23:20 +02001941 yprc_node_common2(ctx, node, &flag);
1942
Radek Krejcid3ca0632019-04-16 16:54:54 +02001943 LEVEL--;
1944 ypr_close(ctx, flag);
1945}
1946
1947static void
Radek Krejci693262f2019-04-29 15:23:20 +02001948yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001949{
1950 switch (node->nodetype) {
1951 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001952 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001953 break;
1954 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001955 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001956 break;
1957 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001958 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001959 break;
1960 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001961 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001962 break;
1963 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001964 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001965 break;
1966 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001967 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001968 break;
1969 case LYS_ANYXML:
1970 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001971 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001972 break;
1973 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001974 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001975 break;
1976 default:
1977 break;
1978 }
1979}
1980
1981static void
Radek Krejci693262f2019-04-29 15:23:20 +02001982yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
1983{
1984 switch (node->nodetype) {
1985 case LYS_CONTAINER:
1986 yprc_container(ctx, node);
1987 break;
1988 case LYS_CHOICE:
1989 yprc_choice(ctx, node);
1990 break;
1991 case LYS_LEAF:
1992 yprc_leaf(ctx, node);
1993 break;
1994 case LYS_LEAFLIST:
1995 yprc_leaflist(ctx, node);
1996 break;
1997 case LYS_LIST:
1998 yprc_list(ctx, node);
1999 break;
2000 case LYS_ANYXML:
2001 case LYS_ANYDATA:
2002 yprc_anydata(ctx, node);
2003 break;
2004 default:
2005 break;
2006 }
2007}
2008
2009static void
2010yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002011{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002012 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002013 struct lysp_deviate_add *add;
2014 struct lysp_deviate_rpl *rpl;
2015 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08002016 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002017
Michal Vasko5233e962020-08-14 14:26:20 +02002018 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002019 LEVEL++;
2020
Radek Krejci693262f2019-04-29 15:23:20 +02002021 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002022 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
2023 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
2024
fredgan2b11ddb2019-10-21 11:03:39 +08002025 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02002026 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08002027 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
2028 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002029 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002030 LEVEL++;
2031
fredgan2b11ddb2019-10-21 11:03:39 +08002032 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002033 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002034 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002035 continue;
2036 }
fredgan2b11ddb2019-10-21 11:03:39 +08002037 } else if (elem->mod == LYS_DEV_ADD) {
2038 add = (struct lysp_deviate_add*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002039 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002040 LEVEL++;
2041
Radek Krejci693262f2019-04-29 15:23:20 +02002042 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
2043 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002044 LY_ARRAY_FOR(add->musts, u) {
2045 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002046 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002047 LY_ARRAY_FOR(add->uniques, u) {
2048 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002049 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002050 LY_ARRAY_FOR(add->dflts, u) {
2051 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002052 }
Radek Krejci693262f2019-04-29 15:23:20 +02002053 ypr_config(ctx, add->flags, add->exts, NULL);
2054 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002055 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002056 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002057 }
2058 if (add->flags & LYS_SET_MAX) {
2059 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002060 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002061 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002062 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002063 }
2064 }
fredgan2b11ddb2019-10-21 11:03:39 +08002065 } else if (elem->mod == LYS_DEV_REPLACE) {
2066 rpl = (struct lysp_deviate_rpl*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002067 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002068 LEVEL++;
2069
Radek Krejci693262f2019-04-29 15:23:20 +02002070 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002071 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002072 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002073 }
Radek Krejci693262f2019-04-29 15:23:20 +02002074 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
2075 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
2076 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2077 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002078 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002079 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002080 }
2081 if (rpl->flags & LYS_SET_MAX) {
2082 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002083 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002084 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002085 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002086 }
2087 }
fredgan2b11ddb2019-10-21 11:03:39 +08002088 } else if (elem->mod == LYS_DEV_DELETE) {
2089 del = (struct lysp_deviate_del*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002090 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002091 LEVEL++;
2092
Radek Krejci693262f2019-04-29 15:23:20 +02002093 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2094 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002095 LY_ARRAY_FOR(del->musts, u) {
2096 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002097 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002098 LY_ARRAY_FOR(del->uniques, u) {
2099 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002100 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002101 LY_ARRAY_FOR(del->dflts, u) {
2102 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002103 }
2104 }
2105
2106 LEVEL--;
2107 ypr_close(ctx, 1);
2108 }
2109
2110 LEVEL--;
2111 ypr_close(ctx, 1);
2112}
2113
Michal Vasko7c8439f2020-08-05 13:25:19 +02002114static void
2115yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002116{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002117 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002118
Radek Krejcid3ca0632019-04-16 16:54:54 +02002119 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002120 ly_print_(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002121 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002122 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2123 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002124 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002125 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002126 }
Radek Krejci693262f2019-04-29 15:23:20 +02002127 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2128 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002129 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002130 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002131 }
2132 LY_ARRAY_FOR(modp->includes, u) {
2133 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 +02002134 ly_print_(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002135 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002136 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002137 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002138 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002139 }
Radek Krejci693262f2019-04-29 15:23:20 +02002140 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2141 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002142 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002143 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002144 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002145 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002146 }
2147 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002148}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002149
Michal Vasko7c8439f2020-08-05 13:25:19 +02002150static void
2151yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2152{
2153 LY_ARRAY_COUNT_TYPE u;
2154 struct lysp_node *data;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002155
Radek Krejcid3ca0632019-04-16 16:54:54 +02002156 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002157 ly_print_(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002158 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002159 }
2160 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002161 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002162 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002163 }
2164
2165 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002166 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002167 }
2168
2169 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002170 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002171 }
2172
2173 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002174 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002175 }
2176
2177 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002178 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002179 }
2180
2181 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002182 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002183 }
2184
2185 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002186 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002187 }
2188
2189 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002190 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002191 }
2192
2193 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002194 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002195 }
2196
2197 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002198 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002199 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002200}
2201
2202LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02002203yang_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, int options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002204{
2205 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02002206 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002207
Michal Vasko5233e962020-08-14 14:26:20 +02002208 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002209 LEVEL++;
2210
2211 /* module-header-stmts */
2212 if (module->version) {
2213 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
2214 }
2215
2216 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2217 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2218
2219 /* linkage-stmts (import/include) */
2220 yang_print_parsed_linkage(ctx, modp);
2221
2222 /* meta-stmts */
2223 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002224 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002225 }
2226 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2227 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2228 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2229 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2230
2231 /* revision-stmts */
2232 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002233 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002234 }
2235 LY_ARRAY_FOR(modp->revs, u) {
2236 yprp_revision(ctx, &modp->revs[u]);
2237 }
2238 /* body-stmts */
2239 yang_print_parsed_body(ctx, modp);
2240
2241 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002242 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002243 ly_print_flush(out);
2244
2245 return LY_SUCCESS;
2246}
2247
2248static void
2249yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2250{
Michal Vasko5233e962020-08-14 14:26:20 +02002251 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->belongsto);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002252 LEVEL++;
2253 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
2254 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2255 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002256 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002257}
2258
2259LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02002260yang_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, int options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002261{
2262 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02002263 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002264
Michal Vasko5233e962020-08-14 14:26:20 +02002265 ly_print_(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002266 LEVEL++;
2267
2268 /* submodule-header-stmts */
2269 if (submodp->version) {
2270 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
2271 }
2272
2273 yprp_belongsto(ctx, submodp);
2274
2275 /* linkage-stmts (import/include) */
2276 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2277
2278 /* meta-stmts */
2279 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002280 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002281 }
2282 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2283 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
2284 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2285 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
2286
2287 /* revision-stmts */
2288 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002289 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002290 }
2291 LY_ARRAY_FOR(submodp->revs, u) {
2292 yprp_revision(ctx, &submodp->revs[u]);
2293 }
2294 /* body-stmts */
2295 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002296
2297 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002298 ly_print_(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002299 ly_print_flush(out);
2300
2301 return LY_SUCCESS;
2302}
2303
2304LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002305yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002306{
Radek Krejci5536d282020-08-04 23:27:44 +02002307 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002308
2309 yprc_node(ctx, node);
2310
2311 ly_print_flush(out);
2312 return LY_SUCCESS;
2313}
2314
2315LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002316yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002317{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002318 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002319 struct lysc_node *data;
2320 struct lysc_module *modc = module->compiled;
Radek Krejci5536d282020-08-04 23:27:44 +02002321 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002322
Michal Vasko5233e962020-08-14 14:26:20 +02002323 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002324 LEVEL++;
2325
Radek Krejci693262f2019-04-29 15:23:20 +02002326 /* module-header-stmts */
2327 if (module->version) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002328 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modc->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002329 }
2330 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2331 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2332
Michal Vasko7c8439f2020-08-05 13:25:19 +02002333 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002334
2335 /* meta-stmts */
2336 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002337 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002338 }
2339 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2340 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2341 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2342 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2343
2344 /* revision-stmts */
2345 if (module->revision) {
Michal Vasko5233e962020-08-14 14:26:20 +02002346 ly_print_(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002347 }
2348
2349 /* body-stmts */
2350 if (modc->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002351 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002352 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2353 }
2354
2355 LY_ARRAY_FOR(modc->features, u) {
2356 yprc_feature(ctx, &modc->features[u]);
2357 }
2358
2359 LY_ARRAY_FOR(modc->identities, u) {
2360 yprc_identity(ctx, &modc->identities[u]);
2361 }
2362
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08002363 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002364 LY_LIST_FOR(modc->data, data) {
2365 yprc_node(ctx, data);
2366 }
Radek Krejci693262f2019-04-29 15:23:20 +02002367
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002368 LY_ARRAY_FOR(modc->rpcs, u) {
2369 yprc_action(ctx, &modc->rpcs[u]);
2370 }
Radek Krejci693262f2019-04-29 15:23:20 +02002371
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002372 LY_ARRAY_FOR(modc->notifs, u) {
2373 yprc_notification(ctx, &modc->notifs[u]);
2374 }
Radek Krejci693262f2019-04-29 15:23:20 +02002375 }
2376
2377 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002378 ly_print_(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002379 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002380
2381 return LY_SUCCESS;
2382}