blob: 2f46c5195be6358d5c8f5bdd98aa9389725be696 [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) {
84 if (ly_out_filepath(yo->out, optarg) != NULL) {
85 YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
86 return 1;
87 }
88 } else {
89 if (ly_out_new_filepath(optarg, &yo->out)) {
90 YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
91 return 1;
92 }
93 }
94 break;
95
96 case 'f': /* --format */
97 if (!strcasecmp(optarg, "yang")) {
98 yo->schema_out_format = LYS_OUT_YANG;
99 } else if (!strcasecmp(optarg, "yin")) {
100 yo->schema_out_format = LYS_OUT_YIN;
101 } else if (!strcasecmp(optarg, "info")) {
102 yo->schema_out_format = LYS_OUT_YANG_COMPILED;
103 } else if (!strcasecmp(optarg, "tree")) {
104 yo->schema_out_format = LYS_OUT_TREE;
105 } else {
106 YLMSG_E("Unknown output format %s\n", optarg);
107 cmd_print_help();
108 return 1;
109 }
110 break;
111
112 case 'L': /* --tree-line-length */
113 yo->line_length = atoi(optarg);
114 break;
115
116 case 'P': /* --schema-node */
117 yo->schema_node_path = optarg;
118 break;
119
120 case 'q': /* --single-node */
121 yo->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
122 break;
123
124 case 'h':
125 cmd_print_help();
126 return 1;
127 default:
128 YLMSG_E("Unknown option.\n");
129 return 1;
130 }
131 }
132
133 *posv = &yo->argv[optind];
134 *posc = argc - optind;
135
136 return 0;
137}
138
139int
140cmd_print_dep(struct yl_opt *yo, int posc)
141{
142 /* file name */
aPiecek04c8c2e2023-06-08 08:38:22 +0200143 if (yo->interactive && !posc && !yo->schema_node_path) {
aPieceka83b8e02023-06-07 15:25:16 +0200144 YLMSG_E("Missing the name of the module to print.\n");
145 return 1;
146 }
147
148 if ((yo->schema_out_format != LYS_OUT_TREE) && yo->line_length) {
aPiecek04c8c2e2023-06-08 08:38:22 +0200149 YLMSG_W("--tree-line-length take effect only in case of the tree output format.\n");
aPieceka83b8e02023-06-07 15:25:16 +0200150 }
151
152 if (!yo->out) {
153 if (ly_out_new_file(stdout, &yo->out)) {
154 YLMSG_E("Could not use stdout to print output.\n");
155 }
156 yo->out_stdout = 1;
157 }
158
159 return 0;
160}
161
aPiecek0bea9872021-04-21 11:13:20 +0200162static LY_ERR
aPieceka83b8e02023-06-07 15:25:16 +0200163print_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 +0200164{
165 LY_ERR erc;
166 const struct lysp_submodule *submodule;
167
168 submodule = revision ?
169 ly_ctx_get_submodule(*ctx, name, revision) :
170 ly_ctx_get_submodule_latest(*ctx, name);
171
172 erc = submodule ?
173 lys_print_submodule(out, submodule, format, line_length, options) :
174 LY_ENOTFOUND;
175
176 return erc;
177}
178
179static LY_ERR
aPieceka83b8e02023-06-07 15:25:16 +0200180print_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 +0200181{
182 LY_ERR erc;
183 struct lys_module *module;
184
185 module = revision ?
186 ly_ctx_get_module(*ctx, name, revision) :
187 ly_ctx_get_module_latest(*ctx, name);
188
189 erc = module ?
190 lys_print_module(out, module, format, line_length, options) :
191 LY_ENOTFOUND;
192
193 return erc;
194}
195
aPieceka83b8e02023-06-07 15:25:16 +0200196static int
197cmd_print_module(const char *posv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format,
198 size_t line_length, uint32_t options)
aPiecek0bea9872021-04-21 11:13:20 +0200199{
aPieceka83b8e02023-06-07 15:25:16 +0200200 int rc = 0;
aPiecek0bea9872021-04-21 11:13:20 +0200201 LY_ERR erc;
aPieceka83b8e02023-06-07 15:25:16 +0200202 char *name = NULL, *revision;
aPiecek0bea9872021-04-21 11:13:20 +0200203 ly_bool search_submodul;
204
aPieceka83b8e02023-06-07 15:25:16 +0200205 name = strdup(posv);
206 /* get revision */
207 revision = strchr(name, '@');
208 if (revision) {
209 revision[0] = '\0';
210 ++revision;
aPiecek0bea9872021-04-21 11:13:20 +0200211 }
aPieceka83b8e02023-06-07 15:25:16 +0200212
213 erc = print_module(out, ctx, name, revision, format, line_length, options);
214
215 if (erc == LY_ENOTFOUND) {
216 search_submodul = 1;
217 erc = print_submodule(out, ctx, name, revision, format, line_length, options);
218 } else {
219 search_submodul = 0;
220 }
221
222 if (erc == LY_SUCCESS) {
223 rc = 0;
224 } else if (erc == LY_ENOTFOUND) {
225 if (revision) {
226 YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision);
227 } else {
228 YLMSG_E("No (sub)module \"%s\" found.\n", name);
229 }
230 rc = 1;
231 } else {
232 if (search_submodul) {
233 YLMSG_E("Unable to print submodule %s.\n", name);
234 } else {
235 YLMSG_E("Unable to print module %s.\n", name);
236 }
237 rc = 1;
238 }
239
240 free(name);
241 return rc;
aPiecek0bea9872021-04-21 11:13:20 +0200242}
243
aPieceka83b8e02023-06-07 15:25:16 +0200244int
245cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100246{
aPieceka83b8e02023-06-07 15:25:16 +0200247 int rc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100248
aPieceka83b8e02023-06-07 15:25:16 +0200249 if (yo->schema_out_format == LYS_OUT_TREE) {
aPiecekc1603ba2021-04-19 13:52:22 +0200250 /* print tree from lysc_nodes */
251 ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED);
252 }
253
aPieceka83b8e02023-06-07 15:25:16 +0200254 if (yo->schema_node_path) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100255 const struct lysc_node *node;
Michal Vasko26bbb272022-08-02 14:54:33 +0200256
aPieceka83b8e02023-06-07 15:25:16 +0200257 node = find_schema_path(*ctx, yo->schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100258 if (!node) {
aPieceka83b8e02023-06-07 15:25:16 +0200259 YLMSG_E("The requested schema node \"%s\" does not exists.\n", yo->schema_node_path);
260 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100261 }
romanf00089c2022-10-06 16:01:31 +0200262
aPieceka83b8e02023-06-07 15:25:16 +0200263 if (lys_print_node(yo->out, node, yo->schema_out_format, yo->line_length, yo->schema_print_options)) {
264 YLMSG_E("Unable to print schema node %s.\n", yo->schema_node_path);
265 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100266 }
267 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200268 rc = cmd_print_module(posv, yo->out, ctx, yo->schema_out_format, yo->line_length, yo->schema_print_options);
269 if (!yo->last_one) {
270 /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
271 if (yo->schema_out_format == LYS_OUT_TREE) {
272 ly_print(yo->out, "\n");
273 }
274 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100275 }
276
aPieceka83b8e02023-06-07 15:25:16 +0200277 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100278}