blob: 37fe3f029e712f76e051d130c15b84904e34a5e8 [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 */
143 if (!posc && !yo->schema_node_path) {
144 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) {
149 YLMSG_E("--tree-line-length take effect only in case of the tree output format.\n");
150 return 1;
151 }
152
153 if (!yo->out) {
154 if (ly_out_new_file(stdout, &yo->out)) {
155 YLMSG_E("Could not use stdout to print output.\n");
156 }
157 yo->out_stdout = 1;
158 }
159
160 return 0;
161}
162
aPiecek0bea9872021-04-21 11:13:20 +0200163static LY_ERR
aPieceka83b8e02023-06-07 15:25:16 +0200164print_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 +0200165{
166 LY_ERR erc;
167 const struct lysp_submodule *submodule;
168
169 submodule = revision ?
170 ly_ctx_get_submodule(*ctx, name, revision) :
171 ly_ctx_get_submodule_latest(*ctx, name);
172
173 erc = submodule ?
174 lys_print_submodule(out, submodule, format, line_length, options) :
175 LY_ENOTFOUND;
176
177 return erc;
178}
179
180static LY_ERR
aPieceka83b8e02023-06-07 15:25:16 +0200181print_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 +0200182{
183 LY_ERR erc;
184 struct lys_module *module;
185
186 module = revision ?
187 ly_ctx_get_module(*ctx, name, revision) :
188 ly_ctx_get_module_latest(*ctx, name);
189
190 erc = module ?
191 lys_print_module(out, module, format, line_length, options) :
192 LY_ENOTFOUND;
193
194 return erc;
195}
196
aPieceka83b8e02023-06-07 15:25:16 +0200197static int
198cmd_print_module(const char *posv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format,
199 size_t line_length, uint32_t options)
aPiecek0bea9872021-04-21 11:13:20 +0200200{
aPieceka83b8e02023-06-07 15:25:16 +0200201 int rc = 0;
aPiecek0bea9872021-04-21 11:13:20 +0200202 LY_ERR erc;
aPieceka83b8e02023-06-07 15:25:16 +0200203 char *name = NULL, *revision;
aPiecek0bea9872021-04-21 11:13:20 +0200204 ly_bool search_submodul;
205
aPieceka83b8e02023-06-07 15:25:16 +0200206 name = strdup(posv);
207 /* get revision */
208 revision = strchr(name, '@');
209 if (revision) {
210 revision[0] = '\0';
211 ++revision;
aPiecek0bea9872021-04-21 11:13:20 +0200212 }
aPieceka83b8e02023-06-07 15:25:16 +0200213
214 erc = print_module(out, ctx, name, revision, format, line_length, options);
215
216 if (erc == LY_ENOTFOUND) {
217 search_submodul = 1;
218 erc = print_submodule(out, ctx, name, revision, format, line_length, options);
219 } else {
220 search_submodul = 0;
221 }
222
223 if (erc == LY_SUCCESS) {
224 rc = 0;
225 } else if (erc == LY_ENOTFOUND) {
226 if (revision) {
227 YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision);
228 } else {
229 YLMSG_E("No (sub)module \"%s\" found.\n", name);
230 }
231 rc = 1;
232 } else {
233 if (search_submodul) {
234 YLMSG_E("Unable to print submodule %s.\n", name);
235 } else {
236 YLMSG_E("Unable to print module %s.\n", name);
237 }
238 rc = 1;
239 }
240
241 free(name);
242 return rc;
aPiecek0bea9872021-04-21 11:13:20 +0200243}
244
aPieceka83b8e02023-06-07 15:25:16 +0200245int
246cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100247{
aPieceka83b8e02023-06-07 15:25:16 +0200248 int rc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100249
aPieceka83b8e02023-06-07 15:25:16 +0200250 if (yo->schema_out_format == LYS_OUT_TREE) {
aPiecekc1603ba2021-04-19 13:52:22 +0200251 /* print tree from lysc_nodes */
252 ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED);
253 }
254
aPieceka83b8e02023-06-07 15:25:16 +0200255 if (yo->schema_node_path) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100256 const struct lysc_node *node;
Michal Vasko26bbb272022-08-02 14:54:33 +0200257
aPieceka83b8e02023-06-07 15:25:16 +0200258 node = find_schema_path(*ctx, yo->schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100259 if (!node) {
aPieceka83b8e02023-06-07 15:25:16 +0200260 YLMSG_E("The requested schema node \"%s\" does not exists.\n", yo->schema_node_path);
261 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100262 }
romanf00089c2022-10-06 16:01:31 +0200263
aPieceka83b8e02023-06-07 15:25:16 +0200264 if (lys_print_node(yo->out, node, yo->schema_out_format, yo->line_length, yo->schema_print_options)) {
265 YLMSG_E("Unable to print schema node %s.\n", yo->schema_node_path);
266 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100267 }
268 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200269 rc = cmd_print_module(posv, yo->out, ctx, yo->schema_out_format, yo->line_length, yo->schema_print_options);
270 if (!yo->last_one) {
271 /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
272 if (yo->schema_out_format == LYS_OUT_TREE) {
273 ly_print(yo->out, "\n");
274 }
275 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100276 }
277
aPieceka83b8e02023-06-07 15:25:16 +0200278 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100279}