blob: ccbc1a5f67ad402e2640d77500d1693200867fec [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
Michal Vasko7f45cf22020-10-01 12:49:44 +0200223yprp_iffeatures(struct ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800224{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200225 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200226 int8_t extflag;
FredGand944bdc2019-11-05 21:57:07 +0800227
Michal Vasko7f45cf22020-10-01 12:49:44 +0200228 LY_ARRAY_FOR(iffs, u) {
FredGand944bdc2019-11-05 21:57:07 +0800229 ypr_close_parent(ctx, flag);
230 extflag = 0;
231
Michal Vasko7f45cf22020-10-01 12:49:44 +0200232 ly_print_(ctx->out, "%*s<if-feature name=\"%s", INDENT, iffs[u].str);
FredGand944bdc2019-11-05 21:57:07 +0800233
234 /* extensions */
235 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200236 LY_ARRAY_FOR(exts, v) {
237 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800238 }
239 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200240 ly_print_(ctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800241 }
242}
243
244static void
245yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
246{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200247 int8_t flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200248 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800249
250 ypr_open(ctx, "extension", "name", ext->name, flag);
251 LEVEL++;
252
253 if (ext->exts) {
254 ypr_close_parent(ctx, &flag);
255 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
256 }
257
258 if (ext->argument) {
259 ypr_close_parent(ctx, &flag);
260 ypr_open(ctx, "argument", "name", ext->argument, flag2);
261
262 LEVEL++;
263 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200264 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200265 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LYEXT_SUBSTMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
FredGand944bdc2019-11-05 21:57:07 +0800266 ypr_close_parent(ctx, &flag2);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200267 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
FredGand944bdc2019-11-05 21:57:07 +0800268 }
269 }
270 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200271 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts))) {
FredGand944bdc2019-11-05 21:57:07 +0800272 ypr_close_parent(ctx, &flag2);
273 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
274 }
275 LEVEL--;
276 ypr_close(ctx, "argument", flag2);
277 }
278
279 ypr_status(ctx, ext->flags, ext->exts, &flag);
280 ypr_description(ctx, ext->dsc, ext->exts, &flag);
281 ypr_reference(ctx, ext->ref, ext->exts, &flag);
282
283 LEVEL--;
284 ypr_close(ctx, "extension", flag);
285}
286
287static void
288yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
289{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200290 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800291
292 ypr_open(ctx, "feature", "name", feat->name, flag);
293 LEVEL++;
294 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
295 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
296 ypr_status(ctx, feat->flags, feat->exts, &flag);
297 ypr_description(ctx, feat->dsc, feat->exts, &flag);
298 ypr_reference(ctx, feat->ref, feat->exts, &flag);
299 LEVEL--;
300 ypr_close(ctx, "feature", flag);
301}
302
303static void
304yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
305{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200306 int8_t flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200307 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800308
309 ypr_open(ctx, "identity", "name", ident->name, flag);
310 LEVEL++;
311
312 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
313 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
314
315 LY_ARRAY_FOR(ident->bases, u) {
316 ypr_close_parent(ctx, &flag);
317 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
318 }
319
320 ypr_status(ctx, ident->flags, ident->exts, &flag);
321 ypr_description(ctx, ident->dsc, ident->exts, &flag);
322 ypr_reference(ctx, ident->ref, ident->exts, &flag);
323
324 LEVEL--;
325 ypr_close(ctx, "identity", flag);
326}
327
328static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200329yprp_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 +0800330{
331 (void)flag;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200332 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800333
334 if (!restr) {
335 return;
336 }
337
Michal Vasko5233e962020-08-14 14:26:20 +0200338 ly_print_(ctx->out, "%*s<%s %s=\"", INDENT, name, attr);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200339 lyxml_dump_text(ctx->out, (restr->arg.str[0] != 0x15 && restr->arg.str[0] != 0x06) ? restr->arg.str : &restr->arg.str[1], 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200340 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800341
342 LEVEL++;
343 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200344 if (restr->arg.str[0] == 0x15) {
FredGand944bdc2019-11-05 21:57:07 +0800345 ypr_close_parent(ctx, &inner_flag);
346 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
347 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
348 }
349 if (restr->emsg) {
350 ypr_close_parent(ctx, &inner_flag);
351 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
352 }
353 if (restr->eapptag) {
354 ypr_close_parent(ctx, &inner_flag);
355 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
356 }
357 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
358 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
359
360 LEVEL--;
361 ypr_close(ctx, name, inner_flag);
362}
363
364static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200365yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800366{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200367 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800368 (void)flag;
369
370 if (!when) {
371 return;
372 }
373
Michal Vasko5233e962020-08-14 14:26:20 +0200374 ly_print_(ctx->out, "%*s<when condition=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800375 lyxml_dump_text(ctx->out, when->cond, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200376 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800377
378 LEVEL++;
379 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
380 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
381 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
382 LEVEL--;
383 ypr_close(ctx, "when", inner_flag);
384}
385
386static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200387yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800388{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200389 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200390 int8_t inner_flag;
FredGand944bdc2019-11-05 21:57:07 +0800391 (void)flag;
392
393 LY_ARRAY_FOR(items, u) {
394 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200395 ly_print_(ctx->out, "%*s<bit name=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800396 lyxml_dump_text(ctx->out, items[u].name, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200397 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800398 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200399 ly_print_(ctx->out, "%*s<enum name=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800400 lyxml_dump_text(ctx->out, items[u].name, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200401 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800402 }
403 inner_flag = 0;
404 LEVEL++;
405 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
406 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
407 if (items[u].flags & LYS_SET_VALUE) {
408 if (type == LY_TYPE_BITS) {
409 ypr_close_parent(ctx, &inner_flag);
410 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value);
411 } else { /* LY_TYPE_ENUM */
412 ypr_close_parent(ctx, &inner_flag);
413 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value);
414 }
415 }
416 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
417 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
418 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
419 LEVEL--;
420 ypr_close(ctx, type == LY_TYPE_BITS ? "bit" : "enum", inner_flag);
421 }
422}
423
424static void
425yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
426{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200427 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200428 int8_t flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200429
FredGand944bdc2019-11-05 21:57:07 +0800430 if (!ctx || !type) {
431 return;
432 }
433
FredGand944bdc2019-11-05 21:57:07 +0800434 ypr_open(ctx, "type", "name", type->name, flag);
435 LEVEL++;
436
437 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
438
439 if (type->range || type->length || type->patterns || type->bits || type->enums) {
440 ypr_close_parent(ctx, &flag);
441 }
442 yprp_restr(ctx, type->range, "range", "value", &flag);
443 yprp_restr(ctx, type->length, "length", "value", &flag);
444 LY_ARRAY_FOR(type->patterns, u) {
445 yprp_restr(ctx, &type->patterns[u], "pattern", "value", &flag);
446 }
447 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
448 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
449
450 if (type->path) {
451 ypr_close_parent(ctx, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200452 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800453 }
454 if (type->flags & LYS_SET_REQINST) {
455 ypr_close_parent(ctx, &flag);
456 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
457 }
458 if (type->flags & LYS_SET_FRDIGITS) {
459 ypr_close_parent(ctx, &flag);
460 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits);
461 }
462 LY_ARRAY_FOR(type->bases, u) {
463 ypr_close_parent(ctx, &flag);
464 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
465 }
466 LY_ARRAY_FOR(type->types, u) {
467 ypr_close_parent(ctx, &flag);
468 yprp_type(ctx, &type->types[u]);
469 }
470
471 LEVEL--;
472 ypr_close(ctx, "type", flag);
473}
474
475static void
476yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
477{
FredGand944bdc2019-11-05 21:57:07 +0800478 ypr_open(ctx, "typedef", "name", tpdf->name, 1);
479 LEVEL++;
480
481 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
482
483 yprp_type(ctx, &tpdf->type);
484
485 if (tpdf->units) {
486 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
487 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200488 if (tpdf->dflt.str) {
489 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800490 }
491
492 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
493 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
494 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
495
496 LEVEL--;
497 ypr_close(ctx, "typedef", 1);
498}
499
500static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
501static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
502
503static void
504yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
505{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200506 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200507 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800508 struct lysp_node *data;
509
FredGand944bdc2019-11-05 21:57:07 +0800510 ypr_open(ctx, "grouping", "name", grp->name, flag);
511 LEVEL++;
512
513 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
514 ypr_status(ctx, grp->flags, grp->exts, &flag);
515 ypr_description(ctx, grp->dsc, grp->exts, &flag);
516 ypr_reference(ctx, grp->ref, grp->exts, &flag);
517
518 LY_ARRAY_FOR(grp->typedefs, u) {
519 ypr_close_parent(ctx, &flag);
520 yprp_typedef(ctx, &grp->typedefs[u]);
521 }
522
523 LY_ARRAY_FOR(grp->groupings, u) {
524 ypr_close_parent(ctx, &flag);
525 yprp_grouping(ctx, &grp->groupings[u]);
526 }
527
528 LY_LIST_FOR(grp->data, data) {
529 ypr_close_parent(ctx, &flag);
530 yprp_node(ctx, data);
531 }
532
533 LY_ARRAY_FOR(grp->actions, u) {
534 ypr_close_parent(ctx, &flag);
535 yprp_action(ctx, &grp->actions[u]);
536 }
537
538 LEVEL--;
539 ypr_close(ctx, "grouping", flag);
540}
541
542static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200543yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800544{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200545 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800546 struct lysp_node *data;
547
Michal Vasko7f45cf22020-10-01 12:49:44 +0200548 if (!inout->data) {
549 /* input/output is empty */
FredGand944bdc2019-11-05 21:57:07 +0800550 return;
551 }
552 ypr_close_parent(ctx, flag);
553
554 ypr_open(ctx, (inout->nodetype == LYS_INPUT ? "input" : "output"), NULL, NULL, *flag);
555 LEVEL++;
556
557 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
558 LY_ARRAY_FOR(inout->musts, u) {
559 yprp_restr(ctx, &inout->musts[u], "must", "condition", NULL);
560 }
561 LY_ARRAY_FOR(inout->typedefs, u) {
562 yprp_typedef(ctx, &inout->typedefs[u]);
563 }
564 LY_ARRAY_FOR(inout->groupings, u) {
565 yprp_grouping(ctx, &inout->groupings[u]);
566 }
567
568 LY_LIST_FOR(inout->data, data) {
569 yprp_node(ctx, data);
570 }
571
572 LEVEL--;
573 ypr_close(ctx, (inout->nodetype == LYS_INPUT ? "input" : "output"), 1);
574}
575
576static void
577yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
578{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200579 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200580 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800581 struct lysp_node *data;
582
FredGand944bdc2019-11-05 21:57:07 +0800583 ypr_open(ctx, "notification", "name", notif->name, flag);
584
585 LEVEL++;
586 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
587 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
588
589 LY_ARRAY_FOR(notif->musts, u) {
590 ypr_close_parent(ctx, &flag);
591 yprp_restr(ctx, &notif->musts[u], "must", "condition", &flag);
592 }
593 ypr_status(ctx, notif->flags, notif->exts, &flag);
594 ypr_description(ctx, notif->dsc, notif->exts, &flag);
595 ypr_reference(ctx, notif->ref, notif->exts, &flag);
596
597 LY_ARRAY_FOR(notif->typedefs, u) {
598 ypr_close_parent(ctx, &flag);
599 yprp_typedef(ctx, &notif->typedefs[u]);
600 }
601
602 LY_ARRAY_FOR(notif->groupings, u) {
603 ypr_close_parent(ctx, &flag);
604 yprp_grouping(ctx, &notif->groupings[u]);
605 }
606
607 LY_LIST_FOR(notif->data, data) {
608 ypr_close_parent(ctx, &flag);
609 yprp_node(ctx, data);
610 }
611
612 LEVEL--;
613 ypr_close(ctx, "notification", flag);
614}
615
616static void
617yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
618{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200619 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200620 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800621
FredGand944bdc2019-11-05 21:57:07 +0800622 ypr_open(ctx, action->parent ? "action" : "rpc", "name", action->name, flag);
623
624 LEVEL++;
625 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
626 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
627 ypr_status(ctx, action->flags, action->exts, &flag);
628 ypr_description(ctx, action->dsc, action->exts, &flag);
629 ypr_reference(ctx, action->ref, action->exts, &flag);
630
631 LY_ARRAY_FOR(action->typedefs, u) {
632 ypr_close_parent(ctx, &flag);
633 yprp_typedef(ctx, &action->typedefs[u]);
634 }
635
636 LY_ARRAY_FOR(action->groupings, u) {
637 ypr_close_parent(ctx, &flag);
638 yprp_grouping(ctx, &action->groupings[u]);
639 }
640
641 yprp_inout(ctx, &action->input, &flag);
642 yprp_inout(ctx, &action->output, &flag);
643
644 LEVEL--;
645 ypr_close(ctx, action->parent ? "action" : "rpc", flag);
646}
647
648static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200649yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800650{
651 ypr_open(ctx, lys_nodetype2str(node->nodetype), "name", node->name, *flag);
652 LEVEL++;
653
654 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
655 yprp_when(ctx, node->when, flag);
656 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
657}
658
659static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200660yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800661{
662 ypr_config(ctx, node->flags, node->exts, flag);
663 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) {
664 ypr_mandatory(ctx, node->flags, node->exts, flag);
665 }
666 ypr_status(ctx, node->flags, node->exts, flag);
667 ypr_description(ctx, node->dsc, node->exts, flag);
668 ypr_reference(ctx, node->ref, node->exts, flag);
669}
670
671static void
672yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
673{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200674 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200675 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800676 struct lysp_node *child;
677 struct lysp_node_container *cont = (struct lysp_node_container *)node;
678
679 yprp_node_common1(ctx, node, &flag);
680
681 LY_ARRAY_FOR(cont->musts, u) {
682 ypr_close_parent(ctx, &flag);
683 yprp_restr(ctx, &cont->musts[u], "must", "condition", &flag);
684 }
685 if (cont->presence) {
686 ypr_close_parent(ctx, &flag);
687 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
688 }
689
690 yprp_node_common2(ctx, node, &flag);
691
692 LY_ARRAY_FOR(cont->typedefs, u) {
693 ypr_close_parent(ctx, &flag);
694 yprp_typedef(ctx, &cont->typedefs[u]);
695 }
696
697 LY_ARRAY_FOR(cont->groupings, u) {
698 ypr_close_parent(ctx, &flag);
699 yprp_grouping(ctx, &cont->groupings[u]);
700 }
701
702 LY_LIST_FOR(cont->child, child) {
703 ypr_close_parent(ctx, &flag);
704 yprp_node(ctx, child);
705 }
706
707 LY_ARRAY_FOR(cont->actions, u) {
708 ypr_close_parent(ctx, &flag);
709 yprp_action(ctx, &cont->actions[u]);
710 }
711
712 LY_ARRAY_FOR(cont->notifs, u) {
713 ypr_close_parent(ctx, &flag);
714 yprp_notification(ctx, &cont->notifs[u]);
715 }
716
717 LEVEL--;
718 ypr_close(ctx, "container", flag);
719}
720
721static void
722yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
723{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200724 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800725 struct lysp_node *child;
726 struct lysp_node_case *cas = (struct lysp_node_case *)node;
727
728 yprp_node_common1(ctx, node, &flag);
729 yprp_node_common2(ctx, node, &flag);
730
731 LY_LIST_FOR(cas->child, child) {
732 ypr_close_parent(ctx, &flag);
733 yprp_node(ctx, child);
734 }
735
736 LEVEL--;
737 ypr_close(ctx, "case", flag);
738}
739
740static void
741yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
742{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200743 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800744 struct lysp_node *child;
745 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
746
747 yprp_node_common1(ctx, node, &flag);
748
Michal Vasko7f45cf22020-10-01 12:49:44 +0200749 if (choice->dflt.str) {
FredGand944bdc2019-11-05 21:57:07 +0800750 ypr_close_parent(ctx, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200751 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt.str, choice->exts);
FredGand944bdc2019-11-05 21:57:07 +0800752 }
753
754 yprp_node_common2(ctx, node, &flag);
755
756 LY_LIST_FOR(choice->child, child) {
757 ypr_close_parent(ctx, &flag);
758 yprp_node(ctx, child);
759 }
760
761 LEVEL--;
762 ypr_close(ctx, "choice", flag);
763}
764
765static void
766yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
767{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200768 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800769 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
770
Radek Krejci1deb5be2020-08-26 16:43:36 +0200771 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800772 yprp_node_common1(ctx, node, &flag);
773
774 yprp_type(ctx, &leaf->type);
775 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
776 LY_ARRAY_FOR(leaf->musts, u) {
777 yprp_restr(ctx, &leaf->musts[u], "must", "condition", &flag);
778 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200779 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800780
781 yprp_node_common2(ctx, node, &flag);
782
783 LEVEL--;
784 ypr_close(ctx, "leaf", flag);
785}
786
787static void
788yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
789{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200790 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800791 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200792 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800793
794 yprp_node_common1(ctx, node, &flag);
795
796 yprp_type(ctx, &llist->type);
797 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
798 LY_ARRAY_FOR(llist->musts, u) {
799 yprp_restr(ctx, &llist->musts[u], "must", "condition", NULL);
800 }
801 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200802 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800803 }
804
805 ypr_config(ctx, node->flags, node->exts, NULL);
806
807 if (llist->flags & LYS_SET_MIN) {
808 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min);
809 }
810 if (llist->flags & LYS_SET_MAX) {
811 if (llist->max) {
812 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max);
813 } else {
814 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
815 }
816 }
817
818 if (llist->flags & LYS_ORDBY_MASK) {
819 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
820 }
821
822 ypr_status(ctx, node->flags, node->exts, &flag);
823 ypr_description(ctx, node->dsc, node->exts, &flag);
824 ypr_reference(ctx, node->ref, node->exts, &flag);
825
826 LEVEL--;
827 ypr_close(ctx, "leaf-list", flag);
828}
829
830static void
831yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
832{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200833 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200834 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800835 struct lysp_node *child;
836 struct lysp_node_list *list = (struct lysp_node_list *)node;
837
838 yprp_node_common1(ctx, node, &flag);
839
840 LY_ARRAY_FOR(list->musts, u) {
841 ypr_close_parent(ctx, &flag);
842 yprp_restr(ctx, &list->musts[u], "must", "condition", &flag);
843 }
844 if (list->key) {
845 ypr_close_parent(ctx, &flag);
846 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
847 }
848 LY_ARRAY_FOR(list->uniques, u) {
849 ypr_close_parent(ctx, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200850 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u].str, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800851 }
852
853 ypr_config(ctx, node->flags, node->exts, NULL);
854
855 if (list->flags & LYS_SET_MIN) {
856 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min);
857 }
858 if (list->flags & LYS_SET_MAX) {
859 if (list->max) {
860 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max);
861 } else {
862 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
863 }
864 }
865
866 if (list->flags & LYS_ORDBY_MASK) {
867 ypr_close_parent(ctx, &flag);
868 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
869 }
870
871 ypr_status(ctx, node->flags, node->exts, &flag);
872 ypr_description(ctx, node->dsc, node->exts, &flag);
873 ypr_reference(ctx, node->ref, node->exts, &flag);
874
875 LY_ARRAY_FOR(list->typedefs, u) {
876 ypr_close_parent(ctx, &flag);
877 yprp_typedef(ctx, &list->typedefs[u]);
878 }
879
880 LY_ARRAY_FOR(list->groupings, u) {
881 ypr_close_parent(ctx, &flag);
882 yprp_grouping(ctx, &list->groupings[u]);
883 }
884
885 LY_LIST_FOR(list->child, child) {
886 ypr_close_parent(ctx, &flag);
887 yprp_node(ctx, child);
888 }
889
890 LY_ARRAY_FOR(list->actions, u) {
891 ypr_close_parent(ctx, &flag);
892 yprp_action(ctx, &list->actions[u]);
893 }
894
895 LY_ARRAY_FOR(list->notifs, u) {
896 ypr_close_parent(ctx, &flag);
897 yprp_notification(ctx, &list->notifs[u]);
898 }
899
900 LEVEL--;
901 ypr_close(ctx, "list", flag);
902}
903
904static void
905yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
906{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200907 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200908 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800909
910 ypr_open(ctx, "refine", "target-node", refine->nodeid, flag);
911 LEVEL++;
912
913 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
914 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
915
916 LY_ARRAY_FOR(refine->musts, u) {
917 ypr_close_parent(ctx, &flag);
918 yprp_restr(ctx, &refine->musts[u], "must", "condition", &flag);
919 }
920
921 if (refine->presence) {
922 ypr_close_parent(ctx, &flag);
923 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
924 }
925
926 LY_ARRAY_FOR(refine->dflts, u) {
927 ypr_close_parent(ctx, &flag);
928 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
929 }
930
931 ypr_config(ctx, refine->flags, refine->exts, &flag);
932 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
933
934 if (refine->flags & LYS_SET_MIN) {
935 ypr_close_parent(ctx, &flag);
936 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min);
937 }
938 if (refine->flags & LYS_SET_MAX) {
939 ypr_close_parent(ctx, &flag);
940 if (refine->max) {
941 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max);
942 } else {
943 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
944 }
945 }
946
947 ypr_description(ctx, refine->dsc, refine->exts, &flag);
948 ypr_reference(ctx, refine->ref, refine->exts, &flag);
949
950 LEVEL--;
951 ypr_close(ctx, "refine", flag);
952}
953
954static void
955yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
956{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200957 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800958 struct lysp_node *child;
959
960 ypr_open(ctx, "augment", "target-node", aug->nodeid, 1);
961 LEVEL++;
962
963 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
964 yprp_when(ctx, aug->when, NULL);
965 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
966 ypr_status(ctx, aug->flags, aug->exts, NULL);
967 ypr_description(ctx, aug->dsc, aug->exts, NULL);
968 ypr_reference(ctx, aug->ref, aug->exts, NULL);
969
970 LY_LIST_FOR(aug->child, child) {
971 yprp_node(ctx, child);
972 }
973
974 LY_ARRAY_FOR(aug->actions, u) {
975 yprp_action(ctx, &aug->actions[u]);
976 }
977
978 LY_ARRAY_FOR(aug->notifs, u) {
979 yprp_notification(ctx, &aug->notifs[u]);
980 }
981
982 LEVEL--;
983 ypr_close(ctx, "augment", 1);
984}
985
FredGand944bdc2019-11-05 21:57:07 +0800986static void
987yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
988{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200989 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200990 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800991 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
992
993 yprp_node_common1(ctx, node, &flag);
994 yprp_node_common2(ctx, node, &flag);
995
996 LY_ARRAY_FOR(uses->refines, u) {
997 ypr_close_parent(ctx, &flag);
998 yprp_refine(ctx, &uses->refines[u]);
999 }
1000
1001 LY_ARRAY_FOR(uses->augments, u) {
1002 ypr_close_parent(ctx, &flag);
1003 yprp_augment(ctx, &uses->augments[u]);
1004 }
1005
1006 LEVEL--;
1007 ypr_close(ctx, "uses", flag);
1008}
1009
1010static void
1011yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
1012{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001013 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001014 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001015 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1016
1017 yprp_node_common1(ctx, node, &flag);
1018
1019 LY_ARRAY_FOR(any->musts, u) {
1020 ypr_close_parent(ctx, &flag);
1021 yprp_restr(ctx, &any->musts[u], "must", "condition", &flag);
1022 }
1023
1024 yprp_node_common2(ctx, node, &flag);
1025
1026 LEVEL--;
1027 ypr_close(ctx, lys_nodetype2str(node->nodetype), flag);
1028}
1029
1030static void
1031yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
1032{
FredGand944bdc2019-11-05 21:57:07 +08001033 switch (node->nodetype) {
1034 case LYS_CONTAINER:
1035 yprp_container(ctx, node);
1036 break;
1037 case LYS_CHOICE:
1038 yprp_choice(ctx, node);
1039 break;
1040 case LYS_LEAF:
1041 yprp_leaf(ctx, node);
1042 break;
1043 case LYS_LEAFLIST:
1044 yprp_leaflist(ctx, node);
1045 break;
1046 case LYS_LIST:
1047 yprp_list(ctx, node);
1048 break;
1049 case LYS_USES:
1050 yprp_uses(ctx, node);
1051 break;
1052 case LYS_ANYXML:
1053 case LYS_ANYDATA:
1054 yprp_anydata(ctx, node);
1055 break;
1056 case LYS_CASE:
1057 yprp_case(ctx, node);
1058 break;
1059 default:
1060 break;
1061 }
1062}
1063
1064static void
1065yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
1066{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001067 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001068 struct lysp_deviate_add *add;
1069 struct lysp_deviate_rpl *rpl;
1070 struct lysp_deviate_del *del;
1071 struct lysp_deviate *elem;
1072
1073 ypr_open(ctx, "deviation", "target-node", deviation->nodeid, 1);
1074 LEVEL++;
1075
1076 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
1077 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1078 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1079
1080 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001081 ly_print_(ctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001082 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1083 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001084 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001085 LEVEL++;
1086
1087 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
1088 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001089 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001090 continue;
1091 }
1092 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001093 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001094 ly_print_(ctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001095 LEVEL++;
1096
1097 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
1098 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001099 LY_ARRAY_FOR(add->musts, u) {
1100 yprp_restr(ctx, &add->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001101 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001102 LY_ARRAY_FOR(add->uniques, u) {
1103 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001104 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001105 LY_ARRAY_FOR(add->dflts, u) {
1106 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001107 }
1108 ypr_config(ctx, add->flags, add->exts, NULL);
1109 ypr_mandatory(ctx, add->flags, add->exts, NULL);
1110 if (add->flags & LYS_SET_MIN) {
1111 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min);
1112 }
1113 if (add->flags & LYS_SET_MAX) {
1114 if (add->max) {
1115 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max);
1116 } else {
1117 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
1118 }
1119 }
1120 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001121 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001122 ly_print_(ctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001123 LEVEL++;
1124
1125 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
1126 if (rpl->type) {
1127 yprp_type(ctx, rpl->type);
1128 }
1129 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
1130 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
1131 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
1132 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
1133 if (rpl->flags & LYS_SET_MIN) {
1134 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min);
1135 }
1136 if (rpl->flags & LYS_SET_MAX) {
1137 if (rpl->max) {
1138 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max);
1139 } else {
1140 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
1141 }
1142 }
1143 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001144 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001145 ly_print_(ctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001146 LEVEL++;
1147
1148 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
1149 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001150 LY_ARRAY_FOR(del->musts, u) {
1151 yprp_restr(ctx, &del->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001152 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001153 LY_ARRAY_FOR(del->uniques, u) {
1154 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001155 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001156 LY_ARRAY_FOR(del->dflts, u) {
1157 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001158 }
1159 }
1160
1161 LEVEL--;
1162 ypr_close(ctx, "deviate", 1);
1163 }
1164
1165 LEVEL--;
1166 ypr_close(ctx, "deviation", 1);
1167}
1168
FredGand944bdc2019-11-05 21:57:07 +08001169static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02001170ypr_xmlns(struct ypr_ctx *ctx, const struct lys_module *module, uint16_t indent)
FredGand944bdc2019-11-05 21:57:07 +08001171{
Michal Vasko5233e962020-08-14 14:26:20 +02001172 ly_print_(ctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1173 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001174}
FredGand944bdc2019-11-05 21:57:07 +08001175
Michal Vasko7c8439f2020-08-05 13:25:19 +02001176static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02001177ypr_import_xmlns(struct ypr_ctx *ctx, const struct lysp_module *modp, uint16_t indent)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001178{
1179 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001180
1181 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko5233e962020-08-14 14:26:20 +02001182 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 +08001183 }
1184}
1185
1186struct ext_substmt_info_s stmt_attr_info[] = {
1187 {NULL, NULL, 0}, /**< LY_STMT_NONE*/
1188 {"status", "value", SUBST_FLAG_ID}, /**< LY_STMT_STATUS */
1189 {"config", "value", SUBST_FLAG_ID}, /**< LY_STMT_CONFIG */
1190 {"mandatory", "value", SUBST_FLAG_ID}, /**< LY_STMT_MANDATORY */
1191 {"units", "name", SUBST_FLAG_ID}, /**< LY_STMT_UNITS */
1192 {"default", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEFAULT */
1193 {"type", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPE */
1194 {"action", "name", SUBST_FLAG_ID}, /**< LY_STMT_ACTION */
1195 {"anydata", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYDATA */
1196 {"anyxml", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYXML */
1197 {"argument", "name", SUBST_FLAG_ID}, /**< LY_STMT_ARGUMENT */
1198 {"augment", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_AUGMENT */
1199 {"base", "name", SUBST_FLAG_ID}, /**< LY_STMT_BASE */
1200 {"belongs-to", "module", SUBST_FLAG_ID}, /**< LY_STMT_BELONGS_TO */
1201 {"bit", "name", SUBST_FLAG_ID}, /**< LY_STMT_BIT */
1202 {"case", "name", SUBST_FLAG_ID}, /**< LY_STMT_CASE */
1203 {"choice", "name", SUBST_FLAG_ID}, /**< LY_STMT_CHOICE */
1204 {"contact", "text", SUBST_FLAG_YIN},/**< LY_STMT_CONTACT */
1205 {"container", "name", SUBST_FLAG_ID}, /**< LY_STMT_CONTAINER */
1206 {"description", "text", SUBST_FLAG_YIN},/**< LY_STMT_DESCRIPTION */
1207 {"deviate", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATE */
1208 {"deviation", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATION */
1209 {"enum", "name", SUBST_FLAG_ID}, /**< LY_STMT_ENUM */
1210 {"error-app-tag", "value", SUBST_FLAG_ID}, /**< LY_STMT_ERROR_APP_TAG */
1211 {"error-message", "value", SUBST_FLAG_YIN},/**< LY_STMT_ERROR_MESSAGE */
1212 {"extension", "name", SUBST_FLAG_ID}, /**< LY_STMT_EXTENSION */
1213 {"feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_FEATURE */
1214 {"fraction-digits", "value", SUBST_FLAG_ID}, /**< LY_STMT_FRACTION_DIGITS */
1215 {"grouping", "name", SUBST_FLAG_ID}, /**< LY_STMT_GROUPING */
1216 {"identity", "name", SUBST_FLAG_ID}, /**< LY_STMT_IDENTITY */
1217 {"if-feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_IF_FEATURE */
1218 {"import", "module", SUBST_FLAG_ID}, /**< LY_STMT_IMPORT */
1219 {"include", "module", SUBST_FLAG_ID}, /**< LY_STMT_INCLUDE */
1220 {"input", NULL, 0}, /**< LY_STMT_INPUT */
1221 {"key", "value", SUBST_FLAG_ID}, /**< LY_STMT_KEY */
1222 {"leaf", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF */
1223 {"leaf-list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF_LIST */
1224 {"length", "value", SUBST_FLAG_ID}, /**< LY_STMT_LENGTH */
1225 {"list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LIST */
1226 {"max-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MAX_ELEMENTS */
1227 {"min-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MIN_ELEMENTS */
1228 {"modifier", "value", SUBST_FLAG_ID}, /**< LY_STMT_MODIFIER */
1229 {"module", "name", SUBST_FLAG_ID}, /**< LY_STMT_MODULE */
1230 {"must", "condition", SUBST_FLAG_ID}, /**< LY_STMT_MUST */
1231 {"namespace", "uri", SUBST_FLAG_ID}, /**< LY_STMT_NAMESPACE */
1232 {"notification", "name", SUBST_FLAG_ID}, /**< LY_STMT_NOTIFICATION */
1233 {"ordered-by", "value", SUBST_FLAG_ID}, /**< LY_STMT_ORDERED_BY */
1234 {"organization", "text", SUBST_FLAG_YIN},/**< LY_STMT_ORGANIZATION */
1235 {"output", NULL, 0}, /**< LY_STMT_OUTPUT */
1236 {"path", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATH */
1237 {"pattern", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATTERN */
1238 {"position", "value", SUBST_FLAG_ID}, /**< LY_STMT_POSITION */
1239 {"prefix", "value", SUBST_FLAG_ID}, /**< LY_STMT_PREFIX */
1240 {"presence", "value", SUBST_FLAG_ID}, /**< LY_STMT_PRESENCE */
1241 {"range", "value", SUBST_FLAG_ID}, /**< LY_STMT_RANGE */
1242 {"reference", "text", SUBST_FLAG_YIN},/**< LY_STMT_REFERENCE */
1243 {"refine", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_REFINE */
1244 {"require-instance", "value", SUBST_FLAG_ID}, /**< LY_STMT_REQUIRE_INSTANCE */
1245 {"revision", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION */
1246 {"revision-date", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION_DATE */
1247 {"rpc", "name", SUBST_FLAG_ID}, /**< LY_STMT_RPC */
1248 {"submodule", "name", SUBST_FLAG_ID}, /**< LY_STMT_SUBMODULE */
1249 {"typedef", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPEDEF */
1250 {"unique", "tag", SUBST_FLAG_ID}, /**< LY_STMT_UNIQUE */
1251 {"uses", "name", SUBST_FLAG_ID}, /**< LY_STMT_USES */
1252 {"value", "value", SUBST_FLAG_ID}, /**< LY_STMT_VALUE */
1253 {"when", "condition", SUBST_FLAG_ID}, /**< LY_STMT_WHEN */
1254 {"yang-version", "value", SUBST_FLAG_ID}, /**< LY_STMT_YANG_VERSION */
1255 {"yin-element", "value", SUBST_FLAG_ID}, /**< LY_STMT_YIN_ELEMENT */
1256 {NULL, NULL, 0}, /**< LY_STMT_EXTENSION_INSTANCE */
1257 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_SEMICOLON */
1258 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_LEFT_BRACE */
1259 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_RIGHT_BRACE */
1260 {NULL, NULL, 0}, /**< LY_STMT_ARG_TEXT */
1261 {NULL, NULL, 0}, /**< LY_STMT_ARG_VALUE */
1262};
1263
1264static void
1265yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
1266{
1267 struct lysp_stmt *childstmt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001268 int8_t flag = stmt->child ? 1 : -1;
FredGand944bdc2019-11-05 21:57:07 +08001269
1270 /* TODO:
1271 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
1272 cannot find the compiled information, so it is needed to be done,
1273 currently it is ignored */
Michal Vaskod989ba02020-08-24 10:59:24 +02001274 if (stmt_attr_info[stmt->kw].name) {
1275 if (stmt_attr_info[stmt->kw].flags & SUBST_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +08001276 ypr_open(ctx, stmt->stmt, NULL, NULL, flag);
1277 ypr_yin_arg(ctx, stmt_attr_info[stmt->kw].arg, stmt->arg);
Radek Krejci0f969882020-08-21 16:56:47 +02001278 } else {
FredGand944bdc2019-11-05 21:57:07 +08001279 ypr_open(ctx, stmt->stmt, stmt_attr_info[stmt->kw].arg, stmt->arg, flag);
1280 }
1281 }
1282
1283 if (stmt->child) {
1284 LEVEL++;
1285 LY_LIST_FOR(stmt->child, childstmt) {
1286 yprp_stmt(ctx, childstmt);
1287 }
1288 LEVEL--;
1289 ypr_close(ctx, stmt->stmt, flag);
1290 }
1291}
1292
1293/**
1294 * @param[in] count Number of extensions to print, 0 to print them all.
1295 */
1296static void
1297yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001298 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count)
FredGand944bdc2019-11-05 21:57:07 +08001299{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001300 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001301 char *str;
1302 struct lysp_stmt *stmt;
1303 const char *argument;
1304 const char *ext_argument;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001305 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001306
1307 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001308 count = LY_ARRAY_COUNT(ext);
FredGand944bdc2019-11-05 21:57:07 +08001309 }
1310 LY_ARRAY_FOR(ext, u) {
1311 if (!count) {
1312 break;
1313 }
1314
1315 count--;
1316 if (ext->insubstmt != substmt || ext->insubstmt_index != substmt_index) {
1317 continue;
1318 }
1319
1320 if (!ext->compiled && ext->yin) {
Michal Vasko5233e962020-08-14 14:26:20 +02001321 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 +08001322 continue;
1323 }
1324
1325 ypr_close_parent(ctx, flag);
Radek Krejci1deb5be2020-08-26 16:43:36 +02001326 inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001327 argument = NULL;
1328 ext_argument = NULL;
1329
1330 if (ext[u].compiled) {
1331 argument = ext[u].compiled->argument;
1332 ext_argument = ext[u].compiled->def->argument;
1333 } else {
1334 argument = ext[u].argument;
1335 }
1336
1337 if (ext->yin) {
1338 ypr_open(ctx, ext[u].name, NULL, NULL, 1);
1339 if (asprintf(&str, "%s:%s", ext[u].compiled->def->module->prefix, ext_argument) == -1) {
1340 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +08001341 return;
1342 }
1343 LEVEL++;
1344 inner_flag = 1;
1345 ypr_yin_arg(ctx, str, argument);
1346 free(str);
1347 str = NULL;
1348 LEVEL--;
1349 } else {
1350 ypr_open(ctx, ext[u].name, ext_argument, argument, inner_flag);
1351 }
1352
1353 LEVEL++;
1354 LY_LIST_FOR(ext[u].child, stmt) {
1355 ypr_close_parent(ctx, &inner_flag);
1356 yprp_stmt(ctx, stmt);
1357 }
1358 LEVEL--;
1359 ypr_close(ctx, ext[u].name, inner_flag);
1360 }
1361}
1362
Michal Vasko7c8439f2020-08-05 13:25:19 +02001363static void
1364yin_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001365{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001366 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001367
FredGand944bdc2019-11-05 21:57:07 +08001368 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001369 ypr_open(ctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001370 LEVEL++;
1371 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
1372 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
1373 if (modp->imports[u].rev[0]) {
1374 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
1375 }
1376 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1377 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
1378 LEVEL--;
1379 ypr_close(ctx, "import", 1);
1380 }
1381 LY_ARRAY_FOR(modp->includes, u) {
1382 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 +02001383 ypr_open(ctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001384 LEVEL++;
1385 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
1386 if (modp->includes[u].rev[0]) {
1387 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
1388 }
1389 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1390 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
1391 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001392 ly_print_(ctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001393 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001394 ypr_open(ctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001395 }
1396 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001397}
FredGand944bdc2019-11-05 21:57:07 +08001398
Michal Vasko7c8439f2020-08-05 13:25:19 +02001399static void
1400yin_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
1401{
1402 LY_ARRAY_COUNT_TYPE u;
1403 struct lysp_node *data;
FredGand944bdc2019-11-05 21:57:07 +08001404
FredGand944bdc2019-11-05 21:57:07 +08001405 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001406 ly_print_(ctx->out, "\n");
FredGand944bdc2019-11-05 21:57:07 +08001407 yprp_extension(ctx, &modp->extensions[u]);
1408 }
1409 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001410 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001411 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001412 }
1413
1414 LY_ARRAY_FOR(modp->features, u) {
1415 yprp_feature(ctx, &modp->features[u]);
1416 }
1417
1418 LY_ARRAY_FOR(modp->identities, u) {
1419 yprp_identity(ctx, &modp->identities[u]);
1420 }
1421
1422 LY_ARRAY_FOR(modp->typedefs, u) {
1423 yprp_typedef(ctx, &modp->typedefs[u]);
1424 }
1425
1426 LY_ARRAY_FOR(modp->groupings, u) {
1427 yprp_grouping(ctx, &modp->groupings[u]);
1428 }
1429
1430 LY_LIST_FOR(modp->data, data) {
1431 yprp_node(ctx, data);
1432 }
1433
1434 LY_ARRAY_FOR(modp->augments, u) {
1435 yprp_augment(ctx, &modp->augments[u]);
1436 }
1437
1438 LY_ARRAY_FOR(modp->rpcs, u) {
1439 yprp_action(ctx, &modp->rpcs[u]);
1440 }
1441
1442 LY_ARRAY_FOR(modp->notifs, u) {
1443 yprp_notification(ctx, &modp->notifs[u]);
1444 }
1445
1446 LY_ARRAY_FOR(modp->deviations, u) {
1447 yprp_deviation(ctx, &modp->deviations[u]);
1448 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001449}
1450
1451LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001452yin_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 +02001453{
1454 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02001455 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001456
Michal Vasko5233e962020-08-14 14:26:20 +02001457 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1458 ly_print_(ctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001459 ypr_xmlns(ctx, module, 8);
1460 ypr_import_xmlns(ctx, modp, 8);
Michal Vasko5233e962020-08-14 14:26:20 +02001461 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001462
1463 LEVEL++;
1464
1465 /* module-header-stmts */
1466 if (module->version) {
1467 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
1468 }
1469 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
1470 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
1471
1472 /* linkage-stmts (import/include) */
1473 yin_print_parsed_linkage(ctx, modp);
1474
1475 /* meta-stmts */
1476 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001477 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001478 }
1479 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
1480 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
1481 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
1482 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
1483
1484 /* revision-stmts */
1485 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001486 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001487 }
1488 LY_ARRAY_FOR(modp->revs, u) {
1489 yprp_revision(ctx, &modp->revs[u]);
1490 }
1491
1492 /* body-stmts */
1493 yin_print_parsed_body(ctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001494
1495 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001496 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001497 ly_print_flush(out);
1498
1499 return LY_SUCCESS;
1500}
1501
Michal Vasko7c8439f2020-08-05 13:25:19 +02001502static void
1503yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
1504{
1505 ypr_open(ctx, "belongs-to", "module", submodp->belongsto, 1);
1506 LEVEL++;
1507 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
1508 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
1509 LEVEL--;
1510 ypr_close(ctx, "belongs-to", 1);
1511}
1512
1513LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001514yin_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 +02001515{
1516 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02001517 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001518
Michal Vasko5233e962020-08-14 14:26:20 +02001519 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1520 ly_print_(ctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001521 ypr_xmlns(ctx, module, 8);
1522 ypr_import_xmlns(ctx, (struct lysp_module *)submodp, 8);
Michal Vasko5233e962020-08-14 14:26:20 +02001523 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001524
1525 LEVEL++;
1526
1527 /* submodule-header-stmts */
1528 if (submodp->version) {
1529 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
1530 }
1531 yprp_belongsto(ctx, submodp);
1532
1533 /* linkage-stmts (import/include) */
1534 yin_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
1535
1536 /* meta-stmts */
1537 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001538 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001539 }
1540 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1541 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
1542 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1543 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
1544
1545 /* revision-stmts */
1546 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001547 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001548 }
1549 LY_ARRAY_FOR(submodp->revs, u) {
1550 yprp_revision(ctx, &submodp->revs[u]);
1551 }
1552
1553 /* body-stmts */
1554 yin_print_parsed_body(ctx, (struct lysp_module *)submodp);
1555
1556 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001557 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001558 ly_print_flush(out);
1559
1560 return LY_SUCCESS;
1561}