blob: b46c957c45910dafc17a5680f71731ec04a9d855 [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
554 LY_ARRAY_FOR(ident->derived, u) {
555 ypr_open(ctx->out, &flag);
556 if (ctx->module != ident->derived[u]->module) {
Michal Vasko5233e962020-08-14 14:26:20 +0200557 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 +0200558 } else {
Michal Vasko5233e962020-08-14 14:26:20 +0200559 ly_print_(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
Radek Krejci693262f2019-04-29 15:23:20 +0200560 }
561 }
562
563 ypr_status(ctx, ident->flags, ident->exts, &flag);
564 ypr_description(ctx, ident->dsc, ident->exts, &flag);
565 ypr_reference(ctx, ident->ref, ident->exts, &flag);
566
567 LEVEL--;
568 ypr_close(ctx, flag);
569}
570
571static void
Radek Krejci39b7fc22021-02-26 23:29:18 +0100572yprp_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 +0200573{
Radek Krejci857189e2020-09-01 13:26:36 +0200574 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200575
576 if (!restr) {
577 return;
578 }
579
580 ypr_open(ctx->out, flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +0100581 ly_print_(ctx->out, "%*s%s \"", INDENT, ly_stmt2str(stmt));
Radek Krejcif13b87b2020-12-01 22:02:17 +0100582 ypr_encode(ctx->out,
583 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
584 restr->arg.str : &restr->arg.str[1], -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200585 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200586
587 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100588 yprp_extension_instances(ctx, stmt, 0, restr->exts, &inner_flag, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100589 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200590 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
591 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100592 ypr_substmt(ctx, LY_STMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200593 }
594 if (restr->emsg) {
595 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100596 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200597 }
598 if (restr->eapptag) {
599 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100600 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200601 }
Radek Krejci693262f2019-04-29 15:23:20 +0200602 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
603 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
604
Radek Krejcid3ca0632019-04-16 16:54:54 +0200605 LEVEL--;
606 ypr_close(ctx, inner_flag);
607}
608
609static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100610yprc_must(struct lys_ypr_ctx *ctx, const struct lysc_must *must, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200611{
Radek Krejci857189e2020-09-01 13:26:36 +0200612 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200613
614 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200615 ly_print_(ctx->out, "%*smust \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200616 ypr_encode(ctx->out, must->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200617 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200618
619 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100620 yprc_extension_instances(ctx, LY_STMT_MUST, 0, must->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200621 if (must->emsg) {
622 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100623 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, must->emsg, must->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200624 }
625 if (must->eapptag) {
626 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100627 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, must->eapptag, must->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200628 }
629 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
630 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
631
632 LEVEL--;
633 ypr_close(ctx, inner_flag);
634}
635
636static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100637yprc_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 +0200638{
Radek Krejci857189e2020-09-01 13:26:36 +0200639 ly_bool inner_flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200640 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200641
Radek Krejci334ccc72019-06-12 13:49:29 +0200642 if (!range) {
643 return;
644 }
645
Radek Krejci693262f2019-04-29 15:23:20 +0200646 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200647 ly_print_(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200648 LY_ARRAY_FOR(range->parts, u) {
649 if (u > 0) {
Michal Vasko5233e962020-08-14 14:26:20 +0200650 ly_print_(ctx->out, " | ");
Radek Krejci693262f2019-04-29 15:23:20 +0200651 }
652 if (range->parts[u].max_64 == range->parts[u].min_64) {
653 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200654 ly_print_(ctx->out, "%" PRIu64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200655 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200656 ly_print_(ctx->out, "%" PRId64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200657 }
658 } else {
659 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
Radek Krejci0f969882020-08-21 16:56:47 +0200660 ly_print_(ctx->out, "%" PRIu64 "..%" PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
Radek Krejci693262f2019-04-29 15:23:20 +0200661 } else { /* signed values */
Radek Krejci0f969882020-08-21 16:56:47 +0200662 ly_print_(ctx->out, "%" PRId64 "..%" PRId64, range->parts[u].min_64, range->parts[u].max_64);
Radek Krejci693262f2019-04-29 15:23:20 +0200663 }
664 }
665 }
Michal Vasko5233e962020-08-14 14:26:20 +0200666 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200667
668 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100669 yprc_extension_instances(ctx, LY_STMT_RANGE, 0, range->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200670 if (range->emsg) {
671 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100672 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, range->emsg, range->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200673 }
674 if (range->eapptag) {
675 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100676 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, range->eapptag, range->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200677 }
678 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
679 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
680
681 LEVEL--;
682 ypr_close(ctx, inner_flag);
683}
684
685static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100686yprc_pattern(struct lys_ypr_ctx *ctx, const struct lysc_pattern *pattern, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200687{
Radek Krejci857189e2020-09-01 13:26:36 +0200688 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200689
690 ypr_open(ctx->out, flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200691 ly_print_(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200692 ypr_encode(ctx->out, pattern->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200693 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200694
695 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100696 yprc_extension_instances(ctx, LY_STMT_PATTERN, 0, pattern->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200697 if (pattern->inverted) {
698 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
699 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100700 ypr_substmt(ctx, LY_STMT_MODIFIER, 0, "invert-match", pattern->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200701 }
702 if (pattern->emsg) {
703 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100704 ypr_substmt(ctx, LY_STMT_ERROR_MESSAGE, 0, pattern->emsg, pattern->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200705 }
706 if (pattern->eapptag) {
707 ypr_open(ctx->out, &inner_flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100708 ypr_substmt(ctx, LY_STMT_ERROR_APP_TAG, 0, pattern->eapptag, pattern->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200709 }
710 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
711 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
712
713 LEVEL--;
714 ypr_close(ctx, inner_flag);
715}
716
717static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100718yprp_when(struct lys_ypr_ctx *ctx, struct lysp_when *when, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200719{
Radek Krejci857189e2020-09-01 13:26:36 +0200720 ly_bool inner_flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200721
722 if (!when) {
723 return;
724 }
725 ypr_open(ctx->out, flag);
726
Michal Vasko5233e962020-08-14 14:26:20 +0200727 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200728 ypr_encode(ctx->out, when->cond, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200729 ly_print_(ctx->out, "\"");
Radek Krejcid3ca0632019-04-16 16:54:54 +0200730
731 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100732 yprp_extension_instances(ctx, LY_STMT_WHEN, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200733 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
734 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
735 LEVEL--;
736 ypr_close(ctx, inner_flag);
737}
738
739static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100740yprc_when(struct lys_ypr_ctx *ctx, struct lysc_when *when, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +0200741{
Radek Krejci857189e2020-09-01 13:26:36 +0200742 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200743
744 if (!when) {
745 return;
746 }
747 ypr_open(ctx->out, flag);
748
Michal Vasko5233e962020-08-14 14:26:20 +0200749 ly_print_(ctx->out, "%*swhen \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200750 ypr_encode(ctx->out, when->cond->expr, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200751 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200752
753 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100754 yprc_extension_instances(ctx, LY_STMT_WHEN, 0, when->exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200755 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
756 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
757 LEVEL--;
758 ypr_close(ctx, inner_flag);
759}
760
761static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100762yprp_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 +0200763{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200764 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200765 ly_bool inner_flag;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200766
767 LY_ARRAY_FOR(items, u) {
768 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200769 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200770 ly_print_(ctx->out, "%*sbit %s", INDENT, items[u].name);
Radek Krejci7871ce52019-06-11 16:44:56 +0200771 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200772 ly_print_(ctx->out, "%*senum \"", INDENT);
Radek Krejci7871ce52019-06-11 16:44:56 +0200773 ypr_encode(ctx->out, items[u].name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200774 ly_print_(ctx->out, "\"");
Radek Krejci7871ce52019-06-11 16:44:56 +0200775 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200776 inner_flag = 0;
777 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +0100778 yprp_extension_instances(ctx, LY_STMT_ENUM, 0, items[u].exts, &inner_flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200779 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200780 if (items[u].flags & LYS_SET_VALUE) {
781 if (type == LY_TYPE_BITS) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100782 ypr_unsigned(ctx, LY_STMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200783 } else { /* LY_TYPE_ENUM */
Radek Krejcifc596f92021-02-26 22:40:26 +0100784 ypr_signed(ctx, LY_STMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200785 }
786 }
Radek Krejci693262f2019-04-29 15:23:20 +0200787 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200788 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
789 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
790 LEVEL--;
791 ypr_close(ctx, inner_flag);
792 }
793}
794
795static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100796yprp_type(struct lys_ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200797{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200798 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200799 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200800
Michal Vasko5233e962020-08-14 14:26:20 +0200801 ly_print_(ctx->out, "%*stype %s", INDENT, type->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200802 LEVEL++;
803
Radek Krejci39b7fc22021-02-26 23:29:18 +0100804 yprp_extension_instances(ctx, LY_STMT_TYPE, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200805
Radek Krejci39b7fc22021-02-26 23:29:18 +0100806 yprp_restr(ctx, type->range, LY_STMT_RANGE, &flag);
807 yprp_restr(ctx, type->length, LY_STMT_LENGTH, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200808 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100809 yprp_restr(ctx, &type->patterns[u], LY_STMT_PATTERN, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200810 }
Radek Krejci693262f2019-04-29 15:23:20 +0200811 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
812 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200813
814 if (type->path) {
815 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100816 ypr_substmt(ctx, LY_STMT_PATH, 0, type->path->expr, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200817 }
818 if (type->flags & LYS_SET_REQINST) {
819 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100820 ypr_substmt(ctx, LY_STMT_REQUIRE_INSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200821 }
822 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100823 ypr_unsigned(ctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200824 }
825 LY_ARRAY_FOR(type->bases, u) {
826 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100827 ypr_substmt(ctx, LY_STMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200828 }
829 LY_ARRAY_FOR(type->types, u) {
830 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200831 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200832 }
833
834 LEVEL--;
835 ypr_close(ctx, flag);
836}
837
838static void
Radek Krejci224d4b42021-04-23 13:54:59 +0200839yprc_dflt_value(struct lys_ypr_ctx *ctx, const struct ly_ctx *ly_ctx, const struct lyd_value *value,
840 struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200841{
Radek Krejci857189e2020-09-01 13:26:36 +0200842 ly_bool dynamic;
Radek Krejcia1911222019-07-22 17:24:50 +0200843 const char *str;
844
Radek Krejci224d4b42021-04-23 13:54:59 +0200845 str = value->realtype->plugin->print(ly_ctx, value, LY_VALUE_JSON, NULL, &dynamic, NULL);
Radek Krejcifc596f92021-02-26 22:40:26 +0100846 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, str, exts);
Radek Krejcia1911222019-07-22 17:24:50 +0200847 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200848 free((void *)str);
Radek Krejcia1911222019-07-22 17:24:50 +0200849 }
850}
851
852static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100853yprc_type(struct lys_ypr_ctx *ctx, const struct lysc_type *type)
Radek Krejci693262f2019-04-29 15:23:20 +0200854{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200855 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +0200856 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200857
Michal Vasko5233e962020-08-14 14:26:20 +0200858 ly_print_(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
Radek Krejci693262f2019-04-29 15:23:20 +0200859 LEVEL++;
860
Radek Krejci39b7fc22021-02-26 23:29:18 +0100861 yprc_extension_instances(ctx, LY_STMT_TYPE, 0, type->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +0200862
Michal Vasko2bb55bc2020-08-05 13:27:04 +0200863 switch (type->basetype) {
Radek Krejci693262f2019-04-29 15:23:20 +0200864 case LY_TYPE_BINARY: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200865 struct lysc_type_bin *bin = (struct lysc_type_bin *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200866 yprc_range(ctx, bin->length, type->basetype, &flag);
867 break;
868 }
869 case LY_TYPE_UINT8:
870 case LY_TYPE_UINT16:
871 case LY_TYPE_UINT32:
872 case LY_TYPE_UINT64:
873 case LY_TYPE_INT8:
874 case LY_TYPE_INT16:
875 case LY_TYPE_INT32:
876 case LY_TYPE_INT64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200877 struct lysc_type_num *num = (struct lysc_type_num *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200878 yprc_range(ctx, num->range, type->basetype, &flag);
879 break;
880 }
881 case LY_TYPE_STRING: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200882 struct lysc_type_str *str = (struct lysc_type_str *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200883 yprc_range(ctx, str->length, type->basetype, &flag);
884 LY_ARRAY_FOR(str->patterns, u) {
885 yprc_pattern(ctx, str->patterns[u], &flag);
886 }
887 break;
888 }
889 case LY_TYPE_BITS:
890 case LY_TYPE_ENUM: {
891 /* bits and enums structures are compatible */
Michal Vasko22df3f02020-08-24 13:29:22 +0200892 struct lysc_type_bits *bits = (struct lysc_type_bits *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200893 LY_ARRAY_FOR(bits->bits, u) {
894 struct lysc_type_bitenum_item *item = &bits->bits[u];
Radek Krejci857189e2020-09-01 13:26:36 +0200895 ly_bool inner_flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200896
897 ypr_open(ctx->out, &flag);
Michal Vasko5233e962020-08-14 14:26:20 +0200898 ly_print_(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
Radek Krejci693262f2019-04-29 15:23:20 +0200899 ypr_encode(ctx->out, item->name, -1);
Michal Vasko5233e962020-08-14 14:26:20 +0200900 ly_print_(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200901 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200902 if (type->basetype == LY_TYPE_BITS) {
Radek Krejci39b7fc22021-02-26 23:29:18 +0100903 yprc_extension_instances(ctx, LY_STMT_BIT, 0, item->exts, &inner_flag, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +0100904 ypr_unsigned(ctx, LY_STMT_POSITION, 0, item->exts, item->position, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200905 } else { /* LY_TYPE_ENUM */
Radek Krejci39b7fc22021-02-26 23:29:18 +0100906 yprc_extension_instances(ctx, LY_STMT_ENUM, 0, item->exts, &inner_flag, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +0100907 ypr_signed(ctx, LY_STMT_VALUE, 0, item->exts, item->value, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200908 }
909 ypr_status(ctx, item->flags, item->exts, &inner_flag);
910 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
911 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
912 LEVEL--;
913 ypr_close(ctx, inner_flag);
914 }
915 break;
916 }
917 case LY_TYPE_BOOL:
918 case LY_TYPE_EMPTY:
919 /* nothing to do */
920 break;
921 case LY_TYPE_DEC64: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200922 struct lysc_type_dec *dec = (struct lysc_type_dec *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200923 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100924 ypr_unsigned(ctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, dec->fraction_digits, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200925 yprc_range(ctx, dec->range, dec->basetype, &flag);
926 break;
927 }
928 case LY_TYPE_IDENT: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200929 struct lysc_type_identityref *ident = (struct lysc_type_identityref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200930 LY_ARRAY_FOR(ident->bases, u) {
931 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100932 ypr_substmt(ctx, LY_STMT_BASE, u, ident->bases[u]->name, type->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200933 }
934 break;
935 }
936 case LY_TYPE_INST: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200937 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200938 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100939 ypr_substmt(ctx, LY_STMT_REQUIRE_INSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200940 break;
941 }
942 case LY_TYPE_LEAFREF: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200943 struct lysc_type_leafref *lr = (struct lysc_type_leafref *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200944 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +0100945 ypr_substmt(ctx, LY_STMT_PATH, 0, lr->path->expr, lr->exts);
946 ypr_substmt(ctx, LY_STMT_REQUIRE_INSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200947 yprc_type(ctx, lr->realtype);
948 break;
949 }
950 case LY_TYPE_UNION: {
Michal Vasko22df3f02020-08-24 13:29:22 +0200951 struct lysc_type_union *un = (struct lysc_type_union *)type;
Radek Krejci693262f2019-04-29 15:23:20 +0200952 LY_ARRAY_FOR(un->types, u) {
953 ypr_open(ctx->out, &flag);
954 yprc_type(ctx, un->types[u]);
955 }
956 break;
957 }
958 default:
959 LOGINT(ctx->module->ctx);
960 }
961
962 LEVEL--;
963 ypr_close(ctx, flag);
964}
965
966static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100967yprp_typedef(struct lys_ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200968{
Radek Krejciaa14a0c2020-09-04 11:16:47 +0200969 ly_print_(ctx->out, "%*stypedef %s {\n", INDENT, tpdf->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200970 LEVEL++;
971
Radek Krejci39b7fc22021-02-26 23:29:18 +0100972 yprp_extension_instances(ctx, LY_STMT_TYPEDEF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200973
Radek Krejci693262f2019-04-29 15:23:20 +0200974 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200975
976 if (tpdf->units) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100977 ypr_substmt(ctx, LY_STMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200978 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200979 if (tpdf->dflt.str) {
Radek Krejcifc596f92021-02-26 22:40:26 +0100980 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200981 }
982
Radek Krejci693262f2019-04-29 15:23:20 +0200983 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200984 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
985 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
986
987 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200988 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200989}
990
Radek Krejciadcf63d2021-02-09 10:21:18 +0100991static void yprp_node(struct lys_ypr_ctx *ctx, const struct lysp_node *node);
992static void yprc_node(struct lys_ypr_ctx *ctx, const struct lysc_node *node);
993static void yprp_action(struct lys_ypr_ctx *ctx, const struct lysp_node_action *action);
994static void yprp_notification(struct lys_ypr_ctx *ctx, const struct lysp_node_notif *notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200995
996static void
Radek Krejciadcf63d2021-02-09 10:21:18 +0100997yprp_grouping(struct lys_ypr_ctx *ctx, const struct lysp_node_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200998{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200999 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001000 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001001 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001002 struct lysp_node_action *action;
1003 struct lysp_node_notif *notif;
1004 struct lysp_node_grp *subgrp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001005
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001006 ly_print_(ctx->out, "%*sgrouping %s", INDENT, grp->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001007 LEVEL++;
1008
Radek Krejci39b7fc22021-02-26 23:29:18 +01001009 yprp_extension_instances(ctx, LY_STMT_GROUPING, 0, grp->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001010 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001011 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1012 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001013
1014 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001015 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001016 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001017 }
1018
Radek Krejci2a9fc652021-01-22 17:44:34 +01001019 LY_LIST_FOR(grp->groupings, subgrp) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001020 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001021 yprp_grouping(ctx, subgrp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001022 }
1023
Radek Krejci01180ac2021-01-27 08:48:22 +01001024 LY_LIST_FOR(grp->child, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001025 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001026 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001027 }
1028
Radek Krejci2a9fc652021-01-22 17:44:34 +01001029 LY_LIST_FOR(grp->actions, action) {
1030 ypr_open(ctx->out, &flag);
1031 yprp_action(ctx, action);
1032 }
1033
1034 LY_LIST_FOR(grp->notifs, notif) {
1035 ypr_open(ctx->out, &flag);
1036 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001037 }
1038
1039 LEVEL--;
1040 ypr_close(ctx, flag);
1041}
1042
1043static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001044yprp_inout(struct lys_ypr_ctx *ctx, const struct lysp_node_action_inout *inout, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001045{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001046 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001047 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001048 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001049
Radek Krejci01180ac2021-01-27 08:48:22 +01001050 if (!inout->child) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02001051 /* no children */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001052 return;
1053 }
1054 ypr_open(ctx->out, flag);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001055 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001056
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001057 ly_print_(ctx->out, "%*s%s {\n", INDENT, inout->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001058 LEVEL++;
1059
Radek Krejci39b7fc22021-02-26 23:29:18 +01001060 yprp_extension_instances(ctx, LY_STMT_MUST, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001061 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001062 yprp_restr(ctx, &inout->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001063 }
1064 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001065 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001066 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001067 LY_LIST_FOR(inout->groupings, grp) {
1068 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001069 }
1070
Radek Krejci01180ac2021-01-27 08:48:22 +01001071 LY_LIST_FOR(inout->child, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001072 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001073 }
1074
1075 LEVEL--;
1076 ypr_close(ctx, 1);
1077}
1078
1079static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001080yprc_inout(struct lys_ypr_ctx *ctx, const struct lysc_node_action_inout *inout, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001081{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001082 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001083 struct lysc_node *data;
1084
Radek Krejci01180ac2021-01-27 08:48:22 +01001085 if (!inout->child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001086 /* input/output is empty */
1087 return;
1088 }
1089 ypr_open(ctx->out, flag);
1090
Michal Vasko544e58a2021-01-28 14:33:41 +01001091 ly_print_(ctx->out, "\n%*s%s {\n", INDENT, inout->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001092 LEVEL++;
1093
Radek Krejci39b7fc22021-02-26 23:29:18 +01001094 yprc_extension_instances(ctx, lys_nodetype2stmt(inout->nodetype), 0, inout->exts, NULL, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001095 LY_ARRAY_FOR(inout->musts, u) {
1096 yprc_must(ctx, &inout->musts[u], NULL);
1097 }
1098
Radek Krejci52f65552020-09-01 17:03:35 +02001099 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci01180ac2021-01-27 08:48:22 +01001100 LY_LIST_FOR(inout->child, data) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001101 yprc_node(ctx, data);
1102 }
Radek Krejci693262f2019-04-29 15:23:20 +02001103 }
1104
1105 LEVEL--;
1106 ypr_close(ctx, 1);
1107}
1108
1109static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001110yprp_notification(struct lys_ypr_ctx *ctx, const struct lysp_node_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001111{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001112 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001113 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001114 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001115 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001116
Michal Vasko5233e962020-08-14 14:26:20 +02001117 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001118
1119 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001120 yprp_extension_instances(ctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001121 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001122
1123 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001124 yprp_restr(ctx, &notif->musts[u], LY_STMT_MUST, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001125 }
Radek Krejci693262f2019-04-29 15:23:20 +02001126 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001127 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1128 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1129
1130 LY_ARRAY_FOR(notif->typedefs, u) {
1131 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001132 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001133 }
1134
Radek Krejci2a9fc652021-01-22 17:44:34 +01001135 LY_LIST_FOR(notif->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001136 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001137 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001138 }
1139
Radek Krejci01180ac2021-01-27 08:48:22 +01001140 LY_LIST_FOR(notif->child, data) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001141 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001142 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001143 }
1144
1145 LEVEL--;
1146 ypr_close(ctx, flag);
1147}
1148
1149static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001150yprc_notification(struct lys_ypr_ctx *ctx, const struct lysc_node_notif *notif)
Radek Krejci693262f2019-04-29 15:23:20 +02001151{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001152 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001153 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001154 struct lysc_node *data;
1155
Michal Vasko5233e962020-08-14 14:26:20 +02001156 ly_print_(ctx->out, "%*snotification %s", INDENT, notif->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001157
1158 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001159 yprc_extension_instances(ctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001160
1161 LY_ARRAY_FOR(notif->musts, u) {
1162 yprc_must(ctx, &notif->musts[u], &flag);
1163 }
1164 ypr_status(ctx, notif->flags, notif->exts, &flag);
1165 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1166 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1167
Radek Krejci52f65552020-09-01 17:03:35 +02001168 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci01180ac2021-01-27 08:48:22 +01001169 LY_LIST_FOR(notif->child, data) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001170 ypr_open(ctx->out, &flag);
1171 yprc_node(ctx, data);
1172 }
Radek Krejci693262f2019-04-29 15:23:20 +02001173 }
1174
1175 LEVEL--;
1176 ypr_close(ctx, flag);
1177}
1178
1179static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001180yprp_action(struct lys_ypr_ctx *ctx, const struct lysp_node_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001181{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001182 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001183 ly_bool flag = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001184 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001185
Michal Vasko5233e962020-08-14 14:26:20 +02001186 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001187
1188 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001189 yprp_extension_instances(ctx, lys_nodetype2stmt(action->nodetype), 0, action->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001190 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1191 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001192 ypr_description(ctx, action->dsc, action->exts, &flag);
1193 ypr_reference(ctx, action->ref, action->exts, &flag);
1194
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001195 YPR_EXTRA_LINE(flag, ctx);
1196
Radek Krejcid3ca0632019-04-16 16:54:54 +02001197 LY_ARRAY_FOR(action->typedefs, u) {
1198 ypr_open(ctx->out, &flag);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001199 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02001200 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001201 }
1202
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001203 YPR_EXTRA_LINE(action->typedefs, ctx);
1204
Radek Krejci2a9fc652021-01-22 17:44:34 +01001205 LY_LIST_FOR(action->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001206 ypr_open(ctx->out, &flag);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001207 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001208 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001209 }
1210
Radek Krejciaa14a0c2020-09-04 11:16:47 +02001211 YPR_EXTRA_LINE(action->groupings, ctx);
1212
Radek Krejci693262f2019-04-29 15:23:20 +02001213 yprp_inout(ctx, &action->input, &flag);
1214 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001215
1216 LEVEL--;
1217 ypr_close(ctx, flag);
1218}
1219
1220static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001221yprc_action(struct lys_ypr_ctx *ctx, const struct lysc_node_action *action)
Radek Krejci693262f2019-04-29 15:23:20 +02001222{
Radek Krejci857189e2020-09-01 13:26:36 +02001223 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001224
Michal Vasko5233e962020-08-14 14:26:20 +02001225 ly_print_(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001226
1227 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01001228 yprc_extension_instances(ctx, lys_nodetype2stmt(action->nodetype), 0, action->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001229 ypr_status(ctx, action->flags, action->exts, &flag);
1230 ypr_description(ctx, action->dsc, action->exts, &flag);
1231 ypr_reference(ctx, action->ref, action->exts, &flag);
1232
Michal Vasko544e58a2021-01-28 14:33:41 +01001233 yprc_inout(ctx, &action->input, &flag);
1234 yprc_inout(ctx, &action->output, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001235
1236 LEVEL--;
1237 ypr_close(ctx, flag);
1238}
1239
1240static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001241yprp_node_common1(struct lys_ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001242{
Michal Vasko5233e962020-08-14 14:26:20 +02001243 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001244 LEVEL++;
1245
Radek Krejci39b7fc22021-02-26 23:29:18 +01001246 yprp_extension_instances(ctx, lys_nodetype2stmt(node->nodetype), 0, node->exts, flag, 0);
Radek Krejci9a3823e2021-01-27 20:26:46 +01001247 yprp_when(ctx, lysp_node_when(node), flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001248 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001249}
1250
1251static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001252yprc_node_common1(struct lys_ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001253{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001254 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9a3823e2021-01-27 20:26:46 +01001255 struct lysc_when **when;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001256
Michal Vasko5233e962020-08-14 14:26:20 +02001257 ly_print_(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001258 LEVEL++;
1259
Radek Krejci39b7fc22021-02-26 23:29:18 +01001260 yprc_extension_instances(ctx, lys_nodetype2stmt(node->nodetype), 0, node->exts, flag, 0);
Radek Krejci9a3823e2021-01-27 20:26:46 +01001261
1262 when = lysc_node_when(node);
1263 LY_ARRAY_FOR(when, u) {
1264 yprc_when(ctx, when[u], flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001265 }
Radek Krejci693262f2019-04-29 15:23:20 +02001266}
1267
1268/* macr oto unify the code */
1269#define YPR_NODE_COMMON2 \
1270 ypr_config(ctx, node->flags, node->exts, flag); \
1271 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1272 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1273 } \
1274 ypr_status(ctx, node->flags, node->exts, flag); \
1275 ypr_description(ctx, node->dsc, node->exts, flag); \
1276 ypr_reference(ctx, node->ref, node->exts, flag)
1277
1278static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001279yprp_node_common2(struct lys_ypr_ctx *ctx, const struct lysp_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001280{
1281 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001282}
1283
1284static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001285yprc_node_common2(struct lys_ypr_ctx *ctx, const struct lysc_node *node, ly_bool *flag)
Radek Krejci693262f2019-04-29 15:23:20 +02001286{
1287 YPR_NODE_COMMON2;
1288}
1289
1290#undef YPR_NODE_COMMON2
1291
1292static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001293yprp_container(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001294{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001295 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001296 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001297 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001298 struct lysp_node_action *action;
1299 struct lysp_node_notif *notif;
1300 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001301 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1302
Radek Krejci693262f2019-04-29 15:23:20 +02001303 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001304
1305 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001306 yprp_restr(ctx, &cont->musts[u], LY_STMT_MUST, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001307 }
1308 if (cont->presence) {
1309 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001310 ypr_substmt(ctx, LY_STMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001311 }
1312
Radek Krejci693262f2019-04-29 15:23:20 +02001313 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001314
1315 LY_ARRAY_FOR(cont->typedefs, u) {
1316 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001317 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001318 }
1319
Radek Krejci2a9fc652021-01-22 17:44:34 +01001320 LY_LIST_FOR(cont->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001321 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001322 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001323 }
1324
1325 LY_LIST_FOR(cont->child, child) {
1326 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001327 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001328 }
1329
Radek Krejci2a9fc652021-01-22 17:44:34 +01001330 LY_LIST_FOR(cont->actions, action) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001331 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001332 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001333 }
1334
Radek Krejci2a9fc652021-01-22 17:44:34 +01001335 LY_LIST_FOR(cont->notifs, notif) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001336 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001337 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001338 }
1339
1340 LEVEL--;
1341 ypr_close(ctx, flag);
1342}
1343
1344static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001345yprc_container(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001346{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001347 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001348 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001349 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001350 struct lysc_node_action *action;
1351 struct lysc_node_notif *notif;
Radek Krejci693262f2019-04-29 15:23:20 +02001352 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1353
1354 yprc_node_common1(ctx, node, &flag);
1355
1356 LY_ARRAY_FOR(cont->musts, u) {
1357 yprc_must(ctx, &cont->musts[u], &flag);
1358 }
1359 if (cont->flags & LYS_PRESENCE) {
1360 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001361 ypr_substmt(ctx, LY_STMT_PRESENCE, 0, "true", cont->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001362 }
1363
1364 yprc_node_common2(ctx, node, &flag);
1365
Radek Krejci52f65552020-09-01 17:03:35 +02001366 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001367 LY_LIST_FOR(cont->child, child) {
1368 ypr_open(ctx->out, &flag);
1369 yprc_node(ctx, child);
1370 }
Radek Krejci693262f2019-04-29 15:23:20 +02001371
Radek Krejci2a9fc652021-01-22 17:44:34 +01001372 LY_LIST_FOR(cont->actions, action) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001373 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001374 yprc_action(ctx, action);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001375 }
Radek Krejci693262f2019-04-29 15:23:20 +02001376
Radek Krejci2a9fc652021-01-22 17:44:34 +01001377 LY_LIST_FOR(cont->notifs, notif) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001378 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001379 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001380 }
Radek Krejci693262f2019-04-29 15:23:20 +02001381 }
1382
1383 LEVEL--;
1384 ypr_close(ctx, flag);
1385}
1386
1387static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001388yprp_case(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001389{
Radek Krejci857189e2020-09-01 13:26:36 +02001390 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001391 struct lysp_node *child;
1392 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1393
1394 yprp_node_common1(ctx, node, &flag);
1395 yprp_node_common2(ctx, node, &flag);
1396
1397 LY_LIST_FOR(cas->child, child) {
1398 ypr_open(ctx->out, &flag);
1399 yprp_node(ctx, child);
1400 }
1401
1402 LEVEL--;
1403 ypr_close(ctx, flag);
1404}
1405
1406static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001407yprc_case(struct lys_ypr_ctx *ctx, const struct lysc_node_case *cs)
Radek Krejci693262f2019-04-29 15:23:20 +02001408{
Radek Krejci857189e2020-09-01 13:26:36 +02001409 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001410 struct lysc_node *child;
1411
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001412 yprc_node_common1(ctx, &cs->node, &flag);
1413 yprc_node_common2(ctx, &cs->node, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001414
Radek Krejci52f65552020-09-01 17:03:35 +02001415 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001416 for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001417 ypr_open(ctx->out, &flag);
1418 yprc_node(ctx, child);
1419 }
Radek Krejci693262f2019-04-29 15:23:20 +02001420 }
1421
1422 LEVEL--;
1423 ypr_close(ctx, flag);
1424}
1425
1426static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001427yprp_choice(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001428{
Radek Krejci857189e2020-09-01 13:26:36 +02001429 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001430 struct lysp_node *child;
1431 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1432
Radek Krejci693262f2019-04-29 15:23:20 +02001433 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001434
Michal Vasko7f45cf22020-10-01 12:49:44 +02001435 if (choice->dflt.str) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001436 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001437 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, choice->dflt.str, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001438 }
1439
Radek Krejci693262f2019-04-29 15:23:20 +02001440 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001441
1442 LY_LIST_FOR(choice->child, child) {
1443 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001444 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001445 }
1446
1447 LEVEL--;
1448 ypr_close(ctx, flag);
1449}
1450
1451static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001452yprc_choice(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001453{
Radek Krejci857189e2020-09-01 13:26:36 +02001454 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001455 struct lysc_node_case *cs;
1456 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1457
1458 yprc_node_common1(ctx, node, &flag);
1459
1460 if (choice->dflt) {
1461 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001462 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, choice->dflt->name, choice->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001463 }
1464
1465 yprc_node_common2(ctx, node, &flag);
1466
Michal Vasko22df3f02020-08-24 13:29:22 +02001467 for (cs = choice->cases; cs; cs = (struct lysc_node_case *)cs->next) {
Radek Krejci693262f2019-04-29 15:23:20 +02001468 ypr_open(ctx->out, &flag);
1469 yprc_case(ctx, cs);
1470 }
1471
1472 LEVEL--;
1473 ypr_close(ctx, flag);
1474}
1475
1476static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001477yprp_leaf(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001478{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001479 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001480 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1481
Radek Krejci693262f2019-04-29 15:23:20 +02001482 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001483
Radek Krejci693262f2019-04-29 15:23:20 +02001484 yprp_type(ctx, &leaf->type);
Radek Krejcifc596f92021-02-26 22:40:26 +01001485 ypr_substmt(ctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001486 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001487 yprp_restr(ctx, &leaf->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001488 }
Radek Krejcifc596f92021-02-26 22:40:26 +01001489 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001490
Radek Krejci693262f2019-04-29 15:23:20 +02001491 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001492
1493 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001494 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001495}
1496
1497static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001498yprc_leaf(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001499{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001500 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001501 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1502
1503 yprc_node_common1(ctx, node, NULL);
1504
1505 yprc_type(ctx, leaf->type);
Radek Krejcifc596f92021-02-26 22:40:26 +01001506 ypr_substmt(ctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001507 LY_ARRAY_FOR(leaf->musts, u) {
1508 yprc_must(ctx, &leaf->musts[u], NULL);
1509 }
Radek Krejcia1911222019-07-22 17:24:50 +02001510
1511 if (leaf->dflt) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001512 yprc_dflt_value(ctx, node->module->ctx, leaf->dflt, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001513 }
Radek Krejci693262f2019-04-29 15:23:20 +02001514
1515 yprc_node_common2(ctx, node, NULL);
1516
1517 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001518 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001519}
1520
1521static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001522yprp_leaflist(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001523{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001524 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001525 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1526
Radek Krejci693262f2019-04-29 15:23:20 +02001527 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001528
Radek Krejci693262f2019-04-29 15:23:20 +02001529 yprp_type(ctx, &llist->type);
Radek Krejcifc596f92021-02-26 22:40:26 +01001530 ypr_substmt(ctx, LY_STMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001531 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001532 yprp_restr(ctx, &llist->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001533 }
1534 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001535 ypr_substmt(ctx, LY_STMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001536 }
1537
Radek Krejci693262f2019-04-29 15:23:20 +02001538 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001539
1540 if (llist->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001541 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001542 }
1543 if (llist->flags & LYS_SET_MAX) {
1544 if (llist->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001545 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001546 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001547 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001548 }
1549 }
1550
1551 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001552 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001553 }
1554
Radek Krejci693262f2019-04-29 15:23:20 +02001555 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001556 ypr_description(ctx, node->dsc, node->exts, NULL);
1557 ypr_reference(ctx, node->ref, node->exts, NULL);
1558
1559 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001560 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001561}
1562
1563static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001564yprc_leaflist(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001565{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001566 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001567 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1568
1569 yprc_node_common1(ctx, node, NULL);
1570
1571 yprc_type(ctx, llist->type);
Radek Krejcifc596f92021-02-26 22:40:26 +01001572 ypr_substmt(ctx, LY_STMT_UNITS, 0, llist->units, llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001573 LY_ARRAY_FOR(llist->musts, u) {
1574 yprc_must(ctx, &llist->musts[u], NULL);
1575 }
1576 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001577 yprc_dflt_value(ctx, node->module->ctx, llist->dflts[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001578 }
1579
1580 ypr_config(ctx, node->flags, node->exts, NULL);
1581
Radek Krejcifc596f92021-02-26 22:40:26 +01001582 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001583 if (llist->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001584 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001585 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001586 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001587 }
1588
Radek Krejcifc596f92021-02-26 22:40:26 +01001589 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001590
1591 ypr_status(ctx, node->flags, node->exts, NULL);
1592 ypr_description(ctx, node->dsc, node->exts, NULL);
1593 ypr_reference(ctx, node->ref, node->exts, NULL);
1594
1595 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001596 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001597}
1598
1599static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001600yprp_list(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001601{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001602 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001603 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001604 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001605 struct lysp_node_action *action;
1606 struct lysp_node_notif *notif;
1607 struct lysp_node_grp *grp;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001608 struct lysp_node_list *list = (struct lysp_node_list *)node;
1609
Radek Krejci693262f2019-04-29 15:23:20 +02001610 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001611
1612 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001613 yprp_restr(ctx, &list->musts[u], LY_STMT_MUST, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001614 }
1615 if (list->key) {
1616 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001617 ypr_substmt(ctx, LY_STMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001618 }
1619 LY_ARRAY_FOR(list->uniques, u) {
1620 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001621 ypr_substmt(ctx, LY_STMT_UNIQUE, u, list->uniques[u].str, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001622 }
1623
Michal Vaskoacb8e742020-10-20 10:28:02 +02001624 ypr_config(ctx, node->flags, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001625
1626 if (list->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001627 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001628 }
1629 if (list->flags & LYS_SET_MAX) {
1630 if (list->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001631 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001632 } else {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001633 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001634 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001635 }
1636 }
1637
1638 if (list->flags & LYS_ORDBY_MASK) {
Michal Vaskoacb8e742020-10-20 10:28:02 +02001639 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001640 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001641 }
1642
Michal Vaskoacb8e742020-10-20 10:28:02 +02001643 ypr_status(ctx, node->flags, node->exts, &flag);
1644 ypr_description(ctx, node->dsc, node->exts, &flag);
1645 ypr_reference(ctx, node->ref, node->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001646
1647 LY_ARRAY_FOR(list->typedefs, u) {
1648 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001649 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001650 }
1651
Radek Krejci2a9fc652021-01-22 17:44:34 +01001652 LY_LIST_FOR(list->groupings, grp) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001653 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001654 yprp_grouping(ctx, grp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001655 }
1656
1657 LY_LIST_FOR(list->child, child) {
1658 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001659 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001660 }
1661
Radek Krejci2a9fc652021-01-22 17:44:34 +01001662 LY_LIST_FOR(list->actions, action) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001663 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001664 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001665 }
1666
Radek Krejci2a9fc652021-01-22 17:44:34 +01001667 LY_LIST_FOR(list->notifs, notif) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001668 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001669 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001670 }
1671
1672 LEVEL--;
1673 ypr_close(ctx, flag);
1674}
1675
1676static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001677yprc_list(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001678{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001679 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001680 struct lysc_node_list *list = (struct lysc_node_list *)node;
1681
Michal Vaskoacb8e742020-10-20 10:28:02 +02001682 yprc_node_common1(ctx, node, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001683
1684 LY_ARRAY_FOR(list->musts, u) {
1685 yprc_must(ctx, &list->musts[u], NULL);
1686 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001687 if (!(list->flags & LYS_KEYLESS)) {
Michal Vasko5233e962020-08-14 14:26:20 +02001688 ly_print_(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001689 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 +02001690 ly_print_(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001691 }
Michal Vasko5233e962020-08-14 14:26:20 +02001692 ly_print_(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001693 }
1694 LY_ARRAY_FOR(list->uniques, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001695 ly_print_(ctx->out, "%*sunique \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02001696 LY_ARRAY_FOR(list->uniques[u], v) {
Michal Vasko5233e962020-08-14 14:26:20 +02001697 ly_print_(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001698 }
1699 ypr_close(ctx, 0);
1700 }
1701
1702 ypr_config(ctx, node->flags, node->exts, NULL);
1703
Radek Krejcifc596f92021-02-26 22:40:26 +01001704 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001705 if (list->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001706 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max, NULL);
Radek Krejci693262f2019-04-29 15:23:20 +02001707 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001708 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001709 }
1710
Radek Krejcifc596f92021-02-26 22:40:26 +01001711 ypr_substmt(ctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001712
1713 ypr_status(ctx, node->flags, node->exts, NULL);
1714 ypr_description(ctx, node->dsc, node->exts, NULL);
1715 ypr_reference(ctx, node->ref, node->exts, NULL);
1716
Radek Krejci52f65552020-09-01 17:03:35 +02001717 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001718 struct lysc_node *child;
1719 struct lysc_node_action *action;
1720 struct lysc_node_notif *notif;
1721
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001722 LY_LIST_FOR(list->child, child) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001723 yprc_node(ctx, child);
1724 }
Radek Krejci693262f2019-04-29 15:23:20 +02001725
Radek Krejci2a9fc652021-01-22 17:44:34 +01001726 LY_LIST_FOR(list->actions, action) {
1727 yprc_action(ctx, action);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001728 }
Radek Krejci693262f2019-04-29 15:23:20 +02001729
Radek Krejci2a9fc652021-01-22 17:44:34 +01001730 LY_LIST_FOR(list->notifs, notif) {
1731 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001732 }
Radek Krejci693262f2019-04-29 15:23:20 +02001733 }
1734
1735 LEVEL--;
Michal Vaskoacb8e742020-10-20 10:28:02 +02001736 ypr_close(ctx, 1);
Radek Krejci693262f2019-04-29 15:23:20 +02001737}
1738
1739static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001740yprp_refine(struct lys_ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001741{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001742 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001743 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001744
Michal Vasko5233e962020-08-14 14:26:20 +02001745 ly_print_(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001746 LEVEL++;
1747
Radek Krejci39b7fc22021-02-26 23:29:18 +01001748 yprp_extension_instances(ctx, LY_STMT_REFINE, 0, refine->exts, &flag, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001749 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001750
1751 LY_ARRAY_FOR(refine->musts, u) {
1752 ypr_open(ctx->out, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001753 yprp_restr(ctx, &refine->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001754 }
1755
1756 if (refine->presence) {
1757 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001758 ypr_substmt(ctx, LY_STMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001759 }
1760
1761 LY_ARRAY_FOR(refine->dflts, u) {
1762 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001763 ypr_substmt(ctx, LY_STMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001764 }
1765
Radek Krejci693262f2019-04-29 15:23:20 +02001766 ypr_config(ctx, refine->flags, refine->exts, &flag);
1767 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001768
1769 if (refine->flags & LYS_SET_MIN) {
1770 ypr_open(ctx->out, &flag);
Radek Krejcifc596f92021-02-26 22:40:26 +01001771 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001772 }
1773 if (refine->flags & LYS_SET_MAX) {
1774 ypr_open(ctx->out, &flag);
1775 if (refine->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001776 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001777 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01001778 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001779 }
1780 }
1781
1782 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1783 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1784
1785 LEVEL--;
1786 ypr_close(ctx, flag);
1787}
1788
1789static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001790yprp_augment(struct lys_ypr_ctx *ctx, const struct lysp_node_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001791{
Radek Krejcid3ca0632019-04-16 16:54:54 +02001792 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001793 struct lysp_node_action *action;
1794 struct lysp_node_notif *notif;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001795
Michal Vasko5233e962020-08-14 14:26:20 +02001796 ly_print_(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001797 LEVEL++;
1798
Radek Krejci39b7fc22021-02-26 23:29:18 +01001799 yprp_extension_instances(ctx, LY_STMT_AUGMENT, 0, aug->exts, NULL, 0);
Radek Krejci693262f2019-04-29 15:23:20 +02001800 yprp_when(ctx, aug->when, NULL);
1801 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1802 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001803 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1804 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1805
1806 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001807 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001808 }
1809
Radek Krejci2a9fc652021-01-22 17:44:34 +01001810 LY_LIST_FOR(aug->actions, action) {
1811 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001812 }
1813
Radek Krejci2a9fc652021-01-22 17:44:34 +01001814 LY_LIST_FOR(aug->notifs, notif) {
1815 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001816 }
1817
1818 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001819 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001820}
1821
Radek Krejcid3ca0632019-04-16 16:54:54 +02001822static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001823yprp_uses(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001824{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001825 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001826 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001827 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001828 struct lysp_node_augment *aug;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001829
Radek Krejci693262f2019-04-29 15:23:20 +02001830 yprp_node_common1(ctx, node, &flag);
1831 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001832
1833 LY_ARRAY_FOR(uses->refines, u) {
1834 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001835 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001836 }
1837
Radek Krejci2a9fc652021-01-22 17:44:34 +01001838 LY_LIST_FOR(uses->augments, aug) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001839 ypr_open(ctx->out, &flag);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001840 yprp_augment(ctx, aug);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001841 }
1842
1843 LEVEL--;
1844 ypr_close(ctx, flag);
1845}
1846
1847static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001848yprp_anydata(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001849{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001850 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001851 ly_bool flag = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001852 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1853
Radek Krejci693262f2019-04-29 15:23:20 +02001854 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001855
1856 LY_ARRAY_FOR(any->musts, u) {
1857 ypr_open(ctx->out, &flag);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001858 yprp_restr(ctx, &any->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001859 }
1860
Radek Krejci693262f2019-04-29 15:23:20 +02001861 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001862
1863 LEVEL--;
1864 ypr_close(ctx, flag);
1865}
1866
1867static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001868yprc_anydata(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001869{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001870 LY_ARRAY_COUNT_TYPE u;
Radek Krejci857189e2020-09-01 13:26:36 +02001871 ly_bool flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001872 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001873
Radek Krejci693262f2019-04-29 15:23:20 +02001874 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001875
Radek Krejci693262f2019-04-29 15:23:20 +02001876 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001877 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001878 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001879 }
1880
Radek Krejci693262f2019-04-29 15:23:20 +02001881 yprc_node_common2(ctx, node, &flag);
1882
Radek Krejcid3ca0632019-04-16 16:54:54 +02001883 LEVEL--;
1884 ypr_close(ctx, flag);
1885}
1886
1887static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001888yprp_node(struct lys_ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001889{
1890 switch (node->nodetype) {
1891 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001892 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001893 break;
1894 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001895 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001896 break;
1897 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001898 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001899 break;
1900 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001901 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001902 break;
1903 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001904 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001905 break;
1906 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001907 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001908 break;
1909 case LYS_ANYXML:
1910 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001911 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001912 break;
1913 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001914 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001915 break;
1916 default:
1917 break;
1918 }
1919}
1920
1921static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001922yprc_node(struct lys_ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejci693262f2019-04-29 15:23:20 +02001923{
1924 switch (node->nodetype) {
1925 case LYS_CONTAINER:
1926 yprc_container(ctx, node);
1927 break;
1928 case LYS_CHOICE:
1929 yprc_choice(ctx, node);
1930 break;
1931 case LYS_LEAF:
1932 yprc_leaf(ctx, node);
1933 break;
1934 case LYS_LEAFLIST:
1935 yprc_leaflist(ctx, node);
1936 break;
1937 case LYS_LIST:
1938 yprc_list(ctx, node);
1939 break;
1940 case LYS_ANYXML:
1941 case LYS_ANYDATA:
1942 yprc_anydata(ctx, node);
1943 break;
1944 default:
1945 break;
1946 }
1947}
1948
1949static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01001950yprp_deviation(struct lys_ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001951{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001952 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001953 struct lysp_deviate_add *add;
1954 struct lysp_deviate_rpl *rpl;
1955 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08001956 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001957
Michal Vasko5233e962020-08-14 14:26:20 +02001958 ly_print_(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001959 LEVEL++;
1960
Radek Krejci39b7fc22021-02-26 23:29:18 +01001961 yprp_extension_instances(ctx, LY_STMT_DEVIATION, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001962 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1963 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1964
fredgan2b11ddb2019-10-21 11:03:39 +08001965 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001966 ly_print_(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08001967 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1968 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001969 ly_print_(ctx->out, "not-supported {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001970 LEVEL++;
1971
Radek Krejci39b7fc22021-02-26 23:29:18 +01001972 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001973 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001974 ly_print_(ctx->out, "not-supported;\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001975 continue;
1976 }
fredgan2b11ddb2019-10-21 11:03:39 +08001977 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001978 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001979 ly_print_(ctx->out, "add {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001980 LEVEL++;
1981
Radek Krejci39b7fc22021-02-26 23:29:18 +01001982 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, add->exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01001983 ypr_substmt(ctx, LY_STMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001984 LY_ARRAY_FOR(add->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01001985 yprp_restr(ctx, &add->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001986 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001987 LY_ARRAY_FOR(add->uniques, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001988 ypr_substmt(ctx, LY_STMT_UNIQUE, u, add->uniques[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001989 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001990 LY_ARRAY_FOR(add->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001991 ypr_substmt(ctx, LY_STMT_DEFAULT, u, add->dflts[u].str, add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001992 }
Radek Krejci693262f2019-04-29 15:23:20 +02001993 ypr_config(ctx, add->flags, add->exts, NULL);
1994 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001995 if (add->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01001996 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001997 }
1998 if (add->flags & LYS_SET_MAX) {
1999 if (add->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002000 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002001 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01002002 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002003 }
2004 }
fredgan2b11ddb2019-10-21 11:03:39 +08002005 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002006 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002007 ly_print_(ctx->out, "replace {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002008 LEVEL++;
2009
Radek Krejci39b7fc22021-02-26 23:29:18 +01002010 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002011 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002012 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002013 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002014 ypr_substmt(ctx, LY_STMT_UNITS, 0, rpl->units, rpl->exts);
2015 ypr_substmt(ctx, LY_STMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002016 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2017 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002018 if (rpl->flags & LYS_SET_MIN) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002019 ypr_unsigned(ctx, LY_STMT_MIN_ELEMENTS, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002020 }
2021 if (rpl->flags & LYS_SET_MAX) {
2022 if (rpl->max) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002023 ypr_unsigned(ctx, LY_STMT_MAX_ELEMENTS, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002024 } else {
Radek Krejcifc596f92021-02-26 22:40:26 +01002025 ypr_substmt(ctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002026 }
2027 }
fredgan2b11ddb2019-10-21 11:03:39 +08002028 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002029 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02002030 ly_print_(ctx->out, "delete {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02002031 LEVEL++;
2032
Radek Krejci39b7fc22021-02-26 23:29:18 +01002033 yprp_extension_instances(ctx, LY_STMT_DEVIATE, 0, del->exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01002034 ypr_substmt(ctx, LY_STMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002035 LY_ARRAY_FOR(del->musts, u) {
Radek Krejci39b7fc22021-02-26 23:29:18 +01002036 yprp_restr(ctx, &del->musts[u], LY_STMT_MUST, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002037 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002038 LY_ARRAY_FOR(del->uniques, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002039 ypr_substmt(ctx, LY_STMT_UNIQUE, u, del->uniques[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002040 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002041 LY_ARRAY_FOR(del->dflts, u) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002042 ypr_substmt(ctx, LY_STMT_DEFAULT, u, del->dflts[u].str, del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002043 }
2044 }
2045
2046 LEVEL--;
2047 ypr_close(ctx, 1);
2048 }
2049
2050 LEVEL--;
2051 ypr_close(ctx, 1);
2052}
2053
Michal Vasko7c8439f2020-08-05 13:25:19 +02002054static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01002055yang_print_parsed_linkage(struct lys_ypr_ctx *ctx, const struct lysp_module *modp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002056{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002057 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002058
Radek Krejcid3ca0632019-04-16 16:54:54 +02002059 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01002060 if (modp->imports[u].flags & LYS_INTERNAL) {
2061 continue;
2062 }
2063
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002064 YPR_EXTRA_LINE_PRINT(ctx);
2065 ly_print_(ctx->out, "%*simport %s {\n", INDENT, modp->imports[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002066 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002067 yprp_extension_instances(ctx, LY_STMT_IMPORT, 0, modp->imports[u].exts, NULL, 0);
Radek Krejcifc596f92021-02-26 22:40:26 +01002068 ypr_substmt(ctx, LY_STMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002069 if (modp->imports[u].rev[0]) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002070 ypr_substmt(ctx, LY_STMT_REVISION_DATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002071 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002072 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2073 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002074 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002075 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002076 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002077 YPR_EXTRA_LINE(modp->imports, ctx);
2078
Radek Krejcid3ca0632019-04-16 16:54:54 +02002079 LY_ARRAY_FOR(modp->includes, u) {
Radek Krejci771928a2021-01-19 13:42:36 +01002080 if (modp->includes[u].injected) {
2081 /* do not print the includes injected from submodules */
2082 continue;
2083 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002084 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002085 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 +02002086 ly_print_(ctx->out, "%*sinclude %s {\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002087 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002088 yprp_extension_instances(ctx, LY_STMT_INCLUDE, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002089 if (modp->includes[u].rev[0]) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002090 ypr_substmt(ctx, LY_STMT_REVISION_DATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002091 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002092 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2093 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002095 ly_print_(ctx->out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002096 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02002097 ly_print_(ctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002098 }
2099 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002100 YPR_EXTRA_LINE(modp->includes, ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002101}
Radek Krejcid3ca0632019-04-16 16:54:54 +02002102
Michal Vasko7c8439f2020-08-05 13:25:19 +02002103static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01002104yang_print_parsed_body(struct lys_ypr_ctx *ctx, const struct lysp_module *modp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002105{
2106 LY_ARRAY_COUNT_TYPE u;
2107 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002108 struct lysp_node_action *action;
2109 struct lysp_node_notif *notif;
2110 struct lysp_node_grp *grp;
2111 struct lysp_node_augment *aug;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002112
Radek Krejcid3ca0632019-04-16 16:54:54 +02002113 LY_ARRAY_FOR(modp->extensions, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002114 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002115 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002116 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002117
2118 YPR_EXTRA_LINE(modp->extensions, ctx);
2119
Radek Krejcid3ca0632019-04-16 16:54:54 +02002120 if (modp->exts) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002121 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002122 yprp_extension_instances(ctx, LY_STMT_MODULE, 0, modp->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002123 }
2124
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002125 YPR_EXTRA_LINE(modp->exts, ctx);
2126
Radek Krejcid3ca0632019-04-16 16:54:54 +02002127 LY_ARRAY_FOR(modp->features, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002128 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002129 yprp_feature(ctx, &modp->features[u]);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002130 YPR_EXTRA_LINE(1, ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002131 }
2132
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002133 YPR_EXTRA_LINE(modp->features, ctx);
2134
Radek Krejcid3ca0632019-04-16 16:54:54 +02002135 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002136 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002137 yprp_identity(ctx, &modp->identities[u]);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002138 YPR_EXTRA_LINE(1, ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002139 }
2140
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002141 YPR_EXTRA_LINE(modp->identities, ctx);
2142
Radek Krejcid3ca0632019-04-16 16:54:54 +02002143 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002144 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002145 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002146 YPR_EXTRA_LINE(1, ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002147 }
2148
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002149 YPR_EXTRA_LINE(modp->typedefs, ctx);
2150
Radek Krejci2a9fc652021-01-22 17:44:34 +01002151 LY_LIST_FOR(modp->groupings, grp) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002152 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002153 yprp_grouping(ctx, grp);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002154 YPR_EXTRA_LINE(1, ctx);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002155 }
2156
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002157 YPR_EXTRA_LINE(modp->groupings, ctx);
2158
Radek Krejcid3ca0632019-04-16 16:54:54 +02002159 LY_LIST_FOR(modp->data, data) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002160 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002161 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002162 }
2163
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002164 YPR_EXTRA_LINE(modp->data, ctx);
2165
Radek Krejci2a9fc652021-01-22 17:44:34 +01002166 LY_LIST_FOR(modp->augments, aug) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002167 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002168 yprp_augment(ctx, aug);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002169 }
2170
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002171 YPR_EXTRA_LINE(modp->augments, ctx);
2172
Radek Krejci2a9fc652021-01-22 17:44:34 +01002173 LY_LIST_FOR(modp->rpcs, action) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002174 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002175 yprp_action(ctx, action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002176 }
2177
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002178 YPR_EXTRA_LINE(modp->rpcs, ctx);
2179
Radek Krejci2a9fc652021-01-22 17:44:34 +01002180 LY_LIST_FOR(modp->notifs, notif) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002181 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002182 yprp_notification(ctx, notif);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002183 }
2184
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002185 YPR_EXTRA_LINE(modp->notifs, ctx);
2186
Radek Krejcid3ca0632019-04-16 16:54:54 +02002187 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002188 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002189 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002190 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002191
2192 YPR_EXTRA_LINE(modp->deviations, ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002193}
2194
2195LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01002196yang_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002197{
2198 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7997d5a2021-02-22 10:55:56 +01002199 const struct lys_module *module = modp->mod;
Radek Krejciadcf63d2021-02-09 10:21:18 +01002200 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 +02002201
Michal Vasko5233e962020-08-14 14:26:20 +02002202 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002203 LEVEL++;
2204
2205 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002206 if (modp->version) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002207 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 +02002208 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002209 ypr_substmt(ctx, LY_STMT_NAMESPACE, 0, module->ns, modp->exts);
2210 ypr_substmt(ctx, LY_STMT_PREFIX, 0, module->prefix, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002211
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002212 YPR_EXTRA_LINE(1, ctx);
2213
Michal Vasko7c8439f2020-08-05 13:25:19 +02002214 /* linkage-stmts (import/include) */
2215 yang_print_parsed_linkage(ctx, modp);
2216
2217 /* meta-stmts */
2218 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002219 YPR_EXTRA_LINE_PRINT(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002220 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002221 ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, module->org, modp->exts);
2222 ypr_substmt(ctx, LY_STMT_CONTACT, 0, module->contact, modp->exts);
2223 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, module->dsc, modp->exts);
2224 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, module->ref, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002225
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002226 YPR_EXTRA_LINE(module->org || module->contact || module->dsc || module->ref, ctx);
2227
Michal Vasko7c8439f2020-08-05 13:25:19 +02002228 /* revision-stmts */
Michal Vasko7c8439f2020-08-05 13:25:19 +02002229 LY_ARRAY_FOR(modp->revs, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002230 YPR_EXTRA_LINE_PRINT(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002231 yprp_revision(ctx, &modp->revs[u]);
2232 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002233
2234 YPR_EXTRA_LINE(modp->revs, ctx);
2235
Michal Vasko7c8439f2020-08-05 13:25:19 +02002236 /* body-stmts */
2237 yang_print_parsed_body(ctx, modp);
2238
2239 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002240 ly_print_(out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002241 ly_print_flush(out);
2242
2243 return LY_SUCCESS;
2244}
2245
2246static void
Radek Krejciadcf63d2021-02-09 10:21:18 +01002247yprp_belongsto(struct lys_ypr_ctx *ctx, const struct lysp_submodule *submodp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002248{
Michal Vaskoc3781c32020-10-06 14:04:08 +02002249 ly_print_(ctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->mod->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002250 LEVEL++;
Radek Krejcifc596f92021-02-26 22:40:26 +01002251 yprp_extension_instances(ctx, LY_STMT_BELONGS_TO, 0, submodp->exts, NULL, 0);
2252 ypr_substmt(ctx, LY_STMT_PREFIX, 0, submodp->prefix, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002253 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002254 ly_print_(ctx->out, "%*s}\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002255}
2256
2257LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01002258yang_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02002259{
2260 LY_ARRAY_COUNT_TYPE u;
Radek Krejci07a55962021-03-02 20:16:43 +01002261 struct lys_ypr_ctx ctx_ = {
2262 .out = out, .level = 0, .module = submodp->mod, .schema = LYS_YPR_PARSED,
2263 .options = options
2264 }, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02002265
Michal Vasko5233e962020-08-14 14:26:20 +02002266 ly_print_(ctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002267 LEVEL++;
2268
2269 /* submodule-header-stmts */
2270 if (submodp->version) {
Radek Krejcifc596f92021-02-26 22:40:26 +01002271 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 +02002272 }
2273
2274 yprp_belongsto(ctx, submodp);
2275
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002276 YPR_EXTRA_LINE(1, ctx);
2277
Michal Vasko7c8439f2020-08-05 13:25:19 +02002278 /* linkage-stmts (import/include) */
2279 yang_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
2280
2281 /* meta-stmts */
2282 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002283 YPR_EXTRA_LINE_PRINT(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002284 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002285 ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, submodp->org, submodp->exts);
2286 ypr_substmt(ctx, LY_STMT_CONTACT, 0, submodp->contact, submodp->exts);
2287 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
2288 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, submodp->ref, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002289
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002290 YPR_EXTRA_LINE(submodp->org || submodp->contact || submodp->dsc || submodp->ref, ctx);
2291
Michal Vasko7c8439f2020-08-05 13:25:19 +02002292 /* revision-stmts */
Michal Vasko7c8439f2020-08-05 13:25:19 +02002293 LY_ARRAY_FOR(submodp->revs, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002294 YPR_EXTRA_LINE_PRINT(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +02002295 yprp_revision(ctx, &submodp->revs[u]);
2296 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002297
2298 YPR_EXTRA_LINE(submodp->revs, ctx);
2299
Michal Vasko7c8439f2020-08-05 13:25:19 +02002300 /* body-stmts */
2301 yang_print_parsed_body(ctx, (struct lysp_module *)submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002302
2303 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002304 ly_print_(out, "%*s}\n", INDENT);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002305 ly_print_flush(out);
2306
2307 return LY_SUCCESS;
2308}
2309
2310LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002311yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options)
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002312{
Radek Krejciadcf63d2021-02-09 10:21:18 +01002313 struct lys_ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options}, *ctx = &ctx_;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002314
2315 yprc_node(ctx, node);
2316
2317 ly_print_flush(out);
2318 return LY_SUCCESS;
2319}
2320
2321LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002322yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002323{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002324 LY_ARRAY_COUNT_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002325 struct lysc_module *modc = module->compiled;
Radek Krejciadcf63d2021-02-09 10:21:18 +01002326 struct lys_ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002327
Michal Vasko5233e962020-08-14 14:26:20 +02002328 ly_print_(ctx->out, "%*smodule %s {\n", INDENT, module->name);
Radek Krejci693262f2019-04-29 15:23:20 +02002329 LEVEL++;
2330
Radek Krejci693262f2019-04-29 15:23:20 +02002331 /* module-header-stmts */
Radek Krejcifc596f92021-02-26 22:40:26 +01002332 ypr_substmt(ctx, LY_STMT_NAMESPACE, 0, module->ns, modc->exts);
2333 ypr_substmt(ctx, LY_STMT_PREFIX, 0, module->prefix, modc->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002334
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002335 YPR_EXTRA_LINE(1, ctx);
2336
Michal Vasko7c8439f2020-08-05 13:25:19 +02002337 /* no linkage-stmts */
Radek Krejci693262f2019-04-29 15:23:20 +02002338
2339 /* meta-stmts */
2340 if (module->org || module->contact || module->dsc || module->ref) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002341 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002342 }
Radek Krejcifc596f92021-02-26 22:40:26 +01002343 ypr_substmt(ctx, LY_STMT_ORGANIZATION, 0, module->org, modc->exts);
2344 ypr_substmt(ctx, LY_STMT_CONTACT, 0, module->contact, modc->exts);
2345 ypr_substmt(ctx, LY_STMT_DESCRIPTION, 0, module->dsc, modc->exts);
2346 ypr_substmt(ctx, LY_STMT_REFERENCE, 0, module->ref, modc->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02002347
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002348 YPR_EXTRA_LINE(module->org || module->contact || module->dsc || module->ref, ctx);
2349
Radek Krejci693262f2019-04-29 15:23:20 +02002350 /* revision-stmts */
2351 if (module->revision) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002352 YPR_EXTRA_LINE_PRINT(ctx);
2353 ly_print_(ctx->out, "%*srevision %s;\n", INDENT, module->revision);
2354 YPR_EXTRA_LINE(1, ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002355 }
2356
2357 /* body-stmts */
2358 if (modc->exts) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002359 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002360 yprc_extension_instances(ctx, LY_STMT_MODULE, 0, module->compiled->exts, NULL, 0);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002361 YPR_EXTRA_LINE(1, ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002362 }
2363
Radek Krejci80d281e2020-09-14 17:42:54 +02002364 LY_ARRAY_FOR(module->identities, u) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002365 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci80d281e2020-09-14 17:42:54 +02002366 yprc_identity(ctx, &module->identities[u]);
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002367 YPR_EXTRA_LINE(1, ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002368 }
2369
Radek Krejci52f65552020-09-01 17:03:35 +02002370 if (!(ctx->options & LYS_PRINT_NO_SUBSTMT)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002371 struct lysc_node *data;
2372 struct lysc_node_action *rpc;
2373 struct lysc_node_notif *notif;
2374
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002375 LY_LIST_FOR(modc->data, data) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002376 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002377 yprc_node(ctx, data);
2378 }
Radek Krejci693262f2019-04-29 15:23:20 +02002379
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002380 YPR_EXTRA_LINE(modc->data, ctx);
2381
Radek Krejci2a9fc652021-01-22 17:44:34 +01002382 LY_LIST_FOR(modc->rpcs, rpc) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002383 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002384 yprc_action(ctx, rpc);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002385 }
Radek Krejci693262f2019-04-29 15:23:20 +02002386
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002387 YPR_EXTRA_LINE(modc->rpcs, ctx);
2388
Radek Krejci2a9fc652021-01-22 17:44:34 +01002389 LY_LIST_FOR(modc->notifs, notif) {
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002390 YPR_EXTRA_LINE_PRINT(ctx);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002391 yprc_notification(ctx, notif);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002392 }
Radek Krejciaa14a0c2020-09-04 11:16:47 +02002393
2394 YPR_EXTRA_LINE(modc->notifs, ctx);
Radek Krejci693262f2019-04-29 15:23:20 +02002395 }
2396
2397 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02002398 ly_print_(out, "%*s}\n", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +02002399 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002400
2401 return LY_SUCCESS;
2402}
Radek Krejciadcf63d2021-02-09 10:21:18 +01002403
2404/**
2405 * @param[in] count Number of extensions to print, 0 to print them all.
2406 */
2407static void
Radek Krejcifc596f92021-02-26 22:40:26 +01002408yprc_extension_instances(struct lys_ypr_ctx *ctx, enum ly_stmt substmt, uint8_t substmt_index,
Radek Krejciadcf63d2021-02-09 10:21:18 +01002409 struct lysc_ext_instance *ext, ly_bool *flag, LY_ARRAY_COUNT_TYPE count)
2410{
2411 LY_ARRAY_COUNT_TYPE u;
2412
2413 if (!count && ext) {
2414 count = LY_ARRAY_COUNT(ext);
2415 }
2416 LY_ARRAY_FOR(ext, u) {
2417 ly_bool inner_flag = 0;
2418
2419 if (!count) {
2420 break;
2421 }
2422
2423 count--;
Radek Krejciab430862021-03-02 20:13:40 +01002424 if ((ext->parent_stmt != substmt) || (ext->parent_stmt_index != substmt_index)) {
Radek Krejciadcf63d2021-02-09 10:21:18 +01002425 continue;
2426 }
2427
2428 ypr_open(ctx->out, flag);
2429 if (ext[u].argument) {
2430 ly_print_(ctx->out, "%*s%s:%s \"", INDENT, ext[u].def->module->name, ext[u].def->name);
2431 ypr_encode(ctx->out, ext[u].argument, -1);
2432 ly_print_(ctx->out, "\"");
2433 } else {
2434 ly_print_(ctx->out, "%*s%s:%s", INDENT, ext[u].def->module->name, ext[u].def->name);
2435 }
2436
2437 LEVEL++;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002438 yprc_extension_instances(ctx, LY_STMT_EXTENSION_INSTANCE, 0, ext[u].exts, &inner_flag, 0);
Radek Krejciadcf63d2021-02-09 10:21:18 +01002439
Radek Krejci4a4d1e02021-04-09 14:04:40 +02002440 if (ext[u].def->plugin && ext[u].def->plugin->sprinter) {
Radek Krejcif8d7f9a2021-03-10 14:32:36 +01002441 ext[u].def->plugin->sprinter(&ctx->printer_ctx, &ext[u], &inner_flag);
Radek Krejciadcf63d2021-02-09 10:21:18 +01002442 }
2443
2444 LEVEL--;
2445 ypr_close(ctx, inner_flag);
2446 }
2447}
2448
2449void
Radek Krejcif8d7f9a2021-03-10 14:32:36 +01002450lysc_print_extension_instance(struct lyspr_ctx *ctx_generic, const struct lysc_ext_instance *ext, ly_bool *flag)
Radek Krejciadcf63d2021-02-09 10:21:18 +01002451{
Radek Krejcif8d7f9a2021-03-10 14:32:36 +01002452 struct lys_ypr_ctx *ctx = (struct lys_ypr_ctx *)ctx_generic;
Radek Krejciadcf63d2021-02-09 10:21:18 +01002453 LY_ARRAY_COUNT_TYPE u, v;
2454
2455 LY_ARRAY_FOR(ext->substmts, u) {
2456 switch (ext->substmts[u].stmt) {
2457 case LY_STMT_CHOICE:
2458 case LY_STMT_CONTAINER: {
2459 const struct lysc_node *node;
2460
2461 LY_LIST_FOR(*(const struct lysc_node **)ext->substmts[u].storage, node) {
2462 ypr_open(ctx->out, flag);
2463 yprc_node(ctx, node);
2464 }
2465 break;
2466 }
2467 case LY_STMT_DESCRIPTION:
2468 case LY_STMT_REFERENCE:
2469 case LY_STMT_UNITS:
2470 if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
2471 if (*(const char **)ext->substmts[u].storage) {
2472 ypr_open(ctx->out, flag);
2473 ypr_substmt(ctx, ext->substmts[u].stmt, 0, *(const char **)ext->substmts[u].storage, ext->exts);
2474 }
2475 } else {
2476 const char **strings = *(const char ***)ext->substmts[u].storage;
2477 LY_ARRAY_FOR(strings, v) {
2478 ypr_open(ctx->out, flag);
2479 ypr_substmt(ctx, ext->substmts[u].stmt, v, strings[v], ext->exts);
2480 }
2481 }
2482 break;
2483 case LY_STMT_IF_FEATURE:
2484 /* nothing to do */
2485 break;
2486 case LY_STMT_STATUS:
2487 ypr_status(ctx, *(uint16_t *)ext->substmts[u].storage, ext->exts, flag);
2488 break;
2489 case LY_STMT_TYPE:
2490 if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
2491 if (*(const struct lysc_type **)ext->substmts[u].storage) {
2492 ypr_open(ctx->out, flag);
2493 yprc_type(ctx, *(const struct lysc_type **)ext->substmts[u].storage);
2494 }
2495 } else {
2496 const struct lysc_type **types = *(const struct lysc_type ***)ext->substmts[u].storage;
2497 LY_ARRAY_FOR(types, v) {
2498 ypr_open(ctx->out, flag);
2499 yprc_type(ctx, types[v]);
2500 }
2501 }
2502 break;
2503 /* TODO support other substatements */
2504 default:
2505 LOGWRN(ctx->module->ctx, "Statement \"%s\" is not supported for an extension printer.",
2506 ly_stmt2str(ext->substmts[u].stmt));
2507 break;
2508 }
2509 }
2510}