blob: 8510faa995b411296c6a0e6adee78c39d77a0f34 [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 Krejci47fab892020-11-05 17:02:41 +010022#include <sys/types.h>
Radek Krejci693262f2019-04-29 15:23:20 +020023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020025#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020028#include "out_internal.h"
Radek Krejci77114102021-03-10 15:21:57 +010029#include "plugins_exts.h"
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010030#include "plugins_exts_print.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "plugins_types.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020032#include "printer_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "tree_data.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020036#include "tree_schema.h"
37#include "tree_schema_internal.h"
Radek Krejci693262f2019-04-29 15:23:20 +020038#include "xpath.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020039
Radek Krejcie7b95092019-05-15 11:03:07 +020040/**
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010041 * @brief Types of the YANG printers
42 */
43enum lys_ypr_schema_type {
44 LYS_YPR_PARSED, /**< YANG printer of the parsed schema */
45 LYS_YPR_COMPILED /**< YANG printer of the compiled schema */
46};
47
Radek Krejciaa14a0c2020-09-04 11:16:47 +020048#define YPR_CTX_FLAG_EXTRA_LINE 0x01 /**< Flag for ::ypr_ctx::flags to print extra line in schema */
49
50#define YPR_EXTRA_LINE(COND, CTX) if (COND) { (CTX)->flags |= YPR_CTX_FLAG_EXTRA_LINE; }
51#define YPR_EXTRA_LINE_PRINT(CTX) \
52 if ((CTX)->flags & YPR_CTX_FLAG_EXTRA_LINE) { \
53 (CTX)->flags &= ~YPR_CTX_FLAG_EXTRA_LINE; \
54 if (DO_FORMAT) { \
55 ly_print_((CTX)->out, "\n"); \
56 } \
57 }
58
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010059/**
60 * @brief Compiled YANG printer context
61 *
62 * Note that the YANG extensions API provides getter to the members for the extension plugins.
63 */
64struct lys_ypr_ctx {
65 union {
66 struct {
67 struct ly_out *out; /**< output specification */
68 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
Radek Krejciaa14a0c2020-09-04 11:16:47 +020069 uint16_t flags; /**< internal flags for use by printer */
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010070 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
71 const struct lys_module *module; /**< schema to print */
72 };
73 struct lyspr_ctx printer_ctx;
74 };
75
76 /* YANG printer specific members */
77 enum lys_ypr_schema_type schema; /**< type of the schema to print */
78};
79
80/**
Radek Krejcie7b95092019-05-15 11:03:07 +020081 * @brief Print the given text as content of a double quoted YANG string,
82 * including encoding characters that have special meanings. The quotation marks
83 * are not printed.
84 *
85 * Follows RFC 7950, section 6.1.3.
86 *
87 * @param[in] out Output specification.
88 * @param[in] text String to be printed.
Radek Krejcif56e2a42019-09-09 14:15:25 +020089 * @param[in] len Length of the string from @p text to be printed. In case of -1,
Radek Krejcie7b95092019-05-15 11:03:07 +020090 * the @p text is printed completely as a NULL-terminated string.
91 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020092static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020093ypr_encode(struct ly_out *out, const char *text, ssize_t len)
Radek Krejcid3ca0632019-04-16 16:54:54 +020094{
Radek Krejci1deb5be2020-08-26 16:43:36 +020095 size_t i, start_len;
Radek Krejcid3ca0632019-04-16 16:54:54 +020096 const char *start;
97 char special = 0;
98
99 if (!len) {
100 return;
101 }
102
103 if (len < 0) {
104 len = strlen(text);
105 }
106
107 start = text;
108 start_len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200109 for (i = 0; i < (size_t)len; ++i) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200110 switch (text[i]) {
111 case '\n':
112 case '\t':
113 case '\"':
114 case '\\':
115 special = text[i];
116 break;
117 default:
118 ++start_len;
119 break;
120 }
121
122 if (special) {
Michal Vasko5233e962020-08-14 14:26:20 +0200123 ly_write_(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200124 switch (special) {
125 case '\n':
Michal Vasko5233e962020-08-14 14:26:20 +0200126 ly_write_(out, "\\n", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200127 break;
128 case '\t':
Michal Vasko5233e962020-08-14 14:26:20 +0200129 ly_write_(out, "\\t", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200130 break;
131 case '\"':
Michal Vasko5233e962020-08-14 14:26:20 +0200132 ly_write_(out, "\\\"", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200133 break;
134 case '\\':
Michal Vasko5233e962020-08-14 14:26:20 +0200135 ly_write_(out, "\\\\", 2);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200136 break;
137 }
138
139 start += start_len + 1;
140 start_len = 0;
141
142 special = 0;
143 }
144 }
145
Michal Vasko5233e962020-08-14 14:26:20 +0200146 ly_write_(out, start, start_len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200147}
148
149static void
Radek Krejci857189e2020-09-01 13:26:36 +0200150ypr_open(struct ly_out *out, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200151{
152 if (flag && !*flag) {
153 *flag = 1;
Michal Vasko5233e962020-08-14 14:26:20 +0200154 ly_print_(out, " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200155 }
156}
157
158static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100159ypr_close(struct lys_ypr_ctx *ctx, ly_bool flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200160{
161 if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +0200162 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200163 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200164 ly_print_(ctx->out, ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200165 }
166}
167
168static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100169ypr_text(struct lys_ypr_ctx *ctx, const char *name, const char *text, ly_bool singleline, ly_bool closed)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200170{
171 const char *s, *t;
172
173 if (singleline) {
Michal Vasko5233e962020-08-14 14:26:20 +0200174 ly_print_(ctx->out, "%*s%s \"", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200175 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200176 ly_print_(ctx->out, "%*s%s\n", INDENT, name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200177 LEVEL++;
178
Michal Vasko5233e962020-08-14 14:26:20 +0200179 ly_print_(ctx->out, "%*s\"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200180 }
181 t = text;
182 while ((s = strchr(t, '\n'))) {
183 ypr_encode(ctx->out, t, s - t);
Michal Vasko5233e962020-08-14 14:26:20 +0200184 ly_print_(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200185 t = s + 1;
186 if (*t != '\n') {
Michal Vasko5233e962020-08-14 14:26:20 +0200187 ly_print_(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200188 }
189 }
190
191 ypr_encode(ctx->out, t, strlen(t));
192 if (closed) {
Michal Vasko5233e962020-08-14 14:26:20 +0200193 ly_print_(ctx->out, "\";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200194 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200195 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200196 }
197 if (!singleline) {
198 LEVEL--;
199 }
200}
201
202static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100203yprp_stmt(struct lys_ypr_ctx *ctx, struct lysp_stmt *stmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200204{
205 struct lysp_stmt *childstmt;
206 const char *s, *t;
207
208 if (stmt->arg) {
209 if (stmt->flags) {
Michal Vasko5233e962020-08-14 14:26:20 +0200210 ly_print_(ctx->out, "%*s%s\n", INDENT, stmt->stmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200211 LEVEL++;
Michal Vasko5233e962020-08-14 14:26:20 +0200212 ly_print_(ctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'');
Radek Krejcid3ca0632019-04-16 16:54:54 +0200213 t = stmt->arg;
214 while ((s = strchr(t, '\n'))) {
215 ypr_encode(ctx->out, t, s - t);
Michal Vasko5233e962020-08-14 14:26:20 +0200216 ly_print_(ctx->out, "\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200217 t = s + 1;
218 if (*t != '\n') {
Michal Vasko5233e962020-08-14 14:26:20 +0200219 ly_print_(ctx->out, "%*s ", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200220 }
221 }
222 LEVEL--;
223 ypr_encode(ctx->out, t, strlen(t));
Michal Vasko5233e962020-08-14 14:26:20 +0200224 ly_print_(ctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200226 ly_print_(ctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227 }
228 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200229 ly_print_(ctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200230 }
231
232 if (stmt->child) {
233 LEVEL++;
234 LY_LIST_FOR(stmt->child, childstmt) {
Radek Krejci693262f2019-04-29 15:23:20 +0200235 yprp_stmt(ctx, childstmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200236 }
237 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200238 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200239 }
240}
241
242/**
243 * @param[in] count Number of extensions to print, 0 to print them all.
244 */
245static void
Radek Krejcifc596f92021-02-26 22:40:26 +0100246yprp_extension_instances(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index,
Radek Krejci857189e2020-09-01 13:26:36 +0200247 struct lysp_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200248{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200249 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200250 struct lysp_stmt *stmt;
Radek Krejci857189e2020-09-01 13:26:36 +0200251 ly_bool child_presence;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200252
253 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200254 count = LY_ARRAY_COUNT(ext);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200255 }
256 LY_ARRAY_FOR(ext, u) {
Radek Krejci85ac8312021-03-03 20:21:33 +0100257 struct lysp_ext *ext_def = NULL;
258
Radek Krejcid3ca0632019-04-16 16:54:54 +0200259 if (!count) {
260 break;
261 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200262
Radek Krejcid3ca0632019-04-16 16:54:54 +0200263 count--;
Radek Krejciab430862021-03-02 20:13:40 +0100264 if ((ext->flags & LYS_INTERNAL) || (ext->parent_stmt != substmt) || (ext->parent_stmt_index != substmt_index)) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200265 continue;
266 }
267
Radek Krejci85ac8312021-03-03 20:21:33 +0100268 lysp_ext_find_definition(ctx->module->ctx, &ext[u], NULL, &ext_def);
269 if (!ext_def) {
270 continue;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200271 }
Radek Krejci85ac8312021-03-03 20:21:33 +0100272
273 ypr_open(ctx->out, flag);
274
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100275 if (ext_def->argname) {
Michal Vasko5233e962020-08-14 14:26:20 +0200276 ly_print_(ctx->out, "%*s%s \"", INDENT, ext[u].name);
Radek Krejci85ac8312021-03-03 20:21:33 +0100277 lysp_ext_instance_resolve_argument(ctx->module->ctx, &ext[u], ext_def);
278 ypr_encode(ctx->out, ext[u].argument, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200279 ly_print_(ctx->out, "\"");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200280 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200281 ly_print_(ctx->out, "%*s%s", INDENT, ext[u].name);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200282 }
283
284 child_presence = 0;
285 LEVEL++;
286 LY_LIST_FOR(ext[u].child, stmt) {
287 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
288 continue;
289 }
290 if (!child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200291 ly_print_(ctx->out, " {\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200292 child_presence = 1;
293 }
294 yprp_stmt(ctx, stmt);
295 }
296 LEVEL--;
297 if (child_presence) {
Michal Vasko5233e962020-08-14 14:26:20 +0200298 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200299 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200300 ly_print_(ctx->out, ";\n");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200301 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200302 }
303}
304
Radek Krejcifc596f92021-02-26 22:40:26 +0100305static void yprc_extension_instances(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index,
Radek Krejciadcf63d2021-02-09 10:21:18 +0100306 struct lysc_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count);
Radek Krejci693262f2019-04-29 15:23:20 +0200307
308static void
Radek Krejcifc596f92021-02-26 22:40:26 +0100309ypr_substmt(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200310{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200311 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200312 ly_bool extflag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200313
314 if (!text) {
315 /* nothing to print */
316 return;
317 }
318
Radek Krejcieccf6602021-02-05 19:42:54 +0100319 if (stmt_attr_info[substmt].flags & STMT_FLAG_ID) {
320 ly_print_(ctx->out, "%*s%s %s", INDENT, stmt_attr_info[substmt].name, text);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200321 } else {
Radek Krejcieccf6602021-02-05 19:42:54 +0100322 ypr_text(ctx, stmt_attr_info[substmt].name, text,
323 (stmt_attr_info[substmt].flags & STMT_FLAG_YIN) ? 0 : 1, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200324 }
325
326 LEVEL++;
327 LY_ARRAY_FOR(ext, u) {
Radek Krejciab430862021-03-02 20:13:40 +0100328 if ((((struct lysp_ext_instance *)ext)[u].parent_stmt != substmt) || (((struct lysp_ext_instance *)ext)[u].parent_stmt_index != substmt_index)) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200329 continue;
330 }
Radek Krejciadcf63d2021-02-09 10:21:18 +0100331 if (ctx->schema == LYS_YPR_PARSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200332 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200333 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +0200334 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance *)ext)[u], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200335 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200336 }
337 LEVEL--;
338 ypr_close(ctx, extflag);
339}
340
341static void
Radek Krejcifc596f92021-02-26 22:40:26 +0100342ypr_unsigned(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, unsigned long int attr_value, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200343{
344 char *str;
345
Radek Krejci1deb5be2020-08-26 16:43:36 +0200346 if (asprintf(&str, "%lu", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200347 LOGMEM(ctx->module->ctx);
348 return;
349 }
350 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200351 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200352 free(str);
353}
354
355static void
Radek Krejcifc596f92021-02-26 22:40:26 +0100356ypr_signed(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, signed long int attr_value, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200357{
358 char *str;
359
Radek Krejci1deb5be2020-08-26 16:43:36 +0200360 if (asprintf(&str, "%ld", attr_value) == -1) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200361 LOGMEM(ctx->module->ctx);
362 return;
363 }
364 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200365 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200366 free(str);
367}
368
369static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100370yprp_revision(struct lys_ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200371{
372 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +0200373 ly_print_(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200374 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100375 yprp_extension_instances(ctx, LY_STMT_REVISION, 0, rev->exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +0100376 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, rev->dsc, rev->exts);
377 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200378 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200379 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200380 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200381 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200382 }
383}
384
385static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100386ypr_mandatory(struct lys_ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200387{
388 if (flags & LYS_MAND_MASK) {
389 ypr_open(ctx->out, flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100390 ypr_substmt(ctx, LY_STMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200391 }
392}
393
394static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100395ypr_config(struct lys_ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200396{
397 if (flags & LYS_CONFIG_MASK) {
398 ypr_open(ctx->out, flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100399 ypr_substmt(ctx, LY_STMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200400 }
401}
402
403static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100404ypr_status(struct lys_ypr_ctx *ctx, uint16_t flags, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200405{
406 const char *status = NULL;
407
408 if (flags & LYS_STATUS_CURR) {
409 ypr_open(ctx->out, flag);
410 status = "current";
411 } else if (flags & LYS_STATUS_DEPRC) {
412 ypr_open(ctx->out, flag);
413 status = "deprecated";
414 } else if (flags & LYS_STATUS_OBSLT) {
415 ypr_open(ctx->out, flag);
416 status = "obsolete";
417 }
Radek Krejci693262f2019-04-29 15:23:20 +0200418
Radek Krejcifc596f92021-02-26 22:40:26 +0100419 ypr_substmt(ctx, LY_STMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200420}
421
422static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100423ypr_description(struct lys_ypr_ctx *ctx, const char *dsc, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200424{
425 if (dsc) {
426 ypr_open(ctx->out, flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100427 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200428 }
429}
430
431static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100432ypr_reference(struct lys_ypr_ctx *ctx, const char *ref, void *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200433{
434 if (ref) {
435 ypr_open(ctx->out, flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100436 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200437 }
438}
439
440static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100441yprp_iffeatures(struct lys_ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200442{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200443 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +0200444 ly_bool extflag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200445
Michal Vasko7f45cf22020-10-01 12:49:44 +0200446 LY_ARRAY_FOR(iffs, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200447 ypr_open(ctx->out, flag);
448 extflag = 0;
449
Michal Vasko7f45cf22020-10-01 12:49:44 +0200450 ly_print_(ctx->out, "%*sif-feature \"%s\"", INDENT, iffs[u].str);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200451
452 /* extensions */
453 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200454 LY_ARRAY_FOR(exts, v) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100455 yprp_extension_instances(ctx, LY_STMT_IF_FEATURE, u, &exts[v], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200456 }
457 LEVEL--;
458 ypr_close(ctx, extflag);
459 }
460}
461
462static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100463yprp_extension(struct lys_ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200464{
Radek Krejci857189e2020-09-01 13:26:36 +0200465 ly_bool flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200466 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200467
Michal Vasko5233e962020-08-14 14:26:20 +0200468 ly_print_(ctx->out, "%*sextension %s", INDENT, ext->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200469 LEVEL++;
470
471 if (ext->exts) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100472 yprp_extension_instances(ctx, LY_STMT_EXTENSION, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200473 }
474
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100475 if (ext->argname) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200476 ypr_open(ctx->out, &flag);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100477 ly_print_(ctx->out, "%*sargument %s", INDENT, ext->argname);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200478 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200479 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200480 u = -1;
Radek Krejcifc596f92021-02-26 22:40:26 +0100481 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LY_STMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
482 yprp_extension_instances(ctx, LY_STMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200483 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200484 }
485 if ((ext->flags & LYS_YINELEM_MASK) ||
Radek Krejcifc596f92021-02-26 22:40:26 +0100486 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LY_STMT_YIN_ELEMENT) != LY_ARRAY_COUNT(ext->exts)))) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200487 ypr_open(ctx->out, &flag2);
Radek Krejcifc596f92021-02-26 22:40:26 +0100488 ypr_substmt(ctx, LY_STMT_YIN_ELEMENT, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200489 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200490 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200491 ypr_close(ctx, flag2);
492 }
493
Radek Krejci693262f2019-04-29 15:23:20 +0200494 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200495 ypr_description(ctx, ext->dsc, ext->exts, &flag);
496 ypr_reference(ctx, ext->ref, ext->exts, &flag);
497
498 LEVEL--;
499 ypr_close(ctx, flag);
500}
501
502static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100503yprp_feature(struct lys_ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200504{
Radek Krejci857189e2020-09-01 13:26:36 +0200505 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200506
Radek Krejciaa14a0c2020-09-04 11:16:47 +0200507 ly_print_(ctx->out, "%*sfeature %s", INDENT, feat->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200508 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100509 yprp_extension_instances(ctx, LY_STMT_FEATURE, 0, feat->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200510 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
511 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200512 ypr_description(ctx, feat->dsc, feat->exts, &flag);
513 ypr_reference(ctx, feat->ref, feat->exts, &flag);
514 LEVEL--;
515 ypr_close(ctx, flag);
516}
517
518static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100519yprp_identity(struct lys_ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200520{
Radek Krejci857189e2020-09-01 13:26:36 +0200521 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200522 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200523
Radek Krejciaa14a0c2020-09-04 11:16:47 +0200524 ly_print_(ctx->out, "%*sidentity %s", INDENT, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200525 LEVEL++;
526
Radek Krejci39b7fc22021-02-26 23:29:18 +0100527 yprp_extension_instances(ctx, LY_STMT_IDENTITY, 0, ident->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200528 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200529
530 LY_ARRAY_FOR(ident->bases, u) {
531 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100532 ypr_substmt(ctx, LY_STMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200533 }
534
Radek Krejci693262f2019-04-29 15:23:20 +0200535 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200536 ypr_description(ctx, ident->dsc, ident->exts, &flag);
537 ypr_reference(ctx, ident->ref, ident->exts, &flag);
538
539 LEVEL--;
540 ypr_close(ctx, flag);
541}
542
543static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100544yprc_identity(struct lys_ypr_ctx *ctx, const struct lysc_ident *ident)
Radek Krejci693262f2019-04-29 15:23:20 +0200545{
Radek Krejci857189e2020-09-01 13:26:36 +0200546 ly_bool flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200547 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200548
Radek Krejciaa14a0c2020-09-04 11:16:47 +0200549 ly_print_(ctx->out, "%*sidentity %s", INDENT, ident->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200550 LEVEL++;
551
Radek Krejci39b7fc22021-02-26 23:29:18 +0100552 yprc_extension_instances(ctx, LY_STMT_IDENTITY, 0, ident->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200553
aPiecekf4a0a192021-08-03 15:14:17 +0200554 ypr_open(ctx->out, &flag);
555 if (lys_identity_iffeature_value(ident) == LY_ENOT) {
556 ly_print_(ctx->out, "%*s/* identity \"%s\" is disabled by if-feature(s) */\n", INDENT, ident->name);
557 }
558
Radek Krejci693262f2019-04-29 15:23:20 +0200559 LY_ARRAY_FOR(ident->derived, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200560 if (ctx->module != ident->derived[u]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200561 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 +0200562 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200563 ly_print_(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200564 }
565 }
566
567 ypr_status(ctx, ident->flags, ident->exts, &flag);
568 ypr_description(ctx, ident->dsc, ident->exts, &flag);
569 ypr_reference(ctx, ident->ref, ident->exts, &flag);
570
571 LEVEL--;
572 ypr_close(ctx, flag);
573}
574
575static void
Radek Krejci39b7fc22021-02-26 23:29:18 +0100576yprp_restr(struct lys_ypr_ctx *ctx, const struct lysp_restr *restr, enum ly_stmt stmt, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200577{
Radek Krejci857189e2020-09-01 13:26:36 +0200578 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200579
580 if (!restr) {
581 return;
582 }
583
584 ypr_open(ctx->out, flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +0100585 ly_print_(ctx->out, "%*s%s \"", INDENT, ly_stmt2str(stmt));
Radek Krejcif13b87b2020-12-01 22:02:17 +0100586 ypr_encode(ctx->out,
587 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
588 restr->arg.str : &restr->arg.str[1], -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200589 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200590
591 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100592 yprp_extension_instances(ctx, stmt, 0, restr->exts, &inner_flag, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100593 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200594 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
595 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100596 ypr_substmt(ctx, LY_STMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200597 }
598 if (restr->emsg) {
599 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100600 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200601 }
602 if (restr->eapptag) {
603 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100604 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200605 }
Radek Krejci693262f2019-04-29 15:23:20 +0200606 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
607 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
608
Radek Krejcid3ca0632019-04-16 16:54:54 +0200609 LEVEL--;
610 ypr_close(ctx, inner_flag);
611}
612
613static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100614yprc_must(struct lys_ypr_ctx *ctx, const struct lysc_must *must, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200615{
Radek Krejci857189e2020-09-01 13:26:36 +0200616 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200617
618 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200619 ly_print_(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200620 ypr_encode(ctx->out, must->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200621 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200622
623 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100624 yprc_extension_instances(ctx, LY_STMT_MUST, 0, must->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200625 if (must->emsg) {
626 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100627 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, must->emsg, must->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200628 }
629 if (must->eapptag) {
630 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100631 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, must->eapptag, must->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200632 }
633 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
634 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
635
636 LEVEL--;
637 ypr_close(ctx, inner_flag);
638}
639
640static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100641yprc_range(struct lys_ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200642{
Radek Krejci857189e2020-09-01 13:26:36 +0200643 ly_bool inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200644 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200645
Radek Krejci334ccc72019-06-12 13:49:29 +0200646 if (!range) {
647 return;
648 }
649
Radek Krejci693262f2019-04-29 15:23:20 +0200650 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200651 ly_print_(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200652 LY_ARRAY_FOR(range->parts, u) {
653 if (u > 0) {
Michal Vasko5233e962020-08-14 14:26:20 +0200654 ly_print_(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200655 }
656 if (range->parts[u].max_64 == range->parts[u].min_64) {
657 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200658 ly_print_(ctx->out, "%" PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200659 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200660 ly_print_(ctx->out, "%" PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200661 }
662 } else {
663 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200664 ly_print_(ctx->out, "%" PRIu64 "..%" PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200665 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200666 ly_print_(ctx->out, "%" PRId64 "..%" PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200667 }
668 }
669 }
Michal Vasko5233e962020-08-14 14:26:20 +0200670 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200671
672 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100673 yprc_extension_instances(ctx, LY_STMT_RANGE, 0, range->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200674 if (range->emsg) {
675 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100676 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, range->emsg, range->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200677 }
678 if (range->eapptag) {
679 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100680 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, range->eapptag, range->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200681 }
682 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
683 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
684
685 LEVEL--;
686 ypr_close(ctx, inner_flag);
687}
688
689static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100690yprc_pattern(struct lys_ypr_ctx *ctx, const struct lysc_pattern *pattern, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200691{
Radek Krejci857189e2020-09-01 13:26:36 +0200692 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200693
694 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200695 ly_print_(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200696 ypr_encode(ctx->out, pattern->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200697 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200698
699 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100700 yprc_extension_instances(ctx, LY_STMT_PATTERN, 0, pattern->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200701 if (pattern->inverted) {
702 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
703 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100704 ypr_substmt(ctx, LY_STMT_MODIFIER, 0, "invert-match", pattern->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200705 }
706 if (pattern->emsg) {
707 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100708 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, pattern->emsg, pattern->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200709 }
710 if (pattern->eapptag) {
711 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100712 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, pattern->eapptag, pattern->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200713 }
714 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
715 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
716
717 LEVEL--;
718 ypr_close(ctx, inner_flag);
719}
720
721static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100722yprp_when(struct lys_ypr_ctx *ctx, struct lysp_when *when, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200723{
Radek Krejci857189e2020-09-01 13:26:36 +0200724 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200725
726 if (!when) {
727 return;
728 }
729 ypr_open(ctx->out, flag);
730
Michal Vasko5233e962020-08-14 14:26:20 +0200731 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200732 ypr_encode(ctx->out, when->cond, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200733 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200734
735 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100736 yprp_extension_instances(ctx, LY_STMT_WHEN, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200737 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
738 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
739 LEVEL--;
740 ypr_close(ctx, inner_flag);
741}
742
743static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100744yprc_when(struct lys_ypr_ctx *ctx, struct lysc_when *when, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200745{
Radek Krejci857189e2020-09-01 13:26:36 +0200746 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200747
748 if (!when) {
749 return;
750 }
751 ypr_open(ctx->out, flag);
752
Michal Vasko5233e962020-08-14 14:26:20 +0200753 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200754 ypr_encode(ctx->out, when->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200755 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200756
757 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100758 yprc_extension_instances(ctx, LY_STMT_WHEN, 0, when->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200759 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
760 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
761 LEVEL--;
762 ypr_close(ctx, inner_flag);
763}
764
765static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100766yprp_enum(struct lys_ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200767{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200768 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200769 ly_bool inner_flag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200770
771 LY_ARRAY_FOR(items, u) {
772 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200773 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200774 ly_print_(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200775 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200776 ly_print_(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200777 ypr_encode(ctx->out, items[u].name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200778 ly_print_(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200779 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200780 inner_flag = 0;
781 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100782 yprp_extension_instances(ctx, LY_STMT_ENUM, 0, items[u].exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200783 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200784 if (items[u].flags & LYS_SET_VALUE) {
785 if (type == LY_TYPE_BITS) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100786 ypr_unsigned(ctx, LY_STMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200787 } else { /* LY_TYPE_ENUM */
Radek Krejcifc596f92021-02-26 22:40:26 +0100788 ypr_signed(ctx, LY_STMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200789 }
790 }
Radek Krejci693262f2019-04-29 15:23:20 +0200791 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200792 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
793 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
794 LEVEL--;
795 ypr_close(ctx, inner_flag);
796 }
797}
798
799static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100800yprp_type(struct lys_ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200801{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200802 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200803 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200804
Michal Vasko5233e962020-08-14 14:26:20 +0200805 ly_print_(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200806 LEVEL++;
807
Radek Krejci39b7fc22021-02-26 23:29:18 +0100808 yprp_extension_instances(ctx, LY_STMT_TYPE, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200809
Radek Krejci39b7fc22021-02-26 23:29:18 +0100810 yprp_restr(ctx, type->range, LY_STMT_RANGE, &flag);
811 yprp_restr(ctx, type->length, LY_STMT_LENGTH, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200812 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100813 yprp_restr(ctx, &type->patterns[u], LY_STMT_PATTERN, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200814 }
Radek Krejci693262f2019-04-29 15:23:20 +0200815 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
816 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200817
818 if (type->path) {
819 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100820 ypr_substmt(ctx, LY_STMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200821 }
822 if (type->flags & LYS_SET_REQINST) {
823 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100824 ypr_substmt(ctx, LY_STMT_REQUIRE_INSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200825 }
826 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100827 ypr_unsigned(ctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200828 }
829 LY_ARRAY_FOR(type->bases, u) {
830 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100831 ypr_substmt(ctx, LY_STMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200832 }
833 LY_ARRAY_FOR(type->types, u) {
834 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200835 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200836 }
837
838 LEVEL--;
839 ypr_close(ctx, flag);
840}
841
842static void
Radek Krejci224d4b42021-04-23 13:54:59 +0200843yprc_dflt_value(struct lys_ypr_ctx *ctx, const struct ly_ctx *ly_ctx, const struct lyd_value *value,
844 struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200845{
Radek Krejci857189e2020-09-01 13:26:36 +0200846 ly_bool dynamic;
Radek Krejcia1911222019-07-22 17:24:50 +0200847 const char *str;
848
Radek Krejci224d4b42021-04-23 13:54:59 +0200849 str = value->realtype->plugin->print(ly_ctx, value, LY_VALUE_JSON, NULL, &dynamic, NULL);
Radek Krejcifc596f92021-02-26 22:40:26 +0100850 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, str, exts);
Radek Krejcia1911222019-07-22 17:24:50 +0200851 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200852 free((void *)str);
Radek Krejcia1911222019-07-22 17:24:50 +0200853 }
854}
855
856static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100857yprc_type(struct lys_ypr_ctx *ctx, const struct lysc_type *type)
Radek Krejci693262f2019-04-29 15:23:20 +0200858{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200859 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200860 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200861
Michal Vasko5233e962020-08-14 14:26:20 +0200862 ly_print_(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200863 LEVEL++;
864
Radek Krejci39b7fc22021-02-26 23:29:18 +0100865 yprc_extension_instances(ctx, LY_STMT_TYPE, 0, type->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200866
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200867 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200868 case LY_TYPE_BINARY: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200869 struct lysc_type_bin *bin = (struct lysc_type_bin *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200870 yprc_range(ctx, bin->length, type->basetype, &flag);
871 break;
872 }
873 case LY_TYPE_UINT8:
874 case LY_TYPE_UINT16:
875 case LY_TYPE_UINT32:
876 case LY_TYPE_UINT64:
877 case LY_TYPE_INT8:
878 case LY_TYPE_INT16:
879 case LY_TYPE_INT32:
880 case LY_TYPE_INT64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200881 struct lysc_type_num *num = (struct lysc_type_num *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200882 yprc_range(ctx, num->range, type->basetype, &flag);
883 break;
884 }
885 case LY_TYPE_STRING: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200886 struct lysc_type_str *str = (struct lysc_type_str *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200887 yprc_range(ctx, str->length, type->basetype, &flag);
888 LY_ARRAY_FOR(str->patterns, u) {
889 yprc_pattern(ctx, str->patterns[u], &flag);
890 }
891 break;
892 }
893 case LY_TYPE_BITS:
894 case LY_TYPE_ENUM: {
895 /* bits and enums structures are compatible */
Michal Vasko22df3f02020-08-24 13:29:22 +0200896 struct lysc_type_bits *bits = (struct lysc_type_bits *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200897 LY_ARRAY_FOR(bits->bits, u) {
898 struct lysc_type_bitenum_item *item = &bits->bits[u];
Radek Krejci857189e2020-09-01 13:26:36 +0200899 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200900
901 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200902 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200903 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200904 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200905 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200906 if (type->basetype == LY_TYPE_BITS) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100907 yprc_extension_instances(ctx, LY_STMT_BIT, 0, item->exts, &inner_flag, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +0100908 ypr_unsigned(ctx, LY_STMT_POSITION, 0, item->exts, item->position, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200909 } else { /* LY_TYPE_ENUM */
Radek Krejci39b7fc22021-02-26 23:29:18 +0100910 yprc_extension_instances(ctx, LY_STMT_ENUM, 0, item->exts, &inner_flag, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +0100911 ypr_signed(ctx, LY_STMT_VALUE, 0, item->exts, item->value, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200912 }
913 ypr_status(ctx, item->flags, item->exts, &inner_flag);
914 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
915 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
916 LEVEL--;
917 ypr_close(ctx, inner_flag);
918 }
919 break;
920 }
921 case LY_TYPE_BOOL:
922 case LY_TYPE_EMPTY:
923 /* nothing to do */
924 break;
925 case LY_TYPE_DEC64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200926 struct lysc_type_dec *dec = (struct lysc_type_dec *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200927 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100928 ypr_unsigned(ctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, dec->fraction_digits, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200929 yprc_range(ctx, dec->range, dec->basetype, &flag);
930 break;
931 }
932 case LY_TYPE_IDENT: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200933 struct lysc_type_identityref *ident = (struct lysc_type_identityref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200934 LY_ARRAY_FOR(ident->bases, u) {
935 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100936 ypr_substmt(ctx, LY_STMT_BASE, u, ident->bases[u]->name, type->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200937 }
938 break;
939 }
940 case LY_TYPE_INST: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200941 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200942 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100943 ypr_substmt(ctx, LY_STMT_REQUIRE_INSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200944 break;
945 }
946 case LY_TYPE_LEAFREF: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200947 struct lysc_type_leafref *lr = (struct lysc_type_leafref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200948 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100949 ypr_substmt(ctx, LY_STMT_PATH, 0, lr->path->expr, lr->exts);
950 ypr_substmt(ctx, LY_STMT_REQUIRE_INSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200951 yprc_type(ctx, lr->realtype);
952 break;
953 }
954 case LY_TYPE_UNION: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200955 struct lysc_type_union *un = (struct lysc_type_union *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200956 LY_ARRAY_FOR(un->types, u) {
957 ypr_open(ctx->out, &flag);
958 yprc_type(ctx, un->types[u]);
959 }
960 break;
961 }
962 default:
963 LOGINT(ctx->module->ctx);
964 }
965
966 LEVEL--;
967 ypr_close(ctx, flag);
968}
969
970static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100971yprp_typedef(struct lys_ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200972{
Radek Krejciaa14a0c2020-09-04 11:16:47 +0200973 ly_print_(ctx->out, "%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200974 LEVEL++;
975
Radek Krejci39b7fc22021-02-26 23:29:18 +0100976 yprp_extension_instances(ctx, LY_STMT_TYPEDEF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200977
Radek Krejci693262f2019-04-29 15:23:20 +0200978 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200979
980 if (tpdf->units) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100981 ypr_substmt(ctx, LY_STMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200982 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200983 if (tpdf->dflt.str) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100984 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200985 }
986
Radek Krejci693262f2019-04-29 15:23:20 +0200987 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200988 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
989 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
990
991 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200992 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200993}
994
Radek Krejciadcf63d2021-02-09 10:21:18 +0100995static void yprp_node(struct lys_ypr_ctx *ctx, const struct lysp_node *node);
996static void yprc_node(struct lys_ypr_ctx *ctx, const struct lysc_node *node);
997static void yprp_action(struct lys_ypr_ctx *ctx, const struct lysp_node_action *action);
998static void yprp_notification(struct lys_ypr_ctx *ctx, const struct lysp_node_notif *notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200999
1000static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001001yprp_grouping(struct lys_ypr_ctx *ctx, const struct lysp_node_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001002{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001003 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001004 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001005 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001006 struct lysp_node_action *action;
1007 struct lysp_node_notif *notif;
1008 struct lysp_node_grp *subgrp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001009
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001010 ly_print_(ctx->out, "%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001011 LEVEL++;
1012
Radek Krejci39b7fc22021-02-26 23:29:18 +01001013 yprp_extension_instances(ctx, LY_STMT_GROUPING, 0, grp->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001014 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001015 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1016 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001017
1018 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001019 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001020 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001021 }
1022
Radek Krejci2a9fc652021-01-22 17:44:34 +01001023 LY_LIST_FOR(grp->groupings, subgrp) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001024 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001025 yprp_grouping(ctx, subgrp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001026 }
1027
Radek Krejci01180ac2021-01-27 08:48:22 +01001028 LY_LIST_FOR(grp->child, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001029 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001030 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001031 }
1032
Radek Krejci2a9fc652021-01-22 17:44:34 +01001033 LY_LIST_FOR(grp->actions, action) {
1034 ypr_open(ctx->out, &flag);
1035 yprp_action(ctx, action);
1036 }
1037
1038 LY_LIST_FOR(grp->notifs, notif) {
1039 ypr_open(ctx->out, &flag);
1040 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001041 }
1042
1043 LEVEL--;
1044 ypr_close(ctx, flag);
1045}
1046
1047static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001048yprp_inout(struct lys_ypr_ctx *ctx, const struct lysp_node_action_inout *inout, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001049{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001050 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001051 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001052 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001053
Radek Krejci01180ac2021-01-27 08:48:22 +01001054 if (!inout->child) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001055 /* no children */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001056 return;
1057 }
1058 ypr_open(ctx->out, flag);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001059 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001060
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001061 ly_print_(ctx->out, "%*s%s {\n", INDENT, inout->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001062 LEVEL++;
1063
Radek Krejci39b7fc22021-02-26 23:29:18 +01001064 yprp_extension_instances(ctx, LY_STMT_MUST, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001065 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001066 yprp_restr(ctx, &inout->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001067 }
1068 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001069 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001070 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001071 LY_LIST_FOR(inout->groupings, grp) {
1072 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001073 }
1074
Radek Krejci01180ac2021-01-27 08:48:22 +01001075 LY_LIST_FOR(inout->child, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001076 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001077 }
1078
1079 LEVEL--;
1080 ypr_close(ctx, 1);
1081}
1082
1083static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001084yprc_inout(struct lys_ypr_ctx *ctx, const struct lysc_node_action_inout *inout, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001085{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001086 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001087 struct lysc_node *data;
1088
Radek Krejci01180ac2021-01-27 08:48:22 +01001089 if (!inout->child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001090 /* input/output is empty */
1091 return;
1092 }
1093 ypr_open(ctx->out, flag);
1094
Michal Vasko544e58a2021-01-28 14:33:41 +01001095 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, inout->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001096 LEVEL++;
1097
Radek Krejci39b7fc22021-02-26 23:29:18 +01001098 yprc_extension_instances(ctx, lys_nodetype2stmt(inout->nodetype), 0, inout->exts, NULL, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001099 LY_ARRAY_FOR(inout->musts, u) {
1100 yprc_must(ctx, &inout->musts[u], NULL);
1101 }
1102
Radek Krejci52f65552020-09-01 17:03:35 +02001103 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci01180ac2021-01-27 08:48:22 +01001104 LY_LIST_FOR(inout->child, data) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001105 yprc_node(ctx, data);
1106 }
Radek Krejci693262f2019-04-29 15:23:20 +02001107 }
1108
1109 LEVEL--;
1110 ypr_close(ctx, 1);
1111}
1112
1113static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001114yprp_notification(struct lys_ypr_ctx *ctx, const struct lysp_node_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001115{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001116 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001117 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001118 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001119 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001120
Michal Vasko5233e962020-08-14 14:26:20 +02001121 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122
1123 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001124 yprp_extension_instances(ctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001125 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001126
1127 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001128 yprp_restr(ctx, &notif->musts[u], LY_STMT_MUST, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001129 }
Radek Krejci693262f2019-04-29 15:23:20 +02001130 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001131 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1132 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1133
1134 LY_ARRAY_FOR(notif->typedefs, u) {
1135 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001136 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001137 }
1138
Radek Krejci2a9fc652021-01-22 17:44:34 +01001139 LY_LIST_FOR(notif->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001140 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001141 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001142 }
1143
Radek Krejci01180ac2021-01-27 08:48:22 +01001144 LY_LIST_FOR(notif->child, data) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001145 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001146 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001147 }
1148
1149 LEVEL--;
1150 ypr_close(ctx, flag);
1151}
1152
1153static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001154yprc_notification(struct lys_ypr_ctx *ctx, const struct lysc_node_notif *notif)
Radek Krejci693262f2019-04-29 15:23:20 +02001155{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001156 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001157 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001158 struct lysc_node *data;
1159
Michal Vasko5233e962020-08-14 14:26:20 +02001160 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001161
1162 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001163 yprc_extension_instances(ctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001164
1165 LY_ARRAY_FOR(notif->musts, u) {
1166 yprc_must(ctx, &notif->musts[u], &flag);
1167 }
1168 ypr_status(ctx, notif->flags, notif->exts, &flag);
1169 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1170 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1171
Radek Krejci52f65552020-09-01 17:03:35 +02001172 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci01180ac2021-01-27 08:48:22 +01001173 LY_LIST_FOR(notif->child, data) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001174 ypr_open(ctx->out, &flag);
1175 yprc_node(ctx, data);
1176 }
Radek Krejci693262f2019-04-29 15:23:20 +02001177 }
1178
1179 LEVEL--;
1180 ypr_close(ctx, flag);
1181}
1182
1183static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001184yprp_action(struct lys_ypr_ctx *ctx, const struct lysp_node_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001185{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001186 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001187 ly_bool flag = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001188 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001189
Michal Vasko5233e962020-08-14 14:26:20 +02001190 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001191
1192 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001193 yprp_extension_instances(ctx, lys_nodetype2stmt(action->nodetype), 0, action->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001194 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1195 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001196 ypr_description(ctx, action->dsc, action->exts, &flag);
1197 ypr_reference(ctx, action->ref, action->exts, &flag);
1198
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001199 YPR_EXTRA_LINE(flag, ctx);
1200
Radek Krejcid3ca0632019-04-16 16:54:54 +02001201 LY_ARRAY_FOR(action->typedefs, u) {
1202 ypr_open(ctx->out, &flag);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001203 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02001204 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001205 }
1206
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001207 YPR_EXTRA_LINE(action->typedefs, ctx);
1208
Radek Krejci2a9fc652021-01-22 17:44:34 +01001209 LY_LIST_FOR(action->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001210 ypr_open(ctx->out, &flag);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001211 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001212 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001213 }
1214
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001215 YPR_EXTRA_LINE(action->groupings, ctx);
1216
Radek Krejci693262f2019-04-29 15:23:20 +02001217 yprp_inout(ctx, &action->input, &flag);
1218 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001219
1220 LEVEL--;
1221 ypr_close(ctx, flag);
1222}
1223
1224static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001225yprc_action(struct lys_ypr_ctx *ctx, const struct lysc_node_action *action)
Radek Krejci693262f2019-04-29 15:23:20 +02001226{
Radek Krejci857189e2020-09-01 13:26:36 +02001227 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001228
Michal Vasko5233e962020-08-14 14:26:20 +02001229 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001230
1231 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001232 yprc_extension_instances(ctx, lys_nodetype2stmt(action->nodetype), 0, action->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001233 ypr_status(ctx, action->flags, action->exts, &flag);
1234 ypr_description(ctx, action->dsc, action->exts, &flag);
1235 ypr_reference(ctx, action->ref, action->exts, &flag);
1236
Michal Vasko544e58a2021-01-28 14:33:41 +01001237 yprc_inout(ctx, &action->input, &flag);
1238 yprc_inout(ctx, &action->output, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001239
1240 LEVEL--;
1241 ypr_close(ctx, flag);
1242}
1243
1244static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001245yprp_node_common1(struct lys_ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001246{
Michal Vasko5233e962020-08-14 14:26:20 +02001247 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001248 LEVEL++;
1249
Radek Krejci39b7fc22021-02-26 23:29:18 +01001250 yprp_extension_instances(ctx, lys_nodetype2stmt(node->nodetype), 0, node->exts, flag, 0);
Radek Krejci9a3823e2021-01-27 20:26:46 +01001251 yprp_when(ctx, lysp_node_when(node), flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001252 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001253}
1254
1255static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001256yprc_node_common1(struct lys_ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001257{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001258 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9a3823e2021-01-27 20:26:46 +01001259 struct lysc_when **when;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001260
Michal Vasko5233e962020-08-14 14:26:20 +02001261 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001262 LEVEL++;
1263
Radek Krejci39b7fc22021-02-26 23:29:18 +01001264 yprc_extension_instances(ctx, lys_nodetype2stmt(node->nodetype), 0, node->exts, flag, 0);
Radek Krejci9a3823e2021-01-27 20:26:46 +01001265
1266 when = lysc_node_when(node);
1267 LY_ARRAY_FOR(when, u) {
1268 yprc_when(ctx, when[u], flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001269 }
Radek Krejci693262f2019-04-29 15:23:20 +02001270}
1271
1272/* macr oto unify the code */
1273#define YPR_NODE_COMMON2 \
1274 ypr_config(ctx, node->flags, node->exts, flag); \
1275 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1276 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1277 } \
1278 ypr_status(ctx, node->flags, node->exts, flag); \
1279 ypr_description(ctx, node->dsc, node->exts, flag); \
1280 ypr_reference(ctx, node->ref, node->exts, flag)
1281
1282static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001283yprp_node_common2(struct lys_ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001284{
1285 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001286}
1287
1288static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001289yprc_node_common2(struct lys_ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001290{
1291 YPR_NODE_COMMON2;
1292}
1293
1294#undef YPR_NODE_COMMON2
1295
1296static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001297yprp_container(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001298{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001299 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001300 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001301 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001302 struct lysp_node_action *action;
1303 struct lysp_node_notif *notif;
1304 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001305 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1306
Radek Krejci693262f2019-04-29 15:23:20 +02001307 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001308
1309 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001310 yprp_restr(ctx, &cont->musts[u], LY_STMT_MUST, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001311 }
1312 if (cont->presence) {
1313 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001314 ypr_substmt(ctx, LY_STMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001315 }
1316
Radek Krejci693262f2019-04-29 15:23:20 +02001317 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001318
1319 LY_ARRAY_FOR(cont->typedefs, u) {
1320 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001321 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001322 }
1323
Radek Krejci2a9fc652021-01-22 17:44:34 +01001324 LY_LIST_FOR(cont->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001325 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001326 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001327 }
1328
1329 LY_LIST_FOR(cont->child, child) {
1330 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001331 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001332 }
1333
Radek Krejci2a9fc652021-01-22 17:44:34 +01001334 LY_LIST_FOR(cont->actions, action) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001335 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001336 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001337 }
1338
Radek Krejci2a9fc652021-01-22 17:44:34 +01001339 LY_LIST_FOR(cont->notifs, notif) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001340 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001341 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001342 }
1343
1344 LEVEL--;
1345 ypr_close(ctx, flag);
1346}
1347
1348static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001349yprc_container(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001350{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001351 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001352 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001353 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001354 struct lysc_node_action *action;
1355 struct lysc_node_notif *notif;
Radek Krejci693262f2019-04-29 15:23:20 +02001356 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1357
1358 yprc_node_common1(ctx, node, &flag);
1359
1360 LY_ARRAY_FOR(cont->musts, u) {
1361 yprc_must(ctx, &cont->musts[u], &flag);
1362 }
1363 if (cont->flags & LYS_PRESENCE) {
1364 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001365 ypr_substmt(ctx, LY_STMT_PRESENCE, 0, "true", cont->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001366 }
1367
1368 yprc_node_common2(ctx, node, &flag);
1369
Radek Krejci52f65552020-09-01 17:03:35 +02001370 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001371 LY_LIST_FOR(cont->child, child) {
1372 ypr_open(ctx->out, &flag);
1373 yprc_node(ctx, child);
1374 }
Radek Krejci693262f2019-04-29 15:23:20 +02001375
Radek Krejci2a9fc652021-01-22 17:44:34 +01001376 LY_LIST_FOR(cont->actions, action) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001377 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001378 yprc_action(ctx, action);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001379 }
Radek Krejci693262f2019-04-29 15:23:20 +02001380
Radek Krejci2a9fc652021-01-22 17:44:34 +01001381 LY_LIST_FOR(cont->notifs, notif) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001382 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001383 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001384 }
Radek Krejci693262f2019-04-29 15:23:20 +02001385 }
1386
1387 LEVEL--;
1388 ypr_close(ctx, flag);
1389}
1390
1391static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001392yprp_case(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001393{
Radek Krejci857189e2020-09-01 13:26:36 +02001394 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001395 struct lysp_node *child;
1396 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1397
1398 yprp_node_common1(ctx, node, &flag);
1399 yprp_node_common2(ctx, node, &flag);
1400
1401 LY_LIST_FOR(cas->child, child) {
1402 ypr_open(ctx->out, &flag);
1403 yprp_node(ctx, child);
1404 }
1405
1406 LEVEL--;
1407 ypr_close(ctx, flag);
1408}
1409
1410static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001411yprc_case(struct lys_ypr_ctx *ctx, const struct lysc_node_case *cs)
Radek Krejci693262f2019-04-29 15:23:20 +02001412{
Radek Krejci857189e2020-09-01 13:26:36 +02001413 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001414 struct lysc_node *child;
1415
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001416 yprc_node_common1(ctx, &cs->node, &flag);
1417 yprc_node_common2(ctx, &cs->node, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001418
Radek Krejci52f65552020-09-01 17:03:35 +02001419 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001420 for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001421 ypr_open(ctx->out, &flag);
1422 yprc_node(ctx, child);
1423 }
Radek Krejci693262f2019-04-29 15:23:20 +02001424 }
1425
1426 LEVEL--;
1427 ypr_close(ctx, flag);
1428}
1429
1430static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001431yprp_choice(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001432{
Radek Krejci857189e2020-09-01 13:26:36 +02001433 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001434 struct lysp_node *child;
1435 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1436
Radek Krejci693262f2019-04-29 15:23:20 +02001437 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001438
Michal Vasko7f45cf22020-10-01 12:49:44 +02001439 if (choice->dflt.str) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001440 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001441 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, choice->dflt.str, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001442 }
1443
Radek Krejci693262f2019-04-29 15:23:20 +02001444 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001445
1446 LY_LIST_FOR(choice->child, child) {
1447 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001448 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001449 }
1450
1451 LEVEL--;
1452 ypr_close(ctx, flag);
1453}
1454
1455static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001456yprc_choice(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001457{
Radek Krejci857189e2020-09-01 13:26:36 +02001458 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001459 struct lysc_node_case *cs;
1460 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1461
1462 yprc_node_common1(ctx, node, &flag);
1463
1464 if (choice->dflt) {
1465 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001466 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, choice->dflt->name, choice->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001467 }
1468
1469 yprc_node_common2(ctx, node, &flag);
1470
Michal Vasko22df3f02020-08-24 13:29:22 +02001471 for (cs = choice->cases; cs; cs = (struct lysc_node_case *)cs->next) {
Radek Krejci693262f2019-04-29 15:23:20 +02001472 ypr_open(ctx->out, &flag);
1473 yprc_case(ctx, cs);
1474 }
1475
1476 LEVEL--;
1477 ypr_close(ctx, flag);
1478}
1479
1480static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001481yprp_leaf(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001482{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001483 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001484 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1485
Radek Krejci693262f2019-04-29 15:23:20 +02001486 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001487
Radek Krejci693262f2019-04-29 15:23:20 +02001488 yprp_type(ctx, &leaf->type);
Radek Krejcifc596f92021-02-26 22:40:26 +01001489 ypr_substmt(ctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001490 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001491 yprp_restr(ctx, &leaf->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001492 }
Radek Krejcifc596f92021-02-26 22:40:26 +01001493 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001494
Radek Krejci693262f2019-04-29 15:23:20 +02001495 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001496
1497 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001498 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001499}
1500
1501static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001502yprc_leaf(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001503{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001504 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001505 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1506
1507 yprc_node_common1(ctx, node, NULL);
1508
1509 yprc_type(ctx, leaf->type);
Radek Krejcifc596f92021-02-26 22:40:26 +01001510 ypr_substmt(ctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001511 LY_ARRAY_FOR(leaf->musts, u) {
1512 yprc_must(ctx, &leaf->musts[u], NULL);
1513 }
Radek Krejcia1911222019-07-22 17:24:50 +02001514
1515 if (leaf->dflt) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001516 yprc_dflt_value(ctx, node->module->ctx, leaf->dflt, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001517 }
Radek Krejci693262f2019-04-29 15:23:20 +02001518
1519 yprc_node_common2(ctx, node, NULL);
1520
1521 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001522 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001523}
1524
1525static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001526yprp_leaflist(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001527{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001528 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001529 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1530
Radek Krejci693262f2019-04-29 15:23:20 +02001531 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001532
Radek Krejci693262f2019-04-29 15:23:20 +02001533 yprp_type(ctx, &llist->type);
Radek Krejcifc596f92021-02-26 22:40:26 +01001534 ypr_substmt(ctx, LY_STMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001535 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001536 yprp_restr(ctx, &llist->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001537 }
1538 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001539 ypr_substmt(ctx, LY_STMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001540 }
1541
Radek Krejci693262f2019-04-29 15:23:20 +02001542 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001543
1544 if (llist->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001545 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001546 }
1547 if (llist->flags & LYS_SET_MAX) {
1548 if (llist->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001549 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001550 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001551 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001552 }
1553 }
1554
1555 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001556 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001557 }
1558
Radek Krejci693262f2019-04-29 15:23:20 +02001559 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001560 ypr_description(ctx, node->dsc, node->exts, NULL);
1561 ypr_reference(ctx, node->ref, node->exts, NULL);
1562
1563 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001564 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001565}
1566
1567static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001568yprc_leaflist(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001569{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001570 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001571 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1572
1573 yprc_node_common1(ctx, node, NULL);
1574
1575 yprc_type(ctx, llist->type);
Radek Krejcifc596f92021-02-26 22:40:26 +01001576 ypr_substmt(ctx, LY_STMT_UNITS, 0, llist->units, llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001577 LY_ARRAY_FOR(llist->musts, u) {
1578 yprc_must(ctx, &llist->musts[u], NULL);
1579 }
1580 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001581 yprc_dflt_value(ctx, node->module->ctx, llist->dflts[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001582 }
1583
1584 ypr_config(ctx, node->flags, node->exts, NULL);
1585
Radek Krejcifc596f92021-02-26 22:40:26 +01001586 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001587 if (llist->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001588 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001589 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001590 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001591 }
1592
Radek Krejcifc596f92021-02-26 22:40:26 +01001593 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001594
1595 ypr_status(ctx, node->flags, node->exts, NULL);
1596 ypr_description(ctx, node->dsc, node->exts, NULL);
1597 ypr_reference(ctx, node->ref, node->exts, NULL);
1598
1599 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001600 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001601}
1602
1603static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001604yprp_list(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001605{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001606 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001607 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001608 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001609 struct lysp_node_action *action;
1610 struct lysp_node_notif *notif;
1611 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001612 struct lysp_node_list *list = (struct lysp_node_list *)node;
1613
Radek Krejci693262f2019-04-29 15:23:20 +02001614 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001615
1616 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001617 yprp_restr(ctx, &list->musts[u], LY_STMT_MUST, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001618 }
1619 if (list->key) {
1620 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001621 ypr_substmt(ctx, LY_STMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001622 }
1623 LY_ARRAY_FOR(list->uniques, u) {
1624 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001625 ypr_substmt(ctx, LY_STMT_UNIQUE, u, list->uniques[u].str, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001626 }
1627
Michal Vaskoacb8e742020-10-20 10:28:02 +02001628 ypr_config(ctx, node->flags, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001629
1630 if (list->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001631 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001632 }
1633 if (list->flags & LYS_SET_MAX) {
1634 if (list->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001635 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001636 } else {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001637 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001638 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001639 }
1640 }
1641
1642 if (list->flags & LYS_ORDBY_MASK) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001643 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001644 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001645 }
1646
Michal Vaskoacb8e742020-10-20 10:28:02 +02001647 ypr_status(ctx, node->flags, node->exts, &flag);
1648 ypr_description(ctx, node->dsc, node->exts, &flag);
1649 ypr_reference(ctx, node->ref, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001650
1651 LY_ARRAY_FOR(list->typedefs, u) {
1652 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001653 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001654 }
1655
Radek Krejci2a9fc652021-01-22 17:44:34 +01001656 LY_LIST_FOR(list->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001657 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001658 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001659 }
1660
1661 LY_LIST_FOR(list->child, child) {
1662 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001663 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001664 }
1665
Radek Krejci2a9fc652021-01-22 17:44:34 +01001666 LY_LIST_FOR(list->actions, action) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001667 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001668 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001669 }
1670
Radek Krejci2a9fc652021-01-22 17:44:34 +01001671 LY_LIST_FOR(list->notifs, notif) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001672 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001673 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001674 }
1675
1676 LEVEL--;
1677 ypr_close(ctx, flag);
1678}
1679
1680static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001681yprc_list(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001682{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001683 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001684 struct lysc_node_list *list = (struct lysc_node_list *)node;
1685
Michal Vaskoacb8e742020-10-20 10:28:02 +02001686 yprc_node_common1(ctx, node, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001687
1688 LY_ARRAY_FOR(list->musts, u) {
1689 yprc_must(ctx, &list->musts[u], NULL);
1690 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001691 if (!(list->flags & LYS_KEYLESS)) {
Michal Vasko5233e962020-08-14 14:26:20 +02001692 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001693 for (struct lysc_node *key = list->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko5233e962020-08-14 14:26:20 +02001694 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001695 }
Michal Vasko5233e962020-08-14 14:26:20 +02001696 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001697 }
1698 LY_ARRAY_FOR(list->uniques, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001699 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001700 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001701 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001702 }
1703 ypr_close(ctx, 0);
1704 }
1705
1706 ypr_config(ctx, node->flags, node->exts, NULL);
1707
Radek Krejcifc596f92021-02-26 22:40:26 +01001708 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001709 if (list->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001710 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001711 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001712 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001713 }
1714
Radek Krejcifc596f92021-02-26 22:40:26 +01001715 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001716
1717 ypr_status(ctx, node->flags, node->exts, NULL);
1718 ypr_description(ctx, node->dsc, node->exts, NULL);
1719 ypr_reference(ctx, node->ref, node->exts, NULL);
1720
Radek Krejci52f65552020-09-01 17:03:35 +02001721 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001722 struct lysc_node *child;
1723 struct lysc_node_action *action;
1724 struct lysc_node_notif *notif;
1725
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001726 LY_LIST_FOR(list->child, child) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001727 yprc_node(ctx, child);
1728 }
Radek Krejci693262f2019-04-29 15:23:20 +02001729
Radek Krejci2a9fc652021-01-22 17:44:34 +01001730 LY_LIST_FOR(list->actions, action) {
1731 yprc_action(ctx, action);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001732 }
Radek Krejci693262f2019-04-29 15:23:20 +02001733
Radek Krejci2a9fc652021-01-22 17:44:34 +01001734 LY_LIST_FOR(list->notifs, notif) {
1735 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001736 }
Radek Krejci693262f2019-04-29 15:23:20 +02001737 }
1738
1739 LEVEL--;
Michal Vaskoacb8e742020-10-20 10:28:02 +02001740 ypr_close(ctx, 1);
Radek Krejci693262f2019-04-29 15:23:20 +02001741}
1742
1743static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001744yprp_refine(struct lys_ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001745{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001746 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001747 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001748
Michal Vasko5233e962020-08-14 14:26:20 +02001749 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001750 LEVEL++;
1751
Radek Krejci39b7fc22021-02-26 23:29:18 +01001752 yprp_extension_instances(ctx, LY_STMT_REFINE, 0, refine->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001753 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001754
1755 LY_ARRAY_FOR(refine->musts, u) {
1756 ypr_open(ctx->out, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001757 yprp_restr(ctx, &refine->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001758 }
1759
1760 if (refine->presence) {
1761 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001762 ypr_substmt(ctx, LY_STMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001763 }
1764
1765 LY_ARRAY_FOR(refine->dflts, u) {
1766 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001767 ypr_substmt(ctx, LY_STMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001768 }
1769
Radek Krejci693262f2019-04-29 15:23:20 +02001770 ypr_config(ctx, refine->flags, refine->exts, &flag);
1771 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001772
1773 if (refine->flags & LYS_SET_MIN) {
1774 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001775 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001776 }
1777 if (refine->flags & LYS_SET_MAX) {
1778 ypr_open(ctx->out, &flag);
1779 if (refine->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001780 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001781 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001782 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001783 }
1784 }
1785
1786 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1787 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1788
1789 LEVEL--;
1790 ypr_close(ctx, flag);
1791}
1792
1793static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001794yprp_augment(struct lys_ypr_ctx *ctx, const struct lysp_node_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001795{
Radek Krejcid3ca0632019-04-16 16:54:54 +02001796 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001797 struct lysp_node_action *action;
1798 struct lysp_node_notif *notif;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001799
Michal Vasko5233e962020-08-14 14:26:20 +02001800 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001801 LEVEL++;
1802
Radek Krejci39b7fc22021-02-26 23:29:18 +01001803 yprp_extension_instances(ctx, LY_STMT_AUGMENT, 0, aug->exts, NULL, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001804 yprp_when(ctx, aug->when, NULL);
1805 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1806 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001807 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1808 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1809
1810 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001811 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001812 }
1813
Radek Krejci2a9fc652021-01-22 17:44:34 +01001814 LY_LIST_FOR(aug->actions, action) {
1815 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001816 }
1817
Radek Krejci2a9fc652021-01-22 17:44:34 +01001818 LY_LIST_FOR(aug->notifs, notif) {
1819 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001820 }
1821
1822 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001823 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001824}
1825
Radek Krejcid3ca0632019-04-16 16:54:54 +02001826static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001827yprp_uses(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001828{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001829 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001830 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001831 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001832 struct lysp_node_augment *aug;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001833
Radek Krejci693262f2019-04-29 15:23:20 +02001834 yprp_node_common1(ctx, node, &flag);
1835 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001836
1837 LY_ARRAY_FOR(uses->refines, u) {
1838 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001839 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001840 }
1841
Radek Krejci2a9fc652021-01-22 17:44:34 +01001842 LY_LIST_FOR(uses->augments, aug) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001843 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001844 yprp_augment(ctx, aug);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001845 }
1846
1847 LEVEL--;
1848 ypr_close(ctx, flag);
1849}
1850
1851static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001852yprp_anydata(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001853{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001854 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001855 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001856 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1857
Radek Krejci693262f2019-04-29 15:23:20 +02001858 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001859
1860 LY_ARRAY_FOR(any->musts, u) {
1861 ypr_open(ctx->out, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001862 yprp_restr(ctx, &any->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001863 }
1864
Radek Krejci693262f2019-04-29 15:23:20 +02001865 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001866
1867 LEVEL--;
1868 ypr_close(ctx, flag);
1869}
1870
1871static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001872yprc_anydata(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001873{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001874 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001875 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001876 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877
Radek Krejci693262f2019-04-29 15:23:20 +02001878 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001879
Radek Krejci693262f2019-04-29 15:23:20 +02001880 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001881 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001882 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001883 }
1884
Radek Krejci693262f2019-04-29 15:23:20 +02001885 yprc_node_common2(ctx, node, &flag);
1886
Radek Krejcid3ca0632019-04-16 16:54:54 +02001887 LEVEL--;
1888 ypr_close(ctx, flag);
1889}
1890
1891static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001892yprp_node(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001893{
1894 switch (node->nodetype) {
1895 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001896 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001897 break;
1898 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001899 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001900 break;
1901 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001902 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001903 break;
1904 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001905 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001906 break;
1907 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001908 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001909 break;
1910 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001911 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001912 break;
1913 case LYS_ANYXML:
1914 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001915 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001916 break;
1917 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001918 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001919 break;
1920 default:
1921 break;
1922 }
1923}
1924
1925static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001926yprc_node(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001927{
1928 switch (node->nodetype) {
1929 case LYS_CONTAINER:
1930 yprc_container(ctx, node);
1931 break;
1932 case LYS_CHOICE:
1933 yprc_choice(ctx, node);
1934 break;
1935 case LYS_LEAF:
1936 yprc_leaf(ctx, node);
1937 break;
1938 case LYS_LEAFLIST:
1939 yprc_leaflist(ctx, node);
1940 break;
1941 case LYS_LIST:
1942 yprc_list(ctx, node);
1943 break;
1944 case LYS_ANYXML:
1945 case LYS_ANYDATA:
1946 yprc_anydata(ctx, node);
1947 break;
1948 default:
1949 break;
1950 }
1951}
1952
1953static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001954yprp_deviation(struct lys_ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001955{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001956 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001957 struct lysp_deviate_add *add;
1958 struct lysp_deviate_rpl *rpl;
1959 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08001960 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001961
Michal Vasko5233e962020-08-14 14:26:20 +02001962 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001963 LEVEL++;
1964
Radek Krejci39b7fc22021-02-26 23:29:18 +01001965 yprp_extension_instances(ctx, LY_STMT_DEVIATION, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001966 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1967 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1968
fredgan2b11ddb2019-10-21 11:03:39 +08001969 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001970 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08001971 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1972 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001973 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001974 LEVEL++;
1975
Radek Krejci39b7fc22021-02-26 23:29:18 +01001976 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001977 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001978 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001979 continue;
1980 }
fredgan2b11ddb2019-10-21 11:03:39 +08001981 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001982 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001983 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001984 LEVEL++;
1985
Radek Krejci39b7fc22021-02-26 23:29:18 +01001986 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, add->exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01001987 ypr_substmt(ctx, LY_STMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001988 LY_ARRAY_FOR(add->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001989 yprp_restr(ctx, &add->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001990 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001991 LY_ARRAY_FOR(add->uniques, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001992 ypr_substmt(ctx, LY_STMT_UNIQUE, u, add->uniques[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001993 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001994 LY_ARRAY_FOR(add->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001995 ypr_substmt(ctx, LY_STMT_DEFAULT, u, add->dflts[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001996 }
Radek Krejci693262f2019-04-29 15:23:20 +02001997 ypr_config(ctx, add->flags, add->exts, NULL);
1998 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001999 if (add->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002000 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002001 }
2002 if (add->flags & LYS_SET_MAX) {
2003 if (add->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002004 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002005 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01002006 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002007 }
2008 }
fredgan2b11ddb2019-10-21 11:03:39 +08002009 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002010 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002011 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002012 LEVEL++;
2013
Radek Krejci39b7fc22021-02-26 23:29:18 +01002014 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002015 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002016 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002017 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002018 ypr_substmt(ctx, LY_STMT_UNITS, 0, rpl->units, rpl->exts);
2019 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002020 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2021 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002022 if (rpl->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002023 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002024 }
2025 if (rpl->flags & LYS_SET_MAX) {
2026 if (rpl->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002027 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002028 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01002029 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002030 }
2031 }
fredgan2b11ddb2019-10-21 11:03:39 +08002032 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002033 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002034 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002035 LEVEL++;
2036
Radek Krejci39b7fc22021-02-26 23:29:18 +01002037 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, del->exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01002038 ypr_substmt(ctx, LY_STMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002039 LY_ARRAY_FOR(del->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01002040 yprp_restr(ctx, &del->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002041 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002042 LY_ARRAY_FOR(del->uniques, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002043 ypr_substmt(ctx, LY_STMT_UNIQUE, u, del->uniques[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002044 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002045 LY_ARRAY_FOR(del->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002046 ypr_substmt(ctx, LY_STMT_DEFAULT, u, del->dflts[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002047 }
2048 }
2049
2050 LEVEL--;
2051 ypr_close(ctx, 1);
2052 }
2053
2054 LEVEL--;
2055 ypr_close(ctx, 1);
2056}
2057
Michal Vasko7c8439f2020-08-05 13:25:19 +02002058static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01002059yang_print_parsed_linkage(struct lys_ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002060{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002061 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002062
Radek Krejcid3ca0632019-04-16 16:54:54 +02002063 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01002064 if (modp->imports[u].flags & LYS_INTERNAL) {
2065 continue;
2066 }
2067
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002068 YPR_EXTRA_LINE_PRINT(ctx);
2069 ly_print_(ctx->out, "%*simport %s {\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002070 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002071 yprp_extension_instances(ctx, LY_STMT_IMPORT, 0, modp->imports[u].exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01002072 ypr_substmt(ctx, LY_STMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002073 if (modp->imports[u].rev[0]) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002074 ypr_substmt(ctx, LY_STMT_REVISION_DATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002075 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002076 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2077 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002078 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002079 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002080 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002081 YPR_EXTRA_LINE(modp->imports, ctx);
2082
Radek Krejcid3ca0632019-04-16 16:54:54 +02002083 LY_ARRAY_FOR(modp->includes, u) {
Radek Krejci771928a2021-01-19 13:42:36 +01002084 if (modp->includes[u].injected) {
2085 /* do not print the includes injected from submodules */
2086 continue;
2087 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002088 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002089 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002090 ly_print_(ctx->out, "%*sinclude %s {\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002091 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002092 yprp_extension_instances(ctx, LY_STMT_INCLUDE, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002093 if (modp->includes[u].rev[0]) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002094 ypr_substmt(ctx, LY_STMT_REVISION_DATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002095 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002096 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2097 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002098 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002099 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002100 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002101 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002102 }
2103 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002104 YPR_EXTRA_LINE(modp->includes, ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002105}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002106
Michal Vasko7c8439f2020-08-05 13:25:19 +02002107static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01002108yang_print_parsed_body(struct lys_ypr_ctx *ctx, const struct lysp_module *modp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002109{
2110 LY_ARRAY_COUNT_TYPE u;
2111 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002112 struct lysp_node_action *action;
2113 struct lysp_node_notif *notif;
2114 struct lysp_node_grp *grp;
2115 struct lysp_node_augment *aug;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002116
Radek Krejcid3ca0632019-04-16 16:54:54 +02002117 LY_ARRAY_FOR(modp->extensions, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002118 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002119 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002120 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002121
2122 YPR_EXTRA_LINE(modp->extensions, ctx);
2123
Radek Krejcid3ca0632019-04-16 16:54:54 +02002124 if (modp->exts) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002125 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002126 yprp_extension_instances(ctx, LY_STMT_MODULE, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002127 }
2128
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002129 YPR_EXTRA_LINE(modp->exts, ctx);
2130
Radek Krejcid3ca0632019-04-16 16:54:54 +02002131 LY_ARRAY_FOR(modp->features, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002132 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002133 yprp_feature(ctx, &modp->features[u]);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002134 YPR_EXTRA_LINE(1, ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002135 }
2136
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002137 YPR_EXTRA_LINE(modp->features, ctx);
2138
Radek Krejcid3ca0632019-04-16 16:54:54 +02002139 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002140 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002141 yprp_identity(ctx, &modp->identities[u]);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002142 YPR_EXTRA_LINE(1, ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002143 }
2144
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002145 YPR_EXTRA_LINE(modp->identities, ctx);
2146
Radek Krejcid3ca0632019-04-16 16:54:54 +02002147 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002148 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002149 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002150 YPR_EXTRA_LINE(1, ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002151 }
2152
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002153 YPR_EXTRA_LINE(modp->typedefs, ctx);
2154
Radek Krejci2a9fc652021-01-22 17:44:34 +01002155 LY_LIST_FOR(modp->groupings, grp) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002156 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002157 yprp_grouping(ctx, grp);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002158 YPR_EXTRA_LINE(1, ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002159 }
2160
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002161 YPR_EXTRA_LINE(modp->groupings, ctx);
2162
Radek Krejcid3ca0632019-04-16 16:54:54 +02002163 LY_LIST_FOR(modp->data, data) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002164 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002165 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002166 }
2167
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002168 YPR_EXTRA_LINE(modp->data, ctx);
2169
Radek Krejci2a9fc652021-01-22 17:44:34 +01002170 LY_LIST_FOR(modp->augments, aug) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002171 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002172 yprp_augment(ctx, aug);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002173 }
2174
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002175 YPR_EXTRA_LINE(modp->augments, ctx);
2176
Radek Krejci2a9fc652021-01-22 17:44:34 +01002177 LY_LIST_FOR(modp->rpcs, action) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002178 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002179 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002180 }
2181
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002182 YPR_EXTRA_LINE(modp->rpcs, ctx);
2183
Radek Krejci2a9fc652021-01-22 17:44:34 +01002184 LY_LIST_FOR(modp->notifs, notif) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002185 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002186 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002187 }
2188
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002189 YPR_EXTRA_LINE(modp->notifs, ctx);
2190
Radek Krejcid3ca0632019-04-16 16:54:54 +02002191 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002192 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002193 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002194 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002195
2196 YPR_EXTRA_LINE(modp->deviations, ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002197}
2198
2199LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01002200yang_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002201{
2202 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7997d5a2021-02-22 10:55:56 +01002203 const struct lys_module *module = modp->mod;
Radek Krejciadcf63d2021-02-09 10:21:18 +01002204 struct lys_ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = LYS_YPR_PARSED, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002205
Michal Vasko5233e962020-08-14 14:26:20 +02002206 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002207 LEVEL++;
2208
2209 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002210 if (modp->version) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002211 ypr_substmt(ctx, LY_STMT_YANG_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002212 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002213 ypr_substmt(ctx, LY_STMT_NAMESPACE, 0, module->ns, modp->exts);
2214 ypr_substmt(ctx, LY_STMT_PREFIX, 0, module->prefix, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002215
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002216 YPR_EXTRA_LINE(1, ctx);
2217
Michal Vasko7c8439f2020-08-05 13:25:19 +02002218 /* linkage-stmts (import/include) */
2219 yang_print_parsed_linkage(ctx, modp);
2220
2221 /* meta-stmts */
2222 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002223 YPR_EXTRA_LINE_PRINT(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002224 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002225 ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, module->org, modp->exts);
2226 ypr_substmt(ctx, LY_STMT_CONTACT, 0, module->contact, modp->exts);
2227 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, module->dsc, modp->exts);
2228 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, module->ref, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002229
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002230 YPR_EXTRA_LINE(module->org || module->contact || module->dsc || module->ref, ctx);
2231
Michal Vasko7c8439f2020-08-05 13:25:19 +02002232 /* revision-stmts */
Michal Vasko7c8439f2020-08-05 13:25:19 +02002233 LY_ARRAY_FOR(modp->revs, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002234 YPR_EXTRA_LINE_PRINT(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002235 yprp_revision(ctx, &modp->revs[u]);
2236 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002237
2238 YPR_EXTRA_LINE(modp->revs, ctx);
2239
Michal Vasko7c8439f2020-08-05 13:25:19 +02002240 /* body-stmts */
2241 yang_print_parsed_body(ctx, modp);
2242
2243 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002244 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002245 ly_print_flush(out);
2246
2247 return LY_SUCCESS;
2248}
2249
2250static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01002251yprp_belongsto(struct lys_ypr_ctx *ctx, const struct lysp_submodule *submodp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002252{
Michal Vaskoc3781c32020-10-06 14:04:08 +02002253 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->mod->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002254 LEVEL++;
Radek Krejcifc596f92021-02-26 22:40:26 +01002255 yprp_extension_instances(ctx, LY_STMT_BELONGS_TO, 0, submodp->exts, NULL, 0);
2256 ypr_substmt(ctx, LY_STMT_PREFIX, 0, submodp->prefix, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002257 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002258 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002259}
2260
2261LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01002262yang_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002263{
2264 LY_ARRAY_COUNT_TYPE u;
Radek Krejci07a55962021-03-02 20:16:43 +01002265 struct lys_ypr_ctx ctx_ = {
2266 .out = out, .level = 0, .module = submodp->mod, .schema = LYS_YPR_PARSED,
2267 .options = options
2268 }, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002269
Michal Vasko5233e962020-08-14 14:26:20 +02002270 ly_print_(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002271 LEVEL++;
2272
2273 /* submodule-header-stmts */
2274 if (submodp->version) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002275 ypr_substmt(ctx, LY_STMT_YANG_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002276 }
2277
2278 yprp_belongsto(ctx, submodp);
2279
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002280 YPR_EXTRA_LINE(1, ctx);
2281
Michal Vasko7c8439f2020-08-05 13:25:19 +02002282 /* linkage-stmts (import/include) */
2283 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2284
2285 /* meta-stmts */
2286 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002287 YPR_EXTRA_LINE_PRINT(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002288 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002289 ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2290 ypr_substmt(ctx, LY_STMT_CONTACT, 0, submodp->contact, submodp->exts);
2291 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2292 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, submodp->ref, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002293
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002294 YPR_EXTRA_LINE(submodp->org || submodp->contact || submodp->dsc || submodp->ref, ctx);
2295
Michal Vasko7c8439f2020-08-05 13:25:19 +02002296 /* revision-stmts */
Michal Vasko7c8439f2020-08-05 13:25:19 +02002297 LY_ARRAY_FOR(submodp->revs, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002298 YPR_EXTRA_LINE_PRINT(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002299 yprp_revision(ctx, &submodp->revs[u]);
2300 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002301
2302 YPR_EXTRA_LINE(submodp->revs, ctx);
2303
Michal Vasko7c8439f2020-08-05 13:25:19 +02002304 /* body-stmts */
2305 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002306
2307 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002308 ly_print_(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002309 ly_print_flush(out);
2310
2311 return LY_SUCCESS;
2312}
2313
2314LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002315yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002316{
Radek Krejciadcf63d2021-02-09 10:21:18 +01002317 struct lys_ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options}, *ctx = &ctx_;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002318
2319 yprc_node(ctx, node);
2320
2321 ly_print_flush(out);
2322 return LY_SUCCESS;
2323}
2324
2325LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002326yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002327{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002328 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002329 struct lysc_module *modc = module->compiled;
Radek Krejciadcf63d2021-02-09 10:21:18 +01002330 struct lys_ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002331
Michal Vasko5233e962020-08-14 14:26:20 +02002332 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002333 LEVEL++;
2334
Radek Krejci693262f2019-04-29 15:23:20 +02002335 /* module-header-stmts */
Radek Krejcifc596f92021-02-26 22:40:26 +01002336 ypr_substmt(ctx, LY_STMT_NAMESPACE, 0, module->ns, modc->exts);
2337 ypr_substmt(ctx, LY_STMT_PREFIX, 0, module->prefix, modc->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002338
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002339 YPR_EXTRA_LINE(1, ctx);
2340
Michal Vasko7c8439f2020-08-05 13:25:19 +02002341 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002342
2343 /* meta-stmts */
2344 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002345 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002346 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002347 ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, module->org, modc->exts);
2348 ypr_substmt(ctx, LY_STMT_CONTACT, 0, module->contact, modc->exts);
2349 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, module->dsc, modc->exts);
2350 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, module->ref, modc->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002351
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002352 YPR_EXTRA_LINE(module->org || module->contact || module->dsc || module->ref, ctx);
2353
Radek Krejci693262f2019-04-29 15:23:20 +02002354 /* revision-stmts */
2355 if (module->revision) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002356 YPR_EXTRA_LINE_PRINT(ctx);
2357 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, module->revision);
2358 YPR_EXTRA_LINE(1, ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002359 }
2360
2361 /* body-stmts */
2362 if (modc->exts) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002363 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002364 yprc_extension_instances(ctx, LY_STMT_MODULE, 0, module->compiled->exts, NULL, 0);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002365 YPR_EXTRA_LINE(1, ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002366 }
2367
Radek Krejci80d281e2020-09-14 17:42:54 +02002368 LY_ARRAY_FOR(module->identities, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002369 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci80d281e2020-09-14 17:42:54 +02002370 yprc_identity(ctx, &module->identities[u]);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002371 YPR_EXTRA_LINE(1, ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002372 }
2373
Radek Krejci52f65552020-09-01 17:03:35 +02002374 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002375 struct lysc_node *data;
2376 struct lysc_node_action *rpc;
2377 struct lysc_node_notif *notif;
2378
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002379 LY_LIST_FOR(modc->data, data) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002380 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002381 yprc_node(ctx, data);
2382 }
Radek Krejci693262f2019-04-29 15:23:20 +02002383
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002384 YPR_EXTRA_LINE(modc->data, ctx);
2385
Radek Krejci2a9fc652021-01-22 17:44:34 +01002386 LY_LIST_FOR(modc->rpcs, rpc) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002387 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002388 yprc_action(ctx, rpc);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002389 }
Radek Krejci693262f2019-04-29 15:23:20 +02002390
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002391 YPR_EXTRA_LINE(modc->rpcs, ctx);
2392
Radek Krejci2a9fc652021-01-22 17:44:34 +01002393 LY_LIST_FOR(modc->notifs, notif) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002394 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002395 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002396 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002397
2398 YPR_EXTRA_LINE(modc->notifs, ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002399 }
2400
2401 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002402 ly_print_(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002403 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002404
2405 return LY_SUCCESS;
2406}
Radek Krejciadcf63d2021-02-09 10:21:18 +01002407
2408/**
2409 * @param[in] count Number of extensions to print, 0 to print them all.
2410 */
2411static void
Radek Krejcifc596f92021-02-26 22:40:26 +01002412yprc_extension_instances(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index,
Radek Krejciadcf63d2021-02-09 10:21:18 +01002413 struct lysc_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
2414{
2415 LY_ARRAY_COUNT_TYPE u;
2416
2417 if (!count && ext) {
2418 count = LY_ARRAY_COUNT(ext);
2419 }
2420 LY_ARRAY_FOR(ext, u) {
2421 ly_bool inner_flag = 0;
2422
2423 if (!count) {
2424 break;
2425 }
2426
2427 count--;
Radek Krejciab430862021-03-02 20:13:40 +01002428 if ((ext->parent_stmt != substmt) || (ext->parent_stmt_index != substmt_index)) {
Radek Krejciadcf63d2021-02-09 10:21:18 +01002429 continue;
2430 }
2431
2432 ypr_open(ctx->out, flag);
2433 if (ext[u].argument) {
2434 ly_print_(ctx->out, "%*s%s:%s \"", INDENT, ext[u].def->module->name, ext[u].def->name);
2435 ypr_encode(ctx->out, ext[u].argument, -1);
2436 ly_print_(ctx->out, "\"");
2437 } else {
2438 ly_print_(ctx->out, "%*s%s:%s", INDENT, ext[u].def->module->name, ext[u].def->name);
2439 }
2440
2441 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002442 yprc_extension_instances(ctx, LY_STMT_EXTENSION_INSTANCE, 0, ext[u].exts, &inner_flag, 0);
Radek Krejciadcf63d2021-02-09 10:21:18 +01002443
Radek Krejci4a4d1e02021-04-09 14:04:40 +02002444 if (ext[u].def->plugin && ext[u].def->plugin->sprinter) {
Radek Krejcif8d7f9a2021-03-10 14:32:36 +01002445 ext[u].def->plugin->sprinter(&ctx->printer_ctx, &ext[u], &inner_flag);
Radek Krejciadcf63d2021-02-09 10:21:18 +01002446 }
2447
2448 LEVEL--;
2449 ypr_close(ctx, inner_flag);
2450 }
2451}
2452
2453void
Radek Krejcif8d7f9a2021-03-10 14:32:36 +01002454lysc_print_extension_instance(struct lyspr_ctx *ctx_generic, const struct lysc_ext_instance *ext, ly_bool *flag)
Radek Krejciadcf63d2021-02-09 10:21:18 +01002455{
Radek Krejcif8d7f9a2021-03-10 14:32:36 +01002456 struct lys_ypr_ctx *ctx = (struct lys_ypr_ctx *)ctx_generic;
Radek Krejciadcf63d2021-02-09 10:21:18 +01002457 LY_ARRAY_COUNT_TYPE u, v;
2458
2459 LY_ARRAY_FOR(ext->substmts, u) {
2460 switch (ext->substmts[u].stmt) {
2461 case LY_STMT_CHOICE:
2462 case LY_STMT_CONTAINER: {
2463 const struct lysc_node *node;
2464
2465 LY_LIST_FOR(*(const struct lysc_node **)ext->substmts[u].storage, node) {
2466 ypr_open(ctx->out, flag);
2467 yprc_node(ctx, node);
2468 }
2469 break;
2470 }
2471 case LY_STMT_DESCRIPTION:
2472 case LY_STMT_REFERENCE:
2473 case LY_STMT_UNITS:
2474 if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
2475 if (*(const char **)ext->substmts[u].storage) {
2476 ypr_open(ctx->out, flag);
2477 ypr_substmt(ctx, ext->substmts[u].stmt, 0, *(const char **)ext->substmts[u].storage, ext->exts);
2478 }
2479 } else {
2480 const char **strings = *(const char ***)ext->substmts[u].storage;
2481 LY_ARRAY_FOR(strings, v) {
2482 ypr_open(ctx->out, flag);
2483 ypr_substmt(ctx, ext->substmts[u].stmt, v, strings[v], ext->exts);
2484 }
2485 }
2486 break;
2487 case LY_STMT_IF_FEATURE:
2488 /* nothing to do */
2489 break;
2490 case LY_STMT_STATUS:
2491 ypr_status(ctx, *(uint16_t *)ext->substmts[u].storage, ext->exts, flag);
2492 break;
2493 case LY_STMT_TYPE:
2494 if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
2495 if (*(const struct lysc_type **)ext->substmts[u].storage) {
2496 ypr_open(ctx->out, flag);
2497 yprc_type(ctx, *(const struct lysc_type **)ext->substmts[u].storage);
2498 }
2499 } else {
2500 const struct lysc_type **types = *(const struct lysc_type ***)ext->substmts[u].storage;
2501 LY_ARRAY_FOR(types, v) {
2502 ypr_open(ctx->out, flag);
2503 yprc_type(ctx, types[v]);
2504 }
2505 }
2506 break;
2507 /* TODO support other substatements */
2508 default:
2509 LOGWRN(ctx->module->ctx, "Statement \"%s\" is not supported for an extension printer.",
2510 ly_stmt2str(ext->substmts[u].stmt));
2511 break;
2512 }
2513 }
2514}