blob: d995e77ffc0c7064af858ce3d7065583987d9087 [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 Vasko69730152020-10-09 16:30:07 +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;
Michal Vasko69730152020-10-09 16:30:07 +0200130
Radek Krejci1deb5be2020-08-26 16:43:36 +0200131 if (asprintf(&str, "%lu", attr_value) == -1) {
FredGand944bdc2019-11-05 21:57:07 +0800132 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800133 return;
134 }
135 ypr_substmt(ctx, substmt, substmt_index, str, exts);
136 free(str);
137}
138
139static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200140ypr_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 +0800141{
142 char *str;
143
Radek Krejci1deb5be2020-08-26 16:43:36 +0200144 if (asprintf(&str, "%ld", attr_value) == -1) {
FredGand944bdc2019-11-05 21:57:07 +0800145 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800146 return;
147 }
148 ypr_substmt(ctx, substmt, substmt_index, str, exts);
149 free(str);
150}
151
152static void
153yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
154{
155 if (rev->dsc || rev->ref || rev->exts) {
156 ypr_open(ctx, "revision", "date", rev->date, 1);
157 LEVEL++;
158 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
159 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
160 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
161 LEVEL--;
162 ypr_close(ctx, "revision", 1);
163 } else {
164 ypr_open(ctx, "revision", "date", rev->date, -1);
165 }
166}
167
168static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200169ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800170{
171 if (flags & LYS_MAND_MASK) {
172 ypr_close_parent(ctx, flag);
173 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
174 }
175}
176
177static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200178ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800179{
180 if (flags & LYS_CONFIG_MASK) {
181 ypr_close_parent(ctx, flag);
182 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
183 }
184}
185
186static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200187ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800188{
189 const char *status = NULL;
190
191 if (flags & LYS_STATUS_CURR) {
192 ypr_close_parent(ctx, flag);
193 status = "current";
194 } else if (flags & LYS_STATUS_DEPRC) {
195 ypr_close_parent(ctx, flag);
196 status = "deprecated";
197 } else if (flags & LYS_STATUS_OBSLT) {
198 ypr_close_parent(ctx, flag);
199 status = "obsolete";
200 }
201
202 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
203}
204
205static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200206ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800207{
208 if (dsc) {
209 ypr_close_parent(ctx, flag);
210 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
211 }
212}
213
214static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200215ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800216{
217 if (ref) {
218 ypr_close_parent(ctx, flag);
219 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
220 }
221}
222
223static void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200224yprp_iffeatures(struct ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800225{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200226 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200227 int8_t extflag;
FredGand944bdc2019-11-05 21:57:07 +0800228
Michal Vasko7f45cf22020-10-01 12:49:44 +0200229 LY_ARRAY_FOR(iffs, u) {
FredGand944bdc2019-11-05 21:57:07 +0800230 ypr_close_parent(ctx, flag);
231 extflag = 0;
232
Michal Vasko7f45cf22020-10-01 12:49:44 +0200233 ly_print_(ctx->out, "%*s<if-feature name=\"%s", INDENT, iffs[u].str);
FredGand944bdc2019-11-05 21:57:07 +0800234
235 /* extensions */
236 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200237 LY_ARRAY_FOR(exts, v) {
238 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800239 }
240 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200241 ly_print_(ctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800242 }
243}
244
245static void
246yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
247{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200248 int8_t flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200249 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800250
251 ypr_open(ctx, "extension", "name", ext->name, flag);
252 LEVEL++;
253
254 if (ext->exts) {
255 ypr_close_parent(ctx, &flag);
256 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
257 }
258
259 if (ext->argument) {
260 ypr_close_parent(ctx, &flag);
261 ypr_open(ctx, "argument", "name", ext->argument, flag2);
262
263 LEVEL++;
264 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200265 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200266 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LYEXT_SUBSTMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
FredGand944bdc2019-11-05 21:57:07 +0800267 ypr_close_parent(ctx, &flag2);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200268 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
FredGand944bdc2019-11-05 21:57:07 +0800269 }
270 }
271 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vasko69730152020-10-09 16:30:07 +0200272 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts)))) {
FredGand944bdc2019-11-05 21:57:07 +0800273 ypr_close_parent(ctx, &flag2);
274 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
275 }
276 LEVEL--;
277 ypr_close(ctx, "argument", flag2);
278 }
279
280 ypr_status(ctx, ext->flags, ext->exts, &flag);
281 ypr_description(ctx, ext->dsc, ext->exts, &flag);
282 ypr_reference(ctx, ext->ref, ext->exts, &flag);
283
284 LEVEL--;
285 ypr_close(ctx, "extension", flag);
286}
287
288static void
289yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
290{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200291 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800292
293 ypr_open(ctx, "feature", "name", feat->name, flag);
294 LEVEL++;
295 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
296 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
297 ypr_status(ctx, feat->flags, feat->exts, &flag);
298 ypr_description(ctx, feat->dsc, feat->exts, &flag);
299 ypr_reference(ctx, feat->ref, feat->exts, &flag);
300 LEVEL--;
301 ypr_close(ctx, "feature", flag);
302}
303
304static void
305yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
306{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200307 int8_t flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200308 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800309
310 ypr_open(ctx, "identity", "name", ident->name, flag);
311 LEVEL++;
312
313 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
314 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
315
316 LY_ARRAY_FOR(ident->bases, u) {
317 ypr_close_parent(ctx, &flag);
318 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
319 }
320
321 ypr_status(ctx, ident->flags, ident->exts, &flag);
322 ypr_description(ctx, ident->dsc, ident->exts, &flag);
323 ypr_reference(ctx, ident->ref, ident->exts, &flag);
324
325 LEVEL--;
326 ypr_close(ctx, "identity", flag);
327}
328
329static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200330yprp_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 +0800331{
332 (void)flag;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200333 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800334
335 if (!restr) {
336 return;
337 }
338
Michal Vasko5233e962020-08-14 14:26:20 +0200339 ly_print_(ctx->out, "%*s<%s %s=\"", INDENT, name, attr);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200340 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 +0200341 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800342
343 LEVEL++;
344 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200345 if (restr->arg.str[0] == 0x15) {
FredGand944bdc2019-11-05 21:57:07 +0800346 ypr_close_parent(ctx, &inner_flag);
347 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
348 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
349 }
350 if (restr->emsg) {
351 ypr_close_parent(ctx, &inner_flag);
352 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
353 }
354 if (restr->eapptag) {
355 ypr_close_parent(ctx, &inner_flag);
356 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
357 }
358 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
359 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
360
361 LEVEL--;
362 ypr_close(ctx, name, inner_flag);
363}
364
365static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200366yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800367{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200368 int8_t inner_flag = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200369
FredGand944bdc2019-11-05 21:57:07 +0800370 (void)flag;
371
372 if (!when) {
373 return;
374 }
375
Michal Vasko5233e962020-08-14 14:26:20 +0200376 ly_print_(ctx->out, "%*s<when condition=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800377 lyxml_dump_text(ctx->out, when->cond, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200378 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800379
380 LEVEL++;
381 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
382 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
383 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
384 LEVEL--;
385 ypr_close(ctx, "when", inner_flag);
386}
387
388static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200389yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800390{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200391 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200392 int8_t inner_flag;
Michal Vasko69730152020-10-09 16:30:07 +0200393
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 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200491 if (tpdf->dflt.str) {
492 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800493 }
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
Michal Vasko7f45cf22020-10-01 12:49:44 +0200551 if (!inout->data) {
552 /* input/output is empty */
FredGand944bdc2019-11-05 21:57:07 +0800553 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
Michal Vasko7f45cf22020-10-01 12:49:44 +0200752 if (choice->dflt.str) {
FredGand944bdc2019-11-05 21:57:07 +0800753 ypr_close_parent(ctx, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200754 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt.str, choice->exts);
FredGand944bdc2019-11-05 21:57:07 +0800755 }
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;
Michal Vasko69730152020-10-09 16:30:07 +0200775
FredGand944bdc2019-11-05 21:57:07 +0800776 yprp_node_common1(ctx, node, &flag);
777
778 yprp_type(ctx, &leaf->type);
779 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
780 LY_ARRAY_FOR(leaf->musts, u) {
781 yprp_restr(ctx, &leaf->musts[u], "must", "condition", &flag);
782 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200783 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800784
785 yprp_node_common2(ctx, node, &flag);
786
787 LEVEL--;
788 ypr_close(ctx, "leaf", flag);
789}
790
791static void
792yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
793{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200794 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800795 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200796 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800797
798 yprp_node_common1(ctx, node, &flag);
799
800 yprp_type(ctx, &llist->type);
801 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
802 LY_ARRAY_FOR(llist->musts, u) {
803 yprp_restr(ctx, &llist->musts[u], "must", "condition", NULL);
804 }
805 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200806 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800807 }
808
809 ypr_config(ctx, node->flags, node->exts, NULL);
810
811 if (llist->flags & LYS_SET_MIN) {
812 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min);
813 }
814 if (llist->flags & LYS_SET_MAX) {
815 if (llist->max) {
816 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max);
817 } else {
818 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
819 }
820 }
821
822 if (llist->flags & LYS_ORDBY_MASK) {
823 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
824 }
825
826 ypr_status(ctx, node->flags, node->exts, &flag);
827 ypr_description(ctx, node->dsc, node->exts, &flag);
828 ypr_reference(ctx, node->ref, node->exts, &flag);
829
830 LEVEL--;
831 ypr_close(ctx, "leaf-list", flag);
832}
833
834static void
835yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
836{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200837 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200838 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800839 struct lysp_node *child;
840 struct lysp_node_list *list = (struct lysp_node_list *)node;
841
842 yprp_node_common1(ctx, node, &flag);
843
844 LY_ARRAY_FOR(list->musts, u) {
845 ypr_close_parent(ctx, &flag);
846 yprp_restr(ctx, &list->musts[u], "must", "condition", &flag);
847 }
848 if (list->key) {
849 ypr_close_parent(ctx, &flag);
850 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
851 }
852 LY_ARRAY_FOR(list->uniques, u) {
853 ypr_close_parent(ctx, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200854 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u].str, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800855 }
856
857 ypr_config(ctx, node->flags, node->exts, NULL);
858
859 if (list->flags & LYS_SET_MIN) {
860 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min);
861 }
862 if (list->flags & LYS_SET_MAX) {
863 if (list->max) {
864 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max);
865 } else {
866 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
867 }
868 }
869
870 if (list->flags & LYS_ORDBY_MASK) {
871 ypr_close_parent(ctx, &flag);
872 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
873 }
874
875 ypr_status(ctx, node->flags, node->exts, &flag);
876 ypr_description(ctx, node->dsc, node->exts, &flag);
877 ypr_reference(ctx, node->ref, node->exts, &flag);
878
879 LY_ARRAY_FOR(list->typedefs, u) {
880 ypr_close_parent(ctx, &flag);
881 yprp_typedef(ctx, &list->typedefs[u]);
882 }
883
884 LY_ARRAY_FOR(list->groupings, u) {
885 ypr_close_parent(ctx, &flag);
886 yprp_grouping(ctx, &list->groupings[u]);
887 }
888
889 LY_LIST_FOR(list->child, child) {
890 ypr_close_parent(ctx, &flag);
891 yprp_node(ctx, child);
892 }
893
894 LY_ARRAY_FOR(list->actions, u) {
895 ypr_close_parent(ctx, &flag);
896 yprp_action(ctx, &list->actions[u]);
897 }
898
899 LY_ARRAY_FOR(list->notifs, u) {
900 ypr_close_parent(ctx, &flag);
901 yprp_notification(ctx, &list->notifs[u]);
902 }
903
904 LEVEL--;
905 ypr_close(ctx, "list", flag);
906}
907
908static void
909yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
910{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200911 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200912 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800913
914 ypr_open(ctx, "refine", "target-node", refine->nodeid, flag);
915 LEVEL++;
916
917 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
918 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
919
920 LY_ARRAY_FOR(refine->musts, u) {
921 ypr_close_parent(ctx, &flag);
922 yprp_restr(ctx, &refine->musts[u], "must", "condition", &flag);
923 }
924
925 if (refine->presence) {
926 ypr_close_parent(ctx, &flag);
927 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
928 }
929
930 LY_ARRAY_FOR(refine->dflts, u) {
931 ypr_close_parent(ctx, &flag);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200932 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +0800933 }
934
935 ypr_config(ctx, refine->flags, refine->exts, &flag);
936 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
937
938 if (refine->flags & LYS_SET_MIN) {
939 ypr_close_parent(ctx, &flag);
940 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min);
941 }
942 if (refine->flags & LYS_SET_MAX) {
943 ypr_close_parent(ctx, &flag);
944 if (refine->max) {
945 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max);
946 } else {
947 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
948 }
949 }
950
951 ypr_description(ctx, refine->dsc, refine->exts, &flag);
952 ypr_reference(ctx, refine->ref, refine->exts, &flag);
953
954 LEVEL--;
955 ypr_close(ctx, "refine", flag);
956}
957
958static void
959yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
960{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200961 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800962 struct lysp_node *child;
963
964 ypr_open(ctx, "augment", "target-node", aug->nodeid, 1);
965 LEVEL++;
966
967 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
968 yprp_when(ctx, aug->when, NULL);
969 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
970 ypr_status(ctx, aug->flags, aug->exts, NULL);
971 ypr_description(ctx, aug->dsc, aug->exts, NULL);
972 ypr_reference(ctx, aug->ref, aug->exts, NULL);
973
974 LY_LIST_FOR(aug->child, child) {
975 yprp_node(ctx, child);
976 }
977
978 LY_ARRAY_FOR(aug->actions, u) {
979 yprp_action(ctx, &aug->actions[u]);
980 }
981
982 LY_ARRAY_FOR(aug->notifs, u) {
983 yprp_notification(ctx, &aug->notifs[u]);
984 }
985
986 LEVEL--;
987 ypr_close(ctx, "augment", 1);
988}
989
FredGand944bdc2019-11-05 21:57:07 +0800990static void
991yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
992{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200993 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200994 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800995 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
996
997 yprp_node_common1(ctx, node, &flag);
998 yprp_node_common2(ctx, node, &flag);
999
1000 LY_ARRAY_FOR(uses->refines, u) {
1001 ypr_close_parent(ctx, &flag);
1002 yprp_refine(ctx, &uses->refines[u]);
1003 }
1004
1005 LY_ARRAY_FOR(uses->augments, u) {
1006 ypr_close_parent(ctx, &flag);
1007 yprp_augment(ctx, &uses->augments[u]);
1008 }
1009
1010 LEVEL--;
1011 ypr_close(ctx, "uses", flag);
1012}
1013
1014static void
1015yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
1016{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001017 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001018 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001019 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1020
1021 yprp_node_common1(ctx, node, &flag);
1022
1023 LY_ARRAY_FOR(any->musts, u) {
1024 ypr_close_parent(ctx, &flag);
1025 yprp_restr(ctx, &any->musts[u], "must", "condition", &flag);
1026 }
1027
1028 yprp_node_common2(ctx, node, &flag);
1029
1030 LEVEL--;
1031 ypr_close(ctx, lys_nodetype2str(node->nodetype), flag);
1032}
1033
1034static void
1035yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
1036{
FredGand944bdc2019-11-05 21:57:07 +08001037 switch (node->nodetype) {
1038 case LYS_CONTAINER:
1039 yprp_container(ctx, node);
1040 break;
1041 case LYS_CHOICE:
1042 yprp_choice(ctx, node);
1043 break;
1044 case LYS_LEAF:
1045 yprp_leaf(ctx, node);
1046 break;
1047 case LYS_LEAFLIST:
1048 yprp_leaflist(ctx, node);
1049 break;
1050 case LYS_LIST:
1051 yprp_list(ctx, node);
1052 break;
1053 case LYS_USES:
1054 yprp_uses(ctx, node);
1055 break;
1056 case LYS_ANYXML:
1057 case LYS_ANYDATA:
1058 yprp_anydata(ctx, node);
1059 break;
1060 case LYS_CASE:
1061 yprp_case(ctx, node);
1062 break;
1063 default:
1064 break;
1065 }
1066}
1067
1068static void
1069yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
1070{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001071 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001072 struct lysp_deviate_add *add;
1073 struct lysp_deviate_rpl *rpl;
1074 struct lysp_deviate_del *del;
1075 struct lysp_deviate *elem;
1076
1077 ypr_open(ctx, "deviation", "target-node", deviation->nodeid, 1);
1078 LEVEL++;
1079
1080 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
1081 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1082 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1083
1084 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001085 ly_print_(ctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001086 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1087 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001088 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001089 LEVEL++;
1090
1091 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
1092 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001093 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001094 continue;
1095 }
1096 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001097 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001098 ly_print_(ctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001099 LEVEL++;
1100
1101 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
1102 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001103 LY_ARRAY_FOR(add->musts, u) {
1104 yprp_restr(ctx, &add->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001105 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001106 LY_ARRAY_FOR(add->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001107 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001108 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001109 LY_ARRAY_FOR(add->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001110 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001111 }
1112 ypr_config(ctx, add->flags, add->exts, NULL);
1113 ypr_mandatory(ctx, add->flags, add->exts, NULL);
1114 if (add->flags & LYS_SET_MIN) {
1115 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min);
1116 }
1117 if (add->flags & LYS_SET_MAX) {
1118 if (add->max) {
1119 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max);
1120 } else {
1121 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
1122 }
1123 }
1124 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001125 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001126 ly_print_(ctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001127 LEVEL++;
1128
1129 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
1130 if (rpl->type) {
1131 yprp_type(ctx, rpl->type);
1132 }
1133 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001134 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
FredGand944bdc2019-11-05 21:57:07 +08001135 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
1136 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
1137 if (rpl->flags & LYS_SET_MIN) {
1138 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min);
1139 }
1140 if (rpl->flags & LYS_SET_MAX) {
1141 if (rpl->max) {
1142 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max);
1143 } else {
1144 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
1145 }
1146 }
1147 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001148 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001149 ly_print_(ctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001150 LEVEL++;
1151
1152 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
1153 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001154 LY_ARRAY_FOR(del->musts, u) {
1155 yprp_restr(ctx, &del->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001156 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001157 LY_ARRAY_FOR(del->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001158 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001159 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001160 LY_ARRAY_FOR(del->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001161 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001162 }
1163 }
1164
1165 LEVEL--;
1166 ypr_close(ctx, "deviate", 1);
1167 }
1168
1169 LEVEL--;
1170 ypr_close(ctx, "deviation", 1);
1171}
1172
FredGand944bdc2019-11-05 21:57:07 +08001173static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02001174ypr_xmlns(struct ypr_ctx *ctx, const struct lys_module *module, uint16_t indent)
FredGand944bdc2019-11-05 21:57:07 +08001175{
Michal Vasko5233e962020-08-14 14:26:20 +02001176 ly_print_(ctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1177 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001178}
FredGand944bdc2019-11-05 21:57:07 +08001179
Michal Vasko7c8439f2020-08-05 13:25:19 +02001180static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02001181ypr_import_xmlns(struct ypr_ctx *ctx, const struct lysp_module *modp, uint16_t indent)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001182{
1183 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001184
1185 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko5233e962020-08-14 14:26:20 +02001186 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 +08001187 }
1188}
1189
1190struct ext_substmt_info_s stmt_attr_info[] = {
1191 {NULL, NULL, 0}, /**< LY_STMT_NONE*/
1192 {"status", "value", SUBST_FLAG_ID}, /**< LY_STMT_STATUS */
1193 {"config", "value", SUBST_FLAG_ID}, /**< LY_STMT_CONFIG */
1194 {"mandatory", "value", SUBST_FLAG_ID}, /**< LY_STMT_MANDATORY */
1195 {"units", "name", SUBST_FLAG_ID}, /**< LY_STMT_UNITS */
1196 {"default", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEFAULT */
1197 {"type", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPE */
1198 {"action", "name", SUBST_FLAG_ID}, /**< LY_STMT_ACTION */
1199 {"anydata", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYDATA */
1200 {"anyxml", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYXML */
1201 {"argument", "name", SUBST_FLAG_ID}, /**< LY_STMT_ARGUMENT */
1202 {"augment", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_AUGMENT */
1203 {"base", "name", SUBST_FLAG_ID}, /**< LY_STMT_BASE */
1204 {"belongs-to", "module", SUBST_FLAG_ID}, /**< LY_STMT_BELONGS_TO */
1205 {"bit", "name", SUBST_FLAG_ID}, /**< LY_STMT_BIT */
1206 {"case", "name", SUBST_FLAG_ID}, /**< LY_STMT_CASE */
1207 {"choice", "name", SUBST_FLAG_ID}, /**< LY_STMT_CHOICE */
1208 {"contact", "text", SUBST_FLAG_YIN},/**< LY_STMT_CONTACT */
1209 {"container", "name", SUBST_FLAG_ID}, /**< LY_STMT_CONTAINER */
1210 {"description", "text", SUBST_FLAG_YIN},/**< LY_STMT_DESCRIPTION */
1211 {"deviate", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATE */
1212 {"deviation", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATION */
1213 {"enum", "name", SUBST_FLAG_ID}, /**< LY_STMT_ENUM */
1214 {"error-app-tag", "value", SUBST_FLAG_ID}, /**< LY_STMT_ERROR_APP_TAG */
1215 {"error-message", "value", SUBST_FLAG_YIN},/**< LY_STMT_ERROR_MESSAGE */
1216 {"extension", "name", SUBST_FLAG_ID}, /**< LY_STMT_EXTENSION */
1217 {"feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_FEATURE */
1218 {"fraction-digits", "value", SUBST_FLAG_ID}, /**< LY_STMT_FRACTION_DIGITS */
1219 {"grouping", "name", SUBST_FLAG_ID}, /**< LY_STMT_GROUPING */
1220 {"identity", "name", SUBST_FLAG_ID}, /**< LY_STMT_IDENTITY */
1221 {"if-feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_IF_FEATURE */
1222 {"import", "module", SUBST_FLAG_ID}, /**< LY_STMT_IMPORT */
1223 {"include", "module", SUBST_FLAG_ID}, /**< LY_STMT_INCLUDE */
1224 {"input", NULL, 0}, /**< LY_STMT_INPUT */
1225 {"key", "value", SUBST_FLAG_ID}, /**< LY_STMT_KEY */
1226 {"leaf", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF */
1227 {"leaf-list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF_LIST */
1228 {"length", "value", SUBST_FLAG_ID}, /**< LY_STMT_LENGTH */
1229 {"list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LIST */
1230 {"max-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MAX_ELEMENTS */
1231 {"min-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MIN_ELEMENTS */
1232 {"modifier", "value", SUBST_FLAG_ID}, /**< LY_STMT_MODIFIER */
1233 {"module", "name", SUBST_FLAG_ID}, /**< LY_STMT_MODULE */
1234 {"must", "condition", SUBST_FLAG_ID}, /**< LY_STMT_MUST */
1235 {"namespace", "uri", SUBST_FLAG_ID}, /**< LY_STMT_NAMESPACE */
1236 {"notification", "name", SUBST_FLAG_ID}, /**< LY_STMT_NOTIFICATION */
1237 {"ordered-by", "value", SUBST_FLAG_ID}, /**< LY_STMT_ORDERED_BY */
1238 {"organization", "text", SUBST_FLAG_YIN},/**< LY_STMT_ORGANIZATION */
1239 {"output", NULL, 0}, /**< LY_STMT_OUTPUT */
1240 {"path", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATH */
1241 {"pattern", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATTERN */
1242 {"position", "value", SUBST_FLAG_ID}, /**< LY_STMT_POSITION */
1243 {"prefix", "value", SUBST_FLAG_ID}, /**< LY_STMT_PREFIX */
1244 {"presence", "value", SUBST_FLAG_ID}, /**< LY_STMT_PRESENCE */
1245 {"range", "value", SUBST_FLAG_ID}, /**< LY_STMT_RANGE */
1246 {"reference", "text", SUBST_FLAG_YIN},/**< LY_STMT_REFERENCE */
1247 {"refine", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_REFINE */
1248 {"require-instance", "value", SUBST_FLAG_ID}, /**< LY_STMT_REQUIRE_INSTANCE */
1249 {"revision", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION */
1250 {"revision-date", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION_DATE */
1251 {"rpc", "name", SUBST_FLAG_ID}, /**< LY_STMT_RPC */
1252 {"submodule", "name", SUBST_FLAG_ID}, /**< LY_STMT_SUBMODULE */
1253 {"typedef", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPEDEF */
1254 {"unique", "tag", SUBST_FLAG_ID}, /**< LY_STMT_UNIQUE */
1255 {"uses", "name", SUBST_FLAG_ID}, /**< LY_STMT_USES */
1256 {"value", "value", SUBST_FLAG_ID}, /**< LY_STMT_VALUE */
1257 {"when", "condition", SUBST_FLAG_ID}, /**< LY_STMT_WHEN */
1258 {"yang-version", "value", SUBST_FLAG_ID}, /**< LY_STMT_YANG_VERSION */
1259 {"yin-element", "value", SUBST_FLAG_ID}, /**< LY_STMT_YIN_ELEMENT */
1260 {NULL, NULL, 0}, /**< LY_STMT_EXTENSION_INSTANCE */
1261 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_SEMICOLON */
1262 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_LEFT_BRACE */
1263 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_RIGHT_BRACE */
1264 {NULL, NULL, 0}, /**< LY_STMT_ARG_TEXT */
1265 {NULL, NULL, 0}, /**< LY_STMT_ARG_VALUE */
1266};
1267
1268static void
1269yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
1270{
1271 struct lysp_stmt *childstmt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001272 int8_t flag = stmt->child ? 1 : -1;
FredGand944bdc2019-11-05 21:57:07 +08001273
1274 /* TODO:
1275 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
1276 cannot find the compiled information, so it is needed to be done,
1277 currently it is ignored */
Michal Vaskod989ba02020-08-24 10:59:24 +02001278 if (stmt_attr_info[stmt->kw].name) {
1279 if (stmt_attr_info[stmt->kw].flags & SUBST_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +08001280 ypr_open(ctx, stmt->stmt, NULL, NULL, flag);
1281 ypr_yin_arg(ctx, stmt_attr_info[stmt->kw].arg, stmt->arg);
Radek Krejci0f969882020-08-21 16:56:47 +02001282 } else {
FredGand944bdc2019-11-05 21:57:07 +08001283 ypr_open(ctx, stmt->stmt, stmt_attr_info[stmt->kw].arg, stmt->arg, flag);
1284 }
1285 }
1286
1287 if (stmt->child) {
1288 LEVEL++;
1289 LY_LIST_FOR(stmt->child, childstmt) {
1290 yprp_stmt(ctx, childstmt);
1291 }
1292 LEVEL--;
1293 ypr_close(ctx, stmt->stmt, flag);
1294 }
1295}
1296
1297/**
1298 * @param[in] count Number of extensions to print, 0 to print them all.
1299 */
1300static void
1301yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001302 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count)
FredGand944bdc2019-11-05 21:57:07 +08001303{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001304 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001305 char *str;
1306 struct lysp_stmt *stmt;
1307 const char *argument;
1308 const char *ext_argument;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001309 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001310
1311 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001312 count = LY_ARRAY_COUNT(ext);
FredGand944bdc2019-11-05 21:57:07 +08001313 }
1314 LY_ARRAY_FOR(ext, u) {
1315 if (!count) {
1316 break;
1317 }
1318
1319 count--;
Michal Vasko69730152020-10-09 16:30:07 +02001320 if ((ext->insubstmt != substmt) || (ext->insubstmt_index != substmt_index)) {
FredGand944bdc2019-11-05 21:57:07 +08001321 continue;
1322 }
1323
1324 if (!ext->compiled && ext->yin) {
Michal Vasko5233e962020-08-14 14:26:20 +02001325 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 +08001326 continue;
1327 }
1328
1329 ypr_close_parent(ctx, flag);
Radek Krejci1deb5be2020-08-26 16:43:36 +02001330 inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001331 argument = NULL;
1332 ext_argument = NULL;
1333
1334 if (ext[u].compiled) {
1335 argument = ext[u].compiled->argument;
1336 ext_argument = ext[u].compiled->def->argument;
1337 } else {
1338 argument = ext[u].argument;
1339 }
1340
1341 if (ext->yin) {
1342 ypr_open(ctx, ext[u].name, NULL, NULL, 1);
1343 if (asprintf(&str, "%s:%s", ext[u].compiled->def->module->prefix, ext_argument) == -1) {
1344 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +08001345 return;
1346 }
1347 LEVEL++;
1348 inner_flag = 1;
1349 ypr_yin_arg(ctx, str, argument);
1350 free(str);
1351 str = NULL;
1352 LEVEL--;
1353 } else {
1354 ypr_open(ctx, ext[u].name, ext_argument, argument, inner_flag);
1355 }
1356
1357 LEVEL++;
1358 LY_LIST_FOR(ext[u].child, stmt) {
1359 ypr_close_parent(ctx, &inner_flag);
1360 yprp_stmt(ctx, stmt);
1361 }
1362 LEVEL--;
1363 ypr_close(ctx, ext[u].name, inner_flag);
1364 }
1365}
1366
Michal Vasko7c8439f2020-08-05 13:25:19 +02001367static void
1368yin_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001369{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001370 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001371
FredGand944bdc2019-11-05 21:57:07 +08001372 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001373 ypr_open(ctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001374 LEVEL++;
1375 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
1376 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
1377 if (modp->imports[u].rev[0]) {
1378 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
1379 }
1380 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1381 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
1382 LEVEL--;
1383 ypr_close(ctx, "import", 1);
1384 }
1385 LY_ARRAY_FOR(modp->includes, u) {
1386 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 +02001387 ypr_open(ctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001388 LEVEL++;
1389 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
1390 if (modp->includes[u].rev[0]) {
1391 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
1392 }
1393 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1394 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
1395 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001396 ly_print_(ctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001397 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001398 ypr_open(ctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001399 }
1400 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001401}
FredGand944bdc2019-11-05 21:57:07 +08001402
Michal Vasko7c8439f2020-08-05 13:25:19 +02001403static void
1404yin_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
1405{
1406 LY_ARRAY_COUNT_TYPE u;
1407 struct lysp_node *data;
FredGand944bdc2019-11-05 21:57:07 +08001408
FredGand944bdc2019-11-05 21:57:07 +08001409 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001410 ly_print_(ctx->out, "\n");
FredGand944bdc2019-11-05 21:57:07 +08001411 yprp_extension(ctx, &modp->extensions[u]);
1412 }
1413 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001414 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001415 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001416 }
1417
1418 LY_ARRAY_FOR(modp->features, u) {
1419 yprp_feature(ctx, &modp->features[u]);
1420 }
1421
1422 LY_ARRAY_FOR(modp->identities, u) {
1423 yprp_identity(ctx, &modp->identities[u]);
1424 }
1425
1426 LY_ARRAY_FOR(modp->typedefs, u) {
1427 yprp_typedef(ctx, &modp->typedefs[u]);
1428 }
1429
1430 LY_ARRAY_FOR(modp->groupings, u) {
1431 yprp_grouping(ctx, &modp->groupings[u]);
1432 }
1433
1434 LY_LIST_FOR(modp->data, data) {
1435 yprp_node(ctx, data);
1436 }
1437
1438 LY_ARRAY_FOR(modp->augments, u) {
1439 yprp_augment(ctx, &modp->augments[u]);
1440 }
1441
1442 LY_ARRAY_FOR(modp->rpcs, u) {
1443 yprp_action(ctx, &modp->rpcs[u]);
1444 }
1445
1446 LY_ARRAY_FOR(modp->notifs, u) {
1447 yprp_notification(ctx, &modp->notifs[u]);
1448 }
1449
1450 LY_ARRAY_FOR(modp->deviations, u) {
1451 yprp_deviation(ctx, &modp->deviations[u]);
1452 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001453}
1454
1455LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001456yin_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 +02001457{
1458 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02001459 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001460
Michal Vasko5233e962020-08-14 14:26:20 +02001461 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1462 ly_print_(ctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001463 ypr_xmlns(ctx, module, 8);
1464 ypr_import_xmlns(ctx, modp, 8);
Michal Vasko5233e962020-08-14 14:26:20 +02001465 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001466
1467 LEVEL++;
1468
1469 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001470 if (modp->version) {
1471 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001472 }
1473 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
1474 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
1475
1476 /* linkage-stmts (import/include) */
1477 yin_print_parsed_linkage(ctx, modp);
1478
1479 /* meta-stmts */
1480 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001481 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001482 }
1483 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
1484 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
1485 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
1486 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
1487
1488 /* revision-stmts */
1489 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001490 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001491 }
1492 LY_ARRAY_FOR(modp->revs, u) {
1493 yprp_revision(ctx, &modp->revs[u]);
1494 }
1495
1496 /* body-stmts */
1497 yin_print_parsed_body(ctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001498
1499 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001500 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001501 ly_print_flush(out);
1502
1503 return LY_SUCCESS;
1504}
1505
Michal Vasko7c8439f2020-08-05 13:25:19 +02001506static void
1507yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
1508{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001509 ypr_open(ctx, "belongs-to", "module", submodp->mod->name, 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001510 LEVEL++;
1511 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
1512 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
1513 LEVEL--;
1514 ypr_close(ctx, "belongs-to", 1);
1515}
1516
1517LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001518yin_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 +02001519{
1520 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02001521 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001522
Michal Vasko5233e962020-08-14 14:26:20 +02001523 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1524 ly_print_(ctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001525 ypr_xmlns(ctx, module, 8);
1526 ypr_import_xmlns(ctx, (struct lysp_module *)submodp, 8);
Michal Vasko5233e962020-08-14 14:26:20 +02001527 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001528
1529 LEVEL++;
1530
1531 /* submodule-header-stmts */
1532 if (submodp->version) {
1533 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
1534 }
1535 yprp_belongsto(ctx, submodp);
1536
1537 /* linkage-stmts (import/include) */
1538 yin_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
1539
1540 /* meta-stmts */
1541 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001542 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001543 }
1544 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1545 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
1546 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1547 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
1548
1549 /* revision-stmts */
1550 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001551 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001552 }
1553 LY_ARRAY_FOR(submodp->revs, u) {
1554 yprp_revision(ctx, &submodp->revs[u]);
1555 }
1556
1557 /* body-stmts */
1558 yin_print_parsed_body(ctx, (struct lysp_module *)submodp);
1559
1560 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001561 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001562 ly_print_flush(out);
1563
1564 return LY_SUCCESS;
1565}