blob: 0273b2cf8e77b1f5abb1b3ccb17b39458f1030bd [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
2 * @file cmd_print.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @author Radek Krejci <rkrejci@cesnet.cz>
5 * @brief 'print' command of the libyang's yanglint tool.
6 *
7 * Copyright (c) 2015-2020 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#define _GNU_SOURCE
17
18#include "cmd.h"
19
20#include <errno.h>
21#include <getopt.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <string.h>
25#include <strings.h>
26
27#include "libyang.h"
28
29#include "common.h"
30
31void
32cmd_print_help(void)
33{
34 printf("Usage: print [-f (yang | yin | tree | info [-q -P PATH])] [-o OUTFILE]\n"
35 " [<module-name1>[@revision]] ...\n"
36 " Print a schema module. The <module-name> is not required\n"
37 " only in case the -P option is specified.\n\n"
38 " -f FORMAT, --format=FORMAT\n"
39 " Print the module in the specified FORMAT. If format not\n"
40 " specified, the 'tree' format is used.\n"
41 " -P PATH, --schema-node=PATH\n"
42 " Print only the specified subtree of the schema.\n"
43 " The PATH is the XPath subset mentioned in documentation as\n"
44 " the Path format. The option can be combined with --single-node\n"
45 " option to print information only about the specified node.\n"
46 " -q, --single-node\n"
47 " Supplement to the --schema-node option to print information\n"
48 " only about a single node specified as PATH argument.\n"
49 " -o OUTFILE, --output=OUTFILE\n"
50 " Write the output to OUTFILE instead of stdout.\n");
51}
52
53void
54cmd_print(struct ly_ctx **ctx, const char *cmdline)
55{
Radek Krejcie9f13b12020-11-09 17:42:04 +010056 int argc = 0;
57 char **argv = NULL;
58 int opt, opt_index;
59 struct option options[] = {
60 {"format", required_argument, NULL, 'f'},
61 {"help", no_argument, NULL, 'h'},
62 {"output", required_argument, NULL, 'o'},
63 {"schema-node", required_argument, NULL, 'P'},
64 {"single-node", no_argument, NULL, 'q'},
65 {NULL, 0, NULL, 0}
66 };
67 uint16_t options_print = 0;
68 const char *node_path = NULL;
69 LYS_OUTFORMAT format = LYS_OUT_TREE;
Radek Krejcie9f13b12020-11-09 17:42:04 +010070 struct ly_out *out = NULL;
Radek Krejci4b0f01b2021-03-28 17:37:21 +020071 ly_bool out_stdout = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +010072
73 if (parse_cmdline(cmdline, &argc, &argv)) {
74 goto cleanup;
75 }
76
77 while ((opt = getopt_long(argc, argv, "f:ho:P:q", options, &opt_index)) != -1) {
78 switch (opt) {
79 case 'o': /* --output */
80 if (out) {
81 if (ly_out_filepath(out, optarg) != NULL) {
Radek Krejci540ce982020-11-26 12:01:30 +010082 YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
Radek Krejcie9f13b12020-11-09 17:42:04 +010083 goto cleanup;
84 }
85 } else {
86 if (ly_out_new_filepath(optarg, &out)) {
Radek Krejci540ce982020-11-26 12:01:30 +010087 YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
Radek Krejcie9f13b12020-11-09 17:42:04 +010088 goto cleanup;
89 }
90 }
91 break;
92
93 case 'f': /* --format */
94 if (!strcasecmp(optarg, "yang")) {
95 format = LYS_OUT_YANG;
96 } else if (!strcasecmp(optarg, "yin")) {
97 format = LYS_OUT_YIN;
98 } else if (!strcasecmp(optarg, "info")) {
99 format = LYS_OUT_YANG_COMPILED;
100 } else if (!strcasecmp(optarg, "tree")) {
101 format = LYS_OUT_TREE;
102 } else {
103 YLMSG_E("Unknown output format %s\n", optarg);
104 cmd_print_help();
105 goto cleanup;
106 }
107 break;
108
109 case 'P': /* --schema-node */
110 node_path = optarg;
111 break;
112
113 case 'q': /* --single-node */
114 options_print |= LYS_PRINT_NO_SUBSTMT;
115 break;
116
117 case 'h':
118 cmd_print_help();
119 goto cleanup;
120 default:
121 YLMSG_E("Unknown option.\n");
122 goto cleanup;
123 }
124 }
125
126 /* file name */
127 if ((argc == optind) && !node_path) {
128 YLMSG_E("Missing the name of the module to print.\n");
129 goto cleanup;
130 }
131
Radek Krejci540ce982020-11-26 12:01:30 +0100132 if (!out) {
133 if (ly_out_new_file(stdout, &out)) {
134 YLMSG_E("Could not use stdout to print output.\n");
135 goto cleanup;
136 }
Radek Krejci4b0f01b2021-03-28 17:37:21 +0200137 out_stdout = 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100138 }
139
aPiecekc1603ba2021-04-19 13:52:22 +0200140 if (format == LYS_OUT_TREE) {
141 /* print tree from lysc_nodes */
142 ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED);
143 }
144
Radek Krejcie9f13b12020-11-09 17:42:04 +0100145 if (node_path) {
146 const struct lysc_node *node;
147 node = lys_find_path(*ctx, NULL, node_path, 0);
148 if (!node) {
149 node = lys_find_path(*ctx, NULL, node_path, 1);
150
151 if (!node) {
152 YLMSG_E("The requested schema node \"%s\" does not exists.\n", node_path);
153 goto cleanup;
154 }
155 }
156 if (lys_print_node(out, node, format, 0, options_print)) {
157 YLMSG_E("Unable to print schema node %s.\n", node_path);
158 goto cleanup;
159 }
160 } else {
161 for (int i = 0; i < argc - optind; i++) {
162 struct lys_module *module;
163 char *revision;
164
165 /* get revision */
166 revision = strchr(argv[optind + i], '@');
167 if (revision) {
168 revision[0] = '\0';
169 ++revision;
170 }
171
172 if (revision) {
173 module = ly_ctx_get_module(*ctx, argv[optind + i], revision);
174 } else {
175 module = ly_ctx_get_module_latest(*ctx, argv[optind + i]);
176 }
177 if (!module) {
178 /* TODO try to find it as a submodule */
179 if (revision) {
180 YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", argv[optind + i], revision);
181 } else {
182 YLMSG_E("No (sub)module \"%s\" found.\n", argv[optind + i]);
183 }
184 goto cleanup;
185 }
186 if (lys_print_module(out, module, format, 0, options_print)) {
187 YLMSG_E("Unable to print module %s.\n", argv[optind + i]);
188 goto cleanup;
189 }
190 }
191 }
192
193cleanup:
194 free_cmdline(argv);
Radek Krejci4b0f01b2021-03-28 17:37:21 +0200195 ly_out_free(out, NULL, out_stdout ? 0 : 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100196}