blob: e1cb12fb0eeb534b3e5773ab0154b559d9fcc84a [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 */
Michal Vaskoe0665742021-02-11 11:08:44 +010070 enum lyd_type data_type;
Radek Krejcie9f13b12020-11-09 17:42:04 +010071 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
Michal Vasko667ce6b2021-01-25 15:00:27 +0100191 " -n, --not-strict\n"
192 " Do not require strict data parsing (silently skip unknown data),\n"
193 " has no effect for schemas.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100194
195 " -e, --present Validate only with the schema modules whose data actually\n"
196 " exist in the provided input data files. Takes effect only\n"
197 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
198 " mandatory nodes from modules which data are not present in the\n"
199 " provided input data files.\n\n"
200
201 " -t TYPE, --type=TYPE\n"
202 " Specify data tree type in the input data file(s):\n"
203 " data - Complete datastore with status data (default type).\n"
204 " config - Configuration datastore (without status data).\n"
205 " get - Result of the NETCONF <get> operation.\n"
206 " getconfig - Result of the NETCONF <get-config> operation.\n"
207 " edit - Content of the NETCONF <edit-config> operation.\n"
208 " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n"
209 " RPC/Action input statement.\n"
Radek Krejci6784a4e2020-12-09 14:23:05 +0100210 " reply - Reply to the RPC/Action. Note that the reply data are\n"
211 " expected inside a container representing the original\n"
212 " RPC/Action. This is necessary to identify appropriate\n"
213 " data definitions in the schema module.\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100214 " notif - Notification instance (content of the <notification>\n"
215 " element without <eventTime>).\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100216
217 " -O FILE, --operational=FILE\n"
218 " Provide optional data to extend validation of the 'rpc',\n"
219 " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n"
220 " the :running configuration datastore and state data\n"
221 " (operational datastore) referenced from the RPC/Notification.\n\n"
222
223 " -m, --merge Merge input data files into a single tree and validate at\n"
224 " once.The option has effect only for 'data' and 'config' TYPEs.\n\n"
225
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100226 " -y, --yang-library\n"
227 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
228 " Note that this module includes definitions of mandatory state\n"
229 " data that can result in unexpected data validation errors.\n\n"
230
Radek Krejcie9f13b12020-11-09 17:42:04 +0100231#if 0
232 " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n"
233 "Tree output specific options:\n"
234 " --tree-help - Print help on tree symbols and exit.\n"
235 " --tree-print-groupings\n"
236 " Print top-level groupings in a separate section.\n"
237 " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n"
238 " --tree-no-leafref-target\n"
239 " Do not print target nodes of leafrefs.\n"
240 " --tree-path=SCHEMA_PATH\n"
241 " Print only the specified subtree.\n"
242 " --tree-line-length=LINE_LENGTH\n"
243 " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n"
244 " can often appear).\n\n"
245#endif
246 "\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200247}
248
Radek Krejcie9f13b12020-11-09 17:42:04 +0100249static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200250libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
251{
252 char *levstr;
253
Radek Krejcie9f13b12020-11-09 17:42:04 +0100254 switch (level) {
255 case LY_LLERR:
256 levstr = "err :";
257 break;
258 case LY_LLWRN:
259 levstr = "warn:";
260 break;
261 case LY_LLVRB:
262 levstr = "verb:";
263 break;
264 default:
265 levstr = "dbg :";
266 break;
267 }
268 if (path) {
269 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
270 } else {
271 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200272 }
273}
274
Radek Krejcie9f13b12020-11-09 17:42:04 +0100275static int
276fill_context_inputs(int argc, char *argv[], struct context *c)
277{
Radek Krejcif53b0d52020-12-03 11:29:24 +0100278 struct ly_in *in;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100279
280 /* process the operational content if any */
281 if (c->data_operational.path) {
282 if (get_input(c->data_operational.path, NULL, &c->data_operational.format, &c->data_operational.in)) {
283 return -1;
284 }
285 }
286
287 for (int i = 0; i < argc - optind; i++) {
288 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
289 LYD_FORMAT format_data = LYD_UNKNOWN;
Radek Krejcif53b0d52020-12-03 11:29:24 +0100290 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100291
292 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100293 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100294 }
295
296 if (format_schema) {
297 LY_ERR ret;
298 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
299 char *dir, *module;
300 const char *fall = "*";
301 const char **features = &fall;
302 const struct lys_module *mod;
303
304 if (parse_schema_path(argv[optind + i], &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100305 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100306 }
307
308 /* add temporarily also the path of the module itself */
309 if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {
310 path_unset = 0;
311 }
312
313 /* get features list for this module */
314 get_features(&c->schema_features, module, &features);
315
316 /* temporary cleanup */
317 free(dir);
318 free(module);
319
320 ret = lys_parse(c->ctx, in, format_schema, features, &mod);
321 ly_ctx_unset_searchdir_last(c->ctx, path_unset);
Radek Krejcif53b0d52020-12-03 11:29:24 +0100322 ly_in_free(in, 1);
323 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100324 if (ret) {
325 YLMSG_E("Processing schema module from %s failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100326 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100327 }
328
329 if (c->schema_out_format) {
330 /* modules will be printed */
331 if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
332 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100333 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100334 }
335 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100336 } else if (format_data) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100337 if (!fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100338 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100339 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100340 in = NULL;
Radek Krejci3329b652020-12-05 23:54:07 +0100341 } else {
342 ly_in_free(in, 1);
343 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100344 }
345 }
346
Radek Krejcie9f13b12020-11-09 17:42:04 +0100347 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100348
349error:
350 ly_in_free(in, 1);
351 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100352}
353
354/**
355 * @brief Process command line options and store the settings into the context.
356 *
357 * return -1 in case of error;
358 * return 0 in case of success and ready to process
359 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200360 */
361static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100362fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200363{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100364 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200365
Radek Krejcie9f13b12020-11-09 17:42:04 +0100366 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200367 struct option options[] = {
Radek Krejciff61c882020-09-03 13:04:18 +0200368 {"default", required_argument, NULL, 'd'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100369 {"disable-searchdir", no_argument, NULL, 'D'},
370 {"present", no_argument, NULL, 'e'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200371 {"format", required_argument, NULL, 'f'},
372 {"features", required_argument, NULL, 'F'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100373#ifndef NDEBUG
374 {"debug", required_argument, NULL, 'G'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200375#endif
376 {"help", no_argument, NULL, 'h'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100377 {"makeimplemented", no_argument, NULL, 'i'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200378 {"list", no_argument, NULL, 'l'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200379 {"merge", no_argument, NULL, 'm'},
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100380 {"yang-library", no_argument, NULL, 'y'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200381 {"output", required_argument, NULL, 'o'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200382 {"operational", required_argument, NULL, 'O'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100383 {"path", required_argument, NULL, 'p'},
384 {"schema-node", required_argument, NULL, 'P'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800385 {"single-node", no_argument, NULL, 'q'},
Michal Vasko667ce6b2021-01-25 15:00:27 +0100386 {"not-strict", no_argument, NULL, 'n'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200387 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200388 {"version", no_argument, NULL, 'v'},
389 {"verbose", no_argument, NULL, 'V'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200390 {NULL, 0, NULL, 0}
391 };
Radek Krejcied5acc52019-04-25 15:57:04 +0200392
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100393 uint16_t options_ctx = YL_DEFAULT_CTX_OPTIONS;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100394 uint8_t data_type_set = 0;
395
Michal Vasko667ce6b2021-01-25 15:00:27 +0100396 c->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;
397
Radek Krejcied5acc52019-04-25 15:57:04 +0200398#ifndef NDEBUG
Michal Vasko667ce6b2021-01-25 15:00:27 +0100399 while ((opt = getopt_long(argc, argv, "d:Def:F:hilmyo:p:P:qnt:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200400#else
Michal Vasko667ce6b2021-01-25 15:00:27 +0100401 while ((opt = getopt_long(argc, argv, "d:Def:F:G:hilmyo:p:P:qnt:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200402#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200403 switch (opt) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100404 case 'd': /* --default */
405 if (!strcasecmp(optarg, "all")) {
406 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
407 } else if (!strcasecmp(optarg, "all-tagged")) {
408 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
409 } else if (!strcasecmp(optarg, "trim")) {
410 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
411 } else if (!strcasecmp(optarg, "implicit-tagged")) {
412 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200413 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100414 YLMSG_E("Unknown default mode %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200415 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100416 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200417 }
418 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200419
Radek Krejcie9f13b12020-11-09 17:42:04 +0100420 case 'D': /* --disable-search */
Radek Krejcied5acc52019-04-25 15:57:04 +0200421 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100422 YLMSG_W("The -D option specified too many times.\n");
423 }
424 if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200425 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
426 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
427 } else {
428 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
429 }
430 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100431
432 case 'p': { /* --path */
433 struct stat st;
434
Radek Krejcied5acc52019-04-25 15:57:04 +0200435 if (stat(optarg, &st) == -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100436 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
437 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200438 }
439 if (!S_ISDIR(st.st_mode)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100440 YLMSG_E("Provided search path is not a directory.\n");
441 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200442 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100443
444 if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
445 YLMSG_E("Storing searchpath failed.\n");
446 return -1;
447 }
448
449 break;
450 } /* case 'p' */
451
452 case 'i': /* --makeimplemented */
453 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
454 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
455 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
456 } else {
457 options_ctx |= LY_CTX_REF_IMPLEMENTED;
458 }
459 break;
460
461 case 'F': /* --features */
462 if (parse_features(optarg, &c->schema_features)) {
463 return -1;
464 }
465 break;
466
467 case 'l': /* --list */
468 c->list = 1;
469 break;
470
471 case 'o': /* --output */
472 if (c->out) {
473 YLMSG_E("Only a single output can be specified.\n");
474 return -1;
475 } else {
476 if (ly_out_new_filepath(optarg, &c->out)) {
477 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
478 return -1;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200479 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200480 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200481 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100482
483 case 'f': /* --format */
484 if (!strcasecmp(optarg, "yang")) {
485 c->schema_out_format = LYS_OUT_YANG;
486 c->data_out_format = 0;
487 } else if (!strcasecmp(optarg, "yin")) {
488 c->schema_out_format = LYS_OUT_YIN;
489 c->data_out_format = 0;
490 } else if (!strcasecmp(optarg, "info")) {
491 c->schema_out_format = LYS_OUT_YANG_COMPILED;
492 c->data_out_format = 0;
493 } else if (!strcasecmp(optarg, "tree")) {
494 c->schema_out_format = LYS_OUT_TREE;
495 c->data_out_format = 0;
496 } else if (!strcasecmp(optarg, "xml")) {
497 c->schema_out_format = 0;
498 c->data_out_format = LYD_XML;
499 } else if (!strcasecmp(optarg, "json")) {
500 c->schema_out_format = 0;
501 c->data_out_format = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200502 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100503 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200504 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100505 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200506 }
507 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100508
509 case 'P': /* --schema-node */
510 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200511 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100512
513 case 'q': /* --single-node */
514 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
515 break;
516
Michal Vasko667ce6b2021-01-25 15:00:27 +0100517 case 'n': /* --not-strict */
518 c->data_parse_options &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100519 break;
520
521 case 'e': /* --present */
522 c->data_validate_options |= LYD_VALIDATE_PRESENT;
523 break;
524
525 case 't': /* --type */
526 if (data_type_set) {
527 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
528 return -1;
529 }
530
531 if (!strcasecmp(optarg, "config")) {
532 c->data_parse_options |= LYD_PARSE_NO_STATE;
Michal Vaskof6bda332021-02-01 08:59:03 +0100533 c->data_validate_options |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100534 } else if (!strcasecmp(optarg, "get")) {
535 c->data_parse_options |= LYD_PARSE_ONLY;
536 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
537 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
538 } else if (!strcasecmp(optarg, "edit")) {
539 c->data_parse_options |= LYD_PARSE_ONLY;
540 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100541 c->data_type = LYD_TYPE_YANG_RPC;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100542 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100543 c->data_type = LYD_TYPE_YANG_REPLY;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100544 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100545 c->data_type = LYD_TYPE_YANG_NOTIF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100546 } else if (!strcasecmp(optarg, "data")) {
547 /* default option */
548 } else {
549 YLMSG_E("Unknown data tree type %s\n", optarg);
550 help(1);
551 return -1;
552 }
553
554 data_type_set = 1;
555 break;
556
557 case 'O': /* --operational */
558 if (c->data_operational.path) {
559 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
560 return -1;
561 }
562 c->data_operational.path = optarg;
563 break;
564
Radek Krejcie9f13b12020-11-09 17:42:04 +0100565 case 'm': /* --merge */
566 c->data_merge = 1;
567 break;
568
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100569 case 'y': /* --yang-library */
570 options_ctx &= ~LY_CTX_NO_YANGLIBRARY;
571 break;
572
Radek Krejcie9f13b12020-11-09 17:42:04 +0100573 case 'h': /* --help */
574 help(0);
575 return 1;
576
577 case 'v': /* --version */
578 version();
579 return 1;
580
581 case 'V': { /* --verbose */
582 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
583 ly_log_level(verbosity);
584
585 if (verbosity < LY_LLDBG) {
586 ly_log_level(verbosity + 1);
587 }
588 break;
589 } /* case 'V' */
590
Radek Krejcied5acc52019-04-25 15:57:04 +0200591#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100592 case 'G': { /* --debug */
593 uint32_t dbg_groups = 0;
594 const char *ptr = optarg;
595
Radek Krejcied5acc52019-04-25 15:57:04 +0200596 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100597 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100598 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100599 ptr += sizeof "dict" - 1;
600 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100601 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100602 ptr += sizeof "xpath" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200603 }
604
605 if (ptr[0]) {
606 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100607 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
608 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200609 }
610 ++ptr;
611 }
612 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100613 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200614 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100615 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200616#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100617 } /* switch */
618 }
619
620 /* libyang context */
621 if (ly_ctx_new(NULL, options_ctx, &c->ctx)) {
622 YLMSG_E("Unable to create libyang context.\n");
623 return -1;
624 }
625 for (uint32_t u = 0; u < c->searchpaths.count; ++u) {
626 ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]);
627 }
628
629 /* additional checks for the options combinations */
630 if (!c->list && (optind >= argc)) {
631 help(1);
632 YLMSG_E("Missing <schema> to process.\n");
633 return 1;
634 }
635
636 if (c->data_merge) {
637 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
638 /* switch off the option, incompatible input data type */
639 c->data_merge = 0;
640 } else {
641 /* postpone validation after the merge of all the input data */
642 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200643 }
644 }
645
Radek Krejcie9f13b12020-11-09 17:42:04 +0100646 if (c->data_operational.path && !c->data_type) {
647 YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
648 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200649 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100650
651 /* default output stream */
652 if (!c->out) {
653 if (ly_out_new_file(stdout, &c->out)) {
654 YLMSG_E("Unable to set stdout as output.\n");
655 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200656 }
657 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100658
659 /* process input files provided as standalone command line arguments,
660 * schema modules are parsed and inserted into the context,
661 * data files are just checked and prepared into internal structures for further processing */
662 ret = fill_context_inputs(argc, argv, c);
663 if (ret) {
664 return ret;
665 }
666
667 /* the second batch of checks */
668 if (c->schema_print_options && !c->schema_out_format) {
669 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
670 }
671 if (c->schema_parse_options && !c->schema_modules.count) {
672 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
673 }
674 if (c->data_print_options && !c->data_out_format) {
675 YLMSG_W("data printer options specified, but the data output format is missing.\n");
676 }
Michal Vasko667ce6b2021-01-25 15:00:27 +0100677 if (((c->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || c->data_type) && !c->data_inputs.count) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100678 YLMSG_W("Data parser options specified, but no data input file provided.\n");
679 }
680
681 if (c->schema_node_path) {
682 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
683 if (!c->schema_node) {
684 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
685
686 if (!c->schema_node) {
687 YLMSG_E("Invalid schema path.\n");
688 return -1;
689 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200690 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200691 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100692
Radek Krejcie9f13b12020-11-09 17:42:04 +0100693 return 0;
694}
695
696int
697main_ni(int argc, char *argv[])
698{
699 int ret = EXIT_SUCCESS, r;
700 struct context c = {0};
Radek Krejcied5acc52019-04-25 15:57:04 +0200701
702 /* set callback for printing libyang messages */
703 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100704
705 r = fill_context(argc, argv, &c);
706 if (r < 0) {
707 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200708 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100709 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200710 goto cleanup;
711 }
712
Radek Krejcie9f13b12020-11-09 17:42:04 +0100713 /* do the required job - parse, validate, print */
714
715 if (c.list) {
716 /* print the list of schemas */
717 print_list(c.out, c.ctx, c.data_out_format);
Radek Krejci047d64e2020-12-03 12:57:10 +0100718 } else {
719 if (c.schema_out_format) {
720 if (c.schema_node) {
721 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 +0100722 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100723 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100724 goto cleanup;
725 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100726 } else {
727 for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
728 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, 0,
729 c.schema_print_options);
730 if (ret) {
731 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
732 goto cleanup;
733 }
734 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100735 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200736 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100737
738 /* do the data validation despite the schema was printed */
739 if (c.data_inputs.size) {
740 if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out,
741 c.data_parse_options, c.data_validate_options, c.data_print_options,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100742 &c.data_operational, &c.data_inputs, NULL)) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100743 goto cleanup;
744 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200745 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200746 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200747
748cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100749 /* cleanup */
750 erase_context(&c);
Radek Krejcied5acc52019-04-25 15:57:04 +0200751
Radek Krejcied5acc52019-04-25 15:57:04 +0200752 return ret;
753}