blob: 26a9eb6199c6549d4fa18f46790713a0c2c3c30f [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 Krejci47fab892020-11-05 17:02:41 +010024#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "out_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080026#include "printer_internal.h"
27#include "tree.h"
28#include "tree_schema.h"
29#include "tree_schema_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080030#include "xml.h"
Michal Vasko004d3152020-06-11 19:59:22 +020031#include "xpath.h"
FredGand944bdc2019-11-05 21:57:07 +080032
33/**
34 * @brief YIN printer context.
35 */
36struct ypr_ctx {
Radek Krejci1deb5be2020-08-26 16:43:36 +020037 struct ly_out *out; /**< output specification */
38 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
39 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
FredGand944bdc2019-11-05 21:57:07 +080040 const struct lys_module *module; /**< schema to print */
41};
42
FredGand944bdc2019-11-05 21:57:07 +080043static void yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +020044 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count);
FredGand944bdc2019-11-05 21:57:07 +080045
46static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020047ypr_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 +080048{
Michal Vasko5233e962020-08-14 14:26:20 +020049 ly_print_(ctx->out, "%*s<%s", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080050
51 if (attr_name) {
Michal Vasko5233e962020-08-14 14:26:20 +020052 ly_print_(ctx->out, " %s=\"", attr_name);
FredGand944bdc2019-11-05 21:57:07 +080053 lyxml_dump_text(ctx->out, attr_value, 1);
Michal Vasko5233e962020-08-14 14:26:20 +020054 ly_print_(ctx->out, "\"%s", flag == -1 ? "/>\n" : flag == 1 ? ">\n" : "");
FredGand944bdc2019-11-05 21:57:07 +080055 } else if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +020056 ly_print_(ctx->out, flag == -1 ? "/>\n" : ">\n");
FredGand944bdc2019-11-05 21:57:07 +080057 }
58}
59
60static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020061ypr_close(struct ypr_ctx *ctx, const char *elem_name, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080062{
63 if (flag) {
Michal Vasko5233e962020-08-14 14:26:20 +020064 ly_print_(ctx->out, "%*s</%s>\n", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080065 } else {
Michal Vasko5233e962020-08-14 14:26:20 +020066 ly_print_(ctx->out, "/>\n");
FredGand944bdc2019-11-05 21:57:07 +080067 }
68}
69
70/*
71 * par_close_flag
72 * 0 - parent not yet closed, printing >\n, setting flag to 1
73 * 1 or NULL - parent already closed, do nothing
74 */
75static void
Radek Krejci1deb5be2020-08-26 16:43:36 +020076ypr_close_parent(struct ypr_ctx *ctx, int8_t *par_close_flag)
FredGand944bdc2019-11-05 21:57:07 +080077{
78 if (par_close_flag && !(*par_close_flag)) {
79 (*par_close_flag) = 1;
Michal Vasko5233e962020-08-14 14:26:20 +020080 ly_print_(ctx->out, ">\n");
FredGand944bdc2019-11-05 21:57:07 +080081 }
82}
83
84static void
85ypr_yin_arg(struct ypr_ctx *ctx, const char *arg, const char *text)
86{
Michal Vasko5233e962020-08-14 14:26:20 +020087 ly_print_(ctx->out, "%*s<%s>", INDENT, arg);
FredGand944bdc2019-11-05 21:57:07 +080088 lyxml_dump_text(ctx->out, text, 0);
Michal Vasko5233e962020-08-14 14:26:20 +020089 ly_print_(ctx->out, "</%s>\n", arg);
FredGand944bdc2019-11-05 21:57:07 +080090}
91
FredGand944bdc2019-11-05 21:57:07 +080092static void
93ypr_substmt(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, const char *text, void *ext)
94{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020095 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +020096 int8_t extflag = 0;
FredGand944bdc2019-11-05 21:57:07 +080097
98 if (!text) {
99 /* nothing to print */
100 return;
101 }
102
103 if (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) {
104 extflag = 1;
105 ypr_open(ctx, ext_substmt_info[substmt].name, NULL, NULL, extflag);
106 } else {
107 ypr_open(ctx, ext_substmt_info[substmt].name, ext_substmt_info[substmt].arg, text, extflag);
108 }
109
110 LEVEL++;
111 LY_ARRAY_FOR(ext, u) {
Michal Vasko69730152020-10-09 16:30:07 +0200112 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 +0800113 continue;
114 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200115 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800116 }
117
118 /* argument as yin-element */
119 if (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) {
120 ypr_yin_arg(ctx, ext_substmt_info[substmt].arg, text);
121 }
122
123 LEVEL--;
124 ypr_close(ctx, ext_substmt_info[substmt].name, extflag);
125}
126
127static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200128ypr_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 +0800129{
130 char *str;
Michal Vasko69730152020-10-09 16:30:07 +0200131
Radek Krejci1deb5be2020-08-26 16:43:36 +0200132 if (asprintf(&str, "%lu", attr_value) == -1) {
FredGand944bdc2019-11-05 21:57:07 +0800133 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800134 return;
135 }
136 ypr_substmt(ctx, substmt, substmt_index, str, exts);
137 free(str);
138}
139
140static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200141ypr_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 +0800142{
143 char *str;
144
Radek Krejci1deb5be2020-08-26 16:43:36 +0200145 if (asprintf(&str, "%ld", attr_value) == -1) {
FredGand944bdc2019-11-05 21:57:07 +0800146 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800147 return;
148 }
149 ypr_substmt(ctx, substmt, substmt_index, str, exts);
150 free(str);
151}
152
153static void
154yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
155{
156 if (rev->dsc || rev->ref || rev->exts) {
157 ypr_open(ctx, "revision", "date", rev->date, 1);
158 LEVEL++;
159 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
160 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
161 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
162 LEVEL--;
163 ypr_close(ctx, "revision", 1);
164 } else {
165 ypr_open(ctx, "revision", "date", rev->date, -1);
166 }
167}
168
169static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200170ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800171{
172 if (flags & LYS_MAND_MASK) {
173 ypr_close_parent(ctx, flag);
174 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
175 }
176}
177
178static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200179ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800180{
181 if (flags & LYS_CONFIG_MASK) {
182 ypr_close_parent(ctx, flag);
183 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
184 }
185}
186
187static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200188ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800189{
190 const char *status = NULL;
191
192 if (flags & LYS_STATUS_CURR) {
193 ypr_close_parent(ctx, flag);
194 status = "current";
195 } else if (flags & LYS_STATUS_DEPRC) {
196 ypr_close_parent(ctx, flag);
197 status = "deprecated";
198 } else if (flags & LYS_STATUS_OBSLT) {
199 ypr_close_parent(ctx, flag);
200 status = "obsolete";
201 }
202
203 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
204}
205
206static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200207ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800208{
209 if (dsc) {
210 ypr_close_parent(ctx, flag);
211 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
212 }
213}
214
215static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200216ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800217{
218 if (ref) {
219 ypr_close_parent(ctx, flag);
220 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
221 }
222}
223
224static void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200225yprp_iffeatures(struct ypr_ctx *ctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800226{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200227 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200228 int8_t extflag;
FredGand944bdc2019-11-05 21:57:07 +0800229
Michal Vasko7f45cf22020-10-01 12:49:44 +0200230 LY_ARRAY_FOR(iffs, u) {
FredGand944bdc2019-11-05 21:57:07 +0800231 ypr_close_parent(ctx, flag);
232 extflag = 0;
233
Michal Vasko7f45cf22020-10-01 12:49:44 +0200234 ly_print_(ctx->out, "%*s<if-feature name=\"%s", INDENT, iffs[u].str);
FredGand944bdc2019-11-05 21:57:07 +0800235
236 /* extensions */
237 LEVEL++;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200238 LY_ARRAY_FOR(exts, v) {
239 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[v], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800240 }
241 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200242 ly_print_(ctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800243 }
244}
245
246static void
247yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
248{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200249 int8_t flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200250 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800251
252 ypr_open(ctx, "extension", "name", ext->name, flag);
253 LEVEL++;
254
255 if (ext->exts) {
256 ypr_close_parent(ctx, &flag);
257 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
258 }
259
260 if (ext->argument) {
261 ypr_close_parent(ctx, &flag);
262 ypr_open(ctx, "argument", "name", ext->argument, flag2);
263
264 LEVEL++;
265 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200266 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200267 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LYEXT_SUBSTMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
FredGand944bdc2019-11-05 21:57:07 +0800268 ypr_close_parent(ctx, &flag2);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200269 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
FredGand944bdc2019-11-05 21:57:07 +0800270 }
271 }
272 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vasko69730152020-10-09 16:30:07 +0200273 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts)))) {
FredGand944bdc2019-11-05 21:57:07 +0800274 ypr_close_parent(ctx, &flag2);
275 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
276 }
277 LEVEL--;
278 ypr_close(ctx, "argument", flag2);
279 }
280
281 ypr_status(ctx, ext->flags, ext->exts, &flag);
282 ypr_description(ctx, ext->dsc, ext->exts, &flag);
283 ypr_reference(ctx, ext->ref, ext->exts, &flag);
284
285 LEVEL--;
286 ypr_close(ctx, "extension", flag);
287}
288
289static void
290yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
291{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200292 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800293
294 ypr_open(ctx, "feature", "name", feat->name, flag);
295 LEVEL++;
296 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
297 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
298 ypr_status(ctx, feat->flags, feat->exts, &flag);
299 ypr_description(ctx, feat->dsc, feat->exts, &flag);
300 ypr_reference(ctx, feat->ref, feat->exts, &flag);
301 LEVEL--;
302 ypr_close(ctx, "feature", flag);
303}
304
305static void
306yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
307{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200308 int8_t flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200309 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800310
311 ypr_open(ctx, "identity", "name", ident->name, flag);
312 LEVEL++;
313
314 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
315 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
316
317 LY_ARRAY_FOR(ident->bases, u) {
318 ypr_close_parent(ctx, &flag);
319 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
320 }
321
322 ypr_status(ctx, ident->flags, ident->exts, &flag);
323 ypr_description(ctx, ident->dsc, ident->exts, &flag);
324 ypr_reference(ctx, ident->ref, ident->exts, &flag);
325
326 LEVEL--;
327 ypr_close(ctx, "identity", flag);
328}
329
330static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200331yprp_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 +0800332{
333 (void)flag;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200334 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800335
336 if (!restr) {
337 return;
338 }
339
Michal Vasko5233e962020-08-14 14:26:20 +0200340 ly_print_(ctx->out, "%*s<%s %s=\"", INDENT, name, attr);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 lyxml_dump_text(ctx->out,
342 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
343 restr->arg.str : &restr->arg.str[1], 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200344 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800345
346 LEVEL++;
347 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100348 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
FredGand944bdc2019-11-05 21:57:07 +0800349 ypr_close_parent(ctx, &inner_flag);
350 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
351 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
352 }
353 if (restr->emsg) {
354 ypr_close_parent(ctx, &inner_flag);
355 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
356 }
357 if (restr->eapptag) {
358 ypr_close_parent(ctx, &inner_flag);
359 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
360 }
361 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
362 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
363
364 LEVEL--;
365 ypr_close(ctx, name, inner_flag);
366}
367
368static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200369yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800370{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200371 int8_t inner_flag = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200372
FredGand944bdc2019-11-05 21:57:07 +0800373 (void)flag;
374
375 if (!when) {
376 return;
377 }
378
Michal Vasko5233e962020-08-14 14:26:20 +0200379 ly_print_(ctx->out, "%*s<when condition=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800380 lyxml_dump_text(ctx->out, when->cond, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200381 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800382
383 LEVEL++;
384 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
385 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
386 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
387 LEVEL--;
388 ypr_close(ctx, "when", inner_flag);
389}
390
391static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200392yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800393{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200394 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200395 int8_t inner_flag;
Michal Vasko69730152020-10-09 16:30:07 +0200396
FredGand944bdc2019-11-05 21:57:07 +0800397 (void)flag;
398
399 LY_ARRAY_FOR(items, u) {
400 if (type == LY_TYPE_BITS) {
Michal Vasko5233e962020-08-14 14:26:20 +0200401 ly_print_(ctx->out, "%*s<bit name=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800402 lyxml_dump_text(ctx->out, items[u].name, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200403 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800404 } else { /* LY_TYPE_ENUM */
Michal Vasko5233e962020-08-14 14:26:20 +0200405 ly_print_(ctx->out, "%*s<enum name=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800406 lyxml_dump_text(ctx->out, items[u].name, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200407 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800408 }
409 inner_flag = 0;
410 LEVEL++;
411 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, items[u].exts, &inner_flag, 0);
412 yprp_iffeatures(ctx, items[u].iffeatures, items[u].exts, &inner_flag);
413 if (items[u].flags & LYS_SET_VALUE) {
414 if (type == LY_TYPE_BITS) {
415 ypr_close_parent(ctx, &inner_flag);
416 ypr_unsigned(ctx, LYEXT_SUBSTMT_POSITION, 0, items[u].exts, items[u].value);
417 } else { /* LY_TYPE_ENUM */
418 ypr_close_parent(ctx, &inner_flag);
419 ypr_signed(ctx, LYEXT_SUBSTMT_VALUE, 0, items[u].exts, items[u].value);
420 }
421 }
422 ypr_status(ctx, items[u].flags, items[u].exts, &inner_flag);
423 ypr_description(ctx, items[u].dsc, items[u].exts, &inner_flag);
424 ypr_reference(ctx, items[u].ref, items[u].exts, &inner_flag);
425 LEVEL--;
426 ypr_close(ctx, type == LY_TYPE_BITS ? "bit" : "enum", inner_flag);
427 }
428}
429
430static void
431yprp_type(struct ypr_ctx *ctx, const struct lysp_type *type)
432{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200433 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200434 int8_t flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200435
FredGand944bdc2019-11-05 21:57:07 +0800436 if (!ctx || !type) {
437 return;
438 }
439
FredGand944bdc2019-11-05 21:57:07 +0800440 ypr_open(ctx, "type", "name", type->name, flag);
441 LEVEL++;
442
443 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
444
445 if (type->range || type->length || type->patterns || type->bits || type->enums) {
446 ypr_close_parent(ctx, &flag);
447 }
448 yprp_restr(ctx, type->range, "range", "value", &flag);
449 yprp_restr(ctx, type->length, "length", "value", &flag);
450 LY_ARRAY_FOR(type->patterns, u) {
451 yprp_restr(ctx, &type->patterns[u], "pattern", "value", &flag);
452 }
453 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
454 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
455
456 if (type->path) {
457 ypr_close_parent(ctx, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200458 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800459 }
460 if (type->flags & LYS_SET_REQINST) {
461 ypr_close_parent(ctx, &flag);
462 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
463 }
464 if (type->flags & LYS_SET_FRDIGITS) {
465 ypr_close_parent(ctx, &flag);
466 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits);
467 }
468 LY_ARRAY_FOR(type->bases, u) {
469 ypr_close_parent(ctx, &flag);
470 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
471 }
472 LY_ARRAY_FOR(type->types, u) {
473 ypr_close_parent(ctx, &flag);
474 yprp_type(ctx, &type->types[u]);
475 }
476
477 LEVEL--;
478 ypr_close(ctx, "type", flag);
479}
480
481static void
482yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
483{
FredGand944bdc2019-11-05 21:57:07 +0800484 ypr_open(ctx, "typedef", "name", tpdf->name, 1);
485 LEVEL++;
486
487 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
488
489 yprp_type(ctx, &tpdf->type);
490
491 if (tpdf->units) {
492 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
493 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200494 if (tpdf->dflt.str) {
495 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800496 }
497
498 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
499 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
500 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
501
502 LEVEL--;
503 ypr_close(ctx, "typedef", 1);
504}
505
506static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
507static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
508
509static void
510yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
511{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200512 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200513 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800514 struct lysp_node *data;
515
FredGand944bdc2019-11-05 21:57:07 +0800516 ypr_open(ctx, "grouping", "name", grp->name, flag);
517 LEVEL++;
518
519 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, grp->exts, &flag, 0);
520 ypr_status(ctx, grp->flags, grp->exts, &flag);
521 ypr_description(ctx, grp->dsc, grp->exts, &flag);
522 ypr_reference(ctx, grp->ref, grp->exts, &flag);
523
524 LY_ARRAY_FOR(grp->typedefs, u) {
525 ypr_close_parent(ctx, &flag);
526 yprp_typedef(ctx, &grp->typedefs[u]);
527 }
528
529 LY_ARRAY_FOR(grp->groupings, u) {
530 ypr_close_parent(ctx, &flag);
531 yprp_grouping(ctx, &grp->groupings[u]);
532 }
533
534 LY_LIST_FOR(grp->data, data) {
535 ypr_close_parent(ctx, &flag);
536 yprp_node(ctx, data);
537 }
538
539 LY_ARRAY_FOR(grp->actions, u) {
540 ypr_close_parent(ctx, &flag);
541 yprp_action(ctx, &grp->actions[u]);
542 }
543
544 LEVEL--;
545 ypr_close(ctx, "grouping", flag);
546}
547
548static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200549yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800550{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200551 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800552 struct lysp_node *data;
553
Michal Vasko7f45cf22020-10-01 12:49:44 +0200554 if (!inout->data) {
555 /* input/output is empty */
FredGand944bdc2019-11-05 21:57:07 +0800556 return;
557 }
558 ypr_close_parent(ctx, flag);
559
560 ypr_open(ctx, (inout->nodetype == LYS_INPUT ? "input" : "output"), NULL, NULL, *flag);
561 LEVEL++;
562
563 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
564 LY_ARRAY_FOR(inout->musts, u) {
565 yprp_restr(ctx, &inout->musts[u], "must", "condition", NULL);
566 }
567 LY_ARRAY_FOR(inout->typedefs, u) {
568 yprp_typedef(ctx, &inout->typedefs[u]);
569 }
570 LY_ARRAY_FOR(inout->groupings, u) {
571 yprp_grouping(ctx, &inout->groupings[u]);
572 }
573
574 LY_LIST_FOR(inout->data, data) {
575 yprp_node(ctx, data);
576 }
577
578 LEVEL--;
579 ypr_close(ctx, (inout->nodetype == LYS_INPUT ? "input" : "output"), 1);
580}
581
582static void
583yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
584{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200585 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200586 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800587 struct lysp_node *data;
588
FredGand944bdc2019-11-05 21:57:07 +0800589 ypr_open(ctx, "notification", "name", notif->name, flag);
590
591 LEVEL++;
592 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, notif->exts, &flag, 0);
593 yprp_iffeatures(ctx, notif->iffeatures, notif->exts, &flag);
594
595 LY_ARRAY_FOR(notif->musts, u) {
596 ypr_close_parent(ctx, &flag);
597 yprp_restr(ctx, &notif->musts[u], "must", "condition", &flag);
598 }
599 ypr_status(ctx, notif->flags, notif->exts, &flag);
600 ypr_description(ctx, notif->dsc, notif->exts, &flag);
601 ypr_reference(ctx, notif->ref, notif->exts, &flag);
602
603 LY_ARRAY_FOR(notif->typedefs, u) {
604 ypr_close_parent(ctx, &flag);
605 yprp_typedef(ctx, &notif->typedefs[u]);
606 }
607
608 LY_ARRAY_FOR(notif->groupings, u) {
609 ypr_close_parent(ctx, &flag);
610 yprp_grouping(ctx, &notif->groupings[u]);
611 }
612
613 LY_LIST_FOR(notif->data, data) {
614 ypr_close_parent(ctx, &flag);
615 yprp_node(ctx, data);
616 }
617
618 LEVEL--;
619 ypr_close(ctx, "notification", flag);
620}
621
622static void
623yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action)
624{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200625 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200626 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800627
FredGand944bdc2019-11-05 21:57:07 +0800628 ypr_open(ctx, action->parent ? "action" : "rpc", "name", action->name, flag);
629
630 LEVEL++;
631 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, action->exts, &flag, 0);
632 yprp_iffeatures(ctx, action->iffeatures, action->exts, &flag);
633 ypr_status(ctx, action->flags, action->exts, &flag);
634 ypr_description(ctx, action->dsc, action->exts, &flag);
635 ypr_reference(ctx, action->ref, action->exts, &flag);
636
637 LY_ARRAY_FOR(action->typedefs, u) {
638 ypr_close_parent(ctx, &flag);
639 yprp_typedef(ctx, &action->typedefs[u]);
640 }
641
642 LY_ARRAY_FOR(action->groupings, u) {
643 ypr_close_parent(ctx, &flag);
644 yprp_grouping(ctx, &action->groupings[u]);
645 }
646
647 yprp_inout(ctx, &action->input, &flag);
648 yprp_inout(ctx, &action->output, &flag);
649
650 LEVEL--;
651 ypr_close(ctx, action->parent ? "action" : "rpc", flag);
652}
653
654static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200655yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800656{
657 ypr_open(ctx, lys_nodetype2str(node->nodetype), "name", node->name, *flag);
658 LEVEL++;
659
660 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, node->exts, flag, 0);
661 yprp_when(ctx, node->when, flag);
662 yprp_iffeatures(ctx, node->iffeatures, node->exts, flag);
663}
664
665static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200666yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800667{
668 ypr_config(ctx, node->flags, node->exts, flag);
669 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) {
670 ypr_mandatory(ctx, node->flags, node->exts, flag);
671 }
672 ypr_status(ctx, node->flags, node->exts, flag);
673 ypr_description(ctx, node->dsc, node->exts, flag);
674 ypr_reference(ctx, node->ref, node->exts, flag);
675}
676
677static void
678yprp_container(struct ypr_ctx *ctx, const struct lysp_node *node)
679{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200680 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200681 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800682 struct lysp_node *child;
683 struct lysp_node_container *cont = (struct lysp_node_container *)node;
684
685 yprp_node_common1(ctx, node, &flag);
686
687 LY_ARRAY_FOR(cont->musts, u) {
688 ypr_close_parent(ctx, &flag);
689 yprp_restr(ctx, &cont->musts[u], "must", "condition", &flag);
690 }
691 if (cont->presence) {
692 ypr_close_parent(ctx, &flag);
693 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, cont->presence, cont->exts);
694 }
695
696 yprp_node_common2(ctx, node, &flag);
697
698 LY_ARRAY_FOR(cont->typedefs, u) {
699 ypr_close_parent(ctx, &flag);
700 yprp_typedef(ctx, &cont->typedefs[u]);
701 }
702
703 LY_ARRAY_FOR(cont->groupings, u) {
704 ypr_close_parent(ctx, &flag);
705 yprp_grouping(ctx, &cont->groupings[u]);
706 }
707
708 LY_LIST_FOR(cont->child, child) {
709 ypr_close_parent(ctx, &flag);
710 yprp_node(ctx, child);
711 }
712
713 LY_ARRAY_FOR(cont->actions, u) {
714 ypr_close_parent(ctx, &flag);
715 yprp_action(ctx, &cont->actions[u]);
716 }
717
718 LY_ARRAY_FOR(cont->notifs, u) {
719 ypr_close_parent(ctx, &flag);
720 yprp_notification(ctx, &cont->notifs[u]);
721 }
722
723 LEVEL--;
724 ypr_close(ctx, "container", flag);
725}
726
727static void
728yprp_case(struct ypr_ctx *ctx, const struct lysp_node *node)
729{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200730 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800731 struct lysp_node *child;
732 struct lysp_node_case *cas = (struct lysp_node_case *)node;
733
734 yprp_node_common1(ctx, node, &flag);
735 yprp_node_common2(ctx, node, &flag);
736
737 LY_LIST_FOR(cas->child, child) {
738 ypr_close_parent(ctx, &flag);
739 yprp_node(ctx, child);
740 }
741
742 LEVEL--;
743 ypr_close(ctx, "case", flag);
744}
745
746static void
747yprp_choice(struct ypr_ctx *ctx, const struct lysp_node *node)
748{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200749 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800750 struct lysp_node *child;
751 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
752
753 yprp_node_common1(ctx, node, &flag);
754
Michal Vasko7f45cf22020-10-01 12:49:44 +0200755 if (choice->dflt.str) {
FredGand944bdc2019-11-05 21:57:07 +0800756 ypr_close_parent(ctx, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200757 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt.str, choice->exts);
FredGand944bdc2019-11-05 21:57:07 +0800758 }
759
760 yprp_node_common2(ctx, node, &flag);
761
762 LY_LIST_FOR(choice->child, child) {
763 ypr_close_parent(ctx, &flag);
764 yprp_node(ctx, child);
765 }
766
767 LEVEL--;
768 ypr_close(ctx, "choice", flag);
769}
770
771static void
772yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
773{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200774 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800775 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
776
Radek Krejci1deb5be2020-08-26 16:43:36 +0200777 int8_t flag = 1;
Michal Vasko69730152020-10-09 16:30:07 +0200778
FredGand944bdc2019-11-05 21:57:07 +0800779 yprp_node_common1(ctx, node, &flag);
780
781 yprp_type(ctx, &leaf->type);
782 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
783 LY_ARRAY_FOR(leaf->musts, u) {
784 yprp_restr(ctx, &leaf->musts[u], "must", "condition", &flag);
785 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200786 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800787
788 yprp_node_common2(ctx, node, &flag);
789
790 LEVEL--;
791 ypr_close(ctx, "leaf", flag);
792}
793
794static void
795yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
796{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200797 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800798 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200799 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800800
801 yprp_node_common1(ctx, node, &flag);
802
803 yprp_type(ctx, &llist->type);
804 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
805 LY_ARRAY_FOR(llist->musts, u) {
806 yprp_restr(ctx, &llist->musts[u], "must", "condition", NULL);
807 }
808 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200809 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800810 }
811
812 ypr_config(ctx, node->flags, node->exts, NULL);
813
814 if (llist->flags & LYS_SET_MIN) {
815 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min);
816 }
817 if (llist->flags & LYS_SET_MAX) {
818 if (llist->max) {
819 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max);
820 } else {
821 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
822 }
823 }
824
825 if (llist->flags & LYS_ORDBY_MASK) {
826 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
827 }
828
829 ypr_status(ctx, node->flags, node->exts, &flag);
830 ypr_description(ctx, node->dsc, node->exts, &flag);
831 ypr_reference(ctx, node->ref, node->exts, &flag);
832
833 LEVEL--;
834 ypr_close(ctx, "leaf-list", flag);
835}
836
837static void
838yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
839{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200840 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200841 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800842 struct lysp_node *child;
843 struct lysp_node_list *list = (struct lysp_node_list *)node;
844
845 yprp_node_common1(ctx, node, &flag);
846
847 LY_ARRAY_FOR(list->musts, u) {
848 ypr_close_parent(ctx, &flag);
849 yprp_restr(ctx, &list->musts[u], "must", "condition", &flag);
850 }
851 if (list->key) {
852 ypr_close_parent(ctx, &flag);
853 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
854 }
855 LY_ARRAY_FOR(list->uniques, u) {
856 ypr_close_parent(ctx, &flag);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200857 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u].str, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800858 }
859
860 ypr_config(ctx, node->flags, node->exts, NULL);
861
862 if (list->flags & LYS_SET_MIN) {
863 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min);
864 }
865 if (list->flags & LYS_SET_MAX) {
866 if (list->max) {
867 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max);
868 } else {
869 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
870 }
871 }
872
873 if (list->flags & LYS_ORDBY_MASK) {
874 ypr_close_parent(ctx, &flag);
875 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
876 }
877
878 ypr_status(ctx, node->flags, node->exts, &flag);
879 ypr_description(ctx, node->dsc, node->exts, &flag);
880 ypr_reference(ctx, node->ref, node->exts, &flag);
881
882 LY_ARRAY_FOR(list->typedefs, u) {
883 ypr_close_parent(ctx, &flag);
884 yprp_typedef(ctx, &list->typedefs[u]);
885 }
886
887 LY_ARRAY_FOR(list->groupings, u) {
888 ypr_close_parent(ctx, &flag);
889 yprp_grouping(ctx, &list->groupings[u]);
890 }
891
892 LY_LIST_FOR(list->child, child) {
893 ypr_close_parent(ctx, &flag);
894 yprp_node(ctx, child);
895 }
896
897 LY_ARRAY_FOR(list->actions, u) {
898 ypr_close_parent(ctx, &flag);
899 yprp_action(ctx, &list->actions[u]);
900 }
901
902 LY_ARRAY_FOR(list->notifs, u) {
903 ypr_close_parent(ctx, &flag);
904 yprp_notification(ctx, &list->notifs[u]);
905 }
906
907 LEVEL--;
908 ypr_close(ctx, "list", flag);
909}
910
911static void
912yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
913{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200914 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200915 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800916
917 ypr_open(ctx, "refine", "target-node", refine->nodeid, flag);
918 LEVEL++;
919
920 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
921 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
922
923 LY_ARRAY_FOR(refine->musts, u) {
924 ypr_close_parent(ctx, &flag);
925 yprp_restr(ctx, &refine->musts[u], "must", "condition", &flag);
926 }
927
928 if (refine->presence) {
929 ypr_close_parent(ctx, &flag);
930 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
931 }
932
933 LY_ARRAY_FOR(refine->dflts, u) {
934 ypr_close_parent(ctx, &flag);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200935 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +0800936 }
937
938 ypr_config(ctx, refine->flags, refine->exts, &flag);
939 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
940
941 if (refine->flags & LYS_SET_MIN) {
942 ypr_close_parent(ctx, &flag);
943 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min);
944 }
945 if (refine->flags & LYS_SET_MAX) {
946 ypr_close_parent(ctx, &flag);
947 if (refine->max) {
948 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max);
949 } else {
950 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
951 }
952 }
953
954 ypr_description(ctx, refine->dsc, refine->exts, &flag);
955 ypr_reference(ctx, refine->ref, refine->exts, &flag);
956
957 LEVEL--;
958 ypr_close(ctx, "refine", flag);
959}
960
961static void
962yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
963{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200964 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800965 struct lysp_node *child;
966
967 ypr_open(ctx, "augment", "target-node", aug->nodeid, 1);
968 LEVEL++;
969
970 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
971 yprp_when(ctx, aug->when, NULL);
972 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
973 ypr_status(ctx, aug->flags, aug->exts, NULL);
974 ypr_description(ctx, aug->dsc, aug->exts, NULL);
975 ypr_reference(ctx, aug->ref, aug->exts, NULL);
976
977 LY_LIST_FOR(aug->child, child) {
978 yprp_node(ctx, child);
979 }
980
981 LY_ARRAY_FOR(aug->actions, u) {
982 yprp_action(ctx, &aug->actions[u]);
983 }
984
985 LY_ARRAY_FOR(aug->notifs, u) {
986 yprp_notification(ctx, &aug->notifs[u]);
987 }
988
989 LEVEL--;
990 ypr_close(ctx, "augment", 1);
991}
992
FredGand944bdc2019-11-05 21:57:07 +0800993static void
994yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
995{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200996 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200997 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800998 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
999
1000 yprp_node_common1(ctx, node, &flag);
1001 yprp_node_common2(ctx, node, &flag);
1002
1003 LY_ARRAY_FOR(uses->refines, u) {
1004 ypr_close_parent(ctx, &flag);
1005 yprp_refine(ctx, &uses->refines[u]);
1006 }
1007
1008 LY_ARRAY_FOR(uses->augments, u) {
1009 ypr_close_parent(ctx, &flag);
1010 yprp_augment(ctx, &uses->augments[u]);
1011 }
1012
1013 LEVEL--;
1014 ypr_close(ctx, "uses", flag);
1015}
1016
1017static void
1018yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
1019{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001020 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001021 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001022 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1023
1024 yprp_node_common1(ctx, node, &flag);
1025
1026 LY_ARRAY_FOR(any->musts, u) {
1027 ypr_close_parent(ctx, &flag);
1028 yprp_restr(ctx, &any->musts[u], "must", "condition", &flag);
1029 }
1030
1031 yprp_node_common2(ctx, node, &flag);
1032
1033 LEVEL--;
1034 ypr_close(ctx, lys_nodetype2str(node->nodetype), flag);
1035}
1036
1037static void
1038yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
1039{
FredGand944bdc2019-11-05 21:57:07 +08001040 switch (node->nodetype) {
1041 case LYS_CONTAINER:
1042 yprp_container(ctx, node);
1043 break;
1044 case LYS_CHOICE:
1045 yprp_choice(ctx, node);
1046 break;
1047 case LYS_LEAF:
1048 yprp_leaf(ctx, node);
1049 break;
1050 case LYS_LEAFLIST:
1051 yprp_leaflist(ctx, node);
1052 break;
1053 case LYS_LIST:
1054 yprp_list(ctx, node);
1055 break;
1056 case LYS_USES:
1057 yprp_uses(ctx, node);
1058 break;
1059 case LYS_ANYXML:
1060 case LYS_ANYDATA:
1061 yprp_anydata(ctx, node);
1062 break;
1063 case LYS_CASE:
1064 yprp_case(ctx, node);
1065 break;
1066 default:
1067 break;
1068 }
1069}
1070
1071static void
1072yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
1073{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001074 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001075 struct lysp_deviate_add *add;
1076 struct lysp_deviate_rpl *rpl;
1077 struct lysp_deviate_del *del;
1078 struct lysp_deviate *elem;
1079
1080 ypr_open(ctx, "deviation", "target-node", deviation->nodeid, 1);
1081 LEVEL++;
1082
1083 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
1084 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1085 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1086
1087 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001088 ly_print_(ctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001089 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1090 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001091 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001092 LEVEL++;
1093
1094 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
1095 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001096 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001097 continue;
1098 }
1099 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001100 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001101 ly_print_(ctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001102 LEVEL++;
1103
1104 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
1105 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001106 LY_ARRAY_FOR(add->musts, u) {
1107 yprp_restr(ctx, &add->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001108 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001109 LY_ARRAY_FOR(add->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001110 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001111 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001112 LY_ARRAY_FOR(add->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001113 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001114 }
1115 ypr_config(ctx, add->flags, add->exts, NULL);
1116 ypr_mandatory(ctx, add->flags, add->exts, NULL);
1117 if (add->flags & LYS_SET_MIN) {
1118 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min);
1119 }
1120 if (add->flags & LYS_SET_MAX) {
1121 if (add->max) {
1122 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max);
1123 } else {
1124 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
1125 }
1126 }
1127 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001128 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001129 ly_print_(ctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001130 LEVEL++;
1131
1132 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
1133 if (rpl->type) {
1134 yprp_type(ctx, rpl->type);
1135 }
1136 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001137 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
FredGand944bdc2019-11-05 21:57:07 +08001138 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
1139 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
1140 if (rpl->flags & LYS_SET_MIN) {
1141 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min);
1142 }
1143 if (rpl->flags & LYS_SET_MAX) {
1144 if (rpl->max) {
1145 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max);
1146 } else {
1147 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
1148 }
1149 }
1150 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001151 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001152 ly_print_(ctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001153 LEVEL++;
1154
1155 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
1156 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001157 LY_ARRAY_FOR(del->musts, u) {
1158 yprp_restr(ctx, &del->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001159 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001160 LY_ARRAY_FOR(del->uniques, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001161 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001162 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001163 LY_ARRAY_FOR(del->dflts, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001164 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001165 }
1166 }
1167
1168 LEVEL--;
1169 ypr_close(ctx, "deviate", 1);
1170 }
1171
1172 LEVEL--;
1173 ypr_close(ctx, "deviation", 1);
1174}
1175
FredGand944bdc2019-11-05 21:57:07 +08001176static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02001177ypr_xmlns(struct ypr_ctx *ctx, const struct lys_module *module, uint16_t indent)
FredGand944bdc2019-11-05 21:57:07 +08001178{
Michal Vasko5233e962020-08-14 14:26:20 +02001179 ly_print_(ctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1180 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001181}
FredGand944bdc2019-11-05 21:57:07 +08001182
Michal Vasko7c8439f2020-08-05 13:25:19 +02001183static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02001184ypr_import_xmlns(struct ypr_ctx *ctx, const struct lysp_module *modp, uint16_t indent)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001185{
1186 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001187
1188 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko5233e962020-08-14 14:26:20 +02001189 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 +08001190 }
1191}
1192
1193struct ext_substmt_info_s stmt_attr_info[] = {
1194 {NULL, NULL, 0}, /**< LY_STMT_NONE*/
1195 {"status", "value", SUBST_FLAG_ID}, /**< LY_STMT_STATUS */
1196 {"config", "value", SUBST_FLAG_ID}, /**< LY_STMT_CONFIG */
1197 {"mandatory", "value", SUBST_FLAG_ID}, /**< LY_STMT_MANDATORY */
1198 {"units", "name", SUBST_FLAG_ID}, /**< LY_STMT_UNITS */
1199 {"default", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEFAULT */
1200 {"type", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPE */
1201 {"action", "name", SUBST_FLAG_ID}, /**< LY_STMT_ACTION */
1202 {"anydata", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYDATA */
1203 {"anyxml", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYXML */
1204 {"argument", "name", SUBST_FLAG_ID}, /**< LY_STMT_ARGUMENT */
1205 {"augment", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_AUGMENT */
1206 {"base", "name", SUBST_FLAG_ID}, /**< LY_STMT_BASE */
1207 {"belongs-to", "module", SUBST_FLAG_ID}, /**< LY_STMT_BELONGS_TO */
1208 {"bit", "name", SUBST_FLAG_ID}, /**< LY_STMT_BIT */
1209 {"case", "name", SUBST_FLAG_ID}, /**< LY_STMT_CASE */
1210 {"choice", "name", SUBST_FLAG_ID}, /**< LY_STMT_CHOICE */
1211 {"contact", "text", SUBST_FLAG_YIN},/**< LY_STMT_CONTACT */
1212 {"container", "name", SUBST_FLAG_ID}, /**< LY_STMT_CONTAINER */
1213 {"description", "text", SUBST_FLAG_YIN},/**< LY_STMT_DESCRIPTION */
1214 {"deviate", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATE */
1215 {"deviation", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATION */
1216 {"enum", "name", SUBST_FLAG_ID}, /**< LY_STMT_ENUM */
1217 {"error-app-tag", "value", SUBST_FLAG_ID}, /**< LY_STMT_ERROR_APP_TAG */
1218 {"error-message", "value", SUBST_FLAG_YIN},/**< LY_STMT_ERROR_MESSAGE */
1219 {"extension", "name", SUBST_FLAG_ID}, /**< LY_STMT_EXTENSION */
1220 {"feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_FEATURE */
1221 {"fraction-digits", "value", SUBST_FLAG_ID}, /**< LY_STMT_FRACTION_DIGITS */
1222 {"grouping", "name", SUBST_FLAG_ID}, /**< LY_STMT_GROUPING */
1223 {"identity", "name", SUBST_FLAG_ID}, /**< LY_STMT_IDENTITY */
1224 {"if-feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_IF_FEATURE */
1225 {"import", "module", SUBST_FLAG_ID}, /**< LY_STMT_IMPORT */
1226 {"include", "module", SUBST_FLAG_ID}, /**< LY_STMT_INCLUDE */
1227 {"input", NULL, 0}, /**< LY_STMT_INPUT */
1228 {"key", "value", SUBST_FLAG_ID}, /**< LY_STMT_KEY */
1229 {"leaf", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF */
1230 {"leaf-list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF_LIST */
1231 {"length", "value", SUBST_FLAG_ID}, /**< LY_STMT_LENGTH */
1232 {"list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LIST */
1233 {"max-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MAX_ELEMENTS */
1234 {"min-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MIN_ELEMENTS */
1235 {"modifier", "value", SUBST_FLAG_ID}, /**< LY_STMT_MODIFIER */
1236 {"module", "name", SUBST_FLAG_ID}, /**< LY_STMT_MODULE */
1237 {"must", "condition", SUBST_FLAG_ID}, /**< LY_STMT_MUST */
1238 {"namespace", "uri", SUBST_FLAG_ID}, /**< LY_STMT_NAMESPACE */
1239 {"notification", "name", SUBST_FLAG_ID}, /**< LY_STMT_NOTIFICATION */
1240 {"ordered-by", "value", SUBST_FLAG_ID}, /**< LY_STMT_ORDERED_BY */
1241 {"organization", "text", SUBST_FLAG_YIN},/**< LY_STMT_ORGANIZATION */
1242 {"output", NULL, 0}, /**< LY_STMT_OUTPUT */
1243 {"path", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATH */
1244 {"pattern", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATTERN */
1245 {"position", "value", SUBST_FLAG_ID}, /**< LY_STMT_POSITION */
1246 {"prefix", "value", SUBST_FLAG_ID}, /**< LY_STMT_PREFIX */
1247 {"presence", "value", SUBST_FLAG_ID}, /**< LY_STMT_PRESENCE */
1248 {"range", "value", SUBST_FLAG_ID}, /**< LY_STMT_RANGE */
1249 {"reference", "text", SUBST_FLAG_YIN},/**< LY_STMT_REFERENCE */
1250 {"refine", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_REFINE */
1251 {"require-instance", "value", SUBST_FLAG_ID}, /**< LY_STMT_REQUIRE_INSTANCE */
1252 {"revision", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION */
1253 {"revision-date", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION_DATE */
1254 {"rpc", "name", SUBST_FLAG_ID}, /**< LY_STMT_RPC */
1255 {"submodule", "name", SUBST_FLAG_ID}, /**< LY_STMT_SUBMODULE */
1256 {"typedef", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPEDEF */
1257 {"unique", "tag", SUBST_FLAG_ID}, /**< LY_STMT_UNIQUE */
1258 {"uses", "name", SUBST_FLAG_ID}, /**< LY_STMT_USES */
1259 {"value", "value", SUBST_FLAG_ID}, /**< LY_STMT_VALUE */
1260 {"when", "condition", SUBST_FLAG_ID}, /**< LY_STMT_WHEN */
1261 {"yang-version", "value", SUBST_FLAG_ID}, /**< LY_STMT_YANG_VERSION */
1262 {"yin-element", "value", SUBST_FLAG_ID}, /**< LY_STMT_YIN_ELEMENT */
1263 {NULL, NULL, 0}, /**< LY_STMT_EXTENSION_INSTANCE */
1264 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_SEMICOLON */
1265 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_LEFT_BRACE */
1266 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_RIGHT_BRACE */
1267 {NULL, NULL, 0}, /**< LY_STMT_ARG_TEXT */
1268 {NULL, NULL, 0}, /**< LY_STMT_ARG_VALUE */
1269};
1270
1271static void
1272yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
1273{
1274 struct lysp_stmt *childstmt;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001275 int8_t flag = stmt->child ? 1 : -1;
FredGand944bdc2019-11-05 21:57:07 +08001276
1277 /* TODO:
1278 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
1279 cannot find the compiled information, so it is needed to be done,
1280 currently it is ignored */
Michal Vaskod989ba02020-08-24 10:59:24 +02001281 if (stmt_attr_info[stmt->kw].name) {
1282 if (stmt_attr_info[stmt->kw].flags & SUBST_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +08001283 ypr_open(ctx, stmt->stmt, NULL, NULL, flag);
1284 ypr_yin_arg(ctx, stmt_attr_info[stmt->kw].arg, stmt->arg);
Radek Krejci0f969882020-08-21 16:56:47 +02001285 } else {
FredGand944bdc2019-11-05 21:57:07 +08001286 ypr_open(ctx, stmt->stmt, stmt_attr_info[stmt->kw].arg, stmt->arg, flag);
1287 }
1288 }
1289
1290 if (stmt->child) {
1291 LEVEL++;
1292 LY_LIST_FOR(stmt->child, childstmt) {
1293 yprp_stmt(ctx, childstmt);
1294 }
1295 LEVEL--;
1296 ypr_close(ctx, stmt->stmt, flag);
1297 }
1298}
1299
1300/**
1301 * @param[in] count Number of extensions to print, 0 to print them all.
1302 */
1303static void
1304yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001305 struct lysp_ext_instance *ext, int8_t *flag, LY_ARRAY_COUNT_TYPE count)
FredGand944bdc2019-11-05 21:57:07 +08001306{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001307 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001308 char *str;
1309 struct lysp_stmt *stmt;
1310 const char *argument;
1311 const char *ext_argument;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001312 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001313
1314 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001315 count = LY_ARRAY_COUNT(ext);
FredGand944bdc2019-11-05 21:57:07 +08001316 }
1317 LY_ARRAY_FOR(ext, u) {
1318 if (!count) {
1319 break;
1320 }
1321
1322 count--;
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01001323 if ((ext->flags & LYS_INTERNAL) || (ext->insubstmt != substmt) || (ext->insubstmt_index != substmt_index)) {
FredGand944bdc2019-11-05 21:57:07 +08001324 continue;
1325 }
1326
1327 if (!ext->compiled && ext->yin) {
Michal Vasko5233e962020-08-14 14:26:20 +02001328 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 +08001329 continue;
1330 }
1331
1332 ypr_close_parent(ctx, flag);
Radek Krejci1deb5be2020-08-26 16:43:36 +02001333 inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001334 argument = NULL;
1335 ext_argument = NULL;
1336
1337 if (ext[u].compiled) {
1338 argument = ext[u].compiled->argument;
1339 ext_argument = ext[u].compiled->def->argument;
1340 } else {
1341 argument = ext[u].argument;
1342 }
1343
1344 if (ext->yin) {
1345 ypr_open(ctx, ext[u].name, NULL, NULL, 1);
1346 if (asprintf(&str, "%s:%s", ext[u].compiled->def->module->prefix, ext_argument) == -1) {
1347 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +08001348 return;
1349 }
1350 LEVEL++;
1351 inner_flag = 1;
1352 ypr_yin_arg(ctx, str, argument);
1353 free(str);
1354 str = NULL;
1355 LEVEL--;
1356 } else {
1357 ypr_open(ctx, ext[u].name, ext_argument, argument, inner_flag);
1358 }
1359
1360 LEVEL++;
1361 LY_LIST_FOR(ext[u].child, stmt) {
1362 ypr_close_parent(ctx, &inner_flag);
1363 yprp_stmt(ctx, stmt);
1364 }
1365 LEVEL--;
1366 ypr_close(ctx, ext[u].name, inner_flag);
1367 }
1368}
1369
Michal Vasko7c8439f2020-08-05 13:25:19 +02001370static void
1371yin_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001372{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001373 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001374
FredGand944bdc2019-11-05 21:57:07 +08001375 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01001376 if (modp->imports[u].flags & LYS_INTERNAL) {
1377 continue;
1378 }
1379
Michal Vasko7c8439f2020-08-05 13:25:19 +02001380 ypr_open(ctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001381 LEVEL++;
1382 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
1383 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
1384 if (modp->imports[u].rev[0]) {
1385 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
1386 }
1387 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1388 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
1389 LEVEL--;
1390 ypr_close(ctx, "import", 1);
1391 }
1392 LY_ARRAY_FOR(modp->includes, u) {
1393 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 +02001394 ypr_open(ctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001395 LEVEL++;
1396 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
1397 if (modp->includes[u].rev[0]) {
1398 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
1399 }
1400 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1401 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
1402 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001403 ly_print_(ctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001404 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001405 ypr_open(ctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001406 }
1407 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001408}
FredGand944bdc2019-11-05 21:57:07 +08001409
Michal Vasko7c8439f2020-08-05 13:25:19 +02001410static void
1411yin_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
1412{
1413 LY_ARRAY_COUNT_TYPE u;
1414 struct lysp_node *data;
FredGand944bdc2019-11-05 21:57:07 +08001415
FredGand944bdc2019-11-05 21:57:07 +08001416 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001417 ly_print_(ctx->out, "\n");
FredGand944bdc2019-11-05 21:57:07 +08001418 yprp_extension(ctx, &modp->extensions[u]);
1419 }
1420 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001421 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001422 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001423 }
1424
1425 LY_ARRAY_FOR(modp->features, u) {
1426 yprp_feature(ctx, &modp->features[u]);
1427 }
1428
1429 LY_ARRAY_FOR(modp->identities, u) {
1430 yprp_identity(ctx, &modp->identities[u]);
1431 }
1432
1433 LY_ARRAY_FOR(modp->typedefs, u) {
1434 yprp_typedef(ctx, &modp->typedefs[u]);
1435 }
1436
1437 LY_ARRAY_FOR(modp->groupings, u) {
1438 yprp_grouping(ctx, &modp->groupings[u]);
1439 }
1440
1441 LY_LIST_FOR(modp->data, data) {
1442 yprp_node(ctx, data);
1443 }
1444
1445 LY_ARRAY_FOR(modp->augments, u) {
1446 yprp_augment(ctx, &modp->augments[u]);
1447 }
1448
1449 LY_ARRAY_FOR(modp->rpcs, u) {
1450 yprp_action(ctx, &modp->rpcs[u]);
1451 }
1452
1453 LY_ARRAY_FOR(modp->notifs, u) {
1454 yprp_notification(ctx, &modp->notifs[u]);
1455 }
1456
1457 LY_ARRAY_FOR(modp->deviations, u) {
1458 yprp_deviation(ctx, &modp->deviations[u]);
1459 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001460}
1461
1462LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001463yin_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 +02001464{
1465 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02001466 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001467
Michal Vasko5233e962020-08-14 14:26:20 +02001468 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1469 ly_print_(ctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001470 ypr_xmlns(ctx, module, XML_NS_INDENT);
1471 ypr_import_xmlns(ctx, modp, XML_NS_INDENT);
Michal Vasko5233e962020-08-14 14:26:20 +02001472 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001473
1474 LEVEL++;
1475
1476 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001477 if (modp->version) {
1478 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 +02001479 }
1480 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
1481 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
1482
1483 /* linkage-stmts (import/include) */
1484 yin_print_parsed_linkage(ctx, modp);
1485
1486 /* meta-stmts */
1487 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001488 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001489 }
1490 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
1491 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
1492 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
1493 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
1494
1495 /* revision-stmts */
1496 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001497 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001498 }
1499 LY_ARRAY_FOR(modp->revs, u) {
1500 yprp_revision(ctx, &modp->revs[u]);
1501 }
1502
1503 /* body-stmts */
1504 yin_print_parsed_body(ctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001505
1506 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001507 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001508 ly_print_flush(out);
1509
1510 return LY_SUCCESS;
1511}
1512
Michal Vasko7c8439f2020-08-05 13:25:19 +02001513static void
1514yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
1515{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001516 ypr_open(ctx, "belongs-to", "module", submodp->mod->name, 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001517 LEVEL++;
1518 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
1519 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
1520 LEVEL--;
1521 ypr_close(ctx, "belongs-to", 1);
1522}
1523
1524LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001525yin_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 +02001526{
1527 LY_ARRAY_COUNT_TYPE u;
Radek Krejci52f65552020-09-01 17:03:35 +02001528 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001529
Michal Vasko5233e962020-08-14 14:26:20 +02001530 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1531 ly_print_(ctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001532 ypr_xmlns(ctx, module, XML_NS_INDENT);
1533 ypr_import_xmlns(ctx, (struct lysp_module *)submodp, XML_NS_INDENT);
Michal Vasko5233e962020-08-14 14:26:20 +02001534 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001535
1536 LEVEL++;
1537
1538 /* submodule-header-stmts */
1539 if (submodp->version) {
1540 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
1541 }
1542 yprp_belongsto(ctx, submodp);
1543
1544 /* linkage-stmts (import/include) */
1545 yin_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
1546
1547 /* meta-stmts */
1548 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001549 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001550 }
1551 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1552 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
1553 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1554 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
1555
1556 /* revision-stmts */
1557 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001558 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001559 }
1560 LY_ARRAY_FOR(submodp->revs, u) {
1561 yprp_revision(ctx, &submodp->revs[u]);
1562 }
1563
1564 /* body-stmts */
1565 yin_print_parsed_body(ctx, (struct lysp_module *)submodp);
1566
1567 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001568 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001569 ly_print_flush(out);
1570
1571 return LY_SUCCESS;
1572}