blob: 1a17b7e098fa091183b6a737cf0e051422605c43 [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
225#if 0
226 " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n"
227 "Tree output specific options:\n"
228 " --tree-help - Print help on tree symbols and exit.\n"
229 " --tree-print-groupings\n"
230 " Print top-level groupings in a separate section.\n"
231 " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n"
232 " --tree-no-leafref-target\n"
233 " Do not print target nodes of leafrefs.\n"
234 " --tree-path=SCHEMA_PATH\n"
235 " Print only the specified subtree.\n"
236 " --tree-line-length=LINE_LENGTH\n"
237 " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n"
238 " can often appear).\n\n"
239#endif
240 "\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200241}
242
Radek Krejcie9f13b12020-11-09 17:42:04 +0100243static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200244libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
245{
246 char *levstr;
247
Radek Krejcie9f13b12020-11-09 17:42:04 +0100248 switch (level) {
249 case LY_LLERR:
250 levstr = "err :";
251 break;
252 case LY_LLWRN:
253 levstr = "warn:";
254 break;
255 case LY_LLVRB:
256 levstr = "verb:";
257 break;
258 default:
259 levstr = "dbg :";
260 break;
261 }
262 if (path) {
263 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
264 } else {
265 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200266 }
267}
268
Radek Krejcie9f13b12020-11-09 17:42:04 +0100269static int
270fill_context_inputs(int argc, char *argv[], struct context *c)
271{
Radek Krejcif53b0d52020-12-03 11:29:24 +0100272 struct ly_in *in;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100273
274 /* process the operational content if any */
275 if (c->data_operational.path) {
276 if (get_input(c->data_operational.path, NULL, &c->data_operational.format, &c->data_operational.in)) {
277 return -1;
278 }
279 }
280
281 for (int i = 0; i < argc - optind; i++) {
282 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
283 LYD_FORMAT format_data = LYD_UNKNOWN;
Radek Krejcif53b0d52020-12-03 11:29:24 +0100284 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100285
286 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100287 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100288 }
289
290 if (format_schema) {
291 LY_ERR ret;
292 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
293 char *dir, *module;
294 const char *fall = "*";
295 const char **features = &fall;
296 const struct lys_module *mod;
297
298 if (parse_schema_path(argv[optind + i], &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100299 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100300 }
301
302 /* add temporarily also the path of the module itself */
303 if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {
304 path_unset = 0;
305 }
306
307 /* get features list for this module */
308 get_features(&c->schema_features, module, &features);
309
310 /* temporary cleanup */
311 free(dir);
312 free(module);
313
314 ret = lys_parse(c->ctx, in, format_schema, features, &mod);
315 ly_ctx_unset_searchdir_last(c->ctx, path_unset);
Radek Krejcif53b0d52020-12-03 11:29:24 +0100316 ly_in_free(in, 1);
317 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100318 if (ret) {
319 YLMSG_E("Processing schema module from %s failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100320 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100321 }
322
323 if (c->schema_out_format) {
324 /* modules will be printed */
325 if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
326 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100327 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100328 }
329 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100330 } else if (format_data) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100331 if (!fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100332 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100333 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100334 in = NULL;
Radek Krejci3329b652020-12-05 23:54:07 +0100335 } else {
336 ly_in_free(in, 1);
337 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100338 }
339 }
340
Radek Krejcie9f13b12020-11-09 17:42:04 +0100341 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100342
343error:
344 ly_in_free(in, 1);
345 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100346}
347
348/**
349 * @brief Process command line options and store the settings into the context.
350 *
351 * return -1 in case of error;
352 * return 0 in case of success and ready to process
353 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200354 */
355static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100356fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200357{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100358 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200359
Radek Krejcie9f13b12020-11-09 17:42:04 +0100360 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200361 struct option options[] = {
Radek Krejciff61c882020-09-03 13:04:18 +0200362 {"default", required_argument, NULL, 'd'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100363 {"disable-searchdir", no_argument, NULL, 'D'},
364 {"present", no_argument, NULL, 'e'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200365 {"format", required_argument, NULL, 'f'},
366 {"features", required_argument, NULL, 'F'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100367#ifndef NDEBUG
368 {"debug", required_argument, NULL, 'G'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200369#endif
370 {"help", no_argument, NULL, 'h'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100371 {"makeimplemented", no_argument, NULL, 'i'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200372 {"list", no_argument, NULL, 'l'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200373 {"merge", no_argument, NULL, 'm'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200374 {"output", required_argument, NULL, 'o'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200375 {"operational", required_argument, NULL, 'O'},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100376 {"path", required_argument, NULL, 'p'},
377 {"schema-node", required_argument, NULL, 'P'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800378 {"single-node", no_argument, NULL, 'q'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200379 {"strict", no_argument, NULL, 's'},
380 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200381 {"version", no_argument, NULL, 'v'},
382 {"verbose", no_argument, NULL, 'V'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200383 {NULL, 0, NULL, 0}
384 };
Radek Krejcied5acc52019-04-25 15:57:04 +0200385
Radek Krejcie9f13b12020-11-09 17:42:04 +0100386 uint16_t options_ctx = 0;
387 uint8_t data_type_set = 0;
388
Radek Krejcied5acc52019-04-25 15:57:04 +0200389#ifndef NDEBUG
Radek Krejcide8d5cc2020-12-18 10:44:26 +0100390 while ((opt = getopt_long(argc, argv, "d:Def:F:hilmo:p:P:qst:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200391#else
Radek Krejcide8d5cc2020-12-18 10:44:26 +0100392 while ((opt = getopt_long(argc, argv, "d:Def:F:G:hilmo:p:P:qst:vV", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200393#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200394 switch (opt) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100395 case 'd': /* --default */
396 if (!strcasecmp(optarg, "all")) {
397 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
398 } else if (!strcasecmp(optarg, "all-tagged")) {
399 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
400 } else if (!strcasecmp(optarg, "trim")) {
401 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
402 } else if (!strcasecmp(optarg, "implicit-tagged")) {
403 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200404 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100405 YLMSG_E("Unknown default mode %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200406 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100407 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200408 }
409 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200410
Radek Krejcie9f13b12020-11-09 17:42:04 +0100411 case 'D': /* --disable-search */
Radek Krejcied5acc52019-04-25 15:57:04 +0200412 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100413 YLMSG_W("The -D option specified too many times.\n");
414 }
415 if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200416 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
417 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
418 } else {
419 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
420 }
421 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100422
423 case 'p': { /* --path */
424 struct stat st;
425
Radek Krejcied5acc52019-04-25 15:57:04 +0200426 if (stat(optarg, &st) == -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100427 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
428 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200429 }
430 if (!S_ISDIR(st.st_mode)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100431 YLMSG_E("Provided search path is not a directory.\n");
432 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200433 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100434
435 if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) {
436 YLMSG_E("Storing searchpath failed.\n");
437 return -1;
438 }
439
440 break;
441 } /* case 'p' */
442
443 case 'i': /* --makeimplemented */
444 if (options_ctx & LY_CTX_REF_IMPLEMENTED) {
445 options_ctx &= ~LY_CTX_REF_IMPLEMENTED;
446 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
447 } else {
448 options_ctx |= LY_CTX_REF_IMPLEMENTED;
449 }
450 break;
451
452 case 'F': /* --features */
453 if (parse_features(optarg, &c->schema_features)) {
454 return -1;
455 }
456 break;
457
458 case 'l': /* --list */
459 c->list = 1;
460 break;
461
462 case 'o': /* --output */
463 if (c->out) {
464 YLMSG_E("Only a single output can be specified.\n");
465 return -1;
466 } else {
467 if (ly_out_new_filepath(optarg, &c->out)) {
468 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
469 return -1;
Radek Krejciba03a5a2020-08-27 14:40:41 +0200470 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200471 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200472 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100473
474 case 'f': /* --format */
475 if (!strcasecmp(optarg, "yang")) {
476 c->schema_out_format = LYS_OUT_YANG;
477 c->data_out_format = 0;
478 } else if (!strcasecmp(optarg, "yin")) {
479 c->schema_out_format = LYS_OUT_YIN;
480 c->data_out_format = 0;
481 } else if (!strcasecmp(optarg, "info")) {
482 c->schema_out_format = LYS_OUT_YANG_COMPILED;
483 c->data_out_format = 0;
484 } else if (!strcasecmp(optarg, "tree")) {
485 c->schema_out_format = LYS_OUT_TREE;
486 c->data_out_format = 0;
487 } else if (!strcasecmp(optarg, "xml")) {
488 c->schema_out_format = 0;
489 c->data_out_format = LYD_XML;
490 } else if (!strcasecmp(optarg, "json")) {
491 c->schema_out_format = 0;
492 c->data_out_format = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200493 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100494 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200495 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100496 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200497 }
498 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100499
500 case 'P': /* --schema-node */
501 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200502 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100503
504 case 'q': /* --single-node */
505 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
506 break;
507
508 case 's': /* --strict */
509 c->data_parse_options |= LYD_PARSE_STRICT;
510 break;
511
512 case 'e': /* --present */
513 c->data_validate_options |= LYD_VALIDATE_PRESENT;
514 break;
515
516 case 't': /* --type */
517 if (data_type_set) {
518 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
519 return -1;
520 }
521
522 if (!strcasecmp(optarg, "config")) {
523 c->data_parse_options |= LYD_PARSE_NO_STATE;
524 } else if (!strcasecmp(optarg, "get")) {
525 c->data_parse_options |= LYD_PARSE_ONLY;
526 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) {
527 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
528 } else if (!strcasecmp(optarg, "edit")) {
529 c->data_parse_options |= LYD_PARSE_ONLY;
530 } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) {
531 c->data_type = LYD_VALIDATE_OP_RPC;
532 } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) {
533 c->data_type = LYD_VALIDATE_OP_REPLY;
534 } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) {
535 c->data_type = LYD_VALIDATE_OP_NOTIF;
536 } else if (!strcasecmp(optarg, "data")) {
537 /* default option */
538 } else {
539 YLMSG_E("Unknown data tree type %s\n", optarg);
540 help(1);
541 return -1;
542 }
543
544 data_type_set = 1;
545 break;
546
547 case 'O': /* --operational */
548 if (c->data_operational.path) {
549 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
550 return -1;
551 }
552 c->data_operational.path = optarg;
553 break;
554
Radek Krejcie9f13b12020-11-09 17:42:04 +0100555 case 'm': /* --merge */
556 c->data_merge = 1;
557 break;
558
559 case 'h': /* --help */
560 help(0);
561 return 1;
562
563 case 'v': /* --version */
564 version();
565 return 1;
566
567 case 'V': { /* --verbose */
568 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
569 ly_log_level(verbosity);
570
571 if (verbosity < LY_LLDBG) {
572 ly_log_level(verbosity + 1);
573 }
574 break;
575 } /* case 'V' */
576
Radek Krejcied5acc52019-04-25 15:57:04 +0200577#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100578 case 'G': { /* --debug */
579 uint32_t dbg_groups = 0;
580 const char *ptr = optarg;
581
Radek Krejcied5acc52019-04-25 15:57:04 +0200582 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100583 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100584 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100585 ptr += sizeof "dict" - 1;
586 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100587 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100588 ptr += sizeof "xpath" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200589 }
590
591 if (ptr[0]) {
592 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100593 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
594 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200595 }
596 ++ptr;
597 }
598 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100599 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200600 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100601 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200602#endif
Radek Krejcie9f13b12020-11-09 17:42:04 +0100603 } /* switch */
604 }
605
606 /* libyang context */
607 if (ly_ctx_new(NULL, options_ctx, &c->ctx)) {
608 YLMSG_E("Unable to create libyang context.\n");
609 return -1;
610 }
611 for (uint32_t u = 0; u < c->searchpaths.count; ++u) {
612 ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]);
613 }
614
615 /* additional checks for the options combinations */
616 if (!c->list && (optind >= argc)) {
617 help(1);
618 YLMSG_E("Missing <schema> to process.\n");
619 return 1;
620 }
621
622 if (c->data_merge) {
623 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
624 /* switch off the option, incompatible input data type */
625 c->data_merge = 0;
626 } else {
627 /* postpone validation after the merge of all the input data */
628 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200629 }
630 }
631
Radek Krejcie9f13b12020-11-09 17:42:04 +0100632 if (c->data_operational.path && !c->data_type) {
633 YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n");
634 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200635 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100636
637 /* default output stream */
638 if (!c->out) {
639 if (ly_out_new_file(stdout, &c->out)) {
640 YLMSG_E("Unable to set stdout as output.\n");
641 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200642 }
643 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100644
645 /* process input files provided as standalone command line arguments,
646 * schema modules are parsed and inserted into the context,
647 * data files are just checked and prepared into internal structures for further processing */
648 ret = fill_context_inputs(argc, argv, c);
649 if (ret) {
650 return ret;
651 }
652
653 /* the second batch of checks */
654 if (c->schema_print_options && !c->schema_out_format) {
655 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
656 }
657 if (c->schema_parse_options && !c->schema_modules.count) {
658 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
659 }
660 if (c->data_print_options && !c->data_out_format) {
661 YLMSG_W("data printer options specified, but the data output format is missing.\n");
662 }
663 if ((c->data_parse_options || c->data_type) && !c->data_inputs.count) {
664 YLMSG_W("Data parser options specified, but no data input file provided.\n");
665 }
666
667 if (c->schema_node_path) {
668 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
669 if (!c->schema_node) {
670 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
671
672 if (!c->schema_node) {
673 YLMSG_E("Invalid schema path.\n");
674 return -1;
675 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200676 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200677 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100678
Radek Krejcie9f13b12020-11-09 17:42:04 +0100679 return 0;
680}
681
682int
683main_ni(int argc, char *argv[])
684{
685 int ret = EXIT_SUCCESS, r;
686 struct context c = {0};
Radek Krejcied5acc52019-04-25 15:57:04 +0200687
688 /* set callback for printing libyang messages */
689 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100690
691 r = fill_context(argc, argv, &c);
692 if (r < 0) {
693 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200694 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100695 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200696 goto cleanup;
697 }
698
Radek Krejcie9f13b12020-11-09 17:42:04 +0100699 /* do the required job - parse, validate, print */
700
701 if (c.list) {
702 /* print the list of schemas */
703 print_list(c.out, c.ctx, c.data_out_format);
Radek Krejci047d64e2020-12-03 12:57:10 +0100704 } else {
705 if (c.schema_out_format) {
706 if (c.schema_node) {
707 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 +0100708 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100709 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100710 goto cleanup;
711 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100712 } else {
713 for (uint32_t u = 0; u < c.schema_modules.count; ++u) {
714 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, 0,
715 c.schema_print_options);
716 if (ret) {
717 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
718 goto cleanup;
719 }
720 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100721 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200722 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100723
724 /* do the data validation despite the schema was printed */
725 if (c.data_inputs.size) {
726 if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out,
727 c.data_parse_options, c.data_validate_options, c.data_print_options,
Radek Krejci6784a4e2020-12-09 14:23:05 +0100728 &c.data_operational, &c.data_inputs, NULL)) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100729 goto cleanup;
730 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200731 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200732 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200733
734cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100735 /* cleanup */
736 erase_context(&c);
Radek Krejcied5acc52019-04-25 15:57:04 +0200737
Radek Krejcied5acc52019-04-25 15:57:04 +0200738 return ret;
739}