blob: 47ae071ed518fadbf1a67969da0637cff205e445 [file] [log] [blame]
Radek Krejcid3ca0632019-04-16 16:54:54 +02001/**
2 * @file printer_yang.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief YANG printer
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcid3ca0632019-04-16 16:54:54 +020016
Radek Krejci693262f2019-04-29 15:23:20 +020017#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Radek Krejci693262f2019-04-29 15:23:20 +020022
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020024#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "plugins_types.h"
27#include "printer.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020028#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "tree_data.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020032#include "tree_schema.h"
33#include "tree_schema_internal.h"
Radek Krejci693262f2019-04-29 15:23:20 +020034#include "xpath.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020035
Radek Krejcie7b95092019-05-15 11:03:07 +020036/**
37 * @brief Types of the YANG printers
38 */
Radek Krejci693262f2019-04-29 15:23:20 +020039enum schema_type {
Radek Krejcie7b95092019-05-15 11:03:07 +020040 YPR_PARSED, /**< YANG printer of the parsed schema */
41 YPR_COMPILED /**< YANG printer of the compiled schema */
Radek Krejci693262f2019-04-29 15:23:20 +020042};
43
Radek Krejcie7b95092019-05-15 11:03:07 +020044/**
45 * @brief YANG printer context.
46 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020047struct ypr_ctx {
Radek Krejci241f6b52020-05-21 18:13:49 +020048 struct ly_out *out; /**< output specification */
Radek Krejcie7b95092019-05-15 11:03:07 +020049 unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
50 const struct lys_module *module; /**< schema to print */
51 enum schema_type schema; /**< type of the schema to print */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080052 int options; /**< Schema output options (see @ref schemaprinterflags). */
Radek Krejcid3ca0632019-04-16 16:54:54 +020053};
54
Radek Krejcie7b95092019-05-15 11:03:07 +020055/**
56 * @brief Print the given text as content of a double quoted YANG string,
57 * including encoding characters that have special meanings. The quotation marks
58 * are not printed.
59 *
60 * Follows RFC 7950, section 6.1.3.
61 *
62 * @param[in] out Output specification.
63 * @param[in] text String to be printed.
Radek Krejcif56e2a42019-09-09 14:15:25 +020064 * @param[in] len Length of the string from @p text to be printed. In case of -1,
Radek Krejcie7b95092019-05-15 11:03:07 +020065 * the @p text is printed completely as a NULL-terminated string.
66 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020067static void
Radek Krejci241f6b52020-05-21 18:13:49 +020068ypr_encode(struct ly_out *out, const char *text, int len)
Radek Krejcid3ca0632019-04-16 16:54:54 +020069{
70 int i, start_len;
71 const char *start;
72 char special = 0;
73
74 if (!len) {
75 return;
76 }
77
78 if (len < 0) {
79 len = strlen(text);
80 }
81
82 start = text;
83 start_len = 0;
84 for (i = 0; i < len; ++i) {
85 switch (text[i]) {
86 case '\n':
87 case '\t':
88 case '\"':
89 case '\\':
90 special = text[i];
91 break;
92 default:
93 ++start_len;
94 break;
95 }
96
97 if (special) {
Radek Krejci241f6b52020-05-21 18:13:49 +020098 ly_write(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +020099 switch (special) {
100 case '\n':
Radek Krejci241f6b52020-05-21 18:13:49 +0200101 ly_write(out, "\\n", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200102 break;
103 case '\t':
Radek Krejci241f6b52020-05-21 18:13:49 +0200104 ly_write(out, "\\t", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200105 break;
106 case '\"':
Radek Krejci241f6b52020-05-21 18:13:49 +0200107 ly_write(out, "\\\"", 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 }
113
114 start += start_len + 1;
115 start_len = 0;
116
117 special = 0;
118 }
119 }
120
Radek Krejci241f6b52020-05-21 18:13:49 +0200121 ly_write(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200122}
123
124static void
Radek Krejci241f6b52020-05-21 18:13:49 +0200125ypr_open(struct ly_out *out, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200126{
127 if (flag && !*flag) {
128 *flag = 1;
Radek Krejci241f6b52020-05-21 18:13:49 +0200129 ly_print(out, " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200130 }
131}
132
133static void
134ypr_close(struct ypr_ctx *ctx, int flag)
135{
136 if (flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200137 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200138 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200139 ly_print(ctx->out, ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200140 }
141}
142
143static void
144ypr_text(struct ypr_ctx *ctx, const char *name, const char *text, int singleline, int closed)
145{
146 const char *s, *t;
147
148 if (singleline) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200149 ly_print(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200150 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200151 ly_print(ctx->out, "%*s%s\n", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200152 LEVEL++;
153
Radek Krejci241f6b52020-05-21 18:13:49 +0200154 ly_print(ctx->out, "%*s\"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200155 }
156 t = text;
157 while ((s = strchr(t, '\n'))) {
158 ypr_encode(ctx->out, t, s - t);
Radek Krejci241f6b52020-05-21 18:13:49 +0200159 ly_print(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200160 t = s + 1;
161 if (*t != '\n') {
Radek Krejci241f6b52020-05-21 18:13:49 +0200162 ly_print(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200163 }
164 }
165
166 ypr_encode(ctx->out, t, strlen(t));
167 if (closed) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200168 ly_print(ctx->out, "\";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200169 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200170 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200171 }
172 if (!singleline) {
173 LEVEL--;
174 }
175}
176
177static void
Radek Krejci693262f2019-04-29 15:23:20 +0200178yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200179{
180 struct lysp_stmt *childstmt;
181 const char *s, *t;
182
183 if (stmt->arg) {
184 if (stmt->flags) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200185 ly_print(ctx->out, "%*s%s\n", INDENT, stmt->stmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200186 LEVEL++;
Radek Krejci241f6b52020-05-21 18:13:49 +0200187 ly_print(ctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'');
Radek Krejcid3ca0632019-04-16 16:54:54 +0200188 t = stmt->arg;
189 while ((s = strchr(t, '\n'))) {
190 ypr_encode(ctx->out, t, s - t);
Radek Krejci241f6b52020-05-21 18:13:49 +0200191 ly_print(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200192 t = s + 1;
193 if (*t != '\n') {
Radek Krejci241f6b52020-05-21 18:13:49 +0200194 ly_print(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200195 }
196 }
197 LEVEL--;
198 ypr_encode(ctx->out, t, strlen(t));
Radek Krejci241f6b52020-05-21 18:13:49 +0200199 ly_print(ctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200200 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200201 ly_print(ctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200202 }
203 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200204 ly_print(ctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200205 }
206
207 if (stmt->child) {
208 LEVEL++;
209 LY_LIST_FOR(stmt->child, childstmt) {
Radek Krejci693262f2019-04-29 15:23:20 +0200210 yprp_stmt(ctx, childstmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200211 }
212 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +0200213 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200214 }
215}
216
217/**
218 * @param[in] count Number of extensions to print, 0 to print them all.
219 */
220static void
Radek Krejci693262f2019-04-29 15:23:20 +0200221yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200222 struct lysp_ext_instance *ext, int *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200223{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200224 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225 struct lysp_stmt *stmt;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200226 int child_presence;
227 const char *argument;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200228
229 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200230 count = LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200231 }
232 LY_ARRAY_FOR(ext, u) {
233 if (!count) {
234 break;
235 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200236
Radek Krejcid3ca0632019-04-16 16:54:54 +0200237 count--;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200238 if (ext->insubstmt != substmt || ext->insubstmt_index != substmt_index) {
239 continue;
240 }
241
242 if (!ext->compiled && ext->yin) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200243 ly_print(ctx->out, "%*s%s; // Model comes from different input format, extensions must be resolved first.\n", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200244 continue;
245 }
246
247
248 ypr_open(ctx->out, flag);
249 argument = NULL;
250 if (ext[u].compiled) {
251 argument = ext[u].compiled->argument;
252 } else {
253 argument = ext[u].argument;
254 }
255 if (argument) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200256 ly_print(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200257 ypr_encode(ctx->out, argument, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200258 ly_print(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200259 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200260 ly_print(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200261 }
262
263 child_presence = 0;
264 LEVEL++;
265 LY_LIST_FOR(ext[u].child, stmt) {
266 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
267 continue;
268 }
269 if (!child_presence) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200270 ly_print(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200271 child_presence = 1;
272 }
273 yprp_stmt(ctx, stmt);
274 }
275 LEVEL--;
276 if (child_presence) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200277 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200278 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200279 ly_print(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200280 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200281 }
282}
283
Radek Krejci693262f2019-04-29 15:23:20 +0200284/**
285 * @param[in] count Number of extensions to print, 0 to print them all.
286 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200287static void
Radek Krejci693262f2019-04-29 15:23:20 +0200288yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200289 struct lysc_ext_instance *ext, int *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200290{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200291 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200292
293 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200294 count = LY_ARRAY_COUNT(ext);
Radek Krejci693262f2019-04-29 15:23:20 +0200295 }
296 LY_ARRAY_FOR(ext, u) {
297 if (!count) {
298 break;
299 }
300 /* TODO compiled extensions */
301 (void) ctx;
302 (void) substmt;
303 (void) substmt_index;
304 (void) flag;
305
306 count--;
307 }
308}
309
310static void
311ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200312{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200313 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200314 int extflag = 0;
315
316 if (!text) {
317 /* nothing to print */
318 return;
319 }
320
321 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200322 ly_print(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200323 } else {
324 ypr_text(ctx, ext_substmt_info[substmt].name, text,
325 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
326 }
327
328 LEVEL++;
329 LY_ARRAY_FOR(ext, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200330 if (((struct lysp_ext_instance*)ext)[u].insubstmt != substmt || ((struct lysp_ext_instance*)ext)[u].insubstmt_index != substmt_index) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200331 continue;
332 }
Radek Krejci693262f2019-04-29 15:23:20 +0200333 if (ctx->schema == YPR_PARSED) {
334 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance*)ext)[u], &extflag, 1);
335 } else {
336 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance*)ext)[u], &extflag, 1);
337 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200338 }
339 LEVEL--;
340 ypr_close(ctx, extflag);
341}
342
343static void
Radek Krejci693262f2019-04-29 15:23:20 +0200344ypr_unsigned(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, unsigned int attr_value, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200345{
346 char *str;
347
348 if (asprintf(&str, "%u", attr_value) == -1) {
349 LOGMEM(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +0200350 ctx->out->status = LY_EMEM;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200351 return;
352 }
353 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200354 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200355 free(str);
356}
357
358static void
Radek Krejci693262f2019-04-29 15:23:20 +0200359ypr_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 +0200360{
361 char *str;
362
363 if (asprintf(&str, "%d", attr_value) == -1) {
364 LOGMEM(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +0200365 ctx->out->status = LY_EMEM;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200366 return;
367 }
368 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200369 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200370 free(str);
371}
372
373static void
Radek Krejci693262f2019-04-29 15:23:20 +0200374yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200375{
376 if (rev->dsc || rev->ref || rev->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200377 ly_print(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200378 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200379 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
380 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
381 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200382 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +0200383 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200384 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200385 ly_print(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200386 }
387}
388
389static void
Radek Krejci693262f2019-04-29 15:23:20 +0200390ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200391{
392 if (flags & LYS_MAND_MASK) {
393 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200394 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200395 }
396}
397
398static void
Radek Krejci693262f2019-04-29 15:23:20 +0200399ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200400{
401 if (flags & LYS_CONFIG_MASK) {
402 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200403 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200404 }
405}
406
407static void
Radek Krejci693262f2019-04-29 15:23:20 +0200408ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200409{
410 const char *status = NULL;
411
412 if (flags & LYS_STATUS_CURR) {
413 ypr_open(ctx->out, flag);
414 status = "current";
415 } else if (flags & LYS_STATUS_DEPRC) {
416 ypr_open(ctx->out, flag);
417 status = "deprecated";
418 } else if (flags & LYS_STATUS_OBSLT) {
419 ypr_open(ctx->out, flag);
420 status = "obsolete";
421 }
Radek Krejci693262f2019-04-29 15:23:20 +0200422
423 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200424}
425
426static void
Radek Krejci693262f2019-04-29 15:23:20 +0200427ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200428{
429 if (dsc) {
430 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200431 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200432 }
433}
434
435static void
Radek Krejci693262f2019-04-29 15:23:20 +0200436ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200437{
438 if (ref) {
439 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200440 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200441 }
442}
443
444static void
Radek Krejci693262f2019-04-29 15:23:20 +0200445yprp_iffeatures(struct ypr_ctx *ctx, const char **iff, struct lysp_ext_instance *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200446{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200447 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200448 int extflag;
449
450 LY_ARRAY_FOR(iff, u) {
451 ypr_open(ctx->out, flag);
452 extflag = 0;
453
Radek Krejci241f6b52020-05-21 18:13:49 +0200454 ly_print(ctx->out, "%*sif-feature \"%s\"", INDENT, iff[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200455
456 /* extensions */
457 LEVEL++;
458 LY_ARRAY_FOR(exts, u) {
459 if (exts[u].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[u].insubstmt_index != u) {
460 continue;
461 }
Radek Krejci693262f2019-04-29 15:23:20 +0200462 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[u], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200463 }
464 LEVEL--;
465 ypr_close(ctx, extflag);
466 }
467}
468
469static void
Radek Krejci693262f2019-04-29 15:23:20 +0200470yprc_iffeature(struct ypr_ctx *ctx, struct lysc_iffeature *feat, int *index_e, int *index_f)
471{
472 int brackets_flag = *index_e;
473 uint8_t op;
474
475 op = lysc_iff_getop(feat->expr, *index_e);
476 (*index_e)++;
477
478 switch (op) {
479 case LYS_IFF_F:
480 if (ctx->module == feat->features[*index_f]->module) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200481 ly_print(ctx->out, "%s", feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200482 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200483 ly_print(ctx->out, "%s:%s", feat->features[*index_f]->module->prefix, feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200484 }
485 (*index_f)++;
486 break;
487 case LYS_IFF_NOT:
Radek Krejci241f6b52020-05-21 18:13:49 +0200488 ly_print(ctx->out, "not ");
Radek Krejci693262f2019-04-29 15:23:20 +0200489 yprc_iffeature(ctx, feat, index_e, index_f);
490 break;
491 case LYS_IFF_AND:
492 if (brackets_flag) {
493 /* AND need brackets only if previous op was not */
494 if (*index_e < 2 || lysc_iff_getop(feat->expr, *index_e - 2) != LYS_IFF_NOT) {
495 brackets_flag = 0;
496 }
497 }
498 /* falls through */
499 case LYS_IFF_OR:
500 if (brackets_flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200501 ly_print(ctx->out, "(");
Radek Krejci693262f2019-04-29 15:23:20 +0200502 }
503 yprc_iffeature(ctx, feat, index_e, index_f);
Radek Krejci241f6b52020-05-21 18:13:49 +0200504 ly_print(ctx->out, " %s ", op == LYS_IFF_OR ? "or" : "and");
Radek Krejci693262f2019-04-29 15:23:20 +0200505 yprc_iffeature(ctx, feat, index_e, index_f);
506 if (brackets_flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200507 ly_print(ctx->out, ")");
Radek Krejci693262f2019-04-29 15:23:20 +0200508 }
509 }
510}
511
512static void
513yprc_iffeatures(struct ypr_ctx *ctx, struct lysc_iffeature *iff, struct lysc_ext_instance *exts, int *flag)
514{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200515 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +0200516 int extflag;
517
518 LY_ARRAY_FOR(iff, u) {
519 int index_e = 0, index_f = 0;
520
521 ypr_open(ctx->out, flag);
522 extflag = 0;
523
Radek Krejci241f6b52020-05-21 18:13:49 +0200524 ly_print(ctx->out, "%*sif-feature \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200525 yprc_iffeature(ctx, iff, &index_e, &index_f);
Radek Krejci241f6b52020-05-21 18:13:49 +0200526 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200527
528 /* extensions */
529 LEVEL++;
Radek Krejci334ccc72019-06-12 13:49:29 +0200530 LY_ARRAY_FOR(exts, v) {
531 if (exts[v].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[v].insubstmt_index != u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200532 continue;
533 }
Radek Krejci334ccc72019-06-12 13:49:29 +0200534 yprc_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200535 }
536 LEVEL--;
537 ypr_close(ctx, extflag);
538 }
539}
540
541static void
542yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200543{
544 int flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200545 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200546
Radek Krejci241f6b52020-05-21 18:13:49 +0200547 ly_print(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200548 LEVEL++;
549
550 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200551 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200552 }
553
554 if (ext->argument) {
555 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200556 ly_print(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200557 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200558 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200559 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200560 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 +0200561 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200562 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200563 }
564 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200565 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200566 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200567 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200568 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200569 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200570 ypr_close(ctx, flag2);
571 }
572
Radek Krejci693262f2019-04-29 15:23:20 +0200573 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200574 ypr_description(ctx, ext->dsc, ext->exts, &flag);
575 ypr_reference(ctx, ext->ref, ext->exts, &flag);
576
577 LEVEL--;
578 ypr_close(ctx, flag);
579}
580
581static void
Radek Krejci693262f2019-04-29 15:23:20 +0200582yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200583{
584 int flag = 0;
585
Radek Krejci241f6b52020-05-21 18:13:49 +0200586 ly_print(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200587 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200588 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
589 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
590 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200591 ypr_description(ctx, feat->dsc, feat->exts, &flag);
592 ypr_reference(ctx, feat->ref, feat->exts, &flag);
593 LEVEL--;
594 ypr_close(ctx, flag);
595}
596
597static void
Radek Krejci693262f2019-04-29 15:23:20 +0200598yprc_feature(struct ypr_ctx *ctx, const struct lysc_feature *feat)
599{
600 int flag = 0;
601
Radek Krejci241f6b52020-05-21 18:13:49 +0200602 ly_print(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200603 LEVEL++;
604 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
605 yprc_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
606 ypr_status(ctx, feat->flags, feat->exts, &flag);
607 ypr_description(ctx, feat->dsc, feat->exts, &flag);
608 ypr_reference(ctx, feat->ref, feat->exts, &flag);
609 LEVEL--;
610 ypr_close(ctx, flag);
611}
612
613static void
614yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200615{
616 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200617 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200618
Radek Krejci241f6b52020-05-21 18:13:49 +0200619 ly_print(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200620 LEVEL++;
621
Radek Krejci693262f2019-04-29 15:23:20 +0200622 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
623 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200624
625 LY_ARRAY_FOR(ident->bases, u) {
626 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200627 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200628 }
629
Radek Krejci693262f2019-04-29 15:23:20 +0200630 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200631 ypr_description(ctx, ident->dsc, ident->exts, &flag);
632 ypr_reference(ctx, ident->ref, ident->exts, &flag);
633
634 LEVEL--;
635 ypr_close(ctx, flag);
636}
637
638static void
Radek Krejci693262f2019-04-29 15:23:20 +0200639yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
640{
641 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200642 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200643
Radek Krejci241f6b52020-05-21 18:13:49 +0200644 ly_print(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200645 LEVEL++;
646
647 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
648 yprc_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
649
650 LY_ARRAY_FOR(ident->derived, u) {
651 ypr_open(ctx->out, &flag);
652 if (ctx->module != ident->derived[u]->module) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200653 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 +0200654 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200655 ly_print(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200656 }
657 }
658
659 ypr_status(ctx, ident->flags, ident->exts, &flag);
660 ypr_description(ctx, ident->dsc, ident->exts, &flag);
661 ypr_reference(ctx, ident->ref, ident->exts, &flag);
662
663 LEVEL--;
664 ypr_close(ctx, flag);
665}
666
667static void
668yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200669{
670 int inner_flag = 0;
671
672 if (!restr) {
673 return;
674 }
675
676 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200677 ly_print(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200678 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 +0200679 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200680
681 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200682 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200683 if (restr->arg[0] == 0x15) {
684 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
685 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200686 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200687 }
688 if (restr->emsg) {
689 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200690 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200691 }
692 if (restr->eapptag) {
693 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200694 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200695 }
Radek Krejci693262f2019-04-29 15:23:20 +0200696 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
697 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
698
Radek Krejcid3ca0632019-04-16 16:54:54 +0200699 LEVEL--;
700 ypr_close(ctx, inner_flag);
701}
702
703static void
Radek Krejci693262f2019-04-29 15:23:20 +0200704yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, int *flag)
705{
706 int inner_flag = 0;
707
708 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200709 ly_print(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200710 ypr_encode(ctx->out, must->cond->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200711 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200712
713 LEVEL++;
714 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
715 if (must->emsg) {
716 ypr_open(ctx->out, &inner_flag);
717 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
718 }
719 if (must->eapptag) {
720 ypr_open(ctx->out, &inner_flag);
721 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
722 }
723 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
724 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
725
726 LEVEL--;
727 ypr_close(ctx, inner_flag);
728}
729
730static void
731yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, int *flag)
732{
733 int inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200734 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200735
Radek Krejci334ccc72019-06-12 13:49:29 +0200736 if (!range) {
737 return;
738 }
739
Radek Krejci693262f2019-04-29 15:23:20 +0200740 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200741 ly_print(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200742 LY_ARRAY_FOR(range->parts, u) {
743 if (u > 0) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200744 ly_print(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200745 }
746 if (range->parts[u].max_64 == range->parts[u].min_64) {
747 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200748 ly_print(ctx->out, "%"PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200749 } else { /* signed values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200750 ly_print(ctx->out, "%"PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200751 }
752 } else {
753 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200754 ly_print(ctx->out, "%"PRIu64"..%"PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200755 } else { /* signed values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200756 ly_print(ctx->out, "%"PRId64"..%"PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200757 }
758 }
759 }
Radek Krejci241f6b52020-05-21 18:13:49 +0200760 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200761
762 LEVEL++;
763 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
764 if (range->emsg) {
765 ypr_open(ctx->out, &inner_flag);
766 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
767 }
768 if (range->eapptag) {
769 ypr_open(ctx->out, &inner_flag);
770 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
771 }
772 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
773 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
774
775 LEVEL--;
776 ypr_close(ctx, inner_flag);
777}
778
779static void
780yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, int *flag)
781{
782 int inner_flag = 0;
783
784 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200785 ly_print(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200786 ypr_encode(ctx->out, pattern->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200787 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200788
789 LEVEL++;
790 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
791 if (pattern->inverted) {
792 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
793 ypr_open(ctx->out, &inner_flag);
794 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
795 }
796 if (pattern->emsg) {
797 ypr_open(ctx->out, &inner_flag);
798 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
799 }
800 if (pattern->eapptag) {
801 ypr_open(ctx->out, &inner_flag);
802 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
803 }
804 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
805 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
806
807 LEVEL--;
808 ypr_close(ctx, inner_flag);
809}
810
811static void
812yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200813{
814 int inner_flag = 0;
815
816 if (!when) {
817 return;
818 }
819 ypr_open(ctx->out, flag);
820
Radek Krejci241f6b52020-05-21 18:13:49 +0200821 ly_print(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200822 ypr_encode(ctx->out, when->cond, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200823 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200824
825 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200826 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200827 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
828 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
829 LEVEL--;
830 ypr_close(ctx, inner_flag);
831}
832
833static void
Radek Krejci693262f2019-04-29 15:23:20 +0200834yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, int *flag)
835{
836 int inner_flag = 0;
837
838 if (!when) {
839 return;
840 }
841 ypr_open(ctx->out, flag);
842
Radek Krejci241f6b52020-05-21 18:13:49 +0200843 ly_print(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200844 ypr_encode(ctx->out, when->cond->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200845 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200846
847 LEVEL++;
848 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
849 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
850 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
851 LEVEL--;
852 ypr_close(ctx, inner_flag);
853}
854
855static void
856yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200857{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200858 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200859 int inner_flag;
860
861 LY_ARRAY_FOR(items, u) {
862 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200863 if (type == LY_TYPE_BITS) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200864 ly_print(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200865 } else { /* LY_TYPE_ENUM */
Radek Krejci241f6b52020-05-21 18:13:49 +0200866 ly_print(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200867 ypr_encode(ctx->out, items[u].name, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200868 ly_print(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200869 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200870 inner_flag = 0;
871 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200872 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
873 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200874 if (items[u].flags & LYS_SET_VALUE) {
875 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200876 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200877 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200878 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200879 }
880 }
Radek Krejci693262f2019-04-29 15:23:20 +0200881 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200882 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
883 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
884 LEVEL--;
885 ypr_close(ctx, inner_flag);
886 }
887}
888
889static void
Radek Krejci693262f2019-04-29 15:23:20 +0200890yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200891{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200892 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200893 int flag = 0;
894
Radek Krejci241f6b52020-05-21 18:13:49 +0200895 ly_print(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200896 LEVEL++;
897
Radek Krejci693262f2019-04-29 15:23:20 +0200898 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200899
Radek Krejci693262f2019-04-29 15:23:20 +0200900 yprp_restr(ctx, type->range, "range", &flag);
901 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200902 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200903 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200904 }
Radek Krejci693262f2019-04-29 15:23:20 +0200905 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
906 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200907
908 if (type->path) {
909 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200910 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200911 }
912 if (type->flags & LYS_SET_REQINST) {
913 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200914 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200915 }
916 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200917 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200918 }
919 LY_ARRAY_FOR(type->bases, u) {
920 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200921 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200922 }
923 LY_ARRAY_FOR(type->types, u) {
924 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200925 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200926 }
927
928 LEVEL--;
929 ypr_close(ctx, flag);
930}
931
932static void
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200933yprc_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 +0200934{
935 int dynamic;
936 const char *str;
937
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200938 str = value->realtype->plugin->print(value, LYD_JSON, lys_get_prefix, (void*)value_mod, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200939 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
940 if (dynamic) {
941 free((void*)str);
942 }
943}
944
945static void
Radek Krejci693262f2019-04-29 15:23:20 +0200946yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
947{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200948 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200949 int flag = 0;
950
Radek Krejci241f6b52020-05-21 18:13:49 +0200951 ly_print(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200952 LEVEL++;
953
954 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
955 if (type->dflt) {
956 ypr_open(ctx->out, &flag);
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200957 yprc_dflt_value(ctx, type->dflt, type->dflt_mod, type->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200958 }
959
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200960 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200961 case LY_TYPE_BINARY: {
962 struct lysc_type_bin *bin = (struct lysc_type_bin*)type;
963 yprc_range(ctx, bin->length, type->basetype, &flag);
964 break;
965 }
966 case LY_TYPE_UINT8:
967 case LY_TYPE_UINT16:
968 case LY_TYPE_UINT32:
969 case LY_TYPE_UINT64:
970 case LY_TYPE_INT8:
971 case LY_TYPE_INT16:
972 case LY_TYPE_INT32:
973 case LY_TYPE_INT64: {
974 struct lysc_type_num *num = (struct lysc_type_num*)type;
975 yprc_range(ctx, num->range, type->basetype, &flag);
976 break;
977 }
978 case LY_TYPE_STRING: {
979 struct lysc_type_str *str = (struct lysc_type_str*)type;
980 yprc_range(ctx, str->length, type->basetype, &flag);
981 LY_ARRAY_FOR(str->patterns, u) {
982 yprc_pattern(ctx, str->patterns[u], &flag);
983 }
984 break;
985 }
986 case LY_TYPE_BITS:
987 case LY_TYPE_ENUM: {
988 /* bits and enums structures are compatible */
989 struct lysc_type_bits *bits = (struct lysc_type_bits*)type;
990 LY_ARRAY_FOR(bits->bits, u) {
991 struct lysc_type_bitenum_item *item = &bits->bits[u];
992 int inner_flag = 0;
993
994 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200995 ly_print(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200996 ypr_encode(ctx->out, item->name, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200997 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200998 LEVEL++;
999 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
1000 yprc_iffeatures(ctx, item->iffeatures, item->exts, &inner_flag);
1001 if (type->basetype == LY_TYPE_BITS) {
1002 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
1003 } else { /* LY_TYPE_ENUM */
1004 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
1005 }
1006 ypr_status(ctx, item->flags, item->exts, &inner_flag);
1007 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
1008 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
1009 LEVEL--;
1010 ypr_close(ctx, inner_flag);
1011 }
1012 break;
1013 }
1014 case LY_TYPE_BOOL:
1015 case LY_TYPE_EMPTY:
1016 /* nothing to do */
1017 break;
1018 case LY_TYPE_DEC64: {
1019 struct lysc_type_dec *dec = (struct lysc_type_dec*)type;
1020 ypr_open(ctx->out, &flag);
1021 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
1022 yprc_range(ctx, dec->range, dec->basetype, &flag);
1023 break;
1024 }
1025 case LY_TYPE_IDENT: {
1026 struct lysc_type_identityref *ident = (struct lysc_type_identityref*)type;
1027 LY_ARRAY_FOR(ident->bases, u) {
1028 ypr_open(ctx->out, &flag);
1029 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
1030 }
1031 break;
1032 }
1033 case LY_TYPE_INST: {
1034 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid*)type;
1035 ypr_open(ctx->out, &flag);
1036 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
1037 break;
1038 }
1039 case LY_TYPE_LEAFREF: {
1040 struct lysc_type_leafref *lr = (struct lysc_type_leafref*)type;
1041 ypr_open(ctx->out, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +02001042 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path->expr, lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001043 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
1044 yprc_type(ctx, lr->realtype);
1045 break;
1046 }
1047 case LY_TYPE_UNION: {
1048 struct lysc_type_union *un = (struct lysc_type_union*)type;
1049 LY_ARRAY_FOR(un->types, u) {
1050 ypr_open(ctx->out, &flag);
1051 yprc_type(ctx, un->types[u]);
1052 }
1053 break;
1054 }
1055 default:
1056 LOGINT(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +02001057 ctx->out->status = LY_EINT;
Radek Krejci693262f2019-04-29 15:23:20 +02001058 }
1059
1060 LEVEL--;
1061 ypr_close(ctx, flag);
1062}
1063
1064static void
1065yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001066{
Radek Krejci56cc0872019-04-30 09:22:27 +02001067 LYOUT_CHECK(ctx->out);
1068
Radek Krejci241f6b52020-05-21 18:13:49 +02001069 ly_print(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001070 LEVEL++;
1071
Radek Krejci693262f2019-04-29 15:23:20 +02001072 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001073
Radek Krejci693262f2019-04-29 15:23:20 +02001074 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001075
1076 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +02001077 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001078 }
1079 if (tpdf->dflt) {
Radek Krejci693262f2019-04-29 15:23:20 +02001080 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001081 }
1082
Radek Krejci693262f2019-04-29 15:23:20 +02001083 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001084 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
1085 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
1086
1087 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001088 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001089}
1090
Radek Krejci693262f2019-04-29 15:23:20 +02001091static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
1092static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
1093static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001094
1095static void
Radek Krejci693262f2019-04-29 15:23:20 +02001096yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001097{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001098 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001099 int flag = 0;
1100 struct lysp_node *data;
1101
Radek Krejci56cc0872019-04-30 09:22:27 +02001102 LYOUT_CHECK(ctx->out);
1103
Radek Krejci241f6b52020-05-21 18:13:49 +02001104 ly_print(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001105 LEVEL++;
1106
Radek Krejci693262f2019-04-29 15:23:20 +02001107 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1108 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001109 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1110 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001111
1112 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001113 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001114 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001115 }
1116
1117 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001118 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001119 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001120 }
1121
1122 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001123 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001124 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001125 }
1126
1127 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001128 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001129 }
1130
1131 LEVEL--;
1132 ypr_close(ctx, flag);
1133}
1134
1135static void
Radek Krejci693262f2019-04-29 15:23:20 +02001136yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001137{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001138 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001139 struct lysp_node *data;
1140
1141 if (!inout->nodetype) {
1142 /* nodetype not set -> input/output is empty */
1143 return;
1144 }
1145 ypr_open(ctx->out, flag);
1146
Radek Krejci241f6b52020-05-21 18:13:49 +02001147 ly_print(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001148 LEVEL++;
1149
Radek Krejci693262f2019-04-29 15:23:20 +02001150 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001151 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001152 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001153 }
1154 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001155 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001156 }
1157 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001158 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001159 }
1160
1161 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001162 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001163 }
1164
1165 LEVEL--;
1166 ypr_close(ctx, 1);
1167}
1168
1169static void
Radek Krejci693262f2019-04-29 15:23:20 +02001170yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, int *flag)
1171{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001172 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001173 struct lysc_node *data;
1174
1175 if (!inout->data) {
1176 /* input/output is empty */
1177 return;
1178 }
1179 ypr_open(ctx->out, flag);
1180
Radek Krejci241f6b52020-05-21 18:13:49 +02001181 ly_print(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001182 LEVEL++;
1183
1184 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1185 LY_ARRAY_FOR(inout->musts, u) {
1186 yprc_must(ctx, &inout->musts[u], NULL);
1187 }
1188
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001189 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001190 LY_LIST_FOR(inout->data, data) {
1191 yprc_node(ctx, data);
1192 }
Radek Krejci693262f2019-04-29 15:23:20 +02001193 }
1194
1195 LEVEL--;
1196 ypr_close(ctx, 1);
1197}
1198
1199static void
1200yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001201{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001202 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001203 int flag = 0;
1204 struct lysp_node *data;
1205
Radek Krejci56cc0872019-04-30 09:22:27 +02001206 LYOUT_CHECK(ctx->out);
1207
Radek Krejci241f6b52020-05-21 18:13:49 +02001208 ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001209
1210 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001211 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1212 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001213
1214 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001215 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001216 }
Radek Krejci693262f2019-04-29 15:23:20 +02001217 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001218 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1219 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1220
1221 LY_ARRAY_FOR(notif->typedefs, u) {
1222 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001223 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001224 }
1225
1226 LY_ARRAY_FOR(notif->groupings, u) {
1227 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001228 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001229 }
1230
1231 LY_LIST_FOR(notif->data, data) {
1232 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001233 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001234 }
1235
1236 LEVEL--;
1237 ypr_close(ctx, flag);
1238}
1239
1240static void
Radek Krejci693262f2019-04-29 15:23:20 +02001241yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1242{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001243 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001244 int flag = 0;
1245 struct lysc_node *data;
1246
Radek Krejci56cc0872019-04-30 09:22:27 +02001247 LYOUT_CHECK(ctx->out);
1248
Radek Krejci241f6b52020-05-21 18:13:49 +02001249 ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001250
1251 LEVEL++;
1252 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1253 yprc_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
1254
1255 LY_ARRAY_FOR(notif->musts, u) {
1256 yprc_must(ctx, &notif->musts[u], &flag);
1257 }
1258 ypr_status(ctx, notif->flags, notif->exts, &flag);
1259 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1260 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1261
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001262 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001263 LY_LIST_FOR(notif->data, data) {
1264 ypr_open(ctx->out, &flag);
1265 yprc_node(ctx, data);
1266 }
Radek Krejci693262f2019-04-29 15:23:20 +02001267 }
1268
1269 LEVEL--;
1270 ypr_close(ctx, flag);
1271}
1272
1273static void
1274yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001275{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001276 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001277 int flag = 0;
1278
Radek Krejci56cc0872019-04-30 09:22:27 +02001279 LYOUT_CHECK(ctx->out);
1280
Radek Krejci241f6b52020-05-21 18:13:49 +02001281 ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001282
1283 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001284 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1285 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1286 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001287 ypr_description(ctx, action->dsc, action->exts, &flag);
1288 ypr_reference(ctx, action->ref, action->exts, &flag);
1289
1290 LY_ARRAY_FOR(action->typedefs, u) {
1291 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001292 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001293 }
1294
1295 LY_ARRAY_FOR(action->groupings, u) {
1296 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001297 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001298 }
1299
Radek Krejci693262f2019-04-29 15:23:20 +02001300 yprp_inout(ctx, &action->input, &flag);
1301 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001302
1303 LEVEL--;
1304 ypr_close(ctx, flag);
1305}
1306
1307static void
Radek Krejci693262f2019-04-29 15:23:20 +02001308yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1309{
1310 int flag = 0;
1311
Radek Krejci56cc0872019-04-30 09:22:27 +02001312 LYOUT_CHECK(ctx->out);
1313
Radek Krejci241f6b52020-05-21 18:13:49 +02001314 ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001315
1316 LEVEL++;
1317 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1318 yprc_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1319 ypr_status(ctx, action->flags, action->exts, &flag);
1320 ypr_description(ctx, action->dsc, action->exts, &flag);
1321 ypr_reference(ctx, action->ref, action->exts, &flag);
1322
1323 yprc_inout(ctx, action, &action->input, &flag);
1324 yprc_inout(ctx, action, &action->output, &flag);
1325
1326 LEVEL--;
1327 ypr_close(ctx, flag);
1328}
1329
1330static void
1331yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001332{
Radek Krejci241f6b52020-05-21 18:13:49 +02001333 ly_print(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001334 LEVEL++;
1335
Radek Krejci693262f2019-04-29 15:23:20 +02001336 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1337 yprp_when(ctx, node->when, flag);
1338 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001339}
1340
1341static void
Radek Krejci693262f2019-04-29 15:23:20 +02001342yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001343{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001344 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001345
Radek Krejci241f6b52020-05-21 18:13:49 +02001346 ly_print(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001347 LEVEL++;
1348
1349 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1350 LY_ARRAY_FOR(node->when, u) {
1351 yprc_when(ctx, node->when[u], flag);
1352 }
1353 yprc_iffeatures(ctx, node->iffeatures, node->exts, flag);
1354}
1355
1356/* macr oto unify the code */
1357#define YPR_NODE_COMMON2 \
1358 ypr_config(ctx, node->flags, node->exts, flag); \
1359 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1360 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1361 } \
1362 ypr_status(ctx, node->flags, node->exts, flag); \
1363 ypr_description(ctx, node->dsc, node->exts, flag); \
1364 ypr_reference(ctx, node->ref, node->exts, flag)
1365
1366static void
1367yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
1368{
1369 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001370}
1371
1372static void
Radek Krejci693262f2019-04-29 15:23:20 +02001373yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
1374{
1375 YPR_NODE_COMMON2;
1376}
1377
1378#undef YPR_NODE_COMMON2
1379
1380static void
1381yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001382{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001383 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001384 int flag = 0;
1385 struct lysp_node *child;
1386 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1387
Radek Krejci693262f2019-04-29 15:23:20 +02001388 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001389
1390 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001391 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001392 }
1393 if (cont->presence) {
1394 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001395 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001396 }
1397
Radek Krejci693262f2019-04-29 15:23:20 +02001398 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001399
1400 LY_ARRAY_FOR(cont->typedefs, u) {
1401 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001402 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001403 }
1404
1405 LY_ARRAY_FOR(cont->groupings, u) {
1406 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001407 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001408 }
1409
1410 LY_LIST_FOR(cont->child, child) {
1411 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001412 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001413 }
1414
1415 LY_ARRAY_FOR(cont->actions, u) {
1416 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001417 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001418 }
1419
1420 LY_ARRAY_FOR(cont->notifs, u) {
1421 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001422 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001423 }
1424
1425 LEVEL--;
1426 ypr_close(ctx, flag);
1427}
1428
1429static void
Radek Krejci693262f2019-04-29 15:23:20 +02001430yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1431{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001432 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001433 int flag = 0;
1434 struct lysc_node *child;
1435 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1436
1437 yprc_node_common1(ctx, node, &flag);
1438
1439 LY_ARRAY_FOR(cont->musts, u) {
1440 yprc_must(ctx, &cont->musts[u], &flag);
1441 }
1442 if (cont->flags & LYS_PRESENCE) {
1443 ypr_open(ctx->out, &flag);
1444 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1445 }
1446
1447 yprc_node_common2(ctx, node, &flag);
1448
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001449 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001450 LY_LIST_FOR(cont->child, child) {
1451 ypr_open(ctx->out, &flag);
1452 yprc_node(ctx, child);
1453 }
Radek Krejci693262f2019-04-29 15:23:20 +02001454
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001455 LY_ARRAY_FOR(cont->actions, u) {
1456 ypr_open(ctx->out, &flag);
1457 yprc_action(ctx, &cont->actions[u]);
1458 }
Radek Krejci693262f2019-04-29 15:23:20 +02001459
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001460 LY_ARRAY_FOR(cont->notifs, u) {
1461 ypr_open(ctx->out, &flag);
1462 yprc_notification(ctx, &cont->notifs[u]);
1463 }
Radek Krejci693262f2019-04-29 15:23:20 +02001464 }
1465
1466 LEVEL--;
1467 ypr_close(ctx, flag);
1468}
1469
1470static void
1471yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1472{
1473 int flag = 0;
1474 struct lysp_node *child;
1475 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1476
1477 yprp_node_common1(ctx, node, &flag);
1478 yprp_node_common2(ctx, node, &flag);
1479
1480 LY_LIST_FOR(cas->child, child) {
1481 ypr_open(ctx->out, &flag);
1482 yprp_node(ctx, child);
1483 }
1484
1485 LEVEL--;
1486 ypr_close(ctx, flag);
1487}
1488
1489static void
1490yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1491{
1492 int flag = 0;
1493 struct lysc_node *child;
1494
1495 yprc_node_common1(ctx, (struct lysc_node*)cs, &flag);
1496 yprc_node_common2(ctx, (struct lysc_node*)cs, &flag);
1497
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001498 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001499 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
1500 ypr_open(ctx->out, &flag);
1501 yprc_node(ctx, child);
1502 }
Radek Krejci693262f2019-04-29 15:23:20 +02001503 }
1504
1505 LEVEL--;
1506 ypr_close(ctx, flag);
1507}
1508
1509static void
1510yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001511{
1512 int flag = 0;
1513 struct lysp_node *child;
1514 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1515
Radek Krejci693262f2019-04-29 15:23:20 +02001516 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001517
1518 if (choice->dflt) {
1519 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001520 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001521 }
1522
Radek Krejci693262f2019-04-29 15:23:20 +02001523 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001524
1525 LY_LIST_FOR(choice->child, child) {
1526 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001527 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001528 }
1529
1530 LEVEL--;
1531 ypr_close(ctx, flag);
1532}
1533
1534static void
Radek Krejci693262f2019-04-29 15:23:20 +02001535yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1536{
1537 int flag = 0;
1538 struct lysc_node_case *cs;
1539 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1540
1541 yprc_node_common1(ctx, node, &flag);
1542
1543 if (choice->dflt) {
1544 ypr_open(ctx->out, &flag);
1545 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1546 }
1547
1548 yprc_node_common2(ctx, node, &flag);
1549
1550 for (cs = choice->cases; cs; cs = (struct lysc_node_case*)cs->next) {
1551 ypr_open(ctx->out, &flag);
1552 yprc_case(ctx, cs);
1553 }
1554
1555 LEVEL--;
1556 ypr_close(ctx, flag);
1557}
1558
1559static void
1560yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001561{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001562 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001563 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1564
Radek Krejci693262f2019-04-29 15:23:20 +02001565 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001566
Radek Krejci693262f2019-04-29 15:23:20 +02001567 yprp_type(ctx, &leaf->type);
1568 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001569 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001570 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001571 }
Radek Krejci693262f2019-04-29 15:23:20 +02001572 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001573
Radek Krejci693262f2019-04-29 15:23:20 +02001574 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001575
1576 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001577 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001578}
1579
1580static void
Radek Krejci693262f2019-04-29 15:23:20 +02001581yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1582{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001583 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001584 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1585
1586 yprc_node_common1(ctx, node, NULL);
1587
1588 yprc_type(ctx, leaf->type);
1589 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1590 LY_ARRAY_FOR(leaf->musts, u) {
1591 yprc_must(ctx, &leaf->musts[u], NULL);
1592 }
Radek Krejcia1911222019-07-22 17:24:50 +02001593
1594 if (leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001595 yprc_dflt_value(ctx, leaf->dflt, leaf->dflt_mod, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001596 }
Radek Krejci693262f2019-04-29 15:23:20 +02001597
1598 yprc_node_common2(ctx, node, NULL);
1599
1600 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001601 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001602}
1603
1604static void
1605yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001606{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001607 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001608 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1609
Radek Krejci693262f2019-04-29 15:23:20 +02001610 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001611
Radek Krejci693262f2019-04-29 15:23:20 +02001612 yprp_type(ctx, &llist->type);
1613 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001614 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001615 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001616 }
1617 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001618 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001619 }
1620
Radek Krejci693262f2019-04-29 15:23:20 +02001621 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001622
1623 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001624 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001625 }
1626 if (llist->flags & LYS_SET_MAX) {
1627 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001628 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001629 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001630 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001631 }
1632 }
1633
1634 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001635 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001636 }
1637
Radek Krejci693262f2019-04-29 15:23:20 +02001638 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001639 ypr_description(ctx, node->dsc, node->exts, NULL);
1640 ypr_reference(ctx, node->ref, node->exts, NULL);
1641
1642 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001643 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001644}
1645
1646static void
Radek Krejci693262f2019-04-29 15:23:20 +02001647yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1648{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001649 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001650 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1651
1652 yprc_node_common1(ctx, node, NULL);
1653
1654 yprc_type(ctx, llist->type);
1655 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1656 LY_ARRAY_FOR(llist->musts, u) {
1657 yprc_must(ctx, &llist->musts[u], NULL);
1658 }
1659 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001660 yprc_dflt_value(ctx, llist->dflts[u], llist->dflts_mods[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001661 }
1662
1663 ypr_config(ctx, node->flags, node->exts, NULL);
1664
1665 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1666 if (llist->max) {
1667 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1668 } else {
1669 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1670 }
1671
1672 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1673
1674 ypr_status(ctx, node->flags, node->exts, NULL);
1675 ypr_description(ctx, node->dsc, node->exts, NULL);
1676 ypr_reference(ctx, node->ref, node->exts, NULL);
1677
1678 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001679 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001680}
1681
1682static void
1683yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001684{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001685 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001686 int flag = 0;
1687 struct lysp_node *child;
1688 struct lysp_node_list *list = (struct lysp_node_list *)node;
1689
Radek Krejci693262f2019-04-29 15:23:20 +02001690 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001691
1692 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001693 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001694 }
1695 if (list->key) {
1696 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001697 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001698 }
1699 LY_ARRAY_FOR(list->uniques, u) {
1700 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001701 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001702 }
1703
Radek Krejci693262f2019-04-29 15:23:20 +02001704 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001705
1706 if (list->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001707 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001708 }
1709 if (list->flags & LYS_SET_MAX) {
1710 if (list->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001711 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001712 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001713 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001714 }
1715 }
1716
1717 if (list->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001718 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001719 }
1720
Radek Krejci693262f2019-04-29 15:23:20 +02001721 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001722 ypr_description(ctx, node->dsc, node->exts, NULL);
1723 ypr_reference(ctx, node->ref, node->exts, NULL);
1724
1725 LY_ARRAY_FOR(list->typedefs, u) {
1726 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001727 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001728 }
1729
1730 LY_ARRAY_FOR(list->groupings, u) {
1731 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001732 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001733 }
1734
1735 LY_LIST_FOR(list->child, child) {
1736 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001737 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001738 }
1739
1740 LY_ARRAY_FOR(list->actions, u) {
1741 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001742 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001743 }
1744
1745 LY_ARRAY_FOR(list->notifs, u) {
1746 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001747 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001748 }
1749
1750 LEVEL--;
1751 ypr_close(ctx, flag);
1752}
1753
1754static void
Radek Krejci693262f2019-04-29 15:23:20 +02001755yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1756{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001757 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001758 int flag = 0;
1759 struct lysc_node *child;
1760 struct lysc_node_list *list = (struct lysc_node_list *)node;
1761
1762 yprc_node_common1(ctx, node, &flag);
1763
1764 LY_ARRAY_FOR(list->musts, u) {
1765 yprc_must(ctx, &list->musts[u], NULL);
1766 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001767 if (!(list->flags & LYS_KEYLESS)) {
Radek Krejci693262f2019-04-29 15:23:20 +02001768 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +02001769 ly_print(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001770 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 +02001771 ly_print(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001772 }
Radek Krejci241f6b52020-05-21 18:13:49 +02001773 ly_print(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001774 }
1775 LY_ARRAY_FOR(list->uniques, u) {
1776 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +02001777 ly_print(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001778 LY_ARRAY_FOR(list->uniques[u], v) {
Radek Krejci241f6b52020-05-21 18:13:49 +02001779 ly_print(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001780 }
1781 ypr_close(ctx, 0);
1782 }
1783
1784 ypr_config(ctx, node->flags, node->exts, NULL);
1785
1786 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1787 if (list->max) {
1788 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1789 } else {
1790 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1791 }
1792
1793 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1794
1795 ypr_status(ctx, node->flags, node->exts, NULL);
1796 ypr_description(ctx, node->dsc, node->exts, NULL);
1797 ypr_reference(ctx, node->ref, node->exts, NULL);
1798
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001799 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001800 LY_LIST_FOR(list->child, child) {
1801 ypr_open(ctx->out, &flag);
1802 yprc_node(ctx, child);
1803 }
Radek Krejci693262f2019-04-29 15:23:20 +02001804
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001805 LY_ARRAY_FOR(list->actions, u) {
1806 ypr_open(ctx->out, &flag);
1807 yprc_action(ctx, &list->actions[u]);
1808 }
Radek Krejci693262f2019-04-29 15:23:20 +02001809
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001810 LY_ARRAY_FOR(list->notifs, u) {
1811 ypr_open(ctx->out, &flag);
1812 yprc_notification(ctx, &list->notifs[u]);
1813 }
Radek Krejci693262f2019-04-29 15:23:20 +02001814 }
1815
1816 LEVEL--;
1817 ypr_close(ctx, flag);
1818}
1819
1820static void
1821yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001822{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001823 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001824 int flag = 0;
1825
Radek Krejci241f6b52020-05-21 18:13:49 +02001826 ly_print(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001827 LEVEL++;
1828
Radek Krejci693262f2019-04-29 15:23:20 +02001829 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1830 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001831
1832 LY_ARRAY_FOR(refine->musts, u) {
1833 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001834 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001835 }
1836
1837 if (refine->presence) {
1838 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001839 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001840 }
1841
1842 LY_ARRAY_FOR(refine->dflts, u) {
1843 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001844 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001845 }
1846
Radek Krejci693262f2019-04-29 15:23:20 +02001847 ypr_config(ctx, refine->flags, refine->exts, &flag);
1848 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001849
1850 if (refine->flags & LYS_SET_MIN) {
1851 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001852 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001853 }
1854 if (refine->flags & LYS_SET_MAX) {
1855 ypr_open(ctx->out, &flag);
1856 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001857 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001858 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001859 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001860 }
1861 }
1862
1863 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1864 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1865
1866 LEVEL--;
1867 ypr_close(ctx, flag);
1868}
1869
1870static void
Radek Krejci693262f2019-04-29 15:23:20 +02001871yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001872{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001873 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001874 struct lysp_node *child;
1875
Radek Krejci241f6b52020-05-21 18:13:49 +02001876 ly_print(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877 LEVEL++;
1878
Radek Krejci693262f2019-04-29 15:23:20 +02001879 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1880 yprp_when(ctx, aug->when, NULL);
1881 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1882 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001883 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1884 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1885
1886 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001887 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001888 }
1889
1890 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001891 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001892 }
1893
1894 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001895 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001896 }
1897
1898 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001899 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001900}
1901
Radek Krejcid3ca0632019-04-16 16:54:54 +02001902static void
Radek Krejci693262f2019-04-29 15:23:20 +02001903yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001904{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001905 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001906 int flag = 0;
1907 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1908
Radek Krejci693262f2019-04-29 15:23:20 +02001909 yprp_node_common1(ctx, node, &flag);
1910 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001911
1912 LY_ARRAY_FOR(uses->refines, u) {
1913 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001914 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001915 }
1916
1917 LY_ARRAY_FOR(uses->augments, u) {
1918 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001919 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001920 }
1921
1922 LEVEL--;
1923 ypr_close(ctx, flag);
1924}
1925
1926static void
Radek Krejci693262f2019-04-29 15:23:20 +02001927yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001928{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001929 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001930 int flag = 0;
1931 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1932
Radek Krejci693262f2019-04-29 15:23:20 +02001933 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001934
1935 LY_ARRAY_FOR(any->musts, u) {
1936 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001937 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001938 }
1939
Radek Krejci693262f2019-04-29 15:23:20 +02001940 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001941
1942 LEVEL--;
1943 ypr_close(ctx, flag);
1944}
1945
1946static void
Radek Krejci693262f2019-04-29 15:23:20 +02001947yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001948{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001949 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001950 int flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001951 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001952
Radek Krejci693262f2019-04-29 15:23:20 +02001953 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001954
Radek Krejci693262f2019-04-29 15:23:20 +02001955 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001956 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001957 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001958 }
1959
Radek Krejci693262f2019-04-29 15:23:20 +02001960 yprc_node_common2(ctx, node, &flag);
1961
Radek Krejcid3ca0632019-04-16 16:54:54 +02001962 LEVEL--;
1963 ypr_close(ctx, flag);
1964}
1965
1966static void
Radek Krejci693262f2019-04-29 15:23:20 +02001967yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001968{
Radek Krejci56cc0872019-04-30 09:22:27 +02001969 LYOUT_CHECK(ctx->out);
1970
Radek Krejcid3ca0632019-04-16 16:54:54 +02001971 switch (node->nodetype) {
1972 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001973 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001974 break;
1975 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001976 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001977 break;
1978 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001979 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001980 break;
1981 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001982 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001983 break;
1984 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001985 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001986 break;
1987 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001988 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001989 break;
1990 case LYS_ANYXML:
1991 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001992 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001993 break;
1994 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001995 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001996 break;
1997 default:
1998 break;
1999 }
2000}
2001
2002static void
Radek Krejci693262f2019-04-29 15:23:20 +02002003yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
2004{
Radek Krejci56cc0872019-04-30 09:22:27 +02002005 LYOUT_CHECK(ctx->out);
2006
Radek Krejci693262f2019-04-29 15:23:20 +02002007 switch (node->nodetype) {
2008 case LYS_CONTAINER:
2009 yprc_container(ctx, node);
2010 break;
2011 case LYS_CHOICE:
2012 yprc_choice(ctx, node);
2013 break;
2014 case LYS_LEAF:
2015 yprc_leaf(ctx, node);
2016 break;
2017 case LYS_LEAFLIST:
2018 yprc_leaflist(ctx, node);
2019 break;
2020 case LYS_LIST:
2021 yprc_list(ctx, node);
2022 break;
2023 case LYS_ANYXML:
2024 case LYS_ANYDATA:
2025 yprc_anydata(ctx, node);
2026 break;
2027 default:
2028 break;
2029 }
2030}
2031
2032static void
2033yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002034{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002035 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002036 struct lysp_deviate_add *add;
2037 struct lysp_deviate_rpl *rpl;
2038 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08002039 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002040
Radek Krejci241f6b52020-05-21 18:13:49 +02002041 ly_print(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002042 LEVEL++;
2043
Radek Krejci693262f2019-04-29 15:23:20 +02002044 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
2046 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
2047
fredgan2b11ddb2019-10-21 11:03:39 +08002048 LY_LIST_FOR(deviation->deviates, elem) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002049 ly_print(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08002050 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
2051 if (elem->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002052 ly_print(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002053 LEVEL++;
2054
fredgan2b11ddb2019-10-21 11:03:39 +08002055 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002056 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +02002057 ly_print(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002058 continue;
2059 }
fredgan2b11ddb2019-10-21 11:03:39 +08002060 } else if (elem->mod == LYS_DEV_ADD) {
2061 add = (struct lysp_deviate_add*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002062 ly_print(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002063 LEVEL++;
2064
Radek Krejci693262f2019-04-29 15:23:20 +02002065 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
2066 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002067 LY_ARRAY_FOR(add->musts, u) {
2068 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002069 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002070 LY_ARRAY_FOR(add->uniques, u) {
2071 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002072 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002073 LY_ARRAY_FOR(add->dflts, u) {
2074 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002075 }
Radek Krejci693262f2019-04-29 15:23:20 +02002076 ypr_config(ctx, add->flags, add->exts, NULL);
2077 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002078 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002079 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002080 }
2081 if (add->flags & LYS_SET_MAX) {
2082 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002083 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002084 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002085 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002086 }
2087 }
fredgan2b11ddb2019-10-21 11:03:39 +08002088 } else if (elem->mod == LYS_DEV_REPLACE) {
2089 rpl = (struct lysp_deviate_rpl*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002090 ly_print(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002091 LEVEL++;
2092
Radek Krejci693262f2019-04-29 15:23:20 +02002093 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002095 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002096 }
Radek Krejci693262f2019-04-29 15:23:20 +02002097 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
2098 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
2099 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2100 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002101 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002102 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002103 }
2104 if (rpl->flags & LYS_SET_MAX) {
2105 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002106 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002107 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002108 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002109 }
2110 }
fredgan2b11ddb2019-10-21 11:03:39 +08002111 } else if (elem->mod == LYS_DEV_DELETE) {
2112 del = (struct lysp_deviate_del*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002113 ly_print(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002114 LEVEL++;
2115
Radek Krejci693262f2019-04-29 15:23:20 +02002116 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2117 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002118 LY_ARRAY_FOR(del->musts, u) {
2119 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002120 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002121 LY_ARRAY_FOR(del->uniques, u) {
2122 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002123 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002124 LY_ARRAY_FOR(del->dflts, u) {
2125 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002126 }
2127 }
2128
2129 LEVEL--;
2130 ypr_close(ctx, 1);
2131 }
2132
2133 LEVEL--;
2134 ypr_close(ctx, 1);
2135}
2136
Michal Vasko7c8439f2020-08-05 13:25:19 +02002137static void
2138yang_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002139{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002140 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002141
Radek Krejcid3ca0632019-04-16 16:54:54 +02002142 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002143 ly_print(ctx->out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002144 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002145 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2146 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002147 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002148 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002149 }
Radek Krejci693262f2019-04-29 15:23:20 +02002150 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2151 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002152 LEVEL--;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002153 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002154 }
2155 LY_ARRAY_FOR(modp->includes, u) {
2156 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 +02002157 ly_print(ctx->out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002158 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002159 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002160 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002161 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002162 }
Radek Krejci693262f2019-04-29 15:23:20 +02002163 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2164 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002165 LEVEL--;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002166 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002167 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002168 ly_print(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002169 }
2170 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002171}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002172
Michal Vasko7c8439f2020-08-05 13:25:19 +02002173static void
2174yang_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
2175{
2176 LY_ARRAY_COUNT_TYPE u;
2177 struct lysp_node *data;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002178
Radek Krejcid3ca0632019-04-16 16:54:54 +02002179 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002180 ly_print(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002181 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002182 }
2183 if (modp->exts) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002184 ly_print(ctx->out, "\n");
2185 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002186 }
2187
2188 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002189 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002190 }
2191
2192 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002193 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002194 }
2195
2196 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002197 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002198 }
2199
2200 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002201 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002202 }
2203
2204 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002205 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002206 }
2207
2208 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002209 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002210 }
2211
2212 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002213 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002214 }
2215
2216 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002217 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002218 }
2219
2220 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002221 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002222 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02002223}
2224
2225LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02002226yang_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, int options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002227{
2228 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02002229 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002230
2231 ly_print(ctx->out, "%*smodule %s {\n", INDENT, module->name);
2232 LEVEL++;
2233
2234 /* module-header-stmts */
2235 if (module->version) {
2236 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
2237 }
2238
2239 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2240 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
2241
2242 /* linkage-stmts (import/include) */
2243 yang_print_parsed_linkage(ctx, modp);
2244
2245 /* meta-stmts */
2246 if (module->org || module->contact || module->dsc || module->ref) {
2247 ly_print(out, "\n");
2248 }
2249 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2250 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2251 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2252 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
2253
2254 /* revision-stmts */
2255 if (modp->revs) {
2256 ly_print(out, "\n");
2257 }
2258 LY_ARRAY_FOR(modp->revs, u) {
2259 yprp_revision(ctx, &modp->revs[u]);
2260 }
2261 /* body-stmts */
2262 yang_print_parsed_body(ctx, modp);
2263
2264 LEVEL--;
2265 ly_print(out, "%*s}\n", INDENT);
2266 ly_print_flush(out);
2267
2268 return LY_SUCCESS;
2269}
2270
2271static void
2272yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
2273{
2274 ly_print(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->belongsto);
2275 LEVEL++;
2276 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
2277 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
2278 LEVEL--;
2279 ly_print(ctx->out, "%*s}\n", INDENT);
2280}
2281
2282LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02002283yang_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, int options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002284{
2285 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02002286 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002287
2288 ly_print(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
2289 LEVEL++;
2290
2291 /* submodule-header-stmts */
2292 if (submodp->version) {
2293 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
2294 }
2295
2296 yprp_belongsto(ctx, submodp);
2297
2298 /* linkage-stmts (import/include) */
2299 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2300
2301 /* meta-stmts */
2302 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
2303 ly_print(out, "\n");
2304 }
2305 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2306 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
2307 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2308 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
2309
2310 /* revision-stmts */
2311 if (submodp->revs) {
2312 ly_print(out, "\n");
2313 }
2314 LY_ARRAY_FOR(submodp->revs, u) {
2315 yprp_revision(ctx, &submodp->revs[u]);
2316 }
2317 /* body-stmts */
2318 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002319
2320 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002321 ly_print(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002322 ly_print_flush(out);
2323
2324 return LY_SUCCESS;
2325}
2326
2327LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002328yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002329{
Radek Krejci5536d282020-08-04 23:27:44 +02002330 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002331
2332 yprc_node(ctx, node);
2333
2334 ly_print_flush(out);
2335 return LY_SUCCESS;
2336}
2337
2338LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002339yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002340{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002341 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002342 struct lysc_node *data;
2343 struct lysc_module *modc = module->compiled;
Radek Krejci5536d282020-08-04 23:27:44 +02002344 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002345
Radek Krejci241f6b52020-05-21 18:13:49 +02002346 ly_print(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002347 LEVEL++;
2348
Radek Krejci693262f2019-04-29 15:23:20 +02002349 /* module-header-stmts */
2350 if (module->version) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02002351 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 +02002352 }
2353 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2354 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2355
Michal Vasko7c8439f2020-08-05 13:25:19 +02002356 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002357
2358 /* meta-stmts */
2359 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002360 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002361 }
2362 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2363 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2364 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2365 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2366
2367 /* revision-stmts */
2368 if (module->revision) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002369 ly_print(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002370 }
2371
2372 /* body-stmts */
2373 if (modc->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002374 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002375 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2376 }
2377
2378 LY_ARRAY_FOR(modc->features, u) {
2379 yprc_feature(ctx, &modc->features[u]);
2380 }
2381
2382 LY_ARRAY_FOR(modc->identities, u) {
2383 yprc_identity(ctx, &modc->identities[u]);
2384 }
2385
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08002386 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002387 LY_LIST_FOR(modc->data, data) {
2388 yprc_node(ctx, data);
2389 }
Radek Krejci693262f2019-04-29 15:23:20 +02002390
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002391 LY_ARRAY_FOR(modc->rpcs, u) {
2392 yprc_action(ctx, &modc->rpcs[u]);
2393 }
Radek Krejci693262f2019-04-29 15:23:20 +02002394
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002395 LY_ARRAY_FOR(modc->notifs, u) {
2396 yprc_notification(ctx, &modc->notifs[u]);
2397 }
Radek Krejci693262f2019-04-29 15:23:20 +02002398 }
2399
2400 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002401 ly_print(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002402 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002403
2404 return LY_SUCCESS;
2405}