blob: 78da7f1055516f8137f231037ec839e608576e45 [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{
Radek Krejcif53b0d52020-12-03 11:29:24 +0100299 struct ly_in *in;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100300 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;
Radek Krejcif53b0d52020-12-03 11:29:24 +0100312 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100313
314 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100315 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100316 }
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)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100327 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100328 }
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);
Radek Krejcif53b0d52020-12-03 11:29:24 +0100344 ly_in_free(in, 1);
345 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100346 if (ret) {
347 YLMSG_E("Processing schema module from %s failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100348 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100349 }
350
351 if (c->schema_out_format) {
352 /* modules will be printed */
353 if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
354 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100355 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100356 }
357 }
358 } else if (request_expected) {
359 if (fill_cmdline_file(&c->data_requests, in, argv[optind + i], format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100360 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100361 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100362 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100363
364 request_expected = 0;
365 } else if (format_data) {
366 struct cmdline_file *rec;
367
368 rec = fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data);
369 if (!rec) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100370 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100371 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100372 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100373
374 if ((c->data_type == LYD_VALIDATE_OP_REPLY) && !c->data_request_paths.count) {
375 /* requests for the replies are expected in another input file */
376 if (++i == argc - optind) {
377 /* there is no such file */
378 YLMSG_E("Missing request input file for the reply input file %s.\n", rec->path);
Radek Krejcif2afd672020-11-26 12:29:23 +0100379 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100380 }
381
382 request_expected = 1;
383 }
Radek Krejci3329b652020-12-05 23:54:07 +0100384 } else {
385 ly_in_free(in, 1);
386 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100387 }
388 }
389
390 if (request_expected) {
391 YLMSG_E("Missing request input file for the reply input file %s.\n",
392 ((struct cmdline_file *)c->data_inputs.objs[c->data_inputs.count - 1])->path);
393 return -1;
394 }
395
396 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100397
398error:
399 ly_in_free(in, 1);
400 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100401}
402
403/**
404 * @brief Process command line options and store the settings into the context.
405 *
406 * return -1 in case of error;
407 * return 0 in case of success and ready to process
408 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200409 */
410static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100411fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200412{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100413 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200414
Radek Krejcie9f13b12020-11-09 17:42:04 +0100415 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200416 struct option options[] = {
Radek Krejciff61c882020-09-03 13:04:18 +0200417 {"default", required_argument, NULL, 'd'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100418 {"disable-searchdir", no_argument, NULL, 'D'},
419 {"present", no_argument, NULL, 'e'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200420 {"format", required_argument, NULL, 'f'},
421 {"features", required_argument, NULL, 'F'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100422#ifndef NDEBUG
423 {"debug", required_argument, NULL, 'G'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200424#endif
425 {"help", no_argument, NULL, 'h'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100426 {"makeimplemented", no_argument, NULL, 'i'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200427 {"list", no_argument, NULL, 'l'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200428 {"merge", no_argument, NULL, 'm'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200429 {"output", required_argument, NULL, 'o'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200430 {"operational", required_argument, NULL, 'O'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100431 {"path", required_argument, NULL, 'p'},
432 {"schema-node", required_argument, NULL, 'P'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800433 {"single-node", no_argument, NULL, 'q'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100434 {"request", required_argument, NULL, 'r'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200435 {"strict", no_argument, NULL, 's'},
436 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200437 {"version", no_argument, NULL, 'v'},
438 {"verbose", no_argument, NULL, 'V'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200439 {NULL, 0, NULL, 0}
440 };
Radek Krejcied5acc52019-04-25 15:57:04 +0200441
Radek Krejcie9f13b12020-11-09 17:42:04 +0100442 uint16_t options_ctx = 0;
443 uint8_t data_type_set = 0;
444
Radek Krejcied5acc52019-04-25 15:57:04 +0200445#ifndef NDEBUG
Radek Krejci3a7831b2020-12-03 13:03:52 +0100446 while ((opt = getopt_long(argc, argv, "d:Def:F:hilmo:P:qr:st:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200447#else
Radek Krejci3a7831b2020-12-03 13:03:52 +0100448 while ((opt = getopt_long(argc, argv, "d:Def:F:G:hilmo:P:qr:st:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200449#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200450 switch (opt) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100451 case 'd': /* --default */
452 if (!strcasecmp(optarg, "all")) {
453 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
454 } else if (!strcasecmp(optarg, "all-tagged")) {
455 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
456 } else if (!strcasecmp(optarg, "trim")) {
457 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
458 } else if (!strcasecmp(optarg, "implicit-tagged")) {
459 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200460 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100461 YLMSG_E("Unknown default mode %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200462 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100463 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200464 }
465 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200466
Radek Krejcie9f13b12020-11-09 17:42:04 +0100467 case 'D': /* --disable-search */
Radek Krejcied5acc52019-04-25 15:57:04 +0200468 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100469 YLMSG_W("The -D option specified too many times.\n");
470 }
471 if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200472 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
473 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
474 } else {
475 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
476 }
477 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100478
479 case 'p': { /* --path */
480 struct stat st;
481
Radek Krejcied5acc52019-04-25 15:57:04 +0200482 if (stat(optarg, &st) == -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100483 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
484 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200485 }
486 if (!S_ISDIR(st.st_mode)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100487 YLMSG_E("Provided search path is not a directory.\n");
488 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200489 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100490
491 if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
492 YLMSG_E("Storing searchpath failed.\n");
493 return -1;
494 }
495
496 break;
497 } /* case 'p' */
498
499 case 'i': /* --makeimplemented */
500 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
501 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
502 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
503 } else {
504 options_ctx |= LY_CTX_REF_IMPLEMENTED;
505 }
506 break;
507
508 case 'F': /* --features */
509 if (parse_features(optarg, &c->schema_features)) {
510 return -1;
511 }
512 break;
513
514 case 'l': /* --list */
515 c->list = 1;
516 break;
517
518 case 'o': /* --output */
519 if (c->out) {
520 YLMSG_E("Only a single output can be specified.\n");
521 return -1;
522 } else {
523 if (ly_out_new_filepath(optarg, &c->out)) {
524 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
525 return -1;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200526 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200527 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200528 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100529
530 case 'f': /* --format */
531 if (!strcasecmp(optarg, "yang")) {
532 c->schema_out_format = LYS_OUT_YANG;
533 c->data_out_format = 0;
534 } else if (!strcasecmp(optarg, "yin")) {
535 c->schema_out_format = LYS_OUT_YIN;
536 c->data_out_format = 0;
537 } else if (!strcasecmp(optarg, "info")) {
538 c->schema_out_format = LYS_OUT_YANG_COMPILED;
539 c->data_out_format = 0;
540 } else if (!strcasecmp(optarg, "tree")) {
541 c->schema_out_format = LYS_OUT_TREE;
542 c->data_out_format = 0;
543 } else if (!strcasecmp(optarg, "xml")) {
544 c->schema_out_format = 0;
545 c->data_out_format = LYD_XML;
546 } else if (!strcasecmp(optarg, "json")) {
547 c->schema_out_format = 0;
548 c->data_out_format = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200549 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100550 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200551 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100552 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200553 }
554 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100555
556 case 'P': /* --schema-node */
557 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200558 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100559
560 case 'q': /* --single-node */
561 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
562 break;
563
564 case 's': /* --strict */
565 c->data_parse_options |= LYD_PARSE_STRICT;
566 break;
567
568 case 'e': /* --present */
569 c->data_validate_options |= LYD_VALIDATE_PRESENT;
570 break;
571
572 case 't': /* --type */
573 if (data_type_set) {
574 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
575 return -1;
576 }
577
578 if (!strcasecmp(optarg, "config")) {
579 c->data_parse_options |= LYD_PARSE_NO_STATE;
580 } else if (!strcasecmp(optarg, "get")) {
581 c->data_parse_options |= LYD_PARSE_ONLY;
582 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
583 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
584 } else if (!strcasecmp(optarg, "edit")) {
585 c->data_parse_options |= LYD_PARSE_ONLY;
586 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
587 c->data_type = LYD_VALIDATE_OP_RPC;
588 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
589 c->data_type = LYD_VALIDATE_OP_REPLY;
590 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
591 c->data_type = LYD_VALIDATE_OP_NOTIF;
592 } else if (!strcasecmp(optarg, "data")) {
593 /* default option */
594 } else {
595 YLMSG_E("Unknown data tree type %s\n", optarg);
596 help(1);
597 return -1;
598 }
599
600 data_type_set = 1;
601 break;
602
603 case 'O': /* --operational */
604 if (c->data_operational.path) {
605 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
606 return -1;
607 }
608 c->data_operational.path = optarg;
609 break;
610
611 case 'r': /* --request */
612 if (ly_set_add(&c->data_request_paths, optarg, 0, NULL)) {
613 YLMSG_E("Storing request path failed.\n");
614 return -1;
615 }
616 break;
617
618 case 'm': /* --merge */
619 c->data_merge = 1;
620 break;
621
622 case 'h': /* --help */
623 help(0);
624 return 1;
625
626 case 'v': /* --version */
627 version();
628 return 1;
629
630 case 'V': { /* --verbose */
631 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
632 ly_log_level(verbosity);
633
634 if (verbosity < LY_LLDBG) {
635 ly_log_level(verbosity + 1);
636 }
637 break;
638 } /* case 'V' */
639
Radek Krejcied5acc52019-04-25 15:57:04 +0200640#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100641 case 'G': { /* --debug */
642 uint32_t dbg_groups = 0;
643 const char *ptr = optarg;
644
Radek Krejcied5acc52019-04-25 15:57:04 +0200645 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100646 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100647 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100648 ptr += sizeof "dict" - 1;
649 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100650 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100651 ptr += sizeof "xpath" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200652 }
653
654 if (ptr[0]) {
655 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100656 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
657 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200658 }
659 ++ptr;
660 }
661 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100662 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200663 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100664 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200665#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100666 } /* switch */
667 }
668
669 /* libyang context */
670 if (ly_ctx_new(NULL, options_ctx, &c->ctx)) {
671 YLMSG_E("Unable to create libyang context.\n");
672 return -1;
673 }
674 for (uint32_t u = 0; u < c->searchpaths.count; ++u) {
675 ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]);
676 }
677
678 /* additional checks for the options combinations */
679 if (!c->list && (optind >= argc)) {
680 help(1);
681 YLMSG_E("Missing <schema> to process.\n");
682 return 1;
683 }
684
685 if (c->data_merge) {
686 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
687 /* switch off the option, incompatible input data type */
688 c->data_merge = 0;
689 } else {
690 /* postpone validation after the merge of all the input data */
691 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200692 }
693 }
694
Radek Krejcie9f13b12020-11-09 17:42:04 +0100695 if (c->data_operational.path && !c->data_type) {
696 YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
697 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200698 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100699
700 /* default output stream */
701 if (!c->out) {
702 if (ly_out_new_file(stdout, &c->out)) {
703 YLMSG_E("Unable to set stdout as output.\n");
704 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200705 }
706 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100707
708 /* process input files provided as standalone command line arguments,
709 * schema modules are parsed and inserted into the context,
710 * data files are just checked and prepared into internal structures for further processing */
711 ret = fill_context_inputs(argc, argv, c);
712 if (ret) {
713 return ret;
714 }
715
716 /* the second batch of checks */
717 if (c->schema_print_options && !c->schema_out_format) {
718 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
719 }
720 if (c->schema_parse_options && !c->schema_modules.count) {
721 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
722 }
723 if (c->data_print_options && !c->data_out_format) {
724 YLMSG_W("data printer options specified, but the data output format is missing.\n");
725 }
726 if ((c->data_parse_options || c->data_type) && !c->data_inputs.count) {
727 YLMSG_W("Data parser options specified, but no data input file provided.\n");
728 }
729
730 if (c->schema_node_path) {
731 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
732 if (!c->schema_node) {
733 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
734
735 if (!c->schema_node) {
736 YLMSG_E("Invalid schema path.\n");
737 return -1;
738 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200739 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200740 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100741
742 if (c->data_type == LYD_VALIDATE_OP_REPLY) {
743 if (check_request_paths(c->ctx, &c->data_request_paths, &c->data_inputs)) {
744 return -1;
745 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200746 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100747
748 return 0;
749}
750
751int
752main_ni(int argc, char *argv[])
753{
754 int ret = EXIT_SUCCESS, r;
755 struct context c = {0};
Radek Krejcied5acc52019-04-25 15:57:04 +0200756
757 /* set callback for printing libyang messages */
758 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100759
760 r = fill_context(argc, argv, &c);
761 if (r < 0) {
762 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200763 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100764 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200765 goto cleanup;
766 }
767
Radek Krejcie9f13b12020-11-09 17:42:04 +0100768 /* do the required job - parse, validate, print */
769
770 if (c.list) {
771 /* print the list of schemas */
772 print_list(c.out, c.ctx, c.data_out_format);
Radek Krejci047d64e2020-12-03 12:57:10 +0100773 } else {
774 if (c.schema_out_format) {
775 if (c.schema_node) {
776 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 +0100777 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100778 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100779 goto cleanup;
780 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100781 } else {
782 for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
783 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, 0,
784 c.schema_print_options);
785 if (ret) {
786 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
787 goto cleanup;
788 }
789 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100790 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200791 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100792
793 /* do the data validation despite the schema was printed */
794 if (c.data_inputs.size) {
795 if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out,
796 c.data_parse_options, c.data_validate_options, c.data_print_options,
797 &c.data_operational, &c.data_inputs, &c.data_request_paths, &c.data_requests, NULL)) {
798 goto cleanup;
799 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200800 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200801 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200802
803cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100804 /* cleanup */
805 erase_context(&c);
Radek Krejcied5acc52019-04-25 15:57:04 +0200806
Radek Krejcied5acc52019-04-25 15:57:04 +0200807 return ret;
808}