blob: a1db0231a2ebc9bbe4a383a222e993c1ecc13632 [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{
34 printf("Usage: print [-f (yang | yin | tree | info [-q -P PATH])] [-o OUTFILE]\n"
35 " [<module-name1>[@revision]] ...\n"
36 " 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"
41 " -P PATH, --schema-node=PATH\n"
42 " Print only the specified subtree of the schema.\n"
43 " The PATH is the XPath subset mentioned in documentation as\n"
44 " the Path format. The option can be combined with --single-node\n"
45 " option to print information only about the specified node.\n"
46 " -q, --single-node\n"
47 " Supplement to the --schema-node option to print information\n"
48 " only about a single node specified as PATH argument.\n"
49 " -o OUTFILE, --output=OUTFILE\n"
50 " Write the output to OUTFILE instead of stdout.\n");
51}
52
53void
54cmd_print(struct ly_ctx **ctx, const char *cmdline)
55{
56 LY_ERR ret;
57 int argc = 0;
58 char **argv = NULL;
59 int opt, opt_index;
60 struct option options[] = {
61 {"format", required_argument, NULL, 'f'},
62 {"help", no_argument, NULL, 'h'},
63 {"output", required_argument, NULL, 'o'},
64 {"schema-node", required_argument, NULL, 'P'},
65 {"single-node", no_argument, NULL, 'q'},
66 {NULL, 0, NULL, 0}
67 };
68 uint16_t options_print = 0;
69 const char *node_path = NULL;
70 LYS_OUTFORMAT format = LYS_OUT_TREE;
71 const char *out_path = NULL;
72 struct ly_out *out = NULL;
73
74 if (parse_cmdline(cmdline, &argc, &argv)) {
75 goto cleanup;
76 }
77
78 while ((opt = getopt_long(argc, argv, "f:ho:P:q", options, &opt_index)) != -1) {
79 switch (opt) {
80 case 'o': /* --output */
81 if (out) {
82 if (ly_out_filepath(out, optarg) != NULL) {
83 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
84 goto cleanup;
85 }
86 } else {
87 if (ly_out_new_filepath(optarg, &out)) {
88 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
89 goto cleanup;
90 }
91 }
92 break;
93
94 case 'f': /* --format */
95 if (!strcasecmp(optarg, "yang")) {
96 format = LYS_OUT_YANG;
97 } else if (!strcasecmp(optarg, "yin")) {
98 format = LYS_OUT_YIN;
99 } else if (!strcasecmp(optarg, "info")) {
100 format = LYS_OUT_YANG_COMPILED;
101 } else if (!strcasecmp(optarg, "tree")) {
102 format = LYS_OUT_TREE;
103 } else {
104 YLMSG_E("Unknown output format %s\n", optarg);
105 cmd_print_help();
106 goto cleanup;
107 }
108 break;
109
110 case 'P': /* --schema-node */
111 node_path = optarg;
112 break;
113
114 case 'q': /* --single-node */
115 options_print |= LYS_PRINT_NO_SUBSTMT;
116 break;
117
118 case 'h':
119 cmd_print_help();
120 goto cleanup;
121 default:
122 YLMSG_E("Unknown option.\n");
123 goto cleanup;
124 }
125 }
126
127 /* file name */
128 if ((argc == optind) && !node_path) {
129 YLMSG_E("Missing the name of the module to print.\n");
130 goto cleanup;
131 }
132
133 if (out_path) {
134 ret = ly_out_new_filepath(out_path, &out);
135 } else {
136 ret = ly_out_new_file(stdout, &out);
137 }
138 if (ret) {
139 YLMSG_E("Could not open the output file (%s).\n", strerror(errno));
140 goto cleanup;
141 }
142
143 if (node_path) {
144 const struct lysc_node *node;
145 node = lys_find_path(*ctx, NULL, node_path, 0);
146 if (!node) {
147 node = lys_find_path(*ctx, NULL, node_path, 1);
148
149 if (!node) {
150 YLMSG_E("The requested schema node \"%s\" does not exists.\n", node_path);
151 goto cleanup;
152 }
153 }
154 if (lys_print_node(out, node, format, 0, options_print)) {
155 YLMSG_E("Unable to print schema node %s.\n", node_path);
156 goto cleanup;
157 }
158 } else {
159 for (int i = 0; i < argc - optind; i++) {
160 struct lys_module *module;
161 char *revision;
162
163 /* get revision */
164 revision = strchr(argv[optind + i], '@');
165 if (revision) {
166 revision[0] = '\0';
167 ++revision;
168 }
169
170 if (revision) {
171 module = ly_ctx_get_module(*ctx, argv[optind + i], revision);
172 } else {
173 module = ly_ctx_get_module_latest(*ctx, argv[optind + i]);
174 }
175 if (!module) {
176 /* TODO try to find it as a submodule */
177 if (revision) {
178 YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", argv[optind + i], revision);
179 } else {
180 YLMSG_E("No (sub)module \"%s\" found.\n", argv[optind + i]);
181 }
182 goto cleanup;
183 }
184 if (lys_print_module(out, module, format, 0, options_print)) {
185 YLMSG_E("Unable to print module %s.\n", argv[optind + i]);
186 goto cleanup;
187 }
188 }
189 }
190
191cleanup:
192 free_cmdline(argv);
193 ly_out_free(out, NULL, out_path ? 1 : 0);
194}