blob: c97e32ae483821ce1e6d39af1b03d25d118b6942 [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#define LEVEL ctx->level /**< current level */
56#define INDENT (LEVEL)*2,"" /**< indentation parameters for printer functions */
57
58/**
59 * @brief Print the given text as content of a double quoted YANG string,
60 * including encoding characters that have special meanings. The quotation marks
61 * are not printed.
62 *
63 * Follows RFC 7950, section 6.1.3.
64 *
65 * @param[in] out Output specification.
66 * @param[in] text String to be printed.
Radek Krejcif56e2a42019-09-09 14:15:25 +020067 * @param[in] len Length of the string from @p text to be printed. In case of -1,
Radek Krejcie7b95092019-05-15 11:03:07 +020068 * the @p text is printed completely as a NULL-terminated string.
69 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020070static void
Radek Krejci241f6b52020-05-21 18:13:49 +020071ypr_encode(struct ly_out *out, const char *text, int len)
Radek Krejcid3ca0632019-04-16 16:54:54 +020072{
73 int i, start_len;
74 const char *start;
75 char special = 0;
76
77 if (!len) {
78 return;
79 }
80
81 if (len < 0) {
82 len = strlen(text);
83 }
84
85 start = text;
86 start_len = 0;
87 for (i = 0; i < len; ++i) {
88 switch (text[i]) {
89 case '\n':
90 case '\t':
91 case '\"':
92 case '\\':
93 special = text[i];
94 break;
95 default:
96 ++start_len;
97 break;
98 }
99
100 if (special) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200101 ly_write(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200102 switch (special) {
103 case '\n':
Radek Krejci241f6b52020-05-21 18:13:49 +0200104 ly_write(out, "\\n", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200105 break;
106 case '\t':
Radek Krejci241f6b52020-05-21 18:13:49 +0200107 ly_write(out, "\\t", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200108 break;
109 case '\"':
Radek Krejci241f6b52020-05-21 18:13:49 +0200110 ly_write(out, "\\\"", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200111 break;
112 case '\\':
Radek Krejci241f6b52020-05-21 18:13:49 +0200113 ly_write(out, "\\\\", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200114 break;
115 }
116
117 start += start_len + 1;
118 start_len = 0;
119
120 special = 0;
121 }
122 }
123
Radek Krejci241f6b52020-05-21 18:13:49 +0200124 ly_write(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200125}
126
127static void
Radek Krejci241f6b52020-05-21 18:13:49 +0200128ypr_open(struct ly_out *out, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200129{
130 if (flag && !*flag) {
131 *flag = 1;
Radek Krejci241f6b52020-05-21 18:13:49 +0200132 ly_print(out, " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200133 }
134}
135
136static void
137ypr_close(struct ypr_ctx *ctx, int flag)
138{
139 if (flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200140 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200141 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200142 ly_print(ctx->out, ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200143 }
144}
145
146static void
147ypr_text(struct ypr_ctx *ctx, const char *name, const char *text, int singleline, int closed)
148{
149 const char *s, *t;
150
151 if (singleline) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200152 ly_print(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200153 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200154 ly_print(ctx->out, "%*s%s\n", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200155 LEVEL++;
156
Radek Krejci241f6b52020-05-21 18:13:49 +0200157 ly_print(ctx->out, "%*s\"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200158 }
159 t = text;
160 while ((s = strchr(t, '\n'))) {
161 ypr_encode(ctx->out, t, s - t);
Radek Krejci241f6b52020-05-21 18:13:49 +0200162 ly_print(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200163 t = s + 1;
164 if (*t != '\n') {
Radek Krejci241f6b52020-05-21 18:13:49 +0200165 ly_print(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200166 }
167 }
168
169 ypr_encode(ctx->out, t, strlen(t));
170 if (closed) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200171 ly_print(ctx->out, "\";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200172 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200173 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200174 }
175 if (!singleline) {
176 LEVEL--;
177 }
178}
179
180static void
Radek Krejci693262f2019-04-29 15:23:20 +0200181yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200182{
183 struct lysp_stmt *childstmt;
184 const char *s, *t;
185
186 if (stmt->arg) {
187 if (stmt->flags) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200188 ly_print(ctx->out, "%*s%s\n", INDENT, stmt->stmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200189 LEVEL++;
Radek Krejci241f6b52020-05-21 18:13:49 +0200190 ly_print(ctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'');
Radek Krejcid3ca0632019-04-16 16:54:54 +0200191 t = stmt->arg;
192 while ((s = strchr(t, '\n'))) {
193 ypr_encode(ctx->out, t, s - t);
Radek Krejci241f6b52020-05-21 18:13:49 +0200194 ly_print(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200195 t = s + 1;
196 if (*t != '\n') {
Radek Krejci241f6b52020-05-21 18:13:49 +0200197 ly_print(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200198 }
199 }
200 LEVEL--;
201 ypr_encode(ctx->out, t, strlen(t));
Radek Krejci241f6b52020-05-21 18:13:49 +0200202 ly_print(ctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200203 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200204 ly_print(ctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200205 }
206 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200207 ly_print(ctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200208 }
209
210 if (stmt->child) {
211 LEVEL++;
212 LY_LIST_FOR(stmt->child, childstmt) {
Radek Krejci693262f2019-04-29 15:23:20 +0200213 yprp_stmt(ctx, childstmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200214 }
215 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +0200216 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200217 }
218}
219
220/**
221 * @param[in] count Number of extensions to print, 0 to print them all.
222 */
223static void
Radek Krejci693262f2019-04-29 15:23:20 +0200224yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200225 struct lysp_ext_instance *ext, int *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200226{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200227 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200228 struct lysp_stmt *stmt;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200229 int child_presence;
230 const char *argument;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200231
232 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200233 count = LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200234 }
235 LY_ARRAY_FOR(ext, u) {
236 if (!count) {
237 break;
238 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200239
Radek Krejcid3ca0632019-04-16 16:54:54 +0200240 count--;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200241 if (ext->insubstmt != substmt || ext->insubstmt_index != substmt_index) {
242 continue;
243 }
244
245 if (!ext->compiled && ext->yin) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200246 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 +0200247 continue;
248 }
249
250
251 ypr_open(ctx->out, flag);
252 argument = NULL;
253 if (ext[u].compiled) {
254 argument = ext[u].compiled->argument;
255 } else {
256 argument = ext[u].argument;
257 }
258 if (argument) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200259 ly_print(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200260 ypr_encode(ctx->out, argument, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200261 ly_print(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200262 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200263 ly_print(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200264 }
265
266 child_presence = 0;
267 LEVEL++;
268 LY_LIST_FOR(ext[u].child, stmt) {
269 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
270 continue;
271 }
272 if (!child_presence) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200273 ly_print(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200274 child_presence = 1;
275 }
276 yprp_stmt(ctx, stmt);
277 }
278 LEVEL--;
279 if (child_presence) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200280 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200281 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200282 ly_print(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200283 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200284 }
285}
286
Radek Krejci693262f2019-04-29 15:23:20 +0200287/**
288 * @param[in] count Number of extensions to print, 0 to print them all.
289 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200290static void
Radek Krejci693262f2019-04-29 15:23:20 +0200291yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200292 struct lysc_ext_instance *ext, int *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200293{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200294 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200295
296 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200297 count = LY_ARRAY_COUNT(ext);
Radek Krejci693262f2019-04-29 15:23:20 +0200298 }
299 LY_ARRAY_FOR(ext, u) {
300 if (!count) {
301 break;
302 }
303 /* TODO compiled extensions */
304 (void) ctx;
305 (void) substmt;
306 (void) substmt_index;
307 (void) flag;
308
309 count--;
310 }
311}
312
313static void
314ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200315{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200316 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200317 int extflag = 0;
318
319 if (!text) {
320 /* nothing to print */
321 return;
322 }
323
324 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200325 ly_print(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200326 } else {
327 ypr_text(ctx, ext_substmt_info[substmt].name, text,
328 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
329 }
330
331 LEVEL++;
332 LY_ARRAY_FOR(ext, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200333 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 +0200334 continue;
335 }
Radek Krejci693262f2019-04-29 15:23:20 +0200336 if (ctx->schema == YPR_PARSED) {
337 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance*)ext)[u], &extflag, 1);
338 } else {
339 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance*)ext)[u], &extflag, 1);
340 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200341 }
342 LEVEL--;
343 ypr_close(ctx, extflag);
344}
345
346static void
Radek Krejci693262f2019-04-29 15:23:20 +0200347ypr_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 +0200348{
349 char *str;
350
351 if (asprintf(&str, "%u", attr_value) == -1) {
352 LOGMEM(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +0200353 ctx->out->status = LY_EMEM;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200354 return;
355 }
356 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200357 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200358 free(str);
359}
360
361static void
Radek Krejci693262f2019-04-29 15:23:20 +0200362ypr_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 +0200363{
364 char *str;
365
366 if (asprintf(&str, "%d", attr_value) == -1) {
367 LOGMEM(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +0200368 ctx->out->status = LY_EMEM;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200369 return;
370 }
371 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200372 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200373 free(str);
374}
375
376static void
Radek Krejci693262f2019-04-29 15:23:20 +0200377yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200378{
379 if (rev->dsc || rev->ref || rev->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200380 ly_print(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200381 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200382 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
383 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
384 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200385 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +0200386 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200387 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200388 ly_print(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200389 }
390}
391
392static void
Radek Krejci693262f2019-04-29 15:23:20 +0200393ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200394{
395 if (flags & LYS_MAND_MASK) {
396 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200397 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200398 }
399}
400
401static void
Radek Krejci693262f2019-04-29 15:23:20 +0200402ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200403{
404 if (flags & LYS_CONFIG_MASK) {
405 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200406 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200407 }
408}
409
410static void
Radek Krejci693262f2019-04-29 15:23:20 +0200411ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200412{
413 const char *status = NULL;
414
415 if (flags & LYS_STATUS_CURR) {
416 ypr_open(ctx->out, flag);
417 status = "current";
418 } else if (flags & LYS_STATUS_DEPRC) {
419 ypr_open(ctx->out, flag);
420 status = "deprecated";
421 } else if (flags & LYS_STATUS_OBSLT) {
422 ypr_open(ctx->out, flag);
423 status = "obsolete";
424 }
Radek Krejci693262f2019-04-29 15:23:20 +0200425
426 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200427}
428
429static void
Radek Krejci693262f2019-04-29 15:23:20 +0200430ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200431{
432 if (dsc) {
433 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200434 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200435 }
436}
437
438static void
Radek Krejci693262f2019-04-29 15:23:20 +0200439ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200440{
441 if (ref) {
442 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200443 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200444 }
445}
446
447static void
Radek Krejci693262f2019-04-29 15:23:20 +0200448yprp_iffeatures(struct ypr_ctx *ctx, const char **iff, struct lysp_ext_instance *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200449{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200450 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200451 int extflag;
452
453 LY_ARRAY_FOR(iff, u) {
454 ypr_open(ctx->out, flag);
455 extflag = 0;
456
Radek Krejci241f6b52020-05-21 18:13:49 +0200457 ly_print(ctx->out, "%*sif-feature \"%s\"", INDENT, iff[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200458
459 /* extensions */
460 LEVEL++;
461 LY_ARRAY_FOR(exts, u) {
462 if (exts[u].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[u].insubstmt_index != u) {
463 continue;
464 }
Radek Krejci693262f2019-04-29 15:23:20 +0200465 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[u], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200466 }
467 LEVEL--;
468 ypr_close(ctx, extflag);
469 }
470}
471
472static void
Radek Krejci693262f2019-04-29 15:23:20 +0200473yprc_iffeature(struct ypr_ctx *ctx, struct lysc_iffeature *feat, int *index_e, int *index_f)
474{
475 int brackets_flag = *index_e;
476 uint8_t op;
477
478 op = lysc_iff_getop(feat->expr, *index_e);
479 (*index_e)++;
480
481 switch (op) {
482 case LYS_IFF_F:
483 if (ctx->module == feat->features[*index_f]->module) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200484 ly_print(ctx->out, "%s", feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200485 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200486 ly_print(ctx->out, "%s:%s", feat->features[*index_f]->module->prefix, feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200487 }
488 (*index_f)++;
489 break;
490 case LYS_IFF_NOT:
Radek Krejci241f6b52020-05-21 18:13:49 +0200491 ly_print(ctx->out, "not ");
Radek Krejci693262f2019-04-29 15:23:20 +0200492 yprc_iffeature(ctx, feat, index_e, index_f);
493 break;
494 case LYS_IFF_AND:
495 if (brackets_flag) {
496 /* AND need brackets only if previous op was not */
497 if (*index_e < 2 || lysc_iff_getop(feat->expr, *index_e - 2) != LYS_IFF_NOT) {
498 brackets_flag = 0;
499 }
500 }
501 /* falls through */
502 case LYS_IFF_OR:
503 if (brackets_flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200504 ly_print(ctx->out, "(");
Radek Krejci693262f2019-04-29 15:23:20 +0200505 }
506 yprc_iffeature(ctx, feat, index_e, index_f);
Radek Krejci241f6b52020-05-21 18:13:49 +0200507 ly_print(ctx->out, " %s ", op == LYS_IFF_OR ? "or" : "and");
Radek Krejci693262f2019-04-29 15:23:20 +0200508 yprc_iffeature(ctx, feat, index_e, index_f);
509 if (brackets_flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200510 ly_print(ctx->out, ")");
Radek Krejci693262f2019-04-29 15:23:20 +0200511 }
512 }
513}
514
515static void
516yprc_iffeatures(struct ypr_ctx *ctx, struct lysc_iffeature *iff, struct lysc_ext_instance *exts, int *flag)
517{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200518 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +0200519 int extflag;
520
521 LY_ARRAY_FOR(iff, u) {
522 int index_e = 0, index_f = 0;
523
524 ypr_open(ctx->out, flag);
525 extflag = 0;
526
Radek Krejci241f6b52020-05-21 18:13:49 +0200527 ly_print(ctx->out, "%*sif-feature \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200528 yprc_iffeature(ctx, iff, &index_e, &index_f);
Radek Krejci241f6b52020-05-21 18:13:49 +0200529 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200530
531 /* extensions */
532 LEVEL++;
Radek Krejci334ccc72019-06-12 13:49:29 +0200533 LY_ARRAY_FOR(exts, v) {
534 if (exts[v].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[v].insubstmt_index != u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200535 continue;
536 }
Radek Krejci334ccc72019-06-12 13:49:29 +0200537 yprc_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200538 }
539 LEVEL--;
540 ypr_close(ctx, extflag);
541 }
542}
543
544static void
545yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200546{
547 int flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200548 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200549
Radek Krejci241f6b52020-05-21 18:13:49 +0200550 ly_print(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200551 LEVEL++;
552
553 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200554 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200555 }
556
557 if (ext->argument) {
558 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200559 ly_print(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200560 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200561 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200562 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200563 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 +0200564 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200565 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200566 }
567 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200568 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200569 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200570 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200571 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200572 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200573 ypr_close(ctx, flag2);
574 }
575
Radek Krejci693262f2019-04-29 15:23:20 +0200576 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200577 ypr_description(ctx, ext->dsc, ext->exts, &flag);
578 ypr_reference(ctx, ext->ref, ext->exts, &flag);
579
580 LEVEL--;
581 ypr_close(ctx, flag);
582}
583
584static void
Radek Krejci693262f2019-04-29 15:23:20 +0200585yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200586{
587 int flag = 0;
588
Radek Krejci241f6b52020-05-21 18:13:49 +0200589 ly_print(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200590 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200591 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
592 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
593 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200594 ypr_description(ctx, feat->dsc, feat->exts, &flag);
595 ypr_reference(ctx, feat->ref, feat->exts, &flag);
596 LEVEL--;
597 ypr_close(ctx, flag);
598}
599
600static void
Radek Krejci693262f2019-04-29 15:23:20 +0200601yprc_feature(struct ypr_ctx *ctx, const struct lysc_feature *feat)
602{
603 int flag = 0;
604
Radek Krejci241f6b52020-05-21 18:13:49 +0200605 ly_print(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200606 LEVEL++;
607 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
608 yprc_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
609 ypr_status(ctx, feat->flags, feat->exts, &flag);
610 ypr_description(ctx, feat->dsc, feat->exts, &flag);
611 ypr_reference(ctx, feat->ref, feat->exts, &flag);
612 LEVEL--;
613 ypr_close(ctx, flag);
614}
615
616static void
617yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200618{
619 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200620 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200621
Radek Krejci241f6b52020-05-21 18:13:49 +0200622 ly_print(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200623 LEVEL++;
624
Radek Krejci693262f2019-04-29 15:23:20 +0200625 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
626 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200627
628 LY_ARRAY_FOR(ident->bases, u) {
629 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200630 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200631 }
632
Radek Krejci693262f2019-04-29 15:23:20 +0200633 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200634 ypr_description(ctx, ident->dsc, ident->exts, &flag);
635 ypr_reference(ctx, ident->ref, ident->exts, &flag);
636
637 LEVEL--;
638 ypr_close(ctx, flag);
639}
640
641static void
Radek Krejci693262f2019-04-29 15:23:20 +0200642yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
643{
644 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200645 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200646
Radek Krejci241f6b52020-05-21 18:13:49 +0200647 ly_print(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200648 LEVEL++;
649
650 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
651 yprc_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
652
653 LY_ARRAY_FOR(ident->derived, u) {
654 ypr_open(ctx->out, &flag);
655 if (ctx->module != ident->derived[u]->module) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200656 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 +0200657 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200658 ly_print(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200659 }
660 }
661
662 ypr_status(ctx, ident->flags, ident->exts, &flag);
663 ypr_description(ctx, ident->dsc, ident->exts, &flag);
664 ypr_reference(ctx, ident->ref, ident->exts, &flag);
665
666 LEVEL--;
667 ypr_close(ctx, flag);
668}
669
670static void
671yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200672{
673 int inner_flag = 0;
674
675 if (!restr) {
676 return;
677 }
678
679 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200680 ly_print(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200681 ypr_encode(ctx->out, (restr->arg[0] != 0x15 && restr->arg[0] != 0x06) ? restr->arg : &restr->arg[1], -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200682 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200683
684 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200685 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200686 if (restr->arg[0] == 0x15) {
687 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
688 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200689 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200690 }
691 if (restr->emsg) {
692 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200693 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200694 }
695 if (restr->eapptag) {
696 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200697 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200698 }
Radek Krejci693262f2019-04-29 15:23:20 +0200699 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
700 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
701
Radek Krejcid3ca0632019-04-16 16:54:54 +0200702 LEVEL--;
703 ypr_close(ctx, inner_flag);
704}
705
706static void
Radek Krejci693262f2019-04-29 15:23:20 +0200707yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, int *flag)
708{
709 int inner_flag = 0;
710
711 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200712 ly_print(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200713 ypr_encode(ctx->out, must->cond->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200714 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200715
716 LEVEL++;
717 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
718 if (must->emsg) {
719 ypr_open(ctx->out, &inner_flag);
720 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
721 }
722 if (must->eapptag) {
723 ypr_open(ctx->out, &inner_flag);
724 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
725 }
726 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
727 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
728
729 LEVEL--;
730 ypr_close(ctx, inner_flag);
731}
732
733static void
734yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, int *flag)
735{
736 int inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200737 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200738
Radek Krejci334ccc72019-06-12 13:49:29 +0200739 if (!range) {
740 return;
741 }
742
Radek Krejci693262f2019-04-29 15:23:20 +0200743 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200744 ly_print(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200745 LY_ARRAY_FOR(range->parts, u) {
746 if (u > 0) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200747 ly_print(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200748 }
749 if (range->parts[u].max_64 == range->parts[u].min_64) {
750 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200751 ly_print(ctx->out, "%"PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200752 } else { /* signed values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200753 ly_print(ctx->out, "%"PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200754 }
755 } else {
756 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200757 ly_print(ctx->out, "%"PRIu64"..%"PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200758 } else { /* signed values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200759 ly_print(ctx->out, "%"PRId64"..%"PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200760 }
761 }
762 }
Radek Krejci241f6b52020-05-21 18:13:49 +0200763 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200764
765 LEVEL++;
766 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
767 if (range->emsg) {
768 ypr_open(ctx->out, &inner_flag);
769 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
770 }
771 if (range->eapptag) {
772 ypr_open(ctx->out, &inner_flag);
773 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
774 }
775 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
776 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
777
778 LEVEL--;
779 ypr_close(ctx, inner_flag);
780}
781
782static void
783yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, int *flag)
784{
785 int inner_flag = 0;
786
787 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200788 ly_print(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200789 ypr_encode(ctx->out, pattern->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200790 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200791
792 LEVEL++;
793 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
794 if (pattern->inverted) {
795 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
796 ypr_open(ctx->out, &inner_flag);
797 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
798 }
799 if (pattern->emsg) {
800 ypr_open(ctx->out, &inner_flag);
801 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
802 }
803 if (pattern->eapptag) {
804 ypr_open(ctx->out, &inner_flag);
805 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
806 }
807 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
808 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
809
810 LEVEL--;
811 ypr_close(ctx, inner_flag);
812}
813
814static void
815yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200816{
817 int inner_flag = 0;
818
819 if (!when) {
820 return;
821 }
822 ypr_open(ctx->out, flag);
823
Radek Krejci241f6b52020-05-21 18:13:49 +0200824 ly_print(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200825 ypr_encode(ctx->out, when->cond, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200826 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200827
828 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200829 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200830 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
831 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
832 LEVEL--;
833 ypr_close(ctx, inner_flag);
834}
835
836static void
Radek Krejci693262f2019-04-29 15:23:20 +0200837yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, int *flag)
838{
839 int inner_flag = 0;
840
841 if (!when) {
842 return;
843 }
844 ypr_open(ctx->out, flag);
845
Radek Krejci241f6b52020-05-21 18:13:49 +0200846 ly_print(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200847 ypr_encode(ctx->out, when->cond->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200848 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200849
850 LEVEL++;
851 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
852 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
853 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
854 LEVEL--;
855 ypr_close(ctx, inner_flag);
856}
857
858static void
859yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200860{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200861 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200862 int inner_flag;
863
864 LY_ARRAY_FOR(items, u) {
865 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200866 if (type == LY_TYPE_BITS) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200867 ly_print(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200868 } else { /* LY_TYPE_ENUM */
Radek Krejci241f6b52020-05-21 18:13:49 +0200869 ly_print(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200870 ypr_encode(ctx->out, items[u].name, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200871 ly_print(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200872 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200873 inner_flag = 0;
874 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200875 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
876 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200877 if (items[u].flags & LYS_SET_VALUE) {
878 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200879 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200880 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200881 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200882 }
883 }
Radek Krejci693262f2019-04-29 15:23:20 +0200884 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200885 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
886 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
887 LEVEL--;
888 ypr_close(ctx, inner_flag);
889 }
890}
891
892static void
Radek Krejci693262f2019-04-29 15:23:20 +0200893yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200894{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200895 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200896 int flag = 0;
897
Radek Krejci241f6b52020-05-21 18:13:49 +0200898 ly_print(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200899 LEVEL++;
900
Radek Krejci693262f2019-04-29 15:23:20 +0200901 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200902
Radek Krejci693262f2019-04-29 15:23:20 +0200903 yprp_restr(ctx, type->range, "range", &flag);
904 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200905 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200906 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200907 }
Radek Krejci693262f2019-04-29 15:23:20 +0200908 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
909 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200910
911 if (type->path) {
912 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200913 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200914 }
915 if (type->flags & LYS_SET_REQINST) {
916 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200917 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200918 }
919 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200920 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200921 }
922 LY_ARRAY_FOR(type->bases, u) {
923 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200924 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200925 }
926 LY_ARRAY_FOR(type->types, u) {
927 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200928 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200929 }
930
931 LEVEL--;
932 ypr_close(ctx, flag);
933}
934
935static void
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200936yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, const struct lys_module *value_mod, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200937{
938 int dynamic;
939 const char *str;
940
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200941 str = value->realtype->plugin->print(value, LYD_JSON, lys_get_prefix, (void*)value_mod, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200942 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
943 if (dynamic) {
944 free((void*)str);
945 }
946}
947
948static void
Radek Krejci693262f2019-04-29 15:23:20 +0200949yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
950{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200951 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200952 int flag = 0;
953
Radek Krejci241f6b52020-05-21 18:13:49 +0200954 ly_print(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200955 LEVEL++;
956
957 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
958 if (type->dflt) {
959 ypr_open(ctx->out, &flag);
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200960 yprc_dflt_value(ctx, type->dflt, type->dflt_mod, type->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200961 }
962
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200963 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200964 case LY_TYPE_BINARY: {
965 struct lysc_type_bin *bin = (struct lysc_type_bin*)type;
966 yprc_range(ctx, bin->length, type->basetype, &flag);
967 break;
968 }
969 case LY_TYPE_UINT8:
970 case LY_TYPE_UINT16:
971 case LY_TYPE_UINT32:
972 case LY_TYPE_UINT64:
973 case LY_TYPE_INT8:
974 case LY_TYPE_INT16:
975 case LY_TYPE_INT32:
976 case LY_TYPE_INT64: {
977 struct lysc_type_num *num = (struct lysc_type_num*)type;
978 yprc_range(ctx, num->range, type->basetype, &flag);
979 break;
980 }
981 case LY_TYPE_STRING: {
982 struct lysc_type_str *str = (struct lysc_type_str*)type;
983 yprc_range(ctx, str->length, type->basetype, &flag);
984 LY_ARRAY_FOR(str->patterns, u) {
985 yprc_pattern(ctx, str->patterns[u], &flag);
986 }
987 break;
988 }
989 case LY_TYPE_BITS:
990 case LY_TYPE_ENUM: {
991 /* bits and enums structures are compatible */
992 struct lysc_type_bits *bits = (struct lysc_type_bits*)type;
993 LY_ARRAY_FOR(bits->bits, u) {
994 struct lysc_type_bitenum_item *item = &bits->bits[u];
995 int inner_flag = 0;
996
997 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200998 ly_print(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200999 ypr_encode(ctx->out, item->name, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +02001000 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +02001001 LEVEL++;
1002 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
1003 yprc_iffeatures(ctx, item->iffeatures, item->exts, &inner_flag);
1004 if (type->basetype == LY_TYPE_BITS) {
1005 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
1006 } else { /* LY_TYPE_ENUM */
1007 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
1008 }
1009 ypr_status(ctx, item->flags, item->exts, &inner_flag);
1010 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
1011 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
1012 LEVEL--;
1013 ypr_close(ctx, inner_flag);
1014 }
1015 break;
1016 }
1017 case LY_TYPE_BOOL:
1018 case LY_TYPE_EMPTY:
1019 /* nothing to do */
1020 break;
1021 case LY_TYPE_DEC64: {
1022 struct lysc_type_dec *dec = (struct lysc_type_dec*)type;
1023 ypr_open(ctx->out, &flag);
1024 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
1025 yprc_range(ctx, dec->range, dec->basetype, &flag);
1026 break;
1027 }
1028 case LY_TYPE_IDENT: {
1029 struct lysc_type_identityref *ident = (struct lysc_type_identityref*)type;
1030 LY_ARRAY_FOR(ident->bases, u) {
1031 ypr_open(ctx->out, &flag);
1032 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
1033 }
1034 break;
1035 }
1036 case LY_TYPE_INST: {
1037 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid*)type;
1038 ypr_open(ctx->out, &flag);
1039 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
1040 break;
1041 }
1042 case LY_TYPE_LEAFREF: {
1043 struct lysc_type_leafref *lr = (struct lysc_type_leafref*)type;
1044 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +02001045 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001046 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
1047 yprc_type(ctx, lr->realtype);
1048 break;
1049 }
1050 case LY_TYPE_UNION: {
1051 struct lysc_type_union *un = (struct lysc_type_union*)type;
1052 LY_ARRAY_FOR(un->types, u) {
1053 ypr_open(ctx->out, &flag);
1054 yprc_type(ctx, un->types[u]);
1055 }
1056 break;
1057 }
1058 default:
1059 LOGINT(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +02001060 ctx->out->status = LY_EINT;
Radek Krejci693262f2019-04-29 15:23:20 +02001061 }
1062
1063 LEVEL--;
1064 ypr_close(ctx, flag);
1065}
1066
1067static void
1068yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001069{
Radek Krejci56cc0872019-04-30 09:22:27 +02001070 LYOUT_CHECK(ctx->out);
1071
Radek Krejci241f6b52020-05-21 18:13:49 +02001072 ly_print(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001073 LEVEL++;
1074
Radek Krejci693262f2019-04-29 15:23:20 +02001075 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001076
Radek Krejci693262f2019-04-29 15:23:20 +02001077 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001078
1079 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +02001080 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001081 }
1082 if (tpdf->dflt) {
Radek Krejci693262f2019-04-29 15:23:20 +02001083 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001084 }
1085
Radek Krejci693262f2019-04-29 15:23:20 +02001086 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001087 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
1088 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
1089
1090 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001091 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001092}
1093
Radek Krejci693262f2019-04-29 15:23:20 +02001094static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
1095static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
1096static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001097
1098static void
Radek Krejci693262f2019-04-29 15:23:20 +02001099yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001100{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001101 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001102 int flag = 0;
1103 struct lysp_node *data;
1104
Radek Krejci56cc0872019-04-30 09:22:27 +02001105 LYOUT_CHECK(ctx->out);
1106
Radek Krejci241f6b52020-05-21 18:13:49 +02001107 ly_print(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001108 LEVEL++;
1109
Radek Krejci693262f2019-04-29 15:23:20 +02001110 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1111 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001112 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1113 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001114
1115 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001116 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001117 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001118 }
1119
1120 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001121 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001122 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001123 }
1124
1125 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001126 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001127 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001128 }
1129
1130 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001131 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001132 }
1133
1134 LEVEL--;
1135 ypr_close(ctx, flag);
1136}
1137
1138static void
Radek Krejci693262f2019-04-29 15:23:20 +02001139yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001140{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001141 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001142 struct lysp_node *data;
1143
1144 if (!inout->nodetype) {
1145 /* nodetype not set -> input/output is empty */
1146 return;
1147 }
1148 ypr_open(ctx->out, flag);
1149
Radek Krejci241f6b52020-05-21 18:13:49 +02001150 ly_print(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001151 LEVEL++;
1152
Radek Krejci693262f2019-04-29 15:23:20 +02001153 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001154 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001155 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001156 }
1157 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001158 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001159 }
1160 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001161 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001162 }
1163
1164 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001165 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001166 }
1167
1168 LEVEL--;
1169 ypr_close(ctx, 1);
1170}
1171
1172static void
Radek Krejci693262f2019-04-29 15:23:20 +02001173yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, int *flag)
1174{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001175 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001176 struct lysc_node *data;
1177
1178 if (!inout->data) {
1179 /* input/output is empty */
1180 return;
1181 }
1182 ypr_open(ctx->out, flag);
1183
Radek Krejci241f6b52020-05-21 18:13:49 +02001184 ly_print(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001185 LEVEL++;
1186
1187 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1188 LY_ARRAY_FOR(inout->musts, u) {
1189 yprc_must(ctx, &inout->musts[u], NULL);
1190 }
1191
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001192 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001193 LY_LIST_FOR(inout->data, data) {
1194 yprc_node(ctx, data);
1195 }
Radek Krejci693262f2019-04-29 15:23:20 +02001196 }
1197
1198 LEVEL--;
1199 ypr_close(ctx, 1);
1200}
1201
1202static void
1203yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001204{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001205 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001206 int flag = 0;
1207 struct lysp_node *data;
1208
Radek Krejci56cc0872019-04-30 09:22:27 +02001209 LYOUT_CHECK(ctx->out);
1210
Radek Krejci241f6b52020-05-21 18:13:49 +02001211 ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001212
1213 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001214 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1215 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001216
1217 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001218 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001219 }
Radek Krejci693262f2019-04-29 15:23:20 +02001220 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001221 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1222 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1223
1224 LY_ARRAY_FOR(notif->typedefs, u) {
1225 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001226 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001227 }
1228
1229 LY_ARRAY_FOR(notif->groupings, u) {
1230 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001231 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001232 }
1233
1234 LY_LIST_FOR(notif->data, data) {
1235 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001236 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001237 }
1238
1239 LEVEL--;
1240 ypr_close(ctx, flag);
1241}
1242
1243static void
Radek Krejci693262f2019-04-29 15:23:20 +02001244yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1245{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001246 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001247 int flag = 0;
1248 struct lysc_node *data;
1249
Radek Krejci56cc0872019-04-30 09:22:27 +02001250 LYOUT_CHECK(ctx->out);
1251
Radek Krejci241f6b52020-05-21 18:13:49 +02001252 ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001253
1254 LEVEL++;
1255 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1256 yprc_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
1257
1258 LY_ARRAY_FOR(notif->musts, u) {
1259 yprc_must(ctx, &notif->musts[u], &flag);
1260 }
1261 ypr_status(ctx, notif->flags, notif->exts, &flag);
1262 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1263 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1264
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001265 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001266 LY_LIST_FOR(notif->data, data) {
1267 ypr_open(ctx->out, &flag);
1268 yprc_node(ctx, data);
1269 }
Radek Krejci693262f2019-04-29 15:23:20 +02001270 }
1271
1272 LEVEL--;
1273 ypr_close(ctx, flag);
1274}
1275
1276static void
1277yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001278{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001279 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001280 int flag = 0;
1281
Radek Krejci56cc0872019-04-30 09:22:27 +02001282 LYOUT_CHECK(ctx->out);
1283
Radek Krejci241f6b52020-05-21 18:13:49 +02001284 ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001285
1286 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001287 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1288 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1289 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001290 ypr_description(ctx, action->dsc, action->exts, &flag);
1291 ypr_reference(ctx, action->ref, action->exts, &flag);
1292
1293 LY_ARRAY_FOR(action->typedefs, u) {
1294 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001295 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001296 }
1297
1298 LY_ARRAY_FOR(action->groupings, u) {
1299 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001300 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001301 }
1302
Radek Krejci693262f2019-04-29 15:23:20 +02001303 yprp_inout(ctx, &action->input, &flag);
1304 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001305
1306 LEVEL--;
1307 ypr_close(ctx, flag);
1308}
1309
1310static void
Radek Krejci693262f2019-04-29 15:23:20 +02001311yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1312{
1313 int flag = 0;
1314
Radek Krejci56cc0872019-04-30 09:22:27 +02001315 LYOUT_CHECK(ctx->out);
1316
Radek Krejci241f6b52020-05-21 18:13:49 +02001317 ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001318
1319 LEVEL++;
1320 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1321 yprc_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1322 ypr_status(ctx, action->flags, action->exts, &flag);
1323 ypr_description(ctx, action->dsc, action->exts, &flag);
1324 ypr_reference(ctx, action->ref, action->exts, &flag);
1325
1326 yprc_inout(ctx, action, &action->input, &flag);
1327 yprc_inout(ctx, action, &action->output, &flag);
1328
1329 LEVEL--;
1330 ypr_close(ctx, flag);
1331}
1332
1333static void
1334yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001335{
Radek Krejci241f6b52020-05-21 18:13:49 +02001336 ly_print(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001337 LEVEL++;
1338
Radek Krejci693262f2019-04-29 15:23:20 +02001339 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1340 yprp_when(ctx, node->when, flag);
1341 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001342}
1343
1344static void
Radek Krejci693262f2019-04-29 15:23:20 +02001345yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001346{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001347 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001348
Radek Krejci241f6b52020-05-21 18:13:49 +02001349 ly_print(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001350 LEVEL++;
1351
1352 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1353 LY_ARRAY_FOR(node->when, u) {
1354 yprc_when(ctx, node->when[u], flag);
1355 }
1356 yprc_iffeatures(ctx, node->iffeatures, node->exts, flag);
1357}
1358
1359/* macr oto unify the code */
1360#define YPR_NODE_COMMON2 \
1361 ypr_config(ctx, node->flags, node->exts, flag); \
1362 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1363 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1364 } \
1365 ypr_status(ctx, node->flags, node->exts, flag); \
1366 ypr_description(ctx, node->dsc, node->exts, flag); \
1367 ypr_reference(ctx, node->ref, node->exts, flag)
1368
1369static void
1370yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
1371{
1372 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001373}
1374
1375static void
Radek Krejci693262f2019-04-29 15:23:20 +02001376yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
1377{
1378 YPR_NODE_COMMON2;
1379}
1380
1381#undef YPR_NODE_COMMON2
1382
1383static void
1384yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001385{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001386 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001387 int flag = 0;
1388 struct lysp_node *child;
1389 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1390
Radek Krejci693262f2019-04-29 15:23:20 +02001391 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001392
1393 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001394 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001395 }
1396 if (cont->presence) {
1397 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001398 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001399 }
1400
Radek Krejci693262f2019-04-29 15:23:20 +02001401 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001402
1403 LY_ARRAY_FOR(cont->typedefs, u) {
1404 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001405 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001406 }
1407
1408 LY_ARRAY_FOR(cont->groupings, u) {
1409 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001410 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001411 }
1412
1413 LY_LIST_FOR(cont->child, child) {
1414 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001415 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001416 }
1417
1418 LY_ARRAY_FOR(cont->actions, u) {
1419 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001420 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001421 }
1422
1423 LY_ARRAY_FOR(cont->notifs, u) {
1424 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001425 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001426 }
1427
1428 LEVEL--;
1429 ypr_close(ctx, flag);
1430}
1431
1432static void
Radek Krejci693262f2019-04-29 15:23:20 +02001433yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1434{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001435 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001436 int flag = 0;
1437 struct lysc_node *child;
1438 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1439
1440 yprc_node_common1(ctx, node, &flag);
1441
1442 LY_ARRAY_FOR(cont->musts, u) {
1443 yprc_must(ctx, &cont->musts[u], &flag);
1444 }
1445 if (cont->flags & LYS_PRESENCE) {
1446 ypr_open(ctx->out, &flag);
1447 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1448 }
1449
1450 yprc_node_common2(ctx, node, &flag);
1451
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001452 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001453 LY_LIST_FOR(cont->child, child) {
1454 ypr_open(ctx->out, &flag);
1455 yprc_node(ctx, child);
1456 }
Radek Krejci693262f2019-04-29 15:23:20 +02001457
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001458 LY_ARRAY_FOR(cont->actions, u) {
1459 ypr_open(ctx->out, &flag);
1460 yprc_action(ctx, &cont->actions[u]);
1461 }
Radek Krejci693262f2019-04-29 15:23:20 +02001462
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001463 LY_ARRAY_FOR(cont->notifs, u) {
1464 ypr_open(ctx->out, &flag);
1465 yprc_notification(ctx, &cont->notifs[u]);
1466 }
Radek Krejci693262f2019-04-29 15:23:20 +02001467 }
1468
1469 LEVEL--;
1470 ypr_close(ctx, flag);
1471}
1472
1473static void
1474yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1475{
1476 int flag = 0;
1477 struct lysp_node *child;
1478 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1479
1480 yprp_node_common1(ctx, node, &flag);
1481 yprp_node_common2(ctx, node, &flag);
1482
1483 LY_LIST_FOR(cas->child, child) {
1484 ypr_open(ctx->out, &flag);
1485 yprp_node(ctx, child);
1486 }
1487
1488 LEVEL--;
1489 ypr_close(ctx, flag);
1490}
1491
1492static void
1493yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1494{
1495 int flag = 0;
1496 struct lysc_node *child;
1497
1498 yprc_node_common1(ctx, (struct lysc_node*)cs, &flag);
1499 yprc_node_common2(ctx, (struct lysc_node*)cs, &flag);
1500
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001501 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001502 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
1503 ypr_open(ctx->out, &flag);
1504 yprc_node(ctx, child);
1505 }
Radek Krejci693262f2019-04-29 15:23:20 +02001506 }
1507
1508 LEVEL--;
1509 ypr_close(ctx, flag);
1510}
1511
1512static void
1513yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001514{
1515 int flag = 0;
1516 struct lysp_node *child;
1517 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1518
Radek Krejci693262f2019-04-29 15:23:20 +02001519 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001520
1521 if (choice->dflt) {
1522 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001523 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001524 }
1525
Radek Krejci693262f2019-04-29 15:23:20 +02001526 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001527
1528 LY_LIST_FOR(choice->child, child) {
1529 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001530 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001531 }
1532
1533 LEVEL--;
1534 ypr_close(ctx, flag);
1535}
1536
1537static void
Radek Krejci693262f2019-04-29 15:23:20 +02001538yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1539{
1540 int flag = 0;
1541 struct lysc_node_case *cs;
1542 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1543
1544 yprc_node_common1(ctx, node, &flag);
1545
1546 if (choice->dflt) {
1547 ypr_open(ctx->out, &flag);
1548 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1549 }
1550
1551 yprc_node_common2(ctx, node, &flag);
1552
1553 for (cs = choice->cases; cs; cs = (struct lysc_node_case*)cs->next) {
1554 ypr_open(ctx->out, &flag);
1555 yprc_case(ctx, cs);
1556 }
1557
1558 LEVEL--;
1559 ypr_close(ctx, flag);
1560}
1561
1562static void
1563yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001564{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001565 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001566 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1567
Radek Krejci693262f2019-04-29 15:23:20 +02001568 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001569
Radek Krejci693262f2019-04-29 15:23:20 +02001570 yprp_type(ctx, &leaf->type);
1571 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001572 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001573 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001574 }
Radek Krejci693262f2019-04-29 15:23:20 +02001575 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001576
Radek Krejci693262f2019-04-29 15:23:20 +02001577 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001578
1579 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001580 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001581}
1582
1583static void
Radek Krejci693262f2019-04-29 15:23:20 +02001584yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1585{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001586 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001587 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1588
1589 yprc_node_common1(ctx, node, NULL);
1590
1591 yprc_type(ctx, leaf->type);
1592 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1593 LY_ARRAY_FOR(leaf->musts, u) {
1594 yprc_must(ctx, &leaf->musts[u], NULL);
1595 }
Radek Krejcia1911222019-07-22 17:24:50 +02001596
1597 if (leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001598 yprc_dflt_value(ctx, leaf->dflt, leaf->dflt_mod, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001599 }
Radek Krejci693262f2019-04-29 15:23:20 +02001600
1601 yprc_node_common2(ctx, node, NULL);
1602
1603 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001604 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001605}
1606
1607static void
1608yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001609{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001610 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001611 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1612
Radek Krejci693262f2019-04-29 15:23:20 +02001613 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001614
Radek Krejci693262f2019-04-29 15:23:20 +02001615 yprp_type(ctx, &llist->type);
1616 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001617 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001618 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001619 }
1620 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001621 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001622 }
1623
Radek Krejci693262f2019-04-29 15:23:20 +02001624 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001625
1626 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001627 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001628 }
1629 if (llist->flags & LYS_SET_MAX) {
1630 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001631 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001632 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001633 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001634 }
1635 }
1636
1637 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001638 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001639 }
1640
Radek Krejci693262f2019-04-29 15:23:20 +02001641 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001642 ypr_description(ctx, node->dsc, node->exts, NULL);
1643 ypr_reference(ctx, node->ref, node->exts, NULL);
1644
1645 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001646 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001647}
1648
1649static void
Radek Krejci693262f2019-04-29 15:23:20 +02001650yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1651{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001652 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001653 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1654
1655 yprc_node_common1(ctx, node, NULL);
1656
1657 yprc_type(ctx, llist->type);
1658 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1659 LY_ARRAY_FOR(llist->musts, u) {
1660 yprc_must(ctx, &llist->musts[u], NULL);
1661 }
1662 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001663 yprc_dflt_value(ctx, llist->dflts[u], llist->dflts_mods[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001664 }
1665
1666 ypr_config(ctx, node->flags, node->exts, NULL);
1667
1668 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1669 if (llist->max) {
1670 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1671 } else {
1672 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1673 }
1674
1675 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1676
1677 ypr_status(ctx, node->flags, node->exts, NULL);
1678 ypr_description(ctx, node->dsc, node->exts, NULL);
1679 ypr_reference(ctx, node->ref, node->exts, NULL);
1680
1681 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001682 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001683}
1684
1685static void
1686yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001687{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001688 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001689 int flag = 0;
1690 struct lysp_node *child;
1691 struct lysp_node_list *list = (struct lysp_node_list *)node;
1692
Radek Krejci693262f2019-04-29 15:23:20 +02001693 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001694
1695 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001696 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001697 }
1698 if (list->key) {
1699 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001700 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001701 }
1702 LY_ARRAY_FOR(list->uniques, u) {
1703 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001704 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001705 }
1706
Radek Krejci693262f2019-04-29 15:23:20 +02001707 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001708
1709 if (list->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001710 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001711 }
1712 if (list->flags & LYS_SET_MAX) {
1713 if (list->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001714 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001715 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001716 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001717 }
1718 }
1719
1720 if (list->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001721 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001722 }
1723
Radek Krejci693262f2019-04-29 15:23:20 +02001724 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001725 ypr_description(ctx, node->dsc, node->exts, NULL);
1726 ypr_reference(ctx, node->ref, node->exts, NULL);
1727
1728 LY_ARRAY_FOR(list->typedefs, u) {
1729 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001730 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001731 }
1732
1733 LY_ARRAY_FOR(list->groupings, u) {
1734 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001735 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001736 }
1737
1738 LY_LIST_FOR(list->child, child) {
1739 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001740 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001741 }
1742
1743 LY_ARRAY_FOR(list->actions, u) {
1744 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001745 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001746 }
1747
1748 LY_ARRAY_FOR(list->notifs, u) {
1749 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001750 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001751 }
1752
1753 LEVEL--;
1754 ypr_close(ctx, flag);
1755}
1756
1757static void
Radek Krejci693262f2019-04-29 15:23:20 +02001758yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1759{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001760 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001761 int flag = 0;
1762 struct lysc_node *child;
1763 struct lysc_node_list *list = (struct lysc_node_list *)node;
1764
1765 yprc_node_common1(ctx, node, &flag);
1766
1767 LY_ARRAY_FOR(list->musts, u) {
1768 yprc_must(ctx, &list->musts[u], NULL);
1769 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001770 if (!(list->flags & LYS_KEYLESS)) {
Radek Krejci693262f2019-04-29 15:23:20 +02001771 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +02001772 ly_print(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001773 for (struct lysc_node *key = list->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Radek Krejci241f6b52020-05-21 18:13:49 +02001774 ly_print(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001775 }
Radek Krejci241f6b52020-05-21 18:13:49 +02001776 ly_print(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001777 }
1778 LY_ARRAY_FOR(list->uniques, u) {
1779 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +02001780 ly_print(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001781 LY_ARRAY_FOR(list->uniques[u], v) {
Radek Krejci241f6b52020-05-21 18:13:49 +02001782 ly_print(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001783 }
1784 ypr_close(ctx, 0);
1785 }
1786
1787 ypr_config(ctx, node->flags, node->exts, NULL);
1788
1789 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1790 if (list->max) {
1791 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1792 } else {
1793 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1794 }
1795
1796 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1797
1798 ypr_status(ctx, node->flags, node->exts, NULL);
1799 ypr_description(ctx, node->dsc, node->exts, NULL);
1800 ypr_reference(ctx, node->ref, node->exts, NULL);
1801
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001802 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001803 LY_LIST_FOR(list->child, child) {
1804 ypr_open(ctx->out, &flag);
1805 yprc_node(ctx, child);
1806 }
Radek Krejci693262f2019-04-29 15:23:20 +02001807
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001808 LY_ARRAY_FOR(list->actions, u) {
1809 ypr_open(ctx->out, &flag);
1810 yprc_action(ctx, &list->actions[u]);
1811 }
Radek Krejci693262f2019-04-29 15:23:20 +02001812
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001813 LY_ARRAY_FOR(list->notifs, u) {
1814 ypr_open(ctx->out, &flag);
1815 yprc_notification(ctx, &list->notifs[u]);
1816 }
Radek Krejci693262f2019-04-29 15:23:20 +02001817 }
1818
1819 LEVEL--;
1820 ypr_close(ctx, flag);
1821}
1822
1823static void
1824yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001825{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001826 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001827 int flag = 0;
1828
Radek Krejci241f6b52020-05-21 18:13:49 +02001829 ly_print(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001830 LEVEL++;
1831
Radek Krejci693262f2019-04-29 15:23:20 +02001832 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1833 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001834
1835 LY_ARRAY_FOR(refine->musts, u) {
1836 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001837 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001838 }
1839
1840 if (refine->presence) {
1841 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001842 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001843 }
1844
1845 LY_ARRAY_FOR(refine->dflts, u) {
1846 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001847 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001848 }
1849
Radek Krejci693262f2019-04-29 15:23:20 +02001850 ypr_config(ctx, refine->flags, refine->exts, &flag);
1851 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001852
1853 if (refine->flags & LYS_SET_MIN) {
1854 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001855 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001856 }
1857 if (refine->flags & LYS_SET_MAX) {
1858 ypr_open(ctx->out, &flag);
1859 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001860 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001861 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001862 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001863 }
1864 }
1865
1866 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1867 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1868
1869 LEVEL--;
1870 ypr_close(ctx, flag);
1871}
1872
1873static void
Radek Krejci693262f2019-04-29 15:23:20 +02001874yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001875{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001876 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877 struct lysp_node *child;
1878
Radek Krejci241f6b52020-05-21 18:13:49 +02001879 ly_print(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001880 LEVEL++;
1881
Radek Krejci693262f2019-04-29 15:23:20 +02001882 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1883 yprp_when(ctx, aug->when, NULL);
1884 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1885 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001886 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1887 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1888
1889 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001890 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001891 }
1892
1893 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001894 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001895 }
1896
1897 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001898 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001899 }
1900
1901 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001902 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001903}
1904
Radek Krejcid3ca0632019-04-16 16:54:54 +02001905static void
Radek Krejci693262f2019-04-29 15:23:20 +02001906yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001907{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001908 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001909 int flag = 0;
1910 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1911
Radek Krejci693262f2019-04-29 15:23:20 +02001912 yprp_node_common1(ctx, node, &flag);
1913 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001914
1915 LY_ARRAY_FOR(uses->refines, u) {
1916 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001917 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001918 }
1919
1920 LY_ARRAY_FOR(uses->augments, u) {
1921 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001922 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001923 }
1924
1925 LEVEL--;
1926 ypr_close(ctx, flag);
1927}
1928
1929static void
Radek Krejci693262f2019-04-29 15:23:20 +02001930yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001931{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001932 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001933 int flag = 0;
1934 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1935
Radek Krejci693262f2019-04-29 15:23:20 +02001936 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001937
1938 LY_ARRAY_FOR(any->musts, u) {
1939 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001940 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001941 }
1942
Radek Krejci693262f2019-04-29 15:23:20 +02001943 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001944
1945 LEVEL--;
1946 ypr_close(ctx, flag);
1947}
1948
1949static void
Radek Krejci693262f2019-04-29 15:23:20 +02001950yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001951{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001952 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001953 int flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001954 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001955
Radek Krejci693262f2019-04-29 15:23:20 +02001956 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001957
Radek Krejci693262f2019-04-29 15:23:20 +02001958 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001959 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001960 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001961 }
1962
Radek Krejci693262f2019-04-29 15:23:20 +02001963 yprc_node_common2(ctx, node, &flag);
1964
Radek Krejcid3ca0632019-04-16 16:54:54 +02001965 LEVEL--;
1966 ypr_close(ctx, flag);
1967}
1968
1969static void
Radek Krejci693262f2019-04-29 15:23:20 +02001970yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001971{
Radek Krejci56cc0872019-04-30 09:22:27 +02001972 LYOUT_CHECK(ctx->out);
1973
Radek Krejcid3ca0632019-04-16 16:54:54 +02001974 switch (node->nodetype) {
1975 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001976 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001977 break;
1978 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001979 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001980 break;
1981 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001982 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001983 break;
1984 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001985 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001986 break;
1987 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001988 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001989 break;
1990 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001991 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001992 break;
1993 case LYS_ANYXML:
1994 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001995 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001996 break;
1997 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001998 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001999 break;
2000 default:
2001 break;
2002 }
2003}
2004
2005static void
Radek Krejci693262f2019-04-29 15:23:20 +02002006yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
2007{
Radek Krejci56cc0872019-04-30 09:22:27 +02002008 LYOUT_CHECK(ctx->out);
2009
Radek Krejci693262f2019-04-29 15:23:20 +02002010 switch (node->nodetype) {
2011 case LYS_CONTAINER:
2012 yprc_container(ctx, node);
2013 break;
2014 case LYS_CHOICE:
2015 yprc_choice(ctx, node);
2016 break;
2017 case LYS_LEAF:
2018 yprc_leaf(ctx, node);
2019 break;
2020 case LYS_LEAFLIST:
2021 yprc_leaflist(ctx, node);
2022 break;
2023 case LYS_LIST:
2024 yprc_list(ctx, node);
2025 break;
2026 case LYS_ANYXML:
2027 case LYS_ANYDATA:
2028 yprc_anydata(ctx, node);
2029 break;
2030 default:
2031 break;
2032 }
2033}
2034
2035static void
2036yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002037{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002038 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002039 struct lysp_deviate_add *add;
2040 struct lysp_deviate_rpl *rpl;
2041 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08002042 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002043
Radek Krejci241f6b52020-05-21 18:13:49 +02002044 ly_print(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045 LEVEL++;
2046
Radek Krejci693262f2019-04-29 15:23:20 +02002047 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002048 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
2049 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
2050
fredgan2b11ddb2019-10-21 11:03:39 +08002051 LY_LIST_FOR(deviation->deviates, elem) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002052 ly_print(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08002053 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
2054 if (elem->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002055 ly_print(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002056 LEVEL++;
2057
fredgan2b11ddb2019-10-21 11:03:39 +08002058 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002059 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +02002060 ly_print(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002061 continue;
2062 }
fredgan2b11ddb2019-10-21 11:03:39 +08002063 } else if (elem->mod == LYS_DEV_ADD) {
2064 add = (struct lysp_deviate_add*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002065 ly_print(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002066 LEVEL++;
2067
Radek Krejci693262f2019-04-29 15:23:20 +02002068 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
2069 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002070 LY_ARRAY_FOR(add->musts, u) {
2071 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002072 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002073 LY_ARRAY_FOR(add->uniques, u) {
2074 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002075 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002076 LY_ARRAY_FOR(add->dflts, u) {
2077 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002078 }
Radek Krejci693262f2019-04-29 15:23:20 +02002079 ypr_config(ctx, add->flags, add->exts, NULL);
2080 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002081 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002082 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002083 }
2084 if (add->flags & LYS_SET_MAX) {
2085 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002086 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002087 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002088 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002089 }
2090 }
fredgan2b11ddb2019-10-21 11:03:39 +08002091 } else if (elem->mod == LYS_DEV_REPLACE) {
2092 rpl = (struct lysp_deviate_rpl*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002093 ly_print(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094 LEVEL++;
2095
Radek Krejci693262f2019-04-29 15:23:20 +02002096 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002097 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002098 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002099 }
Radek Krejci693262f2019-04-29 15:23:20 +02002100 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
2101 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
2102 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2103 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002104 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002105 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002106 }
2107 if (rpl->flags & LYS_SET_MAX) {
2108 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002109 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002110 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002111 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002112 }
2113 }
fredgan2b11ddb2019-10-21 11:03:39 +08002114 } else if (elem->mod == LYS_DEV_DELETE) {
2115 del = (struct lysp_deviate_del*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002116 ly_print(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002117 LEVEL++;
2118
Radek Krejci693262f2019-04-29 15:23:20 +02002119 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2120 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002121 LY_ARRAY_FOR(del->musts, u) {
2122 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002123 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002124 LY_ARRAY_FOR(del->uniques, u) {
2125 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002126 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002127 LY_ARRAY_FOR(del->dflts, u) {
2128 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002129 }
2130 }
2131
2132 LEVEL--;
2133 ypr_close(ctx, 1);
2134 }
2135
2136 LEVEL--;
2137 ypr_close(ctx, 1);
2138}
2139
Michal Vasko7c8439f2020-08-05 13:25:19 +02002140static void
2141yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002142{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002143 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002144
Radek Krejcid3ca0632019-04-16 16:54:54 +02002145 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002146 ly_print(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002147 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002148 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2149 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002150 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002151 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002152 }
Radek Krejci693262f2019-04-29 15:23:20 +02002153 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2154 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002155 LEVEL--;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002156 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002157 }
2158 LY_ARRAY_FOR(modp->includes, u) {
2159 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002160 ly_print(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002161 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002162 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002163 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002164 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002165 }
Radek Krejci693262f2019-04-29 15:23:20 +02002166 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2167 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002168 LEVEL--;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002169 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002170 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002171 ly_print(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002172 }
2173 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002174}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002175
Michal Vasko7c8439f2020-08-05 13:25:19 +02002176static void
2177yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2178{
2179 LY_ARRAY_COUNT_TYPE u;
2180 struct lysp_node *data;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002181
Radek Krejcid3ca0632019-04-16 16:54:54 +02002182 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002183 ly_print(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002184 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002185 }
2186 if (modp->exts) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002187 ly_print(ctx->out, "\n");
2188 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002189 }
2190
2191 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002192 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002193 }
2194
2195 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002196 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002197 }
2198
2199 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002200 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002201 }
2202
2203 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002204 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002205 }
2206
2207 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002208 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002209 }
2210
2211 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002212 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002213 }
2214
2215 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002216 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002217 }
2218
2219 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002220 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002221 }
2222
2223 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002224 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002225 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002226}
2227
2228LY_ERR
2229yang_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp)
2230{
2231 LY_ARRAY_COUNT_TYPE u;
2232 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED}, *ctx = &ctx_;
2233
2234 ly_print(ctx->out, "%*smodule %s {\n", INDENT, module->name);
2235 LEVEL++;
2236
2237 /* module-header-stmts */
2238 if (module->version) {
2239 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
2240 }
2241
2242 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2243 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2244
2245 /* linkage-stmts (import/include) */
2246 yang_print_parsed_linkage(ctx, modp);
2247
2248 /* meta-stmts */
2249 if (module->org || module->contact || module->dsc || module->ref) {
2250 ly_print(out, "\n");
2251 }
2252 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2253 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2254 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2255 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2256
2257 /* revision-stmts */
2258 if (modp->revs) {
2259 ly_print(out, "\n");
2260 }
2261 LY_ARRAY_FOR(modp->revs, u) {
2262 yprp_revision(ctx, &modp->revs[u]);
2263 }
2264 /* body-stmts */
2265 yang_print_parsed_body(ctx, modp);
2266
2267 LEVEL--;
2268 ly_print(out, "%*s}\n", INDENT);
2269 ly_print_flush(out);
2270
2271 return LY_SUCCESS;
2272}
2273
2274static void
2275yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2276{
2277 ly_print(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->belongsto);
2278 LEVEL++;
2279 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
2280 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2281 LEVEL--;
2282 ly_print(ctx->out, "%*s}\n", INDENT);
2283}
2284
2285LY_ERR
2286yang_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp)
2287{
2288 LY_ARRAY_COUNT_TYPE u;
2289 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED}, *ctx = &ctx_;
2290
2291 ly_print(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
2292 LEVEL++;
2293
2294 /* submodule-header-stmts */
2295 if (submodp->version) {
2296 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
2297 }
2298
2299 yprp_belongsto(ctx, submodp);
2300
2301 /* linkage-stmts (import/include) */
2302 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2303
2304 /* meta-stmts */
2305 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
2306 ly_print(out, "\n");
2307 }
2308 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2309 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
2310 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2311 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
2312
2313 /* revision-stmts */
2314 if (submodp->revs) {
2315 ly_print(out, "\n");
2316 }
2317 LY_ARRAY_FOR(submodp->revs, u) {
2318 yprp_revision(ctx, &submodp->revs[u]);
2319 }
2320 /* body-stmts */
2321 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002322
2323 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002324 ly_print(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002325 ly_print_flush(out);
2326
2327 return LY_SUCCESS;
2328}
2329
2330LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002331yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002332{
2333 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options}, *ctx = &ctx_;
2334
2335 yprc_node(ctx, node);
2336
2337 ly_print_flush(out);
2338 return LY_SUCCESS;
2339}
2340
2341LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002342yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002343{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002344 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002345 struct lysc_node *data;
2346 struct lysc_module *modc = module->compiled;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002347 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002348
Radek Krejci241f6b52020-05-21 18:13:49 +02002349 ly_print(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002350 LEVEL++;
2351
Radek Krejci693262f2019-04-29 15:23:20 +02002352 /* module-header-stmts */
2353 if (module->version) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002354 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 +02002355 }
2356 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2357 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2358
Michal Vasko7c8439f2020-08-05 13:25:19 +02002359 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002360
2361 /* meta-stmts */
2362 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002363 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002364 }
2365 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2366 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2367 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2368 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2369
2370 /* revision-stmts */
2371 if (module->revision) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002372 ly_print(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002373 }
2374
2375 /* body-stmts */
2376 if (modc->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002377 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002378 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2379 }
2380
2381 LY_ARRAY_FOR(modc->features, u) {
2382 yprc_feature(ctx, &modc->features[u]);
2383 }
2384
2385 LY_ARRAY_FOR(modc->identities, u) {
2386 yprc_identity(ctx, &modc->identities[u]);
2387 }
2388
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08002389 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002390 LY_LIST_FOR(modc->data, data) {
2391 yprc_node(ctx, data);
2392 }
Radek Krejci693262f2019-04-29 15:23:20 +02002393
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002394 LY_ARRAY_FOR(modc->rpcs, u) {
2395 yprc_action(ctx, &modc->rpcs[u]);
2396 }
Radek Krejci693262f2019-04-29 15:23:20 +02002397
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002398 LY_ARRAY_FOR(modc->notifs, u) {
2399 yprc_notification(ctx, &modc->notifs[u]);
2400 }
Radek Krejci693262f2019-04-29 15:23:20 +02002401 }
2402
2403 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002404 ly_print(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002405 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002406
2407 return LY_SUCCESS;
2408}