<NME> main_ni.c
<BEF> /**
 * @file main_ni.c
 * @author Radek Krejci <rkrejci@cesnet.cz>
 * @brief libyang's yanglint tool - noninteractive code
 *
 * Copyright (c) 2020 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 <errno.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>

#include "libyang.h"

#include "common.h"
#include "out.h"
#include "tools/config.h"

/**
 * @brief Context structure to hold and pass variables in a structured form.
 */
struct context {
    /* libyang context for the run */
    const char *yang_lib_file;
    uint16_t ctx_options;
    struct ly_ctx *ctx;

    /* prepared output (--output option or stdout by default) */
    struct ly_out *out;

    struct ly_set searchpaths;

    /* options flags */
    uint8_t list;        /* -l option to print list of schemas */

    /* line length for 'tree' format */
    size_t line_length; /* --tree-line-length */

    /*
     * schema
     */
    /* set schema modules' features via --features option (struct schema_features *) */
    struct ly_set schema_features;

    /* set of loaded schema modules (struct lys_module *) */
    struct ly_set schema_modules;

    /* options to parse and print schema modules */
    uint32_t schema_parse_options;
    uint32_t schema_print_options;

    /* specification of printing schema node subtree, option --schema-node */
    const char *schema_node_path;
    const struct lysc_node *schema_node;
    const char *submodule;

    /* value of --format in case of schema format */
    LYS_OUTFORMAT schema_out_format;

    /*
     * data
     */
    /* various options based on --type option */
    enum lyd_type data_type;
    uint32_t data_parse_options;
    uint32_t data_validate_options;
    uint32_t data_print_options;

    /* flag for --merge option */
    uint8_t data_merge;

    /* value of --format in case of data format */
    LYD_FORMAT data_out_format;

    /* input data files (struct cmdline_file *) */
    struct ly_set data_inputs;

    /* storage for --operational */
    struct cmdline_file data_operational;

    /* storage for --reply-rpc */
    struct cmdline_file reply_rpc;
};

static void
erase_context(struct context *c)
{
    /* data */
    ly_set_erase(&c->data_inputs, free_cmdline_file);
    ly_in_free(c->data_operational.in, 1);

    /* schema */
    ly_set_erase(&c->schema_features, free_features);
    ly_set_erase(&c->schema_modules, NULL);

    /* context */
    ly_set_erase(&c->searchpaths, NULL);

    ly_out_free(c->out, NULL,  0);
    ly_ctx_destroy(c->ctx);
}

static void
version(void)
{
    printf("yanglint %s\n", PROJECT_VERSION);
}

static void
help(int shortout)
{

    printf("Example usage:\n"
            "    yanglint [-f { yang | yin | info}] <schema>...\n"
            "        Validates the YANG module <schema>(s) and all its dependencies, optionally printing\n"
            "        them in the specified format.\n\n"
            "    yanglint [-f { xml | json }] <schema>... <file>...\n"
            "        Validates the YANG modeled data <file>(s) according to the <schema>(s) optionally\n"
            "        printing them in the specified format.\n\n"
            "    yanglint -t (nc-)rpc/notif [-O <operational-file>] <schema>... <file>\n"
            "        Validates the YANG/NETCONF RPC/notification <file> according to the <schema>(s) using\n"
            "        <operational-file> with possible references to the operational datastore data.\n\n"
            "    yanglint -t nc-reply -R <rpc-file> [-O <operational-file>] <schema>... <file>\n"
            "        Validates the NETCONF rpc-reply <file> of RPC <rpc-file> according to the <schema>(s)\n"
            "        using <operational-file> with possible references to the operational datastore data.\n\n"
            "    yanglint\n"
            "        Starts interactive mode with more features.\n\n");

    if (shortout) {
        return;
    }
    printf("Options:\n"
            "  -h, --help    Show this help message and exit.\n"
            "  -v, --version Show version number and exit.\n"
            "  -V, --verbose Increase libyang verbosity and show verbose messages. If specified\n"
            "                a second time, show even debug messages.\n"
            "  -Q, --quiet   Decrease libyang verbosity and hide warnings. If specified a second\n"
            "                time, hide errors so no libyang messages are printed.\n");

    printf("  -f FORMAT, --format=FORMAT\n"
            "                Convert input into FORMAT. Supported formats: \n"
            "                yang, yin, tree and info for schemas,\n"
            "                xml, json, and lyb for data.\n\n");

    printf("  -p PATH, --path=PATH\n"
            "                Search path for schema (YANG/YIN) modules. The option can be\n"
            "                used multiple times. The current working directory and the\n"
            "                path of the module being added is used implicitly.\n\n");

    printf("  -D, --disable-searchdir\n"
            "                Do not implicitly search in current working directory for\n"
            "                schema modules. If specified a second time, do not even\n"
            "                search in the module directory (all modules must be \n"
            "                explicitly specified).\n\n");

    printf("  -F FEATURES, --features=FEATURES\n"
            "                Specific module features to support in the form <module-name>:(<feature>,)*\n"
            "                Use <feature> '*' to enable all features of a module. This option can be\n"
            "                specified multiple times, to enable features in multiple modules. If this\n"
            "                option is not specified, all the features in all the implemented modules\n"
            "                are enabled.\n\n");

    printf("  -i, --make-implemented\n"
            "                Make the imported modules \"referenced\" from any loaded\n"
            "                module also implemented. If specified a second time, all the\n"
            "                modules are set implemented.\n\n");

    printf("  -P PATH, --schema-node=PATH\n"
            "                Print only the specified subtree of the schema.\n"
            "                The PATH is the XPath subset mentioned in documentation as\n"
            "                the Path format. The option can be combined with --single-node\n"
            "                option to print information only about the specified node.\n"
            "  -q, --single-node\n"
            "                Supplement to the --schema-node option to print information\n"
            "                only about a single node specified as PATH argument.\n\n");

    printf("  -s SUBMODULE, --submodule=SUBMODULE\n"
            "                Print the specific submodule instead of the main module.\n\n");

    printf("  -n, --not-strict\n"
            "                Do not require strict data parsing (silently skip unknown data),\n"
            "                has no effect for schemas.\n\n");

    printf("  -e, --present Validate only with the schema modules whose data actually\n"
            "                exist in the provided input data files. Takes effect only\n"
            "                with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
            "                mandatory nodes from modules which data are not present in the\n"
            "                provided input data files.\n\n");

    printf("  -t TYPE, --type=TYPE\n"
            "                Specify data tree type in the input data file(s):\n"
            "        data          - Complete datastore with status data (default type).\n"
            "        config        - Configuration datastore (without status data).\n"
            "        get           - Data returned by the NETCONF <get> operation.\n"
            "        getconfig     - Data returned by the NETCONF <get-config> operation.\n"
            "        edit          - Config content of the NETCONF <edit-config> operation.\n"
            "        rpc           - Invocation of a YANG RPC/action, defined as input.\n"
            "        nc-rpc        - Similar to 'rpc' but expect and check also the NETCONF\n"
            "                        envelopes <rpc> or <action>.\n"
            "        reply         - Reply to a YANG RPC/action, defined as output. Note that\n"
            "                        the reply data are expected inside a container representing\n"
            "                        the original RPC/action invocation.\n"
            "        nc-reply      - Similar to 'reply' but expect and check also the NETCONF\n"
            "                        envelope <rpc-reply> with output data nodes as direct\n"
            "                        descendants. The original RPC/action invocation is expected\n"
            "                        in a separate parameter '-R' and is parsed as 'nc-rpc'.\n"
            "        notif         - Notification instance of a YANG notification.\n"
            "        nc-notif      - Similar to 'notif' but expect and check also the NETCONF\n"
            "                        envelope <notification> with element <eventTime> and its\n"
            "                        sibling as the actual notification.\n\n");

    printf("  -d MODE, --default=MODE\n"
            "                Print data with default values, according to the MODE\n"
            "                (to print attributes, ietf-netconf-with-defaults model\n"
            "                must be loaded):\n"
            "      all             - Add missing default nodes.\n"
            "      all-tagged      - Add missing default nodes and mark all the default\n"
            "                        nodes with the attribute.\n"
            "      trim            - Remove all nodes with a default value.\n"
            "      implicit-tagged - Add missing nodes and mark them with the attribute.\n\n");

    printf("  -l, --list    Print info about the loaded schemas.\n"
            "                (i - imported module, I - implemented module)\n"
            "                In case the '-f' option with data encoding is specified,\n"
            "                the list is printed as \"ietf-yang-library\" data.\n\n");

    printf("  -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n"
            "                The limit of the maximum line length on which the 'tree'\n"
            "                format will try to be printed.\n\n");

    printf("  -o OUTFILE, --output=OUTFILE\n"
            "                Write the output to OUTFILE instead of stdout.\n\n");

    printf("  -O FILE, --operational=FILE\n"
            "                Provide optional data to extend validation of the '(nc-)rpc',\n"
            "                '(nc-)reply' or '(nc-)notif' TYPEs. The FILE is supposed to contain\n"
            "                the operational datastore referenced from the operation.\n"
            "                In case of a nested operation, its parent existence is also\n"
            "                checked in these operational data.\n\n");

    printf("  -R FILE, --reply-rpc=FILE\n"
            "                Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n"
            "                is supposed to contain the source 'nc-rpc' operation of the reply.\n\n");

    printf("  -m, --merge   Merge input data files into a single tree and validate at\n"
            "                once. The option has effect only for 'data' and 'config' TYPEs.\n\n");

    printf("  -y, --yang-library\n"
            "                Load and implement internal \"ietf-yang-library\" YANG module.\n"
            "                Note that this module includes definitions of mandatory state\n"
    struct option options[] = {
#if 0
        {"auto",             no_argument,       NULL, 'a'},
        {"default",          required_argument, NULL, 'd'},
#endif
        {"format",           required_argument, NULL, 'f'},
        {"features",         required_argument, NULL, 'F'},
#if 0
    printf("  -G GROUPS, --debug=GROUPS\n"
            "                Enable printing of specific debugging message group\n"
            "                (nothing will be printed unless verbosity is set to debug):\n"
            "                <group>[,<group>]* (dict, xpath, dep-sets)\n\n");
#endif
}

static void
libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
{
    char *levstr;

    switch (level) {
    case LY_LLERR:
        levstr = "err :";
        break;
    case LY_LLWRN:
        levstr = "warn:";
        break;
    case LY_LLVRB:
        levstr = "verb:";
        break;
    default:
        levstr = "dbg :";
        break;
    }
    if (path) {
        fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
    } else {
        fprintf(stderr, "libyang %s %s\n", levstr, msg);
    }
}

static struct schema_features *
get_features_not_applied(const struct ly_set *fset)
{
    for (uint32_t u = 0; u < fset->count; ++u) {
        struct schema_features *sf = fset->objs[u];
        if (!sf->applied) {
            return sf;
        }
    }

    return NULL;
}

static int
fill_context_inputs(int argc, char *argv[], struct context *c)
    int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;
    int autodetection = 0, options_parser = 0, merge = 0;
    const char *oper_file = NULL;
#if 0
    const char *envelope_s = NULL;
    char *ylpath = NULL;
    int options_dflt = 0, envelope = 0;
    struct lyxml_elem *iter, *elem;
    struct *subroot, *next, *node;
#endif
        /* ignore features */
        ly_set_erase(&c->schema_features, free_features);

        /* create context from the yang-library file */
        if (ly_ctx_new_ylpath(searchdir, c->yang_lib_file, LYD_UNKNOWN, c->ctx_options, &c->ctx)) {
            YLMSG_E("Unable to create libyang context from yang-library data.\n");
            return -1;
        }
    } else {
        /* set imp feature flag if all should be enabled */
        if (!c->schema_features.count) {
            c->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES;
        }

        /* libyang context */
        if (ly_ctx_new(searchdir, c->ctx_options, &c->ctx)) {
            YLMSG_E("Unable to create libyang context.\n");
    {
        switch (opt) {
#if 0
        case 'a':
            envelope = 1;
            break;
        case 'd':
            if (!strcmp(optarg, "all")) {
                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;
            } else if (!strcmp(optarg, "all-tagged")) {
                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
            } else if (!strcmp(optarg, "trim")) {
                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;
            } else if (!strcmp(optarg, "implicit-tagged")) {
                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
            } else {
                fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg);
                help(1);
                goto cleanup;
            }
            break;
#endif
        case 'f':
            if (!strcasecmp(optarg, "yang")) {
                outformat_s = LYS_OUT_YANG;
            goto error;
        }

        if (format_schema) {
            LY_ERR ret;
            uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
            const char **features;
            struct lys_module *mod;

            /* parse the input */
            if (parse_schema_path(argv[optind + i], &dir, &module)) {
                goto error;
            }

            if (c->yang_lib_file) {
                /* just get the module, it should already be parsed */
                mod = ly_ctx_get_module_implemented(c->ctx, module);
                if (!mod) {
                    YLMSG_E("Schema module \"%s\" not implemented in yang-library data.\n", module);
                    goto error;
                }
            } else {
                /* add temporarily also the path of the module itself */
                if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {
                    path_unset = 0;
                }

                /* get features list for this module */
                if (!c->schema_features.count) {
                    features = all_features;
                } else {
                    get_features(&c->schema_features, module, &features);
                }

                /* parse module */
                ret = lys_parse(c->ctx, in, format_schema, features, &mod);
                ly_ctx_unset_searchdir_last(c->ctx, path_unset);
                if (ret) {
                    YLMSG_E("Parsing schema module \"%s\" failed.\n", argv[optind + i]);
                    goto error;
                }
            }

            /* temporary cleanup */
            free(dir);
            dir = NULL;
            free(module);
            module = NULL;

            if (c->schema_out_format) {
                /* module will be printed */
                if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
                    YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
                    goto error;
                }
            }
        } else if (format_data) {
            if (!fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data)) {
                goto error;
            }
            in = NULL;
        }

        ly_in_free(in, 1);
        in = NULL;
    }

    /* check that all specified features were applied, apply now if possible */
    while ((sf = get_features_not_applied(&c->schema_features))) {
        /* try to find implemented or the latest revision of this module */
        mod = ly_ctx_get_module_implemented(c->ctx, sf->mod_name);
        if (!mod) {
            mod = ly_ctx_get_module_latest(c->ctx, sf->mod_name);
        }
        if (!mod) {
            YLMSG_E("Specified features not applied, module \"%s\" not loaded.\n", sf->mod_name);
            goto error;
        }

        /* we have the module, implement it if needed and enable the specific features */
        if (lys_set_implemented(mod, (const char **)sf->features)) {
            YLMSG_E("Implementing module \"%s\" failed.\n", mod->name);
            goto error;
        }
        sf->applied = 1;
    }

    return 0;

error:
    ly_in_free(in, 1);
    free(dir);
    free(module);
    return -1;
}

