blob: 2e76c6dd7dce615e1bc9e45c6dfb1713c6e4fb7a [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
2 * @file main_ni.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang's yanglint tool - noninteractive code
5 *
Radek Krejcie9f13b12020-11-09 17:42:04 +01006 * Copyright (c) 2020 CESNET, z.s.p.o.
Radek Krejcied5acc52019-04-25 15:57:04 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcied5acc52019-04-25 15:57:04 +020016
Radek Krejcie9f13b12020-11-09 17:42:04 +010017#include <errno.h>
18#include <getopt.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020020#include <stdio.h>
21#include <stdlib.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020022#include <string.h>
Radek Krejci47fab892020-11-05 17:02:41 +010023#include <strings.h>
Radek Krejcie9f13b12020-11-09 17:42:04 +010024#include <sys/stat.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020025
Radek Krejcied5acc52019-04-25 15:57:04 +020026#include "libyang.h"
27
Radek Krejcie9f13b12020-11-09 17:42:04 +010028#include "common.h"
aPiecek8f0b7882021-05-21 10:18:36 +020029#include "out.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "tools/config.h"
31
Radek Krejcie9f13b12020-11-09 17:42:04 +010032/**
33 * @brief Context structure to hold and pass variables in a structured form.
34 */
35struct context {
36 /* libyang context for the run */
37 struct ly_ctx *ctx;
Radek Krejci535ea9f2020-05-29 16:01:05 +020038
Radek Krejcie9f13b12020-11-09 17:42:04 +010039 /* prepared output (--output option or stdout by default) */
40 struct ly_out *out;
Radek Krejci535ea9f2020-05-29 16:01:05 +020041
Radek Krejcie9f13b12020-11-09 17:42:04 +010042 struct ly_set searchpaths;
Radek Krejcied5acc52019-04-25 15:57:04 +020043
Radek Krejcie9f13b12020-11-09 17:42:04 +010044 /* options flags */
45 uint8_t list; /* -l option to print list of schemas */
Radek Krejcied5acc52019-04-25 15:57:04 +020046
aPiecek2cfeeae2021-04-21 13:17:34 +020047 /* line length for 'tree' format */
48 size_t line_length; /* --tree-line-length */
49
Radek Krejcie9f13b12020-11-09 17:42:04 +010050 /*
51 * schema
52 */
Michal Vaskofc5a1d12021-11-22 10:00:27 +010053 /* set schema modules' features via --features option (struct schema_features *) */
Radek Krejcie9f13b12020-11-09 17:42:04 +010054 struct ly_set schema_features;
55
56 /* set of loaded schema modules (struct lys_module *) */
57 struct ly_set schema_modules;
58
59 /* options to parse and print schema modules */
60 uint32_t schema_parse_options;
61 uint32_t schema_print_options;
62
63 /* specification of printing schema node subtree, option --schema-node */
64 const char *schema_node_path;
65 const struct lysc_node *schema_node;
Michal Vaskofe74d1e2021-09-30 13:17:36 +020066 const char *submodule;
Radek Krejcie9f13b12020-11-09 17:42:04 +010067
68 /* value of --format in case of schema format */
69 LYS_OUTFORMAT schema_out_format;
70
71 /*
72 * data
73 */
74 /* various options based on --type option */
Michal Vaskoe0665742021-02-11 11:08:44 +010075 enum lyd_type data_type;
Radek Krejcie9f13b12020-11-09 17:42:04 +010076 uint32_t data_parse_options;
77 uint32_t data_validate_options;
78 uint32_t data_print_options;
79
80 /* flag for --merge option */
81 uint8_t data_merge;
82
83 /* value of --format in case of data format */
84 LYD_FORMAT data_out_format;
85
86 /* input data files (struct cmdline_file *) */
87 struct ly_set data_inputs;
88
Radek Krejcie9f13b12020-11-09 17:42:04 +010089 /* storage for --operational */
90 struct cmdline_file data_operational;
91};
92
93static void
94erase_context(struct context *c)
95{
96 /* data */
97 ly_set_erase(&c->data_inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +010098 ly_in_free(c->data_operational.in, 1);
99
100 /* schema */
101 ly_set_erase(&c->schema_features, free_features);
102 ly_set_erase(&c->schema_modules, NULL);
103
104 /* context */
105 ly_set_erase(&c->searchpaths, NULL);
106
107 ly_out_free(c->out, NULL, 0);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200108 ly_ctx_destroy(c->ctx);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100109}
110
111static void
112version(void)
113{
114 printf("yanglint %s\n", PROJECT_VERSION);
115}
116
117static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200118help(int shortout)
119{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100120
121 printf("Usage:\n"
122 " yanglint [Options] [-f { yang | yin | info}] <schema>...\n"
123 " Validates the YANG module in <schema>, and all its dependencies.\n\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +0100124 " yanglint [Options] [-f { xml | json }] <schema>... <file> ...\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100125 " Validates the YANG modeled data in <file> according to the <schema>.\n\n"
126 " yanglint\n"
127 " Starts interactive mode with more features.\n\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200128
129 if (shortout) {
130 return;
131 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100132 printf("Options:\n"
133 " -h, --help Show this help message and exit.\n"
134 " -v, --version Show version number and exit.\n"
135 " -V, --verbose Show verbose messages, can be used multiple times to\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200136 " increase verbosity.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100137
Michal Vaskoc40548b2021-09-30 13:05:47 +0200138 printf(" -f FORMAT, --format=FORMAT\n"
139 " Convert input into FORMAT. Supported formats: \n"
140 " yang, yin, tree and info for schemas,\n"
141 " xml, json for data.\n\n");
142
143 printf(" -p PATH, --path=PATH\n"
144 " Search path for schema (YANG/YIN) modules. The option can be\n"
145 " used multiple times. The current working directory and the\n"
146 " path of the module being added is used implicitly.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100147
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200148 printf(" -D, --disable-searchdir\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100149 " Do not implicitly search in current working directory for\n"
150 " schema modules. If specified a second time, do not even\n"
151 " search in the module directory (all modules must be \n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200152 " explicitly specified).\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100153
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200154 printf(" -F FEATURES, --features=FEATURES\n"
Michal Vaskob0d0adf2021-11-22 10:01:34 +0100155 " Specific module features to support in the form <module-name>:(<feature>,)*\n"
156 " Use <feature> '*' to enable all features of a module. This option can be\n"
157 " specified multiple times, to enable features in multiple modules. If this\n"
158 " option is not specified, all the features in all the implemented modules\n"
159 " are enabled.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100160
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200161 printf(" -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100162 " Make the imported modules \"referenced\" from any loaded\n"
163 " module also implemented. If specified a second time, all the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200164 " modules are set implemented.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100165
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200166 printf(" -P PATH, --schema-node=PATH\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100167 " Print only the specified subtree of the schema.\n"
168 " The PATH is the XPath subset mentioned in documentation as\n"
169 " the Path format. The option can be combined with --single-node\n"
170 " option to print information only about the specified node.\n"
171 " -q, --single-node\n"
172 " Supplement to the --schema-node option to print information\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200173 " only about a single node specified as PATH argument.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100174
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200175 printf(" -s SUBMODULE, --submodule=SUBMODULE\n"
176 " Print the specific submodule instead of the main module.\n\n");
177
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200178 printf(" -n, --not-strict\n"
Michal Vasko667ce6b2021-01-25 15:00:27 +0100179 " Do not require strict data parsing (silently skip unknown data),\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200180 " has no effect for schemas.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100181
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200182 printf(" -e, --present Validate only with the schema modules whose data actually\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100183 " exist in the provided input data files. Takes effect only\n"
184 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
185 " mandatory nodes from modules which data are not present in the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200186 " provided input data files.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100187
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200188 printf(" -t TYPE, --type=TYPE\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100189 " Specify data tree type in the input data file(s):\n"
190 " data - Complete datastore with status data (default type).\n"
191 " config - Configuration datastore (without status data).\n"
192 " get - Result of the NETCONF <get> operation.\n"
193 " getconfig - Result of the NETCONF <get-config> operation.\n"
194 " edit - Content of the NETCONF <edit-config> operation.\n"
195 " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n"
196 " RPC/Action input statement.\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +0100197 " reply - Reply to the RPC/Action. Note that the reply data are\n"
198 " expected inside a container representing the original\n"
199 " RPC/Action. This is necessary to identify appropriate\n"
200 " data definitions in the schema module.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100201 " notif - Notification instance (content of the <notification>\n"
Michal Vaskoc40548b2021-09-30 13:05:47 +0200202 " element without <eventTime>).\n\n");
203
204 printf(" -d MODE, --default=MODE\n"
205 " Print data with default values, according to the MODE\n"
206 " (to print attributes, ietf-netconf-with-defaults model\n"
207 " must be loaded):\n"
208 " all - Add missing default nodes.\n"
209 " all-tagged - Add missing default nodes and mark all the default\n"
210 " nodes with the attribute.\n"
211 " trim - Remove all nodes with a default value.\n"
212 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n");
213
214 printf(" -l, --list Print info about the loaded schemas.\n"
215 " (i - imported module, I - implemented module)\n"
216 " In case the -f option with data encoding is specified,\n"
217 " the list is printed as ietf-yang-library data.\n\n");
218
219 printf(" -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n"
220 " The limit of the maximum line length on which the 'tree'\n"
221 " format will try to be printed.\n\n");
222
223 printf(" -o OUTFILE, --output=OUTFILE\n"
224 " Write the output to OUTFILE instead of stdout.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100225
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200226 printf(" -O FILE, --operational=FILE\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100227 " Provide optional data to extend validation of the 'rpc',\n"
228 " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n"
229 " the :running configuration datastore and state data\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200230 " (operational datastore) referenced from the RPC/Notification.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100231
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200232 printf(" -m, --merge Merge input data files into a single tree and validate at\n"
233 " once.The option has effect only for 'data' and 'config' TYPEs.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100234
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200235 printf(" -y, --yang-library\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100236 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
237 " Note that this module includes definitions of mandatory state\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200238 " data that can result in unexpected data validation errors.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200239
240#ifndef NDEBUG
241 printf(" -G GROUPS, --debug=GROUPS\n"
242 " Enable printing of specific debugging message group\n"
243 " (nothing will be printed unless verbosity is set to debug):\n"
244 " <group>[,<group>]* (dict, xpath)\n\n");
245#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200246}
247
Radek Krejcie9f13b12020-11-09 17:42:04 +0100248static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200249libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
250{
251 char *levstr;
252
Radek Krejcie9f13b12020-11-09 17:42:04 +0100253 switch (level) {
254 case LY_LLERR:
255 levstr = "err :";
256 break;
257 case LY_LLWRN:
258 levstr = "warn:";
259 break;
260 case LY_LLVRB:
261 levstr = "verb:";
262 break;
263 default:
264 levstr = "dbg :";
265 break;
266 }
267 if (path) {
268 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
269 } else {
270 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200271 }
272}
273
Michal Vasko91b17482021-11-22 10:03:23 +0100274static struct schema_features *
275get_features_not_applied(const struct ly_set *fset)
276{
277 for (uint32_t u = 0; u < fset->count; ++u) {
278 struct schema_features *sf = fset->objs[u];
279 if (!sf->applied) {
280 return sf;
281 }
282 }
283
284 return NULL;
285}
286
Radek Krejcie9f13b12020-11-09 17:42:04 +0100287static int
288fill_context_inputs(int argc, char *argv[], struct context *c)
289{
Radek Krejcif53b0d52020-12-03 11:29:24 +0100290 struct ly_in *in;
Michal Vasko91b17482021-11-22 10:03:23 +0100291 struct schema_features *sf;
292 const char *all_features[] = {"*", NULL};
Radek Krejcie9f13b12020-11-09 17:42:04 +0100293
294 /* process the operational content if any */
295 if (c->data_operational.path) {
296 if (get_input(c->data_operational.path, NULL, &c->data_operational.format, &c->data_operational.in)) {
297 return -1;
298 }
299 }
300
301 for (int i = 0; i < argc - optind; i++) {
302 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
303 LYD_FORMAT format_data = LYD_UNKNOWN;
Radek Krejcif53b0d52020-12-03 11:29:24 +0100304 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100305
306 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100307 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100308 }
309
310 if (format_schema) {
311 LY_ERR ret;
312 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
313 char *dir, *module;
Michal Vaskob0d0adf2021-11-22 10:01:34 +0100314 const char **features;
Michal Vasko4de7d072021-07-09 09:13:18 +0200315 struct lys_module *mod;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100316
317 if (parse_schema_path(argv[optind + i], &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100318 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100319 }
320
321 /* add temporarily also the path of the module itself */
322 if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {
323 path_unset = 0;
324 }
325
326 /* get features list for this module */
Michal Vasko703370c2021-10-19 14:07:16 +0200327 if (!c->schema_features.count) {
Michal Vaskob0d0adf2021-11-22 10:01:34 +0100328 features = all_features;
329 } else {
330 get_features(&c->schema_features, module, &features);
Michal Vasko703370c2021-10-19 14:07:16 +0200331 }
332
Radek Krejcie9f13b12020-11-09 17:42:04 +0100333 /* temporary cleanup */
334 free(dir);
335 free(module);
336
Michal Vasko703370c2021-10-19 14:07:16 +0200337 /* parse module */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100338 ret = lys_parse(c->ctx, in, format_schema, features, &mod);
339 ly_ctx_unset_searchdir_last(c->ctx, path_unset);
Radek Krejcif53b0d52020-12-03 11:29:24 +0100340 ly_in_free(in, 1);
341 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100342 if (ret) {
343 YLMSG_E("Processing schema module from %s failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100344 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100345 }
346
347 if (c->schema_out_format) {
348 /* modules will be printed */
349 if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
350 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100351 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100352 }
353 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100354 } else if (format_data) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100355 if (!fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100356 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100357 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100358 in = NULL;
Radek Krejci3329b652020-12-05 23:54:07 +0100359 } else {
360 ly_in_free(in, 1);
361 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100362 }
363 }
364
Michal Vasko91b17482021-11-22 10:03:23 +0100365 /* check that all secified features were applied */
366 sf = get_features_not_applied(&c->schema_features);
367 if (sf) {
368 if (ly_ctx_get_module_implemented(c->ctx, sf->mod_name)) {
369 YLMSG_E("Specified features not applied, module \"%s\" must be parsed explicitly.\n", sf->mod_name);
370 } else if (ly_ctx_get_module_latest(c->ctx, sf->mod_name)) {
371 YLMSG_E("Specified features not applied, module \"%s\" not implemented.\n", sf->mod_name);
372 } else {
373 YLMSG_E("Specified features not applied, module \"%s\" not found.\n", sf->mod_name);
374 }
375 goto error;
376 }
377
Radek Krejcie9f13b12020-11-09 17:42:04 +0100378 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100379
380error:
381 ly_in_free(in, 1);
382 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100383}
384
385/**
386 * @brief Process command line options and store the settings into the context.
387 *
388 * return -1 in case of error;
389 * return 0 in case of success and ready to process
390 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200391 */
392static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100393fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200394{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100395 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200396
Radek Krejcie9f13b12020-11-09 17:42:04 +0100397 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200398 struct option options[] = {
Radek Krejcied5acc52019-04-25 15:57:04 +0200399 {"help", no_argument, NULL, 'h'},
Michal Vaskoc40548b2021-09-30 13:05:47 +0200400 {"version", no_argument, NULL, 'v'},
401 {"verbose", no_argument, NULL, 'V'},
402 {"format", required_argument, NULL, 'f'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100403 {"path", required_argument, NULL, 'p'},
Michal Vaskoc40548b2021-09-30 13:05:47 +0200404 {"disable-searchdir", no_argument, NULL, 'D'},
405 {"features", required_argument, NULL, 'F'},
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200406 {"make-implemented", no_argument, NULL, 'i'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100407 {"schema-node", required_argument, NULL, 'P'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800408 {"single-node", no_argument, NULL, 'q'},
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200409 {"submodule", required_argument, NULL, 's'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100410 {"not-strict", no_argument, NULL, 'n'},
Michal Vaskoc40548b2021-09-30 13:05:47 +0200411 {"present", no_argument, NULL, 'e'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200412 {"type", required_argument, NULL, 't'},
Michal Vaskoc40548b2021-09-30 13:05:47 +0200413 {"default", required_argument, NULL, 'd'},
414 {"list", no_argument, NULL, 'l'},
415 {"tree-line-length", required_argument, NULL, 'L'},
416 {"output", required_argument, NULL, 'o'},
417 {"operational", required_argument, NULL, 'O'},
418 {"merge", no_argument, NULL, 'm'},
419 {"yang-library", no_argument, NULL, 'y'},
420#ifndef NDEBUG
421 {"debug", required_argument, NULL, 'G'},
422#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200423 {NULL, 0, NULL, 0}
424 };
Radek Krejcied5acc52019-04-25 15:57:04 +0200425
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100426 uint16_t options_ctx = YL_DEFAULT_CTX_OPTIONS;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100427 uint8_t data_type_set = 0;
428
Michal Vasko667ce6b2021-01-25 15:00:27 +0100429 c->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;
aPiecek2cfeeae2021-04-21 13:17:34 +0200430 c->line_length = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100431
Radek Krejcied5acc52019-04-25 15:57:04 +0200432#ifndef NDEBUG
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200433 while ((opt = getopt_long(argc, argv, "hvVf:p:DF:iP:qs:net:d:lL:o:Omy", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200434#else
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200435 while ((opt = getopt_long(argc, argv, "hvVf:p:DF:iP:qs:net:d:lL:o:OmyG:", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200436#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200437 switch (opt) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200438 case 'h': /* --help */
439 help(0);
440 return 1;
441
442 case 'v': /* --version */
443 version();
444 return 1;
445
446 case 'V': { /* --verbose */
447 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
448 ly_log_level(verbosity);
449
450 if (verbosity < LY_LLDBG) {
451 ly_log_level(verbosity + 1);
Radek Krejcied5acc52019-04-25 15:57:04 +0200452 }
453 break;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200454 } /* case 'V' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100455
456 case 'f': /* --format */
457 if (!strcasecmp(optarg, "yang")) {
458 c->schema_out_format = LYS_OUT_YANG;
459 c->data_out_format = 0;
460 } else if (!strcasecmp(optarg, "yin")) {
461 c->schema_out_format = LYS_OUT_YIN;
462 c->data_out_format = 0;
463 } else if (!strcasecmp(optarg, "info")) {
464 c->schema_out_format = LYS_OUT_YANG_COMPILED;
465 c->data_out_format = 0;
466 } else if (!strcasecmp(optarg, "tree")) {
467 c->schema_out_format = LYS_OUT_TREE;
468 c->data_out_format = 0;
469 } else if (!strcasecmp(optarg, "xml")) {
470 c->schema_out_format = 0;
471 c->data_out_format = LYD_XML;
472 } else if (!strcasecmp(optarg, "json")) {
473 c->schema_out_format = 0;
474 c->data_out_format = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200475 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100476 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200477 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100478 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200479 }
480 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100481
Michal Vaskoc40548b2021-09-30 13:05:47 +0200482 case 'p': { /* --path */
483 struct stat st;
484
485 if (stat(optarg, &st) == -1) {
486 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
487 return -1;
488 }
489 if (!S_ISDIR(st.st_mode)) {
490 YLMSG_E("Provided search path is not a directory.\n");
491 return -1;
492 }
493
494 if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
495 YLMSG_E("Storing searchpath failed.\n");
496 return -1;
497 }
498
499 break;
500 } /* case 'p' */
501
502 case 'D': /* --disable-search */
503 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
504 YLMSG_W("The -D option specified too many times.\n");
505 }
506 if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
507 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
508 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
509 } else {
510 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
511 }
512 break;
513
514 case 'F': /* --features */
515 if (parse_features(optarg, &c->schema_features)) {
516 return -1;
517 }
518 break;
519
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200520 case 'i': /* --make-implemented */
Michal Vaskoc40548b2021-09-30 13:05:47 +0200521 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
522 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
523 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
524 } else {
525 options_ctx |= LY_CTX_REF_IMPLEMENTED;
526 }
527 break;
528
Radek Krejcie9f13b12020-11-09 17:42:04 +0100529 case 'P': /* --schema-node */
530 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200531 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100532
533 case 'q': /* --single-node */
534 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
535 break;
536
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200537 case 's': /* --submodule */
538 c->submodule = optarg;
539 break;
540
Michal Vasko667ce6b2021-01-25 15:00:27 +0100541 case 'n': /* --not-strict */
542 c->data_parse_options &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100543 break;
544
545 case 'e': /* --present */
546 c->data_validate_options |= LYD_VALIDATE_PRESENT;
547 break;
548
549 case 't': /* --type */
550 if (data_type_set) {
551 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
552 return -1;
553 }
554
555 if (!strcasecmp(optarg, "config")) {
556 c->data_parse_options |= LYD_PARSE_NO_STATE;
Michal Vaskof6bda332021-02-01 08:59:03 +0100557 c->data_validate_options |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100558 } else if (!strcasecmp(optarg, "get")) {
559 c->data_parse_options |= LYD_PARSE_ONLY;
560 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
561 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
562 } else if (!strcasecmp(optarg, "edit")) {
563 c->data_parse_options |= LYD_PARSE_ONLY;
564 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100565 c->data_type = LYD_TYPE_RPC_YANG;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100566 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100567 c->data_type = LYD_TYPE_REPLY_YANG;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100568 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100569 c->data_type = LYD_TYPE_NOTIF_YANG;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100570 } else if (!strcasecmp(optarg, "data")) {
571 /* default option */
572 } else {
573 YLMSG_E("Unknown data tree type %s\n", optarg);
574 help(1);
575 return -1;
576 }
577
578 data_type_set = 1;
579 break;
580
Michal Vaskoc40548b2021-09-30 13:05:47 +0200581 case 'd': /* --default */
582 if (!strcasecmp(optarg, "all")) {
583 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
584 } else if (!strcasecmp(optarg, "all-tagged")) {
585 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
586 } else if (!strcasecmp(optarg, "trim")) {
587 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
588 } else if (!strcasecmp(optarg, "implicit-tagged")) {
589 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
590 } else {
591 YLMSG_E("Unknown default mode %s\n", optarg);
592 help(1);
593 return -1;
594 }
595 break;
596
597 case 'l': /* --list */
598 c->list = 1;
599 break;
600
601 case 'L': /* --tree-line-length */
602 c->line_length = atoi(optarg);
603 break;
604
605 case 'o': /* --output */
606 if (c->out) {
607 YLMSG_E("Only a single output can be specified.\n");
608 return -1;
609 } else {
610 if (ly_out_new_filepath(optarg, &c->out)) {
611 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
612 return -1;
613 }
614 }
615 break;
616
Radek Krejcie9f13b12020-11-09 17:42:04 +0100617 case 'O': /* --operational */
618 if (c->data_operational.path) {
619 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
620 return -1;
621 }
622 c->data_operational.path = optarg;
623 break;
624
Radek Krejcie9f13b12020-11-09 17:42:04 +0100625 case 'm': /* --merge */
626 c->data_merge = 1;
627 break;
628
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100629 case 'y': /* --yang-library */
630 options_ctx &= ~LY_CTX_NO_YANGLIBRARY;
631 break;
632
Radek Krejcied5acc52019-04-25 15:57:04 +0200633#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100634 case 'G': { /* --debug */
635 uint32_t dbg_groups = 0;
636 const char *ptr = optarg;
637
Radek Krejcied5acc52019-04-25 15:57:04 +0200638 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100639 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100640 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100641 ptr += sizeof "dict" - 1;
642 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100643 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100644 ptr += sizeof "xpath" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200645 }
646
647 if (ptr[0]) {
648 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100649 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
650 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200651 }
652 ++ptr;
653 }
654 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100655 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200656 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100657 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200658#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100659 } /* switch */
660 }
661
Michal Vaskob0d0adf2021-11-22 10:01:34 +0100662 /* set imp feature flag if all should be enabled */
663 if (!c->schema_features.count) {
664 options_ctx |= LY_CTX_ENABLE_IMP_FEATURES;
665 }
666
Radek Krejcie9f13b12020-11-09 17:42:04 +0100667 /* libyang context */
668 if (ly_ctx_new(NULL, options_ctx, &c->ctx)) {
669 YLMSG_E("Unable to create libyang context.\n");
670 return -1;
671 }
672 for (uint32_t u = 0; u < c->searchpaths.count; ++u) {
673 ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]);
674 }
675
676 /* additional checks for the options combinations */
677 if (!c->list && (optind >= argc)) {
678 help(1);
679 YLMSG_E("Missing <schema> to process.\n");
680 return 1;
681 }
682
683 if (c->data_merge) {
684 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
685 /* switch off the option, incompatible input data type */
686 c->data_merge = 0;
687 } else {
688 /* postpone validation after the merge of all the input data */
689 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200690 }
691 }
692
Radek Krejcie9f13b12020-11-09 17:42:04 +0100693 if (c->data_operational.path && !c->data_type) {
694 YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
695 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200696 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100697
aPiecek2cfeeae2021-04-21 13:17:34 +0200698 if ((c->schema_out_format != LYS_OUT_TREE) && c->line_length) {
699 YLMSG_E("--tree-line-length take effect only in case of the tree output format.\n");
700 }
701
Radek Krejcie9f13b12020-11-09 17:42:04 +0100702 /* default output stream */
703 if (!c->out) {
704 if (ly_out_new_file(stdout, &c->out)) {
705 YLMSG_E("Unable to set stdout as output.\n");
706 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200707 }
708 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100709
aPiecek7ac66d52021-05-20 07:54:07 +0200710 if (c->schema_out_format == LYS_OUT_TREE) {
711 /* print tree from lysc_nodes */
712 ly_ctx_set_options(c->ctx, LY_CTX_SET_PRIV_PARSED);
713 }
714
Radek Krejcie9f13b12020-11-09 17:42:04 +0100715 /* process input files provided as standalone command line arguments,
716 * schema modules are parsed and inserted into the context,
717 * data files are just checked and prepared into internal structures for further processing */
718 ret = fill_context_inputs(argc, argv, c);
719 if (ret) {
720 return ret;
721 }
722
723 /* the second batch of checks */
724 if (c->schema_print_options && !c->schema_out_format) {
725 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
726 }
727 if (c->schema_parse_options && !c->schema_modules.count) {
728 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
729 }
730 if (c->data_print_options && !c->data_out_format) {
731 YLMSG_W("data printer options specified, but the data output format is missing.\n");
732 }
Michal Vasko667ce6b2021-01-25 15:00:27 +0100733 if (((c->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || c->data_type) && !c->data_inputs.count) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100734 YLMSG_W("Data parser options specified, but no data input file provided.\n");
735 }
736
737 if (c->schema_node_path) {
738 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
739 if (!c->schema_node) {
740 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
741
742 if (!c->schema_node) {
743 YLMSG_E("Invalid schema path.\n");
744 return -1;
745 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200746 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200747 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100748
Radek Krejcie9f13b12020-11-09 17:42:04 +0100749 return 0;
750}
751
752int
753main_ni(int argc, char *argv[])
754{
755 int ret = EXIT_SUCCESS, r;
756 struct context c = {0};
Radek Krejcied5acc52019-04-25 15:57:04 +0200757
758 /* set callback for printing libyang messages */
759 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100760
761 r = fill_context(argc, argv, &c);
762 if (r < 0) {
763 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200764 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100765 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200766 goto cleanup;
767 }
768
Radek Krejcie9f13b12020-11-09 17:42:04 +0100769 /* do the required job - parse, validate, print */
770
771 if (c.list) {
772 /* print the list of schemas */
773 print_list(c.out, c.ctx, c.data_out_format);
Radek Krejci047d64e2020-12-03 12:57:10 +0100774 } else {
775 if (c.schema_out_format) {
776 if (c.schema_node) {
777 ret = lys_print_node(c.out, c.schema_node, c.schema_out_format, 0, c.schema_print_options);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100778 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100779 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100780 goto cleanup;
781 }
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200782 } else if (c.submodule) {
783 const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(c.ctx, c.submodule);
784 if (!submod) {
785 YLMSG_E("Unable to find submodule %s.\n", c.submodule);
786 goto cleanup;
787 }
788
789 ret = lys_print_submodule(c.out, submod, c.schema_out_format, c.line_length, c.schema_print_options);
790 if (ret) {
791 YLMSG_E("Unable to print submodule %s.\n", submod->name);
792 goto cleanup;
793 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100794 } else {
795 for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
aPiecek2cfeeae2021-04-21 13:17:34 +0200796 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format,
797 c.line_length, c.schema_print_options);
aPiecek8f0b7882021-05-21 10:18:36 +0200798 /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
799 if ((c.schema_out_format == LYS_OUT_TREE) && (u + 1 < c.schema_modules.count)) {
800 ly_print(c.out, "\n");
801 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100802 if (ret) {
803 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
804 goto cleanup;
805 }
806 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100807 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200808 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100809
810 /* do the data validation despite the schema was printed */
811 if (c.data_inputs.size) {
812 if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out,
813 c.data_parse_options, c.data_validate_options, c.data_print_options,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100814 &c.data_operational, &c.data_inputs, NULL)) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100815 goto cleanup;
816 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200817 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200818 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200819
820cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100821 /* cleanup */
822 erase_context(&c);
Radek Krejcied5acc52019-04-25 15:57:04 +0200823
Radek Krejcied5acc52019-04-25 15:57:04 +0200824 return ret;
825}