blob: 0861ffea2daac5847d9267f7ef84be1d03c7cdf5 [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 *
6 * Copyright (c) 2015-2018 CESNET, z.s.p.o.
7 *
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
17#include <stdio.h>
18#include <stdlib.h>
19#include <getopt.h>
20#include <errno.h>
21#include <libgen.h>
22#include <sys/stat.h>
23#include <sys/times.h>
24#include <sys/types.h>
25#include <string.h>
26#include <unistd.h>
27
Radek Krejcied5acc52019-04-25 15:57:04 +020028#include "libyang.h"
29
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "tools/config.h"
31
32#include "commands.h"
33
34
Radek Krejcied5acc52019-04-25 15:57:04 +020035volatile uint8_t verbose = 0;
36
37#if 0
38/* from commands.c */
39int print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat);
40#endif
41
42void
43help(int shortout)
44{
45 fprintf(stdout, "Usage:\n");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080046 fprintf(stdout, " yanglint [options] [-f { yang | yin | tree | tree-rfc | info}] <file>...\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020047 fprintf(stdout, " Validates the YANG module in <file>, and all its dependencies.\n\n");
48 fprintf(stdout, " yanglint [options] [-f { xml | json }] <schema>... <file>...\n");
49 fprintf(stdout, " Validates the YANG modeled data in <file> according to the <schema>.\n\n");
50 fprintf(stdout, " yanglint\n");
51 fprintf(stdout, " Starts interactive mode with more features.\n\n");
52
53 if (shortout) {
54 return;
55 }
56 fprintf(stdout, "Options:\n"
57 " -h, --help Show this help message and exit.\n"
58 " -v, --version Show version number and exit.\n"
59 " -V, --verbose Show verbose messages, can be used multiple times to\n"
60 " increase verbosity.\n"
61#ifndef NDEBUG
62 " -G GROUPS, --debug=GROUPS\n"
63 " Enable printing of specific debugging message group\n"
64 " (nothing will be printed unless verbosity is set to debug):\n"
65 " <group>[,<group>]* (dict, yang, yin, xpath, diff)\n\n"
66#endif
67 " -p PATH, --path=PATH Search path for schema (YANG/YIN) modules. The option can be used multiple times.\n"
68 " Current working directory and path of the module being added is used implicitly.\n\n"
69 " -D, --disable-searchdir\n"
70 " Do not implicitly search in CWD for schema modules. If specified a second time,\n"
71 " do not even search the module directory (all modules must be explicitly specified).\n\n"
72 " -s, --strict Strict data parsing (do not skip unknown data),\n"
73 " has no effect for schemas.\n\n"
74 " -m, --merge Merge input data files into a single tree and validate at once,\n"
75 " has no effect for the auto, rpc, rpcreply and notif TYPEs.\n\n"
76 " -f FORMAT, --format=FORMAT\n"
77 " Convert to FORMAT. Supported formats: \n"
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080078 " yang, yin, tree and info for schemas,\n"
Radek Krejcied5acc52019-04-25 15:57:04 +020079 " xml, json for data.\n"
80 " -a, --auto Modify the xml output by adding envelopes for autodetection.\n\n"
81 " -i, --allimplemented Make all the imported modules implemented.\n\n"
82 " -l, --list Print info about the loaded schemas in ietf-yang-library format,\n"
83 " the -f option applies here to specify data encoding.\n"
84 " (i - imported module, I - implemented module)\n\n"
85 " -o OUTFILE, --output=OUTFILE\n"
86 " Write the output to OUTFILE instead of stdout.\n\n"
87 " -F FEATURES, --features=FEATURES\n"
88 " Features to support, default all.\n"
89 " <modname>:[<feature>,]*\n\n"
90 " -d MODE, --default=MODE\n"
91 " Print data with default values, according to the MODE\n"
92 " (to print attributes, ietf-netconf-with-defaults model\n"
93 " must be loaded):\n"
94 " all - Add missing default nodes.\n"
95 " all-tagged - Add missing default nodes and mark all the default\n"
96 " nodes with the attribute.\n"
97 " trim - Remove all nodes with a default value.\n"
98 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n"
99 " -t TYPE, --type=TYPE\n"
100 " Specify data tree type in the input data file:\n"
101 " auto - Resolve data type (one of the following) automatically\n"
102 " (as pyang does) - applicable only on XML input data.\n"
103 " data - Complete datastore with status data (default type).\n"
104 " config - Configuration datastore (without status data).\n"
105 " get - Result of the NETCONF <get> operation.\n"
106 " getconfig - Result of the NETCONF <get-config> operation.\n"
107 " edit - Content of the NETCONF <edit-config> operation.\n"
108 " rpc - Content of the NETCONF <rpc> message, defined as YANG's rpc input statement.\n"
109 " rpcreply - Reply to the RPC. The input data <file>s are expected in pairs - each RPC reply\n"
110 " input data <file> must be followed by the origin RPC input data <file> for the reply.\n"
111 " The same rule of pairing applies also in case of 'auto' TYPE and input data file\n"
112 " containing RPC reply.\n"
113 " notif - Notification instance (content of the <notification> element without <eventTime>.\n\n"
114 " -O FILE, --operational=FILE\n"
115 " - Optional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the FILE contains running\n"
116 " configuration datastore and state data (operational datastore) referenced from\n"
117 " the RPC/Notification. The same data apply to all input data <file>s. Note that\n"
118 " the file is validated as 'data' TYPE. Special value '!' can be used as FILE argument\n"
119 " to ignore the external references.\n\n"
120 " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n"
121 "Tree output specific options:\n"
122 " --tree-help - Print help on tree symbols and exit.\n"
123 " --tree-print-groupings\n"
124 " Print top-level groupings in a separate section.\n"
125 " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n"
126 " --tree-no-leafref-target\n"
127 " Do not print target nodes of leafrefs.\n"
128 " --tree-path=SCHEMA_PATH\n"
129 " Print only the specified subtree.\n"
130 " --tree-line-length=LINE_LENGTH\n"
131 " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n"
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800132 " can often appear).\n\n"
133 "Info output specific options:\n"
134 " -P INFOPATH, --info-path=INFOPATH\n"
135 " - Schema path with full module names used as node's prefixes, the path identify the root\n"
136 " node of the subtree to print information about.\n"
137 " --single-node - Print information about a single node instead of a subtree."
Radek Krejcied5acc52019-04-25 15:57:04 +0200138 "\n");
139}
140
141void
142tree_help(void)
143{
144 fprintf(stdout, "Each node is printed as:\n\n");
145 fprintf(stdout, "<status> <flags> <name> <opts> <type> <if-features>\n\n"
146 " <status> is one of:\n"
147 " + for current\n"
148 " x for deprecated\n"
149 " o for obsolete\n\n"
150 " <flags> is one of:\n"
151 " rw for configuration data\n"
152 " ro for status data\n"
153 " -x for RPCs\n"
154 " -n for Notification\n\n"
155 " <name> is the name of the node\n"
156 " (<name>) means that the node is a choice node\n"
157 " :(<name>) means that the node is a case node\n\n"
158 " if the node is augmented into the tree from another module,\n"
159 " it is printed with the module name as <module-name>:<name>.\n\n"
160 " <opts> is one of:\n"
161 " ? for an optional leaf or choice\n"
162 " ! for a presence container\n"
163 " * for a leaf-list or list\n"
164 " [<keys>] for a list's keys\n\n"
165 " <type> is the name of the type for leafs and leaf-lists\n"
166 " If there is a default value defined, it is printed within\n"
167 " angle brackets <default-value>.\n"
168 " If the type is a leafref, the type is printed as -> TARGET`\n\n"
169 " <if-features> is the list of features this node depends on,\n"
170 " printed within curly brackets and a question mark {...}?\n\n");
171}
172
173void
174version(void)
175{
176 fprintf(stdout, "yanglint %s\n", PROJECT_VERSION);
177}
178
179void
180libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
181{
182 char *levstr;
183
184 if (level <= verbose) {
185 switch(level) {
186 case LY_LLERR:
187 levstr = "err :";
188 break;
189 case LY_LLWRN:
190 levstr = "warn:";
191 break;
192 case LY_LLVRB:
193 levstr = "verb:";
194 break;
195 default:
196 levstr = "dbg :";
197 break;
198 }
199 if (path) {
200 fprintf(stderr, "%s %s (%s)\n", levstr, msg, path);
201 } else {
202 fprintf(stderr, "%s %s\n", levstr, msg);
203 }
204 }
205}
206
207/*
208 * return:
209 * 0 - error
210 * 1 - schema format
211 * 2 - data format
212 */
213static int
Radek Krejcie7b95092019-05-15 11:03:07 +0200214get_fileformat(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data)
Radek Krejcied5acc52019-04-25 15:57:04 +0200215{
216 char *ptr;
217 LYS_INFORMAT informat_s;
Radek Krejcied5acc52019-04-25 15:57:04 +0200218 LYD_FORMAT informat_d;
Radek Krejcie7b95092019-05-15 11:03:07 +0200219
Radek Krejcied5acc52019-04-25 15:57:04 +0200220 /* get the file format */
221 if ((ptr = strrchr(filename, '.')) != NULL) {
222 ++ptr;
223 if (!strcmp(ptr, "yang")) {
224 informat_s = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200225 informat_d = 0;
226 } else if (!strcmp(ptr, "yin")) {
227 informat_s = LYS_IN_YIN;
228 informat_d = 0;
229 } else if (!strcmp(ptr, "xml")) {
230 informat_s = 0;
231 informat_d = LYD_XML;
232 } else if (!strcmp(ptr, "json")) {
233 informat_s = 0;
234 informat_d = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200235 } else {
236 fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr);
237 return 0;
238 }
239 } else {
240 fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename);
241 return 0;
242 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200243
Radek Krejcied5acc52019-04-25 15:57:04 +0200244 if (data) {
245 (*data) = informat_d;
246 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200247
Radek Krejcied5acc52019-04-25 15:57:04 +0200248 if (schema) {
249 (*schema) = informat_s;
250 }
251
252 if (informat_s) {
253 return 1;
254 } else {
255 return 2;
256 }
257}
258
259int
260main_ni(int argc, char* argv[])
261{
262 int ret = EXIT_FAILURE;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800263 int opt, opt_index = 0, i, featsize = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200264 struct option options[] = {
265#if 0
266 {"auto", no_argument, NULL, 'a'},
267 {"default", required_argument, NULL, 'd'},
268#endif
269 {"format", required_argument, NULL, 'f'},
270 {"features", required_argument, NULL, 'F'},
271#if 0
272 {"tree-print-groupings", no_argument, NULL, 'g'},
273 {"tree-print-uses", no_argument, NULL, 'u'},
274 {"tree-no-leafref-target", no_argument, NULL, 'n'},
275 {"tree-path", required_argument, NULL, 'P'},
276 {"tree-line-length", required_argument, NULL, 'L'},
277#endif
278 {"help", no_argument, NULL, 'h'},
279#if 0
280 {"tree-help", no_argument, NULL, 'H'},
281#endif
282 {"allimplemented", no_argument, NULL, 'i'},
283 {"disable-cwd-search", no_argument, NULL, 'D'},
284 {"list", no_argument, NULL, 'l'},
285#if 0
286 {"merge", no_argument, NULL, 'm'},
287#endif
288 {"output", required_argument, NULL, 'o'},
289 {"path", required_argument, NULL, 'p'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800290 {"info-path", required_argument, NULL, 'P'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200291#if 0
292 {"running", required_argument, NULL, 'r'},
293 {"operational", required_argument, NULL, 'O'},
Radek Krejcie7b95092019-05-15 11:03:07 +0200294#endif
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800295 {"single-node", no_argument, NULL, 'q'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200296 {"strict", no_argument, NULL, 's'},
297 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200298 {"version", no_argument, NULL, 'v'},
299 {"verbose", no_argument, NULL, 'V'},
300#ifndef NDEBUG
301 {"debug", required_argument, NULL, 'G'},
302#endif
303 {NULL, required_argument, NULL, 'y'},
304 {NULL, 0, NULL, 0}
305 };
Radek Krejci241f6b52020-05-21 18:13:49 +0200306 struct ly_out *out = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200307 struct ly_ctx *ctx = NULL;
308 const struct lys_module *mod;
309 LYS_OUTFORMAT outformat_s = 0;
310 LYS_INFORMAT informat_s;
Radek Krejcie7b95092019-05-15 11:03:07 +0200311 LYD_FORMAT informat_d, outformat_d = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200312#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 LYD_FORMAT ylformat = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200314#endif
315 struct ly_set *searchpaths = NULL;
316 const char *outtarget_s = NULL;
317 char **feat = NULL, *ptr, *featlist, *dir;
318 struct stat st;
319 uint32_t u;
320 int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 int autodetection = 0, options_parser = 0, merge = 0;
322 const char *oper_file = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200323#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200324 const char *envelope_s = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200325 char *ylpath = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200326 int options_dflt = 0, envelope = 0;
327 struct lyxml_elem *iter, *elem;
328 struct *subroot, *next, *node;
329#endif
Michal Vaskof03ed032020-03-04 13:31:44 +0100330 struct lyd_node *tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200331 struct dataitem {
332 const char *filename;
Radek Krejcied5acc52019-04-25 15:57:04 +0200333 struct lyd_node *tree;
334 struct dataitem *next;
335 LYD_FORMAT format;
Michal Vaskoa3881362020-01-21 15:57:35 +0100336 int flags;
Radek Krejcied5acc52019-04-25 15:57:04 +0200337 } *data = NULL, *data_item, *data_prev = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200338 struct ly_set *mods = NULL;
339 void *p;
Radek Krejcied5acc52019-04-25 15:57:04 +0200340
341 opterr = 0;
342#ifndef NDEBUG
Radek Krejci693262f2019-04-29 15:23:20 +0200343 while ((opt = getopt_long(argc, argv, "acd:f:F:gunP:L:hHiDlmo:p:r:O:st:vVG:y:", options, &opt_index)) != -1)
Radek Krejcied5acc52019-04-25 15:57:04 +0200344#else
Radek Krejci693262f2019-04-29 15:23:20 +0200345 while ((opt = getopt_long(argc, argv, "acd:f:F:gunP:L:hHiDlmo:p:r:O:st:vVy:", options, &opt_index)) != -1)
Radek Krejcied5acc52019-04-25 15:57:04 +0200346#endif
347 {
348 switch (opt) {
349#if 0
350 case 'a':
351 envelope = 1;
352 break;
353 case 'd':
354 if (!strcmp(optarg, "all")) {
355 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;
356 } else if (!strcmp(optarg, "all-tagged")) {
357 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
358 } else if (!strcmp(optarg, "trim")) {
359 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;
360 } else if (!strcmp(optarg, "implicit-tagged")) {
361 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
362 } else {
363 fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg);
364 help(1);
365 goto cleanup;
366 }
367 break;
368#endif
369 case 'f':
370 if (!strcasecmp(optarg, "yang")) {
371 outformat_s = LYS_OUT_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200372 outformat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200373#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200374 } else if (!strcasecmp(optarg, "tree")) {
375 outformat_s = LYS_OUT_TREE;
376 outformat_d = 0;
377 } else if (!strcasecmp(optarg, "tree-rfc")) {
378 outformat_s = LYS_OUT_TREE;
379 outoptions_s |= LYS_OUTOPT_TREE_RFC;
380 outformat_d = 0;
FredGand944bdc2019-11-05 21:57:07 +0800381#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200382 } else if (!strcasecmp(optarg, "yin")) {
383 outformat_s = LYS_OUT_YIN;
384 outformat_d = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800385 } else if (!strcasecmp(optarg, "info")) {
386 outformat_s = LYS_OUT_YANG_COMPILED;
Radek Krejcied5acc52019-04-25 15:57:04 +0200387 outformat_d = 0;
388 } else if (!strcasecmp(optarg, "xml")) {
389 outformat_s = 0;
390 outformat_d = LYD_XML;
391 } else if (!strcasecmp(optarg, "json")) {
392 outformat_s = 0;
393 outformat_d = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200394 } else {
395 fprintf(stderr, "yanglint error: unknown output format %s\n", optarg);
396 help(1);
397 goto cleanup;
398 }
399 break;
400 case 'F':
401 featsize++;
402 if (!feat) {
403 p = malloc(sizeof *feat);
404 } else {
405 p = realloc(feat, featsize * sizeof *feat);
406 }
407 if (!p) {
408 fprintf(stderr, "yanglint error: Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
409 goto cleanup;
410 }
411 feat = p;
412 feat[featsize - 1] = strdup(optarg);
413 ptr = strchr(feat[featsize - 1], ':');
414 if (!ptr) {
415 fprintf(stderr, "yanglint error: Invalid format of the features specification (%s)", optarg);
416 goto cleanup;
417 }
418 *ptr = '\0';
419
420 break;
421#if 0
422 case 'g':
423 outoptions_s |= LYS_OUTOPT_TREE_GROUPING;
424 break;
425 case 'u':
426 outoptions_s |= LYS_OUTOPT_TREE_USES;
427 break;
428 case 'n':
429 outoptions_s |= LYS_OUTOPT_TREE_NO_LEAFREF;
430 break;
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800431#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200432 case 'P':
433 outtarget_s = optarg;
434 break;
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800435#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200436 case 'L':
437 outline_length_s = atoi(optarg);
438 break;
439#endif
440 case 'h':
441 help(0);
442 ret = EXIT_SUCCESS;
443 goto cleanup;
444#if 0
445 case 'H':
446 tree_help();
447 ret = EXIT_SUCCESS;
448 goto cleanup;
449#endif
450 case 'i':
451 options_ctx |= LY_CTX_ALLIMPLEMENTED;
452 break;
453 case 'D':
454 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
455 fprintf(stderr, "yanglint error: -D specified too many times.\n");
456 goto cleanup;
457 } else if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
458 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
459 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
460 } else {
461 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
462 }
463 break;
464 case 'l':
465 list = 1;
466 break;
467#if 0
468 case 'm':
469 merge = 1;
470 break;
471#endif
472 case 'o':
Radek Krejcia5bba312020-01-09 15:41:20 +0100473 if (out) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200474 if (ly_out_filepath(out, optarg) != NULL) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100475 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
476 goto cleanup;
477 }
478 } else {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200479 if (ly_out_new_filepath(optarg, &out)) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100480 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
481 goto cleanup;
482 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200483 }
484 break;
485 case 'p':
486 if (stat(optarg, &st) == -1) {
487 fprintf(stderr, "yanglint error: Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
488 goto cleanup;
489 }
490 if (!S_ISDIR(st.st_mode)) {
491 fprintf(stderr, "yanglint error: Provided search path is not a directory.\n");
492 goto cleanup;
493 }
494 if (!searchpaths) {
495 searchpaths = ly_set_new();
496 }
497 ly_set_add(searchpaths, optarg, 0);
498 break;
499#if 0
500 case 'r':
501 case 'O':
502 if (oper_file || (options_parser & LYD_OPT_NOEXTDEPS)) {
503 fprintf(stderr, "yanglint error: The operational datastore (-O) cannot be set multiple times.\n");
504 goto cleanup;
505 }
506 if (optarg[0] == '!') {
507 /* ignore extenral dependencies to the operational datastore */
508 options_parser |= LYD_OPT_NOEXTDEPS;
509 } else {
510 /* external file with the operational datastore */
511 oper_file = optarg;
512 }
513 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200514#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200515 case 's':
Radek Krejci7931b192020-06-25 17:05:03 +0200516 options_parser |= LYD_PARSE_STRICT;
Radek Krejcied5acc52019-04-25 15:57:04 +0200517 break;
518 case 't':
519 if (!strcmp(optarg, "auto")) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200520 autodetection = 1;
Michal Vaskob36053d2020-03-26 15:49:30 +0100521 /*} else if (!strcmp(optarg, "config")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100522 options_parser |= LYD_OPT_CONFIG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200523 } else if (!strcmp(optarg, "get")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100524 options_parser |= LYD_OPT_GET;
Radek Krejcied5acc52019-04-25 15:57:04 +0200525 } else if (!strcmp(optarg, "getconfig")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100526 options_parser |= LYD_OPT_GETCONFIG;
Michal Vaskob36053d2020-03-26 15:49:30 +0100527 } else if (!strcmp(optarg, "edit")) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100528 options_parser |= LYD_OPT_EDIT;*/
Radek Krejcied5acc52019-04-25 15:57:04 +0200529 } else if (!strcmp(optarg, "data")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100530 /* no options */
Radek Krejcied5acc52019-04-25 15:57:04 +0200531 } else {
532 fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg);
533 help(1);
534 goto cleanup;
535 }
536 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200537 case 'v':
538 version();
539 ret = EXIT_SUCCESS;
540 goto cleanup;
541 case 'V':
542 verbose++;
543 break;
544#ifndef NDEBUG
545 case 'G':
546 u = 0;
547 ptr = optarg;
548 while (ptr[0]) {
549 if (!strncmp(ptr, "dict", 4)) {
550 u |= LY_LDGDICT;
551 ptr += 4;
552 } else if (!strncmp(ptr, "yang", 4)) {
553 u |= LY_LDGYANG;
554 ptr += 4;
555 } else if (!strncmp(ptr, "yin", 3)) {
556 u |= LY_LDGYIN;
557 ptr += 3;
558 } else if (!strncmp(ptr, "xpath", 5)) {
559 u |= LY_LDGXPATH;
560 ptr += 5;
561 } else if (!strncmp(ptr, "diff", 4)) {
562 u |= LY_LDGDIFF;
563 ptr += 4;
564 }
565
566 if (ptr[0]) {
567 if (ptr[0] != ',') {
568 fprintf(stderr, "yanglint error: unknown debug group string \"%s\"\n", optarg);
569 goto cleanup;
570 }
571 ++ptr;
572 }
573 }
574 ly_verb_dbg(u);
575 break;
576#endif
577#if 0
578 case 'y':
579 ptr = strrchr(optarg, '.');
580 if (ptr) {
581 ptr++;
582 if (!strcmp(ptr, "xml")) {
583 ylformat = LYD_XML;
584 } else if (!strcmp(ptr, "json")) {
585 ylformat = LYD_JSON;
586 } else {
587 fprintf(stderr, "yanglint error: yang-library file in an unknown format \"%s\".\n", ptr);
588 goto cleanup;
589 }
590 } else {
591 fprintf(stderr, "yanglint error: yang-library file in an unknown format.\n");
592 goto cleanup;
593 }
594 ylpath = optarg;
595 break;
596#endif
597 default:
598 help(1);
599 if (optopt) {
600 fprintf(stderr, "yanglint error: invalid option: -%c\n", optopt);
601 } else {
602 fprintf(stderr, "yanglint error: invalid option: %s\n", argv[optind - 1]);
603 }
604 goto cleanup;
605 }
606 }
607
608 /* check options compatibility */
609 if (!list && optind >= argc) {
610 help(1);
611 fprintf(stderr, "yanglint error: missing <file> to process\n");
612 goto cleanup;
613 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200614 if (outformat_s && outformat_s != LYS_OUT_TREE && (optind + 1) < argc) {
615 /* we have multiple schemas to be printed as YIN or YANG */
616 fprintf(stderr, "yanglint error: too many schemas to convert and store.\n");
617 goto cleanup;
618 }
619 if (outoptions_s || outtarget_s || outline_length_s) {
620#if 0
621 if (outformat_d || (outformat_s && outformat_s != LYS_OUT_TREE)) {
622 /* we have --tree-print-grouping with other output format than tree */
623 fprintf(stderr,
624 "yanglint warning: --tree options take effect only in case of the tree output format.\n");
625 }
626 }
627 if (merge) {
628 if (autodetection || (options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) {
629 fprintf(stderr, "yanglint warning: merging not allowed, ignoring option -m.\n");
630 merge = 0;
631 } else {
632 /* first, files will be parsed as trusted to allow missing data, then the data trees will be merged
633 * and the result will be validated */
634 options_parser |= LYD_OPT_TRUSTED;
635 }
636#endif
637 }
638#if 0
639 if (!outformat_d && options_dflt) {
640 /* we have options for printing default nodes, but data output not specified */
641 fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n");
642 }
643 if (outformat_s && (options_parser || autodetection)) {
644 /* we have options for printing data tree, but output is schema */
645 fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n");
646 }
647 if (oper_file && (!autodetection && !(options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) {
648 fprintf(stderr, "yanglint warning: operational datastore applies only to RPCs or Notifications.\n");
649 /* ignore operational datastore file */
650 oper_file = NULL;
651 }
652 if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) {
653 /* add option to ignore ietf-yang-library data for implicit data type */
654 options_parser |= LYD_OPT_DATA_NO_YANGLIB;
655 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100656#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200657
658 /* set callback for printing libyang messages */
659 ly_set_log_clb(libyang_verbclb, 1);
660#if 0
661 /* create libyang context */
662 if (ylpath) {
663 ctx = ly_ctx_new_ylpath(searchpaths ? (const char*)searchpaths->set.g[0] : NULL, ylpath, ylformat, options_ctx);
664 } else {
665#else
666 {
667#endif
668 ly_ctx_new(NULL, options_ctx, &ctx);
669 }
670 if (!ctx) {
671 goto cleanup;
672 }
673
674 /* set searchpaths */
675 if (searchpaths) {
676 for (u = 0; u < searchpaths->count; u++) {
677 ly_ctx_set_searchdir(ctx, (const char*)searchpaths->objs[u]);
678 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200679 }
680
681 /* derefered setting of verbosity in libyang after context initiation */
682 ly_verb(verbose);
683
684 mods = ly_set_new();
685
686
687 /* divide input files */
688 for (i = 0; i < argc - optind; i++) {
689 /* get the file format */
Radek Krejcie7b95092019-05-15 11:03:07 +0200690 if (!get_fileformat(argv[optind + i], &informat_s, &informat_d)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200691 goto cleanup;
692 }
693
694 if (informat_s) {
695 /* load/validate schema */
Radek Krejci83126492020-08-15 15:40:04 +0200696 int unset_path = 1;
697
Radek Krejcied5acc52019-04-25 15:57:04 +0200698 if (verbose >= 2) {
699 fprintf(stdout, "Validating %s schema file.\n", argv[optind + i]);
700 }
Radek Krejci83126492020-08-15 15:40:04 +0200701
702 /* add temporarily also the path of the module itself */
Radek Krejcied5acc52019-04-25 15:57:04 +0200703 dir = strdup(argv[optind + i]);
Radek Krejci83126492020-08-15 15:40:04 +0200704 if (ly_ctx_set_searchdir(ctx, ptr = dirname(dir)) == LY_EEXIST) {
705 unset_path = 0;
706 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200707 lys_parse_path(ctx, argv[optind + i], informat_s, &mod);
Radek Krejcie58f97f2020-08-18 11:45:08 +0200708 ly_ctx_unset_searchdir_last(ctx, unset_path);
Radek Krejcied5acc52019-04-25 15:57:04 +0200709 free(dir);
710 if (!mod) {
711 goto cleanup;
712 }
713 ly_set_add(mods, (void *)mod, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200714 } else {
715 if (autodetection && informat_d != LYD_XML) {
716 /* data file content autodetection is possible only for XML input */
717 fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n");
718 goto cleanup;
719 }
720
721 /* remember data filename and its format */
722 if (!data) {
723 data = data_item = malloc(sizeof *data);
724 } else {
725 for (data_item = data; data_item->next; data_item = data_item->next);
726 data_item->next = malloc(sizeof *data_item);
727 data_item = data_item->next;
728 }
729 data_item->filename = argv[optind + i];
730 data_item->format = informat_d;
Michal Vaskoa3881362020-01-21 15:57:35 +0100731 data_item->flags = options_parser;
Radek Krejcied5acc52019-04-25 15:57:04 +0200732 data_item->tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200733 data_item->next = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200734 }
735 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200736 if (outformat_d && !data && !list) {
737 fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n");
738 goto cleanup;
739 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200740
741 /* enable specified features, if not specified, all the module's features are enabled */
742 u = 4; /* skip internal libyang modules */
743 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci3d46f612020-08-13 12:01:53 +0200744 if (!mod->implemented) {
745 continue;
746 }
747
Radek Krejcied5acc52019-04-25 15:57:04 +0200748 for (i = 0; i < featsize; i++) {
749 if (!strcmp(feat[i], mod->name)) {
750 /* parse features spec */
751 featlist = strdup(feat[i] + strlen(feat[i]) + 1);
752 ptr = NULL;
753 while((ptr = strtok(ptr ? NULL : featlist, ","))) {
754 if (verbose >= 2) {
755 fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name);
756 }
757 if (lys_feature_enable(mod, ptr)) {
758 fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name);
759 }
760 }
761 free(featlist);
762 break;
763 }
764 }
765 if (i == featsize) {
766 if (verbose >= 2) {
767 fprintf(stdout, "Enabling all features in module %s.\n", mod->name);
768 }
769 lys_feature_enable(mod, "*");
770 }
771 }
772
773 /* convert (print) to FORMAT */
774 if (outformat_s) {
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800775 if (outtarget_s) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100776 const struct lysc_node *node = lys_find_node(ctx, NULL, outtarget_s);
777 if (node) {
778 lys_print_node(out, node, outformat_s, outline_length_s, outoptions_s);
779 } else {
780 fprintf(stderr, "yanglint error: The requested schema node \"%s\" does not exists.\n", outtarget_s);
781 }
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800782 } else {
783 for (u = 0; u < mods->count; u++) {
784 if (u) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200785 ly_print(out, "\n");
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800786 }
Michal Vasko7c8439f2020-08-05 13:25:19 +0200787 lys_print_module(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s);
Radek Krejcied5acc52019-04-25 15:57:04 +0200788 }
789 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200790 } else if (data) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200791
792 /* prepare operational datastore when specified for RPC/Notification */
793 if (oper_file) {
Radek Krejci7931b192020-06-25 17:05:03 +0200794 struct ly_in *in;
795 tree = NULL;
796
797 if (ly_in_new_filepath(oper_file, 0, &in)) {
798 fprintf(stderr, "yanglint error: Unable to open an operational data file \"%s\".\n", oper_file);
Radek Krejcied5acc52019-04-25 15:57:04 +0200799 goto cleanup;
800 }
Radek Krejci7931b192020-06-25 17:05:03 +0200801 if (lyd_parse_data(ctx, in, 0, LYD_PARSE_ONLY, 0, &tree) || !tree) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200802 fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n");
Radek Krejci7931b192020-06-25 17:05:03 +0200803 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200804 goto cleanup;
805 }
Radek Krejci7931b192020-06-25 17:05:03 +0200806 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200807 }
808
809 for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) {
810 /* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */
Radek Krejcie7b95092019-05-15 11:03:07 +0200811#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200812 if (autodetection) {
813 /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */
814 options_parser &= ~LYD_OPT_DATA_NO_YANGLIB;
815 /* automatically detect data type from the data top level */
816 data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0);
817 if (!data_item->xml) {
818 fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n");
819 goto cleanup;
820 }
821
822 /* NOTE: namespace is ignored to simplify usage of this feature */
823 if (!strcmp(data_item->xml->name, "data")) {
824 if (verbose >= 2) {
825 fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename);
826 }
827 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
828 data_item->type = LYD_OPT_DATA;
829 } else if (!strcmp(data_item->xml->name, "config")) {
830 if (verbose >= 2) {
831 fprintf(stdout, "Parsing %s as config data.\n", data_item->filename);
832 }
833 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
834 data_item->type = LYD_OPT_CONFIG;
835 } else if (!strcmp(data_item->xml->name, "get-reply")) {
836 if (verbose >= 2) {
837 fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename);
838 }
839 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
840 data_item->type = LYD_OPT_GET;
841 } else if (!strcmp(data_item->xml->name, "get-config-reply")) {
842 if (verbose >= 2) {
843 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename);
844 }
845 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
846 data_item->type = LYD_OPT_GETCONFIG;
847 } else if (!strcmp(data_item->xml->name, "edit-config")) {
848 if (verbose >= 2) {
849 fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename);
850 }
851 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
852 data_item->type = LYD_OPT_EDIT;
853 } else if (!strcmp(data_item->xml->name, "rpc")) {
854 if (verbose >= 2) {
855 fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename);
856 }
857 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
858 data_item->type = LYD_OPT_RPC;
859 } else if (!strcmp(data_item->xml->name, "rpc-reply")) {
860 if (verbose >= 2) {
861 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename);
862 }
863
864 data_item->type = LYD_OPT_RPCREPLY;
865 if (!data_item->next || (data_prev && !data_prev->tree)) {
866 fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
867 goto cleanup;
868 }
869
870 continue;
871 } else if (!strcmp(data_item->xml->name, "notification")) {
872 if (verbose >= 2) {
873 fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename);
874 }
875 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
876 data_item->type = LYD_OPT_NOTIF;
877
878 /* ignore eventTime element if present */
879 while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) {
880 lyxml_free(ctx, data_item->xml->child);
881 }
882 } else {
883 fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n",
884 data_item->xml->name);
885 goto cleanup;
886 }
887
888 data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper);
889 if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) {
890parse_reply:
891 /* check result of the RPC parsing, we are going to do another parsing in this step */
892 if (ly_errno) {
893 goto cleanup;
894 }
895
896 /* check that we really have RPC for the reply */
897 if (data_item->type != LYD_OPT_RPC) {
898 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename);
899 goto cleanup;
900 }
901
902 if (data_prev->format == LYD_XML) {
903 /* ignore <ok> and <rpc-error> elements if present */
904 u = 0;
905 LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) {
906 if (!strcmp(data_prev->xml->child->name, "ok")) {
907 if (u) {
908 /* rpc-error or ok already present */
909 u = 0x8; /* error flag */
910 } else {
911 u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */
912 }
913 } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) {
914 if (u && (u & 0x1)) {
915 /* ok already present, rpc-error can be present multiple times */
916 u = 0x8; /* error flag */
917 } else {
918 u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */
919 }
920 }
921
922 if (u == 0x8) {
923 fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename);
924 goto cleanup;
925 } else if (u & 0x4) {
926 lyxml_free(ctx, data_prev->xml->child);
927 u &= ~0x4; /* unset lyxml_free() flag */
928 }
929 }
930
931 /* finally, parse RPC reply from the previous step */
932 data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child,
933 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
934 } else { /* LYD_JSON */
935 data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format,
936 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
937 }
938 }
939 } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) {
940 if (data_prev && !data_prev->tree) {
941 /* now we should have RPC for the preceding RPC reply */
942 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format,
943 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper);
944 data_item->type = LYD_OPT_RPC;
945 goto parse_reply;
946 } else {
947 /* now we have RPC reply which will be parsed in next step together with its RPC */
948 if (!data_item->next) {
949 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
950 goto cleanup;
951 }
952 if (data_item->format == LYD_XML) {
953 /* create rpc-reply container to unify handling with autodetection */
954 data_item->xml = calloc(1, sizeof *data_item->xml);
955 if (!data_item->xml) {
956 fprintf(stderr, "yanglint error: Memory allocation failed failed.\n");
957 goto cleanup;
958 }
959 data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9);
960 data_item->xml->prev = data_item->xml;
961 data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT);
962 if (data_item->xml->child) {
963 data_item->xml->child->parent = data_item->xml;
964 }
965 }
966 continue;
967 }
968 } else {
Radek Krejcie7b95092019-05-15 11:03:07 +0200969#else
970 {
971#endif
Radek Krejci7931b192020-06-25 17:05:03 +0200972 /* TODO optimize use of ly_in in the loop */
973 struct ly_in *in;
974 if (ly_in_new_filepath(data_item->filename, 0, &in)) {
975 fprintf(stderr, "yanglint error: input data file \"%s\".\n", data_item->filename);
976 goto cleanup;
977 }
Radek Krejci5536d282020-08-04 23:27:44 +0200978 if (lyd_parse_data(ctx, in, 0, options_parser, LYD_VALIDATE_PRESENT, &data_item->tree)) {
Radek Krejci7931b192020-06-25 17:05:03 +0200979 fprintf(stderr, "yanglint error: Failed to parse input data file \"%s\".\n", data_item->filename);
980 ly_in_free(in, 0);
981 goto cleanup;
982 }
983 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200984 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200985#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200986 if (merge && data != data_item) {
987 if (!data->tree) {
988 data->tree = data_item->tree;
989 } else if (data_item->tree) {
990 /* merge results */
991 if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
992 fprintf(stderr, "yanglint error: merging multiple data trees failed.\n");
993 goto cleanup;
994 }
995 }
996 data_item->tree = NULL;
997 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200998#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200999 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001000#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001001 if (merge) {
1002 /* validate the merged data tree, do not trust the input, invalidate all the data first */
1003 LY_TREE_FOR(data->tree, subroot) {
1004 LY_TREE_DFS_BEGIN(subroot, next, node) {
1005 node->validity = LYD_VAL_OK;
1006 switch (node->schema->nodetype) {
1007 case LYS_LEAFLIST:
1008 case LYS_LEAF:
1009 if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) {
1010 node->validity |= LYD_VAL_LEAFREF;
1011 }
1012 break;
1013 case LYS_LIST:
1014 node->validity |= LYD_VAL_UNIQUE;
1015 /* falls through */
1016 case LYS_CONTAINER:
1017 case LYS_NOTIF:
1018 case LYS_RPC:
1019 case LYS_ACTION:
1020 node->validity |= LYD_VAL_MAND;
1021 break;
1022 default:
1023 break;
1024 }
1025 LY_TREE_DFS_END(subroot, next, node)
1026 }
1027 }
1028 if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) {
1029 goto cleanup;
1030 }
1031 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001032#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001033 /* print only if data output format specified */
1034 if (outformat_d) {
1035 for (data_item = data; data_item; data_item = data_item->next) {
1036 if (!merge && verbose >= 2) {
1037 fprintf(stdout, "File %s:\n", data_item->filename);
1038 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001039#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001040 if (outformat_d == LYD_XML && envelope) {
1041 switch (data_item->type) {
1042 case LYD_OPT_DATA:
1043 envelope_s = "data";
1044 break;
1045 case LYD_OPT_CONFIG:
1046 envelope_s = "config";
1047 break;
1048 case LYD_OPT_GET:
1049 envelope_s = "get-reply";
1050 break;
1051 case LYD_OPT_GETCONFIG:
1052 envelope_s = "get-config-reply";
1053 break;
1054 case LYD_OPT_EDIT:
1055 envelope_s = "edit-config";
1056 break;
1057 case LYD_OPT_RPC:
1058 envelope_s = "rpc";
1059 break;
1060 case LYD_OPT_RPCREPLY:
1061 envelope_s = "rpc-reply";
1062 break;
1063 case LYD_OPT_NOTIF:
1064 envelope_s = "notification";
1065 break;
1066 }
1067 fprintf(out, "<%s>\n", envelope_s);
1068 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1069 /* action */
1070 fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n");
1071 }
1072 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001073#endif
Radek Krejci5536d282020-08-04 23:27:44 +02001074 if (!out) {
1075 ly_out_new_file(stdout, &out);
1076 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001077 lyd_print_all(out, data_item->tree, outformat_d, LYD_PRINT_FORMAT /* TODO defaults | options_dflt */);
Radek Krejcie7b95092019-05-15 11:03:07 +02001078#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001079 if (envelope_s) {
1080 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1081 fprintf(out, "</action>\n");
1082 }
1083 fprintf(out, "</%s>\n", envelope_s);
1084 }
1085 if (merge) {
1086 /* stop after first item */
1087 break;
1088 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001089#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001090 }
1091 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001092 }
1093#if 0
1094 if (list) {
1095 print_list(out, ctx, outformat_d);
1096 }
1097#endif
1098
1099 ret = EXIT_SUCCESS;
1100
1101cleanup:
Radek Krejcied5acc52019-04-25 15:57:04 +02001102 ly_set_free(mods, NULL);
1103 ly_set_free(searchpaths, NULL);
1104 for (i = 0; i < featsize; i++) {
1105 free(feat[i]);
1106 }
1107 free(feat);
Radek Krejcied5acc52019-04-25 15:57:04 +02001108 for (; data; data = data_item) {
1109 data_item = data->next;
Radek Krejcie7b95092019-05-15 11:03:07 +02001110 lyd_free_all(data->tree);
Radek Krejcied5acc52019-04-25 15:57:04 +02001111 free(data);
1112 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001113 ly_ctx_destroy(ctx, NULL);
1114
Radek Krejci241f6b52020-05-21 18:13:49 +02001115 ly_out_free(out, NULL, 1);
Radek Krejcied5acc52019-04-25 15:57:04 +02001116 return ret;
1117}