blob: 6cf598ed767fa0f2db83ababfc876bd9263f312f [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;
217 } else if (!strcmp(ptr, "yin")) {
218 informat_s = LYS_IN_YIN;
219 informat_d = 0;
220 } else if (!strcmp(ptr, "xml")) {
221 informat_s = 0;
222 informat_d = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200223#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200224 } else if (!strcmp(ptr, "json")) {
225 informat_s = 0;
226 informat_d = LYD_JSON;
227#endif
228 } else {
229 fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr);
230 return 0;
231 }
232 } else {
233 fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename);
234 return 0;
235 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200236
Radek Krejcied5acc52019-04-25 15:57:04 +0200237 if (data) {
238 (*data) = informat_d;
239 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200240
Radek Krejcied5acc52019-04-25 15:57:04 +0200241 if (schema) {
242 (*schema) = informat_s;
243 }
244
245 if (informat_s) {
246 return 1;
247 } else {
248 return 2;
249 }
250}
251
252int
253main_ni(int argc, char* argv[])
254{
255 int ret = EXIT_FAILURE;
Radek Krejci693262f2019-04-29 15:23:20 +0200256 int opt, opt_index = 0, i, featsize = 0, compiled = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200257 struct option options[] = {
258#if 0
259 {"auto", no_argument, NULL, 'a'},
260 {"default", required_argument, NULL, 'd'},
261#endif
262 {"format", required_argument, NULL, 'f'},
263 {"features", required_argument, NULL, 'F'},
264#if 0
265 {"tree-print-groupings", no_argument, NULL, 'g'},
266 {"tree-print-uses", no_argument, NULL, 'u'},
267 {"tree-no-leafref-target", no_argument, NULL, 'n'},
268 {"tree-path", required_argument, NULL, 'P'},
269 {"tree-line-length", required_argument, NULL, 'L'},
270#endif
Radek Krejci693262f2019-04-29 15:23:20 +0200271 {"compiled", no_argument, NULL, 'c'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200272 {"help", no_argument, NULL, 'h'},
273#if 0
274 {"tree-help", no_argument, NULL, 'H'},
275#endif
276 {"allimplemented", no_argument, NULL, 'i'},
277 {"disable-cwd-search", no_argument, NULL, 'D'},
278 {"list", no_argument, NULL, 'l'},
279#if 0
280 {"merge", no_argument, NULL, 'm'},
281#endif
282 {"output", required_argument, NULL, 'o'},
283 {"path", required_argument, NULL, 'p'},
284#if 0
285 {"running", required_argument, NULL, 'r'},
286 {"operational", required_argument, NULL, 'O'},
Radek Krejcie7b95092019-05-15 11:03:07 +0200287#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200288 {"strict", no_argument, NULL, 's'},
289 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200290 {"version", no_argument, NULL, 'v'},
291 {"verbose", no_argument, NULL, 'V'},
292#ifndef NDEBUG
293 {"debug", required_argument, NULL, 'G'},
294#endif
295 {NULL, required_argument, NULL, 'y'},
296 {NULL, 0, NULL, 0}
297 };
298 FILE *out = stdout;
299 struct ly_ctx *ctx = NULL;
300 const struct lys_module *mod;
301 LYS_OUTFORMAT outformat_s = 0;
302 LYS_INFORMAT informat_s;
Radek Krejcie7b95092019-05-15 11:03:07 +0200303 LYD_FORMAT informat_d, outformat_d = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200304#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200305 LYD_FORMAT ylformat = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200306#endif
307 struct ly_set *searchpaths = NULL;
308 const char *outtarget_s = NULL;
309 char **feat = NULL, *ptr, *featlist, *dir;
310 struct stat st;
311 uint32_t u;
312 int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 int autodetection = 0, options_parser = 0, merge = 0;
314 const char *oper_file = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200315#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200316 const char *envelope_s = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200317 char *ylpath = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200318 int options_dflt = 0, envelope = 0;
319 struct lyxml_elem *iter, *elem;
320 struct *subroot, *next, *node;
321#endif
322 struct lyd_node *oper = NULL;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200323 const struct lyd_node **trees = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200324 struct dataitem {
325 const char *filename;
Radek Krejcied5acc52019-04-25 15:57:04 +0200326 struct lyd_node *tree;
327 struct dataitem *next;
328 LYD_FORMAT format;
329 int type;
330 } *data = NULL, *data_item, *data_prev = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200331 struct ly_set *mods = NULL;
332 void *p;
333 int index = 0;
334
335 opterr = 0;
336#ifndef NDEBUG
Radek Krejci693262f2019-04-29 15:23:20 +0200337 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 +0200338#else
Radek Krejci693262f2019-04-29 15:23:20 +0200339 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 +0200340#endif
341 {
342 switch (opt) {
343#if 0
344 case 'a':
345 envelope = 1;
346 break;
347 case 'd':
348 if (!strcmp(optarg, "all")) {
349 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;
350 } else if (!strcmp(optarg, "all-tagged")) {
351 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
352 } else if (!strcmp(optarg, "trim")) {
353 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;
354 } else if (!strcmp(optarg, "implicit-tagged")) {
355 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
356 } else {
357 fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg);
358 help(1);
359 goto cleanup;
360 }
361 break;
362#endif
Radek Krejci693262f2019-04-29 15:23:20 +0200363 case 'c':
364 compiled = 1;
365 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200366 case 'f':
367 if (!strcasecmp(optarg, "yang")) {
368 outformat_s = LYS_OUT_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200369 outformat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200370#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200371 } else if (!strcasecmp(optarg, "tree")) {
372 outformat_s = LYS_OUT_TREE;
373 outformat_d = 0;
374 } else if (!strcasecmp(optarg, "tree-rfc")) {
375 outformat_s = LYS_OUT_TREE;
376 outoptions_s |= LYS_OUTOPT_TREE_RFC;
377 outformat_d = 0;
378 } else if (!strcasecmp(optarg, "yin")) {
379 outformat_s = LYS_OUT_YIN;
380 outformat_d = 0;
381 } else if (!strcasecmp(optarg, "jsons")) {
382 outformat_s = LYS_OUT_JSON;
383 outformat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200384#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200385 } else if (!strcasecmp(optarg, "xml")) {
386 outformat_s = 0;
387 outformat_d = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200388#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200389 } else if (!strcasecmp(optarg, "json")) {
390 outformat_s = 0;
391 outformat_d = LYD_JSON;
392#endif
393 } else {
394 fprintf(stderr, "yanglint error: unknown output format %s\n", optarg);
395 help(1);
396 goto cleanup;
397 }
398 break;
399 case 'F':
400 featsize++;
401 if (!feat) {
402 p = malloc(sizeof *feat);
403 } else {
404 p = realloc(feat, featsize * sizeof *feat);
405 }
406 if (!p) {
407 fprintf(stderr, "yanglint error: Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
408 goto cleanup;
409 }
410 feat = p;
411 feat[featsize - 1] = strdup(optarg);
412 ptr = strchr(feat[featsize - 1], ':');
413 if (!ptr) {
414 fprintf(stderr, "yanglint error: Invalid format of the features specification (%s)", optarg);
415 goto cleanup;
416 }
417 *ptr = '\0';
418
419 break;
420#if 0
421 case 'g':
422 outoptions_s |= LYS_OUTOPT_TREE_GROUPING;
423 break;
424 case 'u':
425 outoptions_s |= LYS_OUTOPT_TREE_USES;
426 break;
427 case 'n':
428 outoptions_s |= LYS_OUTOPT_TREE_NO_LEAFREF;
429 break;
430 case 'P':
431 outtarget_s = optarg;
432 break;
433 case 'L':
434 outline_length_s = atoi(optarg);
435 break;
436#endif
437 case 'h':
438 help(0);
439 ret = EXIT_SUCCESS;
440 goto cleanup;
441#if 0
442 case 'H':
443 tree_help();
444 ret = EXIT_SUCCESS;
445 goto cleanup;
446#endif
447 case 'i':
448 options_ctx |= LY_CTX_ALLIMPLEMENTED;
449 break;
450 case 'D':
451 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
452 fprintf(stderr, "yanglint error: -D specified too many times.\n");
453 goto cleanup;
454 } else if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
455 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
456 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
457 } else {
458 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
459 }
460 break;
461 case 'l':
462 list = 1;
463 break;
464#if 0
465 case 'm':
466 merge = 1;
467 break;
468#endif
469 case 'o':
470 if (out != stdout) {
471 fclose(out);
472 }
473 out = fopen(optarg, "w");
474 if (!out) {
475 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
476 goto cleanup;
477 }
478 break;
479 case 'p':
480 if (stat(optarg, &st) == -1) {
481 fprintf(stderr, "yanglint error: Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
482 goto cleanup;
483 }
484 if (!S_ISDIR(st.st_mode)) {
485 fprintf(stderr, "yanglint error: Provided search path is not a directory.\n");
486 goto cleanup;
487 }
488 if (!searchpaths) {
489 searchpaths = ly_set_new();
490 }
491 ly_set_add(searchpaths, optarg, 0);
492 break;
493#if 0
494 case 'r':
495 case 'O':
496 if (oper_file || (options_parser & LYD_OPT_NOEXTDEPS)) {
497 fprintf(stderr, "yanglint error: The operational datastore (-O) cannot be set multiple times.\n");
498 goto cleanup;
499 }
500 if (optarg[0] == '!') {
501 /* ignore extenral dependencies to the operational datastore */
502 options_parser |= LYD_OPT_NOEXTDEPS;
503 } else {
504 /* external file with the operational datastore */
505 oper_file = optarg;
506 }
507 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200508#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200509 case 's':
510 options_parser |= LYD_OPT_STRICT;
511 break;
512 case 't':
513 if (!strcmp(optarg, "auto")) {
514 options_parser = (options_parser & ~LYD_OPT_TYPEMASK);
515 autodetection = 1;
516 } else if (!strcmp(optarg, "config")) {
517 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
518 } else if (!strcmp(optarg, "get")) {
519 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
520 } else if (!strcmp(optarg, "getconfig")) {
521 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
522 } else if (!strcmp(optarg, "edit")) {
523 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
524 } else if (!strcmp(optarg, "data")) {
525 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
526 } else if (!strcmp(optarg, "rpc")) {
527 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
528 } else if (!strcmp(optarg, "rpcreply")) {
529 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
530 } else if (!strcmp(optarg, "notif")) {
531 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
532 } else {
533 fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg);
534 help(1);
535 goto cleanup;
536 }
537 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200538 case 'v':
539 version();
540 ret = EXIT_SUCCESS;
541 goto cleanup;
542 case 'V':
543 verbose++;
544 break;
545#ifndef NDEBUG
546 case 'G':
547 u = 0;
548 ptr = optarg;
549 while (ptr[0]) {
550 if (!strncmp(ptr, "dict", 4)) {
551 u |= LY_LDGDICT;
552 ptr += 4;
553 } else if (!strncmp(ptr, "yang", 4)) {
554 u |= LY_LDGYANG;
555 ptr += 4;
556 } else if (!strncmp(ptr, "yin", 3)) {
557 u |= LY_LDGYIN;
558 ptr += 3;
559 } else if (!strncmp(ptr, "xpath", 5)) {
560 u |= LY_LDGXPATH;
561 ptr += 5;
562 } else if (!strncmp(ptr, "diff", 4)) {
563 u |= LY_LDGDIFF;
564 ptr += 4;
565 }
566
567 if (ptr[0]) {
568 if (ptr[0] != ',') {
569 fprintf(stderr, "yanglint error: unknown debug group string \"%s\"\n", optarg);
570 goto cleanup;
571 }
572 ++ptr;
573 }
574 }
575 ly_verb_dbg(u);
576 break;
577#endif
578#if 0
579 case 'y':
580 ptr = strrchr(optarg, '.');
581 if (ptr) {
582 ptr++;
583 if (!strcmp(ptr, "xml")) {
584 ylformat = LYD_XML;
585 } else if (!strcmp(ptr, "json")) {
586 ylformat = LYD_JSON;
587 } else {
588 fprintf(stderr, "yanglint error: yang-library file in an unknown format \"%s\".\n", ptr);
589 goto cleanup;
590 }
591 } else {
592 fprintf(stderr, "yanglint error: yang-library file in an unknown format.\n");
593 goto cleanup;
594 }
595 ylpath = optarg;
596 break;
597#endif
598 default:
599 help(1);
600 if (optopt) {
601 fprintf(stderr, "yanglint error: invalid option: -%c\n", optopt);
602 } else {
603 fprintf(stderr, "yanglint error: invalid option: %s\n", argv[optind - 1]);
604 }
605 goto cleanup;
606 }
607 }
608
609 /* check options compatibility */
610 if (!list && optind >= argc) {
611 help(1);
612 fprintf(stderr, "yanglint error: missing <file> to process\n");
613 goto cleanup;
614 }
Radek Krejci693262f2019-04-29 15:23:20 +0200615 if (compiled) {
616 if (!outformat_s) {
617 fprintf(stderr, "yanglint warning: --compiled option takes effect only in case of printing schemas.\n");
618 } else {
619 outformat_s++;
620 }
621 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200622 if (outformat_s && outformat_s != LYS_OUT_TREE && (optind + 1) < argc) {
623 /* we have multiple schemas to be printed as YIN or YANG */
624 fprintf(stderr, "yanglint error: too many schemas to convert and store.\n");
625 goto cleanup;
626 }
627 if (outoptions_s || outtarget_s || outline_length_s) {
628#if 0
629 if (outformat_d || (outformat_s && outformat_s != LYS_OUT_TREE)) {
630 /* we have --tree-print-grouping with other output format than tree */
631 fprintf(stderr,
632 "yanglint warning: --tree options take effect only in case of the tree output format.\n");
633 }
634 }
635 if (merge) {
636 if (autodetection || (options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) {
637 fprintf(stderr, "yanglint warning: merging not allowed, ignoring option -m.\n");
638 merge = 0;
639 } else {
640 /* first, files will be parsed as trusted to allow missing data, then the data trees will be merged
641 * and the result will be validated */
642 options_parser |= LYD_OPT_TRUSTED;
643 }
644#endif
645 }
646#if 0
647 if (!outformat_d && options_dflt) {
648 /* we have options for printing default nodes, but data output not specified */
649 fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n");
650 }
651 if (outformat_s && (options_parser || autodetection)) {
652 /* we have options for printing data tree, but output is schema */
653 fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n");
654 }
655 if (oper_file && (!autodetection && !(options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) {
656 fprintf(stderr, "yanglint warning: operational datastore applies only to RPCs or Notifications.\n");
657 /* ignore operational datastore file */
658 oper_file = NULL;
659 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200660#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200661 if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) {
662 /* add option to ignore ietf-yang-library data for implicit data type */
663 options_parser |= LYD_OPT_DATA_NO_YANGLIB;
664 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200665
666 /* set callback for printing libyang messages */
667 ly_set_log_clb(libyang_verbclb, 1);
668#if 0
669 /* create libyang context */
670 if (ylpath) {
671 ctx = ly_ctx_new_ylpath(searchpaths ? (const char*)searchpaths->set.g[0] : NULL, ylpath, ylformat, options_ctx);
672 } else {
673#else
674 {
675#endif
676 ly_ctx_new(NULL, options_ctx, &ctx);
677 }
678 if (!ctx) {
679 goto cleanup;
680 }
681
682 /* set searchpaths */
683 if (searchpaths) {
684 for (u = 0; u < searchpaths->count; u++) {
685 ly_ctx_set_searchdir(ctx, (const char*)searchpaths->objs[u]);
686 }
687 index = u + 1;
688 }
689
690 /* derefered setting of verbosity in libyang after context initiation */
691 ly_verb(verbose);
692
693 mods = ly_set_new();
694
695
696 /* divide input files */
697 for (i = 0; i < argc - optind; i++) {
698 /* get the file format */
Radek Krejcie7b95092019-05-15 11:03:07 +0200699 if (!get_fileformat(argv[optind + i], &informat_s, &informat_d)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200700 goto cleanup;
701 }
702
703 if (informat_s) {
704 /* load/validate schema */
705 if (verbose >= 2) {
706 fprintf(stdout, "Validating %s schema file.\n", argv[optind + i]);
707 }
708 dir = strdup(argv[optind + i]);
709 ly_ctx_set_searchdir(ctx, ptr = dirname(dir));
710 mod = lys_parse_path(ctx, argv[optind + i], informat_s);
711 ly_ctx_unset_searchdir(ctx, index);
712 free(dir);
713 if (!mod) {
714 goto cleanup;
715 }
716 ly_set_add(mods, (void *)mod, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200717 } else {
718 if (autodetection && informat_d != LYD_XML) {
719 /* data file content autodetection is possible only for XML input */
720 fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n");
721 goto cleanup;
722 }
723
724 /* remember data filename and its format */
725 if (!data) {
726 data = data_item = malloc(sizeof *data);
727 } else {
728 for (data_item = data; data_item->next; data_item = data_item->next);
729 data_item->next = malloc(sizeof *data_item);
730 data_item = data_item->next;
731 }
732 data_item->filename = argv[optind + i];
733 data_item->format = informat_d;
734 data_item->type = options_parser & LYD_OPT_TYPEMASK;
735 data_item->tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200736 data_item->next = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200737 }
738 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200739 if (outformat_d && !data && !list) {
740 fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n");
741 goto cleanup;
742 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200743
744 /* enable specified features, if not specified, all the module's features are enabled */
745 u = 4; /* skip internal libyang modules */
746 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
747 for (i = 0; i < featsize; i++) {
748 if (!strcmp(feat[i], mod->name)) {
749 /* parse features spec */
750 featlist = strdup(feat[i] + strlen(feat[i]) + 1);
751 ptr = NULL;
752 while((ptr = strtok(ptr ? NULL : featlist, ","))) {
753 if (verbose >= 2) {
754 fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name);
755 }
756 if (lys_feature_enable(mod, ptr)) {
757 fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name);
758 }
759 }
760 free(featlist);
761 break;
762 }
763 }
764 if (i == featsize) {
765 if (verbose >= 2) {
766 fprintf(stdout, "Enabling all features in module %s.\n", mod->name);
767 }
768 lys_feature_enable(mod, "*");
769 }
770 }
771
772 /* convert (print) to FORMAT */
773 if (outformat_s) {
774 if (outformat_s == LYS_OUT_JSON && mods->count > 1) {
775 fputs("[", out);
776 }
777 for (u = 0; u < mods->count; u++) {
778 if (u) {
779 if (outformat_s == LYS_OUT_JSON) {
780 fputs(",\n", out);
781 } else {
782 fputs("\n", out);
783 }
784 }
785 lys_print_file(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s);
786 }
787 if (outformat_s == LYS_OUT_JSON) {
788 if (mods->count > 1) {
789 fputs("]\n", out);
790 } else if (mods->count == 1) {
791 fputs("\n", out);
792 }
793 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200794 } else if (data) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200795
796 /* prepare operational datastore when specified for RPC/Notification */
797 if (oper_file) {
798 /* get the file format */
799 if (!get_fileformat(oper_file, NULL, &informat_d)) {
800 goto cleanup;
801 } else if (!informat_d) {
802 fprintf(stderr, "yanglint error: The operational data are expected in XML or JSON format.\n");
803 goto cleanup;
804 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200805 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 +0200806 if (!oper) {
807 fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n");
808 goto cleanup;
809 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200810 trees = lyd_trees_new(1, oper);
Radek Krejcied5acc52019-04-25 15:57:04 +0200811 }
812
813 for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) {
814 /* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */
Radek Krejcie7b95092019-05-15 11:03:07 +0200815#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200816 if (autodetection) {
817 /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */
818 options_parser &= ~LYD_OPT_DATA_NO_YANGLIB;
819 /* automatically detect data type from the data top level */
820 data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0);
821 if (!data_item->xml) {
822 fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n");
823 goto cleanup;
824 }
825
826 /* NOTE: namespace is ignored to simplify usage of this feature */
827 if (!strcmp(data_item->xml->name, "data")) {
828 if (verbose >= 2) {
829 fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename);
830 }
831 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
832 data_item->type = LYD_OPT_DATA;
833 } else if (!strcmp(data_item->xml->name, "config")) {
834 if (verbose >= 2) {
835 fprintf(stdout, "Parsing %s as config data.\n", data_item->filename);
836 }
837 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
838 data_item->type = LYD_OPT_CONFIG;
839 } else if (!strcmp(data_item->xml->name, "get-reply")) {
840 if (verbose >= 2) {
841 fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename);
842 }
843 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
844 data_item->type = LYD_OPT_GET;
845 } else if (!strcmp(data_item->xml->name, "get-config-reply")) {
846 if (verbose >= 2) {
847 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename);
848 }
849 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
850 data_item->type = LYD_OPT_GETCONFIG;
851 } else if (!strcmp(data_item->xml->name, "edit-config")) {
852 if (verbose >= 2) {
853 fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename);
854 }
855 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
856 data_item->type = LYD_OPT_EDIT;
857 } else if (!strcmp(data_item->xml->name, "rpc")) {
858 if (verbose >= 2) {
859 fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename);
860 }
861 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
862 data_item->type = LYD_OPT_RPC;
863 } else if (!strcmp(data_item->xml->name, "rpc-reply")) {
864 if (verbose >= 2) {
865 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename);
866 }
867
868 data_item->type = LYD_OPT_RPCREPLY;
869 if (!data_item->next || (data_prev && !data_prev->tree)) {
870 fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
871 goto cleanup;
872 }
873
874 continue;
875 } else if (!strcmp(data_item->xml->name, "notification")) {
876 if (verbose >= 2) {
877 fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename);
878 }
879 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
880 data_item->type = LYD_OPT_NOTIF;
881
882 /* ignore eventTime element if present */
883 while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) {
884 lyxml_free(ctx, data_item->xml->child);
885 }
886 } else {
887 fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n",
888 data_item->xml->name);
889 goto cleanup;
890 }
891
892 data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper);
893 if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) {
894parse_reply:
895 /* check result of the RPC parsing, we are going to do another parsing in this step */
896 if (ly_errno) {
897 goto cleanup;
898 }
899
900 /* check that we really have RPC for the reply */
901 if (data_item->type != LYD_OPT_RPC) {
902 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename);
903 goto cleanup;
904 }
905
906 if (data_prev->format == LYD_XML) {
907 /* ignore <ok> and <rpc-error> elements if present */
908 u = 0;
909 LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) {
910 if (!strcmp(data_prev->xml->child->name, "ok")) {
911 if (u) {
912 /* rpc-error or ok already present */
913 u = 0x8; /* error flag */
914 } else {
915 u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */
916 }
917 } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) {
918 if (u && (u & 0x1)) {
919 /* ok already present, rpc-error can be present multiple times */
920 u = 0x8; /* error flag */
921 } else {
922 u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */
923 }
924 }
925
926 if (u == 0x8) {
927 fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename);
928 goto cleanup;
929 } else if (u & 0x4) {
930 lyxml_free(ctx, data_prev->xml->child);
931 u &= ~0x4; /* unset lyxml_free() flag */
932 }
933 }
934
935 /* finally, parse RPC reply from the previous step */
936 data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child,
937 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
938 } else { /* LYD_JSON */
939 data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format,
940 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
941 }
942 }
943 } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) {
944 if (data_prev && !data_prev->tree) {
945 /* now we should have RPC for the preceding RPC reply */
946 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format,
947 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper);
948 data_item->type = LYD_OPT_RPC;
949 goto parse_reply;
950 } else {
951 /* now we have RPC reply which will be parsed in next step together with its RPC */
952 if (!data_item->next) {
953 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
954 goto cleanup;
955 }
956 if (data_item->format == LYD_XML) {
957 /* create rpc-reply container to unify handling with autodetection */
958 data_item->xml = calloc(1, sizeof *data_item->xml);
959 if (!data_item->xml) {
960 fprintf(stderr, "yanglint error: Memory allocation failed failed.\n");
961 goto cleanup;
962 }
963 data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9);
964 data_item->xml->prev = data_item->xml;
965 data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT);
966 if (data_item->xml->child) {
967 data_item->xml->child->parent = data_item->xml;
968 }
969 }
970 continue;
971 }
972 } else {
Radek Krejcie7b95092019-05-15 11:03:07 +0200973#else
974 {
975#endif
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200976 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, options_parser, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200977 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200978 if (ly_err_first(ctx)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200979 goto cleanup;
980 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200981#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200982 if (merge && data != data_item) {
983 if (!data->tree) {
984 data->tree = data_item->tree;
985 } else if (data_item->tree) {
986 /* merge results */
987 if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
988 fprintf(stderr, "yanglint error: merging multiple data trees failed.\n");
989 goto cleanup;
990 }
991 }
992 data_item->tree = NULL;
993 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200994#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200995 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200996#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200997 if (merge) {
998 /* validate the merged data tree, do not trust the input, invalidate all the data first */
999 LY_TREE_FOR(data->tree, subroot) {
1000 LY_TREE_DFS_BEGIN(subroot, next, node) {
1001 node->validity = LYD_VAL_OK;
1002 switch (node->schema->nodetype) {
1003 case LYS_LEAFLIST:
1004 case LYS_LEAF:
1005 if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) {
1006 node->validity |= LYD_VAL_LEAFREF;
1007 }
1008 break;
1009 case LYS_LIST:
1010 node->validity |= LYD_VAL_UNIQUE;
1011 /* falls through */
1012 case LYS_CONTAINER:
1013 case LYS_NOTIF:
1014 case LYS_RPC:
1015 case LYS_ACTION:
1016 node->validity |= LYD_VAL_MAND;
1017 break;
1018 default:
1019 break;
1020 }
1021 LY_TREE_DFS_END(subroot, next, node)
1022 }
1023 }
1024 if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) {
1025 goto cleanup;
1026 }
1027 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001028#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001029 /* print only if data output format specified */
1030 if (outformat_d) {
1031 for (data_item = data; data_item; data_item = data_item->next) {
1032 if (!merge && verbose >= 2) {
1033 fprintf(stdout, "File %s:\n", data_item->filename);
1034 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001035#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001036 if (outformat_d == LYD_XML && envelope) {
1037 switch (data_item->type) {
1038 case LYD_OPT_DATA:
1039 envelope_s = "data";
1040 break;
1041 case LYD_OPT_CONFIG:
1042 envelope_s = "config";
1043 break;
1044 case LYD_OPT_GET:
1045 envelope_s = "get-reply";
1046 break;
1047 case LYD_OPT_GETCONFIG:
1048 envelope_s = "get-config-reply";
1049 break;
1050 case LYD_OPT_EDIT:
1051 envelope_s = "edit-config";
1052 break;
1053 case LYD_OPT_RPC:
1054 envelope_s = "rpc";
1055 break;
1056 case LYD_OPT_RPCREPLY:
1057 envelope_s = "rpc-reply";
1058 break;
1059 case LYD_OPT_NOTIF:
1060 envelope_s = "notification";
1061 break;
1062 }
1063 fprintf(out, "<%s>\n", envelope_s);
1064 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1065 /* action */
1066 fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n");
1067 }
1068 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001069#endif
1070 lyd_print_file(out, (data_item->type == LYD_OPT_RPCREPLY) ? lyd_node_children(data_item->tree) : data_item->tree,
1071 outformat_d, LYDP_WITHSIBLINGS | LYDP_FORMAT /* TODO defaults | options_dflt */);
1072#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001073 if (envelope_s) {
1074 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1075 fprintf(out, "</action>\n");
1076 }
1077 fprintf(out, "</%s>\n", envelope_s);
1078 }
1079 if (merge) {
1080 /* stop after first item */
1081 break;
1082 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001083#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001084 }
1085 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001086 }
1087#if 0
1088 if (list) {
1089 print_list(out, ctx, outformat_d);
1090 }
1091#endif
1092
1093 ret = EXIT_SUCCESS;
1094
1095cleanup:
1096 if (out && out != stdout) {
1097 fclose(out);
1098 }
1099 ly_set_free(mods, NULL);
1100 ly_set_free(searchpaths, NULL);
1101 for (i = 0; i < featsize; i++) {
1102 free(feat[i]);
1103 }
1104 free(feat);
Radek Krejcied5acc52019-04-25 15:57:04 +02001105 for (; data; data = data_item) {
1106 data_item = data->next;
Radek Krejcie7b95092019-05-15 11:03:07 +02001107 lyd_free_all(data->tree);
Radek Krejcied5acc52019-04-25 15:57:04 +02001108 free(data);
1109 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +02001110 lyd_trees_free(trees, 1);
Radek Krejcied5acc52019-04-25 15:57:04 +02001111 ly_ctx_destroy(ctx, NULL);
1112
1113 return ret;
1114}