blob: 8746ba766bf2f32f72cf3f80adfcfee729e3a63d [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 }
384 }
385 }
386
387 if (request_expected) {
388 YLMSG_E("Missing request input file for the reply input file %s.\n",
389 ((struct cmdline_file *)c->data_inputs.objs[c->data_inputs.count - 1])->path);
390 return -1;
391 }
392
393 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100394
395error:
396 ly_in_free(in, 1);
397 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100398}
399
400/**
401 * @brief Process command line options and store the settings into the context.
402 *
403 * return -1 in case of error;
404 * return 0 in case of success and ready to process
405 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200406 */
407static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100408fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200409{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100410 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200411
Radek Krejcie9f13b12020-11-09 17:42:04 +0100412 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200413 struct option options[] = {
Radek Krejciff61c882020-09-03 13:04:18 +0200414 {"default", required_argument, NULL, 'd'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100415 {"disable-searchdir", no_argument, NULL, 'D'},
416 {"present", no_argument, NULL, 'e'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200417 {"format", required_argument, NULL, 'f'},
418 {"features", required_argument, NULL, 'F'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100419#ifndef NDEBUG
420 {"debug", required_argument, NULL, 'G'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200421#endif
422 {"help", no_argument, NULL, 'h'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100423 {"makeimplemented", no_argument, NULL, 'i'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200424 {"list", no_argument, NULL, 'l'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200425 {"merge", no_argument, NULL, 'm'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200426 {"output", required_argument, NULL, 'o'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200427 {"operational", required_argument, NULL, 'O'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100428 {"path", required_argument, NULL, 'p'},
429 {"schema-node", required_argument, NULL, 'P'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800430 {"single-node", no_argument, NULL, 'q'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100431 {"request", required_argument, NULL, 'r'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200432 {"strict", no_argument, NULL, 's'},
433 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200434 {"version", no_argument, NULL, 'v'},
435 {"verbose", no_argument, NULL, 'V'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200436 {NULL, 0, NULL, 0}
437 };
Radek Krejcied5acc52019-04-25 15:57:04 +0200438
Radek Krejcie9f13b12020-11-09 17:42:04 +0100439 uint16_t options_ctx = 0;
440 uint8_t data_type_set = 0;
441
Radek Krejcied5acc52019-04-25 15:57:04 +0200442#ifndef NDEBUG
Radek Krejci3a7831b2020-12-03 13:03:52 +0100443 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 +0200444#else
Radek Krejci3a7831b2020-12-03 13:03:52 +0100445 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 +0200446#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200447 switch (opt) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100448 case 'd': /* --default */
449 if (!strcasecmp(optarg, "all")) {
450 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
451 } else if (!strcasecmp(optarg, "all-tagged")) {
452 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
453 } else if (!strcasecmp(optarg, "trim")) {
454 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
455 } else if (!strcasecmp(optarg, "implicit-tagged")) {
456 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200457 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100458 YLMSG_E("Unknown default mode %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200459 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100460 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200461 }
462 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200463
Radek Krejcie9f13b12020-11-09 17:42:04 +0100464 case 'D': /* --disable-search */
Radek Krejcied5acc52019-04-25 15:57:04 +0200465 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100466 YLMSG_W("The -D option specified too many times.\n");
467 }
468 if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200469 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
470 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
471 } else {
472 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
473 }
474 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100475
476 case 'p': { /* --path */
477 struct stat st;
478
Radek Krejcied5acc52019-04-25 15:57:04 +0200479 if (stat(optarg, &st) == -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100480 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
481 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200482 }
483 if (!S_ISDIR(st.st_mode)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100484 YLMSG_E("Provided search path is not a directory.\n");
485 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200486 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100487
488 if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
489 YLMSG_E("Storing searchpath failed.\n");
490 return -1;
491 }
492
493 break;
494 } /* case 'p' */
495
496 case 'i': /* --makeimplemented */
497 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
498 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
499 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
500 } else {
501 options_ctx |= LY_CTX_REF_IMPLEMENTED;
502 }
503 break;
504
505 case 'F': /* --features */
506 if (parse_features(optarg, &c->schema_features)) {
507 return -1;
508 }
509 break;
510
511 case 'l': /* --list */
512 c->list = 1;
513 break;
514
515 case 'o': /* --output */
516 if (c->out) {
517 YLMSG_E("Only a single output can be specified.\n");
518 return -1;
519 } else {
520 if (ly_out_new_filepath(optarg, &c->out)) {
521 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
522 return -1;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200523 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200524 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200525 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100526
527 case 'f': /* --format */
528 if (!strcasecmp(optarg, "yang")) {
529 c->schema_out_format = LYS_OUT_YANG;
530 c->data_out_format = 0;
531 } else if (!strcasecmp(optarg, "yin")) {
532 c->schema_out_format = LYS_OUT_YIN;
533 c->data_out_format = 0;
534 } else if (!strcasecmp(optarg, "info")) {
535 c->schema_out_format = LYS_OUT_YANG_COMPILED;
536 c->data_out_format = 0;
537 } else if (!strcasecmp(optarg, "tree")) {
538 c->schema_out_format = LYS_OUT_TREE;
539 c->data_out_format = 0;
540 } else if (!strcasecmp(optarg, "xml")) {
541 c->schema_out_format = 0;
542 c->data_out_format = LYD_XML;
543 } else if (!strcasecmp(optarg, "json")) {
544 c->schema_out_format = 0;
545 c->data_out_format = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200546 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100547 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200548 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100549 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200550 }
551 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100552
553 case 'P': /* --schema-node */
554 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200555 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100556
557 case 'q': /* --single-node */
558 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
559 break;
560
561 case 's': /* --strict */
562 c->data_parse_options |= LYD_PARSE_STRICT;
563 break;
564
565 case 'e': /* --present */
566 c->data_validate_options |= LYD_VALIDATE_PRESENT;
567 break;
568
569 case 't': /* --type */
570 if (data_type_set) {
571 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
572 return -1;
573 }
574
575 if (!strcasecmp(optarg, "config")) {
576 c->data_parse_options |= LYD_PARSE_NO_STATE;
577 } else if (!strcasecmp(optarg, "get")) {
578 c->data_parse_options |= LYD_PARSE_ONLY;
579 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
580 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
581 } else if (!strcasecmp(optarg, "edit")) {
582 c->data_parse_options |= LYD_PARSE_ONLY;
583 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
584 c->data_type = LYD_VALIDATE_OP_RPC;
585 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
586 c->data_type = LYD_VALIDATE_OP_REPLY;
587 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
588 c->data_type = LYD_VALIDATE_OP_NOTIF;
589 } else if (!strcasecmp(optarg, "data")) {
590 /* default option */
591 } else {
592 YLMSG_E("Unknown data tree type %s\n", optarg);
593 help(1);
594 return -1;
595 }
596
597 data_type_set = 1;
598 break;
599
600 case 'O': /* --operational */
601 if (c->data_operational.path) {
602 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
603 return -1;
604 }
605 c->data_operational.path = optarg;
606 break;
607
608 case 'r': /* --request */
609 if (ly_set_add(&c->data_request_paths, optarg, 0, NULL)) {
610 YLMSG_E("Storing request path failed.\n");
611 return -1;
612 }
613 break;
614
615 case 'm': /* --merge */
616 c->data_merge = 1;
617 break;
618
619 case 'h': /* --help */
620 help(0);
621 return 1;
622
623 case 'v': /* --version */
624 version();
625 return 1;
626
627 case 'V': { /* --verbose */
628 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
629 ly_log_level(verbosity);
630
631 if (verbosity < LY_LLDBG) {
632 ly_log_level(verbosity + 1);
633 }
634 break;
635 } /* case 'V' */
636
Radek Krejcied5acc52019-04-25 15:57:04 +0200637#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100638 case 'G': { /* --debug */
639 uint32_t dbg_groups = 0;
640 const char *ptr = optarg;
641
Radek Krejcied5acc52019-04-25 15:57:04 +0200642 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100643 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100644 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100645 ptr += sizeof "dict" - 1;
646 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100647 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100648 ptr += sizeof "xpath" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200649 }
650
651 if (ptr[0]) {
652 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100653 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
654 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200655 }
656 ++ptr;
657 }
658 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100659 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200660 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100661 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200662#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100663 } /* switch */
664 }
665
666 /* libyang context */
667 if (ly_ctx_new(NULL, options_ctx, &c->ctx)) {
668 YLMSG_E("Unable to create libyang context.\n");
669 return -1;
670 }
671 for (uint32_t u = 0; u < c->searchpaths.count; ++u) {
672 ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]);
673 }
674
675 /* additional checks for the options combinations */
676 if (!c->list && (optind >= argc)) {
677 help(1);
678 YLMSG_E("Missing <schema> to process.\n");
679 return 1;
680 }
681
682 if (c->data_merge) {
683 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
684 /* switch off the option, incompatible input data type */
685 c->data_merge = 0;
686 } else {
687 /* postpone validation after the merge of all the input data */
688 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200689 }
690 }
691
Radek Krejcie9f13b12020-11-09 17:42:04 +0100692 if (c->data_operational.path && !c->data_type) {
693 YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
694 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200695 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100696
697 /* default output stream */
698 if (!c->out) {
699 if (ly_out_new_file(stdout, &c->out)) {
700 YLMSG_E("Unable to set stdout as output.\n");
701 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200702 }
703 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100704
705 /* process input files provided as standalone command line arguments,
706 * schema modules are parsed and inserted into the context,
707 * data files are just checked and prepared into internal structures for further processing */
708 ret = fill_context_inputs(argc, argv, c);
709 if (ret) {
710 return ret;
711 }
712
713 /* the second batch of checks */
714 if (c->schema_print_options && !c->schema_out_format) {
715 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
716 }
717 if (c->schema_parse_options && !c->schema_modules.count) {
718 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
719 }
720 if (c->data_print_options && !c->data_out_format) {
721 YLMSG_W("data printer options specified, but the data output format is missing.\n");
722 }
723 if ((c->data_parse_options || c->data_type) && !c->data_inputs.count) {
724 YLMSG_W("Data parser options specified, but no data input file provided.\n");
725 }
726
727 if (c->schema_node_path) {
728 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
729 if (!c->schema_node) {
730 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
731
732 if (!c->schema_node) {
733 YLMSG_E("Invalid schema path.\n");
734 return -1;
735 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200736 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200737 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100738
739 if (c->data_type == LYD_VALIDATE_OP_REPLY) {
740 if (check_request_paths(c->ctx, &c->data_request_paths, &c->data_inputs)) {
741 return -1;
742 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200743 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100744
745 return 0;
746}
747
748int
749main_ni(int argc, char *argv[])
750{
751 int ret = EXIT_SUCCESS, r;
752 struct context c = {0};
Radek Krejcied5acc52019-04-25 15:57:04 +0200753
754 /* set callback for printing libyang messages */
755 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100756
757 r = fill_context(argc, argv, &c);
758 if (r < 0) {
759 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200760 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100761 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200762 goto cleanup;
763 }
764
Radek Krejcie9f13b12020-11-09 17:42:04 +0100765 /* do the required job - parse, validate, print */
766
767 if (c.list) {
768 /* print the list of schemas */
769 print_list(c.out, c.ctx, c.data_out_format);
Radek Krejci047d64e2020-12-03 12:57:10 +0100770 } else {
771 if (c.schema_out_format) {
772 if (c.schema_node) {
773 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 +0100774 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100775 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100776 goto cleanup;
777 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100778 } else {
779 for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
780 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, 0,
781 c.schema_print_options);
782 if (ret) {
783 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
784 goto cleanup;
785 }
786 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100787 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200788 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100789
790 /* do the data validation despite the schema was printed */
791 if (c.data_inputs.size) {
792 if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out,
793 c.data_parse_options, c.data_validate_options, c.data_print_options,
794 &c.data_operational, &c.data_inputs, &c.data_request_paths, &c.data_requests, NULL)) {
795 goto cleanup;
796 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200797 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200798 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200799
800cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100801 /* cleanup */
802 erase_context(&c);
Radek Krejcied5acc52019-04-25 15:57:04 +0200803
Radek Krejcied5acc52019-04-25 15:57:04 +0200804 return ret;
805}