blob: 94011b4fedff23e57c0af397c4006ea47a6c0b67 [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"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "tools/config.h"
30
Radek Krejcie9f13b12020-11-09 17:42:04 +010031/**
32 * @brief Context structure to hold and pass variables in a structured form.
33 */
34struct context {
35 /* libyang context for the run */
36 struct ly_ctx *ctx;
Radek Krejci535ea9f2020-05-29 16:01:05 +020037
Radek Krejcie9f13b12020-11-09 17:42:04 +010038 /* prepared output (--output option or stdout by default) */
39 struct ly_out *out;
Radek Krejci535ea9f2020-05-29 16:01:05 +020040
Radek Krejcie9f13b12020-11-09 17:42:04 +010041 struct ly_set searchpaths;
Radek Krejcied5acc52019-04-25 15:57:04 +020042
Radek Krejcie9f13b12020-11-09 17:42:04 +010043 /* options flags */
44 uint8_t list; /* -l option to print list of schemas */
Radek Krejcied5acc52019-04-25 15:57:04 +020045
Radek Krejcie9f13b12020-11-09 17:42:04 +010046 /*
47 * schema
48 */
49 /* set schema modules' features via --feature option (struct schema_features *) */
50 struct ly_set schema_features;
51
52 /* set of loaded schema modules (struct lys_module *) */
53 struct ly_set schema_modules;
54
55 /* options to parse and print schema modules */
56 uint32_t schema_parse_options;
57 uint32_t schema_print_options;
58
59 /* specification of printing schema node subtree, option --schema-node */
60 const char *schema_node_path;
61 const struct lysc_node *schema_node;
62
63 /* value of --format in case of schema format */
64 LYS_OUTFORMAT schema_out_format;
65
66 /*
67 * data
68 */
69 /* various options based on --type option */
70 uint8_t data_type; /* values taken from LYD_VALIDATE_OP and extended by 0 for standard data tree */
71 uint32_t data_parse_options;
72 uint32_t data_validate_options;
73 uint32_t data_print_options;
74
75 /* flag for --merge option */
76 uint8_t data_merge;
77
78 /* value of --format in case of data format */
79 LYD_FORMAT data_out_format;
80
81 /* input data files (struct cmdline_file *) */
82 struct ly_set data_inputs;
83
84 /* the request files for reply data (struct cmdline_file *)
85 * In case the data_type is PARSE_REPLY, the parsing function requires information about the request for this reply.
86 * One way to provide the request is a data file containing the full RPC/Action request which will be parsed.
87 * Alternatively, it can be set as the Path of the requested RPC/Action and in that case it is stored in
88 * data_request_paths.
89 */
90 struct ly_set data_requests;
91 /* An alternative way of providing requests for parsing data replies, instead of providing full
92 * request in a data file, only the Path of the requested RPC/Action is provided and stored as
93 * const char *. Note that the number of items in the set must be 1 (1 applies to all) or equal to
94 * data_inputs_count (1 to 1 mapping).
95 */
96 struct ly_set data_request_paths;
97
98 /* storage for --operational */
99 struct cmdline_file data_operational;
100};
101
102static void
103erase_context(struct context *c)
104{
105 /* data */
106 ly_set_erase(&c->data_inputs, free_cmdline_file);
107 ly_set_erase(&c->data_requests, free_cmdline_file);
108 ly_set_erase(&c->data_request_paths, NULL);
109 ly_in_free(c->data_operational.in, 1);
110
111 /* schema */
112 ly_set_erase(&c->schema_features, free_features);
113 ly_set_erase(&c->schema_modules, NULL);
114
115 /* context */
116 ly_set_erase(&c->searchpaths, NULL);
117
118 ly_out_free(c->out, NULL, 0);
119 ly_ctx_destroy(c->ctx, NULL);
120}
121
122static void
123version(void)
124{
125 printf("yanglint %s\n", PROJECT_VERSION);
126}
127
128static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200129help(int shortout)
130{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100131
132 printf("Usage:\n"
133 " yanglint [Options] [-f { yang | yin | info}] <schema>...\n"
134 " Validates the YANG module in <schema>, and all its dependencies.\n\n"
135 " yanglint [Options] [-f { xml | json }] <schema>... <file> [<request>]...\n"
136 " Validates the YANG modeled data in <file> according to the <schema>.\n\n"
137 " yanglint\n"
138 " Starts interactive mode with more features.\n\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200139
140 if (shortout) {
141 return;
142 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100143 printf("Options:\n"
144 " -h, --help Show this help message and exit.\n"
145 " -v, --version Show version number and exit.\n"
146 " -V, --verbose Show verbose messages, can be used multiple times to\n"
147 " increase verbosity.\n"
Radek Krejcied5acc52019-04-25 15:57:04 +0200148#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100149 " -G GROUPS, --debug=GROUPS\n"
150 " Enable printing of specific debugging message group\n"
151 " (nothing will be printed unless verbosity is set to debug):\n"
152 " <group>[,<group>]* (dict, xpath)\n\n"
Radek Krejcied5acc52019-04-25 15:57:04 +0200153#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100154
155 " -d MODE, --default=MODE\n"
156 " Print data with default values, according to the MODE\n"
157 " (to print attributes, ietf-netconf-with-defaults model\n"
158 " must be loaded):\n"
159 " all - Add missing default nodes.\n"
160 " all-tagged - Add missing default nodes and mark all the default\n"
161 " nodes with the attribute.\n"
162 " trim - Remove all nodes with a default value.\n"
163 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n"
164
165 " -D, --disable-searchdir\n"
166 " Do not implicitly search in current working directory for\n"
167 " schema modules. If specified a second time, do not even\n"
168 " search in the module directory (all modules must be \n"
169 " explicitly specified).\n\n"
170
171 " -p PATH, --path=PATH\n"
172 " Search path for schema (YANG/YIN) modules. The option can be\n"
173 " used multiple times. The current working directory and the\n"
174 " path of the module being added is used implicitly.\n\n"
175
176 " -F FEATURES, --features=FEATURES\n"
177 " Features to support, default all.\n"
178 " <modname>:[<feature>,]*\n\n"
179
180 " -i, --makeimplemented\n"
181 " Make the imported modules \"referenced\" from any loaded\n"
182 " module also implemented. If specified a second time, all the\n"
183 " modules are set implemented.\n\n"
184
185 " -l, --list Print info about the loaded schemas.\n"
186 " (i - imported module, I - implemented module)\n"
187 " In case the -f option with data encoding is specified,\n"
188 " the list is printed as ietf-yang-library data.\n\n"
189
190 " -o OUTFILE, --output=OUTFILE\n"
191 " Write the output to OUTFILE instead of stdout.\n\n"
192
193 " -f FORMAT, --format=FORMAT\n"
194 " Convert input into FORMAT. Supported formats: \n"
195 " yang, yin, tree and info for schemas,\n"
196 " xml, json for data.\n\n"
197
198 " -P PATH, --schema-node=PATH\n"
199 " Print only the specified subtree of the schema.\n"
200 " The PATH is the XPath subset mentioned in documentation as\n"
201 " the Path format. The option can be combined with --single-node\n"
202 " option to print information only about the specified node.\n"
203 " -q, --single-node\n"
204 " Supplement to the --schema-node option to print information\n"
205 " only about a single node specified as PATH argument.\n\n"
206
207 " -s, --strict Strict data parsing (do not skip unknown data), has no effect\n"
208 " for schemas.\n\n"
209
210 " -e, --present Validate only with the schema modules whose data actually\n"
211 " exist in the provided input data files. Takes effect only\n"
212 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
213 " mandatory nodes from modules which data are not present in the\n"
214 " provided input data files.\n\n"
215
216 " -t TYPE, --type=TYPE\n"
217 " Specify data tree type in the input data file(s):\n"
218 " data - Complete datastore with status data (default type).\n"
219 " config - Configuration datastore (without status data).\n"
220 " get - Result of the NETCONF <get> operation.\n"
221 " getconfig - Result of the NETCONF <get-config> operation.\n"
222 " edit - Content of the NETCONF <edit-config> operation.\n"
223 " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n"
224 " RPC/Action input statement.\n"
225 " reply - Reply to the RPC/Action. Besides the reply itself,\n"
226 " yanglint(1) requires information about the request for\n"
227 " the reply. The request (RPC/Action) can be provided as\n"
228 " the --request option or as another input data <file>\n"
229 " provided right after the reply data <file> and\n"
230 " containing complete RPC/Action for the reply.\n"
231 " notif - Notification instance (content of the <notification>\n"
232 " element without <eventTime>).\n"
233 " -r PATH, --request=PATH\n"
234 " The alternative way of providing request information for the\n"
235 " '--type=reply'. The PATH is the XPath subset described in\n"
236 " documentation as Path format. It is required to point to the\n"
237 " RPC or Action in the schema which is supposed to be a request\n"
238 " for the reply(ies) being parsed from the input data files.\n"
239 " In case of multiple input data files, the 'request' option can\n"
240 " be set once for all the replies or multiple times each for the\n"
241 " respective input data file.\n\n"
242
243 " -O FILE, --operational=FILE\n"
244 " Provide optional data to extend validation of the 'rpc',\n"
245 " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n"
246 " the :running configuration datastore and state data\n"
247 " (operational datastore) referenced from the RPC/Notification.\n\n"
248
249 " -m, --merge Merge input data files into a single tree and validate at\n"
250 " once.The option has effect only for 'data' and 'config' TYPEs.\n\n"
251
252#if 0
253 " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n"
254 "Tree output specific options:\n"
255 " --tree-help - Print help on tree symbols and exit.\n"
256 " --tree-print-groupings\n"
257 " Print top-level groupings in a separate section.\n"
258 " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n"
259 " --tree-no-leafref-target\n"
260 " Do not print target nodes of leafrefs.\n"
261 " --tree-path=SCHEMA_PATH\n"
262 " Print only the specified subtree.\n"
263 " --tree-line-length=LINE_LENGTH\n"
264 " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n"
265 " can often appear).\n\n"
266#endif
267 "\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200268}
269
Radek Krejcie9f13b12020-11-09 17:42:04 +0100270static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200271libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
272{
273 char *levstr;
274
Radek Krejcie9f13b12020-11-09 17:42:04 +0100275 switch (level) {
276 case LY_LLERR:
277 levstr = "err :";
278 break;
279 case LY_LLWRN:
280 levstr = "warn:";
281 break;
282 case LY_LLVRB:
283 levstr = "verb:";
284 break;
285 default:
286 levstr = "dbg :";
287 break;
288 }
289 if (path) {
290 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
291 } else {
292 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200293 }
294}
295
Radek Krejcie9f13b12020-11-09 17:42:04 +0100296static int
297fill_context_inputs(int argc, char *argv[], struct context *c)
298{
299 struct ly_in *in;
300 uint8_t request_expected = 0;
301
302 /* process the operational content if any */
303 if (c->data_operational.path) {
304 if (get_input(c->data_operational.path, NULL, &c->data_operational.format, &c->data_operational.in)) {
305 return -1;
306 }
307 }
308
309 for (int i = 0; i < argc - optind; i++) {
310 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
311 LYD_FORMAT format_data = LYD_UNKNOWN;
312 in = NULL;
313
314 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
315 return -1;
316 }
317
318 if (format_schema) {
319 LY_ERR ret;
320 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
321 char *dir, *module;
322 const char *fall = "*";
323 const char **features = &fall;
324 const struct lys_module *mod;
325
326 if (parse_schema_path(argv[optind + i], &dir, &module)) {
327 return -1;
328 }
329
330 /* add temporarily also the path of the module itself */
331 if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {
332 path_unset = 0;
333 }
334
335 /* get features list for this module */
336 get_features(&c->schema_features, module, &features);
337
338 /* temporary cleanup */
339 free(dir);
340 free(module);
341
342 ret = lys_parse(c->ctx, in, format_schema, features, &mod);
343 ly_ctx_unset_searchdir_last(c->ctx, path_unset);
344 ly_in_free(in, 1);
345 if (ret) {
346 YLMSG_E("Processing schema module from %s failed.\n", argv[optind + i]);
347 return -1;
348 }
349
350 if (c->schema_out_format) {
351 /* modules will be printed */
352 if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
353 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
354 return -1;
355 }
356 }
357 } else if (request_expected) {
358 if (fill_cmdline_file(&c->data_requests, in, argv[optind + i], format_data)) {
359 ly_in_free(in, 1);
360 return -1;
361 }
362
363 request_expected = 0;
364 } else if (format_data) {
365 struct cmdline_file *rec;
366
367 rec = fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data);
368 if (!rec) {
369 ly_in_free(in, 1);
370 return -1;
371 }
372
373 if ((c->data_type == LYD_VALIDATE_OP_REPLY) && !c->data_request_paths.count) {
374 /* requests for the replies are expected in another input file */
375 if (++i == argc - optind) {
376 /* there is no such file */
377 YLMSG_E("Missing request input file for the reply input file %s.\n", rec->path);
378 return -1;
379 }
380
381 request_expected = 1;
382 }
383 }
384 }
385
386 if (request_expected) {
387 YLMSG_E("Missing request input file for the reply input file %s.\n",
388 ((struct cmdline_file *)c->data_inputs.objs[c->data_inputs.count - 1])->path);
389 return -1;
390 }
391
392 return 0;
393}
394
395/**
396 * @brief Process command line options and store the settings into the context.
397 *
398 * return -1 in case of error;
399 * return 0 in case of success and ready to process
400 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200401 */
402static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100403fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200404{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100405 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200406
Radek Krejcie9f13b12020-11-09 17:42:04 +0100407 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200408 struct option options[] = {
Radek Krejciff61c882020-09-03 13:04:18 +0200409 {"default", required_argument, NULL, 'd'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100410 {"disable-searchdir", no_argument, NULL, 'D'},
411 {"present", no_argument, NULL, 'e'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200412 {"format", required_argument, NULL, 'f'},
413 {"features", required_argument, NULL, 'F'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100414#ifndef NDEBUG
415 {"debug", required_argument, NULL, 'G'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200416#endif
417 {"help", no_argument, NULL, 'h'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100418 {"makeimplemented", no_argument, NULL, 'i'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200419 {"list", no_argument, NULL, 'l'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200420 {"merge", no_argument, NULL, 'm'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200421 {"output", required_argument, NULL, 'o'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200422 {"operational", required_argument, NULL, 'O'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100423 {"path", required_argument, NULL, 'p'},
424 {"schema-node", required_argument, NULL, 'P'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800425 {"single-node", no_argument, NULL, 'q'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100426 {"request", required_argument, NULL, 'r'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200427 {"strict", no_argument, NULL, 's'},
428 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200429 {"version", no_argument, NULL, 'v'},
430 {"verbose", no_argument, NULL, 'V'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200431 {NULL, 0, NULL, 0}
432 };
Radek Krejcied5acc52019-04-25 15:57:04 +0200433
Radek Krejcie9f13b12020-11-09 17:42:04 +0100434 uint16_t options_ctx = 0;
435 uint8_t data_type_set = 0;
436
Radek Krejcied5acc52019-04-25 15:57:04 +0200437#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100438 while ((opt = getopt_long(argc, argv, "d:Df:F:hilmo:P:qr:st:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200439#else
Radek Krejcie9f13b12020-11-09 17:42:04 +0100440 while ((opt = getopt_long(argc, argv, "d:Df:F:G:hilmo:P:qr:st:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200441#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200442 switch (opt) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100443 case 'd': /* --default */
444 if (!strcasecmp(optarg, "all")) {
445 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
446 } else if (!strcasecmp(optarg, "all-tagged")) {
447 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
448 } else if (!strcasecmp(optarg, "trim")) {
449 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
450 } else if (!strcasecmp(optarg, "implicit-tagged")) {
451 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200452 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100453 YLMSG_E("Unknown default mode %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200454 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100455 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200456 }
457 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200458
Radek Krejcie9f13b12020-11-09 17:42:04 +0100459 case 'D': /* --disable-search */
Radek Krejcied5acc52019-04-25 15:57:04 +0200460 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100461 YLMSG_W("The -D option specified too many times.\n");
462 }
463 if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200464 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
465 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
466 } else {
467 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
468 }
469 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100470
471 case 'p': { /* --path */
472 struct stat st;
473
Radek Krejcied5acc52019-04-25 15:57:04 +0200474 if (stat(optarg, &st) == -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100475 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
476 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200477 }
478 if (!S_ISDIR(st.st_mode)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100479 YLMSG_E("Provided search path is not a directory.\n");
480 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200481 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100482
483 if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
484 YLMSG_E("Storing searchpath failed.\n");
485 return -1;
486 }
487
488 break;
489 } /* case 'p' */
490
491 case 'i': /* --makeimplemented */
492 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
493 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
494 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
495 } else {
496 options_ctx |= LY_CTX_REF_IMPLEMENTED;
497 }
498 break;
499
500 case 'F': /* --features */
501 if (parse_features(optarg, &c->schema_features)) {
502 return -1;
503 }
504 break;
505
506 case 'l': /* --list */
507 c->list = 1;
508 break;
509
510 case 'o': /* --output */
511 if (c->out) {
512 YLMSG_E("Only a single output can be specified.\n");
513 return -1;
514 } else {
515 if (ly_out_new_filepath(optarg, &c->out)) {
516 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
517 return -1;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200518 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200519 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200520 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100521
522 case 'f': /* --format */
523 if (!strcasecmp(optarg, "yang")) {
524 c->schema_out_format = LYS_OUT_YANG;
525 c->data_out_format = 0;
526 } else if (!strcasecmp(optarg, "yin")) {
527 c->schema_out_format = LYS_OUT_YIN;
528 c->data_out_format = 0;
529 } else if (!strcasecmp(optarg, "info")) {
530 c->schema_out_format = LYS_OUT_YANG_COMPILED;
531 c->data_out_format = 0;
532 } else if (!strcasecmp(optarg, "tree")) {
533 c->schema_out_format = LYS_OUT_TREE;
534 c->data_out_format = 0;
535 } else if (!strcasecmp(optarg, "xml")) {
536 c->schema_out_format = 0;
537 c->data_out_format = LYD_XML;
538 } else if (!strcasecmp(optarg, "json")) {
539 c->schema_out_format = 0;
540 c->data_out_format = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200541 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100542 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200543 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100544 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200545 }
546 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100547
548 case 'P': /* --schema-node */
549 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200550 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100551
552 case 'q': /* --single-node */
553 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
554 break;
555
556 case 's': /* --strict */
557 c->data_parse_options |= LYD_PARSE_STRICT;
558 break;
559
560 case 'e': /* --present */
561 c->data_validate_options |= LYD_VALIDATE_PRESENT;
562 break;
563
564 case 't': /* --type */
565 if (data_type_set) {
566 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
567 return -1;
568 }
569
570 if (!strcasecmp(optarg, "config")) {
571 c->data_parse_options |= LYD_PARSE_NO_STATE;
572 } else if (!strcasecmp(optarg, "get")) {
573 c->data_parse_options |= LYD_PARSE_ONLY;
574 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
575 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
576 } else if (!strcasecmp(optarg, "edit")) {
577 c->data_parse_options |= LYD_PARSE_ONLY;
578 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
579 c->data_type = LYD_VALIDATE_OP_RPC;
580 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
581 c->data_type = LYD_VALIDATE_OP_REPLY;
582 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
583 c->data_type = LYD_VALIDATE_OP_NOTIF;
584 } else if (!strcasecmp(optarg, "data")) {
585 /* default option */
586 } else {
587 YLMSG_E("Unknown data tree type %s\n", optarg);
588 help(1);
589 return -1;
590 }
591
592 data_type_set = 1;
593 break;
594
595 case 'O': /* --operational */
596 if (c->data_operational.path) {
597 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
598 return -1;
599 }
600 c->data_operational.path = optarg;
601 break;
602
603 case 'r': /* --request */
604 if (ly_set_add(&c->data_request_paths, optarg, 0, NULL)) {
605 YLMSG_E("Storing request path failed.\n");
606 return -1;
607 }
608 break;
609
610 case 'm': /* --merge */
611 c->data_merge = 1;
612 break;
613
614 case 'h': /* --help */
615 help(0);
616 return 1;
617
618 case 'v': /* --version */
619 version();
620 return 1;
621
622 case 'V': { /* --verbose */
623 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
624 ly_log_level(verbosity);
625
626 if (verbosity < LY_LLDBG) {
627 ly_log_level(verbosity + 1);
628 }
629 break;
630 } /* case 'V' */
631
Radek Krejcied5acc52019-04-25 15:57:04 +0200632#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100633 case 'G': { /* --debug */
634 uint32_t dbg_groups = 0;
635 const char *ptr = optarg;
636
Radek Krejcied5acc52019-04-25 15:57:04 +0200637 while (ptr[0]) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100638 if (!strncasecmp(ptr, "dict", 4)) {
639 dbg_groups |= LY_LDGDICT;
Radek Krejcied5acc52019-04-25 15:57:04 +0200640 ptr += 4;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100641 } else if (!strncasecmp(ptr, "xpath", 5)) {
642 dbg_groups |= LY_LDGXPATH;
Radek Krejcied5acc52019-04-25 15:57:04 +0200643 ptr += 5;
Radek Krejcied5acc52019-04-25 15:57:04 +0200644 }
645
646 if (ptr[0]) {
647 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100648 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
649 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200650 }
651 ++ptr;
652 }
653 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100654 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200655 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100656 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200657#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100658 } /* switch */
659 }
660
661 /* libyang context */
662 if (ly_ctx_new(NULL, options_ctx, &c->ctx)) {
663 YLMSG_E("Unable to create libyang context.\n");
664 return -1;
665 }
666 for (uint32_t u = 0; u < c->searchpaths.count; ++u) {
667 ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]);
668 }
669
670 /* additional checks for the options combinations */
671 if (!c->list && (optind >= argc)) {
672 help(1);
673 YLMSG_E("Missing <schema> to process.\n");
674 return 1;
675 }
676
677 if (c->data_merge) {
678 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
679 /* switch off the option, incompatible input data type */
680 c->data_merge = 0;
681 } else {
682 /* postpone validation after the merge of all the input data */
683 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200684 }
685 }
686
Radek Krejcie9f13b12020-11-09 17:42:04 +0100687 if (c->data_operational.path && !c->data_type) {
688 YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
689 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200690 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100691
692 /* default output stream */
693 if (!c->out) {
694 if (ly_out_new_file(stdout, &c->out)) {
695 YLMSG_E("Unable to set stdout as output.\n");
696 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200697 }
698 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100699
700 /* process input files provided as standalone command line arguments,
701 * schema modules are parsed and inserted into the context,
702 * data files are just checked and prepared into internal structures for further processing */
703 ret = fill_context_inputs(argc, argv, c);
704 if (ret) {
705 return ret;
706 }
707
708 /* the second batch of checks */
709 if (c->schema_print_options && !c->schema_out_format) {
710 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
711 }
712 if (c->schema_parse_options && !c->schema_modules.count) {
713 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
714 }
715 if (c->data_print_options && !c->data_out_format) {
716 YLMSG_W("data printer options specified, but the data output format is missing.\n");
717 }
718 if ((c->data_parse_options || c->data_type) && !c->data_inputs.count) {
719 YLMSG_W("Data parser options specified, but no data input file provided.\n");
720 }
721
722 if (c->schema_node_path) {
723 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
724 if (!c->schema_node) {
725 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
726
727 if (!c->schema_node) {
728 YLMSG_E("Invalid schema path.\n");
729 return -1;
730 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200731 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200732 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100733
734 if (c->data_type == LYD_VALIDATE_OP_REPLY) {
735 if (check_request_paths(c->ctx, &c->data_request_paths, &c->data_inputs)) {
736 return -1;
737 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200738 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100739
740 return 0;
741}
742
743int
744main_ni(int argc, char *argv[])
745{
746 int ret = EXIT_SUCCESS, r;
747 struct context c = {0};
Radek Krejcied5acc52019-04-25 15:57:04 +0200748
749 /* set callback for printing libyang messages */
750 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100751
752 r = fill_context(argc, argv, &c);
753 if (r < 0) {
754 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200755 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100756 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200757 goto cleanup;
758 }
759
Radek Krejcie9f13b12020-11-09 17:42:04 +0100760 /* do the required job - parse, validate, print */
761
762 if (c.list) {
763 /* print the list of schemas */
764 print_list(c.out, c.ctx, c.data_out_format);
765 } else if (c.schema_out_format) {
766 if (c.schema_node) {
767 ret = lys_print_node(c.out, c.schema_node, c.schema_out_format, 0, c.schema_print_options);
768 if (ret) {
769 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
770 goto cleanup;
771 }
772 } else {
773 for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
774 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, 0,
775 c.schema_print_options);
776 if (ret) {
777 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
778 goto cleanup;
779 }
780 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200781 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100782 } else if (c.data_out_format) {
783 if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out,
784 c.data_parse_options, c.data_validate_options, c.data_print_options,
785 &c.data_operational, &c.data_inputs, &c.data_request_paths, &c.data_requests, NULL)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200786 goto cleanup;
787 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200788 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200789
790cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100791 /* cleanup */
792 erase_context(&c);
Radek Krejcied5acc52019-04-25 15:57:04 +0200793
Radek Krejcied5acc52019-04-25 15:57:04 +0200794 return ret;
795}