blob: a50bd92bbd8e25b0dfd132473451fb3167256178 [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,
Radek Krejci0f969882020-08-21 16:56:47 +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
Radek Krejcif56e2a42019-09-09 14:15:25 +0200247 ypr_open(ctx->out, flag);
248 argument = NULL;
249 if (ext[u].compiled) {
250 argument = ext[u].compiled->argument;
251 } else {
252 argument = ext[u].argument;
253 }
254 if (argument) {
Michal Vasko5233e962020-08-14 14:26:20 +0200255 ly_print_(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200256 ypr_encode(ctx->out, argument, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200257 ly_print_(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200258 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200259 ly_print_(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200260 }
261
262 child_presence = 0;
263 LEVEL++;
264 LY_LIST_FOR(ext[u].child, stmt) {
265 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
266 continue;
267 }
268 if (!child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200269 ly_print_(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200270 child_presence = 1;
271 }
272 yprp_stmt(ctx, stmt);
273 }
274 LEVEL--;
275 if (child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200276 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200277 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200278 ly_print_(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200279 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200280 }
281}
282
Radek Krejci693262f2019-04-29 15:23:20 +0200283/**
284 * @param[in] count Number of extensions to print, 0 to print them all.
285 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200286static void
Radek Krejci693262f2019-04-29 15:23:20 +0200287yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200288 struct lysc_ext_instance *ext, int *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200289{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200290 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200291
292 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200293 count = LY_ARRAY_COUNT(ext);
Radek Krejci693262f2019-04-29 15:23:20 +0200294 }
295 LY_ARRAY_FOR(ext, u) {
296 if (!count) {
297 break;
298 }
299 /* TODO compiled extensions */
300 (void) ctx;
301 (void) substmt;
302 (void) substmt_index;
303 (void) flag;
304
305 count--;
306 }
307}
308
309static void
310ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200311{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200312 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200313 int extflag = 0;
314
315 if (!text) {
316 /* nothing to print */
317 return;
318 }
319
320 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
Michal Vasko5233e962020-08-14 14:26:20 +0200321 ly_print_(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200322 } else {
323 ypr_text(ctx, ext_substmt_info[substmt].name, text,
Radek Krejci0f969882020-08-21 16:56:47 +0200324 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200325 }
326
327 LEVEL++;
328 LY_ARRAY_FOR(ext, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200329 if (((struct lysp_ext_instance*)ext)[u].insubstmt != substmt || ((struct lysp_ext_instance*)ext)[u].insubstmt_index != substmt_index) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200330 continue;
331 }
Radek Krejci693262f2019-04-29 15:23:20 +0200332 if (ctx->schema == YPR_PARSED) {
333 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance*)ext)[u], &extflag, 1);
334 } else {
335 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance*)ext)[u], &extflag, 1);
336 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200337 }
338 LEVEL--;
339 ypr_close(ctx, extflag);
340}
341
342static void
Radek Krejci693262f2019-04-29 15:23:20 +0200343ypr_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 +0200344{
345 char *str;
346
347 if (asprintf(&str, "%u", attr_value) == -1) {
348 LOGMEM(ctx->module->ctx);
349 return;
350 }
351 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200352 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200353 free(str);
354}
355
356static void
Radek Krejci693262f2019-04-29 15:23:20 +0200357ypr_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 +0200358{
359 char *str;
360
361 if (asprintf(&str, "%d", attr_value) == -1) {
362 LOGMEM(ctx->module->ctx);
363 return;
364 }
365 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200366 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200367 free(str);
368}
369
370static void
Radek Krejci693262f2019-04-29 15:23:20 +0200371yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200372{
373 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +0200374 ly_print_(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200375 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200376 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
377 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
378 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200379 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200380 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200381 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200382 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200383 }
384}
385
386static void
Radek Krejci693262f2019-04-29 15:23:20 +0200387ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200388{
389 if (flags & LYS_MAND_MASK) {
390 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200391 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200392 }
393}
394
395static void
Radek Krejci693262f2019-04-29 15:23:20 +0200396ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200397{
398 if (flags & LYS_CONFIG_MASK) {
399 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200400 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200401 }
402}
403
404static void
Radek Krejci693262f2019-04-29 15:23:20 +0200405ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200406{
407 const char *status = NULL;
408
409 if (flags & LYS_STATUS_CURR) {
410 ypr_open(ctx->out, flag);
411 status = "current";
412 } else if (flags & LYS_STATUS_DEPRC) {
413 ypr_open(ctx->out, flag);
414 status = "deprecated";
415 } else if (flags & LYS_STATUS_OBSLT) {
416 ypr_open(ctx->out, flag);
417 status = "obsolete";
418 }
Radek Krejci693262f2019-04-29 15:23:20 +0200419
420 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200421}
422
423static void
Radek Krejci693262f2019-04-29 15:23:20 +0200424ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200425{
426 if (dsc) {
427 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200428 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200429 }
430}
431
432static void
Radek Krejci693262f2019-04-29 15:23:20 +0200433ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200434{
435 if (ref) {
436 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200437 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200438 }
439}
440
441static void
Radek Krejci693262f2019-04-29 15:23:20 +0200442yprp_iffeatures(struct ypr_ctx *ctx, const char **iff, struct lysp_ext_instance *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200443{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200444 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200445 int extflag;
446
447 LY_ARRAY_FOR(iff, u) {
448 ypr_open(ctx->out, flag);
449 extflag = 0;
450
Michal Vasko5233e962020-08-14 14:26:20 +0200451 ly_print_(ctx->out, "%*sif-feature \"%s\"", INDENT, iff[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200452
453 /* extensions */
454 LEVEL++;
455 LY_ARRAY_FOR(exts, u) {
456 if (exts[u].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[u].insubstmt_index != u) {
457 continue;
458 }
Radek Krejci693262f2019-04-29 15:23:20 +0200459 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[u], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200460 }
461 LEVEL--;
462 ypr_close(ctx, extflag);
463 }
464}
465
466static void
Radek Krejci693262f2019-04-29 15:23:20 +0200467yprc_iffeature(struct ypr_ctx *ctx, struct lysc_iffeature *feat, int *index_e, int *index_f)
468{
469 int brackets_flag = *index_e;
470 uint8_t op;
471
472 op = lysc_iff_getop(feat->expr, *index_e);
473 (*index_e)++;
474
475 switch (op) {
476 case LYS_IFF_F:
477 if (ctx->module == feat->features[*index_f]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200478 ly_print_(ctx->out, "%s", feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200479 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200480 ly_print_(ctx->out, "%s:%s", feat->features[*index_f]->module->prefix, feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200481 }
482 (*index_f)++;
483 break;
484 case LYS_IFF_NOT:
Michal Vasko5233e962020-08-14 14:26:20 +0200485 ly_print_(ctx->out, "not ");
Radek Krejci693262f2019-04-29 15:23:20 +0200486 yprc_iffeature(ctx, feat, index_e, index_f);
487 break;
488 case LYS_IFF_AND:
489 if (brackets_flag) {
490 /* AND need brackets only if previous op was not */
491 if (*index_e < 2 || lysc_iff_getop(feat->expr, *index_e - 2) != LYS_IFF_NOT) {
492 brackets_flag = 0;
493 }
494 }
Radek Krejci0f969882020-08-21 16:56:47 +0200495 /* falls through */
Radek Krejci693262f2019-04-29 15:23:20 +0200496 case LYS_IFF_OR:
497 if (brackets_flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200498 ly_print_(ctx->out, "(");
Radek Krejci693262f2019-04-29 15:23:20 +0200499 }
500 yprc_iffeature(ctx, feat, index_e, index_f);
Michal Vasko5233e962020-08-14 14:26:20 +0200501 ly_print_(ctx->out, " %s ", op == LYS_IFF_OR ? "or" : "and");
Radek Krejci693262f2019-04-29 15:23:20 +0200502 yprc_iffeature(ctx, feat, index_e, index_f);
503 if (brackets_flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200504 ly_print_(ctx->out, ")");
Radek Krejci693262f2019-04-29 15:23:20 +0200505 }
506 }
507}
508
509static void
510yprc_iffeatures(struct ypr_ctx *ctx, struct lysc_iffeature *iff, struct lysc_ext_instance *exts, int *flag)
511{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200512 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +0200513 int extflag;
514
515 LY_ARRAY_FOR(iff, u) {
516 int index_e = 0, index_f = 0;
517
518 ypr_open(ctx->out, flag);
519 extflag = 0;
520
Michal Vasko5233e962020-08-14 14:26:20 +0200521 ly_print_(ctx->out, "%*sif-feature \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200522 yprc_iffeature(ctx, iff, &index_e, &index_f);
Michal Vasko5233e962020-08-14 14:26:20 +0200523 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200524
525 /* extensions */
526 LEVEL++;
Radek Krejci334ccc72019-06-12 13:49:29 +0200527 LY_ARRAY_FOR(exts, v) {
528 if (exts[v].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[v].insubstmt_index != u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200529 continue;
530 }
Radek Krejci334ccc72019-06-12 13:49:29 +0200531 yprc_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200532 }
533 LEVEL--;
534 ypr_close(ctx, extflag);
535 }
536}
537
538static void
539yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200540{
541 int flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200542 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200543
Michal Vasko5233e962020-08-14 14:26:20 +0200544 ly_print_(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200545 LEVEL++;
546
547 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200548 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200549 }
550
551 if (ext->argument) {
552 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200553 ly_print_(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200554 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200555 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200556 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200557 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 +0200558 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200559 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200560 }
561 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200562 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200563 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200564 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200565 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200566 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200567 ypr_close(ctx, flag2);
568 }
569
Radek Krejci693262f2019-04-29 15:23:20 +0200570 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200571 ypr_description(ctx, ext->dsc, ext->exts, &flag);
572 ypr_reference(ctx, ext->ref, ext->exts, &flag);
573
574 LEVEL--;
575 ypr_close(ctx, flag);
576}
577
578static void
Radek Krejci693262f2019-04-29 15:23:20 +0200579yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200580{
581 int flag = 0;
582
Michal Vasko5233e962020-08-14 14:26:20 +0200583 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200584 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200585 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
586 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
587 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200588 ypr_description(ctx, feat->dsc, feat->exts, &flag);
589 ypr_reference(ctx, feat->ref, feat->exts, &flag);
590 LEVEL--;
591 ypr_close(ctx, flag);
592}
593
594static void
Radek Krejci693262f2019-04-29 15:23:20 +0200595yprc_feature(struct ypr_ctx *ctx, const struct lysc_feature *feat)
596{
597 int flag = 0;
598
Michal Vasko5233e962020-08-14 14:26:20 +0200599 ly_print_(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200600 LEVEL++;
601 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
602 yprc_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
603 ypr_status(ctx, feat->flags, feat->exts, &flag);
604 ypr_description(ctx, feat->dsc, feat->exts, &flag);
605 ypr_reference(ctx, feat->ref, feat->exts, &flag);
606 LEVEL--;
607 ypr_close(ctx, flag);
608}
609
610static void
611yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200612{
613 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200614 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200615
Michal Vasko5233e962020-08-14 14:26:20 +0200616 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200617 LEVEL++;
618
Radek Krejci693262f2019-04-29 15:23:20 +0200619 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
620 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200621
622 LY_ARRAY_FOR(ident->bases, u) {
623 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200624 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200625 }
626
Radek Krejci693262f2019-04-29 15:23:20 +0200627 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200628 ypr_description(ctx, ident->dsc, ident->exts, &flag);
629 ypr_reference(ctx, ident->ref, ident->exts, &flag);
630
631 LEVEL--;
632 ypr_close(ctx, flag);
633}
634
635static void
Radek Krejci693262f2019-04-29 15:23:20 +0200636yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
637{
638 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200639 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200640
Michal Vasko5233e962020-08-14 14:26:20 +0200641 ly_print_(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200642 LEVEL++;
643
644 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
645 yprc_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
646
647 LY_ARRAY_FOR(ident->derived, u) {
648 ypr_open(ctx->out, &flag);
649 if (ctx->module != ident->derived[u]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200650 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 +0200651 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200652 ly_print_(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200653 }
654 }
655
656 ypr_status(ctx, ident->flags, ident->exts, &flag);
657 ypr_description(ctx, ident->dsc, ident->exts, &flag);
658 ypr_reference(ctx, ident->ref, ident->exts, &flag);
659
660 LEVEL--;
661 ypr_close(ctx, flag);
662}
663
664static void
665yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200666{
667 int inner_flag = 0;
668
669 if (!restr) {
670 return;
671 }
672
673 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200674 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200675 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 +0200676 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200677
678 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200679 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200680 if (restr->arg[0] == 0x15) {
681 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
682 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200683 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200684 }
685 if (restr->emsg) {
686 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200687 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200688 }
689 if (restr->eapptag) {
690 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200691 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200692 }
Radek Krejci693262f2019-04-29 15:23:20 +0200693 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
694 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
695
Radek Krejcid3ca0632019-04-16 16:54:54 +0200696 LEVEL--;
697 ypr_close(ctx, inner_flag);
698}
699
700static void
Radek Krejci693262f2019-04-29 15:23:20 +0200701yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, int *flag)
702{
703 int inner_flag = 0;
704
705 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200706 ly_print_(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200707 ypr_encode(ctx->out, must->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200708 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200709
710 LEVEL++;
711 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
712 if (must->emsg) {
713 ypr_open(ctx->out, &inner_flag);
714 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
715 }
716 if (must->eapptag) {
717 ypr_open(ctx->out, &inner_flag);
718 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
719 }
720 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
721 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
722
723 LEVEL--;
724 ypr_close(ctx, inner_flag);
725}
726
727static void
728yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, int *flag)
729{
730 int inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200731 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200732
Radek Krejci334ccc72019-06-12 13:49:29 +0200733 if (!range) {
734 return;
735 }
736
Radek Krejci693262f2019-04-29 15:23:20 +0200737 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200738 ly_print_(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200739 LY_ARRAY_FOR(range->parts, u) {
740 if (u > 0) {
Michal Vasko5233e962020-08-14 14:26:20 +0200741 ly_print_(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200742 }
743 if (range->parts[u].max_64 == range->parts[u].min_64) {
744 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200745 ly_print_(ctx->out, "%" PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200746 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200747 ly_print_(ctx->out, "%" PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200748 }
749 } else {
750 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200751 ly_print_(ctx->out, "%" PRIu64 "..%" PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200752 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200753 ly_print_(ctx->out, "%" PRId64 "..%" PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200754 }
755 }
756 }
Michal Vasko5233e962020-08-14 14:26:20 +0200757 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200758
759 LEVEL++;
760 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
761 if (range->emsg) {
762 ypr_open(ctx->out, &inner_flag);
763 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
764 }
765 if (range->eapptag) {
766 ypr_open(ctx->out, &inner_flag);
767 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
768 }
769 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
770 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
771
772 LEVEL--;
773 ypr_close(ctx, inner_flag);
774}
775
776static void
777yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, int *flag)
778{
779 int inner_flag = 0;
780
781 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200782 ly_print_(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200783 ypr_encode(ctx->out, pattern->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200784 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200785
786 LEVEL++;
787 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
788 if (pattern->inverted) {
789 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
790 ypr_open(ctx->out, &inner_flag);
791 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
792 }
793 if (pattern->emsg) {
794 ypr_open(ctx->out, &inner_flag);
795 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
796 }
797 if (pattern->eapptag) {
798 ypr_open(ctx->out, &inner_flag);
799 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
800 }
801 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
802 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
803
804 LEVEL--;
805 ypr_close(ctx, inner_flag);
806}
807
808static void
809yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200810{
811 int inner_flag = 0;
812
813 if (!when) {
814 return;
815 }
816 ypr_open(ctx->out, flag);
817
Michal Vasko5233e962020-08-14 14:26:20 +0200818 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200819 ypr_encode(ctx->out, when->cond, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200820 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200821
822 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200823 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200824 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
825 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
826 LEVEL--;
827 ypr_close(ctx, inner_flag);
828}
829
830static void
Radek Krejci693262f2019-04-29 15:23:20 +0200831yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, int *flag)
832{
833 int inner_flag = 0;
834
835 if (!when) {
836 return;
837 }
838 ypr_open(ctx->out, flag);
839
Michal Vasko5233e962020-08-14 14:26:20 +0200840 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200841 ypr_encode(ctx->out, when->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200842 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200843
844 LEVEL++;
845 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
846 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
847 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
848 LEVEL--;
849 ypr_close(ctx, inner_flag);
850}
851
852static void
853yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200854{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200855 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200856 int inner_flag;
857
858 LY_ARRAY_FOR(items, u) {
859 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200860 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200861 ly_print_(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200862 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200863 ly_print_(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200864 ypr_encode(ctx->out, items[u].name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200865 ly_print_(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200866 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200867 inner_flag = 0;
868 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200869 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
870 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200871 if (items[u].flags & LYS_SET_VALUE) {
872 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200873 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200874 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200875 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200876 }
877 }
Radek Krejci693262f2019-04-29 15:23:20 +0200878 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200879 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
880 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
881 LEVEL--;
882 ypr_close(ctx, inner_flag);
883 }
884}
885
886static void
Radek Krejci693262f2019-04-29 15:23:20 +0200887yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200888{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200889 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200890 int flag = 0;
891
Michal Vasko5233e962020-08-14 14:26:20 +0200892 ly_print_(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200893 LEVEL++;
894
Radek Krejci693262f2019-04-29 15:23:20 +0200895 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200896
Radek Krejci693262f2019-04-29 15:23:20 +0200897 yprp_restr(ctx, type->range, "range", &flag);
898 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200899 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200900 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200901 }
Radek Krejci693262f2019-04-29 15:23:20 +0200902 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
903 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200904
905 if (type->path) {
906 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200907 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200908 }
909 if (type->flags & LYS_SET_REQINST) {
910 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200911 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200912 }
913 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200914 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200915 }
916 LY_ARRAY_FOR(type->bases, u) {
917 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200918 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200919 }
920 LY_ARRAY_FOR(type->types, u) {
921 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200922 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200923 }
924
925 LEVEL--;
926 ypr_close(ctx, flag);
927}
928
929static void
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200930yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200931{
932 int dynamic;
933 const char *str;
934
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200935 str = value->realtype->plugin->print(value, LY_PREF_JSON, NULL, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200936 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
937 if (dynamic) {
938 free((void*)str);
939 }
940}
941
942static void
Radek Krejci693262f2019-04-29 15:23:20 +0200943yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
944{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200945 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200946 int flag = 0;
947
Michal Vasko5233e962020-08-14 14:26:20 +0200948 ly_print_(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200949 LEVEL++;
950
951 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200952
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200953 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200954 case LY_TYPE_BINARY: {
955 struct lysc_type_bin *bin = (struct lysc_type_bin*)type;
956 yprc_range(ctx, bin->length, type->basetype, &flag);
957 break;
958 }
959 case LY_TYPE_UINT8:
960 case LY_TYPE_UINT16:
961 case LY_TYPE_UINT32:
962 case LY_TYPE_UINT64:
963 case LY_TYPE_INT8:
964 case LY_TYPE_INT16:
965 case LY_TYPE_INT32:
966 case LY_TYPE_INT64: {
967 struct lysc_type_num *num = (struct lysc_type_num*)type;
968 yprc_range(ctx, num->range, type->basetype, &flag);
969 break;
970 }
971 case LY_TYPE_STRING: {
972 struct lysc_type_str *str = (struct lysc_type_str*)type;
973 yprc_range(ctx, str->length, type->basetype, &flag);
974 LY_ARRAY_FOR(str->patterns, u) {
975 yprc_pattern(ctx, str->patterns[u], &flag);
976 }
977 break;
978 }
979 case LY_TYPE_BITS:
980 case LY_TYPE_ENUM: {
981 /* bits and enums structures are compatible */
982 struct lysc_type_bits *bits = (struct lysc_type_bits*)type;
983 LY_ARRAY_FOR(bits->bits, u) {
984 struct lysc_type_bitenum_item *item = &bits->bits[u];
985 int inner_flag = 0;
986
987 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200988 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200989 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200990 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200991 LEVEL++;
992 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
993 yprc_iffeatures(ctx, item->iffeatures, item->exts, &inner_flag);
994 if (type->basetype == LY_TYPE_BITS) {
995 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
996 } else { /* LY_TYPE_ENUM */
997 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
998 }
999 ypr_status(ctx, item->flags, item->exts, &inner_flag);
1000 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
1001 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
1002 LEVEL--;
1003 ypr_close(ctx, inner_flag);
1004 }
1005 break;
1006 }
1007 case LY_TYPE_BOOL:
1008 case LY_TYPE_EMPTY:
1009 /* nothing to do */
1010 break;
1011 case LY_TYPE_DEC64: {
1012 struct lysc_type_dec *dec = (struct lysc_type_dec*)type;
1013 ypr_open(ctx->out, &flag);
1014 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
1015 yprc_range(ctx, dec->range, dec->basetype, &flag);
1016 break;
1017 }
1018 case LY_TYPE_IDENT: {
1019 struct lysc_type_identityref *ident = (struct lysc_type_identityref*)type;
1020 LY_ARRAY_FOR(ident->bases, u) {
1021 ypr_open(ctx->out, &flag);
1022 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
1023 }
1024 break;
1025 }
1026 case LY_TYPE_INST: {
1027 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid*)type;
1028 ypr_open(ctx->out, &flag);
1029 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
1030 break;
1031 }
1032 case LY_TYPE_LEAFREF: {
1033 struct lysc_type_leafref *lr = (struct lysc_type_leafref*)type;
1034 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +02001035 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001036 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
1037 yprc_type(ctx, lr->realtype);
1038 break;
1039 }
1040 case LY_TYPE_UNION: {
1041 struct lysc_type_union *un = (struct lysc_type_union*)type;
1042 LY_ARRAY_FOR(un->types, u) {
1043 ypr_open(ctx->out, &flag);
1044 yprc_type(ctx, un->types[u]);
1045 }
1046 break;
1047 }
1048 default:
1049 LOGINT(ctx->module->ctx);
1050 }
1051
1052 LEVEL--;
1053 ypr_close(ctx, flag);
1054}
1055
1056static void
1057yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001058{
Michal Vasko5233e962020-08-14 14:26:20 +02001059 ly_print_(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001060 LEVEL++;
1061
Radek Krejci693262f2019-04-29 15:23:20 +02001062 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001063
Radek Krejci693262f2019-04-29 15:23:20 +02001064 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001065
1066 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +02001067 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001068 }
1069 if (tpdf->dflt) {
Radek Krejci693262f2019-04-29 15:23:20 +02001070 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001071 }
1072
Radek Krejci693262f2019-04-29 15:23:20 +02001073 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001074 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
1075 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
1076
1077 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001078 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001079}
1080
Radek Krejci693262f2019-04-29 15:23:20 +02001081static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
1082static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
1083static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001084
1085static void
Radek Krejci693262f2019-04-29 15:23:20 +02001086yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001087{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001088 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001089 int flag = 0;
1090 struct lysp_node *data;
1091
Michal Vasko5233e962020-08-14 14:26:20 +02001092 ly_print_(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001093 LEVEL++;
1094
Radek Krejci693262f2019-04-29 15:23:20 +02001095 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1096 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001097 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1098 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001099
1100 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001101 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001102 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001103 }
1104
1105 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001106 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001107 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001108 }
1109
1110 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001111 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001112 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001113 }
1114
1115 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001116 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001117 }
1118
1119 LEVEL--;
1120 ypr_close(ctx, flag);
1121}
1122
1123static void
Radek Krejci693262f2019-04-29 15:23:20 +02001124yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001125{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001126 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001127 struct lysp_node *data;
1128
1129 if (!inout->nodetype) {
1130 /* nodetype not set -> input/output is empty */
1131 return;
1132 }
1133 ypr_open(ctx->out, flag);
1134
Michal Vasko5233e962020-08-14 14:26:20 +02001135 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001136 LEVEL++;
1137
Radek Krejci693262f2019-04-29 15:23:20 +02001138 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001139 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001140 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001141 }
1142 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001143 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001144 }
1145 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001146 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001147 }
1148
1149 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001150 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001151 }
1152
1153 LEVEL--;
1154 ypr_close(ctx, 1);
1155}
1156
1157static void
Radek Krejci693262f2019-04-29 15:23:20 +02001158yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, int *flag)
1159{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001160 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001161 struct lysc_node *data;
1162
1163 if (!inout->data) {
1164 /* input/output is empty */
1165 return;
1166 }
1167 ypr_open(ctx->out, flag);
1168
Michal Vasko5233e962020-08-14 14:26:20 +02001169 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001170 LEVEL++;
1171
1172 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1173 LY_ARRAY_FOR(inout->musts, u) {
1174 yprc_must(ctx, &inout->musts[u], NULL);
1175 }
1176
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001177 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001178 LY_LIST_FOR(inout->data, data) {
1179 yprc_node(ctx, data);
1180 }
Radek Krejci693262f2019-04-29 15:23:20 +02001181 }
1182
1183 LEVEL--;
1184 ypr_close(ctx, 1);
1185}
1186
1187static void
1188yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001189{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001190 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001191 int flag = 0;
1192 struct lysp_node *data;
1193
Michal Vasko5233e962020-08-14 14:26:20 +02001194 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001195
1196 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001197 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1198 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001199
1200 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001201 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001202 }
Radek Krejci693262f2019-04-29 15:23:20 +02001203 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001204 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1205 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1206
1207 LY_ARRAY_FOR(notif->typedefs, u) {
1208 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001209 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001210 }
1211
1212 LY_ARRAY_FOR(notif->groupings, u) {
1213 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001214 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001215 }
1216
1217 LY_LIST_FOR(notif->data, data) {
1218 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001219 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001220 }
1221
1222 LEVEL--;
1223 ypr_close(ctx, flag);
1224}
1225
1226static void
Radek Krejci693262f2019-04-29 15:23:20 +02001227yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1228{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001229 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001230 int flag = 0;
1231 struct lysc_node *data;
1232
Michal Vasko5233e962020-08-14 14:26:20 +02001233 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001234
1235 LEVEL++;
1236 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1237 yprc_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
1238
1239 LY_ARRAY_FOR(notif->musts, u) {
1240 yprc_must(ctx, &notif->musts[u], &flag);
1241 }
1242 ypr_status(ctx, notif->flags, notif->exts, &flag);
1243 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1244 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1245
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001246 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001247 LY_LIST_FOR(notif->data, data) {
1248 ypr_open(ctx->out, &flag);
1249 yprc_node(ctx, data);
1250 }
Radek Krejci693262f2019-04-29 15:23:20 +02001251 }
1252
1253 LEVEL--;
1254 ypr_close(ctx, flag);
1255}
1256
1257static void
1258yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001259{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001260 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001261 int flag = 0;
1262
Michal Vasko5233e962020-08-14 14:26:20 +02001263 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001264
1265 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001266 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1267 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1268 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001269 ypr_description(ctx, action->dsc, action->exts, &flag);
1270 ypr_reference(ctx, action->ref, action->exts, &flag);
1271
1272 LY_ARRAY_FOR(action->typedefs, u) {
1273 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001274 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001275 }
1276
1277 LY_ARRAY_FOR(action->groupings, u) {
1278 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001279 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001280 }
1281
Radek Krejci693262f2019-04-29 15:23:20 +02001282 yprp_inout(ctx, &action->input, &flag);
1283 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001284
1285 LEVEL--;
1286 ypr_close(ctx, flag);
1287}
1288
1289static void
Radek Krejci693262f2019-04-29 15:23:20 +02001290yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1291{
1292 int flag = 0;
1293
Michal Vasko5233e962020-08-14 14:26:20 +02001294 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001295
1296 LEVEL++;
1297 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1298 yprc_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1299 ypr_status(ctx, action->flags, action->exts, &flag);
1300 ypr_description(ctx, action->dsc, action->exts, &flag);
1301 ypr_reference(ctx, action->ref, action->exts, &flag);
1302
1303 yprc_inout(ctx, action, &action->input, &flag);
1304 yprc_inout(ctx, action, &action->output, &flag);
1305
1306 LEVEL--;
1307 ypr_close(ctx, flag);
1308}
1309
1310static void
1311yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001312{
Michal Vasko5233e962020-08-14 14:26:20 +02001313 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001314 LEVEL++;
1315
Radek Krejci693262f2019-04-29 15:23:20 +02001316 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1317 yprp_when(ctx, node->when, flag);
1318 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001319}
1320
1321static void
Radek Krejci693262f2019-04-29 15:23:20 +02001322yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001323{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001324 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001325
Michal Vasko5233e962020-08-14 14:26:20 +02001326 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001327 LEVEL++;
1328
1329 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1330 LY_ARRAY_FOR(node->when, u) {
1331 yprc_when(ctx, node->when[u], flag);
1332 }
1333 yprc_iffeatures(ctx, node->iffeatures, node->exts, flag);
1334}
1335
1336/* macr oto unify the code */
1337#define YPR_NODE_COMMON2 \
1338 ypr_config(ctx, node->flags, node->exts, flag); \
1339 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1340 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1341 } \
1342 ypr_status(ctx, node->flags, node->exts, flag); \
1343 ypr_description(ctx, node->dsc, node->exts, flag); \
1344 ypr_reference(ctx, node->ref, node->exts, flag)
1345
1346static void
1347yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
1348{
1349 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001350}
1351
1352static void
Radek Krejci693262f2019-04-29 15:23:20 +02001353yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
1354{
1355 YPR_NODE_COMMON2;
1356}
1357
1358#undef YPR_NODE_COMMON2
1359
1360static void
1361yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001362{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001363 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001364 int flag = 0;
1365 struct lysp_node *child;
1366 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1367
Radek Krejci693262f2019-04-29 15:23:20 +02001368 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001369
1370 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001371 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001372 }
1373 if (cont->presence) {
1374 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001375 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001376 }
1377
Radek Krejci693262f2019-04-29 15:23:20 +02001378 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001379
1380 LY_ARRAY_FOR(cont->typedefs, u) {
1381 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001382 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001383 }
1384
1385 LY_ARRAY_FOR(cont->groupings, u) {
1386 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001387 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001388 }
1389
1390 LY_LIST_FOR(cont->child, child) {
1391 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001392 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001393 }
1394
1395 LY_ARRAY_FOR(cont->actions, u) {
1396 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001397 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001398 }
1399
1400 LY_ARRAY_FOR(cont->notifs, u) {
1401 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001402 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001403 }
1404
1405 LEVEL--;
1406 ypr_close(ctx, flag);
1407}
1408
1409static void
Radek Krejci693262f2019-04-29 15:23:20 +02001410yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1411{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001412 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001413 int flag = 0;
1414 struct lysc_node *child;
1415 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1416
1417 yprc_node_common1(ctx, node, &flag);
1418
1419 LY_ARRAY_FOR(cont->musts, u) {
1420 yprc_must(ctx, &cont->musts[u], &flag);
1421 }
1422 if (cont->flags & LYS_PRESENCE) {
1423 ypr_open(ctx->out, &flag);
1424 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1425 }
1426
1427 yprc_node_common2(ctx, node, &flag);
1428
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001429 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001430 LY_LIST_FOR(cont->child, child) {
1431 ypr_open(ctx->out, &flag);
1432 yprc_node(ctx, child);
1433 }
Radek Krejci693262f2019-04-29 15:23:20 +02001434
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001435 LY_ARRAY_FOR(cont->actions, u) {
1436 ypr_open(ctx->out, &flag);
1437 yprc_action(ctx, &cont->actions[u]);
1438 }
Radek Krejci693262f2019-04-29 15:23:20 +02001439
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001440 LY_ARRAY_FOR(cont->notifs, u) {
1441 ypr_open(ctx->out, &flag);
1442 yprc_notification(ctx, &cont->notifs[u]);
1443 }
Radek Krejci693262f2019-04-29 15:23:20 +02001444 }
1445
1446 LEVEL--;
1447 ypr_close(ctx, flag);
1448}
1449
1450static void
1451yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1452{
1453 int flag = 0;
1454 struct lysp_node *child;
1455 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1456
1457 yprp_node_common1(ctx, node, &flag);
1458 yprp_node_common2(ctx, node, &flag);
1459
1460 LY_LIST_FOR(cas->child, child) {
1461 ypr_open(ctx->out, &flag);
1462 yprp_node(ctx, child);
1463 }
1464
1465 LEVEL--;
1466 ypr_close(ctx, flag);
1467}
1468
1469static void
1470yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1471{
1472 int flag = 0;
1473 struct lysc_node *child;
1474
1475 yprc_node_common1(ctx, (struct lysc_node*)cs, &flag);
1476 yprc_node_common2(ctx, (struct lysc_node*)cs, &flag);
1477
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001478 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001479 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
1480 ypr_open(ctx->out, &flag);
1481 yprc_node(ctx, child);
1482 }
Radek Krejci693262f2019-04-29 15:23:20 +02001483 }
1484
1485 LEVEL--;
1486 ypr_close(ctx, flag);
1487}
1488
1489static void
1490yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001491{
1492 int flag = 0;
1493 struct lysp_node *child;
1494 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1495
Radek Krejci693262f2019-04-29 15:23:20 +02001496 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001497
1498 if (choice->dflt) {
1499 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001500 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001501 }
1502
Radek Krejci693262f2019-04-29 15:23:20 +02001503 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001504
1505 LY_LIST_FOR(choice->child, child) {
1506 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001507 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001508 }
1509
1510 LEVEL--;
1511 ypr_close(ctx, flag);
1512}
1513
1514static void
Radek Krejci693262f2019-04-29 15:23:20 +02001515yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1516{
1517 int flag = 0;
1518 struct lysc_node_case *cs;
1519 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1520
1521 yprc_node_common1(ctx, node, &flag);
1522
1523 if (choice->dflt) {
1524 ypr_open(ctx->out, &flag);
1525 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1526 }
1527
1528 yprc_node_common2(ctx, node, &flag);
1529
1530 for (cs = choice->cases; cs; cs = (struct lysc_node_case*)cs->next) {
1531 ypr_open(ctx->out, &flag);
1532 yprc_case(ctx, cs);
1533 }
1534
1535 LEVEL--;
1536 ypr_close(ctx, flag);
1537}
1538
1539static void
1540yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001541{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001542 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001543 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1544
Radek Krejci693262f2019-04-29 15:23:20 +02001545 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001546
Radek Krejci693262f2019-04-29 15:23:20 +02001547 yprp_type(ctx, &leaf->type);
1548 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001549 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001550 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001551 }
Radek Krejci693262f2019-04-29 15:23:20 +02001552 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001553
Radek Krejci693262f2019-04-29 15:23:20 +02001554 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001555
1556 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001557 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001558}
1559
1560static void
Radek Krejci693262f2019-04-29 15:23:20 +02001561yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1562{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001563 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001564 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1565
1566 yprc_node_common1(ctx, node, NULL);
1567
1568 yprc_type(ctx, leaf->type);
1569 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1570 LY_ARRAY_FOR(leaf->musts, u) {
1571 yprc_must(ctx, &leaf->musts[u], NULL);
1572 }
Radek Krejcia1911222019-07-22 17:24:50 +02001573
1574 if (leaf->dflt) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001575 yprc_dflt_value(ctx, leaf->dflt, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001576 }
Radek Krejci693262f2019-04-29 15:23:20 +02001577
1578 yprc_node_common2(ctx, node, NULL);
1579
1580 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001581 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001582}
1583
1584static void
1585yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001586{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001587 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001588 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1589
Radek Krejci693262f2019-04-29 15:23:20 +02001590 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001591
Radek Krejci693262f2019-04-29 15:23:20 +02001592 yprp_type(ctx, &llist->type);
1593 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001594 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001595 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001596 }
1597 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001598 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001599 }
1600
Radek Krejci693262f2019-04-29 15:23:20 +02001601 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001602
1603 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001604 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001605 }
1606 if (llist->flags & LYS_SET_MAX) {
1607 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001608 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001609 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001610 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001611 }
1612 }
1613
1614 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001615 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001616 }
1617
Radek Krejci693262f2019-04-29 15:23:20 +02001618 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001619 ypr_description(ctx, node->dsc, node->exts, NULL);
1620 ypr_reference(ctx, node->ref, node->exts, NULL);
1621
1622 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001623 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001624}
1625
1626static void
Radek Krejci693262f2019-04-29 15:23:20 +02001627yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1628{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001629 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001630 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1631
1632 yprc_node_common1(ctx, node, NULL);
1633
1634 yprc_type(ctx, llist->type);
1635 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1636 LY_ARRAY_FOR(llist->musts, u) {
1637 yprc_must(ctx, &llist->musts[u], NULL);
1638 }
1639 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001640 yprc_dflt_value(ctx, llist->dflts[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001641 }
1642
1643 ypr_config(ctx, node->flags, node->exts, NULL);
1644
1645 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1646 if (llist->max) {
1647 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1648 } else {
1649 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1650 }
1651
1652 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1653
1654 ypr_status(ctx, node->flags, node->exts, NULL);
1655 ypr_description(ctx, node->dsc, node->exts, NULL);
1656 ypr_reference(ctx, node->ref, node->exts, NULL);
1657
1658 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001659 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001660}
1661
1662static void
1663yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001664{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001665 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001666 int flag = 0;
1667 struct lysp_node *child;
1668 struct lysp_node_list *list = (struct lysp_node_list *)node;
1669
Radek Krejci693262f2019-04-29 15:23:20 +02001670 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001671
1672 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001673 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001674 }
1675 if (list->key) {
1676 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001677 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001678 }
1679 LY_ARRAY_FOR(list->uniques, u) {
1680 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001681 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001682 }
1683
Radek Krejci693262f2019-04-29 15:23:20 +02001684 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001685
1686 if (list->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001687 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001688 }
1689 if (list->flags & LYS_SET_MAX) {
1690 if (list->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001691 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001692 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001693 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001694 }
1695 }
1696
1697 if (list->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001698 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001699 }
1700
Radek Krejci693262f2019-04-29 15:23:20 +02001701 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001702 ypr_description(ctx, node->dsc, node->exts, NULL);
1703 ypr_reference(ctx, node->ref, node->exts, NULL);
1704
1705 LY_ARRAY_FOR(list->typedefs, u) {
1706 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001707 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001708 }
1709
1710 LY_ARRAY_FOR(list->groupings, u) {
1711 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001712 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001713 }
1714
1715 LY_LIST_FOR(list->child, child) {
1716 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001717 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001718 }
1719
1720 LY_ARRAY_FOR(list->actions, u) {
1721 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001722 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001723 }
1724
1725 LY_ARRAY_FOR(list->notifs, u) {
1726 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001727 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001728 }
1729
1730 LEVEL--;
1731 ypr_close(ctx, flag);
1732}
1733
1734static void
Radek Krejci693262f2019-04-29 15:23:20 +02001735yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1736{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001737 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001738 int flag = 0;
1739 struct lysc_node *child;
1740 struct lysc_node_list *list = (struct lysc_node_list *)node;
1741
1742 yprc_node_common1(ctx, node, &flag);
1743
1744 LY_ARRAY_FOR(list->musts, u) {
1745 yprc_must(ctx, &list->musts[u], NULL);
1746 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001747 if (!(list->flags & LYS_KEYLESS)) {
Radek Krejci693262f2019-04-29 15:23:20 +02001748 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +02001749 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001750 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 +02001751 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001752 }
Michal Vasko5233e962020-08-14 14:26:20 +02001753 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001754 }
1755 LY_ARRAY_FOR(list->uniques, u) {
1756 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +02001757 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001758 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001759 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001760 }
1761 ypr_close(ctx, 0);
1762 }
1763
1764 ypr_config(ctx, node->flags, node->exts, NULL);
1765
1766 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1767 if (list->max) {
1768 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1769 } else {
1770 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1771 }
1772
1773 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1774
1775 ypr_status(ctx, node->flags, node->exts, NULL);
1776 ypr_description(ctx, node->dsc, node->exts, NULL);
1777 ypr_reference(ctx, node->ref, node->exts, NULL);
1778
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001779 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001780 LY_LIST_FOR(list->child, child) {
1781 ypr_open(ctx->out, &flag);
1782 yprc_node(ctx, child);
1783 }
Radek Krejci693262f2019-04-29 15:23:20 +02001784
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001785 LY_ARRAY_FOR(list->actions, u) {
1786 ypr_open(ctx->out, &flag);
1787 yprc_action(ctx, &list->actions[u]);
1788 }
Radek Krejci693262f2019-04-29 15:23:20 +02001789
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001790 LY_ARRAY_FOR(list->notifs, u) {
1791 ypr_open(ctx->out, &flag);
1792 yprc_notification(ctx, &list->notifs[u]);
1793 }
Radek Krejci693262f2019-04-29 15:23:20 +02001794 }
1795
1796 LEVEL--;
1797 ypr_close(ctx, flag);
1798}
1799
1800static void
1801yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001802{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001803 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001804 int flag = 0;
1805
Michal Vasko5233e962020-08-14 14:26:20 +02001806 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001807 LEVEL++;
1808
Radek Krejci693262f2019-04-29 15:23:20 +02001809 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1810 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001811
1812 LY_ARRAY_FOR(refine->musts, u) {
1813 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001814 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001815 }
1816
1817 if (refine->presence) {
1818 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001819 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001820 }
1821
1822 LY_ARRAY_FOR(refine->dflts, u) {
1823 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001824 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001825 }
1826
Radek Krejci693262f2019-04-29 15:23:20 +02001827 ypr_config(ctx, refine->flags, refine->exts, &flag);
1828 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001829
1830 if (refine->flags & LYS_SET_MIN) {
1831 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001832 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001833 }
1834 if (refine->flags & LYS_SET_MAX) {
1835 ypr_open(ctx->out, &flag);
1836 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001837 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001838 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001839 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001840 }
1841 }
1842
1843 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1844 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1845
1846 LEVEL--;
1847 ypr_close(ctx, flag);
1848}
1849
1850static void
Radek Krejci693262f2019-04-29 15:23:20 +02001851yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001852{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001853 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001854 struct lysp_node *child;
1855
Michal Vasko5233e962020-08-14 14:26:20 +02001856 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001857 LEVEL++;
1858
Radek Krejci693262f2019-04-29 15:23:20 +02001859 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1860 yprp_when(ctx, aug->when, NULL);
1861 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1862 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001863 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1864 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1865
1866 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001867 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001868 }
1869
1870 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001871 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001872 }
1873
1874 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001875 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001876 }
1877
1878 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001879 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001880}
1881
Radek Krejcid3ca0632019-04-16 16:54:54 +02001882static void
Radek Krejci693262f2019-04-29 15:23:20 +02001883yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001884{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001885 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001886 int flag = 0;
1887 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1888
Radek Krejci693262f2019-04-29 15:23:20 +02001889 yprp_node_common1(ctx, node, &flag);
1890 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001891
1892 LY_ARRAY_FOR(uses->refines, u) {
1893 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001894 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001895 }
1896
1897 LY_ARRAY_FOR(uses->augments, u) {
1898 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001899 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001900 }
1901
1902 LEVEL--;
1903 ypr_close(ctx, flag);
1904}
1905
1906static void
Radek Krejci693262f2019-04-29 15:23:20 +02001907yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001908{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001909 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001910 int flag = 0;
1911 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1912
Radek Krejci693262f2019-04-29 15:23:20 +02001913 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001914
1915 LY_ARRAY_FOR(any->musts, u) {
1916 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001917 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001918 }
1919
Radek Krejci693262f2019-04-29 15:23:20 +02001920 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001921
1922 LEVEL--;
1923 ypr_close(ctx, flag);
1924}
1925
1926static void
Radek Krejci693262f2019-04-29 15:23:20 +02001927yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001928{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001929 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001930 int flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001931 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001932
Radek Krejci693262f2019-04-29 15:23:20 +02001933 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001934
Radek Krejci693262f2019-04-29 15:23:20 +02001935 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001936 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001937 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001938 }
1939
Radek Krejci693262f2019-04-29 15:23:20 +02001940 yprc_node_common2(ctx, node, &flag);
1941
Radek Krejcid3ca0632019-04-16 16:54:54 +02001942 LEVEL--;
1943 ypr_close(ctx, flag);
1944}
1945
1946static void
Radek Krejci693262f2019-04-29 15:23:20 +02001947yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001948{
1949 switch (node->nodetype) {
1950 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001951 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001952 break;
1953 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001954 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001955 break;
1956 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001957 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001958 break;
1959 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001960 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001961 break;
1962 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001963 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001964 break;
1965 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001966 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001967 break;
1968 case LYS_ANYXML:
1969 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001970 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001971 break;
1972 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001973 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001974 break;
1975 default:
1976 break;
1977 }
1978}
1979
1980static void
Radek Krejci693262f2019-04-29 15:23:20 +02001981yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
1982{
1983 switch (node->nodetype) {
1984 case LYS_CONTAINER:
1985 yprc_container(ctx, node);
1986 break;
1987 case LYS_CHOICE:
1988 yprc_choice(ctx, node);
1989 break;
1990 case LYS_LEAF:
1991 yprc_leaf(ctx, node);
1992 break;
1993 case LYS_LEAFLIST:
1994 yprc_leaflist(ctx, node);
1995 break;
1996 case LYS_LIST:
1997 yprc_list(ctx, node);
1998 break;
1999 case LYS_ANYXML:
2000 case LYS_ANYDATA:
2001 yprc_anydata(ctx, node);
2002 break;
2003 default:
2004 break;
2005 }
2006}
2007
2008static void
2009yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002010{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002011 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002012 struct lysp_deviate_add *add;
2013 struct lysp_deviate_rpl *rpl;
2014 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08002015 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002016
Michal Vasko5233e962020-08-14 14:26:20 +02002017 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002018 LEVEL++;
2019
Radek Krejci693262f2019-04-29 15:23:20 +02002020 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002021 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
2022 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
2023
fredgan2b11ddb2019-10-21 11:03:39 +08002024 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02002025 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08002026 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
2027 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002028 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002029 LEVEL++;
2030
fredgan2b11ddb2019-10-21 11:03:39 +08002031 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002032 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002033 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002034 continue;
2035 }
fredgan2b11ddb2019-10-21 11:03:39 +08002036 } else if (elem->mod == LYS_DEV_ADD) {
2037 add = (struct lysp_deviate_add*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002038 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002039 LEVEL++;
2040
Radek Krejci693262f2019-04-29 15:23:20 +02002041 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
2042 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002043 LY_ARRAY_FOR(add->musts, u) {
2044 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002046 LY_ARRAY_FOR(add->uniques, u) {
2047 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002048 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002049 LY_ARRAY_FOR(add->dflts, u) {
2050 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002051 }
Radek Krejci693262f2019-04-29 15:23:20 +02002052 ypr_config(ctx, add->flags, add->exts, NULL);
2053 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002054 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002055 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002056 }
2057 if (add->flags & LYS_SET_MAX) {
2058 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002059 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002060 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002061 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002062 }
2063 }
fredgan2b11ddb2019-10-21 11:03:39 +08002064 } else if (elem->mod == LYS_DEV_REPLACE) {
2065 rpl = (struct lysp_deviate_rpl*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002066 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002067 LEVEL++;
2068
Radek Krejci693262f2019-04-29 15:23:20 +02002069 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002070 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002071 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002072 }
Radek Krejci693262f2019-04-29 15:23:20 +02002073 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
2074 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
2075 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2076 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002077 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002078 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002079 }
2080 if (rpl->flags & LYS_SET_MAX) {
2081 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002082 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002083 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002084 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002085 }
2086 }
fredgan2b11ddb2019-10-21 11:03:39 +08002087 } else if (elem->mod == LYS_DEV_DELETE) {
2088 del = (struct lysp_deviate_del*)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002089 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002090 LEVEL++;
2091
Radek Krejci693262f2019-04-29 15:23:20 +02002092 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2093 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002094 LY_ARRAY_FOR(del->musts, u) {
2095 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002096 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002097 LY_ARRAY_FOR(del->uniques, u) {
2098 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002099 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002100 LY_ARRAY_FOR(del->dflts, u) {
2101 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002102 }
2103 }
2104
2105 LEVEL--;
2106 ypr_close(ctx, 1);
2107 }
2108
2109 LEVEL--;
2110 ypr_close(ctx, 1);
2111}
2112
Michal Vasko7c8439f2020-08-05 13:25:19 +02002113static void
2114yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002115{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002116 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002117
Radek Krejcid3ca0632019-04-16 16:54:54 +02002118 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002119 ly_print_(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002120 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002121 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2122 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002123 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002124 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002125 }
Radek Krejci693262f2019-04-29 15:23:20 +02002126 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2127 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002128 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002129 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002130 }
2131 LY_ARRAY_FOR(modp->includes, u) {
2132 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 +02002133 ly_print_(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002134 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002135 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002136 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002137 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002138 }
Radek Krejci693262f2019-04-29 15:23:20 +02002139 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2140 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002141 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002142 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002143 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002144 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002145 }
2146 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002147}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002148
Michal Vasko7c8439f2020-08-05 13:25:19 +02002149static void
2150yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2151{
2152 LY_ARRAY_COUNT_TYPE u;
2153 struct lysp_node *data;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002154
Radek Krejcid3ca0632019-04-16 16:54:54 +02002155 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02002156 ly_print_(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002157 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002158 }
2159 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002160 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002161 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002162 }
2163
2164 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002165 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002166 }
2167
2168 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002169 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002170 }
2171
2172 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002173 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002174 }
2175
2176 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002177 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002178 }
2179
2180 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002181 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002182 }
2183
2184 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002185 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002186 }
2187
2188 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002189 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002190 }
2191
2192 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002193 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002194 }
2195
2196 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002197 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002198 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002199}
2200
2201LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02002202yang_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 +02002203{
2204 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02002205 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 +02002206
Michal Vasko5233e962020-08-14 14:26:20 +02002207 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002208 LEVEL++;
2209
2210 /* module-header-stmts */
2211 if (module->version) {
2212 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
2213 }
2214
2215 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2216 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2217
2218 /* linkage-stmts (import/include) */
2219 yang_print_parsed_linkage(ctx, modp);
2220
2221 /* meta-stmts */
2222 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002223 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002224 }
2225 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2226 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2227 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2228 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2229
2230 /* revision-stmts */
2231 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002232 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002233 }
2234 LY_ARRAY_FOR(modp->revs, u) {
2235 yprp_revision(ctx, &modp->revs[u]);
2236 }
2237 /* body-stmts */
2238 yang_print_parsed_body(ctx, modp);
2239
2240 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002241 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002242 ly_print_flush(out);
2243
2244 return LY_SUCCESS;
2245}
2246
2247static void
2248yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2249{
Michal Vasko5233e962020-08-14 14:26:20 +02002250 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->belongsto);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002251 LEVEL++;
2252 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
2253 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2254 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002255 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002256}
2257
2258LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02002259yang_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 +02002260{
2261 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02002262 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 +02002263
Michal Vasko5233e962020-08-14 14:26:20 +02002264 ly_print_(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002265 LEVEL++;
2266
2267 /* submodule-header-stmts */
2268 if (submodp->version) {
2269 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
2270 }
2271
2272 yprp_belongsto(ctx, submodp);
2273
2274 /* linkage-stmts (import/include) */
2275 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2276
2277 /* meta-stmts */
2278 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002279 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002280 }
2281 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2282 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
2283 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2284 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
2285
2286 /* revision-stmts */
2287 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02002288 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02002289 }
2290 LY_ARRAY_FOR(submodp->revs, u) {
2291 yprp_revision(ctx, &submodp->revs[u]);
2292 }
2293 /* body-stmts */
2294 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002295
2296 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002297 ly_print_(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002298 ly_print_flush(out);
2299
2300 return LY_SUCCESS;
2301}
2302
2303LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002304yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002305{
Radek Krejci5536d282020-08-04 23:27:44 +02002306 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 +08002307
2308 yprc_node(ctx, node);
2309
2310 ly_print_flush(out);
2311 return LY_SUCCESS;
2312}
2313
2314LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002315yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002316{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002317 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002318 struct lysc_node *data;
2319 struct lysc_module *modc = module->compiled;
Radek Krejci5536d282020-08-04 23:27:44 +02002320 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002321
Michal Vasko5233e962020-08-14 14:26:20 +02002322 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002323 LEVEL++;
2324
Radek Krejci693262f2019-04-29 15:23:20 +02002325 /* module-header-stmts */
2326 if (module->version) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002327 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 +02002328 }
2329 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2330 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2331
Michal Vasko7c8439f2020-08-05 13:25:19 +02002332 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002333
2334 /* meta-stmts */
2335 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02002336 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002337 }
2338 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2339 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2340 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2341 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2342
2343 /* revision-stmts */
2344 if (module->revision) {
Michal Vasko5233e962020-08-14 14:26:20 +02002345 ly_print_(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002346 }
2347
2348 /* body-stmts */
2349 if (modc->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02002350 ly_print_(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002351 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2352 }
2353
2354 LY_ARRAY_FOR(modc->features, u) {
2355 yprc_feature(ctx, &modc->features[u]);
2356 }
2357
2358 LY_ARRAY_FOR(modc->identities, u) {
2359 yprc_identity(ctx, &modc->identities[u]);
2360 }
2361
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08002362 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002363 LY_LIST_FOR(modc->data, data) {
2364 yprc_node(ctx, data);
2365 }
Radek Krejci693262f2019-04-29 15:23:20 +02002366
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002367 LY_ARRAY_FOR(modc->rpcs, u) {
2368 yprc_action(ctx, &modc->rpcs[u]);
2369 }
Radek Krejci693262f2019-04-29 15:23:20 +02002370
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002371 LY_ARRAY_FOR(modc->notifs, u) {
2372 yprc_notification(ctx, &modc->notifs[u]);
2373 }
Radek Krejci693262f2019-04-29 15:23:20 +02002374 }
2375
2376 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002377 ly_print_(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002378 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002379
2380 return LY_SUCCESS;
2381}