blob: aa22c27343f2aa64f43232e13a0793e9df3b7c46 [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
Radek Krejcie9f13b12020-11-09 17:42:04 +010084 /* storage for --operational */
85 struct cmdline_file data_operational;
86};
87
88static void
89erase_context(struct context *c)
90{
91 /* data */
92 ly_set_erase(&c->data_inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +010093 ly_in_free(c->data_operational.in, 1);
94
95 /* schema */
96 ly_set_erase(&c->schema_features, free_features);
97 ly_set_erase(&c->schema_modules, NULL);
98
99 /* context */
100 ly_set_erase(&c->searchpaths, NULL);
101
102 ly_out_free(c->out, NULL, 0);
103 ly_ctx_destroy(c->ctx, NULL);
104}
105
106static void
107version(void)
108{
109 printf("yanglint %s\n", PROJECT_VERSION);
110}
111
112static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200113help(int shortout)
114{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100115
116 printf("Usage:\n"
117 " yanglint [Options] [-f { yang | yin | info}] <schema>...\n"
118 " Validates the YANG module in <schema>, and all its dependencies.\n\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +0100119 " yanglint [Options] [-f { xml | json }] <schema>... <file> ...\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100120 " Validates the YANG modeled data in <file> according to the <schema>.\n\n"
121 " yanglint\n"
122 " Starts interactive mode with more features.\n\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200123
124 if (shortout) {
125 return;
126 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100127 printf("Options:\n"
128 " -h, --help Show this help message and exit.\n"
129 " -v, --version Show version number and exit.\n"
130 " -V, --verbose Show verbose messages, can be used multiple times to\n"
131 " increase verbosity.\n"
Radek Krejcied5acc52019-04-25 15:57:04 +0200132#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100133 " -G GROUPS, --debug=GROUPS\n"
134 " Enable printing of specific debugging message group\n"
135 " (nothing will be printed unless verbosity is set to debug):\n"
136 " <group>[,<group>]* (dict, xpath)\n\n"
Radek Krejcied5acc52019-04-25 15:57:04 +0200137#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100138
139 " -d MODE, --default=MODE\n"
140 " Print data with default values, according to the MODE\n"
141 " (to print attributes, ietf-netconf-with-defaults model\n"
142 " must be loaded):\n"
143 " all - Add missing default nodes.\n"
144 " all-tagged - Add missing default nodes and mark all the default\n"
145 " nodes with the attribute.\n"
146 " trim - Remove all nodes with a default value.\n"
147 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n"
148
149 " -D, --disable-searchdir\n"
150 " Do not implicitly search in current working directory for\n"
151 " schema modules. If specified a second time, do not even\n"
152 " search in the module directory (all modules must be \n"
153 " explicitly specified).\n\n"
154
155 " -p PATH, --path=PATH\n"
156 " Search path for schema (YANG/YIN) modules. The option can be\n"
157 " used multiple times. The current working directory and the\n"
158 " path of the module being added is used implicitly.\n\n"
159
160 " -F FEATURES, --features=FEATURES\n"
161 " Features to support, default all.\n"
162 " <modname>:[<feature>,]*\n\n"
163
164 " -i, --makeimplemented\n"
165 " Make the imported modules \"referenced\" from any loaded\n"
166 " module also implemented. If specified a second time, all the\n"
167 " modules are set implemented.\n\n"
168
169 " -l, --list Print info about the loaded schemas.\n"
170 " (i - imported module, I - implemented module)\n"
171 " In case the -f option with data encoding is specified,\n"
172 " the list is printed as ietf-yang-library data.\n\n"
173
174 " -o OUTFILE, --output=OUTFILE\n"
175 " Write the output to OUTFILE instead of stdout.\n\n"
176
177 " -f FORMAT, --format=FORMAT\n"
178 " Convert input into FORMAT. Supported formats: \n"
179 " yang, yin, tree and info for schemas,\n"
180 " xml, json for data.\n\n"
181
182 " -P PATH, --schema-node=PATH\n"
183 " Print only the specified subtree of the schema.\n"
184 " The PATH is the XPath subset mentioned in documentation as\n"
185 " the Path format. The option can be combined with --single-node\n"
186 " option to print information only about the specified node.\n"
187 " -q, --single-node\n"
188 " Supplement to the --schema-node option to print information\n"
189 " only about a single node specified as PATH argument.\n\n"
190
191 " -s, --strict Strict data parsing (do not skip unknown data), has no effect\n"
192 " for schemas.\n\n"
193
194 " -e, --present Validate only with the schema modules whose data actually\n"
195 " exist in the provided input data files. Takes effect only\n"
196 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
197 " mandatory nodes from modules which data are not present in the\n"
198 " provided input data files.\n\n"
199
200 " -t TYPE, --type=TYPE\n"
201 " Specify data tree type in the input data file(s):\n"
202 " data - Complete datastore with status data (default type).\n"
203 " config - Configuration datastore (without status data).\n"
204 " get - Result of the NETCONF <get> operation.\n"
205 " getconfig - Result of the NETCONF <get-config> operation.\n"
206 " edit - Content of the NETCONF <edit-config> operation.\n"
207 " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n"
208 " RPC/Action input statement.\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +0100209 " reply - Reply to the RPC/Action. Note that the reply data are\n"
210 " expected inside a container representing the original\n"
211 " RPC/Action. This is necessary to identify appropriate\n"
212 " data definitions in the schema module.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100213 " notif - Notification instance (content of the <notification>\n"
214 " element without <eventTime>).\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100215
216 " -O FILE, --operational=FILE\n"
217 " Provide optional data to extend validation of the 'rpc',\n"
218 " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n"
219 " the :running configuration datastore and state data\n"
220 " (operational datastore) referenced from the RPC/Notification.\n\n"
221
222 " -m, --merge Merge input data files into a single tree and validate at\n"
223 " once.The option has effect only for 'data' and 'config' TYPEs.\n\n"
224
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100225 " -y, --yang-library\n"
226 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
227 " Note that this module includes definitions of mandatory state\n"
228 " data that can result in unexpected data validation errors.\n\n"
229
Radek Krejcie9f13b12020-11-09 17:42:04 +0100230#if 0
231 " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n"
232 "Tree output specific options:\n"
233 " --tree-help - Print help on tree symbols and exit.\n"
234 " --tree-print-groupings\n"
235 " Print top-level groupings in a separate section.\n"
236 " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n"
237 " --tree-no-leafref-target\n"
238 " Do not print target nodes of leafrefs.\n"
239 " --tree-path=SCHEMA_PATH\n"
240 " Print only the specified subtree.\n"
241 " --tree-line-length=LINE_LENGTH\n"
242 " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n"
243 " can often appear).\n\n"
244#endif
245 "\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200246}
247
Radek Krejcie9f13b12020-11-09 17:42:04 +0100248static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200249libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
250{
251 char *levstr;
252
Radek Krejcie9f13b12020-11-09 17:42:04 +0100253 switch (level) {
254 case LY_LLERR:
255 levstr = "err :";
256 break;
257 case LY_LLWRN:
258 levstr = "warn:";
259 break;
260 case LY_LLVRB:
261 levstr = "verb:";
262 break;
263 default:
264 levstr = "dbg :";
265 break;
266 }
267 if (path) {
268 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
269 } else {
270 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200271 }
272}
273
Radek Krejcie9f13b12020-11-09 17:42:04 +0100274static int
275fill_context_inputs(int argc, char *argv[], struct context *c)
276{
Radek Krejcif53b0d52020-12-03 11:29:24 +0100277 struct ly_in *in;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100278
279 /* process the operational content if any */
280 if (c->data_operational.path) {
281 if (get_input(c->data_operational.path, NULL, &c->data_operational.format, &c->data_operational.in)) {
282 return -1;
283 }
284 }
285
286 for (int i = 0; i < argc - optind; i++) {
287 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
288 LYD_FORMAT format_data = LYD_UNKNOWN;
Radek Krejcif53b0d52020-12-03 11:29:24 +0100289 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100290
291 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100292 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100293 }
294
295 if (format_schema) {
296 LY_ERR ret;
297 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
298 char *dir, *module;
299 const char *fall = "*";
300 const char **features = &fall;
301 const struct lys_module *mod;
302
303 if (parse_schema_path(argv[optind + i], &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100304 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100305 }
306
307 /* add temporarily also the path of the module itself */
308 if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {
309 path_unset = 0;
310 }
311
312 /* get features list for this module */
313 get_features(&c->schema_features, module, &features);
314
315 /* temporary cleanup */
316 free(dir);
317 free(module);
318
319 ret = lys_parse(c->ctx, in, format_schema, features, &mod);
320 ly_ctx_unset_searchdir_last(c->ctx, path_unset);
Radek Krejcif53b0d52020-12-03 11:29:24 +0100321 ly_in_free(in, 1);
322 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100323 if (ret) {
324 YLMSG_E("Processing schema module from %s failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100325 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100326 }
327
328 if (c->schema_out_format) {
329 /* modules will be printed */
330 if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
331 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100332 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100333 }
334 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100335 } else if (format_data) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100336 if (!fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100337 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100338 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100339 in = NULL;
Radek Krejci3329b652020-12-05 23:54:07 +0100340 } else {
341 ly_in_free(in, 1);
342 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100343 }
344 }
345
Radek Krejcie9f13b12020-11-09 17:42:04 +0100346 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100347
348error:
349 ly_in_free(in, 1);
350 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100351}
352
353/**
354 * @brief Process command line options and store the settings into the context.
355 *
356 * return -1 in case of error;
357 * return 0 in case of success and ready to process
358 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200359 */
360static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100361fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200362{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100363 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200364
Radek Krejcie9f13b12020-11-09 17:42:04 +0100365 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200366 struct option options[] = {
Radek Krejciff61c882020-09-03 13:04:18 +0200367 {"default", required_argument, NULL, 'd'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100368 {"disable-searchdir", no_argument, NULL, 'D'},
369 {"present", no_argument, NULL, 'e'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200370 {"format", required_argument, NULL, 'f'},
371 {"features", required_argument, NULL, 'F'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100372#ifndef NDEBUG
373 {"debug", required_argument, NULL, 'G'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200374#endif
375 {"help", no_argument, NULL, 'h'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100376 {"makeimplemented", no_argument, NULL, 'i'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200377 {"list", no_argument, NULL, 'l'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200378 {"merge", no_argument, NULL, 'm'},
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100379 {"yang-library", no_argument, NULL, 'y'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200380 {"output", required_argument, NULL, 'o'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200381 {"operational", required_argument, NULL, 'O'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100382 {"path", required_argument, NULL, 'p'},
383 {"schema-node", required_argument, NULL, 'P'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800384 {"single-node", no_argument, NULL, 'q'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200385 {"strict", no_argument, NULL, 's'},
386 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200387 {"version", no_argument, NULL, 'v'},
388 {"verbose", no_argument, NULL, 'V'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200389 {NULL, 0, NULL, 0}
390 };
Radek Krejcied5acc52019-04-25 15:57:04 +0200391
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100392 uint16_t options_ctx = YL_DEFAULT_CTX_OPTIONS;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100393 uint8_t data_type_set = 0;
394
Radek Krejcied5acc52019-04-25 15:57:04 +0200395#ifndef NDEBUG
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100396 while ((opt = getopt_long(argc, argv, "d:Def:F:hilmyo:p:P:qst:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200397#else
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100398 while ((opt = getopt_long(argc, argv, "d:Def:F:G:hilmyo:p:P:qst:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200399#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200400 switch (opt) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100401 case 'd': /* --default */
402 if (!strcasecmp(optarg, "all")) {
403 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
404 } else if (!strcasecmp(optarg, "all-tagged")) {
405 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
406 } else if (!strcasecmp(optarg, "trim")) {
407 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
408 } else if (!strcasecmp(optarg, "implicit-tagged")) {
409 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200410 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100411 YLMSG_E("Unknown default mode %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200412 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100413 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200414 }
415 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200416
Radek Krejcie9f13b12020-11-09 17:42:04 +0100417 case 'D': /* --disable-search */
Radek Krejcied5acc52019-04-25 15:57:04 +0200418 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100419 YLMSG_W("The -D option specified too many times.\n");
420 }
421 if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200422 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
423 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
424 } else {
425 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
426 }
427 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100428
429 case 'p': { /* --path */
430 struct stat st;
431
Radek Krejcied5acc52019-04-25 15:57:04 +0200432 if (stat(optarg, &st) == -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100433 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
434 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200435 }
436 if (!S_ISDIR(st.st_mode)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100437 YLMSG_E("Provided search path is not a directory.\n");
438 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200439 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100440
441 if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
442 YLMSG_E("Storing searchpath failed.\n");
443 return -1;
444 }
445
446 break;
447 } /* case 'p' */
448
449 case 'i': /* --makeimplemented */
450 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
451 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
452 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
453 } else {
454 options_ctx |= LY_CTX_REF_IMPLEMENTED;
455 }
456 break;
457
458 case 'F': /* --features */
459 if (parse_features(optarg, &c->schema_features)) {
460 return -1;
461 }
462 break;
463
464 case 'l': /* --list */
465 c->list = 1;
466 break;
467
468 case 'o': /* --output */
469 if (c->out) {
470 YLMSG_E("Only a single output can be specified.\n");
471 return -1;
472 } else {
473 if (ly_out_new_filepath(optarg, &c->out)) {
474 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
475 return -1;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200476 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200477 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200478 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100479
480 case 'f': /* --format */
481 if (!strcasecmp(optarg, "yang")) {
482 c->schema_out_format = LYS_OUT_YANG;
483 c->data_out_format = 0;
484 } else if (!strcasecmp(optarg, "yin")) {
485 c->schema_out_format = LYS_OUT_YIN;
486 c->data_out_format = 0;
487 } else if (!strcasecmp(optarg, "info")) {
488 c->schema_out_format = LYS_OUT_YANG_COMPILED;
489 c->data_out_format = 0;
490 } else if (!strcasecmp(optarg, "tree")) {
491 c->schema_out_format = LYS_OUT_TREE;
492 c->data_out_format = 0;
493 } else if (!strcasecmp(optarg, "xml")) {
494 c->schema_out_format = 0;
495 c->data_out_format = LYD_XML;
496 } else if (!strcasecmp(optarg, "json")) {
497 c->schema_out_format = 0;
498 c->data_out_format = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200499 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100500 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200501 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100502 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200503 }
504 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100505
506 case 'P': /* --schema-node */
507 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200508 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100509
510 case 'q': /* --single-node */
511 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
512 break;
513
514 case 's': /* --strict */
515 c->data_parse_options |= LYD_PARSE_STRICT;
516 break;
517
518 case 'e': /* --present */
519 c->data_validate_options |= LYD_VALIDATE_PRESENT;
520 break;
521
522 case 't': /* --type */
523 if (data_type_set) {
524 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
525 return -1;
526 }
527
528 if (!strcasecmp(optarg, "config")) {
529 c->data_parse_options |= LYD_PARSE_NO_STATE;
530 } else if (!strcasecmp(optarg, "get")) {
531 c->data_parse_options |= LYD_PARSE_ONLY;
532 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
533 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
534 } else if (!strcasecmp(optarg, "edit")) {
535 c->data_parse_options |= LYD_PARSE_ONLY;
536 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
537 c->data_type = LYD_VALIDATE_OP_RPC;
538 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
539 c->data_type = LYD_VALIDATE_OP_REPLY;
540 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
541 c->data_type = LYD_VALIDATE_OP_NOTIF;
542 } else if (!strcasecmp(optarg, "data")) {
543 /* default option */
544 } else {
545 YLMSG_E("Unknown data tree type %s\n", optarg);
546 help(1);
547 return -1;
548 }
549
550 data_type_set = 1;
551 break;
552
553 case 'O': /* --operational */
554 if (c->data_operational.path) {
555 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
556 return -1;
557 }
558 c->data_operational.path = optarg;
559 break;
560
Radek Krejcie9f13b12020-11-09 17:42:04 +0100561 case 'm': /* --merge */
562 c->data_merge = 1;
563 break;
564
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100565 case 'y': /* --yang-library */
566 options_ctx &= ~LY_CTX_NO_YANGLIBRARY;
567 break;
568
Radek Krejcie9f13b12020-11-09 17:42:04 +0100569 case 'h': /* --help */
570 help(0);
571 return 1;
572
573 case 'v': /* --version */
574 version();
575 return 1;
576
577 case 'V': { /* --verbose */
578 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
579 ly_log_level(verbosity);
580
581 if (verbosity < LY_LLDBG) {
582 ly_log_level(verbosity + 1);
583 }
584 break;
585 } /* case 'V' */
586
Radek Krejcied5acc52019-04-25 15:57:04 +0200587#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100588 case 'G': { /* --debug */
589 uint32_t dbg_groups = 0;
590 const char *ptr = optarg;
591
Radek Krejcied5acc52019-04-25 15:57:04 +0200592 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100593 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100594 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100595 ptr += sizeof "dict" - 1;
596 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100597 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100598 ptr += sizeof "xpath" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200599 }
600
601 if (ptr[0]) {
602 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100603 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
604 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200605 }
606 ++ptr;
607 }
608 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100609 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200610 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100611 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200612#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100613 } /* switch */
614 }
615
616 /* libyang context */
617 if (ly_ctx_new(NULL, options_ctx, &c->ctx)) {
618 YLMSG_E("Unable to create libyang context.\n");
619 return -1;
620 }
621 for (uint32_t u = 0; u < c->searchpaths.count; ++u) {
622 ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]);
623 }
624
625 /* additional checks for the options combinations */
626 if (!c->list && (optind >= argc)) {
627 help(1);
628 YLMSG_E("Missing <schema> to process.\n");
629 return 1;
630 }
631
632 if (c->data_merge) {
633 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
634 /* switch off the option, incompatible input data type */
635 c->data_merge = 0;
636 } else {
637 /* postpone validation after the merge of all the input data */
638 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200639 }
640 }
641
Radek Krejcie9f13b12020-11-09 17:42:04 +0100642 if (c->data_operational.path && !c->data_type) {
643 YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
644 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200645 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100646
647 /* default output stream */
648 if (!c->out) {
649 if (ly_out_new_file(stdout, &c->out)) {
650 YLMSG_E("Unable to set stdout as output.\n");
651 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200652 }
653 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100654
655 /* process input files provided as standalone command line arguments,
656 * schema modules are parsed and inserted into the context,
657 * data files are just checked and prepared into internal structures for further processing */
658 ret = fill_context_inputs(argc, argv, c);
659 if (ret) {
660 return ret;
661 }
662
663 /* the second batch of checks */
664 if (c->schema_print_options && !c->schema_out_format) {
665 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
666 }
667 if (c->schema_parse_options && !c->schema_modules.count) {
668 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
669 }
670 if (c->data_print_options && !c->data_out_format) {
671 YLMSG_W("data printer options specified, but the data output format is missing.\n");
672 }
673 if ((c->data_parse_options || c->data_type) && !c->data_inputs.count) {
674 YLMSG_W("Data parser options specified, but no data input file provided.\n");
675 }
676
677 if (c->schema_node_path) {
678 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
679 if (!c->schema_node) {
680 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
681
682 if (!c->schema_node) {
683 YLMSG_E("Invalid schema path.\n");
684 return -1;
685 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200686 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200687 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100688
Radek Krejcie9f13b12020-11-09 17:42:04 +0100689 return 0;
690}
691
692int
693main_ni(int argc, char *argv[])
694{
695 int ret = EXIT_SUCCESS, r;
696 struct context c = {0};
Radek Krejcied5acc52019-04-25 15:57:04 +0200697
698 /* set callback for printing libyang messages */
699 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100700
701 r = fill_context(argc, argv, &c);
702 if (r < 0) {
703 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200704 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100705 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200706 goto cleanup;
707 }
708
Radek Krejcie9f13b12020-11-09 17:42:04 +0100709 /* do the required job - parse, validate, print */
710
711 if (c.list) {
712 /* print the list of schemas */
713 print_list(c.out, c.ctx, c.data_out_format);
Radek Krejci047d64e2020-12-03 12:57:10 +0100714 } else {
715 if (c.schema_out_format) {
716 if (c.schema_node) {
717 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 +0100718 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100719 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100720 goto cleanup;
721 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100722 } else {
723 for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
724 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, 0,
725 c.schema_print_options);
726 if (ret) {
727 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
728 goto cleanup;
729 }
730 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100731 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200732 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100733
734 /* do the data validation despite the schema was printed */
735 if (c.data_inputs.size) {
736 if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out,
737 c.data_parse_options, c.data_validate_options, c.data_print_options,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100738 &c.data_operational, &c.data_inputs, NULL)) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100739 goto cleanup;
740 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200741 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200742 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200743
744cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100745 /* cleanup */
746 erase_context(&c);
Radek Krejcied5acc52019-04-25 15:57:04 +0200747
Radek Krejcied5acc52019-04-25 15:57:04 +0200748 return ret;
749}