/**
 * @brief Process command line options and store the settings into the context.
 *
 * return -1 in case of error;
 * return 0 in case of success and ready to process
 * return 1 in case of success, but expect to exit.
 */
static int
fill_context(int argc, char *argv[], struct context *c)
{
    int ret;

    int opt, opt_index;
    struct option options[] = {
        {"help",              no_argument,       NULL, 'h'},
        {"version",           no_argument,       NULL, 'v'},
        {"verbose",           no_argument,       NULL, 'V'},
        {"quiet",             no_argument,       NULL, 'Q'},
        {"format",            required_argument, NULL, 'f'},
        {"path",              required_argument, NULL, 'p'},
        {"disable-searchdir", no_argument,       NULL, 'D'},
        {"features",          required_argument, NULL, 'F'},
        {"make-implemented",  no_argument,       NULL, 'i'},
        {"schema-node",       required_argument, NULL, 'P'},
        {"single-node",       no_argument,       NULL, 'q'},
        {"submodule",         required_argument, NULL, 's'},
        {"not-strict",        no_argument,       NULL, 'n'},
        {"present",           no_argument,       NULL, 'e'},
        {"type",              required_argument, NULL, 't'},
        {"default",           required_argument, NULL, 'd'},
        {"list",              no_argument,       NULL, 'l'},
        {"tree-line-length",  required_argument, NULL, 'L'},
        {"output",            required_argument, NULL, 'o'},
        {"operational",       required_argument, NULL, 'O'},
        {"reply-rpc",         required_argument, NULL, 'R'},
        {"merge",             no_argument,       NULL, 'm'},
        {"yang-library",      no_argument,       NULL, 'y'},
        {"yang-library-file", required_argument, NULL, 'Y'},
#ifndef NDEBUG
        {"debug",            required_argument, NULL, 'G'},
#endif
        {NULL,               0,                 NULL, 0}
    };
    uint8_t data_type_set = 0;

    c->ctx_options = YL_DEFAULT_CTX_OPTIONS;
    c->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;
    c->line_length = 0;

    opterr = 0;
#ifndef NDEBUG
    while ((opt = getopt_long(argc, argv, "hvVQf:p:DF:iP:qs:net:d:lL:o:O:R:myY:G:", options, &opt_index)) != -1)
#else
    while ((opt = getopt_long(argc, argv, "hvVQf:p:DF:iP:qs:net:d:lL:o:O:R:myY:", options, &opt_index)) != -1)
#endif
    {
        switch (opt) {
        case 'h': /* --help */
            help(0);
            return 1;

        case 'v': /* --version */
            version();
            return 1;

        case 'V': { /* --verbose */
            LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
            if (verbosity < LY_LLDBG) {
                ++verbosity;
            }
            ly_log_level(verbosity);
            break;
        } /* case 'V' */

        case 'Q': { /* --quiet */
            LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
            if (verbosity == LY_LLERR) {
                /* turn logging off */
                ly_log_options(LY_LOSTORE_LAST);
            } else if (verbosity > LY_LLERR) {
                --verbosity;
            }
            ly_log_level(verbosity);
            break;
        } /* case 'Q' */

        case 'f': /* --format */
            if (!strcasecmp(optarg, "yang")) {
                c->schema_out_format = LYS_OUT_YANG;
                c->data_out_format = 0;
            } else if (!strcasecmp(optarg, "yin")) {
                c->schema_out_format = LYS_OUT_YIN;
                c->data_out_format = 0;
            } else if (!strcasecmp(optarg, "info")) {
                c->schema_out_format = LYS_OUT_YANG_COMPILED;
                c->data_out_format = 0;
            } else if (!strcasecmp(optarg, "tree")) {
                c->schema_out_format = LYS_OUT_TREE;
                c->data_out_format = 0;
            } else if (!strcasecmp(optarg, "xml")) {
                c->schema_out_format = 0;
                c->data_out_format = LYD_XML;
            } else if (!strcasecmp(optarg, "json")) {
                c->schema_out_format = 0;
                c->data_out_format = LYD_JSON;
            } else if (!strcasecmp(optarg, "lyb")) {
                c->schema_out_format = 0;
                c->data_out_format = LYD_LYB;
            } else {
                YLMSG_E("Unknown output format %s\n", optarg);
                help(1);
                return -1;
            }
            break;

        case 'p': { /* --path */
            struct stat st;

            if (stat(optarg, &st) == -1) {
                YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
                return -1;
            }
            if (!S_ISDIR(st.st_mode)) {
                YLMSG_E("Provided search path is not a directory.\n");
                return -1;
            }

            if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
                YLMSG_E("Storing searchpath failed.\n");
                return -1;
            }

            break;
        } /* case 'p' */

        case 'D': /* --disable-search */
            if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
                YLMSG_W("The -D option specified too many times.\n");
            }
            if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) {
                c->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
                c->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS;
            } else {
                c->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD;
            }
            break;

        case 'F': /* --features */
            if (parse_features(optarg, &c->schema_features)) {
                return -1;
            }
            break;

        case 'i': /* --make-implemented */
            if (c->ctx_options & LY_CTX_REF_IMPLEMENTED) {
                c->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;
                c->ctx_options |= LY_CTX_ALL_IMPLEMENTED;
            } else {
                c->ctx_options |= LY_CTX_REF_IMPLEMENTED;
            }
            break;

        case 'P': /* --schema-node */
            c->schema_node_path = optarg;
            break;

        case 'q': /* --single-node */
            c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
            break;

        case 's': /* --submodule */
            c->submodule = optarg;
            break;
        }
