blob: 2f46c5195be6358d5c8f5bdd98aa9389725be696 [file] [log] [blame]
/**
* @file cmd_print.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Adam Piecek <piecek@cesnet.cz>
* @brief 'print' command of the libyang's yanglint tool.
*
* Copyright (c) 2015-2023 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 "cmd.h"
#include <errno.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "libyang.h"
#include "common.h"
#include "yl_opt.h"
void
cmd_print_help(void)
{
printf("Usage: print [-f (yang | yin | tree [-q -P PATH -L LINE_LENGTH ] | info [-q -P PATH])]\n"
" [-o OUTFILE] [<module-name1>[@revision]] ...\n"
" Print a schema module. The <module-name> is not required\n"
" only in case the -P option is specified. For yang, yin and tree\n"
" formats, a submodule can also be printed.\n\n"
" -f FORMAT, --format=FORMAT\n"
" Print the module in the specified FORMAT. If format not\n"
" specified, the 'tree' format is used.\n"
" -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"
" -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"
" -o OUTFILE, --output=OUTFILE\n"
" Write the output to OUTFILE instead of stdout.\n");
}
int
cmd_print_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
{
int rc = 0, argc = 0;
int opt, opt_index;
struct option options[] = {
{"format", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"tree-line-length", required_argument, NULL, 'L'},
{"output", required_argument, NULL, 'o'},
{"schema-node", required_argument, NULL, 'P'},
{"single-node", no_argument, NULL, 'q'},
{NULL, 0, NULL, 0}
};
yo->schema_out_format = LYS_OUT_TREE;
if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
return rc;
}
while ((opt = getopt_long(argc, yo->argv, commands[CMD_PRINT].optstring, options, &opt_index)) != -1) {
switch (opt) {
case 'o': /* --output */
if (yo->out) {
if (ly_out_filepath(yo->out, optarg) != NULL) {
YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
return 1;
}
} else {
if (ly_out_new_filepath(optarg, &yo->out)) {
YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
return 1;
}
}
break;
case 'f': /* --format */
if (!strcasecmp(optarg, "yang")) {
yo->schema_out_format = LYS_OUT_YANG;
} else if (!strcasecmp(optarg, "yin")) {
yo->schema_out_format = LYS_OUT_YIN;
} else if (!strcasecmp(optarg, "info")) {
yo->schema_out_format = LYS_OUT_YANG_COMPILED;
} else if (!strcasecmp(optarg, "tree")) {
yo->schema_out_format = LYS_OUT_TREE;
} else {
YLMSG_E("Unknown output format %s\n", optarg);
cmd_print_help();
return 1;
}
break;
case 'L': /* --tree-line-length */
yo->line_length = atoi(optarg);
break;
case 'P': /* --schema-node */
yo->schema_node_path = optarg;
break;
case 'q': /* --single-node */
yo->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
break;
case 'h':
cmd_print_help();
return 1;
default:
YLMSG_E("Unknown option.\n");
return 1;
}
}
*posv = &yo->argv[optind];
*posc = argc - optind;
return 0;
}
int
cmd_print_dep(struct yl_opt *yo, int posc)
{
/* file name */
if (yo->interactive && !posc && !yo->schema_node_path) {
YLMSG_E("Missing the name of the module to print.\n");
return 1;
}
if ((yo->schema_out_format != LYS_OUT_TREE) && yo->line_length) {
YLMSG_W("--tree-line-length take effect only in case of the tree output format.\n");
}
if (!yo->out) {
if (ly_out_new_file(stdout, &yo->out)) {
YLMSG_E("Could not use stdout to print output.\n");
}
yo->out_stdout = 1;
}
return 0;
}
static LY_ERR
print_submodule(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
{
LY_ERR erc;
const struct lysp_submodule *submodule;
submodule = revision ?
ly_ctx_get_submodule(*ctx, name, revision) :
ly_ctx_get_submodule_latest(*ctx, name);
erc = submodule ?
lys_print_submodule(out, submodule, format, line_length, options) :
LY_ENOTFOUND;
return erc;
}
static LY_ERR
print_module(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
{
LY_ERR erc;
struct lys_module *module;
module = revision ?
ly_ctx_get_module(*ctx, name, revision) :
ly_ctx_get_module_latest(*ctx, name);
erc = module ?
lys_print_module(out, module, format, line_length, options) :
LY_ENOTFOUND;
return erc;
}
static int
cmd_print_module(const char *posv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format,
size_t line_length, uint32_t options)
{
int rc = 0;
LY_ERR erc;
char *name = NULL, *revision;
ly_bool search_submodul;
name = strdup(posv);
/* get revision */
revision = strchr(name, '@');
if (revision) {
revision[0] = '\0';
++revision;
}
erc = print_module(out, ctx, name, revision, format, line_length, options);
if (erc == LY_ENOTFOUND) {
search_submodul = 1;
erc = print_submodule(out, ctx, name, revision, format, line_length, options);
} else {
search_submodul = 0;
}
if (erc == LY_SUCCESS) {
rc = 0;
} else if (erc == LY_ENOTFOUND) {
if (revision) {
YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision);
} else {
YLMSG_E("No (sub)module \"%s\" found.\n", name);
}
rc = 1;
} else {
if (search_submodul) {
YLMSG_E("Unable to print submodule %s.\n", name);
} else {
YLMSG_E("Unable to print module %s.\n", name);
}
rc = 1;
}
free(name);
return rc;
}
int
cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
{
int rc = 0;
if (yo->schema_out_format == LYS_OUT_TREE) {
/* print tree from lysc_nodes */
ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED);
}
if (yo->schema_node_path) {
const struct lysc_node *node;
node = find_schema_path(*ctx, yo->schema_node_path);
if (!node) {
YLMSG_E("The requested schema node \"%s\" does not exists.\n", yo->schema_node_path);
return 1;
}
if (lys_print_node(yo->out, node, yo->schema_out_format, yo->line_length, yo->schema_print_options)) {
YLMSG_E("Unable to print schema node %s.\n", yo->schema_node_path);
return 1;
}
} else {
rc = cmd_print_module(posv, yo->out, ctx, yo->schema_out_format, yo->line_length, yo->schema_print_options);
if (!yo->last_one) {
/* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
if (yo->schema_out_format == LYS_OUT_TREE) {
ly_print(yo->out, "\n");
}
}
}
return rc;
}