blob: 9b19ee1d9e454fc86cd98eb4ba497e3fe008b149 [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
15#include "config.h"
16
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
28#include "commands.h"
29#include "libyang.h"
30
31volatile uint8_t verbose = 0;
32
33#if 0
34/* from commands.c */
35int print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat);
36#endif
37
38void
39help(int shortout)
40{
41 fprintf(stdout, "Usage:\n");
42 fprintf(stdout, " yanglint [options] [-f { yang | yin | tree | tree-rfc | jsons}] <file>...\n");
43 fprintf(stdout, " Validates the YANG module in <file>, and all its dependencies.\n\n");
44 fprintf(stdout, " yanglint [options] [-f { xml | json }] <schema>... <file>...\n");
45 fprintf(stdout, " Validates the YANG modeled data in <file> according to the <schema>.\n\n");
46 fprintf(stdout, " yanglint\n");
47 fprintf(stdout, " Starts interactive mode with more features.\n\n");
48
49 if (shortout) {
50 return;
51 }
52 fprintf(stdout, "Options:\n"
53 " -h, --help Show this help message and exit.\n"
54 " -v, --version Show version number and exit.\n"
55 " -V, --verbose Show verbose messages, can be used multiple times to\n"
56 " increase verbosity.\n"
57#ifndef NDEBUG
58 " -G GROUPS, --debug=GROUPS\n"
59 " Enable printing of specific debugging message group\n"
60 " (nothing will be printed unless verbosity is set to debug):\n"
61 " <group>[,<group>]* (dict, yang, yin, xpath, diff)\n\n"
62#endif
63 " -p PATH, --path=PATH Search path for schema (YANG/YIN) modules. The option can be used multiple times.\n"
64 " Current working directory and path of the module being added is used implicitly.\n\n"
65 " -D, --disable-searchdir\n"
66 " Do not implicitly search in CWD for schema modules. If specified a second time,\n"
67 " do not even search the module directory (all modules must be explicitly specified).\n\n"
68 " -s, --strict Strict data parsing (do not skip unknown data),\n"
69 " has no effect for schemas.\n\n"
70 " -m, --merge Merge input data files into a single tree and validate at once,\n"
71 " has no effect for the auto, rpc, rpcreply and notif TYPEs.\n\n"
72 " -f FORMAT, --format=FORMAT\n"
73 " Convert to FORMAT. Supported formats: \n"
74 " yang, yin, tree and jsons (JSON) for schemas,\n"
75 " xml, json for data.\n"
76 " -a, --auto Modify the xml output by adding envelopes for autodetection.\n\n"
77 " -i, --allimplemented Make all the imported modules implemented.\n\n"
78 " -l, --list Print info about the loaded schemas in ietf-yang-library format,\n"
79 " the -f option applies here to specify data encoding.\n"
80 " (i - imported module, I - implemented module)\n\n"
81 " -o OUTFILE, --output=OUTFILE\n"
82 " Write the output to OUTFILE instead of stdout.\n\n"
83 " -F FEATURES, --features=FEATURES\n"
84 " Features to support, default all.\n"
85 " <modname>:[<feature>,]*\n\n"
86 " -d MODE, --default=MODE\n"
87 " Print data with default values, according to the MODE\n"
88 " (to print attributes, ietf-netconf-with-defaults model\n"
89 " must be loaded):\n"
90 " all - Add missing default nodes.\n"
91 " all-tagged - Add missing default nodes and mark all the default\n"
92 " nodes with the attribute.\n"
93 " trim - Remove all nodes with a default value.\n"
94 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n"
95 " -t TYPE, --type=TYPE\n"
96 " Specify data tree type in the input data file:\n"
97 " auto - Resolve data type (one of the following) automatically\n"
98 " (as pyang does) - applicable only on XML input data.\n"
99 " data - Complete datastore with status data (default type).\n"
100 " config - Configuration datastore (without status data).\n"
101 " get - Result of the NETCONF <get> operation.\n"
102 " getconfig - Result of the NETCONF <get-config> operation.\n"
103 " edit - Content of the NETCONF <edit-config> operation.\n"
104 " rpc - Content of the NETCONF <rpc> message, defined as YANG's rpc input statement.\n"
105 " rpcreply - Reply to the RPC. The input data <file>s are expected in pairs - each RPC reply\n"
106 " input data <file> must be followed by the origin RPC input data <file> for the reply.\n"
107 " The same rule of pairing applies also in case of 'auto' TYPE and input data file\n"
108 " containing RPC reply.\n"
109 " notif - Notification instance (content of the <notification> element without <eventTime>.\n\n"
110 " -O FILE, --operational=FILE\n"
111 " - Optional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the FILE contains running\n"
112 " configuration datastore and state data (operational datastore) referenced from\n"
113 " the RPC/Notification. The same data apply to all input data <file>s. Note that\n"
114 " the file is validated as 'data' TYPE. Special value '!' can be used as FILE argument\n"
115 " to ignore the external references.\n\n"
116 " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n"
117 "Tree output specific options:\n"
118 " --tree-help - Print help on tree symbols and exit.\n"
119 " --tree-print-groupings\n"
120 " Print top-level groupings in a separate section.\n"
121 " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n"
122 " --tree-no-leafref-target\n"
123 " Do not print target nodes of leafrefs.\n"
124 " --tree-path=SCHEMA_PATH\n"
125 " Print only the specified subtree.\n"
126 " --tree-line-length=LINE_LENGTH\n"
127 " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n"
128 " can often appear).\n"
129 "\n");
130}
131
132void
133tree_help(void)
134{
135 fprintf(stdout, "Each node is printed as:\n\n");
136 fprintf(stdout, "<status> <flags> <name> <opts> <type> <if-features>\n\n"
137 " <status> is one of:\n"
138 " + for current\n"
139 " x for deprecated\n"
140 " o for obsolete\n\n"
141 " <flags> is one of:\n"
142 " rw for configuration data\n"
143 " ro for status data\n"
144 " -x for RPCs\n"
145 " -n for Notification\n\n"
146 " <name> is the name of the node\n"
147 " (<name>) means that the node is a choice node\n"
148 " :(<name>) means that the node is a case node\n\n"
149 " if the node is augmented into the tree from another module,\n"
150 " it is printed with the module name as <module-name>:<name>.\n\n"
151 " <opts> is one of:\n"
152 " ? for an optional leaf or choice\n"
153 " ! for a presence container\n"
154 " * for a leaf-list or list\n"
155 " [<keys>] for a list's keys\n\n"
156 " <type> is the name of the type for leafs and leaf-lists\n"
157 " If there is a default value defined, it is printed within\n"
158 " angle brackets <default-value>.\n"
159 " If the type is a leafref, the type is printed as -> TARGET`\n\n"
160 " <if-features> is the list of features this node depends on,\n"
161 " printed within curly brackets and a question mark {...}?\n\n");
162}
163
164void
165version(void)
166{
167 fprintf(stdout, "yanglint %s\n", PROJECT_VERSION);
168}
169
170void
171libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
172{
173 char *levstr;
174
175 if (level <= verbose) {
176 switch(level) {
177 case LY_LLERR:
178 levstr = "err :";
179 break;
180 case LY_LLWRN:
181 levstr = "warn:";
182 break;
183 case LY_LLVRB:
184 levstr = "verb:";
185 break;
186 default:
187 levstr = "dbg :";
188 break;
189 }
190 if (path) {
191 fprintf(stderr, "%s %s (%s)\n", levstr, msg, path);
192 } else {
193 fprintf(stderr, "%s %s\n", levstr, msg);
194 }
195 }
196}
197
198/*
199 * return:
200 * 0 - error
201 * 1 - schema format
202 * 2 - data format
203 */
204static int
Radek Krejcie7b95092019-05-15 11:03:07 +0200205get_fileformat(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data)
Radek Krejcied5acc52019-04-25 15:57:04 +0200206{
207 char *ptr;
208 LYS_INFORMAT informat_s;
Radek Krejcied5acc52019-04-25 15:57:04 +0200209 LYD_FORMAT informat_d;
Radek Krejcie7b95092019-05-15 11:03:07 +0200210
Radek Krejcied5acc52019-04-25 15:57:04 +0200211 /* get the file format */
212 if ((ptr = strrchr(filename, '.')) != NULL) {
213 ++ptr;
214 if (!strcmp(ptr, "yang")) {
215 informat_s = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200216 informat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200217#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200218 } else if (!strcmp(ptr, "yin")) {
219 informat_s = LYS_IN_YIN;
220 informat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200221#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200222 } else if (!strcmp(ptr, "xml")) {
223 informat_s = 0;
224 informat_d = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200225#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200226 } else if (!strcmp(ptr, "json")) {
227 informat_s = 0;
228 informat_d = LYD_JSON;
229#endif
230 } else {
231 fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr);
232 return 0;
233 }
234 } else {
235 fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename);
236 return 0;
237 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200238
Radek Krejcied5acc52019-04-25 15:57:04 +0200239 if (data) {
240 (*data) = informat_d;
241 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200242
Radek Krejcied5acc52019-04-25 15:57:04 +0200243 if (schema) {
244 (*schema) = informat_s;
245 }
246
247 if (informat_s) {
248 return 1;
249 } else {
250 return 2;
251 }
252}
253
254int
255main_ni(int argc, char* argv[])
256{
257 int ret = EXIT_FAILURE;
Radek Krejci693262f2019-04-29 15:23:20 +0200258 int opt, opt_index = 0, i, featsize = 0, compiled = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200259 struct option options[] = {
260#if 0
261 {"auto", no_argument, NULL, 'a'},
262 {"default", required_argument, NULL, 'd'},
263#endif
264 {"format", required_argument, NULL, 'f'},
265 {"features", required_argument, NULL, 'F'},
266#if 0
267 {"tree-print-groupings", no_argument, NULL, 'g'},
268 {"tree-print-uses", no_argument, NULL, 'u'},
269 {"tree-no-leafref-target", no_argument, NULL, 'n'},
270 {"tree-path", required_argument, NULL, 'P'},
271 {"tree-line-length", required_argument, NULL, 'L'},
272#endif
Radek Krejci693262f2019-04-29 15:23:20 +0200273 {"compiled", no_argument, NULL, 'c'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200274 {"help", no_argument, NULL, 'h'},
275#if 0
276 {"tree-help", no_argument, NULL, 'H'},
277#endif
278 {"allimplemented", no_argument, NULL, 'i'},
279 {"disable-cwd-search", no_argument, NULL, 'D'},
280 {"list", no_argument, NULL, 'l'},
281#if 0
282 {"merge", no_argument, NULL, 'm'},
283#endif
284 {"output", required_argument, NULL, 'o'},
285 {"path", required_argument, NULL, 'p'},
286#if 0
287 {"running", required_argument, NULL, 'r'},
288 {"operational", required_argument, NULL, 'O'},
Radek Krejcie7b95092019-05-15 11:03:07 +0200289#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200290 {"strict", no_argument, NULL, 's'},
291 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200292 {"version", no_argument, NULL, 'v'},
293 {"verbose", no_argument, NULL, 'V'},
294#ifndef NDEBUG
295 {"debug", required_argument, NULL, 'G'},
296#endif
297 {NULL, required_argument, NULL, 'y'},
298 {NULL, 0, NULL, 0}
299 };
300 FILE *out = stdout;
301 struct ly_ctx *ctx = NULL;
302 const struct lys_module *mod;
303 LYS_OUTFORMAT outformat_s = 0;
304 LYS_INFORMAT informat_s;
Radek Krejcie7b95092019-05-15 11:03:07 +0200305 LYD_FORMAT informat_d, outformat_d = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200306#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200307 LYD_FORMAT ylformat = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200308#endif
309 struct ly_set *searchpaths = NULL;
310 const char *outtarget_s = NULL;
311 char **feat = NULL, *ptr, *featlist, *dir;
312 struct stat st;
313 uint32_t u;
314 int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200315 int autodetection = 0, options_parser = 0, merge = 0;
316 const char *oper_file = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200317#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200318 const char *envelope_s = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200319 char *ylpath = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200320 int options_dflt = 0, envelope = 0;
321 struct lyxml_elem *iter, *elem;
322 struct *subroot, *next, *node;
323#endif
324 struct lyd_node *oper = NULL;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200325 const struct lyd_node **trees = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200326 struct dataitem {
327 const char *filename;
Radek Krejcied5acc52019-04-25 15:57:04 +0200328 struct lyd_node *tree;
329 struct dataitem *next;
330 LYD_FORMAT format;
331 int type;
332 } *data = NULL, *data_item, *data_prev = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200333 struct ly_set *mods = NULL;
334 void *p;
335 int index = 0;
336
337 opterr = 0;
338#ifndef NDEBUG
Radek Krejci693262f2019-04-29 15:23:20 +0200339 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 +0200340#else
Radek Krejci693262f2019-04-29 15:23:20 +0200341 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 +0200342#endif
343 {
344 switch (opt) {
345#if 0
346 case 'a':
347 envelope = 1;
348 break;
349 case 'd':
350 if (!strcmp(optarg, "all")) {
351 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;
352 } else if (!strcmp(optarg, "all-tagged")) {
353 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
354 } else if (!strcmp(optarg, "trim")) {
355 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;
356 } else if (!strcmp(optarg, "implicit-tagged")) {
357 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
358 } else {
359 fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg);
360 help(1);
361 goto cleanup;
362 }
363 break;
364#endif
Radek Krejci693262f2019-04-29 15:23:20 +0200365 case 'c':
366 compiled = 1;
367 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200368 case 'f':
369 if (!strcasecmp(optarg, "yang")) {
370 outformat_s = LYS_OUT_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200371 outformat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200372#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200373 } else if (!strcasecmp(optarg, "tree")) {
374 outformat_s = LYS_OUT_TREE;
375 outformat_d = 0;
376 } else if (!strcasecmp(optarg, "tree-rfc")) {
377 outformat_s = LYS_OUT_TREE;
378 outoptions_s |= LYS_OUTOPT_TREE_RFC;
379 outformat_d = 0;
380 } else if (!strcasecmp(optarg, "yin")) {
381 outformat_s = LYS_OUT_YIN;
382 outformat_d = 0;
383 } else if (!strcasecmp(optarg, "jsons")) {
384 outformat_s = LYS_OUT_JSON;
385 outformat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200386#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200387 } else if (!strcasecmp(optarg, "xml")) {
388 outformat_s = 0;
389 outformat_d = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200390#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200391 } else if (!strcasecmp(optarg, "json")) {
392 outformat_s = 0;
393 outformat_d = LYD_JSON;
394#endif
395 } else {
396 fprintf(stderr, "yanglint error: unknown output format %s\n", optarg);
397 help(1);
398 goto cleanup;
399 }
400 break;
401 case 'F':
402 featsize++;
403 if (!feat) {
404 p = malloc(sizeof *feat);
405 } else {
406 p = realloc(feat, featsize * sizeof *feat);
407 }
408 if (!p) {
409 fprintf(stderr, "yanglint error: Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
410 goto cleanup;
411 }
412 feat = p;
413 feat[featsize - 1] = strdup(optarg);
414 ptr = strchr(feat[featsize - 1], ':');
415 if (!ptr) {
416 fprintf(stderr, "yanglint error: Invalid format of the features specification (%s)", optarg);
417 goto cleanup;
418 }
419 *ptr = '\0';
420
421 break;
422#if 0
423 case 'g':
424 outoptions_s |= LYS_OUTOPT_TREE_GROUPING;
425 break;
426 case 'u':
427 outoptions_s |= LYS_OUTOPT_TREE_USES;
428 break;
429 case 'n':
430 outoptions_s |= LYS_OUTOPT_TREE_NO_LEAFREF;
431 break;
432 case 'P':
433 outtarget_s = optarg;
434 break;
435 case 'L':
436 outline_length_s = atoi(optarg);
437 break;
438#endif
439 case 'h':
440 help(0);
441 ret = EXIT_SUCCESS;
442 goto cleanup;
443#if 0
444 case 'H':
445 tree_help();
446 ret = EXIT_SUCCESS;
447 goto cleanup;
448#endif
449 case 'i':
450 options_ctx |= LY_CTX_ALLIMPLEMENTED;
451 break;
452 case 'D':
453 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
454 fprintf(stderr, "yanglint error: -D specified too many times.\n");
455 goto cleanup;
456 } else if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
457 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
458 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
459 } else {
460 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
461 }
462 break;
463 case 'l':
464 list = 1;
465 break;
466#if 0
467 case 'm':
468 merge = 1;
469 break;
470#endif
471 case 'o':
472 if (out != stdout) {
473 fclose(out);
474 }
475 out = fopen(optarg, "w");
476 if (!out) {
477 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
478 goto cleanup;
479 }
480 break;
481 case 'p':
482 if (stat(optarg, &st) == -1) {
483 fprintf(stderr, "yanglint error: Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
484 goto cleanup;
485 }
486 if (!S_ISDIR(st.st_mode)) {
487 fprintf(stderr, "yanglint error: Provided search path is not a directory.\n");
488 goto cleanup;
489 }
490 if (!searchpaths) {
491 searchpaths = ly_set_new();
492 }
493 ly_set_add(searchpaths, optarg, 0);
494 break;
495#if 0
496 case 'r':
497 case 'O':
498 if (oper_file || (options_parser & LYD_OPT_NOEXTDEPS)) {
499 fprintf(stderr, "yanglint error: The operational datastore (-O) cannot be set multiple times.\n");
500 goto cleanup;
501 }
502 if (optarg[0] == '!') {
503 /* ignore extenral dependencies to the operational datastore */
504 options_parser |= LYD_OPT_NOEXTDEPS;
505 } else {
506 /* external file with the operational datastore */
507 oper_file = optarg;
508 }
509 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200510#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200511 case 's':
512 options_parser |= LYD_OPT_STRICT;
513 break;
514 case 't':
515 if (!strcmp(optarg, "auto")) {
516 options_parser = (options_parser & ~LYD_OPT_TYPEMASK);
517 autodetection = 1;
518 } else if (!strcmp(optarg, "config")) {
519 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
520 } else if (!strcmp(optarg, "get")) {
521 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
522 } else if (!strcmp(optarg, "getconfig")) {
523 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
524 } else if (!strcmp(optarg, "edit")) {
525 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
526 } else if (!strcmp(optarg, "data")) {
527 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
528 } else if (!strcmp(optarg, "rpc")) {
529 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
530 } else if (!strcmp(optarg, "rpcreply")) {
531 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
532 } else if (!strcmp(optarg, "notif")) {
533 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
534 } else {
535 fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg);
536 help(1);
537 goto cleanup;
538 }
539 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200540 case 'v':
541 version();
542 ret = EXIT_SUCCESS;
543 goto cleanup;
544 case 'V':
545 verbose++;
546 break;
547#ifndef NDEBUG
548 case 'G':
549 u = 0;
550 ptr = optarg;
551 while (ptr[0]) {
552 if (!strncmp(ptr, "dict", 4)) {
553 u |= LY_LDGDICT;
554 ptr += 4;
555 } else if (!strncmp(ptr, "yang", 4)) {
556 u |= LY_LDGYANG;
557 ptr += 4;
558 } else if (!strncmp(ptr, "yin", 3)) {
559 u |= LY_LDGYIN;
560 ptr += 3;
561 } else if (!strncmp(ptr, "xpath", 5)) {
562 u |= LY_LDGXPATH;
563 ptr += 5;
564 } else if (!strncmp(ptr, "diff", 4)) {
565 u |= LY_LDGDIFF;
566 ptr += 4;
567 }
568
569 if (ptr[0]) {
570 if (ptr[0] != ',') {
571 fprintf(stderr, "yanglint error: unknown debug group string \"%s\"\n", optarg);
572 goto cleanup;
573 }
574 ++ptr;
575 }
576 }
577 ly_verb_dbg(u);
578 break;
579#endif
580#if 0
581 case 'y':
582 ptr = strrchr(optarg, '.');
583 if (ptr) {
584 ptr++;
585 if (!strcmp(ptr, "xml")) {
586 ylformat = LYD_XML;
587 } else if (!strcmp(ptr, "json")) {
588 ylformat = LYD_JSON;
589 } else {
590 fprintf(stderr, "yanglint error: yang-library file in an unknown format \"%s\".\n", ptr);
591 goto cleanup;
592 }
593 } else {
594 fprintf(stderr, "yanglint error: yang-library file in an unknown format.\n");
595 goto cleanup;
596 }
597 ylpath = optarg;
598 break;
599#endif
600 default:
601 help(1);
602 if (optopt) {
603 fprintf(stderr, "yanglint error: invalid option: -%c\n", optopt);
604 } else {
605 fprintf(stderr, "yanglint error: invalid option: %s\n", argv[optind - 1]);
606 }
607 goto cleanup;
608 }
609 }
610
611 /* check options compatibility */
612 if (!list && optind >= argc) {
613 help(1);
614 fprintf(stderr, "yanglint error: missing <file> to process\n");
615 goto cleanup;
616 }
Radek Krejci693262f2019-04-29 15:23:20 +0200617 if (compiled) {
618 if (!outformat_s) {
619 fprintf(stderr, "yanglint warning: --compiled option takes effect only in case of printing schemas.\n");
620 } else {
621 outformat_s++;
622 }
623 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200624 if (outformat_s && outformat_s != LYS_OUT_TREE && (optind + 1) < argc) {
625 /* we have multiple schemas to be printed as YIN or YANG */
626 fprintf(stderr, "yanglint error: too many schemas to convert and store.\n");
627 goto cleanup;
628 }
629 if (outoptions_s || outtarget_s || outline_length_s) {
630#if 0
631 if (outformat_d || (outformat_s && outformat_s != LYS_OUT_TREE)) {
632 /* we have --tree-print-grouping with other output format than tree */
633 fprintf(stderr,
634 "yanglint warning: --tree options take effect only in case of the tree output format.\n");
635 }
636 }
637 if (merge) {
638 if (autodetection || (options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) {
639 fprintf(stderr, "yanglint warning: merging not allowed, ignoring option -m.\n");
640 merge = 0;
641 } else {
642 /* first, files will be parsed as trusted to allow missing data, then the data trees will be merged
643 * and the result will be validated */
644 options_parser |= LYD_OPT_TRUSTED;
645 }
646#endif
647 }
648#if 0
649 if (!outformat_d && options_dflt) {
650 /* we have options for printing default nodes, but data output not specified */
651 fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n");
652 }
653 if (outformat_s && (options_parser || autodetection)) {
654 /* we have options for printing data tree, but output is schema */
655 fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n");
656 }
657 if (oper_file && (!autodetection && !(options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) {
658 fprintf(stderr, "yanglint warning: operational datastore applies only to RPCs or Notifications.\n");
659 /* ignore operational datastore file */
660 oper_file = NULL;
661 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200662#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200663 if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) {
664 /* add option to ignore ietf-yang-library data for implicit data type */
665 options_parser |= LYD_OPT_DATA_NO_YANGLIB;
666 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200667
668 /* set callback for printing libyang messages */
669 ly_set_log_clb(libyang_verbclb, 1);
670#if 0
671 /* create libyang context */
672 if (ylpath) {
673 ctx = ly_ctx_new_ylpath(searchpaths ? (const char*)searchpaths->set.g[0] : NULL, ylpath, ylformat, options_ctx);
674 } else {
675#else
676 {
677#endif
678 ly_ctx_new(NULL, options_ctx, &ctx);
679 }
680 if (!ctx) {
681 goto cleanup;
682 }
683
684 /* set searchpaths */
685 if (searchpaths) {
686 for (u = 0; u < searchpaths->count; u++) {
687 ly_ctx_set_searchdir(ctx, (const char*)searchpaths->objs[u]);
688 }
689 index = u + 1;
690 }
691
692 /* derefered setting of verbosity in libyang after context initiation */
693 ly_verb(verbose);
694
695 mods = ly_set_new();
696
697
698 /* divide input files */
699 for (i = 0; i < argc - optind; i++) {
700 /* get the file format */
Radek Krejcie7b95092019-05-15 11:03:07 +0200701 if (!get_fileformat(argv[optind + i], &informat_s, &informat_d)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200702 goto cleanup;
703 }
704
705 if (informat_s) {
706 /* load/validate schema */
707 if (verbose >= 2) {
708 fprintf(stdout, "Validating %s schema file.\n", argv[optind + i]);
709 }
710 dir = strdup(argv[optind + i]);
711 ly_ctx_set_searchdir(ctx, ptr = dirname(dir));
712 mod = lys_parse_path(ctx, argv[optind + i], informat_s);
713 ly_ctx_unset_searchdir(ctx, index);
714 free(dir);
715 if (!mod) {
716 goto cleanup;
717 }
718 ly_set_add(mods, (void *)mod, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200719 } else {
720 if (autodetection && informat_d != LYD_XML) {
721 /* data file content autodetection is possible only for XML input */
722 fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n");
723 goto cleanup;
724 }
725
726 /* remember data filename and its format */
727 if (!data) {
728 data = data_item = malloc(sizeof *data);
729 } else {
730 for (data_item = data; data_item->next; data_item = data_item->next);
731 data_item->next = malloc(sizeof *data_item);
732 data_item = data_item->next;
733 }
734 data_item->filename = argv[optind + i];
735 data_item->format = informat_d;
736 data_item->type = options_parser & LYD_OPT_TYPEMASK;
737 data_item->tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200738 data_item->next = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200739 }
740 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200741 if (outformat_d && !data && !list) {
742 fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n");
743 goto cleanup;
744 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200745
746 /* enable specified features, if not specified, all the module's features are enabled */
747 u = 4; /* skip internal libyang modules */
748 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
749 for (i = 0; i < featsize; i++) {
750 if (!strcmp(feat[i], mod->name)) {
751 /* parse features spec */
752 featlist = strdup(feat[i] + strlen(feat[i]) + 1);
753 ptr = NULL;
754 while((ptr = strtok(ptr ? NULL : featlist, ","))) {
755 if (verbose >= 2) {
756 fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name);
757 }
758 if (lys_feature_enable(mod, ptr)) {
759 fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name);
760 }
761 }
762 free(featlist);
763 break;
764 }
765 }
766 if (i == featsize) {
767 if (verbose >= 2) {
768 fprintf(stdout, "Enabling all features in module %s.\n", mod->name);
769 }
770 lys_feature_enable(mod, "*");
771 }
772 }
773
774 /* convert (print) to FORMAT */
775 if (outformat_s) {
776 if (outformat_s == LYS_OUT_JSON && mods->count > 1) {
777 fputs("[", out);
778 }
779 for (u = 0; u < mods->count; u++) {
780 if (u) {
781 if (outformat_s == LYS_OUT_JSON) {
782 fputs(",\n", out);
783 } else {
784 fputs("\n", out);
785 }
786 }
787 lys_print_file(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s);
788 }
789 if (outformat_s == LYS_OUT_JSON) {
790 if (mods->count > 1) {
791 fputs("]\n", out);
792 } else if (mods->count == 1) {
793 fputs("\n", out);
794 }
795 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200796 } else if (data) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200797
798 /* prepare operational datastore when specified for RPC/Notification */
799 if (oper_file) {
800 /* get the file format */
801 if (!get_fileformat(oper_file, NULL, &informat_d)) {
802 goto cleanup;
803 } else if (!informat_d) {
804 fprintf(stderr, "yanglint error: The operational data are expected in XML or JSON format.\n");
805 goto cleanup;
806 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200807 oper = lyd_parse_path(ctx, oper_file, informat_d, LYD_OPT_DATA_NO_YANGLIB | LYD_OPT_TRUSTED, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +0200808 if (!oper) {
809 fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n");
810 goto cleanup;
811 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200812 trees = lyd_trees_new(1, oper);
Radek Krejcied5acc52019-04-25 15:57:04 +0200813 }
814
815 for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) {
816 /* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */
Radek Krejcie7b95092019-05-15 11:03:07 +0200817#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200818 if (autodetection) {
819 /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */
820 options_parser &= ~LYD_OPT_DATA_NO_YANGLIB;
821 /* automatically detect data type from the data top level */
822 data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0);
823 if (!data_item->xml) {
824 fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n");
825 goto cleanup;
826 }
827
828 /* NOTE: namespace is ignored to simplify usage of this feature */
829 if (!strcmp(data_item->xml->name, "data")) {
830 if (verbose >= 2) {
831 fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename);
832 }
833 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
834 data_item->type = LYD_OPT_DATA;
835 } else if (!strcmp(data_item->xml->name, "config")) {
836 if (verbose >= 2) {
837 fprintf(stdout, "Parsing %s as config data.\n", data_item->filename);
838 }
839 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
840 data_item->type = LYD_OPT_CONFIG;
841 } else if (!strcmp(data_item->xml->name, "get-reply")) {
842 if (verbose >= 2) {
843 fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename);
844 }
845 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
846 data_item->type = LYD_OPT_GET;
847 } else if (!strcmp(data_item->xml->name, "get-config-reply")) {
848 if (verbose >= 2) {
849 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename);
850 }
851 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
852 data_item->type = LYD_OPT_GETCONFIG;
853 } else if (!strcmp(data_item->xml->name, "edit-config")) {
854 if (verbose >= 2) {
855 fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename);
856 }
857 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
858 data_item->type = LYD_OPT_EDIT;
859 } else if (!strcmp(data_item->xml->name, "rpc")) {
860 if (verbose >= 2) {
861 fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename);
862 }
863 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
864 data_item->type = LYD_OPT_RPC;
865 } else if (!strcmp(data_item->xml->name, "rpc-reply")) {
866 if (verbose >= 2) {
867 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename);
868 }
869
870 data_item->type = LYD_OPT_RPCREPLY;
871 if (!data_item->next || (data_prev && !data_prev->tree)) {
872 fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
873 goto cleanup;
874 }
875
876 continue;
877 } else if (!strcmp(data_item->xml->name, "notification")) {
878 if (verbose >= 2) {
879 fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename);
880 }
881 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
882 data_item->type = LYD_OPT_NOTIF;
883
884 /* ignore eventTime element if present */
885 while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) {
886 lyxml_free(ctx, data_item->xml->child);
887 }
888 } else {
889 fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n",
890 data_item->xml->name);
891 goto cleanup;
892 }
893
894 data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper);
895 if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) {
896parse_reply:
897 /* check result of the RPC parsing, we are going to do another parsing in this step */
898 if (ly_errno) {
899 goto cleanup;
900 }
901
902 /* check that we really have RPC for the reply */
903 if (data_item->type != LYD_OPT_RPC) {
904 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename);
905 goto cleanup;
906 }
907
908 if (data_prev->format == LYD_XML) {
909 /* ignore <ok> and <rpc-error> elements if present */
910 u = 0;
911 LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) {
912 if (!strcmp(data_prev->xml->child->name, "ok")) {
913 if (u) {
914 /* rpc-error or ok already present */
915 u = 0x8; /* error flag */
916 } else {
917 u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */
918 }
919 } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) {
920 if (u && (u & 0x1)) {
921 /* ok already present, rpc-error can be present multiple times */
922 u = 0x8; /* error flag */
923 } else {
924 u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */
925 }
926 }
927
928 if (u == 0x8) {
929 fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename);
930 goto cleanup;
931 } else if (u & 0x4) {
932 lyxml_free(ctx, data_prev->xml->child);
933 u &= ~0x4; /* unset lyxml_free() flag */
934 }
935 }
936
937 /* finally, parse RPC reply from the previous step */
938 data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child,
939 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
940 } else { /* LYD_JSON */
941 data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format,
942 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
943 }
944 }
945 } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) {
946 if (data_prev && !data_prev->tree) {
947 /* now we should have RPC for the preceding RPC reply */
948 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format,
949 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper);
950 data_item->type = LYD_OPT_RPC;
951 goto parse_reply;
952 } else {
953 /* now we have RPC reply which will be parsed in next step together with its RPC */
954 if (!data_item->next) {
955 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
956 goto cleanup;
957 }
958 if (data_item->format == LYD_XML) {
959 /* create rpc-reply container to unify handling with autodetection */
960 data_item->xml = calloc(1, sizeof *data_item->xml);
961 if (!data_item->xml) {
962 fprintf(stderr, "yanglint error: Memory allocation failed failed.\n");
963 goto cleanup;
964 }
965 data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9);
966 data_item->xml->prev = data_item->xml;
967 data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT);
968 if (data_item->xml->child) {
969 data_item->xml->child->parent = data_item->xml;
970 }
971 }
972 continue;
973 }
974 } else {
Radek Krejcie7b95092019-05-15 11:03:07 +0200975#else
976 {
977#endif
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200978 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, options_parser, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200979 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200980 if (ly_err_first(ctx)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200981 goto cleanup;
982 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200983#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200984 if (merge && data != data_item) {
985 if (!data->tree) {
986 data->tree = data_item->tree;
987 } else if (data_item->tree) {
988 /* merge results */
989 if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
990 fprintf(stderr, "yanglint error: merging multiple data trees failed.\n");
991 goto cleanup;
992 }
993 }
994 data_item->tree = NULL;
995 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200996#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200997 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200998#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200999 if (merge) {
1000 /* validate the merged data tree, do not trust the input, invalidate all the data first */
1001 LY_TREE_FOR(data->tree, subroot) {
1002 LY_TREE_DFS_BEGIN(subroot, next, node) {
1003 node->validity = LYD_VAL_OK;
1004 switch (node->schema->nodetype) {
1005 case LYS_LEAFLIST:
1006 case LYS_LEAF:
1007 if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) {
1008 node->validity |= LYD_VAL_LEAFREF;
1009 }
1010 break;
1011 case LYS_LIST:
1012 node->validity |= LYD_VAL_UNIQUE;
1013 /* falls through */
1014 case LYS_CONTAINER:
1015 case LYS_NOTIF:
1016 case LYS_RPC:
1017 case LYS_ACTION:
1018 node->validity |= LYD_VAL_MAND;
1019 break;
1020 default:
1021 break;
1022 }
1023 LY_TREE_DFS_END(subroot, next, node)
1024 }
1025 }
1026 if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) {
1027 goto cleanup;
1028 }
1029 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001030#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001031 /* print only if data output format specified */
1032 if (outformat_d) {
1033 for (data_item = data; data_item; data_item = data_item->next) {
1034 if (!merge && verbose >= 2) {
1035 fprintf(stdout, "File %s:\n", data_item->filename);
1036 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001037#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001038 if (outformat_d == LYD_XML && envelope) {
1039 switch (data_item->type) {
1040 case LYD_OPT_DATA:
1041 envelope_s = "data";
1042 break;
1043 case LYD_OPT_CONFIG:
1044 envelope_s = "config";
1045 break;
1046 case LYD_OPT_GET:
1047 envelope_s = "get-reply";
1048 break;
1049 case LYD_OPT_GETCONFIG:
1050 envelope_s = "get-config-reply";
1051 break;
1052 case LYD_OPT_EDIT:
1053 envelope_s = "edit-config";
1054 break;
1055 case LYD_OPT_RPC:
1056 envelope_s = "rpc";
1057 break;
1058 case LYD_OPT_RPCREPLY:
1059 envelope_s = "rpc-reply";
1060 break;
1061 case LYD_OPT_NOTIF:
1062 envelope_s = "notification";
1063 break;
1064 }
1065 fprintf(out, "<%s>\n", envelope_s);
1066 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1067 /* action */
1068 fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n");
1069 }
1070 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001071#endif
1072 lyd_print_file(out, (data_item->type == LYD_OPT_RPCREPLY) ? lyd_node_children(data_item->tree) : data_item->tree,
1073 outformat_d, LYDP_WITHSIBLINGS | LYDP_FORMAT /* TODO defaults | options_dflt */);
1074#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001075 if (envelope_s) {
1076 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1077 fprintf(out, "</action>\n");
1078 }
1079 fprintf(out, "</%s>\n", envelope_s);
1080 }
1081 if (merge) {
1082 /* stop after first item */
1083 break;
1084 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001085#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001086 }
1087 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001088 }
1089#if 0
1090 if (list) {
1091 print_list(out, ctx, outformat_d);
1092 }
1093#endif
1094
1095 ret = EXIT_SUCCESS;
1096
1097cleanup:
1098 if (out && out != stdout) {
1099 fclose(out);
1100 }
1101 ly_set_free(mods, NULL);
1102 ly_set_free(searchpaths, NULL);
1103 for (i = 0; i < featsize; i++) {
1104 free(feat[i]);
1105 }
1106 free(feat);
Radek Krejcied5acc52019-04-25 15:57:04 +02001107 for (; data; data = data_item) {
1108 data_item = data->next;
Radek Krejcie7b95092019-05-15 11:03:07 +02001109 lyd_free_all(data->tree);
Radek Krejcied5acc52019-04-25 15:57:04 +02001110 free(data);
1111 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +02001112 lyd_trees_free(trees, 1);
Radek Krejcied5acc52019-04-25 15:57:04 +02001113 ly_ctx_destroy(ctx, NULL);
1114
1115 return ret;
1116}