#endif
    }
#if 0
    if (!outformat_d && options_dflt) {
        /* we have options for printing default nodes, but data output not specified */
        fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n");
    }
    if (outformat_s && (options_parser || autodetection)) {
        /* we have options for printing data tree, but output is schema */
        fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n");
                YLMSG_E("The data type (-t) cannot be set multiple times.\n");
                return -1;
            }

            if (!strcasecmp(optarg, "config")) {
                c->data_parse_options |= LYD_PARSE_NO_STATE;
                c->data_validate_options |= LYD_VALIDATE_NO_STATE;
            } else if (!strcasecmp(optarg, "get")) {
                c->data_parse_options |= LYD_PARSE_ONLY;
            } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
                c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
            } else if (!strcasecmp(optarg, "edit")) {
                c->data_parse_options |= LYD_PARSE_ONLY;
            } else if (!strcasecmp(optarg, "rpc")) {
                c->data_type = LYD_TYPE_RPC_YANG;
            } else if (!strcasecmp(optarg, "nc-rpc")) {
                c->data_type = LYD_TYPE_RPC_NETCONF;
            } else if (!strcasecmp(optarg, "reply")) {
                c->data_type = LYD_TYPE_REPLY_YANG;
            } else if (!strcasecmp(optarg, "nc-reply")) {
                c->data_type = LYD_TYPE_REPLY_NETCONF;
            } else if (!strcasecmp(optarg, "notif")) {
                c->data_type = LYD_TYPE_NOTIF_YANG;
            } else if (!strcasecmp(optarg, "nc-notif")) {
                c->data_type = LYD_TYPE_NOTIF_NETCONF;
            } else if (!strcasecmp(optarg, "data")) {
                /* default option */
            } else {
                YLMSG_E("Unknown data tree type %s\n", optarg);
                help(1);
                return -1;
            }

            data_type_set = 1;
            break;

        case 'd': /* --default */
            if (!strcasecmp(optarg, "all")) {
                c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
            } else if (!strcasecmp(optarg, "all-tagged")) {
                c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
            } else if (!strcasecmp(optarg, "trim")) {
                c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
            } else if (!strcasecmp(optarg, "implicit-tagged")) {
                c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
            } else {
                YLMSG_E("Unknown default mode %s\n", optarg);
                help(1);
                return -1;
            }
            break;

        case 'l': /* --list */
            c->list = 1;
            break;

        case 'L': /* --tree-line-length */
            c->line_length = atoi(optarg);
            break;

        case 'o': /* --output */
            if (c->out) {
                YLMSG_E("Only a single output can be specified.\n");
                return -1;
            } else {
                if (ly_out_new_filepath(optarg, &c->out)) {
                    YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
                    return -1;
                }
            }
            break;

        case 'O': /* --operational */
            if (c->data_operational.path) {
                YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
                return -1;
            }
            c->data_operational.path = optarg;
            break;

        case 'R': /* --reply-rpc */
            if (c->reply_rpc.path) {
                YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\n");
                return -1;
            }
            c->reply_rpc.path = optarg;
            break;

        case 'm': /* --merge */
            c->data_merge = 1;
            break;

        case 'y': /* --yang-library */
            c->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
            break;

        case 'Y': /* --yang-library-file */
            c->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
            c->yang_lib_file = optarg;
            break;

#ifndef NDEBUG
        case 'G': { /* --debug */
            uint32_t dbg_groups = 0;
            const char *ptr = optarg;

            while (ptr[0]) {
                if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
                    dbg_groups |= LY_LDGDICT;
                    ptr += sizeof "dict" - 1;
                } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
                    dbg_groups |= LY_LDGXPATH;
                    ptr += sizeof "xpath" - 1;
                } else if (!strncasecmp(ptr, "dep-sets", sizeof "dep-sets" - 1)) {
                    dbg_groups |= LY_LDGDEPSETS;
                    ptr += sizeof "dep-sets" - 1;
                }

                if (ptr[0]) {
                    if (ptr[0] != ',') {
                        YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
                        return -1;
                    }
                    ++ptr;
                }
            }
            ly_log_dbg_groups(dbg_groups);
            break;
        } /* case 'G' */
#endif
        default:
            YLMSG_E("Invalid option or missing argument: -%c\n", optopt);
            return -1;
        } /* switch */
    }

    /* additional checks for the options combinations */
    if (!c->list && (optind >= argc)) {
        help(1);
        YLMSG_E("Missing <schema> to process.\n");
        return 1;
    }

    if (c->data_merge) {
        if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
            /* switch off the option, incompatible input data type */
            c->data_merge = 0;
        } else {
            /* postpone validation after the merge of all the input data */
            c->data_parse_options |= LYD_PARSE_ONLY;
        }
    }

    if (c->data_operational.path && !c->data_type) {
        YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notification input data types.\n");
        c->data_operational.path = NULL;
    }

    if (c->reply_rpc.path && (c->data_type != LYD_TYPE_REPLY_NETCONF)) {
        YLMSG_E("Source RPC is needed only for NETCONF Reply input data type.\n");
        c->data_operational.path = NULL;
    } else if (!c->reply_rpc.path && (c->data_type == LYD_TYPE_REPLY_NETCONF)) {
        YLMSG_E("Missing source RPC (-R) for NETCONF Reply input data type.\n");
        return -1;
    }

    if ((c->schema_out_format != LYS_OUT_TREE) && c->line_length) {
        YLMSG_E("--tree-line-length take effect only in case of the tree output format.\n");
    }

    /* default output stream */
    if (!c->out) {
        if (ly_out_new_file(stdout, &c->out)) {
            YLMSG_E("Unable to set stdout as output.\n");
            return -1;
        }
    }

    if (c->schema_out_format == LYS_OUT_TREE) {
        /* print tree from lysc_nodes */
        c->ctx_options |= LY_CTX_SET_PRIV_PARSED;
    }

    /* process input files provided as standalone command line arguments,
     * schema modules are parsed and inserted into the context,
     * data files are just checked and prepared into internal structures for further processing */
    ret = fill_context_inputs(argc, argv, c);
    if (ret) {
        return ret;
    }

    /* the second batch of checks */
    if (c->schema_print_options && !c->schema_out_format) {
        YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
    }
    if (c->schema_parse_options && !c->schema_modules.count) {
        YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
    }
    if (c->data_print_options && !c->data_out_format) {
        YLMSG_W("data printer options specified, but the data output format is missing.\n");
    }
    if (((c->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || c->data_type) && !c->data_inputs.count) {
        YLMSG_W("Data parser options specified, but no data input file provided.\n");
    }

    if (c->schema_node_path) {
        c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
        if (!c->schema_node) {
            c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);

            if (!c->schema_node) {
                YLMSG_E("Invalid schema path.\n");
                return -1;
            }
        }
    }

    return 0;
}

