blob: badf9b18f80105a756b1afed1e28985c921dd008 [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{
Radek Krejcie9f13b12020-11-09 17:42:04 +010056 int argc = 0;
57 char **argv = NULL;
58 int opt, opt_index;
59 struct option options[] = {
60 {"format", required_argument, NULL, 'f'},
61 {"help", no_argument, NULL, 'h'},
62 {"output", required_argument, NULL, 'o'},
63 {"schema-node", required_argument, NULL, 'P'},
64 {"single-node", no_argument, NULL, 'q'},
65 {NULL, 0, NULL, 0}
66 };
67 uint16_t options_print = 0;
68 const char *node_path = NULL;
69 LYS_OUTFORMAT format = LYS_OUT_TREE;
Radek Krejcie9f13b12020-11-09 17:42:04 +010070 struct ly_out *out = NULL;
71
72 if (parse_cmdline(cmdline, &argc, &argv)) {
73 goto cleanup;
74 }
75
76 while ((opt = getopt_long(argc, argv, "f:ho:P:q", options, &opt_index)) != -1) {
77 switch (opt) {
78 case 'o': /* --output */
79 if (out) {
80 if (ly_out_filepath(out, optarg) != NULL) {
Radek Krejci540ce982020-11-26 12:01:30 +010081 YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
Radek Krejcie9f13b12020-11-09 17:42:04 +010082 goto cleanup;
83 }
84 } else {
85 if (ly_out_new_filepath(optarg, &out)) {
Radek Krejci540ce982020-11-26 12:01:30 +010086 YLMSG_E("Unable to use output file %s for printing output.\n", optarg);
Radek Krejcie9f13b12020-11-09 17:42:04 +010087 goto cleanup;
88 }
89 }
90 break;
91
92 case 'f': /* --format */
93 if (!strcasecmp(optarg, "yang")) {
94 format = LYS_OUT_YANG;
95 } else if (!strcasecmp(optarg, "yin")) {
96 format = LYS_OUT_YIN;
97 } else if (!strcasecmp(optarg, "info")) {
98 format = LYS_OUT_YANG_COMPILED;
99 } else if (!strcasecmp(optarg, "tree")) {
100 format = LYS_OUT_TREE;
101 } else {
102 YLMSG_E("Unknown output format %s\n", optarg);
103 cmd_print_help();
104 goto cleanup;
105 }
106 break;
107
108 case 'P': /* --schema-node */
109 node_path = optarg;
110 break;
111
112 case 'q': /* --single-node */
113 options_print |= LYS_PRINT_NO_SUBSTMT;
114 break;
115
116 case 'h':
117 cmd_print_help();
118 goto cleanup;
119 default:
120 YLMSG_E("Unknown option.\n");
121 goto cleanup;
122 }
123 }
124
125 /* file name */
126 if ((argc == optind) && !node_path) {
127 YLMSG_E("Missing the name of the module to print.\n");
128 goto cleanup;
129 }
130
Radek Krejci540ce982020-11-26 12:01:30 +0100131 if (!out) {
132 if (ly_out_new_file(stdout, &out)) {
133 YLMSG_E("Could not use stdout to print output.\n");
134 goto cleanup;
135 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100136 }
137
138 if (node_path) {
139 const struct lysc_node *node;
140 node = lys_find_path(*ctx, NULL, node_path, 0);
141 if (!node) {
142 node = lys_find_path(*ctx, NULL, node_path, 1);
143
144 if (!node) {
145 YLMSG_E("The requested schema node \"%s\" does not exists.\n", node_path);
146 goto cleanup;
147 }
148 }
149 if (lys_print_node(out, node, format, 0, options_print)) {
150 YLMSG_E("Unable to print schema node %s.\n", node_path);
151 goto cleanup;
152 }
153 } else {
154 for (int i = 0; i < argc - optind; i++) {
155 struct lys_module *module;
156 char *revision;
157
158 /* get revision */
159 revision = strchr(argv[optind + i], '@');
160 if (revision) {
161 revision[0] = '\0';
162 ++revision;
163 }
164
165 if (revision) {
166 module = ly_ctx_get_module(*ctx, argv[optind + i], revision);
167 } else {
168 module = ly_ctx_get_module_latest(*ctx, argv[optind + i]);
169 }
170 if (!module) {
171 /* TODO try to find it as a submodule */
172 if (revision) {
173 YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", argv[optind + i], revision);
174 } else {
175 YLMSG_E("No (sub)module \"%s\" found.\n", argv[optind + i]);
176 }
177 goto cleanup;
178 }
179 if (lys_print_module(out, module, format, 0, options_print)) {
180 YLMSG_E("Unable to print module %s.\n", argv[optind + i]);
181 goto cleanup;
182 }
183 }
184 }
185
186cleanup:
187 free_cmdline(argv);
Radek Krejci540ce982020-11-26 12:01:30 +0100188 ly_out_free(out, NULL, ly_out_file(out, NULL) != stdout ? 1 : 0);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100189}