blob: 9d30254ff7ac62821033a6782f70d13d6d5c5ed9 [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
15#include "common.h"
16
Radek Krejci693262f2019-04-29 15:23:20 +020017#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Radek Krejci693262f2019-04-29 15:23:20 +020022
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020024#include "plugins_exts.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020025#include "printer_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "tree.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020027#include "tree_schema.h"
28#include "tree_schema_internal.h"
Radek Krejcia1911222019-07-22 17:24:50 +020029#include "plugins_types.h"
Radek Krejci693262f2019-04-29 15:23:20 +020030#include "xpath.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020031
Radek Krejcie7b95092019-05-15 11:03:07 +020032/**
33 * @brief Types of the YANG printers
34 */
Radek Krejci693262f2019-04-29 15:23:20 +020035enum schema_type {
Radek Krejcie7b95092019-05-15 11:03:07 +020036 YPR_PARSED, /**< YANG printer of the parsed schema */
37 YPR_COMPILED /**< YANG printer of the compiled schema */
Radek Krejci693262f2019-04-29 15:23:20 +020038};
39
Radek Krejcie7b95092019-05-15 11:03:07 +020040/**
41 * @brief YANG printer context.
42 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020043struct ypr_ctx {
Radek Krejcie7b95092019-05-15 11:03:07 +020044 struct lyout *out; /**< output specification */
45 unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
46 const struct lys_module *module; /**< schema to print */
47 enum schema_type schema; /**< type of the schema to print */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080048 int options; /**< Schema output options (see @ref schemaprinterflags). */
Radek Krejcid3ca0632019-04-16 16:54:54 +020049};
50
Radek Krejcie7b95092019-05-15 11:03:07 +020051#define LEVEL ctx->level /**< current level */
52#define INDENT (LEVEL)*2,"" /**< indentation parameters for printer functions */
53
54/**
55 * @brief Print the given text as content of a double quoted YANG string,
56 * including encoding characters that have special meanings. The quotation marks
57 * are not printed.
58 *
59 * Follows RFC 7950, section 6.1.3.
60 *
61 * @param[in] out Output specification.
62 * @param[in] text String to be printed.
Radek Krejcif56e2a42019-09-09 14:15:25 +020063 * @param[in] len Length of the string from @p text to be printed. In case of -1,
Radek Krejcie7b95092019-05-15 11:03:07 +020064 * the @p text is printed completely as a NULL-terminated string.
65 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020066static void
67ypr_encode(struct lyout *out, const char *text, int len)
68{
69 int i, start_len;
70 const char *start;
71 char special = 0;
72
73 if (!len) {
74 return;
75 }
76
77 if (len < 0) {
78 len = strlen(text);
79 }
80
81 start = text;
82 start_len = 0;
83 for (i = 0; i < len; ++i) {
84 switch (text[i]) {
85 case '\n':
86 case '\t':
87 case '\"':
88 case '\\':
89 special = text[i];
90 break;
91 default:
92 ++start_len;
93 break;
94 }
95
96 if (special) {
97 ly_write(out, start, start_len);
98 switch (special) {
99 case '\n':
100 ly_write(out, "\\n", 2);
101 break;
102 case '\t':
103 ly_write(out, "\\t", 2);
104 break;
105 case '\"':
106 ly_write(out, "\\\"", 2);
107 break;
108 case '\\':
109 ly_write(out, "\\\\", 2);
110 break;
111 }
112
113 start += start_len + 1;
114 start_len = 0;
115
116 special = 0;
117 }
118 }
119
120 ly_write(out, start, start_len);
121}
122
123static void
124ypr_open(struct lyout *out, int *flag)
125{
126 if (flag && !*flag) {
127 *flag = 1;
128 ly_print(out, " {\n");
129 }
130}
131
132static void
133ypr_close(struct ypr_ctx *ctx, int flag)
134{
135 if (flag) {
136 ly_print(ctx->out, "%*s}\n", INDENT);
137 } else {
138 ly_print(ctx->out, ";\n");
139 }
140}
141
142static void
143ypr_text(struct ypr_ctx *ctx, const char *name, const char *text, int singleline, int closed)
144{
145 const char *s, *t;
146
147 if (singleline) {
148 ly_print(ctx->out, "%*s%s \"", INDENT, name);
149 } else {
150 ly_print(ctx->out, "%*s%s\n", INDENT, name);
151 LEVEL++;
152
153 ly_print(ctx->out, "%*s\"", INDENT);
154 }
155 t = text;
156 while ((s = strchr(t, '\n'))) {
157 ypr_encode(ctx->out, t, s - t);
158 ly_print(ctx->out, "\n");
159 t = s + 1;
160 if (*t != '\n') {
161 ly_print(ctx->out, "%*s ", INDENT);
162 }
163 }
164
165 ypr_encode(ctx->out, t, strlen(t));
166 if (closed) {
167 ly_print(ctx->out, "\";\n");
168 } else {
169 ly_print(ctx->out, "\"");
170 }
171 if (!singleline) {
172 LEVEL--;
173 }
174}
175
176static void
Radek Krejci693262f2019-04-29 15:23:20 +0200177yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200178{
179 struct lysp_stmt *childstmt;
180 const char *s, *t;
181
182 if (stmt->arg) {
183 if (stmt->flags) {
184 ly_print(ctx->out, "%*s%s\n", INDENT, stmt->stmt);
185 LEVEL++;
186 ly_print(ctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'');
187 t = stmt->arg;
188 while ((s = strchr(t, '\n'))) {
189 ypr_encode(ctx->out, t, s - t);
190 ly_print(ctx->out, "\n");
191 t = s + 1;
192 if (*t != '\n') {
193 ly_print(ctx->out, "%*s ", INDENT);
194 }
195 }
196 LEVEL--;
197 ypr_encode(ctx->out, t, strlen(t));
198 ly_print(ctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n");
199 } else {
200 ly_print(ctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n");
201 }
202 } else {
203 ly_print(ctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n");
204 }
205
206 if (stmt->child) {
207 LEVEL++;
208 LY_LIST_FOR(stmt->child, childstmt) {
Radek Krejci693262f2019-04-29 15:23:20 +0200209 yprp_stmt(ctx, childstmt);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200210 }
211 LEVEL--;
212 ly_print(ctx->out, "%*s}\n", INDENT);
213 }
214}
215
216/**
217 * @param[in] count Number of extensions to print, 0 to print them all.
218 */
219static void
Radek Krejci693262f2019-04-29 15:23:20 +0200220yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200221 struct lysp_ext_instance *ext, int *flag, LY_ARRAY_SIZE_TYPE count)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200222{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200223 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200224 struct lysp_stmt *stmt;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200225 int child_presence;
226 const char *argument;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227
228 if (!count && ext) {
229 count = LY_ARRAY_SIZE(ext);
230 }
231 LY_ARRAY_FOR(ext, u) {
232 if (!count) {
233 break;
234 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200235
Radek Krejcid3ca0632019-04-16 16:54:54 +0200236 count--;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200237 if (ext->insubstmt != substmt || ext->insubstmt_index != substmt_index) {
238 continue;
239 }
240
241 if (!ext->compiled && ext->yin) {
242 ly_print(ctx->out, "%*s%s; // Model comes from different input format, extensions must be resolved first.\n", INDENT, ext[u].name);
243 continue;
244 }
245
246
247 ypr_open(ctx->out, flag);
248 argument = NULL;
249 if (ext[u].compiled) {
250 argument = ext[u].compiled->argument;
251 } else {
252 argument = ext[u].argument;
253 }
254 if (argument) {
255 ly_print(ctx->out, "%*s%s \"", INDENT, ext[u].name);
256 ypr_encode(ctx->out, argument, -1);
257 ly_print(ctx->out, "\"");
258 } else {
259 ly_print(ctx->out, "%*s%s", INDENT, ext[u].name);
260 }
261
262 child_presence = 0;
263 LEVEL++;
264 LY_LIST_FOR(ext[u].child, stmt) {
265 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
266 continue;
267 }
268 if (!child_presence) {
269 ly_print(ctx->out, " {\n");
270 child_presence = 1;
271 }
272 yprp_stmt(ctx, stmt);
273 }
274 LEVEL--;
275 if (child_presence) {
276 ly_print(ctx->out, "%*s}\n", INDENT);
277 } else {
278 ly_print(ctx->out, ";\n");
279 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200280 }
281}
282
Radek Krejci693262f2019-04-29 15:23:20 +0200283/**
284 * @param[in] count Number of extensions to print, 0 to print them all.
285 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200286static void
Radek Krejci693262f2019-04-29 15:23:20 +0200287yprc_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200288 struct lysc_ext_instance *ext, int *flag, LY_ARRAY_SIZE_TYPE count)
Radek Krejci693262f2019-04-29 15:23:20 +0200289{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200290 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200291
292 if (!count && ext) {
293 count = LY_ARRAY_SIZE(ext);
294 }
295 LY_ARRAY_FOR(ext, u) {
296 if (!count) {
297 break;
298 }
299 /* TODO compiled extensions */
300 (void) ctx;
301 (void) substmt;
302 (void) substmt_index;
303 (void) flag;
304
305 count--;
306 }
307}
308
309static void
310ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200311{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200312 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200313 int extflag = 0;
314
315 if (!text) {
316 /* nothing to print */
317 return;
318 }
319
320 if (ext_substmt_info[substmt].flags & SUBST_FLAG_ID) {
321 ly_print(ctx->out, "%*s%s %s", INDENT, ext_substmt_info[substmt].name, text);
322 } else {
323 ypr_text(ctx, ext_substmt_info[substmt].name, text,
324 (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) ? 0 : 1, 0);
325 }
326
327 LEVEL++;
328 LY_ARRAY_FOR(ext, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200329 if (((struct lysp_ext_instance*)ext)[u].insubstmt != substmt || ((struct lysp_ext_instance*)ext)[u].insubstmt_index != substmt_index) {
Radek Krejcid3ca0632019-04-16 16:54:54 +0200330 continue;
331 }
Radek Krejci693262f2019-04-29 15:23:20 +0200332 if (ctx->schema == YPR_PARSED) {
333 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance*)ext)[u], &extflag, 1);
334 } else {
335 yprc_extension_instances(ctx, substmt, substmt_index, &((struct lysc_ext_instance*)ext)[u], &extflag, 1);
336 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200337 }
338 LEVEL--;
339 ypr_close(ctx, extflag);
340}
341
342static void
Radek Krejci693262f2019-04-29 15:23:20 +0200343ypr_unsigned(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, unsigned int attr_value, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200344{
345 char *str;
346
347 if (asprintf(&str, "%u", attr_value) == -1) {
348 LOGMEM(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +0200349 ctx->out->status = LY_EMEM;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200350 return;
351 }
352 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200353 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200354 free(str);
355}
356
357static void
Radek Krejci693262f2019-04-29 15:23:20 +0200358ypr_signed(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, signed int attr_value, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200359{
360 char *str;
361
362 if (asprintf(&str, "%d", attr_value) == -1) {
363 LOGMEM(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +0200364 ctx->out->status = LY_EMEM;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200365 return;
366 }
367 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200368 ypr_substmt(ctx, substmt, substmt_index, str, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200369 free(str);
370}
371
372static void
Radek Krejci693262f2019-04-29 15:23:20 +0200373yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200374{
375 if (rev->dsc || rev->ref || rev->exts) {
376 ly_print(ctx->out, "%*srevision %s {\n", INDENT, rev->date);
377 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200378 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
379 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
380 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200381 LEVEL--;
382 ly_print(ctx->out, "%*s}\n", INDENT);
383 } else {
384 ly_print(ctx->out, "%*srevision %s;\n", INDENT, rev->date);
385 }
386}
387
388static void
Radek Krejci693262f2019-04-29 15:23:20 +0200389ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200390{
391 if (flags & LYS_MAND_MASK) {
392 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200393 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200394 }
395}
396
397static void
Radek Krejci693262f2019-04-29 15:23:20 +0200398ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200399{
400 if (flags & LYS_CONFIG_MASK) {
401 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200402 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200403 }
404}
405
406static void
Radek Krejci693262f2019-04-29 15:23:20 +0200407ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200408{
409 const char *status = NULL;
410
411 if (flags & LYS_STATUS_CURR) {
412 ypr_open(ctx->out, flag);
413 status = "current";
414 } else if (flags & LYS_STATUS_DEPRC) {
415 ypr_open(ctx->out, flag);
416 status = "deprecated";
417 } else if (flags & LYS_STATUS_OBSLT) {
418 ypr_open(ctx->out, flag);
419 status = "obsolete";
420 }
Radek Krejci693262f2019-04-29 15:23:20 +0200421
422 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200423}
424
425static void
Radek Krejci693262f2019-04-29 15:23:20 +0200426ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200427{
428 if (dsc) {
429 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200430 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200431 }
432}
433
434static void
Radek Krejci693262f2019-04-29 15:23:20 +0200435ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200436{
437 if (ref) {
438 ypr_open(ctx->out, flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200439 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200440 }
441}
442
443static void
Radek Krejci693262f2019-04-29 15:23:20 +0200444yprp_iffeatures(struct ypr_ctx *ctx, const char **iff, struct lysp_ext_instance *exts, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200445{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200446 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200447 int extflag;
448
449 LY_ARRAY_FOR(iff, u) {
450 ypr_open(ctx->out, flag);
451 extflag = 0;
452
453 ly_print(ctx->out, "%*sif-feature \"%s\"", INDENT, iff[u]);
454
455 /* extensions */
456 LEVEL++;
457 LY_ARRAY_FOR(exts, u) {
458 if (exts[u].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[u].insubstmt_index != u) {
459 continue;
460 }
Radek Krejci693262f2019-04-29 15:23:20 +0200461 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[u], &extflag, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200462 }
463 LEVEL--;
464 ypr_close(ctx, extflag);
465 }
466}
467
468static void
Radek Krejci693262f2019-04-29 15:23:20 +0200469yprc_iffeature(struct ypr_ctx *ctx, struct lysc_iffeature *feat, int *index_e, int *index_f)
470{
471 int brackets_flag = *index_e;
472 uint8_t op;
473
474 op = lysc_iff_getop(feat->expr, *index_e);
475 (*index_e)++;
476
477 switch (op) {
478 case LYS_IFF_F:
479 if (ctx->module == feat->features[*index_f]->module) {
480 ly_print(ctx->out, "%s", feat->features[*index_f]->name);
481 } else {
482 ly_print(ctx->out, "%s:%s", feat->features[*index_f]->module->prefix, feat->features[*index_f]->name);
483 }
484 (*index_f)++;
485 break;
486 case LYS_IFF_NOT:
487 ly_print(ctx->out, "not ");
488 yprc_iffeature(ctx, feat, index_e, index_f);
489 break;
490 case LYS_IFF_AND:
491 if (brackets_flag) {
492 /* AND need brackets only if previous op was not */
493 if (*index_e < 2 || lysc_iff_getop(feat->expr, *index_e - 2) != LYS_IFF_NOT) {
494 brackets_flag = 0;
495 }
496 }
497 /* falls through */
498 case LYS_IFF_OR:
499 if (brackets_flag) {
500 ly_print(ctx->out, "(");
501 }
502 yprc_iffeature(ctx, feat, index_e, index_f);
503 ly_print(ctx->out, " %s ", op == LYS_IFF_OR ? "or" : "and");
504 yprc_iffeature(ctx, feat, index_e, index_f);
505 if (brackets_flag) {
506 ly_print(ctx->out, ")");
507 }
508 }
509}
510
511static void
512yprc_iffeatures(struct ypr_ctx *ctx, struct lysc_iffeature *iff, struct lysc_ext_instance *exts, int *flag)
513{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200514 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +0200515 int extflag;
516
517 LY_ARRAY_FOR(iff, u) {
518 int index_e = 0, index_f = 0;
519
520 ypr_open(ctx->out, flag);
521 extflag = 0;
522
Radek Krejci989e53b2019-04-30 09:51:09 +0200523 ly_print(ctx->out, "%*sif-feature \"", INDENT);
Radek Krejci693262f2019-04-29 15:23:20 +0200524 yprc_iffeature(ctx, iff, &index_e, &index_f);
Radek Krejci989e53b2019-04-30 09:51:09 +0200525 ly_print(ctx->out, "\"");
Radek Krejci693262f2019-04-29 15:23:20 +0200526
527 /* extensions */
528 LEVEL++;
Radek Krejci334ccc72019-06-12 13:49:29 +0200529 LY_ARRAY_FOR(exts, v) {
530 if (exts[v].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[v].insubstmt_index != u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200531 continue;
532 }
Radek Krejci334ccc72019-06-12 13:49:29 +0200533 yprc_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
Radek Krejci693262f2019-04-29 15:23:20 +0200534 }
535 LEVEL--;
536 ypr_close(ctx, extflag);
537 }
538}
539
540static void
541yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200542{
543 int flag = 0, flag2 = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200544 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200545
546 ly_print(ctx->out, "%*sextension %s", INDENT, ext->name);
547 LEVEL++;
548
549 if (ext->exts) {
Radek Krejci693262f2019-04-29 15:23:20 +0200550 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200551 }
552
553 if (ext->argument) {
554 ypr_open(ctx->out, &flag);
555 ly_print(ctx->out, "%*sargument %s", INDENT, ext->argument);
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200556 LEVEL++;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200557 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200558 u = -1;
559 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LYEXT_SUBSTMT_ARGUMENT)) != LY_ARRAY_SIZE(ext->exts)) {
560 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200561 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200562 }
563 if ((ext->flags & LYS_YINELEM_MASK) ||
564 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_SIZE(ext->exts))) {
565 ypr_open(ctx->out, &flag2);
Radek Krejci693262f2019-04-29 15:23:20 +0200566 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200567 }
Radek Krejci38d2e9f2019-09-09 10:31:51 +0200568 LEVEL--;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200569 ypr_close(ctx, flag2);
570 }
571
Radek Krejci693262f2019-04-29 15:23:20 +0200572 ypr_status(ctx, ext->flags, ext->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200573 ypr_description(ctx, ext->dsc, ext->exts, &flag);
574 ypr_reference(ctx, ext->ref, ext->exts, &flag);
575
576 LEVEL--;
577 ypr_close(ctx, flag);
578}
579
580static void
Radek Krejci693262f2019-04-29 15:23:20 +0200581yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200582{
583 int flag = 0;
584
585 ly_print(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
586 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200587 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
588 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
589 ypr_status(ctx, feat->flags, feat->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200590 ypr_description(ctx, feat->dsc, feat->exts, &flag);
591 ypr_reference(ctx, feat->ref, feat->exts, &flag);
592 LEVEL--;
593 ypr_close(ctx, flag);
594}
595
596static void
Radek Krejci693262f2019-04-29 15:23:20 +0200597yprc_feature(struct ypr_ctx *ctx, const struct lysc_feature *feat)
598{
599 int flag = 0;
600
601 ly_print(ctx->out, "\n%*sfeature %s", INDENT, feat->name);
602 LEVEL++;
603 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
604 yprc_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
605 ypr_status(ctx, feat->flags, feat->exts, &flag);
606 ypr_description(ctx, feat->dsc, feat->exts, &flag);
607 ypr_reference(ctx, feat->ref, feat->exts, &flag);
608 LEVEL--;
609 ypr_close(ctx, flag);
610}
611
612static void
613yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200614{
615 int flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200616 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200617
618 ly_print(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
619 LEVEL++;
620
Radek Krejci693262f2019-04-29 15:23:20 +0200621 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
622 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200623
624 LY_ARRAY_FOR(ident->bases, u) {
625 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200626 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200627 }
628
Radek Krejci693262f2019-04-29 15:23:20 +0200629 ypr_status(ctx, ident->flags, ident->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200630 ypr_description(ctx, ident->dsc, ident->exts, &flag);
631 ypr_reference(ctx, ident->ref, ident->exts, &flag);
632
633 LEVEL--;
634 ypr_close(ctx, flag);
635}
636
637static void
Radek Krejci693262f2019-04-29 15:23:20 +0200638yprc_identity(struct ypr_ctx *ctx, const struct lysc_ident *ident)
639{
640 int flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200641 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200642
643 ly_print(ctx->out, "\n%*sidentity %s", INDENT, ident->name);
644 LEVEL++;
645
646 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
647 yprc_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
648
649 LY_ARRAY_FOR(ident->derived, u) {
650 ypr_open(ctx->out, &flag);
651 if (ctx->module != ident->derived[u]->module) {
652 ly_print(ctx->out, "%*sderived %s:%s;\n", INDENT, ident->derived[u]->module->prefix, ident->derived[u]->name);
653 } else {
654 ly_print(ctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name);
655 }
656 }
657
658 ypr_status(ctx, ident->flags, ident->exts, &flag);
659 ypr_description(ctx, ident->dsc, ident->exts, &flag);
660 ypr_reference(ctx, ident->ref, ident->exts, &flag);
661
662 LEVEL--;
663 ypr_close(ctx, flag);
664}
665
666static void
667yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200668{
669 int inner_flag = 0;
670
671 if (!restr) {
672 return;
673 }
674
675 ypr_open(ctx->out, flag);
676 ly_print(ctx->out, "%*s%s \"", INDENT, name);
677 ypr_encode(ctx->out, (restr->arg[0] != 0x15 && restr->arg[0] != 0x06) ? restr->arg : &restr->arg[1], -1);
678 ly_print(ctx->out, "\"");
679
680 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200681 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200682 if (restr->arg[0] == 0x15) {
683 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
684 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200685 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200686 }
687 if (restr->emsg) {
688 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200689 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200690 }
691 if (restr->eapptag) {
692 ypr_open(ctx->out, &inner_flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200693 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200694 }
Radek Krejci693262f2019-04-29 15:23:20 +0200695 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
696 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
697
Radek Krejcid3ca0632019-04-16 16:54:54 +0200698 LEVEL--;
699 ypr_close(ctx, inner_flag);
700}
701
702static void
Radek Krejci693262f2019-04-29 15:23:20 +0200703yprc_must(struct ypr_ctx *ctx, const struct lysc_must *must, int *flag)
704{
705 int inner_flag = 0;
706
707 ypr_open(ctx->out, flag);
708 ly_print(ctx->out, "%*smust \"", INDENT);
709 ypr_encode(ctx->out, must->cond->expr, -1);
710 ly_print(ctx->out, "\"");
711
712 LEVEL++;
713 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, must->exts, &inner_flag, 0);
714 if (must->emsg) {
715 ypr_open(ctx->out, &inner_flag);
716 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, must->emsg, must->exts);
717 }
718 if (must->eapptag) {
719 ypr_open(ctx->out, &inner_flag);
720 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, must->eapptag, must->exts);
721 }
722 ypr_description(ctx, must->dsc, must->exts, &inner_flag);
723 ypr_reference(ctx, must->ref, must->exts, &inner_flag);
724
725 LEVEL--;
726 ypr_close(ctx, inner_flag);
727}
728
729static void
730yprc_range(struct ypr_ctx *ctx, const struct lysc_range *range, LY_DATA_TYPE basetype, int *flag)
731{
732 int inner_flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200733 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200734
Radek Krejci334ccc72019-06-12 13:49:29 +0200735 if (!range) {
736 return;
737 }
738
Radek Krejci693262f2019-04-29 15:23:20 +0200739 ypr_open(ctx->out, flag);
Radek Krejci334ccc72019-06-12 13:49:29 +0200740 ly_print(ctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range");
Radek Krejci693262f2019-04-29 15:23:20 +0200741 LY_ARRAY_FOR(range->parts, u) {
742 if (u > 0) {
743 ly_print(ctx->out, " | ");
744 }
745 if (range->parts[u].max_64 == range->parts[u].min_64) {
746 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
747 ly_print(ctx->out, "%"PRIu64, range->parts[u].max_u64);
748 } else { /* signed values */
749 ly_print(ctx->out, "%"PRId64, range->parts[u].max_64);
750 }
751 } else {
752 if (basetype <= LY_TYPE_STRING) { /* unsigned values */
753 ly_print(ctx->out, "%"PRIu64"..%"PRIu64, range->parts[u].min_u64, range->parts[u].max_u64);
754 } else { /* signed values */
755 ly_print(ctx->out, "%"PRId64"..%"PRId64, range->parts[u].min_64, range->parts[u].max_64);
756 }
757 }
758 }
759 ly_print(ctx->out, "\"");
760
761 LEVEL++;
762 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, range->exts, &inner_flag, 0);
763 if (range->emsg) {
764 ypr_open(ctx->out, &inner_flag);
765 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, range->emsg, range->exts);
766 }
767 if (range->eapptag) {
768 ypr_open(ctx->out, &inner_flag);
769 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, range->eapptag, range->exts);
770 }
771 ypr_description(ctx, range->dsc, range->exts, &inner_flag);
772 ypr_reference(ctx, range->ref, range->exts, &inner_flag);
773
774 LEVEL--;
775 ypr_close(ctx, inner_flag);
776}
777
778static void
779yprc_pattern(struct ypr_ctx *ctx, const struct lysc_pattern *pattern, int *flag)
780{
781 int inner_flag = 0;
782
783 ypr_open(ctx->out, flag);
784 ly_print(ctx->out, "%*spattern \"", INDENT);
Radek Krejci54579462019-04-30 12:47:06 +0200785 ypr_encode(ctx->out, pattern->expr, -1);
Radek Krejci693262f2019-04-29 15:23:20 +0200786 ly_print(ctx->out, "\"");
787
788 LEVEL++;
789 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, pattern->exts, &inner_flag, 0);
790 if (pattern->inverted) {
791 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
792 ypr_open(ctx->out, &inner_flag);
793 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", pattern->exts);
794 }
795 if (pattern->emsg) {
796 ypr_open(ctx->out, &inner_flag);
797 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, pattern->emsg, pattern->exts);
798 }
799 if (pattern->eapptag) {
800 ypr_open(ctx->out, &inner_flag);
801 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, pattern->eapptag, pattern->exts);
802 }
803 ypr_description(ctx, pattern->dsc, pattern->exts, &inner_flag);
804 ypr_reference(ctx, pattern->ref, pattern->exts, &inner_flag);
805
806 LEVEL--;
807 ypr_close(ctx, inner_flag);
808}
809
810static void
811yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200812{
813 int inner_flag = 0;
814
815 if (!when) {
816 return;
817 }
818 ypr_open(ctx->out, flag);
819
820 ly_print(ctx->out, "%*swhen \"", INDENT);
821 ypr_encode(ctx->out, when->cond, -1);
822 ly_print(ctx->out, "\"");
823
824 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200825 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200826 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
827 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
828 LEVEL--;
829 ypr_close(ctx, inner_flag);
830}
831
832static void
Radek Krejci693262f2019-04-29 15:23:20 +0200833yprc_when(struct ypr_ctx *ctx, struct lysc_when *when, int *flag)
834{
835 int inner_flag = 0;
836
837 if (!when) {
838 return;
839 }
840 ypr_open(ctx->out, flag);
841
842 ly_print(ctx->out, "%*swhen \"", INDENT);
843 ypr_encode(ctx->out, when->cond->expr, -1);
844 ly_print(ctx->out, "\"");
845
846 LEVEL++;
847 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
848 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
849 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
850 LEVEL--;
851 ypr_close(ctx, inner_flag);
852}
853
854static void
855yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200856{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200857 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200858 int inner_flag;
859
860 LY_ARRAY_FOR(items, u) {
861 ypr_open(ctx->out, flag);
Radek Krejci7871ce52019-06-11 16:44:56 +0200862 if (type == LY_TYPE_BITS) {
863 ly_print(ctx->out, "%*sbit %s", INDENT, items[u].name);
864 } else { /* LY_TYPE_ENUM */
865 ly_print(ctx->out, "%*senum \"", INDENT);
866 ypr_encode(ctx->out, items[u].name, -1);
867 ly_print(ctx->out, "\"");
868 }
Radek Krejcid3ca0632019-04-16 16:54:54 +0200869 inner_flag = 0;
870 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +0200871 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
872 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200873 if (items[u].flags & LYS_SET_VALUE) {
874 if (type == LY_TYPE_BITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200875 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200876 } else { /* LY_TYPE_ENUM */
Radek Krejci693262f2019-04-29 15:23:20 +0200877 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200878 }
879 }
Radek Krejci693262f2019-04-29 15:23:20 +0200880 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200881 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
882 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
883 LEVEL--;
884 ypr_close(ctx, inner_flag);
885 }
886}
887
888static void
Radek Krejci693262f2019-04-29 15:23:20 +0200889yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200890{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200891 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200892 int flag = 0;
893
894 ly_print(ctx->out, "%*stype %s", INDENT, type->name);
895 LEVEL++;
896
Radek Krejci693262f2019-04-29 15:23:20 +0200897 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200898
Radek Krejci693262f2019-04-29 15:23:20 +0200899 yprp_restr(ctx, type->range, "range", &flag);
900 yprp_restr(ctx, type->length, "length", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200901 LY_ARRAY_FOR(type->patterns, u) {
Radek Krejci693262f2019-04-29 15:23:20 +0200902 yprp_restr(ctx, &type->patterns[u], "pattern", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200903 }
Radek Krejci693262f2019-04-29 15:23:20 +0200904 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
905 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200906
907 if (type->path) {
908 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200909 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path, type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200910 }
911 if (type->flags & LYS_SET_REQINST) {
912 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200913 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200914 }
915 if (type->flags & LYS_SET_FRDIGITS) {
Radek Krejci693262f2019-04-29 15:23:20 +0200916 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200917 }
918 LY_ARRAY_FOR(type->bases, u) {
919 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200920 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200921 }
922 LY_ARRAY_FOR(type->types, u) {
923 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +0200924 yprp_type(ctx, &type->types[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200925 }
926
927 LEVEL--;
928 ypr_close(ctx, flag);
929}
930
931static void
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200932yprc_dflt_value(struct ypr_ctx *ctx, const struct lyd_value *value, const struct lys_module *value_mod, struct lysc_ext_instance *exts)
Radek Krejcia1911222019-07-22 17:24:50 +0200933{
934 int dynamic;
935 const char *str;
936
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200937 str = value->realtype->plugin->print(value, LYD_JSON, lys_get_prefix, (void*)value_mod, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +0200938 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, str, exts);
939 if (dynamic) {
940 free((void*)str);
941 }
942}
943
944static void
Radek Krejci693262f2019-04-29 15:23:20 +0200945yprc_type(struct ypr_ctx *ctx, const struct lysc_type *type)
946{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200947 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +0200948 int flag = 0;
949
950 ly_print(ctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype));
951 LEVEL++;
952
953 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
954 if (type->dflt) {
955 ypr_open(ctx->out, &flag);
Radek Krejcid0ef1af2019-07-23 12:22:05 +0200956 yprc_dflt_value(ctx, type->dflt, type->dflt_mod, type->exts);
Radek Krejci693262f2019-04-29 15:23:20 +0200957 }
958
959 switch(type->basetype) {
960 case LY_TYPE_BINARY: {
961 struct lysc_type_bin *bin = (struct lysc_type_bin*)type;
962 yprc_range(ctx, bin->length, type->basetype, &flag);
963 break;
964 }
965 case LY_TYPE_UINT8:
966 case LY_TYPE_UINT16:
967 case LY_TYPE_UINT32:
968 case LY_TYPE_UINT64:
969 case LY_TYPE_INT8:
970 case LY_TYPE_INT16:
971 case LY_TYPE_INT32:
972 case LY_TYPE_INT64: {
973 struct lysc_type_num *num = (struct lysc_type_num*)type;
974 yprc_range(ctx, num->range, type->basetype, &flag);
975 break;
976 }
977 case LY_TYPE_STRING: {
978 struct lysc_type_str *str = (struct lysc_type_str*)type;
979 yprc_range(ctx, str->length, type->basetype, &flag);
980 LY_ARRAY_FOR(str->patterns, u) {
981 yprc_pattern(ctx, str->patterns[u], &flag);
982 }
983 break;
984 }
985 case LY_TYPE_BITS:
986 case LY_TYPE_ENUM: {
987 /* bits and enums structures are compatible */
988 struct lysc_type_bits *bits = (struct lysc_type_bits*)type;
989 LY_ARRAY_FOR(bits->bits, u) {
990 struct lysc_type_bitenum_item *item = &bits->bits[u];
991 int inner_flag = 0;
992
993 ypr_open(ctx->out, &flag);
994 ly_print(ctx->out, "%*s%s \"", INDENT, type->basetype == LY_TYPE_BITS ? "bit" : "enum");
995 ypr_encode(ctx->out, item->name, -1);
996 ly_print(ctx->out, "\"");
997 LEVEL++;
998 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, item->exts, &inner_flag, 0);
999 yprc_iffeatures(ctx, item->iffeatures, item->exts, &inner_flag);
1000 if (type->basetype == LY_TYPE_BITS) {
1001 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, item->exts, item->position, &inner_flag);
1002 } else { /* LY_TYPE_ENUM */
1003 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, item->exts, item->value, &inner_flag);
1004 }
1005 ypr_status(ctx, item->flags, item->exts, &inner_flag);
1006 ypr_description(ctx, item->dsc, item->exts, &inner_flag);
1007 ypr_reference(ctx, item->ref, item->exts, &inner_flag);
1008 LEVEL--;
1009 ypr_close(ctx, inner_flag);
1010 }
1011 break;
1012 }
1013 case LY_TYPE_BOOL:
1014 case LY_TYPE_EMPTY:
1015 /* nothing to do */
1016 break;
1017 case LY_TYPE_DEC64: {
1018 struct lysc_type_dec *dec = (struct lysc_type_dec*)type;
1019 ypr_open(ctx->out, &flag);
1020 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, dec->fraction_digits, &flag);
1021 yprc_range(ctx, dec->range, dec->basetype, &flag);
1022 break;
1023 }
1024 case LY_TYPE_IDENT: {
1025 struct lysc_type_identityref *ident = (struct lysc_type_identityref*)type;
1026 LY_ARRAY_FOR(ident->bases, u) {
1027 ypr_open(ctx->out, &flag);
1028 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u]->name, type->exts);
1029 }
1030 break;
1031 }
1032 case LY_TYPE_INST: {
1033 struct lysc_type_instanceid *inst = (struct lysc_type_instanceid*)type;
1034 ypr_open(ctx->out, &flag);
1035 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts);
1036 break;
1037 }
1038 case LY_TYPE_LEAFREF: {
1039 struct lysc_type_leafref *lr = (struct lysc_type_leafref*)type;
1040 ypr_open(ctx->out, &flag);
1041 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, lr->path, lr->exts);
1042 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts);
1043 yprc_type(ctx, lr->realtype);
1044 break;
1045 }
1046 case LY_TYPE_UNION: {
1047 struct lysc_type_union *un = (struct lysc_type_union*)type;
1048 LY_ARRAY_FOR(un->types, u) {
1049 ypr_open(ctx->out, &flag);
1050 yprc_type(ctx, un->types[u]);
1051 }
1052 break;
1053 }
1054 default:
1055 LOGINT(ctx->module->ctx);
Radek Krejci56cc0872019-04-30 09:22:27 +02001056 ctx->out->status = LY_EINT;
Radek Krejci693262f2019-04-29 15:23:20 +02001057 }
1058
1059 LEVEL--;
1060 ypr_close(ctx, flag);
1061}
1062
1063static void
1064yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001065{
Radek Krejci56cc0872019-04-30 09:22:27 +02001066 LYOUT_CHECK(ctx->out);
1067
Radek Krejcid3ca0632019-04-16 16:54:54 +02001068 ly_print(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
1069 LEVEL++;
1070
Radek Krejci693262f2019-04-29 15:23:20 +02001071 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001072
Radek Krejci693262f2019-04-29 15:23:20 +02001073 yprp_type(ctx, &tpdf->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001074
1075 if (tpdf->units) {
Radek Krejci693262f2019-04-29 15:23:20 +02001076 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001077 }
1078 if (tpdf->dflt) {
Radek Krejci693262f2019-04-29 15:23:20 +02001079 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001080 }
1081
Radek Krejci693262f2019-04-29 15:23:20 +02001082 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001083 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
1084 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
1085
1086 LEVEL--;
1087 ly_print(ctx->out, "%*s}\n", INDENT);
1088}
1089
Radek Krejci693262f2019-04-29 15:23:20 +02001090static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
1091static void yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node);
1092static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001093
1094static void
Radek Krejci693262f2019-04-29 15:23:20 +02001095yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001096{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001097 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001098 int flag = 0;
1099 struct lysp_node *data;
1100
Radek Krejci56cc0872019-04-30 09:22:27 +02001101 LYOUT_CHECK(ctx->out);
1102
Radek Krejcid3ca0632019-04-16 16:54:54 +02001103 ly_print(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
1104 LEVEL++;
1105
Radek Krejci693262f2019-04-29 15:23:20 +02001106 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
1107 ypr_status(ctx, grp->flags, grp->exts, &flag);
Radek Krejcifc81ea82019-04-18 13:27:22 +02001108 ypr_description(ctx, grp->dsc, grp->exts, &flag);
1109 ypr_reference(ctx, grp->ref, grp->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001110
1111 LY_ARRAY_FOR(grp->typedefs, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001112 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001113 yprp_typedef(ctx, &grp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001114 }
1115
1116 LY_ARRAY_FOR(grp->groupings, u) {
Radek Krejci59edcf72019-05-02 09:53:17 +02001117 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001118 yprp_grouping(ctx, &grp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001119 }
1120
1121 LY_LIST_FOR(grp->data, data) {
Radek Krejcifc81ea82019-04-18 13:27:22 +02001122 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001123 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001124 }
1125
1126 LY_ARRAY_FOR(grp->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001127 yprp_action(ctx, &grp->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001128 }
1129
1130 LEVEL--;
1131 ypr_close(ctx, flag);
1132}
1133
1134static void
Radek Krejci693262f2019-04-29 15:23:20 +02001135yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001136{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001137 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001138 struct lysp_node *data;
1139
1140 if (!inout->nodetype) {
1141 /* nodetype not set -> input/output is empty */
1142 return;
1143 }
1144 ypr_open(ctx->out, flag);
1145
1146 ly_print(ctx->out, "\n%*s%s {\n", INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
1147 LEVEL++;
1148
Radek Krejci693262f2019-04-29 15:23:20 +02001149 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001150 LY_ARRAY_FOR(inout->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001151 yprp_restr(ctx, &inout->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001152 }
1153 LY_ARRAY_FOR(inout->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001154 yprp_typedef(ctx, &inout->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001155 }
1156 LY_ARRAY_FOR(inout->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001157 yprp_grouping(ctx, &inout->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001158 }
1159
1160 LY_LIST_FOR(inout->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02001161 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001162 }
1163
1164 LEVEL--;
1165 ypr_close(ctx, 1);
1166}
1167
1168static void
Radek Krejci693262f2019-04-29 15:23:20 +02001169yprc_inout(struct ypr_ctx *ctx, const struct lysc_action *action, const struct lysc_action_inout *inout, int *flag)
1170{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001171 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001172 struct lysc_node *data;
1173
1174 if (!inout->data) {
1175 /* input/output is empty */
1176 return;
1177 }
1178 ypr_open(ctx->out, flag);
1179
1180 ly_print(ctx->out, "\n%*s%s {\n", INDENT, (&action->input == inout) ? "input" : "output");
1181 LEVEL++;
1182
1183 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, (&action->input == inout) ? action->input_exts : action->output_exts, NULL, 0);
1184 LY_ARRAY_FOR(inout->musts, u) {
1185 yprc_must(ctx, &inout->musts[u], NULL);
1186 }
1187
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001188 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001189 LY_LIST_FOR(inout->data, data) {
1190 yprc_node(ctx, data);
1191 }
Radek Krejci693262f2019-04-29 15:23:20 +02001192 }
1193
1194 LEVEL--;
1195 ypr_close(ctx, 1);
1196}
1197
1198static void
1199yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001200{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001201 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001202 int flag = 0;
1203 struct lysp_node *data;
1204
Radek Krejci56cc0872019-04-30 09:22:27 +02001205 LYOUT_CHECK(ctx->out);
1206
Radek Krejcid3ca0632019-04-16 16:54:54 +02001207 ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
1208
1209 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001210 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1211 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001212
1213 LY_ARRAY_FOR(notif->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001214 yprp_restr(ctx, &notif->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001215 }
Radek Krejci693262f2019-04-29 15:23:20 +02001216 ypr_status(ctx, notif->flags, notif->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001217 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1218 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1219
1220 LY_ARRAY_FOR(notif->typedefs, u) {
1221 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001222 yprp_typedef(ctx, &notif->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001223 }
1224
1225 LY_ARRAY_FOR(notif->groupings, u) {
1226 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001227 yprp_grouping(ctx, &notif->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001228 }
1229
1230 LY_LIST_FOR(notif->data, data) {
1231 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001232 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001233 }
1234
1235 LEVEL--;
1236 ypr_close(ctx, flag);
1237}
1238
1239static void
Radek Krejci693262f2019-04-29 15:23:20 +02001240yprc_notification(struct ypr_ctx *ctx, const struct lysc_notif *notif)
1241{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001242 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001243 int flag = 0;
1244 struct lysc_node *data;
1245
Radek Krejci56cc0872019-04-30 09:22:27 +02001246 LYOUT_CHECK(ctx->out);
1247
Radek Krejci693262f2019-04-29 15:23:20 +02001248 ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
1249
1250 LEVEL++;
1251 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
1252 yprc_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
1253
1254 LY_ARRAY_FOR(notif->musts, u) {
1255 yprc_must(ctx, &notif->musts[u], &flag);
1256 }
1257 ypr_status(ctx, notif->flags, notif->exts, &flag);
1258 ypr_description(ctx, notif->dsc, notif->exts, &flag);
1259 ypr_reference(ctx, notif->ref, notif->exts, &flag);
1260
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001261 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001262 LY_LIST_FOR(notif->data, data) {
1263 ypr_open(ctx->out, &flag);
1264 yprc_node(ctx, data);
1265 }
Radek Krejci693262f2019-04-29 15:23:20 +02001266 }
1267
1268 LEVEL--;
1269 ypr_close(ctx, flag);
1270}
1271
1272static void
1273yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001274{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001275 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001276 int flag = 0;
1277
Radek Krejci56cc0872019-04-30 09:22:27 +02001278 LYOUT_CHECK(ctx->out);
1279
Radek Krejcid3ca0632019-04-16 16:54:54 +02001280 ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
1281
1282 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02001283 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1284 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1285 ypr_status(ctx, action->flags, action->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001286 ypr_description(ctx, action->dsc, action->exts, &flag);
1287 ypr_reference(ctx, action->ref, action->exts, &flag);
1288
1289 LY_ARRAY_FOR(action->typedefs, u) {
1290 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001291 yprp_typedef(ctx, &action->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001292 }
1293
1294 LY_ARRAY_FOR(action->groupings, u) {
1295 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001296 yprp_grouping(ctx, &action->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001297 }
1298
Radek Krejci693262f2019-04-29 15:23:20 +02001299 yprp_inout(ctx, &action->input, &flag);
1300 yprp_inout(ctx, &action->output, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001301
1302 LEVEL--;
1303 ypr_close(ctx, flag);
1304}
1305
1306static void
Radek Krejci693262f2019-04-29 15:23:20 +02001307yprc_action(struct ypr_ctx *ctx, const struct lysc_action *action)
1308{
1309 int flag = 0;
1310
Radek Krejci56cc0872019-04-30 09:22:27 +02001311 LYOUT_CHECK(ctx->out);
1312
Radek Krejci693262f2019-04-29 15:23:20 +02001313 ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
1314
1315 LEVEL++;
1316 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
1317 yprc_iffeatures(ctx, action->iffeatures, action->exts, &flag);
1318 ypr_status(ctx, action->flags, action->exts, &flag);
1319 ypr_description(ctx, action->dsc, action->exts, &flag);
1320 ypr_reference(ctx, action->ref, action->exts, &flag);
1321
1322 yprc_inout(ctx, action, &action->input, &flag);
1323 yprc_inout(ctx, action, &action->output, &flag);
1324
1325 LEVEL--;
1326 ypr_close(ctx, flag);
1327}
1328
1329static void
1330yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001331{
Radek Krejci7871ce52019-06-11 16:44:56 +02001332 ly_print(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejcid3ca0632019-04-16 16:54:54 +02001333 LEVEL++;
1334
Radek Krejci693262f2019-04-29 15:23:20 +02001335 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1336 yprp_when(ctx, node->when, flag);
1337 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001338}
1339
1340static void
Radek Krejci693262f2019-04-29 15:23:20 +02001341yprc_node_common1(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001342{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001343 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001344
Radek Krejci42903ea2019-06-14 16:24:56 +02001345 ly_print(ctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001346 LEVEL++;
1347
1348 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
1349 LY_ARRAY_FOR(node->when, u) {
1350 yprc_when(ctx, node->when[u], flag);
1351 }
1352 yprc_iffeatures(ctx, node->iffeatures, node->exts, flag);
1353}
1354
1355/* macr oto unify the code */
1356#define YPR_NODE_COMMON2 \
1357 ypr_config(ctx, node->flags, node->exts, flag); \
1358 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \
1359 ypr_mandatory(ctx, node->flags, node->exts, flag); \
1360 } \
1361 ypr_status(ctx, node->flags, node->exts, flag); \
1362 ypr_description(ctx, node->dsc, node->exts, flag); \
1363 ypr_reference(ctx, node->ref, node->exts, flag)
1364
1365static void
1366yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
1367{
1368 YPR_NODE_COMMON2;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001369}
1370
1371static void
Radek Krejci693262f2019-04-29 15:23:20 +02001372yprc_node_common2(struct ypr_ctx *ctx, const struct lysc_node *node, int *flag)
1373{
1374 YPR_NODE_COMMON2;
1375}
1376
1377#undef YPR_NODE_COMMON2
1378
1379static void
1380yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001381{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001382 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001383 int flag = 0;
1384 struct lysp_node *child;
1385 struct lysp_node_container *cont = (struct lysp_node_container *)node;
1386
Radek Krejci693262f2019-04-29 15:23:20 +02001387 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001388
1389 LY_ARRAY_FOR(cont->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001390 yprp_restr(ctx, &cont->musts[u], "must", &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001391 }
1392 if (cont->presence) {
1393 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001394 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001395 }
1396
Radek Krejci693262f2019-04-29 15:23:20 +02001397 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001398
1399 LY_ARRAY_FOR(cont->typedefs, u) {
1400 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001401 yprp_typedef(ctx, &cont->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001402 }
1403
1404 LY_ARRAY_FOR(cont->groupings, u) {
1405 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001406 yprp_grouping(ctx, &cont->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001407 }
1408
1409 LY_LIST_FOR(cont->child, child) {
1410 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001411 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001412 }
1413
1414 LY_ARRAY_FOR(cont->actions, u) {
1415 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001416 yprp_action(ctx, &cont->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001417 }
1418
1419 LY_ARRAY_FOR(cont->notifs, u) {
1420 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001421 yprp_notification(ctx, &cont->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001422 }
1423
1424 LEVEL--;
1425 ypr_close(ctx, flag);
1426}
1427
1428static void
Radek Krejci693262f2019-04-29 15:23:20 +02001429yprc_container(struct ypr_ctx *ctx, const struct lysc_node *node)
1430{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001431 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001432 int flag = 0;
1433 struct lysc_node *child;
1434 struct lysc_node_container *cont = (struct lysc_node_container *)node;
1435
1436 yprc_node_common1(ctx, node, &flag);
1437
1438 LY_ARRAY_FOR(cont->musts, u) {
1439 yprc_must(ctx, &cont->musts[u], &flag);
1440 }
1441 if (cont->flags & LYS_PRESENCE) {
1442 ypr_open(ctx->out, &flag);
1443 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, "true", cont->exts);
1444 }
1445
1446 yprc_node_common2(ctx, node, &flag);
1447
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001448 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001449 LY_LIST_FOR(cont->child, child) {
1450 ypr_open(ctx->out, &flag);
1451 yprc_node(ctx, child);
1452 }
Radek Krejci693262f2019-04-29 15:23:20 +02001453
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001454 LY_ARRAY_FOR(cont->actions, u) {
1455 ypr_open(ctx->out, &flag);
1456 yprc_action(ctx, &cont->actions[u]);
1457 }
Radek Krejci693262f2019-04-29 15:23:20 +02001458
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001459 LY_ARRAY_FOR(cont->notifs, u) {
1460 ypr_open(ctx->out, &flag);
1461 yprc_notification(ctx, &cont->notifs[u]);
1462 }
Radek Krejci693262f2019-04-29 15:23:20 +02001463 }
1464
1465 LEVEL--;
1466 ypr_close(ctx, flag);
1467}
1468
1469static void
1470yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
1471{
1472 int flag = 0;
1473 struct lysp_node *child;
1474 struct lysp_node_case *cas = (struct lysp_node_case *)node;
1475
1476 yprp_node_common1(ctx, node, &flag);
1477 yprp_node_common2(ctx, node, &flag);
1478
1479 LY_LIST_FOR(cas->child, child) {
1480 ypr_open(ctx->out, &flag);
1481 yprp_node(ctx, child);
1482 }
1483
1484 LEVEL--;
1485 ypr_close(ctx, flag);
1486}
1487
1488static void
1489yprc_case(struct ypr_ctx *ctx, const struct lysc_node_case *cs)
1490{
1491 int flag = 0;
1492 struct lysc_node *child;
1493
1494 yprc_node_common1(ctx, (struct lysc_node*)cs, &flag);
1495 yprc_node_common2(ctx, (struct lysc_node*)cs, &flag);
1496
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001497 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001498 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
1499 ypr_open(ctx->out, &flag);
1500 yprc_node(ctx, child);
1501 }
Radek Krejci693262f2019-04-29 15:23:20 +02001502 }
1503
1504 LEVEL--;
1505 ypr_close(ctx, flag);
1506}
1507
1508static void
1509yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001510{
1511 int flag = 0;
1512 struct lysp_node *child;
1513 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
1514
Radek Krejci693262f2019-04-29 15:23:20 +02001515 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001516
1517 if (choice->dflt) {
1518 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001519 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001520 }
1521
Radek Krejci693262f2019-04-29 15:23:20 +02001522 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001523
1524 LY_LIST_FOR(choice->child, child) {
1525 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001526 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001527 }
1528
1529 LEVEL--;
1530 ypr_close(ctx, flag);
1531}
1532
1533static void
Radek Krejci693262f2019-04-29 15:23:20 +02001534yprc_choice(struct ypr_ctx *ctx, const struct lysc_node *node)
1535{
1536 int flag = 0;
1537 struct lysc_node_case *cs;
1538 struct lysc_node_choice *choice = (struct lysc_node_choice *)node;
1539
1540 yprc_node_common1(ctx, node, &flag);
1541
1542 if (choice->dflt) {
1543 ypr_open(ctx->out, &flag);
1544 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt->name, choice->exts);
1545 }
1546
1547 yprc_node_common2(ctx, node, &flag);
1548
1549 for (cs = choice->cases; cs; cs = (struct lysc_node_case*)cs->next) {
1550 ypr_open(ctx->out, &flag);
1551 yprc_case(ctx, cs);
1552 }
1553
1554 LEVEL--;
1555 ypr_close(ctx, flag);
1556}
1557
1558static void
1559yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001560{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001561 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001562 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
1563
Radek Krejci693262f2019-04-29 15:23:20 +02001564 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001565
Radek Krejci693262f2019-04-29 15:23:20 +02001566 yprp_type(ctx, &leaf->type);
1567 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001568 LY_ARRAY_FOR(leaf->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001569 yprp_restr(ctx, &leaf->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001570 }
Radek Krejci693262f2019-04-29 15:23:20 +02001571 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001572
Radek Krejci693262f2019-04-29 15:23:20 +02001573 yprp_node_common2(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001574
1575 LEVEL--;
1576 ly_print(ctx->out, "%*s}\n", INDENT);
1577}
1578
1579static void
Radek Krejci693262f2019-04-29 15:23:20 +02001580yprc_leaf(struct ypr_ctx *ctx, const struct lysc_node *node)
1581{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001582 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001583 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
1584
1585 yprc_node_common1(ctx, node, NULL);
1586
1587 yprc_type(ctx, leaf->type);
1588 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
1589 LY_ARRAY_FOR(leaf->musts, u) {
1590 yprc_must(ctx, &leaf->musts[u], NULL);
1591 }
Radek Krejcia1911222019-07-22 17:24:50 +02001592
1593 if (leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001594 yprc_dflt_value(ctx, leaf->dflt, leaf->dflt_mod, leaf->exts);
Radek Krejcia1911222019-07-22 17:24:50 +02001595 }
Radek Krejci693262f2019-04-29 15:23:20 +02001596
1597 yprc_node_common2(ctx, node, NULL);
1598
1599 LEVEL--;
1600 ly_print(ctx->out, "%*s}\n", INDENT);
1601}
1602
1603static void
1604yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001605{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001606 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001607 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
1608
Radek Krejci693262f2019-04-29 15:23:20 +02001609 yprp_node_common1(ctx, node, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001610
Radek Krejci693262f2019-04-29 15:23:20 +02001611 yprp_type(ctx, &llist->type);
1612 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001613 LY_ARRAY_FOR(llist->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001614 yprp_restr(ctx, &llist->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001615 }
1616 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001617 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001618 }
1619
Radek Krejci693262f2019-04-29 15:23:20 +02001620 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001621
1622 if (llist->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001623 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001624 }
1625 if (llist->flags & LYS_SET_MAX) {
1626 if (llist->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001627 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001628 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001629 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001630 }
1631 }
1632
1633 if (llist->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001634 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001635 }
1636
Radek Krejci693262f2019-04-29 15:23:20 +02001637 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001638 ypr_description(ctx, node->dsc, node->exts, NULL);
1639 ypr_reference(ctx, node->ref, node->exts, NULL);
1640
1641 LEVEL--;
1642 ly_print(ctx->out, "%*s}\n", INDENT);
1643}
1644
1645static void
Radek Krejci693262f2019-04-29 15:23:20 +02001646yprc_leaflist(struct ypr_ctx *ctx, const struct lysc_node *node)
1647{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001648 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02001649 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
1650
1651 yprc_node_common1(ctx, node, NULL);
1652
1653 yprc_type(ctx, llist->type);
1654 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
1655 LY_ARRAY_FOR(llist->musts, u) {
1656 yprc_must(ctx, &llist->musts[u], NULL);
1657 }
1658 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02001659 yprc_dflt_value(ctx, llist->dflts[u], llist->dflts_mods[u], llist->exts);
Radek Krejci693262f2019-04-29 15:23:20 +02001660 }
1661
1662 ypr_config(ctx, node->flags, node->exts, NULL);
1663
1664 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min, NULL);
1665 if (llist->max) {
1666 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max, NULL);
1667 } else {
1668 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
1669 }
1670
1671 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
1672
1673 ypr_status(ctx, node->flags, node->exts, NULL);
1674 ypr_description(ctx, node->dsc, node->exts, NULL);
1675 ypr_reference(ctx, node->ref, node->exts, NULL);
1676
1677 LEVEL--;
1678 ly_print(ctx->out, "%*s}\n", INDENT);
1679}
1680
1681static void
1682yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001683{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001684 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001685 int flag = 0;
1686 struct lysp_node *child;
1687 struct lysp_node_list *list = (struct lysp_node_list *)node;
1688
Radek Krejci693262f2019-04-29 15:23:20 +02001689 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001690
1691 LY_ARRAY_FOR(list->musts, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001692 yprp_restr(ctx, &list->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001693 }
1694 if (list->key) {
1695 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001696 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001697 }
1698 LY_ARRAY_FOR(list->uniques, u) {
1699 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001700 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001701 }
1702
Radek Krejci693262f2019-04-29 15:23:20 +02001703 ypr_config(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001704
1705 if (list->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02001706 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001707 }
1708 if (list->flags & LYS_SET_MAX) {
1709 if (list->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001710 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001711 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001712 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001713 }
1714 }
1715
1716 if (list->flags & LYS_ORDBY_MASK) {
Radek Krejci693262f2019-04-29 15:23:20 +02001717 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001718 }
1719
Radek Krejci693262f2019-04-29 15:23:20 +02001720 ypr_status(ctx, node->flags, node->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001721 ypr_description(ctx, node->dsc, node->exts, NULL);
1722 ypr_reference(ctx, node->ref, node->exts, NULL);
1723
1724 LY_ARRAY_FOR(list->typedefs, u) {
1725 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001726 yprp_typedef(ctx, &list->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001727 }
1728
1729 LY_ARRAY_FOR(list->groupings, u) {
1730 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001731 yprp_grouping(ctx, &list->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001732 }
1733
1734 LY_LIST_FOR(list->child, child) {
1735 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001736 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001737 }
1738
1739 LY_ARRAY_FOR(list->actions, u) {
1740 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001741 yprp_action(ctx, &list->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001742 }
1743
1744 LY_ARRAY_FOR(list->notifs, u) {
1745 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001746 yprp_notification(ctx, &list->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001747 }
1748
1749 LEVEL--;
1750 ypr_close(ctx, flag);
1751}
1752
1753static void
Radek Krejci693262f2019-04-29 15:23:20 +02001754yprc_list(struct ypr_ctx *ctx, const struct lysc_node *node)
1755{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001756 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci693262f2019-04-29 15:23:20 +02001757 int flag = 0;
1758 struct lysc_node *child;
1759 struct lysc_node_list *list = (struct lysc_node_list *)node;
1760
1761 yprc_node_common1(ctx, node, &flag);
1762
1763 LY_ARRAY_FOR(list->musts, u) {
1764 yprc_must(ctx, &list->musts[u], NULL);
1765 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02001766 if (!(list->flags & LYS_KEYLESS)) {
Radek Krejci693262f2019-04-29 15:23:20 +02001767 ypr_open(ctx->out, &flag);
1768 ly_print(ctx->out, "%*skey \"", INDENT);
Radek Krejci0fe9b512019-07-26 17:51:05 +02001769 for (struct lysc_node *key = list->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
1770 ly_print(ctx->out, "%s%s", u > 0 ? ", " : "", key->name);
Radek Krejci693262f2019-04-29 15:23:20 +02001771 }
Radek Krejci73c88f52019-06-14 16:24:19 +02001772 ly_print(ctx->out, "\";\n");
Radek Krejci693262f2019-04-29 15:23:20 +02001773 }
1774 LY_ARRAY_FOR(list->uniques, u) {
1775 ypr_open(ctx->out, &flag);
1776 ly_print(ctx->out, "%*sunique \"", INDENT);
1777 LY_ARRAY_FOR(list->uniques[u], v) {
1778 ly_print(ctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name);
1779 }
1780 ypr_close(ctx, 0);
1781 }
1782
1783 ypr_config(ctx, node->flags, node->exts, NULL);
1784
1785 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min, NULL);
1786 if (list->max) {
1787 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max, NULL);
1788 } else {
1789 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
1790 }
1791
1792 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
1793
1794 ypr_status(ctx, node->flags, node->exts, NULL);
1795 ypr_description(ctx, node->dsc, node->exts, NULL);
1796 ypr_reference(ctx, node->ref, node->exts, NULL);
1797
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08001798 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001799 LY_LIST_FOR(list->child, child) {
1800 ypr_open(ctx->out, &flag);
1801 yprc_node(ctx, child);
1802 }
Radek Krejci693262f2019-04-29 15:23:20 +02001803
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001804 LY_ARRAY_FOR(list->actions, u) {
1805 ypr_open(ctx->out, &flag);
1806 yprc_action(ctx, &list->actions[u]);
1807 }
Radek Krejci693262f2019-04-29 15:23:20 +02001808
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08001809 LY_ARRAY_FOR(list->notifs, u) {
1810 ypr_open(ctx->out, &flag);
1811 yprc_notification(ctx, &list->notifs[u]);
1812 }
Radek Krejci693262f2019-04-29 15:23:20 +02001813 }
1814
1815 LEVEL--;
1816 ypr_close(ctx, flag);
1817}
1818
1819static void
1820yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001821{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001822 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001823 int flag = 0;
1824
1825 ly_print(ctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid);
1826 LEVEL++;
1827
Radek Krejci693262f2019-04-29 15:23:20 +02001828 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
1829 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001830
1831 LY_ARRAY_FOR(refine->musts, u) {
1832 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001833 yprp_restr(ctx, &refine->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001834 }
1835
1836 if (refine->presence) {
1837 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001838 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001839 }
1840
1841 LY_ARRAY_FOR(refine->dflts, u) {
1842 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001843 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001844 }
1845
Radek Krejci693262f2019-04-29 15:23:20 +02001846 ypr_config(ctx, refine->flags, refine->exts, &flag);
1847 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001848
1849 if (refine->flags & LYS_SET_MIN) {
1850 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001851 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001852 }
1853 if (refine->flags & LYS_SET_MAX) {
1854 ypr_open(ctx->out, &flag);
1855 if (refine->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02001856 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001857 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02001858 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001859 }
1860 }
1861
1862 ypr_description(ctx, refine->dsc, refine->exts, &flag);
1863 ypr_reference(ctx, refine->ref, refine->exts, &flag);
1864
1865 LEVEL--;
1866 ypr_close(ctx, flag);
1867}
1868
1869static void
Radek Krejci693262f2019-04-29 15:23:20 +02001870yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001871{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001872 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001873 struct lysp_node *child;
1874
1875 ly_print(ctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid);
1876 LEVEL++;
1877
Radek Krejci693262f2019-04-29 15:23:20 +02001878 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
1879 yprp_when(ctx, aug->when, NULL);
1880 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
1881 ypr_status(ctx, aug->flags, aug->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001882 ypr_description(ctx, aug->dsc, aug->exts, NULL);
1883 ypr_reference(ctx, aug->ref, aug->exts, NULL);
1884
1885 LY_LIST_FOR(aug->child, child) {
Radek Krejci693262f2019-04-29 15:23:20 +02001886 yprp_node(ctx, child);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001887 }
1888
1889 LY_ARRAY_FOR(aug->actions, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001890 yprp_action(ctx, &aug->actions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001891 }
1892
1893 LY_ARRAY_FOR(aug->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02001894 yprp_notification(ctx, &aug->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001895 }
1896
1897 LEVEL--;
Radek Krejcifc81ea82019-04-18 13:27:22 +02001898 ypr_close(ctx, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001899}
1900
1901
1902static void
Radek Krejci693262f2019-04-29 15:23:20 +02001903yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001904{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001905 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001906 int flag = 0;
1907 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
1908
Radek Krejci693262f2019-04-29 15:23:20 +02001909 yprp_node_common1(ctx, node, &flag);
1910 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001911
1912 LY_ARRAY_FOR(uses->refines, u) {
1913 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001914 yprp_refine(ctx, &uses->refines[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001915 }
1916
1917 LY_ARRAY_FOR(uses->augments, u) {
1918 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001919 yprp_augment(ctx, &uses->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001920 }
1921
1922 LEVEL--;
1923 ypr_close(ctx, flag);
1924}
1925
1926static void
Radek Krejci693262f2019-04-29 15:23:20 +02001927yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001928{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001929 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001930 int flag = 0;
1931 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1932
Radek Krejci693262f2019-04-29 15:23:20 +02001933 yprp_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001934
1935 LY_ARRAY_FOR(any->musts, u) {
1936 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001937 yprp_restr(ctx, &any->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001938 }
1939
Radek Krejci693262f2019-04-29 15:23:20 +02001940 yprp_node_common2(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001941
1942 LEVEL--;
1943 ypr_close(ctx, flag);
1944}
1945
1946static void
Radek Krejci693262f2019-04-29 15:23:20 +02001947yprc_anydata(struct ypr_ctx *ctx, const struct lysc_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001948{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001949 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001950 int flag = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001951 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejcid3ca0632019-04-16 16:54:54 +02001952
Radek Krejci693262f2019-04-29 15:23:20 +02001953 yprc_node_common1(ctx, node, &flag);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001954
Radek Krejci693262f2019-04-29 15:23:20 +02001955 LY_ARRAY_FOR(any->musts, u) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02001956 ypr_open(ctx->out, &flag);
Radek Krejci693262f2019-04-29 15:23:20 +02001957 yprc_must(ctx, &any->musts[u], NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001958 }
1959
Radek Krejci693262f2019-04-29 15:23:20 +02001960 yprc_node_common2(ctx, node, &flag);
1961
Radek Krejcid3ca0632019-04-16 16:54:54 +02001962 LEVEL--;
1963 ypr_close(ctx, flag);
1964}
1965
1966static void
Radek Krejci693262f2019-04-29 15:23:20 +02001967yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
Radek Krejcid3ca0632019-04-16 16:54:54 +02001968{
Radek Krejci56cc0872019-04-30 09:22:27 +02001969 LYOUT_CHECK(ctx->out);
1970
Radek Krejcid3ca0632019-04-16 16:54:54 +02001971 switch (node->nodetype) {
1972 case LYS_CONTAINER:
Radek Krejci693262f2019-04-29 15:23:20 +02001973 yprp_container(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001974 break;
1975 case LYS_CHOICE:
Radek Krejci693262f2019-04-29 15:23:20 +02001976 yprp_choice(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001977 break;
1978 case LYS_LEAF:
Radek Krejci693262f2019-04-29 15:23:20 +02001979 yprp_leaf(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001980 break;
1981 case LYS_LEAFLIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001982 yprp_leaflist(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001983 break;
1984 case LYS_LIST:
Radek Krejci693262f2019-04-29 15:23:20 +02001985 yprp_list(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001986 break;
1987 case LYS_USES:
Radek Krejci693262f2019-04-29 15:23:20 +02001988 yprp_uses(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001989 break;
1990 case LYS_ANYXML:
1991 case LYS_ANYDATA:
Radek Krejci693262f2019-04-29 15:23:20 +02001992 yprp_anydata(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001993 break;
1994 case LYS_CASE:
Radek Krejci693262f2019-04-29 15:23:20 +02001995 yprp_case(ctx, node);
Radek Krejcid3ca0632019-04-16 16:54:54 +02001996 break;
1997 default:
1998 break;
1999 }
2000}
2001
2002static void
Radek Krejci693262f2019-04-29 15:23:20 +02002003yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
2004{
Radek Krejci56cc0872019-04-30 09:22:27 +02002005 LYOUT_CHECK(ctx->out);
2006
Radek Krejci693262f2019-04-29 15:23:20 +02002007 switch (node->nodetype) {
2008 case LYS_CONTAINER:
2009 yprc_container(ctx, node);
2010 break;
2011 case LYS_CHOICE:
2012 yprc_choice(ctx, node);
2013 break;
2014 case LYS_LEAF:
2015 yprc_leaf(ctx, node);
2016 break;
2017 case LYS_LEAFLIST:
2018 yprc_leaflist(ctx, node);
2019 break;
2020 case LYS_LIST:
2021 yprc_list(ctx, node);
2022 break;
2023 case LYS_ANYXML:
2024 case LYS_ANYDATA:
2025 yprc_anydata(ctx, node);
2026 break;
2027 default:
2028 break;
2029 }
2030}
2031
2032static void
2033yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002034{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002035 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002036 struct lysp_deviate_add *add;
2037 struct lysp_deviate_rpl *rpl;
2038 struct lysp_deviate_del *del;
fredgan2b11ddb2019-10-21 11:03:39 +08002039 struct lysp_deviate *elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002040
2041 ly_print(ctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid);
2042 LEVEL++;
2043
Radek Krejci693262f2019-04-29 15:23:20 +02002044 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002045 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
2046 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
2047
fredgan2b11ddb2019-10-21 11:03:39 +08002048 LY_LIST_FOR(deviation->deviates, elem) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002049 ly_print(ctx->out, "%*sdeviate ", INDENT);
fredgan2b11ddb2019-10-21 11:03:39 +08002050 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
2051 if (elem->exts) {
Radek Krejcid3ca0632019-04-16 16:54:54 +02002052 ly_print(ctx->out, "not-supported {\n");
2053 LEVEL++;
2054
fredgan2b11ddb2019-10-21 11:03:39 +08002055 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002056 } else {
2057 ly_print(ctx->out, "not-supported;\n");
2058 continue;
2059 }
fredgan2b11ddb2019-10-21 11:03:39 +08002060 } else if (elem->mod == LYS_DEV_ADD) {
2061 add = (struct lysp_deviate_add*)elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002062 ly_print(ctx->out, "add {\n");
2063 LEVEL++;
2064
Radek Krejci693262f2019-04-29 15:23:20 +02002065 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
2066 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002067 LY_ARRAY_FOR(add->musts, u) {
2068 yprp_restr(ctx, &add->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002069 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002070 LY_ARRAY_FOR(add->uniques, u) {
2071 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002072 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002073 LY_ARRAY_FOR(add->dflts, u) {
2074 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002075 }
Radek Krejci693262f2019-04-29 15:23:20 +02002076 ypr_config(ctx, add->flags, add->exts, NULL);
2077 ypr_mandatory(ctx, add->flags, add->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002078 if (add->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002079 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002080 }
2081 if (add->flags & LYS_SET_MAX) {
2082 if (add->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002083 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002084 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002085 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002086 }
2087 }
fredgan2b11ddb2019-10-21 11:03:39 +08002088 } else if (elem->mod == LYS_DEV_REPLACE) {
2089 rpl = (struct lysp_deviate_rpl*)elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002090 ly_print(ctx->out, "replace {\n");
2091 LEVEL++;
2092
Radek Krejci693262f2019-04-29 15:23:20 +02002093 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002094 if (rpl->type) {
Radek Krejci693262f2019-04-29 15:23:20 +02002095 yprp_type(ctx, rpl->type);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002096 }
Radek Krejci693262f2019-04-29 15:23:20 +02002097 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
2098 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
2099 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
2100 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002101 if (rpl->flags & LYS_SET_MIN) {
Radek Krejci693262f2019-04-29 15:23:20 +02002102 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002103 }
2104 if (rpl->flags & LYS_SET_MAX) {
2105 if (rpl->max) {
Radek Krejci693262f2019-04-29 15:23:20 +02002106 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max, NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002107 } else {
Radek Krejci693262f2019-04-29 15:23:20 +02002108 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002109 }
2110 }
fredgan2b11ddb2019-10-21 11:03:39 +08002111 } else if (elem->mod == LYS_DEV_DELETE) {
2112 del = (struct lysp_deviate_del*)elem;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002113 ly_print(ctx->out, "delete {\n");
2114 LEVEL++;
2115
Radek Krejci693262f2019-04-29 15:23:20 +02002116 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
2117 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002118 LY_ARRAY_FOR(del->musts, u) {
2119 yprp_restr(ctx, &del->musts[u], "must", NULL);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002120 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002121 LY_ARRAY_FOR(del->uniques, u) {
2122 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002123 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002124 LY_ARRAY_FOR(del->dflts, u) {
2125 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002126 }
2127 }
2128
2129 LEVEL--;
2130 ypr_close(ctx, 1);
2131 }
2132
2133 LEVEL--;
2134 ypr_close(ctx, 1);
2135}
2136
Radek Krejci693262f2019-04-29 15:23:20 +02002137/**
2138 * @brief Minimal print of a schema.
2139 *
2140 * To print
2141 * a) compiled schema when it is not compiled or
2142 * b) parsed when the parsed form was already removed
2143 */
2144static LY_ERR
2145ypr_missing_format(struct ypr_ctx *ctx, const struct lys_module *module)
2146{
2147 /* module-header-stmts */
2148 if (module->version) {
2149 if (module->version) {
2150 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", NULL);
2151 }
2152 }
2153 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, NULL);
2154 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, NULL);
2155
2156 /* meta-stmts */
2157 if (module->org || module->contact || module->dsc || module->ref) {
2158 ly_print(ctx->out, "\n");
2159 }
2160 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, NULL);
2161 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, NULL);
2162 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, NULL);
2163 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, NULL);
2164
2165 /* revision-stmts */
2166 if (module->revision) {
2167 ly_print(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
2168 }
2169
2170 LEVEL--;
2171 ly_print(ctx->out, "%*s}\n", INDENT);
2172 ly_print_flush(ctx->out);
2173
2174 return LY_SUCCESS;
2175}
2176
Radek Krejcid3ca0632019-04-16 16:54:54 +02002177LY_ERR
2178yang_print_parsed(struct lyout *out, const struct lys_module *module)
2179{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002180 LY_ARRAY_SIZE_TYPE u;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002181 struct lysp_node *data;
2182 struct lysp_module *modp = module->parsed;
Radek Krejci693262f2019-04-29 15:23:20 +02002183 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .schema = YPR_PARSED}, *ctx = &ctx_;
Radek Krejcid3ca0632019-04-16 16:54:54 +02002184
2185 ly_print(ctx->out, "%*smodule %s {\n", INDENT, module->name);
2186 LEVEL++;
2187
Radek Krejci693262f2019-04-29 15:23:20 +02002188 if (!modp) {
2189 ly_print(ctx->out, "%*s/* PARSED INFORMATION ARE NOT FULLY PRESENT */\n", INDENT);
2190 return ypr_missing_format(ctx, module);
2191 }
2192
Radek Krejcid3ca0632019-04-16 16:54:54 +02002193 /* module-header-stmts */
2194 if (module->version) {
2195 if (module->version) {
Radek Krejci693262f2019-04-29 15:23:20 +02002196 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002197 }
2198 }
Radek Krejci693262f2019-04-29 15:23:20 +02002199 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
2200 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002201
2202 /* linkage-stmts */
2203 LY_ARRAY_FOR(modp->imports, u) {
Radek Krejci7871ce52019-06-11 16:44:56 +02002204 ly_print(out, "%s%*simport %s {\n", u ? "" : "\n", INDENT, modp->imports[u].module->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002205 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002206 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
2207 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002208 if (modp->imports[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002209 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002210 }
Radek Krejci693262f2019-04-29 15:23:20 +02002211 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
2212 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002213 LEVEL--;
2214 ly_print(out, "%*s}\n", INDENT);
2215 }
2216 LY_ARRAY_FOR(modp->includes, u) {
2217 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Radek Krejci7871ce52019-06-11 16:44:56 +02002218 ly_print(out, "%s%*sinclude %s {\n", u ? "" : "\n", INDENT, modp->includes[u].submodule->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002219 LEVEL++;
Radek Krejci693262f2019-04-29 15:23:20 +02002220 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002221 if (modp->includes[u].rev[0]) {
Radek Krejci693262f2019-04-29 15:23:20 +02002222 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002223 }
Radek Krejci693262f2019-04-29 15:23:20 +02002224 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
2225 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002226 LEVEL--;
2227 ly_print(out, "%*s}\n", INDENT);
2228 } else {
2229 ly_print(out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].submodule->name);
2230 }
2231 }
2232
2233 /* meta-stmts */
2234 if (module->org || module->contact || module->dsc || module->ref) {
2235 ly_print(out, "\n");
2236 }
Radek Krejci693262f2019-04-29 15:23:20 +02002237 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
2238 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
2239 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
2240 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002241
2242 /* revision-stmts */
2243 if (modp->revs) {
2244 ly_print(out, "\n");
2245 }
2246 LY_ARRAY_FOR(modp->revs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002247 yprp_revision(ctx, &modp->revs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002248 }
2249 /* body-stmts */
2250 LY_ARRAY_FOR(modp->extensions, u) {
2251 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002252 yprp_extension(ctx, &modp->extensions[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002253 }
2254 if (modp->exts) {
2255 ly_print(out, "\n");
Radek Krejci693262f2019-04-29 15:23:20 +02002256 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->parsed->exts, NULL, 0);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002257 }
2258
2259 LY_ARRAY_FOR(modp->features, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002260 yprp_feature(ctx, &modp->features[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002261 }
2262
2263 LY_ARRAY_FOR(modp->identities, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002264 yprp_identity(ctx, &modp->identities[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002265 }
2266
2267 LY_ARRAY_FOR(modp->typedefs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002268 yprp_typedef(ctx, &modp->typedefs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002269 }
2270
2271 LY_ARRAY_FOR(modp->groupings, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002272 yprp_grouping(ctx, &modp->groupings[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002273 }
2274
2275 LY_LIST_FOR(modp->data, data) {
Radek Krejci693262f2019-04-29 15:23:20 +02002276 yprp_node(ctx, data);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002277 }
2278
2279 LY_ARRAY_FOR(modp->augments, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002280 yprp_augment(ctx, &modp->augments[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002281 }
2282
2283 LY_ARRAY_FOR(modp->rpcs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002284 yprp_action(ctx, &modp->rpcs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002285 }
2286
2287 LY_ARRAY_FOR(modp->notifs, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002288 yprp_notification(ctx, &modp->notifs[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002289 }
2290
2291 LY_ARRAY_FOR(modp->deviations, u) {
Radek Krejci693262f2019-04-29 15:23:20 +02002292 yprp_deviation(ctx, &modp->deviations[u]);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002293 }
2294
2295 LEVEL--;
2296 ly_print(out, "%*s}\n", INDENT);
2297 ly_print_flush(out);
2298
2299 return LY_SUCCESS;
2300}
2301
2302LY_ERR
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002303yang_print_compiled_node(struct lyout *out, const struct lysc_node *node, int options)
2304{
2305 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = node->module, .options = options}, *ctx = &ctx_;
2306
2307 yprc_node(ctx, node);
2308
2309 ly_print_flush(out);
2310 return LY_SUCCESS;
2311}
2312
2313LY_ERR
2314yang_print_compiled(struct lyout *out, const struct lys_module *module, int options)
Radek Krejcid3ca0632019-04-16 16:54:54 +02002315{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002316 LY_ARRAY_SIZE_TYPE u;
Radek Krejci693262f2019-04-29 15:23:20 +02002317 struct lysc_node *data;
2318 struct lysc_module *modc = module->compiled;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002319 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Radek Krejci693262f2019-04-29 15:23:20 +02002320
2321 ly_print(ctx->out, "%*smodule %s {\n", INDENT, module->name);
2322 LEVEL++;
2323
2324 if (!modc) {
2325 ly_print(ctx->out, "%*s/* COMPILED INFORMATION ARE NOT PRESENT */\n", INDENT);
2326 return ypr_missing_format(ctx, module);
2327 }
2328
2329 /* module-header-stmts */
2330 if (module->version) {
2331 if (module->version) {
2332 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modc->exts);
2333 }
2334 }
2335 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modc->exts);
2336 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modc->exts);
2337
2338 /* linkage-stmts */
2339 LY_ARRAY_FOR(modc->imports, u) {
2340 ly_print(out, "\n%*simport %s {\n", INDENT, modc->imports[u].module->name);
2341 LEVEL++;
2342 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modc->imports[u].exts, NULL, 0);
2343 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modc->imports[u].prefix, modc->imports[u].exts);
2344 if (modc->imports[u].module->revision) {
2345 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modc->imports[u].module->revision, modc->imports[u].exts);
2346 }
2347 LEVEL--;
2348 ly_print(out, "%*s}\n", INDENT);
2349 }
2350
2351 /* meta-stmts */
2352 if (module->org || module->contact || module->dsc || module->ref) {
2353 ly_print(out, "\n");
2354 }
2355 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modc->exts);
2356 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modc->exts);
2357 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modc->exts);
2358 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modc->exts);
2359
2360 /* revision-stmts */
2361 if (module->revision) {
2362 ly_print(ctx->out, "\n%*srevision %s;\n", INDENT, module->revision);
2363 }
2364
2365 /* body-stmts */
2366 if (modc->exts) {
2367 ly_print(out, "\n");
2368 yprc_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, module->compiled->exts, NULL, 0);
2369 }
2370
2371 LY_ARRAY_FOR(modc->features, u) {
2372 yprc_feature(ctx, &modc->features[u]);
2373 }
2374
2375 LY_ARRAY_FOR(modc->identities, u) {
2376 yprc_identity(ctx, &modc->identities[u]);
2377 }
2378
Radek Krejci4fa6ebf2019-11-21 13:34:35 +08002379 if (!(ctx->options & LYS_OUTPUT_NO_SUBSTMT)) {
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002380 LY_LIST_FOR(modc->data, data) {
2381 yprc_node(ctx, data);
2382 }
Radek Krejci693262f2019-04-29 15:23:20 +02002383
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002384 LY_ARRAY_FOR(modc->rpcs, u) {
2385 yprc_action(ctx, &modc->rpcs[u]);
2386 }
Radek Krejci693262f2019-04-29 15:23:20 +02002387
Radek Krejcid8c0f5e2019-11-17 12:18:34 +08002388 LY_ARRAY_FOR(modc->notifs, u) {
2389 yprc_notification(ctx, &modc->notifs[u]);
2390 }
Radek Krejci693262f2019-04-29 15:23:20 +02002391 }
2392
2393 LEVEL--;
2394 ly_print(out, "%*s}\n", INDENT);
2395 ly_print_flush(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +02002396
2397 return LY_SUCCESS;
2398}