int
main_ni(int argc, char *argv[])
{
    int ret = EXIT_SUCCESS, r;
    struct context c = {0};

    /* set callback for printing libyang messages */
    ly_set_log_clb(libyang_verbclb, 1);

    r = fill_context(argc, argv, &c);
    if (r < 0) {
        ret = EXIT_FAILURE;
    }
    if (r) {
        goto cleanup;
    }

    /* do the required job - parse, validate, print */

    if (c.list) {
        /* print the list of schemas */
        print_list(c.out, c.ctx, c.data_out_format);
    } else {
        if (c.schema_out_format) {
            if (c.schema_node) {
                ret = lys_print_node(c.out, c.schema_node, c.schema_out_format, 0, c.schema_print_options);
                if (ret) {
                    YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
                    goto cleanup;
                }
            } else if (c.submodule) {
                const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(c.ctx, c.submodule);
                if (!submod) {
                    YLMSG_E("Unable to find submodule %s.\n", c.submodule);
                    goto cleanup;
                }

                ret = lys_print_submodule(c.out, submod, c.schema_out_format, c.line_length, c.schema_print_options);
                if (ret) {
                    YLMSG_E("Unable to print submodule %s.\n", submod->name);
                    goto cleanup;
                }
            } else {
                for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
                    ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format,
                            c.line_length, c.schema_print_options);
                    /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
                    if ((c.schema_out_format == LYS_OUT_TREE) && (u + 1 < c.schema_modules.count)) {
                        ly_print(c.out, "\n");
                    }
                    if (ret) {
                        YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
                        goto cleanup;
                    }
                }
            }
        }

        /* do the data validation despite the schema was printed */
        if (c.data_inputs.size) {
            ret = process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out, c.data_parse_options,
                    c.data_validate_options, c.data_print_options, &c.data_operational, &c.reply_rpc, &c.data_inputs, NULL);
            if (ret) {
                goto cleanup;
            }
        }
    }

cleanup:
    /* cleanup */
    erase_context(&c);

    return ret;
}
                if (!out) {
                    ly_out_new_file(stdout, &out);
                }
                lyd_print_all(out, data_item->tree, outformat_d, 0 /* TODO defaults | options_dflt */);
