blob: 9557ab28f7cfcf567aaad6e017e262ca6b7c1187 [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 Krejcie7b95092019-05-15 11:03:07 +020024#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "plugins_types.h"
26#include "printer.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020027#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "tree_data.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020031#include "tree_schema.h"
32#include "tree_schema_internal.h"
Radek Krejci693262f2019-04-29 15:23:20 +020033#include "xpath.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020034
Radek Krejcie7b95092019-05-15 11:03:07 +020035/**
36 * @brief Types of the YANG printers
37 */
Radek Krejci693262f2019-04-29 15:23:20 +020038enum schema_type {
Radek Krejcie7b95092019-05-15 11:03:07 +020039 YPR_PARSED, /**< YANG printer of the parsed schema */
40 YPR_COMPILED /**< YANG printer of the compiled schema */
Radek Krejci693262f2019-04-29 15:23:20 +020041};
42
Radek Krejcie7b95092019-05-15 11:03:07 +020043/**
44 * @brief YANG printer context.
45 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020046struct ypr_ctx {
Radek Krejci241f6b52020-05-21 18:13:49 +020047 struct ly_out *out; /**< output specification */
Radek Krejcie7b95092019-05-15 11:03:07 +020048 unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
49 const struct lys_module *module; /**< schema to print */
50 enum schema_type schema; /**< type of the schema to print */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080051 int options; /**< Schema output options (see @ref schemaprinterflags). */
Radek Krejcid3ca0632019-04-16 16:54:54 +020052};
53
Radek Krejcie7b95092019-05-15 11:03:07 +020054#define LEVEL ctx->level /**< current level */
55#define INDENT (LEVEL)*2,"" /**< indentation parameters for printer functions */
56
57/**
58 * @brief Print the given text as content of a double quoted YANG string,
59 * including encoding characters that have special meanings. The quotation marks
60 * are not printed.
61 *
62 * Follows RFC 7950, section 6.1.3.
63 *
64 * @param[in] out Output specification.
65 * @param[in] text String to be printed.
Radek Krejcif56e2a42019-09-09 14:15:25 +020066 * @param[in] len Length of the string from @p text to be printed. In case of -1,
Radek Krejcie7b95092019-05-15 11:03:07 +020067 * the @p text is printed completely as a NULL-terminated string.
68 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020069static void
Radek Krejci241f6b52020-05-21 18:13:49 +020070ypr_encode(struct ly_out *out, const char *text, int len)
Radek Krejcid3ca0632019-04-16 16:54:54 +020071{
72 int i, start_len;
73 const char *start;
74 char special = 0;
75
76 if (!len) {
77 return;
78 }
79
80 if (len < 0) {
81 len = strlen(text);
82 }
83
84 start = text;
85 start_len = 0;
86 for (i = 0; i < len; ++i) {
87 switch (text[i]) {
88 case '\n':
89 case '\t':
90 case '\"':
91 case '\\':
92 special = text[i];
93 break;
94 default:
95 ++start_len;
96 break;
97 }
98
99 if (special) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200100 ly_write(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200101 switch (special) {
102 case '\n':
Radek Krejci241f6b52020-05-21 18:13:49 +0200103 ly_write(out, "\\n", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200104 break;
105 case '\t':
Radek Krejci241f6b52020-05-21 18:13:49 +0200106 ly_write(out, "\\t", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200107 break;
108 case '\"':
Radek Krejci241f6b52020-05-21 18:13:49 +0200109 ly_write(out, "\\\"", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200110 break;
111 case '\\':
Radek Krejci241f6b52020-05-21 18:13:49 +0200112 ly_write(out, "\\\\", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200113 break;
114 }
115
116 start += start_len + 1;
117 start_len = 0;
118
119 special = 0;
120 }
121 }
122
Radek Krejci241f6b52020-05-21 18:13:49 +0200123 ly_write(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200124}
125
126static void
Radek Krejci241f6b52020-05-21 18:13:49 +0200127ypr_open(struct ly_out *out, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200128{
129 if (flag && !*flag) {
130 *flag = 1;
Radek Krejci241f6b52020-05-21 18:13:49 +0200131 ly_print(out, " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200132 }
133}
134
135static void
136ypr_close(struct ypr_ctx *ctx, int flag)
137{
138 if (flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200139 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200140 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200141 ly_print(ctx->out, ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200142 }
143}
144
145static void
146ypr_text(struct ypr_ctx *ctx, const char *name, const char *text, int singleline, int closed)
147{
148 const char *s, *t;
149
150 if (singleline) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200151 ly_print(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200152 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200153 ly_print(ctx->out, "%*s%s\n", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200154 LEVEL++;
155
Radek Krejci241f6b52020-05-21 18:13:49 +0200156 ly_print(ctx->out, "%*s\"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200157 }
158 t = text;
159 while ((s = strchr(t, '\n'))) {
160 ypr_encode(ctx->out, t, s - t);
Radek Krejci241f6b52020-05-21 18:13:49 +0200161 ly_print(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200162 t = s + 1;
163 if (*t != '\n') {
Radek Krejci241f6b52020-05-21 18:13:49 +0200164 ly_print(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200165 }
166 }
167
168 ypr_encode(ctx->out, t, strlen(t));
169 if (closed) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200170 ly_print(ctx->out, "\";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200171 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200172 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200173 }
174 if (!singleline) {
175 LEVEL--;
176 }
177}
178
179static void
Radek Krejci693262f2019-04-29 15:23:20 +0200180yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200181{
182 struct lysp_stmt *childstmt;
183 const char *s, *t;
184
185 if (stmt->arg) {
186 if (stmt->flags) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200187 ly_print(ctx->out, "%*s%s\n", INDENT, stmt->stmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200188 LEVEL++;
Radek Krejci241f6b52020-05-21 18:13:49 +0200189 ly_print(ctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'');
Radek Krejcid3ca0632019-04-16 16:54:54 +0200190 t = stmt->arg;
191 while ((s = strchr(t, '\n'))) {
192 ypr_encode(ctx->out, t, s - t);
Radek Krejci241f6b52020-05-21 18:13:49 +0200193 ly_print(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200194 t = s + 1;
195 if (*t != '\n') {
Radek Krejci241f6b52020-05-21 18:13:49 +0200196 ly_print(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200197 }
198 }
199 LEVEL--;
200 ypr_encode(ctx->out, t, strlen(t));
Radek Krejci241f6b52020-05-21 18:13:49 +0200201 ly_print(ctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200202 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200203 ly_print(ctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200204 }
205 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200206 ly_print(ctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200207 }
208
209 if (stmt->child) {
210 LEVEL++;
211 LY_LIST_FOR(stmt->child, childstmt) {
Radek Krejci693262f2019-04-29 15:23:20 +0200212 yprp_stmt(ctx, childstmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200213 }
214 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +0200215 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200216 }
217}
218
219/**
220 * @param[in] count Number of extensions to print, 0 to print them all.
221 */
222static void
Radek Krejci693262f2019-04-29 15:23:20 +0200223yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200224 struct lysp_ext_instance *ext, int *flag, LY_ARRAY_SIZE_TYPE count)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200226 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227 struct lysp_stmt *stmt;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200228 int child_presence;
229 const char *argument;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200230
231 if (!count && ext) {
232 count = LY_ARRAY_SIZE(ext);
233 }
234 LY_ARRAY_FOR(ext, u) {
235 if (!count) {
236 break;
237 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200238
Radek Krejcid3ca0632019-04-16 16:54:54 +0200239 count--;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200240 if (ext->insubstmt != substmt || ext->insubstmt_index != substmt_index) {
241 continue;
242 }
243
244 if (!ext->compiled && ext->yin) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200245 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 +0200246 continue;
247 }
248
249
250 ypr_open(ctx->out, flag);
251 argument = NULL;
252 if (ext[u].compiled) {
253 argument = ext[u].compiled->argument;
254 } else {
255 argument = ext[u].argument;
256 }
257 if (argument) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200258 ly_print(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200259 ypr_encode(ctx->out, argument, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200260 ly_print(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200261 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200262 ly_print(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200263 }
264
265 child_presence = 0;
266 LEVEL++;
267 LY_LIST_FOR(ext[u].child, stmt) {
268 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
269 continue;
270 }
271 if (!child_presence) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200272 ly_print(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200273 child_presence = 1;
274 }
275 yprp_stmt(ctx, stmt);
276 }
277 LEVEL--;
278 if (child_presence) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200279 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200280 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200281 ly_print(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200282 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200283 }
284}
285
Radek Krejci693262f2019-04-29 15:23:20 +0200286/**
287 * @param[in] count Number of extensions to print, 0 to print them all.
288 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200289static void
Radek Krejci693262f2019-04-29 15:23:20 +0200290yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200291 struct lysc_ext_instance *ext, int *flag, LY_ARRAY_SIZE_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200292{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200293 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200294
295 if (!count && ext) {
296 count = LY_ARRAY_SIZE(ext);
297 }
298 LY_ARRAY_FOR(ext, u) {
299 if (!count) {
300 break;
301 }
302 /* TODO compiled extensions */
303 (void) ctx;
304 (void) substmt;
305 (void) substmt_index;
306 (void) flag;
307
308 count--;
309 }
310}
311
312static void
313ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200314{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200315 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200316 int extflag = 0;
317
318 if (!text) {
319 /* nothing to print */
320 return;
321 }
322
323 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200324 ly_print(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200325 } else {
326 ypr_text(ctx, ext_substmt_info[substmt].name, text,
327 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
328 }
329
330 LEVEL++;
331 LY_ARRAY_FOR(ext, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200332 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 +0200333 continue;
334 }
Radek Krejci693262f2019-04-29 15:23:20 +0200335 if (ctx->schema == YPR_PARSED) {
336 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance*)ext)[u], &extflag, 1);
337 } else {
338 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance*)ext)[u], &extflag, 1);
339 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200340 }
341 LEVEL--;
342 ypr_close(ctx, extflag);
343}
344
345static void
Radek Krejci693262f2019-04-29 15:23:20 +0200346ypr_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 +0200347{
348 char *str;
349
350 if (asprintf(&str, "%u", attr_value) == -1) {
351 LOGMEM(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +0200352 ctx->out->status = LY_EMEM;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200353 return;
354 }
355 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200356 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200357 free(str);
358}
359
360static void
Radek Krejci693262f2019-04-29 15:23:20 +0200361ypr_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 +0200362{
363 char *str;
364
365 if (asprintf(&str, "%d", attr_value) == -1) {
366 LOGMEM(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +0200367 ctx->out->status = LY_EMEM;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200368 return;
369 }
370 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200371 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200372 free(str);
373}
374
375static void
Radek Krejci693262f2019-04-29 15:23:20 +0200376yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200377{
378 if (rev->dsc || rev->ref || rev->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200379 ly_print(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200380 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200381 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
382 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
383 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200384 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +0200385 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200386 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200387 ly_print(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200388 }
389}
390
391static void
Radek Krejci693262f2019-04-29 15:23:20 +0200392ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200393{
394 if (flags & LYS_MAND_MASK) {
395 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200396 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200397 }
398}
399
400static void
Radek Krejci693262f2019-04-29 15:23:20 +0200401ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200402{
403 if (flags & LYS_CONFIG_MASK) {
404 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200405 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200406 }
407}
408
409static void
Radek Krejci693262f2019-04-29 15:23:20 +0200410ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200411{
412 const char *status = NULL;
413
414 if (flags & LYS_STATUS_CURR) {
415 ypr_open(ctx->out, flag);
416 status = "current";
417 } else if (flags & LYS_STATUS_DEPRC) {
418 ypr_open(ctx->out, flag);
419 status = "deprecated";
420 } else if (flags & LYS_STATUS_OBSLT) {
421 ypr_open(ctx->out, flag);
422 status = "obsolete";
423 }
Radek Krejci693262f2019-04-29 15:23:20 +0200424
425 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200426}
427
428static void
Radek Krejci693262f2019-04-29 15:23:20 +0200429ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200430{
431 if (dsc) {
432 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200433 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200434 }
435}
436
437static void
Radek Krejci693262f2019-04-29 15:23:20 +0200438ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200439{
440 if (ref) {
441 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200442 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200443 }
444}
445
446static void
Radek Krejci693262f2019-04-29 15:23:20 +0200447yprp_iffeatures(struct ypr_ctx *ctx, const char **iff, struct lysp_ext_instance *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200448{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200449 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200450 int extflag;
451
452 LY_ARRAY_FOR(iff, u) {
453 ypr_open(ctx->out, flag);
454 extflag = 0;
455
Radek Krejci241f6b52020-05-21 18:13:49 +0200456 ly_print(ctx->out, "%*sif-feature \"%s\"", INDENT, iff[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200457
458 /* extensions */
459 LEVEL++;
460 LY_ARRAY_FOR(exts, u) {
461 if (exts[u].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[u].insubstmt_index != u) {
462 continue;
463 }
Radek Krejci693262f2019-04-29 15:23:20 +0200464 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[u], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200465 }
466 LEVEL--;
467 ypr_close(ctx, extflag);
468 }
469}
470
471static void
Radek Krejci693262f2019-04-29 15:23:20 +0200472yprc_iffeature(struct ypr_ctx *ctx, struct lysc_iffeature *feat, int *index_e, int *index_f)
473{
474 int brackets_flag = *index_e;
475 uint8_t op;
476
477 op = lysc_iff_getop(feat->expr, *index_e);
478 (*index_e)++;
479
480 switch (op) {
481 case LYS_IFF_F:
482 if (ctx->module == feat->features[*index_f]->module) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200483 ly_print(ctx->out, "%s", feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200484 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200485 ly_print(ctx->out, "%s:%s", feat->features[*index_f]->module->prefix, feat->features[*index_f]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200486 }
487 (*index_f)++;
488 break;
489 case LYS_IFF_NOT:
Radek Krejci241f6b52020-05-21 18:13:49 +0200490 ly_print(ctx->out, "not ");
Radek Krejci693262f2019-04-29 15:23:20 +0200491 yprc_iffeature(ctx, feat, index_e, index_f);
492 break;
493 case LYS_IFF_AND:
494 if (brackets_flag) {
495 /* AND need brackets only if previous op was not */
496 if (*index_e < 2 || lysc_iff_getop(feat->expr, *index_e - 2) != LYS_IFF_NOT) {
497 brackets_flag = 0;
498 }
499 }
500 /* falls through */
501 case LYS_IFF_OR:
502 if (brackets_flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200503 ly_print(ctx->out, "(");
Radek Krejci693262f2019-04-29 15:23:20 +0200504 }
505 yprc_iffeature(ctx, feat, index_e, index_f);
Radek Krejci241f6b52020-05-21 18:13:49 +0200506 ly_print(ctx->out, " %s ", op == LYS_IFF_OR ? "or" : "and");
Radek Krejci693262f2019-04-29 15:23:20 +0200507 yprc_iffeature(ctx, feat, index_e, index_f);
508 if (brackets_flag) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200509 ly_print(ctx->out, ")");
Radek Krejci693262f2019-04-29 15:23:20 +0200510 }
511 }
512}
513
514static void
515yprc_iffeatures(struct ypr_ctx *ctx, struct lysc_iffeature *iff, struct lysc_ext_instance *exts, int *flag)
516{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200517 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +0200518 int extflag;
519
520 LY_ARRAY_FOR(iff, u) {
521 int index_e = 0, index_f = 0;
522
523 ypr_open(ctx->out, flag);
524 extflag = 0;
525
Radek Krejci241f6b52020-05-21 18:13:49 +0200526 ly_print(ctx->out, "%*sif-feature \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200527 yprc_iffeature(ctx, iff, &index_e, &index_f);
Radek Krejci241f6b52020-05-21 18:13:49 +0200528 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200529
530 /* extensions */
531 LEVEL++;
Radek Krejci334ccc72019-06-12 13:49:29 +0200532 LY_ARRAY_FOR(exts, v) {
533 if (exts[v].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[v].insubstmt_index != u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200534 continue;
535 }
Radek Krejci334ccc72019-06-12 13:49:29 +0200536 yprc_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200537 }
538 LEVEL--;
539 ypr_close(ctx, extflag);
540 }
541}
542
543static void
544yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200545{
546 int flag = 0, flag2 = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200547 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200548
Radek Krejci241f6b52020-05-21 18:13:49 +0200549 ly_print(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200550 LEVEL++;
551
552 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200553 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200554 }
555
556 if (ext->argument) {
557 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200558 ly_print(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200559 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200560 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200561 u = -1;
562 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LYEXT_SUBSTMT_ARGUMENT)) != LY_ARRAY_SIZE(ext->exts)) {
563 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200564 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200565 }
566 if ((ext->flags & LYS_YINELEM_MASK) ||
567 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_SIZE(ext->exts))) {
568 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200569 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200570 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200571 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200572 ypr_close(ctx, flag2);
573 }
574
Radek Krejci693262f2019-04-29 15:23:20 +0200575 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200576 ypr_description(ctx, ext->dsc, ext->exts, &flag);
577 ypr_reference(ctx, ext->ref, ext->exts, &flag);
578
579 LEVEL--;
580 ypr_close(ctx, flag);
581}
582
583static void
Radek Krejci693262f2019-04-29 15:23:20 +0200584yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200585{
586 int flag = 0;
587
Radek Krejci241f6b52020-05-21 18:13:49 +0200588 ly_print(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200589 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200590 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
591 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
592 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200593 ypr_description(ctx, feat->dsc, feat->exts, &flag);
594 ypr_reference(ctx, feat->ref, feat->exts, &flag);
595 LEVEL--;
596 ypr_close(ctx, flag);
597}
598
599static void
Radek Krejci693262f2019-04-29 15:23:20 +0200600yprc_feature(struct ypr_ctx *ctx, const struct lysc_feature *feat)
601{
602 int flag = 0;
603
Radek Krejci241f6b52020-05-21 18:13:49 +0200604 ly_print(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200605 LEVEL++;
606 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
607 yprc_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
608 ypr_status(ctx, feat->flags, feat->exts, &flag);
609 ypr_description(ctx, feat->dsc, feat->exts, &flag);
610 ypr_reference(ctx, feat->ref, feat->exts, &flag);
611 LEVEL--;
612 ypr_close(ctx, flag);
613}
614
615static void
616yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200617{
618 int flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200619 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200620
Radek Krejci241f6b52020-05-21 18:13:49 +0200621 ly_print(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200622 LEVEL++;
623
Radek Krejci693262f2019-04-29 15:23:20 +0200624 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
625 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200626
627 LY_ARRAY_FOR(ident->bases, u) {
628 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200629 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200630 }
631
Radek Krejci693262f2019-04-29 15:23:20 +0200632 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200633 ypr_description(ctx, ident->dsc, ident->exts, &flag);
634 ypr_reference(ctx, ident->ref, ident->exts, &flag);
635
636 LEVEL--;
637 ypr_close(ctx, flag);
638}
639
640static void
Radek Krejci693262f2019-04-29 15:23:20 +0200641yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
642{
643 int flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200644 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200645
Radek Krejci241f6b52020-05-21 18:13:49 +0200646 ly_print(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200647 LEVEL++;
648
649 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
650 yprc_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
651
652 LY_ARRAY_FOR(ident->derived, u) {
653 ypr_open(ctx->out, &flag);
654 if (ctx->module != ident->derived[u]->module) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200655 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 +0200656 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200657 ly_print(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200658 }
659 }
660
661 ypr_status(ctx, ident->flags, ident->exts, &flag);
662 ypr_description(ctx, ident->dsc, ident->exts, &flag);
663 ypr_reference(ctx, ident->ref, ident->exts, &flag);
664
665 LEVEL--;
666 ypr_close(ctx, flag);
667}
668
669static void
670yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200671{
672 int inner_flag = 0;
673
674 if (!restr) {
675 return;
676 }
677
678 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200679 ly_print(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200680 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 +0200681 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200682
683 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200684 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200685 if (restr->arg[0] == 0x15) {
686 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
687 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200688 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200689 }
690 if (restr->emsg) {
691 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200692 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200693 }
694 if (restr->eapptag) {
695 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200696 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200697 }
Radek Krejci693262f2019-04-29 15:23:20 +0200698 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
699 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
700
Radek Krejcid3ca0632019-04-16 16:54:54 +0200701 LEVEL--;
702 ypr_close(ctx, inner_flag);
703}
704
705static void
Radek Krejci693262f2019-04-29 15:23:20 +0200706yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, int *flag)
707{
708 int inner_flag = 0;
709
710 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200711 ly_print(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200712 ypr_encode(ctx->out, must->cond->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200713 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200714
715 LEVEL++;
716 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
717 if (must->emsg) {
718 ypr_open(ctx->out, &inner_flag);
719 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
720 }
721 if (must->eapptag) {
722 ypr_open(ctx->out, &inner_flag);
723 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
724 }
725 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
726 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
727
728 LEVEL--;
729 ypr_close(ctx, inner_flag);
730}
731
732static void
733yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, int *flag)
734{
735 int inner_flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200736 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200737
Radek Krejci334ccc72019-06-12 13:49:29 +0200738 if (!range) {
739 return;
740 }
741
Radek Krejci693262f2019-04-29 15:23:20 +0200742 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200743 ly_print(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200744 LY_ARRAY_FOR(range->parts, u) {
745 if (u > 0) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200746 ly_print(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200747 }
748 if (range->parts[u].max_64 == range->parts[u].min_64) {
749 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200750 ly_print(ctx->out, "%"PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200751 } else { /* signed values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200752 ly_print(ctx->out, "%"PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200753 }
754 } else {
755 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200756 ly_print(ctx->out, "%"PRIu64"..%"PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200757 } else { /* signed values */
Radek Krejci241f6b52020-05-21 18:13:49 +0200758 ly_print(ctx->out, "%"PRId64"..%"PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200759 }
760 }
761 }
Radek Krejci241f6b52020-05-21 18:13:49 +0200762 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200763
764 LEVEL++;
765 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
766 if (range->emsg) {
767 ypr_open(ctx->out, &inner_flag);
768 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
769 }
770 if (range->eapptag) {
771 ypr_open(ctx->out, &inner_flag);
772 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
773 }
774 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
775 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
776
777 LEVEL--;
778 ypr_close(ctx, inner_flag);
779}
780
781static void
782yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, int *flag)
783{
784 int inner_flag = 0;
785
786 ypr_open(ctx->out, flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200787 ly_print(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200788 ypr_encode(ctx->out, pattern->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200789 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200790
791 LEVEL++;
792 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
793 if (pattern->inverted) {
794 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
795 ypr_open(ctx->out, &inner_flag);
796 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
797 }
798 if (pattern->emsg) {
799 ypr_open(ctx->out, &inner_flag);
800 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
801 }
802 if (pattern->eapptag) {
803 ypr_open(ctx->out, &inner_flag);
804 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
805 }
806 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
807 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
808
809 LEVEL--;
810 ypr_close(ctx, inner_flag);
811}
812
813static void
814yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200815{
816 int inner_flag = 0;
817
818 if (!when) {
819 return;
820 }
821 ypr_open(ctx->out, flag);
822
Radek Krejci241f6b52020-05-21 18:13:49 +0200823 ly_print(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200824 ypr_encode(ctx->out, when->cond, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200825 ly_print(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200826
827 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200828 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200829 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
830 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
831 LEVEL--;
832 ypr_close(ctx, inner_flag);
833}
834
835static void
Radek Krejci693262f2019-04-29 15:23:20 +0200836yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, int *flag)
837{
838 int inner_flag = 0;
839
840 if (!when) {
841 return;
842 }
843 ypr_open(ctx->out, flag);
844
Radek Krejci241f6b52020-05-21 18:13:49 +0200845 ly_print(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200846 ypr_encode(ctx->out, when->cond->expr, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200847 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200848
849 LEVEL++;
850 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
851 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
852 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
853 LEVEL--;
854 ypr_close(ctx, inner_flag);
855}
856
857static void
858yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200859{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200860 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200861 int inner_flag;
862
863 LY_ARRAY_FOR(items, u) {
864 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200865 if (type == LY_TYPE_BITS) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200866 ly_print(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200867 } else { /* LY_TYPE_ENUM */
Radek Krejci241f6b52020-05-21 18:13:49 +0200868 ly_print(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200869 ypr_encode(ctx->out, items[u].name, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200870 ly_print(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200871 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200872 inner_flag = 0;
873 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200874 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
875 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200876 if (items[u].flags & LYS_SET_VALUE) {
877 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200878 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200879 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200880 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200881 }
882 }
Radek Krejci693262f2019-04-29 15:23:20 +0200883 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200884 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
885 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
886 LEVEL--;
887 ypr_close(ctx, inner_flag);
888 }
889}
890
891static void
Radek Krejci693262f2019-04-29 15:23:20 +0200892yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200893{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200894 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200895 int flag = 0;
896
Radek Krejci241f6b52020-05-21 18:13:49 +0200897 ly_print(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200898 LEVEL++;
899
Radek Krejci693262f2019-04-29 15:23:20 +0200900 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200901
Radek Krejci693262f2019-04-29 15:23:20 +0200902 yprp_restr(ctx, type->range, "range", &flag);
903 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200904 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200905 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200906 }
Radek Krejci693262f2019-04-29 15:23:20 +0200907 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
908 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200909
910 if (type->path) {
911 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200912 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200913 }
914 if (type->flags & LYS_SET_REQINST) {
915 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200916 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200917 }
918 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200919 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200920 }
921 LY_ARRAY_FOR(type->bases, u) {
922 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200923 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200924 }
925 LY_ARRAY_FOR(type->types, u) {
926 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200927 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200928 }
929
930 LEVEL--;
931 ypr_close(ctx, flag);
932}
933
934static void
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200935yprc_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 +0200936{
937 int dynamic;
938 const char *str;
939
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200940 str = value->realtype->plugin->print(value, LYD_JSON, lys_get_prefix, (void*)value_mod, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200941 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
942 if (dynamic) {
943 free((void*)str);
944 }
945}
946
947static void
Radek Krejci693262f2019-04-29 15:23:20 +0200948yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
949{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200950 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200951 int flag = 0;
952
Radek Krejci241f6b52020-05-21 18:13:49 +0200953 ly_print(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200954 LEVEL++;
955
956 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
957 if (type->dflt) {
958 ypr_open(ctx->out, &flag);
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200959 yprc_dflt_value(ctx, type->dflt, type->dflt_mod, type->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200960 }
961
962 switch(type->basetype) {
963 case LY_TYPE_BINARY: {
964 struct lysc_type_bin *bin = (struct lysc_type_bin*)type;
965 yprc_range(ctx, bin->length, type->basetype, &flag);
966 break;
967 }
968 case LY_TYPE_UINT8:
969 case LY_TYPE_UINT16:
970 case LY_TYPE_UINT32:
971 case LY_TYPE_UINT64:
972 case LY_TYPE_INT8:
973 case LY_TYPE_INT16:
974 case LY_TYPE_INT32:
975 case LY_TYPE_INT64: {
976 struct lysc_type_num *num = (struct lysc_type_num*)type;
977 yprc_range(ctx, num->range, type->basetype, &flag);
978 break;
979 }
980 case LY_TYPE_STRING: {
981 struct lysc_type_str *str = (struct lysc_type_str*)type;
982 yprc_range(ctx, str->length, type->basetype, &flag);
983 LY_ARRAY_FOR(str->patterns, u) {
984 yprc_pattern(ctx, str->patterns[u], &flag);
985 }
986 break;
987 }
988 case LY_TYPE_BITS:
989 case LY_TYPE_ENUM: {
990 /* bits and enums structures are compatible */
991 struct lysc_type_bits *bits = (struct lysc_type_bits*)type;
992 LY_ARRAY_FOR(bits->bits, u) {
993 struct lysc_type_bitenum_item *item = &bits->bits[u];
994 int inner_flag = 0;
995
996 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +0200997 ly_print(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200998 ypr_encode(ctx->out, item->name, -1);
Radek Krejci241f6b52020-05-21 18:13:49 +0200999 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +02001000 LEVEL++;
1001 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
1002 yprc_iffeatures(ctx, item->iffeatures, item->exts, &inner_flag);
1003 if (type->basetype == LY_TYPE_BITS) {
1004 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
1005 } else { /* LY_TYPE_ENUM */
1006 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
1007 }
1008 ypr_status(ctx, item->flags, item->exts, &inner_flag);
1009 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
1010 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
1011 LEVEL--;
1012 ypr_close(ctx, inner_flag);
1013 }
1014 break;
1015 }
1016 case LY_TYPE_BOOL:
1017 case LY_TYPE_EMPTY:
1018 /* nothing to do */
1019 break;
1020 case LY_TYPE_DEC64: {
1021 struct lysc_type_dec *dec = (struct lysc_type_dec*)type;
1022 ypr_open(ctx->out, &flag);
1023 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
1024 yprc_range(ctx, dec->range, dec->basetype, &flag);
1025 break;
1026 }
1027 case LY_TYPE_IDENT: {
1028 struct lysc_type_identityref *ident = (struct lysc_type_identityref*)type;
1029 LY_ARRAY_FOR(ident->bases, u) {
1030 ypr_open(ctx->out, &flag);
1031 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
1032 }
1033 break;
1034 }
1035 case LY_TYPE_INST: {
1036 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid*)type;
1037 ypr_open(ctx->out, &flag);
1038 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
1039 break;
1040 }
1041 case LY_TYPE_LEAFREF: {
1042 struct lysc_type_leafref *lr = (struct lysc_type_leafref*)type;
1043 ypr_open(ctx->out, &flag);
1044 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path, lr->exts);
1045 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
1046 yprc_type(ctx, lr->realtype);
1047 break;
1048 }
1049 case LY_TYPE_UNION: {
1050 struct lysc_type_union *un = (struct lysc_type_union*)type;
1051 LY_ARRAY_FOR(un->types, u) {
1052 ypr_open(ctx->out, &flag);
1053 yprc_type(ctx, un->types[u]);
1054 }
1055 break;
1056 }
1057 default:
1058 LOGINT(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +02001059 ctx->out->status = LY_EINT;
Radek Krejci693262f2019-04-29 15:23:20 +02001060 }
1061
1062 LEVEL--;
1063 ypr_close(ctx, flag);
1064}
1065
1066static void
1067yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001068{
Radek Krejci56cc0872019-04-30 09:22:27 +02001069 LYOUT_CHECK(ctx->out);
1070
Radek Krejci241f6b52020-05-21 18:13:49 +02001071 ly_print(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001072 LEVEL++;
1073
Radek Krejci693262f2019-04-29 15:23:20 +02001074 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001075
Radek Krejci693262f2019-04-29 15:23:20 +02001076 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001077
1078 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +02001079 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001080 }
1081 if (tpdf->dflt) {
Radek Krejci693262f2019-04-29 15:23:20 +02001082 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001083 }
1084
Radek Krejci693262f2019-04-29 15:23:20 +02001085 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001086 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
1087 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
1088
1089 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001090 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001091}
1092
Radek Krejci693262f2019-04-29 15:23:20 +02001093static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
1094static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
1095static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001096
1097static void
Radek Krejci693262f2019-04-29 15:23:20 +02001098yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001099{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001100 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001101 int flag = 0;
1102 struct lysp_node *data;
1103
Radek Krejci56cc0872019-04-30 09:22:27 +02001104 LYOUT_CHECK(ctx->out);
1105
Radek Krejci241f6b52020-05-21 18:13:49 +02001106 ly_print(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001107 LEVEL++;
1108
Radek Krejci693262f2019-04-29 15:23:20 +02001109 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1110 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001111 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1112 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001113
1114 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001115 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001116 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001117 }
1118
1119 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001120 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001121 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122 }
1123
1124 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001125 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001126 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001127 }
1128
1129 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001130 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001131 }
1132
1133 LEVEL--;
1134 ypr_close(ctx, flag);
1135}
1136
1137static void
Radek Krejci693262f2019-04-29 15:23:20 +02001138yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001139{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001140 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001141 struct lysp_node *data;
1142
1143 if (!inout->nodetype) {
1144 /* nodetype not set -> input/output is empty */
1145 return;
1146 }
1147 ypr_open(ctx->out, flag);
1148
Radek Krejci241f6b52020-05-21 18:13:49 +02001149 ly_print(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Radek Krejcid3ca0632019-04-16 16:54:54 +02001150 LEVEL++;
1151
Radek Krejci693262f2019-04-29 15:23:20 +02001152 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001153 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001154 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001155 }
1156 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001157 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001158 }
1159 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001160 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001161 }
1162
1163 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001164 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001165 }
1166
1167 LEVEL--;
1168 ypr_close(ctx, 1);
1169}
1170
1171static void
Radek Krejci693262f2019-04-29 15:23:20 +02001172yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, int *flag)
1173{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001174 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001175 struct lysc_node *data;
1176
1177 if (!inout->data) {
1178 /* input/output is empty */
1179 return;
1180 }
1181 ypr_open(ctx->out, flag);
1182
Radek Krejci241f6b52020-05-21 18:13:49 +02001183 ly_print(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
Radek Krejci693262f2019-04-29 15:23:20 +02001184 LEVEL++;
1185
1186 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1187 LY_ARRAY_FOR(inout->musts, u) {
1188 yprc_must(ctx, &inout->musts[u], NULL);
1189 }
1190
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001191 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001192 LY_LIST_FOR(inout->data, data) {
1193 yprc_node(ctx, data);
1194 }
Radek Krejci693262f2019-04-29 15:23:20 +02001195 }
1196
1197 LEVEL--;
1198 ypr_close(ctx, 1);
1199}
1200
1201static void
1202yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001203{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001204 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001205 int flag = 0;
1206 struct lysp_node *data;
1207
Radek Krejci56cc0872019-04-30 09:22:27 +02001208 LYOUT_CHECK(ctx->out);
1209
Radek Krejci241f6b52020-05-21 18:13:49 +02001210 ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001211
1212 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001213 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1214 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001215
1216 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001217 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001218 }
Radek Krejci693262f2019-04-29 15:23:20 +02001219 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001220 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1221 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1222
1223 LY_ARRAY_FOR(notif->typedefs, u) {
1224 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001225 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001226 }
1227
1228 LY_ARRAY_FOR(notif->groupings, u) {
1229 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001230 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001231 }
1232
1233 LY_LIST_FOR(notif->data, data) {
1234 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001235 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001236 }
1237
1238 LEVEL--;
1239 ypr_close(ctx, flag);
1240}
1241
1242static void
Radek Krejci693262f2019-04-29 15:23:20 +02001243yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1244{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001245 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001246 int flag = 0;
1247 struct lysc_node *data;
1248
Radek Krejci56cc0872019-04-30 09:22:27 +02001249 LYOUT_CHECK(ctx->out);
1250
Radek Krejci241f6b52020-05-21 18:13:49 +02001251 ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001252
1253 LEVEL++;
1254 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1255 yprc_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
1256
1257 LY_ARRAY_FOR(notif->musts, u) {
1258 yprc_must(ctx, &notif->musts[u], &flag);
1259 }
1260 ypr_status(ctx, notif->flags, notif->exts, &flag);
1261 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1262 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1263
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001264 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001265 LY_LIST_FOR(notif->data, data) {
1266 ypr_open(ctx->out, &flag);
1267 yprc_node(ctx, data);
1268 }
Radek Krejci693262f2019-04-29 15:23:20 +02001269 }
1270
1271 LEVEL--;
1272 ypr_close(ctx, flag);
1273}
1274
1275static void
1276yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001277{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001278 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001279 int flag = 0;
1280
Radek Krejci56cc0872019-04-30 09:22:27 +02001281 LYOUT_CHECK(ctx->out);
1282
Radek Krejci241f6b52020-05-21 18:13:49 +02001283 ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001284
1285 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001286 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1287 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1288 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001289 ypr_description(ctx, action->dsc, action->exts, &flag);
1290 ypr_reference(ctx, action->ref, action->exts, &flag);
1291
1292 LY_ARRAY_FOR(action->typedefs, u) {
1293 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001294 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001295 }
1296
1297 LY_ARRAY_FOR(action->groupings, u) {
1298 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001299 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001300 }
1301
Radek Krejci693262f2019-04-29 15:23:20 +02001302 yprp_inout(ctx, &action->input, &flag);
1303 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001304
1305 LEVEL--;
1306 ypr_close(ctx, flag);
1307}
1308
1309static void
Radek Krejci693262f2019-04-29 15:23:20 +02001310yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1311{
1312 int flag = 0;
1313
Radek Krejci56cc0872019-04-30 09:22:27 +02001314 LYOUT_CHECK(ctx->out);
1315
Radek Krejci241f6b52020-05-21 18:13:49 +02001316 ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001317
1318 LEVEL++;
1319 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1320 yprc_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1321 ypr_status(ctx, action->flags, action->exts, &flag);
1322 ypr_description(ctx, action->dsc, action->exts, &flag);
1323 ypr_reference(ctx, action->ref, action->exts, &flag);
1324
1325 yprc_inout(ctx, action, &action->input, &flag);
1326 yprc_inout(ctx, action, &action->output, &flag);
1327
1328 LEVEL--;
1329 ypr_close(ctx, flag);
1330}
1331
1332static void
1333yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001334{
Radek Krejci241f6b52020-05-21 18:13:49 +02001335 ly_print(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001336 LEVEL++;
1337
Radek Krejci693262f2019-04-29 15:23:20 +02001338 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1339 yprp_when(ctx, node->when, flag);
1340 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001341}
1342
1343static void
Radek Krejci693262f2019-04-29 15:23:20 +02001344yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001345{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001346 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001347
Radek Krejci241f6b52020-05-21 18:13:49 +02001348 ly_print(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001349 LEVEL++;
1350
1351 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1352 LY_ARRAY_FOR(node->when, u) {
1353 yprc_when(ctx, node->when[u], flag);
1354 }
1355 yprc_iffeatures(ctx, node->iffeatures, node->exts, flag);
1356}
1357
1358/* macr oto unify the code */
1359#define YPR_NODE_COMMON2 \
1360 ypr_config(ctx, node->flags, node->exts, flag); \
1361 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1362 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1363 } \
1364 ypr_status(ctx, node->flags, node->exts, flag); \
1365 ypr_description(ctx, node->dsc, node->exts, flag); \
1366 ypr_reference(ctx, node->ref, node->exts, flag)
1367
1368static void
1369yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
1370{
1371 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001372}
1373
1374static void
Radek Krejci693262f2019-04-29 15:23:20 +02001375yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
1376{
1377 YPR_NODE_COMMON2;
1378}
1379
1380#undef YPR_NODE_COMMON2
1381
1382static void
1383yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001384{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001385 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001386 int flag = 0;
1387 struct lysp_node *child;
1388 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1389
Radek Krejci693262f2019-04-29 15:23:20 +02001390 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001391
1392 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001393 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001394 }
1395 if (cont->presence) {
1396 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001397 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001398 }
1399
Radek Krejci693262f2019-04-29 15:23:20 +02001400 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001401
1402 LY_ARRAY_FOR(cont->typedefs, u) {
1403 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001404 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001405 }
1406
1407 LY_ARRAY_FOR(cont->groupings, u) {
1408 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001409 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001410 }
1411
1412 LY_LIST_FOR(cont->child, child) {
1413 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001414 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001415 }
1416
1417 LY_ARRAY_FOR(cont->actions, u) {
1418 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001419 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001420 }
1421
1422 LY_ARRAY_FOR(cont->notifs, u) {
1423 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001424 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001425 }
1426
1427 LEVEL--;
1428 ypr_close(ctx, flag);
1429}
1430
1431static void
Radek Krejci693262f2019-04-29 15:23:20 +02001432yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1433{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001434 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001435 int flag = 0;
1436 struct lysc_node *child;
1437 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1438
1439 yprc_node_common1(ctx, node, &flag);
1440
1441 LY_ARRAY_FOR(cont->musts, u) {
1442 yprc_must(ctx, &cont->musts[u], &flag);
1443 }
1444 if (cont->flags & LYS_PRESENCE) {
1445 ypr_open(ctx->out, &flag);
1446 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1447 }
1448
1449 yprc_node_common2(ctx, node, &flag);
1450
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001451 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001452 LY_LIST_FOR(cont->child, child) {
1453 ypr_open(ctx->out, &flag);
1454 yprc_node(ctx, child);
1455 }
Radek Krejci693262f2019-04-29 15:23:20 +02001456
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001457 LY_ARRAY_FOR(cont->actions, u) {
1458 ypr_open(ctx->out, &flag);
1459 yprc_action(ctx, &cont->actions[u]);
1460 }
Radek Krejci693262f2019-04-29 15:23:20 +02001461
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001462 LY_ARRAY_FOR(cont->notifs, u) {
1463 ypr_open(ctx->out, &flag);
1464 yprc_notification(ctx, &cont->notifs[u]);
1465 }
Radek Krejci693262f2019-04-29 15:23:20 +02001466 }
1467
1468 LEVEL--;
1469 ypr_close(ctx, flag);
1470}
1471
1472static void
1473yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1474{
1475 int flag = 0;
1476 struct lysp_node *child;
1477 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1478
1479 yprp_node_common1(ctx, node, &flag);
1480 yprp_node_common2(ctx, node, &flag);
1481
1482 LY_LIST_FOR(cas->child, child) {
1483 ypr_open(ctx->out, &flag);
1484 yprp_node(ctx, child);
1485 }
1486
1487 LEVEL--;
1488 ypr_close(ctx, flag);
1489}
1490
1491static void
1492yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1493{
1494 int flag = 0;
1495 struct lysc_node *child;
1496
1497 yprc_node_common1(ctx, (struct lysc_node*)cs, &flag);
1498 yprc_node_common2(ctx, (struct lysc_node*)cs, &flag);
1499
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001500 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001501 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
1502 ypr_open(ctx->out, &flag);
1503 yprc_node(ctx, child);
1504 }
Radek Krejci693262f2019-04-29 15:23:20 +02001505 }
1506
1507 LEVEL--;
1508 ypr_close(ctx, flag);
1509}
1510
1511static void
1512yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001513{
1514 int flag = 0;
1515 struct lysp_node *child;
1516 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1517
Radek Krejci693262f2019-04-29 15:23:20 +02001518 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001519
1520 if (choice->dflt) {
1521 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001522 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001523 }
1524
Radek Krejci693262f2019-04-29 15:23:20 +02001525 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001526
1527 LY_LIST_FOR(choice->child, child) {
1528 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001529 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001530 }
1531
1532 LEVEL--;
1533 ypr_close(ctx, flag);
1534}
1535
1536static void
Radek Krejci693262f2019-04-29 15:23:20 +02001537yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1538{
1539 int flag = 0;
1540 struct lysc_node_case *cs;
1541 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1542
1543 yprc_node_common1(ctx, node, &flag);
1544
1545 if (choice->dflt) {
1546 ypr_open(ctx->out, &flag);
1547 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1548 }
1549
1550 yprc_node_common2(ctx, node, &flag);
1551
1552 for (cs = choice->cases; cs; cs = (struct lysc_node_case*)cs->next) {
1553 ypr_open(ctx->out, &flag);
1554 yprc_case(ctx, cs);
1555 }
1556
1557 LEVEL--;
1558 ypr_close(ctx, flag);
1559}
1560
1561static void
1562yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001563{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001564 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001565 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1566
Radek Krejci693262f2019-04-29 15:23:20 +02001567 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001568
Radek Krejci693262f2019-04-29 15:23:20 +02001569 yprp_type(ctx, &leaf->type);
1570 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001571 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001572 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001573 }
Radek Krejci693262f2019-04-29 15:23:20 +02001574 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001575
Radek Krejci693262f2019-04-29 15:23:20 +02001576 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001577
1578 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001579 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001580}
1581
1582static void
Radek Krejci693262f2019-04-29 15:23:20 +02001583yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1584{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001585 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001586 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1587
1588 yprc_node_common1(ctx, node, NULL);
1589
1590 yprc_type(ctx, leaf->type);
1591 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1592 LY_ARRAY_FOR(leaf->musts, u) {
1593 yprc_must(ctx, &leaf->musts[u], NULL);
1594 }
Radek Krejcia1911222019-07-22 17:24:50 +02001595
1596 if (leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001597 yprc_dflt_value(ctx, leaf->dflt, leaf->dflt_mod, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001598 }
Radek Krejci693262f2019-04-29 15:23:20 +02001599
1600 yprc_node_common2(ctx, node, NULL);
1601
1602 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001603 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001604}
1605
1606static void
1607yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001608{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001609 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001610 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1611
Radek Krejci693262f2019-04-29 15:23:20 +02001612 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001613
Radek Krejci693262f2019-04-29 15:23:20 +02001614 yprp_type(ctx, &llist->type);
1615 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001616 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001617 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001618 }
1619 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001620 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001621 }
1622
Radek Krejci693262f2019-04-29 15:23:20 +02001623 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001624
1625 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001626 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001627 }
1628 if (llist->flags & LYS_SET_MAX) {
1629 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001630 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001631 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001632 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001633 }
1634 }
1635
1636 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001637 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001638 }
1639
Radek Krejci693262f2019-04-29 15:23:20 +02001640 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001641 ypr_description(ctx, node->dsc, node->exts, NULL);
1642 ypr_reference(ctx, node->ref, node->exts, NULL);
1643
1644 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001645 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001646}
1647
1648static void
Radek Krejci693262f2019-04-29 15:23:20 +02001649yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1650{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001651 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001652 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1653
1654 yprc_node_common1(ctx, node, NULL);
1655
1656 yprc_type(ctx, llist->type);
1657 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1658 LY_ARRAY_FOR(llist->musts, u) {
1659 yprc_must(ctx, &llist->musts[u], NULL);
1660 }
1661 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001662 yprc_dflt_value(ctx, llist->dflts[u], llist->dflts_mods[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001663 }
1664
1665 ypr_config(ctx, node->flags, node->exts, NULL);
1666
1667 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1668 if (llist->max) {
1669 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1670 } else {
1671 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1672 }
1673
1674 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1675
1676 ypr_status(ctx, node->flags, node->exts, NULL);
1677 ypr_description(ctx, node->dsc, node->exts, NULL);
1678 ypr_reference(ctx, node->ref, node->exts, NULL);
1679
1680 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02001681 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001682}
1683
1684static void
1685yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001686{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001687 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001688 int flag = 0;
1689 struct lysp_node *child;
1690 struct lysp_node_list *list = (struct lysp_node_list *)node;
1691
Radek Krejci693262f2019-04-29 15:23:20 +02001692 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001693
1694 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001695 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001696 }
1697 if (list->key) {
1698 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001699 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001700 }
1701 LY_ARRAY_FOR(list->uniques, u) {
1702 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001703 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001704 }
1705
Radek Krejci693262f2019-04-29 15:23:20 +02001706 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001707
1708 if (list->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001709 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001710 }
1711 if (list->flags & LYS_SET_MAX) {
1712 if (list->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001713 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001714 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001715 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001716 }
1717 }
1718
1719 if (list->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001720 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001721 }
1722
Radek Krejci693262f2019-04-29 15:23:20 +02001723 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001724 ypr_description(ctx, node->dsc, node->exts, NULL);
1725 ypr_reference(ctx, node->ref, node->exts, NULL);
1726
1727 LY_ARRAY_FOR(list->typedefs, u) {
1728 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001729 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001730 }
1731
1732 LY_ARRAY_FOR(list->groupings, u) {
1733 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001734 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001735 }
1736
1737 LY_LIST_FOR(list->child, child) {
1738 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001739 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001740 }
1741
1742 LY_ARRAY_FOR(list->actions, u) {
1743 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001744 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001745 }
1746
1747 LY_ARRAY_FOR(list->notifs, u) {
1748 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001749 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001750 }
1751
1752 LEVEL--;
1753 ypr_close(ctx, flag);
1754}
1755
1756static void
Radek Krejci693262f2019-04-29 15:23:20 +02001757yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1758{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001759 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001760 int flag = 0;
1761 struct lysc_node *child;
1762 struct lysc_node_list *list = (struct lysc_node_list *)node;
1763
1764 yprc_node_common1(ctx, node, &flag);
1765
1766 LY_ARRAY_FOR(list->musts, u) {
1767 yprc_must(ctx, &list->musts[u], NULL);
1768 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001769 if (!(list->flags & LYS_KEYLESS)) {
Radek Krejci693262f2019-04-29 15:23:20 +02001770 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +02001771 ly_print(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001772 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 +02001773 ly_print(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001774 }
Radek Krejci241f6b52020-05-21 18:13:49 +02001775 ly_print(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001776 }
1777 LY_ARRAY_FOR(list->uniques, u) {
1778 ypr_open(ctx->out, &flag);
Radek Krejci241f6b52020-05-21 18:13:49 +02001779 ly_print(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001780 LY_ARRAY_FOR(list->uniques[u], v) {
Radek Krejci241f6b52020-05-21 18:13:49 +02001781 ly_print(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001782 }
1783 ypr_close(ctx, 0);
1784 }
1785
1786 ypr_config(ctx, node->flags, node->exts, NULL);
1787
1788 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1789 if (list->max) {
1790 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1791 } else {
1792 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1793 }
1794
1795 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1796
1797 ypr_status(ctx, node->flags, node->exts, NULL);
1798 ypr_description(ctx, node->dsc, node->exts, NULL);
1799 ypr_reference(ctx, node->ref, node->exts, NULL);
1800
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001801 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001802 LY_LIST_FOR(list->child, child) {
1803 ypr_open(ctx->out, &flag);
1804 yprc_node(ctx, child);
1805 }
Radek Krejci693262f2019-04-29 15:23:20 +02001806
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001807 LY_ARRAY_FOR(list->actions, u) {
1808 ypr_open(ctx->out, &flag);
1809 yprc_action(ctx, &list->actions[u]);
1810 }
Radek Krejci693262f2019-04-29 15:23:20 +02001811
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001812 LY_ARRAY_FOR(list->notifs, u) {
1813 ypr_open(ctx->out, &flag);
1814 yprc_notification(ctx, &list->notifs[u]);
1815 }
Radek Krejci693262f2019-04-29 15:23:20 +02001816 }
1817
1818 LEVEL--;
1819 ypr_close(ctx, flag);
1820}
1821
1822static void
1823yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001824{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001825 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001826 int flag = 0;
1827
Radek Krejci241f6b52020-05-21 18:13:49 +02001828 ly_print(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001829 LEVEL++;
1830
Radek Krejci693262f2019-04-29 15:23:20 +02001831 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1832 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001833
1834 LY_ARRAY_FOR(refine->musts, u) {
1835 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001836 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001837 }
1838
1839 if (refine->presence) {
1840 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001841 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001842 }
1843
1844 LY_ARRAY_FOR(refine->dflts, u) {
1845 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001846 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001847 }
1848
Radek Krejci693262f2019-04-29 15:23:20 +02001849 ypr_config(ctx, refine->flags, refine->exts, &flag);
1850 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001851
1852 if (refine->flags & LYS_SET_MIN) {
1853 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001854 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001855 }
1856 if (refine->flags & LYS_SET_MAX) {
1857 ypr_open(ctx->out, &flag);
1858 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001859 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001860 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001861 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001862 }
1863 }
1864
1865 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1866 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1867
1868 LEVEL--;
1869 ypr_close(ctx, flag);
1870}
1871
1872static void
Radek Krejci693262f2019-04-29 15:23:20 +02001873yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001874{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001875 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001876 struct lysp_node *child;
1877
Radek Krejci241f6b52020-05-21 18:13:49 +02001878 ly_print(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001879 LEVEL++;
1880
Radek Krejci693262f2019-04-29 15:23:20 +02001881 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1882 yprp_when(ctx, aug->when, NULL);
1883 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1884 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001885 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1886 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1887
1888 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001889 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001890 }
1891
1892 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001893 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001894 }
1895
1896 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001897 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001898 }
1899
1900 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001901 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001902}
1903
1904
1905static void
Radek Krejci693262f2019-04-29 15:23:20 +02001906yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001907{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001908 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001909 int flag = 0;
1910 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1911
Radek Krejci693262f2019-04-29 15:23:20 +02001912 yprp_node_common1(ctx, node, &flag);
1913 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001914
1915 LY_ARRAY_FOR(uses->refines, u) {
1916 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001917 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001918 }
1919
1920 LY_ARRAY_FOR(uses->augments, u) {
1921 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001922 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001923 }
1924
1925 LEVEL--;
1926 ypr_close(ctx, flag);
1927}
1928
1929static void
Radek Krejci693262f2019-04-29 15:23:20 +02001930yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001931{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001932 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001933 int flag = 0;
1934 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1935
Radek Krejci693262f2019-04-29 15:23:20 +02001936 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001937
1938 LY_ARRAY_FOR(any->musts, u) {
1939 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001940 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001941 }
1942
Radek Krejci693262f2019-04-29 15:23:20 +02001943 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001944
1945 LEVEL--;
1946 ypr_close(ctx, flag);
1947}
1948
1949static void
Radek Krejci693262f2019-04-29 15:23:20 +02001950yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001951{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001952 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001953 int flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001954 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001955
Radek Krejci693262f2019-04-29 15:23:20 +02001956 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001957
Radek Krejci693262f2019-04-29 15:23:20 +02001958 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001959 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001960 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001961 }
1962
Radek Krejci693262f2019-04-29 15:23:20 +02001963 yprc_node_common2(ctx, node, &flag);
1964
Radek Krejcid3ca0632019-04-16 16:54:54 +02001965 LEVEL--;
1966 ypr_close(ctx, flag);
1967}
1968
1969static void
Radek Krejci693262f2019-04-29 15:23:20 +02001970yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001971{
Radek Krejci56cc0872019-04-30 09:22:27 +02001972 LYOUT_CHECK(ctx->out);
1973
Radek Krejcid3ca0632019-04-16 16:54:54 +02001974 switch (node->nodetype) {
1975 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001976 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001977 break;
1978 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001979 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001980 break;
1981 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001982 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001983 break;
1984 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001985 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001986 break;
1987 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001988 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001989 break;
1990 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001991 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001992 break;
1993 case LYS_ANYXML:
1994 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001995 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001996 break;
1997 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001998 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001999 break;
2000 default:
2001 break;
2002 }
2003}
2004
2005static void
Radek Krejci693262f2019-04-29 15:23:20 +02002006yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
2007{
Radek Krejci56cc0872019-04-30 09:22:27 +02002008 LYOUT_CHECK(ctx->out);
2009
Radek Krejci693262f2019-04-29 15:23:20 +02002010 switch (node->nodetype) {
2011 case LYS_CONTAINER:
2012 yprc_container(ctx, node);
2013 break;
2014 case LYS_CHOICE:
2015 yprc_choice(ctx, node);
2016 break;
2017 case LYS_LEAF:
2018 yprc_leaf(ctx, node);
2019 break;
2020 case LYS_LEAFLIST:
2021 yprc_leaflist(ctx, node);
2022 break;
2023 case LYS_LIST:
2024 yprc_list(ctx, node);
2025 break;
2026 case LYS_ANYXML:
2027 case LYS_ANYDATA:
2028 yprc_anydata(ctx, node);
2029 break;
2030 default:
2031 break;
2032 }
2033}
2034
2035static void
2036yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002037{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002038 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002039 struct lysp_deviate_add *add;
2040 struct lysp_deviate_rpl *rpl;
2041 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08002042 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002043
Radek Krejci241f6b52020-05-21 18:13:49 +02002044 ly_print(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045 LEVEL++;
2046
Radek Krejci693262f2019-04-29 15:23:20 +02002047 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002048 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
2049 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
2050
fredgan2b11ddb2019-10-21 11:03:39 +08002051 LY_LIST_FOR(deviation->deviates, elem) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002052 ly_print(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08002053 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
2054 if (elem->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002055 ly_print(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002056 LEVEL++;
2057
fredgan2b11ddb2019-10-21 11:03:39 +08002058 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002059 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +02002060 ly_print(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002061 continue;
2062 }
fredgan2b11ddb2019-10-21 11:03:39 +08002063 } else if (elem->mod == LYS_DEV_ADD) {
2064 add = (struct lysp_deviate_add*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002065 ly_print(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002066 LEVEL++;
2067
Radek Krejci693262f2019-04-29 15:23:20 +02002068 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
2069 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002070 LY_ARRAY_FOR(add->musts, u) {
2071 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002072 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002073 LY_ARRAY_FOR(add->uniques, u) {
2074 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002075 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002076 LY_ARRAY_FOR(add->dflts, u) {
2077 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002078 }
Radek Krejci693262f2019-04-29 15:23:20 +02002079 ypr_config(ctx, add->flags, add->exts, NULL);
2080 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002081 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002082 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002083 }
2084 if (add->flags & LYS_SET_MAX) {
2085 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002086 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002087 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002088 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002089 }
2090 }
fredgan2b11ddb2019-10-21 11:03:39 +08002091 } else if (elem->mod == LYS_DEV_REPLACE) {
2092 rpl = (struct lysp_deviate_rpl*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002093 ly_print(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094 LEVEL++;
2095
Radek Krejci693262f2019-04-29 15:23:20 +02002096 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002097 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002098 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002099 }
Radek Krejci693262f2019-04-29 15:23:20 +02002100 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
2101 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
2102 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2103 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002104 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002105 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002106 }
2107 if (rpl->flags & LYS_SET_MAX) {
2108 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002109 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002110 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002111 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002112 }
2113 }
fredgan2b11ddb2019-10-21 11:03:39 +08002114 } else if (elem->mod == LYS_DEV_DELETE) {
2115 del = (struct lysp_deviate_del*)elem;
Radek Krejci241f6b52020-05-21 18:13:49 +02002116 ly_print(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002117 LEVEL++;
2118
Radek Krejci693262f2019-04-29 15:23:20 +02002119 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2120 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002121 LY_ARRAY_FOR(del->musts, u) {
2122 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002123 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002124 LY_ARRAY_FOR(del->uniques, u) {
2125 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002126 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002127 LY_ARRAY_FOR(del->dflts, u) {
2128 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002129 }
2130 }
2131
2132 LEVEL--;
2133 ypr_close(ctx, 1);
2134 }
2135
2136 LEVEL--;
2137 ypr_close(ctx, 1);
2138}
2139
Radek Krejci693262f2019-04-29 15:23:20 +02002140/**
2141 * @brief Minimal print of a schema.
2142 *
2143 * To print
2144 * a) compiled schema when it is not compiled or
2145 * b) parsed when the parsed form was already removed
2146 */
2147static LY_ERR
2148ypr_missing_format(struct ypr_ctx *ctx, const struct lys_module *module)
2149{
2150 /* module-header-stmts */
2151 if (module->version) {
2152 if (module->version) {
2153 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", NULL);
2154 }
2155 }
2156 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, NULL);
2157 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, NULL);
2158
2159 /* meta-stmts */
2160 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002161 ly_print(ctx->out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002162 }
2163 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, NULL);
2164 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, NULL);
2165 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, NULL);
2166 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, NULL);
2167
2168 /* revision-stmts */
2169 if (module->revision) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002170 ly_print(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002171 }
2172
2173 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002174 ly_print(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002175 ly_print_flush(ctx->out);
2176
2177 return LY_SUCCESS;
2178}
2179
Radek Krejcid3ca0632019-04-16 16:54:54 +02002180LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002181yang_print_parsed(struct ly_out *out, const struct lys_module *module)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002182{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002183 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002184 struct lysp_node *data;
2185 struct lysp_module *modp = module->parsed;
Radek Krejci693262f2019-04-29 15:23:20 +02002186 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED}, *ctx = &ctx_;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002187
Radek Krejci241f6b52020-05-21 18:13:49 +02002188 ly_print(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002189 LEVEL++;
2190
Radek Krejci693262f2019-04-29 15:23:20 +02002191 if (!modp) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002192 ly_print(ctx->out, "%*s/* PARSED INFORMATION ARE NOT FULLY PRESENT */\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002193 return ypr_missing_format(ctx, module);
2194 }
2195
Radek Krejcid3ca0632019-04-16 16:54:54 +02002196 /* module-header-stmts */
2197 if (module->version) {
2198 if (module->version) {
Radek Krejci693262f2019-04-29 15:23:20 +02002199 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002200 }
2201 }
Radek Krejci693262f2019-04-29 15:23:20 +02002202 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2203 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002204
2205 /* linkage-stmts */
2206 LY_ARRAY_FOR(modp->imports, u) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002207 ly_print(out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].module->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002208 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002209 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2210 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002211 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002212 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002213 }
Radek Krejci693262f2019-04-29 15:23:20 +02002214 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2215 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002216 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002217 ly_print(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002218 }
2219 LY_ARRAY_FOR(modp->includes, u) {
2220 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002221 ly_print(out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].submodule->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002222 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002223 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002224 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002225 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002226 }
Radek Krejci693262f2019-04-29 15:23:20 +02002227 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2228 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002229 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002230 ly_print(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002231 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +02002232 ly_print(out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].submodule->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002233 }
2234 }
2235
2236 /* meta-stmts */
2237 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002238 ly_print(out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002239 }
Radek Krejci693262f2019-04-29 15:23:20 +02002240 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2241 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2242 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2243 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002244
2245 /* revision-stmts */
2246 if (modp->revs) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002247 ly_print(out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002248 }
2249 LY_ARRAY_FOR(modp->revs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002250 yprp_revision(ctx, &modp->revs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002251 }
2252 /* body-stmts */
2253 LY_ARRAY_FOR(modp->extensions, u) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002254 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002255 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002256 }
2257 if (modp->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002258 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002259 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->parsed->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002260 }
2261
2262 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002263 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002264 }
2265
2266 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002267 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002268 }
2269
2270 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002271 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002272 }
2273
2274 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002275 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002276 }
2277
2278 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002279 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002280 }
2281
2282 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002283 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002284 }
2285
2286 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002287 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002288 }
2289
2290 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002291 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002292 }
2293
2294 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002295 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002296 }
2297
2298 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002299 ly_print(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002300 ly_print_flush(out);
2301
2302 return LY_SUCCESS;
2303}
2304
2305LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002306yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002307{
2308 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options}, *ctx = &ctx_;
2309
2310 yprc_node(ctx, node);
2311
2312 ly_print_flush(out);
2313 return LY_SUCCESS;
2314}
2315
2316LY_ERR
Radek Krejci241f6b52020-05-21 18:13:49 +02002317yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002318{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002319 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002320 struct lysc_node *data;
2321 struct lysc_module *modc = module->compiled;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002322 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002323
Radek Krejci241f6b52020-05-21 18:13:49 +02002324 ly_print(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002325 LEVEL++;
2326
2327 if (!modc) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002328 ly_print(ctx->out, "%*s/* COMPILED INFORMATION ARE NOT PRESENT */\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002329 return ypr_missing_format(ctx, module);
2330 }
2331
2332 /* module-header-stmts */
2333 if (module->version) {
2334 if (module->version) {
2335 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modc->exts);
2336 }
2337 }
2338 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2339 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2340
2341 /* linkage-stmts */
2342 LY_ARRAY_FOR(modc->imports, u) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002343 ly_print(out, "\n%*simport %s {\n", INDENT, modc->imports[u].module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002344 LEVEL++;
2345 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modc->imports[u].exts, NULL, 0);
2346 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modc->imports[u].prefix, modc->imports[u].exts);
2347 if (modc->imports[u].module->revision) {
2348 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modc->imports[u].module->revision, modc->imports[u].exts);
2349 }
2350 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002351 ly_print(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002352 }
2353
2354 /* meta-stmts */
2355 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002356 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002357 }
2358 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2359 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2360 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2361 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2362
2363 /* revision-stmts */
2364 if (module->revision) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002365 ly_print(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
Radek Krejci693262f2019-04-29 15:23:20 +02002366 }
2367
2368 /* body-stmts */
2369 if (modc->exts) {
Radek Krejci241f6b52020-05-21 18:13:49 +02002370 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002371 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2372 }
2373
2374 LY_ARRAY_FOR(modc->features, u) {
2375 yprc_feature(ctx, &modc->features[u]);
2376 }
2377
2378 LY_ARRAY_FOR(modc->identities, u) {
2379 yprc_identity(ctx, &modc->identities[u]);
2380 }
2381
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08002382 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002383 LY_LIST_FOR(modc->data, data) {
2384 yprc_node(ctx, data);
2385 }
Radek Krejci693262f2019-04-29 15:23:20 +02002386
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002387 LY_ARRAY_FOR(modc->rpcs, u) {
2388 yprc_action(ctx, &modc->rpcs[u]);
2389 }
Radek Krejci693262f2019-04-29 15:23:20 +02002390
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002391 LY_ARRAY_FOR(modc->notifs, u) {
2392 yprc_notification(ctx, &modc->notifs[u]);
2393 }
Radek Krejci693262f2019-04-29 15:23:20 +02002394 }
2395
2396 LEVEL--;
Radek Krejci241f6b52020-05-21 18:13:49 +02002397 ly_print(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002398 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002399
2400 return LY_SUCCESS;
2401}