blob: 04b0323ecb3f9f9d82d0130c9707826ab169bbb3 [file] [log] [blame]
FredGand944bdc2019-11-05 21:57:07 +08001/**
2 * @file printer_yin.c
3 * @author Fred Gan <ganshaolong@vip.qq.com>
4 * @brief YIN printer
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
FredGand944bdc2019-11-05 21:57:07 +080016
FredGand944bdc2019-11-05 21:57:07 +080017#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
FredGand944bdc2019-11-05 21:57:07 +080020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020022#include "compat.h"
FredGand944bdc2019-11-05 21:57:07 +080023#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "printer.h"
FredGand944bdc2019-11-05 21:57:07 +080025#include "printer_internal.h"
26#include "tree.h"
27#include "tree_schema.h"
28#include "tree_schema_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080029#include "xml.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "xpath.h"
FredGand944bdc2019-11-05 21:57:07 +080031
32/**
33 * @brief YIN printer context.
34 */
35struct ypr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020036 struct ly_out *out; /**< output specification */
37 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
38 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
FredGand944bdc2019-11-05 21:57:07 +080039 const struct lys_module *module; /**< schema to print */
40};
41
FredGand944bdc2019-11-05 21:57:07 +080042static void yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +020043 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count);
FredGand944bdc2019-11-05 21:57:07 +080044
45static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020046ypr_open(struct ypr_ctx *ctx, const char *elem_name, const char *attr_name, const char *attr_value, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080047{
Michal Vasko5233e962020-08-14 14:26:20 +020048 ly_print_(ctx->out, "%*s<%s", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080049
50 if (attr_name) {
Michal Vasko5233e962020-08-14 14:26:20 +020051 ly_print_(ctx->out, " %s=\"", attr_name);
FredGand944bdc2019-11-05 21:57:07 +080052 lyxml_dump_text(ctx->out, attr_value, 1);
Michal Vasko5233e962020-08-14 14:26:20 +020053 ly_print_(ctx->out, "\"%s", flag == -1 ? "/>\n" : flag == 1 ? ">\n" : "");
FredGand944bdc2019-11-05 21:57:07 +080054 } else if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +020055 ly_print_(ctx->out, flag == -1 ? "/>\n" : ">\n");
FredGand944bdc2019-11-05 21:57:07 +080056 }
57}
58
59static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020060ypr_close(struct ypr_ctx *ctx, const char *elem_name, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080061{
62 if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +020063 ly_print_(ctx->out, "%*s</%s>\n", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080064 } else {
Michal Vasko5233e962020-08-14 14:26:20 +020065 ly_print_(ctx->out, "/>\n");
FredGand944bdc2019-11-05 21:57:07 +080066 }
67}
68
69/*
70 * par_close_flag
71 * 0 - parent not yet closed, printing >\n, setting flag to 1
72 * 1 or NULL - parent already closed, do nothing
73 */
74static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020075ypr_close_parent(struct ypr_ctx *ctx, int8_t *par_close_flag)
FredGand944bdc2019-11-05 21:57:07 +080076{
77 if (par_close_flag && !(*par_close_flag)) {
78 (*par_close_flag) = 1;
Michal Vasko5233e962020-08-14 14:26:20 +020079 ly_print_(ctx->out, ">\n");
FredGand944bdc2019-11-05 21:57:07 +080080 }
81}
82
83static void
84ypr_yin_arg(struct ypr_ctx *ctx, const char *arg, const char *text)
85{
Michal Vasko5233e962020-08-14 14:26:20 +020086 ly_print_(ctx->out, "%*s<%s>", INDENT, arg);
FredGand944bdc2019-11-05 21:57:07 +080087 lyxml_dump_text(ctx->out, text, 0);
Michal Vasko5233e962020-08-14 14:26:20 +020088 ly_print_(ctx->out, "</%s>\n", arg);
FredGand944bdc2019-11-05 21:57:07 +080089}
90
FredGand944bdc2019-11-05 21:57:07 +080091static void
92ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
93{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020094 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +020095 int8_t extflag = 0;
FredGand944bdc2019-11-05 21:57:07 +080096
97 if (!text) {
98 /* nothing to print */
99 return;
100 }
101
102 if (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) {
103 extflag = 1;
104 ypr_open(ctx, ext_substmt_info[substmt].name, NULL, NULL, extflag);
105 } else {
106 ypr_open(ctx, ext_substmt_info[substmt].name, ext_substmt_info[substmt].arg, text, extflag);
107 }
108
109 LEVEL++;
110 LY_ARRAY_FOR(ext, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200111 if (((struct lysp_ext_instance *)ext)[u].insubstmt != substmt || ((struct lysp_ext_instance *)ext)[u].insubstmt_index != substmt_index) {
FredGand944bdc2019-11-05 21:57:07 +0800112 continue;
113 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200114 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800115 }
116
117 /* argument as yin-element */
118 if (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) {
119 ypr_yin_arg(ctx, ext_substmt_info[substmt].arg, text);
120 }
121
122 LEVEL--;
123 ypr_close(ctx, ext_substmt_info[substmt].name, extflag);
124}
125
126static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200127ypr_unsigned(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, unsigned long int attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800128{
129 char *str;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200130 if (asprintf(&str, "%lu", attr_value) == -1) {
FredGand944bdc2019-11-05 21:57:07 +0800131 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800132 return;
133 }
134 ypr_substmt(ctx, substmt, substmt_index, str, exts);
135 free(str);
136}
137
138static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200139ypr_signed(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, signed long int attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800140{
141 char *str;
142
Radek Krejci1deb5be2020-08-26 16:43:36 +0200143 if (asprintf(&str, "%ld", attr_value) == -1) {
FredGand944bdc2019-11-05 21:57:07 +0800144 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800145 return;
146 }
147 ypr_substmt(ctx, substmt, substmt_index, str, exts);
148 free(str);
149}
150
151static void
152yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
153{
154 if (rev->dsc || rev->ref || rev->exts) {
155 ypr_open(ctx, "revision", "date", rev->date, 1);
156 LEVEL++;
157 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
158 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
159 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
160 LEVEL--;
161 ypr_close(ctx, "revision", 1);
162 } else {
163 ypr_open(ctx, "revision", "date", rev->date, -1);
164 }
165}
166
167static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200168ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800169{
170 if (flags & LYS_MAND_MASK) {
171 ypr_close_parent(ctx, flag);
172 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
173 }
174}
175
176static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200177ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800178{
179 if (flags & LYS_CONFIG_MASK) {
180 ypr_close_parent(ctx, flag);
181 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
182 }
183}
184
185static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200186ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800187{
188 const char *status = NULL;
189
190 if (flags & LYS_STATUS_CURR) {
191 ypr_close_parent(ctx, flag);
192 status = "current";
193 } else if (flags & LYS_STATUS_DEPRC) {
194 ypr_close_parent(ctx, flag);
195 status = "deprecated";
196 } else if (flags & LYS_STATUS_OBSLT) {
197 ypr_close_parent(ctx, flag);
198 status = "obsolete";
199 }
200
201 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
202}
203
204static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200205ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800206{
207 if (dsc) {
208 ypr_close_parent(ctx, flag);
209 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
210 }
211}
212
213static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200214ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800215{
216 if (ref) {
217 ypr_close_parent(ctx, flag);
218 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
219 }
220}
221
222static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200223yprp_iffeatures(struct ypr_ctx *ctx, const char **iff, struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800224{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200225 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200226 int8_t extflag;
FredGand944bdc2019-11-05 21:57:07 +0800227
228 LY_ARRAY_FOR(iff, u) {
229 ypr_close_parent(ctx, flag);
230 extflag = 0;
231
Michal Vasko5233e962020-08-14 14:26:20 +0200232 ly_print_(ctx->out, "%*s<if-feature name=\"%s", INDENT, iff[u]);
FredGand944bdc2019-11-05 21:57:07 +0800233
234 /* extensions */
235 LEVEL++;
236 LY_ARRAY_FOR(exts, u) {
237 if (exts[u].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[u].insubstmt_index != u) {
238 continue;
239 }
240 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[u], &extflag, 1);
241 }
242 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200243 ly_print_(ctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800244 }
245}
246
247static void
248yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
249{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200250 int8_t flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200251 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800252
253 ypr_open(ctx, "extension", "name", ext->name, flag);
254 LEVEL++;
255
256 if (ext->exts) {
257 ypr_close_parent(ctx, &flag);
258 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
259 }
260
261 if (ext->argument) {
262 ypr_close_parent(ctx, &flag);
263 ypr_open(ctx, "argument", "name", ext->argument, flag2);
264
265 LEVEL++;
266 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200267 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200268 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LYEXT_SUBSTMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
FredGand944bdc2019-11-05 21:57:07 +0800269 ypr_close_parent(ctx, &flag2);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200270 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
FredGand944bdc2019-11-05 21:57:07 +0800271 }
272 }
273 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200274 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts))) {
FredGand944bdc2019-11-05 21:57:07 +0800275 ypr_close_parent(ctx, &flag2);
276 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
277 }
278 LEVEL--;
279 ypr_close(ctx, "argument", flag2);
280 }
281
282 ypr_status(ctx, ext->flags, ext->exts, &flag);
283 ypr_description(ctx, ext->dsc, ext->exts, &flag);
284 ypr_reference(ctx, ext->ref, ext->exts, &flag);
285
286 LEVEL--;
287 ypr_close(ctx, "extension", flag);
288}
289
290static void
291yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
292{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200293 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800294
295 ypr_open(ctx, "feature", "name", feat->name, flag);
296 LEVEL++;
297 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
298 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
299 ypr_status(ctx, feat->flags, feat->exts, &flag);
300 ypr_description(ctx, feat->dsc, feat->exts, &flag);
301 ypr_reference(ctx, feat->ref, feat->exts, &flag);
302 LEVEL--;
303 ypr_close(ctx, "feature", flag);
304}
305
306static void
307yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
308{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200309 int8_t flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200310 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800311
312 ypr_open(ctx, "identity", "name", ident->name, flag);
313 LEVEL++;
314
315 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
316 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
317
318 LY_ARRAY_FOR(ident->bases, u) {
319 ypr_close_parent(ctx, &flag);
320 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
321 }
322
323 ypr_status(ctx, ident->flags, ident->exts, &flag);
324 ypr_description(ctx, ident->dsc, ident->exts, &flag);
325 ypr_reference(ctx, ident->ref, ident->exts, &flag);
326
327 LEVEL--;
328 ypr_close(ctx, "identity", flag);
329}
330
331static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200332yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, const char *attr, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800333{
334 (void)flag;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200335 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800336
337 if (!restr) {
338 return;
339 }
340
Michal Vasko5233e962020-08-14 14:26:20 +0200341 ly_print_(ctx->out, "%*s<%s %s=\"", INDENT, name, attr);
FredGand944bdc2019-11-05 21:57:07 +0800342 lyxml_dump_text(ctx->out, (restr->arg[0] != 0x15 && restr->arg[0] != 0x06) ? restr->arg : &restr->arg[1], 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200343 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800344
345 LEVEL++;
346 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
347 if (restr->arg[0] == 0x15) {
348 ypr_close_parent(ctx, &inner_flag);
349 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
350 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
351 }
352 if (restr->emsg) {
353 ypr_close_parent(ctx, &inner_flag);
354 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
355 }
356 if (restr->eapptag) {
357 ypr_close_parent(ctx, &inner_flag);
358 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
359 }
360 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
361 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
362
363 LEVEL--;
364 ypr_close(ctx, name, inner_flag);
365}
366
367static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200368yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800369{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200370 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800371 (void)flag;
372
373 if (!when) {
374 return;
375 }
376
Michal Vasko5233e962020-08-14 14:26:20 +0200377 ly_print_(ctx->out, "%*s<when condition=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800378 lyxml_dump_text(ctx->out, when->cond, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200379 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800380
381 LEVEL++;
382 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
383 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
384 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
385 LEVEL--;
386 ypr_close(ctx, "when", inner_flag);
387}
388
389static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200390yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800391{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200392 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200393 int8_t inner_flag;
FredGand944bdc2019-11-05 21:57:07 +0800394 (void)flag;
395
396 LY_ARRAY_FOR(items, u) {
397 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200398 ly_print_(ctx->out, "%*s<bit name=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800399 lyxml_dump_text(ctx->out, items[u].name, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200400 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800401 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200402 ly_print_(ctx->out, "%*s<enum name=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800403 lyxml_dump_text(ctx->out, items[u].name, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200404 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800405 }
406 inner_flag = 0;
407 LEVEL++;
408 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
409 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
410 if (items[u].flags & LYS_SET_VALUE) {
411 if (type == LY_TYPE_BITS) {
412 ypr_close_parent(ctx, &inner_flag);
413 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value);
414 } else { /* LY_TYPE_ENUM */
415 ypr_close_parent(ctx, &inner_flag);
416 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value);
417 }
418 }
419 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
420 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
421 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
422 LEVEL--;
423 ypr_close(ctx, type == LY_TYPE_BITS ? "bit" : "enum", inner_flag);
424 }
425}
426
427static void
428yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
429{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200430 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200431 int8_t flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200432
FredGand944bdc2019-11-05 21:57:07 +0800433 if (!ctx || !type) {
434 return;
435 }
436
FredGand944bdc2019-11-05 21:57:07 +0800437 ypr_open(ctx, "type", "name", type->name, flag);
438 LEVEL++;
439
440 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
441
442 if (type->range || type->length || type->patterns || type->bits || type->enums) {
443 ypr_close_parent(ctx, &flag);
444 }
445 yprp_restr(ctx, type->range, "range", "value", &flag);
446 yprp_restr(ctx, type->length, "length", "value", &flag);
447 LY_ARRAY_FOR(type->patterns, u) {
448 yprp_restr(ctx, &type->patterns[u], "pattern", "value", &flag);
449 }
450 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
451 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
452
453 if (type->path) {
454 ypr_close_parent(ctx, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200455 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800456 }
457 if (type->flags & LYS_SET_REQINST) {
458 ypr_close_parent(ctx, &flag);
459 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
460 }
461 if (type->flags & LYS_SET_FRDIGITS) {
462 ypr_close_parent(ctx, &flag);
463 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits);
464 }
465 LY_ARRAY_FOR(type->bases, u) {
466 ypr_close_parent(ctx, &flag);
467 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
468 }
469 LY_ARRAY_FOR(type->types, u) {
470 ypr_close_parent(ctx, &flag);
471 yprp_type(ctx, &type->types[u]);
472 }
473
474 LEVEL--;
475 ypr_close(ctx, "type", flag);
476}
477
478static void
479yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
480{
FredGand944bdc2019-11-05 21:57:07 +0800481 ypr_open(ctx, "typedef", "name", tpdf->name, 1);
482 LEVEL++;
483
484 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
485
486 yprp_type(ctx, &tpdf->type);
487
488 if (tpdf->units) {
489 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
490 }
491 if (tpdf->dflt) {
492 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
493 }
494
495 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
496 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
497 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
498
499 LEVEL--;
500 ypr_close(ctx, "typedef", 1);
501}
502
503static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
504static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
505
506static void
507yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
508{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200509 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200510 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800511 struct lysp_node *data;
512
FredGand944bdc2019-11-05 21:57:07 +0800513 ypr_open(ctx, "grouping", "name", grp->name, flag);
514 LEVEL++;
515
516 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
517 ypr_status(ctx, grp->flags, grp->exts, &flag);
518 ypr_description(ctx, grp->dsc, grp->exts, &flag);
519 ypr_reference(ctx, grp->ref, grp->exts, &flag);
520
521 LY_ARRAY_FOR(grp->typedefs, u) {
522 ypr_close_parent(ctx, &flag);
523 yprp_typedef(ctx, &grp->typedefs[u]);
524 }
525
526 LY_ARRAY_FOR(grp->groupings, u) {
527 ypr_close_parent(ctx, &flag);
528 yprp_grouping(ctx, &grp->groupings[u]);
529 }
530
531 LY_LIST_FOR(grp->data, data) {
532 ypr_close_parent(ctx, &flag);
533 yprp_node(ctx, data);
534 }
535
536 LY_ARRAY_FOR(grp->actions, u) {
537 ypr_close_parent(ctx, &flag);
538 yprp_action(ctx, &grp->actions[u]);
539 }
540
541 LEVEL--;
542 ypr_close(ctx, "grouping", flag);
543}
544
545static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200546yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800547{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200548 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800549 struct lysp_node *data;
550
551 if (!inout->nodetype) {
552 /* nodetype not set -> input/output is empty */
553 return;
554 }
555 ypr_close_parent(ctx, flag);
556
557 ypr_open(ctx, (inout->nodetype == LYS_INPUT ? "input" : "output"), NULL, NULL, *flag);
558 LEVEL++;
559
560 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
561 LY_ARRAY_FOR(inout->musts, u) {
562 yprp_restr(ctx, &inout->musts[u], "must", "condition", NULL);
563 }
564 LY_ARRAY_FOR(inout->typedefs, u) {
565 yprp_typedef(ctx, &inout->typedefs[u]);
566 }
567 LY_ARRAY_FOR(inout->groupings, u) {
568 yprp_grouping(ctx, &inout->groupings[u]);
569 }
570
571 LY_LIST_FOR(inout->data, data) {
572 yprp_node(ctx, data);
573 }
574
575 LEVEL--;
576 ypr_close(ctx, (inout->nodetype == LYS_INPUT ? "input" : "output"), 1);
577}
578
579static void
580yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
581{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200582 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200583 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800584 struct lysp_node *data;
585
FredGand944bdc2019-11-05 21:57:07 +0800586 ypr_open(ctx, "notification", "name", notif->name, flag);
587
588 LEVEL++;
589 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
590 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
591
592 LY_ARRAY_FOR(notif->musts, u) {
593 ypr_close_parent(ctx, &flag);
594 yprp_restr(ctx, &notif->musts[u], "must", "condition", &flag);
595 }
596 ypr_status(ctx, notif->flags, notif->exts, &flag);
597 ypr_description(ctx, notif->dsc, notif->exts, &flag);
598 ypr_reference(ctx, notif->ref, notif->exts, &flag);
599
600 LY_ARRAY_FOR(notif->typedefs, u) {
601 ypr_close_parent(ctx, &flag);
602 yprp_typedef(ctx, &notif->typedefs[u]);
603 }
604
605 LY_ARRAY_FOR(notif->groupings, u) {
606 ypr_close_parent(ctx, &flag);
607 yprp_grouping(ctx, &notif->groupings[u]);
608 }
609
610 LY_LIST_FOR(notif->data, data) {
611 ypr_close_parent(ctx, &flag);
612 yprp_node(ctx, data);
613 }
614
615 LEVEL--;
616 ypr_close(ctx, "notification", flag);
617}
618
619static void
620yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
621{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200622 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200623 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800624
FredGand944bdc2019-11-05 21:57:07 +0800625 ypr_open(ctx, action->parent ? "action" : "rpc", "name", action->name, flag);
626
627 LEVEL++;
628 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
629 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
630 ypr_status(ctx, action->flags, action->exts, &flag);
631 ypr_description(ctx, action->dsc, action->exts, &flag);
632 ypr_reference(ctx, action->ref, action->exts, &flag);
633
634 LY_ARRAY_FOR(action->typedefs, u) {
635 ypr_close_parent(ctx, &flag);
636 yprp_typedef(ctx, &action->typedefs[u]);
637 }
638
639 LY_ARRAY_FOR(action->groupings, u) {
640 ypr_close_parent(ctx, &flag);
641 yprp_grouping(ctx, &action->groupings[u]);
642 }
643
644 yprp_inout(ctx, &action->input, &flag);
645 yprp_inout(ctx, &action->output, &flag);
646
647 LEVEL--;
648 ypr_close(ctx, action->parent ? "action" : "rpc", flag);
649}
650
651static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200652yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800653{
654 ypr_open(ctx, lys_nodetype2str(node->nodetype), "name", node->name, *flag);
655 LEVEL++;
656
657 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
658 yprp_when(ctx, node->when, flag);
659 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
660}
661
662static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200663yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800664{
665 ypr_config(ctx, node->flags, node->exts, flag);
666 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) {
667 ypr_mandatory(ctx, node->flags, node->exts, flag);
668 }
669 ypr_status(ctx, node->flags, node->exts, flag);
670 ypr_description(ctx, node->dsc, node->exts, flag);
671 ypr_reference(ctx, node->ref, node->exts, flag);
672}
673
674static void
675yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
676{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200677 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200678 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800679 struct lysp_node *child;
680 struct lysp_node_container *cont = (struct lysp_node_container *)node;
681
682 yprp_node_common1(ctx, node, &flag);
683
684 LY_ARRAY_FOR(cont->musts, u) {
685 ypr_close_parent(ctx, &flag);
686 yprp_restr(ctx, &cont->musts[u], "must", "condition", &flag);
687 }
688 if (cont->presence) {
689 ypr_close_parent(ctx, &flag);
690 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
691 }
692
693 yprp_node_common2(ctx, node, &flag);
694
695 LY_ARRAY_FOR(cont->typedefs, u) {
696 ypr_close_parent(ctx, &flag);
697 yprp_typedef(ctx, &cont->typedefs[u]);
698 }
699
700 LY_ARRAY_FOR(cont->groupings, u) {
701 ypr_close_parent(ctx, &flag);
702 yprp_grouping(ctx, &cont->groupings[u]);
703 }
704
705 LY_LIST_FOR(cont->child, child) {
706 ypr_close_parent(ctx, &flag);
707 yprp_node(ctx, child);
708 }
709
710 LY_ARRAY_FOR(cont->actions, u) {
711 ypr_close_parent(ctx, &flag);
712 yprp_action(ctx, &cont->actions[u]);
713 }
714
715 LY_ARRAY_FOR(cont->notifs, u) {
716 ypr_close_parent(ctx, &flag);
717 yprp_notification(ctx, &cont->notifs[u]);
718 }
719
720 LEVEL--;
721 ypr_close(ctx, "container", flag);
722}
723
724static void
725yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
726{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200727 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800728 struct lysp_node *child;
729 struct lysp_node_case *cas = (struct lysp_node_case *)node;
730
731 yprp_node_common1(ctx, node, &flag);
732 yprp_node_common2(ctx, node, &flag);
733
734 LY_LIST_FOR(cas->child, child) {
735 ypr_close_parent(ctx, &flag);
736 yprp_node(ctx, child);
737 }
738
739 LEVEL--;
740 ypr_close(ctx, "case", flag);
741}
742
743static void
744yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
745{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200746 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800747 struct lysp_node *child;
748 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
749
750 yprp_node_common1(ctx, node, &flag);
751
752 if (choice->dflt) {
753 ypr_close_parent(ctx, &flag);
754 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
755 }
756
757 yprp_node_common2(ctx, node, &flag);
758
759 LY_LIST_FOR(choice->child, child) {
760 ypr_close_parent(ctx, &flag);
761 yprp_node(ctx, child);
762 }
763
764 LEVEL--;
765 ypr_close(ctx, "choice", flag);
766}
767
768static void
769yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
770{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200771 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800772 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
773
Radek Krejci1deb5be2020-08-26 16:43:36 +0200774 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800775 yprp_node_common1(ctx, node, &flag);
776
777 yprp_type(ctx, &leaf->type);
778 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
779 LY_ARRAY_FOR(leaf->musts, u) {
780 yprp_restr(ctx, &leaf->musts[u], "must", "condition", &flag);
781 }
782 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
783
784 yprp_node_common2(ctx, node, &flag);
785
786 LEVEL--;
787 ypr_close(ctx, "leaf", flag);
788}
789
790static void
791yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
792{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200793 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800794 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200795 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800796
797 yprp_node_common1(ctx, node, &flag);
798
799 yprp_type(ctx, &llist->type);
800 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
801 LY_ARRAY_FOR(llist->musts, u) {
802 yprp_restr(ctx, &llist->musts[u], "must", "condition", NULL);
803 }
804 LY_ARRAY_FOR(llist->dflts, u) {
805 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
806 }
807
808 ypr_config(ctx, node->flags, node->exts, NULL);
809
810 if (llist->flags & LYS_SET_MIN) {
811 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min);
812 }
813 if (llist->flags & LYS_SET_MAX) {
814 if (llist->max) {
815 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max);
816 } else {
817 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
818 }
819 }
820
821 if (llist->flags & LYS_ORDBY_MASK) {
822 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
823 }
824
825 ypr_status(ctx, node->flags, node->exts, &flag);
826 ypr_description(ctx, node->dsc, node->exts, &flag);
827 ypr_reference(ctx, node->ref, node->exts, &flag);
828
829 LEVEL--;
830 ypr_close(ctx, "leaf-list", flag);
831}
832
833static void
834yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
835{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200836 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200837 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800838 struct lysp_node *child;
839 struct lysp_node_list *list = (struct lysp_node_list *)node;
840
841 yprp_node_common1(ctx, node, &flag);
842
843 LY_ARRAY_FOR(list->musts, u) {
844 ypr_close_parent(ctx, &flag);
845 yprp_restr(ctx, &list->musts[u], "must", "condition", &flag);
846 }
847 if (list->key) {
848 ypr_close_parent(ctx, &flag);
849 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
850 }
851 LY_ARRAY_FOR(list->uniques, u) {
852 ypr_close_parent(ctx, &flag);
853 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
854 }
855
856 ypr_config(ctx, node->flags, node->exts, NULL);
857
858 if (list->flags & LYS_SET_MIN) {
859 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min);
860 }
861 if (list->flags & LYS_SET_MAX) {
862 if (list->max) {
863 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max);
864 } else {
865 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
866 }
867 }
868
869 if (list->flags & LYS_ORDBY_MASK) {
870 ypr_close_parent(ctx, &flag);
871 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
872 }
873
874 ypr_status(ctx, node->flags, node->exts, &flag);
875 ypr_description(ctx, node->dsc, node->exts, &flag);
876 ypr_reference(ctx, node->ref, node->exts, &flag);
877
878 LY_ARRAY_FOR(list->typedefs, u) {
879 ypr_close_parent(ctx, &flag);
880 yprp_typedef(ctx, &list->typedefs[u]);
881 }
882
883 LY_ARRAY_FOR(list->groupings, u) {
884 ypr_close_parent(ctx, &flag);
885 yprp_grouping(ctx, &list->groupings[u]);
886 }
887
888 LY_LIST_FOR(list->child, child) {
889 ypr_close_parent(ctx, &flag);
890 yprp_node(ctx, child);
891 }
892
893 LY_ARRAY_FOR(list->actions, u) {
894 ypr_close_parent(ctx, &flag);
895 yprp_action(ctx, &list->actions[u]);
896 }
897
898 LY_ARRAY_FOR(list->notifs, u) {
899 ypr_close_parent(ctx, &flag);
900 yprp_notification(ctx, &list->notifs[u]);
901 }
902
903 LEVEL--;
904 ypr_close(ctx, "list", flag);
905}
906
907static void
908yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
909{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200910 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200911 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800912
913 ypr_open(ctx, "refine", "target-node", refine->nodeid, flag);
914 LEVEL++;
915
916 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
917 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
918
919 LY_ARRAY_FOR(refine->musts, u) {
920 ypr_close_parent(ctx, &flag);
921 yprp_restr(ctx, &refine->musts[u], "must", "condition", &flag);
922 }
923
924 if (refine->presence) {
925 ypr_close_parent(ctx, &flag);
926 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
927 }
928
929 LY_ARRAY_FOR(refine->dflts, u) {
930 ypr_close_parent(ctx, &flag);
931 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
932 }
933
934 ypr_config(ctx, refine->flags, refine->exts, &flag);
935 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
936
937 if (refine->flags & LYS_SET_MIN) {
938 ypr_close_parent(ctx, &flag);
939 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min);
940 }
941 if (refine->flags & LYS_SET_MAX) {
942 ypr_close_parent(ctx, &flag);
943 if (refine->max) {
944 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max);
945 } else {
946 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
947 }
948 }
949
950 ypr_description(ctx, refine->dsc, refine->exts, &flag);
951 ypr_reference(ctx, refine->ref, refine->exts, &flag);
952
953 LEVEL--;
954 ypr_close(ctx, "refine", flag);
955}
956
957static void
958yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
959{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200960 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800961 struct lysp_node *child;
962
963 ypr_open(ctx, "augment", "target-node", aug->nodeid, 1);
964 LEVEL++;
965
966 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
967 yprp_when(ctx, aug->when, NULL);
968 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
969 ypr_status(ctx, aug->flags, aug->exts, NULL);
970 ypr_description(ctx, aug->dsc, aug->exts, NULL);
971 ypr_reference(ctx, aug->ref, aug->exts, NULL);
972
973 LY_LIST_FOR(aug->child, child) {
974 yprp_node(ctx, child);
975 }
976
977 LY_ARRAY_FOR(aug->actions, u) {
978 yprp_action(ctx, &aug->actions[u]);
979 }
980
981 LY_ARRAY_FOR(aug->notifs, u) {
982 yprp_notification(ctx, &aug->notifs[u]);
983 }
984
985 LEVEL--;
986 ypr_close(ctx, "augment", 1);
987}
988
FredGand944bdc2019-11-05 21:57:07 +0800989static void
990yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
991{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200992 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200993 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800994 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
995
996 yprp_node_common1(ctx, node, &flag);
997 yprp_node_common2(ctx, node, &flag);
998
999 LY_ARRAY_FOR(uses->refines, u) {
1000 ypr_close_parent(ctx, &flag);
1001 yprp_refine(ctx, &uses->refines[u]);
1002 }
1003
1004 LY_ARRAY_FOR(uses->augments, u) {
1005 ypr_close_parent(ctx, &flag);
1006 yprp_augment(ctx, &uses->augments[u]);
1007 }
1008
1009 LEVEL--;
1010 ypr_close(ctx, "uses", flag);
1011}
1012
1013static void
1014yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
1015{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001016 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001017 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001018 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1019
1020 yprp_node_common1(ctx, node, &flag);
1021
1022 LY_ARRAY_FOR(any->musts, u) {
1023 ypr_close_parent(ctx, &flag);
1024 yprp_restr(ctx, &any->musts[u], "must", "condition", &flag);
1025 }
1026
1027 yprp_node_common2(ctx, node, &flag);
1028
1029 LEVEL--;
1030 ypr_close(ctx, lys_nodetype2str(node->nodetype), flag);
1031}
1032
1033static void
1034yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
1035{
FredGand944bdc2019-11-05 21:57:07 +08001036 switch (node->nodetype) {
1037 case LYS_CONTAINER:
1038 yprp_container(ctx, node);
1039 break;
1040 case LYS_CHOICE:
1041 yprp_choice(ctx, node);
1042 break;
1043 case LYS_LEAF:
1044 yprp_leaf(ctx, node);
1045 break;
1046 case LYS_LEAFLIST:
1047 yprp_leaflist(ctx, node);
1048 break;
1049 case LYS_LIST:
1050 yprp_list(ctx, node);
1051 break;
1052 case LYS_USES:
1053 yprp_uses(ctx, node);
1054 break;
1055 case LYS_ANYXML:
1056 case LYS_ANYDATA:
1057 yprp_anydata(ctx, node);
1058 break;
1059 case LYS_CASE:
1060 yprp_case(ctx, node);
1061 break;
1062 default:
1063 break;
1064 }
1065}
1066
1067static void
1068yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
1069{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001070 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001071 struct lysp_deviate_add *add;
1072 struct lysp_deviate_rpl *rpl;
1073 struct lysp_deviate_del *del;
1074 struct lysp_deviate *elem;
1075
1076 ypr_open(ctx, "deviation", "target-node", deviation->nodeid, 1);
1077 LEVEL++;
1078
1079 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
1080 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1081 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1082
1083 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001084 ly_print_(ctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001085 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1086 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001087 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001088 LEVEL++;
1089
1090 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
1091 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001092 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001093 continue;
1094 }
1095 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001096 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001097 ly_print_(ctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001098 LEVEL++;
1099
1100 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
1101 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001102 LY_ARRAY_FOR(add->musts, u) {
1103 yprp_restr(ctx, &add->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001104 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001105 LY_ARRAY_FOR(add->uniques, u) {
1106 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001107 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001108 LY_ARRAY_FOR(add->dflts, u) {
1109 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001110 }
1111 ypr_config(ctx, add->flags, add->exts, NULL);
1112 ypr_mandatory(ctx, add->flags, add->exts, NULL);
1113 if (add->flags & LYS_SET_MIN) {
1114 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min);
1115 }
1116 if (add->flags & LYS_SET_MAX) {
1117 if (add->max) {
1118 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max);
1119 } else {
1120 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
1121 }
1122 }
1123 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001124 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001125 ly_print_(ctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001126 LEVEL++;
1127
1128 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
1129 if (rpl->type) {
1130 yprp_type(ctx, rpl->type);
1131 }
1132 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
1133 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
1134 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
1135 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
1136 if (rpl->flags & LYS_SET_MIN) {
1137 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min);
1138 }
1139 if (rpl->flags & LYS_SET_MAX) {
1140 if (rpl->max) {
1141 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max);
1142 } else {
1143 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
1144 }
1145 }
1146 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001147 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001148 ly_print_(ctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001149 LEVEL++;
1150
1151 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
1152 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001153 LY_ARRAY_FOR(del->musts, u) {
1154 yprp_restr(ctx, &del->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001155 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001156 LY_ARRAY_FOR(del->uniques, u) {
1157 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001158 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001159 LY_ARRAY_FOR(del->dflts, u) {
1160 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001161 }
1162 }
1163
1164 LEVEL--;
1165 ypr_close(ctx, "deviate", 1);
1166 }
1167
1168 LEVEL--;
1169 ypr_close(ctx, "deviation", 1);
1170}
1171
FredGand944bdc2019-11-05 21:57:07 +08001172static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02001173ypr_xmlns(struct ypr_ctx *ctx, const struct lys_module *module, uint16_t indent)
FredGand944bdc2019-11-05 21:57:07 +08001174{
Michal Vasko5233e962020-08-14 14:26:20 +02001175 ly_print_(ctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1176 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001177}
FredGand944bdc2019-11-05 21:57:07 +08001178
Michal Vasko7c8439f2020-08-05 13:25:19 +02001179static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02001180ypr_import_xmlns(struct ypr_ctx *ctx, const struct lysp_module *modp, uint16_t indent)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001181{
1182 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001183
1184 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko5233e962020-08-14 14:26:20 +02001185 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, modp->imports[u].prefix, modp->imports[u].module->ns);
FredGand944bdc2019-11-05 21:57:07 +08001186 }
1187}
1188
1189struct ext_substmt_info_s stmt_attr_info[] = {
1190 {NULL, NULL, 0}, /**< LY_STMT_NONE*/
1191 {"status", "value", SUBST_FLAG_ID}, /**< LY_STMT_STATUS */
1192 {"config", "value", SUBST_FLAG_ID}, /**< LY_STMT_CONFIG */
1193 {"mandatory", "value", SUBST_FLAG_ID}, /**< LY_STMT_MANDATORY */
1194 {"units", "name", SUBST_FLAG_ID}, /**< LY_STMT_UNITS */
1195 {"default", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEFAULT */
1196 {"type", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPE */
1197 {"action", "name", SUBST_FLAG_ID}, /**< LY_STMT_ACTION */
1198 {"anydata", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYDATA */
1199 {"anyxml", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYXML */
1200 {"argument", "name", SUBST_FLAG_ID}, /**< LY_STMT_ARGUMENT */
1201 {"augment", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_AUGMENT */
1202 {"base", "name", SUBST_FLAG_ID}, /**< LY_STMT_BASE */
1203 {"belongs-to", "module", SUBST_FLAG_ID}, /**< LY_STMT_BELONGS_TO */
1204 {"bit", "name", SUBST_FLAG_ID}, /**< LY_STMT_BIT */
1205 {"case", "name", SUBST_FLAG_ID}, /**< LY_STMT_CASE */
1206 {"choice", "name", SUBST_FLAG_ID}, /**< LY_STMT_CHOICE */
1207 {"contact", "text", SUBST_FLAG_YIN},/**< LY_STMT_CONTACT */
1208 {"container", "name", SUBST_FLAG_ID}, /**< LY_STMT_CONTAINER */
1209 {"description", "text", SUBST_FLAG_YIN},/**< LY_STMT_DESCRIPTION */
1210 {"deviate", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATE */
1211 {"deviation", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATION */
1212 {"enum", "name", SUBST_FLAG_ID}, /**< LY_STMT_ENUM */
1213 {"error-app-tag", "value", SUBST_FLAG_ID}, /**< LY_STMT_ERROR_APP_TAG */
1214 {"error-message", "value", SUBST_FLAG_YIN},/**< LY_STMT_ERROR_MESSAGE */
1215 {"extension", "name", SUBST_FLAG_ID}, /**< LY_STMT_EXTENSION */
1216 {"feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_FEATURE */
1217 {"fraction-digits", "value", SUBST_FLAG_ID}, /**< LY_STMT_FRACTION_DIGITS */
1218 {"grouping", "name", SUBST_FLAG_ID}, /**< LY_STMT_GROUPING */
1219 {"identity", "name", SUBST_FLAG_ID}, /**< LY_STMT_IDENTITY */
1220 {"if-feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_IF_FEATURE */
1221 {"import", "module", SUBST_FLAG_ID}, /**< LY_STMT_IMPORT */
1222 {"include", "module", SUBST_FLAG_ID}, /**< LY_STMT_INCLUDE */
1223 {"input", NULL, 0}, /**< LY_STMT_INPUT */
1224 {"key", "value", SUBST_FLAG_ID}, /**< LY_STMT_KEY */
1225 {"leaf", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF */
1226 {"leaf-list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF_LIST */
1227 {"length", "value", SUBST_FLAG_ID}, /**< LY_STMT_LENGTH */
1228 {"list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LIST */
1229 {"max-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MAX_ELEMENTS */
1230 {"min-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MIN_ELEMENTS */
1231 {"modifier", "value", SUBST_FLAG_ID}, /**< LY_STMT_MODIFIER */
1232 {"module", "name", SUBST_FLAG_ID}, /**< LY_STMT_MODULE */
1233 {"must", "condition", SUBST_FLAG_ID}, /**< LY_STMT_MUST */
1234 {"namespace", "uri", SUBST_FLAG_ID}, /**< LY_STMT_NAMESPACE */
1235 {"notification", "name", SUBST_FLAG_ID}, /**< LY_STMT_NOTIFICATION */
1236 {"ordered-by", "value", SUBST_FLAG_ID}, /**< LY_STMT_ORDERED_BY */
1237 {"organization", "text", SUBST_FLAG_YIN},/**< LY_STMT_ORGANIZATION */
1238 {"output", NULL, 0}, /**< LY_STMT_OUTPUT */
1239 {"path", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATH */
1240 {"pattern", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATTERN */
1241 {"position", "value", SUBST_FLAG_ID}, /**< LY_STMT_POSITION */
1242 {"prefix", "value", SUBST_FLAG_ID}, /**< LY_STMT_PREFIX */
1243 {"presence", "value", SUBST_FLAG_ID}, /**< LY_STMT_PRESENCE */
1244 {"range", "value", SUBST_FLAG_ID}, /**< LY_STMT_RANGE */
1245 {"reference", "text", SUBST_FLAG_YIN},/**< LY_STMT_REFERENCE */
1246 {"refine", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_REFINE */
1247 {"require-instance", "value", SUBST_FLAG_ID}, /**< LY_STMT_REQUIRE_INSTANCE */
1248 {"revision", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION */
1249 {"revision-date", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION_DATE */
1250 {"rpc", "name", SUBST_FLAG_ID}, /**< LY_STMT_RPC */
1251 {"submodule", "name", SUBST_FLAG_ID}, /**< LY_STMT_SUBMODULE */
1252 {"typedef", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPEDEF */
1253 {"unique", "tag", SUBST_FLAG_ID}, /**< LY_STMT_UNIQUE */
1254 {"uses", "name", SUBST_FLAG_ID}, /**< LY_STMT_USES */
1255 {"value", "value", SUBST_FLAG_ID}, /**< LY_STMT_VALUE */
1256 {"when", "condition", SUBST_FLAG_ID}, /**< LY_STMT_WHEN */
1257 {"yang-version", "value", SUBST_FLAG_ID}, /**< LY_STMT_YANG_VERSION */
1258 {"yin-element", "value", SUBST_FLAG_ID}, /**< LY_STMT_YIN_ELEMENT */
1259 {NULL, NULL, 0}, /**< LY_STMT_EXTENSION_INSTANCE */
1260 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_SEMICOLON */
1261 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_LEFT_BRACE */
1262 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_RIGHT_BRACE */
1263 {NULL, NULL, 0}, /**< LY_STMT_ARG_TEXT */
1264 {NULL, NULL, 0}, /**< LY_STMT_ARG_VALUE */
1265};
1266
1267static void
1268yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
1269{
1270 struct lysp_stmt *childstmt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001271 int8_t flag = stmt->child ? 1 : -1;
FredGand944bdc2019-11-05 21:57:07 +08001272
1273 /* TODO:
1274 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
1275 cannot find the compiled information, so it is needed to be done,
1276 currently it is ignored */
Michal Vaskod989ba02020-08-24 10:59:24 +02001277 if (stmt_attr_info[stmt->kw].name) {
1278 if (stmt_attr_info[stmt->kw].flags & SUBST_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +08001279 ypr_open(ctx, stmt->stmt, NULL, NULL, flag);
1280 ypr_yin_arg(ctx, stmt_attr_info[stmt->kw].arg, stmt->arg);
Radek Krejci0f969882020-08-21 16:56:47 +02001281 } else {
FredGand944bdc2019-11-05 21:57:07 +08001282 ypr_open(ctx, stmt->stmt, stmt_attr_info[stmt->kw].arg, stmt->arg, flag);
1283 }
1284 }
1285
1286 if (stmt->child) {
1287 LEVEL++;
1288 LY_LIST_FOR(stmt->child, childstmt) {
1289 yprp_stmt(ctx, childstmt);
1290 }
1291 LEVEL--;
1292 ypr_close(ctx, stmt->stmt, flag);
1293 }
1294}
1295
1296/**
1297 * @param[in] count Number of extensions to print, 0 to print them all.
1298 */
1299static void
1300yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001301 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count)
FredGand944bdc2019-11-05 21:57:07 +08001302{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001303 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001304 char *str;
1305 struct lysp_stmt *stmt;
1306 const char *argument;
1307 const char *ext_argument;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001308 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001309
1310 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001311 count = LY_ARRAY_COUNT(ext);
FredGand944bdc2019-11-05 21:57:07 +08001312 }
1313 LY_ARRAY_FOR(ext, u) {
1314 if (!count) {
1315 break;
1316 }
1317
1318 count--;
1319 if (ext->insubstmt != substmt || ext->insubstmt_index != substmt_index) {
1320 continue;
1321 }
1322
1323 if (!ext->compiled && ext->yin) {
Michal Vasko5233e962020-08-14 14:26:20 +02001324 ly_print_(ctx->out, "%*s<%s/> <!-- Model comes from different input format, extensions must be resolved first. -->\n", INDENT, ext[u].name);
FredGand944bdc2019-11-05 21:57:07 +08001325 continue;
1326 }
1327
1328 ypr_close_parent(ctx, flag);
Radek Krejci1deb5be2020-08-26 16:43:36 +02001329 inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001330 argument = NULL;
1331 ext_argument = NULL;
1332
1333 if (ext[u].compiled) {
1334 argument = ext[u].compiled->argument;
1335 ext_argument = ext[u].compiled->def->argument;
1336 } else {
1337 argument = ext[u].argument;
1338 }
1339
1340 if (ext->yin) {
1341 ypr_open(ctx, ext[u].name, NULL, NULL, 1);
1342 if (asprintf(&str, "%s:%s", ext[u].compiled->def->module->prefix, ext_argument) == -1) {
1343 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +08001344 return;
1345 }
1346 LEVEL++;
1347 inner_flag = 1;
1348 ypr_yin_arg(ctx, str, argument);
1349 free(str);
1350 str = NULL;
1351 LEVEL--;
1352 } else {
1353 ypr_open(ctx, ext[u].name, ext_argument, argument, inner_flag);
1354 }
1355
1356 LEVEL++;
1357 LY_LIST_FOR(ext[u].child, stmt) {
1358 ypr_close_parent(ctx, &inner_flag);
1359 yprp_stmt(ctx, stmt);
1360 }
1361 LEVEL--;
1362 ypr_close(ctx, ext[u].name, inner_flag);
1363 }
1364}
1365
Michal Vasko7c8439f2020-08-05 13:25:19 +02001366static void
1367yin_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001368{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001369 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001370
FredGand944bdc2019-11-05 21:57:07 +08001371 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001372 ypr_open(ctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001373 LEVEL++;
1374 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
1375 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
1376 if (modp->imports[u].rev[0]) {
1377 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
1378 }
1379 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1380 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
1381 LEVEL--;
1382 ypr_close(ctx, "import", 1);
1383 }
1384 LY_ARRAY_FOR(modp->includes, u) {
1385 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001386 ypr_open(ctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001387 LEVEL++;
1388 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
1389 if (modp->includes[u].rev[0]) {
1390 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
1391 }
1392 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1393 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
1394 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001395 ly_print_(ctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001396 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001397 ypr_open(ctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001398 }
1399 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001400}
FredGand944bdc2019-11-05 21:57:07 +08001401
Michal Vasko7c8439f2020-08-05 13:25:19 +02001402static void
1403yin_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
1404{
1405 LY_ARRAY_COUNT_TYPE u;
1406 struct lysp_node *data;
FredGand944bdc2019-11-05 21:57:07 +08001407
FredGand944bdc2019-11-05 21:57:07 +08001408 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001409 ly_print_(ctx->out, "\n");
FredGand944bdc2019-11-05 21:57:07 +08001410 yprp_extension(ctx, &modp->extensions[u]);
1411 }
1412 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001413 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001414 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001415 }
1416
1417 LY_ARRAY_FOR(modp->features, u) {
1418 yprp_feature(ctx, &modp->features[u]);
1419 }
1420
1421 LY_ARRAY_FOR(modp->identities, u) {
1422 yprp_identity(ctx, &modp->identities[u]);
1423 }
1424
1425 LY_ARRAY_FOR(modp->typedefs, u) {
1426 yprp_typedef(ctx, &modp->typedefs[u]);
1427 }
1428
1429 LY_ARRAY_FOR(modp->groupings, u) {
1430 yprp_grouping(ctx, &modp->groupings[u]);
1431 }
1432
1433 LY_LIST_FOR(modp->data, data) {
1434 yprp_node(ctx, data);
1435 }
1436
1437 LY_ARRAY_FOR(modp->augments, u) {
1438 yprp_augment(ctx, &modp->augments[u]);
1439 }
1440
1441 LY_ARRAY_FOR(modp->rpcs, u) {
1442 yprp_action(ctx, &modp->rpcs[u]);
1443 }
1444
1445 LY_ARRAY_FOR(modp->notifs, u) {
1446 yprp_notification(ctx, &modp->notifs[u]);
1447 }
1448
1449 LY_ARRAY_FOR(modp->deviations, u) {
1450 yprp_deviation(ctx, &modp->deviations[u]);
1451 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001452}
1453
1454LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001455yin_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001456{
1457 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02001458 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001459
Michal Vasko5233e962020-08-14 14:26:20 +02001460 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1461 ly_print_(ctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001462 ypr_xmlns(ctx, module, 8);
1463 ypr_import_xmlns(ctx, modp, 8);
Michal Vasko5233e962020-08-14 14:26:20 +02001464 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001465
1466 LEVEL++;
1467
1468 /* module-header-stmts */
1469 if (module->version) {
1470 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
1471 }
1472 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
1473 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
1474
1475 /* linkage-stmts (import/include) */
1476 yin_print_parsed_linkage(ctx, modp);
1477
1478 /* meta-stmts */
1479 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001480 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001481 }
1482 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
1483 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
1484 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
1485 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
1486
1487 /* revision-stmts */
1488 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001489 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001490 }
1491 LY_ARRAY_FOR(modp->revs, u) {
1492 yprp_revision(ctx, &modp->revs[u]);
1493 }
1494
1495 /* body-stmts */
1496 yin_print_parsed_body(ctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001497
1498 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001499 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001500 ly_print_flush(out);
1501
1502 return LY_SUCCESS;
1503}
1504
Michal Vasko7c8439f2020-08-05 13:25:19 +02001505static void
1506yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
1507{
1508 ypr_open(ctx, "belongs-to", "module", submodp->belongsto, 1);
1509 LEVEL++;
1510 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
1511 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
1512 LEVEL--;
1513 ypr_close(ctx, "belongs-to", 1);
1514}
1515
1516LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001517yin_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001518{
1519 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02001520 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001521
Michal Vasko5233e962020-08-14 14:26:20 +02001522 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1523 ly_print_(ctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001524 ypr_xmlns(ctx, module, 8);
1525 ypr_import_xmlns(ctx, (struct lysp_module *)submodp, 8);
Michal Vasko5233e962020-08-14 14:26:20 +02001526 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001527
1528 LEVEL++;
1529
1530 /* submodule-header-stmts */
1531 if (submodp->version) {
1532 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
1533 }
1534 yprp_belongsto(ctx, submodp);
1535
1536 /* linkage-stmts (import/include) */
1537 yin_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
1538
1539 /* meta-stmts */
1540 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001541 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001542 }
1543 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1544 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
1545 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1546 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
1547
1548 /* revision-stmts */
1549 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001550 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001551 }
1552 LY_ARRAY_FOR(submodp->revs, u) {
1553 yprp_revision(ctx, &submodp->revs[u]);
1554 }
1555
1556 /* body-stmts */
1557 yin_print_parsed_body(ctx, (struct lysp_module *)submodp);
1558
1559 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001560 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001561 ly_print_flush(out);
1562
1563 return LY_SUCCESS;
1564}