#if 0
                if (envelope_s) {
                    if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {

<MSG> yanglint CHANGE handle default values modes

<DFF> @@ -264,8 +264,8 @@ main_ni(int argc, char* argv[])
     struct option options[] = {
 #if 0
         {"auto",             no_argument,       NULL, 'a'},
-        {"default",          required_argument, NULL, 'd'},
 #endif
+        {"default",          required_argument, NULL, 'd'},
         {"format",           required_argument, NULL, 'f'},
         {"features",         required_argument, NULL, 'F'},
 #if 0
@@ -320,10 +320,11 @@ main_ni(int argc, char* argv[])
     int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;
     int autodetection = 0, options_parser = 0, merge = 0;
     const char *oper_file = NULL;
+    int options_dflt = 0;
 #if 0
+    ly_bool envelope = 0;
     const char *envelope_s = NULL;
     char *ylpath = NULL;
-    int options_dflt = 0, envelope = 0;
     struct lyxml_elem *iter, *elem;
     struct *subroot, *next, *node;
 #endif
@@ -347,25 +348,26 @@ main_ni(int argc, char* argv[])
     {
         switch (opt) {
 #if 0
+        }
         case 'a':
             envelope = 1;
             break;
+#endif
         case 'd':
             if (!strcmp(optarg, "all")) {
-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;
+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
             } else if (!strcmp(optarg, "all-tagged")) {
-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
             } else if (!strcmp(optarg, "trim")) {
-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;
+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
             } else if (!strcmp(optarg, "implicit-tagged")) {
-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
             } else {
                 fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg);
                 help(1);
                 goto cleanup;
             }
             break;
-#endif
         case 'f':
             if (!strcasecmp(optarg, "yang")) {
                 outformat_s = LYS_OUT_YANG;
@@ -641,11 +643,11 @@ main_ni(int argc, char* argv[])
         }
 #endif
     }
-#if 0
     if (!outformat_d && options_dflt) {
         /* we have options for printing default nodes, but data output not specified */
         fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n");
     }
+#if 0
     if (outformat_s && (options_parser || autodetection)) {
         /* we have options for printing data tree, but output is schema */
         fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n");
@@ -1086,7 +1088,7 @@ parse_reply:
                 if (!out) {
                     ly_out_new_file(stdout, &out);
                 }
-                lyd_print_all(out, data_item->tree, outformat_d, 0 /* TODO defaults | options_dflt */);
+                lyd_print_all(out, data_item->tree, outformat_d, options_dflt);
 #if 0
                 if (envelope_s) {
                     if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {

{'commit': 'diff --git a/tools/lint/commands.c b/tools/lint/commands.c\nindex ab6f2059..9fced6de 100644\n--- a/tools/lint/commands.c\n+++ b/tools/lint/commands.c\n@@ -713,19 +713,17 @@ cmd_data(const char *arg)\n         }\n \n         switch (c) {\n-#if 0\n         case \'d\':\n             if (!strcmp(optarg, "all")) {\n-                printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL;\n+                printopt = (printopt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;\n             } else if (!strcmp(optarg, "all-tagged")) {\n-                printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;\n+                printopt = (printopt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;\n             } else if (!strcmp(optarg, "trim")) {\n-                printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM;\n+                printopt = (printopt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;\n             } else if (!strcmp(optarg, "implicit-tagged")) {\n-                printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;\n+                printopt = (printopt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;\n             }\n             break;\n-#endif\n         case \'h\':\n             cmd_data_help();\n             ret = 0;\ndiff --git a/tools/lint/main_ni.c b/tools/lint/main_ni.c\nindex 00ebd47f..7223495f 100644\n--- a/tools/lint/main_ni.c\n+++ b/tools/lint/main_ni.c\n@@ -264,8 +264,8 @@ main_ni(int argc, char* argv[])\n     struct option options[] = {\n #if 0\n         {"auto",             no_argument,       NULL, \'a\'},\n-        {"default",          required_argument, NULL, \'d\'},\n #endif\n+        {"default",          required_argument, NULL, \'d\'},\n         {"format",           required_argument, NULL, \'f\'},\n         {"features",         required_argument, NULL, \'F\'},\n #if 0\n@@ -320,10 +320,11 @@ main_ni(int argc, char* argv[])\n     int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;\n     int autodetection = 0, options_parser = 0, merge = 0;\n     const char *oper_file = NULL;\n+    int options_dflt = 0;\n #if 0\n+    ly_bool envelope = 0;\n     const char *envelope_s = NULL;\n     char *ylpath = NULL;\n-    int options_dflt = 0, envelope = 0;\n     struct lyxml_elem *iter, *elem;\n     struct *subroot, *next, *node;\n #endif\n@@ -347,25 +348,26 @@ main_ni(int argc, char* argv[])\n     {\n         switch (opt) {\n #if 0\n+        }\n         case \'a\':\n             envelope = 1;\n             break;\n+#endif\n         case \'d\':\n             if (!strcmp(optarg, "all")) {\n-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;\n+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;\n             } else if (!strcmp(optarg, "all-tagged")) {\n-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;\n+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;\n             } else if (!strcmp(optarg, "trim")) {\n-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;\n+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;\n             } else if (!strcmp(optarg, "implicit-tagged")) {\n-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;\n+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;\n             } else {\n                 fprintf(stderr, "yanglint error: unknown default mode %s\\n", optarg);\n                 help(1);\n                 goto cleanup;\n             }\n             break;\n-#endif\n         case \'f\':\n             if (!strcasecmp(optarg, "yang")) {\n                 outformat_s = LYS_OUT_YANG;\n@@ -641,11 +643,11 @@ main_ni(int argc, char* argv[])\n         }\n #endif\n     }\n-#if 0\n     if (!outformat_d && options_dflt) {\n         /* we have options for printing default nodes, but data output not specified */\n         fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\\n");\n     }\n+#if 0\n     if (outformat_s && (options_parser || autodetection)) {\n         /* we have options for printing data tree, but output is schema */\n         fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\\n");\n@@ -1086,7 +1088,7 @@ parse_reply:\n                 if (!out) {\n                     ly_out_new_file(stdout, &out);\n                 }\n-                lyd_print_all(out, data_item->tree, outformat_d, 0 /* TODO defaults | options_dflt */);\n+                lyd_print_all(out, data_item->tree, outformat_d, options_dflt);\n #if 0\n                 if (envelope_s) {\n                     if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {\n', 'commit_subject': 'yanglint CHANGE handle default values modes', 'repo_name': 'CESNET/libyang', 'committer': {'name': 'Radek Krejci', 'email': 'ac6bc358cf742cff4f5723566468a80fdae7adfe@cesnet.cz', 'time_sec': '1599131058', 'tz_offset': '120', 'date': {'seconds': '1599131058'}}, 'commit_tree': 'd00a7fa15407e002a8780edf5b3d8ab57ba5f094', 'commit_message': 'yanglint CHANGE handle default values modes\n', 'language_name': 'Shell', 'license': 'bsd-3-clause', 'hunks': '@@ -264,8 +264,8 @@ main_ni(int argc, char* argv[])\n     struct option options[] = {\n #if 0\n         {"auto",             no_argument,       NULL, \'a\'},\n-        {"default",          required_argument, NULL, \'d\'},\n #endif\n+        {"default",          required_argument, NULL, \'d\'},\n         {"format",           required_argument, NULL, \'f\'},\n         {"features",         required_argument, NULL, \'F\'},\n #if 0\n@@ -320,10 +320,11 @@ main_ni(int argc, char* argv[])\n     int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;\n     int autodetection = 0, options_parser = 0, merge = 0;\n     const char *oper_file = NULL;\n+    int options_dflt = 0;\n #if 0\n+    ly_bool envelope = 0;\n     const char *envelope_s = NULL;\n     char *ylpath = NULL;\n-    int options_dflt = 0, envelope = 0;\n     struct lyxml_elem *iter, *elem;\n     struct *subroot, *next, *node;\n #endif\n@@ -347,25 +348,26 @@ main_ni(int argc, char* argv[])\n     {\n         switch (opt) {\n #if 0\n+        }\n         case \'a\':\n             envelope = 1;\n             break;\n+#endif\n         case \'d\':\n             if (!strcmp(optarg, "all")) {\n-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;\n+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;\n             } else if (!strcmp(optarg, "all-tagged")) {\n-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;\n+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;\n             } else if (!strcmp(optarg, "trim")) {\n-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;\n+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;\n             } else if (!strcmp(optarg, "implicit-tagged")) {\n-                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;\n+                options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;\n             } else {\n                 fprintf(stderr, "yanglint error: unknown default mode %s\\n", optarg);\n                 help(1);\n                 goto cleanup;\n             }\n             break;\n-#endif\n         case \'f\':\n             if (!strcasecmp(optarg, "yang")) {\n                 outformat_s = LYS_OUT_YANG;\n@@ -641,11 +643,11 @@ main_ni(int argc, char* argv[])\n         }\n #endif\n     }\n-#if 0\n     if (!outformat_d && options_dflt) {\n         /* we have options for printing default nodes, but data output not specified */\n         fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\\n");\n     }\n+#if 0\n     if (outformat_s && (options_parser || autodetection)) {\n         /* we have options for printing data tree, but output is schema */\n         fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\\n");\n@@ -1086,7 +1088,7 @@ parse_reply:\n                 if (!out) {\n                     ly_out_new_file(stdout, &out);\n                 }\n-                lyd_print_all(out, data_item->tree, outformat_d, 0 /* TODO defaults | options_dflt */);\n+                lyd_print_all(out, data_item->tree, outformat_d, options_dflt);\n #if 0\n                 if (envelope_s) {\n                     if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {\n', 'addition_count': 11, 'deletion_count': 9, 'src_file': 'a/tools/lint/main_ni.c', 'tgt_file': 'b/tools/lint/main_ni.c', 'file_extension': '.c', 'before_file': '/**\n * @file main_ni.c\n * @author Radek Krejci <rkrejci@cesnet.cz>\n * @brief libyang\'s yanglint tool - noninteractive code\n *\n * Copyright (c) 2020 CESNET, z.s.p.o.\n *\n * This source code is licensed under BSD 3-Clause License (the "License").\n * You may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     https://opensource.org/licenses/BSD-3-Clause\n */\n\n#define _GNU_SOURCE\n\n#include <errno.h>\n#include <getopt.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <strings.h>\n#include <sys/stat.h>\n\n#include "libyang.h"\n\n#include "common.h"\n#include "out.h"\n#include "tools/config.h"\n\n/**\n * @brief Context structure to hold and pass variables in a structured form.\n */\nstruct context {\n    /* libyang context for the run */\n    const char *yang_lib_file;\n    uint16_t ctx_options;\n    struct ly_ctx *ctx;\n\n    /* prepared output (--output option or stdout by default) */\n    struct ly_out *out;\n\n    struct ly_set searchpaths;\n\n    /* options flags */\n    uint8_t list;        /* -l option to print list of schemas */\n\n    /* line length for \'tree\' format */\n    size_t line_length; /* --tree-line-length */\n\n    /*\n     * schema\n     */\n    /* set schema modules\' features via --features option (struct schema_features *) */\n    struct ly_set schema_features;\n\n    /* set of loaded schema modules (struct lys_module *) */\n    struct ly_set schema_modules;\n\n    /* options to parse and print schema modules */\n    uint32_t schema_parse_options;\n    uint32_t schema_print_options;\n\n    /* specification of printing schema node subtree, option --schema-node */\n    const char *schema_node_path;\n    const struct lysc_node *schema_node;\n    const char *submodule;\n\n    /* value of --format in case of schema format */\n    LYS_OUTFORMAT schema_out_format;\n\n    /*\n     * data\n     */\n    /* various options based on --type option */\n    enum lyd_type data_type;\n    uint32_t data_parse_options;\n    uint32_t data_validate_options;\n    uint32_t data_print_options;\n\n    /* flag for --merge option */\n    uint8_t data_merge;\n\n    /* value of --format in case of data format */\n    LYD_FORMAT data_out_format;\n\n    /* input data files (struct cmdline_file *) */\n    struct ly_set data_inputs;\n\n    /* storage for --operational */\n    struct cmdline_file data_operational;\n\n    /* storage for --reply-rpc */\n    struct cmdline_file reply_rpc;\n};\n\nstatic void\nerase_context(struct context *c)\n{\n    /* data */\n    ly_set_erase(&c->data_inputs, free_cmdline_file);\n    ly_in_free(c->data_operational.in, 1);\n\n    /* schema */\n    ly_set_erase(&c->schema_features, free_features);\n    ly_set_erase(&c->schema_modules, NULL);\n\n    /* context */\n    ly_set_erase(&c->searchpaths, NULL);\n\n    ly_out_free(c->out, NULL,  0);\n    ly_ctx_destroy(c->ctx);\n}\n\nstatic void\nversion(void)\n{\n    printf("yanglint %s\\n", PROJECT_VERSION);\n}\n\nstatic void\nhelp(int shortout)\n{\n\n    printf("Example usage:\\n"\n            "    yanglint [-f { yang | yin | info}] <schema>...\\n"\n            "        Validates the YANG module <schema>(s) and all its dependencies, optionally printing\\n"\n            "        them in the specified format.\\n\\n"\n            "    yanglint [-f { xml | json }] <schema>... <file>...\\n"\n            "        Validates the YANG modeled data <file>(s) according to the <schema>(s) optionally\\n"\n            "        printing them in the specified format.\\n\\n"\n            "    yanglint -t (nc-)rpc/notif [-O <operational-file>] <schema>... <file>\\n"\n            "        Validates the YANG/NETCONF RPC/notification <file> according to the <schema>(s) using\\n"\n            "        <operational-file> with possible references to the operational datastore data.\\n\\n"\n            "    yanglint -t nc-reply -R <rpc-file> [-O <operational-file>] <schema>... <file>\\n"\n            "        Validates the NETCONF rpc-reply <file> of RPC <rpc-file> according to the <schema>(s)\\n"\n            "        using <operational-file> with possible references to the operational datastore data.\\n\\n"\n            "    yanglint\\n"\n            "        Starts interactive mode with more features.\\n\\n");\n\n    if (shortout) {\n        return;\n    }\n    printf("Options:\\n"\n            "  -h, --help    Show this help message and exit.\\n"\n            "  -v, --version Show version number and exit.\\n"\n            "  -V, --verbose Increase libyang verbosity and show verbose messages. If specified\\n"\n            "                a second time, show even debug messages.\\n"\n            "  -Q, --quiet   Decrease libyang verbosity and hide warnings. If specified a second\\n"\n            "                time, hide errors so no libyang messages are printed.\\n");\n\n    printf("  -f FORMAT, --format=FORMAT\\n"\n            "                Convert input into FORMAT. Supported formats: \\n"\n            "                yang, yin, tree and info for schemas,\\n"\n            "                xml, json, and lyb for data.\\n\\n");\n\n    printf("  -p PATH, --path=PATH\\n"\n            "                Search path for schema (YANG/YIN) modules. The option can be\\n"\n            "                used multiple times. The current working directory and the\\n"\n            "                path of the module being added is used implicitly.\\n\\n");\n\n    printf("  -D, --disable-searchdir\\n"\n            "                Do not implicitly search in current working directory for\\n"\n            "                schema modules. If specified a second time, do not even\\n"\n            "                search in the module directory (all modules must be \\n"\n            "                explicitly specified).\\n\\n");\n\n    printf("  -F FEATURES, --features=FEATURES\\n"\n            "                Specific module features to support in the form <module-name>:(<feature>,)*\\n"\n            "                Use <feature> \'*\' to enable all features of a module. This option can be\\n"\n            "                specified multiple times, to enable features in multiple modules. If this\\n"\n            "                option is not specified, all the features in all the implemented modules\\n"\n            "                are enabled.\\n\\n");\n\n    printf("  -i, --make-implemented\\n"\n            "                Make the imported modules \\"referenced\\" from any loaded\\n"\n            "                module also implemented. If specified a second time, all the\\n"\n            "                modules are set implemented.\\n\\n");\n\n    printf("  -P PATH, --schema-node=PATH\\n"\n            "                Print only the specified subtree of the schema.\\n"\n            "                The PATH is the XPath subset mentioned in documentation as\\n"\n            "                the Path format. The option can be combined with --single-node\\n"\n            "                option to print information only about the specified node.\\n"\n            "  -q, --single-node\\n"\n            "                Supplement to the --schema-node option to print information\\n"\n            "                only about a single node specified as PATH argument.\\n\\n");\n\n    printf("  -s SUBMODULE, --submodule=SUBMODULE\\n"\n            "                Print the specific submodule instead of the main module.\\n\\n");\n\n    printf("  -n, --not-strict\\n"\n            "                Do not require strict data parsing (silently skip unknown data),\\n"\n            "                has no effect for schemas.\\n\\n");\n\n    printf("  -e, --present Validate only with the schema modules whose data actually\\n"\n            "                exist in the provided input data files. Takes effect only\\n"\n            "                with the \'data\' or \'config\' TYPEs. Used to avoid requiring\\n"\n            "                mandatory nodes from modules which data are not present in the\\n"\n            "                provided input data files.\\n\\n");\n\n    printf("  -t TYPE, --type=TYPE\\n"\n            "                Specify data tree type in the input data file(s):\\n"\n            "        data          - Complete datastore with status data (default type).\\n"\n            "        config        - Configuration datastore (without status data).\\n"\n            "        get           - Data returned by the NETCONF <get> operation.\\n"\n            "        getconfig     - Data returned by the NETCONF <get-config> operation.\\n"\n            "        edit          - Config content of the NETCONF <edit-config> operation.\\n"\n            "        rpc           - Invocation of a YANG RPC/action, defined as input.\\n"\n            "        nc-rpc        - Similar to \'rpc\' but expect and check also the NETCONF\\n"\n            "                        envelopes <rpc> or <action>.\\n"\n            "        reply         - Reply to a YANG RPC/action, defined as output. Note that\\n"\n            "                        the reply data are expected inside a container representing\\n"\n            "                        the original RPC/action invocation.\\n"\n            "        nc-reply      - Similar to \'reply\' but expect and check also the NETCONF\\n"\n            "                        envelope <rpc-reply> with output data nodes as direct\\n"\n            "                        descendants. The original RPC/action invocation is expected\\n"\n            "                        in a separate parameter \'-R\' and is parsed as \'nc-rpc\'.\\n"\n            "        notif         - Notification instance of a YANG notification.\\n"\n            "        nc-notif      - Similar to \'notif\' but expect and check also the NETCONF\\n"\n            "                        envelope <notification> with element <eventTime> and its\\n"\n            "                        sibling as the actual notification.\\n\\n");\n\n    printf("  -d MODE, --default=MODE\\n"\n            "                Print data with default values, according to the MODE\\n"\n            "                (to print attributes, ietf-netconf-with-defaults model\\n"\n            "                must be loaded):\\n"\n            "      all             - Add missing default nodes.\\n"\n            "      all-tagged      - Add missing default nodes and mark all the default\\n"\n            "                        nodes with the attribute.\\n"\n            "      trim            - Remove all nodes with a default value.\\n"\n            "      implicit-tagged - Add missing nodes and mark them with the attribute.\\n\\n");\n\n    printf("  -l, --list    Print info about the loaded schemas.\\n"\n            "                (i - imported module, I - implemented module)\\n"\n            "                In case the \'-f\' option with data encoding is specified,\\n"\n            "                the list is printed as \\"ietf-yang-library\\" data.\\n\\n");\n\n    printf("  -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\\n"\n            "                The limit of the maximum line length on which the \'tree\'\\n"\n            "                format will try to be printed.\\n\\n");\n\n    printf("  -o OUTFILE, --output=OUTFILE\\n"\n            "                Write the output to OUTFILE instead of stdout.\\n\\n");\n\n    printf("  -O FILE, --operational=FILE\\n"\n            "                Provide optional data to extend validation of the \'(nc-)rpc\',\\n"\n            "                \'(nc-)reply\' or \'(nc-)notif\' TYPEs. The FILE is supposed to contain\\n"\n            "                the operational datastore referenced from the operation.\\n"\n            "                In case of a nested operation, its parent existence is also\\n"\n            "                checked in these operational data.\\n\\n");\n\n    printf("  -R FILE, --reply-rpc=FILE\\n"\n            "                Provide source RPC for parsing of the \'nc-reply\' TYPE. The FILE\\n"\n            "                is supposed to contain the source \'nc-rpc\' operation of the reply.\\n\\n");\n\n    printf("  -m, --merge   Merge input data files into a single tree and validate at\\n"\n            "                once. The option has effect only for \'data\' and \'config\' TYPEs.\\n\\n");\n\n    printf("  -y, --yang-library\\n"\n            "                Load and implement internal \\"ietf-yang-library\\" YANG module.\\n"\n            "                Note that this module includes definitions of mandatory state\\n"\n    struct option options[] = {\n#if 0\n        {"auto",             no_argument,       NULL, \'a\'},\n        {"default",          required_argument, NULL, \'d\'},\n#endif\n        {"format",           required_argument, NULL, \'f\'},\n        {"features",         required_argument, NULL, \'F\'},\n#if 0\n    printf("  -G GROUPS, --debug=GROUPS\\n"\n            "                Enable printing of specific debugging message group\\n"\n            "                (nothing will be printed unless verbosity is set to debug):\\n"\n            "                <group>[,<group>]* (dict, xpath, dep-sets)\\n\\n");\n#endif\n}\n\nstatic void\nlibyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)\n{\n    char *levstr;\n\n    switch (level) {\n    case LY_LLERR:\n        levstr = "err :";\n        break;\n    case LY_LLWRN:\n        levstr = "warn:";\n        break;\n    case LY_LLVRB:\n        levstr = "verb:";\n        break;\n    default:\n        levstr = "dbg :";\n        break;\n    }\n    if (path) {\n        fprintf(stderr, "libyang %s %s (%s)\\n", levstr, msg, path);\n    } else {\n        fprintf(stderr, "libyang %s %s\\n", levstr, msg);\n    }\n}\n\nstatic struct schema_features *\nget_features_not_applied(const struct ly_set *fset)\n{\n    for (uint32_t u = 0; u < fset->count; ++u) {\n        struct schema_features *sf = fset->objs[u];\n        if (!sf->applied) {\n            return sf;\n        }\n    }\n\n    return NULL;\n}\n\nstatic int\nfill_context_inputs(int argc, char *argv[], struct context *c)\n    int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;\n    int autodetection = 0, options_parser = 0, merge = 0;\n    const char *oper_file = NULL;\n#if 0\n    const char *envelope_s = NULL;\n    char *ylpath = NULL;\n    int options_dflt = 0, envelope = 0;\n    struct lyxml_elem *iter, *elem;\n    struct *subroot, *next, *node;\n#endif\n        /* ignore features */\n        ly_set_erase(&c->schema_features, free_features);\n\n        /* create context from the yang-library file */\n        if (ly_ctx_new_ylpath(searchdir, c->yang_lib_file, LYD_UNKNOWN, c->ctx_options, &c->ctx)) {\n            YLMSG_E("Unable to create libyang context from yang-library data.\\n");\n            return -1;\n        }\n    } else {\n        /* set imp feature flag if all should be enabled */\n        if (!c->schema_features.count) {\n            c->ctx_options |= LY_CTX_ENABLE_IMP_FEATURES;\n        }\n\n        /* libyang context */\n        if (ly_ctx_new(searchdir, c->ctx_options, &c->ctx)) {\n            YLMSG_E("Unable to create libyang context.\\n");\n    {\n        switch (opt) {\n#if 0\n        case \'a\':\n            envelope = 1;\n            break;\n        case \'d\':\n            if (!strcmp(optarg, "all")) {\n                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;\n            } else if (!strcmp(optarg, "all-tagged")) {\n                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;\n            } else if (!strcmp(optarg, "trim")) {\n                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;\n            } else if (!strcmp(optarg, "implicit-tagged")) {\n                options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;\n            } else {\n                fprintf(stderr, "yanglint error: unknown default mode %s\\n", optarg);\n                help(1);\n                goto cleanup;\n            }\n            break;\n#endif\n        case \'f\':\n            if (!strcasecmp(optarg, "yang")) {\n                outformat_s = LYS_OUT_YANG;\n            goto error;\n        }\n\n        if (format_schema) {\n            LY_ERR ret;\n            uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */\n            const char **features;\n            struct lys_module *mod;\n\n            /* parse the input */\n            if (parse_schema_path(argv[optind + i], &dir, &module)) {\n                goto error;\n            }\n\n            if (c->yang_lib_file) {\n                /* just get the module, it should already be parsed */\n                mod = ly_ctx_get_module_implemented(c->ctx, module);\n                if (!mod) {\n                    YLMSG_E("Schema module \\"%s\\" not implemented in yang-library data.\\n", module);\n                    goto error;\n                }\n            } else {\n                /* add temporarily also the path of the module itself */\n                if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {\n                    path_unset = 0;\n                }\n\n                /* get features list for this module */\n                if (!c->schema_features.count) {\n                    features = all_features;\n                } else {\n                    get_features(&c->schema_features, module, &features);\n                }\n\n                /* parse module */\n                ret = lys_parse(c->ctx, in, format_schema, features, &mod);\n                ly_ctx_unset_searchdir_last(c->ctx, path_unset);\n                if (ret) {\n                    YLMSG_E("Parsing schema module \\"%s\\" failed.\\n", argv[optind + i]);\n                    goto error;\n                }\n            }\n\n            /* temporary cleanup */\n            free(dir);\n            dir = NULL;\n            free(module);\n            module = NULL;\n\n            if (c->schema_out_format) {\n                /* module will be printed */\n                if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {\n                    YLMSG_E("Storing parsed schema module (%s) for print failed.\\n", argv[optind + i]);\n                    goto error;\n                }\n            }\n        } else if (format_data) {\n            if (!fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data)) {\n                goto error;\n            }\n            in = NULL;\n        }\n\n        ly_in_free(in, 1);\n        in = NULL;\n    }\n\n    /* check that all specified features were applied, apply now if possible */\n    while ((sf = get_features_not_applied(&c->schema_features))) {\n        /* try to find implemented or the latest revision of this module */\n        mod = ly_ctx_get_module_implemented(c->ctx, sf->mod_name);\n        if (!mod) {\n            mod = ly_ctx_get_module_latest(c->ctx, sf->mod_name);\n        }\n        if (!mod) {\n            YLMSG_E("Specified features not applied, module \\"%s\\" not loaded.\\n", sf->mod_name);\n            goto error;\n        }\n\n        /* we have the module, implement it if needed and enable the specific features */\n        if (lys_set_implemented(mod, (const char **)sf->features)) {\n            YLMSG_E("Implementing module \\"%s\\" failed.\\n", mod->name);\n            goto error;\n        }\n        sf->applied = 1;\n    }\n\n    return 0;\n\nerror:\n    ly_in_free(in, 1);\n    free(dir);\n    free(module);\n    return -1;\n}\n\n/**\n * @brief Process command line options and store the settings into the context.\n *\n * return -1 in case of error;\n * return 0 in case of success and ready to process\n * return 1 in case of success, but expect to exit.\n */\nstatic int\nfill_context(int argc, char *argv[], struct context *c)\n{\n    int ret;\n\n    int opt, opt_index;\n    struct option options[] = {\n        {"help",              no_argument,       NULL, \'h\'},\n        {"version",           no_argument,       NULL, \'v\'},\n        {"verbose",           no_argument,       NULL, \'V\'},\n        {"quiet",             no_argument,       NULL, \'Q\'},\n        {"format",            required_argument, NULL, \'f\'},\n        {"path",              required_argument, NULL, \'p\'},\n        {"disable-searchdir", no_argument,       NULL, \'D\'},\n        {"features",          required_argument, NULL, \'F\'},\n        {"make-implemented",  no_argument,       NULL, \'i\'},\n        {"schema-node",       required_argument, NULL, \'P\'},\n        {"single-node",       no_argument,       NULL, \'q\'},\n        {"submodule",         required_argument, NULL, \'s\'},\n        {"not-strict",        no_argument,       NULL, \'n\'},\n        {"present",           no_argument,       NULL, \'e\'},\n        {"type",              required_argument, NULL, \'t\'},\n        {"default",           required_argument, NULL, \'d\'},\n        {"list",              no_argument,       NULL, \'l\'},\n        {"tree-line-length",  required_argument, NULL, \'L\'},\n        {"output",            required_argument, NULL, \'o\'},\n        {"operational",       required_argument, NULL, \'O\'},\n        {"reply-rpc",         required_argument, NULL, \'R\'},\n        {"merge",             no_argument,       NULL, \'m\'},\n        {"yang-library",      no_argument,       NULL, \'y\'},\n        {"yang-library-file", required_argument, NULL, \'Y\'},\n#ifndef NDEBUG\n        {"debug",            required_argument, NULL, \'G\'},\n#endif\n        {NULL,               0,                 NULL, 0}\n    };\n    uint8_t data_type_set = 0;\n\n    c->ctx_options = YL_DEFAULT_CTX_OPTIONS;\n    c->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;\n    c->line_length = 0;\n\n    opterr = 0;\n#ifndef NDEBUG\n    while ((opt = getopt_long(argc, argv, "hvVQf:p:DF:iP:qs:net:d:lL:o:O:R:myY:G:", options, &opt_index)) != -1)\n#else\n    while ((opt = getopt_long(argc, argv, "hvVQf:p:DF:iP:qs:net:d:lL:o:O:R:myY:", options, &opt_index)) != -1)\n#endif\n    {\n        switch (opt) {\n        case \'h\': /* --help */\n            help(0);\n            return 1;\n\n        case \'v\': /* --version */\n            version();\n            return 1;\n\n        case \'V\': { /* --verbose */\n            LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);\n            if (verbosity < LY_LLDBG) {\n                ++verbosity;\n            }\n            ly_log_level(verbosity);\n            break;\n        } /* case \'V\' */\n\n        case \'Q\': { /* --quiet */\n            LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);\n            if (verbosity == LY_LLERR) {\n                /* turn logging off */\n                ly_log_options(LY_LOSTORE_LAST);\n            } else if (verbosity > LY_LLERR) {\n                --verbosity;\n            }\n            ly_log_level(verbosity);\n            break;\n        } /* case \'Q\' */\n\n        case \'f\': /* --format */\n            if (!strcasecmp(optarg, "yang")) {\n                c->schema_out_format = LYS_OUT_YANG;\n                c->data_out_format = 0;\n            } else if (!strcasecmp(optarg, "yin")) {\n                c->schema_out_format = LYS_OUT_YIN;\n                c->data_out_format = 0;\n            } else if (!strcasecmp(optarg, "info")) {\n                c->schema_out_format = LYS_OUT_YANG_COMPILED;\n                c->data_out_format = 0;\n            } else if (!strcasecmp(optarg, "tree")) {\n                c->schema_out_format = LYS_OUT_TREE;\n                c->data_out_format = 0;\n            } else if (!strcasecmp(optarg, "xml")) {\n                c->schema_out_format = 0;\n                c->data_out_format = LYD_XML;\n            } else if (!strcasecmp(optarg, "json")) {\n                c->schema_out_format = 0;\n                c->data_out_format = LYD_JSON;\n            } else if (!strcasecmp(optarg, "lyb")) {\n                c->schema_out_format = 0;\n                c->data_out_format = LYD_LYB;\n            } else {\n                YLMSG_E("Unknown output format %s\\n", optarg);\n                help(1);\n                return -1;\n            }\n            break;\n\n        case \'p\': { /* --path */\n            struct stat st;\n\n            if (stat(optarg, &st) == -1) {\n                YLMSG_E("Unable to use search path (%s) - %s.\\n", optarg, strerror(errno));\n                return -1;\n            }\n            if (!S_ISDIR(st.st_mode)) {\n                YLMSG_E("Provided search path is not a directory.\\n");\n                return -1;\n            }\n\n            if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {\n                YLMSG_E("Storing searchpath failed.\\n");\n                return -1;\n            }\n\n            break;\n        } /* case \'p\' */\n\n        case \'D\': /* --disable-search */\n            if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {\n                YLMSG_W("The -D option specified too many times.\\n");\n            }\n            if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) {\n                c->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;\n                c->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS;\n            } else {\n                c->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD;\n            }\n            break;\n\n        case \'F\': /* --features */\n            if (parse_features(optarg, &c->schema_features)) {\n                return -1;\n            }\n            break;\n\n        case \'i\': /* --make-implemented */\n            if (c->ctx_options & LY_CTX_REF_IMPLEMENTED) {\n                c->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;\n                c->ctx_options |= LY_CTX_ALL_IMPLEMENTED;\n            } else {\n                c->ctx_options |= LY_CTX_REF_IMPLEMENTED;\n            }\n            break;\n\n        case \'P\': /* --schema-node */\n            c->schema_node_path = optarg;\n            break;\n\n        case \'q\': /* --single-node */\n            c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;\n            break;\n\n        case \'s\': /* --submodule */\n            c->submodule = optarg;\n            break;\n        }\n#endif\n    }\n#if 0\n    if (!outformat_d && options_dflt) {\n        /* we have options for printing default nodes, but data output not specified */\n        fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\\n");\n    }\n    if (outformat_s && (options_parser || autodetection)) {\n        /* we have options for printing data tree, but output is schema */\n        fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\\n");\n                YLMSG_E("The data type (-t) cannot be set multiple times.\\n");\n                return -1;\n            }\n\n            if (!strcasecmp(optarg, "config")) {\n                c->data_parse_options |= LYD_PARSE_NO_STATE;\n                c->data_validate_options |= LYD_VALIDATE_NO_STATE;\n            } else if (!strcasecmp(optarg, "get")) {\n                c->data_parse_options |= LYD_PARSE_ONLY;\n            } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {\n                c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;\n            } else if (!strcasecmp(optarg, "edit")) {\n                c->data_parse_options |= LYD_PARSE_ONLY;\n            } else if (!strcasecmp(optarg, "rpc")) {\n                c->data_type = LYD_TYPE_RPC_YANG;\n            } else if (!strcasecmp(optarg, "nc-rpc")) {\n                c->data_type = LYD_TYPE_RPC_NETCONF;\n            } else if (!strcasecmp(optarg, "reply")) {\n                c->data_type = LYD_TYPE_REPLY_YANG;\n            } else if (!strcasecmp(optarg, "nc-reply")) {\n                c->data_type = LYD_TYPE_REPLY_NETCONF;\n            } else if (!strcasecmp(optarg, "notif")) {\n                c->data_type = LYD_TYPE_NOTIF_YANG;\n            } else if (!strcasecmp(optarg, "nc-notif")) {\n                c->data_type = LYD_TYPE_NOTIF_NETCONF;\n            } else if (!strcasecmp(optarg, "data")) {\n                /* default option */\n            } else {\n                YLMSG_E("Unknown data tree type %s\\n", optarg);\n                help(1);\n                return -1;\n            }\n\n            data_type_set = 1;\n            break;\n\n        case \'d\': /* --default */\n            if (!strcasecmp(optarg, "all")) {\n                c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;\n            } else if (!strcasecmp(optarg, "all-tagged")) {\n                c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;\n            } else if (!strcasecmp(optarg, "trim")) {\n                c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;\n            } else if (!strcasecmp(optarg, "implicit-tagged")) {\n                c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;\n            } else {\n                YLMSG_E("Unknown default mode %s\\n", optarg);\n                help(1);\n                return -1;\n            }\n            break;\n\n        case \'l\': /* --list */\n            c->list = 1;\n            break;\n\n        case \'L\': /* --tree-line-length */\n            c->line_length = atoi(optarg);\n            break;\n\n        case \'o\': /* --output */\n            if (c->out) {\n                YLMSG_E("Only a single output can be specified.\\n");\n                return -1;\n            } else {\n                if (ly_out_new_filepath(optarg, &c->out)) {\n                    YLMSG_E("Unable open output file %s (%s)\\n", optarg, strerror(errno));\n                    return -1;\n                }\n            }\n            break;\n\n        case \'O\': /* --operational */\n            if (c->data_operational.path) {\n                YLMSG_E("The operational datastore (-O) cannot be set multiple times.\\n");\n                return -1;\n            }\n            c->data_operational.path = optarg;\n            break;\n\n        case \'R\': /* --reply-rpc */\n            if (c->reply_rpc.path) {\n                YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\\n");\n                return -1;\n            }\n            c->reply_rpc.path = optarg;\n            break;\n\n        case \'m\': /* --merge */\n            c->data_merge = 1;\n            break;\n\n        case \'y\': /* --yang-library */\n            c->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;\n            break;\n\n        case \'Y\': /* --yang-library-file */\n            c->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;\n            c->yang_lib_file = optarg;\n            break;\n\n#ifndef NDEBUG\n        case \'G\': { /* --debug */\n            uint32_t dbg_groups = 0;\n            const char *ptr = optarg;\n\n            while (ptr[0]) {\n                if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {\n                    dbg_groups |= LY_LDGDICT;\n                    ptr += sizeof "dict" - 1;\n                } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {\n                    dbg_groups |= LY_LDGXPATH;\n                    ptr += sizeof "xpath" - 1;\n                } else if (!strncasecmp(ptr, "dep-sets", sizeof "dep-sets" - 1)) {\n                    dbg_groups |= LY_LDGDEPSETS;\n                    ptr += sizeof "dep-sets" - 1;\n                }\n\n                if (ptr[0]) {\n                    if (ptr[0] != \',\') {\n                        YLMSG_E("Unknown debug group string \\"%s\\"\\n", optarg);\n                        return -1;\n                    }\n                    ++ptr;\n                }\n            }\n            ly_log_dbg_groups(dbg_groups);\n            break;\n        } /* case \'G\' */\n#endif\n        default:\n            YLMSG_E("Invalid option or missing argument: -%c\\n", optopt);\n            return -1;\n        } /* switch */\n    }\n\n    /* additional checks for the options combinations */\n    if (!c->list && (optind >= argc)) {\n        help(1);\n        YLMSG_E("Missing <schema> to process.\\n");\n        return 1;\n    }\n\n    if (c->data_merge) {\n        if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {\n            /* switch off the option, incompatible input data type */\n            c->data_merge = 0;\n        } else {\n            /* postpone validation after the merge of all the input data */\n            c->data_parse_options |= LYD_PARSE_ONLY;\n        }\n    }\n\n    if (c->data_operational.path && !c->data_type) {\n        YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notification input data types.\\n");\n        c->data_operational.path = NULL;\n    }\n\n    if (c->reply_rpc.path && (c->data_type != LYD_TYPE_REPLY_NETCONF)) {\n        YLMSG_E("Source RPC is needed only for NETCONF Reply input data type.\\n");\n        c->data_operational.path = NULL;\n    } else if (!c->reply_rpc.path && (c->data_type == LYD_TYPE_REPLY_NETCONF)) {\n        YLMSG_E("Missing source RPC (-R) for NETCONF Reply input data type.\\n");\n        return -1;\n    }\n\n    if ((c->schema_out_format != LYS_OUT_TREE) && c->line_length) {\n        YLMSG_E("--tree-line-length take effect only in case of the tree output format.\\n");\n    }\n\n    /* default output stream */\n    if (!c->out) {\n        if (ly_out_new_file(stdout, &c->out)) {\n            YLMSG_E("Unable to set stdout as output.\\n");\n            return -1;\n        }\n    }\n\n    if (c->schema_out_format == LYS_OUT_TREE) {\n        /* print tree from lysc_nodes */\n        c->ctx_options |= LY_CTX_SET_PRIV_PARSED;\n    }\n\n    /* process input files provided as standalone command line arguments,\n     * schema modules are parsed and inserted into the context,\n     * data files are just checked and prepared into internal structures for further processing */\n    ret = fill_context_inputs(argc, argv, c);\n    if (ret) {\n        return ret;\n    }\n\n    /* the second batch of checks */\n    if (c->schema_print_options && !c->schema_out_format) {\n        YLMSG_W("Schema printer options specified, but the schema output format is missing.\\n");\n    }\n    if (c->schema_parse_options && !c->schema_modules.count) {\n        YLMSG_W("Schema parser options specified, but no schema input file provided.\\n");\n    }\n    if (c->data_print_options && !c->data_out_format) {\n        YLMSG_W("data printer options specified, but the data output format is missing.\\n");\n    }\n    if (((c->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || c->data_type) && !c->data_inputs.count) {\n        YLMSG_W("Data parser options specified, but no data input file provided.\\n");\n    }\n\n    if (c->schema_node_path) {\n        c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);\n        if (!c->schema_node) {\n            c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);\n\n            if (!c->schema_node) {\n                YLMSG_E("Invalid schema path.\\n");\n                return -1;\n            }\n        }\n    }\n\n    return 0;\n}\n\nint\nmain_ni(int argc, char *argv[])\n{\n    int ret = EXIT_SUCCESS, r;\n    struct context c = {0};\n\n    /* set callback for printing libyang messages */\n    ly_set_log_clb(libyang_verbclb, 1);\n\n    r = fill_context(argc, argv, &c);\n    if (r < 0) {\n        ret = EXIT_FAILURE;\n    }\n    if (r) {\n        goto cleanup;\n    }\n\n    /* do the required job - parse, validate, print */\n\n    if (c.list) {\n        /* print the list of schemas */\n        print_list(c.out, c.ctx, c.data_out_format);\n    } else {\n        if (c.schema_out_format) {\n            if (c.schema_node) {\n                ret = lys_print_node(c.out, c.schema_node, c.schema_out_format, 0, c.schema_print_options);\n                if (ret) {\n                    YLMSG_E("Unable to print schema node %s.\\n", c.schema_node_path);\n                    goto cleanup;\n                }\n            } else if (c.submodule) {\n                const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(c.ctx, c.submodule);\n                if (!submod) {\n                    YLMSG_E("Unable to find submodule %s.\\n", c.submodule);\n                    goto cleanup;\n                }\n\n                ret = lys_print_submodule(c.out, submod, c.schema_out_format, c.line_length, c.schema_print_options);\n                if (ret) {\n                    YLMSG_E("Unable to print submodule %s.\\n", submod->name);\n                    goto cleanup;\n                }\n            } else {\n                for (uint32_t u = 0; u < c.schema_modules.count; ++u) {\n                    ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format,\n                            c.line_length, c.schema_print_options);\n                    /* for YANG Tree Diagrams printing it\'s more readable to print a blank line between modules. */\n                    if ((c.schema_out_format == LYS_OUT_TREE) && (u + 1 < c.schema_modules.count)) {\n                        ly_print(c.out, "\\n");\n                    }\n                    if (ret) {\n                        YLMSG_E("Unable to print module %s.\\n", ((struct lys_module *)c.schema_modules.objs[u])->name);\n                        goto cleanup;\n                    }\n                }\n            }\n        }\n\n        /* do the data validation despite the schema was printed */\n        if (c.data_inputs.size) {\n            ret = process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out, c.data_parse_options,\n                    c.data_validate_options, c.data_print_options, &c.data_operational, &c.reply_rpc, &c.data_inputs, NULL);\n            if (ret) {\n                goto cleanup;\n            }\n        }\n    }\n\ncleanup:\n    /* cleanup */\n    erase_context(&c);\n\n    return ret;\n}\n                if (!out) {\n                    ly_out_new_file(stdout, &out);\n                }\n                lyd_print_all(out, data_item->tree, outformat_d, 0 /* TODO defaults | options_dflt */);\n#if 0\n                if (envelope_s) {\n                    if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {\n', 'message': 'yanglint CHANGE handle default values modes\n', 'author_name': 'Radek Krejci', 'lang': 'c'}