diff options
Diffstat (limited to '')
-rw-r--r-- | src/printer_yang.c | 2657 |
1 files changed, 2657 insertions, 0 deletions
diff --git a/src/printer_yang.c b/src/printer_yang.c new file mode 100644 index 0000000..ea643ac --- /dev/null +++ b/src/printer_yang.c @@ -0,0 +1,2657 @@ +/** + * @file printer_yang.c + * @author Radek Krejci <rkrejci@cesnet.cz> + * @author Michal Vasko <mvasko@cesnet.cz> + * @brief YANG printer + * + * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. + * + * This source code is licensed under BSD 3-Clause License (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + */ + +#define _GNU_SOURCE + +#include <assert.h> +#include <inttypes.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> + +#include "common.h" +#include "compat.h" +#include "log.h" +#include "out.h" +#include "out_internal.h" +#include "plugins_exts.h" +#include "plugins_types.h" +#include "printer_internal.h" +#include "printer_schema.h" +#include "tree.h" +#include "tree_data.h" +#include "tree_schema.h" +#include "tree_schema_internal.h" +#include "xpath.h" + +/** + * @brief Types of the YANG printers + */ +enum lys_ypr_schema_type { + LYS_YPR_PARSED, /**< YANG printer of the parsed schema */ + LYS_YPR_COMPILED /**< YANG printer of the compiled schema */ +}; + +#define YPR_CTX_FLAG_EXTRA_LINE 0x01 /**< Flag for ::ypr_ctx::flags to print extra line in schema */ + +#define YPR_EXTRA_LINE(COND, PCTX) if (COND) { (PCTX)->flags |= YPR_CTX_FLAG_EXTRA_LINE; } +#define YPR_EXTRA_LINE_PRINT(PCTX) \ + if ((PCTX)->flags & YPR_CTX_FLAG_EXTRA_LINE) { \ + (PCTX)->flags &= ~YPR_CTX_FLAG_EXTRA_LINE; \ + if (DO_FORMAT) { \ + ly_print_((PCTX)->out, "\n"); \ + } \ + } + +/** + * @brief Compiled YANG printer context + * + * Note that the YANG extensions API provides getter to the members for the extension plugins. + */ +struct lys_ypr_ctx { + union { + struct { + struct ly_out *out; /**< output specification */ + uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ + uint16_t flags; /**< internal flags for use by printer */ + uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */ + const struct lys_module *module; /**< schema to print */ + }; + struct lyspr_ctx printer_ctx; + }; + + /* YANG printer specific members */ + enum lys_ypr_schema_type schema; /**< type of the schema to print */ +}; + +/** + * @brief Print the given text as content of a double quoted YANG string, + * including encoding characters that have special meanings. The quotation marks + * are not printed. + * + * Follows RFC 7950, section 6.1.3. + * + * @param[in] out Output specification. + * @param[in] text String to be printed. + * @param[in] len Length of the string from @p text to be printed. In case of -1, + * the @p text is printed completely as a NULL-terminated string. + */ +static void +ypr_encode(struct ly_out *out, const char *text, ssize_t len) +{ + size_t i, start_len; + const char *start; + char special = 0; + + if (!len) { + return; + } + + if (len < 0) { + len = strlen(text); + } + + start = text; + start_len = 0; + for (i = 0; i < (size_t)len; ++i) { + switch (text[i]) { + case '\n': + case '\t': + case '\"': + case '\\': + special = text[i]; + break; + default: + ++start_len; + break; + } + + if (special) { + ly_write_(out, start, start_len); + switch (special) { + case '\n': + ly_write_(out, "\\n", 2); + break; + case '\t': + ly_write_(out, "\\t", 2); + break; + case '\"': + ly_write_(out, "\\\"", 2); + break; + case '\\': + ly_write_(out, "\\\\", 2); + break; + } + + start += start_len + 1; + start_len = 0; + + special = 0; + } + } + + ly_write_(out, start, start_len); +} + +static void +ypr_open(struct ly_out *out, ly_bool *flag) +{ + if (flag && !*flag) { + *flag = 1; + ly_print_(out, " {\n"); + } +} + +static void +ypr_close(struct lys_ypr_ctx *pctx, ly_bool flag) +{ + if (flag) { + ly_print_(pctx->out, "%*s}\n", INDENT); + } else { + ly_print_(pctx->out, ";\n"); + } +} + +static void +ypr_text(struct lys_ypr_ctx *pctx, const char *name, const char *text, ly_bool singleline, ly_bool closed) +{ + const char *s, *t; + + if (singleline) { + ly_print_(pctx->out, "%*s%s \"", INDENT, name); + } else { + ly_print_(pctx->out, "%*s%s\n", INDENT, name); + LEVEL++; + + ly_print_(pctx->out, "%*s\"", INDENT); + } + t = text; + while ((s = strchr(t, '\n'))) { + ypr_encode(pctx->out, t, s - t); + ly_print_(pctx->out, "\n"); + t = s + 1; + if (*t != '\n') { + ly_print_(pctx->out, "%*s ", INDENT); + } + } + + ypr_encode(pctx->out, t, strlen(t)); + if (closed) { + ly_print_(pctx->out, "\";\n"); + } else { + ly_print_(pctx->out, "\""); + } + if (!singleline) { + LEVEL--; + } +} + +static void +yprp_stmt(struct lys_ypr_ctx *pctx, struct lysp_stmt *stmt) +{ + struct lysp_stmt *childstmt; + const char *s, *t; + + if (stmt->arg) { + if (stmt->flags) { + ly_print_(pctx->out, "%*s%s\n", INDENT, stmt->stmt); + LEVEL++; + ly_print_(pctx->out, "%*s%c", INDENT, (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\''); + t = stmt->arg; + while ((s = strchr(t, '\n'))) { + ypr_encode(pctx->out, t, s - t); + ly_print_(pctx->out, "\n"); + t = s + 1; + if (*t != '\n') { + ly_print_(pctx->out, "%*s ", INDENT); + } + } + LEVEL--; + ypr_encode(pctx->out, t, strlen(t)); + ly_print_(pctx->out, "%c%s", (stmt->flags & LYS_DOUBLEQUOTED) ? '\"' : '\'', stmt->child ? " {\n" : ";\n"); + } else { + ly_print_(pctx->out, "%*s%s %s%s", INDENT, stmt->stmt, stmt->arg, stmt->child ? " {\n" : ";\n"); + } + } else { + ly_print_(pctx->out, "%*s%s%s", INDENT, stmt->stmt, stmt->child ? " {\n" : ";\n"); + } + + if (stmt->child) { + LEVEL++; + LY_LIST_FOR(stmt->child, childstmt) { + yprp_stmt(pctx, childstmt); + } + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); + } +} + +static void +yprp_extension_instance(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, + struct lysp_ext_instance *ext, ly_bool *flag) +{ + struct lysp_stmt *stmt; + ly_bool child_presence; + + if ((ext->flags & LYS_INTERNAL) || (ext->parent_stmt != substmt) || (ext->parent_stmt_index != substmt_index)) { + return; + } + + ypr_open(pctx->out, flag); + + if (ext->def->argname) { + ly_print_(pctx->out, "%*s%s \"", INDENT, ext->name); + ypr_encode(pctx->out, ext->argument, -1); + ly_print_(pctx->out, "\""); + } else { + ly_print_(pctx->out, "%*s%s", INDENT, ext->name); + } + + child_presence = 0; + LEVEL++; + LY_LIST_FOR(ext->child, stmt) { + if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { + continue; + } + if (!child_presence) { + ly_print_(pctx->out, " {\n"); + child_presence = 1; + } + yprp_stmt(pctx, stmt); + } + LEVEL--; + if (child_presence) { + ly_print_(pctx->out, "%*s}\n", INDENT); + } else { + ly_print_(pctx->out, ";\n"); + } +} + +static void +yprp_extension_instances(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, + struct lysp_ext_instance *exts, ly_bool *flag) +{ + LY_ARRAY_COUNT_TYPE u; + + LY_ARRAY_FOR(exts, u) { + yprp_extension_instance(pctx, substmt, substmt_index, &exts[u], flag); + } +} + +static void +yprc_extension_instances(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, + struct lysc_ext_instance *exts, ly_bool *flag) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool inner_flag; + + LY_ARRAY_FOR(exts, u) { + if ((exts[u].parent_stmt != substmt) || (exts[u].parent_stmt_index != substmt_index)) { + return; + } + + ypr_open(pctx->out, flag); + if (exts[u].argument) { + ly_print_(pctx->out, "%*s%s:%s \"", INDENT, exts[u].def->module->name, exts[u].def->name); + ypr_encode(pctx->out, exts[u].argument, -1); + ly_print_(pctx->out, "\""); + } else { + ly_print_(pctx->out, "%*s%s:%s", INDENT, exts[u].def->module->name, exts[u].def->name); + } + + LEVEL++; + inner_flag = 0; + yprc_extension_instances(pctx, LY_STMT_EXTENSION_INSTANCE, 0, exts[u].exts, &inner_flag); + + if (exts[u].def->plugin && exts[u].def->plugin->printer_info) { + exts[u].def->plugin->printer_info(&pctx->printer_ctx, &exts[u], &inner_flag); + } + + LEVEL--; + ypr_close(pctx, inner_flag); + } +} + +static void +ypr_substmt(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, const char *text, void *exts) +{ + ly_bool extflag = 0; + + if (!text) { + /* nothing to print */ + return; + } + + if (lys_stmt_flags(substmt) & LY_STMT_FLAG_ID) { + ly_print_(pctx->out, "%*s%s %s", INDENT, lys_stmt_str(substmt), text); + } else { + ypr_text(pctx, lys_stmt_str(substmt), text, (lys_stmt_flags(substmt) & LY_STMT_FLAG_YIN) ? 0 : 1, 0); + } + + LEVEL++; + if (pctx->schema == LYS_YPR_PARSED) { + yprp_extension_instances(pctx, substmt, substmt_index, exts, &extflag); + } else { + yprc_extension_instances(pctx, substmt, substmt_index, exts, &extflag); + } + LEVEL--; + ypr_close(pctx, extflag); +} + +static void +ypr_unsigned(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, + unsigned long attr_value, ly_bool *flag) +{ + char *str; + + if (asprintf(&str, "%lu", attr_value) == -1) { + LOGMEM(pctx->module->ctx); + return; + } + ypr_open(pctx->out, flag); + ypr_substmt(pctx, substmt, substmt_index, str, exts); + free(str); +} + +static void +ypr_signed(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, long attr_value, + ly_bool *flag) +{ + char *str; + + if (asprintf(&str, "%ld", attr_value) == -1) { + LOGMEM(pctx->module->ctx); + return; + } + ypr_open(pctx->out, flag); + ypr_substmt(pctx, substmt, substmt_index, str, exts); + free(str); +} + +static void +yprp_revision(struct lys_ypr_ctx *pctx, const struct lysp_revision *rev) +{ + if (rev->dsc || rev->ref || rev->exts) { + ly_print_(pctx->out, "%*srevision %s {\n", INDENT, rev->date); + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_REVISION, 0, rev->exts, NULL); + ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, rev->dsc, rev->exts); + ypr_substmt(pctx, LY_STMT_REFERENCE, 0, rev->ref, rev->exts); + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); + } else { + ly_print_(pctx->out, "%*srevision %s;\n", INDENT, rev->date); + } +} + +static void +ypr_mandatory(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, ly_bool *flag) +{ + if (flags & LYS_MAND_MASK) { + ypr_open(pctx->out, flag); + ypr_substmt(pctx, LY_STMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts); + } +} + +static void +ypr_config(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, ly_bool *flag) +{ + if (flags & LYS_CONFIG_MASK) { + ypr_open(pctx->out, flag); + ypr_substmt(pctx, LY_STMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts); + } +} + +static void +ypr_status(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, ly_bool *flag) +{ + const char *status = NULL; + + if (flags & LYS_STATUS_CURR) { + ypr_open(pctx->out, flag); + status = "current"; + } else if (flags & LYS_STATUS_DEPRC) { + ypr_open(pctx->out, flag); + status = "deprecated"; + } else if (flags & LYS_STATUS_OBSLT) { + ypr_open(pctx->out, flag); + status = "obsolete"; + } + + ypr_substmt(pctx, LY_STMT_STATUS, 0, status, exts); +} + +static void +ypr_description(struct lys_ypr_ctx *pctx, const char *dsc, void *exts, ly_bool *flag) +{ + if (dsc) { + ypr_open(pctx->out, flag); + ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, dsc, exts); + } +} + +static void +ypr_reference(struct lys_ypr_ctx *pctx, const char *ref, void *exts, ly_bool *flag) +{ + if (ref) { + ypr_open(pctx->out, flag); + ypr_substmt(pctx, LY_STMT_REFERENCE, 0, ref, exts); + } +} + +static void +yprp_iffeatures(struct lys_ypr_ctx *pctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, ly_bool *flag) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool extflag; + + LY_ARRAY_FOR(iffs, u) { + ypr_open(pctx->out, flag); + extflag = 0; + + ly_print_(pctx->out, "%*sif-feature \"%s\"", INDENT, iffs[u].str); + + /* extensions */ + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_IF_FEATURE, u, exts, &extflag); + LEVEL--; + ypr_close(pctx, extflag); + } +} + +static void +yprp_extension(struct lys_ypr_ctx *pctx, const struct lysp_ext *ext) +{ + ly_bool flag = 0, flag2 = 0; + LY_ARRAY_COUNT_TYPE u; + + ly_print_(pctx->out, "%*sextension %s", INDENT, ext->name); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_EXTENSION, 0, ext->exts, &flag); + + if (ext->argname) { + ypr_open(pctx->out, &flag); + ly_print_(pctx->out, "%*sargument %s", INDENT, ext->argname); + LEVEL++; + if (ext->exts) { + u = -1; + while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LY_STMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) { + yprp_extension_instance(pctx, LY_STMT_ARGUMENT, 0, &ext->exts[u], &flag2); + } + } + if ((ext->flags & LYS_YINELEM_MASK) || + (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LY_STMT_YIN_ELEMENT) != LY_ARRAY_COUNT(ext->exts)))) { + ypr_open(pctx->out, &flag2); + ypr_substmt(pctx, LY_STMT_YIN_ELEMENT, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts); + } + LEVEL--; + ypr_close(pctx, flag2); + } + + ypr_status(pctx, ext->flags, ext->exts, &flag); + ypr_description(pctx, ext->dsc, ext->exts, &flag); + ypr_reference(pctx, ext->ref, ext->exts, &flag); + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_feature(struct lys_ypr_ctx *pctx, const struct lysp_feature *feat) +{ + ly_bool flag = 0; + + ly_print_(pctx->out, "%*sfeature %s", INDENT, feat->name); + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_FEATURE, 0, feat->exts, &flag); + yprp_iffeatures(pctx, feat->iffeatures, feat->exts, &flag); + ypr_status(pctx, feat->flags, feat->exts, &flag); + ypr_description(pctx, feat->dsc, feat->exts, &flag); + ypr_reference(pctx, feat->ref, feat->exts, &flag); + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_identity(struct lys_ypr_ctx *pctx, const struct lysp_ident *ident) +{ + ly_bool flag = 0; + LY_ARRAY_COUNT_TYPE u; + + ly_print_(pctx->out, "%*sidentity %s", INDENT, ident->name); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_IDENTITY, 0, ident->exts, &flag); + yprp_iffeatures(pctx, ident->iffeatures, ident->exts, &flag); + + LY_ARRAY_FOR(ident->bases, u) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_BASE, u, ident->bases[u], ident->exts); + } + + ypr_status(pctx, ident->flags, ident->exts, &flag); + ypr_description(pctx, ident->dsc, ident->exts, &flag); + ypr_reference(pctx, ident->ref, ident->exts, &flag); + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_identity(struct lys_ypr_ctx *pctx, const struct lysc_ident *ident) +{ + ly_bool flag = 0; + LY_ARRAY_COUNT_TYPE u; + + ly_print_(pctx->out, "%*sidentity %s", INDENT, ident->name); + LEVEL++; + + yprc_extension_instances(pctx, LY_STMT_IDENTITY, 0, ident->exts, &flag); + + ypr_open(pctx->out, &flag); + if (lys_identity_iffeature_value(ident) == LY_ENOT) { + ly_print_(pctx->out, "%*s/* identity \"%s\" is disabled by if-feature(s) */\n", INDENT, ident->name); + } + + LY_ARRAY_FOR(ident->derived, u) { + if (pctx->module != ident->derived[u]->module) { + ly_print_(pctx->out, "%*sderived %s:%s;\n", INDENT, ident->derived[u]->module->prefix, ident->derived[u]->name); + } else { + ly_print_(pctx->out, "%*sderived %s;\n", INDENT, ident->derived[u]->name); + } + } + + ypr_status(pctx, ident->flags, ident->exts, &flag); + ypr_description(pctx, ident->dsc, ident->exts, &flag); + ypr_reference(pctx, ident->ref, ident->exts, &flag); + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_restr(struct lys_ypr_ctx *pctx, const struct lysp_restr *restr, enum ly_stmt stmt, ly_bool *flag) +{ + ly_bool inner_flag = 0, singleline; + const char *text; + + if (!restr) { + return; + } + + ypr_open(pctx->out, flag); + text = ((restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK) && (restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK)) ? + restr->arg.str : restr->arg.str + 1; + singleline = strchr(text, '\n') ? 0 : 1; + ypr_text(pctx, lyplg_ext_stmt2str(stmt), text, singleline, 0); + + LEVEL++; + yprp_extension_instances(pctx, stmt, 0, restr->exts, &inner_flag); + if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) { + /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */ + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_MODIFIER, 0, "invert-match", restr->exts); + } + if (restr->emsg) { + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_ERROR_MESSAGE, 0, restr->emsg, restr->exts); + } + if (restr->eapptag) { + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_ERROR_APP_TAG, 0, restr->eapptag, restr->exts); + } + ypr_description(pctx, restr->dsc, restr->exts, &inner_flag); + ypr_reference(pctx, restr->ref, restr->exts, &inner_flag); + + LEVEL--; + ypr_close(pctx, inner_flag); +} + +static void +yprc_must(struct lys_ypr_ctx *pctx, const struct lysc_must *must, ly_bool *flag) +{ + ly_bool inner_flag = 0; + + ypr_open(pctx->out, flag); + ly_print_(pctx->out, "%*smust \"", INDENT); + ypr_encode(pctx->out, must->cond->expr, -1); + ly_print_(pctx->out, "\""); + + LEVEL++; + yprc_extension_instances(pctx, LY_STMT_MUST, 0, must->exts, &inner_flag); + if (must->emsg) { + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_ERROR_MESSAGE, 0, must->emsg, must->exts); + } + if (must->eapptag) { + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_ERROR_APP_TAG, 0, must->eapptag, must->exts); + } + ypr_description(pctx, must->dsc, must->exts, &inner_flag); + ypr_reference(pctx, must->ref, must->exts, &inner_flag); + + LEVEL--; + ypr_close(pctx, inner_flag); +} + +static void +yprc_range(struct lys_ypr_ctx *pctx, const struct lysc_range *range, LY_DATA_TYPE basetype, ly_bool *flag) +{ + ly_bool inner_flag = 0; + LY_ARRAY_COUNT_TYPE u; + + if (!range) { + return; + } + + ypr_open(pctx->out, flag); + ly_print_(pctx->out, "%*s%s \"", INDENT, (basetype == LY_TYPE_STRING || basetype == LY_TYPE_BINARY) ? "length" : "range"); + LY_ARRAY_FOR(range->parts, u) { + if (u > 0) { + ly_print_(pctx->out, " | "); + } + if (range->parts[u].max_64 == range->parts[u].min_64) { + if (basetype <= LY_TYPE_STRING) { /* unsigned values */ + ly_print_(pctx->out, "%" PRIu64, range->parts[u].max_u64); + } else { /* signed values */ + ly_print_(pctx->out, "%" PRId64, range->parts[u].max_64); + } + } else { + if (basetype <= LY_TYPE_STRING) { /* unsigned values */ + ly_print_(pctx->out, "%" PRIu64 "..%" PRIu64, range->parts[u].min_u64, range->parts[u].max_u64); + } else { /* signed values */ + ly_print_(pctx->out, "%" PRId64 "..%" PRId64, range->parts[u].min_64, range->parts[u].max_64); + } + } + } + ly_print_(pctx->out, "\""); + + LEVEL++; + yprc_extension_instances(pctx, LY_STMT_RANGE, 0, range->exts, &inner_flag); + if (range->emsg) { + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_ERROR_MESSAGE, 0, range->emsg, range->exts); + } + if (range->eapptag) { + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_ERROR_APP_TAG, 0, range->eapptag, range->exts); + } + ypr_description(pctx, range->dsc, range->exts, &inner_flag); + ypr_reference(pctx, range->ref, range->exts, &inner_flag); + + LEVEL--; + ypr_close(pctx, inner_flag); +} + +static void +yprc_pattern(struct lys_ypr_ctx *pctx, const struct lysc_pattern *pattern, ly_bool *flag) +{ + ly_bool inner_flag = 0; + + ypr_open(pctx->out, flag); + ly_print_(pctx->out, "%*spattern \"", INDENT); + ypr_encode(pctx->out, pattern->expr, -1); + ly_print_(pctx->out, "\""); + + LEVEL++; + yprc_extension_instances(pctx, LY_STMT_PATTERN, 0, pattern->exts, &inner_flag); + if (pattern->inverted) { + /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */ + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_MODIFIER, 0, "invert-match", pattern->exts); + } + if (pattern->emsg) { + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_ERROR_MESSAGE, 0, pattern->emsg, pattern->exts); + } + if (pattern->eapptag) { + ypr_open(pctx->out, &inner_flag); + ypr_substmt(pctx, LY_STMT_ERROR_APP_TAG, 0, pattern->eapptag, pattern->exts); + } + ypr_description(pctx, pattern->dsc, pattern->exts, &inner_flag); + ypr_reference(pctx, pattern->ref, pattern->exts, &inner_flag); + + LEVEL--; + ypr_close(pctx, inner_flag); +} + +static void +yprc_bits_enum(struct lys_ypr_ctx *pctx, const struct lysc_type_bitenum_item *items, LY_DATA_TYPE basetype, ly_bool *flag) +{ + LY_ARRAY_COUNT_TYPE u; + const struct lysc_type_bitenum_item *item; + ly_bool inner_flag; + + assert((basetype == LY_TYPE_BITS) || (basetype == LY_TYPE_ENUM)); + + LY_ARRAY_FOR(items, u) { + item = &items[u]; + inner_flag = 0; + + ypr_open(pctx->out, flag); + ly_print_(pctx->out, "%*s%s \"", INDENT, basetype == LY_TYPE_BITS ? "bit" : "enum"); + ypr_encode(pctx->out, item->name, -1); + ly_print_(pctx->out, "\""); + LEVEL++; + if (basetype == LY_TYPE_BITS) { + yprc_extension_instances(pctx, LY_STMT_BIT, 0, item->exts, &inner_flag); + ypr_unsigned(pctx, LY_STMT_POSITION, 0, item->exts, item->position, &inner_flag); + } else { /* LY_TYPE_ENUM */ + yprc_extension_instances(pctx, LY_STMT_ENUM, 0, item->exts, &inner_flag); + ypr_signed(pctx, LY_STMT_VALUE, 0, item->exts, item->value, &inner_flag); + } + ypr_status(pctx, item->flags, item->exts, &inner_flag); + ypr_description(pctx, item->dsc, item->exts, &inner_flag); + ypr_reference(pctx, item->ref, item->exts, &inner_flag); + LEVEL--; + ypr_close(pctx, inner_flag); + } +} + +static void +yprp_when(struct lys_ypr_ctx *pctx, const struct lysp_when *when, ly_bool *flag) +{ + ly_bool inner_flag = 0; + + if (!when) { + return; + } + ypr_open(pctx->out, flag); + + ly_print_(pctx->out, "%*swhen \"", INDENT); + ypr_encode(pctx->out, when->cond, -1); + ly_print_(pctx->out, "\""); + + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_WHEN, 0, when->exts, &inner_flag); + ypr_description(pctx, when->dsc, when->exts, &inner_flag); + ypr_reference(pctx, when->ref, when->exts, &inner_flag); + LEVEL--; + ypr_close(pctx, inner_flag); +} + +static void +yprc_when(struct lys_ypr_ctx *pctx, const struct lysc_when *when, ly_bool *flag) +{ + ly_bool inner_flag = 0; + + if (!when) { + return; + } + ypr_open(pctx->out, flag); + + ly_print_(pctx->out, "%*swhen \"", INDENT); + ypr_encode(pctx->out, when->cond->expr, -1); + ly_print_(pctx->out, "\""); + + LEVEL++; + yprc_extension_instances(pctx, LY_STMT_WHEN, 0, when->exts, &inner_flag); + ypr_description(pctx, when->dsc, when->exts, &inner_flag); + ypr_reference(pctx, when->ref, when->exts, &inner_flag); + LEVEL--; + ypr_close(pctx, inner_flag); +} + +static void +yprp_bits_enum(struct lys_ypr_ctx *pctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, ly_bool *flag) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool inner_flag; + + LY_ARRAY_FOR(items, u) { + ypr_open(pctx->out, flag); + if (type == LY_TYPE_BITS) { + ly_print_(pctx->out, "%*sbit %s", INDENT, items[u].name); + } else { /* LY_TYPE_ENUM */ + ly_print_(pctx->out, "%*senum \"", INDENT); + ypr_encode(pctx->out, items[u].name, -1); + ly_print_(pctx->out, "\""); + } + inner_flag = 0; + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_ENUM, 0, items[u].exts, &inner_flag); + yprp_iffeatures(pctx, items[u].iffeatures, items[u].exts, &inner_flag); + if (items[u].flags & LYS_SET_VALUE) { + if (type == LY_TYPE_BITS) { + ypr_unsigned(pctx, LY_STMT_POSITION, 0, items[u].exts, items[u].value, &inner_flag); + } else { /* LY_TYPE_ENUM */ + ypr_signed(pctx, LY_STMT_VALUE, 0, items[u].exts, items[u].value, &inner_flag); + } + } + ypr_status(pctx, items[u].flags, items[u].exts, &inner_flag); + ypr_description(pctx, items[u].dsc, items[u].exts, &inner_flag); + ypr_reference(pctx, items[u].ref, items[u].exts, &inner_flag); + LEVEL--; + ypr_close(pctx, inner_flag); + } +} + +static void +yprp_type(struct lys_ypr_ctx *pctx, const struct lysp_type *type) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + + ly_print_(pctx->out, "%*stype %s", INDENT, type->name); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_TYPE, 0, type->exts, &flag); + + yprp_restr(pctx, type->range, LY_STMT_RANGE, &flag); + yprp_restr(pctx, type->length, LY_STMT_LENGTH, &flag); + LY_ARRAY_FOR(type->patterns, u) { + yprp_restr(pctx, &type->patterns[u], LY_STMT_PATTERN, &flag); + } + yprp_bits_enum(pctx, type->bits, LY_TYPE_BITS, &flag); + yprp_bits_enum(pctx, type->enums, LY_TYPE_ENUM, &flag); + + if (type->path) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_PATH, 0, type->path->expr, type->exts); + } + if (type->flags & LYS_SET_REQINST) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_REQUIRE_INSTANCE, 0, type->require_instance ? "true" : "false", type->exts); + } + if (type->flags & LYS_SET_FRDIGITS) { + ypr_unsigned(pctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, type->fraction_digits, &flag); + } + LY_ARRAY_FOR(type->bases, u) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_BASE, u, type->bases[u], type->exts); + } + LY_ARRAY_FOR(type->types, u) { + ypr_open(pctx->out, &flag); + yprp_type(pctx, &type->types[u]); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_dflt_value(struct lys_ypr_ctx *pctx, const struct ly_ctx *ly_pctx, const struct lyd_value *value, + struct lysc_ext_instance *exts) +{ + ly_bool dynamic; + const char *str; + + str = value->realtype->plugin->print(ly_pctx, value, LY_VALUE_JSON, NULL, &dynamic, NULL); + ypr_substmt(pctx, LY_STMT_DEFAULT, 0, str, exts); + if (dynamic) { + free((void *)str); + } +} + +static void +yprc_type(struct lys_ypr_ctx *pctx, const struct lysc_type *type) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + + ly_print_(pctx->out, "%*stype %s", INDENT, lys_datatype2str(type->basetype)); + LEVEL++; + + yprc_extension_instances(pctx, LY_STMT_TYPE, 0, type->exts, &flag); + + switch (type->basetype) { + case LY_TYPE_BINARY: { + struct lysc_type_bin *bin = (struct lysc_type_bin *)type; + + yprc_range(pctx, bin->length, type->basetype, &flag); + break; + } + case LY_TYPE_UINT8: + case LY_TYPE_UINT16: + case LY_TYPE_UINT32: + case LY_TYPE_UINT64: + case LY_TYPE_INT8: + case LY_TYPE_INT16: + case LY_TYPE_INT32: + case LY_TYPE_INT64: { + struct lysc_type_num *num = (struct lysc_type_num *)type; + + yprc_range(pctx, num->range, type->basetype, &flag); + break; + } + case LY_TYPE_STRING: { + struct lysc_type_str *str = (struct lysc_type_str *)type; + + yprc_range(pctx, str->length, type->basetype, &flag); + LY_ARRAY_FOR(str->patterns, u) { + yprc_pattern(pctx, str->patterns[u], &flag); + } + break; + } + case LY_TYPE_BITS: + case LY_TYPE_ENUM: { + /* bits and enums structures are compatible */ + struct lysc_type_bits *bits = (struct lysc_type_bits *)type; + + yprc_bits_enum(pctx, bits->bits, type->basetype, &flag); + break; + } + case LY_TYPE_BOOL: + case LY_TYPE_EMPTY: + /* nothing to do */ + break; + case LY_TYPE_DEC64: { + struct lysc_type_dec *dec = (struct lysc_type_dec *)type; + + ypr_open(pctx->out, &flag); + ypr_unsigned(pctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, dec->fraction_digits, &flag); + yprc_range(pctx, dec->range, dec->basetype, &flag); + break; + } + case LY_TYPE_IDENT: { + struct lysc_type_identityref *ident = (struct lysc_type_identityref *)type; + + LY_ARRAY_FOR(ident->bases, u) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_BASE, u, ident->bases[u]->name, type->exts); + } + break; + } + case LY_TYPE_INST: { + struct lysc_type_instanceid *inst = (struct lysc_type_instanceid *)type; + + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_REQUIRE_INSTANCE, 0, inst->require_instance ? "true" : "false", inst->exts); + break; + } + case LY_TYPE_LEAFREF: { + struct lysc_type_leafref *lr = (struct lysc_type_leafref *)type; + + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_PATH, 0, lr->path->expr, lr->exts); + ypr_substmt(pctx, LY_STMT_REQUIRE_INSTANCE, 0, lr->require_instance ? "true" : "false", lr->exts); + yprc_type(pctx, lr->realtype); + break; + } + case LY_TYPE_UNION: { + struct lysc_type_union *un = (struct lysc_type_union *)type; + + LY_ARRAY_FOR(un->types, u) { + ypr_open(pctx->out, &flag); + yprc_type(pctx, un->types[u]); + } + break; + } + default: + LOGINT(pctx->module->ctx); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_typedef(struct lys_ypr_ctx *pctx, const struct lysp_tpdf *tpdf) +{ + ly_print_(pctx->out, "%*stypedef %s {\n", INDENT, tpdf->name); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_TYPEDEF, 0, tpdf->exts, NULL); + + yprp_type(pctx, &tpdf->type); + + if (tpdf->units) { + ypr_substmt(pctx, LY_STMT_UNITS, 0, tpdf->units, tpdf->exts); + } + if (tpdf->dflt.str) { + ypr_substmt(pctx, LY_STMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts); + } + + ypr_status(pctx, tpdf->flags, tpdf->exts, NULL); + ypr_description(pctx, tpdf->dsc, tpdf->exts, NULL); + ypr_reference(pctx, tpdf->ref, tpdf->exts, NULL); + + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); +} + +static void yprp_node(struct lys_ypr_ctx *pctx, const struct lysp_node *node); +static void yprc_node(struct lys_ypr_ctx *pctx, const struct lysc_node *node); +static void yprp_action(struct lys_ypr_ctx *pctx, const struct lysp_node_action *action); +static void yprp_notification(struct lys_ypr_ctx *pctx, const struct lysp_node_notif *notif); + +static void +yprp_grouping(struct lys_ypr_ctx *pctx, const struct lysp_node_grp *grp) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysp_node *data; + struct lysp_node_action *action; + struct lysp_node_notif *notif; + struct lysp_node_grp *subgrp; + + ly_print_(pctx->out, "%*sgrouping %s", INDENT, grp->name); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_GROUPING, 0, grp->exts, &flag); + ypr_status(pctx, grp->flags, grp->exts, &flag); + ypr_description(pctx, grp->dsc, grp->exts, &flag); + ypr_reference(pctx, grp->ref, grp->exts, &flag); + + LY_ARRAY_FOR(grp->typedefs, u) { + ypr_open(pctx->out, &flag); + yprp_typedef(pctx, &grp->typedefs[u]); + } + + LY_LIST_FOR(grp->groupings, subgrp) { + ypr_open(pctx->out, &flag); + yprp_grouping(pctx, subgrp); + } + + LY_LIST_FOR(grp->child, data) { + ypr_open(pctx->out, &flag); + yprp_node(pctx, data); + } + + LY_LIST_FOR(grp->actions, action) { + ypr_open(pctx->out, &flag); + yprp_action(pctx, action); + } + + LY_LIST_FOR(grp->notifs, notif) { + ypr_open(pctx->out, &flag); + yprp_notification(pctx, notif); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_inout(struct lys_ypr_ctx *pctx, const struct lysp_node_action_inout *inout, ly_bool *flag) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysp_node *data; + struct lysp_node_grp *grp; + + if (!inout->child) { + /* no children */ + return; + } + ypr_open(pctx->out, flag); + YPR_EXTRA_LINE_PRINT(pctx); + + ly_print_(pctx->out, "%*s%s {\n", INDENT, inout->name); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_MUST, 0, inout->exts, NULL); + LY_ARRAY_FOR(inout->musts, u) { + yprp_restr(pctx, &inout->musts[u], LY_STMT_MUST, NULL); + } + LY_ARRAY_FOR(inout->typedefs, u) { + yprp_typedef(pctx, &inout->typedefs[u]); + } + LY_LIST_FOR(inout->groupings, grp) { + yprp_grouping(pctx, grp); + } + + LY_LIST_FOR(inout->child, data) { + yprp_node(pctx, data); + } + + LEVEL--; + ypr_close(pctx, 1); +} + +static void +yprc_inout(struct lys_ypr_ctx *pctx, const struct lysc_node_action_inout *inout, ly_bool *flag) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysc_node *data; + + if (!inout->child) { + /* input/output is empty */ + return; + } + ypr_open(pctx->out, flag); + + ly_print_(pctx->out, "\n%*s%s {\n", INDENT, inout->name); + LEVEL++; + + yprc_extension_instances(pctx, lyplg_ext_nodetype2stmt(inout->nodetype), 0, inout->exts, NULL); + LY_ARRAY_FOR(inout->musts, u) { + yprc_must(pctx, &inout->musts[u], NULL); + } + + if (!(pctx->options & LYS_PRINT_NO_SUBSTMT)) { + LY_LIST_FOR(inout->child, data) { + yprc_node(pctx, data); + } + } + + LEVEL--; + ypr_close(pctx, 1); +} + +static void +yprp_notification(struct lys_ypr_ctx *pctx, const struct lysp_node_notif *notif) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysp_node *data; + struct lysp_node_grp *grp; + + ly_print_(pctx->out, "%*snotification %s", INDENT, notif->name); + + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag); + yprp_iffeatures(pctx, notif->iffeatures, notif->exts, &flag); + + LY_ARRAY_FOR(notif->musts, u) { + yprp_restr(pctx, ¬if->musts[u], LY_STMT_MUST, &flag); + } + ypr_status(pctx, notif->flags, notif->exts, &flag); + ypr_description(pctx, notif->dsc, notif->exts, &flag); + ypr_reference(pctx, notif->ref, notif->exts, &flag); + + LY_ARRAY_FOR(notif->typedefs, u) { + ypr_open(pctx->out, &flag); + yprp_typedef(pctx, ¬if->typedefs[u]); + } + + LY_LIST_FOR(notif->groupings, grp) { + ypr_open(pctx->out, &flag); + yprp_grouping(pctx, grp); + } + + LY_LIST_FOR(notif->child, data) { + ypr_open(pctx->out, &flag); + yprp_node(pctx, data); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_notification(struct lys_ypr_ctx *pctx, const struct lysc_node_notif *notif) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysc_node *data; + + ly_print_(pctx->out, "%*snotification %s", INDENT, notif->name); + + LEVEL++; + yprc_extension_instances(pctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag); + + LY_ARRAY_FOR(notif->musts, u) { + yprc_must(pctx, ¬if->musts[u], &flag); + } + ypr_status(pctx, notif->flags, notif->exts, &flag); + ypr_description(pctx, notif->dsc, notif->exts, &flag); + ypr_reference(pctx, notif->ref, notif->exts, &flag); + + if (!(pctx->options & LYS_PRINT_NO_SUBSTMT)) { + LY_LIST_FOR(notif->child, data) { + ypr_open(pctx->out, &flag); + yprc_node(pctx, data); + } + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_action(struct lys_ypr_ctx *pctx, const struct lysp_node_action *action) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysp_node_grp *grp; + + ly_print_(pctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name); + + LEVEL++; + yprp_extension_instances(pctx, lyplg_ext_nodetype2stmt(action->nodetype), 0, action->exts, &flag); + yprp_iffeatures(pctx, action->iffeatures, action->exts, &flag); + ypr_status(pctx, action->flags, action->exts, &flag); + ypr_description(pctx, action->dsc, action->exts, &flag); + ypr_reference(pctx, action->ref, action->exts, &flag); + + YPR_EXTRA_LINE(flag, pctx); + + LY_ARRAY_FOR(action->typedefs, u) { + ypr_open(pctx->out, &flag); + YPR_EXTRA_LINE_PRINT(pctx); + yprp_typedef(pctx, &action->typedefs[u]); + } + + YPR_EXTRA_LINE(action->typedefs, pctx); + + LY_LIST_FOR(action->groupings, grp) { + ypr_open(pctx->out, &flag); + YPR_EXTRA_LINE_PRINT(pctx); + yprp_grouping(pctx, grp); + } + + YPR_EXTRA_LINE(action->groupings, pctx); + + yprp_inout(pctx, &action->input, &flag); + yprp_inout(pctx, &action->output, &flag); + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_action(struct lys_ypr_ctx *pctx, const struct lysc_node_action *action) +{ + ly_bool flag = 0; + + ly_print_(pctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name); + + LEVEL++; + yprc_extension_instances(pctx, lyplg_ext_nodetype2stmt(action->nodetype), 0, action->exts, &flag); + ypr_status(pctx, action->flags, action->exts, &flag); + ypr_description(pctx, action->dsc, action->exts, &flag); + ypr_reference(pctx, action->ref, action->exts, &flag); + + yprc_inout(pctx, &action->input, &flag); + yprc_inout(pctx, &action->output, &flag); + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_node_common1(struct lys_ypr_ctx *pctx, const struct lysp_node *node, ly_bool *flag) +{ + ly_print_(pctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n"); + LEVEL++; + + yprp_extension_instances(pctx, lyplg_ext_nodetype2stmt(node->nodetype), 0, node->exts, flag); + yprp_when(pctx, lysp_node_when(node), flag); + yprp_iffeatures(pctx, node->iffeatures, node->exts, flag); +} + +static void +yprc_node_common1(struct lys_ypr_ctx *pctx, const struct lysc_node *node, ly_bool *flag) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysc_when **when; + + ly_print_(pctx->out, "%*s%s %s%s", INDENT, lys_nodetype2str(node->nodetype), node->name, flag ? "" : " {\n"); + LEVEL++; + + yprc_extension_instances(pctx, lyplg_ext_nodetype2stmt(node->nodetype), 0, node->exts, flag); + + when = lysc_node_when(node); + LY_ARRAY_FOR(when, u) { + yprc_when(pctx, when[u], flag); + } +} + +/* macro to unify the code */ +#define YPR_NODE_COMMON2 \ + ypr_config(pctx, node->flags, node->exts, flag); \ + if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) { \ + ypr_mandatory(pctx, node->flags, node->exts, flag); \ + } \ + ypr_status(pctx, node->flags, node->exts, flag); \ + ypr_description(pctx, node->dsc, node->exts, flag); \ + ypr_reference(pctx, node->ref, node->exts, flag) + +static void +yprp_node_common2(struct lys_ypr_ctx *pctx, const struct lysp_node *node, ly_bool *flag) +{ + YPR_NODE_COMMON2; +} + +static void +yprc_node_common2(struct lys_ypr_ctx *pctx, const struct lysc_node *node, ly_bool *flag) +{ + YPR_NODE_COMMON2; +} + +#undef YPR_NODE_COMMON2 + +static void +yprp_container(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysp_node *child; + struct lysp_node_action *action; + struct lysp_node_notif *notif; + struct lysp_node_grp *grp; + struct lysp_node_container *cont = (struct lysp_node_container *)node; + + yprp_node_common1(pctx, node, &flag); + + LY_ARRAY_FOR(cont->musts, u) { + yprp_restr(pctx, &cont->musts[u], LY_STMT_MUST, &flag); + } + if (cont->presence) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_PRESENCE, 0, cont->presence, cont->exts); + } + + yprp_node_common2(pctx, node, &flag); + + LY_ARRAY_FOR(cont->typedefs, u) { + ypr_open(pctx->out, &flag); + yprp_typedef(pctx, &cont->typedefs[u]); + } + + LY_LIST_FOR(cont->groupings, grp) { + ypr_open(pctx->out, &flag); + yprp_grouping(pctx, grp); + } + + LY_LIST_FOR(cont->child, child) { + ypr_open(pctx->out, &flag); + yprp_node(pctx, child); + } + + LY_LIST_FOR(cont->actions, action) { + ypr_open(pctx->out, &flag); + yprp_action(pctx, action); + } + + LY_LIST_FOR(cont->notifs, notif) { + ypr_open(pctx->out, &flag); + yprp_notification(pctx, notif); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_container(struct lys_ypr_ctx *pctx, const struct lysc_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysc_node *child; + struct lysc_node_action *action; + struct lysc_node_notif *notif; + struct lysc_node_container *cont = (struct lysc_node_container *)node; + + yprc_node_common1(pctx, node, &flag); + + LY_ARRAY_FOR(cont->musts, u) { + yprc_must(pctx, &cont->musts[u], &flag); + } + if (cont->flags & LYS_PRESENCE) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_PRESENCE, 0, "true", cont->exts); + } + + yprc_node_common2(pctx, node, &flag); + + if (!(pctx->options & LYS_PRINT_NO_SUBSTMT)) { + LY_LIST_FOR(cont->child, child) { + ypr_open(pctx->out, &flag); + yprc_node(pctx, child); + } + + LY_LIST_FOR(cont->actions, action) { + ypr_open(pctx->out, &flag); + yprc_action(pctx, action); + } + + LY_LIST_FOR(cont->notifs, notif) { + ypr_open(pctx->out, &flag); + yprc_notification(pctx, notif); + } + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_case(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + ly_bool flag = 0; + struct lysp_node *child; + struct lysp_node_case *cas = (struct lysp_node_case *)node; + + yprp_node_common1(pctx, node, &flag); + yprp_node_common2(pctx, node, &flag); + + LY_LIST_FOR(cas->child, child) { + ypr_open(pctx->out, &flag); + yprp_node(pctx, child); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_case(struct lys_ypr_ctx *pctx, const struct lysc_node_case *cs) +{ + ly_bool flag = 0; + struct lysc_node *child; + + yprc_node_common1(pctx, &cs->node, &flag); + yprc_node_common2(pctx, &cs->node, &flag); + + if (!(pctx->options & LYS_PRINT_NO_SUBSTMT)) { + for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) { + ypr_open(pctx->out, &flag); + yprc_node(pctx, child); + } + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_choice(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + ly_bool flag = 0; + struct lysp_node *child; + struct lysp_node_choice *choice = (struct lysp_node_choice *)node; + + yprp_node_common1(pctx, node, &flag); + + if (choice->dflt.str) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_DEFAULT, 0, choice->dflt.str, choice->exts); + } + + yprp_node_common2(pctx, node, &flag); + + LY_LIST_FOR(choice->child, child) { + ypr_open(pctx->out, &flag); + yprp_node(pctx, child); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_choice(struct lys_ypr_ctx *pctx, const struct lysc_node *node) +{ + ly_bool flag = 0; + struct lysc_node_case *cs; + struct lysc_node_choice *choice = (struct lysc_node_choice *)node; + + yprc_node_common1(pctx, node, &flag); + + if (choice->dflt) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_DEFAULT, 0, choice->dflt->name, choice->exts); + } + + yprc_node_common2(pctx, node, &flag); + + for (cs = choice->cases; cs; cs = (struct lysc_node_case *)cs->next) { + ypr_open(pctx->out, &flag); + yprc_case(pctx, cs); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_leaf(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node; + + yprp_node_common1(pctx, node, NULL); + + yprp_type(pctx, &leaf->type); + ypr_substmt(pctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts); + LY_ARRAY_FOR(leaf->musts, u) { + yprp_restr(pctx, &leaf->musts[u], LY_STMT_MUST, NULL); + } + ypr_substmt(pctx, LY_STMT_DEFAULT, 0, leaf->dflt.str, leaf->exts); + + yprp_node_common2(pctx, node, NULL); + + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); +} + +static void +yprc_leaf(struct lys_ypr_ctx *pctx, const struct lysc_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; + + yprc_node_common1(pctx, node, NULL); + + yprc_type(pctx, leaf->type); + ypr_substmt(pctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts); + LY_ARRAY_FOR(leaf->musts, u) { + yprc_must(pctx, &leaf->musts[u], NULL); + } + + if (leaf->dflt) { + yprc_dflt_value(pctx, node->module->ctx, leaf->dflt, leaf->exts); + } + + yprc_node_common2(pctx, node, NULL); + + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); +} + +static void +yprp_leaflist(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node; + + yprp_node_common1(pctx, node, NULL); + + yprp_type(pctx, &llist->type); + ypr_substmt(pctx, LY_STMT_UNITS, 0, llist->units, llist->exts); + LY_ARRAY_FOR(llist->musts, u) { + yprp_restr(pctx, &llist->musts[u], LY_STMT_MUST, NULL); + } + LY_ARRAY_FOR(llist->dflts, u) { + ypr_substmt(pctx, LY_STMT_DEFAULT, u, llist->dflts[u].str, llist->exts); + } + + ypr_config(pctx, node->flags, node->exts, NULL); + + if (llist->flags & LYS_SET_MIN) { + ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min, NULL); + } + if (llist->flags & LYS_SET_MAX) { + if (llist->max) { + ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max, NULL); + } else { + ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts); + } + } + + if (llist->flags & LYS_ORDBY_MASK) { + ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts); + } + + ypr_status(pctx, node->flags, node->exts, NULL); + ypr_description(pctx, node->dsc, node->exts, NULL); + ypr_reference(pctx, node->ref, node->exts, NULL); + + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); +} + +static void +yprc_leaflist(struct lys_ypr_ctx *pctx, const struct lysc_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; + + yprc_node_common1(pctx, node, NULL); + + yprc_type(pctx, llist->type); + ypr_substmt(pctx, LY_STMT_UNITS, 0, llist->units, llist->exts); + LY_ARRAY_FOR(llist->musts, u) { + yprc_must(pctx, &llist->musts[u], NULL); + } + LY_ARRAY_FOR(llist->dflts, u) { + yprc_dflt_value(pctx, node->module->ctx, llist->dflts[u], llist->exts); + } + + ypr_config(pctx, node->flags, node->exts, NULL); + + ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min, NULL); + if (llist->max) { + ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max, NULL); + } else { + ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts); + } + + ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts); + + ypr_status(pctx, node->flags, node->exts, NULL); + ypr_description(pctx, node->dsc, node->exts, NULL); + ypr_reference(pctx, node->ref, node->exts, NULL); + + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); +} + +static void +yprp_list(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysp_node *child; + struct lysp_node_action *action; + struct lysp_node_notif *notif; + struct lysp_node_grp *grp; + struct lysp_node_list *list = (struct lysp_node_list *)node; + + yprp_node_common1(pctx, node, &flag); + + LY_ARRAY_FOR(list->musts, u) { + yprp_restr(pctx, &list->musts[u], LY_STMT_MUST, &flag); + } + if (list->key) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_KEY, 0, list->key, list->exts); + } + LY_ARRAY_FOR(list->uniques, u) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_UNIQUE, u, list->uniques[u].str, list->exts); + } + + ypr_config(pctx, node->flags, node->exts, &flag); + + if (list->flags & LYS_SET_MIN) { + ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min, &flag); + } + if (list->flags & LYS_SET_MAX) { + if (list->max) { + ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max, &flag); + } else { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts); + } + } + + if (list->flags & LYS_ORDBY_MASK) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts); + } + + ypr_status(pctx, node->flags, node->exts, &flag); + ypr_description(pctx, node->dsc, node->exts, &flag); + ypr_reference(pctx, node->ref, node->exts, &flag); + + LY_ARRAY_FOR(list->typedefs, u) { + ypr_open(pctx->out, &flag); + yprp_typedef(pctx, &list->typedefs[u]); + } + + LY_LIST_FOR(list->groupings, grp) { + ypr_open(pctx->out, &flag); + yprp_grouping(pctx, grp); + } + + LY_LIST_FOR(list->child, child) { + ypr_open(pctx->out, &flag); + yprp_node(pctx, child); + } + + LY_LIST_FOR(list->actions, action) { + ypr_open(pctx->out, &flag); + yprp_action(pctx, action); + } + + LY_LIST_FOR(list->notifs, notif) { + ypr_open(pctx->out, &flag); + yprp_notification(pctx, notif); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_list(struct lys_ypr_ctx *pctx, const struct lysc_node *node) +{ + LY_ARRAY_COUNT_TYPE u, v; + struct lysc_node_list *list = (struct lysc_node_list *)node; + + yprc_node_common1(pctx, node, NULL); + + LY_ARRAY_FOR(list->musts, u) { + yprc_must(pctx, &list->musts[u], NULL); + } + if (!(list->flags & LYS_KEYLESS)) { + ly_print_(pctx->out, "%*skey \"", INDENT); + for (struct lysc_node *key = list->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) { + ly_print_(pctx->out, "%s%s", u > 0 ? ", " : "", key->name); + } + ly_print_(pctx->out, "\";\n"); + } + LY_ARRAY_FOR(list->uniques, u) { + ly_print_(pctx->out, "%*sunique \"", INDENT); + LY_ARRAY_FOR(list->uniques[u], v) { + ly_print_(pctx->out, "%s%s", v > 0 ? ", " : "", list->uniques[u][v]->name); + } + ypr_close(pctx, 0); + } + + ypr_config(pctx, node->flags, node->exts, NULL); + + ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min, NULL); + if (list->max) { + ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max, NULL); + } else { + ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts); + } + + ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts); + + ypr_status(pctx, node->flags, node->exts, NULL); + ypr_description(pctx, node->dsc, node->exts, NULL); + ypr_reference(pctx, node->ref, node->exts, NULL); + + if (!(pctx->options & LYS_PRINT_NO_SUBSTMT)) { + struct lysc_node *child; + struct lysc_node_action *action; + struct lysc_node_notif *notif; + + LY_LIST_FOR(list->child, child) { + yprc_node(pctx, child); + } + + LY_LIST_FOR(list->actions, action) { + yprc_action(pctx, action); + } + + LY_LIST_FOR(list->notifs, notif) { + yprc_notification(pctx, notif); + } + } + + LEVEL--; + ypr_close(pctx, 1); +} + +static void +yprp_refine(struct lys_ypr_ctx *pctx, struct lysp_refine *refine) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + + ly_print_(pctx->out, "%*srefine \"%s\"", INDENT, refine->nodeid); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_REFINE, 0, refine->exts, &flag); + yprp_iffeatures(pctx, refine->iffeatures, refine->exts, &flag); + + LY_ARRAY_FOR(refine->musts, u) { + ypr_open(pctx->out, &flag); + yprp_restr(pctx, &refine->musts[u], LY_STMT_MUST, NULL); + } + + if (refine->presence) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_PRESENCE, 0, refine->presence, refine->exts); + } + + LY_ARRAY_FOR(refine->dflts, u) { + ypr_open(pctx->out, &flag); + ypr_substmt(pctx, LY_STMT_DEFAULT, u, refine->dflts[u].str, refine->exts); + } + + ypr_config(pctx, refine->flags, refine->exts, &flag); + ypr_mandatory(pctx, refine->flags, refine->exts, &flag); + + if (refine->flags & LYS_SET_MIN) { + ypr_open(pctx->out, &flag); + ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, refine->exts, refine->min, NULL); + } + if (refine->flags & LYS_SET_MAX) { + ypr_open(pctx->out, &flag); + if (refine->max) { + ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, refine->exts, refine->max, NULL); + } else { + ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", refine->exts); + } + } + + ypr_description(pctx, refine->dsc, refine->exts, &flag); + ypr_reference(pctx, refine->ref, refine->exts, &flag); + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_augment(struct lys_ypr_ctx *pctx, const struct lysp_node_augment *aug) +{ + struct lysp_node *child; + struct lysp_node_action *action; + struct lysp_node_notif *notif; + + ly_print_(pctx->out, "%*saugment \"%s\" {\n", INDENT, aug->nodeid); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_AUGMENT, 0, aug->exts, NULL); + yprp_when(pctx, aug->when, NULL); + yprp_iffeatures(pctx, aug->iffeatures, aug->exts, NULL); + ypr_status(pctx, aug->flags, aug->exts, NULL); + ypr_description(pctx, aug->dsc, aug->exts, NULL); + ypr_reference(pctx, aug->ref, aug->exts, NULL); + + LY_LIST_FOR(aug->child, child) { + yprp_node(pctx, child); + } + + LY_LIST_FOR(aug->actions, action) { + yprp_action(pctx, action); + } + + LY_LIST_FOR(aug->notifs, notif) { + yprp_notification(pctx, notif); + } + + LEVEL--; + ypr_close(pctx, 1); +} + +static void +yprp_uses(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysp_node_uses *uses = (struct lysp_node_uses *)node; + struct lysp_node_augment *aug; + + yprp_node_common1(pctx, node, &flag); + yprp_node_common2(pctx, node, &flag); + + LY_ARRAY_FOR(uses->refines, u) { + ypr_open(pctx->out, &flag); + yprp_refine(pctx, &uses->refines[u]); + } + + LY_LIST_FOR(uses->augments, aug) { + ypr_open(pctx->out, &flag); + yprp_augment(pctx, aug); + } + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_anydata(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysp_node_anydata *any = (struct lysp_node_anydata *)node; + + yprp_node_common1(pctx, node, &flag); + + LY_ARRAY_FOR(any->musts, u) { + ypr_open(pctx->out, &flag); + yprp_restr(pctx, &any->musts[u], LY_STMT_MUST, NULL); + } + + yprp_node_common2(pctx, node, &flag); + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprc_anydata(struct lys_ypr_ctx *pctx, const struct lysc_node *node) +{ + LY_ARRAY_COUNT_TYPE u; + ly_bool flag = 0; + struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; + + yprc_node_common1(pctx, node, &flag); + + LY_ARRAY_FOR(any->musts, u) { + ypr_open(pctx->out, &flag); + yprc_must(pctx, &any->musts[u], NULL); + } + + yprc_node_common2(pctx, node, &flag); + + LEVEL--; + ypr_close(pctx, flag); +} + +static void +yprp_node(struct lys_ypr_ctx *pctx, const struct lysp_node *node) +{ + switch (node->nodetype) { + case LYS_CONTAINER: + yprp_container(pctx, node); + break; + case LYS_CHOICE: + yprp_choice(pctx, node); + break; + case LYS_LEAF: + yprp_leaf(pctx, node); + break; + case LYS_LEAFLIST: + yprp_leaflist(pctx, node); + break; + case LYS_LIST: + yprp_list(pctx, node); + break; + case LYS_USES: + yprp_uses(pctx, node); + break; + case LYS_ANYXML: + case LYS_ANYDATA: + yprp_anydata(pctx, node); + break; + case LYS_CASE: + yprp_case(pctx, node); + break; + default: + break; + } +} + +static void +yprc_node(struct lys_ypr_ctx *pctx, const struct lysc_node *node) +{ + switch (node->nodetype) { + case LYS_CONTAINER: + yprc_container(pctx, node); + break; + case LYS_CHOICE: + yprc_choice(pctx, node); + break; + case LYS_LEAF: + yprc_leaf(pctx, node); + break; + case LYS_LEAFLIST: + yprc_leaflist(pctx, node); + break; + case LYS_LIST: + yprc_list(pctx, node); + break; + case LYS_ANYXML: + case LYS_ANYDATA: + yprc_anydata(pctx, node); + break; + default: + break; + } +} + +static void +yprp_deviation(struct lys_ypr_ctx *pctx, const struct lysp_deviation *deviation) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysp_deviate_add *add; + struct lysp_deviate_rpl *rpl; + struct lysp_deviate_del *del; + struct lysp_deviate *elem; + + ly_print_(pctx->out, "%*sdeviation \"%s\" {\n", INDENT, deviation->nodeid); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_DEVIATION, 0, deviation->exts, NULL); + ypr_description(pctx, deviation->dsc, deviation->exts, NULL); + ypr_reference(pctx, deviation->ref, deviation->exts, NULL); + + LY_LIST_FOR(deviation->deviates, elem) { + ly_print_(pctx->out, "%*sdeviate ", INDENT); + if (elem->mod == LYS_DEV_NOT_SUPPORTED) { + if (elem->exts) { + ly_print_(pctx->out, "not-supported {\n"); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, elem->exts, NULL); + } else { + ly_print_(pctx->out, "not-supported;\n"); + continue; + } + } else if (elem->mod == LYS_DEV_ADD) { + add = (struct lysp_deviate_add *)elem; + ly_print_(pctx->out, "add {\n"); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, add->exts, NULL); + ypr_substmt(pctx, LY_STMT_UNITS, 0, add->units, add->exts); + LY_ARRAY_FOR(add->musts, u) { + yprp_restr(pctx, &add->musts[u], LY_STMT_MUST, NULL); + } + LY_ARRAY_FOR(add->uniques, u) { + ypr_substmt(pctx, LY_STMT_UNIQUE, u, add->uniques[u].str, add->exts); + } + LY_ARRAY_FOR(add->dflts, u) { + ypr_substmt(pctx, LY_STMT_DEFAULT, u, add->dflts[u].str, add->exts); + } + ypr_config(pctx, add->flags, add->exts, NULL); + ypr_mandatory(pctx, add->flags, add->exts, NULL); + if (add->flags & LYS_SET_MIN) { + ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, add->exts, add->min, NULL); + } + if (add->flags & LYS_SET_MAX) { + if (add->max) { + ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, add->exts, add->max, NULL); + } else { + ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", add->exts); + } + } + } else if (elem->mod == LYS_DEV_REPLACE) { + rpl = (struct lysp_deviate_rpl *)elem; + ly_print_(pctx->out, "replace {\n"); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, rpl->exts, NULL); + if (rpl->type) { + yprp_type(pctx, rpl->type); + } + ypr_substmt(pctx, LY_STMT_UNITS, 0, rpl->units, rpl->exts); + ypr_substmt(pctx, LY_STMT_DEFAULT, 0, rpl->dflt.str, rpl->exts); + ypr_config(pctx, rpl->flags, rpl->exts, NULL); + ypr_mandatory(pctx, rpl->flags, rpl->exts, NULL); + if (rpl->flags & LYS_SET_MIN) { + ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, rpl->exts, rpl->min, NULL); + } + if (rpl->flags & LYS_SET_MAX) { + if (rpl->max) { + ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, rpl->exts, rpl->max, NULL); + } else { + ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", rpl->exts); + } + } + } else if (elem->mod == LYS_DEV_DELETE) { + del = (struct lysp_deviate_del *)elem; + ly_print_(pctx->out, "delete {\n"); + LEVEL++; + + yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, del->exts, NULL); + ypr_substmt(pctx, LY_STMT_UNITS, 0, del->units, del->exts); + LY_ARRAY_FOR(del->musts, u) { + yprp_restr(pctx, &del->musts[u], LY_STMT_MUST, NULL); + } + LY_ARRAY_FOR(del->uniques, u) { + ypr_substmt(pctx, LY_STMT_UNIQUE, u, del->uniques[u].str, del->exts); + } + LY_ARRAY_FOR(del->dflts, u) { + ypr_substmt(pctx, LY_STMT_DEFAULT, u, del->dflts[u].str, del->exts); + } + } + + LEVEL--; + ypr_close(pctx, 1); + } + + LEVEL--; + ypr_close(pctx, 1); +} + +static void +yang_print_parsed_linkage(struct lys_ypr_ctx *pctx, const struct lysp_module *modp) +{ + LY_ARRAY_COUNT_TYPE u; + + LY_ARRAY_FOR(modp->imports, u) { + if (modp->imports[u].flags & LYS_INTERNAL) { + continue; + } + + YPR_EXTRA_LINE_PRINT(pctx); + ly_print_(pctx->out, "%*simport %s {\n", INDENT, modp->imports[u].name); + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_IMPORT, 0, modp->imports[u].exts, NULL); + ypr_substmt(pctx, LY_STMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts); + if (modp->imports[u].rev[0]) { + ypr_substmt(pctx, LY_STMT_REVISION_DATE, 0, modp->imports[u].rev, modp->imports[u].exts); + } + ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts); + ypr_substmt(pctx, LY_STMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts); + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); + } + YPR_EXTRA_LINE(modp->imports, pctx); + + LY_ARRAY_FOR(modp->includes, u) { + if (modp->includes[u].injected) { + /* do not print the includes injected from submodules */ + continue; + } + YPR_EXTRA_LINE_PRINT(pctx); + if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) { + ly_print_(pctx->out, "%*sinclude %s {\n", INDENT, modp->includes[u].name); + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_INCLUDE, 0, modp->includes[u].exts, NULL); + if (modp->includes[u].rev[0]) { + ypr_substmt(pctx, LY_STMT_REVISION_DATE, 0, modp->includes[u].rev, modp->includes[u].exts); + } + ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts); + ypr_substmt(pctx, LY_STMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts); + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); + } else { + ly_print_(pctx->out, "\n%*sinclude \"%s\";\n", INDENT, modp->includes[u].name); + } + } + YPR_EXTRA_LINE(modp->includes, pctx); +} + +static void +yang_print_parsed_body(struct lys_ypr_ctx *pctx, const struct lysp_module *modp) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysp_node *data; + struct lysp_node_action *action; + struct lysp_node_notif *notif; + struct lysp_node_grp *grp; + struct lysp_node_augment *aug; + + LY_ARRAY_FOR(modp->extensions, u) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_extension(pctx, &modp->extensions[u]); + } + + YPR_EXTRA_LINE(modp->extensions, pctx); + + if (modp->exts) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_extension_instances(pctx, LY_STMT_MODULE, 0, modp->exts, NULL); + } + + YPR_EXTRA_LINE(modp->exts, pctx); + + LY_ARRAY_FOR(modp->features, u) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_feature(pctx, &modp->features[u]); + YPR_EXTRA_LINE(1, pctx); + } + + YPR_EXTRA_LINE(modp->features, pctx); + + LY_ARRAY_FOR(modp->identities, u) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_identity(pctx, &modp->identities[u]); + YPR_EXTRA_LINE(1, pctx); + } + + YPR_EXTRA_LINE(modp->identities, pctx); + + LY_ARRAY_FOR(modp->typedefs, u) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_typedef(pctx, &modp->typedefs[u]); + YPR_EXTRA_LINE(1, pctx); + } + + YPR_EXTRA_LINE(modp->typedefs, pctx); + + LY_LIST_FOR(modp->groupings, grp) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_grouping(pctx, grp); + YPR_EXTRA_LINE(1, pctx); + } + + YPR_EXTRA_LINE(modp->groupings, pctx); + + LY_LIST_FOR(modp->data, data) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_node(pctx, data); + } + + YPR_EXTRA_LINE(modp->data, pctx); + + LY_LIST_FOR(modp->augments, aug) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_augment(pctx, aug); + } + + YPR_EXTRA_LINE(modp->augments, pctx); + + LY_LIST_FOR(modp->rpcs, action) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_action(pctx, action); + } + + YPR_EXTRA_LINE(modp->rpcs, pctx); + + LY_LIST_FOR(modp->notifs, notif) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_notification(pctx, notif); + } + + YPR_EXTRA_LINE(modp->notifs, pctx); + + LY_ARRAY_FOR(modp->deviations, u) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_deviation(pctx, &modp->deviations[u]); + } + + YPR_EXTRA_LINE(modp->deviations, pctx); +} + +LY_ERR +yang_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options) +{ + LY_ARRAY_COUNT_TYPE u; + const struct lys_module *module = modp->mod; + struct lys_ypr_ctx pctx_ = { + .out = out, + .level = 0, + .options = options, + .module = module, + .schema = LYS_YPR_PARSED + }, *pctx = &pctx_; + + ly_print_(pctx->out, "%*smodule %s {\n", INDENT, module->name); + LEVEL++; + + /* module-header-stmts */ + if (modp->version) { + ypr_substmt(pctx, LY_STMT_YANG_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts); + } + ypr_substmt(pctx, LY_STMT_NAMESPACE, 0, module->ns, modp->exts); + ypr_substmt(pctx, LY_STMT_PREFIX, 0, module->prefix, modp->exts); + + YPR_EXTRA_LINE(1, pctx); + + /* linkage-stmts (import/include) */ + yang_print_parsed_linkage(pctx, modp); + + /* meta-stmts */ + if (module->org || module->contact || module->dsc || module->ref) { + YPR_EXTRA_LINE_PRINT(pctx); + } + ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, module->org, modp->exts); + ypr_substmt(pctx, LY_STMT_CONTACT, 0, module->contact, modp->exts); + ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, module->dsc, modp->exts); + ypr_substmt(pctx, LY_STMT_REFERENCE, 0, module->ref, modp->exts); + + YPR_EXTRA_LINE(module->org || module->contact || module->dsc || module->ref, pctx); + + /* revision-stmts */ + LY_ARRAY_FOR(modp->revs, u) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_revision(pctx, &modp->revs[u]); + } + + YPR_EXTRA_LINE(modp->revs, pctx); + + /* body-stmts */ + yang_print_parsed_body(pctx, modp); + + LEVEL--; + ly_print_(out, "%*s}\n", INDENT); + ly_print_flush(out); + + return LY_SUCCESS; +} + +static void +yprp_belongsto(struct lys_ypr_ctx *pctx, const struct lysp_submodule *submodp) +{ + ly_print_(pctx->out, "%*sbelongs-to %s {\n", INDENT, submodp->mod->name); + LEVEL++; + yprp_extension_instances(pctx, LY_STMT_BELONGS_TO, 0, submodp->exts, NULL); + ypr_substmt(pctx, LY_STMT_PREFIX, 0, submodp->prefix, submodp->exts); + LEVEL--; + ly_print_(pctx->out, "%*s}\n", INDENT); +} + +LY_ERR +yang_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options) +{ + LY_ARRAY_COUNT_TYPE u; + struct lys_ypr_ctx pctx_ = { + .out = out, + .level = 0, + .options = options, + .module = submodp->mod, + .schema = LYS_YPR_PARSED + }, *pctx = &pctx_; + + ly_print_(pctx->out, "%*ssubmodule %s {\n", INDENT, submodp->name); + LEVEL++; + + /* submodule-header-stmts */ + if (submodp->version) { + ypr_substmt(pctx, LY_STMT_YANG_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts); + } + + yprp_belongsto(pctx, submodp); + + YPR_EXTRA_LINE(1, pctx); + + /* linkage-stmts (import/include) */ + yang_print_parsed_linkage(pctx, (struct lysp_module *)submodp); + + /* meta-stmts */ + if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) { + YPR_EXTRA_LINE_PRINT(pctx); + } + ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, submodp->org, submodp->exts); + ypr_substmt(pctx, LY_STMT_CONTACT, 0, submodp->contact, submodp->exts); + ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, submodp->dsc, submodp->exts); + ypr_substmt(pctx, LY_STMT_REFERENCE, 0, submodp->ref, submodp->exts); + + YPR_EXTRA_LINE(submodp->org || submodp->contact || submodp->dsc || submodp->ref, pctx); + + /* revision-stmts */ + LY_ARRAY_FOR(submodp->revs, u) { + YPR_EXTRA_LINE_PRINT(pctx); + yprp_revision(pctx, &submodp->revs[u]); + } + + YPR_EXTRA_LINE(submodp->revs, pctx); + + /* body-stmts */ + yang_print_parsed_body(pctx, (struct lysp_module *)submodp); + + LEVEL--; + ly_print_(out, "%*s}\n", INDENT); + ly_print_flush(out); + + return LY_SUCCESS; +} + +LY_ERR +yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options) +{ + struct lys_ypr_ctx pctx_ = { + .out = out, + .level = 0, + .options = options, + .module = node->module, + .schema = LYS_YPR_COMPILED + }, *pctx = &pctx_; + + yprc_node(pctx, node); + + ly_print_flush(out); + return LY_SUCCESS; +} + +LY_ERR +yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options) +{ + LY_ARRAY_COUNT_TYPE u; + struct lysc_module *modc = module->compiled; + struct lys_ypr_ctx pctx_ = { + .out = out, + .level = 0, + .options = options, + .module = module, + .schema = LYS_YPR_COMPILED + }, *pctx = &pctx_; + + ly_print_(pctx->out, "%*smodule %s {\n", INDENT, module->name); + LEVEL++; + + /* module-header-stmts */ + ypr_substmt(pctx, LY_STMT_NAMESPACE, 0, module->ns, modc->exts); + ypr_substmt(pctx, LY_STMT_PREFIX, 0, module->prefix, modc->exts); + + YPR_EXTRA_LINE(1, pctx); + + /* no linkage-stmts */ + + /* meta-stmts */ + if (module->org || module->contact || module->dsc || module->ref) { + YPR_EXTRA_LINE_PRINT(pctx); + } + ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, module->org, modc->exts); + ypr_substmt(pctx, LY_STMT_CONTACT, 0, module->contact, modc->exts); + ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, module->dsc, modc->exts); + ypr_substmt(pctx, LY_STMT_REFERENCE, 0, module->ref, modc->exts); + + YPR_EXTRA_LINE(module->org || module->contact || module->dsc || module->ref, pctx); + + /* revision-stmts */ + if (module->revision) { + YPR_EXTRA_LINE_PRINT(pctx); + ly_print_(pctx->out, "%*srevision %s;\n", INDENT, module->revision); + YPR_EXTRA_LINE(1, pctx); + } + + /* body-stmts */ + if (modc->exts) { + YPR_EXTRA_LINE_PRINT(pctx); + yprc_extension_instances(pctx, LY_STMT_MODULE, 0, module->compiled->exts, NULL); + YPR_EXTRA_LINE(1, pctx); + } + + LY_ARRAY_FOR(module->identities, u) { + YPR_EXTRA_LINE_PRINT(pctx); + yprc_identity(pctx, &module->identities[u]); + YPR_EXTRA_LINE(1, pctx); + } + + if (!(pctx->options & LYS_PRINT_NO_SUBSTMT)) { + struct lysc_node *data; + struct lysc_node_action *rpc; + struct lysc_node_notif *notif; + + LY_LIST_FOR(modc->data, data) { + YPR_EXTRA_LINE_PRINT(pctx); + yprc_node(pctx, data); + } + + YPR_EXTRA_LINE(modc->data, pctx); + + LY_LIST_FOR(modc->rpcs, rpc) { + YPR_EXTRA_LINE_PRINT(pctx); + yprc_action(pctx, rpc); + } + + YPR_EXTRA_LINE(modc->rpcs, pctx); + + LY_LIST_FOR(modc->notifs, notif) { + YPR_EXTRA_LINE_PRINT(pctx); + yprc_notification(pctx, notif); + } + + YPR_EXTRA_LINE(modc->notifs, pctx); + } + + LEVEL--; + ly_print_(out, "%*s}\n", INDENT); + ly_print_flush(out); + + return LY_SUCCESS; +} + +LIBYANG_API_DEF void +lyplg_ext_print_info_extension_instance(struct lyspr_ctx *ctx, const struct lysc_ext_instance *ext, ly_bool *flag) +{ + struct lys_ypr_ctx *pctx = (struct lys_ypr_ctx *)ctx; + LY_ARRAY_COUNT_TYPE u, v; + ly_bool data_printed = 0; + + LY_ARRAY_FOR(ext->substmts, u) { + switch (ext->substmts[u].stmt) { + case LY_STMT_NOTIFICATION: + case LY_STMT_INPUT: + case LY_STMT_OUTPUT: + case LY_STMT_ACTION: + case LY_STMT_RPC: + case LY_STMT_ANYDATA: + case LY_STMT_ANYXML: + case LY_STMT_CASE: + case LY_STMT_CHOICE: + case LY_STMT_CONTAINER: + case LY_STMT_LEAF: + case LY_STMT_LEAF_LIST: + case LY_STMT_LIST: + case LY_STMT_USES: { + const struct lysc_node *node; + + if (data_printed) { + break; + } + + LY_LIST_FOR(*(const struct lysc_node **)ext->substmts[u].storage, node) { + ypr_open(pctx->out, flag); + if (ext->substmts[u].stmt == LY_STMT_NOTIFICATION) { + yprc_notification(pctx, (struct lysc_node_notif *)node); + } else if (ext->substmts[u].stmt & (LY_STMT_INPUT | LY_STMT_OUTPUT)) { + yprc_inout(pctx, (struct lysc_node_action_inout *)node, flag); + } else if (ext->substmts[u].stmt & (LY_STMT_ACTION | LY_STMT_RPC)) { + yprc_action(pctx, (struct lysc_node_action *)node); + } else { + yprc_node(pctx, node); + } + } + + /* all data nodes are stored in a linked list so all were printed */ + data_printed = 1; + break; + } + case LY_STMT_ARGUMENT: + case LY_STMT_CONTACT: + case LY_STMT_DESCRIPTION: + case LY_STMT_ERROR_APP_TAG: + case LY_STMT_ERROR_MESSAGE: + case LY_STMT_KEY: + case LY_STMT_MODIFIER: + case LY_STMT_NAMESPACE: + case LY_STMT_ORGANIZATION: + case LY_STMT_PRESENCE: + case LY_STMT_REFERENCE: + case LY_STMT_UNITS: + if (*(const char **)ext->substmts[u].storage) { + ypr_open(pctx->out, flag); + ypr_substmt(pctx, ext->substmts[u].stmt, 0, *(const char **)ext->substmts[u].storage, ext->exts); + } + break; + case LY_STMT_BIT: + case LY_STMT_ENUM: { + const struct lysc_type_bitenum_item *items = *(struct lysc_type_bitenum_item **)ext->substmts[u].storage; + + yprc_bits_enum(pctx, items, ext->substmts[u].stmt == LY_STMT_BIT ? LY_TYPE_BITS : LY_TYPE_ENUM, flag); + break; + } + case LY_STMT_CONFIG: + ypr_config(pctx, *(uint16_t *)ext->substmts[u].storage, ext->exts, flag); + break; + case LY_STMT_EXTENSION_INSTANCE: + yprc_extension_instances(pctx, LY_STMT_EXTENSION_INSTANCE, 0, + *(struct lysc_ext_instance **)ext->substmts[u].storage, flag); + break; + case LY_STMT_FRACTION_DIGITS: + if (*(uint8_t *)ext->substmts[u].storage) { + ypr_unsigned(pctx, LY_STMT_FRACTION_DIGITS, 0, ext->exts, *(uint8_t *)ext->substmts[u].storage, flag); + } + break; + case LY_STMT_IDENTITY: { + const struct lysc_ident *idents = *(struct lysc_ident **)ext->substmts[u].storage; + + LY_ARRAY_FOR(idents, v) { + yprc_identity(pctx, &idents[v]); + } + break; + } + case LY_STMT_LENGTH: + if (*(struct lysc_range **)ext->substmts[u].storage) { + yprc_range(pctx, *(struct lysc_range **)ext->substmts[u].storage, LY_TYPE_STRING, flag); + } + break; + case LY_STMT_MANDATORY: + ypr_mandatory(pctx, *(uint16_t *)ext->substmts[u].storage, ext->exts, flag); + break; + case LY_STMT_MAX_ELEMENTS: { + uint32_t max = *(uint32_t *)ext->substmts[u].storage; + + if (max) { + ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, ext->exts, max, flag); + } else { + ypr_open(pctx->out, flag); + ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", ext->exts); + } + break; + } + case LY_STMT_MIN_ELEMENTS: + ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, ext->exts, *(uint32_t *)ext->substmts[u].storage, flag); + break; + case LY_STMT_ORDERED_BY: + ypr_open(pctx->out, flag); + ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, + (*(uint16_t *)ext->substmts[u].storage & LYS_ORDBY_USER) ? "user" : "system", ext->exts); + break; + case LY_STMT_MUST: { + const struct lysc_must *musts = *(struct lysc_must **)ext->substmts[u].storage; + + LY_ARRAY_FOR(musts, v) { + yprc_must(pctx, &musts[v], flag); + } + break; + } + case LY_STMT_PATTERN: { + const struct lysc_pattern *patterns = *(struct lysc_pattern **)ext->substmts[u].storage; + + LY_ARRAY_FOR(patterns, v) { + yprc_pattern(pctx, &patterns[v], flag); + } + break; + } + case LY_STMT_POSITION: + if (*(int64_t *)ext->substmts[u].storage) { + ypr_unsigned(pctx, ext->substmts[u].stmt, 0, ext->exts, *(int64_t *)ext->substmts[u].storage, flag); + } + break; + case LY_STMT_VALUE: + if (*(int64_t *)ext->substmts[u].storage) { + ypr_signed(pctx, ext->substmts[u].stmt, 0, ext->exts, *(int64_t *)ext->substmts[u].storage, flag); + } + break; + case LY_STMT_RANGE: + if (*(struct lysc_range **)ext->substmts[u].storage) { + yprc_range(pctx, *(struct lysc_range **)ext->substmts[u].storage, LY_TYPE_UINT64, flag); + } + break; + case LY_STMT_REQUIRE_INSTANCE: + ypr_open(pctx->out, flag); + ypr_substmt(pctx, LY_STMT_REQUIRE_INSTANCE, 0, *(uint8_t *)ext->substmts[u].storage ? "true" : "false", + ext->exts); + break; + case LY_STMT_STATUS: + ypr_status(pctx, *(uint16_t *)ext->substmts[u].storage, ext->exts, flag); + break; + case LY_STMT_TYPE: + if (*(const struct lysc_type **)ext->substmts[u].storage) { + ypr_open(pctx->out, flag); + yprc_type(pctx, *(const struct lysc_type **)ext->substmts[u].storage); + } + break; + case LY_STMT_WHEN: + yprc_when(pctx, *(struct lysc_when **)ext->substmts[u].storage, flag); + break; + case LY_STMT_AUGMENT: + case LY_STMT_BASE: + case LY_STMT_BELONGS_TO: + case LY_STMT_DEFAULT: + case LY_STMT_DEVIATE: + case LY_STMT_DEVIATION: + case LY_STMT_EXTENSION: + case LY_STMT_FEATURE: + case LY_STMT_GROUPING: + case LY_STMT_IF_FEATURE: + case LY_STMT_IMPORT: + case LY_STMT_INCLUDE: + case LY_STMT_MODULE: + case LY_STMT_PATH: + case LY_STMT_PREFIX: + case LY_STMT_REFINE: + case LY_STMT_REVISION: + case LY_STMT_REVISION_DATE: + case LY_STMT_SUBMODULE: + case LY_STMT_TYPEDEF: + case LY_STMT_UNIQUE: + case LY_STMT_YANG_VERSION: + case LY_STMT_YIN_ELEMENT: + /* nothing to do */ + break; + default: + LOGINT(pctx->module->ctx); + break; + } + } +} |