blob: ff5fb904c3d861efaaa024ce4e335ee99b83a26e [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>
aPieceka83b8e02023-06-07 15:25:16 +02005 * @author Adam Piecek <piecek@cesnet.cz>
Radek Krejcie9f13b12020-11-09 17:42:04 +01006 * @brief 'print' command of the libyang's yanglint tool.
7 *
aPieceka83b8e02023-06-07 15:25:16 +02008 * Copyright (c) 2015-2023 CESNET, z.s.p.o.
Radek Krejcie9f13b12020-11-09 17:42:04 +01009 *
10 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * https://opensource.org/licenses/BSD-3-Clause
15 */
16
17#define _GNU_SOURCE
18
19#include "cmd.h"
20
21#include <errno.h>
22#include <getopt.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <string.h>
26#include <strings.h>
27
28#include "libyang.h"
29
30#include "common.h"
aPieceka83b8e02023-06-07 15:25:16 +020031#include "yl_opt.h"
Radek Krejcie9f13b12020-11-09 17:42:04 +010032
33void
34cmd_print_help(void)
35{
aPiecek2cfeeae2021-04-21 13:17:34 +020036 printf("Usage: print [-f (yang | yin | tree [-q -P PATH -L LINE_LENGTH ] | info [-q -P PATH])]\n"
37 " [-o OUTFILE] [<module-name1>[@revision]] ...\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010038 " Print a schema module. The <module-name> is not required\n"
aPiecekdbe35d22023-03-31 10:33:15 +020039 " only in case the -P option is specified. For yang, yin and tree\n"
40 " formats, a submodule can also be printed.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010041 " -f FORMAT, --format=FORMAT\n"
42 " Print the module in the specified FORMAT. If format not\n"
43 " specified, the 'tree' format is used.\n"
aPiecek2cfeeae2021-04-21 13:17:34 +020044 " -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n"
45 " The limit of the maximum line length on which the 'tree'\n"
46 " format will try to be printed.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010047 " -P PATH, --schema-node=PATH\n"
48 " Print only the specified subtree of the schema.\n"
49 " The PATH is the XPath subset mentioned in documentation as\n"
50 " the Path format. The option can be combined with --single-node\n"
51 " option to print information only about the specified node.\n"
52 " -q, --single-node\n"
53 " Supplement to the --schema-node option to print information\n"
54 " only about a single node specified as PATH argument.\n"
55 " -o OUTFILE, --output=OUTFILE\n"
56 " Write the output to OUTFILE instead of stdout.\n");
57}
58
aPieceka83b8e02023-06-07 15:25:16 +020059int
60cmd_print_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
61{
62 int rc = 0, argc = 0;
63 int opt, opt_index;
64 struct option options[] = {
65 {"format", required_argument, NULL, 'f'},
66 {"help", no_argument, NULL, 'h'},
67 {"tree-line-length", required_argument, NULL, 'L'},
68 {"output", required_argument, NULL, 'o'},
69 {"schema-node", required_argument, NULL, 'P'},
70 {"single-node", no_argument, NULL, 'q'},
71 {NULL, 0, NULL, 0}
72 };
73
74 yo->schema_out_format = LYS_OUT_TREE;
75
76 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
77 return rc;
78 }
79
80 while ((opt = getopt_long(argc, yo->argv, commands[CMD_PRINT].optstring, options, &opt_index)) != -1) {
81 switch (opt) {
82 case 'o': /* --output */
83 if (yo->out) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020084 YLMSG_E("Only a single output can be specified.");
aPiecek94560e12023-06-08 10:02:06 +020085 return 1;
aPieceka83b8e02023-06-07 15:25:16 +020086 } else {
87 if (ly_out_new_filepath(optarg, &yo->out)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020088 YLMSG_E("Unable open output file %s (%s).", optarg, strerror(errno));
aPieceka83b8e02023-06-07 15:25:16 +020089 return 1;
90 }
91 }
92 break;
93
94 case 'f': /* --format */
aPiecek113e0f02023-06-09 08:47:48 +020095 if (yl_opt_update_schema_out_format(optarg, yo)) {
aPieceka83b8e02023-06-07 15:25:16 +020096 cmd_print_help();
97 return 1;
98 }
99 break;
100
101 case 'L': /* --tree-line-length */
102 yo->line_length = atoi(optarg);
103 break;
104
105 case 'P': /* --schema-node */
106 yo->schema_node_path = optarg;
107 break;
108
109 case 'q': /* --single-node */
110 yo->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
111 break;
112
113 case 'h':
114 cmd_print_help();
115 return 1;
116 default:
Michal Vasko1407d7d2023-08-21 09:57:54 +0200117 YLMSG_E("Unknown option.");
aPieceka83b8e02023-06-07 15:25:16 +0200118 return 1;
119 }
120 }
121
122 *posv = &yo->argv[optind];
123 *posc = argc - optind;
124
125 return 0;
126}
127
128int
129cmd_print_dep(struct yl_opt *yo, int posc)
130{
131 /* file name */
aPiecek04c8c2e2023-06-08 08:38:22 +0200132 if (yo->interactive && !posc && !yo->schema_node_path) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200133 YLMSG_E("Missing the name of the module to print.");
aPieceka83b8e02023-06-07 15:25:16 +0200134 return 1;
135 }
136
137 if ((yo->schema_out_format != LYS_OUT_TREE) && yo->line_length) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200138 YLMSG_W("--tree-line-length take effect only in case of the tree output format.");
aPieceka83b8e02023-06-07 15:25:16 +0200139 }
140
141 if (!yo->out) {
142 if (ly_out_new_file(stdout, &yo->out)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200143 YLMSG_E("Could not use stdout to print output.");
aPieceka83b8e02023-06-07 15:25:16 +0200144 }
145 yo->out_stdout = 1;
146 }
147
aPiecek816107f2023-06-15 15:31:53 +0200148 if (yo->schema_out_format == LYS_OUT_TREE) {
149 /* print tree from lysc_nodes */
150 yo->ctx_options |= LY_CTX_SET_PRIV_PARSED;
151 }
152
aPieceka83b8e02023-06-07 15:25:16 +0200153 return 0;
154}
155
aPiecek0bea9872021-04-21 11:13:20 +0200156static LY_ERR
aPieceka83b8e02023-06-07 15:25:16 +0200157print_submodule(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
aPiecek0bea9872021-04-21 11:13:20 +0200158{
159 LY_ERR erc;
160 const struct lysp_submodule *submodule;
161
162 submodule = revision ?
163 ly_ctx_get_submodule(*ctx, name, revision) :
164 ly_ctx_get_submodule_latest(*ctx, name);
165
166 erc = submodule ?
167 lys_print_submodule(out, submodule, format, line_length, options) :
168 LY_ENOTFOUND;
169
aPiecek816107f2023-06-15 15:31:53 +0200170 if (!erc) {
171 return 0;
172 } else if ((erc == LY_ENOTFOUND) && revision) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200173 YLMSG_E("No submodule \"%s\" found.", name);
aPiecek816107f2023-06-15 15:31:53 +0200174 } else {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200175 YLMSG_E("Unable to print submodule %s.", name);
aPiecek816107f2023-06-15 15:31:53 +0200176 }
177
aPiecek0bea9872021-04-21 11:13:20 +0200178 return erc;
179}
180
181static LY_ERR
aPieceka83b8e02023-06-07 15:25:16 +0200182print_module(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
aPiecek0bea9872021-04-21 11:13:20 +0200183{
184 LY_ERR erc;
185 struct lys_module *module;
186
187 module = revision ?
188 ly_ctx_get_module(*ctx, name, revision) :
189 ly_ctx_get_module_latest(*ctx, name);
190
191 erc = module ?
192 lys_print_module(out, module, format, line_length, options) :
193 LY_ENOTFOUND;
194
aPiecek816107f2023-06-15 15:31:53 +0200195 if (!erc) {
196 return 0;
197 } else if ((erc == LY_ENOTFOUND) && revision) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200198 YLMSG_E("No module \"%s\" found.", name);
aPiecek816107f2023-06-15 15:31:53 +0200199 } else {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200200 YLMSG_E("Unable to print module %s.", name);
aPiecek816107f2023-06-15 15:31:53 +0200201 }
202
aPiecek0bea9872021-04-21 11:13:20 +0200203 return erc;
204}
205
aPieceka83b8e02023-06-07 15:25:16 +0200206static int
207cmd_print_module(const char *posv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format,
208 size_t line_length, uint32_t options)
aPiecek0bea9872021-04-21 11:13:20 +0200209{
210 LY_ERR erc;
aPieceka83b8e02023-06-07 15:25:16 +0200211 char *name = NULL, *revision;
aPiecek0bea9872021-04-21 11:13:20 +0200212
aPieceka83b8e02023-06-07 15:25:16 +0200213 name = strdup(posv);
214 /* get revision */
215 revision = strchr(name, '@');
216 if (revision) {
217 revision[0] = '\0';
218 ++revision;
aPiecek0bea9872021-04-21 11:13:20 +0200219 }
aPieceka83b8e02023-06-07 15:25:16 +0200220
221 erc = print_module(out, ctx, name, revision, format, line_length, options);
222
223 if (erc == LY_ENOTFOUND) {
aPieceka83b8e02023-06-07 15:25:16 +0200224 erc = print_submodule(out, ctx, name, revision, format, line_length, options);
aPieceka83b8e02023-06-07 15:25:16 +0200225 }
226
227 free(name);
aPiecek816107f2023-06-15 15:31:53 +0200228 return erc;
aPiecek0bea9872021-04-21 11:13:20 +0200229}
230
aPieceke73292e2023-06-15 13:25:35 +0200231/**
232 * @brief Print schema node path.
233 *
234 * @param[in] ctx Context for libyang.
235 * @param[in] yo Context for yanglint.
236 * @return 0 on success.
237 */
238static int
239print_node(struct ly_ctx *ctx, struct yl_opt *yo)
240{
241 const struct lysc_node *node;
242 uint32_t temp_lo = 0;
243
244 if (yo->interactive) {
245 /* Use the same approach as for completion. */
246 node = find_schema_path(ctx, yo->schema_node_path);
247 if (!node) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200248 YLMSG_E("The requested schema node \"%s\" does not exists.", yo->schema_node_path);
aPieceke73292e2023-06-15 13:25:35 +0200249 return 1;
250 }
251 } else {
252 /* turn off logging so that the message is not repeated */
253 ly_temp_log_options(&temp_lo);
254 /* search operation input */
255 node = lys_find_path(ctx, NULL, yo->schema_node_path, 0);
256 if (!node) {
257 /* restore logging so an error may be displayed */
258 ly_temp_log_options(NULL);
259 /* search operation output */
260 node = lys_find_path(ctx, NULL, yo->schema_node_path, 1);
261 if (!node) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200262 YLMSG_E("Invalid schema path.");
aPieceke73292e2023-06-15 13:25:35 +0200263 return 1;
264 }
265 }
266 }
267
268 if (lys_print_node(yo->out, node, yo->schema_out_format, yo->line_length, yo->schema_print_options)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200269 YLMSG_E("Unable to print schema node %s.", yo->schema_node_path);
aPieceke73292e2023-06-15 13:25:35 +0200270 return 1;
271 }
272
273 return 0;
274}
275
aPieceka83b8e02023-06-07 15:25:16 +0200276int
277cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100278{
aPieceka83b8e02023-06-07 15:25:16 +0200279 int rc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100280
aPiecek816107f2023-06-15 15:31:53 +0200281 if (yo->ctx_options & LY_CTX_SET_PRIV_PARSED) {
aPiecekc1603ba2021-04-19 13:52:22 +0200282 /* print tree from lysc_nodes */
283 ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED);
284 }
285
aPieceka83b8e02023-06-07 15:25:16 +0200286 if (yo->schema_node_path) {
aPieceke73292e2023-06-15 13:25:35 +0200287 rc = print_node(*ctx, yo);
aPiecek816107f2023-06-15 15:31:53 +0200288 } else if (!yo->interactive && yo->submodule) {
289 rc = print_submodule(yo->out, ctx, yo->submodule, NULL, yo->schema_out_format, yo->line_length,
290 yo->schema_print_options);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100291 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200292 rc = cmd_print_module(posv, yo->out, ctx, yo->schema_out_format, yo->line_length, yo->schema_print_options);
aPieceke73292e2023-06-15 13:25:35 +0200293 if (!yo->last_one && (yo->schema_out_format == LYS_OUT_TREE)) {
294 ly_print(yo->out, "\n");
aPieceka83b8e02023-06-07 15:25:16 +0200295 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100296 }
297
aPieceka83b8e02023-06-07 15:25:16 +0200298 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100299}