blob: 20b01b51540ee9ff7eb2c3960fb95601b0e13c49 [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
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200931yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, const struct lys_module *value_mod, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200932{
933 int dynamic;
934 const char *str;
935
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200936 str = value->realtype->plugin->print(value, LY_PREF_SCHEMA, (void *)value_mod, &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);
953 if (type->dflt) {
954 ypr_open(ctx->out, &flag);
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200955 yprc_dflt_value(ctx, type->dflt, type->dflt_mod, type->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200956 }
957
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200958 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200959 case LY_TYPE_BINARY: {
960 struct lysc_type_bin *bin = (struct lysc_type_bin*)type;
961 yprc_range(ctx, bin->length, type->basetype, &flag);
962 break;
963 }
964 case LY_TYPE_UINT8:
965 case LY_TYPE_UINT16:
966 case LY_TYPE_UINT32:
967 case LY_TYPE_UINT64:
968 case LY_TYPE_INT8:
969 case LY_TYPE_INT16:
970 case LY_TYPE_INT32:
971 case LY_TYPE_INT64: {
972 struct lysc_type_num *num = (struct lysc_type_num*)type;
973 yprc_range(ctx, num->range, type->basetype, &flag);
974 break;
975 }
976 case LY_TYPE_STRING: {
977 struct lysc_type_str *str = (struct lysc_type_str*)type;
978 yprc_range(ctx, str->length, type->basetype, &flag);
979 LY_ARRAY_FOR(str->patterns, u) {
980 yprc_pattern(ctx, str->patterns[u], &flag);
981 }
982 break;
983 }
984 case LY_TYPE_BITS:
985 case LY_TYPE_ENUM: {
986 /* bits and enums structures are compatible */
987 struct lysc_type_bits *bits = (struct lysc_type_bits*)type;
988 LY_ARRAY_FOR(bits->bits, u) {
989 struct lysc_type_bitenum_item *item = &bits->bits[u];
990 int inner_flag = 0;
991
992 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200993 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200994 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200995 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200996 LEVEL++;
997 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
998 yprc_iffeatures(ctx, item->iffeatures, item->exts, &inner_flag);
999 if (type->basetype == LY_TYPE_BITS) {
1000 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
1001 } else { /* LY_TYPE_ENUM */
1002 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
1003 }
1004 ypr_status(ctx, item->flags, item->exts, &inner_flag);
1005 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
1006 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
1007 LEVEL--;
1008 ypr_close(ctx, inner_flag);
1009 }
1010 break;
1011 }
1012 case LY_TYPE_BOOL:
1013 case LY_TYPE_EMPTY:
1014 /* nothing to do */
1015 break;
1016 case LY_TYPE_DEC64: {
1017 struct lysc_type_dec *dec = (struct lysc_type_dec*)type;
1018 ypr_open(ctx->out, &flag);
1019 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
1020 yprc_range(ctx, dec->range, dec->basetype, &flag);
1021 break;
1022 }
1023 case LY_TYPE_IDENT: {
1024 struct lysc_type_identityref *ident = (struct lysc_type_identityref*)type;
1025 LY_ARRAY_FOR(ident->bases, u) {
1026 ypr_open(ctx->out, &flag);
1027 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
1028 }
1029 break;
1030 }
1031 case LY_TYPE_INST: {
1032 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid*)type;
1033 ypr_open(ctx->out, &flag);
1034 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
1035 break;
1036 }
1037 case LY_TYPE_LEAFREF: {
1038 struct lysc_type_leafref *lr = (struct lysc_type_leafref*)type;
1039 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +02001040 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001041 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
1042 yprc_type(ctx, lr->realtype);
1043 break;
1044 }
1045 case LY_TYPE_UNION: {
1046 struct lysc_type_union *un = (struct lysc_type_union*)type;
1047 LY_ARRAY_FOR(un->types, u) {
1048 ypr_open(ctx->out, &flag);
1049 yprc_type(ctx, un->types[u]);
1050 }
1051 break;
1052 }
1053 default:
1054 LOGINT(ctx->module->ctx);
1055 }
1056
1057 LEVEL--;
1058 ypr_close(ctx, flag);
1059}
1060
1061static void
1062yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001063{
Michal Vasko5233e962020-08-14 14:26:20 +02001064 ly_print_(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001065 LEVEL++;
1066
Radek Krejci693262f2019-04-29 15:23:20 +02001067 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001068
Radek Krejci693262f2019-04-29 15:23:20 +02001069 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001070
1071 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +02001072 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001073 }
1074 if (tpdf->dflt) {
Radek Krejci693262f2019-04-29 15:23:20 +02001075 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001076 }
1077
Radek Krejci693262f2019-04-29 15:23:20 +02001078 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001079 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
1080 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
1081
1082 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001083 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001084}
1085
Radek Krejci693262f2019-04-29 15:23:20 +02001086static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
1087static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
1088static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001089
1090static void
Radek Krejci693262f2019-04-29 15:23:20 +02001091yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001092{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001093 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001094 int flag = 0;
1095 struct lysp_node *data;
1096
Michal Vasko5233e962020-08-14 14:26:20 +02001097 ly_print_(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001098 LEVEL++;
1099
Radek Krejci693262f2019-04-29 15:23:20 +02001100 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1101 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001102 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1103 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001104
1105 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001106 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001107 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001108 }
1109
1110 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001111 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001112 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001113 }
1114
1115 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001116 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001117 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001118 }
1119
1120 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001121 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122 }
1123
1124 LEVEL--;
1125 ypr_close(ctx, flag);
1126}
1127
1128static void
Radek Krejci693262f2019-04-29 15:23:20 +02001129yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001130{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001131 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001132 struct lysp_node *data;
1133
1134 if (!inout->nodetype) {
1135 /* nodetype not set -> input/output is empty */
1136 return;
1137 }
1138 ypr_open(ctx->out, flag);
1139
Michal Vasko5233e962020-08-14 14:26:20 +02001140 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001141 LEVEL++;
1142
Radek Krejci693262f2019-04-29 15:23:20 +02001143 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001144 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001145 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001146 }
1147 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001148 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001149 }
1150 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001151 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001152 }
1153
1154 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001155 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001156 }
1157
1158 LEVEL--;
1159 ypr_close(ctx, 1);
1160}
1161
1162static void
Radek Krejci693262f2019-04-29 15:23:20 +02001163yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, int *flag)
1164{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001165 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001166 struct lysc_node *data;
1167
1168 if (!inout->data) {
1169 /* input/output is empty */
1170 return;
1171 }
1172 ypr_open(ctx->out, flag);
1173
Michal Vasko5233e962020-08-14 14:26:20 +02001174 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001175 LEVEL++;
1176
1177 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1178 LY_ARRAY_FOR(inout->musts, u) {
1179 yprc_must(ctx, &inout->musts[u], NULL);
1180 }
1181
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001182 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001183 LY_LIST_FOR(inout->data, data) {
1184 yprc_node(ctx, data);
1185 }
Radek Krejci693262f2019-04-29 15:23:20 +02001186 }
1187
1188 LEVEL--;
1189 ypr_close(ctx, 1);
1190}
1191
1192static void
1193yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001194{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001195 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001196 int flag = 0;
1197 struct lysp_node *data;
1198
Michal Vasko5233e962020-08-14 14:26:20 +02001199 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001200
1201 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001202 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1203 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001204
1205 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001206 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001207 }
Radek Krejci693262f2019-04-29 15:23:20 +02001208 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001209 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1210 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1211
1212 LY_ARRAY_FOR(notif->typedefs, u) {
1213 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001214 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001215 }
1216
1217 LY_ARRAY_FOR(notif->groupings, u) {
1218 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001219 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001220 }
1221
1222 LY_LIST_FOR(notif->data, data) {
1223 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001224 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001225 }
1226
1227 LEVEL--;
1228 ypr_close(ctx, flag);
1229}
1230
1231static void
Radek Krejci693262f2019-04-29 15:23:20 +02001232yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1233{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001234 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001235 int flag = 0;
1236 struct lysc_node *data;
1237
Michal Vasko5233e962020-08-14 14:26:20 +02001238 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001239
1240 LEVEL++;
1241 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1242 yprc_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
1243
1244 LY_ARRAY_FOR(notif->musts, u) {
1245 yprc_must(ctx, &notif->musts[u], &flag);
1246 }
1247 ypr_status(ctx, notif->flags, notif->exts, &flag);
1248 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1249 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1250
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001251 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001252 LY_LIST_FOR(notif->data, data) {
1253 ypr_open(ctx->out, &flag);
1254 yprc_node(ctx, data);
1255 }
Radek Krejci693262f2019-04-29 15:23:20 +02001256 }
1257
1258 LEVEL--;
1259 ypr_close(ctx, flag);
1260}
1261
1262static void
1263yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001264{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001265 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001266 int flag = 0;
1267
Michal Vasko5233e962020-08-14 14:26:20 +02001268 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001269
1270 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001271 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1272 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1273 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001274 ypr_description(ctx, action->dsc, action->exts, &flag);
1275 ypr_reference(ctx, action->ref, action->exts, &flag);
1276
1277 LY_ARRAY_FOR(action->typedefs, u) {
1278 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001279 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001280 }
1281
1282 LY_ARRAY_FOR(action->groupings, u) {
1283 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001284 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001285 }
1286
Radek Krejci693262f2019-04-29 15:23:20 +02001287 yprp_inout(ctx, &action->input, &flag);
1288 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001289
1290 LEVEL--;
1291 ypr_close(ctx, flag);
1292}
1293
1294static void
Radek Krejci693262f2019-04-29 15:23:20 +02001295yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1296{
1297 int flag = 0;
1298
Michal Vasko5233e962020-08-14 14:26:20 +02001299 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001300
1301 LEVEL++;
1302 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1303 yprc_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1304 ypr_status(ctx, action->flags, action->exts, &flag);
1305 ypr_description(ctx, action->dsc, action->exts, &flag);
1306 ypr_reference(ctx, action->ref, action->exts, &flag);
1307
1308 yprc_inout(ctx, action, &action->input, &flag);
1309 yprc_inout(ctx, action, &action->output, &flag);
1310
1311 LEVEL--;
1312 ypr_close(ctx, flag);
1313}
1314
1315static void
1316yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001317{
Michal Vasko5233e962020-08-14 14:26:20 +02001318 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001319 LEVEL++;
1320
Radek Krejci693262f2019-04-29 15:23:20 +02001321 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1322 yprp_when(ctx, node->when, flag);
1323 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001324}
1325
1326static void
Radek Krejci693262f2019-04-29 15:23:20 +02001327yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001328{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001329 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001330
Michal Vasko5233e962020-08-14 14:26:20 +02001331 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001332 LEVEL++;
1333
1334 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1335 LY_ARRAY_FOR(node->when, u) {
1336 yprc_when(ctx, node->when[u], flag);
1337 }
1338 yprc_iffeatures(ctx, node->iffeatures, node->exts, flag);
1339}
1340
1341/* macr oto unify the code */
1342#define YPR_NODE_COMMON2 \
1343 ypr_config(ctx, node->flags, node->exts, flag); \
1344 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1345 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1346 } \
1347 ypr_status(ctx, node->flags, node->exts, flag); \
1348 ypr_description(ctx, node->dsc, node->exts, flag); \
1349 ypr_reference(ctx, node->ref, node->exts, flag)
1350
1351static void
1352yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
1353{
1354 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001355}
1356
1357static void
Radek Krejci693262f2019-04-29 15:23:20 +02001358yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
1359{
1360 YPR_NODE_COMMON2;
1361}
1362
1363#undef YPR_NODE_COMMON2
1364
1365static void
1366yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001367{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001368 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001369 int flag = 0;
1370 struct lysp_node *child;
1371 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1372
Radek Krejci693262f2019-04-29 15:23:20 +02001373 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001374
1375 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001376 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001377 }
1378 if (cont->presence) {
1379 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001380 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001381 }
1382
Radek Krejci693262f2019-04-29 15:23:20 +02001383 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001384
1385 LY_ARRAY_FOR(cont->typedefs, u) {
1386 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001387 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001388 }
1389
1390 LY_ARRAY_FOR(cont->groupings, u) {
1391 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001392 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001393 }
1394
1395 LY_LIST_FOR(cont->child, child) {
1396 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001397 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001398 }
1399
1400 LY_ARRAY_FOR(cont->actions, u) {
1401 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001402 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001403 }
1404
1405 LY_ARRAY_FOR(cont->notifs, u) {
1406 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001407 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001408 }
1409
1410 LEVEL--;
1411 ypr_close(ctx, flag);
1412}
1413
1414static void
Radek Krejci693262f2019-04-29 15:23:20 +02001415yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1416{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001417 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001418 int flag = 0;
1419 struct lysc_node *child;
1420 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1421
1422 yprc_node_common1(ctx, node, &flag);
1423
1424 LY_ARRAY_FOR(cont->musts, u) {
1425 yprc_must(ctx, &cont->musts[u], &flag);
1426 }
1427 if (cont->flags & LYS_PRESENCE) {
1428 ypr_open(ctx->out, &flag);
1429 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1430 }
1431
1432 yprc_node_common2(ctx, node, &flag);
1433
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001434 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001435 LY_LIST_FOR(cont->child, child) {
1436 ypr_open(ctx->out, &flag);
1437 yprc_node(ctx, child);
1438 }
Radek Krejci693262f2019-04-29 15:23:20 +02001439
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001440 LY_ARRAY_FOR(cont->actions, u) {
1441 ypr_open(ctx->out, &flag);
1442 yprc_action(ctx, &cont->actions[u]);
1443 }
Radek Krejci693262f2019-04-29 15:23:20 +02001444
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001445 LY_ARRAY_FOR(cont->notifs, u) {
1446 ypr_open(ctx->out, &flag);
1447 yprc_notification(ctx, &cont->notifs[u]);
1448 }
Radek Krejci693262f2019-04-29 15:23:20 +02001449 }
1450
1451 LEVEL--;
1452 ypr_close(ctx, flag);
1453}
1454
1455static void
1456yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1457{
1458 int flag = 0;
1459 struct lysp_node *child;
1460 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1461
1462 yprp_node_common1(ctx, node, &flag);
1463 yprp_node_common2(ctx, node, &flag);
1464
1465 LY_LIST_FOR(cas->child, child) {
1466 ypr_open(ctx->out, &flag);
1467 yprp_node(ctx, child);
1468 }
1469
1470 LEVEL--;
1471 ypr_close(ctx, flag);
1472}
1473
1474static void
1475yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1476{
1477 int flag = 0;
1478 struct lysc_node *child;
1479
1480 yprc_node_common1(ctx, (struct lysc_node*)cs, &flag);
1481 yprc_node_common2(ctx, (struct lysc_node*)cs, &flag);
1482
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001483 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001484 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
1485 ypr_open(ctx->out, &flag);
1486 yprc_node(ctx, child);
1487 }
Radek Krejci693262f2019-04-29 15:23:20 +02001488 }
1489
1490 LEVEL--;
1491 ypr_close(ctx, flag);
1492}
1493
1494static void
1495yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001496{
1497 int flag = 0;
1498 struct lysp_node *child;
1499 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1500
Radek Krejci693262f2019-04-29 15:23:20 +02001501 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001502
1503 if (choice->dflt) {
1504 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001505 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001506 }
1507
Radek Krejci693262f2019-04-29 15:23:20 +02001508 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001509
1510 LY_LIST_FOR(choice->child, child) {
1511 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001512 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001513 }
1514
1515 LEVEL--;
1516 ypr_close(ctx, flag);
1517}
1518
1519static void
Radek Krejci693262f2019-04-29 15:23:20 +02001520yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1521{
1522 int flag = 0;
1523 struct lysc_node_case *cs;
1524 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1525
1526 yprc_node_common1(ctx, node, &flag);
1527
1528 if (choice->dflt) {
1529 ypr_open(ctx->out, &flag);
1530 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1531 }
1532
1533 yprc_node_common2(ctx, node, &flag);
1534
1535 for (cs = choice->cases; cs; cs = (struct lysc_node_case*)cs->next) {
1536 ypr_open(ctx->out, &flag);
1537 yprc_case(ctx, cs);
1538 }
1539
1540 LEVEL--;
1541 ypr_close(ctx, flag);
1542}
1543
1544static void
1545yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001546{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001547 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001548 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1549
Radek Krejci693262f2019-04-29 15:23:20 +02001550 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001551
Radek Krejci693262f2019-04-29 15:23:20 +02001552 yprp_type(ctx, &leaf->type);
1553 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001554 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001555 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001556 }
Radek Krejci693262f2019-04-29 15:23:20 +02001557 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001558
Radek Krejci693262f2019-04-29 15:23:20 +02001559 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001560
1561 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001562 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001563}
1564
1565static void
Radek Krejci693262f2019-04-29 15:23:20 +02001566yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1567{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001568 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001569 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1570
1571 yprc_node_common1(ctx, node, NULL);
1572
1573 yprc_type(ctx, leaf->type);
1574 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1575 LY_ARRAY_FOR(leaf->musts, u) {
1576 yprc_must(ctx, &leaf->musts[u], NULL);
1577 }
Radek Krejcia1911222019-07-22 17:24:50 +02001578
1579 if (leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001580 yprc_dflt_value(ctx, leaf->dflt, leaf->dflt_mod, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001581 }
Radek Krejci693262f2019-04-29 15:23:20 +02001582
1583 yprc_node_common2(ctx, node, NULL);
1584
1585 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001586 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001587}
1588
1589static void
1590yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001591{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001592 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001593 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1594
Radek Krejci693262f2019-04-29 15:23:20 +02001595 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001596
Radek Krejci693262f2019-04-29 15:23:20 +02001597 yprp_type(ctx, &llist->type);
1598 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001599 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001600 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001601 }
1602 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001603 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001604 }
1605
Radek Krejci693262f2019-04-29 15:23:20 +02001606 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001607
1608 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001609 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001610 }
1611 if (llist->flags & LYS_SET_MAX) {
1612 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001613 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001614 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001615 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001616 }
1617 }
1618
1619 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001620 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001621 }
1622
Radek Krejci693262f2019-04-29 15:23:20 +02001623 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001624 ypr_description(ctx, node->dsc, node->exts, NULL);
1625 ypr_reference(ctx, node->ref, node->exts, NULL);
1626
1627 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001628 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001629}
1630
1631static void
Radek Krejci693262f2019-04-29 15:23:20 +02001632yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1633{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001634 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001635 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1636
1637 yprc_node_common1(ctx, node, NULL);
1638
1639 yprc_type(ctx, llist->type);
1640 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1641 LY_ARRAY_FOR(llist->musts, u) {
1642 yprc_must(ctx, &llist->musts[u], NULL);
1643 }
1644 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001645 yprc_dflt_value(ctx, llist->dflts[u], llist->dflts_mods[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001646 }
1647
1648 ypr_config(ctx, node->flags, node->exts, NULL);
1649
1650 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1651 if (llist->max) {
1652 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1653 } else {
1654 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1655 }
1656
1657 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1658
1659 ypr_status(ctx, node->flags, node->exts, NULL);
1660 ypr_description(ctx, node->dsc, node->exts, NULL);
1661 ypr_reference(ctx, node->ref, node->exts, NULL);
1662
1663 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001664 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001665}
1666
1667static void
1668yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001669{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001670 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001671 int flag = 0;
1672 struct lysp_node *child;
1673 struct lysp_node_list *list = (struct lysp_node_list *)node;
1674
Radek Krejci693262f2019-04-29 15:23:20 +02001675 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001676
1677 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001678 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001679 }
1680 if (list->key) {
1681 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001682 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001683 }
1684 LY_ARRAY_FOR(list->uniques, u) {
1685 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001686 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001687 }
1688
Radek Krejci693262f2019-04-29 15:23:20 +02001689 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001690
1691 if (list->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001692 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001693 }
1694 if (list->flags & LYS_SET_MAX) {
1695 if (list->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001696 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001697 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001698 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001699 }
1700 }
1701
1702 if (list->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001703 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001704 }
1705
Radek Krejci693262f2019-04-29 15:23:20 +02001706 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001707 ypr_description(ctx, node->dsc, node->exts, NULL);
1708 ypr_reference(ctx, node->ref, node->exts, NULL);
1709
1710 LY_ARRAY_FOR(list->typedefs, u) {
1711 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001712 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001713 }
1714
1715 LY_ARRAY_FOR(list->groupings, u) {
1716 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001717 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001718 }
1719
1720 LY_LIST_FOR(list->child, child) {
1721 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001722 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001723 }
1724
1725 LY_ARRAY_FOR(list->actions, u) {
1726 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001727 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001728 }
1729
1730 LY_ARRAY_FOR(list->notifs, u) {
1731 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001732 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001733 }
1734
1735 LEVEL--;
1736 ypr_close(ctx, flag);
1737}
1738
1739static void
Radek Krejci693262f2019-04-29 15:23:20 +02001740yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1741{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001742 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001743 int flag = 0;
1744 struct lysc_node *child;
1745 struct lysc_node_list *list = (struct lysc_node_list *)node;
1746
1747 yprc_node_common1(ctx, node, &flag);
1748
1749 LY_ARRAY_FOR(list->musts, u) {
1750 yprc_must(ctx, &list->musts[u], NULL);
1751 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001752 if (!(list->flags & LYS_KEYLESS)) {
Radek Krejci693262f2019-04-29 15:23:20 +02001753 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +02001754 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001755 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 +02001756 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001757 }
Michal Vasko5233e962020-08-14 14:26:20 +02001758 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001759 }
1760 LY_ARRAY_FOR(list->uniques, u) {
1761 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +02001762 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001763 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001764 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001765 }
1766 ypr_close(ctx, 0);
1767 }
1768
1769 ypr_config(ctx, node->flags, node->exts, NULL);
1770
1771 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1772 if (list->max) {
1773 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1774 } else {
1775 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1776 }
1777
1778 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1779
1780 ypr_status(ctx, node->flags, node->exts, NULL);
1781 ypr_description(ctx, node->dsc, node->exts, NULL);
1782 ypr_reference(ctx, node->ref, node->exts, NULL);
1783
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001784 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001785 LY_LIST_FOR(list->child, child) {
1786 ypr_open(ctx->out, &flag);
1787 yprc_node(ctx, child);
1788 }
Radek Krejci693262f2019-04-29 15:23:20 +02001789
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001790 LY_ARRAY_FOR(list->actions, u) {
1791 ypr_open(ctx->out, &flag);
1792 yprc_action(ctx, &list->actions[u]);
1793 }
Radek Krejci693262f2019-04-29 15:23:20 +02001794
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001795 LY_ARRAY_FOR(list->notifs, u) {
1796 ypr_open(ctx->out, &flag);
1797 yprc_notification(ctx, &list->notifs[u]);
1798 }
Radek Krejci693262f2019-04-29 15:23:20 +02001799 }
1800
1801 LEVEL--;
1802 ypr_close(ctx, flag);
1803}
1804
1805static void
1806yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001807{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001808 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001809 int flag = 0;
1810
Michal Vasko5233e962020-08-14 14:26:20 +02001811 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001812 LEVEL++;
1813
Radek Krejci693262f2019-04-29 15:23:20 +02001814 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1815 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001816
1817 LY_ARRAY_FOR(refine->musts, u) {
1818 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001819 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001820 }
1821
1822 if (refine->presence) {
1823 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001824 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001825 }
1826
1827 LY_ARRAY_FOR(refine->dflts, u) {
1828 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001829 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001830 }
1831
Radek Krejci693262f2019-04-29 15:23:20 +02001832 ypr_config(ctx, refine->flags, refine->exts, &flag);
1833 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001834
1835 if (refine->flags & LYS_SET_MIN) {
1836 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001837 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001838 }
1839 if (refine->flags & LYS_SET_MAX) {
1840 ypr_open(ctx->out, &flag);
1841 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001842 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001843 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001844 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001845 }
1846 }
1847
1848 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1849 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1850
1851 LEVEL--;
1852 ypr_close(ctx, flag);
1853}
1854
1855static void
Radek Krejci693262f2019-04-29 15:23:20 +02001856yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001857{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001858 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001859 struct lysp_node *child;
1860
Michal Vasko5233e962020-08-14 14:26:20 +02001861 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001862 LEVEL++;
1863
Radek Krejci693262f2019-04-29 15:23:20 +02001864 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1865 yprp_when(ctx, aug->when, NULL);
1866 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1867 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001868 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1869 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1870
1871 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001872 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001873 }
1874
1875 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001876 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877 }
1878
1879 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001880 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001881 }
1882
1883 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001884 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001885}
1886
Radek Krejcid3ca0632019-04-16 16:54:54 +02001887static void
Radek Krejci693262f2019-04-29 15:23:20 +02001888yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001889{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001890 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001891 int flag = 0;
1892 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1893
Radek Krejci693262f2019-04-29 15:23:20 +02001894 yprp_node_common1(ctx, node, &flag);
1895 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001896
1897 LY_ARRAY_FOR(uses->refines, u) {
1898 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001899 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001900 }
1901
1902 LY_ARRAY_FOR(uses->augments, u) {
1903 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001904 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001905 }
1906
1907 LEVEL--;
1908 ypr_close(ctx, flag);
1909}
1910
1911static void
Radek Krejci693262f2019-04-29 15:23:20 +02001912yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001913{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001914 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001915 int flag = 0;
1916 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1917
Radek Krejci693262f2019-04-29 15:23:20 +02001918 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001919
1920 LY_ARRAY_FOR(any->musts, u) {
1921 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001922 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001923 }
1924
Radek Krejci693262f2019-04-29 15:23:20 +02001925 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001926
1927 LEVEL--;
1928 ypr_close(ctx, flag);
1929}
1930
1931static void
Radek Krejci693262f2019-04-29 15:23:20 +02001932yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001933{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001934 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001935 int flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001936 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001937
Radek Krejci693262f2019-04-29 15:23:20 +02001938 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001939
Radek Krejci693262f2019-04-29 15:23:20 +02001940 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001941 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001942 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001943 }
1944
Radek Krejci693262f2019-04-29 15:23:20 +02001945 yprc_node_common2(ctx, node, &flag);
1946
Radek Krejcid3ca0632019-04-16 16:54:54 +02001947 LEVEL--;
1948 ypr_close(ctx, flag);
1949}
1950
1951static void
Radek Krejci693262f2019-04-29 15:23:20 +02001952yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001953{
1954 switch (node->nodetype) {
1955 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001956 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001957 break;
1958 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001959 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001960 break;
1961 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001962 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001963 break;
1964 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001965 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001966 break;
1967 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001968 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001969 break;
1970 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001971 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001972 break;
1973 case LYS_ANYXML:
1974 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001975 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001976 break;
1977 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001978 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001979 break;
1980 default:
1981 break;
1982 }
1983}
1984
1985static void
Radek Krejci693262f2019-04-29 15:23:20 +02001986yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
1987{
1988 switch (node->nodetype) {
1989 case LYS_CONTAINER:
1990 yprc_container(ctx, node);
1991 break;
1992 case LYS_CHOICE:
1993 yprc_choice(ctx, node);
1994 break;
1995 case LYS_LEAF:
1996 yprc_leaf(ctx, node);
1997 break;
1998 case LYS_LEAFLIST:
1999 yprc_leaflist(ctx, node);
2000 break;
2001 case LYS_LIST:
2002 yprc_list(ctx, node);
2003 break;
2004 case LYS_ANYXML:
2005 case LYS_ANYDATA:
2006 yprc_anydata(ctx, node);
2007 break;
2008 default:
2009 break;
2010 }
2011}
2012
2013static void
2014yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002015{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002016 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002017 struct lysp_deviate_add *add;
2018 struct lysp_deviate_rpl *rpl;
2019 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08002020 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002021
Michal Vasko5233e962020-08-14 14:26:20 +02002022 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002023 LEVEL++;
2024
Radek Krejci693262f2019-04-29 15:23:20 +02002025 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002026 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
2027 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
2028
fredgan2b11ddb2019-10-21 11:03:39 +08002029 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02002030 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08002031 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
2032 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002033 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002034 LEVEL++;
2035
fredgan2b11ddb2019-10-21 11:03:39 +08002036 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002037 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002038 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002039 continue;
2040 }
fredgan2b11ddb2019-10-21 11:03:39 +08002041 } else if (elem->mod == LYS_DEV_ADD) {
2042 add = (struct lysp_deviate_add*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002043 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002044 LEVEL++;
2045
Radek Krejci693262f2019-04-29 15:23:20 +02002046 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
2047 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002048 LY_ARRAY_FOR(add->musts, u) {
2049 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002050 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002051 LY_ARRAY_FOR(add->uniques, u) {
2052 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002053 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002054 LY_ARRAY_FOR(add->dflts, u) {
2055 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002056 }
Radek Krejci693262f2019-04-29 15:23:20 +02002057 ypr_config(ctx, add->flags, add->exts, NULL);
2058 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002059 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002060 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002061 }
2062 if (add->flags & LYS_SET_MAX) {
2063 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002064 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002065 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002066 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002067 }
2068 }
fredgan2b11ddb2019-10-21 11:03:39 +08002069 } else if (elem->mod == LYS_DEV_REPLACE) {
2070 rpl = (struct lysp_deviate_rpl*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002071 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002072 LEVEL++;
2073
Radek Krejci693262f2019-04-29 15:23:20 +02002074 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002075 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002076 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002077 }
Radek Krejci693262f2019-04-29 15:23:20 +02002078 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
2079 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
2080 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2081 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002082 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002083 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002084 }
2085 if (rpl->flags & LYS_SET_MAX) {
2086 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002087 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002088 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002089 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002090 }
2091 }
fredgan2b11ddb2019-10-21 11:03:39 +08002092 } else if (elem->mod == LYS_DEV_DELETE) {
2093 del = (struct lysp_deviate_del*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002094 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002095 LEVEL++;
2096
Radek Krejci693262f2019-04-29 15:23:20 +02002097 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2098 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002099 LY_ARRAY_FOR(del->musts, u) {
2100 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002101 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002102 LY_ARRAY_FOR(del->uniques, u) {
2103 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002104 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002105 LY_ARRAY_FOR(del->dflts, u) {
2106 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002107 }
2108 }
2109
2110 LEVEL--;
2111 ypr_close(ctx, 1);
2112 }
2113
2114 LEVEL--;
2115 ypr_close(ctx, 1);
2116}
2117
Michal Vasko7c8439f2020-08-05 13:25:19 +02002118static void
2119yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002120{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002121 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002122
Radek Krejcid3ca0632019-04-16 16:54:54 +02002123 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002124 ly_print_(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002125 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002126 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2127 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002128 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002129 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002130 }
Radek Krejci693262f2019-04-29 15:23:20 +02002131 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2132 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002133 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002134 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002135 }
2136 LY_ARRAY_FOR(modp->includes, u) {
2137 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 +02002138 ly_print_(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002139 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002140 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002141 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002142 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002143 }
Radek Krejci693262f2019-04-29 15:23:20 +02002144 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2145 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002146 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002147 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002148 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002149 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002150 }
2151 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002152}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002153
Michal Vasko7c8439f2020-08-05 13:25:19 +02002154static void
2155yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2156{
2157 LY_ARRAY_COUNT_TYPE u;
2158 struct lysp_node *data;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002159
Radek Krejcid3ca0632019-04-16 16:54:54 +02002160 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002161 ly_print_(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002162 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002163 }
2164 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002165 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002166 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002167 }
2168
2169 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002170 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002171 }
2172
2173 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002174 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002175 }
2176
2177 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002178 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002179 }
2180
2181 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002182 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002183 }
2184
2185 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002186 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002187 }
2188
2189 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002190 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002191 }
2192
2193 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002194 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002195 }
2196
2197 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002198 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002199 }
2200
2201 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002202 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002203 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002204}
2205
2206LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02002207yang_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 +02002208{
2209 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02002210 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 +02002211
Michal Vasko5233e962020-08-14 14:26:20 +02002212 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002213 LEVEL++;
2214
2215 /* module-header-stmts */
2216 if (module->version) {
2217 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
2218 }
2219
2220 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2221 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2222
2223 /* linkage-stmts (import/include) */
2224 yang_print_parsed_linkage(ctx, modp);
2225
2226 /* meta-stmts */
2227 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002228 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002229 }
2230 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2231 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2232 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2233 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2234
2235 /* revision-stmts */
2236 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002237 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002238 }
2239 LY_ARRAY_FOR(modp->revs, u) {
2240 yprp_revision(ctx, &modp->revs[u]);
2241 }
2242 /* body-stmts */
2243 yang_print_parsed_body(ctx, modp);
2244
2245 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002246 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002247 ly_print_flush(out);
2248
2249 return LY_SUCCESS;
2250}
2251
2252static void
2253yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2254{
Michal Vasko5233e962020-08-14 14:26:20 +02002255 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->belongsto);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002256 LEVEL++;
2257 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
2258 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2259 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002260 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002261}
2262
2263LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02002264yang_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 +02002265{
2266 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02002267 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 +02002268
Michal Vasko5233e962020-08-14 14:26:20 +02002269 ly_print_(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002270 LEVEL++;
2271
2272 /* submodule-header-stmts */
2273 if (submodp->version) {
2274 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
2275 }
2276
2277 yprp_belongsto(ctx, submodp);
2278
2279 /* linkage-stmts (import/include) */
2280 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2281
2282 /* meta-stmts */
2283 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002284 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002285 }
2286 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2287 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
2288 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2289 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
2290
2291 /* revision-stmts */
2292 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002293 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002294 }
2295 LY_ARRAY_FOR(submodp->revs, u) {
2296 yprp_revision(ctx, &submodp->revs[u]);
2297 }
2298 /* body-stmts */
2299 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002300
2301 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002302 ly_print_(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002303 ly_print_flush(out);
2304
2305 return LY_SUCCESS;
2306}
2307
2308LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002309yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002310{
Radek Krejci5536d282020-08-04 23:27:44 +02002311 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 +08002312
2313 yprc_node(ctx, node);
2314
2315 ly_print_flush(out);
2316 return LY_SUCCESS;
2317}
2318
2319LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002320yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002321{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002322 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002323 struct lysc_node *data;
2324 struct lysc_module *modc = module->compiled;
Radek Krejci5536d282020-08-04 23:27:44 +02002325 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002326
Michal Vasko5233e962020-08-14 14:26:20 +02002327 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002328 LEVEL++;
2329
Radek Krejci693262f2019-04-29 15:23:20 +02002330 /* module-header-stmts */
2331 if (module->version) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002332 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 +02002333 }
2334 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2335 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2336
Michal Vasko7c8439f2020-08-05 13:25:19 +02002337 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002338
2339 /* meta-stmts */
2340 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002341 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002342 }
2343 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2344 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2345 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2346 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2347
2348 /* revision-stmts */
2349 if (module->revision) {
Michal Vasko5233e962020-08-14 14:26:20 +02002350 ly_print_(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002351 }
2352
2353 /* body-stmts */
2354 if (modc->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002355 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002356 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2357 }
2358
2359 LY_ARRAY_FOR(modc->features, u) {
2360 yprc_feature(ctx, &modc->features[u]);
2361 }
2362
2363 LY_ARRAY_FOR(modc->identities, u) {
2364 yprc_identity(ctx, &modc->identities[u]);
2365 }
2366
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08002367 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002368 LY_LIST_FOR(modc->data, data) {
2369 yprc_node(ctx, data);
2370 }
Radek Krejci693262f2019-04-29 15:23:20 +02002371
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002372 LY_ARRAY_FOR(modc->rpcs, u) {
2373 yprc_action(ctx, &modc->rpcs[u]);
2374 }
Radek Krejci693262f2019-04-29 15:23:20 +02002375
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002376 LY_ARRAY_FOR(modc->notifs, u) {
2377 yprc_notification(ctx, &modc->notifs[u]);
2378 }
Radek Krejci693262f2019-04-29 15:23:20 +02002379 }
2380
2381 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002382 ly_print_(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002383 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002384
2385 return LY_SUCCESS;
2386}