blob: 60ecfe1a69bcc8d3cb353ba4a8dd5864e588004a [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;
Michal Vaskoa3881362020-01-21 15:57:35 +0100329 int flags;
Radek Krejcied5acc52019-04-25 15:57:04 +0200330 } *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;
FredGand944bdc2019-11-05 21:57:07 +0800378#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200379 } else if (!strcasecmp(optarg, "yin")) {
380 outformat_s = LYS_OUT_YIN;
381 outformat_d = 0;
FredGand944bdc2019-11-05 21:57:07 +0800382#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200383 } 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")) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200516 autodetection = 1;
517 } else if (!strcmp(optarg, "config")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100518 options_parser |= LYD_OPT_CONFIG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200519 } else if (!strcmp(optarg, "get")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100520 options_parser |= LYD_OPT_GET;
Radek Krejcied5acc52019-04-25 15:57:04 +0200521 } else if (!strcmp(optarg, "getconfig")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100522 options_parser |= LYD_OPT_GETCONFIG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200523 } else if (!strcmp(optarg, "edit")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100524 options_parser |= LYD_OPT_EDIT;
Radek Krejcied5acc52019-04-25 15:57:04 +0200525 } else if (!strcmp(optarg, "data")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100526 /* no options */
Radek Krejcied5acc52019-04-25 15:57:04 +0200527 } else {
528 fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg);
529 help(1);
530 goto cleanup;
531 }
532 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200533 case 'v':
534 version();
535 ret = EXIT_SUCCESS;
536 goto cleanup;
537 case 'V':
538 verbose++;
539 break;
540#ifndef NDEBUG
541 case 'G':
542 u = 0;
543 ptr = optarg;
544 while (ptr[0]) {
545 if (!strncmp(ptr, "dict", 4)) {
546 u |= LY_LDGDICT;
547 ptr += 4;
548 } else if (!strncmp(ptr, "yang", 4)) {
549 u |= LY_LDGYANG;
550 ptr += 4;
551 } else if (!strncmp(ptr, "yin", 3)) {
552 u |= LY_LDGYIN;
553 ptr += 3;
554 } else if (!strncmp(ptr, "xpath", 5)) {
555 u |= LY_LDGXPATH;
556 ptr += 5;
557 } else if (!strncmp(ptr, "diff", 4)) {
558 u |= LY_LDGDIFF;
559 ptr += 4;
560 }
561
562 if (ptr[0]) {
563 if (ptr[0] != ',') {
564 fprintf(stderr, "yanglint error: unknown debug group string \"%s\"\n", optarg);
565 goto cleanup;
566 }
567 ++ptr;
568 }
569 }
570 ly_verb_dbg(u);
571 break;
572#endif
573#if 0
574 case 'y':
575 ptr = strrchr(optarg, '.');
576 if (ptr) {
577 ptr++;
578 if (!strcmp(ptr, "xml")) {
579 ylformat = LYD_XML;
580 } else if (!strcmp(ptr, "json")) {
581 ylformat = LYD_JSON;
582 } else {
583 fprintf(stderr, "yanglint error: yang-library file in an unknown format \"%s\".\n", ptr);
584 goto cleanup;
585 }
586 } else {
587 fprintf(stderr, "yanglint error: yang-library file in an unknown format.\n");
588 goto cleanup;
589 }
590 ylpath = optarg;
591 break;
592#endif
593 default:
594 help(1);
595 if (optopt) {
596 fprintf(stderr, "yanglint error: invalid option: -%c\n", optopt);
597 } else {
598 fprintf(stderr, "yanglint error: invalid option: %s\n", argv[optind - 1]);
599 }
600 goto cleanup;
601 }
602 }
603
604 /* check options compatibility */
605 if (!list && optind >= argc) {
606 help(1);
607 fprintf(stderr, "yanglint error: missing <file> to process\n");
608 goto cleanup;
609 }
Radek Krejci693262f2019-04-29 15:23:20 +0200610 if (compiled) {
FredGand944bdc2019-11-05 21:57:07 +0800611 if (outformat_s != LYS_OUT_YANG) {
612 fprintf(stderr, "yanglint warning: --compiled option takes effect only in case of printing schemas in YANG format.\n");
Radek Krejci693262f2019-04-29 15:23:20 +0200613 } else {
FredGand944bdc2019-11-05 21:57:07 +0800614 outformat_s = LYS_OUT_YANG_COMPILED;
Radek Krejci693262f2019-04-29 15:23:20 +0200615 }
616 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200617 if (outformat_s && outformat_s != LYS_OUT_TREE && (optind + 1) < argc) {
618 /* we have multiple schemas to be printed as YIN or YANG */
619 fprintf(stderr, "yanglint error: too many schemas to convert and store.\n");
620 goto cleanup;
621 }
622 if (outoptions_s || outtarget_s || outline_length_s) {
623#if 0
624 if (outformat_d || (outformat_s && outformat_s != LYS_OUT_TREE)) {
625 /* we have --tree-print-grouping with other output format than tree */
626 fprintf(stderr,
627 "yanglint warning: --tree options take effect only in case of the tree output format.\n");
628 }
629 }
630 if (merge) {
631 if (autodetection || (options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) {
632 fprintf(stderr, "yanglint warning: merging not allowed, ignoring option -m.\n");
633 merge = 0;
634 } else {
635 /* first, files will be parsed as trusted to allow missing data, then the data trees will be merged
636 * and the result will be validated */
637 options_parser |= LYD_OPT_TRUSTED;
638 }
639#endif
640 }
641#if 0
642 if (!outformat_d && options_dflt) {
643 /* we have options for printing default nodes, but data output not specified */
644 fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n");
645 }
646 if (outformat_s && (options_parser || autodetection)) {
647 /* we have options for printing data tree, but output is schema */
648 fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n");
649 }
650 if (oper_file && (!autodetection && !(options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) {
651 fprintf(stderr, "yanglint warning: operational datastore applies only to RPCs or Notifications.\n");
652 /* ignore operational datastore file */
653 oper_file = NULL;
654 }
655 if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) {
656 /* add option to ignore ietf-yang-library data for implicit data type */
657 options_parser |= LYD_OPT_DATA_NO_YANGLIB;
658 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100659#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200660
661 /* set callback for printing libyang messages */
662 ly_set_log_clb(libyang_verbclb, 1);
663#if 0
664 /* create libyang context */
665 if (ylpath) {
666 ctx = ly_ctx_new_ylpath(searchpaths ? (const char*)searchpaths->set.g[0] : NULL, ylpath, ylformat, options_ctx);
667 } else {
668#else
669 {
670#endif
671 ly_ctx_new(NULL, options_ctx, &ctx);
672 }
673 if (!ctx) {
674 goto cleanup;
675 }
676
677 /* set searchpaths */
678 if (searchpaths) {
679 for (u = 0; u < searchpaths->count; u++) {
680 ly_ctx_set_searchdir(ctx, (const char*)searchpaths->objs[u]);
681 }
682 index = u + 1;
683 }
684
685 /* derefered setting of verbosity in libyang after context initiation */
686 ly_verb(verbose);
687
688 mods = ly_set_new();
689
690
691 /* divide input files */
692 for (i = 0; i < argc - optind; i++) {
693 /* get the file format */
Radek Krejcie7b95092019-05-15 11:03:07 +0200694 if (!get_fileformat(argv[optind + i], &informat_s, &informat_d)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200695 goto cleanup;
696 }
697
698 if (informat_s) {
699 /* load/validate schema */
700 if (verbose >= 2) {
701 fprintf(stdout, "Validating %s schema file.\n", argv[optind + i]);
702 }
703 dir = strdup(argv[optind + i]);
704 ly_ctx_set_searchdir(ctx, ptr = dirname(dir));
705 mod = lys_parse_path(ctx, argv[optind + i], informat_s);
706 ly_ctx_unset_searchdir(ctx, index);
707 free(dir);
708 if (!mod) {
709 goto cleanup;
710 }
711 ly_set_add(mods, (void *)mod, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200712 } else {
713 if (autodetection && informat_d != LYD_XML) {
714 /* data file content autodetection is possible only for XML input */
715 fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n");
716 goto cleanup;
717 }
718
719 /* remember data filename and its format */
720 if (!data) {
721 data = data_item = malloc(sizeof *data);
722 } else {
723 for (data_item = data; data_item->next; data_item = data_item->next);
724 data_item->next = malloc(sizeof *data_item);
725 data_item = data_item->next;
726 }
727 data_item->filename = argv[optind + i];
728 data_item->format = informat_d;
Michal Vaskoa3881362020-01-21 15:57:35 +0100729 data_item->flags = options_parser;
Radek Krejcied5acc52019-04-25 15:57:04 +0200730 data_item->tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200731 data_item->next = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200732 }
733 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200734 if (outformat_d && !data && !list) {
735 fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n");
736 goto cleanup;
737 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200738
739 /* enable specified features, if not specified, all the module's features are enabled */
740 u = 4; /* skip internal libyang modules */
741 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
742 for (i = 0; i < featsize; i++) {
743 if (!strcmp(feat[i], mod->name)) {
744 /* parse features spec */
745 featlist = strdup(feat[i] + strlen(feat[i]) + 1);
746 ptr = NULL;
747 while((ptr = strtok(ptr ? NULL : featlist, ","))) {
748 if (verbose >= 2) {
749 fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name);
750 }
751 if (lys_feature_enable(mod, ptr)) {
752 fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name);
753 }
754 }
755 free(featlist);
756 break;
757 }
758 }
759 if (i == featsize) {
760 if (verbose >= 2) {
761 fprintf(stdout, "Enabling all features in module %s.\n", mod->name);
762 }
763 lys_feature_enable(mod, "*");
764 }
765 }
766
767 /* convert (print) to FORMAT */
768 if (outformat_s) {
769 if (outformat_s == LYS_OUT_JSON && mods->count > 1) {
770 fputs("[", out);
771 }
772 for (u = 0; u < mods->count; u++) {
773 if (u) {
774 if (outformat_s == LYS_OUT_JSON) {
775 fputs(",\n", out);
776 } else {
777 fputs("\n", out);
778 }
779 }
780 lys_print_file(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s);
781 }
782 if (outformat_s == LYS_OUT_JSON) {
783 if (mods->count > 1) {
784 fputs("]\n", out);
785 } else if (mods->count == 1) {
786 fputs("\n", out);
787 }
788 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200789 } else if (data) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200790
791 /* prepare operational datastore when specified for RPC/Notification */
792 if (oper_file) {
793 /* get the file format */
794 if (!get_fileformat(oper_file, NULL, &informat_d)) {
795 goto cleanup;
796 } else if (!informat_d) {
797 fprintf(stderr, "yanglint error: The operational data are expected in XML or JSON format.\n");
798 goto cleanup;
799 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100800 oper = lyd_parse_path(ctx, oper_file, informat_d, LYD_OPT_PARSE_ONLY);
Radek Krejcied5acc52019-04-25 15:57:04 +0200801 if (!oper) {
802 fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n");
803 goto cleanup;
804 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200805 trees = lyd_trees_new(1, oper);
Radek Krejcied5acc52019-04-25 15:57:04 +0200806 }
807
808 for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) {
809 /* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */
Radek Krejcie7b95092019-05-15 11:03:07 +0200810#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200811 if (autodetection) {
812 /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */
813 options_parser &= ~LYD_OPT_DATA_NO_YANGLIB;
814 /* automatically detect data type from the data top level */
815 data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0);
816 if (!data_item->xml) {
817 fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n");
818 goto cleanup;
819 }
820
821 /* NOTE: namespace is ignored to simplify usage of this feature */
822 if (!strcmp(data_item->xml->name, "data")) {
823 if (verbose >= 2) {
824 fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename);
825 }
826 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
827 data_item->type = LYD_OPT_DATA;
828 } else if (!strcmp(data_item->xml->name, "config")) {
829 if (verbose >= 2) {
830 fprintf(stdout, "Parsing %s as config data.\n", data_item->filename);
831 }
832 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
833 data_item->type = LYD_OPT_CONFIG;
834 } else if (!strcmp(data_item->xml->name, "get-reply")) {
835 if (verbose >= 2) {
836 fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename);
837 }
838 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
839 data_item->type = LYD_OPT_GET;
840 } else if (!strcmp(data_item->xml->name, "get-config-reply")) {
841 if (verbose >= 2) {
842 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename);
843 }
844 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
845 data_item->type = LYD_OPT_GETCONFIG;
846 } else if (!strcmp(data_item->xml->name, "edit-config")) {
847 if (verbose >= 2) {
848 fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename);
849 }
850 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
851 data_item->type = LYD_OPT_EDIT;
852 } else if (!strcmp(data_item->xml->name, "rpc")) {
853 if (verbose >= 2) {
854 fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename);
855 }
856 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
857 data_item->type = LYD_OPT_RPC;
858 } else if (!strcmp(data_item->xml->name, "rpc-reply")) {
859 if (verbose >= 2) {
860 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename);
861 }
862
863 data_item->type = LYD_OPT_RPCREPLY;
864 if (!data_item->next || (data_prev && !data_prev->tree)) {
865 fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
866 goto cleanup;
867 }
868
869 continue;
870 } else if (!strcmp(data_item->xml->name, "notification")) {
871 if (verbose >= 2) {
872 fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename);
873 }
874 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
875 data_item->type = LYD_OPT_NOTIF;
876
877 /* ignore eventTime element if present */
878 while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) {
879 lyxml_free(ctx, data_item->xml->child);
880 }
881 } else {
882 fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n",
883 data_item->xml->name);
884 goto cleanup;
885 }
886
887 data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper);
888 if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) {
889parse_reply:
890 /* check result of the RPC parsing, we are going to do another parsing in this step */
891 if (ly_errno) {
892 goto cleanup;
893 }
894
895 /* check that we really have RPC for the reply */
896 if (data_item->type != LYD_OPT_RPC) {
897 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename);
898 goto cleanup;
899 }
900
901 if (data_prev->format == LYD_XML) {
902 /* ignore <ok> and <rpc-error> elements if present */
903 u = 0;
904 LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) {
905 if (!strcmp(data_prev->xml->child->name, "ok")) {
906 if (u) {
907 /* rpc-error or ok already present */
908 u = 0x8; /* error flag */
909 } else {
910 u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */
911 }
912 } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) {
913 if (u && (u & 0x1)) {
914 /* ok already present, rpc-error can be present multiple times */
915 u = 0x8; /* error flag */
916 } else {
917 u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */
918 }
919 }
920
921 if (u == 0x8) {
922 fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename);
923 goto cleanup;
924 } else if (u & 0x4) {
925 lyxml_free(ctx, data_prev->xml->child);
926 u &= ~0x4; /* unset lyxml_free() flag */
927 }
928 }
929
930 /* finally, parse RPC reply from the previous step */
931 data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child,
932 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
933 } else { /* LYD_JSON */
934 data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format,
935 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
936 }
937 }
938 } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) {
939 if (data_prev && !data_prev->tree) {
940 /* now we should have RPC for the preceding RPC reply */
941 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format,
942 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper);
943 data_item->type = LYD_OPT_RPC;
944 goto parse_reply;
945 } else {
946 /* now we have RPC reply which will be parsed in next step together with its RPC */
947 if (!data_item->next) {
948 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
949 goto cleanup;
950 }
951 if (data_item->format == LYD_XML) {
952 /* create rpc-reply container to unify handling with autodetection */
953 data_item->xml = calloc(1, sizeof *data_item->xml);
954 if (!data_item->xml) {
955 fprintf(stderr, "yanglint error: Memory allocation failed failed.\n");
956 goto cleanup;
957 }
958 data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9);
959 data_item->xml->prev = data_item->xml;
960 data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT);
961 if (data_item->xml->child) {
962 data_item->xml->child->parent = data_item->xml;
963 }
964 }
965 continue;
966 }
967 } else {
Radek Krejcie7b95092019-05-15 11:03:07 +0200968#else
969 {
970#endif
Michal Vaskoa3881362020-01-21 15:57:35 +0100971 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, options_parser);
Radek Krejcied5acc52019-04-25 15:57:04 +0200972 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200973 if (ly_err_first(ctx)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200974 goto cleanup;
975 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200976#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200977 if (merge && data != data_item) {
978 if (!data->tree) {
979 data->tree = data_item->tree;
980 } else if (data_item->tree) {
981 /* merge results */
982 if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
983 fprintf(stderr, "yanglint error: merging multiple data trees failed.\n");
984 goto cleanup;
985 }
986 }
987 data_item->tree = NULL;
988 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200989#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200990 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200991#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200992 if (merge) {
993 /* validate the merged data tree, do not trust the input, invalidate all the data first */
994 LY_TREE_FOR(data->tree, subroot) {
995 LY_TREE_DFS_BEGIN(subroot, next, node) {
996 node->validity = LYD_VAL_OK;
997 switch (node->schema->nodetype) {
998 case LYS_LEAFLIST:
999 case LYS_LEAF:
1000 if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) {
1001 node->validity |= LYD_VAL_LEAFREF;
1002 }
1003 break;
1004 case LYS_LIST:
1005 node->validity |= LYD_VAL_UNIQUE;
1006 /* falls through */
1007 case LYS_CONTAINER:
1008 case LYS_NOTIF:
1009 case LYS_RPC:
1010 case LYS_ACTION:
1011 node->validity |= LYD_VAL_MAND;
1012 break;
1013 default:
1014 break;
1015 }
1016 LY_TREE_DFS_END(subroot, next, node)
1017 }
1018 }
1019 if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) {
1020 goto cleanup;
1021 }
1022 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001023#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001024 /* print only if data output format specified */
1025 if (outformat_d) {
1026 for (data_item = data; data_item; data_item = data_item->next) {
1027 if (!merge && verbose >= 2) {
1028 fprintf(stdout, "File %s:\n", data_item->filename);
1029 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001030#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001031 if (outformat_d == LYD_XML && envelope) {
1032 switch (data_item->type) {
1033 case LYD_OPT_DATA:
1034 envelope_s = "data";
1035 break;
1036 case LYD_OPT_CONFIG:
1037 envelope_s = "config";
1038 break;
1039 case LYD_OPT_GET:
1040 envelope_s = "get-reply";
1041 break;
1042 case LYD_OPT_GETCONFIG:
1043 envelope_s = "get-config-reply";
1044 break;
1045 case LYD_OPT_EDIT:
1046 envelope_s = "edit-config";
1047 break;
1048 case LYD_OPT_RPC:
1049 envelope_s = "rpc";
1050 break;
1051 case LYD_OPT_RPCREPLY:
1052 envelope_s = "rpc-reply";
1053 break;
1054 case LYD_OPT_NOTIF:
1055 envelope_s = "notification";
1056 break;
1057 }
1058 fprintf(out, "<%s>\n", envelope_s);
1059 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1060 /* action */
1061 fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n");
1062 }
1063 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001064#endif
Michal Vaskoa3881362020-01-21 15:57:35 +01001065 lyd_print_file(out, data_item->tree, outformat_d, LYDP_WITHSIBLINGS | LYDP_FORMAT /* TODO defaults | options_dflt */);
Radek Krejcie7b95092019-05-15 11:03:07 +02001066#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001067 if (envelope_s) {
1068 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1069 fprintf(out, "</action>\n");
1070 }
1071 fprintf(out, "</%s>\n", envelope_s);
1072 }
1073 if (merge) {
1074 /* stop after first item */
1075 break;
1076 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001077#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001078 }
1079 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001080 }
1081#if 0
1082 if (list) {
1083 print_list(out, ctx, outformat_d);
1084 }
1085#endif
1086
1087 ret = EXIT_SUCCESS;
1088
1089cleanup:
1090 if (out && out != stdout) {
1091 fclose(out);
1092 }
1093 ly_set_free(mods, NULL);
1094 ly_set_free(searchpaths, NULL);
1095 for (i = 0; i < featsize; i++) {
1096 free(feat[i]);
1097 }
1098 free(feat);
Radek Krejcied5acc52019-04-25 15:57:04 +02001099 for (; data; data = data_item) {
1100 data_item = data->next;
Radek Krejcie7b95092019-05-15 11:03:07 +02001101 lyd_free_all(data->tree);
Radek Krejcied5acc52019-04-25 15:57:04 +02001102 free(data);
1103 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +02001104 lyd_trees_free(trees, 1);
Radek Krejcied5acc52019-04-25 15:57:04 +02001105 ly_ctx_destroy(ctx, NULL);
1106
1107 return ret;
1108}