blob: efbf9ebfdb9edfaaa9df81e5526235d1de8f679b [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) {
aPiecek94560e12023-06-08 10:02:06 +020084 YLMSG_E("Only a single output can be specified.\n");
85 return 1;
aPieceka83b8e02023-06-07 15:25:16 +020086 } else {
87 if (ly_out_new_filepath(optarg, &yo->out)) {
aPiecek94560e12023-06-08 10:02:06 +020088 YLMSG_E("Unable open output file %s (%s)\n", 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:
117 YLMSG_E("Unknown option.\n");
118 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) {
aPieceka83b8e02023-06-07 15:25:16 +0200133 YLMSG_E("Missing the name of the module to print.\n");
134 return 1;
135 }
136
137 if ((yo->schema_out_format != LYS_OUT_TREE) && yo->line_length) {
aPiecek04c8c2e2023-06-08 08:38:22 +0200138 YLMSG_W("--tree-line-length take effect only in case of the tree output format.\n");
aPieceka83b8e02023-06-07 15:25:16 +0200139 }
140
141 if (!yo->out) {
142 if (ly_out_new_file(stdout, &yo->out)) {
143 YLMSG_E("Could not use stdout to print output.\n");
144 }
145 yo->out_stdout = 1;
146 }
147
148 return 0;
149}
150
aPiecek0bea9872021-04-21 11:13:20 +0200151static LY_ERR
aPieceka83b8e02023-06-07 15:25:16 +0200152print_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 +0200153{
154 LY_ERR erc;
155 const struct lysp_submodule *submodule;
156
157 submodule = revision ?
158 ly_ctx_get_submodule(*ctx, name, revision) :
159 ly_ctx_get_submodule_latest(*ctx, name);
160
161 erc = submodule ?
162 lys_print_submodule(out, submodule, format, line_length, options) :
163 LY_ENOTFOUND;
164
165 return erc;
166}
167
168static LY_ERR
aPieceka83b8e02023-06-07 15:25:16 +0200169print_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 +0200170{
171 LY_ERR erc;
172 struct lys_module *module;
173
174 module = revision ?
175 ly_ctx_get_module(*ctx, name, revision) :
176 ly_ctx_get_module_latest(*ctx, name);
177
178 erc = module ?
179 lys_print_module(out, module, format, line_length, options) :
180 LY_ENOTFOUND;
181
182 return erc;
183}
184
aPieceka83b8e02023-06-07 15:25:16 +0200185static int
186cmd_print_module(const char *posv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format,
187 size_t line_length, uint32_t options)
aPiecek0bea9872021-04-21 11:13:20 +0200188{
aPieceka83b8e02023-06-07 15:25:16 +0200189 int rc = 0;
aPiecek0bea9872021-04-21 11:13:20 +0200190 LY_ERR erc;
aPieceka83b8e02023-06-07 15:25:16 +0200191 char *name = NULL, *revision;
aPiecek0bea9872021-04-21 11:13:20 +0200192 ly_bool search_submodul;
193
aPieceka83b8e02023-06-07 15:25:16 +0200194 name = strdup(posv);
195 /* get revision */
196 revision = strchr(name, '@');
197 if (revision) {
198 revision[0] = '\0';
199 ++revision;
aPiecek0bea9872021-04-21 11:13:20 +0200200 }
aPieceka83b8e02023-06-07 15:25:16 +0200201
202 erc = print_module(out, ctx, name, revision, format, line_length, options);
203
204 if (erc == LY_ENOTFOUND) {
205 search_submodul = 1;
206 erc = print_submodule(out, ctx, name, revision, format, line_length, options);
207 } else {
208 search_submodul = 0;
209 }
210
211 if (erc == LY_SUCCESS) {
212 rc = 0;
213 } else if (erc == LY_ENOTFOUND) {
214 if (revision) {
215 YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision);
216 } else {
217 YLMSG_E("No (sub)module \"%s\" found.\n", name);
218 }
219 rc = 1;
220 } else {
221 if (search_submodul) {
222 YLMSG_E("Unable to print submodule %s.\n", name);
223 } else {
224 YLMSG_E("Unable to print module %s.\n", name);
225 }
226 rc = 1;
227 }
228
229 free(name);
230 return rc;
aPiecek0bea9872021-04-21 11:13:20 +0200231}
232
aPieceka83b8e02023-06-07 15:25:16 +0200233int
234cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100235{
aPieceka83b8e02023-06-07 15:25:16 +0200236 int rc = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100237
aPieceka83b8e02023-06-07 15:25:16 +0200238 if (yo->schema_out_format == LYS_OUT_TREE) {
aPiecekc1603ba2021-04-19 13:52:22 +0200239 /* print tree from lysc_nodes */
240 ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED);
241 }
242
aPieceka83b8e02023-06-07 15:25:16 +0200243 if (yo->schema_node_path) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100244 const struct lysc_node *node;
Michal Vasko26bbb272022-08-02 14:54:33 +0200245
aPieceka83b8e02023-06-07 15:25:16 +0200246 node = find_schema_path(*ctx, yo->schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100247 if (!node) {
aPieceka83b8e02023-06-07 15:25:16 +0200248 YLMSG_E("The requested schema node \"%s\" does not exists.\n", yo->schema_node_path);
249 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100250 }
romanf00089c2022-10-06 16:01:31 +0200251
aPieceka83b8e02023-06-07 15:25:16 +0200252 if (lys_print_node(yo->out, node, yo->schema_out_format, yo->line_length, yo->schema_print_options)) {
253 YLMSG_E("Unable to print schema node %s.\n", yo->schema_node_path);
254 return 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100255 }
256 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200257 rc = cmd_print_module(posv, yo->out, ctx, yo->schema_out_format, yo->line_length, yo->schema_print_options);
258 if (!yo->last_one) {
259 /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
260 if (yo->schema_out_format == LYS_OUT_TREE) {
261 ly_print(yo->out, "\n");
262 }
263 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100264 }
265
aPieceka83b8e02023-06-07 15:25:16 +0200266 return rc;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100267}