blob: c1a5359928be4ec70be0aca600851d028fdceac3 [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{
aPiecek2cfeeae2021-04-21 13:17:34 +020034 printf("Usage: print [-f (yang | yin | tree [-q -P PATH -L LINE_LENGTH ] | info [-q -P PATH])]\n"
35 " [-o OUTFILE] [<module-name1>[@revision]] ...\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010036 " 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"
aPiecek2cfeeae2021-04-21 13:17:34 +020041 " -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n"
42 " The limit of the maximum line length on which the 'tree'\n"
43 " format will try to be printed.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010044 " -P PATH, --schema-node=PATH\n"
45 " Print only the specified subtree of the schema.\n"
46 " The PATH is the XPath subset mentioned in documentation as\n"
47 " the Path format. The option can be combined with --single-node\n"
48 " option to print information only about the specified node.\n"
49 " -q, --single-node\n"
50 " Supplement to the --schema-node option to print information\n"
51 " only about a single node specified as PATH argument.\n"
52 " -o OUTFILE, --output=OUTFILE\n"
53 " Write the output to OUTFILE instead of stdout.\n");
54}
55
aPiecek0bea9872021-04-21 11:13:20 +020056static LY_ERR
57cmd_print_submodule(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
58{
59 LY_ERR erc;
60 const struct lysp_submodule *submodule;
61
62 submodule = revision ?
63 ly_ctx_get_submodule(*ctx, name, revision) :
64 ly_ctx_get_submodule_latest(*ctx, name);
65
66 erc = submodule ?
67 lys_print_submodule(out, submodule, format, line_length, options) :
68 LY_ENOTFOUND;
69
70 return erc;
71}
72
73static LY_ERR
74cmd_print_module(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
75{
76 LY_ERR erc;
77 struct lys_module *module;
78
79 module = revision ?
80 ly_ctx_get_module(*ctx, name, revision) :
81 ly_ctx_get_module_latest(*ctx, name);
82
83 erc = module ?
84 lys_print_module(out, module, format, line_length, options) :
85 LY_ENOTFOUND;
86
87 return erc;
88}
89
90static void
91cmd_print_modules(int argc, char **argv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format, size_t line_length, uint32_t options)
92{
93 LY_ERR erc;
94 char *name, *revision;
95 ly_bool search_submodul;
aPiecek8f0b7882021-05-21 10:18:36 +020096 const int stop = argc - optind;
aPiecek0bea9872021-04-21 11:13:20 +020097
aPiecek8f0b7882021-05-21 10:18:36 +020098 for (int i = 0; i < stop; i++) {
aPiecek0bea9872021-04-21 11:13:20 +020099 name = argv[optind + i];
100 /* get revision */
101 revision = strchr(name, '@');
102 if (revision) {
103 revision[0] = '\0';
104 ++revision;
105 }
106
107 erc = cmd_print_module(out, ctx, name, revision, format, line_length, options);
108
109 if (erc == LY_ENOTFOUND) {
110 search_submodul = 1;
111 erc = cmd_print_submodule(out, ctx, name, revision, format, line_length, options);
112 } else {
113 search_submodul = 0;
114 }
115
116 if (erc == LY_SUCCESS) {
aPiecek8f0b7882021-05-21 10:18:36 +0200117 /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
118 if ((format == LYS_OUT_TREE) && (i + 1 < stop)) {
119 ly_print(out, "\n");
120 }
aPiecek0bea9872021-04-21 11:13:20 +0200121 continue;
122 } else if (erc == LY_ENOTFOUND) {
123 if (revision) {
124 YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision);
125 } else {
126 YLMSG_E("No (sub)module \"%s\" found.\n", name);
127 }
128 break;
129 } else {
130 if (search_submodul) {
131 YLMSG_E("Unable to print submodule %s.\n", name);
132 } else {
133 YLMSG_E("Unable to print module %s.\n", name);
134 }
135 break;
136 }
137 }
138}
139
Radek Krejcie9f13b12020-11-09 17:42:04 +0100140void
141cmd_print(struct ly_ctx **ctx, const char *cmdline)
142{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100143 int argc = 0;
144 char **argv = NULL;
145 int opt, opt_index;
146 struct option options[] = {
147 {"format", required_argument, NULL, 'f'},
148 {"help", no_argument, NULL, 'h'},
aPiecek2cfeeae2021-04-21 13:17:34 +0200149 {"tree-line-length", required_argument, NULL, 'L'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100150 {"output", required_argument, NULL, 'o'},
151 {"schema-node", required_argument, NULL, 'P'},
152 {"single-node", no_argument, NULL, 'q'},
153 {NULL, 0, NULL, 0}
154 };
155 uint16_t options_print = 0;
156 const char *node_path = NULL;
157 LYS_OUTFORMAT format = LYS_OUT_TREE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100158 struct ly_out *out = NULL;
Radek Krejci4b0f01b2021-03-28 17:37:21 +0200159 ly_bool out_stdout = 0;
aPiecek2cfeeae2021-04-21 13:17:34 +0200160 size_t line_length = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100161
162 if (parse_cmdline(cmdline, &argc, &argv)) {
163 goto cleanup;
164 }
165
aPiecek2cfeeae2021-04-21 13:17:34 +0200166 while ((opt = getopt_long(argc, argv, "f:hL:o:P:q", options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100167 switch (opt) {
168 case 'o': /* --output */
169 if (out) {
170 if (ly_out_filepath(out, optarg) != NULL) {
Radek Krejci540ce982020-11-26 12:01:30 +0100171 YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100172 goto cleanup;
173 }
174 } else {
175 if (ly_out_new_filepath(optarg, &out)) {
Radek Krejci540ce982020-11-26 12:01:30 +0100176 YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100177 goto cleanup;
178 }
179 }
180 break;
181
182 case 'f': /* --format */
183 if (!strcasecmp(optarg, "yang")) {
184 format = LYS_OUT_YANG;
185 } else if (!strcasecmp(optarg, "yin")) {
186 format = LYS_OUT_YIN;
187 } else if (!strcasecmp(optarg, "info")) {
188 format = LYS_OUT_YANG_COMPILED;
189 } else if (!strcasecmp(optarg, "tree")) {
190 format = LYS_OUT_TREE;
191 } else {
192 YLMSG_E("Unknown output format %s\n", optarg);
193 cmd_print_help();
194 goto cleanup;
195 }
196 break;
197
aPiecek2cfeeae2021-04-21 13:17:34 +0200198 case 'L': /* --tree-line-length */
199 line_length = atoi(optarg);
200 break;
201
Radek Krejcie9f13b12020-11-09 17:42:04 +0100202 case 'P': /* --schema-node */
203 node_path = optarg;
204 break;
205
206 case 'q': /* --single-node */
207 options_print |= LYS_PRINT_NO_SUBSTMT;
208 break;
209
210 case 'h':
211 cmd_print_help();
212 goto cleanup;
213 default:
214 YLMSG_E("Unknown option.\n");
215 goto cleanup;
216 }
217 }
218
219 /* file name */
220 if ((argc == optind) && !node_path) {
221 YLMSG_E("Missing the name of the module to print.\n");
222 goto cleanup;
223 }
224
aPiecek2cfeeae2021-04-21 13:17:34 +0200225 if ((format != LYS_OUT_TREE) && line_length) {
226 YLMSG_E("--tree-line-length take effect only in case of the tree output format.\n");
227 goto cleanup;
228 }
229
Radek Krejci540ce982020-11-26 12:01:30 +0100230 if (!out) {
231 if (ly_out_new_file(stdout, &out)) {
232 YLMSG_E("Could not use stdout to print output.\n");
233 goto cleanup;
234 }
Radek Krejci4b0f01b2021-03-28 17:37:21 +0200235 out_stdout = 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100236 }
237
aPiecekc1603ba2021-04-19 13:52:22 +0200238 if (format == LYS_OUT_TREE) {
239 /* print tree from lysc_nodes */
240 ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED);
241 }
242
Radek Krejcie9f13b12020-11-09 17:42:04 +0100243 if (node_path) {
244 const struct lysc_node *node;
Michal Vasko26bbb272022-08-02 14:54:33 +0200245
romanf00089c2022-10-06 16:01:31 +0200246 node = find_schema_path(*ctx, node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100247 if (!node) {
romanf00089c2022-10-06 16:01:31 +0200248 YLMSG_E("The requested schema node \"%s\" does not exists.\n", node_path);
249 goto cleanup;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100250 }
romanf00089c2022-10-06 16:01:31 +0200251
Radek Krejcie9f13b12020-11-09 17:42:04 +0100252 if (lys_print_node(out, node, format, 0, options_print)) {
253 YLMSG_E("Unable to print schema node %s.\n", node_path);
254 goto cleanup;
255 }
256 } else {
aPiecek2cfeeae2021-04-21 13:17:34 +0200257 cmd_print_modules(argc, argv, out, ctx, format, line_length, options_print);
aPiecek0bea9872021-04-21 11:13:20 +0200258 goto cleanup;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100259 }
260
261cleanup:
262 free_cmdline(argv);
Radek Krejci4b0f01b2021-03-28 17:37:21 +0200263 ly_out_free(out, NULL, out_stdout ? 0 : 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100264}