blob: bef314a38516fe54d88896c3ce75f3961b9272d6 [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 Krejci241f6b52020-05-21 18:13:49 +020036 struct ly_out *out; /**< output specification */
FredGand944bdc2019-11-05 21:57:07 +080037 unsigned int level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
38 const struct lys_module *module; /**< schema to print */
Radek Krejci5536d282020-08-04 23:27:44 +020039 int options; /**< Schema output options (see @ref schemaprinterflags). */
FredGand944bdc2019-11-05 21:57:07 +080040};
41
FredGand944bdc2019-11-05 21:57:07 +080042static void yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +020043 struct lysp_ext_instance *ext, int *flag, LY_ARRAY_COUNT_TYPE count);
FredGand944bdc2019-11-05 21:57:07 +080044
45static void
Michal Vasko7c8439f2020-08-05 13:25:19 +020046ypr_open(struct ypr_ctx *ctx, const char *elem_name, const char *attr_name, const char *attr_value, int 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
60ypr_close(struct ypr_ctx *ctx, const char *elem_name, int flag)
61{
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
75ypr_close_parent(struct ypr_ctx *ctx, int *par_close_flag)
76{
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;
FredGand944bdc2019-11-05 21:57:07 +080095 int extflag = 0;
96
97 if (!text) {
98 /* nothing to print */
99 return;
100 }
101
102 if (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) {
103 extflag = 1;
104 ypr_open(ctx, ext_substmt_info[substmt].name, NULL, NULL, extflag);
105 } else {
106 ypr_open(ctx, ext_substmt_info[substmt].name, ext_substmt_info[substmt].arg, text, extflag);
107 }
108
109 LEVEL++;
110 LY_ARRAY_FOR(ext, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200111 if (((struct lysp_ext_instance *)ext)[u].insubstmt != substmt || ((struct lysp_ext_instance *)ext)[u].insubstmt_index != substmt_index) {
FredGand944bdc2019-11-05 21:57:07 +0800112 continue;
113 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200114 yprp_extension_instances(ctx, substmt, substmt_index, &((struct lysp_ext_instance *)ext)[u], &extflag, 1);
FredGand944bdc2019-11-05 21:57:07 +0800115 }
116
117 /* argument as yin-element */
118 if (ext_substmt_info[substmt].flags & SUBST_FLAG_YIN) {
119 ypr_yin_arg(ctx, ext_substmt_info[substmt].arg, text);
120 }
121
122 LEVEL--;
123 ypr_close(ctx, ext_substmt_info[substmt].name, extflag);
124}
125
126static void
127ypr_unsigned(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, unsigned int attr_value)
128{
129 char *str;
130 if (asprintf(&str, "%u", attr_value) == -1) {
131 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800132 return;
133 }
134 ypr_substmt(ctx, substmt, substmt_index, str, exts);
135 free(str);
136}
137
138static void
139ypr_signed(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index, void *exts, signed int attr_value)
140{
141 char *str;
142
143 if (asprintf(&str, "%d", attr_value) == -1) {
144 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800145 return;
146 }
147 ypr_substmt(ctx, substmt, substmt_index, str, exts);
148 free(str);
149}
150
151static void
152yprp_revision(struct ypr_ctx *ctx, const struct lysp_revision *rev)
153{
154 if (rev->dsc || rev->ref || rev->exts) {
155 ypr_open(ctx, "revision", "date", rev->date, 1);
156 LEVEL++;
157 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rev->exts, NULL, 0);
158 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, rev->dsc, rev->exts);
159 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, rev->ref, rev->exts);
160 LEVEL--;
161 ypr_close(ctx, "revision", 1);
162 } else {
163 ypr_open(ctx, "revision", "date", rev->date, -1);
164 }
165}
166
167static void
168ypr_mandatory(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
169{
170 if (flags & LYS_MAND_MASK) {
171 ypr_close_parent(ctx, flag);
172 ypr_substmt(ctx, LYEXT_SUBSTMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
173 }
174}
175
176static void
177ypr_config(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
178{
179 if (flags & LYS_CONFIG_MASK) {
180 ypr_close_parent(ctx, flag);
181 ypr_substmt(ctx, LYEXT_SUBSTMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
182 }
183}
184
185static void
186ypr_status(struct ypr_ctx *ctx, uint16_t flags, void *exts, int *flag)
187{
188 const char *status = NULL;
189
190 if (flags & LYS_STATUS_CURR) {
191 ypr_close_parent(ctx, flag);
192 status = "current";
193 } else if (flags & LYS_STATUS_DEPRC) {
194 ypr_close_parent(ctx, flag);
195 status = "deprecated";
196 } else if (flags & LYS_STATUS_OBSLT) {
197 ypr_close_parent(ctx, flag);
198 status = "obsolete";
199 }
200
201 ypr_substmt(ctx, LYEXT_SUBSTMT_STATUS, 0, status, exts);
202}
203
204static void
205ypr_description(struct ypr_ctx *ctx, const char *dsc, void *exts, int *flag)
206{
207 if (dsc) {
208 ypr_close_parent(ctx, flag);
209 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, dsc, exts);
210 }
211}
212
213static void
214ypr_reference(struct ypr_ctx *ctx, const char *ref, void *exts, int *flag)
215{
216 if (ref) {
217 ypr_close_parent(ctx, flag);
218 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, ref, exts);
219 }
220}
221
222static void
223yprp_iffeatures(struct ypr_ctx *ctx, const char **iff, struct lysp_ext_instance *exts, int *flag)
224{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200225 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800226 int extflag;
227
228 LY_ARRAY_FOR(iff, u) {
229 ypr_close_parent(ctx, flag);
230 extflag = 0;
231
Michal Vasko5233e962020-08-14 14:26:20 +0200232 ly_print_(ctx->out, "%*s<if-feature name=\"%s", INDENT, iff[u]);
FredGand944bdc2019-11-05 21:57:07 +0800233
234 /* extensions */
235 LEVEL++;
236 LY_ARRAY_FOR(exts, u) {
237 if (exts[u].insubstmt != LYEXT_SUBSTMT_IFFEATURE || exts[u].insubstmt_index != u) {
238 continue;
239 }
240 yprp_extension_instances(ctx, LYEXT_SUBSTMT_IFFEATURE, u, &exts[u], &extflag, 1);
241 }
242 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +0200243 ly_print_(ctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800244 }
245}
246
247static void
248yprp_extension(struct ypr_ctx *ctx, const struct lysp_ext *ext)
249{
250 int flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200251 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800252
253 ypr_open(ctx, "extension", "name", ext->name, flag);
254 LEVEL++;
255
256 if (ext->exts) {
257 ypr_close_parent(ctx, &flag);
258 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ext->exts, &flag, 0);
259 }
260
261 if (ext->argument) {
262 ypr_close_parent(ctx, &flag);
263 ypr_open(ctx, "argument", "name", ext->argument, flag2);
264
265 LEVEL++;
266 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200267 u = -1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200268 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LYEXT_SUBSTMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
FredGand944bdc2019-11-05 21:57:07 +0800269 ypr_close_parent(ctx, &flag2);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200270 yprp_extension_instances(ctx, LYEXT_SUBSTMT_ARGUMENT, 0, &ext->exts[u], &flag2, 1);
FredGand944bdc2019-11-05 21:57:07 +0800271 }
272 }
273 if ((ext->flags & LYS_YINELEM_MASK) ||
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200274 (ext->exts && lysp_ext_instance_iter(ext->exts, 0, LYEXT_SUBSTMT_YINELEM) != LY_ARRAY_COUNT(ext->exts))) {
FredGand944bdc2019-11-05 21:57:07 +0800275 ypr_close_parent(ctx, &flag2);
276 ypr_substmt(ctx, LYEXT_SUBSTMT_YINELEM, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
277 }
278 LEVEL--;
279 ypr_close(ctx, "argument", flag2);
280 }
281
282 ypr_status(ctx, ext->flags, ext->exts, &flag);
283 ypr_description(ctx, ext->dsc, ext->exts, &flag);
284 ypr_reference(ctx, ext->ref, ext->exts, &flag);
285
286 LEVEL--;
287 ypr_close(ctx, "extension", flag);
288}
289
290static void
291yprp_feature(struct ypr_ctx *ctx, const struct lysp_feature *feat)
292{
293 int flag = 0;
294
295 ypr_open(ctx, "feature", "name", feat->name, flag);
296 LEVEL++;
297 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, feat->exts, &flag, 0);
298 yprp_iffeatures(ctx, feat->iffeatures, feat->exts, &flag);
299 ypr_status(ctx, feat->flags, feat->exts, &flag);
300 ypr_description(ctx, feat->dsc, feat->exts, &flag);
301 ypr_reference(ctx, feat->ref, feat->exts, &flag);
302 LEVEL--;
303 ypr_close(ctx, "feature", flag);
304}
305
306static void
307yprp_identity(struct ypr_ctx *ctx, const struct lysp_ident *ident)
308{
309 int flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200310 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800311
312 ypr_open(ctx, "identity", "name", ident->name, flag);
313 LEVEL++;
314
315 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, ident->exts, &flag, 0);
316 yprp_iffeatures(ctx, ident->iffeatures, ident->exts, &flag);
317
318 LY_ARRAY_FOR(ident->bases, u) {
319 ypr_close_parent(ctx, &flag);
320 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, ident->bases[u], ident->exts);
321 }
322
323 ypr_status(ctx, ident->flags, ident->exts, &flag);
324 ypr_description(ctx, ident->dsc, ident->exts, &flag);
325 ypr_reference(ctx, ident->ref, ident->exts, &flag);
326
327 LEVEL--;
328 ypr_close(ctx, "identity", flag);
329}
330
331static void
332yprp_restr(struct ypr_ctx *ctx, const struct lysp_restr *restr, const char *name, const char *attr, int *flag)
333{
334 (void)flag;
335 int inner_flag = 0;
336
337 if (!restr) {
338 return;
339 }
340
Michal Vasko5233e962020-08-14 14:26:20 +0200341 ly_print_(ctx->out, "%*s<%s %s=\"", INDENT, name, attr);
FredGand944bdc2019-11-05 21:57:07 +0800342 lyxml_dump_text(ctx->out, (restr->arg[0] != 0x15 && restr->arg[0] != 0x06) ? restr->arg : &restr->arg[1], 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200343 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800344
345 LEVEL++;
346 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, restr->exts, &inner_flag, 0);
347 if (restr->arg[0] == 0x15) {
348 ypr_close_parent(ctx, &inner_flag);
349 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
350 ypr_substmt(ctx, LYEXT_SUBSTMT_MODIFIER, 0, "invert-match", restr->exts);
351 }
352 if (restr->emsg) {
353 ypr_close_parent(ctx, &inner_flag);
354 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRMSG, 0, restr->emsg, restr->exts);
355 }
356 if (restr->eapptag) {
357 ypr_close_parent(ctx, &inner_flag);
358 ypr_substmt(ctx, LYEXT_SUBSTMT_ERRTAG, 0, restr->eapptag, restr->exts);
359 }
360 ypr_description(ctx, restr->dsc, restr->exts, &inner_flag);
361 ypr_reference(ctx, restr->ref, restr->exts, &inner_flag);
362
363 LEVEL--;
364 ypr_close(ctx, name, inner_flag);
365}
366
367static void
368yprp_when(struct ypr_ctx *ctx, struct lysp_when *when, int *flag)
369{
370 int inner_flag = 0;
371 (void)flag;
372
373 if (!when) {
374 return;
375 }
376
Michal Vasko5233e962020-08-14 14:26:20 +0200377 ly_print_(ctx->out, "%*s<when condition=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +0800378 lyxml_dump_text(ctx->out, when->cond, 1);
Michal Vasko5233e962020-08-14 14:26:20 +0200379 ly_print_(ctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800380
381 LEVEL++;
382 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, when->exts, &inner_flag, 0);
383 ypr_description(ctx, when->dsc, when->exts, &inner_flag);
384 ypr_reference(ctx, when->ref, when->exts, &inner_flag);
385 LEVEL--;
386 ypr_close(ctx, "when", inner_flag);
387}
388
389static void
390yprp_enum(struct ypr_ctx *ctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int *flag)
391{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200392 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800393 int inner_flag;
394 (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 Krejci7eb54ba2020-05-18 16:30:04 +0200431 int flag = 0;
432
FredGand944bdc2019-11-05 21:57:07 +0800433 if (!ctx || !type) {
434 return;
435 }
436
FredGand944bdc2019-11-05 21:57:07 +0800437 ypr_open(ctx, "type", "name", type->name, flag);
438 LEVEL++;
439
440 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, type->exts, &flag, 0);
441
442 if (type->range || type->length || type->patterns || type->bits || type->enums) {
443 ypr_close_parent(ctx, &flag);
444 }
445 yprp_restr(ctx, type->range, "range", "value", &flag);
446 yprp_restr(ctx, type->length, "length", "value", &flag);
447 LY_ARRAY_FOR(type->patterns, u) {
448 yprp_restr(ctx, &type->patterns[u], "pattern", "value", &flag);
449 }
450 yprp_enum(ctx, type->bits, LY_TYPE_BITS, &flag);
451 yprp_enum(ctx, type->enums, LY_TYPE_ENUM, &flag);
452
453 if (type->path) {
454 ypr_close_parent(ctx, &flag);
Michal Vasko004d3152020-06-11 19:59:22 +0200455 ypr_substmt(ctx, LYEXT_SUBSTMT_PATH, 0, type->path->expr, type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800456 }
457 if (type->flags & LYS_SET_REQINST) {
458 ypr_close_parent(ctx, &flag);
459 ypr_substmt(ctx, LYEXT_SUBSTMT_REQINSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
460 }
461 if (type->flags & LYS_SET_FRDIGITS) {
462 ypr_close_parent(ctx, &flag);
463 ypr_unsigned(ctx, LYEXT_SUBSTMT_FRACDIGITS, 0, type->exts, type->fraction_digits);
464 }
465 LY_ARRAY_FOR(type->bases, u) {
466 ypr_close_parent(ctx, &flag);
467 ypr_substmt(ctx, LYEXT_SUBSTMT_BASE, u, type->bases[u], type->exts);
468 }
469 LY_ARRAY_FOR(type->types, u) {
470 ypr_close_parent(ctx, &flag);
471 yprp_type(ctx, &type->types[u]);
472 }
473
474 LEVEL--;
475 ypr_close(ctx, "type", flag);
476}
477
478static void
479yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
480{
FredGand944bdc2019-11-05 21:57:07 +0800481 ypr_open(ctx, "typedef", "name", tpdf->name, 1);
482 LEVEL++;
483
484 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, tpdf->exts, NULL, 0);
485
486 yprp_type(ctx, &tpdf->type);
487
488 if (tpdf->units) {
489 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, tpdf->units, tpdf->exts);
490 }
491 if (tpdf->dflt) {
492 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, tpdf->dflt, tpdf->exts);
493 }
494
495 ypr_status(ctx, tpdf->flags, tpdf->exts, NULL);
496 ypr_description(ctx, tpdf->dsc, tpdf->exts, NULL);
497 ypr_reference(ctx, tpdf->ref, tpdf->exts, NULL);
498
499 LEVEL--;
500 ypr_close(ctx, "typedef", 1);
501}
502
503static void yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node);
504static void yprp_action(struct ypr_ctx *ctx, const struct lysp_action *action);
505
506static void
507yprp_grouping(struct ypr_ctx *ctx, const struct lysp_grp *grp)
508{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200509 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800510 int flag = 0;
511 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
546yprp_inout(struct ypr_ctx *ctx, const struct lysp_action_inout *inout, int *flag)
547{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200548 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800549 struct lysp_node *data;
550
551 if (!inout->nodetype) {
552 /* nodetype not set -> input/output is empty */
553 return;
554 }
555 ypr_close_parent(ctx, flag);
556
557 ypr_open(ctx, (inout->nodetype == LYS_INPUT ? "input" : "output"), NULL, NULL, *flag);
558 LEVEL++;
559
560 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, inout->exts, NULL, 0);
561 LY_ARRAY_FOR(inout->musts, u) {
562 yprp_restr(ctx, &inout->musts[u], "must", "condition", NULL);
563 }
564 LY_ARRAY_FOR(inout->typedefs, u) {
565 yprp_typedef(ctx, &inout->typedefs[u]);
566 }
567 LY_ARRAY_FOR(inout->groupings, u) {
568 yprp_grouping(ctx, &inout->groupings[u]);
569 }
570
571 LY_LIST_FOR(inout->data, data) {
572 yprp_node(ctx, data);
573 }
574
575 LEVEL--;
576 ypr_close(ctx, (inout->nodetype == LYS_INPUT ? "input" : "output"), 1);
577}
578
579static void
580yprp_notification(struct ypr_ctx *ctx, const struct lysp_notif *notif)
581{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200582 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800583 int flag = 0;
584 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;
FredGand944bdc2019-11-05 21:57:07 +0800623 int flag = 0;
624
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
652yprp_node_common1(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
653{
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
663yprp_node_common2(struct ypr_ctx *ctx, const struct lysp_node *node, int *flag)
664{
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;
FredGand944bdc2019-11-05 21:57:07 +0800678 int flag = 0;
679 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{
727 int flag = 0;
728 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{
746 int flag = 0;
747 struct lysp_node *child;
748 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
749
750 yprp_node_common1(ctx, node, &flag);
751
752 if (choice->dflt) {
753 ypr_close_parent(ctx, &flag);
754 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, choice->dflt, choice->exts);
755 }
756
757 yprp_node_common2(ctx, node, &flag);
758
759 LY_LIST_FOR(choice->child, child) {
760 ypr_close_parent(ctx, &flag);
761 yprp_node(ctx, child);
762 }
763
764 LEVEL--;
765 ypr_close(ctx, "choice", flag);
766}
767
768static void
769yprp_leaf(struct ypr_ctx *ctx, const struct lysp_node *node)
770{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200771 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800772 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
773
774 int flag = 1;
775 yprp_node_common1(ctx, node, &flag);
776
777 yprp_type(ctx, &leaf->type);
778 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, leaf->units, leaf->exts);
779 LY_ARRAY_FOR(leaf->musts, u) {
780 yprp_restr(ctx, &leaf->musts[u], "must", "condition", &flag);
781 }
782 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, leaf->dflt, leaf->exts);
783
784 yprp_node_common2(ctx, node, &flag);
785
786 LEVEL--;
787 ypr_close(ctx, "leaf", flag);
788}
789
790static void
791yprp_leaflist(struct ypr_ctx *ctx, const struct lysp_node *node)
792{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200793 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800794 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
795 int flag = 1;
796
797 yprp_node_common1(ctx, node, &flag);
798
799 yprp_type(ctx, &llist->type);
800 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, llist->units, llist->exts);
801 LY_ARRAY_FOR(llist->musts, u) {
802 yprp_restr(ctx, &llist->musts[u], "must", "condition", NULL);
803 }
804 LY_ARRAY_FOR(llist->dflts, u) {
805 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, llist->dflts[u], llist->exts);
806 }
807
808 ypr_config(ctx, node->flags, node->exts, NULL);
809
810 if (llist->flags & LYS_SET_MIN) {
811 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, llist->exts, llist->min);
812 }
813 if (llist->flags & LYS_SET_MAX) {
814 if (llist->max) {
815 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, llist->exts, llist->max);
816 } else {
817 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", llist->exts);
818 }
819 }
820
821 if (llist->flags & LYS_ORDBY_MASK) {
822 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
823 }
824
825 ypr_status(ctx, node->flags, node->exts, &flag);
826 ypr_description(ctx, node->dsc, node->exts, &flag);
827 ypr_reference(ctx, node->ref, node->exts, &flag);
828
829 LEVEL--;
830 ypr_close(ctx, "leaf-list", flag);
831}
832
833static void
834yprp_list(struct ypr_ctx *ctx, const struct lysp_node *node)
835{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200836 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800837 int flag = 0;
838 struct lysp_node *child;
839 struct lysp_node_list *list = (struct lysp_node_list *)node;
840
841 yprp_node_common1(ctx, node, &flag);
842
843 LY_ARRAY_FOR(list->musts, u) {
844 ypr_close_parent(ctx, &flag);
845 yprp_restr(ctx, &list->musts[u], "must", "condition", &flag);
846 }
847 if (list->key) {
848 ypr_close_parent(ctx, &flag);
849 ypr_substmt(ctx, LYEXT_SUBSTMT_KEY, 0, list->key, list->exts);
850 }
851 LY_ARRAY_FOR(list->uniques, u) {
852 ypr_close_parent(ctx, &flag);
853 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, list->uniques[u], list->exts);
854 }
855
856 ypr_config(ctx, node->flags, node->exts, NULL);
857
858 if (list->flags & LYS_SET_MIN) {
859 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, list->exts, list->min);
860 }
861 if (list->flags & LYS_SET_MAX) {
862 if (list->max) {
863 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, list->exts, list->max);
864 } else {
865 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", list->exts);
866 }
867 }
868
869 if (list->flags & LYS_ORDBY_MASK) {
870 ypr_close_parent(ctx, &flag);
871 ypr_substmt(ctx, LYEXT_SUBSTMT_ORDEREDBY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
872 }
873
874 ypr_status(ctx, node->flags, node->exts, &flag);
875 ypr_description(ctx, node->dsc, node->exts, &flag);
876 ypr_reference(ctx, node->ref, node->exts, &flag);
877
878 LY_ARRAY_FOR(list->typedefs, u) {
879 ypr_close_parent(ctx, &flag);
880 yprp_typedef(ctx, &list->typedefs[u]);
881 }
882
883 LY_ARRAY_FOR(list->groupings, u) {
884 ypr_close_parent(ctx, &flag);
885 yprp_grouping(ctx, &list->groupings[u]);
886 }
887
888 LY_LIST_FOR(list->child, child) {
889 ypr_close_parent(ctx, &flag);
890 yprp_node(ctx, child);
891 }
892
893 LY_ARRAY_FOR(list->actions, u) {
894 ypr_close_parent(ctx, &flag);
895 yprp_action(ctx, &list->actions[u]);
896 }
897
898 LY_ARRAY_FOR(list->notifs, u) {
899 ypr_close_parent(ctx, &flag);
900 yprp_notification(ctx, &list->notifs[u]);
901 }
902
903 LEVEL--;
904 ypr_close(ctx, "list", flag);
905}
906
907static void
908yprp_refine(struct ypr_ctx *ctx, struct lysp_refine *refine)
909{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200910 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800911 int flag = 0;
912
913 ypr_open(ctx, "refine", "target-node", refine->nodeid, flag);
914 LEVEL++;
915
916 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, refine->exts, &flag, 0);
917 yprp_iffeatures(ctx, refine->iffeatures, refine->exts, &flag);
918
919 LY_ARRAY_FOR(refine->musts, u) {
920 ypr_close_parent(ctx, &flag);
921 yprp_restr(ctx, &refine->musts[u], "must", "condition", &flag);
922 }
923
924 if (refine->presence) {
925 ypr_close_parent(ctx, &flag);
926 ypr_substmt(ctx, LYEXT_SUBSTMT_PRESENCE, 0, refine->presence, refine->exts);
927 }
928
929 LY_ARRAY_FOR(refine->dflts, u) {
930 ypr_close_parent(ctx, &flag);
931 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, refine->dflts[u], refine->exts);
932 }
933
934 ypr_config(ctx, refine->flags, refine->exts, &flag);
935 ypr_mandatory(ctx, refine->flags, refine->exts, &flag);
936
937 if (refine->flags & LYS_SET_MIN) {
938 ypr_close_parent(ctx, &flag);
939 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, refine->exts, refine->min);
940 }
941 if (refine->flags & LYS_SET_MAX) {
942 ypr_close_parent(ctx, &flag);
943 if (refine->max) {
944 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, refine->exts, refine->max);
945 } else {
946 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", refine->exts);
947 }
948 }
949
950 ypr_description(ctx, refine->dsc, refine->exts, &flag);
951 ypr_reference(ctx, refine->ref, refine->exts, &flag);
952
953 LEVEL--;
954 ypr_close(ctx, "refine", flag);
955}
956
957static void
958yprp_augment(struct ypr_ctx *ctx, const struct lysp_augment *aug)
959{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200960 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800961 struct lysp_node *child;
962
963 ypr_open(ctx, "augment", "target-node", aug->nodeid, 1);
964 LEVEL++;
965
966 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, aug->exts, NULL, 0);
967 yprp_when(ctx, aug->when, NULL);
968 yprp_iffeatures(ctx, aug->iffeatures, aug->exts, NULL);
969 ypr_status(ctx, aug->flags, aug->exts, NULL);
970 ypr_description(ctx, aug->dsc, aug->exts, NULL);
971 ypr_reference(ctx, aug->ref, aug->exts, NULL);
972
973 LY_LIST_FOR(aug->child, child) {
974 yprp_node(ctx, child);
975 }
976
977 LY_ARRAY_FOR(aug->actions, u) {
978 yprp_action(ctx, &aug->actions[u]);
979 }
980
981 LY_ARRAY_FOR(aug->notifs, u) {
982 yprp_notification(ctx, &aug->notifs[u]);
983 }
984
985 LEVEL--;
986 ypr_close(ctx, "augment", 1);
987}
988
FredGand944bdc2019-11-05 21:57:07 +0800989static void
990yprp_uses(struct ypr_ctx *ctx, const struct lysp_node *node)
991{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200992 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800993 int flag = 0;
994 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
995
996 yprp_node_common1(ctx, node, &flag);
997 yprp_node_common2(ctx, node, &flag);
998
999 LY_ARRAY_FOR(uses->refines, u) {
1000 ypr_close_parent(ctx, &flag);
1001 yprp_refine(ctx, &uses->refines[u]);
1002 }
1003
1004 LY_ARRAY_FOR(uses->augments, u) {
1005 ypr_close_parent(ctx, &flag);
1006 yprp_augment(ctx, &uses->augments[u]);
1007 }
1008
1009 LEVEL--;
1010 ypr_close(ctx, "uses", flag);
1011}
1012
1013static void
1014yprp_anydata(struct ypr_ctx *ctx, const struct lysp_node *node)
1015{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001016 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001017 int flag = 0;
1018 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1019
1020 yprp_node_common1(ctx, node, &flag);
1021
1022 LY_ARRAY_FOR(any->musts, u) {
1023 ypr_close_parent(ctx, &flag);
1024 yprp_restr(ctx, &any->musts[u], "must", "condition", &flag);
1025 }
1026
1027 yprp_node_common2(ctx, node, &flag);
1028
1029 LEVEL--;
1030 ypr_close(ctx, lys_nodetype2str(node->nodetype), flag);
1031}
1032
1033static void
1034yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
1035{
FredGand944bdc2019-11-05 21:57:07 +08001036 switch (node->nodetype) {
1037 case LYS_CONTAINER:
1038 yprp_container(ctx, node);
1039 break;
1040 case LYS_CHOICE:
1041 yprp_choice(ctx, node);
1042 break;
1043 case LYS_LEAF:
1044 yprp_leaf(ctx, node);
1045 break;
1046 case LYS_LEAFLIST:
1047 yprp_leaflist(ctx, node);
1048 break;
1049 case LYS_LIST:
1050 yprp_list(ctx, node);
1051 break;
1052 case LYS_USES:
1053 yprp_uses(ctx, node);
1054 break;
1055 case LYS_ANYXML:
1056 case LYS_ANYDATA:
1057 yprp_anydata(ctx, node);
1058 break;
1059 case LYS_CASE:
1060 yprp_case(ctx, node);
1061 break;
1062 default:
1063 break;
1064 }
1065}
1066
1067static void
1068yprp_deviation(struct ypr_ctx *ctx, const struct lysp_deviation *deviation)
1069{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001070 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001071 struct lysp_deviate_add *add;
1072 struct lysp_deviate_rpl *rpl;
1073 struct lysp_deviate_del *del;
1074 struct lysp_deviate *elem;
1075
1076 ypr_open(ctx, "deviation", "target-node", deviation->nodeid, 1);
1077 LEVEL++;
1078
1079 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, deviation->exts, NULL, 0);
1080 ypr_description(ctx, deviation->dsc, deviation->exts, NULL);
1081 ypr_reference(ctx, deviation->ref, deviation->exts, NULL);
1082
1083 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko5233e962020-08-14 14:26:20 +02001084 ly_print_(ctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001085 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1086 if (elem->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001087 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001088 LEVEL++;
1089
1090 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, elem->exts, NULL, 0);
1091 } else {
Michal Vasko5233e962020-08-14 14:26:20 +02001092 ly_print_(ctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001093 continue;
1094 }
1095 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001096 add = (struct lysp_deviate_add *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001097 ly_print_(ctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001098 LEVEL++;
1099
1100 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, add->exts, NULL, 0);
1101 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001102 LY_ARRAY_FOR(add->musts, u) {
1103 yprp_restr(ctx, &add->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001104 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001105 LY_ARRAY_FOR(add->uniques, u) {
1106 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, add->uniques[u], add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001107 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001108 LY_ARRAY_FOR(add->dflts, u) {
1109 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, add->dflts[u], add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001110 }
1111 ypr_config(ctx, add->flags, add->exts, NULL);
1112 ypr_mandatory(ctx, add->flags, add->exts, NULL);
1113 if (add->flags & LYS_SET_MIN) {
1114 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, add->exts, add->min);
1115 }
1116 if (add->flags & LYS_SET_MAX) {
1117 if (add->max) {
1118 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, add->exts, add->max);
1119 } else {
1120 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", add->exts);
1121 }
1122 }
1123 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001124 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001125 ly_print_(ctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001126 LEVEL++;
1127
1128 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, rpl->exts, NULL, 0);
1129 if (rpl->type) {
1130 yprp_type(ctx, rpl->type);
1131 }
1132 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, rpl->units, rpl->exts);
1133 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, 0, rpl->dflt, rpl->exts);
1134 ypr_config(ctx, rpl->flags, rpl->exts, NULL);
1135 ypr_mandatory(ctx, rpl->flags, rpl->exts, NULL);
1136 if (rpl->flags & LYS_SET_MIN) {
1137 ypr_unsigned(ctx, LYEXT_SUBSTMT_MIN, 0, rpl->exts, rpl->min);
1138 }
1139 if (rpl->flags & LYS_SET_MAX) {
1140 if (rpl->max) {
1141 ypr_unsigned(ctx, LYEXT_SUBSTMT_MAX, 0, rpl->exts, rpl->max);
1142 } else {
1143 ypr_substmt(ctx, LYEXT_SUBSTMT_MAX, 0, "unbounded", rpl->exts);
1144 }
1145 }
1146 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001147 del = (struct lysp_deviate_del *)elem;
Michal Vasko5233e962020-08-14 14:26:20 +02001148 ly_print_(ctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001149 LEVEL++;
1150
1151 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, del->exts, NULL, 0);
1152 ypr_substmt(ctx, LYEXT_SUBSTMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001153 LY_ARRAY_FOR(del->musts, u) {
1154 yprp_restr(ctx, &del->musts[u], "must", "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001155 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001156 LY_ARRAY_FOR(del->uniques, u) {
1157 ypr_substmt(ctx, LYEXT_SUBSTMT_UNIQUE, u, del->uniques[u], del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001158 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001159 LY_ARRAY_FOR(del->dflts, u) {
1160 ypr_substmt(ctx, LYEXT_SUBSTMT_DEFAULT, u, del->dflts[u], del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001161 }
1162 }
1163
1164 LEVEL--;
1165 ypr_close(ctx, "deviate", 1);
1166 }
1167
1168 LEVEL--;
1169 ypr_close(ctx, "deviation", 1);
1170}
1171
FredGand944bdc2019-11-05 21:57:07 +08001172static void
1173ypr_xmlns(struct ypr_ctx *ctx, const struct lys_module *module, unsigned int indent)
1174{
Michal Vasko5233e962020-08-14 14:26:20 +02001175 ly_print_(ctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1176 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001177}
FredGand944bdc2019-11-05 21:57:07 +08001178
Michal Vasko7c8439f2020-08-05 13:25:19 +02001179static void
1180ypr_import_xmlns(struct ypr_ctx *ctx, const struct lysp_module *modp, unsigned int indent)
1181{
1182 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001183
1184 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko5233e962020-08-14 14:26:20 +02001185 ly_print_(ctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, modp->imports[u].prefix, modp->imports[u].module->ns);
FredGand944bdc2019-11-05 21:57:07 +08001186 }
1187}
1188
1189struct ext_substmt_info_s stmt_attr_info[] = {
1190 {NULL, NULL, 0}, /**< LY_STMT_NONE*/
1191 {"status", "value", SUBST_FLAG_ID}, /**< LY_STMT_STATUS */
1192 {"config", "value", SUBST_FLAG_ID}, /**< LY_STMT_CONFIG */
1193 {"mandatory", "value", SUBST_FLAG_ID}, /**< LY_STMT_MANDATORY */
1194 {"units", "name", SUBST_FLAG_ID}, /**< LY_STMT_UNITS */
1195 {"default", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEFAULT */
1196 {"type", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPE */
1197 {"action", "name", SUBST_FLAG_ID}, /**< LY_STMT_ACTION */
1198 {"anydata", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYDATA */
1199 {"anyxml", "name", SUBST_FLAG_ID}, /**< LY_STMT_ANYXML */
1200 {"argument", "name", SUBST_FLAG_ID}, /**< LY_STMT_ARGUMENT */
1201 {"augment", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_AUGMENT */
1202 {"base", "name", SUBST_FLAG_ID}, /**< LY_STMT_BASE */
1203 {"belongs-to", "module", SUBST_FLAG_ID}, /**< LY_STMT_BELONGS_TO */
1204 {"bit", "name", SUBST_FLAG_ID}, /**< LY_STMT_BIT */
1205 {"case", "name", SUBST_FLAG_ID}, /**< LY_STMT_CASE */
1206 {"choice", "name", SUBST_FLAG_ID}, /**< LY_STMT_CHOICE */
1207 {"contact", "text", SUBST_FLAG_YIN},/**< LY_STMT_CONTACT */
1208 {"container", "name", SUBST_FLAG_ID}, /**< LY_STMT_CONTAINER */
1209 {"description", "text", SUBST_FLAG_YIN},/**< LY_STMT_DESCRIPTION */
1210 {"deviate", "value", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATE */
1211 {"deviation", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_DEVIATION */
1212 {"enum", "name", SUBST_FLAG_ID}, /**< LY_STMT_ENUM */
1213 {"error-app-tag", "value", SUBST_FLAG_ID}, /**< LY_STMT_ERROR_APP_TAG */
1214 {"error-message", "value", SUBST_FLAG_YIN},/**< LY_STMT_ERROR_MESSAGE */
1215 {"extension", "name", SUBST_FLAG_ID}, /**< LY_STMT_EXTENSION */
1216 {"feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_FEATURE */
1217 {"fraction-digits", "value", SUBST_FLAG_ID}, /**< LY_STMT_FRACTION_DIGITS */
1218 {"grouping", "name", SUBST_FLAG_ID}, /**< LY_STMT_GROUPING */
1219 {"identity", "name", SUBST_FLAG_ID}, /**< LY_STMT_IDENTITY */
1220 {"if-feature", "name", SUBST_FLAG_ID}, /**< LY_STMT_IF_FEATURE */
1221 {"import", "module", SUBST_FLAG_ID}, /**< LY_STMT_IMPORT */
1222 {"include", "module", SUBST_FLAG_ID}, /**< LY_STMT_INCLUDE */
1223 {"input", NULL, 0}, /**< LY_STMT_INPUT */
1224 {"key", "value", SUBST_FLAG_ID}, /**< LY_STMT_KEY */
1225 {"leaf", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF */
1226 {"leaf-list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LEAF_LIST */
1227 {"length", "value", SUBST_FLAG_ID}, /**< LY_STMT_LENGTH */
1228 {"list", "name", SUBST_FLAG_ID}, /**< LY_STMT_LIST */
1229 {"max-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MAX_ELEMENTS */
1230 {"min-elements", "value", SUBST_FLAG_ID}, /**< LY_STMT_MIN_ELEMENTS */
1231 {"modifier", "value", SUBST_FLAG_ID}, /**< LY_STMT_MODIFIER */
1232 {"module", "name", SUBST_FLAG_ID}, /**< LY_STMT_MODULE */
1233 {"must", "condition", SUBST_FLAG_ID}, /**< LY_STMT_MUST */
1234 {"namespace", "uri", SUBST_FLAG_ID}, /**< LY_STMT_NAMESPACE */
1235 {"notification", "name", SUBST_FLAG_ID}, /**< LY_STMT_NOTIFICATION */
1236 {"ordered-by", "value", SUBST_FLAG_ID}, /**< LY_STMT_ORDERED_BY */
1237 {"organization", "text", SUBST_FLAG_YIN},/**< LY_STMT_ORGANIZATION */
1238 {"output", NULL, 0}, /**< LY_STMT_OUTPUT */
1239 {"path", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATH */
1240 {"pattern", "value", SUBST_FLAG_ID}, /**< LY_STMT_PATTERN */
1241 {"position", "value", SUBST_FLAG_ID}, /**< LY_STMT_POSITION */
1242 {"prefix", "value", SUBST_FLAG_ID}, /**< LY_STMT_PREFIX */
1243 {"presence", "value", SUBST_FLAG_ID}, /**< LY_STMT_PRESENCE */
1244 {"range", "value", SUBST_FLAG_ID}, /**< LY_STMT_RANGE */
1245 {"reference", "text", SUBST_FLAG_YIN},/**< LY_STMT_REFERENCE */
1246 {"refine", "target-node", SUBST_FLAG_ID}, /**< LY_STMT_REFINE */
1247 {"require-instance", "value", SUBST_FLAG_ID}, /**< LY_STMT_REQUIRE_INSTANCE */
1248 {"revision", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION */
1249 {"revision-date", "date", SUBST_FLAG_ID}, /**< LY_STMT_REVISION_DATE */
1250 {"rpc", "name", SUBST_FLAG_ID}, /**< LY_STMT_RPC */
1251 {"submodule", "name", SUBST_FLAG_ID}, /**< LY_STMT_SUBMODULE */
1252 {"typedef", "name", SUBST_FLAG_ID}, /**< LY_STMT_TYPEDEF */
1253 {"unique", "tag", SUBST_FLAG_ID}, /**< LY_STMT_UNIQUE */
1254 {"uses", "name", SUBST_FLAG_ID}, /**< LY_STMT_USES */
1255 {"value", "value", SUBST_FLAG_ID}, /**< LY_STMT_VALUE */
1256 {"when", "condition", SUBST_FLAG_ID}, /**< LY_STMT_WHEN */
1257 {"yang-version", "value", SUBST_FLAG_ID}, /**< LY_STMT_YANG_VERSION */
1258 {"yin-element", "value", SUBST_FLAG_ID}, /**< LY_STMT_YIN_ELEMENT */
1259 {NULL, NULL, 0}, /**< LY_STMT_EXTENSION_INSTANCE */
1260 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_SEMICOLON */
1261 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_LEFT_BRACE */
1262 {NULL, NULL, 0}, /**< LY_STMT_SYNTAX_RIGHT_BRACE */
1263 {NULL, NULL, 0}, /**< LY_STMT_ARG_TEXT */
1264 {NULL, NULL, 0}, /**< LY_STMT_ARG_VALUE */
1265};
1266
1267static void
1268yprp_stmt(struct ypr_ctx *ctx, struct lysp_stmt *stmt)
1269{
1270 struct lysp_stmt *childstmt;
1271 int flag = stmt->child ? 1 : -1;
1272
1273 /* TODO:
1274 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
1275 cannot find the compiled information, so it is needed to be done,
1276 currently it is ignored */
Michal Vaskod989ba02020-08-24 10:59:24 +02001277 if (stmt_attr_info[stmt->kw].name) {
1278 if (stmt_attr_info[stmt->kw].flags & SUBST_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +08001279 ypr_open(ctx, stmt->stmt, NULL, NULL, flag);
1280 ypr_yin_arg(ctx, stmt_attr_info[stmt->kw].arg, stmt->arg);
Radek Krejci0f969882020-08-21 16:56:47 +02001281 } else {
FredGand944bdc2019-11-05 21:57:07 +08001282 ypr_open(ctx, stmt->stmt, stmt_attr_info[stmt->kw].arg, stmt->arg, flag);
1283 }
1284 }
1285
1286 if (stmt->child) {
1287 LEVEL++;
1288 LY_LIST_FOR(stmt->child, childstmt) {
1289 yprp_stmt(ctx, childstmt);
1290 }
1291 LEVEL--;
1292 ypr_close(ctx, stmt->stmt, flag);
1293 }
1294}
1295
1296/**
1297 * @param[in] count Number of extensions to print, 0 to print them all.
1298 */
1299static void
1300yprp_extension_instances(struct ypr_ctx *ctx, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci0f969882020-08-21 16:56:47 +02001301 struct lysp_ext_instance *ext, int *flag, LY_ARRAY_COUNT_TYPE count)
FredGand944bdc2019-11-05 21:57:07 +08001302{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001303 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001304 char *str;
1305 struct lysp_stmt *stmt;
1306 const char *argument;
1307 const char *ext_argument;
1308
1309 if (!count && ext) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001310 count = LY_ARRAY_COUNT(ext);
FredGand944bdc2019-11-05 21:57:07 +08001311 }
1312 LY_ARRAY_FOR(ext, u) {
1313 if (!count) {
1314 break;
1315 }
1316
1317 count--;
1318 if (ext->insubstmt != substmt || ext->insubstmt_index != substmt_index) {
1319 continue;
1320 }
1321
1322 if (!ext->compiled && ext->yin) {
Michal Vasko5233e962020-08-14 14:26:20 +02001323 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 +08001324 continue;
1325 }
1326
1327 ypr_close_parent(ctx, flag);
1328 int inner_flag = 0;
1329 argument = NULL;
1330 ext_argument = NULL;
1331
1332 if (ext[u].compiled) {
1333 argument = ext[u].compiled->argument;
1334 ext_argument = ext[u].compiled->def->argument;
1335 } else {
1336 argument = ext[u].argument;
1337 }
1338
1339 if (ext->yin) {
1340 ypr_open(ctx, ext[u].name, NULL, NULL, 1);
1341 if (asprintf(&str, "%s:%s", ext[u].compiled->def->module->prefix, ext_argument) == -1) {
1342 LOGMEM(ctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +08001343 return;
1344 }
1345 LEVEL++;
1346 inner_flag = 1;
1347 ypr_yin_arg(ctx, str, argument);
1348 free(str);
1349 str = NULL;
1350 LEVEL--;
1351 } else {
1352 ypr_open(ctx, ext[u].name, ext_argument, argument, inner_flag);
1353 }
1354
1355 LEVEL++;
1356 LY_LIST_FOR(ext[u].child, stmt) {
1357 ypr_close_parent(ctx, &inner_flag);
1358 yprp_stmt(ctx, stmt);
1359 }
1360 LEVEL--;
1361 ypr_close(ctx, ext[u].name, inner_flag);
1362 }
1363}
1364
Michal Vasko7c8439f2020-08-05 13:25:19 +02001365static void
1366yin_print_parsed_linkage(struct ypr_ctx *ctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001367{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001368 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001369
FredGand944bdc2019-11-05 21:57:07 +08001370 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001371 ypr_open(ctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001372 LEVEL++;
1373 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->imports[u].exts, NULL, 0);
1374 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
1375 if (modp->imports[u].rev[0]) {
1376 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->imports[u].rev, modp->imports[u].exts);
1377 }
1378 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1379 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
1380 LEVEL--;
1381 ypr_close(ctx, "import", 1);
1382 }
1383 LY_ARRAY_FOR(modp->includes, u) {
1384 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 +02001385 ypr_open(ctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001386 LEVEL++;
1387 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->includes[u].exts, NULL, 0);
1388 if (modp->includes[u].rev[0]) {
1389 ypr_substmt(ctx, LYEXT_SUBSTMT_REVISIONDATE, 0, modp->includes[u].rev, modp->includes[u].exts);
1390 }
1391 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1392 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
1393 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001394 ly_print_(ctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001395 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001396 ypr_open(ctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001397 }
1398 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001399}
FredGand944bdc2019-11-05 21:57:07 +08001400
Michal Vasko7c8439f2020-08-05 13:25:19 +02001401static void
1402yin_print_parsed_body(struct ypr_ctx *ctx, const struct lysp_module *modp)
1403{
1404 LY_ARRAY_COUNT_TYPE u;
1405 struct lysp_node *data;
FredGand944bdc2019-11-05 21:57:07 +08001406
FredGand944bdc2019-11-05 21:57:07 +08001407 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko5233e962020-08-14 14:26:20 +02001408 ly_print_(ctx->out, "\n");
FredGand944bdc2019-11-05 21:57:07 +08001409 yprp_extension(ctx, &modp->extensions[u]);
1410 }
1411 if (modp->exts) {
Michal Vasko5233e962020-08-14 14:26:20 +02001412 ly_print_(ctx->out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001413 yprp_extension_instances(ctx, LYEXT_SUBSTMT_SELF, 0, modp->exts, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +08001414 }
1415
1416 LY_ARRAY_FOR(modp->features, u) {
1417 yprp_feature(ctx, &modp->features[u]);
1418 }
1419
1420 LY_ARRAY_FOR(modp->identities, u) {
1421 yprp_identity(ctx, &modp->identities[u]);
1422 }
1423
1424 LY_ARRAY_FOR(modp->typedefs, u) {
1425 yprp_typedef(ctx, &modp->typedefs[u]);
1426 }
1427
1428 LY_ARRAY_FOR(modp->groupings, u) {
1429 yprp_grouping(ctx, &modp->groupings[u]);
1430 }
1431
1432 LY_LIST_FOR(modp->data, data) {
1433 yprp_node(ctx, data);
1434 }
1435
1436 LY_ARRAY_FOR(modp->augments, u) {
1437 yprp_augment(ctx, &modp->augments[u]);
1438 }
1439
1440 LY_ARRAY_FOR(modp->rpcs, u) {
1441 yprp_action(ctx, &modp->rpcs[u]);
1442 }
1443
1444 LY_ARRAY_FOR(modp->notifs, u) {
1445 yprp_notification(ctx, &modp->notifs[u]);
1446 }
1447
1448 LY_ARRAY_FOR(modp->deviations, u) {
1449 yprp_deviation(ctx, &modp->deviations[u]);
1450 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001451}
1452
1453LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02001454yin_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, int options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001455{
1456 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02001457 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001458
Michal Vasko5233e962020-08-14 14:26:20 +02001459 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1460 ly_print_(ctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001461 ypr_xmlns(ctx, module, 8);
1462 ypr_import_xmlns(ctx, modp, 8);
Michal Vasko5233e962020-08-14 14:26:20 +02001463 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001464
1465 LEVEL++;
1466
1467 /* module-header-stmts */
1468 if (module->version) {
1469 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, module->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
1470 }
1471 ypr_substmt(ctx, LYEXT_SUBSTMT_NAMESPACE, 0, module->ns, modp->exts);
1472 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, module->prefix, modp->exts);
1473
1474 /* linkage-stmts (import/include) */
1475 yin_print_parsed_linkage(ctx, modp);
1476
1477 /* meta-stmts */
1478 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001479 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001480 }
1481 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, module->org, modp->exts);
1482 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, module->contact, modp->exts);
1483 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, module->dsc, modp->exts);
1484 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, module->ref, modp->exts);
1485
1486 /* revision-stmts */
1487 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001488 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001489 }
1490 LY_ARRAY_FOR(modp->revs, u) {
1491 yprp_revision(ctx, &modp->revs[u]);
1492 }
1493
1494 /* body-stmts */
1495 yin_print_parsed_body(ctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001496
1497 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001498 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001499 ly_print_flush(out);
1500
1501 return LY_SUCCESS;
1502}
1503
Michal Vasko7c8439f2020-08-05 13:25:19 +02001504static void
1505yprp_belongsto(struct ypr_ctx *ctx, const struct lysp_submodule *submodp)
1506{
1507 ypr_open(ctx, "belongs-to", "module", submodp->belongsto, 1);
1508 LEVEL++;
1509 yprp_extension_instances(ctx, LYEXT_SUBSTMT_BELONGSTO, 0, submodp->exts, NULL, 0);
1510 ypr_substmt(ctx, LYEXT_SUBSTMT_PREFIX, 0, submodp->prefix, submodp->exts);
1511 LEVEL--;
1512 ypr_close(ctx, "belongs-to", 1);
1513}
1514
1515LY_ERR
Radek Krejci5536d282020-08-04 23:27:44 +02001516yin_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, int options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001517{
1518 LY_ARRAY_COUNT_TYPE u;
Radek Krejci5536d282020-08-04 23:27:44 +02001519 struct ypr_ctx ctx_ = {.out = out, .level = 0, .module = module, .options = options | LYD_PRINT_FORMAT}, *ctx = &ctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001520
Michal Vasko5233e962020-08-14 14:26:20 +02001521 ly_print_(ctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1522 ly_print_(ctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001523 ypr_xmlns(ctx, module, 8);
1524 ypr_import_xmlns(ctx, (struct lysp_module *)submodp, 8);
Michal Vasko5233e962020-08-14 14:26:20 +02001525 ly_print_(ctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001526
1527 LEVEL++;
1528
1529 /* submodule-header-stmts */
1530 if (submodp->version) {
1531 ypr_substmt(ctx, LYEXT_SUBSTMT_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
1532 }
1533 yprp_belongsto(ctx, submodp);
1534
1535 /* linkage-stmts (import/include) */
1536 yin_print_parsed_linkage(ctx, (struct lysp_module *)submodp);
1537
1538 /* meta-stmts */
1539 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001540 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001541 }
1542 ypr_substmt(ctx, LYEXT_SUBSTMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1543 ypr_substmt(ctx, LYEXT_SUBSTMT_CONTACT, 0, submodp->contact, submodp->exts);
1544 ypr_substmt(ctx, LYEXT_SUBSTMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1545 ypr_substmt(ctx, LYEXT_SUBSTMT_REFERENCE, 0, submodp->ref, submodp->exts);
1546
1547 /* revision-stmts */
1548 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001549 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001550 }
1551 LY_ARRAY_FOR(submodp->revs, u) {
1552 yprp_revision(ctx, &submodp->revs[u]);
1553 }
1554
1555 /* body-stmts */
1556 yin_print_parsed_body(ctx, (struct lysp_module *)submodp);
1557
1558 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001559 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001560 ly_print_flush(out);
1561
1562 return LY_SUCCESS;
1563}