blob: 3559f73d3d68defc5e5d3d4c1d4d2f254b439397 [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 Krejcif2afd672020-11-26 12:29:23 +0100299 struct ly_in *in = NULL;
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 Krejcie9f13b12020-11-09 17:42:04 +0100312
313 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100314 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100315 }
316
317 if (format_schema) {
318 LY_ERR ret;
319 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
320 char *dir, *module;
321 const char *fall = "*";
322 const char **features = &fall;
323 const struct lys_module *mod;
324
325 if (parse_schema_path(argv[optind + i], &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100326 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100327 }
328
329 /* add temporarily also the path of the module itself */
330 if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {
331 path_unset = 0;
332 }
333
334 /* get features list for this module */
335 get_features(&c->schema_features, module, &features);
336
337 /* temporary cleanup */
338 free(dir);
339 free(module);
340
341 ret = lys_parse(c->ctx, in, format_schema, features, &mod);
342 ly_ctx_unset_searchdir_last(c->ctx, path_unset);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100343 if (ret) {
344 YLMSG_E("Processing schema module from %s failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100345 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100346 }
347
348 if (c->schema_out_format) {
349 /* modules will be printed */
350 if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
351 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100352 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100353 }
354 }
355 } else if (request_expected) {
356 if (fill_cmdline_file(&c->data_requests, in, argv[optind + i], format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100357 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100358 }
359
360 request_expected = 0;
361 } else if (format_data) {
362 struct cmdline_file *rec;
363
364 rec = fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data);
365 if (!rec) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100366 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100367 }
368
369 if ((c->data_type == LYD_VALIDATE_OP_REPLY) && !c->data_request_paths.count) {
370 /* requests for the replies are expected in another input file */
371 if (++i == argc - optind) {
372 /* there is no such file */
373 YLMSG_E("Missing request input file for the reply input file %s.\n", rec->path);
Radek Krejcif2afd672020-11-26 12:29:23 +0100374 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100375 }
376
377 request_expected = 1;
378 }
379 }
Radek Krejcif2afd672020-11-26 12:29:23 +0100380
381 ly_in_free(in, 1);
382 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100383 }
384
385 if (request_expected) {
386 YLMSG_E("Missing request input file for the reply input file %s.\n",
387 ((struct cmdline_file *)c->data_inputs.objs[c->data_inputs.count - 1])->path);
388 return -1;
389 }
390
391 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100392
393error:
394 ly_in_free(in, 1);
395 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100396}
397
398/**
399 * @brief Process command line options and store the settings into the context.
400 *
401 * return -1 in case of error;
402 * return 0 in case of success and ready to process
403 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200404 */
405static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100406fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200407{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100408 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200409
Radek Krejcie9f13b12020-11-09 17:42:04 +0100410 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200411 struct option options[] = {
Radek Krejciff61c882020-09-03 13:04:18 +0200412 {"default", required_argument, NULL, 'd'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100413 {"disable-searchdir", no_argument, NULL, 'D'},
414 {"present", no_argument, NULL, 'e'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200415 {"format", required_argument, NULL, 'f'},
416 {"features", required_argument, NULL, 'F'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100417#ifndef NDEBUG
418 {"debug", required_argument, NULL, 'G'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200419#endif
420 {"help", no_argument, NULL, 'h'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100421 {"makeimplemented", no_argument, NULL, 'i'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200422 {"list", no_argument, NULL, 'l'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200423 {"merge", no_argument, NULL, 'm'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200424 {"output", required_argument, NULL, 'o'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200425 {"operational", required_argument, NULL, 'O'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100426 {"path", required_argument, NULL, 'p'},
427 {"schema-node", required_argument, NULL, 'P'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800428 {"single-node", no_argument, NULL, 'q'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100429 {"request", required_argument, NULL, 'r'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200430 {"strict", no_argument, NULL, 's'},
431 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200432 {"version", no_argument, NULL, 'v'},
433 {"verbose", no_argument, NULL, 'V'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200434 {NULL, 0, NULL, 0}
435 };
Radek Krejcied5acc52019-04-25 15:57:04 +0200436
Radek Krejcie9f13b12020-11-09 17:42:04 +0100437 uint16_t options_ctx = 0;
438 uint8_t data_type_set = 0;
439
Radek Krejcied5acc52019-04-25 15:57:04 +0200440#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100441 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 +0200442#else
Radek Krejcie9f13b12020-11-09 17:42:04 +0100443 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 +0200444#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200445 switch (opt) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100446 case 'd': /* --default */
447 if (!strcasecmp(optarg, "all")) {
448 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
449 } else if (!strcasecmp(optarg, "all-tagged")) {
450 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
451 } else if (!strcasecmp(optarg, "trim")) {
452 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
453 } else if (!strcasecmp(optarg, "implicit-tagged")) {
454 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200455 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100456 YLMSG_E("Unknown default mode %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200457 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100458 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200459 }
460 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200461
Radek Krejcie9f13b12020-11-09 17:42:04 +0100462 case 'D': /* --disable-search */
Radek Krejcied5acc52019-04-25 15:57:04 +0200463 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100464 YLMSG_W("The -D option specified too many times.\n");
465 }
466 if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200467 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
468 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
469 } else {
470 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
471 }
472 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100473
474 case 'p': { /* --path */
475 struct stat st;
476
Radek Krejcied5acc52019-04-25 15:57:04 +0200477 if (stat(optarg, &st) == -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100478 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
479 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200480 }
481 if (!S_ISDIR(st.st_mode)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100482 YLMSG_E("Provided search path is not a directory.\n");
483 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200484 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100485
486 if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
487 YLMSG_E("Storing searchpath failed.\n");
488 return -1;
489 }
490
491 break;
492 } /* case 'p' */
493
494 case 'i': /* --makeimplemented */
495 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
496 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
497 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
498 } else {
499 options_ctx |= LY_CTX_REF_IMPLEMENTED;
500 }
501 break;
502
503 case 'F': /* --features */
504 if (parse_features(optarg, &c->schema_features)) {
505 return -1;
506 }
507 break;
508
509 case 'l': /* --list */
510 c->list = 1;
511 break;
512
513 case 'o': /* --output */
514 if (c->out) {
515 YLMSG_E("Only a single output can be specified.\n");
516 return -1;
517 } else {
518 if (ly_out_new_filepath(optarg, &c->out)) {
519 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
520 return -1;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200521 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200522 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200523 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100524
525 case 'f': /* --format */
526 if (!strcasecmp(optarg, "yang")) {
527 c->schema_out_format = LYS_OUT_YANG;
528 c->data_out_format = 0;
529 } else if (!strcasecmp(optarg, "yin")) {
530 c->schema_out_format = LYS_OUT_YIN;
531 c->data_out_format = 0;
532 } else if (!strcasecmp(optarg, "info")) {
533 c->schema_out_format = LYS_OUT_YANG_COMPILED;
534 c->data_out_format = 0;
535 } else if (!strcasecmp(optarg, "tree")) {
536 c->schema_out_format = LYS_OUT_TREE;
537 c->data_out_format = 0;
538 } else if (!strcasecmp(optarg, "xml")) {
539 c->schema_out_format = 0;
540 c->data_out_format = LYD_XML;
541 } else if (!strcasecmp(optarg, "json")) {
542 c->schema_out_format = 0;
543 c->data_out_format = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200544 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100545 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200546 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100547 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200548 }
549 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100550
551 case 'P': /* --schema-node */
552 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200553 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100554
555 case 'q': /* --single-node */
556 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
557 break;
558
559 case 's': /* --strict */
560 c->data_parse_options |= LYD_PARSE_STRICT;
561 break;
562
563 case 'e': /* --present */
564 c->data_validate_options |= LYD_VALIDATE_PRESENT;
565 break;
566
567 case 't': /* --type */
568 if (data_type_set) {
569 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
570 return -1;
571 }
572
573 if (!strcasecmp(optarg, "config")) {
574 c->data_parse_options |= LYD_PARSE_NO_STATE;
575 } else if (!strcasecmp(optarg, "get")) {
576 c->data_parse_options |= LYD_PARSE_ONLY;
577 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
578 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
579 } else if (!strcasecmp(optarg, "edit")) {
580 c->data_parse_options |= LYD_PARSE_ONLY;
581 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
582 c->data_type = LYD_VALIDATE_OP_RPC;
583 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
584 c->data_type = LYD_VALIDATE_OP_REPLY;
585 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
586 c->data_type = LYD_VALIDATE_OP_NOTIF;
587 } else if (!strcasecmp(optarg, "data")) {
588 /* default option */
589 } else {
590 YLMSG_E("Unknown data tree type %s\n", optarg);
591 help(1);
592 return -1;
593 }
594
595 data_type_set = 1;
596 break;
597
598 case 'O': /* --operational */
599 if (c->data_operational.path) {
600 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
601 return -1;
602 }
603 c->data_operational.path = optarg;
604 break;
605
606 case 'r': /* --request */
607 if (ly_set_add(&c->data_request_paths, optarg, 0, NULL)) {
608 YLMSG_E("Storing request path failed.\n");
609 return -1;
610 }
611 break;
612
613 case 'm': /* --merge */
614 c->data_merge = 1;
615 break;
616
617 case 'h': /* --help */
618 help(0);
619 return 1;
620
621 case 'v': /* --version */
622 version();
623 return 1;
624
625 case 'V': { /* --verbose */
626 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
627 ly_log_level(verbosity);
628
629 if (verbosity < LY_LLDBG) {
630 ly_log_level(verbosity + 1);
631 }
632 break;
633 } /* case 'V' */
634
Radek Krejcied5acc52019-04-25 15:57:04 +0200635#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100636 case 'G': { /* --debug */
637 uint32_t dbg_groups = 0;
638 const char *ptr = optarg;
639
Radek Krejcied5acc52019-04-25 15:57:04 +0200640 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100641 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100642 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100643 ptr += sizeof "dict" - 1;
644 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100645 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100646 ptr += sizeof "xpath" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200647 }
648
649 if (ptr[0]) {
650 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100651 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
652 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200653 }
654 ++ptr;
655 }
656 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100657 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200658 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100659 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200660#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100661 } /* switch */
662 }
663
664 /* libyang context */
665 if (ly_ctx_new(NULL, options_ctx, &c->ctx)) {
666 YLMSG_E("Unable to create libyang context.\n");
667 return -1;
668 }
669 for (uint32_t u = 0; u < c->searchpaths.count; ++u) {
670 ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]);
671 }
672
673 /* additional checks for the options combinations */
674 if (!c->list && (optind >= argc)) {
675 help(1);
676 YLMSG_E("Missing <schema> to process.\n");
677 return 1;
678 }
679
680 if (c->data_merge) {
681 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
682 /* switch off the option, incompatible input data type */
683 c->data_merge = 0;
684 } else {
685 /* postpone validation after the merge of all the input data */
686 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200687 }
688 }
689
Radek Krejcie9f13b12020-11-09 17:42:04 +0100690 if (c->data_operational.path && !c->data_type) {
691 YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
692 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200693 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100694
695 /* default output stream */
696 if (!c->out) {
697 if (ly_out_new_file(stdout, &c->out)) {
698 YLMSG_E("Unable to set stdout as output.\n");
699 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200700 }
701 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100702
703 /* process input files provided as standalone command line arguments,
704 * schema modules are parsed and inserted into the context,
705 * data files are just checked and prepared into internal structures for further processing */
706 ret = fill_context_inputs(argc, argv, c);
707 if (ret) {
708 return ret;
709 }
710
711 /* the second batch of checks */
712 if (c->schema_print_options && !c->schema_out_format) {
713 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
714 }
715 if (c->schema_parse_options && !c->schema_modules.count) {
716 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
717 }
718 if (c->data_print_options && !c->data_out_format) {
719 YLMSG_W("data printer options specified, but the data output format is missing.\n");
720 }
721 if ((c->data_parse_options || c->data_type) && !c->data_inputs.count) {
722 YLMSG_W("Data parser options specified, but no data input file provided.\n");
723 }
724
725 if (c->schema_node_path) {
726 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
727 if (!c->schema_node) {
728 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
729
730 if (!c->schema_node) {
731 YLMSG_E("Invalid schema path.\n");
732 return -1;
733 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200734 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200735 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100736
737 if (c->data_type == LYD_VALIDATE_OP_REPLY) {
738 if (check_request_paths(c->ctx, &c->data_request_paths, &c->data_inputs)) {
739 return -1;
740 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200741 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100742
743 return 0;
744}
745
746int
747main_ni(int argc, char *argv[])
748{
749 int ret = EXIT_SUCCESS, r;
750 struct context c = {0};
Radek Krejcied5acc52019-04-25 15:57:04 +0200751
752 /* set callback for printing libyang messages */
753 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100754
755 r = fill_context(argc, argv, &c);
756 if (r < 0) {
757 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200758 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100759 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200760 goto cleanup;
761 }
762
Radek Krejcie9f13b12020-11-09 17:42:04 +0100763 /* do the required job - parse, validate, print */
764
765 if (c.list) {
766 /* print the list of schemas */
767 print_list(c.out, c.ctx, c.data_out_format);
768 } else if (c.schema_out_format) {
769 if (c.schema_node) {
770 ret = lys_print_node(c.out, c.schema_node, c.schema_out_format, 0, c.schema_print_options);
771 if (ret) {
772 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
773 goto cleanup;
774 }
775 } else {
776 for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
777 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, 0,
778 c.schema_print_options);
779 if (ret) {
780 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
781 goto cleanup;
782 }
783 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200784 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100785 } else if (c.data_out_format) {
786 if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out,
787 c.data_parse_options, c.data_validate_options, c.data_print_options,
788 &c.data_operational, &c.data_inputs, &c.data_request_paths, &c.data_requests, NULL)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200789 goto cleanup;
790 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200791 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200792
793cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100794 /* cleanup */
795 erase_context(&c);
Radek Krejcied5acc52019-04-25 15:57:04 +0200796
Radek Krejcied5acc52019-04-25 15:57:04 +0200797 return ret;
798}